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

76.96% Statements 137/178
74.35% Branches 58/78
52.63% Functions 20/38
79.73% Lines 122/153

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  16x               16x     44x                 41804x 41804x 41804x                 840762x   328x   301x   456x   839677x 838232x   701944x   134191x   2097x                           33356x       866413x 866413x 866413x 866413x 332001x         534412x 524453x 524453x 524453x 9959x 9959x 9959x 9959x         228x 228x 228x 228x 59x         169x 110x 110x 110x 59x 59x 59x 59x           198053x       899997x 33584x 33356x                                                                                                     67794x       742069x 742069x 742069x 742069x 742069x 742069x 742069x 8916x 8870x 8870x 8870x 46x 42x 42x 42x   4x 4x 4x   742069x 742069x 8916x 46x 4x                                       1901x                   3346x 3346x 23x 4x   530109x                   2845x       4942x 88x 84x 4x 4x         4940x 4940x 4940x 4940x 540080x 540080x 540080x         14x   2x 2x   2x 2x   1x 1x   1x 1x   1x 1x   7x 3x 3x 4x 2x 2x 2x 2x 2x           14x 14x 14x 14x                   8x       8x 3x 2x 1x 1x         8x 8x 8x      
import type {IWriter, IWriterGrowable} from '@jsonjoy.com/buffers/lib';
import {Writer} from '@jsonjoy.com/buffers/lib/Writer';
import type {JsonPackExtension} from '../JsonPackExtension';
import type {BinaryJsonEncoder, TlvBinaryJsonEncoder} from '../types';
import type {IMessagePackEncoder} from './types';
 
/**
 * @category Encoder
 */
