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 | 174x 174x 174x 174x 174x 174x 174x 174x 52505x 52505x 52505x 52505x 52505x 52505x 52505x 52505x 52505x 52505x 52505x 52505x 52505x 226706x 226706x 226706x 60332x 60332x 60332x 52505x 52505x 114190x 114190x 114190x 114190x 114190x 114190x 12153x 12153x 12153x 813x 813x 10179x 10179x 2x 2x 6822x 6822x 2406x 2406x 2613x 2613x 1803x 1803x 1803x 1803x 16993x 16993x 16993x 16993x 31353x 31353x 31353x 31353x 16993x 16993x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 40754x 40754x 40754x 40754x 40754x 40754x 6743x 6743x 6743x 6743x 6743x 6743x 6743x 1984x 1984x 1984x 1984x 5069x 1984x 1984x 10385x 10385x 10385x 60332x 10385x 10385x 539x 539x 539x | import {CrdtReader} from '../../util/binary/CrdtReader'; import { interval, type ITimespanStruct, type ITimestampStruct, ClockVector, ServerClockVector, Timestamp, } from '../../clock'; import type {Patch} from '../../Patch'; import {PatchBuilder} from '../../PatchBuilder'; import {SESSION} from '../../constants'; import {CborDecoder} from '@jsonjoy.com/json-pack/lib/cbor/CborDecoder'; import {JsonCrdtPatchOpcode} from '../../constants'; /** * JSON CRDT Patch "binary" codec decoder. */ export class Decoder extends CborDecoder<CrdtReader> { protected builder!: PatchBuilder; private patchSid?: number; /** * Creates a new JSON CRDT patch decoder. * * @param reader An optional custom implementation of a CRDT decoder. */ constructor(reader: CrdtReader = new CrdtReader()) { super(reader); } /** * Decodes a JSON CRDT patch from a binary blob. * * @param data Binary data to decode. * @returns A JSON CRDT patch. */ public decode(data: Uint8Array): Patch { this.reader.reset(data); return this.readPatch(); } public readPatch(): Patch { const reader = this.reader; const sid = reader.vu57(); const time = reader.vu57(); const isServerClock = sid === SESSION.SERVER; const clock = isServerClock ? new ServerClockVector(SESSION.SERVER, time) : new ClockVector(sid, time); this.patchSid = clock.sid; const builder = (this.builder = new PatchBuilder(clock)); const map = this.val(); if (map instanceof Array) builder.patch.meta = map[0]; this.decodeOperations(); return builder.patch; } protected decodeId(): ITimestampStruct { const reader = this.reader; const [isSessionDifferent, x] = reader.b1vu56(); return isSessionDifferent ? new Timestamp(reader.vu57(), x) : new Timestamp(this.patchSid!, x); } protected decodeTss(): ITimespanStruct { const id = this.decodeId(); const span = this.reader.vu57(); return interval(id, 0, span); } protected decodeOperations(): void { const reader = this.reader; const length = reader.vu57(); for (let i = 0; i < length; i++) this.decodeOperation(); } protected decodeOperation(): void { const builder = this.builder; const reader = this.reader; const octet = reader.u8(); const opcode = octet >> 3; switch (opcode) { case JsonCrdtPatchOpcode.new_con: { const length = octet & 0b111; builder.const(!length ? this.val() : this.decodeId()); break; } case JsonCrdtPatchOpcode.new_val: { builder.val(); break; } case JsonCrdtPatchOpcode.new_obj: { builder.obj(); break; } case JsonCrdtPatchOpcode.new_vec: { builder.vec(); break; } case JsonCrdtPatchOpcode.new_str: { builder.str(); break; } case JsonCrdtPatchOpcode.new_bin: { builder.bin(); break; } case JsonCrdtPatchOpcode.new_arr: { builder.arr(); break; } case JsonCrdtPatchOpcode.ins_val: { const obj = this.decodeId(); const val = this.decodeId(); builder.setVal(obj, val); break; } case JsonCrdtPatchOpcode.ins_obj: { const length = octet & 0b111 || reader.vu57(); const obj = this.decodeId(); const tuples: [key: string, value: ITimestampStruct][] = []; for (let i = 0; i < length; i++) { const key = this.val(); Iif (typeof key !== 'string') continue; const value = this.decodeId(); tuples.push([key, value]); } builder.insObj(obj, tuples); break; } case JsonCrdtPatchOpcode.ins_vec: { const length = octet & 0b111 || reader.vu57(); const obj = this.decodeId(); const tuples: [index: number, value: ITimestampStruct][] = []; for (let i = 0; i < length; i++) { const index = this.val(); Iif (typeof index !== 'number') continue; const value = this.decodeId(); tuples.push([index, value]); } builder.insVec(obj, tuples); break; } case JsonCrdtPatchOpcode.ins_str: { const length = octet & 0b111 || reader.vu57(); const obj = this.decodeId(); const after = this.decodeId(); const str = reader.utf8(length); builder.insStr(obj, after, str); break; } case JsonCrdtPatchOpcode.ins_bin: { const length = octet & 0b111 || reader.vu57(); const obj = this.decodeId(); const after = this.decodeId(); const buf = reader.buf(length); Iif (!(buf instanceof Uint8Array)) return; builder.insBin(obj, after, buf); break; } case JsonCrdtPatchOpcode.ins_arr: { const length = octet & 0b111 || reader.vu57(); const obj = this.decodeId(); const after = this.decodeId(); const elements: ITimestampStruct[] = []; for (let i = 0; i < length; i++) elements.push(this.decodeId()); builder.insArr(obj, after, elements); break; } case JsonCrdtPatchOpcode.del: { const length = octet & 0b111 || reader.vu57(); const obj = this.decodeId(); const what: ITimespanStruct[] = []; for (let i = 0; i < length; i++) what.push(this.decodeTss()); builder.del(obj, what); break; } case JsonCrdtPatchOpcode.nop: { const length = octet & 0b111 || reader.vu57(); builder.nop(length); break; } default: { throw new Error('UNKNOWN_OP'); } } } } |