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

82.6% Statements 76/92
89.77% Branches 79/88
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                         851309x 851309x 851309x 851255x 407857x 203761x 3982x   204096x   201246x     443398x 441125x 2078x 2070x 915x   8x 5x     439047x                               2273x 382x                               1891x   1695x   25x   3x   10x   2x   154x   2x                                             4138x 4138x 278363x 278363x 278362x   4137x         410107x 410107x 410107x 404580x 404580x 404580x 404580x 5527x 5519x 5519x 5201x 5201x 5201x     326x 326x   318x   8x                   2862x 268087x 2862x         14x 14x 14x 14x 14x 14x              
import {Reader} from '@jsonjoy.com/buffers/lib/Reader';
import sharedCachedUtf8Decoder from '@jsonjoy.com/buffers/lib/utf8/sharedCachedUtf8Decoder';
import {JsonPackExtension} from '../JsonPackExtension';
import {ERROR} from '../cbor/constants';
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();
    if (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;
  }
}