All files / json-pack/src/msgpack MsgPackDecoderFast.ts

82.6% Statements 76/92
90.12% Branches 73/81
63.63% Functions 7/11
81.7% Lines 67/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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 18615x 15x   15x             15x   41x 41x         41805x 41805x                         851305x 851305x 851305x 851263x 407815x 203837x 4028x   203978x   201132x     443448x 441218x 2176x 2168x 913x   8x 5x     439042x                               2230x 379x                               1851x   1675x   25x   3x   12x   2x   132x   2x                                             4162x 4162x 278383x 278383x 278382x   4161x         410127x 410127x 410127x 404645x 404645x 404645x 404645x 5482x 5474x 5474x 5156x 5156x 5156x     326x 326x   318x   8x                   2860x 268063x 2860x         14x 14x 14x 14x 14x 14x              
import {JsonPackExtension} from '../JsonPackExtension';
import {Reader} from '@jsonjoy.com/buffers/lib/Reader';
import {ERROR} from '../cbor/constants';
import sharedCachedUtf8Decoder from '@jsonjoy.com/buffers/lib/utf8/sharedCachedUtf8Decoder';
import type {BinaryJsonDecoder, PackValue} from '../types';
import type {CachedUtf8Decoder} from '@jsonjoy.com/buffers/lib/utf8/CachedUtf8Decoder';
 
/**
 * @category Decoder
 */
export class MsgPackDecoderFast<R extends Reader> implements BinaryJsonDecoder {
  public constructor(
    public reader: R = new Reader() as R,
    protected readonly keyDecoder: CachedUtf8Decoder = sharedCachedUtf8Decoder,
  ) {}
 
  /** @deprecated */
  public decode(uint8: Uint8Array): unknown {
    this.reader.reset(uint8);
    return this.readAny();
  }
 
  public read(uint8: Uint8Array): PackValue {
    this.reader.reset(uint8);
    return this.readAny() as PackValue;
  }
 
  public val(): unknown {
    return this.readAny();
  }
 
  public readAny(): unknown {
    const reader = this.reader;
    const byte = reader.u8();
    if (byte >= 0xe0) return byte - 0x100; // 0xe0
    if (byte <= 0xbf) {
      if (byte < 0x90) {
        if (byte <= 0b1111111) return byte; // 0x7f
        return this.obj(byte & 0b1111); // 0x80
      } else {
        if (byte < 0xa0) return this.arr(byte & 0b1111);
        // 0x90
        else return reader.utf8(byte & 0b11111); // 0xa0
      }
    }
    if (byte <= 0xd0) {
      if (byte <= 0xc8) {
        if (byte <= 0xc4) {
          if (byte <= 0xc2) return byte === 0xc2 ? false : byte === 0xc0 ? null : undefined;
          else return byte === 0xc4 ? reader.buf(reader.u8()) : true;
        } else {
          if (byte <= 0xc6) return byte === 0xc6 ? reader.buf(reader.u32()) : reader.buf(reader.u16());
          else return byte === 0xc8 ? this.ext(reader.u16()) : this.ext(reader.u8());
        }
      } else {
        return byte <= 0xcc
          ? byte <= 0xca
            ? byte === 0xca
              ? reader.f32()
              : this.ext(reader.u32())
            : byte === 0xcc
              ? reader.u8()
              : reader.f64()
          : byte <= 0xce
            ? byte === 0xce
              ? reader.u32()
              : reader.u16()
            : byte === 0xd0
              ? reader.i8()
              : reader.u32() * 4294967296 + reader.u32();
      }
    } else if (byte <= 0xd8) {
      return byte <= 0xd4
        ? byte <= 0xd2
          ? byte === 0xd2
            ? reader.i32()
            : reader.i16()
          : byte === 0xd4
            ? this.ext(1)
            : reader.i32() * 4294967296 + reader.u32()
        : byte <= 0xd6
          ? byte === 0xd6
            ? this.ext(4)
            : this.ext(2)
          : byte === 0xd8
            ? this.ext(16)
            : this.ext(8);
    } else {
      switch (byte) {
        case 0xd9:
          return reader.utf8(reader.u8());
        case 0xda:
          return reader.utf8(reader.u16());
        case 0xdb:
          return reader.utf8(reader.u32());
        case 0xdc:
          return this.arr(reader.u16());
        case 0xdd:
          return this.arr(reader.u32());
        case 0xde:
          return this.obj(reader.u16());
        case 0xdf:
          return this.obj(reader.u32());
      }
    }
    return undefined;
  }
 
  public str(): unknown {
    const reader = this.reader;
    const byte = reader.u8();
    Iif (byte >> 5 === 0b101) return reader.utf8(byte & 0b11111);
    switch (byte) {
      case 0xd9:
        return reader.utf8(reader.u8());
      case 0xda:
        return reader.utf8(reader.u16());
      case 0xdb:
        return reader.utf8(reader.u32());
    }
    return undefined;
  }
 
  /** @ignore */
  protected obj(size: number): object {
    const obj: Record<string, unknown> = {};
    for (let i = 0; i < size; i++) {
      const key = this.key();
      if (key === '__proto__') throw ERROR.UNEXPECTED_OBJ_KEY;
      obj[key] = this.readAny();
    }
    return obj;
  }
 
  /** @ignore */
  protected key(): string {
    const reader = this.reader;
    const byte = reader.view.getUint8(reader.x);
    if (byte >= 0b10100000 && byte <= 0b10111111) {
      const size = byte & 0b11111;
      const key = this.keyDecoder.decode(reader.uint8, reader.x + 1, size);
      reader.x += 1 + size;
      return key;
    } else if (byte === 0xd9) {
      const size = reader.view.getUint8(reader.x + 1);
      if (size < 32) {
        const key = this.keyDecoder.decode(reader.uint8, reader.x + 2, size);
        reader.x += 2 + size;
        return key;
      }
    }
    reader.x++;
    switch (byte) {
      case 0xd9:
        return reader.utf8(reader.u8());
      case 0xda:
        return reader.utf8(reader.u16());
      case 0xdb:
        return reader.utf8(reader.u32());
      default:
        return '';
    }
  }
 
  /** @ignore */
  protected arr(size: number): unknown[] {
    const arr: unknown[] = [];
    for (let i = 0; i < size; i++) arr.push(this.readAny());
    return arr;
  }
 
  /** @ignore */
  protected ext(size: number): JsonPackExtension {
    const reader = this.reader;
    const type = reader.u8();
    const end = reader.x + size;
    const buf = reader.uint8.subarray(reader.x, end);
    reader.x = end;
    return new JsonPackExtension(type, buf);
  }
 
  protected back(bytes: number) {
    this.reader.x -= bytes;
  }
}