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

98.26% Statements 113/115
96% Branches 24/25
100% Functions 14/14
98.13% Lines 105/107

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 16718x 18x 18x 18x 18x     18x         2491x 2491x 2491x 329x   2162x   2491x     2491x 2491x 2491x       293462x 22618x   270844x 270844x 270844x               116973x   34025x   2200x   36845x   6x   26579x   4461x   12857x           34025x 34025x 34025x 10135x 10135x 2x   34025x 34025x 34025x       2200x 2200x 2200x 2200x 2200x       36845x 36845x 36845x 36845x 36845x 36845x 98317x 98317x 98317x   36845x 36845x       6x 6x 6x 6x 6x 6x 18x 18x   16x 16x     6x 6x       26579x 26579x 26579x 26579x 26579x 26579x 118675x 118675x 118675x 118675x 60357x   26579x 26579x       4461x 4461x 4461x 4461x 4461x 4461x 47506x 47506x 47506x 47506x 13191x   4461x 4461x       12857x 12857x 12857x 12857x 12857x 12857x 10306x 10306x 10306x 10306x 13993x 9530x   12857x 12857x      
import * as nodes from '../../../nodes';
import {ClockDecoder} from '../../../../json-crdt-patch/codec/clock/ClockDecoder';
import {type ITimestampStruct, Timestamp} from '../../../../json-crdt-patch/clock';
import {Model, UNDEFINED} from '../../../model/Model';
import {JsonCrdtDataType, SESSION} from '../../../../json-crdt-patch/constants';
import type * as t from './types';
 
export class Decoder {
  protected time?: number;
  protected clockDecoder?: ClockDecoder;
 
  public decode(doc: t.JsonCrdtCompactDocument): Model {
    const [time, root] = doc;
    const isServerTime = typeof time === 'number';
    if (isServerTime) {
      this.time = time;
    } else {
      this.clockDecoder = ClockDecoder.fromArr(time as number[]);
    }
    const model = isServerTime
      ? Model.withServerClock(time as number)
      : Model.withLogicalClock(this.clockDecoder!.clock);
    const val = root ? this.decNode(model, root) : UNDEFINED;
    model.root = new nodes.RootNode(model, val.id);
    return model;
  }
 
  protected ts(x: t.JsonCrdtCompactTimestamp): ITimestampStruct {
    if (typeof x === 'number') {
      return new Timestamp(SESSION.SERVER, this.time! - x);
    } else {
      const [sid, time] = x as [number, number];
      if (sid < 0) {
        return this.clockDecoder!.decodeId(-sid, time);
      } else E{
        return new Timestamp(sid, time);
      }
    }
  }
 
  protected decNode(model: Model, node: t.JsonCrdtCompactNode): nodes.JsonNode {
    switch (node[0]) {
      case JsonCrdtDataType.con:
        return this.decCon(model, node);
      case JsonCrdtDataType.val:
        return this.decVal(model, node);
      case JsonCrdtDataType.obj:
        return this.decObj(model, node);
      case JsonCrdtDataType.vec:
        return this.decVec(model, node);
      case JsonCrdtDataType.str:
        return this.decStr(model, node);
      case JsonCrdtDataType.bin:
        return this.decBin(model, node);
      case JsonCrdtDataType.arr:
        return this.decArr(model, node);
    }
    throw new Error('UNKNOWN_NODE');
  }
 
  protected decCon(doc: Model, node: t.JsonCrdtCompactCon): nodes.ConNode {
    const id = this.ts(node[1]);
    let data: unknown | undefined | Timestamp = node[2];
    if (node.length > 3) {
      const specialData = node[3] as unknown;
      if (!specialData) data = undefined;
      else data = this.ts(specialData as t.JsonCrdtCompactTimestamp);
    }
    const obj = new nodes.ConNode(id, data);
    doc.index.set(id, obj);
    return obj;
  }
 
  protected decVal(doc: Model, node: t.JsonCrdtCompactVal): nodes.ValNode {
    const id = this.ts(node[1]);
    const child = this.decNode(doc, node[2]);
    const obj = new nodes.ValNode(doc, id, child.id);
    doc.index.set(id, obj);
    return obj;
  }
 
  protected decObj(model: Model, node: t.JsonCrdtCompactObj): nodes.ObjNode {
    const id = this.ts(node[1]);
    const obj = new nodes.ObjNode(model, id);
    const map = node[2] as t.JsonCrdtCompactObj[2];
    const keys = Object.keys(map);
    const length = keys.length;
    for (let i = 0; i < length; i++) {
      const key = keys[i];
      const val = this.decNode(model, map[key]);
      obj.put(key, val.id);
    }
    model.index.set(id, obj);
    return obj;
  }
 
  protected decVec(model: Model, node: t.JsonCrdtCompactVec): nodes.VecNode {
    const id = this.ts(node[1]);
    const obj = new nodes.VecNode(model, id);
    const map = node[2] as t.JsonCrdtCompactVec[2];
    const elements = obj.elements;
    const length = map.length;
    for (let i = 0; i < length; i++) {
      const item = map[i];
      if (!item) elements.push(undefined);
      else {
        const child = this.decNode(model, item);
        elements.push(child.id);
      }
    }
    model.index.set(id, obj);
    return obj;
  }
 
  protected decStr(doc: Model, node: t.JsonCrdtCompactStr): nodes.StrNode {
    const id = this.ts(node[1]);
    const obj = new nodes.StrNode(id);
    const chunks = node[2] as t.JsonCrdtCompactStr[2];
    const size = chunks.length;
    let i = 0;
    obj.ingest(size, () => {
      const chunk = chunks[i++];
      const chunkId = this.ts(chunk[0]);
      const content = chunk[1];
      if (typeof content === 'number') return new nodes.StrChunk(chunkId, content, '');
      return new nodes.StrChunk(chunkId, (content as string).length, content as string);
    });
    doc.index.set(id, obj);
    return obj;
  }
 
  protected decBin(doc: Model, node: t.JsonCrdtCompactBin): nodes.BinNode {
    const id = this.ts(node[1]);
    const obj = new nodes.BinNode(id);
    const chunks = node[2] as t.JsonCrdtCompactBin[2];
    const size = chunks.length;
    let i = 0;
    obj.ingest(size, () => {
      const chunk = chunks[i++];
      const chunkId = this.ts(chunk[0]);
      const content = chunk[1];
      if (typeof content === 'number') return new nodes.BinChunk(chunkId, content, undefined);
      return new nodes.BinChunk(chunkId, content.length, content);
    });
    doc.index.set(id, obj);
    return obj;
  }
 
  protected decArr(doc: Model, node: t.JsonCrdtCompactArr): nodes.ArrNode {
    const id = this.ts(node[1]);
    const obj = new nodes.ArrNode(doc, id);
    const chunks = node[2] as t.JsonCrdtCompactArr[2];
    const size = chunks.length;
    let i = 0;
    obj.ingest(size, () => {
      const chunk = chunks[i++];
      const chunkId = this.ts(chunk[0]);
      const content = chunk[1];
      if (typeof content === 'number') return new nodes.ArrChunk(chunkId, content, undefined);
      const ids = (content as t.JsonCrdtCompactNode[]).map((c) => this.decNode(doc, c).id);
      return new nodes.ArrChunk(chunkId, content.length, ids);
    });
    doc.index.set(id, obj);
    return obj;
  }
}