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

98.13% Statements 158/161
92.3% Branches 36/39
100% Functions 7/7
98.02% Lines 149/152

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 239 240 241 242 243 244 245 246 247193x   193x 193x 193x           193x 193x             193x 193x                   8024x 8024x 8024x 8024x 8024x 8024x 8024x 8024x 3x 8024x 8024x       8024x 8024x 8024x 33574x       59254x 59254x 59254x 59254x 31726x   27528x 27528x         12078x 12078x       5929x 5929x 3287x   2642x 2642x   5929x 5929x 5929x       33574x 33574x 33574x   2622x 2622x 2622x 4x 4x   2618x 2618x   2622x     738x 738x     3964x 3964x     2x 2x     3256x 3256x     792x 792x     2442x 2442x     1603x 1603x 1603x 1603x 1603x     4498x 4498x 4498x 4498x 4334x   164x 164x   4498x 4498x 11363x 11363x 11363x   4498x     1x 1x 1x 1x 1x         1x 1x 1x 1x 1x   1x     5752x 5752x 5752x 5752x 5752x 5752x 5752x 5752x 5752x 177x 177x   5752x     2310x 2310x 2310x 2310x 730x   1580x 1580x   2310x 2310x 2310x 2310x     1795x 1795x 1795x 1795x 1629x   166x 166x   1795x 1795x 4758x 1795x     15x 15x 15x 15x 15x 15x     3232x 3232x 3232x 3232x 2818x   414x 414x   3232x 12078x 3232x     552x 552x 552x 194x   358x 358x   552x                
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.UpdArrOp: {
        const operation = <operations.UpdArrOp>op;
        writer.u8(JsonCrdtPatchOpcodeOverlay.upd_arr);
        this.encodeId(operation.obj);
        this.encodeId(operation.ref);
        this.encodeId(operation.val);
        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');
      }
    }
  }
}