All files / json-pack/src/ubjson UbjsonEncoder.ts

86.25% Statements 113/131
94.73% Branches 36/38
51.72% Functions 15/29
86.06% Lines 105/122

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      4x 4x     1197x 1197x 1197x 1197x       37099x   2673x   13222x   11515x   9677x 8305x 8305x   3412x   18x   4875x       10x   2x             1372x       2x       2673x       13222x 12090x       1150x 1150x 742x 162x 162x 580x 332x 332x 332x 332x 248x 248x 248x 248x 248x                   12090x 12090x 12090x 12090x 12090x 12090x 12090x       10x 10x 10x 10x 10x 10x 10x       18x 18x 18x 18x 18x       11515x 11515x 11515x 11515x 11515x 11515x 11515x 11515x 11515x 11515x 11504x 11504x   11x 11x   11515x 11515x 11x               3412x 3412x 3412x 9784x 3412x       4875x 4875x 4875x 4875x 4875x 26118x 26118x 26118x 26118x   4875x       26118x 26118x 26118x 26118x 26118x 26118x 26118x 26118x 26118x 26114x 26114x   4x 4x   26118x 26118x 4x                                                                                                            
import type {IWriter, IWriterGrowable} from '@jsonjoy.com/buffers/lib';
import type {BinaryJsonEncoder, StreamingBinaryJsonEncoder} from '../types';
 
export class UbjsonEncoder implements BinaryJsonEncoder, StreamingBinaryJsonEncoder {
  constructor(public readonly writer: IWriter & IWriterGrowable) {}
 
  public encode(value: unknown): Uint8Array {
    const writer = this.writer;
    writer.reset();
    this.writeAny(value);
    return writer.flush();
  }
 
  public writeAny(value: unknown): void {
    switch (typeof value) {
      case 'boolean':
        return this.writeBoolean(value);
      case 'number':
        return this.writeNumber(value as number);
      case 'string':
        return this.writeStr(value);
      case 'object': {
        if (value === null) return this.writeNull();
        const construct = value.constructor;
        switch (construct) {
          case Array:
            return this.writeArr(value as unknown[]);
          case Uint8Array:
            return this.writeBin(value as Uint8Array);
          default:
            return this.writeObj(value as Record<string, unknown>);
        }
      }
      case 'bigint':
        return this.writeBigInt(value as bigint);
      case 'undefined':
        return this.writeUndef();
      default:
        return this.writeNull();
    }
  }
 
  public writeNull(): void {
    this.writer.u8(0x5a);
  }
 
  public writeUndef(): void {
    this.writer.u8(0x4e);
  }
 
  public writeBoolean(bool: boolean): void {
    this.writer.u8(bool ? 0x54 : 0x46);
  }
 
  public writeNumber(num: number): void {
    if (num >> 0 === num) return this.writeInteger(num);
    this.writeFloat(num);
  }
 
  public writeInteger(int: number): void {
    const writer = this.writer;
    if (int <= 0xff && 0 <= int) writer.u16(0x5500 | int);
    else if (int <= 127 && -128 <= int) {
      writer.u16(0x6900);
      writer.view.setInt8(writer.x - 1, int);
    } else if (int <= 32767 && -32768 <= int) {
      writer.ensureCapacity(3);
      writer.u8(0x49);
      writer.view.setInt16(writer.x, int, false);
      writer.x += 2;
    } else if (int <= 2147483647 && -2147483648 <= int) {
      writer.ensureCapacity(5);
      writer.u8(0x6c);
      writer.view.setInt32(writer.x, int, false);
      writer.x += 4;
    }
  }
 
  public writeUInteger(uint: number): void {
    const writer = this.writer;
    Iif (uint < 0xff) writer.u16(0x5500 + uint);
  }
 
  public writeFloat(float: number): void {
    const writer = this.writer;
    writer.ensureCapacity(9);
    const view = writer.view;
    const x = writer.x;
    view.setUint8(x, 0x44);
    view.setFloat64(x + 1, float, false);
    writer.x = x + 9;
  }
 
