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                   7621x 7621x 7621x 7621x 7621x 7621x 7621x 7621x 3x 7621x 7621x       7621x 7621x 7621x 32916x       57389x 57389x 57389x 57389x 31337x   26052x 26052x         11395x 11395x       5498x 5498x 3065x   2433x 2433x   5498x 5498x 5498x       32916x 32916x 32916x   2618x 2618x 2618x 4x 4x   2614x 2614x   2618x     720x 720x     4034x 4034x     2x 2x     3216x 3216x     761x 761x     2432x 2432x     1532x 1532x 1532x 1532x 1532x     4568x 4568x 4568x 4568x 4377x   191x 191x   4568x 4568x 11375x 11375x 11375x   4568x     1x 1x 1x 1x 1x         1x 1x 1x 1x 1x   1x     5367x 5367x 5367x 5367x 5367x 5367x 5367x 5367x 5367x 131x 131x   5367x     2276x 2276x 2276x 2276x 622x   1654x 1654x   2276x 2276x 2276x 2276x     1772x 1772x 1772x 1772x 1577x   195x 195x   1772x 1772x 4795x 1772x     3094x 3094x 3094x 3094x 2715x   379x 379x   3094x 11395x 3094x     523x 523x 523x 173x   350x 350x   523x                
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');
      }
    }
  }
}