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

99.15% Statements 118/119
96.55% Branches 28/29
100% Functions 16/16
99.13% Lines 115/116

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 18213x 13x 13x 13x       13x   1886x 1886x 1886x 1886x       1560x 1560x 1560x 1560x 28452x 28452x 28452x   1560x       233497x 233497x       1886x 1886x 1886x 1886x       79160x   24839x   8522x   17494x   1412x   23895x   3x   2995x           24839x 24839x 24839x 24839x 24839x 65126x 65126x 65126x 65126x   24839x 24839x       3x 3x 3x 3x 3x 3x 9x 9x   8x 8x 8x     3x 3x       8522x 8522x 8522x 8522x 8522x 5683x 5683x 8082x 8082x 8082x 484x   7598x 10728x 10728x 10728x   7598x       8522x 8522x       17494x 17494x 17494x 17494x 17494x 17439x 17439x 98806x 98806x 98806x 52069x   46737x 46737x       17494x 17494x       2995x 2995x 2995x 2995x 2995x 2942x 2942x 47448x 47448x 47448x 37158x   10290x 10290x 10290x       2995x 2995x       1412x 1412x 1412x 1412x 1412x 1412x       23895x 23895x 23895x 23895x 23895x      
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(void 0, time) : Model.create(void 0, 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);
    val.parent = root;
    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];
      const child = this.cNode(doc, keyNode);
      child.parent = obj;
      obj.put(key, child.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 {
        const child = this.cNode(doc, component);
        child.parent = obj;
        elements.push(child.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) => {
            const child = this.cNode(doc, n);
            child.parent = rga;
            return child.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 child = this.cNode(doc, node.value);
    const obj = new nodes.ValNode(doc, id, child.id);
    child.parent = obj;
    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;
  }
}