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

81.57% Statements 93/114
60.27% Branches 44/73
77.77% Functions 14/18
80.95% Lines 85/105

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  12x 12x         12x   1x   1x   1x     62x 62x 62x                   62x 62x         6839x 6839x 6827x 4715x 1435x 1349x   3280x   2363x     2112x 360x 256x 256x 177x           104x                               1752x 17x                               1735x   1721x   11x       1x       2x                   4095x 4095x 4095x 4095x 4095x 38157x 38157x 38114x 43x 13x 30x 23x 7x 7x 7x 7x 7x 7x 7x 7x 7x   7x         4095x 4095x         1351x 1351x 2990x 2990x 2990x 2990x   1351x         2990x         918x 918x 797x 797x   918x                                           8560x         40x 40x 40x         23x 23x 23x                   12x 12x 12x         5x 5x 5x                       55x 55x 55x      
import type {json_string} from '@jsonjoy.com/util/lib/json-brand';
import {asString} from '@jsonjoy.com/util/lib/strings/asString';
import {toDataUri} from '../util/buffers/toDataUri';
 
/**
 * @category Decoder
 */
export class MsgPackToJsonConverter {
  /** @ignore */
  protected uint8 = new Uint8Array([]);
  /** @ignore */
  protected view = new DataView(this.uint8.buffer);
  /** @ignore */
  protected x = 0;
 
  public reset(uint8: Uint8Array): void {
    this.x = 0;
    this.uint8 = uint8 as Uint8Array<any>;
    this.view = new DataView(uint8.buffer, uint8.byteOffset, uint8.length) as DataView<any>;
  }
 
  /**
   * Converts a MessagePack blob directly to JSON string.
   *
   * @param uint8 Binary data with MessagePack encoded value.
   * @returns JSON string.
   */
  public convert<T = unknown>(uint8: Uint8Array): json_string<T> {
    this.reset(uint8);
    return this.val() as json_string<T>;
  }
 
  /** @ignore */
  protected val(): string {
    const byte = this.u8();
    if (byte >= 0xe0) return (byte - 0x100).toString(); // 0xe0
    if (byte <= 0xbf) {
      if (byte < 0x90) {
        if (byte <= 0b1111111) return byte.toString(); // 0x7f
        return this.obj(byte & 0b1111); // 0x80
      } else {
        if (byte < 0xa0) return this.arr(byte & 0b1111);
        // 0x90
        else return this.str(byte & 0b11111); // 0xa0
      }
    }
    if (byte <= 0xd0) {
      if (byte <= 0xc8) {
        if (byte <= 0xc4) {
          if (byte <= 0xc2) return byte === 0xc2 ? 'false' : 'null';
          else return byte === 0xc4 ? this.bin(this.u8()) : 'true';
        } else E{
          if (byte <= 0xc6) return byte === 0xc6 ? this.bin(this.u32()) : this.bin(this.u16());
          else return byte === 0xc8 ? this.ext(this.u16()) : this.ext(this.u8());
        }
      } else {
        return byte <= 0xcc
          ? byte <= 0xca
            ? byte === 0xca
              ? this.f32().toString()
              : this.ext(this.u32())
            : byte === 0xcc
              ? this.u8().toString()
              : this.f64().toString()
          : byte <= 0xce
            ? byte === 0xce
              ? this.u32().toString()
              : this.u16().toString()
            : byte === 0xd0
              ? this.i8().toString()
              : (this.u32() * 4294967296 + this.u32()).toString();
      }
    } else if (byte <= 0xd8) {
      return byte <= 0xd4
        ? byte <= 0xd2
          ? byte === 0xd2
            ? this.i32().toString()
            : this.i16().toString()
          : byte === 0xd4
            ? this.ext(1)
            : (this.i32() * 4294967296 + this.i32()).toString()
        : byte <= 0xd6
          ? byte === 0xd6
            ? this.ext(4)
            : this.ext(2)
          : byte === 0xd8
            ? this.ext(16)
            : this.ext(8);
    } else {
      switch (byte) {
        case 0xd9:
          return this.str(this.u8());
        case 0xda:
          return this.str(this.u16());
        case 0xdb:
          return this.str(this.u32());
        case 0xdc:
          return this.arr(this.u16());
        case 0xdd:
          return this.arr(this.u32());
        case 0xde:
          return this.obj(this.u16());
        case 0xdf:
          return this.obj(this.u32());
      }
    }
    return '';
  }
 
