All files / src/msgpack unpack.ts

100% Statements 37/37
100% Branches 25/25
100% Functions 1/1
100% Lines 32/32

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 7519x                           19x 19x   19x       303075x   303075x   2x   1x   4x   1x   4x   256x   66024x   34465x   1x   1x   1x   2x   1x   1x   1x   1x   1x   1x   1x   1x     202305x 101041x 101035x 101030x 2x   1x    
import {
  bigInt64,
  bigUint64,
  float32,
  float64,
  int16,
  int32,
  int8,
  uint16,
  uint32,
  uint8
} from "..";
 
import { Context, Encoding } from "../types";
import { unpackArrayBody } from "./array";
import { unpackMapBody } from "./map";
 
export function unpack(
  ctx: Context,
  encoding: Encoding<string>
): unknown {
  const byte = ctx.view.getUint8(ctx.i++);
 
  switch (byte) {
    case 0xc0:
      return null;
    case 0xc2:
      return false;
    case 0xc3:
      return true;
    case 0xca:
      return float32.des(ctx);
    case 0xcb:
      return float64.des(ctx);
    case 0xcc:
      return uint8.des(ctx);
    case 0xcd:
      return uint16.des(ctx);
    case 0xce:
      return uint32.des(ctx);
    case 0xcf:
      return Number(bigUint64.des(ctx));
    case 0xd0:
      return int8.des(ctx);
    case 0xd1:
      return int16.des(ctx);
    case 0xd2:
      return int32.des(ctx);
    case 0xd3:
      return Number(bigInt64.des(ctx));
    case 0xd9:
      return encoding.decode(ctx, uint8.des(ctx));
    case 0xda:
      return encoding.decode(ctx, uint16.des(ctx));
    case 0xdb:
      return encoding.decode(ctx, uint32.des(ctx));
    case 0xdc:
      return unpackArrayBody(ctx, uint16.des(ctx), encoding);
    case 0xdd:
      return unpackArrayBody(ctx, uint32.des(ctx), encoding);
    case 0xde:
      return unpackMapBody(ctx, uint16.des(ctx), encoding);
    case 0xdf:
      return unpackMapBody(ctx, uint32.des(ctx), encoding);
  }
 
  if (byte < 0x80) return byte;
  if (byte < 0x90) return unpackMapBody(ctx, byte - 0x80, encoding);
  if (byte < 0xa0) return unpackArrayBody(ctx, byte - 0x90, encoding);
  if (byte < 0xc0) return encoding.decode(ctx, byte - 0xa0);
  if (byte >= 0xe0) return byte - 0x100;
 
  throw new Error("Unsupported type");
}