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

98.23% Statements 111/113
89.65% Branches 26/29
100% Functions 15/15
99.02% Lines 102/103

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 17711x 11x 11x   11x 11x 11x 11x   11x           150x                 1658x 1658x 1658x 1658x                   1660x 1660x 1660x 1660x 1660x 1660x 1658x 1658x 1658x   1660x 1660x 1660x 1660x 63764x 63764x 60448x 60448x 60448x 60448x 60448x   1660x       216718x 216718x       60448x 60448x 60448x 60448x 60448x 60448x   20790x   742x   18528x   1x   12737x   3124x   4526x           20790x 20790x 20790x 20790x       742x 742x 742x       18528x 18528x 18528x 18528x 52104x 52104x 52104x   18528x       1x 1x 1x 1x 1x 1x 1x   1x       12737x 12737x 12737x     150x 96228x 96228x 96228x 96228x 96228x 53159x       3124x 3124x 3124x     150x 55022x 55022x 55022x 55022x 12419x       4526x 4526x 4526x     150x 5015x 5015x 5015x 5015x   4376x 5947x 4376x        
import * as nodes from '../../../nodes';
import {ClockTable} from '../../../../json-crdt-patch/codec/clock/ClockTable';
import {CrdtReader} from '../../../../json-crdt-patch/util/binary/CrdtReader';
import type {IndexedFields, FieldName, IndexedNodeFields} from './types';
import {type ITimestampStruct, type IClockVector, Timestamp, ClockVector} from '../../../../json-crdt-patch/clock';
import {Model, UNDEFINED} from '../../../model/Model';
import {CborDecoderBase} from '@jsonjoy.com/json-pack/lib/cbor/CborDecoderBase';
import {CRDT_MAJOR} from '../../structural/binary/constants';
 
export class Decoder {
  public readonly dec: CborDecoderBase<CrdtReader>;
  protected doc!: Model;
  protected clockTable?: ClockTable;
 
  constructor(reader?: CrdtReader) {
    this.dec = new CborDecoderBase<CrdtReader>(reader || new CrdtReader());
  }
 
  public decode<M extends Model>(
    fields: IndexedFields,
    ModelConstructor: new (clock: IClockVector) => M = Model as unknown as new (
      clock: IClockVector,
    ) => M,
  ): M {
    const reader = this.dec.reader;
    reader.reset(fields.c);
    const clockTable = (this.clockTable = ClockTable.decode(reader));
    return this.decodeFields(clockTable, fields, ModelConstructor);
  }
 
  public decodeFields<M extends Model>(
    clockTable: ClockTable,
    fields: IndexedNodeFields,
    ModelConstructor: new (clock: IClockVector) => M = Model as unknown as new (
      clock: IClockVector,
    ) => M,
  ): M {
    const reader = this.dec.reader;
    const firstClock = clockTable.byIdx[0];
    const vectorClock = new ClockVector(firstClock.sid, firstClock.time + 1);
    const doc = (this.doc = new ModelConstructor(vectorClock));
    const root = fields.r;
    if (root && root.length) {
      reader.reset(root);
      const rootValue = this.ts();
      doc.root.set(rootValue);
    }
    const index = doc.index;
    const keys = Object.keys(fields);
    const length = keys.length;
    for (let i = 0; i < length; i++) {
      const field = keys[i];
      if (field.length < 3) continue; // Skip "c" and "r".
      const arr = fields[field as FieldName];
      const id = clockTable.parseField(field as FieldName);
      reader.reset(arr);
      const node = this.decodeNode(id);
      index.set(id, node);
    }
    return doc;
  }
 
  protected ts(): ITimestampStruct {
    const [sessionIndex, timeDiff] = this.dec.reader.id();
    return new Timestamp(this.clockTable!.byIdx[sessionIndex].sid, timeDiff);
  }
 
  protected decodeNode(id: ITimestampStruct): nodes.JsonNode {
    const reader = this.dec.reader;
    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.decodeCon(id, length);
      case CRDT_MAJOR.VAL:
        return this.decodeVal(id);
      case CRDT_MAJOR.OBJ:
        return this.decodeObj(id, length);
      case CRDT_MAJOR.VEC:
        return this.decodeVec(id, length);
      case CRDT_MAJOR.STR:
        return this.decodeStr(id, length);
      case CRDT_MAJOR.BIN:
        return this.decodeBin(id, length);
      case CRDT_MAJOR.ARR:
        return this.decodeArr(id, length);
    }
    return UNDEFINED;
  }
 
  public decodeCon(id: ITimestampStruct, length: number): nodes.ConNode {
    const decoder = this.dec;
    const data = !length ? decoder.val() : this.ts();
    const node = new nodes.ConNode(id, data);
    return node;
  }
 
  public decodeVal(id: ITimestampStruct): nodes.ValNode {
    const val = this.ts();
    const node = new nodes.ValNode(this.doc, id, val);
    return node;
  }
 
  public decodeObj(id: ITimestampStruct, length: number): nodes.ObjNode {
    const decoder = this.dec;
    const obj = new nodes.ObjNode(this.doc, id);
    const keys = obj.keys;
    for (let i = 0; i < length; i++) {
      const key = decoder.val() + '';
      const val = this.ts();
      keys.set(key, val);
    }
    return obj;
  }
 
  public decodeVec(id: ITimestampStruct, length: number): nodes.VecNode {
    const reader = this.dec.reader;
    const node = new nodes.VecNode(this.doc, id);
    const elements = node.elements;
    for (let i = 0; i < length; i++) {
      const octet = reader.u8();
      Iif (!octet) elements.push(undefined);
      else elements.push(this.ts());
    }
    return node;
  }
 
  protected decodeStr(id: ITimestampStruct, length: number): nodes.StrNode {
    const node = new nodes.StrNode(id);
    node.ingest(length, this.decodeStrChunk);
    return node;
  }
 
  private decodeStrChunk = (): nodes.StrChunk => {
    const decoder = this.dec;
    const reader = decoder.reader;
    const id = this.ts();
    const val = decoder.val();
    if (typeof val === 'string') return new nodes.StrChunk(id, val.length, val);
    return new nodes.StrChunk(id, ~~(<number>val), '');
  };
 
  protected decodeBin(id: ITimestampStruct, length: number): nodes.BinNode {
    const node = new nodes.BinNode(id);
    node.ingest(length, this.decodeBinChunk);
    return node;
  }
 
  private decodeBinChunk = (): nodes.BinChunk => {
    const id = this.ts();
    const reader = this.dec.reader;
    const [deleted, length] = reader.b1vu56();
    if (deleted) return new nodes.BinChunk(id, length, undefined);
    return new nodes.BinChunk(id, length, reader.buf(length));
  };
 
  protected decodeArr(id: ITimestampStruct, length: number): nodes.ArrNode {
    const node = new nodes.ArrNode(this.doc, id);
    node.ingest(length, this.decodeArrChunk);
    return node;
  }
 
  private decodeArrChunk = (): nodes.ArrChunk => {
    const id = this.ts();
    const reader = this.dec.reader;
    const [deleted, length] = reader.b1vu56();
    if (deleted) return new nodes.ArrChunk(id, length, undefined);
    else {
      const data: ITimestampStruct[] = [];
      for (let i = 0; i < length; i++) data.push(this.ts());
      return new nodes.ArrChunk(id, length, data);
    }
  };
}