All files / json-pack/src/cbor CborDecoderBase.ts

95.45% Statements 210/220
97.79% Branches 133/136
96.15% Functions 25/26
95.23% Lines 180/189

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 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356  17x 17x 17x 17x 17x         17x       46x 46x       809x       26784x 26784x           17x       1580153x 1580153x 1579351x 1579351x 1579351x 1142184x 190213x   437167x 174521x         755x 755x 755x 347x 102x   408x 231x         86641x 405x   58x   66x   60x   45x   84x   92x             906471x 338263x   568208x 511735x 56473x 56328x 55958x 145x             45745x 1192x   44553x 443x 44110x 43985x 43595x 125x             53229x 53229x 29276x   29034x   56x   28x   29x   45x 45x 45x 45x 10x 10x   4x 4x 4x 4x 4x 7x 7x 7x   4x     84x         45x 45x 45x 45x 15x 12x                             137112x 137112x 111226x   110997x   45x   43x   22x   41x 44x 5x 5x     78x         521647x 409233x   365411x   43798x   3x   4x   17x         44x 44x 44x 44x 17x 14x           79892x 79800x 58x       79741x 617252x 61512x       58x 170x 27x 27x           182931x 182792x 182792x   107x 104x   34x 27x   29x 24x   30x 9x   182756x 182756x 522599x 509985x 509973x 473313x   133470x 139x 96x         1x 1x 1x 1x               43x 43x 52x 38x 35x 31x 26x   15x 15x       522777x 522777x 522349x 522349x 522349x 521647x 521309x 394039x 390431x 390431x 390431x           30411x 26893x   37x   26545x   147x   27x   137x         30181x           144340x   27524x   19822x   35999x   13370x   45x   72x   84x   46710x   714x 130x       72x      
import {CONST, ERROR, MAJOR} from './constants';
import {decodeF16} from '@jsonjoy.com/buffers/lib/f16';
import {JsonPackExtension} from '../JsonPackExtension';
import {JsonPackValue} from '../JsonPackValue';
import {Reader} from '@jsonjoy.com/buffers/lib/Reader';
import sharedCachedUtf8Decoder from '@jsonjoy.com/buffers/lib/utf8/sharedCachedUtf8Decoder';
import type {CachedUtf8Decoder} from '@jsonjoy.com/buffers/lib/utf8/CachedUtf8Decoder';
import type {IReader, IReaderResettable} from '@jsonjoy.com/buffers/lib';
import type {BinaryJsonDecoder, PackValue} from '../types';
 
