text: import { CID } from 'multiformats';
  import { base64 } from 'multiformats/bases/base64';
  import {
    Token,
    Type
  } from 'cborg';
  import * as cborgJson from 'cborg/json';
  function cidEncoder(obj) {
    if (obj.asCID !== obj) {
      return null;
    }
    const cid = CID.asCID(obj);
    if (!cid) {
      return null;
    }
    const cidString = cid.toString();
    return [
      new Token(Type.map, Infinity, 1),
      new Token(Type.string, '/', 1),
      new Token(Type.string, cidString, cidString.length),
      new Token(Type.break, undefined, 1)
    ];
  }
  function bytesEncoder(bytes) {
    const bytesString = base64.encode(bytes).slice(1);
    return [
      new Token(Type.map, Infinity, 1),
      new Token(Type.string, '/', 1),
      new Token(Type.map, Infinity, 1),
      new Token(Type.string, 'bytes', 5),
      new Token(Type.string, bytesString, bytesString.length),
      new Token(Type.break, undefined, 1),
      new Token(Type.break, undefined, 1)
    ];
  }
  function undefinedEncoder() {
    throw new Error('`undefined` is not supported by the IPLD Data Model and cannot be encoded');
  }
  function numberEncoder(num) {
    if (Number.isNaN(num)) {
      throw new Error('`NaN` is not supported by the IPLD Data Model and cannot be encoded');
    }
    if (num === Infinity || num === -Infinity) {
      throw new Error('`Infinity` and `-Infinity` is not supported by the IPLD Data Model and cannot be encoded');
    }
    return null;
  }
  const encodeOptions = {
    typeEncoders: {
      Object: cidEncoder,
      Uint8Array: bytesEncoder,
      Buffer: bytesEncoder,
      undefined: undefinedEncoder,
      number: numberEncoder
    }
  };
  class DagJsonTokenizer extends cborgJson.Tokenizer {
    constructor(data, options) {
      super(data, options);
      this.tokenBuffer = [];
    }
    done() {
      return this.tokenBuffer.length === 0 && super.done();
    }
    _next() {
      if (this.tokenBuffer.length > 0) {
        return this.tokenBuffer.pop();
      }
      return super.next();
    }
    next() {
      const token = this._next();
      if (token.type === Type.map) {
        const keyToken = this._next();
        if (keyToken.type === Type.string && keyToken.value === '/') {
          const valueToken = this._next();
          if (valueToken.type === Type.string) {
            const breakToken = this._next();
            if (breakToken.type !== Type.break) {
              throw new Error('Invalid encoded CID form');
            }
            this.tokenBuffer.push(valueToken);
            return new Token(Type.tag, 42, 0);
          }
          if (valueToken.type === Type.map) {
            const innerKeyToken = this._next();
            if (innerKeyToken.type === Type.string && innerKeyToken.value === 'bytes') {
              const innerValueToken = this._next();
              if (innerValueToken.type === Type.string) {
                for (let i = 0; i < 2; i++) {
                  const breakToken = this._next();
                  if (breakToken.type !== Type.break) {
                    throw new Error('Invalid encoded Bytes form');
                  }
                }
                const bytes = base64.decode(`m${ innerValueToken.value }`);
                return new Token(Type.bytes, bytes, innerValueToken.value.length);
              }
              this.tokenBuffer.push(innerValueToken);
            }
            this.tokenBuffer.push(innerKeyToken);
          }
          this.tokenBuffer.push(valueToken);
        }
        this.tokenBuffer.push(keyToken);
      }
      return token;
    }
  }
  const decodeOptions = {
    allowIndefinite: false,
    allowUndefined: false,
    allowNaN: false,
    allowInfinity: false,
    allowBigInt: true,
    strict: true,
    useMaps: false,
    tags: []
  };
  decodeOptions.tags[42] = CID.parse;
  export const name = 'dag-json';
  export const code = 297;
  export const encode = node => cborgJson.encode(node, encodeOptions);
  export const decode = data => {
    const options = Object.assign(decodeOptions, { tokenizer: new DagJsonTokenizer(data, decodeOptions) });
    return cborgJson.decode(data, options);
  };