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 425x 425x 425x   217x       15694x 15694x 15694x       217x 217x 217x       10550x 10550x 10550x 10550x 10550x 10550x 10550x   2020x   211x   3422x   6x   2762x   161x   1968x           2020x 2020x 2020x 2020x       211x 211x 211x 211x 211x       3422x 3422x 3422x 3422x 3422x 3422x 8168x 8168x 8168x   3422x 3422x       6x 6x 6x 6x 6x 16x 16x 16x 14x   6x 6x       2762x 2762x 2762x 2762x 2762x 3468x 3468x 3468x 3351x 3351x 3351x   2762x 2762x       161x 161x 161x 161x 161x 279x 279x 279x 257x 257x 257x   161x 161x       1968x 1968x 1968x 1968x 1968x 1396x 1396x 1396x 1363x 1938x 1363x   1968x 1968x      
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;
  }
}