export class CborDecoderBase<R extends IReader & IReaderResettable = IReader & IReaderResettable>
  implements BinaryJsonDecoder
{
  public constructor(
    public reader: R = new Reader() as any,
    public readonly keyDecoder: CachedUtf8Decoder = sharedCachedUtf8Decoder,
  ) {}
 
  public read(uint8: Uint8Array): PackValue {
    return this.decode(uint8) as PackValue;
  }
 
  public decode(uint8: Uint8Array): unknown {
    this.reader.reset(uint8);
    return this.readAny();
  }
 
  // -------------------------------------------------------- Any value reading
 
  public val(): unknown {
    return this.readAny();
  }
 
  public readAny(): unknown {
    const reader = this.reader;
    const octet = reader.u8();
    const major = octet >> 5;
    const minor = octet & CONST.MINOR_MASK;
    if (major < MAJOR.ARR) {
      if (major < MAJOR.BIN) return major === MAJOR.UIN ? this.readUint(minor) : this.readNint(minor);
      else return major === MAJOR.BIN ? this.readBin(minor) : this.readStr(minor);
    } else {
      if (major < MAJOR.TAG) return major === MAJOR.ARR ? this.readArr(minor) : this.readObj(minor);
      else return major === MAJOR.TAG ? this.readTag(minor) : this.readTkn(minor);
    }
  }
 
  public readAnyRaw(octet: number): unknown {
    const major = octet >> 5;
    const minor = octet & CONST.MINOR_MASK;
    if (major < MAJOR.ARR) {
      if (major < MAJOR.BIN) return major === MAJOR.UIN ? this.readUint(minor) : this.readNint(minor);
      else return major === MAJOR.BIN ? this.readBin(minor) : this.readStr(minor);
    } else {
      if (major < MAJOR.TAG) return major === MAJOR.ARR ? this.readArr(minor) : this.readObj(minor);
      else return major === MAJOR.TAG ? this.readTag(minor) : this.readTkn(minor);
    }
  }
 
  public readMinorLen(minor: number): number {
    if (minor < 24) return minor;
    switch (minor) {
      case 24:
        return this.reader.u8();
      case 25:
        return this.reader.u16();
      case 26:
        return this.reader.u32();
      case 27:
        return Number(this.reader.u64());
      case 31:
        return -1;
      default:
        throw ERROR.UNEXPECTED_MINOR;
    }
  }
 
  // ----------------------------------------------------- Unsigned int reading
 
  public readUint(minor: number): number | bigint {
    if (minor < 25) {
      return minor === 24 ? this.reader.u8() : minor;
    } else {
      if (minor < 27) {
        return minor === 25 ? this.reader.u16() : this.reader.u32();
      } else if (minor === 27) {
        const num = this.reader.u64();
        return num > CONST.MAX_UINT ? num : Number(num);
      } else throw ERROR.UNEXPECTED_MINOR;
    }
  }
 
  // ----------------------------------------------------- Negative int reading
 
  public readNint(minor: number): number | bigint {
    if (minor < 25) {
      return minor === 24 ? -this.reader.u8() - 1 : -minor - 1;
    } else {
      if (minor < 27) {
        return minor === 25 ? -this.reader.u16() - 1 : -this.reader.u32() - 1;
      } else if (minor === 27) {
        const num = this.reader.u64();
        return num > CONST.MAX_UINT - 1 ? -num - BigInt(1) : -Number(num) - 1;
      } else throw ERROR.UNEXPECTED_MINOR;
    }
  }
 
  // ----------------------------------------------------------- Binary reading
 
  public readBin(minor: number): Uint8Array {
    const reader = this.reader;
    if (minor <= 23) return reader.buf(minor);
    switch (minor) {
      case 24:
        return reader.buf(reader.u8());
      case 25:
        return reader.buf(reader.u16());
      case 26:
        return reader.buf(reader.u32());
      case 27:
        return reader.buf(Number(reader.u64()));
      case 31: {
        let size = 0;
        const list: Uint8Array[] = [];
        while (this.reader.peak() !== CONST.END) {
          const uint8 = this.readBinChunk();
          size += uint8.length;
          list.push(uint8);
        }
        this.reader.x++;
        const res = new Uint8Array(size);
        let offset = 0;
        const length = list.length;
        for (let i = 0; i < length; i++) {
          const arr = list[i];
          res.set(arr, offset);
          offset += arr.length;
        }
        return res;
      }
      default:
        throw ERROR.UNEXPECTED_MINOR;
    }
  }
 
  public readBinChunk(): Uint8Array {
    const octet = this.reader.u8();
    const major = octet >> 5;
    const minor = octet & CONST.MINOR_MASK;
    if (major !== MAJOR.BIN) throw ERROR.UNEXPECTED_BIN_CHUNK_MAJOR;
    if (minor > 27) throw ERROR.UNEXPECTED_BIN_CHUNK_MINOR;
    return this.readBin(minor);
  }
 
  // ----------------------------------------------------------- String reading
 
  public readAsStr(): string {
    const reader = this.reader;
    const octet = reader.u8();
    const major = octet >> 5;
    const minor = octet & CONST.MINOR_MASK;
    if (major !== MAJOR.STR) throw ERROR.UNEXPECTED_STR_MAJOR;
    return this.readStr(minor);
  }
 
  public readStr(minor: number): string {
    const reader = this.reader;
    if (minor <= 23) return reader.utf8(minor);
    switch (minor) {
      case 24:
        return reader.utf8(reader.u8());
      case 25:
        return reader.utf8(reader.u16());
      case 26:
        return reader.utf8(reader.u32());
      case 27:
        return reader.utf8(Number(reader.u64()));
      case 31: {
        let str = '';
        while (reader.peak() !== CONST.END) str += this.readStrChunk();
        this.reader.x++;
        return str;
      }
      default:
        throw ERROR.UNEXPECTED_MINOR;
    }
  }
 
  public readStrLen(minor: number): number {
    if (minor <= 23) return minor;
    switch (minor) {
      case 24:
        return this.reader.u8();
      case 25:
        return this.reader.u16();
      case 26:
        return this.reader.u32();
      case 27:
        return Number(this.reader.u64());
      default:
        throw ERROR.UNEXPECTED_MINOR;
    }
  }
 
  public readStrChunk(): string {
    const octet = this.reader.u8();
    const major = octet >> 5;
    const minor = octet & CONST.MINOR_MASK;
    if (major !== MAJOR.STR) throw ERROR.UNEXPECTED_STR_CHUNK_MAJOR;
    if (minor > 27) throw ERROR.UNEXPECTED_STR_CHUNK_MINOR;
    return this.readStr(minor);
  }
 
  // ------------------------------------------------------------ Array reading
 
  public readArr(minor: number): unknown[] {
    const length = this.readMinorLen(minor);
    if (length >= 0) return this.readArrRaw(length);
    return this.readArrIndef();
  }
 
  public readArrRaw(length: number): unknown[] {
    const arr: unknown[] = [];
    for (let i = 0; i < length; i++) arr.push(this.readAny());
    return arr;
  }
 
  public readArrIndef(): unknown[] {
    const arr: unknown[] = [];
    while (this.reader.peak() !== CONST.END) arr.push(this.readAny());
    this.reader.x++;
    return arr;
  }
 
  // ----------------------------------------------------------- Object reading
 
  public readObj(minor: number): Record<string, unknown> {
    if (minor < 28) {
      let length = minor;
      switch (minor) {
        case 24:
          length = this.reader.u8();
          break;
        case 25:
          length = this.reader.u16();
          break;
        case 26:
          length = this.reader.u32();
          break;
        case 27:
          length = Number(this.reader.u64());
          break;
      }
      const obj: Record<string, unknown> = {};
      for (let i = 0; i < length; i++) {
        const key = this.key();
        if (key === '__proto__') throw ERROR.UNEXPECTED_OBJ_KEY;
        const value = this.readAny();
        obj[key] = value;
      }
      return obj;
    } else if (minor === 31) return this.readObjIndef();
    else throw ERROR.UNEXPECTED_MINOR;
  }
 
  /** Remove this? */
  public readObjRaw(length: number): Record<string, unknown> {
    const obj: Record<string, unknown> = {};
    for (let i = 0; i < length; i++) {
      const key = this.key();
      Eif (key === '__proto__') throw ERROR.UNEXPECTED_OBJ_KEY;
      const value = this.readAny();
      obj[key] = value;
    }
    return obj;
  }
 
  public readObjIndef(): Record<string, unknown> {
    const obj: Record<string, unknown> = {};
    while (this.reader.peak() !== CONST.END) {
      const key = this.key();
      if (key === '__proto__') throw ERROR.UNEXPECTED_OBJ_KEY;
      if (this.reader.peak() === CONST.END) throw ERROR.UNEXPECTED_OBJ_BREAK;
      const value = this.readAny();
      obj[key] = value;
    }
    this.reader.x++;
    return obj;
  }
 
  public key(): string {
    const reader = this.reader;
    const octet = reader.u8();
    const major = octet >> 5;
    const minor = octet & CONST.MINOR_MASK;
    if (major !== MAJOR.STR) return String(this.readAnyRaw(octet));
    const length = this.readStrLen(minor);
    if (length > 31) return reader.utf8(length);
    if (length > reader.size()) throw ERROR.UNEXPECTED_EOF;
    const key = this.keyDecoder.decode(reader.uint8, reader.x, length);
    reader.skip(length);
    return key;
  }
 
  // -------------------------------------------------------------- Tag reading
 
  public readTag(minor: number): JsonPackExtension<unknown> | unknown {
    if (minor <= 23) return this.readTagRaw(minor);
    switch (minor) {
      case 24:
        return this.readTagRaw(this.reader.u8());
      case 25:
        return this.readTagRaw(this.reader.u16());
      case 26:
        return this.readTagRaw(this.reader.u32());
      case 27:
        return this.readTagRaw(Number(this.reader.u64()));
      default:
        throw ERROR.UNEXPECTED_MINOR;
    }
  }
 
  public readTagRaw(tag: number): JsonPackExtension<unknown> | unknown {
    return new JsonPackExtension<unknown>(tag, this.readAny());
  }
 
  // ------------------------------------------------------------ Token reading
 
  public readTkn(minor: number): number | true | false | null | undefined | JsonPackValue<number> {
    switch (minor) {
      case 0xf4 & CONST.MINOR_MASK:
        return false;
      case 0xf5 & CONST.MINOR_MASK:
        return true;
      case 0xf6 & CONST.MINOR_MASK:
        return null;
      case 0xf7 & CONST.MINOR_MASK:
        return undefined;
      case 0xf8 & CONST.MINOR_MASK:
        return new JsonPackValue<number>(this.reader.u8());
      case 0xf9 & CONST.MINOR_MASK:
        return this.f16();
      case 0xfa & CONST.MINOR_MASK:
        return this.reader.f32();
      case 0xfb & CONST.MINOR_MASK:
        return this.reader.f64();
    }
    if (minor <= 23) return new JsonPackValue<number>(minor);
    throw ERROR.UNEXPECTED_MINOR;
  }
 
  public f16(): number {
    return decodeF16(this.reader.u16());
  }
}