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

83.7% Statements 113/135
55.44% Branches 56/101
100% Functions 14/14
81.41% Lines 92/113

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 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 25711x 11x             11x               304x 304x 303x 244x 113x 58x   131x   109x     59x 59x 47x 47x 34x           12x                                                                                                             22x 51x 22x         58x 58x 87x   58x           39x 39x       39x 39x 39x 11x 11x 11x 11x 131667x 131667x   11x   28x 28x 9x 9x 9x 131365x 9x   19x                 263032x 263032x 263032x 263032x 5x 5x 5x 5x   263027x       121x 121x                                     5x 5x 5x 5x 5x 5x           97x 97x 97x 97x 2x   1x       1x       58x 58x 58x                         55x 55x 77x 77x 22x           34x 34x 34x 34x 2x   1x       1x       19x 19x 16x 16x       27x 49x 49x 12x   26x      
import {JsonPackValue} from '.';
import {MsgPackDecoderFast} from './MsgPackDecoderFast';
import type {Path} from '@jsonjoy.com/json-pointer';
import type {Reader} from '@jsonjoy.com/buffers/lib/Reader';
 
/**
 * @category Decoder
 */
export class MsgPackDecoder extends MsgPackDecoderFast<Reader> {
  // ---------------------------------------------------------- Skipping values
 
  /**
   * Skips a whole JSON value and returns back the number of bytes
   * that value consumed.
   */
  public skipAny(): number {
    const byte = this.reader.u8();
    if (byte >= 0xe0) return 1; // 0xe0
    if (byte <= 0xbf) {
      if (byte < 0x90) {
        if (byte <= 0b1111111) return 1; // 0x7f
        return 1 + this.skipObj(byte & 0b1111); // 0x80, obj(1)
      } else {
        if (byte < 0xa0) return 1 + this.skipArr(byte & 0b1111);
        // 0x90
        else return 1 + this.skip(byte & 0b11111); // 0xa0, str(1)
      }
    }
    if (byte <= 0xd0) {
      if (byte <= 0xc8) {
        if (byte <= 0xc4) {
          if (byte <= 0xc2) return byte === 0xc2 ? 1 : 1;
          else return byte === 0xc4 ? 2 + this.skip(this.reader.u8()) : 1;
        } else E{
          if (byte <= 0xc6) return byte === 0xc6 ? 5 + this.skip(this.reader.u32()) : 3 + this.skip(this.reader.u16());
          else return byte === 0xc8 ? 4 + this.skip(this.reader.u16()) : 3 + this.skip(this.reader.u8());
        }
      } else {
        return byte <= 0xcc
          ? byte <= 0xca
            ? byte === 0xca
              ? 1 + this.skip(4) // f32
              : 1 + 1 + 4 + this.skip(this.reader.u32()) // ext32
            : byte === 0xcc
              ? 1 + this.skip(1) // u8
              : 1 + this.skip(8) // f64
          : byte <= 0xce
            ? byte === 0xce
              ? 1 + this.skip(4) // u32
              : 1 + this.skip(2) // u16
            : byte === 0xd0
              ? 1 + this.skip(1) // i8
              : 1 + this.skip(8); // u64
      }
    } else Eif (byte <= 0xd8) {
      return byte <= 0xd4
        ? byte <= 0xd2
          ? byte === 0xd2
            ? 1 + this.skip(4) // i32
            : 1 + this.skip(2) // i16
          : byte === 0xd4
            ? 1 + this.skip(2) // ext1
            : 1 + this.skip(8) // i64
        : byte <= 0xd6
          ? byte === 0xd6
            ? 1 + this.skip(5) // ext4
            : 1 + this.skip(3) // ext2
          : byte === 0xd8
            ? 1 + this.skip(17) // ext16
            : 1 + this.skip(9); // ext8
    } else {
      switch (byte) {
        case 0xd9:
          return 2 + this.skip(this.reader.u8()); // str8
        case 0xda:
          return 3 + this.skip(this.reader.u16()); // str16
        case 0xdb:
          return 5 + this.skip(this.reader.u32()); // str32
        case 0xdc:
          return 3 + this.skipArr(this.reader.u16());
        case 0xdd:
          return 5 + this.skipArr(this.reader.u32());
        case 0xde:
          return 3 + this.skipObj(this.reader.u16());
        case 0xdf:
          return 5 + this.skipObj(this.reader.u32());
      }
    }
    return 1;
  }
 
  /** @ignore */
  protected skipArr(size: number): number {
    let length = 0;
    for (let i = 0; i < size; i++) length += this.skipAny();
    return length;
  }
 
