All files / json-crdt-patch/codec/binary Encoder.ts

98.07% Statements 153/156
92.1% Branches 35/38
100% Functions 7/7
97.95% Lines 144/147

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 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239174x 174x 174x 174x 174x           174x 174x             174x 174x                   7865x 7865x 7865x 7865x 7865x 7865x 7865x 7865x 3x 7865x 7865x       7865x 7865x 7865x 34019x       58531x 58531x 58531x 58531x 32095x   26436x 26436x         11544x 11544x       6031x 6031x 3368x   2663x 2663x   6031x 6031x 6031x       34019x 34019x 34019x   2989x 2989x 2989x 4x 4x   2985x 2985x   2989x     758x 758x     4018x 4018x     2x 2x     3372x 3372x     766x 766x     2545x 2545x     1624x 1624x 1624x 1624x 1624x     4453x 4453x 4453x 4453x 4272x   181x 181x   4453x 4453x 11269x 11269x 11269x   4453x     1x 1x 1x 1x 1x         1x 1x 1x 1x 1x   1x     5846x 5846x 5846x 5846x 5846x 5846x 5846x 5846x 5846x 185x 185x   5846x     2273x 2273x 2273x 2273x 646x   1627x 1627x   2273x 2273x 2273x 2273x     1776x 1776x 1776x 1776x 1595x   181x 181x   1776x 1776x 4794x 1776x     3057x 3057x 3057x 3057x 2670x   387x 387x   3057x 11544x 3057x     539x 539x 539x 173x   366x 366x   539x                
import * as operations from '../../operations';
import {JsonCrdtPatchOpcodeOverlay} from '../../constants';
import {CrdtWriter} from '../../util/binary/CrdtWriter';
import {type ITimespanStruct, type ITimestampStruct, Timestamp} from '../../clock';
import {CborEncoder} from '@jsonjoy.com/json-pack/lib/cbor/CborEncoder';
import type {JsonCrdtPatchOperation, Patch} from '../../Patch';
 
/**
 * JSON CRDT Patch "binary" codec encoder.
 */
export class Encoder extends CborEncoder<CrdtWriter> {
  private patchSid: number = 0;
 
  /**
   * Creates a new encoder instance.
   *
   * @param writer An optional custom implementation of CRDT writer.
   */
  constructor(public readonly writer: CrdtWriter = new CrdtWriter()) {
    super(writer);
  }
 
  /**
   * Encodes a JSON CRDT Patch into a {@link Uint8Array} blob.
   *
   * @param patch A JSON CRDT Patch to encode.
   * @returns A {@link Uint8Array} blob containing the encoded JSON CRDT Patch.
   */
  public encode(patch: Patch): Uint8Array {
    this.writer.reset();
    const id = patch.getId()!;
    const sid = (this.patchSid = id.sid);
    const writer = this.writer;
    writer.vu57(sid);
    writer.vu57(id.time);
    const meta = patch.meta;
    if (meta === undefined) this.writeUndef();
    else this.writeArr([meta]);
    this.encodeOperations(patch);
    return writer.flush();
  }
 
  protected encodeOperations(patch: Patch): void {
    const ops = patch.ops;
    const length = ops.length;
    this.writer.vu57(length);
    for (let i = 0; i < length; i++) this.encodeOperation(ops[i]);
  }
 
  protected encodeId(id: ITimestampStruct) {
    const sessionId = id.sid;
    const time = id.time;
    const writer = this.writer;
    if (sessionId === this.patchSid) {
      writer.b1vu56(0, time);
    } else {
      writer.b1vu56(1, time);
      writer.vu57(sessionId);
    }
  }
 
  protected encodeTss(span: ITimespanStruct): void {
    this.encodeId(span);
    this.writer.vu57(span.span);
  }
 
  private writeInsStr(length: number, obj: ITimestampStruct, ref: ITimestampStruct, str: string): number {
    const writer = this.writer;
    if (length <= 0b111) {
      writer.u8(JsonCrdtPatchOpcodeOverlay.ins_str + length);
    } else {
      writer.u8(JsonCrdtPatchOpcodeOverlay.ins_str);
      writer.vu57(length);
    }
    this.encodeId(obj);
    this.encodeId(ref);
    return writer.utf8(str);
  }
 
