All files / json-crdt/codec/sidecar/binary Decoder.ts

94.96% Statements 132/139
75% Branches 24/32
100% Functions 16/16
99.19% Lines 123/124

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 1805x 5x   5x 5x 5x 5x 5x     5x   159x 159x 159x     217x 217x 217x 217x 217x 217x 217x 217x 217x       217x 217x 217x 217x 217x 217x 217x 217x 217x 368x 368x 368x   217x       14409x 14409x 14409x       217x 217x 217x       9644x 9644x 9644x 9644x 9644x 9644x 9644x   1691x   220x   3150x   6x   2541x   117x   1919x           1691x 1691x 1691x 1691x       220x 220x 220x 220x 220x       3150x 3150x 3150x 3150x 3150x 3150x 7285x 7285x 7285x   3150x 3150x       6x 6x 6x 6x 6x 16x 16x 16x 14x   6x 6x       2541x 2541x 2541x 2541x 2541x 3200x 3200x 3200x 3095x 3095x 3095x   2541x 2541x       117x 117x 117x 117x 117x 210x 210x 210x 199x 199x 199x   117x 117x       1919x 1919x 1919x 1919x 1919x 1354x 1354x 1354x 1313x 1906x 1313x   1919x 1919x      
import {ClockDecoder} from '../../../../json-crdt-patch/codec/clock/ClockDecoder';
import {CrdtReader} from '../../../../json-crdt-patch/util/binary/CrdtReader';
import type {ITimestampStruct} from '../../../../json-crdt-patch/clock';
import {Model, UNDEFINED} from '../../../model/Model';
import {CborDecoderBase} from '@jsonjoy.com/json-pack/lib/cbor/CborDecoderBase';
import * as nodes from '../../../nodes';
import {CRDT_MAJOR} from '../../structural/binary/constants';
import {sort} from '@jsonjoy.com/util/lib/sort/insertion';
import {SESSION} from '../../../../json-crdt-patch/constants';
 
export class Decoder {
  protected doc!: Model;
  protected clockDecoder?: ClockDecoder = undefined;
  protected time: number = -1;
  protected readonly decoder: CborDecoderBase<CrdtReader> = new CborDecoderBase<CrdtReader>(new CrdtReader());
 
  public decode(view: unknown, meta: Uint8Array): Model {
    this.clockDecoder = undefined;
    this.time = -1;
    this.decoder.reader.reset(meta);
    this.decodeClockTable();
    const clock = this.clockDecoder!.clock;
    this.doc = Model.create(void 0, clock);
    this.doc.root = new nodes.RootNode(this.doc, this.cRoot(view).id);
    this.clockDecoder = undefined;
    return this.doc;
  }
 
  protected decodeClockTable(): void {
    const reader = this.decoder.reader;
    const clockTableOffset = reader.u32();
    const offset = reader.x;
    reader.x += clockTableOffset;
    const length = reader.vu57();
    const sessionId = reader.vu57();
    const time = reader.vu57();
    this.clockDecoder = new ClockDecoder(sessionId, time);
    for (let i = 1; i < length; i++) {
      const sid = reader.vu57();
      const time = reader.vu57();
      this.clockDecoder.pushTuple(sid, time);
    }
    reader.x = offset;
  }
 
  protected ts(): ITimestampStruct {
    const decoderTime = this.time!;
    const [sessionIndex, timeDiff] = this.decoder.reader.id();
    return this.clockDecoder!.decodeId(sessionIndex, timeDiff);
  }
 
  protected cRoot(view: unknown): nodes.JsonNode {
    const reader = this.decoder.reader;
    const peek = reader.uint8[reader.x];
    return !peek ? UNDEFINED : this.cNode(view);
  }
 
  protected cNode(view: unknown): nodes.JsonNode {
    const reader = this.decoder.reader;
    const id = this.ts();
    const octet = reader.u8();
    const major = octet >> 5;
    const minor = octet & 0b11111;
    const length = minor < 24 ? minor : minor === 24 ? reader.u8() : minor === 25 ? reader.u16() : reader.u32();
    switch (major) {
      case CRDT_MAJOR.CON:
        return this.cCon(view, id, length);
      case CRDT_MAJOR.VAL:
        return this.cVal(view, id);
      case CRDT_MAJOR.OBJ:
        return this.cObj(view, id, length);
      case CRDT_MAJOR.VEC:
        return this.cVec(view, id, length);
      case CRDT_MAJOR.STR:
        return this.cStr(view, id, length);
      case CRDT_MAJOR.BIN:
        return this.cBin(view, id, length);
      case CRDT_MAJOR.ARR:
        return this.cArr(view, id, length);
    }
    throw new Error('UNKNOWN_NODE');
  }
 