  /** @ignore */
  protected skipObj(size: number): number {
    let length = 0;
    for (let i = 0; i < size; i++) {
      length += this.skipAny() + this.skipAny();
    }
    return length;
  }
 
  // -------------------------------------------------------- One level reading
 
  public readLevel(uint8: Uint8Array): unknown {
    this.reader.reset(uint8);
    return this.valOneLevel();
  }
 
  protected valOneLevel(): unknown {
    const byte = this.reader.view.getUint8(this.reader.x);
    const isMap = byte === 0xde || byte === 0xdf || byte >> 4 === 0b1000;
    if (isMap) {
      this.reader.x++;
      const size = byte === 0xde ? this.reader.u16() : byte === 0xdf ? this.reader.u32() : byte & 0b1111;
      const obj: Record<string, unknown> = {};
      for (let i = 0; i < size; i++) {
        const key = this.key();
        obj[key] = this.primitive();
      }
      return obj;
    }
    const isArray = byte === 0xdc || byte === 0xdd || byte >> 4 === 0b1001;
    if (isArray) {
      this.reader.x++;
      const size = byte === 0xdc ? this.reader.u16() : byte === 0xdd ? this.reader.u32() : byte & 0b1111;
      const arr: unknown[] = [];
      for (let i = 0; i < size; i++) arr.push(this.primitive());
      return arr;
    }
    return this.readAny();
  }
 
  /**
   * @ignore
   * @returns Returns a primitive value or {@link JsonPackValue} object, if the value
   *          is a "map" or an "arr".
   */
  protected primitive(): unknown {
    const reader = this.reader;
    const byte = reader.view.getUint8(reader.x);
    const isMapOrArray = byte === 0xde || byte === 0xdf || byte === 0xdc || byte === 0xdd || byte >> 5 === 0b100;
    if (isMapOrArray) {
      const length = this.skipAny();
      reader.x -= length;
      const buf = reader.buf(length);
      return new JsonPackValue(buf);
    }
    return this.readAny();
  }
 
  protected skip(length: number): number {
    this.reader.x += length;
    return length;
  }
 
  // --------------------------------------------------------------- Validation
 
  /**
   * Throws if at given offset in a buffer there is an invalid MessagePack
   * value, or if the value does not span the exact length specified in `size`.
   * I.e. throws if:
   *
   * - The value is not a valid MessagePack value.
   * - The value is shorter than `size`.
   * - The value is longer than `size`.
   *
   * @param value Buffer in which to validate MessagePack value.
   * @param offset Offset at which the value starts.
   * @param size Expected size of the value.
   */
  public validate(value: Uint8Array, offset: number = 0, size: number = value.length): void {
    this.reader.reset(value);
    this.reader.x = offset;
    const start = offset;
    this.skipAny();
    const end = this.reader.x;
    if (end - start !== size) throw new Error('INVALID_SIZE');
  }
 
  // ---------------------------------------------------------- Shallow reading
 
  public readObjHdr(): number {
    const reader = this.reader;
    const byte = reader.u8();
    const isFixMap = byte >> 4 === 0b1000;
    if (isFixMap) return byte & 0b1111;
    switch (byte) {
      case 0xde:
        return reader.u16();
      case 0xdf:
        return reader.u32();
    }
    throw new Error('NOT_OBJ');
  }
 
  public readStrHdr(): number {
    const reader = this.reader;
    const byte = reader.u8();
    if (byte >> 5 === 0b101) return byte & 0b11111;
    switch (byte) {
      case 0xd9:
        return reader.u8();
      case 0xda:
        return reader.u16();
      case 0xdb:
        return reader.u32();
    }
    throw new Error('NOT_STR');
  }
 
  public findKey(key: string): this {
    const size = this.readObjHdr();
    for (let i = 0; i < size; i++) {
      const k = this.key();
      if (k === key) return this;
      this.skipAny();
    }
    throw new Error('KEY_NOT_FOUND');
  }
 
  public readArrHdr(): number {
    const reader = this.reader;
    const byte = reader.u8();
    const isFixArr = byte >> 4 === 0b1001;
    if (isFixArr) return byte & 0b1111;
    switch (byte) {
      case 0xdc:
        return this.reader.u16();
      case 0xdd:
        return this.reader.u32();
    }
    throw new Error('NOT_ARR');
  }
 
  public findIndex(index: number): this {
    const size = this.readArrHdr();
    if (index >= size) throw new Error('INDEX_OUT_OF_BOUNDS');
    for (let i = 0; i < index; i++) this.skipAny();
    return this;
  }
 
  public find(path: Path): this {
    for (let i = 0; i < path.length; i++) {
      const segment = path[i];
      if (typeof segment === 'string') this.findKey(segment);
      else this.findIndex(segment);
    }
    return this;
  }
}