All files / json-crdt/codec/structural/verbose Decoder.ts

99.1% Statements 111/112
96.15% Branches 25/26
100% Functions 16/16
99.07% Lines 107/108

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 17013x 13x 13x 13x 13x     13x   1932x 1932x 1932x 1932x       1606x 1606x 1606x 1606x 26403x 26403x 26403x   1606x       245813x 245813x       1932x 1932x 1932x       83758x   26127x   8195x   17593x   2147x   26008x   3x   3685x           26127x 26127x 26127x 26127x 26127x 69551x 69551x   26127x 26127x       3x 3x 3x 3x 3x 3x 9x 9x 8x   3x 3x       8195x 8195x 8195x 8195x 8195x 5370x 5370x 7491x 7491x 7491x 593x   10120x 6898x       8195x 8195x       17593x 17593x 17593x 17593x 17593x 17535x 17535x 98530x 98530x 98530x 53865x   44665x 44665x       17593x 17593x       3685x 3685x 3685x 3685x 3685x 3633x 3633x 56033x 56033x 56033x 43109x   12924x 12924x 12924x       3685x 3685x       2147x 2147x 2147x 2147x 2147x       26008x 26008x 26008x 26008x 26008x      
import * as nodes from '../../../nodes';
import {fromBase64} from '@jsonjoy.com/base64/lib/fromBase64';
import {type ITimestampStruct, ts, ClockVector} from '../../../../json-crdt-patch/clock';
import {Model} from '../../../model';
import {SESSION} from '../../../../json-crdt-patch/constants';
import type * as types from './types';
 
export class Decoder {
  public decode({time, root}: types.JsonCrdtVerboseDocument): Model {
    const isServerClock = typeof time === 'number';
    const doc = isServerClock ? Model.withServerClock(time) : Model.withLogicalClock(this.cClock(time));
    this.cRoot(doc, root);
    return doc;
  }
 
  protected cClock(timestamps: types.JsonCrdtVerboseLogicalTimestamp[]): ClockVector {
    const [stamp] = timestamps;
    const vectorClock = new ClockVector(stamp[0], stamp[1]);
    const length = timestamps.length;
    for (let i = 1; i < length; i++) {
      const stamp = timestamps[i];
      const [sessionId, time] = stamp;
      vectorClock.observe(ts(sessionId, time), 1);
    }
    return vectorClock;
  }
 
  protected cTs(stamp: types.JsonCrdtVerboseTimestamp): ITimestampStruct {
    const isServerClock = typeof stamp === 'number';
    return isServerClock ? ts(SESSION.SERVER, stamp) : ts(stamp[0], stamp[1]);
  }
 
  protected cRoot(doc: Model, {value}: types.JsonCrdtVerboseVal): void {
    const val = value ? this.cNode(doc, value) : new nodes.ConNode(doc.clock.tick(0), null);
    const root = new nodes.RootNode(doc, val.id);
    doc.root = root;
  }
 
  protected cNode(doc: Model, node: types.JsonCrdtNode): nodes.JsonNode {
    switch (node.type) {
      case 'obj':
        return this.cObj(doc, node);
      case 'arr':
        return this.cArr(doc, node);
      case 'str':
        return this.cStr(doc, node);
      case 'val':
        return this.cVal(doc, node);
      case 'con':
        return this.cCon(doc, node);
      case 'vec':
        return this.cVec(doc, node);
      case 'bin':
        return this.cBin(doc, node);
    }
    throw new Error('UNKNOWN_NODE');
  }
 
  protected cObj(doc: Model, node: types.JsonCrdtVerboseObj): nodes.ObjNode {
    const id = this.cTs(node.id);
    const obj = new nodes.ObjNode(doc, id);
    const map = node.map;
    const keys = Object.keys(map);
    for (const key of keys) {
      const keyNode = map[key];
      obj.put(key, this.cNode(doc, keyNode).id);
    }
    doc.index.set(id, obj);
    return obj;
  }
 
  protected cVec(doc: Model, node: types.JsonCrdtVerboseVec): nodes.VecNode {
    const id = this.cTs(node.id);
    const obj = new nodes.VecNode(doc, id);
    const elements = obj.elements;
    const map = node.map;
    const length = map.length;
    for (let i = 0; i < length; i++) {
      const component = map[i];
      if (!component) elements.push(undefined);
      else elements.push(this.cNode(doc, component).id);
    }
    doc.index.set(id, obj);
    return obj;
  }
 
  protected cArr(doc: Model, node: types.JsonCrdtVerboseArr): nodes.ArrNode {
    const id = this.cTs(node.id);
    const rga = new nodes.ArrNode(doc, id);
    const chunks = node.chunks;
    const length = chunks.length;
    if (length) {
      let i = 0;
      rga.ingest(length, () => {
        const c = chunks[i++];
        const id = this.cTs(c.id);
        if (typeof (c as types.JsonCrdtVerboseTombstone).span === 'number')
          return new nodes.ArrChunk(id, (c as types.JsonCrdtVerboseTombstone).span, undefined);
        else {
          const ids = (c as types.JsonCrdtVerboseArrChunk).value.map((n) => this.cNode(doc, n).id);
          return new nodes.ArrChunk(id, ids.length, ids);
        }
      });
    }
    doc.index.set(id, rga);
    return rga;
  }
 
  protected cStr(doc: Model, node: types.JsonCrdtVerboseStr): nodes.StrNode {
    const id = this.cTs(node.id);
    const rga = new nodes.StrNode(id);
    const chunks = node.chunks;
    const length = chunks.length;
    if (length) {
      let i = 0;
      rga.ingest(length, () => {
        const c = chunks[i++];
        const id = this.cTs(c.id);
        if (typeof (c as types.JsonCrdtVerboseTombstone).span === 'number')
          return new nodes.StrChunk(id, (c as types.JsonCrdtVerboseTombstone).span, '');
        else {
          const value = (c as types.JsonCrdtVerboseStrChunk).value;
          return new nodes.StrChunk(id, value.length, value);
        }
      });
    }
    doc.index.set(id, rga);
    return rga;
  }
 
  protected cBin(doc: Model, node: types.JsonCrdtVerboseBin): nodes.BinNode {
    const id = this.cTs(node.id);
    const rga = new nodes.BinNode(id);
    const chunks = node.chunks;
    const length = chunks.length;
    if (length) {
      let i = 0;
      rga.ingest(length, () => {
        const c = chunks[i++];
        const id = this.cTs(c.id);
        if (typeof (c as types.JsonCrdtVerboseTombstone).span === 'number')
          return new nodes.BinChunk(id, (c as types.JsonCrdtVerboseTombstone).span, undefined);
        else {
          const value = (c as types.JsonCrdtVerboseBinChunk).value;
          const buf = fromBase64(value);
          return new nodes.BinChunk(id, buf.length, buf);
        }
      });
    }
    doc.index.set(id, rga);
    return rga;
  }
 
  protected cVal(doc: Model, node: types.JsonCrdtVerboseVal): nodes.ValNode {
    const id = this.cTs(node.id);
    const val = this.cNode(doc, node.value);
    const obj = new nodes.ValNode(doc, id, val.id);
    doc.index.set(id, obj);
    return obj;
  }
 
  protected cCon(doc: Model, node: types.JsonCrdtVerboseCon): nodes.ConNode {
    const id = this.cTs(node.id);
    const val = node.timestamp ? this.cTs(node.value as types.JsonCrdtVerboseLogicalTimestamp) : node.value;
    const obj = new nodes.ConNode(id, val);
    doc.index.set(id, obj);
    return obj;
  }
}