@loro-dev/json-patch 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +314 -2
- package/dist/diff.cjs +1666 -0
- package/dist/diff.d.cts +126 -0
- package/dist/diff.d.ts +126 -0
- package/dist/diff.js +1463 -0
- package/dist/index.cjs +1124 -0
- package/dist/index.d.cts +74 -0
- package/dist/index.d.ts +74 -0
- package/dist/index.js +1097 -0
- package/dist/loro.cjs +2722 -0
- package/dist/loro.d.cts +115 -0
- package/dist/loro.d.ts +115 -0
- package/dist/loro.js +2710 -0
- package/package.json +70 -3
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,1124 @@
|
|
|
1
|
+
const require_diff = require('./diff.cjs');
|
|
2
|
+
let __msgpack_msgpack = require("@msgpack/msgpack");
|
|
3
|
+
__msgpack_msgpack = require_diff.__toESM(__msgpack_msgpack);
|
|
4
|
+
|
|
5
|
+
//#region src/codec.ts
|
|
6
|
+
const UTF8_DECODER = new TextDecoder("utf-8", { fatal: true });
|
|
7
|
+
var FloatToken = class {
|
|
8
|
+
value;
|
|
9
|
+
constructor(value) {
|
|
10
|
+
this.value = value;
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
function checkNoLoneSurrogates(s, isPath = false) {
|
|
14
|
+
if (require_diff.hasLoneSurrogates(s)) {
|
|
15
|
+
if (isPath) throw new require_diff.DecodeError("InvalidPath", "path segment contains lone UTF-16 surrogate");
|
|
16
|
+
throw new require_diff.DecodeError("InvalidFormat", "string contains lone UTF-16 surrogate");
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
function isEscapeKey(k) {
|
|
20
|
+
return k === "$bytes" || k === "$esc";
|
|
21
|
+
}
|
|
22
|
+
const B64_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
23
|
+
const B64_LOOKUP = (() => {
|
|
24
|
+
const table = new Int16Array(256).fill(-1);
|
|
25
|
+
for (let i = 0; i < 64; i++) table[B64_CHARS.charCodeAt(i)] = i;
|
|
26
|
+
return table;
|
|
27
|
+
})();
|
|
28
|
+
function encodeBase64(bytes) {
|
|
29
|
+
let out = "";
|
|
30
|
+
let i = 0;
|
|
31
|
+
for (; i + 2 < bytes.length; i += 3) {
|
|
32
|
+
const n = bytes[i] << 16 | bytes[i + 1] << 8 | bytes[i + 2];
|
|
33
|
+
out += B64_CHARS[n >>> 18 & 63] + B64_CHARS[n >>> 12 & 63] + B64_CHARS[n >>> 6 & 63] + B64_CHARS[n & 63];
|
|
34
|
+
}
|
|
35
|
+
const rem = bytes.length - i;
|
|
36
|
+
if (rem === 1) {
|
|
37
|
+
const n = bytes[i];
|
|
38
|
+
out += B64_CHARS[n >>> 2 & 63] + B64_CHARS[n << 4 & 63] + "==";
|
|
39
|
+
} else if (rem === 2) {
|
|
40
|
+
const n = bytes[i] << 8 | bytes[i + 1];
|
|
41
|
+
out += B64_CHARS[n >>> 10 & 63] + B64_CHARS[n >>> 4 & 63] + B64_CHARS[n << 2 & 63] + "=";
|
|
42
|
+
}
|
|
43
|
+
return out;
|
|
44
|
+
}
|
|
45
|
+
function decodeB64Char(code) {
|
|
46
|
+
if (code >= 256) throw new require_diff.DecodeError("InvalidBase64", "invalid base64 character");
|
|
47
|
+
const v = B64_LOOKUP[code];
|
|
48
|
+
if (v < 0) throw new require_diff.DecodeError("InvalidBase64", "invalid base64 character");
|
|
49
|
+
return v;
|
|
50
|
+
}
|
|
51
|
+
function decodeBase64(s) {
|
|
52
|
+
if (s.length === 0) return new Uint8Array(0);
|
|
53
|
+
if (s.length % 4 !== 0) throw new require_diff.DecodeError("InvalidBase64", "invalid base64 length");
|
|
54
|
+
let pad = 0;
|
|
55
|
+
if (s.charCodeAt(s.length - 1) === 61) pad = s.charCodeAt(s.length - 2) === 61 ? 2 : 1;
|
|
56
|
+
const outLen = s.length / 4 * 3 - pad;
|
|
57
|
+
const out = new Uint8Array(outLen);
|
|
58
|
+
let outPos = 0;
|
|
59
|
+
for (let i = 0; i < s.length; i += 4) {
|
|
60
|
+
const c0 = decodeB64Char(s.charCodeAt(i));
|
|
61
|
+
const c1 = decodeB64Char(s.charCodeAt(i + 1));
|
|
62
|
+
const isLast = i + 4 === s.length;
|
|
63
|
+
const c2 = isLast && pad >= 2 ? 0 : decodeB64Char(s.charCodeAt(i + 2));
|
|
64
|
+
const c3 = isLast && pad >= 1 ? 0 : decodeB64Char(s.charCodeAt(i + 3));
|
|
65
|
+
if (isLast) {
|
|
66
|
+
if (pad === 2 && (c1 & 15) !== 0) throw new require_diff.DecodeError("InvalidBase64", "non-zero trailing padding bits");
|
|
67
|
+
if (pad === 1 && (c2 & 3) !== 0) throw new require_diff.DecodeError("InvalidBase64", "non-zero trailing padding bits");
|
|
68
|
+
}
|
|
69
|
+
const n = c0 << 18 | c1 << 12 | c2 << 6 | c3;
|
|
70
|
+
if (outPos < outLen) out[outPos++] = n >>> 16 & 255;
|
|
71
|
+
if (outPos < outLen) out[outPos++] = n >>> 8 & 255;
|
|
72
|
+
if (outPos < outLen) out[outPos++] = n & 255;
|
|
73
|
+
}
|
|
74
|
+
return out;
|
|
75
|
+
}
|
|
76
|
+
function parseJsonLossless(input) {
|
|
77
|
+
let pos = 0;
|
|
78
|
+
const len = input.length;
|
|
79
|
+
function skipWs() {
|
|
80
|
+
while (pos < len) {
|
|
81
|
+
const c = input.charCodeAt(pos);
|
|
82
|
+
if (c === 32 || c === 10 || c === 13 || c === 9) pos++;
|
|
83
|
+
else break;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
function parseValue() {
|
|
87
|
+
skipWs();
|
|
88
|
+
if (pos >= len) throw new require_diff.DecodeError("Json", "unexpected end of JSON input");
|
|
89
|
+
const c = input.charCodeAt(pos);
|
|
90
|
+
if (c === 123) return parseObject();
|
|
91
|
+
if (c === 91) return parseArray();
|
|
92
|
+
if (c === 34) return parseString();
|
|
93
|
+
if (c === 116) return parseLiteral("true", true);
|
|
94
|
+
if (c === 102) return parseLiteral("false", false);
|
|
95
|
+
if (c === 110) return parseLiteral("null", null);
|
|
96
|
+
if (c === 45 || c >= 48 && c <= 57) return parseNumber();
|
|
97
|
+
throw new require_diff.DecodeError("Json", `unexpected character '${input[pos]}' at position ${pos}`);
|
|
98
|
+
}
|
|
99
|
+
function parseLiteral(lit, val) {
|
|
100
|
+
if (input.startsWith(lit, pos)) {
|
|
101
|
+
pos += lit.length;
|
|
102
|
+
return val;
|
|
103
|
+
}
|
|
104
|
+
throw new require_diff.DecodeError("Json", `expected '${lit}' at position ${pos}`);
|
|
105
|
+
}
|
|
106
|
+
function parseString() {
|
|
107
|
+
const start = pos;
|
|
108
|
+
pos++;
|
|
109
|
+
let hasEscape = false;
|
|
110
|
+
while (pos < len) {
|
|
111
|
+
const c = input.charCodeAt(pos);
|
|
112
|
+
if (c === 34) {
|
|
113
|
+
if (!hasEscape) {
|
|
114
|
+
const s = input.slice(start + 1, pos);
|
|
115
|
+
pos++;
|
|
116
|
+
checkNoLoneSurrogates(s);
|
|
117
|
+
return s;
|
|
118
|
+
}
|
|
119
|
+
pos++;
|
|
120
|
+
let parsed;
|
|
121
|
+
try {
|
|
122
|
+
parsed = JSON.parse(input.slice(start, pos));
|
|
123
|
+
} catch (err) {
|
|
124
|
+
throw new require_diff.DecodeError("Json", `invalid string literal: ${String(err)}`);
|
|
125
|
+
}
|
|
126
|
+
checkNoLoneSurrogates(parsed);
|
|
127
|
+
return parsed;
|
|
128
|
+
}
|
|
129
|
+
if (c === 92) {
|
|
130
|
+
hasEscape = true;
|
|
131
|
+
pos += 2;
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
if (c < 32) throw new require_diff.DecodeError("Json", `unescaped control character in string at ${pos}`);
|
|
135
|
+
pos++;
|
|
136
|
+
}
|
|
137
|
+
throw new require_diff.DecodeError("Json", "unterminated string literal");
|
|
138
|
+
}
|
|
139
|
+
function parseNumber() {
|
|
140
|
+
const start = pos;
|
|
141
|
+
if (input.charCodeAt(pos) === 45) pos++;
|
|
142
|
+
if (pos >= len) throw new require_diff.DecodeError("Json", "invalid number");
|
|
143
|
+
if (input.charCodeAt(pos) === 48) pos++;
|
|
144
|
+
else if (input.charCodeAt(pos) >= 49 && input.charCodeAt(pos) <= 57) {
|
|
145
|
+
pos++;
|
|
146
|
+
while (pos < len && input.charCodeAt(pos) >= 48 && input.charCodeAt(pos) <= 57) pos++;
|
|
147
|
+
} else throw new require_diff.DecodeError("Json", `invalid number at ${start}`);
|
|
148
|
+
let isFloat = false;
|
|
149
|
+
if (pos < len && input.charCodeAt(pos) === 46) {
|
|
150
|
+
isFloat = true;
|
|
151
|
+
pos++;
|
|
152
|
+
if (pos >= len || input.charCodeAt(pos) < 48 || input.charCodeAt(pos) > 57) throw new require_diff.DecodeError("Json", `invalid fraction in number at ${start}`);
|
|
153
|
+
while (pos < len && input.charCodeAt(pos) >= 48 && input.charCodeAt(pos) <= 57) pos++;
|
|
154
|
+
}
|
|
155
|
+
if (pos < len && (input.charCodeAt(pos) === 101 || input.charCodeAt(pos) === 69)) {
|
|
156
|
+
isFloat = true;
|
|
157
|
+
pos++;
|
|
158
|
+
if (pos < len && (input.charCodeAt(pos) === 43 || input.charCodeAt(pos) === 45)) pos++;
|
|
159
|
+
if (pos >= len || input.charCodeAt(pos) < 48 || input.charCodeAt(pos) > 57) throw new require_diff.DecodeError("Json", `invalid exponent in number at ${start}`);
|
|
160
|
+
while (pos < len && input.charCodeAt(pos) >= 48 && input.charCodeAt(pos) <= 57) pos++;
|
|
161
|
+
}
|
|
162
|
+
const raw = input.slice(start, pos);
|
|
163
|
+
if (!isFloat && raw !== "-0") {
|
|
164
|
+
const bi = BigInt(raw);
|
|
165
|
+
if (bi >= require_diff.MIN_SAFE_BIG && bi <= require_diff.MAX_SAFE_BIG) return Number(raw);
|
|
166
|
+
if (bi >= require_diff.I64_MIN && bi <= require_diff.U64_MAX) return bi;
|
|
167
|
+
}
|
|
168
|
+
const f = Number(raw);
|
|
169
|
+
if (!Number.isFinite(f)) throw new require_diff.DecodeError("NonFiniteFloat", `number out of finite f64 range: ${raw}`);
|
|
170
|
+
return new FloatToken(Object.is(f, -0) ? 0 : f);
|
|
171
|
+
}
|
|
172
|
+
function parseArray() {
|
|
173
|
+
pos++;
|
|
174
|
+
const arr = [];
|
|
175
|
+
skipWs();
|
|
176
|
+
if (pos < len && input.charCodeAt(pos) === 93) {
|
|
177
|
+
pos++;
|
|
178
|
+
return arr;
|
|
179
|
+
}
|
|
180
|
+
while (true) {
|
|
181
|
+
arr.push(parseValue());
|
|
182
|
+
skipWs();
|
|
183
|
+
if (pos >= len) throw new require_diff.DecodeError("Json", "unterminated array");
|
|
184
|
+
if (input.charCodeAt(pos) === 93) {
|
|
185
|
+
pos++;
|
|
186
|
+
return arr;
|
|
187
|
+
}
|
|
188
|
+
if (input.charCodeAt(pos) !== 44) throw new require_diff.DecodeError("Json", `expected ',' or ']' at ${pos}`);
|
|
189
|
+
pos++;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
function parseObject() {
|
|
193
|
+
pos++;
|
|
194
|
+
const obj = {};
|
|
195
|
+
skipWs();
|
|
196
|
+
if (pos < len && input.charCodeAt(pos) === 125) {
|
|
197
|
+
pos++;
|
|
198
|
+
return obj;
|
|
199
|
+
}
|
|
200
|
+
while (true) {
|
|
201
|
+
skipWs();
|
|
202
|
+
if (pos >= len || input.charCodeAt(pos) !== 34) throw new require_diff.DecodeError("Json", `expected string key at ${pos}`);
|
|
203
|
+
const key = parseString();
|
|
204
|
+
skipWs();
|
|
205
|
+
if (pos >= len || input.charCodeAt(pos) !== 58) throw new require_diff.DecodeError("Json", `expected ':' after key at ${pos}`);
|
|
206
|
+
pos++;
|
|
207
|
+
require_diff.setOwnProp(obj, key, parseValue());
|
|
208
|
+
skipWs();
|
|
209
|
+
if (pos >= len) throw new require_diff.DecodeError("Json", "unterminated object");
|
|
210
|
+
if (input.charCodeAt(pos) === 125) {
|
|
211
|
+
pos++;
|
|
212
|
+
break;
|
|
213
|
+
}
|
|
214
|
+
if (input.charCodeAt(pos) !== 44) throw new require_diff.DecodeError("Json", `expected ',' or '}' at ${pos}`);
|
|
215
|
+
pos++;
|
|
216
|
+
}
|
|
217
|
+
return obj;
|
|
218
|
+
}
|
|
219
|
+
const root = parseValue();
|
|
220
|
+
skipWs();
|
|
221
|
+
if (pos !== len) throw new require_diff.DecodeError("Json", `trailing garbage after JSON at position ${pos}`);
|
|
222
|
+
return root;
|
|
223
|
+
}
|
|
224
|
+
function decodeRawJsonValue(raw, bytesMode) {
|
|
225
|
+
if (raw === null || typeof raw === "boolean") return raw;
|
|
226
|
+
if (typeof raw === "string") {
|
|
227
|
+
checkNoLoneSurrogates(raw);
|
|
228
|
+
return raw;
|
|
229
|
+
}
|
|
230
|
+
if (raw instanceof FloatToken) return raw.value;
|
|
231
|
+
if (typeof raw === "number") {
|
|
232
|
+
if (!Number.isFinite(raw)) throw new require_diff.DecodeError("NonFiniteFloat", "non-finite float (NaN or Infinity) is forbidden");
|
|
233
|
+
return Object.is(raw, -0) ? 0 : raw;
|
|
234
|
+
}
|
|
235
|
+
if (typeof raw === "bigint") {
|
|
236
|
+
if (raw < require_diff.I64_MIN || raw > require_diff.U64_MAX) throw new require_diff.DecodeError("InvalidFormat", `integer out of i64/u64 range: ${raw}`);
|
|
237
|
+
if (raw >= require_diff.MIN_SAFE_BIG && raw <= require_diff.MAX_SAFE_BIG) return Number(raw);
|
|
238
|
+
return raw;
|
|
239
|
+
}
|
|
240
|
+
if (raw instanceof Uint8Array) return raw;
|
|
241
|
+
if (Array.isArray(raw)) {
|
|
242
|
+
const out = new Array(raw.length);
|
|
243
|
+
for (let i = 0; i < raw.length; i++) out[i] = decodeRawJsonValue(raw[i], bytesMode);
|
|
244
|
+
return out;
|
|
245
|
+
}
|
|
246
|
+
if (require_diff.isPlainObject(raw)) {
|
|
247
|
+
const keys = Object.keys(raw);
|
|
248
|
+
if (bytesMode === "tagged" && keys.length === 1) {
|
|
249
|
+
const k = keys[0];
|
|
250
|
+
if (k === "$bytes" && typeof raw.$bytes === "string") return decodeBase64(raw.$bytes);
|
|
251
|
+
if (k === "$esc" && require_diff.isPlainObject(raw.$esc)) {
|
|
252
|
+
const innerKeys = Object.keys(raw.$esc);
|
|
253
|
+
if (innerKeys.length === 1 && isEscapeKey(innerKeys[0])) {
|
|
254
|
+
const innerK = innerKeys[0];
|
|
255
|
+
checkNoLoneSurrogates(innerK);
|
|
256
|
+
const out$1 = {};
|
|
257
|
+
require_diff.setOwnProp(out$1, innerK, decodeRawJsonValue(raw.$esc[innerK], bytesMode));
|
|
258
|
+
return out$1;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
const out = {};
|
|
263
|
+
for (let i = 0; i < keys.length; i++) {
|
|
264
|
+
const k = keys[i];
|
|
265
|
+
checkNoLoneSurrogates(k);
|
|
266
|
+
require_diff.setOwnProp(out, k, decodeRawJsonValue(raw[k], bytesMode));
|
|
267
|
+
}
|
|
268
|
+
return out;
|
|
269
|
+
}
|
|
270
|
+
throw new require_diff.DecodeError("InvalidFormat", `unsupported decoded value type: ${typeof raw}`);
|
|
271
|
+
}
|
|
272
|
+
function serializeJsonValue(val, bytesMode) {
|
|
273
|
+
if (val === null) return "null";
|
|
274
|
+
switch (typeof val) {
|
|
275
|
+
case "boolean": return val ? "true" : "false";
|
|
276
|
+
case "bigint":
|
|
277
|
+
if (val < require_diff.I64_MIN || val > require_diff.U64_MAX) throw new require_diff.DecodeError("InvalidFormat", `bigint out of i64/u64 range: ${val}`);
|
|
278
|
+
return val.toString();
|
|
279
|
+
case "number":
|
|
280
|
+
if (!Number.isFinite(val)) throw new require_diff.DecodeError("NonFiniteFloat", "non-finite float (NaN or Infinity) is forbidden");
|
|
281
|
+
return Object.is(val, -0) ? "0" : JSON.stringify(val);
|
|
282
|
+
case "string":
|
|
283
|
+
checkNoLoneSurrogates(val);
|
|
284
|
+
return JSON.stringify(val);
|
|
285
|
+
case "object":
|
|
286
|
+
if (val instanceof Uint8Array) {
|
|
287
|
+
if (bytesMode === "reject") throw new require_diff.DecodeError("BytesRejected", "bytes cannot be encoded with bytesMode='reject'");
|
|
288
|
+
return `{"$bytes":${JSON.stringify(encodeBase64(val))}}`;
|
|
289
|
+
}
|
|
290
|
+
if (Array.isArray(val)) {
|
|
291
|
+
const parts = [];
|
|
292
|
+
for (let i = 0; i < val.length; i++) parts.push(serializeJsonValue(val[i], bytesMode));
|
|
293
|
+
return `[${parts.join(",")}]`;
|
|
294
|
+
}
|
|
295
|
+
if (require_diff.isPlainObject(val)) {
|
|
296
|
+
const keys = Object.keys(val);
|
|
297
|
+
if (bytesMode === "tagged" && keys.length === 1 && isEscapeKey(keys[0])) {
|
|
298
|
+
const k = keys[0];
|
|
299
|
+
checkNoLoneSurrogates(k);
|
|
300
|
+
return `{"$esc":{${JSON.stringify(k)}:${serializeJsonValue(val[k], bytesMode)}}}`;
|
|
301
|
+
}
|
|
302
|
+
keys.sort();
|
|
303
|
+
const parts = [];
|
|
304
|
+
for (let i = 0; i < keys.length; i++) {
|
|
305
|
+
const k = keys[i];
|
|
306
|
+
checkNoLoneSurrogates(k);
|
|
307
|
+
parts.push(`${JSON.stringify(k)}:${serializeJsonValue(val[k], bytesMode)}`);
|
|
308
|
+
}
|
|
309
|
+
return `{${parts.join(",")}}`;
|
|
310
|
+
}
|
|
311
|
+
throw new require_diff.DecodeError("InvalidFormat", "unsupported object value in JsonValue");
|
|
312
|
+
default: throw new require_diff.DecodeError("InvalidFormat", `unsupported value type: ${typeof val}`);
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
function serializePath(path) {
|
|
316
|
+
const parts = [];
|
|
317
|
+
for (let i = 0; i < path.length; i++) {
|
|
318
|
+
const seg = path[i];
|
|
319
|
+
if (typeof seg === "string") {
|
|
320
|
+
checkNoLoneSurrogates(seg, true);
|
|
321
|
+
parts.push(JSON.stringify(seg));
|
|
322
|
+
} else if (typeof seg === "number" && Number.isSafeInteger(seg) && seg >= 0) parts.push(String(seg));
|
|
323
|
+
else throw new require_diff.DecodeError("InvalidPath", `invalid path segment: ${String(seg)}`);
|
|
324
|
+
}
|
|
325
|
+
return `[${parts.join(",")}]`;
|
|
326
|
+
}
|
|
327
|
+
function parsePath(raw) {
|
|
328
|
+
if (!Array.isArray(raw)) throw new require_diff.DecodeError("InvalidPath", "path must be an array");
|
|
329
|
+
const path = [];
|
|
330
|
+
for (let i = 0; i < raw.length; i++) {
|
|
331
|
+
const seg = raw[i];
|
|
332
|
+
if (typeof seg === "string") {
|
|
333
|
+
checkNoLoneSurrogates(seg, true);
|
|
334
|
+
path.push(seg);
|
|
335
|
+
} else if (typeof seg === "number" && Number.isSafeInteger(seg) && seg >= 0) path.push(seg);
|
|
336
|
+
else if (typeof seg === "bigint" && seg >= 0n && seg <= require_diff.MAX_SAFE_BIG) path.push(Number(seg));
|
|
337
|
+
else throw new require_diff.DecodeError("InvalidPath", `invalid path segment: ${String(seg)}`);
|
|
338
|
+
}
|
|
339
|
+
return path;
|
|
340
|
+
}
|
|
341
|
+
function parseNonNegSafeInt(raw, field) {
|
|
342
|
+
if (typeof raw === "number" && Number.isSafeInteger(raw) && raw >= 0) return raw;
|
|
343
|
+
if (typeof raw === "bigint" && raw >= 0n && raw <= require_diff.MAX_SAFE_BIG) return Number(raw);
|
|
344
|
+
throw new require_diff.DecodeError("InvalidFormat", `field '${field}' must be a non-negative safe integer`);
|
|
345
|
+
}
|
|
346
|
+
function checkEncodeNonNegSafeInt(val, field) {
|
|
347
|
+
if (!Number.isSafeInteger(val) || val < 0) throw new require_diff.DecodeError("InvalidFormat", `field '${field}' must be a non-negative safe integer`);
|
|
348
|
+
return Object.is(val, -0) ? 0 : val;
|
|
349
|
+
}
|
|
350
|
+
/**
|
|
351
|
+
* Serializes a {@link JsonPatch} to lossless wire JSON (preserving exact 64-bit `bigint` integers,
|
|
352
|
+
* rejecting non-finite floats / lone surrogates, and encoding `Uint8Array` per `options.bytesMode`).
|
|
353
|
+
* @throws {@link DecodeError} on invalid integers, floats, paths, or bytes.
|
|
354
|
+
*/
|
|
355
|
+
function encodeJson(patch, options = {}) {
|
|
356
|
+
const bytesMode = options.bytesMode ?? "tagged";
|
|
357
|
+
const ops = [];
|
|
358
|
+
for (let i = 0; i < patch.length; i++) {
|
|
359
|
+
const op = patch[i];
|
|
360
|
+
switch (op.op) {
|
|
361
|
+
case "set":
|
|
362
|
+
ops.push(`{"op":"set","path":${serializePath(op.path)},"value":${serializeJsonValue(op.value, bytesMode)}}`);
|
|
363
|
+
break;
|
|
364
|
+
case "unset":
|
|
365
|
+
ops.push(`{"op":"unset","path":${serializePath(op.path)}}`);
|
|
366
|
+
break;
|
|
367
|
+
case "arr_splice": {
|
|
368
|
+
const at = checkEncodeNonNegSafeInt(op.at, "at");
|
|
369
|
+
const del = checkEncodeNonNegSafeInt(op.del, "del");
|
|
370
|
+
if (at + del > Number.MAX_SAFE_INTEGER) throw new require_diff.DecodeError("InvalidFormat", "arr_splice at + del exceeds MAX_SAFE_INTEGER");
|
|
371
|
+
const items = [];
|
|
372
|
+
for (let j = 0; j < op.items.length; j++) items.push(serializeJsonValue(op.items[j], bytesMode));
|
|
373
|
+
ops.push(`{"op":"arr_splice","path":${serializePath(op.path)},"at":${at},"del":${del},"items":[${items.join(",")}]}`);
|
|
374
|
+
break;
|
|
375
|
+
}
|
|
376
|
+
case "move":
|
|
377
|
+
ops.push(`{"op":"move","from":${serializePath(op.from)},"to":${serializePath(op.to)}}`);
|
|
378
|
+
break;
|
|
379
|
+
case "str_splice": {
|
|
380
|
+
const at = checkEncodeNonNegSafeInt(op.at, "at");
|
|
381
|
+
const del = checkEncodeNonNegSafeInt(op.del, "del");
|
|
382
|
+
if (at + del > Number.MAX_SAFE_INTEGER) throw new require_diff.DecodeError("InvalidFormat", "str_splice at + del exceeds MAX_SAFE_INTEGER");
|
|
383
|
+
checkNoLoneSurrogates(op.ins);
|
|
384
|
+
ops.push(`{"op":"str_splice","path":${serializePath(op.path)},"at":${at},"del":${del},"ins":${JSON.stringify(op.ins)}}`);
|
|
385
|
+
break;
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
return `[${ops.join(",")}]`;
|
|
390
|
+
}
|
|
391
|
+
/**
|
|
392
|
+
* Parses wire JSON text back into a validated {@link JsonOp} array, restoring out-of-safe-range integers
|
|
393
|
+
* as `bigint` and `{"$bytes":"..."}` as `Uint8Array` (when `bytesMode` is `"tagged"`).
|
|
394
|
+
* @throws {@link DecodeError} on malformed JSON, unknown op names, or invalid values/paths.
|
|
395
|
+
*/
|
|
396
|
+
function decodeJson(input, options = {}) {
|
|
397
|
+
const bytesMode = options.bytesMode ?? "tagged";
|
|
398
|
+
const root = parseJsonLossless(input);
|
|
399
|
+
if (!Array.isArray(root)) throw new require_diff.DecodeError("InvalidFormat", "patch root must be a JSON array");
|
|
400
|
+
const out = [];
|
|
401
|
+
for (let i = 0; i < root.length; i++) {
|
|
402
|
+
const item = root[i];
|
|
403
|
+
if (!require_diff.isPlainObject(item)) throw new require_diff.DecodeError("InvalidFormat", "each JSON op must be an object");
|
|
404
|
+
const opName = item.op;
|
|
405
|
+
if (typeof opName !== "string") throw new require_diff.DecodeError("InvalidFormat", "op object missing string 'op' field");
|
|
406
|
+
switch (opName) {
|
|
407
|
+
case "set":
|
|
408
|
+
if (!require_diff.hasOwn(item, "path")) throw new require_diff.DecodeError("InvalidFormat", "set missing 'path'");
|
|
409
|
+
if (!require_diff.hasOwn(item, "value")) throw new require_diff.DecodeError("InvalidFormat", "set missing 'value'");
|
|
410
|
+
out.push({
|
|
411
|
+
op: "set",
|
|
412
|
+
path: parsePath(item.path),
|
|
413
|
+
value: decodeRawJsonValue(item.value, bytesMode)
|
|
414
|
+
});
|
|
415
|
+
break;
|
|
416
|
+
case "unset":
|
|
417
|
+
if (!require_diff.hasOwn(item, "path")) throw new require_diff.DecodeError("InvalidFormat", "unset missing 'path'");
|
|
418
|
+
out.push({
|
|
419
|
+
op: "unset",
|
|
420
|
+
path: parsePath(item.path)
|
|
421
|
+
});
|
|
422
|
+
break;
|
|
423
|
+
case "arr_splice":
|
|
424
|
+
if (!require_diff.hasOwn(item, "path")) throw new require_diff.DecodeError("InvalidFormat", "arr_splice missing 'path'");
|
|
425
|
+
if (!Array.isArray(item.items)) throw new require_diff.DecodeError("InvalidFormat", "arr_splice missing array 'items'");
|
|
426
|
+
out.push({
|
|
427
|
+
op: "arr_splice",
|
|
428
|
+
path: parsePath(item.path),
|
|
429
|
+
at: parseNonNegSafeInt(item.at, "at"),
|
|
430
|
+
del: parseNonNegSafeInt(item.del, "del"),
|
|
431
|
+
items: item.items.map((v) => decodeRawJsonValue(v, bytesMode))
|
|
432
|
+
});
|
|
433
|
+
break;
|
|
434
|
+
case "move":
|
|
435
|
+
if (!require_diff.hasOwn(item, "from")) throw new require_diff.DecodeError("InvalidFormat", "move missing 'from'");
|
|
436
|
+
if (!require_diff.hasOwn(item, "to")) throw new require_diff.DecodeError("InvalidFormat", "move missing 'to'");
|
|
437
|
+
out.push({
|
|
438
|
+
op: "move",
|
|
439
|
+
from: parsePath(item.from),
|
|
440
|
+
to: parsePath(item.to)
|
|
441
|
+
});
|
|
442
|
+
break;
|
|
443
|
+
case "str_splice":
|
|
444
|
+
if (!require_diff.hasOwn(item, "path")) throw new require_diff.DecodeError("InvalidFormat", "str_splice missing 'path'");
|
|
445
|
+
if (typeof item.ins !== "string") throw new require_diff.DecodeError("InvalidFormat", "str_splice missing string 'ins'");
|
|
446
|
+
checkNoLoneSurrogates(item.ins);
|
|
447
|
+
out.push({
|
|
448
|
+
op: "str_splice",
|
|
449
|
+
path: parsePath(item.path),
|
|
450
|
+
at: parseNonNegSafeInt(item.at, "at"),
|
|
451
|
+
del: parseNonNegSafeInt(item.del, "del"),
|
|
452
|
+
ins: item.ins
|
|
453
|
+
});
|
|
454
|
+
break;
|
|
455
|
+
default: throw new require_diff.DecodeError("UnknownOpName", `unknown op name: ${opName}`);
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
return out;
|
|
459
|
+
}
|
|
460
|
+
function normalizeForMsgpack(val) {
|
|
461
|
+
if (val === null || typeof val === "boolean") return val;
|
|
462
|
+
if (typeof val === "string") {
|
|
463
|
+
checkNoLoneSurrogates(val);
|
|
464
|
+
return val;
|
|
465
|
+
}
|
|
466
|
+
if (typeof val === "number") {
|
|
467
|
+
if (!Number.isFinite(val)) throw new require_diff.DecodeError("NonFiniteFloat", "non-finite float (NaN or Infinity) is forbidden");
|
|
468
|
+
if (Object.is(val, -0)) return 0;
|
|
469
|
+
if (Number.isSafeInteger(val) && (val >= 4294967296 || val < -2147483648)) return BigInt(val);
|
|
470
|
+
return val;
|
|
471
|
+
}
|
|
472
|
+
if (typeof val === "bigint") {
|
|
473
|
+
if (val < require_diff.I64_MIN || val > require_diff.U64_MAX) throw new require_diff.DecodeError("InvalidFormat", `bigint out of i64/u64 range: ${val}`);
|
|
474
|
+
if (val >= -2147483648n && val <= 4294967295n) return Number(val);
|
|
475
|
+
return val;
|
|
476
|
+
}
|
|
477
|
+
if (val instanceof Uint8Array) return val;
|
|
478
|
+
if (Array.isArray(val)) {
|
|
479
|
+
const out = new Array(val.length);
|
|
480
|
+
for (let i = 0; i < val.length; i++) out[i] = normalizeForMsgpack(val[i]);
|
|
481
|
+
return out;
|
|
482
|
+
}
|
|
483
|
+
if (require_diff.isPlainObject(val)) {
|
|
484
|
+
const keys = Object.keys(val).sort();
|
|
485
|
+
const out = {};
|
|
486
|
+
for (let i = 0; i < keys.length; i++) {
|
|
487
|
+
const k = keys[i];
|
|
488
|
+
checkNoLoneSurrogates(k);
|
|
489
|
+
require_diff.setOwnProp(out, k, normalizeForMsgpack(val[k]));
|
|
490
|
+
}
|
|
491
|
+
return out;
|
|
492
|
+
}
|
|
493
|
+
throw new require_diff.DecodeError("InvalidFormat", "unsupported object value in JsonValue");
|
|
494
|
+
}
|
|
495
|
+
function nonNegIntForMsgpack(n) {
|
|
496
|
+
const clean = Object.is(n, -0) ? 0 : n;
|
|
497
|
+
return clean >= 4294967296 ? BigInt(clean) : clean;
|
|
498
|
+
}
|
|
499
|
+
function pathForMsgpack(path) {
|
|
500
|
+
const out = new Array(path.length);
|
|
501
|
+
for (let i = 0; i < path.length; i++) {
|
|
502
|
+
const seg = path[i];
|
|
503
|
+
if (typeof seg === "string") {
|
|
504
|
+
checkNoLoneSurrogates(seg, true);
|
|
505
|
+
out[i] = seg;
|
|
506
|
+
} else if (typeof seg === "number" && Number.isSafeInteger(seg) && seg >= 0) out[i] = nonNegIntForMsgpack(seg);
|
|
507
|
+
else throw new require_diff.DecodeError("InvalidPath", `invalid path segment: ${String(seg)}`);
|
|
508
|
+
}
|
|
509
|
+
return out;
|
|
510
|
+
}
|
|
511
|
+
/**
|
|
512
|
+
* Encodes a {@link JsonPatch} into compact binary MessagePack op-tuples (`[opCode, ...]`)
|
|
513
|
+
* compatible with the Rust `loro-json-patch` codec.
|
|
514
|
+
* @throws {@link DecodeError} on invalid paths, values, or integer bounds.
|
|
515
|
+
*/
|
|
516
|
+
function encodeMsgpack(patch) {
|
|
517
|
+
const tuples = new Array(patch.length);
|
|
518
|
+
for (let i = 0; i < patch.length; i++) {
|
|
519
|
+
const op = patch[i];
|
|
520
|
+
switch (op.op) {
|
|
521
|
+
case "set":
|
|
522
|
+
tuples[i] = [
|
|
523
|
+
1,
|
|
524
|
+
pathForMsgpack(op.path),
|
|
525
|
+
normalizeForMsgpack(op.value)
|
|
526
|
+
];
|
|
527
|
+
break;
|
|
528
|
+
case "unset":
|
|
529
|
+
tuples[i] = [2, pathForMsgpack(op.path)];
|
|
530
|
+
break;
|
|
531
|
+
case "arr_splice": {
|
|
532
|
+
const at = checkEncodeNonNegSafeInt(op.at, "at");
|
|
533
|
+
const del = checkEncodeNonNegSafeInt(op.del, "del");
|
|
534
|
+
if (at + del > Number.MAX_SAFE_INTEGER) throw new require_diff.DecodeError("InvalidFormat", "arr_splice at + del exceeds MAX_SAFE_INTEGER");
|
|
535
|
+
tuples[i] = [
|
|
536
|
+
3,
|
|
537
|
+
pathForMsgpack(op.path),
|
|
538
|
+
nonNegIntForMsgpack(at),
|
|
539
|
+
nonNegIntForMsgpack(del),
|
|
540
|
+
op.items.map((item) => normalizeForMsgpack(item))
|
|
541
|
+
];
|
|
542
|
+
break;
|
|
543
|
+
}
|
|
544
|
+
case "move":
|
|
545
|
+
tuples[i] = [
|
|
546
|
+
4,
|
|
547
|
+
pathForMsgpack(op.from),
|
|
548
|
+
pathForMsgpack(op.to)
|
|
549
|
+
];
|
|
550
|
+
break;
|
|
551
|
+
case "str_splice": {
|
|
552
|
+
const at = checkEncodeNonNegSafeInt(op.at, "at");
|
|
553
|
+
const del = checkEncodeNonNegSafeInt(op.del, "del");
|
|
554
|
+
if (at + del > Number.MAX_SAFE_INTEGER) throw new require_diff.DecodeError("InvalidFormat", "str_splice at + del exceeds MAX_SAFE_INTEGER");
|
|
555
|
+
checkNoLoneSurrogates(op.ins);
|
|
556
|
+
tuples[i] = [
|
|
557
|
+
5,
|
|
558
|
+
pathForMsgpack(op.path),
|
|
559
|
+
nonNegIntForMsgpack(at),
|
|
560
|
+
nonNegIntForMsgpack(del),
|
|
561
|
+
op.ins
|
|
562
|
+
];
|
|
563
|
+
break;
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
return (0, __msgpack_msgpack.encode)(tuples, {
|
|
568
|
+
useBigInt64: true,
|
|
569
|
+
sortKeys: true
|
|
570
|
+
});
|
|
571
|
+
}
|
|
572
|
+
function decodeMsgpackRaw(bytes) {
|
|
573
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
574
|
+
const total = bytes.byteLength;
|
|
575
|
+
let pos = 0;
|
|
576
|
+
function need(n) {
|
|
577
|
+
if (pos + n > total) throw new require_diff.DecodeError("MessagePack", "unexpected end of MessagePack input");
|
|
578
|
+
}
|
|
579
|
+
function readU8() {
|
|
580
|
+
need(1);
|
|
581
|
+
return view.getUint8(pos++);
|
|
582
|
+
}
|
|
583
|
+
function readU16() {
|
|
584
|
+
need(2);
|
|
585
|
+
const v = view.getUint16(pos, false);
|
|
586
|
+
pos += 2;
|
|
587
|
+
return v;
|
|
588
|
+
}
|
|
589
|
+
function readU32() {
|
|
590
|
+
need(4);
|
|
591
|
+
const v = view.getUint32(pos, false);
|
|
592
|
+
pos += 4;
|
|
593
|
+
return v;
|
|
594
|
+
}
|
|
595
|
+
function readI8() {
|
|
596
|
+
need(1);
|
|
597
|
+
return view.getInt8(pos++);
|
|
598
|
+
}
|
|
599
|
+
function readI16() {
|
|
600
|
+
need(2);
|
|
601
|
+
const v = view.getInt16(pos, false);
|
|
602
|
+
pos += 2;
|
|
603
|
+
return v;
|
|
604
|
+
}
|
|
605
|
+
function readI32() {
|
|
606
|
+
need(4);
|
|
607
|
+
const v = view.getInt32(pos, false);
|
|
608
|
+
pos += 4;
|
|
609
|
+
return v;
|
|
610
|
+
}
|
|
611
|
+
function readStr(byteLen) {
|
|
612
|
+
need(byteLen);
|
|
613
|
+
const slice = bytes.subarray(pos, pos + byteLen);
|
|
614
|
+
pos += byteLen;
|
|
615
|
+
try {
|
|
616
|
+
return UTF8_DECODER.decode(slice);
|
|
617
|
+
} catch (err) {
|
|
618
|
+
throw new require_diff.DecodeError("MessagePack", `invalid UTF-8 string in MessagePack: ${String(err)}`);
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
function readBin(byteLen) {
|
|
622
|
+
need(byteLen);
|
|
623
|
+
const out = bytes.slice(pos, pos + byteLen);
|
|
624
|
+
pos += byteLen;
|
|
625
|
+
return out;
|
|
626
|
+
}
|
|
627
|
+
function readArray(len) {
|
|
628
|
+
const arr = new Array(len);
|
|
629
|
+
for (let i = 0; i < len; i++) arr[i] = readValue();
|
|
630
|
+
return arr;
|
|
631
|
+
}
|
|
632
|
+
function readMap(len) {
|
|
633
|
+
const obj = {};
|
|
634
|
+
for (let i = 0; i < len; i++) {
|
|
635
|
+
const k = readValue();
|
|
636
|
+
if (typeof k !== "string") throw new require_diff.DecodeError("InvalidFormat", "map keys must be strings");
|
|
637
|
+
require_diff.setOwnProp(obj, k, readValue());
|
|
638
|
+
}
|
|
639
|
+
return obj;
|
|
640
|
+
}
|
|
641
|
+
function readValue() {
|
|
642
|
+
const b = readU8();
|
|
643
|
+
if (b <= 127) return b;
|
|
644
|
+
if (b >= 224) return b - 256;
|
|
645
|
+
if ((b & 240) === 128) return readMap(b & 15);
|
|
646
|
+
if ((b & 240) === 144) return readArray(b & 15);
|
|
647
|
+
if ((b & 224) === 160) return readStr(b & 31);
|
|
648
|
+
switch (b) {
|
|
649
|
+
case 192: return null;
|
|
650
|
+
case 193: throw new require_diff.DecodeError("MessagePack", "encountered reserved 0xc1 byte");
|
|
651
|
+
case 194: return false;
|
|
652
|
+
case 195: return true;
|
|
653
|
+
case 196: return readBin(readU8());
|
|
654
|
+
case 197: return readBin(readU16());
|
|
655
|
+
case 198: return readBin(readU32());
|
|
656
|
+
case 199: {
|
|
657
|
+
const len = readU8();
|
|
658
|
+
const extType = readI8();
|
|
659
|
+
need(len);
|
|
660
|
+
throw new require_diff.DecodeError("UnsupportedExtension", `custom MessagePack extension (${extType}) is forbidden`);
|
|
661
|
+
}
|
|
662
|
+
case 200: {
|
|
663
|
+
const len = readU16();
|
|
664
|
+
const extType = readI8();
|
|
665
|
+
need(len);
|
|
666
|
+
throw new require_diff.DecodeError("UnsupportedExtension", `custom MessagePack extension (${extType}) is forbidden`);
|
|
667
|
+
}
|
|
668
|
+
case 201: {
|
|
669
|
+
const len = readU32();
|
|
670
|
+
const extType = readI8();
|
|
671
|
+
need(len);
|
|
672
|
+
throw new require_diff.DecodeError("UnsupportedExtension", `custom MessagePack extension (${extType}) is forbidden`);
|
|
673
|
+
}
|
|
674
|
+
case 202: {
|
|
675
|
+
need(4);
|
|
676
|
+
const f = view.getFloat32(pos, false);
|
|
677
|
+
pos += 4;
|
|
678
|
+
if (!Number.isFinite(f)) throw new require_diff.DecodeError("NonFiniteFloat", "non-finite float (NaN or Infinity) is forbidden");
|
|
679
|
+
return new FloatToken(Object.is(f, -0) ? 0 : f);
|
|
680
|
+
}
|
|
681
|
+
case 203: {
|
|
682
|
+
need(8);
|
|
683
|
+
const f = view.getFloat64(pos, false);
|
|
684
|
+
pos += 8;
|
|
685
|
+
if (!Number.isFinite(f)) throw new require_diff.DecodeError("NonFiniteFloat", "non-finite float (NaN or Infinity) is forbidden");
|
|
686
|
+
return new FloatToken(Object.is(f, -0) ? 0 : f);
|
|
687
|
+
}
|
|
688
|
+
case 204: return readU8();
|
|
689
|
+
case 205: return readU16();
|
|
690
|
+
case 206: return readU32();
|
|
691
|
+
case 207: {
|
|
692
|
+
need(8);
|
|
693
|
+
const u = view.getBigUint64(pos, false);
|
|
694
|
+
pos += 8;
|
|
695
|
+
return u <= require_diff.MAX_SAFE_BIG ? Number(u) : u;
|
|
696
|
+
}
|
|
697
|
+
case 208: return readI8();
|
|
698
|
+
case 209: return readI16();
|
|
699
|
+
case 210: return readI32();
|
|
700
|
+
case 211: {
|
|
701
|
+
need(8);
|
|
702
|
+
const i = view.getBigInt64(pos, false);
|
|
703
|
+
pos += 8;
|
|
704
|
+
return i >= require_diff.MIN_SAFE_BIG && i <= require_diff.MAX_SAFE_BIG ? Number(i) : i;
|
|
705
|
+
}
|
|
706
|
+
case 212:
|
|
707
|
+
case 213:
|
|
708
|
+
case 214:
|
|
709
|
+
case 215:
|
|
710
|
+
case 216: {
|
|
711
|
+
const extLen = 1 << b - 212;
|
|
712
|
+
const extType = readI8();
|
|
713
|
+
need(extLen);
|
|
714
|
+
throw new require_diff.DecodeError("UnsupportedExtension", `custom MessagePack extension (${extType}) is forbidden`);
|
|
715
|
+
}
|
|
716
|
+
case 217: return readStr(readU8());
|
|
717
|
+
case 218: return readStr(readU16());
|
|
718
|
+
case 219: return readStr(readU32());
|
|
719
|
+
case 220: return readArray(readU16());
|
|
720
|
+
case 221: return readArray(readU32());
|
|
721
|
+
case 222: return readMap(readU16());
|
|
722
|
+
case 223: return readMap(readU32());
|
|
723
|
+
default: throw new require_diff.DecodeError("MessagePack", `unexpected MessagePack tag 0x${b.toString(16)}`);
|
|
724
|
+
}
|
|
725
|
+
}
|
|
726
|
+
const root = readValue();
|
|
727
|
+
if (pos !== total) throw new require_diff.DecodeError("InvalidFormat", "trailing bytes after msgpack patch");
|
|
728
|
+
return root;
|
|
729
|
+
}
|
|
730
|
+
/**
|
|
731
|
+
* Decodes compact binary MessagePack op-tuples back into validated {@link JsonOp} operations.
|
|
732
|
+
* @throws {@link DecodeError} on unknown op codes, unsupported MsgPack extensions, or trailing bytes.
|
|
733
|
+
*/
|
|
734
|
+
function decodeMsgpack(bytes) {
|
|
735
|
+
const root = decodeMsgpackRaw(bytes);
|
|
736
|
+
if (!Array.isArray(root)) throw new require_diff.DecodeError("InvalidFormat", "msgpack patch root must be an array");
|
|
737
|
+
const out = [];
|
|
738
|
+
for (let i = 0; i < root.length; i++) {
|
|
739
|
+
const tuple = root[i];
|
|
740
|
+
if (!Array.isArray(tuple) || tuple.length === 0) throw new require_diff.DecodeError("InvalidFormat", "each msgpack op must be a non-empty array");
|
|
741
|
+
const rawCode = tuple[0];
|
|
742
|
+
let code;
|
|
743
|
+
if (typeof rawCode === "number" && Number.isInteger(rawCode)) code = rawCode;
|
|
744
|
+
else if (typeof rawCode === "bigint" && rawCode >= require_diff.I64_MIN && rawCode <= require_diff.I64_MAX) code = rawCode;
|
|
745
|
+
else throw new require_diff.DecodeError("InvalidFormat", "op code must be an integer");
|
|
746
|
+
if (code === 1 || code === 1n) {
|
|
747
|
+
if (tuple.length < 3) throw new require_diff.DecodeError("InvalidFormat", "op 1 (set) requires at least 3 elements");
|
|
748
|
+
out.push({
|
|
749
|
+
op: "set",
|
|
750
|
+
path: parsePath(tuple[1]),
|
|
751
|
+
value: decodeRawJsonValue(tuple[2], "reject")
|
|
752
|
+
});
|
|
753
|
+
} else if (code === 2 || code === 2n) {
|
|
754
|
+
if (tuple.length < 2) throw new require_diff.DecodeError("InvalidFormat", "op 2 (unset) requires at least 2 elements");
|
|
755
|
+
out.push({
|
|
756
|
+
op: "unset",
|
|
757
|
+
path: parsePath(tuple[1])
|
|
758
|
+
});
|
|
759
|
+
} else if (code === 3 || code === 3n) {
|
|
760
|
+
if (tuple.length < 5) throw new require_diff.DecodeError("InvalidFormat", "op 3 (arr_splice) requires at least 5 elements");
|
|
761
|
+
const itemsRaw = tuple[4];
|
|
762
|
+
if (!Array.isArray(itemsRaw)) throw new require_diff.DecodeError("InvalidFormat", "op 3 (arr_splice) items must be an array");
|
|
763
|
+
out.push({
|
|
764
|
+
op: "arr_splice",
|
|
765
|
+
path: parsePath(tuple[1]),
|
|
766
|
+
at: parseNonNegSafeInt(tuple[2], "at"),
|
|
767
|
+
del: parseNonNegSafeInt(tuple[3], "del"),
|
|
768
|
+
items: itemsRaw.map((v) => decodeRawJsonValue(v, "reject"))
|
|
769
|
+
});
|
|
770
|
+
} else if (code === 4 || code === 4n) {
|
|
771
|
+
if (tuple.length < 3) throw new require_diff.DecodeError("InvalidFormat", "op 4 (move) requires at least 3 elements");
|
|
772
|
+
out.push({
|
|
773
|
+
op: "move",
|
|
774
|
+
from: parsePath(tuple[1]),
|
|
775
|
+
to: parsePath(tuple[2])
|
|
776
|
+
});
|
|
777
|
+
} else if (code === 5 || code === 5n) {
|
|
778
|
+
if (tuple.length < 5) throw new require_diff.DecodeError("InvalidFormat", "op 5 (str_splice) requires at least 5 elements");
|
|
779
|
+
const ins = tuple[4];
|
|
780
|
+
if (typeof ins !== "string") throw new require_diff.DecodeError("InvalidFormat", "op 5 (str_splice) ins must be a string");
|
|
781
|
+
checkNoLoneSurrogates(ins);
|
|
782
|
+
out.push({
|
|
783
|
+
op: "str_splice",
|
|
784
|
+
path: parsePath(tuple[1]),
|
|
785
|
+
at: parseNonNegSafeInt(tuple[2], "at"),
|
|
786
|
+
del: parseNonNegSafeInt(tuple[3], "del"),
|
|
787
|
+
ins
|
|
788
|
+
});
|
|
789
|
+
} else throw new require_diff.DecodeError("UnknownOp", `unknown op code: ${String(code)}`, code);
|
|
790
|
+
}
|
|
791
|
+
return out;
|
|
792
|
+
}
|
|
793
|
+
/**
|
|
794
|
+
* Encodes one plain JSON value as MessagePack with the same value rules as the compact patch codec
|
|
795
|
+
* (sorted object keys, minimal integer widths, i64/u64 as bigint, `Uint8Array` as `bin`, no extension types).
|
|
796
|
+
*/
|
|
797
|
+
function encodeMsgpackValue(value) {
|
|
798
|
+
return (0, __msgpack_msgpack.encode)(normalizeForMsgpack(value), {
|
|
799
|
+
useBigInt64: true,
|
|
800
|
+
sortKeys: true
|
|
801
|
+
});
|
|
802
|
+
}
|
|
803
|
+
/**
|
|
804
|
+
* Decodes one MessagePack value with the same value rules as the compact patch codec: integers in the
|
|
805
|
+
* safe range are numbers, larger i64/u64 are bigint, `bin` is `Uint8Array`, extension types, non-finite
|
|
806
|
+
* floats and trailing bytes are rejected with `DecodeError`.
|
|
807
|
+
*/
|
|
808
|
+
function decodeMsgpackValue(bytes) {
|
|
809
|
+
return decodeRawJsonValue(decodeMsgpackRaw(bytes), "reject");
|
|
810
|
+
}
|
|
811
|
+
|
|
812
|
+
//#endregion
|
|
813
|
+
//#region src/compact.ts
|
|
814
|
+
function isPathValid(path) {
|
|
815
|
+
for (let i = 0; i < path.length; i++) {
|
|
816
|
+
const seg = path[i];
|
|
817
|
+
if (typeof seg === "string") {
|
|
818
|
+
if (require_diff.hasLoneSurrogates(seg)) return false;
|
|
819
|
+
} else if (typeof seg !== "number" || !Number.isSafeInteger(seg) || seg < 0) return false;
|
|
820
|
+
}
|
|
821
|
+
return true;
|
|
822
|
+
}
|
|
823
|
+
function isValueValid(val) {
|
|
824
|
+
try {
|
|
825
|
+
require_diff.validateJsonValue(val, 0);
|
|
826
|
+
return true;
|
|
827
|
+
} catch {
|
|
828
|
+
return false;
|
|
829
|
+
}
|
|
830
|
+
}
|
|
831
|
+
function isOpStaticallyValid(op) {
|
|
832
|
+
switch (op.op) {
|
|
833
|
+
case "set": return isPathValid(op.path) && isValueValid(op.value);
|
|
834
|
+
case "unset": return op.path.length > 0 && typeof op.path[op.path.length - 1] === "string" && isPathValid(op.path);
|
|
835
|
+
case "arr_splice": return isPathValid(op.path) && Number.isSafeInteger(op.at) && op.at >= 0 && Number.isSafeInteger(op.del) && op.del >= 0 && op.at + op.del <= Number.MAX_SAFE_INTEGER && op.items.every(isValueValid);
|
|
836
|
+
case "move": return op.from.length > 0 && op.to.length > 0 && isPathValid(op.from) && isPathValid(op.to);
|
|
837
|
+
case "str_splice": return isPathValid(op.path) && Number.isSafeInteger(op.at) && op.at >= 0 && Number.isSafeInteger(op.del) && op.del >= 0 && op.at + op.del <= Number.MAX_SAFE_INTEGER && !require_diff.hasLoneSurrogates(op.ins);
|
|
838
|
+
}
|
|
839
|
+
}
|
|
840
|
+
function opTargetPath(op) {
|
|
841
|
+
switch (op.op) {
|
|
842
|
+
case "set":
|
|
843
|
+
case "unset":
|
|
844
|
+
case "arr_splice":
|
|
845
|
+
case "str_splice": return op.path;
|
|
846
|
+
case "move": return;
|
|
847
|
+
}
|
|
848
|
+
}
|
|
849
|
+
function pathPrefixKeys(path) {
|
|
850
|
+
const keys = new Array(path.length + 1);
|
|
851
|
+
let acc = "";
|
|
852
|
+
keys[0] = acc;
|
|
853
|
+
for (let i = 0; i < path.length; i++) {
|
|
854
|
+
const seg = path[i];
|
|
855
|
+
if (typeof seg === "string") acc += `k${seg.length}:${seg}`;
|
|
856
|
+
else acc += `i${seg};`;
|
|
857
|
+
keys[i + 1] = acc;
|
|
858
|
+
}
|
|
859
|
+
return keys;
|
|
860
|
+
}
|
|
861
|
+
var ConflictTracker = class {
|
|
862
|
+
barrierIdx = -1;
|
|
863
|
+
lastExact = /* @__PURE__ */ new Map();
|
|
864
|
+
lastTouch = /* @__PURE__ */ new Map();
|
|
865
|
+
maxDescendant = /* @__PURE__ */ new Map();
|
|
866
|
+
recordTouch(path, idx) {
|
|
867
|
+
const keys = pathPrefixKeys(path);
|
|
868
|
+
this.lastTouch.set(keys[keys.length - 1], idx);
|
|
869
|
+
for (let len = 0; len < keys.length - 1; len++) this.maxDescendant.set(keys[len], idx);
|
|
870
|
+
}
|
|
871
|
+
recordOp(idx, op, valid) {
|
|
872
|
+
if (!valid) {
|
|
873
|
+
this.barrierIdx = idx;
|
|
874
|
+
return;
|
|
875
|
+
}
|
|
876
|
+
if (op.op === "move") {
|
|
877
|
+
const fp = op.from.length > 0 && typeof op.from[op.from.length - 1] === "number" ? op.from.slice(0, -1) : op.from;
|
|
878
|
+
const tp = op.to.length > 0 && typeof op.to[op.to.length - 1] === "number" ? op.to.slice(0, -1) : op.to;
|
|
879
|
+
this.recordTouch(fp, idx);
|
|
880
|
+
this.recordTouch(tp, idx);
|
|
881
|
+
return;
|
|
882
|
+
}
|
|
883
|
+
const keys = pathPrefixKeys(opTargetPath(op));
|
|
884
|
+
this.lastExact.set(keys[keys.length - 1], idx);
|
|
885
|
+
this.lastTouch.set(keys[keys.length - 1], idx);
|
|
886
|
+
for (let len = 0; len < keys.length - 1; len++) this.maxDescendant.set(keys[len], idx);
|
|
887
|
+
}
|
|
888
|
+
foldableCandidate(path) {
|
|
889
|
+
const keys = pathPrefixKeys(path);
|
|
890
|
+
const fullKey = keys[keys.length - 1];
|
|
891
|
+
const cand = this.lastExact.get(fullKey);
|
|
892
|
+
if (cand === void 0) return void 0;
|
|
893
|
+
if (this.barrierIdx > cand) return void 0;
|
|
894
|
+
for (let len = 0; len < keys.length; len++) {
|
|
895
|
+
const tIdx = this.lastTouch.get(keys[len]);
|
|
896
|
+
if (tIdx !== void 0 && tIdx > cand) return void 0;
|
|
897
|
+
}
|
|
898
|
+
const dIdx = this.maxDescendant.get(fullKey);
|
|
899
|
+
if (dIdx !== void 0 && dIdx > cand) return void 0;
|
|
900
|
+
return cand;
|
|
901
|
+
}
|
|
902
|
+
};
|
|
903
|
+
function tryMergeStrSplice(op1, op2) {
|
|
904
|
+
if (!require_diff.pathsEqual(op1.path, op2.path)) return void 0;
|
|
905
|
+
const end1InPost = op1.at + op1.ins.length;
|
|
906
|
+
const end2 = op2.at + op2.del;
|
|
907
|
+
if (end1InPost > Number.MAX_SAFE_INTEGER || end2 > Number.MAX_SAFE_INTEGER) return;
|
|
908
|
+
if (op2.at === end1InPost) {
|
|
909
|
+
const newDel = op1.del + op2.del;
|
|
910
|
+
if (newDel > Number.MAX_SAFE_INTEGER) return void 0;
|
|
911
|
+
return {
|
|
912
|
+
op: "str_splice",
|
|
913
|
+
path: op1.path,
|
|
914
|
+
at: op1.at,
|
|
915
|
+
del: newDel,
|
|
916
|
+
ins: op1.ins + op2.ins
|
|
917
|
+
};
|
|
918
|
+
}
|
|
919
|
+
if (op2.at >= op1.at && end2 <= end1InPost) {
|
|
920
|
+
const localAt = op2.at - op1.at;
|
|
921
|
+
const localEnd = end2 - op1.at;
|
|
922
|
+
try {
|
|
923
|
+
require_diff.assertValidUtf16Boundary(op1.ins, localAt, 0);
|
|
924
|
+
require_diff.assertValidUtf16Boundary(op1.ins, localEnd, 0);
|
|
925
|
+
} catch {
|
|
926
|
+
return;
|
|
927
|
+
}
|
|
928
|
+
return {
|
|
929
|
+
op: "str_splice",
|
|
930
|
+
path: op1.path,
|
|
931
|
+
at: op1.at,
|
|
932
|
+
del: op1.del,
|
|
933
|
+
ins: op1.ins.slice(0, localAt) + op2.ins + op1.ins.slice(localEnd)
|
|
934
|
+
};
|
|
935
|
+
}
|
|
936
|
+
if (op2.at >= op1.at && op2.at <= end1InPost && end2 > end1InPost) {
|
|
937
|
+
const localAt = op2.at - op1.at;
|
|
938
|
+
try {
|
|
939
|
+
require_diff.assertValidUtf16Boundary(op1.ins, localAt, 0);
|
|
940
|
+
} catch {
|
|
941
|
+
return;
|
|
942
|
+
}
|
|
943
|
+
const extraDel = end2 - end1InPost;
|
|
944
|
+
const newDel = op1.del + extraDel;
|
|
945
|
+
if (newDel > Number.MAX_SAFE_INTEGER) return void 0;
|
|
946
|
+
return {
|
|
947
|
+
op: "str_splice",
|
|
948
|
+
path: op1.path,
|
|
949
|
+
at: op1.at,
|
|
950
|
+
del: newDel,
|
|
951
|
+
ins: op1.ins.slice(0, localAt) + op2.ins
|
|
952
|
+
};
|
|
953
|
+
}
|
|
954
|
+
if (op2.at <= op1.at && end2 >= op1.at) if (end2 <= end1InPost) {
|
|
955
|
+
const overlap = end2 - op1.at;
|
|
956
|
+
try {
|
|
957
|
+
require_diff.assertValidUtf16Boundary(op1.ins, overlap, 0);
|
|
958
|
+
} catch {
|
|
959
|
+
return;
|
|
960
|
+
}
|
|
961
|
+
const extraDel = op1.at - op2.at;
|
|
962
|
+
const newDel = op1.del + extraDel;
|
|
963
|
+
if (newDel > Number.MAX_SAFE_INTEGER) return void 0;
|
|
964
|
+
return {
|
|
965
|
+
op: "str_splice",
|
|
966
|
+
path: op1.path,
|
|
967
|
+
at: op2.at,
|
|
968
|
+
del: newDel,
|
|
969
|
+
ins: op2.ins + op1.ins.slice(overlap)
|
|
970
|
+
};
|
|
971
|
+
} else {
|
|
972
|
+
const extraDel = end2 - end1InPost + (op1.at - op2.at);
|
|
973
|
+
const newDel = op1.del + extraDel;
|
|
974
|
+
if (newDel > Number.MAX_SAFE_INTEGER) return void 0;
|
|
975
|
+
return {
|
|
976
|
+
op: "str_splice",
|
|
977
|
+
path: op1.path,
|
|
978
|
+
at: op2.at,
|
|
979
|
+
del: newDel,
|
|
980
|
+
ins: op2.ins
|
|
981
|
+
};
|
|
982
|
+
}
|
|
983
|
+
}
|
|
984
|
+
function tryFoldSamePath(prev, next) {
|
|
985
|
+
if (prev.op === "str_splice" && next.op === "str_splice") return tryMergeStrSplice(prev, next);
|
|
986
|
+
if (prev.op === "set" && next.op === "set" && require_diff.pathsEqual(prev.path, next.path)) return {
|
|
987
|
+
op: "set",
|
|
988
|
+
path: next.path,
|
|
989
|
+
value: require_diff.validateAndNormalizeJsonValue(next.value, 0)
|
|
990
|
+
};
|
|
991
|
+
if (prev.op === "set" && next.op === "unset" && next.path.length > 0 && typeof next.path[next.path.length - 1] === "string" && require_diff.pathsEqual(prev.path, next.path)) return next;
|
|
992
|
+
if (prev.op === "unset" && next.op === "set" && next.path.length > 0 && typeof next.path[next.path.length - 1] === "string" && require_diff.pathsEqual(prev.path, next.path)) return {
|
|
993
|
+
op: "set",
|
|
994
|
+
path: next.path,
|
|
995
|
+
value: require_diff.validateAndNormalizeJsonValue(next.value, 0)
|
|
996
|
+
};
|
|
997
|
+
if (prev.op === "unset" && next.op === "unset" && require_diff.pathsEqual(prev.path, next.path)) return prev;
|
|
998
|
+
if (prev.op === "set" && next.op === "str_splice" && require_diff.pathsEqual(prev.path, next.path) && typeof prev.value === "string") try {
|
|
999
|
+
const nextVal = require_diff.applyOp(prev.value, {
|
|
1000
|
+
...next,
|
|
1001
|
+
path: []
|
|
1002
|
+
}, 0);
|
|
1003
|
+
return {
|
|
1004
|
+
op: "set",
|
|
1005
|
+
path: prev.path,
|
|
1006
|
+
value: nextVal
|
|
1007
|
+
};
|
|
1008
|
+
} catch {
|
|
1009
|
+
return;
|
|
1010
|
+
}
|
|
1011
|
+
if (prev.op === "set" && next.op === "arr_splice" && require_diff.pathsEqual(prev.path, next.path) && Array.isArray(prev.value)) try {
|
|
1012
|
+
const nextVal = require_diff.applyOp(prev.value, {
|
|
1013
|
+
...next,
|
|
1014
|
+
path: []
|
|
1015
|
+
}, 0);
|
|
1016
|
+
return {
|
|
1017
|
+
op: "set",
|
|
1018
|
+
path: prev.path,
|
|
1019
|
+
value: nextVal
|
|
1020
|
+
};
|
|
1021
|
+
} catch {
|
|
1022
|
+
return;
|
|
1023
|
+
}
|
|
1024
|
+
}
|
|
1025
|
+
/**
|
|
1026
|
+
* Stateless O(M) compactor for slow consumers and streaming patches.
|
|
1027
|
+
* Preserves unvalidated non-root prefixes and statically invalid ops as barriers.
|
|
1028
|
+
*/
|
|
1029
|
+
function compact(patch) {
|
|
1030
|
+
let hasRootSnap = false;
|
|
1031
|
+
let rootSnap = null;
|
|
1032
|
+
const out = [];
|
|
1033
|
+
const tracker = new ConflictTracker();
|
|
1034
|
+
for (let i = 0; i < patch.length; i++) {
|
|
1035
|
+
const op = patch[i];
|
|
1036
|
+
if (!isOpStaticallyValid(op)) {
|
|
1037
|
+
if (hasRootSnap) {
|
|
1038
|
+
const snapOp = {
|
|
1039
|
+
op: "set",
|
|
1040
|
+
path: [],
|
|
1041
|
+
value: rootSnap
|
|
1042
|
+
};
|
|
1043
|
+
tracker.recordOp(out.length, snapOp, true);
|
|
1044
|
+
out.push(snapOp);
|
|
1045
|
+
hasRootSnap = false;
|
|
1046
|
+
}
|
|
1047
|
+
tracker.recordOp(out.length, op, false);
|
|
1048
|
+
out.push(op);
|
|
1049
|
+
continue;
|
|
1050
|
+
}
|
|
1051
|
+
if (op.op === "set" && op.path.length === 0) {
|
|
1052
|
+
hasRootSnap = true;
|
|
1053
|
+
rootSnap = require_diff.validateAndNormalizeJsonValue(op.value, i);
|
|
1054
|
+
continue;
|
|
1055
|
+
}
|
|
1056
|
+
if (hasRootSnap) try {
|
|
1057
|
+
rootSnap = require_diff.applyOp(rootSnap, op, i);
|
|
1058
|
+
continue;
|
|
1059
|
+
} catch {
|
|
1060
|
+
const snapOp = {
|
|
1061
|
+
op: "set",
|
|
1062
|
+
path: [],
|
|
1063
|
+
value: rootSnap
|
|
1064
|
+
};
|
|
1065
|
+
tracker.recordOp(out.length, snapOp, true);
|
|
1066
|
+
out.push(snapOp);
|
|
1067
|
+
hasRootSnap = false;
|
|
1068
|
+
tracker.recordOp(out.length, op, true);
|
|
1069
|
+
out.push(op);
|
|
1070
|
+
continue;
|
|
1071
|
+
}
|
|
1072
|
+
let folded = false;
|
|
1073
|
+
const path = opTargetPath(op);
|
|
1074
|
+
if (path !== void 0) {
|
|
1075
|
+
const candIdx = tracker.foldableCandidate(path);
|
|
1076
|
+
if (candIdx !== void 0) {
|
|
1077
|
+
const merged = tryFoldSamePath(out[candIdx], op);
|
|
1078
|
+
if (merged) {
|
|
1079
|
+
out[candIdx] = merged;
|
|
1080
|
+
folded = true;
|
|
1081
|
+
}
|
|
1082
|
+
}
|
|
1083
|
+
}
|
|
1084
|
+
if (!folded) {
|
|
1085
|
+
tracker.recordOp(out.length, op, true);
|
|
1086
|
+
out.push(op);
|
|
1087
|
+
}
|
|
1088
|
+
}
|
|
1089
|
+
if (hasRootSnap) out.push({
|
|
1090
|
+
op: "set",
|
|
1091
|
+
path: [],
|
|
1092
|
+
value: rootSnap
|
|
1093
|
+
});
|
|
1094
|
+
return out;
|
|
1095
|
+
}
|
|
1096
|
+
|
|
1097
|
+
//#endregion
|
|
1098
|
+
exports.ANY_REST_SEG = require_diff.ANY_REST_SEG;
|
|
1099
|
+
exports.ANY_SEG = require_diff.ANY_SEG;
|
|
1100
|
+
exports.ApplyError = require_diff.ApplyError;
|
|
1101
|
+
exports.DecodeError = require_diff.DecodeError;
|
|
1102
|
+
exports.apply = require_diff.apply;
|
|
1103
|
+
exports.applyOp = require_diff.applyOp;
|
|
1104
|
+
exports.assertNoLoneSurrogates = require_diff.assertNoLoneSurrogates;
|
|
1105
|
+
exports.assertValidUtf16Boundary = require_diff.assertValidUtf16Boundary;
|
|
1106
|
+
exports.assertValidUtf16Range = require_diff.assertValidUtf16Range;
|
|
1107
|
+
exports.compact = compact;
|
|
1108
|
+
exports.decodeJson = decodeJson;
|
|
1109
|
+
exports.decodeMsgpack = decodeMsgpack;
|
|
1110
|
+
exports.decodeMsgpackValue = decodeMsgpackValue;
|
|
1111
|
+
exports.diff = require_diff.diff;
|
|
1112
|
+
exports.diffStringAt = require_diff.diffStringAt;
|
|
1113
|
+
exports.encodeJson = encodeJson;
|
|
1114
|
+
exports.encodeMsgpack = encodeMsgpack;
|
|
1115
|
+
exports.encodeMsgpackValue = encodeMsgpackValue;
|
|
1116
|
+
exports.isPlainObject = require_diff.isPlainObject;
|
|
1117
|
+
exports.lisMask = require_diff.lisMask;
|
|
1118
|
+
exports.lisMovePairs = require_diff.lisMovePairs;
|
|
1119
|
+
exports.numbersEqual = require_diff.numbersEqual;
|
|
1120
|
+
exports.pathsEqual = require_diff.pathsEqual;
|
|
1121
|
+
exports.pathsOverlap = require_diff.pathsOverlap;
|
|
1122
|
+
exports.patternMatches = require_diff.patternMatches;
|
|
1123
|
+
exports.validateJsonValue = require_diff.validateJsonValue;
|
|
1124
|
+
exports.valuesEqual = require_diff.valuesEqual;
|