@graffy/common 0.15.25 → 0.16.0-alpha.10

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 CHANGED
@@ -1,17 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
3
+ const isEqual = require("lodash/isEqual.js");
3
4
  const mergeIterators = require("merge-async-iterators");
4
5
  const stream = require("@graffy/stream");
5
- const isEqual = require("lodash/isEqual.js");
6
6
  const _interopDefaultLegacy = (e) => e && typeof e === "object" && "default" in e ? e : { default: e };
7
- const mergeIterators__default = /* @__PURE__ */ _interopDefaultLegacy(mergeIterators);
8
7
  const isEqual__default = /* @__PURE__ */ _interopDefaultLegacy(isEqual);
9
- function encode$7(query) {
10
- return encodeURIComponent(JSON.stringify(query));
11
- }
12
- function decode$7(fields) {
13
- return JSON.parse(decodeURIComponent(fields));
14
- }
8
+ const mergeIterators__default = /* @__PURE__ */ _interopDefaultLegacy(mergeIterators);
15
9
  const textEncoder = new TextEncoder();
16
10
  const textDecoder = new TextDecoder("utf-8");
17
11
  function encode$6(string) {
@@ -33,7 +27,9 @@ function encode$5(number) {
33
27
  return new Uint8Array(buffer);
34
28
  }
35
29
  function decode$5(u8Arr) {
36
- const { buffer, byteOffset, byteLength } = u8Arr;
30
+ const copy = new Uint8Array(8);
31
+ copy.set(u8Arr, 0);
32
+ const { buffer, byteOffset, byteLength } = copy;
37
33
  const view = new DataView(buffer, byteOffset, byteLength);
38
34
  const high = view.getUint8(0);
39
35
  if (high & 128) {
@@ -44,41 +40,93 @@ function decode$5(u8Arr) {
44
40
  }
45
41
  return view.getFloat64(0);
46
42
  }
47
- const alpha = "-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz";
48
- function getByte(view, offset) {
49
- return offset < view.byteLength ? view.getUint8(offset) : 0;
43
+ const MIN_KEY = new Uint8Array();
44
+ const MAX_KEY = new Uint8Array([255]);
45
+ function isMinKey(key) {
46
+ return key.length === 0;
50
47
  }
51
- function getChar(string, offset) {
52
- return offset < string.length ? alpha.indexOf(string[offset]) : 0;
48
+ function isMaxKey(key) {
49
+ return key.length === 1 && key[0] === 255;
53
50
  }
54
- function encode$4(u8Arr) {
55
- const { buffer, byteOffset, byteLength } = u8Arr;
56
- const view = new DataView(buffer, byteOffset, byteLength);
57
- let str = "";
58
- for (let i = 0; i < view.byteLength; i += 3) {
59
- let value = (getByte(view, i) << 16) + (getByte(view, i + 1) << 8) + getByte(view, i + 2);
60
- let gstr = "";
61
- for (let j = 0; j < 4; j++) {
62
- gstr = alpha[value & 63] + gstr;
63
- value = value >> 6 | 0;
64
- }
65
- str += gstr;
51
+ function err(message, { cause = null, ...args } = {}) {
52
+ const e = new Error(message + (args ? " " + JSON.stringify(args) : ""));
53
+ e.cause = cause;
54
+ throw e;
55
+ }
56
+ function errIf(message, condition, args) {
57
+ if (condition)
58
+ err(message, args);
59
+ }
60
+ function isEmpty(object) {
61
+ for (const _ in object)
62
+ return false;
63
+ return true;
64
+ }
65
+ function isDef(value) {
66
+ return typeof value !== "undefined";
67
+ }
68
+ function isPlainObject(arg) {
69
+ return typeof arg === "object" && arg && !Array.isArray(arg) && !ArrayBuffer.isView(arg);
70
+ }
71
+ function cmp(a, b) {
72
+ const l = a.length < b.length ? a.length : b.length;
73
+ for (let i = 0; i < l; i++) {
74
+ if (a[i] < b[i])
75
+ return -1;
76
+ if (a[i] > b[i])
77
+ return 1;
66
78
  }
67
- return str.substr(0, Math.ceil(view.byteLength * 4 / 3));
79
+ if (a.length < b.length)
80
+ return -1;
81
+ if (a.length > b.length)
82
+ return 1;
83
+ return 0;
68
84
  }
69
- function decode$4(string, start = 0) {
70
- const buffer = new ArrayBuffer(Math.floor((string.length - start) * 3 / 4));
71
- const view = new DataView(buffer);
72
- for (let i = start; i < string.length; i += 4) {
73
- let value = (getChar(string, i) << 18) + (getChar(string, i + 1) << 12) + (getChar(string, i + 2) << 6) + getChar(string, i + 3);
74
- for (let j = i * 3 / 4 + 2; j >= i * 3 / 4; j--) {
75
- if (j < view.byteLength)
76
- view.setUint8(j, value & 255);
77
- value = value >> 8 | 0;
85
+ function find(items, compare2, first = 0, last = items.length) {
86
+ let currentFirst = first;
87
+ let currentLast = last;
88
+ while (currentFirst < currentLast) {
89
+ const ix = (currentFirst + currentLast) / 2 | 0;
90
+ const d = compare2(items[ix]);
91
+ if (d < 0) {
92
+ currentFirst = ix + 1;
93
+ } else if (d > 0) {
94
+ currentLast = ix;
95
+ } else {
96
+ return ix;
78
97
  }
79
98
  }
80
- return new Uint8Array(buffer);
99
+ return currentFirst;
81
100
  }
101
+ const stringifyDescriptor = {
102
+ value: function() {
103
+ var _a;
104
+ if ((this == null ? void 0 : this.length) === 0)
105
+ return "\xB7";
106
+ let str = "";
107
+ let bull = false;
108
+ (_a = this == null ? void 0 : this.forEach) == null ? void 0 : _a.call(this, (value, i) => {
109
+ if (value >= 32 && value <= 126) {
110
+ str += String.fromCharCode(value);
111
+ bull = true;
112
+ } else {
113
+ str += (bull ? "\xB7" : "") + ("0" + value.toString(16)).slice(-2) + (i < this.length - 1 ? "\xB7" : "");
114
+ bull = false;
115
+ }
116
+ });
117
+ return str;
118
+ }
119
+ };
120
+ function addStringify(buffer) {
121
+ Object.defineProperties(buffer, {
122
+ toJSON: stringifyDescriptor,
123
+ toString: stringifyDescriptor,
124
+ [Symbol.for("nodejs.util.inspect.custom")]: stringifyDescriptor
125
+ });
126
+ return buffer;
127
+ }
128
+ addStringify(MIN_KEY);
129
+ addStringify(MAX_KEY);
82
130
  const END = 0;
83
131
  const NULL = 1;
84
132
  const FALSE = 2;
@@ -87,12 +135,9 @@ const NUM = 4;
87
135
  const STR = 5;
88
136
  const ARR = 6;
89
137
  const OBJ = 7;
138
+ const EOK = 127;
90
139
  function encodeArray(array) {
91
- return [
92
- ARR,
93
- ...array.flatMap((value) => encodeParts(value)),
94
- END
95
- ];
140
+ return [ARR, ...array.flatMap((value) => encodeParts(value)), END];
96
141
  }
97
142
  function encodeObject(object) {
98
143
  const keys = Object.keys(object).sort();
@@ -124,10 +169,21 @@ function encodeParts(value) {
124
169
  return encodeObject(value);
125
170
  return [NULL];
126
171
  }
127
- function encode$3(value) {
172
+ function encode$4(value) {
128
173
  const parts = encodeParts(value);
129
174
  while (parts[parts.length - 1] === END)
130
175
  parts.pop();
176
+ const lastPart = parts[parts.length - 1];
177
+ if (typeof lastPart !== "number") {
178
+ let end = lastPart.length - 1;
179
+ while (end >= 0 && !lastPart[end])
180
+ end--;
181
+ if (lastPart[end] !== 255) {
182
+ parts[parts.length - 1] = lastPart.slice(0, end + 1);
183
+ } else {
184
+ parts.push(EOK);
185
+ }
186
+ }
131
187
  const length = parts.reduce(
132
188
  (sum, part) => sum + (typeof part === "number" ? 1 : part.length),
133
189
  0
@@ -143,12 +199,12 @@ function encode$3(value) {
143
199
  i += part.length;
144
200
  }
145
201
  }
146
- return encode$4(buffer);
202
+ addStringify(buffer);
203
+ return buffer;
147
204
  }
148
205
  const nextKey = /* @__PURE__ */ new WeakMap();
149
- function decode$3(key) {
206
+ function decode$4(buffer) {
150
207
  let i = 0;
151
- const buffer = decode$4(key, 0);
152
208
  const stack = [[]];
153
209
  function readString() {
154
210
  let start = i;
@@ -182,6 +238,8 @@ function decode$3(key) {
182
238
  const type = buffer[i];
183
239
  const start = ++i;
184
240
  switch (type) {
241
+ case EOK:
242
+ return stack[0][0];
185
243
  case END:
186
244
  popToken();
187
245
  break;
@@ -214,87 +272,67 @@ function decode$3(key) {
214
272
  return stack[0][0];
215
273
  }
216
274
  function keyStep(key) {
217
- if (key === "")
275
+ if (isMinKey(key))
218
276
  return { key, step: 1 };
219
- if (key === "\uFFFF")
277
+ if (isMaxKey(key))
220
278
  return { key, step: -1 };
221
279
  const l = key.length - 1;
222
- switch (key.charCodeAt(l)) {
280
+ let newKey;
281
+ let step;
282
+ switch (key[l]) {
223
283
  case 0:
224
- return { key: key.substr(0, l), step: 1 };
225
- case 65535:
226
- return {
227
- key: key.substr(0, l - 1) + String.fromCharCode(key.charCodeAt(l - 1) + 1),
228
- step: -1
229
- };
284
+ newKey = key.slice(0, l);
285
+ step = 1;
286
+ break;
287
+ case 255:
288
+ newKey = key.slice(0, l);
289
+ newKey[l - 1]++;
290
+ step = -1;
291
+ break;
230
292
  default:
231
- return { key, step: 0 };
293
+ newKey = key;
294
+ step = 0;
232
295
  }
296
+ return { key: addStringify(newKey), step };
233
297
  }
234
298
  function keyBefore(key) {
235
- if (key === "" || key === "\uFFFF" || key === "\0" || key === "\0\uFFFF") {
299
+ if (isMinKey(key) || isMaxKey(key))
236
300
  return key;
237
- }
238
301
  const l = key.length - 1;
239
- return key.charCodeAt(l) === 0 ? key.substr(0, l) : key.substr(0, l) + String.fromCharCode(key.charCodeAt(l) - 1) + "\uFFFF";
302
+ let newKey;
303
+ if (key[l] === 0) {
304
+ newKey = key.slice(0, l);
305
+ } else {
306
+ newKey = new Uint8Array(l + 2);
307
+ newKey.set(key, 0);
308
+ newKey[l]--;
309
+ newKey[l + 1] = 255;
310
+ }
311
+ addStringify(newKey);
312
+ return newKey;
240
313
  }
241
314
  function keyAfter(key) {
242
- if (key === "\uFFFF" || key === "\0" || key === "\0\uFFFF") {
315
+ if (isMaxKey(key))
243
316
  return key;
244
- }
245
317
  const l = key.length - 1;
246
- return key.charCodeAt(l) === 65535 ? key.substr(0, l - 1) + String.fromCharCode(key.charCodeAt(l - 1) + 1) : key + "\0";
247
- }
248
- function err(message, { cause, ...args } = {}) {
249
- const e = new Error(message + (args ? " " + JSON.stringify(args) : ""));
250
- e.cause = cause;
251
- throw e;
252
- }
253
- function errIf(message, condition, args) {
254
- if (condition)
255
- err(message, args);
256
- }
257
- function isEmpty(object) {
258
- for (const _ in object)
259
- return false;
260
- return true;
261
- }
262
- function isDef(value) {
263
- return typeof value !== "undefined";
264
- }
265
- function isPlainObject(arg) {
266
- return typeof arg === "object" && arg && !Array.isArray(arg);
267
- }
268
- function isEncodedKey(str) {
269
- return str[0] === "\0";
270
- }
271
- function find(items, compare2, first = 0, last = items.length) {
272
- let currentFirst = first;
273
- let currentLast = last;
274
- while (currentFirst < currentLast) {
275
- const ix = (currentFirst + currentLast) / 2 | 0;
276
- const d = compare2(items[ix]);
277
- if (d < 0) {
278
- currentFirst = ix + 1;
279
- } else if (d > 0) {
280
- currentLast = ix;
281
- } else {
282
- return ix;
283
- }
284
- }
285
- return currentFirst;
286
- }
287
- function maybeEncode(value) {
288
- return typeof value === "string" ? value : "\0" + encode$3(value);
289
- }
290
- function maybeDecode(string) {
291
- if (isEncodedKey(string)) {
292
- const { key, step } = keyStep(string.slice(1));
293
- const value = key === "" || key === "\uFFFF" ? key : decode$3(key);
294
- return { key: value, step };
318
+ let newKey;
319
+ if (key[l] === 255) {
320
+ newKey = key.slice(0, l);
321
+ newKey[l - 1]++;
295
322
  } else {
296
- return keyStep(string);
323
+ newKey = new Uint8Array(l + 2);
324
+ newKey.set(key, 0);
325
+ newKey[l + 1] = 0;
297
326
  }
327
+ addStringify(newKey);
328
+ return newKey;
329
+ }
330
+ function decodeBound(bound) {
331
+ const { key, step } = keyStep(bound);
332
+ if (isMinKey(key) || isMaxKey(key))
333
+ return { step };
334
+ const value = decode$4(key);
335
+ return { key: value, step };
298
336
  }
299
337
  const pageProps = {
300
338
  $all: 1,
@@ -316,13 +354,13 @@ function splitArgs(arg) {
316
354
  isEmpty(filter) ? void 0 : filter
317
355
  ];
318
356
  }
319
- function encode$2(arg) {
357
+ function encode$3(arg) {
320
358
  if (!isPlainObject(arg))
321
- return { key: maybeEncode(arg) };
359
+ return { key: encode$4(arg) };
322
360
  const [page, filter] = splitArgs(arg);
323
361
  errIf("page_and_filter", page && filter, arg);
324
362
  if (!page)
325
- return { key: maybeEncode(filter || {}) };
363
+ return { key: encode$4(filter || {}) };
326
364
  const { $cursor, ...range } = page;
327
365
  const { $first, $all, $last, $after, $before, $since, $until } = range;
328
366
  const hasRange = !isEmpty(range);
@@ -332,17 +370,17 @@ function encode$2(arg) {
332
370
  errIf("after_and_since", isDef($after) && isDef($since), arg);
333
371
  errIf("before_and_until", isDef($before) && isDef($until), arg);
334
372
  errIf("cursor_and_range_arg", isDef($cursor) && hasRange, arg);
335
- let [key, end] = hasRange ? ["", "\uFFFF"] : [];
373
+ let [key, end] = hasRange ? [MIN_KEY, MAX_KEY] : [];
336
374
  if (isDef($cursor))
337
- key = maybeEncode($cursor);
375
+ key = encode$4($cursor);
338
376
  if (isDef($after))
339
- key = keyAfter(maybeEncode($after));
377
+ key = keyAfter(encode$4($after));
340
378
  if (isDef($before))
341
- end = keyBefore(maybeEncode($before));
379
+ end = keyBefore(encode$4($before));
342
380
  if (isDef($since))
343
- key = maybeEncode($since);
381
+ key = encode$4($since);
344
382
  if (isDef($until))
345
- end = maybeEncode($until);
383
+ end = encode$4($until);
346
384
  if (isDef($last))
347
385
  [key, end] = [end, key];
348
386
  const node = { key };
@@ -352,35 +390,32 @@ function encode$2(arg) {
352
390
  node.limit = $first || $last;
353
391
  return node;
354
392
  }
355
- function decode$2(node) {
356
- if (typeof node === "string")
357
- return node;
393
+ function decode$3(node) {
358
394
  const { key, end, limit } = node;
359
- if (!isEncodedKey(key) && (!isDef(end) || end === key))
360
- return key;
361
395
  errIf("no_key", !isDef(key));
362
396
  errIf("limit_without_end", isDef(limit) && !isDef(end));
363
- const kParts = maybeDecode(key);
364
- if (!isDef(end))
397
+ const kParts = decodeBound(key);
398
+ if (!isDef(end) || cmp(key, end) === 0)
365
399
  return kParts.key;
366
- const eParts = maybeDecode(end);
367
- const [lower, upper] = key < end ? [kParts, eParts] : [eParts, kParts];
400
+ const eParts = decodeBound(end);
401
+ const reverse = cmp(key, end) > 0;
402
+ const [lower, upper] = reverse ? [eParts, kParts] : [kParts, eParts];
368
403
  const args = {};
369
404
  if (limit) {
370
- args[key < end ? "$first" : "$last"] = limit;
371
- } else if (lower.key === "" && upper.key === "\uFFFF") {
405
+ args[reverse ? "$last" : "$first"] = limit;
406
+ } else if (isMinKey(key) && isMaxKey(end) || isMinKey(end) && isMaxKey(key)) {
372
407
  args.$all = true;
373
408
  }
374
- if (lower.key !== "") {
409
+ if (lower.key && !isMinKey(lower.key)) {
375
410
  args[lower.step === 1 ? "$after" : "$since"] = lower.key;
376
411
  }
377
- if (upper.key !== "\uFFFF") {
412
+ if (upper.key && !isMaxKey(upper.key)) {
378
413
  args[upper.step === -1 ? "$before" : "$until"] = upper.key;
379
414
  }
380
415
  return args;
381
416
  }
382
417
  const PATH_SEPARATOR = ".";
383
- function encode$1(path) {
418
+ function encode$2(path) {
384
419
  if (typeof path === "string") {
385
420
  if (!path.length || path === PATH_SEPARATOR)
386
421
  return [];
@@ -390,25 +425,25 @@ function encode$1(path) {
390
425
  throw Error("encodePath.invalid:" + JSON.stringify(path));
391
426
  }
392
427
  function encodeSegment(seg) {
393
- if (typeof seg === "string")
428
+ if (ArrayBuffer.isView(seg))
394
429
  return seg;
395
- const { key, end } = encode$2(seg);
396
- if (end)
397
- throw "encodePath.unexpected_range_key";
398
- return key;
430
+ const node = encode$3(seg);
431
+ if (node.end)
432
+ return node;
433
+ return node.key;
399
434
  }
400
- if (!isPlainObject(path[path.length - 1]))
401
- return path.map(encodeSegment);
402
- const [page, filter = {}] = splitArgs(path[path.length - 1]);
403
- if (!page)
404
- return path.map(encodeSegment);
405
- return path.slice(0, -1).concat([filter]).map(encodeSegment);
435
+ if (isPlainObject(path[path.length - 1])) {
436
+ const [page, filter] = splitArgs(path[path.length - 1]);
437
+ if (page)
438
+ path = path.slice(0, -1).concat([filter || MIN_KEY]);
439
+ }
440
+ return path.map(encodeSegment);
406
441
  }
407
- function decode$1(path) {
442
+ function decode$2(path) {
408
443
  if (!Array.isArray(path)) {
409
444
  throw Error("decodePath.invalid:" + JSON.stringify(path));
410
445
  }
411
- return path.map((key) => decode$2({ key }));
446
+ return path.map((key) => decode$3({ key }));
412
447
  }
413
448
  function splitRef($ref) {
414
449
  if (!Array.isArray($ref))
@@ -428,16 +463,17 @@ let customAlphabet = (alphabet, defaultSize = 21) => {
428
463
  return id2;
429
464
  };
430
465
  };
466
+ const alpha = "-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz";
431
467
  const id = customAlphabet(alpha, 20);
432
468
  function findFirst(children, target, first, last) {
433
469
  return find(
434
470
  children,
435
471
  ({ key, end }) => {
436
- if (key === target || end && key < target && end >= target)
472
+ const keyCmp = cmp(key, target);
473
+ const endCmp = end && cmp(end, target);
474
+ if (end && keyCmp < 0 && endCmp >= 0)
437
475
  return 0;
438
- if (key < target)
439
- return -1;
440
- return 1;
476
+ return keyCmp;
441
477
  },
442
478
  first,
443
479
  last
@@ -445,7 +481,7 @@ function findFirst(children, target, first, last) {
445
481
  }
446
482
  function findLast(children, end, first, last) {
447
483
  const ix = findFirst(children, end, first, last);
448
- return children[ix] && children[ix].key <= end ? ix + 1 : ix;
484
+ return children[ix] && cmp(children[ix].key, end) <= 0 ? ix + 1 : ix;
449
485
  }
450
486
  function isRange(node) {
451
487
  return node && typeof node.end !== "undefined";
@@ -459,9 +495,6 @@ function isPrefix(node) {
459
495
  function isLink(node) {
460
496
  return node && typeof node.path !== "undefined";
461
497
  }
462
- function isEncoded(node) {
463
- return node && node.key[0] === "\0";
464
- }
465
498
  function isOlder(node, version) {
466
499
  return typeof node.version !== "undefined" && node.version < version;
467
500
  }
@@ -472,12 +505,12 @@ function add(base, diff) {
472
505
  let changed = false;
473
506
  let index = 0;
474
507
  for (const node of diff) {
475
- const cmp = compare(node);
508
+ const cmp2 = compare(node);
476
509
  const nodeIsBranch = isBranch(node);
477
- index = find(base, cmp, index);
510
+ index = find(base, cmp2, index);
478
511
  const item = base[index];
479
512
  const itemIsBranch = isBranch(item);
480
- if (!item || cmp(item)) {
513
+ if (!item || cmp2(item)) {
481
514
  base.splice(index, 0, clone(node));
482
515
  changed = true;
483
516
  continue;
@@ -502,7 +535,7 @@ function add(base, diff) {
502
535
  }
503
536
  function compare(node) {
504
537
  return (item) => {
505
- const v = compareValue(item.key, node.key) || compareValue(item.end, node.end) || compareValue(item.limit, node.limit);
538
+ const v = cmp(item.key, node.key) || compareValue(!!item.end, !!node.end) || item.end && cmp(item.end, node.end) || compareValue(item.limit, node.limit);
506
539
  return v;
507
540
  };
508
541
  }
@@ -546,9 +579,9 @@ function mergeRanges$1(base, node) {
546
579
  if (node.version < base.version)
547
580
  [node, base] = [base, node];
548
581
  return [
549
- base.key < node.key && { ...base, end: keyBefore(node.key) },
582
+ cmp(base.key, node.key) < 0 && { ...base, end: keyBefore(node.key) },
550
583
  node,
551
- base.end > node.end && { ...base, key: keyAfter(node.end) }
584
+ cmp(base.end, node.end) > 0 && { ...base, key: keyAfter(node.end) }
552
585
  ].filter(Boolean);
553
586
  }
554
587
  function insertNode$1(current, change, start = 0) {
@@ -557,7 +590,7 @@ function insertNode$1(current, change, start = 0) {
557
590
  const key = change.key;
558
591
  const index = findFirst(current, key, start);
559
592
  const node = current[index];
560
- if (node && node.key <= key) {
593
+ if (node && cmp(node.key, key) <= 0) {
561
594
  return isRange(node) ? insertNodeIntoRange$1(current, index, change) : updateNode$1(current, index, change);
562
595
  } else {
563
596
  current.splice(index, 0, change);
@@ -571,9 +604,9 @@ function insertNodeIntoRange$1(current, index, change) {
571
604
  if (!newChange)
572
605
  return;
573
606
  const insertions = [
574
- range.key < key && { ...range, end: keyBefore(key) },
607
+ cmp(range.key, key) < 0 && { ...range, end: keyBefore(key) },
575
608
  newChange,
576
- range.end > key && { ...range, key: keyAfter(key) }
609
+ cmp(range.end, key) > 0 && { ...range, key: keyAfter(key) }
577
610
  ].filter(Boolean);
578
611
  current.splice(index, 1, ...insertions);
579
612
  return index + insertions.length - 1;
@@ -597,7 +630,7 @@ function updateNode$1(current, index, change) {
597
630
  function getNewer(node, base) {
598
631
  const { version } = base;
599
632
  if (isBranch(node)) {
600
- const children = [{ key: "", end: "\uFFFF", version }];
633
+ const children = [{ key: MIN_KEY, end: MAX_KEY, version }];
601
634
  merge(children, node.children);
602
635
  return children.length === 1 ? null : { ...node, children };
603
636
  } else {
@@ -605,8 +638,13 @@ function getNewer(node, base) {
605
638
  }
606
639
  }
607
640
  const IS_VAL = Symbol("IS_VAL");
641
+ function makeNode(seg, props2) {
642
+ if (ArrayBuffer.isView(seg))
643
+ return { key: seg, ...props2 };
644
+ return { ...seg, ...props2 };
645
+ }
608
646
  function wrapValue(value, path, version = 0) {
609
- const node = { ...encode$2(path[path.length - 1]), value, version };
647
+ const node = makeNode(path[path.length - 1], { value, version });
610
648
  return wrap([node], path.slice(0, -1), version);
611
649
  }
612
650
  function wrap(children, path, version = 0, prefix = false) {
@@ -616,16 +654,16 @@ function wrap(children, path, version = 0, prefix = false) {
616
654
  return children;
617
655
  let i = path.length - 1;
618
656
  if (!Array.isArray(children)) {
619
- children = [{ ...encode$2(path[i--]), value: children, version }];
657
+ children = [makeNode(path[i--], { value: children, version })];
620
658
  } else {
621
659
  if (!children.length)
622
660
  return;
623
- children = [{ ...encode$2(path[i--]), version, children }];
661
+ children = [makeNode(path[i--], { children, version })];
624
662
  }
625
663
  if (prefix)
626
664
  children[0].prefix = true;
627
665
  while (i >= 0)
628
- children = [{ ...encode$2(path[i--]), version, children }];
666
+ children = [makeNode(path[i--], { children, version })];
629
667
  return children;
630
668
  }
631
669
  function unwrap(tree, path) {
@@ -634,12 +672,14 @@ function unwrap(tree, path) {
634
672
  let children = tree;
635
673
  let node = { children };
636
674
  for (let i = 0; i < path.length; i++) {
637
- const { key } = encode$2(path[i]);
675
+ const key = path[i];
676
+ if (!ArrayBuffer.isView(key))
677
+ throw Error("unwrap.ranges_unsupported");
638
678
  children = node.children;
639
679
  if (!children)
640
680
  return null;
641
681
  node = children[findFirst(children, key)];
642
- if (!node || node.key > key)
682
+ if (!node || cmp(node.key, key) > 0)
643
683
  return void 0;
644
684
  if (isRange(node))
645
685
  return null;
@@ -666,7 +706,7 @@ function remove(children, path) {
666
706
  const key = path[0];
667
707
  const ix = findFirst(children, key);
668
708
  const node = children[ix];
669
- if (!node || node.key > key || isRange(node))
709
+ if (!node || cmp(node.key, key) > 0 || isRange(node))
670
710
  return children;
671
711
  if (path.length === 1) {
672
712
  return children.slice(0, ix).concat(children.slice(ix + 1));
@@ -721,12 +761,12 @@ function slice(graph, query, root) {
721
761
  function sliceNode(graph, query, result) {
722
762
  const { key, version } = query;
723
763
  const { root } = result;
724
- if (!graph || graph.key > key || isOlder(graph, version)) {
764
+ if (!graph || cmp(graph.key, key) > 0 || isOlder(graph, version)) {
725
765
  result.addUnknown(query);
726
766
  } else if (isRange(graph)) {
727
767
  if (isBranch(query)) {
728
768
  const { known } = slice(
729
- [{ key: "", end: "\uFFFF", version: graph.version }],
769
+ [{ key: MIN_KEY, end: MAX_KEY, version: graph.version }],
730
770
  query.children
731
771
  );
732
772
  result.addKnown({ key, version: graph.version, children: known });
@@ -757,7 +797,7 @@ function sliceNode(graph, query, result) {
757
797
  result.addKnown(graph);
758
798
  } else if (isBranch(query)) {
759
799
  const { known } = slice(
760
- [{ key: "", end: "\uFFFF", version: graph.version }],
800
+ [{ key: MIN_KEY, end: MAX_KEY, version: graph.version }],
761
801
  query.children
762
802
  );
763
803
  result.addKnown({ key, version: graph.version, children: known });
@@ -767,8 +807,8 @@ function sliceNode(graph, query, result) {
767
807
  }
768
808
  function sliceRange(graph, query, result) {
769
809
  let { key, end, limit = Infinity, version } = query;
770
- const step = key < end ? 1 : -1;
771
- if (graph[0].key === "" && graph[0].prefix && graph[0].children) {
810
+ const step = cmp(key, end) < 0 ? 1 : -1;
811
+ if (isMinKey(graph[0].key) && graph[0].prefix && graph[0].children) {
772
812
  const { known, unknown } = slice(graph[0].children, [query], result.root);
773
813
  if (known)
774
814
  result.addKnown({ ...graph[0], children: known });
@@ -776,10 +816,10 @@ function sliceRange(graph, query, result) {
776
816
  result.addUnknown({ ...query[0], children: unknown });
777
817
  return;
778
818
  }
779
- if (key < end) {
780
- for (let i = findFirst(graph, key); key <= end && limit > 0; i++) {
819
+ if (cmp(key, end) < 0) {
820
+ for (let i = findFirst(graph, key); cmp(key, end) <= 0 && limit > 0; i++) {
781
821
  const node = graph[i];
782
- if (!node || key < node.key || isOlder(node, version))
822
+ if (!node || cmp(key, node.key) < 0 || isOlder(node, version))
783
823
  break;
784
824
  if (isRange(node)) {
785
825
  result.addKnown(getOverlap(node, key, end));
@@ -790,9 +830,9 @@ function sliceRange(graph, query, result) {
790
830
  key = keyAfter(node.end || node.key);
791
831
  }
792
832
  } else {
793
- for (let i = findLast(graph, key) - 1; key >= end && limit > 0; i--) {
833
+ for (let i = findLast(graph, key) - 1; cmp(key, end) >= 0 && limit > 0; i--) {
794
834
  const node = graph[i];
795
- if (!node || key > (node.end || node.key) || isOlder(node, version))
835
+ if (!node || cmp(key, node.end || node.key) > 0 || isOlder(node, version))
796
836
  break;
797
837
  if (isRange(node)) {
798
838
  result.addKnown(getOverlap(node, end, key));
@@ -803,18 +843,18 @@ function sliceRange(graph, query, result) {
803
843
  key = keyBefore(node.key);
804
844
  }
805
845
  }
806
- if (limit && (step < 0 ? key > end : key < end)) {
846
+ if (limit && (step < 0 ? cmp(key, end) > 0 : cmp(key, end) < 0)) {
807
847
  const unknown = { ...query, key, end, limit };
808
848
  result.addUnknown(unknown);
809
849
  }
810
850
  }
811
851
  function getOverlap(node, key, end) {
812
- if (node.key >= key && node.end <= end)
852
+ if (cmp(node.key, key) >= 0 && cmp(node.end, end) <= 0)
813
853
  return node;
814
854
  return {
815
855
  ...node,
816
- key: node.key > key ? node.key : key,
817
- end: node.end < end ? node.end : end
856
+ key: cmp(node.key, key) > 0 ? node.key : key,
857
+ end: cmp(node.end, end) < 0 ? node.end : end
818
858
  };
819
859
  }
820
860
  function sieve(current, changes, result = []) {
@@ -828,7 +868,7 @@ function insertRange(current, change, result, start = 0) {
828
868
  const { key, end } = change;
829
869
  const keyIx = findFirst(current, key, start);
830
870
  const endIx = findLast(current, end, keyIx);
831
- if (keyIx === endIx && !(current[keyIx] && current[keyIx].key <= key && current[keyIx].end >= end)) {
871
+ if (keyIx === endIx && !(current[keyIx] && cmp(current[keyIx].key, end) <= 0 && cmp(current[keyIx].end || current[keyIx].key, key) >= 0)) {
832
872
  return keyIx;
833
873
  }
834
874
  const appliedChange = [];
@@ -836,7 +876,7 @@ function insertRange(current, change, result, start = 0) {
836
876
  for (let i = keyIx; i < endIx; i++) {
837
877
  const node = current[i];
838
878
  if (isRange(node) && node.version >= 0) {
839
- if (node.key > currentKey) {
879
+ if (cmp(node.key, currentKey) > 0) {
840
880
  appliedChange.push({
841
881
  key: currentKey,
842
882
  end: keyBefore(node.key),
@@ -854,11 +894,11 @@ function insertRange(current, change, result, start = 0) {
854
894
  currentKey = keyAfter(node.key);
855
895
  }
856
896
  }
857
- if (currentKey >= change.end) {
897
+ if (cmp(currentKey, change.end) >= 0) {
858
898
  break;
859
899
  }
860
900
  }
861
- if (currentKey <= change.end) {
901
+ if (cmp(currentKey, change.end) <= 0) {
862
902
  appliedChange.push({
863
903
  key: currentKey,
864
904
  end: change.end,
@@ -883,16 +923,16 @@ function mergeRanges(base, node) {
883
923
  if (node.version < base.version)
884
924
  [node, base] = [base, node];
885
925
  return [
886
- base.key < node.key && { ...base, end: keyBefore(node.key) },
926
+ cmp(base.key, node.key) < 0 && { ...base, end: keyBefore(node.key) },
887
927
  node,
888
- base.end > node.end && { ...base, key: keyAfter(node.end) }
928
+ cmp(base.end, node.end) > 0 && { ...base, key: keyAfter(node.end) }
889
929
  ].filter(Boolean);
890
930
  }
891
931
  function insertNode(current, change, result, start = 0) {
892
932
  const key = change.key;
893
933
  const index = findFirst(current, key, start);
894
934
  const node = current[index];
895
- if (node && node.key <= key) {
935
+ if (node && cmp(node.key, key) <= 0) {
896
936
  return isRange(node) ? insertNodeIntoRange(current, index, change, result) : updateNode(current, index, change, result);
897
937
  } else {
898
938
  return index;
@@ -907,9 +947,9 @@ function insertNodeIntoRange(current, index, change, result) {
907
947
  return;
908
948
  result.push(newChange);
909
949
  const insertions = [
910
- range.key < key && { ...range, end: keyBefore(key) },
950
+ cmp(range.key, key) < 0 && { ...range, end: keyBefore(key) },
911
951
  newNode,
912
- range.end > key && { ...range, key: keyAfter(key) }
952
+ cmp(range.end, key) > 0 && { ...range, key: keyAfter(key) }
913
953
  ].filter(Boolean);
914
954
  current.splice(index, 1, ...insertions);
915
955
  return index + insertions.length - 1;
@@ -953,7 +993,7 @@ function isPathEqual(first, second) {
953
993
  }
954
994
  function getNewerNode(node, base) {
955
995
  if (isBranch(node)) {
956
- const emptyNode = { key: "", end: "\uFFFF", version: base.version };
996
+ const emptyNode = { key: MIN_KEY, end: MAX_KEY, version: base.version };
957
997
  const children = [emptyNode];
958
998
  sieve(children, node.children);
959
999
  return children.length === 1 && children[0] === emptyNode ? null : { ...node, children };
@@ -971,9 +1011,10 @@ function getNewerChange(node, base) {
971
1011
  return node.version >= base.version ? node : null;
972
1012
  }
973
1013
  }
974
- function setVersion(graph, version) {
1014
+ function setVersion(graph, version, onlyIfZero = false) {
975
1015
  for (const node of graph) {
976
- node.version = version;
1016
+ if (!onlyIfZero || !node.version)
1017
+ node.version = version;
977
1018
  if (node.children)
978
1019
  setVersion(node.children, version);
979
1020
  }
@@ -984,7 +1025,7 @@ function getKnown(graph, version = 0) {
984
1025
  for (const { key, end, children } of graph) {
985
1026
  const node = { key, version };
986
1027
  if (end) {
987
- if (end !== key)
1028
+ if (cmp(end, key) !== 0)
988
1029
  node.end = end;
989
1030
  node.value = 1;
990
1031
  }
@@ -997,15 +1038,17 @@ function getKnown(graph, version = 0) {
997
1038
  }
998
1039
  return query;
999
1040
  }
1000
- function finalize(graph, query, version = Date.now()) {
1001
- let result = [{ key: "", end: "\uFFFF", version: 0 }];
1041
+ function finalize(graph, query, version = Date.now(), suppressSetVersion = false) {
1042
+ let result = [{ key: MIN_KEY, end: MAX_KEY, version: 0 }];
1002
1043
  merge(result, graph);
1003
1044
  if (query)
1004
1045
  result = slice(result, query).known || [];
1005
- result = setVersion(result, version);
1046
+ if (!suppressSetVersion)
1047
+ result = setVersion(result, version);
1006
1048
  return result;
1007
1049
  }
1008
- function decode(nodes = [], { isGraph } = {}) {
1050
+ const PRE_CHI_PUT = Symbol("PREFIX_CHILDREN_$PUT");
1051
+ function decode$1(nodes = [], { isGraph } = {}) {
1009
1052
  function decodeChildren(nodes2) {
1010
1053
  let result = [];
1011
1054
  let allStrs = true;
@@ -1020,20 +1063,22 @@ function decode(nodes = [], { isGraph } = {}) {
1020
1063
  result.push(...objects);
1021
1064
  }
1022
1065
  const putRanges = [];
1066
+ const prefixChildPuts = [];
1023
1067
  let lastNode = null;
1024
1068
  function addPutRange({ key, end }) {
1025
1069
  if (lastNode) {
1026
1070
  if (lastNode.end) {
1027
- if (key === keyAfter(lastNode.end)) {
1071
+ if (cmp(key, keyAfter(lastNode.end)) === 0) {
1028
1072
  lastNode.end = end || key;
1029
- return end && end !== key;
1073
+ return end && cmp(end, key) !== 0;
1030
1074
  }
1031
1075
  } else {
1032
- if (key === keyAfter(lastNode.key))
1076
+ if (cmp(key, keyAfter(lastNode.key)) === 0) {
1033
1077
  key = lastNode.key;
1078
+ }
1034
1079
  }
1035
1080
  }
1036
- if (end && key !== end) {
1081
+ if (end && cmp(key, end) !== 0) {
1037
1082
  lastNode = { key, end };
1038
1083
  putRanges.push(lastNode);
1039
1084
  return true;
@@ -1044,9 +1089,13 @@ function decode(nodes = [], { isGraph } = {}) {
1044
1089
  for (const node of nodes2) {
1045
1090
  if (isGraph && addPutRange(node))
1046
1091
  continue;
1047
- if (isPrefix(node))
1048
- pushResult(...decodePrefixNode(node));
1049
- else if (isGraph && isRange(node))
1092
+ if (isPrefix(node)) {
1093
+ const decodedChildren = decodePrefixNode(node);
1094
+ if (PRE_CHI_PUT in decodedChildren) {
1095
+ prefixChildPuts.push(...decodedChildren[PRE_CHI_PUT]);
1096
+ }
1097
+ pushResult(...decodedChildren);
1098
+ } else if (isGraph && isRange(node))
1050
1099
  pushResult(decodeRangeNode(node));
1051
1100
  else if (isBranch(node))
1052
1101
  pushResult(decodeBranchNode(node));
@@ -1055,7 +1104,7 @@ function decode(nodes = [], { isGraph } = {}) {
1055
1104
  else
1056
1105
  pushResult(decodeLeafNode(node));
1057
1106
  }
1058
- if (allNums || allStrs) {
1107
+ if (allStrs || allNums && putRanges.length === 1 && cmp(putRanges[0].key, 0) === 0 && cmp(putRanges[0].end, Infinity) === 0) {
1059
1108
  result = result.reduce(
1060
1109
  (collection, item) => {
1061
1110
  if (Array.isArray(item)) {
@@ -1076,26 +1125,28 @@ function decode(nodes = [], { isGraph } = {}) {
1076
1125
  );
1077
1126
  }
1078
1127
  if (isGraph && putRanges.length) {
1079
- if (putRanges[0].key === "" && putRanges[0].end === "\uFFFF") {
1128
+ if (isMinKey(putRanges[0].key) && isMaxKey(putRanges[0].end)) {
1080
1129
  Object.defineProperty(result, "$put", { value: true });
1081
1130
  } else {
1082
1131
  Object.defineProperty(result, "$put", {
1083
- value: putRanges.map((rNode) => decode$2(rNode))
1132
+ value: putRanges.map((rNode) => decode$3(rNode)).concat(prefixChildPuts)
1084
1133
  });
1085
1134
  }
1135
+ } else if (prefixChildPuts.length) {
1136
+ Object.defineProperty(result, "$put", { value: prefixChildPuts });
1086
1137
  }
1087
1138
  return result;
1088
1139
  }
1089
1140
  function decodePrefixNode(node) {
1090
- let args = decode$2(node);
1091
- if (args === "")
1141
+ let args = decode$3(node);
1142
+ if (!args)
1092
1143
  args = {};
1093
1144
  if (typeof args === "string") {
1094
1145
  throw Error("decode.unencoded_prefix: " + args);
1095
1146
  }
1096
1147
  if (isLink(node)) {
1097
1148
  args.$all = true;
1098
- const $ref = decode$1(node.path);
1149
+ const $ref = decode$2(node.path);
1099
1150
  const lastKey = $ref[$ref.length - 1];
1100
1151
  if (typeof lastKey === "string") {
1101
1152
  throw Error("decode.unencoded_prefix_ref: " + node.path);
@@ -1118,34 +1169,45 @@ function decode(nodes = [], { isGraph } = {}) {
1118
1169
  }
1119
1170
  child.$key = { ...args, ...child.$key };
1120
1171
  }
1172
+ if (children.$put === true) {
1173
+ children[PRE_CHI_PUT] = [{ ...args, $all: true }];
1174
+ } else if (Array.isArray(children.$put)) {
1175
+ children[PRE_CHI_PUT] = children.$put.map((rarg) => ({
1176
+ ...args,
1177
+ ...rarg
1178
+ }));
1179
+ } else if (isDef(children.$put)) {
1180
+ children[PRE_CHI_PUT] = [{ ...args, ...children.$put }];
1181
+ }
1121
1182
  return children;
1122
1183
  }
1123
1184
  function decodeBranchNode(node) {
1124
1185
  const child = decodeChildren(node.children);
1125
- child.$key = decode$2(node);
1186
+ child.$key = decode$3(node);
1126
1187
  return child;
1127
1188
  }
1128
1189
  function decodeLeafNode(node) {
1129
1190
  const child = isGraph ? { $val: node.value } : {};
1130
- child.$key = decode$2(node);
1191
+ child.$key = decode$3(node);
1131
1192
  return child;
1132
1193
  }
1133
1194
  function decodeRangeNode(node) {
1134
- if (node.key === node.end)
1135
- return { $key: decode$2({ key: node.key }) };
1195
+ if (cmp(node.key, node.end) === 0) {
1196
+ return { $key: decode$3({ key: node.key }) };
1197
+ }
1136
1198
  }
1137
1199
  function decodeLinkNode(node) {
1138
- const linkObject = { $key: decode$2(node) };
1139
- Object.defineProperty(linkObject, "$ref", { value: decode$1(node.path) });
1200
+ const linkObject = { $key: decode$3(node) };
1201
+ Object.defineProperty(linkObject, "$ref", { value: decode$2(node.path) });
1140
1202
  return linkObject;
1141
1203
  }
1142
1204
  return decodeChildren(nodes);
1143
1205
  }
1144
1206
  function decodeGraph(graph) {
1145
- return decode(graph, { isGraph: true });
1207
+ return decode$1(graph, { isGraph: true });
1146
1208
  }
1147
1209
  function decodeQuery(query) {
1148
- return decode(query, { isGraph: false });
1210
+ return decode$1(query, { isGraph: false });
1149
1211
  }
1150
1212
  const REF = Symbol();
1151
1213
  const PRE = Symbol();
@@ -1159,25 +1221,27 @@ function decorate(rootGraph, rootQuery) {
1159
1221
  query = [query];
1160
1222
  let graph;
1161
1223
  if (query.$ref) {
1162
- const { $ref, ...props } = query;
1224
+ const { $ref, ...props2 } = query;
1163
1225
  const [range, filter] = splitRef($ref);
1164
- const path = encode$1($ref);
1226
+ const path = encode$2($ref);
1165
1227
  const targetPlumGraph = unwrap(rootGraph, path);
1166
- if (range)
1167
- targetPlumGraph[PRE] = filter;
1168
- graph = construct(
1169
- targetPlumGraph,
1170
- range ? { $key: range, ...props } : props
1171
- );
1172
- Object.defineProperty(graph, "$ref", { value: $ref });
1228
+ if (targetPlumGraph) {
1229
+ if (range)
1230
+ targetPlumGraph[PRE] = filter;
1231
+ graph = construct(
1232
+ targetPlumGraph,
1233
+ range ? { $key: range, ...props2 } : props2
1234
+ );
1235
+ Object.defineProperty(graph, "$ref", { value: $ref });
1236
+ }
1173
1237
  } else if (Array.isArray(query)) {
1174
1238
  let pageKey;
1175
1239
  graph = query.flatMap((item, i) => {
1176
1240
  if (!(item == null ? void 0 : item.$key)) {
1177
1241
  return construct(descend(plumGraph, i), item);
1178
1242
  }
1179
- const { $key, $chi, ...props } = item;
1180
- const subQuery = $chi || (isEmpty(props) ? 1 : props);
1243
+ const { $key, $chi, ...props2 } = item;
1244
+ const subQuery = $chi || (isEmpty(props2) ? 1 : props2);
1181
1245
  if (!isPlainObject($key) || !splitArgs($key)[0]) {
1182
1246
  return construct(descend(plumGraph, $key), subQuery);
1183
1247
  }
@@ -1189,10 +1253,10 @@ function decorate(rootGraph, rootQuery) {
1189
1253
  pageKey = $key;
1190
1254
  const children = slice2(plumGraph, $key);
1191
1255
  return children.filter((node) => !isRange(node)).map((node) => {
1192
- const $key2 = decode$2(node);
1256
+ const $key2 = decode$3(node);
1193
1257
  const subResult = construct(getValue(node), subQuery);
1194
1258
  if (typeof subResult === "object") {
1195
- subResult.$key = children[PRE] ? { ...children[PRE], $cursor: $key2 } : $key2;
1259
+ subResult.$key = children[PRE] && !isMinKey(children[PRE]) ? { ...children[PRE], $cursor: $key2 } : $key2;
1196
1260
  }
1197
1261
  return subResult;
1198
1262
  });
@@ -1220,13 +1284,13 @@ function decorate(rootGraph, rootQuery) {
1220
1284
  }
1221
1285
  if (plumGraph[REF]) {
1222
1286
  Object.defineProperty(graph, "$ref", {
1223
- value: decode$1(plumGraph[REF])
1287
+ value: decode$2(plumGraph[REF])
1224
1288
  });
1225
1289
  }
1226
1290
  return graph;
1227
1291
  }
1228
1292
  function descend(children, $key) {
1229
- const { key } = encode$2($key);
1293
+ const key = ArrayBuffer.isView($key) ? $key : encode$3($key).key;
1230
1294
  if (!Array.isArray(children))
1231
1295
  return null;
1232
1296
  const ix = findFirst(children, key);
@@ -1235,7 +1299,7 @@ function decorate(rootGraph, rootQuery) {
1235
1299
  return;
1236
1300
  if (isRange(node) && node.end >= key)
1237
1301
  return null;
1238
- if (node.key !== key)
1302
+ if (cmp(node.key, key) !== 0)
1239
1303
  return;
1240
1304
  const result2 = getValue(node);
1241
1305
  if (node.prefix)
@@ -1257,14 +1321,14 @@ function decorate(rootGraph, rootQuery) {
1257
1321
  const [range, filter] = splitArgs($key);
1258
1322
  if (isDef(filter)) {
1259
1323
  children = descend(children, filter);
1260
- } else if (children[0].key === "" && children[0].prefix) {
1261
- children = descend(children, "");
1324
+ } else if (isMinKey(children[0].key) && children[0].prefix) {
1325
+ children = descend(children, MIN_KEY);
1262
1326
  }
1263
- const { key, end, limit = Infinity } = encode$2(range);
1327
+ const { key, end, limit = Infinity } = encode$3(range);
1264
1328
  const ix = findFirst(children, key);
1265
1329
  let i = ix;
1266
1330
  let result2;
1267
- if (key < end) {
1331
+ if (cmp(key, end) < 0) {
1268
1332
  for (let n = 0; i < children.length && n < limit; i++) {
1269
1333
  if (!isRange(children[i]))
1270
1334
  n++;
@@ -1309,90 +1373,228 @@ function addPageMeta(graph, args) {
1309
1373
  let $next = isDef($page.$before) ? { ...filter, $first: count, $since: $page.$before } : isDef($page.$until) ? { ...filter, $first: count, $after: $page.$until } : null;
1310
1374
  Object.assign(graph, { $page, $next, $prev });
1311
1375
  }
1312
- function serialize(obj) {
1313
- return JSON.stringify(obj).replace(/\uffff/g, "\\uffff");
1376
+ function getByte(view, offset) {
1377
+ return offset < view.byteLength ? view.getUint8(offset) : 0;
1378
+ }
1379
+ function getChar(string, offset) {
1380
+ return offset < string.length ? alpha.indexOf(string[offset]) : 0;
1381
+ }
1382
+ function encode$1(u8Arr) {
1383
+ const { buffer, byteOffset, byteLength } = u8Arr;
1384
+ const view = new DataView(buffer, byteOffset, byteLength);
1385
+ let str = "";
1386
+ for (let i = 0; i < view.byteLength; i += 3) {
1387
+ let value = (getByte(view, i) << 16) + (getByte(view, i + 1) << 8) + getByte(view, i + 2);
1388
+ let gstr = "";
1389
+ for (let j = 0; j < 4; j++) {
1390
+ gstr = alpha[value & 63] + gstr;
1391
+ value = value >> 6 | 0;
1392
+ }
1393
+ str += gstr;
1394
+ }
1395
+ return str.substring(0, Math.ceil(view.byteLength * 4 / 3));
1396
+ }
1397
+ function decode(string, start = 0) {
1398
+ const buffer = new ArrayBuffer(Math.floor((string.length - start) * 3 / 4));
1399
+ const view = new DataView(buffer);
1400
+ for (let i = start; i < string.length; i += 4) {
1401
+ let value = (getChar(string, i) << 18) + (getChar(string, i + 1) << 12) + (getChar(string, i + 2) << 6) + getChar(string, i + 3);
1402
+ for (let j = i * 3 / 4 + 2; j >= i * 3 / 4; j--) {
1403
+ if (j < view.byteLength)
1404
+ view.setUint8(j, value & 255);
1405
+ value = value >> 8 | 0;
1406
+ }
1407
+ }
1408
+ return addStringify(new Uint8Array(buffer));
1409
+ }
1410
+ const props = [
1411
+ "end",
1412
+ "version",
1413
+ "limit",
1414
+ "value",
1415
+ "path",
1416
+ "prefix",
1417
+ "children"
1418
+ ];
1419
+ function serializeKey(key) {
1420
+ if (key[0] === STR) {
1421
+ const last = key[key.length - 1];
1422
+ if (last !== 0 && last !== 255) {
1423
+ return decode$4(key);
1424
+ }
1425
+ }
1426
+ return "\0" + encode$1(key);
1427
+ }
1428
+ function deserializeKey(key) {
1429
+ if (key[0] === "\0")
1430
+ return decode(key.slice(1));
1431
+ return encode$4(key);
1432
+ }
1433
+ function pack(children, parentVersion) {
1434
+ if (!Array.isArray(children))
1435
+ return children;
1436
+ const array = children.map(
1437
+ (node) => props.reduce(
1438
+ (array2, prop, i) => {
1439
+ if (!(prop in node))
1440
+ return array2;
1441
+ let value = node[prop];
1442
+ if (prop === "version" && value === parentVersion)
1443
+ return array2;
1444
+ if (prop === "children")
1445
+ value = pack(value, node.version);
1446
+ if (prop === "end")
1447
+ value = serializeKey(value);
1448
+ if (prop === "path")
1449
+ value = value.map(serializeKey);
1450
+ array2[1] |= 1 << i;
1451
+ array2.push(value);
1452
+ return array2;
1453
+ },
1454
+ [serializeKey(node.key), 0]
1455
+ )
1456
+ );
1457
+ return array;
1314
1458
  }
1315
- function deserialize(str) {
1316
- return JSON.parse(str);
1459
+ function unpack(children, parentVersion) {
1460
+ if (!Array.isArray(children))
1461
+ return children;
1462
+ const node = children.map(
1463
+ ([key, type, ...values]) => props.reduce(
1464
+ (node2, prop, i) => {
1465
+ if (!(type & 1 << i))
1466
+ return node2;
1467
+ let value = values.shift();
1468
+ if (prop === "children")
1469
+ value = unpack(value, node2.version);
1470
+ if (prop === "end")
1471
+ value = deserializeKey(value);
1472
+ if (prop === "path")
1473
+ value = value.map(deserializeKey);
1474
+ node2[prop] = value;
1475
+ return node2;
1476
+ },
1477
+ { key: deserializeKey(key), version: parentVersion }
1478
+ )
1479
+ );
1480
+ return node;
1317
1481
  }
1318
1482
  const ROOT_KEY = Symbol();
1319
1483
  function encode(value, { version, isGraph } = {}) {
1320
1484
  var _a;
1321
1485
  const links = [];
1322
- function pushLink($ref, $ver, props, $val, $chi) {
1486
+ function pushLink($ref, $ver, props2, $val, $chi) {
1323
1487
  const [range, _] = splitRef($ref);
1324
- let children = !isEmpty(props) ? makeNode(
1325
- range ? [{ $key: range, ...props }] : props,
1488
+ let children = !isEmpty(props2) ? makeNode2(
1489
+ range ? [{ $key: range, ...props2 }] : props2,
1326
1490
  void 0,
1327
1491
  $ver
1328
- ).children : isDef($chi) ? makeNode(
1492
+ ).children : isDef($chi) ? makeNode2(
1329
1493
  range ? [{ $key: range, $chi }] : $chi,
1330
1494
  void 0,
1331
1495
  $ver
1332
1496
  ).children : isDef($val) ? $val : isGraph ? void 0 : 1;
1333
1497
  if (children) {
1334
- links.push(wrap(children, encode$1($ref), $ver, !!range)[0]);
1498
+ links.push(wrap(children, encode$2($ref), $ver, !!range)[0]);
1335
1499
  }
1336
1500
  }
1337
1501
  const combine = isGraph ? merge : add;
1338
- function makeNode(object, key, ver) {
1502
+ function makeNode2(object, key, ver, parentPuts = []) {
1339
1503
  var _a2;
1340
1504
  if (!isDef(object))
1341
1505
  return;
1342
1506
  if (typeof object === "object" && object && isEmpty(object))
1343
1507
  return;
1344
- const { $key, $ver, $ref, $val, $chi, $put, ...props } = object || {};
1508
+ const { $key, $ver, $ref, $val, $chi, $put, ...props2 } = object || {};
1509
+ if (typeof object === "object" && object && !Array.isArray(object)) {
1510
+ object = Object.fromEntries(
1511
+ Object.entries({ $key, $ver, $ref, $val, $chi, $put, ...props2 }).filter(
1512
+ ([_, val]) => isDef(val)
1513
+ )
1514
+ );
1515
+ }
1345
1516
  if (isDef($ver))
1346
1517
  ver = $ver;
1347
1518
  if (isPlainObject($key)) {
1348
1519
  const [page, filter] = splitArgs($key);
1349
- if (isGraph && page && !isDef(page.$cursor) && ($ref || $val || $chi || $put || !isEmpty(props))) {
1350
- const node2 = makeNode({ ...object, $key: filter || "" }, key, ver);
1520
+ const foundPuts = parentPuts.filter(([_, putFilter]) => isEqual__default.default(filter, putFilter)).map(([range]) => range);
1521
+ if (isGraph && page && !isDef(page.$cursor) && ($ref || $val || $chi || $put || !isEmpty(props2))) {
1522
+ const node2 = makeNode2(
1523
+ { ...object, $key: filter || {}, $put: foundPuts },
1524
+ key,
1525
+ ver
1526
+ );
1527
+ if (!filter)
1528
+ node2.key = MIN_KEY;
1351
1529
  node2.prefix = true;
1352
1530
  return node2;
1353
1531
  }
1354
1532
  if ((!isDef(key) || Number.isInteger(key)) && page && (filter || isDef(page.$cursor))) {
1355
- const node2 = makeNode(
1533
+ const node2 = makeNode2(
1356
1534
  {
1357
- $key: filter || "",
1535
+ $key: filter || {},
1358
1536
  $chi: [
1359
1537
  { ...object, $key: isDef(page.$cursor) ? page.$cursor : page }
1360
- ]
1538
+ ],
1539
+ ...isGraph ? { $put: foundPuts } : {}
1361
1540
  },
1362
1541
  key,
1363
1542
  ver
1364
1543
  );
1544
+ if (!filter)
1545
+ node2.key = MIN_KEY;
1365
1546
  node2.prefix = true;
1366
1547
  return node2;
1367
1548
  }
1368
1549
  }
1550
+ let putQuery = [], prefixPuts = [];
1551
+ if (Array.isArray(object) && !object.some((it) => isDef(it == null ? void 0 : it.$key))) {
1552
+ putQuery = [encode$3({ $since: 0, $until: Infinity })];
1553
+ }
1554
+ function classifyPut(put) {
1555
+ const [range, filter] = splitArgs(put);
1556
+ if (filter) {
1557
+ prefixPuts.push([range, filter]);
1558
+ } else {
1559
+ putQuery.push(encode$3(put));
1560
+ }
1561
+ }
1562
+ if ($put === true) {
1563
+ putQuery = null;
1564
+ } else if (Array.isArray($put)) {
1565
+ $put.forEach(classifyPut);
1566
+ } else if (isDef($put)) {
1567
+ classifyPut($put);
1568
+ }
1369
1569
  if (isDef($key) && (Number.isInteger(key) || !isDef(key)))
1370
1570
  key = $key;
1371
- const node = key === ROOT_KEY || !isDef(key) ? {} : encode$2(key);
1571
+ const node = key === ROOT_KEY || !isDef(key) ? {} : encode$3(key);
1372
1572
  node.version = ver;
1373
1573
  if (object === null) {
1374
1574
  node.end = node.key;
1375
1575
  } else if (isDef($key) && isDef(key) && key !== $key) {
1376
- node.children = [makeNode(object, void 0, ver)].filter(Boolean);
1576
+ node.children = [makeNode2(object, void 0, ver, prefixPuts)].filter(
1577
+ Boolean
1578
+ );
1377
1579
  return node;
1378
1580
  } else if ($ref) {
1379
- pushLink($ref, node.version, props, $val, $chi);
1581
+ pushLink($ref, node.version, props2, $val, $chi);
1380
1582
  if (!isGraph)
1381
1583
  return;
1382
- node.path = encode$1($ref);
1584
+ node.path = encode$2($ref);
1383
1585
  } else if ($val === true) {
1384
- node.value = props;
1586
+ node.value = props2;
1385
1587
  } else if (isDef($val)) {
1386
1588
  node.value = $val;
1387
1589
  } else if (typeof object !== "object") {
1388
1590
  node.value = isGraph || typeof object === "number" ? object : 1;
1389
1591
  } else if (isDef($chi)) {
1390
- const children = $chi.map((obj) => makeNode(obj, void 0, ver)).filter(Boolean).sort((a, b) => a.key <= b.key ? -1 : 1);
1592
+ const children = $chi.map((obj) => makeNode2(obj, void 0, ver, prefixPuts)).filter(Boolean).sort((a, b) => cmp(a.key, b.key));
1391
1593
  if (children.length) {
1392
1594
  node.children = children;
1393
1595
  }
1394
1596
  } else if (Array.isArray(object)) {
1395
- const children = object.map((obj, i) => makeNode(obj, i, ver)).filter(Boolean).reduce((acc, it) => {
1597
+ const children = object.map((obj, i) => makeNode2(obj, i, ver, prefixPuts)).filter(Boolean).reduce((acc, it) => {
1396
1598
  combine(acc, [it]);
1397
1599
  return acc;
1398
1600
  }, []);
@@ -1400,7 +1602,7 @@ function encode(value, { version, isGraph } = {}) {
1400
1602
  node.children = children;
1401
1603
  }
1402
1604
  } else {
1403
- const children = Object.keys(props).sort().map((key2) => makeNode(object[key2], key2, ver)).filter(Boolean);
1605
+ const children = Object.keys(props2).sort().map((key2) => makeNode2(object[key2], key2, ver)).filter(Boolean);
1404
1606
  if (children.length) {
1405
1607
  node.children = children;
1406
1608
  } else if (isGraph) {
@@ -1414,19 +1616,13 @@ function encode(value, { version, isGraph } = {}) {
1414
1616
  node.value = 1;
1415
1617
  }
1416
1618
  }
1417
- let putQuery;
1418
- if (Array.isArray(object) && !object.some((it) => isDef(it == null ? void 0 : it.$key))) {
1419
- putQuery = [encode$2({ $since: 0, $until: Infinity })];
1420
- }
1421
- if ($put === true) {
1422
- putQuery = null;
1423
- } else if (Array.isArray($put)) {
1424
- putQuery = $put.map((arg) => encode$2(arg));
1425
- } else if (isDef($put)) {
1426
- putQuery = [encode$2($put)];
1427
- }
1428
- if (isGraph && isDef(putQuery)) {
1429
- node.children = finalize(node.children || [], putQuery, node.version);
1619
+ if (isGraph && (putQuery === null || putQuery.length)) {
1620
+ node.children = finalize(
1621
+ node.children || [],
1622
+ putQuery,
1623
+ node.version,
1624
+ true
1625
+ );
1430
1626
  }
1431
1627
  if (((_a2 = node.children) == null ? void 0 : _a2.length) || isDef(node.end) || isDef(node.value) || isDef(node.path)) {
1432
1628
  return node;
@@ -1434,14 +1630,14 @@ function encode(value, { version, isGraph } = {}) {
1434
1630
  }
1435
1631
  if (value == null ? void 0 : value.$key)
1436
1632
  value = [value];
1437
- let result = ((_a = makeNode(value, ROOT_KEY, version)) == null ? void 0 : _a.children) || [];
1633
+ let result = ((_a = makeNode2(value, ROOT_KEY, version)) == null ? void 0 : _a.children) || [];
1438
1634
  while (links.length) {
1439
1635
  combine(result, [links.pop()]);
1440
1636
  }
1441
1637
  return result;
1442
1638
  }
1443
1639
  function encodeGraph(obj, version = Date.now()) {
1444
- return encode(obj, { version, isGraph: true });
1640
+ return setVersion(encode(obj, { version, isGraph: true }), version, true);
1445
1641
  }
1446
1642
  function encodeQuery(obj, version = 0) {
1447
1643
  return encode(obj, { version, isGraph: false });
@@ -1552,22 +1748,23 @@ function unwrapObject(object, path) {
1552
1748
  return object;
1553
1749
  }
1554
1750
  exports.IS_VAL = IS_VAL;
1751
+ exports.MAX_KEY = MAX_KEY;
1752
+ exports.MIN_KEY = MIN_KEY;
1555
1753
  exports.add = add;
1754
+ exports.addStringify = addStringify;
1556
1755
  exports.cloneObject = cloneObject;
1557
- exports.decodeArgs = decode$2;
1756
+ exports.cmp = cmp;
1757
+ exports.decodeArgs = decode$3;
1558
1758
  exports.decodeGraph = decodeGraph;
1559
- exports.decodePath = decode$1;
1759
+ exports.decodePath = decode$2;
1560
1760
  exports.decodeQuery = decodeQuery;
1561
- exports.decodeUrl = decode$7;
1562
- exports.decodeValue = decode$3;
1761
+ exports.decodeValue = decode$4;
1563
1762
  exports.decorate = decorate;
1564
- exports.deserialize = deserialize;
1565
- exports.encodeArgs = encode$2;
1763
+ exports.encodeArgs = encode$3;
1566
1764
  exports.encodeGraph = encodeGraph;
1567
- exports.encodePath = encode$1;
1765
+ exports.encodePath = encode$2;
1568
1766
  exports.encodeQuery = encodeQuery;
1569
- exports.encodeUrl = encode$7;
1570
- exports.encodeValue = encode$3;
1767
+ exports.encodeValue = encode$4;
1571
1768
  exports.err = err;
1572
1769
  exports.errIf = errIf;
1573
1770
  exports.finalize = finalize;
@@ -1579,9 +1776,9 @@ exports.getNodeValue = getNodeValue;
1579
1776
  exports.isBranch = isBranch;
1580
1777
  exports.isDef = isDef;
1581
1778
  exports.isEmpty = isEmpty;
1582
- exports.isEncoded = isEncoded;
1583
- exports.isEncodedKey = isEncodedKey;
1584
1779
  exports.isLink = isLink;
1780
+ exports.isMaxKey = isMaxKey;
1781
+ exports.isMinKey = isMinKey;
1585
1782
  exports.isNewer = isNewer;
1586
1783
  exports.isOlder = isOlder;
1587
1784
  exports.isPlainObject = isPlainObject;
@@ -1595,13 +1792,14 @@ exports.makeWatcher = makeWatcher;
1595
1792
  exports.merge = merge;
1596
1793
  exports.mergeObject = mergeObject;
1597
1794
  exports.mergeStreams = mergeStreams;
1795
+ exports.pack = pack;
1598
1796
  exports.remove = remove;
1599
- exports.serialize = serialize;
1600
1797
  exports.setVersion = setVersion;
1601
1798
  exports.sieve = sieve;
1602
1799
  exports.slice = slice;
1603
1800
  exports.splitArgs = splitArgs;
1604
1801
  exports.splitRef = splitRef;
1802
+ exports.unpack = unpack;
1605
1803
  exports.unwrap = unwrap;
1606
1804
  exports.unwrapObject = unwrapObject;
1607
1805
  exports.wrap = wrap;