@graffy/common 0.15.8-alpha.2 → 0.15.8
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/index.cjs +1561 -0
- package/index.mjs +1498 -0
- package/package.json +9 -8
- package/types/coding/string.d.ts +2 -2
- package/cjs/coding/alphabet.js +0 -12
- package/cjs/coding/args.js +0 -151
- package/cjs/coding/base64.js +0 -61
- package/cjs/coding/decodeTree.js +0 -253
- package/cjs/coding/decorate.js +0 -279
- package/cjs/coding/encodeTree.js +0 -278
- package/cjs/coding/id.js +0 -16
- package/cjs/coding/index.js +0 -83
- package/cjs/coding/number.js +0 -43
- package/cjs/coding/path.js +0 -77
- package/cjs/coding/serialize.js +0 -17
- package/cjs/coding/string.js +0 -27
- package/cjs/coding/struct.js +0 -209
- package/cjs/coding/url.js +0 -47
- package/cjs/index.js +0 -57
- package/cjs/node/find.js +0 -24
- package/cjs/node/index.js +0 -25
- package/cjs/node/types.js +0 -38
- package/cjs/object.js +0 -128
- package/cjs/ops/add.js +0 -106
- package/cjs/ops/finalize.js +0 -31
- package/cjs/ops/getKnown.js +0 -64
- package/cjs/ops/index.js +0 -67
- package/cjs/ops/merge.js +0 -177
- package/cjs/ops/path.js +0 -140
- package/cjs/ops/setVersion.js +0 -54
- package/cjs/ops/sieve.js +0 -273
- package/cjs/ops/slice.js +0 -228
- package/cjs/ops/step.js +0 -56
- package/cjs/provider/index.js +0 -26
- package/cjs/stream/index.js +0 -14
- package/cjs/stream/makeWatcher.js +0 -64
- package/cjs/stream/mergeStreams.js +0 -148
- package/cjs/util.js +0 -83
package/index.cjs
ADDED
|
@@ -0,0 +1,1561 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __defProps = Object.defineProperties;
|
|
4
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
5
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
8
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
9
|
+
var __spreadValues = (a, b) => {
|
|
10
|
+
for (var prop in b || (b = {}))
|
|
11
|
+
if (__hasOwnProp.call(b, prop))
|
|
12
|
+
__defNormalProp(a, prop, b[prop]);
|
|
13
|
+
if (__getOwnPropSymbols)
|
|
14
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
15
|
+
if (__propIsEnum.call(b, prop))
|
|
16
|
+
__defNormalProp(a, prop, b[prop]);
|
|
17
|
+
}
|
|
18
|
+
return a;
|
|
19
|
+
};
|
|
20
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
21
|
+
var __objRest = (source, exclude) => {
|
|
22
|
+
var target = {};
|
|
23
|
+
for (var prop in source)
|
|
24
|
+
if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
|
|
25
|
+
target[prop] = source[prop];
|
|
26
|
+
if (source != null && __getOwnPropSymbols)
|
|
27
|
+
for (var prop of __getOwnPropSymbols(source)) {
|
|
28
|
+
if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
|
|
29
|
+
target[prop] = source[prop];
|
|
30
|
+
}
|
|
31
|
+
return target;
|
|
32
|
+
};
|
|
33
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
34
|
+
exports[Symbol.toStringTag] = "Module";
|
|
35
|
+
var nonSecure = require("nanoid/non-secure");
|
|
36
|
+
var mergeIterators = require("merge-async-iterators");
|
|
37
|
+
var stream = require("@graffy/stream");
|
|
38
|
+
var isEqual = require("lodash/isEqual.js");
|
|
39
|
+
function _interopDefaultLegacy(e) {
|
|
40
|
+
return e && typeof e === "object" && "default" in e ? e : { "default": e };
|
|
41
|
+
}
|
|
42
|
+
var mergeIterators__default = /* @__PURE__ */ _interopDefaultLegacy(mergeIterators);
|
|
43
|
+
var isEqual__default = /* @__PURE__ */ _interopDefaultLegacy(isEqual);
|
|
44
|
+
function encode$7(query) {
|
|
45
|
+
return encodeURIComponent(JSON.stringify(query));
|
|
46
|
+
}
|
|
47
|
+
function decode$7(fields) {
|
|
48
|
+
return JSON.parse(decodeURIComponent(fields));
|
|
49
|
+
}
|
|
50
|
+
const textEncoder = new TextEncoder();
|
|
51
|
+
const textDecoder = new TextDecoder("utf-8");
|
|
52
|
+
function encode$6(string) {
|
|
53
|
+
return textEncoder.encode(string);
|
|
54
|
+
}
|
|
55
|
+
function decode$6(u8Arr) {
|
|
56
|
+
return textDecoder.decode(u8Arr);
|
|
57
|
+
}
|
|
58
|
+
function encode$5(number) {
|
|
59
|
+
const buffer = new ArrayBuffer(8);
|
|
60
|
+
const view = new DataView(buffer);
|
|
61
|
+
view.setFloat64(0, number);
|
|
62
|
+
if (number < 0) {
|
|
63
|
+
view.setUint32(0, ~view.getUint32(0) >>> 0);
|
|
64
|
+
view.setUint32(4, ~view.getUint32(4) >>> 0);
|
|
65
|
+
} else {
|
|
66
|
+
view.setUint8(0, view.getUint8(0) | 128);
|
|
67
|
+
}
|
|
68
|
+
return new Uint8Array(buffer);
|
|
69
|
+
}
|
|
70
|
+
function decode$5(u8Arr) {
|
|
71
|
+
const { buffer, byteOffset, byteLength } = u8Arr;
|
|
72
|
+
const view = new DataView(buffer, byteOffset, byteLength);
|
|
73
|
+
const high = view.getUint8(0);
|
|
74
|
+
if (high & 128) {
|
|
75
|
+
view.setUint8(0, high & 127);
|
|
76
|
+
} else {
|
|
77
|
+
view.setUint32(0, ~view.getUint32(0) >>> 0);
|
|
78
|
+
view.setUint32(4, ~view.getUint32(4) >>> 0);
|
|
79
|
+
}
|
|
80
|
+
return view.getFloat64(0);
|
|
81
|
+
}
|
|
82
|
+
var alpha = "-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz";
|
|
83
|
+
function getByte(view, offset) {
|
|
84
|
+
return offset < view.byteLength ? view.getUint8(offset) : 0;
|
|
85
|
+
}
|
|
86
|
+
function getChar(string, offset) {
|
|
87
|
+
return offset < string.length ? alpha.indexOf(string[offset]) : 0;
|
|
88
|
+
}
|
|
89
|
+
function encode$4(u8Arr) {
|
|
90
|
+
const { buffer, byteOffset, byteLength } = u8Arr;
|
|
91
|
+
const view = new DataView(buffer, byteOffset, byteLength);
|
|
92
|
+
let str = "";
|
|
93
|
+
for (let i = 0; i < view.byteLength; i += 3) {
|
|
94
|
+
let value = (getByte(view, i) << 16) + (getByte(view, i + 1) << 8) + getByte(view, i + 2);
|
|
95
|
+
let gstr = "";
|
|
96
|
+
for (let j = 0; j < 4; j++) {
|
|
97
|
+
gstr = alpha[value & 63] + gstr;
|
|
98
|
+
value = value >> 6 | 0;
|
|
99
|
+
}
|
|
100
|
+
str += gstr;
|
|
101
|
+
}
|
|
102
|
+
return str.substr(0, Math.ceil(view.byteLength * 4 / 3));
|
|
103
|
+
}
|
|
104
|
+
function decode$4(string, start = 0) {
|
|
105
|
+
const buffer = new ArrayBuffer(Math.floor((string.length - start) * 3 / 4));
|
|
106
|
+
const view = new DataView(buffer);
|
|
107
|
+
for (let i = start; i < string.length; i += 4) {
|
|
108
|
+
let value = (getChar(string, i) << 18) + (getChar(string, i + 1) << 12) + (getChar(string, i + 2) << 6) + getChar(string, i + 3);
|
|
109
|
+
for (let j = i * 3 / 4 + 2; j >= i * 3 / 4; j--) {
|
|
110
|
+
if (j < view.byteLength)
|
|
111
|
+
view.setUint8(j, value & 255);
|
|
112
|
+
value = value >> 8 | 0;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
return new Uint8Array(buffer);
|
|
116
|
+
}
|
|
117
|
+
const END = 0;
|
|
118
|
+
const NULL = 1;
|
|
119
|
+
const FALSE = 2;
|
|
120
|
+
const TRUE = 3;
|
|
121
|
+
const NUM = 4;
|
|
122
|
+
const STR = 5;
|
|
123
|
+
const ARR = 6;
|
|
124
|
+
const OBJ = 7;
|
|
125
|
+
function encodeArray(array) {
|
|
126
|
+
return [
|
|
127
|
+
ARR,
|
|
128
|
+
...array.flatMap((value) => encodeParts(value)),
|
|
129
|
+
END
|
|
130
|
+
];
|
|
131
|
+
}
|
|
132
|
+
function encodeObject(object) {
|
|
133
|
+
const keys = Object.keys(object).sort();
|
|
134
|
+
return [
|
|
135
|
+
OBJ,
|
|
136
|
+
...keys.flatMap((key) => [
|
|
137
|
+
STR,
|
|
138
|
+
encode$6(key),
|
|
139
|
+
END,
|
|
140
|
+
...encodeParts(object[key])
|
|
141
|
+
]),
|
|
142
|
+
END
|
|
143
|
+
];
|
|
144
|
+
}
|
|
145
|
+
function encodeParts(value) {
|
|
146
|
+
if (value === null)
|
|
147
|
+
return [NULL];
|
|
148
|
+
if (value === false)
|
|
149
|
+
return [FALSE];
|
|
150
|
+
if (value === true)
|
|
151
|
+
return [TRUE];
|
|
152
|
+
if (typeof value === "number")
|
|
153
|
+
return [NUM, encode$5(value)];
|
|
154
|
+
if (typeof value === "string")
|
|
155
|
+
return [STR, encode$6(value), END];
|
|
156
|
+
if (Array.isArray(value))
|
|
157
|
+
return encodeArray(value);
|
|
158
|
+
if (typeof value === "object")
|
|
159
|
+
return encodeObject(value);
|
|
160
|
+
return [NULL];
|
|
161
|
+
}
|
|
162
|
+
function encode$3(value) {
|
|
163
|
+
const parts = encodeParts(value);
|
|
164
|
+
while (parts[parts.length - 1] === END)
|
|
165
|
+
parts.pop();
|
|
166
|
+
const length = parts.reduce((sum, part) => sum + (typeof part === "number" ? 1 : part.length), 0);
|
|
167
|
+
const buffer = new Uint8Array(length);
|
|
168
|
+
let i = 0;
|
|
169
|
+
for (const part of parts) {
|
|
170
|
+
if (typeof part === "number") {
|
|
171
|
+
buffer[i] = part;
|
|
172
|
+
i++;
|
|
173
|
+
} else {
|
|
174
|
+
buffer.set(part, i);
|
|
175
|
+
i += part.length;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
return encode$4(buffer);
|
|
179
|
+
}
|
|
180
|
+
const nextKey = new WeakMap();
|
|
181
|
+
function decode$3(key) {
|
|
182
|
+
let i = 0;
|
|
183
|
+
const buffer = decode$4(key, 0);
|
|
184
|
+
const stack = [[]];
|
|
185
|
+
function readString() {
|
|
186
|
+
let start = i;
|
|
187
|
+
while (i < buffer.length && buffer[i] !== END)
|
|
188
|
+
i++;
|
|
189
|
+
const str = decode$6(buffer.subarray(start, i));
|
|
190
|
+
i++;
|
|
191
|
+
return str;
|
|
192
|
+
}
|
|
193
|
+
function pushToken(type, value) {
|
|
194
|
+
const current = stack[stack.length - 1];
|
|
195
|
+
if (type === ARR || type === OBJ)
|
|
196
|
+
stack.push(value);
|
|
197
|
+
if (!current)
|
|
198
|
+
return;
|
|
199
|
+
if (Array.isArray(current)) {
|
|
200
|
+
current.push(value);
|
|
201
|
+
} else {
|
|
202
|
+
if (nextKey.has(current)) {
|
|
203
|
+
current[nextKey.get(current)] = value;
|
|
204
|
+
nextKey.delete(current);
|
|
205
|
+
} else {
|
|
206
|
+
nextKey.set(current, value);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
function popToken() {
|
|
211
|
+
stack.pop();
|
|
212
|
+
}
|
|
213
|
+
while (i < buffer.length) {
|
|
214
|
+
const type = buffer[i];
|
|
215
|
+
const start = ++i;
|
|
216
|
+
switch (type) {
|
|
217
|
+
case END:
|
|
218
|
+
popToken();
|
|
219
|
+
break;
|
|
220
|
+
case NULL:
|
|
221
|
+
pushToken(type, null);
|
|
222
|
+
break;
|
|
223
|
+
case FALSE:
|
|
224
|
+
pushToken(type, false);
|
|
225
|
+
break;
|
|
226
|
+
case TRUE:
|
|
227
|
+
pushToken(type, true);
|
|
228
|
+
break;
|
|
229
|
+
case NUM:
|
|
230
|
+
i += 8;
|
|
231
|
+
pushToken(type, decode$5(buffer.subarray(start, i)));
|
|
232
|
+
break;
|
|
233
|
+
case STR:
|
|
234
|
+
pushToken(type, readString());
|
|
235
|
+
break;
|
|
236
|
+
case ARR:
|
|
237
|
+
pushToken(type, []);
|
|
238
|
+
break;
|
|
239
|
+
case OBJ:
|
|
240
|
+
pushToken(type, {});
|
|
241
|
+
break;
|
|
242
|
+
default:
|
|
243
|
+
throw new Error("Invalid byte " + type + " at " + start);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
return stack[0][0];
|
|
247
|
+
}
|
|
248
|
+
function keyStep(key) {
|
|
249
|
+
if (key === "")
|
|
250
|
+
return { key, step: 1 };
|
|
251
|
+
if (key === "\uFFFF")
|
|
252
|
+
return { key, step: -1 };
|
|
253
|
+
const l = key.length - 1;
|
|
254
|
+
switch (key.charCodeAt(l)) {
|
|
255
|
+
case 0:
|
|
256
|
+
return { key: key.substr(0, l), step: 1 };
|
|
257
|
+
case 65535:
|
|
258
|
+
return {
|
|
259
|
+
key: key.substr(0, l - 1) + String.fromCharCode(key.charCodeAt(l - 1) + 1),
|
|
260
|
+
step: -1
|
|
261
|
+
};
|
|
262
|
+
default:
|
|
263
|
+
return { key, step: 0 };
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
function keyBefore(key) {
|
|
267
|
+
if (key === "" || key === "\uFFFF" || key === "\0" || key === "\0\uFFFF") {
|
|
268
|
+
return key;
|
|
269
|
+
}
|
|
270
|
+
const l = key.length - 1;
|
|
271
|
+
return key.charCodeAt(l) === 0 ? key.substr(0, l) : key.substr(0, l) + String.fromCharCode(key.charCodeAt(l) - 1) + "\uFFFF";
|
|
272
|
+
}
|
|
273
|
+
function keyAfter(key) {
|
|
274
|
+
if (key === "" || key === "\uFFFF" || key === "\0" || key === "\0\uFFFF") {
|
|
275
|
+
return key;
|
|
276
|
+
}
|
|
277
|
+
const l = key.length - 1;
|
|
278
|
+
return key.charCodeAt(l) === 65535 ? key.substr(0, l - 1) + String.fromCharCode(key.charCodeAt(l - 1) + 1) : key + "\0";
|
|
279
|
+
}
|
|
280
|
+
function err(message, _a = {}) {
|
|
281
|
+
var _b = _a, { cause } = _b, args = __objRest(_b, ["cause"]);
|
|
282
|
+
const e = new Error(message + (args ? " " + JSON.stringify(args) : ""));
|
|
283
|
+
e.cause = cause;
|
|
284
|
+
throw e;
|
|
285
|
+
}
|
|
286
|
+
function errIf(message, condition, args) {
|
|
287
|
+
if (condition)
|
|
288
|
+
err(message, args);
|
|
289
|
+
}
|
|
290
|
+
function isEmpty(object) {
|
|
291
|
+
for (const _ in object)
|
|
292
|
+
return false;
|
|
293
|
+
return true;
|
|
294
|
+
}
|
|
295
|
+
function isDef(value) {
|
|
296
|
+
return typeof value !== "undefined";
|
|
297
|
+
}
|
|
298
|
+
function isPlainObject(arg) {
|
|
299
|
+
return typeof arg === "object" && arg && !Array.isArray(arg);
|
|
300
|
+
}
|
|
301
|
+
function isEncodedKey(str) {
|
|
302
|
+
return str[0] === "\0";
|
|
303
|
+
}
|
|
304
|
+
function find(items, compare2, first = 0, last = items.length) {
|
|
305
|
+
let currentFirst = first;
|
|
306
|
+
let currentLast = last;
|
|
307
|
+
while (currentFirst < currentLast) {
|
|
308
|
+
const ix = (currentFirst + currentLast) / 2 | 0;
|
|
309
|
+
const d = compare2(items[ix]);
|
|
310
|
+
if (d < 0) {
|
|
311
|
+
currentFirst = ix + 1;
|
|
312
|
+
} else if (d > 0) {
|
|
313
|
+
currentLast = ix;
|
|
314
|
+
} else {
|
|
315
|
+
return ix;
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
return currentFirst;
|
|
319
|
+
}
|
|
320
|
+
function maybeEncode(value) {
|
|
321
|
+
return typeof value === "string" ? value : "\0" + encode$3(value);
|
|
322
|
+
}
|
|
323
|
+
function maybeDecode(string) {
|
|
324
|
+
if (isEncodedKey(string)) {
|
|
325
|
+
const { key, step } = keyStep(string.slice(1));
|
|
326
|
+
const value = key === "" || key === "\uFFFF" ? key : decode$3(key);
|
|
327
|
+
return { key: value, step };
|
|
328
|
+
} else {
|
|
329
|
+
return keyStep(string);
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
const pageProps = {
|
|
333
|
+
$all: 1,
|
|
334
|
+
$first: 1,
|
|
335
|
+
$last: 1,
|
|
336
|
+
$after: 1,
|
|
337
|
+
$before: 1,
|
|
338
|
+
$since: 1,
|
|
339
|
+
$until: 1,
|
|
340
|
+
$cursor: 1
|
|
341
|
+
};
|
|
342
|
+
function splitArgs(arg) {
|
|
343
|
+
const page = {};
|
|
344
|
+
const filter = {};
|
|
345
|
+
for (const p in arg)
|
|
346
|
+
(p in pageProps ? page : filter)[p] = arg[p];
|
|
347
|
+
return [
|
|
348
|
+
isEmpty(page) ? void 0 : page,
|
|
349
|
+
isEmpty(filter) ? void 0 : filter
|
|
350
|
+
];
|
|
351
|
+
}
|
|
352
|
+
function encode$2(arg) {
|
|
353
|
+
if (!isPlainObject(arg))
|
|
354
|
+
return { key: maybeEncode(arg) };
|
|
355
|
+
const [page, filter] = splitArgs(arg);
|
|
356
|
+
errIf("page_and_filter", page && filter, arg);
|
|
357
|
+
if (!page)
|
|
358
|
+
return { key: maybeEncode(filter || {}) };
|
|
359
|
+
const _a = page, { $cursor } = _a, range = __objRest(_a, ["$cursor"]);
|
|
360
|
+
const { $first, $all, $last, $after, $before, $since, $until } = range;
|
|
361
|
+
const hasRange = !isEmpty(range);
|
|
362
|
+
errIf("first_and_last", isDef($first) && isDef($last), arg);
|
|
363
|
+
errIf("all_and_last", isDef($all) && isDef($last), arg);
|
|
364
|
+
errIf("all_and_first", isDef($first) && isDef($all), arg);
|
|
365
|
+
errIf("after_and_since", isDef($after) && isDef($since), arg);
|
|
366
|
+
errIf("before_and_until", isDef($before) && isDef($until), arg);
|
|
367
|
+
errIf("cursor_and_range_arg", isDef($cursor) && hasRange, arg);
|
|
368
|
+
let [key, end] = hasRange ? ["", "\uFFFF"] : [];
|
|
369
|
+
if (isDef($cursor))
|
|
370
|
+
key = maybeEncode($cursor);
|
|
371
|
+
if (isDef($after))
|
|
372
|
+
key = keyAfter(maybeEncode($after));
|
|
373
|
+
if (isDef($before))
|
|
374
|
+
end = keyBefore(maybeEncode($before));
|
|
375
|
+
if (isDef($since))
|
|
376
|
+
key = maybeEncode($since);
|
|
377
|
+
if (isDef($until))
|
|
378
|
+
end = maybeEncode($until);
|
|
379
|
+
if (isDef($last))
|
|
380
|
+
[key, end] = [end, key];
|
|
381
|
+
const node = { key };
|
|
382
|
+
if (isDef(end))
|
|
383
|
+
node.end = end;
|
|
384
|
+
if ($first || $last)
|
|
385
|
+
node.limit = $first || $last;
|
|
386
|
+
return node;
|
|
387
|
+
}
|
|
388
|
+
function decode$2(node) {
|
|
389
|
+
if (typeof node === "string")
|
|
390
|
+
return node;
|
|
391
|
+
const { key, end, limit } = node;
|
|
392
|
+
if (!isEncodedKey(key) && (!isDef(end) || end === key))
|
|
393
|
+
return key;
|
|
394
|
+
errIf("no_key", !isDef(key));
|
|
395
|
+
errIf("limit_without_end", isDef(limit) && !isDef(end));
|
|
396
|
+
const kParts = maybeDecode(key);
|
|
397
|
+
if (!isDef(end))
|
|
398
|
+
return kParts.key;
|
|
399
|
+
const eParts = maybeDecode(end);
|
|
400
|
+
const [lower, upper] = key < end ? [kParts, eParts] : [eParts, kParts];
|
|
401
|
+
const args = {};
|
|
402
|
+
if (limit) {
|
|
403
|
+
args[key < end ? "$first" : "$last"] = limit;
|
|
404
|
+
} else if (lower.key === "" && upper.key === "\uFFFF") {
|
|
405
|
+
args.$all = true;
|
|
406
|
+
}
|
|
407
|
+
if (lower.key !== "") {
|
|
408
|
+
args[lower.step === 1 ? "$after" : "$since"] = lower.key;
|
|
409
|
+
}
|
|
410
|
+
if (upper.key !== "\uFFFF") {
|
|
411
|
+
args[upper.step === -1 ? "$before" : "$until"] = upper.key;
|
|
412
|
+
}
|
|
413
|
+
return args;
|
|
414
|
+
}
|
|
415
|
+
const PATH_SEPARATOR = ".";
|
|
416
|
+
function encode$1(path) {
|
|
417
|
+
if (typeof path === "string") {
|
|
418
|
+
if (!path.length || path === PATH_SEPARATOR)
|
|
419
|
+
return [];
|
|
420
|
+
path = path.split(PATH_SEPARATOR);
|
|
421
|
+
}
|
|
422
|
+
if (!Array.isArray(path)) {
|
|
423
|
+
throw Error("encodePath.invalid:" + JSON.stringify(path));
|
|
424
|
+
}
|
|
425
|
+
function encodeSegment(seg) {
|
|
426
|
+
if (typeof seg === "string")
|
|
427
|
+
return seg;
|
|
428
|
+
const { key, end } = encode$2(seg);
|
|
429
|
+
if (end)
|
|
430
|
+
throw "encodePath.unexpected_range_key";
|
|
431
|
+
return key;
|
|
432
|
+
}
|
|
433
|
+
if (!isPlainObject(path[path.length - 1]))
|
|
434
|
+
return path.map(encodeSegment);
|
|
435
|
+
const [page, filter = {}] = splitArgs(path[path.length - 1]);
|
|
436
|
+
if (!page)
|
|
437
|
+
return path.map(encodeSegment);
|
|
438
|
+
return path.slice(0, -1).concat([filter]).map(encodeSegment);
|
|
439
|
+
}
|
|
440
|
+
function decode$1(path) {
|
|
441
|
+
if (!Array.isArray(path)) {
|
|
442
|
+
throw Error("decodePath.invalid:" + JSON.stringify(path));
|
|
443
|
+
}
|
|
444
|
+
return path.map((key) => decode$2({ key }));
|
|
445
|
+
}
|
|
446
|
+
function splitRef($ref) {
|
|
447
|
+
if (!Array.isArray($ref))
|
|
448
|
+
return [];
|
|
449
|
+
const tail = $ref[$ref.length - 1];
|
|
450
|
+
if (!isPlainObject(tail))
|
|
451
|
+
return [];
|
|
452
|
+
return splitArgs(tail);
|
|
453
|
+
}
|
|
454
|
+
var id = nonSecure.customAlphabet(alpha, 20);
|
|
455
|
+
function findFirst(children, target) {
|
|
456
|
+
return find(children, ({ key, end }) => {
|
|
457
|
+
if (key === target || end && key < target && end >= target)
|
|
458
|
+
return 0;
|
|
459
|
+
if (key < target)
|
|
460
|
+
return -1;
|
|
461
|
+
return 1;
|
|
462
|
+
});
|
|
463
|
+
}
|
|
464
|
+
function findLast(children, end, first, last) {
|
|
465
|
+
const ix = findFirst(children, end);
|
|
466
|
+
return children[ix] && children[ix].key <= end ? ix + 1 : ix;
|
|
467
|
+
}
|
|
468
|
+
function isRange(node) {
|
|
469
|
+
return node && typeof node.end !== "undefined";
|
|
470
|
+
}
|
|
471
|
+
function isBranch(node) {
|
|
472
|
+
return node && typeof node.children !== "undefined";
|
|
473
|
+
}
|
|
474
|
+
function isPrefix(node) {
|
|
475
|
+
return node && node.prefix;
|
|
476
|
+
}
|
|
477
|
+
function isLink(node) {
|
|
478
|
+
return node && typeof node.path !== "undefined";
|
|
479
|
+
}
|
|
480
|
+
function isEncoded(node) {
|
|
481
|
+
return node && node.key[0] === "\0";
|
|
482
|
+
}
|
|
483
|
+
function isOlder(node, version) {
|
|
484
|
+
return typeof node.version !== "undefined" && node.version < version;
|
|
485
|
+
}
|
|
486
|
+
function isNewer(node, version) {
|
|
487
|
+
return typeof node.version !== "undefined" && node.version > version;
|
|
488
|
+
}
|
|
489
|
+
function add(base, diff) {
|
|
490
|
+
let changed = false;
|
|
491
|
+
let index = 0;
|
|
492
|
+
for (const node of diff) {
|
|
493
|
+
const cmp = compare(node);
|
|
494
|
+
const nodeIsBranch = isBranch(node);
|
|
495
|
+
index = find(base, cmp, index);
|
|
496
|
+
const item = base[index];
|
|
497
|
+
const itemIsBranch = isBranch(item);
|
|
498
|
+
if (!item || cmp(item)) {
|
|
499
|
+
base.splice(index, 0, clone(node));
|
|
500
|
+
changed = true;
|
|
501
|
+
continue;
|
|
502
|
+
}
|
|
503
|
+
if (nodeIsBranch && itemIsBranch) {
|
|
504
|
+
changed = add(item.children, node.children) || changed;
|
|
505
|
+
} else if (nodeIsBranch || itemIsBranch) {
|
|
506
|
+
throw new Error("add.branch_leaf_mismatch");
|
|
507
|
+
} else {
|
|
508
|
+
item.value += node.value;
|
|
509
|
+
}
|
|
510
|
+
const size = itemIsBranch ? item.children.length : item.value;
|
|
511
|
+
if (!size) {
|
|
512
|
+
base.splice(index, 1);
|
|
513
|
+
changed = true;
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
return changed;
|
|
517
|
+
}
|
|
518
|
+
function compare(node) {
|
|
519
|
+
return (item) => {
|
|
520
|
+
const v = compareValue(item.key, node.key) || compareValue(item.end, node.end) || compareValue(item.limit, node.limit);
|
|
521
|
+
return v;
|
|
522
|
+
};
|
|
523
|
+
}
|
|
524
|
+
function compareValue(a, b) {
|
|
525
|
+
if (a === b)
|
|
526
|
+
return 0;
|
|
527
|
+
return a < b ? -1 : 1;
|
|
528
|
+
}
|
|
529
|
+
function clone(node) {
|
|
530
|
+
const copy = __spreadValues({}, node);
|
|
531
|
+
if (node.children)
|
|
532
|
+
copy.children = node.children.map((child) => clone(child));
|
|
533
|
+
return copy;
|
|
534
|
+
}
|
|
535
|
+
function merge(current, changes) {
|
|
536
|
+
let index = 0;
|
|
537
|
+
for (const change of changes) {
|
|
538
|
+
index = isRange(change) ? insertRange$1(current, change, index) : insertNode$1(current, change, index);
|
|
539
|
+
}
|
|
540
|
+
return current;
|
|
541
|
+
}
|
|
542
|
+
function insertRange$1(current, change, start = 0) {
|
|
543
|
+
const { key, end } = change;
|
|
544
|
+
const keyIx = findFirst(current, key);
|
|
545
|
+
const endIx = findLast(current, end);
|
|
546
|
+
const insertions = [change];
|
|
547
|
+
for (let i = keyIx; i < endIx; i++) {
|
|
548
|
+
const node = current[i];
|
|
549
|
+
if (isRange(node)) {
|
|
550
|
+
insertions.push(...mergeRanges$1(insertions.pop(), node));
|
|
551
|
+
} else {
|
|
552
|
+
insertNode$1(insertions, node, insertions.length - 1);
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
current.splice(keyIx, endIx - keyIx, ...insertions);
|
|
556
|
+
return keyIx + insertions.length;
|
|
557
|
+
}
|
|
558
|
+
function mergeRanges$1(base, node) {
|
|
559
|
+
if (node.version < base.version)
|
|
560
|
+
[node, base] = [base, node];
|
|
561
|
+
return [
|
|
562
|
+
base.key < node.key && __spreadProps(__spreadValues({}, base), { end: keyBefore(node.key) }),
|
|
563
|
+
node,
|
|
564
|
+
base.end > node.end && __spreadProps(__spreadValues({}, base), { key: keyAfter(node.end) })
|
|
565
|
+
].filter(Boolean);
|
|
566
|
+
}
|
|
567
|
+
function insertNode$1(current, change, start = 0) {
|
|
568
|
+
if (!current)
|
|
569
|
+
throw new Error("merge.insertNode: " + current);
|
|
570
|
+
const key = change.key;
|
|
571
|
+
const index = findFirst(current, key);
|
|
572
|
+
const node = current[index];
|
|
573
|
+
if (node && node.key <= key) {
|
|
574
|
+
return isRange(node) ? insertNodeIntoRange$1(current, index, change) : updateNode$1(current, index, change);
|
|
575
|
+
} else {
|
|
576
|
+
current.splice(index, 0, change);
|
|
577
|
+
return index + 1;
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
function insertNodeIntoRange$1(current, index, change) {
|
|
581
|
+
const key = change.key;
|
|
582
|
+
const range = current[index];
|
|
583
|
+
const newChange = getNewer(change, range);
|
|
584
|
+
if (!newChange)
|
|
585
|
+
return;
|
|
586
|
+
const insertions = [
|
|
587
|
+
range.key < key && __spreadProps(__spreadValues({}, range), { end: keyBefore(key) }),
|
|
588
|
+
newChange,
|
|
589
|
+
range.end > key && __spreadProps(__spreadValues({}, range), { key: keyAfter(key) })
|
|
590
|
+
].filter(Boolean);
|
|
591
|
+
current.splice(index, 1, ...insertions);
|
|
592
|
+
return index + insertions.length;
|
|
593
|
+
}
|
|
594
|
+
function updateNode$1(current, index, change) {
|
|
595
|
+
const node = current[index];
|
|
596
|
+
if (isBranch(change) && isBranch(node)) {
|
|
597
|
+
merge(node.children, change.children);
|
|
598
|
+
} else if (isBranch(node)) {
|
|
599
|
+
const newNode = getNewer(node, change);
|
|
600
|
+
current[index] = newNode || change;
|
|
601
|
+
} else {
|
|
602
|
+
const newChange = getNewer(change, node);
|
|
603
|
+
if (newChange)
|
|
604
|
+
current[index] = newChange;
|
|
605
|
+
}
|
|
606
|
+
if (change.prefix)
|
|
607
|
+
current[index].prefix = true;
|
|
608
|
+
return index + 1;
|
|
609
|
+
}
|
|
610
|
+
function getNewer(node, base) {
|
|
611
|
+
const { version } = base;
|
|
612
|
+
if (isBranch(node)) {
|
|
613
|
+
const children = [{ key: "", end: "\uFFFF", version }];
|
|
614
|
+
merge(children, node.children);
|
|
615
|
+
return children.length === 1 ? null : __spreadProps(__spreadValues({}, node), { children });
|
|
616
|
+
} else {
|
|
617
|
+
return node.version >= version ? node : null;
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
const IS_VAL = Symbol("IS_VAL");
|
|
621
|
+
function wrapValue(value, path, version = 0) {
|
|
622
|
+
const node = __spreadProps(__spreadValues({}, encode$2(path[path.length - 1])), { value, version });
|
|
623
|
+
return wrap([node], path.slice(0, -1), version);
|
|
624
|
+
}
|
|
625
|
+
function wrap(children, path, version = 0, prefix = false) {
|
|
626
|
+
if (!Array.isArray(path))
|
|
627
|
+
throw Error("wrap.path_not_array " + path);
|
|
628
|
+
if (!path.length)
|
|
629
|
+
return children;
|
|
630
|
+
let i = path.length - 1;
|
|
631
|
+
if (!Array.isArray(children)) {
|
|
632
|
+
children = [__spreadProps(__spreadValues({}, encode$2(path[i--])), { value: children, version })];
|
|
633
|
+
} else {
|
|
634
|
+
if (!children.length)
|
|
635
|
+
return;
|
|
636
|
+
children = [__spreadProps(__spreadValues({}, encode$2(path[i--])), { version, children })];
|
|
637
|
+
}
|
|
638
|
+
if (prefix)
|
|
639
|
+
children[0].prefix = true;
|
|
640
|
+
while (i >= 0)
|
|
641
|
+
children = [__spreadProps(__spreadValues({}, encode$2(path[i--])), { version, children })];
|
|
642
|
+
return children;
|
|
643
|
+
}
|
|
644
|
+
function unwrap(children, path) {
|
|
645
|
+
if (!Array.isArray(path))
|
|
646
|
+
throw Error("unwrap.path_not_array " + path);
|
|
647
|
+
let node = { children };
|
|
648
|
+
for (let i = 0; i < path.length; i++) {
|
|
649
|
+
const { key } = encode$2(path[i]);
|
|
650
|
+
children = node.children;
|
|
651
|
+
if (!children)
|
|
652
|
+
return null;
|
|
653
|
+
node = children[findFirst(children, key)];
|
|
654
|
+
if (!node || node.key > key)
|
|
655
|
+
return void 0;
|
|
656
|
+
if (isRange(node))
|
|
657
|
+
return null;
|
|
658
|
+
}
|
|
659
|
+
return getNodeValue(node);
|
|
660
|
+
}
|
|
661
|
+
function getNodeValue(node) {
|
|
662
|
+
if (node.children)
|
|
663
|
+
return node.children;
|
|
664
|
+
if (node.value && typeof node.value === "object") {
|
|
665
|
+
node.value[IS_VAL] = true;
|
|
666
|
+
}
|
|
667
|
+
return node.value;
|
|
668
|
+
}
|
|
669
|
+
function remove(children, path) {
|
|
670
|
+
if (!Array.isArray(path))
|
|
671
|
+
throw Error("del.path_not_array " + path);
|
|
672
|
+
if (!children)
|
|
673
|
+
return null;
|
|
674
|
+
if (!path.length)
|
|
675
|
+
return [];
|
|
676
|
+
const key = path[0];
|
|
677
|
+
const ix = findFirst(children, key);
|
|
678
|
+
const node = children[ix];
|
|
679
|
+
if (!node || node.key > key || isRange(node))
|
|
680
|
+
return children;
|
|
681
|
+
if (path.length === 1) {
|
|
682
|
+
return children.slice(0, ix).concat(children.slice(ix + 1));
|
|
683
|
+
}
|
|
684
|
+
if (!isBranch(node))
|
|
685
|
+
return children;
|
|
686
|
+
const filteredChildren = remove(node.children, path.slice(1));
|
|
687
|
+
if (filteredChildren === path.children)
|
|
688
|
+
return children;
|
|
689
|
+
const filteredNode = filteredChildren.length ? __spreadProps(__spreadValues({}, node), { children: filteredChildren }) : [];
|
|
690
|
+
return children.slice(0, ix).concat(filteredNode, children.slice(ix + 1));
|
|
691
|
+
}
|
|
692
|
+
class Result {
|
|
693
|
+
constructor(root) {
|
|
694
|
+
this.root = root || this;
|
|
695
|
+
}
|
|
696
|
+
addKnown(node) {
|
|
697
|
+
this.known = this.known || [];
|
|
698
|
+
merge(this.known, [node]);
|
|
699
|
+
}
|
|
700
|
+
addUnknown(node) {
|
|
701
|
+
this.unknown = this.unknown || [];
|
|
702
|
+
this.unknown.push(node);
|
|
703
|
+
}
|
|
704
|
+
addLinked(children) {
|
|
705
|
+
if (this.root !== this)
|
|
706
|
+
return this.root.addLinked(children);
|
|
707
|
+
this.linked = this.linked || [];
|
|
708
|
+
add(this.linked, children);
|
|
709
|
+
}
|
|
710
|
+
}
|
|
711
|
+
function slice(graph, query, root) {
|
|
712
|
+
let result = new Result(root);
|
|
713
|
+
let currentQuery = query;
|
|
714
|
+
while (currentQuery) {
|
|
715
|
+
let index = 0;
|
|
716
|
+
for (const queryNode of currentQuery) {
|
|
717
|
+
if (isRange(queryNode)) {
|
|
718
|
+
sliceRange(graph, queryNode, result);
|
|
719
|
+
} else {
|
|
720
|
+
const key = queryNode.key;
|
|
721
|
+
index = findFirst(graph, key);
|
|
722
|
+
sliceNode(graph[index], queryNode, result);
|
|
723
|
+
}
|
|
724
|
+
}
|
|
725
|
+
currentQuery = root ? void 0 : result.linked;
|
|
726
|
+
delete result.linked;
|
|
727
|
+
}
|
|
728
|
+
delete result.root;
|
|
729
|
+
return result;
|
|
730
|
+
}
|
|
731
|
+
function sliceNode(graph, query, result) {
|
|
732
|
+
const { key, version } = query;
|
|
733
|
+
const { root } = result;
|
|
734
|
+
if (!graph || graph.key > key || isOlder(graph, version)) {
|
|
735
|
+
result.addUnknown(query);
|
|
736
|
+
} else if (isRange(graph)) {
|
|
737
|
+
if (isBranch(query)) {
|
|
738
|
+
const { known } = slice([{ key: "", end: "\uFFFF", version: graph.version }], query.children);
|
|
739
|
+
result.addKnown({ key, version: graph.version, children: known });
|
|
740
|
+
} else {
|
|
741
|
+
result.addKnown({ key, end: key, version: graph.version });
|
|
742
|
+
}
|
|
743
|
+
} else if (isBranch(graph) && isBranch(query)) {
|
|
744
|
+
const { known, unknown } = slice(graph.children, query.children, root);
|
|
745
|
+
if (known)
|
|
746
|
+
result.addKnown(__spreadProps(__spreadValues({}, graph), { children: known }));
|
|
747
|
+
if (unknown)
|
|
748
|
+
result.addUnknown(__spreadProps(__spreadValues({}, query), { children: unknown }));
|
|
749
|
+
} else if (isLink(graph)) {
|
|
750
|
+
result.addKnown(graph);
|
|
751
|
+
if (graph.prefix && isRange(query)) {
|
|
752
|
+
result.addLinked(wrap([query], graph.path, version, true));
|
|
753
|
+
} else {
|
|
754
|
+
result.addLinked(wrap(query.children || query.value, graph.path, version, graph.prefix || query.prefix));
|
|
755
|
+
}
|
|
756
|
+
} else if (isBranch(graph)) {
|
|
757
|
+
result.addKnown(graph);
|
|
758
|
+
} else if (isBranch(query)) {
|
|
759
|
+
const { known } = slice([{ key: "", end: "\uFFFF", version: graph.version }], query.children);
|
|
760
|
+
result.addKnown({ key, version: graph.version, children: known });
|
|
761
|
+
} else {
|
|
762
|
+
result.addKnown(graph);
|
|
763
|
+
}
|
|
764
|
+
}
|
|
765
|
+
function sliceRange(graph, query, result) {
|
|
766
|
+
let { key, end, limit = Infinity, version } = query;
|
|
767
|
+
const step = key < end ? 1 : -1;
|
|
768
|
+
if (graph[0].key === "" && graph[0].prefix) {
|
|
769
|
+
sliceNode(graph[0], query, result);
|
|
770
|
+
return;
|
|
771
|
+
}
|
|
772
|
+
if (key < end) {
|
|
773
|
+
for (let i = findFirst(graph, key); key <= end && limit > 0; i++) {
|
|
774
|
+
const node = graph[i];
|
|
775
|
+
if (!node || key < node.key || isOlder(node, version))
|
|
776
|
+
break;
|
|
777
|
+
if (isRange(node)) {
|
|
778
|
+
result.addKnown(getOverlap(node, key, end));
|
|
779
|
+
} else {
|
|
780
|
+
sliceNode(node, __spreadProps(__spreadValues({}, query), { key }), result);
|
|
781
|
+
limit--;
|
|
782
|
+
}
|
|
783
|
+
key = keyAfter(node.end || node.key);
|
|
784
|
+
}
|
|
785
|
+
} else {
|
|
786
|
+
for (let i = findLast(graph, key) - 1; key >= end && limit > 0; i--) {
|
|
787
|
+
const node = graph[i];
|
|
788
|
+
if (!node || key > (node.end || node.key) || isOlder(node, version))
|
|
789
|
+
break;
|
|
790
|
+
if (isRange(node)) {
|
|
791
|
+
result.addKnown(getOverlap(node, end, key));
|
|
792
|
+
} else {
|
|
793
|
+
sliceNode(node, __spreadProps(__spreadValues({}, query), { key }), result);
|
|
794
|
+
limit--;
|
|
795
|
+
}
|
|
796
|
+
key = keyBefore(node.key);
|
|
797
|
+
}
|
|
798
|
+
}
|
|
799
|
+
if (limit && (step < 0 ? key > end : key < end)) {
|
|
800
|
+
const unknown = __spreadProps(__spreadValues({}, query), { key, end, limit });
|
|
801
|
+
result.addUnknown(unknown);
|
|
802
|
+
}
|
|
803
|
+
}
|
|
804
|
+
function getOverlap(node, key, end) {
|
|
805
|
+
if (node.key >= key && node.end <= end)
|
|
806
|
+
return node;
|
|
807
|
+
return __spreadProps(__spreadValues({}, node), {
|
|
808
|
+
key: node.key > key ? node.key : key,
|
|
809
|
+
end: node.end < end ? node.end : end
|
|
810
|
+
});
|
|
811
|
+
}
|
|
812
|
+
function sieve(current, changes, result = []) {
|
|
813
|
+
let index = 0;
|
|
814
|
+
for (const change of changes) {
|
|
815
|
+
index = isRange(change) ? insertRange(current, change, result, index) : insertNode(current, change, result, index);
|
|
816
|
+
}
|
|
817
|
+
return result;
|
|
818
|
+
}
|
|
819
|
+
function insertRange(current, change, result, start = 0) {
|
|
820
|
+
const { key, end } = change;
|
|
821
|
+
const keyIx = findFirst(current, key);
|
|
822
|
+
const endIx = findLast(current, end);
|
|
823
|
+
if (keyIx === endIx && !(current[keyIx] && current[keyIx].key <= key && current[keyIx].end >= end)) {
|
|
824
|
+
return keyIx;
|
|
825
|
+
}
|
|
826
|
+
const appliedChange = [];
|
|
827
|
+
let currentKey = change.key;
|
|
828
|
+
for (let i = keyIx; i < endIx; i++) {
|
|
829
|
+
const node = current[i];
|
|
830
|
+
if (isRange(node) && node.version >= 0) {
|
|
831
|
+
if (node.key > currentKey) {
|
|
832
|
+
appliedChange.push({
|
|
833
|
+
key: currentKey,
|
|
834
|
+
end: keyBefore(node.key),
|
|
835
|
+
version: change.version
|
|
836
|
+
});
|
|
837
|
+
}
|
|
838
|
+
currentKey = keyAfter(node.end);
|
|
839
|
+
} else {
|
|
840
|
+
if (getNewerChange(node, change)) {
|
|
841
|
+
appliedChange.push({
|
|
842
|
+
key: currentKey,
|
|
843
|
+
end: keyBefore(node.key),
|
|
844
|
+
version: change.version
|
|
845
|
+
});
|
|
846
|
+
currentKey = keyAfter(node.key);
|
|
847
|
+
}
|
|
848
|
+
}
|
|
849
|
+
if (currentKey >= change.end) {
|
|
850
|
+
break;
|
|
851
|
+
}
|
|
852
|
+
}
|
|
853
|
+
if (currentKey <= change.end) {
|
|
854
|
+
appliedChange.push({
|
|
855
|
+
key: currentKey,
|
|
856
|
+
end: change.end,
|
|
857
|
+
version: change.version
|
|
858
|
+
});
|
|
859
|
+
}
|
|
860
|
+
if (appliedChange.length)
|
|
861
|
+
result.push(...appliedChange);
|
|
862
|
+
const insertions = [change];
|
|
863
|
+
for (let i = keyIx; i < endIx; i++) {
|
|
864
|
+
const node = current[i];
|
|
865
|
+
if (isRange(node)) {
|
|
866
|
+
insertions.push(...mergeRanges(insertions.pop(), node));
|
|
867
|
+
} else {
|
|
868
|
+
insertNode(insertions, node, [], insertions.length - 1);
|
|
869
|
+
}
|
|
870
|
+
}
|
|
871
|
+
current.splice(keyIx, endIx - keyIx, ...insertions);
|
|
872
|
+
return keyIx + insertions.length;
|
|
873
|
+
}
|
|
874
|
+
function mergeRanges(base, node) {
|
|
875
|
+
if (node.version < base.version)
|
|
876
|
+
[node, base] = [base, node];
|
|
877
|
+
return [
|
|
878
|
+
base.key < node.key && __spreadProps(__spreadValues({}, base), { end: keyBefore(node.key) }),
|
|
879
|
+
node,
|
|
880
|
+
base.end > node.end && __spreadProps(__spreadValues({}, base), { key: keyAfter(node.end) })
|
|
881
|
+
].filter(Boolean);
|
|
882
|
+
}
|
|
883
|
+
function insertNode(current, change, result, start = 0) {
|
|
884
|
+
const key = change.key;
|
|
885
|
+
const index = findFirst(current, key);
|
|
886
|
+
const node = current[index];
|
|
887
|
+
if (node && node.key <= key) {
|
|
888
|
+
return isRange(node) ? insertNodeIntoRange(current, index, change, result) : updateNode(current, index, change, result);
|
|
889
|
+
} else {
|
|
890
|
+
return index;
|
|
891
|
+
}
|
|
892
|
+
}
|
|
893
|
+
function insertNodeIntoRange(current, index, change, result) {
|
|
894
|
+
const key = change.key;
|
|
895
|
+
const range = current[index];
|
|
896
|
+
const newChange = getNewerChange(change, range);
|
|
897
|
+
const newNode = getNewerNode(change, range);
|
|
898
|
+
if (!newChange)
|
|
899
|
+
return;
|
|
900
|
+
result.push(newChange);
|
|
901
|
+
const insertions = [
|
|
902
|
+
range.key < key && __spreadProps(__spreadValues({}, range), { end: keyBefore(key) }),
|
|
903
|
+
newNode,
|
|
904
|
+
range.end > key && __spreadProps(__spreadValues({}, range), { key: keyAfter(key) })
|
|
905
|
+
].filter(Boolean);
|
|
906
|
+
current.splice(index, 1, ...insertions);
|
|
907
|
+
return index + insertions.length;
|
|
908
|
+
}
|
|
909
|
+
function updateNode(current, index, change, result) {
|
|
910
|
+
const node = current[index];
|
|
911
|
+
if (isBranch(change) && isBranch(node)) {
|
|
912
|
+
const nextResult = [];
|
|
913
|
+
node.version = change.version;
|
|
914
|
+
sieve(node.children, change.children, nextResult);
|
|
915
|
+
if (nextResult.length)
|
|
916
|
+
result.push(__spreadProps(__spreadValues({}, change), { children: nextResult }));
|
|
917
|
+
} else if (isBranch(node)) {
|
|
918
|
+
const newNode = getNewerNode(node, change);
|
|
919
|
+
current[index] = newNode || change;
|
|
920
|
+
if (!newNode)
|
|
921
|
+
result.push(change);
|
|
922
|
+
} else {
|
|
923
|
+
const newChange = getNewerChange(change, node);
|
|
924
|
+
const newNode = getNewerNode(change, node);
|
|
925
|
+
if (newNode)
|
|
926
|
+
current[index] = newNode;
|
|
927
|
+
if (newChange && (change.value !== node.value || !isPathEqual(change.path, node.path))) {
|
|
928
|
+
result.push(newChange);
|
|
929
|
+
}
|
|
930
|
+
}
|
|
931
|
+
return index + 1;
|
|
932
|
+
}
|
|
933
|
+
function isPathEqual(first, second) {
|
|
934
|
+
if (!first && !second)
|
|
935
|
+
return true;
|
|
936
|
+
if (!first || !second)
|
|
937
|
+
return false;
|
|
938
|
+
if (first.length !== second.length)
|
|
939
|
+
return false;
|
|
940
|
+
for (let i = 0; i < first.length; i++) {
|
|
941
|
+
if (first[i] !== second[i])
|
|
942
|
+
return false;
|
|
943
|
+
}
|
|
944
|
+
return true;
|
|
945
|
+
}
|
|
946
|
+
function getNewerNode(node, base) {
|
|
947
|
+
if (isBranch(node)) {
|
|
948
|
+
const emptyNode = { key: "", end: "\uFFFF", version: base.version };
|
|
949
|
+
const children = [emptyNode];
|
|
950
|
+
sieve(children, node.children);
|
|
951
|
+
return children.length === 1 && children[0] === emptyNode ? null : __spreadProps(__spreadValues({}, node), { children });
|
|
952
|
+
} else {
|
|
953
|
+
return node.version >= base.version ? node : null;
|
|
954
|
+
}
|
|
955
|
+
}
|
|
956
|
+
function getNewerChange(node, base) {
|
|
957
|
+
if (isBranch(node)) {
|
|
958
|
+
const children = node.children.filter((child) => getNewerChange(child, base));
|
|
959
|
+
return children.length && __spreadProps(__spreadValues({}, node), { children });
|
|
960
|
+
} else {
|
|
961
|
+
return node.version >= base.version ? node : null;
|
|
962
|
+
}
|
|
963
|
+
}
|
|
964
|
+
function setVersion(graph, version) {
|
|
965
|
+
for (const node of graph) {
|
|
966
|
+
node.version = version;
|
|
967
|
+
if (node.children)
|
|
968
|
+
setVersion(node.children, version);
|
|
969
|
+
}
|
|
970
|
+
return graph;
|
|
971
|
+
}
|
|
972
|
+
function getKnown(graph, version = 0) {
|
|
973
|
+
const query = [];
|
|
974
|
+
for (const { key, end, children } of graph) {
|
|
975
|
+
const node = { key, version };
|
|
976
|
+
if (end) {
|
|
977
|
+
if (end !== key)
|
|
978
|
+
node.end = end;
|
|
979
|
+
node.value = 1;
|
|
980
|
+
}
|
|
981
|
+
if (children) {
|
|
982
|
+
node.children = getKnown(children);
|
|
983
|
+
} else {
|
|
984
|
+
node.value = 1;
|
|
985
|
+
}
|
|
986
|
+
query.push(node);
|
|
987
|
+
}
|
|
988
|
+
return query;
|
|
989
|
+
}
|
|
990
|
+
function finalize(graph, query, version = Date.now()) {
|
|
991
|
+
let result = [{ key: "", end: "\uFFFF", version: 0 }];
|
|
992
|
+
if (query)
|
|
993
|
+
result = slice(result, query).known || [];
|
|
994
|
+
result = setVersion(merge(result, graph), version);
|
|
995
|
+
return result;
|
|
996
|
+
}
|
|
997
|
+
function decode(nodes = [], { isGraph } = {}) {
|
|
998
|
+
function decodeChildren(nodes2) {
|
|
999
|
+
let result = [];
|
|
1000
|
+
let allStrs = true;
|
|
1001
|
+
let allNums = true;
|
|
1002
|
+
function pushResult(...objects) {
|
|
1003
|
+
for (const object of objects) {
|
|
1004
|
+
if (isDef(object) && !Number.isInteger(object.$key))
|
|
1005
|
+
allNums = false;
|
|
1006
|
+
if (isDef(object) && typeof object.$key !== "string")
|
|
1007
|
+
allStrs = false;
|
|
1008
|
+
}
|
|
1009
|
+
result.push(...objects);
|
|
1010
|
+
}
|
|
1011
|
+
const putRanges = [];
|
|
1012
|
+
let lastNode = null;
|
|
1013
|
+
function addPutRange({ key, end }) {
|
|
1014
|
+
if (lastNode) {
|
|
1015
|
+
if (lastNode.end) {
|
|
1016
|
+
if (key === keyAfter(lastNode.end)) {
|
|
1017
|
+
lastNode.end = end || key;
|
|
1018
|
+
return end && end !== key;
|
|
1019
|
+
}
|
|
1020
|
+
} else {
|
|
1021
|
+
if (key === keyAfter(lastNode.key))
|
|
1022
|
+
key = lastNode.key;
|
|
1023
|
+
}
|
|
1024
|
+
}
|
|
1025
|
+
if (end && key !== end) {
|
|
1026
|
+
lastNode = { key, end };
|
|
1027
|
+
putRanges.push(lastNode);
|
|
1028
|
+
return true;
|
|
1029
|
+
}
|
|
1030
|
+
lastNode = { key };
|
|
1031
|
+
return false;
|
|
1032
|
+
}
|
|
1033
|
+
for (const node of nodes2) {
|
|
1034
|
+
if (isGraph && addPutRange(node))
|
|
1035
|
+
continue;
|
|
1036
|
+
if (isPrefix(node))
|
|
1037
|
+
pushResult(...decodePrefixNode(node));
|
|
1038
|
+
else if (isGraph && isRange(node))
|
|
1039
|
+
pushResult(decodeRangeNode(node));
|
|
1040
|
+
else if (isBranch(node))
|
|
1041
|
+
pushResult(decodeBranchNode(node));
|
|
1042
|
+
else if (isLink(node))
|
|
1043
|
+
pushResult(decodeLinkNode(node));
|
|
1044
|
+
else
|
|
1045
|
+
pushResult(decodeLeafNode(node));
|
|
1046
|
+
}
|
|
1047
|
+
if (allNums || allStrs) {
|
|
1048
|
+
result = result.reduce((collection, item) => {
|
|
1049
|
+
if (Array.isArray(item)) {
|
|
1050
|
+
collection[item.$key] = item;
|
|
1051
|
+
delete item.$key;
|
|
1052
|
+
return collection;
|
|
1053
|
+
}
|
|
1054
|
+
const _a = item, { $key, $val } = _a, rest = __objRest(_a, ["$key", "$val"]);
|
|
1055
|
+
if (typeof $val === "object")
|
|
1056
|
+
$val.$val = true;
|
|
1057
|
+
collection[$key] = isDef($val) ? $val : !isEmpty(rest) ? rest : isGraph ? null : true;
|
|
1058
|
+
return collection;
|
|
1059
|
+
}, allStrs ? {} : []);
|
|
1060
|
+
}
|
|
1061
|
+
if (isGraph && putRanges.length) {
|
|
1062
|
+
if (putRanges.length === 1 && putRanges[0].key === "" && putRanges[0].end === "\uFFFF") {
|
|
1063
|
+
result.$put = true;
|
|
1064
|
+
} else {
|
|
1065
|
+
result.$put = putRanges.map((rNode) => decode$2(rNode));
|
|
1066
|
+
}
|
|
1067
|
+
}
|
|
1068
|
+
return result;
|
|
1069
|
+
}
|
|
1070
|
+
function decodePrefixNode(node) {
|
|
1071
|
+
let args = decode$2(node);
|
|
1072
|
+
if (args === "")
|
|
1073
|
+
args = {};
|
|
1074
|
+
if (typeof args === "string") {
|
|
1075
|
+
throw Error("decode.unencoded_prefix: " + args);
|
|
1076
|
+
}
|
|
1077
|
+
if (isLink(node)) {
|
|
1078
|
+
args.$all = true;
|
|
1079
|
+
const $ref = decode$1(node.path);
|
|
1080
|
+
const lastKey = $ref[$ref.length - 1];
|
|
1081
|
+
if (typeof lastKey === "string") {
|
|
1082
|
+
throw Error("decode.unencoded_prefix_ref: " + node.path);
|
|
1083
|
+
}
|
|
1084
|
+
lastKey.$all = true;
|
|
1085
|
+
return [{ $key: args, $ref }];
|
|
1086
|
+
}
|
|
1087
|
+
const children = decodeChildren(node.children);
|
|
1088
|
+
if (!Array.isArray(children)) {
|
|
1089
|
+
throw Error("decode.prefix_without_encoded_child_keys:" + node.key);
|
|
1090
|
+
}
|
|
1091
|
+
for (const child of children) {
|
|
1092
|
+
if (typeof child.$key === "string") {
|
|
1093
|
+
throw Error("decode.prefix_with_unencoded_child_key:" + child.$key);
|
|
1094
|
+
}
|
|
1095
|
+
if (!splitArgs(child.$key)[0]) {
|
|
1096
|
+
child.$key = { $cursor: child.$key };
|
|
1097
|
+
}
|
|
1098
|
+
child.$key = __spreadValues(__spreadValues({}, args), child.$key);
|
|
1099
|
+
}
|
|
1100
|
+
return children;
|
|
1101
|
+
}
|
|
1102
|
+
function decodeBranchNode(node) {
|
|
1103
|
+
const child = decodeChildren(node.children);
|
|
1104
|
+
child.$key = decode$2(node);
|
|
1105
|
+
return child;
|
|
1106
|
+
}
|
|
1107
|
+
function decodeLeafNode(node) {
|
|
1108
|
+
const child = isGraph ? { $val: node.value } : {};
|
|
1109
|
+
child.$key = decode$2(node);
|
|
1110
|
+
return child;
|
|
1111
|
+
}
|
|
1112
|
+
function decodeRangeNode(node) {
|
|
1113
|
+
if (node.key === node.end)
|
|
1114
|
+
return { $key: decode$2({ key: node.key }) };
|
|
1115
|
+
}
|
|
1116
|
+
function decodeLinkNode(node) {
|
|
1117
|
+
return { $key: decode$2(node), $ref: decode$1(node.path) };
|
|
1118
|
+
}
|
|
1119
|
+
return decodeChildren(nodes);
|
|
1120
|
+
}
|
|
1121
|
+
function decodeGraph(graph) {
|
|
1122
|
+
return decode(graph, { isGraph: true });
|
|
1123
|
+
}
|
|
1124
|
+
function decodeQuery(query) {
|
|
1125
|
+
return decode(query, { isGraph: false });
|
|
1126
|
+
}
|
|
1127
|
+
const REF = Symbol();
|
|
1128
|
+
const PRE = Symbol();
|
|
1129
|
+
function decorate(rootGraph, rootQuery) {
|
|
1130
|
+
function construct(plumGraph, query) {
|
|
1131
|
+
if (plumGraph === null)
|
|
1132
|
+
return null;
|
|
1133
|
+
if (!isDef(plumGraph))
|
|
1134
|
+
plumGraph = [];
|
|
1135
|
+
if (query.$key)
|
|
1136
|
+
query = [query];
|
|
1137
|
+
let graph;
|
|
1138
|
+
if (query.$ref) {
|
|
1139
|
+
const _a = query, { $ref } = _a, props = __objRest(_a, ["$ref"]);
|
|
1140
|
+
const [range, filter] = splitRef($ref);
|
|
1141
|
+
const path = encode$1($ref);
|
|
1142
|
+
const targetPlumGraph = unwrap(rootGraph, path);
|
|
1143
|
+
if (range)
|
|
1144
|
+
targetPlumGraph[PRE] = filter;
|
|
1145
|
+
graph = construct(targetPlumGraph, range ? __spreadValues({ $key: range }, props) : props);
|
|
1146
|
+
graph.$ref = $ref;
|
|
1147
|
+
} else if (Array.isArray(query)) {
|
|
1148
|
+
let pageKey;
|
|
1149
|
+
graph = query.flatMap((item, i) => {
|
|
1150
|
+
if (!(item == null ? void 0 : item.$key)) {
|
|
1151
|
+
return construct(descend(plumGraph, i), item);
|
|
1152
|
+
}
|
|
1153
|
+
const _a2 = item, { $key, $chi } = _a2, props = __objRest(_a2, ["$key", "$chi"]);
|
|
1154
|
+
const subQuery = $chi || (isEmpty(props) ? 1 : props);
|
|
1155
|
+
if (!isPlainObject($key) || !splitArgs($key)[0]) {
|
|
1156
|
+
return construct(descend(plumGraph, $key), subQuery);
|
|
1157
|
+
}
|
|
1158
|
+
if (pageKey) {
|
|
1159
|
+
throw Error("decorate.multi_range_query:" + JSON.stringify({ $key, pageKey }));
|
|
1160
|
+
}
|
|
1161
|
+
pageKey = $key;
|
|
1162
|
+
const children = slice2(plumGraph, $key);
|
|
1163
|
+
return children.filter((node) => !isRange(node)).map((node) => {
|
|
1164
|
+
const $key2 = decode$2(node);
|
|
1165
|
+
const subResult = construct(getValue(node), subQuery);
|
|
1166
|
+
if (typeof subResult === "object") {
|
|
1167
|
+
subResult.$key = children[PRE] ? __spreadProps(__spreadValues({}, children[PRE]), { $cursor: $key2 }) : $key2;
|
|
1168
|
+
}
|
|
1169
|
+
return subResult;
|
|
1170
|
+
});
|
|
1171
|
+
});
|
|
1172
|
+
addPageMeta(graph, pageKey);
|
|
1173
|
+
} else if (typeof query === "object") {
|
|
1174
|
+
graph = {};
|
|
1175
|
+
for (const prop in query) {
|
|
1176
|
+
graph[prop] = construct(descend(plumGraph, prop), query[prop]);
|
|
1177
|
+
}
|
|
1178
|
+
} else if (query) {
|
|
1179
|
+
if (Array.isArray(plumGraph) && !plumGraph.length) {
|
|
1180
|
+
graph = void 0;
|
|
1181
|
+
} else if (typeof plumGraph !== "object" || !plumGraph) {
|
|
1182
|
+
graph = plumGraph;
|
|
1183
|
+
} else if (plumGraph[IS_VAL]) {
|
|
1184
|
+
graph = Array.isArray(plumGraph) ? plumGraph.slice(0) : __spreadValues({}, plumGraph);
|
|
1185
|
+
graph.$val = true;
|
|
1186
|
+
} else if (Array.isArray(plumGraph)) {
|
|
1187
|
+
graph = decodeGraph(plumGraph);
|
|
1188
|
+
} else {
|
|
1189
|
+
throw Error("decorate.unexpected_graph", plumGraph);
|
|
1190
|
+
}
|
|
1191
|
+
}
|
|
1192
|
+
if (plumGraph[REF])
|
|
1193
|
+
graph.$ref = decode$1(plumGraph[REF]);
|
|
1194
|
+
return graph;
|
|
1195
|
+
}
|
|
1196
|
+
function descend(children, $key) {
|
|
1197
|
+
const { key } = encode$2($key);
|
|
1198
|
+
if (!Array.isArray(children))
|
|
1199
|
+
return null;
|
|
1200
|
+
const ix = findFirst(children, key);
|
|
1201
|
+
const node = children[ix];
|
|
1202
|
+
if (!node)
|
|
1203
|
+
return;
|
|
1204
|
+
if (isRange(node) && node.end >= key)
|
|
1205
|
+
return null;
|
|
1206
|
+
if (node.key !== key)
|
|
1207
|
+
return;
|
|
1208
|
+
const result2 = getValue(node);
|
|
1209
|
+
if (node.prefix)
|
|
1210
|
+
result2[PRE] = $key;
|
|
1211
|
+
return result2;
|
|
1212
|
+
}
|
|
1213
|
+
function getValue(node) {
|
|
1214
|
+
let result2;
|
|
1215
|
+
if (node.path) {
|
|
1216
|
+
result2 = unwrap(rootGraph, node.path);
|
|
1217
|
+
if (typeof result2 === "object")
|
|
1218
|
+
result2[REF] = node.path;
|
|
1219
|
+
} else {
|
|
1220
|
+
result2 = getNodeValue(node);
|
|
1221
|
+
}
|
|
1222
|
+
return result2;
|
|
1223
|
+
}
|
|
1224
|
+
function slice2(children, $key) {
|
|
1225
|
+
const [range, filter] = splitArgs($key);
|
|
1226
|
+
if (isDef(filter)) {
|
|
1227
|
+
children = descend(children, filter);
|
|
1228
|
+
} else if (children[0].key === "" && children[0].prefix) {
|
|
1229
|
+
children = descend(children, "");
|
|
1230
|
+
}
|
|
1231
|
+
const { key, end, limit = Infinity } = encode$2(range);
|
|
1232
|
+
const ix = findFirst(children, key);
|
|
1233
|
+
let i = ix;
|
|
1234
|
+
let result2;
|
|
1235
|
+
if (key < end) {
|
|
1236
|
+
for (let n = 0; i < children.length && n < limit; i++) {
|
|
1237
|
+
if (!isRange(children[i]))
|
|
1238
|
+
n++;
|
|
1239
|
+
}
|
|
1240
|
+
result2 = children.slice(ix, i);
|
|
1241
|
+
} else {
|
|
1242
|
+
for (let n = 0; i >= 0 && n < limit; i--) {
|
|
1243
|
+
if (!isRange(children[i]))
|
|
1244
|
+
n++;
|
|
1245
|
+
}
|
|
1246
|
+
result2 = children.slice(i + 1, ix + 1);
|
|
1247
|
+
}
|
|
1248
|
+
if (children[REF])
|
|
1249
|
+
result2[REF] = children[REF];
|
|
1250
|
+
if (children[PRE])
|
|
1251
|
+
result2[PRE] = children[PRE];
|
|
1252
|
+
return result2;
|
|
1253
|
+
}
|
|
1254
|
+
const result = construct(rootGraph, rootQuery);
|
|
1255
|
+
return result;
|
|
1256
|
+
}
|
|
1257
|
+
function addPageMeta(graph, args) {
|
|
1258
|
+
if (args.$all) {
|
|
1259
|
+
Object.assign(graph, { $page: args, $prev: null, $next: null });
|
|
1260
|
+
return;
|
|
1261
|
+
}
|
|
1262
|
+
const [_a, ..._b] = splitArgs(args), _c = _a, { $first, $last } = _c, bounds = __objRest(_c, ["$first", "$last"]), [filter] = _b;
|
|
1263
|
+
const count = $first || $last;
|
|
1264
|
+
const $page = __spreadProps(__spreadValues(__spreadValues({}, filter), bounds), { $all: true });
|
|
1265
|
+
if (graph.length === count) {
|
|
1266
|
+
if ($first) {
|
|
1267
|
+
const boundKey = graph[graph.length - 1].$key;
|
|
1268
|
+
$page.$until = isDef(boundKey == null ? void 0 : boundKey.$cursor) ? boundKey.$cursor : boundKey;
|
|
1269
|
+
delete $page.$before;
|
|
1270
|
+
} else {
|
|
1271
|
+
const boundKey = graph[0].$key;
|
|
1272
|
+
$page.$since = isDef(boundKey == null ? void 0 : boundKey.$cursor) ? boundKey.$cursor : boundKey;
|
|
1273
|
+
delete $page.$after;
|
|
1274
|
+
}
|
|
1275
|
+
}
|
|
1276
|
+
const $prev = isDef($page.$after) ? __spreadProps(__spreadValues({}, filter), { $last: count, $until: $page.$after }) : isDef($page.$since) ? __spreadProps(__spreadValues({}, filter), { $last: count, $before: $page.$since }) : null;
|
|
1277
|
+
let $next = isDef($page.$before) ? __spreadProps(__spreadValues({}, filter), { $first: count, $since: $page.$before }) : isDef($page.$until) ? __spreadProps(__spreadValues({}, filter), { $first: count, $after: $page.$until }) : null;
|
|
1278
|
+
Object.assign(graph, { $page, $next, $prev });
|
|
1279
|
+
}
|
|
1280
|
+
function serialize(obj) {
|
|
1281
|
+
return JSON.stringify(obj).replace(/\uffff/g, "\\uffff");
|
|
1282
|
+
}
|
|
1283
|
+
function deserialize(str) {
|
|
1284
|
+
return JSON.parse(str);
|
|
1285
|
+
}
|
|
1286
|
+
const ROOT_KEY = Symbol();
|
|
1287
|
+
function encode(value, { version, isGraph } = {}) {
|
|
1288
|
+
var _a;
|
|
1289
|
+
const links = [];
|
|
1290
|
+
function pushLink($ref, $ver, props, $val, $chi) {
|
|
1291
|
+
const [range, _] = splitRef($ref);
|
|
1292
|
+
let children = !isEmpty(props) ? makeNode(range ? [__spreadValues({ $key: range }, props)] : props, void 0, $ver).children : isDef($chi) ? makeNode(range ? [{ $key: range, $chi }] : $chi, void 0, $ver).children : isDef($val) ? $val : isGraph ? void 0 : 1;
|
|
1293
|
+
if (children) {
|
|
1294
|
+
links.push(wrap(children, encode$1($ref), $ver, !!range)[0]);
|
|
1295
|
+
}
|
|
1296
|
+
}
|
|
1297
|
+
const combine = isGraph ? merge : add;
|
|
1298
|
+
function makeNode(object, key, ver) {
|
|
1299
|
+
var _c;
|
|
1300
|
+
if (!isDef(object))
|
|
1301
|
+
return;
|
|
1302
|
+
if (typeof object === "object" && object && isEmpty(object))
|
|
1303
|
+
return;
|
|
1304
|
+
const _a2 = object || {}, { $key, $ver } = _a2, data = __objRest(_a2, ["$key", "$ver"]);
|
|
1305
|
+
const _b = data, { $ref, $val, $chi, $put } = _b, props = __objRest(_b, ["$ref", "$val", "$chi", "$put"]);
|
|
1306
|
+
if (isDef($ver))
|
|
1307
|
+
ver = $ver;
|
|
1308
|
+
if (isPlainObject($key)) {
|
|
1309
|
+
const [page, filter] = splitArgs($key);
|
|
1310
|
+
if (isGraph && page && !isDef(page.$cursor) && !isEmpty(data)) {
|
|
1311
|
+
const node2 = makeNode(__spreadProps(__spreadValues({}, object), { $key: filter || "" }), key, ver);
|
|
1312
|
+
node2.prefix = true;
|
|
1313
|
+
return node2;
|
|
1314
|
+
}
|
|
1315
|
+
if ((!isDef(key) || Number.isInteger(key)) && page && filter) {
|
|
1316
|
+
const node2 = makeNode({
|
|
1317
|
+
$key: filter,
|
|
1318
|
+
$chi: [
|
|
1319
|
+
__spreadProps(__spreadValues({}, object), { $key: isDef(page.$cursor) ? page.$cursor : page })
|
|
1320
|
+
]
|
|
1321
|
+
}, key, ver);
|
|
1322
|
+
node2.prefix = true;
|
|
1323
|
+
return node2;
|
|
1324
|
+
}
|
|
1325
|
+
}
|
|
1326
|
+
if (isDef($key) && (Number.isInteger(key) || !isDef(key)))
|
|
1327
|
+
key = $key;
|
|
1328
|
+
const node = key === ROOT_KEY || !isDef(key) ? {} : encode$2(key);
|
|
1329
|
+
node.version = ver;
|
|
1330
|
+
if (object === null) {
|
|
1331
|
+
node.end = node.key;
|
|
1332
|
+
} else if (isDef($key) && isDef(key) && key !== $key) {
|
|
1333
|
+
node.children = [makeNode(object, void 0, ver)].filter(Boolean);
|
|
1334
|
+
} else if ($ref) {
|
|
1335
|
+
pushLink($ref, node.version, props, $val, $chi);
|
|
1336
|
+
if (!isGraph)
|
|
1337
|
+
return;
|
|
1338
|
+
node.path = encode$1($ref);
|
|
1339
|
+
} else if ($val === true) {
|
|
1340
|
+
node.value = props;
|
|
1341
|
+
} else if (isDef($val)) {
|
|
1342
|
+
node.value = $val;
|
|
1343
|
+
} else if (typeof object !== "object") {
|
|
1344
|
+
node.value = isGraph || typeof object === "number" ? object : 1;
|
|
1345
|
+
} else if (isDef($chi)) {
|
|
1346
|
+
const children = $chi.map((obj) => makeNode(obj, void 0, ver)).filter(Boolean).sort((a, b) => a.key <= b.key ? -1 : 1);
|
|
1347
|
+
if (children.length) {
|
|
1348
|
+
node.children = children;
|
|
1349
|
+
}
|
|
1350
|
+
} else if (Array.isArray(object)) {
|
|
1351
|
+
const children = object.map((obj, i) => makeNode(obj, i, ver)).filter(Boolean).reduce((acc, it) => {
|
|
1352
|
+
combine(acc, [it]);
|
|
1353
|
+
return acc;
|
|
1354
|
+
}, []);
|
|
1355
|
+
if (children.length) {
|
|
1356
|
+
node.children = children;
|
|
1357
|
+
}
|
|
1358
|
+
} else {
|
|
1359
|
+
const children = Object.keys(props).sort().map((key2) => makeNode(object[key2], key2, ver)).filter(Boolean);
|
|
1360
|
+
if (children.length) {
|
|
1361
|
+
node.children = children;
|
|
1362
|
+
} else if (isGraph) {
|
|
1363
|
+
if (node.key && !node.end)
|
|
1364
|
+
node.end = node.key;
|
|
1365
|
+
} else {
|
|
1366
|
+
node.value = 1;
|
|
1367
|
+
}
|
|
1368
|
+
}
|
|
1369
|
+
let putQuery;
|
|
1370
|
+
if (Array.isArray(object) && !object.some((it) => isDef(it == null ? void 0 : it.$key))) {
|
|
1371
|
+
putQuery = [encode$2({ $since: 0, $until: Infinity })];
|
|
1372
|
+
}
|
|
1373
|
+
if ($put === true) {
|
|
1374
|
+
putQuery = null;
|
|
1375
|
+
} else if (Array.isArray($put)) {
|
|
1376
|
+
putQuery = $put.map((arg) => encode$2(arg));
|
|
1377
|
+
} else if (isDef($put)) {
|
|
1378
|
+
putQuery = [encode$2($put)];
|
|
1379
|
+
}
|
|
1380
|
+
if (isGraph && isDef(putQuery)) {
|
|
1381
|
+
node.children = finalize(node.children || [], putQuery, node.version);
|
|
1382
|
+
}
|
|
1383
|
+
if (((_c = node.children) == null ? void 0 : _c.length) || isDef(node.end) || isDef(node.value) || isDef(node.path)) {
|
|
1384
|
+
return node;
|
|
1385
|
+
}
|
|
1386
|
+
}
|
|
1387
|
+
if (value == null ? void 0 : value.$key)
|
|
1388
|
+
value = [value];
|
|
1389
|
+
let result = ((_a = makeNode(value, ROOT_KEY, version)) == null ? void 0 : _a.children) || [];
|
|
1390
|
+
while (links.length) {
|
|
1391
|
+
combine(result, [links.pop()]);
|
|
1392
|
+
}
|
|
1393
|
+
return result;
|
|
1394
|
+
}
|
|
1395
|
+
function encodeGraph(obj, version = Date.now()) {
|
|
1396
|
+
return encode(obj, { version, isGraph: true });
|
|
1397
|
+
}
|
|
1398
|
+
function encodeQuery(obj, version = 0) {
|
|
1399
|
+
return encode(obj, { version, isGraph: false });
|
|
1400
|
+
}
|
|
1401
|
+
async function* mergeStreams(...streams) {
|
|
1402
|
+
const firstValues = (await Promise.all(streams.map((stream2) => stream2.next()))).map((iter) => iter.value);
|
|
1403
|
+
if (firstValues.some((value) => typeof value === "undefined")) {
|
|
1404
|
+
yield void 0;
|
|
1405
|
+
for (const value of firstValues) {
|
|
1406
|
+
if (typeof value !== "undefined")
|
|
1407
|
+
yield value;
|
|
1408
|
+
}
|
|
1409
|
+
} else {
|
|
1410
|
+
let merged = [];
|
|
1411
|
+
for (const value of firstValues)
|
|
1412
|
+
merge(merged, value);
|
|
1413
|
+
yield merged;
|
|
1414
|
+
}
|
|
1415
|
+
yield* mergeIterators__default["default"](streams);
|
|
1416
|
+
}
|
|
1417
|
+
function makeWatcher() {
|
|
1418
|
+
const listeners = new Set();
|
|
1419
|
+
function write(change) {
|
|
1420
|
+
for (const push of listeners)
|
|
1421
|
+
push(change);
|
|
1422
|
+
}
|
|
1423
|
+
function watch(...args) {
|
|
1424
|
+
return stream.makeStream((push, _end) => {
|
|
1425
|
+
listeners.add(push);
|
|
1426
|
+
if (args.length)
|
|
1427
|
+
Promise.resolve(args[0]).then(push);
|
|
1428
|
+
return () => listeners.delete(push);
|
|
1429
|
+
});
|
|
1430
|
+
}
|
|
1431
|
+
return { write, watch };
|
|
1432
|
+
}
|
|
1433
|
+
function mergeObject(base, change) {
|
|
1434
|
+
if (typeof change !== "object" || typeof base !== "object" || !base || !change) {
|
|
1435
|
+
return change;
|
|
1436
|
+
}
|
|
1437
|
+
for (const prop in change) {
|
|
1438
|
+
if (prop in base) {
|
|
1439
|
+
const value = mergeObject(base[prop], change[prop]);
|
|
1440
|
+
if (value === null) {
|
|
1441
|
+
delete base[prop];
|
|
1442
|
+
} else {
|
|
1443
|
+
base[prop] = value;
|
|
1444
|
+
}
|
|
1445
|
+
} else {
|
|
1446
|
+
base[prop] = change[prop];
|
|
1447
|
+
}
|
|
1448
|
+
}
|
|
1449
|
+
return isEmpty(base) ? null : base;
|
|
1450
|
+
}
|
|
1451
|
+
function cloneObject(object) {
|
|
1452
|
+
if (typeof object !== "object" || !object) {
|
|
1453
|
+
return object;
|
|
1454
|
+
}
|
|
1455
|
+
const clone2 = {};
|
|
1456
|
+
for (const prop in object) {
|
|
1457
|
+
const value = cloneObject(object[prop]);
|
|
1458
|
+
if (value === null)
|
|
1459
|
+
continue;
|
|
1460
|
+
clone2[prop] = value;
|
|
1461
|
+
}
|
|
1462
|
+
return isEmpty(clone2) ? null : clone2;
|
|
1463
|
+
}
|
|
1464
|
+
function wrapObject(object, path) {
|
|
1465
|
+
if (!Array.isArray(path))
|
|
1466
|
+
throw Error("wrapObject.path_not_array " + path);
|
|
1467
|
+
for (let i = path.length - 1; i >= 0; i--) {
|
|
1468
|
+
const $key = path[i];
|
|
1469
|
+
if (typeof $key === "string") {
|
|
1470
|
+
object = { [$key]: object };
|
|
1471
|
+
} else if (Array.isArray(object)) {
|
|
1472
|
+
object = [{ $key, $chi: object }];
|
|
1473
|
+
} else {
|
|
1474
|
+
object = [__spreadValues({ $key }, object)];
|
|
1475
|
+
}
|
|
1476
|
+
}
|
|
1477
|
+
return object;
|
|
1478
|
+
}
|
|
1479
|
+
function unwrapObject(object, path) {
|
|
1480
|
+
if (!Array.isArray(path))
|
|
1481
|
+
throw Error("unwrapObject.path_not_array " + path);
|
|
1482
|
+
for (let i = 0; i < path.length; i++) {
|
|
1483
|
+
if (!object || typeof object !== "object")
|
|
1484
|
+
return;
|
|
1485
|
+
const $key = path[i];
|
|
1486
|
+
if (typeof $key === "string") {
|
|
1487
|
+
if (Array.isArray(object)) {
|
|
1488
|
+
throw Error("unwrapObject.string_key_array:" + $key);
|
|
1489
|
+
}
|
|
1490
|
+
object = object[$key];
|
|
1491
|
+
} else {
|
|
1492
|
+
if (!Array.isArray(object)) {
|
|
1493
|
+
throw Error("unwrapObject.arg_key_object:" + JSON.stringify($key));
|
|
1494
|
+
}
|
|
1495
|
+
const [page, filter] = splitArgs($key);
|
|
1496
|
+
if (page && !page.$cursor) {
|
|
1497
|
+
return object;
|
|
1498
|
+
} else {
|
|
1499
|
+
const target = (page == null ? void 0 : page.$cursor) ? __spreadProps(__spreadValues({}, filter), { $cursor: page.$cursor }) : filter;
|
|
1500
|
+
object = object.find(({ $key: $key2 }) => isEqual__default["default"]($key2, target));
|
|
1501
|
+
}
|
|
1502
|
+
}
|
|
1503
|
+
}
|
|
1504
|
+
return object;
|
|
1505
|
+
}
|
|
1506
|
+
exports.IS_VAL = IS_VAL;
|
|
1507
|
+
exports.add = add;
|
|
1508
|
+
exports.cloneObject = cloneObject;
|
|
1509
|
+
exports.decodeArgs = decode$2;
|
|
1510
|
+
exports.decodeGraph = decodeGraph;
|
|
1511
|
+
exports.decodePath = decode$1;
|
|
1512
|
+
exports.decodeQuery = decodeQuery;
|
|
1513
|
+
exports.decodeUrl = decode$7;
|
|
1514
|
+
exports.decodeValue = decode$3;
|
|
1515
|
+
exports.decorate = decorate;
|
|
1516
|
+
exports.deserialize = deserialize;
|
|
1517
|
+
exports.encodeArgs = encode$2;
|
|
1518
|
+
exports.encodeGraph = encodeGraph;
|
|
1519
|
+
exports.encodePath = encode$1;
|
|
1520
|
+
exports.encodeQuery = encodeQuery;
|
|
1521
|
+
exports.encodeUrl = encode$7;
|
|
1522
|
+
exports.encodeValue = encode$3;
|
|
1523
|
+
exports.err = err;
|
|
1524
|
+
exports.errIf = errIf;
|
|
1525
|
+
exports.finalize = finalize;
|
|
1526
|
+
exports.find = find;
|
|
1527
|
+
exports.findFirst = findFirst;
|
|
1528
|
+
exports.findLast = findLast;
|
|
1529
|
+
exports.getKnown = getKnown;
|
|
1530
|
+
exports.getNodeValue = getNodeValue;
|
|
1531
|
+
exports.isBranch = isBranch;
|
|
1532
|
+
exports.isDef = isDef;
|
|
1533
|
+
exports.isEmpty = isEmpty;
|
|
1534
|
+
exports.isEncoded = isEncoded;
|
|
1535
|
+
exports.isEncodedKey = isEncodedKey;
|
|
1536
|
+
exports.isLink = isLink;
|
|
1537
|
+
exports.isNewer = isNewer;
|
|
1538
|
+
exports.isOlder = isOlder;
|
|
1539
|
+
exports.isPlainObject = isPlainObject;
|
|
1540
|
+
exports.isPrefix = isPrefix;
|
|
1541
|
+
exports.isRange = isRange;
|
|
1542
|
+
exports.keyAfter = keyAfter;
|
|
1543
|
+
exports.keyBefore = keyBefore;
|
|
1544
|
+
exports.keyStep = keyStep;
|
|
1545
|
+
exports.makeId = id;
|
|
1546
|
+
exports.makeWatcher = makeWatcher;
|
|
1547
|
+
exports.merge = merge;
|
|
1548
|
+
exports.mergeObject = mergeObject;
|
|
1549
|
+
exports.mergeStreams = mergeStreams;
|
|
1550
|
+
exports.remove = remove;
|
|
1551
|
+
exports.serialize = serialize;
|
|
1552
|
+
exports.setVersion = setVersion;
|
|
1553
|
+
exports.sieve = sieve;
|
|
1554
|
+
exports.slice = slice;
|
|
1555
|
+
exports.splitArgs = splitArgs;
|
|
1556
|
+
exports.splitRef = splitRef;
|
|
1557
|
+
exports.unwrap = unwrap;
|
|
1558
|
+
exports.unwrapObject = unwrapObject;
|
|
1559
|
+
exports.wrap = wrap;
|
|
1560
|
+
exports.wrapObject = wrapObject;
|
|
1561
|
+
exports.wrapValue = wrapValue;
|