  protected encodeOperation(op: JsonCrdtPatchOperation): void {
    const writer = this.writer;
    const constr = op.constructor;
    switch (constr) {
      case operations.NewConOp: {
        const operation = <operations.NewConOp>op;
        const val = operation.val;
        if (val instanceof Timestamp) {
          writer.u8(JsonCrdtPatchOpcodeOverlay.new_con + 1);
          this.encodeId(val);
        } else {
          writer.u8(JsonCrdtPatchOpcodeOverlay.new_con);
          this.writeAny(val);
        }
        break;
      }
      case operations.NewValOp: {
        writer.u8(JsonCrdtPatchOpcodeOverlay.new_val);
        break;
      }
      case operations.NewObjOp: {
        writer.u8(JsonCrdtPatchOpcodeOverlay.new_obj);
        break;
      }
      case operations.NewVecOp: {
        writer.u8(JsonCrdtPatchOpcodeOverlay.new_vec);
        break;
      }
      case operations.NewStrOp: {
        writer.u8(JsonCrdtPatchOpcodeOverlay.new_str);
        break;
      }
      case operations.NewBinOp: {
        writer.u8(JsonCrdtPatchOpcodeOverlay.new_bin);
        break;
      }
      case operations.NewArrOp: {
        writer.u8(JsonCrdtPatchOpcodeOverlay.new_arr);
        break;
      }
      case operations.InsValOp: {
        const operation = <operations.InsValOp>op;
        writer.u8(JsonCrdtPatchOpcodeOverlay.ins_val);
        this.encodeId(operation.obj);
        this.encodeId(operation.val);
        break;
      }
      case operations.InsObjOp: {
        const operation = <operations.InsObjOp>op;
        const data = operation.data;
        const length = data.length;
        if (length <= 0b111) {
          writer.u8(JsonCrdtPatchOpcodeOverlay.ins_obj + length);
        } else {
          writer.u8(JsonCrdtPatchOpcodeOverlay.ins_obj);
          writer.vu57(length);
        }
        this.encodeId(operation.obj);
        for (let i = 0; i < length; i++) {
          const tuple = data[i];
          this.writeStr(tuple[0]);
          this.encodeId(tuple[1]);
        }
        break;
      }
      case operations.InsVecOp: {
        const operation = <operations.InsVecOp>op;
        const data = operation.data;
        const length = data.length;
        if (length <= 0b111) {
          writer.u8(JsonCrdtPatchOpcodeOverlay.ins_vec + length);
        } else E{
          writer.u8(JsonCrdtPatchOpcodeOverlay.ins_vec);
          writer.vu57(length);
        }
        this.encodeId(operation.obj);
        for (let i = 0; i < length; i++) {
          const tuple = data[i];
          writer.u8(tuple[0]);
          this.encodeId(tuple[1]);
        }
        break;
      }
      case operations.InsStrOp: {
        const operation = <operations.InsStrOp>op;
        const obj = operation.obj;
        const ref = operation.ref;
        const str = operation.data;
        const len1 = str.length;
        writer.ensureCapacity(24 + len1 * 4);
        const x = writer.x;
        const len2 = this.writeInsStr(len1, obj, ref, str);
        if (len1 !== len2) {
          writer.x = x;
          this.writeInsStr(len2, obj, ref, str);
        }
        break;
      }
      case operations.InsBinOp: {
        const operation = <operations.InsBinOp>op;
        const buf = operation.data;
        const length = buf.length;
        if (length <= 0b111) {
          writer.u8(JsonCrdtPatchOpcodeOverlay.ins_bin + length);
        } else {
          writer.u8(JsonCrdtPatchOpcodeOverlay.ins_bin);
          writer.vu57(length);
        }
        this.encodeId(operation.obj);
        this.encodeId(operation.ref);
        writer.buf(buf, length);
        break;
      }
      case operations.InsArrOp: {
        const operation = <operations.InsArrOp>op;
        const elements = operation.data;
        const length = elements.length;
        if (length <= 0b111) {
          writer.u8(JsonCrdtPatchOpcodeOverlay.ins_arr + length);
        } else {
          writer.u8(JsonCrdtPatchOpcodeOverlay.ins_arr);
          writer.vu57(length);
        }
        this.encodeId(operation.obj);
        this.encodeId(operation.ref);
        for (let i = 0; i < length; i++) this.encodeId(elements[i]);
        break;
      }
      case operations.DelOp: {
        const operation = <operations.DelOp>op;
        const what = operation.what;
        const length = what.length;
        if (length <= 0b111) {
          writer.u8(JsonCrdtPatchOpcodeOverlay.del + length);
        } else {
          writer.u8(JsonCrdtPatchOpcodeOverlay.del);
          writer.vu57(length);
        }
        this.encodeId(operation.obj);
        for (let i = 0; i < length; i++) this.encodeTss(what[i]);
        break;
      }
      case operations.NopOp: {
        const operation = <operations.NopOp>op;
        const length = operation.len;
        if (length <= 0b111) {
          writer.u8(JsonCrdtPatchOpcodeOverlay.nop + length);
        } else {
          writer.u8(JsonCrdtPatchOpcodeOverlay.nop);
          writer.vu57(length);
        }
        break;
      }
      default: {
        throw new Error('UNKNOWN_OP');
      }
    }
  }
}