  protected cCon(view: unknown, id: ITimestampStruct, length: number): nodes.ConNode {
    const doc = this.doc;
    const node = new nodes.ConNode(id, length ? this.ts() : view);
    doc.index.set(id, node);
    return node;
  }
 
  protected cVal(view: unknown, id: ITimestampStruct): nodes.ValNode {
    const child = this.cNode(view);
    const doc = this.doc;
    const node = new nodes.ValNode(doc, id, child.id);
    doc.index.set(id, node);
    return node;
  }
 
  protected cObj(view: unknown, id: ITimestampStruct, length: number): nodes.ObjNode {
    const obj = new nodes.ObjNode(this.doc, id);
    Iif (!view || typeof view !== 'object') throw new Error('INVALID_OBJ');
    const keys = sort(Object.keys(view));
    Iif (keys.length !== length) throw new Error('INVALID_OBJ');
    const objKeys = obj.keys;
    for (let i = 0; i < length; i++) {
      const key = keys[i];
      const childNode = this.cNode((view as any)[key]);
      objKeys.set(key, childNode.id);
    }
    this.doc.index.set(id, obj);
    return obj;
  }
 
  protected cVec(view: unknown, id: ITimestampStruct, length: number): nodes.VecNode {
    const obj = new nodes.VecNode(this.doc, id);
    Iif (!Array.isArray(view) || view.length !== length) throw new Error('INVALID_VEC');
    const elements = obj.elements;
    const reader = this.decoder.reader;
    for (let i = 0; i < length; i++) {
      const child = this.cNode(view[i]);
      const childId = child.id;
      if (childId.sid === SESSION.SYSTEM) elements.push(undefined);
      else elements.push(childId);
    }
    this.doc.index.set(id, obj);
    return obj;
  }
 
  protected cStr(view: unknown, id: ITimestampStruct, length: number): nodes.StrNode {
    Iif (typeof view !== 'string') throw new Error('INVALID_STR');
    const node = new nodes.StrNode(id);
    const reader = this.decoder.reader;
    let offset = 0;
    node.ingest(length, (): nodes.StrChunk => {
      const id = this.ts();
      const [deleted, span] = reader.b1vu56();
      if (deleted) return new nodes.StrChunk(id, span, '');
      const text = view.slice(offset, offset + span);
      offset += span;
      return new nodes.StrChunk(id, text.length, text);
    });
    this.doc.index.set(id, node);
    return node;
  }
 
  protected cBin(view: unknown, id: ITimestampStruct, length: number): nodes.BinNode {
    Iif (!(view instanceof Uint8Array)) throw new Error('INVALID_BIN');
    const node = new nodes.BinNode(id);
    const reader = this.decoder.reader;
    let offset = 0;
    node.ingest(length, (): nodes.BinChunk => {
      const id = this.ts();
      const [deleted, span] = reader.b1vu56();
      if (deleted) return new nodes.BinChunk(id, span, undefined);
      const slice = view.slice(offset, offset + span);
      offset += span;
      return new nodes.BinChunk(id, slice.length, slice);
    });
    this.doc.index.set(id, node);
    return node;
  }
 
  protected cArr(view: unknown, id: ITimestampStruct, length: number): nodes.ArrNode {
    Iif (!Array.isArray(view)) throw new Error('INVALID_ARR');
    const obj = new nodes.ArrNode(this.doc, id);
    const reader = this.decoder.reader;
    let i = 0;
    obj.ingest(length, (): nodes.ArrChunk => {
      const id = this.ts();
      const [deleted, span] = reader.b1vu56();
      if (deleted) return new nodes.ArrChunk(id, span, undefined);
      const ids: ITimestampStruct[] = [];
      for (let j = 0; j < span; j++) ids.push(this.cNode(view[i++]).id);
      return new nodes.ArrChunk(id, span, ids);
    });
    this.doc.index.set(id, obj);
    return obj;
  }
}