  public writeBigInt(int: bigint): void {
    const writer = this.writer;
    writer.ensureCapacity(9);
    const view = writer.view;
    const x = writer.x;
    view.setUint8(x, 0x4c);
    view.setBigInt64(x + 1, int, false);
    writer.x = x + 9;
  }
 
  public writeBin(buf: Uint8Array): void {
    const writer = this.writer;
    const length = buf.length;
    writer.u32(0x5b_24_55_23); // "[$U#"
    this.writeInteger(length);
    writer.buf(buf, length);
  }
 
  public writeStr(str: string): void {
    const length = str.length;
    const maxLength = length * 4;
    const capacity = maxLength + 1 + 5; // 1 for string type, 5 for length.
    const writer = this.writer;
    writer.ensureCapacity(capacity);
    const uint8 = writer.uint8;
    uint8[writer.x++] = 0x53;
    const x = writer.x;
    const oneByteLength = maxLength < 0xff;
    if (oneByteLength) {
      uint8[writer.x++] = 0x55;
      writer.x++;
    } else {
      uint8[writer.x++] = 0x6c;
      writer.x += 4;
    }
    const size = writer.utf8(str);
    if (oneByteLength) uint8[x + 1] = size;
    else writer.view.setUint32(x + 1, size);
  }
 
  public writeAsciiStr(str: string): void {
    this.writeStr(str);
  }
 
  public writeArr(arr: unknown[]): void {
    const writer = this.writer;
    writer.u8(0x5b);
    const length = arr.length;
    for (let i = 0; i < length; i++) this.writeAny(arr[i]);
    writer.u8(0x5d);
  }
 
  public writeObj(obj: Record<string, unknown>): void {
    const writer = this.writer;
    const keys = Object.keys(obj);
    const length = keys.length;
    writer.u8(0x7b);
    for (let i = 0; i < length; i++) {
      const key = keys[i];
      const value = obj[key];
      this.writeKey(key);
      this.writeAny(value);
    }
    writer.u8(0x7d);
  }
 
  public writeKey(str: string): void {
    const length = str.length;
    const maxLength = length * 4;
    const capacity = maxLength + 5; // 5 for int.
    const writer = this.writer;
    writer.ensureCapacity(capacity);
    const uint8 = writer.uint8;
    const x = writer.x;
    const oneByteLength = maxLength < 0xff;
    if (oneByteLength) {
      uint8[writer.x++] = 0x55;
      writer.x++;
    } else {
      uint8[writer.x++] = 0x6c;
      writer.x += 4;
    }
    const size = writer.utf8(str);
    if (oneByteLength) uint8[x + 1] = size;
    else writer.view.setUint32(x + 1, size);
  }
 
  // ------------------------------------------------------- Streaming encoding
 
  public writeStartStr(): void {
    throw new Error('Method not implemented.');
  }
 
  public writeStrChunk(str: string): void {
    throw new Error('Method not implemented.');
  }
 
  public writeEndStr(): void {
    throw new Error('Method not implemented.');
  }
 
  public writeStartBin(): void {
    throw new Error('Method not implemented.');
  }
 
  public writeBinChunk(buf: Uint8Array): void {
    throw new Error('Method not implemented.');
  }
 
  public writeEndBin(): void {
    throw new Error('Method not implemented.');
  }
 
  public writeStartArr(): void {
    this.writer.u8(0x5b);
  }
 
  public writeArrChunk(item: unknown): void {
    this.writeAny(item);
  }
 
  public writeEndArr(): void {
    this.writer.u8(0x5d);
  }
 
  public writeStartObj(): void {
    this.writer.u8(0x7b);
  }
 
  public writeObjChunk(key: string, value: unknown): void {
    this.writeKey(key);
    this.writeAny(value);
  }
 
  public writeEndObj(): void {
    this.writer.u8(0x7d);
  }
}