  /** @ignore */
  protected str(size: number): string {
    const uint8 = this.uint8;
    const end = this.x + size;
    let x = this.x;
    let str = '';
    while (x < end) {
      const b1 = uint8[x++]!;
      if ((b1 & 0x80) === 0) {
        str += String.fromCharCode(b1);
      } else if ((b1 & 0xe0) === 0xc0) {
        str += String.fromCharCode(((b1 & 0x1f) << 6) | (uint8[x++]! & 0x3f));
      } else if ((b1 & 0xf0) === 0xe0) {
        str += String.fromCharCode(((b1 & 0x1f) << 12) | ((uint8[x++]! & 0x3f) << 6) | (uint8[x++]! & 0x3f));
      } else if ((b1 & 0xf8) === 0xf0) {
        const b2 = uint8[x++]! & 0x3f;
        const b3 = uint8[x++]! & 0x3f;
        const b4 = uint8[x++]! & 0x3f;
        let code = ((b1 & 0x07) << 0x12) | (b2 << 0x0c) | (b3 << 0x06) | b4;
        if (code > 0xffff) {
          code -= 0x10000;
          str += String.fromCharCode(((code >>> 10) & 0x3ff) | 0xd800);
          code = 0xdc00 | (code & 0x3ff);
        }
        str += String.fromCharCode(code);
      } else E{
        str += String.fromCharCode(b1);
      }
    }
    this.x = end;
    return asString(str);
  }
 
  /** @ignore */
  protected obj(size: number): json_string<object> {
    let str = '{';
    for (let i = 0; i < size; i++) {
      if (i > 0) str += ',';
      str += this.key();
      str += ':';
      str += this.val();
    }
    return (str + '}') as json_string<object>;
  }
 
  /** @ignore */
  protected key(): json_string<string> {
    return this.val() as json_string<string>;
  }
 
  /** @ignore */
  protected arr(size: number): json_string<unknown[]> {
    let str = '[';
    for (let i = 0; i < size; i++) {
      if (i > 0) str += ',';
      str += this.val();
    }
    return (str + ']') as json_string<unknown[]>;
  }
 
  /** @ignore */
  protected bin(size: number): string {
    const end = this.x + size;
    const buf = this.uint8.subarray(this.x, end);
    this.x = end;
    return '"' + toDataUri(buf) + '"';
  }
 
  /** @ignore */
  protected ext(size: number): string {
    const ext = this.u8();
    const end = this.x + size;
    const buf = this.uint8.subarray(this.x, end);
    this.x = end;
    return '"' + toDataUri(buf, {ext}) + '"';
  }
 
  /** @ignore */
  protected u8(): number {
    return this.view.getUint8(this.x++);
  }
 
  /** @ignore */
  protected u16(): number {
    const num = this.view.getUint16(this.x);
    this.x += 2;
    return num;
  }
 
  /** @ignore */
  protected u32(): number {
    const num = this.view.getUint32(this.x);
    this.x += 4;
    return num;
  }
 
  /** @ignore */
  protected i8(): number {
    return this.view.getInt8(this.x++);
  }
 
  /** @ignore */
  protected i16(): number {
    const num = this.view.getInt16(this.x);
    this.x += 2;
    return num;
  }
 
  /** @ignore */
  protected i32(): number {
    const num = this.view.getInt32(this.x);
    this.x += 4;
    return num;
  }
 
  /** @ignore */
  protected f32(): number {
    const pos = this.x;
    this.x += 4;
    return this.view.getFloat32(pos);
  }
 
  /** @ignore */
  protected f64(): number {
    const pos = this.x;
    this.x += 8;
    return this.view.getFloat64(pos);
  }
}