export class MsgPackEncoderFast<W extends IWriter & IWriterGrowable = IWriter & IWriterGrowable>
  implements IMessagePackEncoder, BinaryJsonEncoder, TlvBinaryJsonEncoder
{
  constructor(public readonly writer: W = new Writer() as any) {}
 
  /**
   * Use this method to encode a JavaScript document into MessagePack format.
   *
   * @param json JSON value to encode.
   * @returns Encoded memory buffer with MessagePack contents.
   */
  public encode(json: unknown): Uint8Array {
    this.writer.reset();
    this.writeAny(json);
    return this.writer.flush();
  }
 
  /** @deprecated */
  public encodeAny(json: unknown): void {
    this.writeAny(json);
  }
 
  public writeAny(value: unknown): void {
    switch (value) {
      case null:
        return this.writer.u8(0xc0);
      case false:
        return this.writer.u8(0xc2);
      case true:
        return this.writer.u8(0xc3);
    }
    if (value instanceof Array) return this.writeArr(value);
    switch (typeof value) {
      case 'number':
        return this.writeNumber(value);
      case 'string':
        return this.writeStr(value);
      case 'object':
        return this.writeObj(value as Record<string, unknown>);
    }
  }
 
  /** @deprecated */
  protected encodeFloat64(num: number): void {
    this.writeFloat(num);
  }
 
  public writeNull(): void {
    return this.writer.u8(0xc0);
  }
 
  public writeFloat(float: number): void {
    this.writer.u8f64(0xcb, float);
  }
 
  public u32(num: number): void {
    const writer = this.writer;
    this.writer.ensureCapacity(5);
    const uint8 = writer.uint8;
    if (num <= 0b1111111) {
      uint8[writer.x++] = num;
      // Commenting this out improves performance, there is not much space savings.
      // } else if (num <= 0xff) {
      //   uint8[writer.x++] = 0xcc;
      //   uint8[writer.x++] = num;
    } else if (num <= 0xffff) {
      uint8[writer.x++] = 0xcd;
      writer.view.setUint16(writer.x, num);
      writer.x += 2;
    } else if (num <= 0xffffffff) {
      uint8[writer.x++] = 0xce;
      writer.view.setUint32(writer.x, num);
      writer.x += 4;
    } else Ethis.writeFloat(num);
  }
 
  public n32(num: number): void {
    const writer = this.writer;
    this.writer.ensureCapacity(5);
    const uint8 = writer.uint8;
    if (num >= -0x20) {
      uint8[writer.x++] = 0x100 + num;
      // Commenting this out improves performance, there is not much space savings.
      // } else if (num >= -0x80) {
      //   uint8[writer.x++] = 0xd0;
      //   uint8[writer.x++] = num + 0x100;
    } else if (num >= -0x8000) {
      uint8[writer.x++] = 0xd1;
      writer.view.setInt16(writer.x, num);
      writer.x += 2;
    } else if (num >= -0x80000000) {
      uint8[writer.x++] = 0xd2;
      writer.view.setInt32(writer.x, num);
      writer.x += 4;
    } else Ethis.writeFloat(num);
  }
 
  /** @deprecated */
  public encodeNumber(num: number): void {
    this.writeNumber(num);
  }
 
  public writeNumber(num: number): void {
    if (num >>> 0 === num) return this.u32(num);
    if (num >> 0 === num) return this.n32(num);
    this.writeFloat(num);
  }
 
  public writeInteger(int: number): void {
    Iif (int >= 0)
      if (int <= 0xffffffff) return this.u32(int);
      else Iif (int > -0x80000000) return this.n32(int);
    this.writeFloat(int);
  }
 
  public writeUInteger(uint: number): void {
    Iif (uint <= 0xffffffff) return this.u32(uint);
    this.writeFloat(uint);
  }
 
  public encodeNull(): void {
    this.writer.u8(0xc0);
  }
 
  public encodeTrue(): void {
    this.writer.u8(0xc3);
  }
 
  public encodeFalse(): void {
    this.writer.u8(0xc2);
  }
 
  /** @deprecated */
  public encodeBoolean(bool: boolean): void {
    this.writeBoolean(bool);
  }
 
  public writeBoolean(bool: boolean): void {
    if (bool) this.writer.u8(0xc3);
    else this.writer.u8(0xc2);
  }
 
  /** @deprecated */
  public encodeStringHeader(length: number): void {
    this.writeStrHdr(length);
  }
 
  public writeStrHdr(length: number): void {
    if (length <= 0b11111) this.writer.u8(0b10100000 | length);
    else if (length <= 0xff) this.writer.u16(0xd900 + length);
    else if (length <= 0xffff) this.writer.u8u16(0xda, length);
    else this.writer.u8u32(0xdb, length);
  }
 
  /** @deprecated */
  public encodeString(str: string) {
    this.writeStr(str);
  }
 
  public writeStr(str: string): void {
    const writer = this.writer;
    const length = str.length;
    const maxSize = length * 4;
    writer.ensureCapacity(5 + maxSize);
    const uint8 = writer.uint8;
    let lengthOffset: number = writer.x;
    if (maxSize <= 0b11111) writer.x++;
    else if (maxSize <= 0xff) {
      uint8[writer.x++] = 0xd9;
      lengthOffset = writer.x;
      writer.x++;
    } else if (maxSize <= 0xffff) {
      uint8[writer.x++] = 0xda;
      lengthOffset = writer.x;
      writer.x += 2;
    } else {
      uint8[writer.x++] = 0xdb;
      lengthOffset = writer.x;
      writer.x += 4;
    }
    const bytesWritten = this.writer.utf8(str);
    if (maxSize <= 0b11111) uint8[lengthOffset] = 0b10100000 | bytesWritten;
    else if (maxSize <= 0xff) uint8[lengthOffset] = bytesWritten;
    else if (maxSize <= 0xffff) writer.view.setUint16(lengthOffset, bytesWritten);
    else writer.view.setUint32(lengthOffset, bytesWritten);
  }
 
  /** @deprecated */
  public encodeAsciiString(str: string) {
    this.writeAsciiStr(str);
  }
 
  public writeAsciiStr(str: string): void {
    this.writeStrHdr(str.length);
    this.writer.ascii(str);
  }
 
  /** @deprecated */
  public encodeArrayHeader(length: number): void {
    this.writeArrHdr(length);
  }
 
  /** @deprecated */
  public encodeArray(arr: unknown[]): void {
    this.writeArr(arr);
  }
 
  public writeArrHdr(length: number): void {
    if (length <= 0b1111) this.writer.u8(0b10010000 | length);
    else if (length <= 0xffff) this.writer.u8u16(0xdc, length);
    else Iif (length <= 0xffffffff) this.writer.u8u32(0xdd, length);
  }
 
  public writeArr(arr: unknown[]): void {
    const length = arr.length;
    if (length <= 0b1111) this.writer.u8(0b10010000 | length);
    else if (length <= 0xffff) this.writer.u8u16(0xdc, length);
    else if (length <= 0xffffffff) this.writer.u8u32(0xdd, length);
    // else return;
    for (let i = 0; i < length; i++) this.writeAny(arr[i]);
  }
 
  /** @deprecated */
  public encodeObjectHeader(length: number): void {
    this.writeObjHdr(length);
  }
 
  /** @deprecated */
  public encodeObject(obj: Record<string, unknown>): void {
    this.writeObj(obj);
  }
 
  public writeObjHdr(length: number): void {
    if (length <= 0b1111) this.writer.u8(0b10000000 | length);
    else if (length <= 0xffff) {
      this.writer.u8u16(0xde, length);
    } else if (length <= 0xffffffff) {
      this.writer.u8u32(0xdf, length);
    }
  }
 
  public writeObj(obj: Record<string, unknown>): void {
    const keys = Object.keys(obj);
    const length = keys.length;
    this.writeObjHdr(length);
    for (let i = 0; i < length; i++) {
      const key = keys[i];
      this.writeStr(key);
      this.writeAny(obj[key]);
    }
  }
 
  public encodeExtHeader(type: number, length: number) {
    switch (length) {
      case 1:
        this.writer.u16((0xd4 << 8) | type);
        break;
      case 2:
        this.writer.u16((0xd5 << 8) | type);
        break;
      case 4:
        this.writer.u16((0xd6 << 8) | type);
        break;
      case 8:
        this.writer.u16((0xd7 << 8) | type);
        break;
      case 16:
        this.writer.u16((0xd8 << 8) | type);
        break;
      default:
        if (length <= 0xff) {
          this.writer.u16((0xc7 << 8) | length);
          this.writer.u8(type);
        } else if (length <= 0xffff) {
          this.writer.u8u16(0xc8, length);
          this.writer.u8(type);
        } else if (length <= 0xffffffff) {
          this.writer.u8u32(0xc9, length);
          this.writer.u8(type);
        }
    }
  }
 
  public encodeExt(ext: JsonPackExtension): void {
    const {tag: type, val: buf} = ext;
    const length = buf.length;
    this.encodeExtHeader(type, length);
    this.writer.buf(buf, length);
  }
 
  /** @deprecated */
  public encodeBinaryHeader(length: number): void {
    this.writeBinHdr(length);
  }
 
  /** @deprecated */
  public encodeBinary(buf: Uint8Array): void {
    this.writeBin(buf);
  }
 
  public writeBinHdr(length: number): void {
    if (length <= 0xff) this.writer.u16((0xc4 << 8) | length);
    else if (length <= 0xffff) {
      this.writer.u8u16(0xc5, length);
    } else if (length <= 0xffffffff) {
      this.writer.u8u32(0xc6, length);
    }
  }
 
  public writeBin(buf: Uint8Array): void {
    const length = buf.length;
    this.writeBinHdr(length);
    this.writer.buf(buf, length);
  }
}