@graffy/common 0.15.25 → 0.16.0-alpha.2

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
@@ -6,12 +6,6 @@ const isEqual = require("lodash/isEqual.js");
6
6
  const _interopDefaultLegacy = (e) => e && typeof e === "object" && "default" in e ? e : { default: e };
7
7
  const mergeIterators__default = /* @__PURE__ */ _interopDefaultLegacy(mergeIterators);
8
8
  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
- }
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,92 @@ 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
+ toString: stringifyDescriptor,
123
+ [Symbol.for("nodejs.util.inspect.custom")]: stringifyDescriptor
124
+ });
125
+ return buffer;
126
+ }
127
+ addStringify(MIN_KEY);
128
+ addStringify(MAX_KEY);
82
129
  const END = 0;
83
130
  const NULL = 1;
84
131
  const FALSE = 2;
@@ -87,12 +134,9 @@ const NUM = 4;
87
134
  const STR = 5;
88
135
  const ARR = 6;
89
136
  const OBJ = 7;
137
+ const EOK = 127;
90
138
  function encodeArray(array) {
91
- return [
92
- ARR,
93
- ...array.flatMap((value) => encodeParts(value)),
94
- END
95
- ];
139
+ return [ARR, ...array.flatMap((value) => encodeParts(value)), END];
96
140
  }
