All files / json-pack/src/json JsonDecoderDag.ts

72.61% Statements 61/84
62.06% Branches 18/29
75% Functions 3/4
71.95% Lines 59/82

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 1341x 1x   1x   1x   1x   10x 10x 6x 6x 3x       10x 10x 10x         10x 10x   3x 3x   7x 7x         7x 7x   3x 3x   4x 4x                         4x 4x         4x 4x         4x 4x 4x 4x 4x         4x 4x         4x 4x       6x 6x 6x         6x 6x   3x 3x   3x 3x         3x 3x         3x 3x 3x 3x 3x         3x 3x 3x 3x 3x              
import {JsonDecoder} from './JsonDecoder';
import {findEndingQuote} from './util';
import type {PackValue} from '../types';
import {createFromBase64Bin} from '@jsonjoy.com/base64/lib/createFromBase64Bin';
 
export const fromBase64Bin = createFromBase64Bin(undefined, '');
 
export class JsonDecoderDag extends JsonDecoder {
  public readObj(): PackValue | Record<string, PackValue> | Uint8Array | unknown {
    const bytes = this.tryReadBytes();
    if (bytes) return bytes;
    const cid = this.tryReadCid();
    if (cid) return cid;
    return super.readObj();
  }
 
  protected tryReadBytes(): Uint8Array | undefined {
    const reader = this.reader;
    const x = reader.x;
    Iif (reader.u8() !== 0x7b) {
      // {
      reader.x = x;
      return;
    }
    this.skipWhitespace();
    if (reader.u8() !== 0x22 || reader.u8() !== 0x2f || reader.u8() !== 0x22) {
      // "/"
      reader.x = x;
      return;
    }
    this.skipWhitespace();
    Iif (reader.u8() !== 0x3a) {
      // :
      reader.x = x;
      return;
    }
    this.skipWhitespace();
    if (reader.u8() !== 0x7b) {
      // {
      reader.x = x;
      return;
    }
    this.skipWhitespace();
    Iif (
      reader.u8() !== 0x22 ||
      reader.u8() !== 0x62 ||
      reader.u8() !== 0x79 ||
      reader.u8() !== 0x74 ||
      reader.u8() !== 0x65 ||
      reader.u8() !== 0x73 ||
      reader.u8() !== 0x22
    ) {
      // "bytes"
      reader.x = x;
      return;
    }
    this.skipWhitespace();
    Iif (reader.u8() !== 0x3a) {
      // :
      reader.x = x;
      return;
    }
    this.skipWhitespace();
    Iif (reader.u8() !== 0x22) {
      // "
      reader.x = x;
      return;
    }
    const bufStart = reader.x;
    const bufEnd = findEndingQuote(reader.uint8, bufStart);
    reader.x = 1 + bufEnd;
    this.skipWhitespace();
    Iif (reader.u8() !== 0x7d) {
      // }
      reader.x = x;
      return;
    }
    this.skipWhitespace();
    Iif (reader.u8() !== 0x7d) {
      // }
      reader.x = x;
      return;
    }
    const bin = fromBase64Bin(reader.view, bufStart, bufEnd - bufStart);
    return bin;
  }
 
  protected tryReadCid(): undefined | unknown {
    const reader = this.reader;
    const x = reader.x;
    Iif (reader.u8() !== 0x7b) {
      // {
      reader.x = x;
      return;
    }
    this.skipWhitespace();
    if (reader.u8() !== 0x22 || reader.u8() !== 0x2f || reader.u8() !== 0x22) {
      // "/"
      reader.x = x;
      return;
    }
    this.skipWhitespace();
    Iif (reader.u8() !== 0x3a) {
      // :
      reader.x = x;
      return;
    }
    this.skipWhitespace();
    Iif (reader.u8() !== 0x22) {
      // "
      reader.x = x;
      return;
    }
    const bufStart = reader.x;
    const bufEnd = findEndingQuote(reader.uint8, bufStart);
    reader.x = 1 + bufEnd;
    this.skipWhitespace();
    Iif (reader.u8() !== 0x7d) {
      // }
      reader.x = x;
      return;
    }
    const finalX = reader.x;
    reader.x = bufStart;
    const cid = reader.ascii(bufEnd - bufStart);
    reader.x = finalX;
    return this.readCid(cid);
  }
 
  public readCid(cid: string): unknown {
    return cid;
  }
}