97
141
  function encodeObject(object) {
98
142
  const keys = Object.keys(object).sort();
@@ -124,10 +168,21 @@ function encodeParts(value) {
124
168
  return encodeObject(value);
125
169
  return [NULL];
126
170
  }
127
- function encode$3(value) {
171
+ function encode$4(value) {
128
172
  const parts = encodeParts(value);
129
173
  while (parts[parts.length - 1] === END)
130
174
  parts.pop();
175
+ const lastPart = parts[parts.length - 1];
176
+ if (typeof lastPart !== "number") {
177
+ let end = lastPart.length - 1;
178
+ while (end >= 0 && !lastPart[end])
179
+ end--;
180
+ if (lastPart[end] !== 255) {
181
+ parts[parts.length - 1] = lastPart.slice(0, end + 1);
182
+ } else {
183
+ parts.push(EOK);
184
+ }
185
+ }
131
186
  const length = parts.reduce(
132
187
  (sum, part) => sum + (typeof part === "number" ? 1 : part.length),
133
188
  0
@@ -143,12 +198,12 @@ function encode$3(value) {
143
198
  i += part.length;
144
199
  }
145
200
  }
146
- return encode$4(buffer);
201
+ addStringify(buffer);
202
+ return buffer;
147
203
  }
148
204
  const nextKey = /* @__PURE__ */ new WeakMap();
149
- function decode$3(key) {
205
+ function decode$4(buffer) {
150
206
  let i = 0;
151
- const buffer = decode$4(key, 0);
152
207
  const stack = [[]];
153
208
  function readString() {
154
209
  let start = i;
@@ -182,6 +237,8 @@ function decode$3(key) {
182
237
  const type = buffer[i];
183
238
  const start = ++i;
184
239
  switch (type) {
240
+ case EOK:
241
+ return stack[0][0];
185
242
  case END:
186
243
  popToken();
187
244
  break;
@@ -214,87 +271,67 @@ function decode$3(key) {
214
271
  return stack[0][0];
215
272
  }
216
273
  function keyStep(key) {
217
- if (key === "")
274
+ if (isMinKey(key))
218
275
  return { key, step: 1 };
219
- if (key === "\uFFFF")
276
+ if (isMaxKey(key))
220
277
  return { key, step: -1 };
221
278
  const l = key.length - 1;
222
- switch (key.charCodeAt(l)) {
279
+ let newKey;
280
+ let step;
281
+ switch (key[l]) {
223
282
  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
- };
283
+ newKey = key.slice(0, l);
284
+ step = 1;
285
+ break;
286
+ case 255:
287
+ newKey = key.slice(0, l);
288
+ newKey[l - 1]++;
289
+ step = -1;
290
+ break;
230
291
  default:
231
- return { key, step: 0 };
292
+ newKey = key;
293
+ step = 0;
232
294
  }
295
+ return { key: addStringify(newKey), step };
233
296
  }
234
297
  function keyBefore(key) {
235
- if (key === "" || key === "\uFFFF" || key === "\0" || key === "\0\uFFFF") {
298
+ if (isMinKey(key) || isMaxKey(key))
236
299
  return key;
237
- }
238
300
  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";
301
+ let newKey;
302
+ if (key[l] === 0) {
303
+ newKey = key.slice(0, l);
304
+ } else {
305
+ newKey = new Uint8Array(l + 2);
306
+ newKey.set(key, 0);
307
+ newKey[l]--;
308
+ newKey[l + 1] = 255;
309
+ }
310
+ addStringify(newKey);
311
+ return newKey;
240
312
  }
241
313
  function keyAfter(key) {
242
- if (key === "\uFFFF" || key === "\0" || key === "\0\uFFFF") {
314
+ if (isMaxKey(key))
243
315
  return key;
244
- }
245
316
  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 };
317
+ let newKey;
318
+ if (key[l] === 255) {
319
+ newKey = key.slice(0, l);
320
+ newKey[l - 1]++;
295
321
  } else {
296
- return keyStep(string);
322
+ newKey = new Uint8Array(l + 2);
323
+ newKey.set(key, 0);
324
+ newKey[l + 1] = 0;
297
325
  }
326
+ addStringify(newKey);
327
+ return newKey;
328
+ }
329
+ function decodeBound(bound) {
330
+ const { key, step } = keyStep(bound);
331
+ if (isMinKey(key) || isMaxKey(key))
332
+ return { step };
333
+ const value = decode$4(key);
334
+ return { key: value, step };
298
335
  }
299
336
  const pageProps = {
300
337
  $all: 1,
@@ -316,13 +353,13 @@ function splitArgs(arg) {
316
353
  isEmpty(filter) ? void 0 : filter
317
354
  ];
318
355
  }
319
- function encode$2(arg) {
356
+ function encode$3(arg) {
320
357
  if (!isPlainObject(arg))
321
- return { key: maybeEncode(arg) };
358
+ return { key: encode$4(arg) };
322
359
  const [page, filter] = splitArgs(arg);
323
360
  errIf("page_and_filter", page && filter, arg);
324
361
  if (!page)
325
- return { key: maybeEncode(filter || {}) };
362
+ return { key: encode$4(filter || {}) };
326
363
  const { $cursor, ...range } = page;
327
364
  const { $first, $all, $last, $after, $before, $since, $until } = range;
328
365
  const hasRange = !isEmpty(range);
@@ -332,17 +369,17 @@ function encode$2(arg) {
332
369
  errIf("after_and_since", isDef($after) && isDef($since), arg);
333
370
  errIf("before_and_until", isDef($before) && isDef($until), arg);
334
371
  errIf("cursor_and_range_arg", isDef($cursor) && hasRange, arg);
335
- let [key, end] = hasRange ? ["", "\uFFFF"] : [];
372
+ let [key, end] = hasRange ? [MIN_KEY, MAX_KEY] : [];
336
373
  if (isDef($cursor))
337
- key = maybeEncode($cursor);
374
+ key = encode$4($cursor);
338
375
  if (isDef($after))
339
- key = keyAfter(maybeEncode($after));
376
+ key = keyAfter(encode$4($after));
340
377
  if (isDef($before))
341
- end = keyBefore(maybeEncode($before));
378
+ end = keyBefore(encode$4($before));
342
379
  if (isDef($since))
343
- key = maybeEncode($since);
380
+ key = encode$4($since);
344
381
  if (isDef($until))
345
- end = maybeEncode($until);
382
+ end = encode$4($until);
346
383
  if (isDef($last))
347
384
  [key, end] = [end, key];
348
385
  const node = { key };
@@ -352,35 +389,34 @@ function encode$2(arg) {
352
389
  node.limit = $first || $last;
353
390
  return node;
354
391
  }
355
- function decode$2(node) {
392
+ function decode$3(node) {
356
393
  if (typeof node === "string")
357
- return node;
394
+ throw Error("why?");
358
395
  const { key, end, limit } = node;
359
- if (!isEncodedKey(key) && (!isDef(end) || end === key))
360
- return key;
361
396
  errIf("no_key", !isDef(key));
362
397
  errIf("limit_without_end", isDef(limit) && !isDef(end));
363
- const kParts = maybeDecode(key);
364
- if (!isDef(end))
398
+ const kParts = decodeBound(key);
399
+ if (!isDef(end) || cmp(key, end) === 0)
365
400
  return kParts.key;
366
- const eParts = maybeDecode(end);
367
- const [lower, upper] = key < end ? [kParts, eParts] : [eParts, kParts];
401
+ const eParts = decodeBound(end);
402
+ const reverse = cmp(key, end) > 0;
403
+ const [lower, upper] = reverse ? [eParts, kParts] : [kParts, eParts];
368
404
  const args = {};
369
405
  if (limit) {
370
- args[key < end ? "$first" : "$last"] = limit;
371
- } else if (lower.key === "" && upper.key === "\uFFFF") {
406
+ args[reverse ? "$last" : "$first"] = limit;
407
+ } else if (isMinKey(key) && isMaxKey(end) || isMinKey(end) && isMaxKey(key)) {
372
408
  args.$all = true;
373
409
  }
374
- if (lower.key !== "") {
410
+ if (lower.key && !isMinKey(lower.key)) {
375
411
  args[lower.step === 1 ? "$after" : "$since"] = lower.key;
376
412
  }
377
- if (upper.key !== "\uFFFF") {
413
+ if (upper.key && !isMaxKey(upper.key)) {
378
414
  args[upper.step === -1 ? "$before" : "$until"] = upper.key;
379
415
  }
380
416
  return args;
381
417
  }
382
418
  const PATH_SEPARATOR = ".";
383
- function encode$1(path) {
419
+ function encode$2(path) {
384
420
  if (typeof path === "string") {
385
421
  if (!path.length || path === PATH_SEPARATOR)
386
422
  return [];
@@ -390,25 +426,25 @@ function encode$1(path) {
390
426
  throw Error("encodePath.invalid:" + JSON.stringify(path));
391
427
  }
392
428
  function encodeSegment(seg) {
393
- if (typeof seg === "string")
429
+ if (ArrayBuffer.isView(seg))
394
430
  return seg;
395
- const { key, end } = encode$2(seg);
396
- if (end)
397
- throw "encodePath.unexpected_range_key";
398
- return key;
431
+ const node = encode$3(seg);
432
+ if (node.end)
433
+ return node;
434
+ return node.key;
399
435
  }
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);
436
+ if (isPlainObject(path[path.length - 1])) {
437
+ const [page, filter] = splitArgs(path[path.length - 1]);
438
+ if (page)
439
+ path = path.slice(0, -1).concat([filter || MIN_KEY]);
440
+ }
441
+ return path.map(encodeSegment);
406
442
  }
407
- function decode$1(path) {
443
+ function decode$2(path) {
408
444
  if (!Array.isArray(path)) {
409
445
  throw Error("decodePath.invalid:" + JSON.stringify(path));
410
446
  }
411
- return path.map((key) => decode$2({ key }));
447
+ return path.map((key) => decode$3({ key }));
412
448
  }
413
449
  function splitRef($ref) {
414
450
  if (!Array.isArray($ref))
@@ -428,16 +464,17 @@ let customAlphabet = (alphabet, defaultSize = 21) => {
428
464
  return id2;
429
465
  };
430
466
  };
467
+ const alpha = "-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz";
431
468
  const id = customAlphabet(alpha, 20);
432
469
  function findFirst(children, target, first, last) {
433
470
  return find(
434
471
  children,
435
472
  ({ key, end }) => {
436
- if (key === target || end && key < target && end >= target)
473
+ const keyCmp = cmp(key, target);
474
+ const endCmp = end && cmp(end, target);
475
+ if (end && keyCmp < 0 && endCmp >= 0)
437
476
  return 0;
438
- if (key < target)
439
- return -1;
440
- return 1;
477
+ return keyCmp;
441
478
  },
442
479
  first,
443
480
  last
@@ -445,7 +482,7 @@ function findFirst(children, target, first, last) {
445
482
  }
446
483
  function findLast(children, end, first, last) {
447
484
  const ix = findFirst(children, end, first, last);
448
- return children[ix] && children[ix].key <= end ? ix + 1 : ix;
485
+ return children[ix] && cmp(children[ix].key, end) <= 0 ? ix + 1 : ix;
449
486
  }
450
487
  function isRange(node) {
451
488
  return node && typeof node.end !== "undefined";
@@ -459,9 +496,6 @@ function isPrefix(node) {
459
496
  function isLink(node) {
460
497
  return node && typeof node.path !== "undefined";
461
498
  }
462
- function isEncoded(node) {
463
- return node && node.key[0] === "\0";
464
- }
465
499
  function isOlder(node, version) {
466
500
  return typeof node.version !== "undefined" && node.version < version;
467
501
  }
@@ -472,12 +506,12 @@ function add(base, diff) {
472
506
  let changed = false;
473
507
  let index = 0;
474
508
  for (const node of diff) {
475
- const cmp = compare(node);
509
+ const cmp2 = compare(node);
476
510
  const nodeIsBranch = isBranch(node);
477
- index = find(base, cmp, index);
511
+ index = find(base, cmp2, index);
478
512
  const item = base[index];
479
513
  const itemIsBranch = isBranch(item);
480
- if (!item || cmp(item)) {
514
+ if (!item || cmp2(item)) {
481
515
  base.splice(index, 0, clone(node));
482
516
  changed = true;
483
517
  continue;
@@ -502,7 +536,7 @@ function add(base, diff) {
502
536
  }
503
537
  function compare(node) {
504
538
  return (item) => {
505
- const v = compareValue(item.key, node.key) || compareValue(item.end, node.end) || compareValue(item.limit, node.limit);
539
+ 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
540
  return v;
507
541
  };
508
542
  }
@@ -546,9 +580,9 @@ function mergeRanges$1(base, node) {
546
580
  if (node.version < base.version)
547
581
  [node, base] = [base, node];
548
582
  return [
549
- base.key < node.key && { ...base, end: keyBefore(node.key) },
583
+ cmp(base.key, node.key) < 0 && { ...base, end: keyBefore(node.key) },
550
584
  node,
551
- base.end > node.end && { ...base, key: keyAfter(node.end) }
585
+ cmp(base.end, node.end) > 0 && { ...base, key: keyAfter(node.end) }
552
586
  ].filter(Boolean);
553
587
  }
554
588
  function insertNode$1(current, change, start = 0) {
@@ -557,7 +591,7 @@ function insertNode$1(current, change, start = 0) {
557
591
  const key = change.key;
558
592
  const index = findFirst(current, key, start);
559
593
  const node = current[index];
560
- if (node && node.key <= key) {
594
+ if (node && cmp(node.key, key) <= 0) {
561
595
  return isRange(node) ? insertNodeIntoRange$1(current, index, change) : updateNode$1(current, index, change);
562
596
  } else {
563
597
  current.splice(index, 0, change);
@@ -571,9 +605,9 @@ function insertNodeIntoRange$1(current, index, change) {
571
605
  if (!newChange)
572
606
  return;
573
607
  const insertions = [
574
- range.key < key && { ...range, end: keyBefore(key) },
608
+ cmp(range.key, key) < 0 && { ...range, end: keyBefore(key) },
575
609
  newChange,
576
- range.end > key && { ...range, key: keyAfter(key) }
610
+ cmp(range.end, key) > 0 && { ...range, key: keyAfter(key) }
577
611
  ].filter(Boolean);
578
612
  current.splice(index, 1, ...insertions);
579
613
  return index + insertions.length - 1;
@@ -597,7 +631,7 @@ function updateNode$1(current, index, change) {
597
631
  function getNewer(node, base) {
598
632
  const { version } = base;
599
633
  if (isBranch(node)) {
600
- const children = [{ key: "", end: "\uFFFF", version }];
634
+ const children = [{ key: MIN_KEY, end: MAX_KEY, version }];
601
635
  merge(children, node.children);
602
636
  return children.length === 1 ? null : { ...node, children };
603
637
  } else {
@@ -605,8 +639,13 @@ function getNewer(node, base) {
605
639
  }
606
640
  }
607
641
  const IS_VAL = Symbol("IS_VAL");
642
+ function makeNode(seg, props2) {
643
+ if (ArrayBuffer.isView(seg))
644
+ return { key: seg, ...props2 };
645
+ return { ...seg, ...props2 };
646
+ }
608
647
  function wrapValue(value, path, version = 0) {
609
- const node = { ...encode$2(path[path.length - 1]), value, version };
648
+ const node = makeNode(path[path.length - 1], { value, version });
610
649
  return wrap([node], path.slice(0, -1), version);
611
650
  }
612
651
  function wrap(children, path, version = 0, prefix = false) {
@@ -616,16 +655,16 @@ function wrap(children, path, version = 0, prefix = false) {
616
655
  return children;
617
656
  let i = path.length - 1;
618
657
  if (!Array.isArray(children)) {
619
- children = [{ ...encode$2(path[i--]), value: children, version }];
658
+ children = [makeNode(path[i--], { value: children, version })];
620
659
  } else {
621
660
  if (!children.length)
622
661
  return;
623
- children = [{ ...encode$2(path[i--]), version, children }];
662
+ children = [makeNode(path[i--], { children, version })];
624
663
  }
625
664
  if (prefix)
626
665
  children[0].prefix = true;
627
666
  while (i >= 0)
628
- children = [{ ...encode$2(path[i--]), version, children }];
667
+ children = [makeNode(path[i--], { children, version })];
629
668
  return children;
630
669
  }
631
670
  function unwrap(tree, path) {
@@ -634,12 +673,14 @@ function unwrap(tree, path) {
634
673
  let children = tree;
635
674
  let node = { children };
636
675
  for (let i = 0; i < path.length; i++) {
637
- const { key } = encode$2(path[i]);
676
+ const key = path[i];
677
+ if (!ArrayBuffer.isView(key))
678
+ throw Error("unwrap.ranges_unsupported");
638
679
  children = node.children;
639
680
  if (!children)
640
681
  return null;
641
682
  node = children[findFirst(children, key)];
642
- if (!node || node.key > key)
683
+ if (!node || cmp(node.key, key) > 0)
643
684
  return void 0;
644
685
  if (isRange(node))
645
686
  return null;
@@ -666,7 +707,7 @@ function remove(children, path) {
666
707
  const key = path[0];
667
708
  const ix = findFirst(children, key);
668
709
  const node = children[ix];
669
- if (!node || node.key > key || isRange(node))
710
+ if (!node || cmp(node.key, key) > 0 || isRange(node))
670
711
  return children;
671
712
  if (path.length === 1) {
672
713
  return children.slice(0, ix).concat(children.slice(ix + 1));
@@ -721,12 +762,12 @@ function slice(graph, query, root) {
721
762
  function sliceNode(graph, query, result) {
722
763
  const { key, version } = query;
723
764
  const { root } = result;
724
- if (!graph || graph.key > key || isOlder(graph, version)) {
765
+ if (!graph || cmp(graph.key, key) > 0 || isOlder(graph, version)) {
725
766
  result.addUnknown(query);
726
767
  } else if (isRange(graph)) {
727
768
  if (isBranch(query)) {
728
769
  const { known } = slice(
729
- [{ key: "", end: "\uFFFF", version: graph.version }],
770
+ [{ key: MIN_KEY, end: MAX_KEY, version: graph.version }],
730
771
  query.children
731
772
  );
732
773
  result.addKnown({ key, version: graph.version, children: known });
@@ -757,7 +798,7 @@ function sliceNode(graph, query, result) {
757
798
  result.addKnown(graph);
758
799
  } else if (isBranch(query)) {
759
800
  const { known } = slice(
760
- [{ key: "", end: "\uFFFF", version: graph.version }],
801
+ [{ key: MIN_KEY, end: MAX_KEY, version: graph.version }],
761
802
  query.children
762
803
  );
763
804
  result.addKnown({ key, version: graph.version, children: known });
@@ -767,8 +808,8 @@ function sliceNode(graph, query, result) {
767
808
  }
768
809
  function sliceRange(graph, query, result) {
769
810
  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) {
811
+ const step = cmp(key, end) < 0 ? 1 : -1;
812
+ if (isMinKey(graph[0].key) && graph[0].prefix && graph[0].children) {
772
813
  const { known, unknown } = slice(graph[0].children, [query], result.root);
773
814
  if (known)
774
815
  result.addKnown({ ...graph[0], children: known });
@@ -776,10 +817,10 @@ function sliceRange(graph, query, result) {
776
817
  result.addUnknown({ ...query[0], children: unknown });
777
818
  return;
778
819
  }
779
- if (key < end) {
780
- for (let i = findFirst(graph, key); key <= end && limit > 0; i++) {
820
+ if (cmp(key, end) < 0) {
821
+ for (let i = findFirst(graph, key); cmp(key, end) <= 0 && limit > 0; i++) {
781
822
  const node = graph[i];
782
- if (!node || key < node.key || isOlder(node, version))
823
+ if (!node || cmp(key, node.key) < 0 || isOlder(node, version))
783
824
  break;
784
825
  if (isRange(node)) {
785
826
  result.addKnown(getOverlap(node, key, end));
@@ -790,9 +831,9 @@ function sliceRange(graph, query, result) {
790
831
  key = keyAfter(node.end || node.key);
791
832
  }
792
833
  } else {
793
- for (let i = findLast(graph, key) - 1; key >= end && limit > 0; i--) {
834
+ for (let i = findLast(graph, key) - 1; cmp(key, end) >= 0 && limit > 0; i--) {
794
835
  const node = graph[i];
795
- if (!node || key > (node.end || node.key) || isOlder(node, version))
836
+ if (!node || cmp(key, node.end || node.key) > 0 || isOlder(node, version))
796
837
  break;
797
838
  if (isRange(node)) {
798
839
  result.addKnown(getOverlap(node, end, key));
@@ -803,18 +844,18 @@ function sliceRange(graph, query, result) {
803
844
  key = keyBefore(node.key);
804
845
  }
805
846
  }
806
- if (limit && (step < 0 ? key > end : key < end)) {
847
+ if (limit && (step < 0 ? cmp(key, end) > 0 : cmp(key, end) < 0)) {
807
848
  const unknown = { ...query, key, end, limit };
808
849
  result.addUnknown(unknown);
809
850
  }
810
851
  }
811
852
  function getOverlap(node, key, end) {
812
- if (node.key >= key && node.end <= end)
853
+ if (cmp(node.key, key) >= 0 && cmp(node.end, end) <= 0)
813
854
  return node;
814
855
  return {
815
856
  ...node,
816
- key: node.key > key ? node.key : key,
817
- end: node.end < end ? node.end : end
857
+ key: cmp(node.key, key) > 0 ? node.key : key,
858
+ end: cmp(node.end, end) < 0 ? node.end : end
818
859
  };
819
860
  }
820
861
  function sieve(current, changes, result = []) {
@@ -828,7 +869,7 @@ function insertRange(current, change, result, start = 0) {
828
869
  const { key, end } = change;
829
870
  const keyIx = findFirst(current, key, start);
830
871
  const endIx = findLast(current, end, keyIx);
831
- if (keyIx === endIx && !(current[keyIx] && current[keyIx].key <= key && current[keyIx].end >= end)) {
872
+ if (keyIx === endIx && !(current[keyIx] && cmp(current[keyIx].key, key) <= 0 && cmp(current[keyIx].end, end) >= 0)) {
832
873
  return keyIx;
833
874
  }
834
875
  const appliedChange = [];
@@ -836,7 +877,7 @@ function insertRange(current, change, result, start = 0) {
836
877
  for (let i = keyIx; i < endIx; i++) {
837
878
  const node = current[i];
838
879
  if (isRange(node) && node.version >= 0) {
839
- if (node.key > currentKey) {
880
+ if (cmp(node.key, currentKey) > 0) {
840
881
  appliedChange.push({
841
882
  key: currentKey,
842
883
  end: keyBefore(node.key),
@@ -854,11 +895,11 @@ function insertRange(current, change, result, start = 0) {
854
895
  currentKey = keyAfter(node.key);
855
896
  }
856
897
  }
857
- if (currentKey >= change.end) {
898
+ if (cmp(currentKey, change.end) >= 0) {
858
899
  break;
859
900
  }
860
901
  }
861
- if (currentKey <= change.end) {
902
+ if (cmp(currentKey, change.end) <= 0) {
862
903
  appliedChange.push({
863
904
  key: currentKey,
864
905
  end: change.end,
@@ -883,16 +924,16 @@ function mergeRanges(base, node) {
883
924
  if (node.version < base.version)
884
925
  [node, base] = [base, node];
885
926
  return [
886
- base.key < node.key && { ...base, end: keyBefore(node.key) },
927
+ cmp(base.key, node.key) < 0 && { ...base, end: keyBefore(node.key) },
887
928
  node,
888
- base.end > node.end && { ...base, key: keyAfter(node.end) }
929
+ cmp(base.end, node.end) > 0 && { ...base, key: keyAfter(node.end) }
889
930
  ].filter(Boolean);
890
931
  }
891
932
  function insertNode(current, change, result, start = 0) {
892
933
  const key = change.key;
893
934
  const index = findFirst(current, key, start);
894
935
  const node = current[index];
895
- if (node && node.key <= key) {
936
+ if (node && cmp(node.key, key) <= 0) {
896
937
  return isRange(node) ? insertNodeIntoRange(current, index, change, result) : updateNode(current, index, change, result);
897
938
  } else {
898
939
  return index;
@@ -907,9 +948,9 @@ function insertNodeIntoRange(current, index, change, result) {
907
948
  return;
908
949
  result.push(newChange);
909
950
  const insertions = [
910
- range.key < key && { ...range, end: keyBefore(key) },
951
+ cmp(range.key, key) < 0 && { ...range, end: keyBefore(key) },
911
952
  newNode,
912
- range.end > key && { ...range, key: keyAfter(key) }
953
+ cmp(range.end, key) > 0 && { ...range, key: keyAfter(key) }
913
954
  ].filter(Boolean);
914
955
  current.splice(index, 1, ...insertions);
915
956
  return index + insertions.length - 1;
@@ -953,7 +994,7 @@ function isPathEqual(first, second) {
953
994
  }
954
995
  function getNewerNode(node, base) {
955
996
  if (isBranch(node)) {
956
- const emptyNode = { key: "", end: "\uFFFF", version: base.version };
997
+ const emptyNode = { key: MIN_KEY, end: MAX_KEY, version: base.version };
957
998
  const children = [emptyNode];
958
999
  sieve(children, node.children);
959
1000
  return children.length === 1 && children[0] === emptyNode ? null : { ...node, children };
@@ -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
  }
@@ -998,14 +1039,14 @@ function getKnown(graph, version = 0) {
998
1039
  return query;
999
1040
  }
1000
1041
  function finalize(graph, query, version = Date.now()) {
1001
- let result = [{ key: "", end: "\uFFFF", version: 0 }];
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
1046
  result = setVersion(result, version);
1006
1047
  return result;
1007
1048
  }
1008
- function decode(nodes = [], { isGraph } = {}) {
1049
+ function decode$1(nodes = [], { isGraph } = {}) {
1009
1050
  function decodeChildren(nodes2) {
1010
1051
  let result = [];
1011
1052
  let allStrs = true;
@@ -1024,16 +1065,17 @@ function decode(nodes = [], { isGraph } = {}) {
1024
1065
  function addPutRange({ key, end }) {
1025
1066
  if (lastNode) {
1026
1067
  if (lastNode.end) {
1027
- if (key === keyAfter(lastNode.end)) {
1068
+ if (cmp(key, keyAfter(lastNode.end)) === 0) {
1028
1069
  lastNode.end = end || key;
1029
- return end && end !== key;
1070
+ return end && cmp(end, key) !== 0;
1030
1071
  }
1031
1072
  } else {
1032
- if (key === keyAfter(lastNode.key))
1073
+ if (cmp(key, keyAfter(lastNode.key)) === 0) {
1033
1074
  key = lastNode.key;
1075
+ }
1034
1076
  }
1035
1077
  }
1036
- if (end && key !== end) {
1078
+ if (end && cmp(key, end) !== 0) {
1037
1079
  lastNode = { key, end };
1038
1080
  putRanges.push(lastNode);
1039
1081
  return true;
@@ -1055,7 +1097,7 @@ function decode(nodes = [], { isGraph } = {}) {
1055
1097
  else
1056
1098
  pushResult(decodeLeafNode(node));
1057
1099
  }
1058
- if (allNums || allStrs) {
1100
+ if (allStrs || allNums && putRanges.length === 1 && cmp(putRanges[0].key, 0) === 0 && cmp(putRanges[0].end, Infinity) === 0) {
1059
1101
  result = result.reduce(
1060
1102
  (collection, item) => {
1061
1103
  if (Array.isArray(item)) {
@@ -1076,18 +1118,18 @@ function decode(nodes = [], { isGraph } = {}) {
1076
1118
  );
1077
1119
  }
1078
1120
  if (isGraph && putRanges.length) {
1079
- if (putRanges[0].key === "" && putRanges[0].end === "\uFFFF") {
1121
+ if (isMinKey(putRanges[0].key) && isMaxKey(putRanges[0].end)) {
1080
1122
  Object.defineProperty(result, "$put", { value: true });
1081
1123
  } else {
1082
1124
  Object.defineProperty(result, "$put", {
1083
- value: putRanges.map((rNode) => decode$2(rNode))
1125
+ value: putRanges.map((rNode) => decode$3(rNode))
1084
1126
  });
1085
1127
  }
1086
1128
  }
1087
1129
  return result;
1088
1130
  }
1089
1131
  function decodePrefixNode(node) {
1090
- let args = decode$2(node);
1132
+ let args = decode$3(node);
1091
1133
  if (args === "")
1092
1134
  args = {};
1093
1135
  if (typeof args === "string") {
@@ -1095,7 +1137,7 @@ function decode(nodes = [], { isGraph } = {}) {
1095
1137
  }
1096
1138
  if (isLink(node)) {
1097
1139
  args.$all = true;
1098
- const $ref = decode$1(node.path);
1140
+ const $ref = decode$2(node.path);
1099
1141
  const lastKey = $ref[$ref.length - 1];
1100
1142
  if (typeof lastKey === "string") {
1101
1143
  throw Error("decode.unencoded_prefix_ref: " + node.path);
@@ -1122,30 +1164,31 @@ function decode(nodes = [], { isGraph } = {}) {
1122
1164
  }
1123
1165
  function decodeBranchNode(node) {
1124
1166
  const child = decodeChildren(node.children);
1125
- child.$key = decode$2(node);
1167
+ child.$key = decode$3(node);
1126
1168
  return child;
1127
1169
  }
1128
1170
  function decodeLeafNode(node) {
1129
1171
  const child = isGraph ? { $val: node.value } : {};
1130
- child.$key = decode$2(node);
1172
+ child.$key = decode$3(node);
1131
1173
  return child;
1132
1174
  }
1133
1175
  function decodeRangeNode(node) {
1134
- if (node.key === node.end)
1135
- return { $key: decode$2({ key: node.key }) };
1176
+ if (cmp(node.key, node.end) === 0) {
1177
+ return { $key: decode$3({ key: node.key }) };
1178
+ }
1136
1179
  }
1137
1180
  function decodeLinkNode(node) {
1138
- const linkObject = { $key: decode$2(node) };
1139
- Object.defineProperty(linkObject, "$ref", { value: decode$1(node.path) });
1181
+ const linkObject = { $key: decode$3(node) };
1182
+ Object.defineProperty(linkObject, "$ref", { value: decode$2(node.path) });
1140
1183
  return linkObject;
1141
1184
  }
1142
1185
  return decodeChildren(nodes);
1143
1186
  }
1144
1187
  function decodeGraph(graph) {
1145
- return decode(graph, { isGraph: true });
1188
+ return decode$1(graph, { isGraph: true });
1146
1189
  }
1147
1190
  function decodeQuery(query) {
1148
- return decode(query, { isGraph: false });
1191
+ return decode$1(query, { isGraph: false });
1149
1192
  }
1150
1193
  const REF = Symbol();
1151
1194
  const PRE = Symbol();
@@ -1159,25 +1202,27 @@ function decorate(rootGraph, rootQuery) {
1159
1202
  query = [query];
1160
1203
  let graph;
1161
1204
  if (query.$ref) {
1162
- const { $ref, ...props } = query;
1205
+ const { $ref, ...props2 } = query;
1163
1206
  const [range, filter] = splitRef($ref);
1164
- const path = encode$1($ref);
1207
+ const path = encode$2($ref);
1165
1208
  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 });
1209
+ if (targetPlumGraph) {
1210
+ if (range)
1211
+ targetPlumGraph[PRE] = filter;
1212
+ graph = construct(
1213
+ targetPlumGraph,
1214
+ range ? { $key: range, ...props2 } : props2
1215
+ );
1216
+ Object.defineProperty(graph, "$ref", { value: $ref });
1217
+ }
1173
1218
  } else if (Array.isArray(query)) {
1174
1219
  let pageKey;
1175
1220
  graph = query.flatMap((item, i) => {
1176
1221
  if (!(item == null ? void 0 : item.$key)) {
1177
1222
  return construct(descend(plumGraph, i), item);
1178
1223
  }
1179
- const { $key, $chi, ...props } = item;
1180
- const subQuery = $chi || (isEmpty(props) ? 1 : props);
1224
+ const { $key, $chi, ...props2 } = item;
1225
+ const subQuery = $chi || (isEmpty(props2) ? 1 : props2);
1181
1226
  if (!isPlainObject($key) || !splitArgs($key)[0]) {
1182
1227
  return construct(descend(plumGraph, $key), subQuery);
1183
1228
  }
@@ -1189,10 +1234,10 @@ function decorate(rootGraph, rootQuery) {
1189
1234
  pageKey = $key;
1190
1235
  const children = slice2(plumGraph, $key);
1191
1236
  return children.filter((node) => !isRange(node)).map((node) => {
1192
- const $key2 = decode$2(node);
1237
+ const $key2 = decode$3(node);
1193
1238
  const subResult = construct(getValue(node), subQuery);
1194
1239
  if (typeof subResult === "object") {
1195
- subResult.$key = children[PRE] ? { ...children[PRE], $cursor: $key2 } : $key2;
1240
+ subResult.$key = children[PRE] && !isMinKey(children[PRE]) ? { ...children[PRE], $cursor: $key2 } : $key2;
1196
1241
  }
1197
1242
  return subResult;
1198
1243
  });
@@ -1220,13 +1265,13 @@ function decorate(rootGraph, rootQuery) {
1220
1265
  }
1221
1266
  if (plumGraph[REF]) {
1222
1267
  Object.defineProperty(graph, "$ref", {
1223
- value: decode$1(plumGraph[REF])
1268
+ value: decode$2(plumGraph[REF])
1224
1269
  });
1225
1270
  }
1226
1271
  return graph;
1227
1272
  }
1228
1273
  function descend(children, $key) {
1229
- const { key } = encode$2($key);
1274
+ const key = ArrayBuffer.isView($key) ? $key : encode$3($key).key;
1230
1275
  if (!Array.isArray(children))
1231
1276
  return null;
1232
1277
  const ix = findFirst(children, key);
@@ -1235,7 +1280,7 @@ function decorate(rootGraph, rootQuery) {
1235
1280
  return;
1236
1281
  if (isRange(node) && node.end >= key)
1237
1282
  return null;
1238
- if (node.key !== key)
1283
+ if (cmp(node.key, key) !== 0)
1239
1284
  return;
1240
1285
  const result2 = getValue(node);
1241
1286
  if (node.prefix)
@@ -1257,14 +1302,14 @@ function decorate(rootGraph, rootQuery) {
1257
1302
  const [range, filter] = splitArgs($key);
1258
1303
  if (isDef(filter)) {
1259
1304
  children = descend(children, filter);
1260
- } else if (children[0].key === "" && children[0].prefix) {
1261
- children = descend(children, "");
1305
+ } else if (isMinKey(children[0].key) && children[0].prefix) {
1306
+ children = descend(children, MIN_KEY);
1262
1307
  }
1263
- const { key, end, limit = Infinity } = encode$2(range);
1308
+ const { key, end, limit = Infinity } = encode$3(range);
1264
1309
  const ix = findFirst(children, key);
1265
1310
  let i = ix;
1266
1311
  let result2;
1267
- if (key < end) {
1312
+ if (cmp(key, end) < 0) {
1268
1313
  for (let n = 0; i < children.length && n < limit; i++) {
1269
1314
  if (!isRange(children[i]))
1270
1315
  n++;
@@ -1309,52 +1354,152 @@ function addPageMeta(graph, args) {
1309
1354
  let $next = isDef($page.$before) ? { ...filter, $first: count, $since: $page.$before } : isDef($page.$until) ? { ...filter, $first: count, $after: $page.$until } : null;
1310
1355
  Object.assign(graph, { $page, $next, $prev });
1311
1356
  }
1312
- function serialize(obj) {
1313
- return JSON.stringify(obj).replace(/\uffff/g, "\\uffff");
1357
+ function getByte(view, offset) {
1358
+ return offset < view.byteLength ? view.getUint8(offset) : 0;
1359
+ }
1360
+ function getChar(string, offset) {
1361
+ return offset < string.length ? alpha.indexOf(string[offset]) : 0;
1362
+ }
1363
+ function encode$1(u8Arr) {
1364
+ const { buffer, byteOffset, byteLength } = u8Arr;
1365
+ const view = new DataView(buffer, byteOffset, byteLength);
1366
+ let str = "";
1367
+ for (let i = 0; i < view.byteLength; i += 3) {
1368
+ let value = (getByte(view, i) << 16) + (getByte(view, i + 1) << 8) + getByte(view, i + 2);
1369
+ let gstr = "";
1370
+ for (let j = 0; j < 4; j++) {
1371
+ gstr = alpha[value & 63] + gstr;
1372
+ value = value >> 6 | 0;
1373
+ }
1374
+ str += gstr;
1375
+ }
1376
+ return str.substring(0, Math.ceil(view.byteLength * 4 / 3));
1377
+ }
1378
+ function decode(string, start = 0) {
1379
+ const buffer = new ArrayBuffer(Math.floor((string.length - start) * 3 / 4));
1380
+ const view = new DataView(buffer);
1381
+ for (let i = start; i < string.length; i += 4) {
1382
+ let value = (getChar(string, i) << 18) + (getChar(string, i + 1) << 12) + (getChar(string, i + 2) << 6) + getChar(string, i + 3);
1383
+ for (let j = i * 3 / 4 + 2; j >= i * 3 / 4; j--) {
1384
+ if (j < view.byteLength)
1385
+ view.setUint8(j, value & 255);
1386
+ value = value >> 8 | 0;
1387
+ }
1388
+ }
1389
+ return addStringify(new Uint8Array(buffer));
1390
+ }
1391
+ const props = [
1392
+ "end",
1393
+ "version",
1394
+ "limit",
1395
+ "value",
1396
+ "path",
1397
+ "prefix",
1398
+ "children"
1399
+ ];
1400
+ function serializeKey(key) {
1401
+ if (key[0] === STR)
1402
+ return decode$4(key);
1403
+ return "\0" + encode$1(key);
1404
+ }
1405
+ function deserializeKey(key) {
1406
+ if (key[0] === "\0")
1407
+ return decode(key.slice(1));
1408
+ return encode$4(key);
1409
+ }
1410
+ function serializeNodes(children, parentVersion) {
1411
+ const array = children.map(
1412
+ (node) => props.reduce(
1413
+ (array2, prop, i) => {
1414
+ if (!(prop in node))
1415
+ return array2;
1416
+ let value = node[prop];
1417
+ if (prop === "version" && value === parentVersion)
1418
+ return array2;
1419
+ if (prop === "children")
1420
+ value = serializeNodes(value, node.version);
1421
+ if (prop === "end")
1422
+ value = serializeKey(value);
1423
+ if (prop === "path")
1424
+ value = value.map(serializeKey);
1425
+ array2[1] |= 1 << i;
1426
+ array2.push(value);
1427
+ return array2;
1428
+ },
1429
+ [serializeKey(node.key), 0]
1430
+ )
1431
+ );
1432
+ return array;
1433
+ }
1434
+ function deserializeNodes(children, parentVersion) {
1435
+ const node = children.map(
1436
+ ([key, type, ...values]) => props.reduce(
1437
+ (node2, prop, i) => {
1438
+ if (!(type & 1 << i))
1439
+ return node2;
1440
+ let value = values.shift();
1441
+ if (prop === "children")
1442
+ value = deserializeNodes(value, node2.version);
1443
+ if (prop === "end")
1444
+ value = deserializeKey(value);
1445
+ if (prop === "path")
1446
+ value = value.map(deserializeKey);
1447
+ node2[prop] = value;
1448
+ return node2;
1449
+ },
1450
+ { key: deserializeKey(key), version: parentVersion }
1451
+ )
1452
+ );
1453
+ return node;
1454
+ }
1455
+ function serialize(payload) {
1456
+ return JSON.stringify(serializeNodes(payload));
1314
1457
  }
1315
1458
  function deserialize(str) {
1316
- return JSON.parse(str);
1459
+ return deserializeNodes(JSON.parse(str));
1317
1460
  }
1318
1461
  const ROOT_KEY = Symbol();
1319
1462
  function encode(value, { version, isGraph } = {}) {
1320
1463
  var _a;
1321
1464
  const links = [];
1322
- function pushLink($ref, $ver, props, $val, $chi) {
1465
+ function pushLink($ref, $ver, props2, $val, $chi) {
1323
1466
  const [range, _] = splitRef($ref);
1324
- let children = !isEmpty(props) ? makeNode(
1325
- range ? [{ $key: range, ...props }] : props,
1467
+ let children = !isEmpty(props2) ? makeNode2(
1468
+ range ? [{ $key: range, ...props2 }] : props2,
1326
1469
  void 0,
1327
1470
  $ver
1328
- ).children : isDef($chi) ? makeNode(
1471
+ ).children : isDef($chi) ? makeNode2(
1329
1472
  range ? [{ $key: range, $chi }] : $chi,
1330
1473
  void 0,
1331
1474
  $ver
1332
1475
  ).children : isDef($val) ? $val : isGraph ? void 0 : 1;
1333
1476
  if (children) {
1334
- links.push(wrap(children, encode$1($ref), $ver, !!range)[0]);
1477
+ links.push(wrap(children, encode$2($ref), $ver, !!range)[0]);
1335
1478
  }
1336
1479
  }
1337
1480
  const combine = isGraph ? merge : add;
1338
- function makeNode(object, key, ver) {
1481
+ function makeNode2(object, key, ver) {
1339
1482
  var _a2;
1340
1483
  if (!isDef(object))
1341
1484
  return;
1342
1485
  if (typeof object === "object" && object && isEmpty(object))
1343
1486
  return;
1344
- const { $key, $ver, $ref, $val, $chi, $put, ...props } = object || {};
1487
+ const { $key, $ver, $ref, $val, $chi, $put, ...props2 } = object || {};
1345
1488
  if (isDef($ver))
1346
1489
  ver = $ver;
1347
1490
  if (isPlainObject($key)) {
1348
1491
  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);
1492
+ if (isGraph && page && !isDef(page.$cursor) && ($ref || $val || $chi || $put || !isEmpty(props2))) {
1493
+ const node2 = makeNode2({ ...object, $key: filter || {} }, key, ver);
1494
+ if (!filter)
1495
+ node2.key = MIN_KEY;
1351
1496
  node2.prefix = true;
1352
1497
  return node2;
1353
1498
  }
1354
1499
  if ((!isDef(key) || Number.isInteger(key)) && page && (filter || isDef(page.$cursor))) {
1355
- const node2 = makeNode(
1500
+ const node2 = makeNode2(
1356
1501
  {
1357
- $key: filter || "",
1502
+ $key: filter || {},
1358
1503
  $chi: [
1359
1504
  { ...object, $key: isDef(page.$cursor) ? page.$cursor : page }
1360
1505
  ]
@@ -1362,37 +1507,39 @@ function encode(value, { version, isGraph } = {}) {
1362
1507
  key,
1363
1508
  ver
1364
1509
  );
1510
+ if (!filter)
1511
+ node2.key = MIN_KEY;
1365
1512
  node2.prefix = true;
1366
1513
  return node2;
1367
1514
  }
1368
1515
  }
1369
1516
  if (isDef($key) && (Number.isInteger(key) || !isDef(key)))
1370
1517
  key = $key;
1371
- const node = key === ROOT_KEY || !isDef(key) ? {} : encode$2(key);
1518
+ const node = key === ROOT_KEY || !isDef(key) ? {} : encode$3(key);
1372
1519
  node.version = ver;
1373
1520
  if (object === null) {
1374
1521
  node.end = node.key;
1375
1522
  } else if (isDef($key) && isDef(key) && key !== $key) {
1376
- node.children = [makeNode(object, void 0, ver)].filter(Boolean);
1523
+ node.children = [makeNode2(object, void 0, ver)].filter(Boolean);
1377
1524
  return node;
1378
1525
  } else if ($ref) {
1379
- pushLink($ref, node.version, props, $val, $chi);
1526
+ pushLink($ref, node.version, props2, $val, $chi);
1380
1527
  if (!isGraph)
1381
1528
  return;
1382
- node.path = encode$1($ref);
1529
+ node.path = encode$2($ref);
1383
1530
  } else if ($val === true) {
1384
- node.value = props;
1531
+ node.value = props2;
1385
1532
  } else if (isDef($val)) {
1386
1533
  node.value = $val;
1387
1534
  } else if (typeof object !== "object") {
1388
1535
  node.value = isGraph || typeof object === "number" ? object : 1;
1389
1536
  } 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);
1537
+ const children = $chi.map((obj) => makeNode2(obj, void 0, ver)).filter(Boolean).sort((a, b) => cmp(a.key, b.key));
1391
1538
  if (children.length) {
1392
1539
  node.children = children;
1393
1540
  }
1394
1541
  } else if (Array.isArray(object)) {
1395
- const children = object.map((obj, i) => makeNode(obj, i, ver)).filter(Boolean).reduce((acc, it) => {
1542
+ const children = object.map((obj, i) => makeNode2(obj, i, ver)).filter(Boolean).reduce((acc, it) => {
1396
1543
  combine(acc, [it]);
1397
1544
  return acc;
1398
1545
  }, []);
@@ -1400,7 +1547,7 @@ function encode(value, { version, isGraph } = {}) {
1400
1547
  node.children = children;
1401
1548
  }
1402
1549
  } else {
1403
- const children = Object.keys(props).sort().map((key2) => makeNode(object[key2], key2, ver)).filter(Boolean);
1550
+ const children = Object.keys(props2).sort().map((key2) => makeNode2(object[key2], key2, ver)).filter(Boolean);
1404
1551
  if (children.length) {
1405
1552
  node.children = children;
1406
1553
  } else if (isGraph) {
@@ -1416,14 +1563,14 @@ function encode(value, { version, isGraph } = {}) {
1416
1563
  }
1417
1564
  let putQuery;
1418
1565
  if (Array.isArray(object) && !object.some((it) => isDef(it == null ? void 0 : it.$key))) {
1419
- putQuery = [encode$2({ $since: 0, $until: Infinity })];
1566
+ putQuery = [encode$3({ $since: 0, $until: Infinity })];
1420
1567
  }
1421
1568
  if ($put === true) {
1422
1569
  putQuery = null;
1423
1570
  } else if (Array.isArray($put)) {
1424
- putQuery = $put.map((arg) => encode$2(arg));
1571
+ putQuery = $put.map((arg) => encode$3(arg));
1425
1572
  } else if (isDef($put)) {
1426
- putQuery = [encode$2($put)];
1573
+ putQuery = [encode$3($put)];
1427
1574
  }
1428
1575
  if (isGraph && isDef(putQuery)) {
1429
1576
  node.children = finalize(node.children || [], putQuery, node.version);
@@ -1434,7 +1581,7 @@ function encode(value, { version, isGraph } = {}) {
1434
1581
  }
1435
1582
  if (value == null ? void 0 : value.$key)
1436
1583
  value = [value];
1437
- let result = ((_a = makeNode(value, ROOT_KEY, version)) == null ? void 0 : _a.children) || [];
1584
+ let result = ((_a = makeNode2(value, ROOT_KEY, version)) == null ? void 0 : _a.children) || [];
1438
1585
  while (links.length) {
1439
1586
  combine(result, [links.pop()]);
1440
1587
  }
@@ -1552,22 +1699,24 @@ function unwrapObject(object, path) {
1552
1699
  return object;
1553
1700
  }
1554
1701
  exports.IS_VAL = IS_VAL;
1702
+ exports.MAX_KEY = MAX_KEY;
1703
+ exports.MIN_KEY = MIN_KEY;
1555
1704
  exports.add = add;
1705
+ exports.addStringify = addStringify;
1556
1706
  exports.cloneObject = cloneObject;
1557
- exports.decodeArgs = decode$2;
1707
+ exports.cmp = cmp;
1708
+ exports.decodeArgs = decode$3;
1558
1709
  exports.decodeGraph = decodeGraph;
1559
- exports.decodePath = decode$1;
1710
+ exports.decodePath = decode$2;
1560
1711
  exports.decodeQuery = decodeQuery;
1561
- exports.decodeUrl = decode$7;
1562
- exports.decodeValue = decode$3;
1712
+ exports.decodeValue = decode$4;
1563
1713
  exports.decorate = decorate;
1564
1714
  exports.deserialize = deserialize;
1565
- exports.encodeArgs = encode$2;
1715
+ exports.encodeArgs = encode$3;
1566
1716
  exports.encodeGraph = encodeGraph;
1567
- exports.encodePath = encode$1;
1717
+ exports.encodePath = encode$2;
1568
1718
  exports.encodeQuery = encodeQuery;
1569
- exports.encodeUrl = encode$7;
1570
- exports.encodeValue = encode$3;
1719
+ exports.encodeValue = encode$4;
1571
1720
  exports.err = err;
1572
1721
  exports.errIf = errIf;
1573
1722
  exports.finalize = finalize;
@@ -1579,9 +1728,9 @@ exports.getNodeValue = getNodeValue;
1579
1728
  exports.isBranch = isBranch;
1580
1729
  exports.isDef = isDef;
1581
1730
  exports.isEmpty = isEmpty;
1582
- exports.isEncoded = isEncoded;
1583
- exports.isEncodedKey = isEncodedKey;
1584
1731
  exports.isLink = isLink;
1732
+ exports.isMaxKey = isMaxKey;
1733
+ exports.isMinKey = isMinKey;
1585
1734
  exports.isNewer = isNewer;
1586
1735
  exports.isOlder = isOlder;
1587
1736
  exports.isPlainObject = isPlainObject;