@graffy/common 0.17.8-alpha.1 → 0.17.8-alpha.3

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
@@ -2,8 +2,8 @@
2
2
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
3
  const debug = require("debug");
4
4
  const isEqual = require("lodash/isEqual.js");
5
- const mergeIterators = require("merge-async-iterators");
6
5
  const stream = require("@graffy/stream");
6
+ const mergeIterators = require("merge-async-iterators");
7
7
  const isDebugMode = debug("graffy*").enabled;
8
8
  const MIN_KEY = new Uint8Array();
9
9
  const MAX_KEY = new Uint8Array([255]);
@@ -64,11 +64,10 @@ function find(items, compare2, first = 0, last = items.length) {
64
64
  return currentFirst;
65
65
  }
66
66
  function stringify() {
67
- var _a;
68
- if ((this == null ? void 0 : this.length) === 0) return "·";
67
+ if (this?.length === 0) return "·";
69
68
  let str = "";
70
69
  let bull = false;
71
- (_a = this == null ? void 0 : this.forEach) == null ? void 0 : _a.call(this, (value, i) => {
70
+ this?.forEach?.((value, i) => {
72
71
  if (value >= 32 && value <= 126) {
73
72
  str += String.fromCharCode(value);
74
73
  bull = true;
@@ -79,7 +78,7 @@ function stringify() {
79
78
  });
80
79
  return str;
81
80
  }
82
- const inspectSymbol = Symbol.for("nodejs.util.inspect.custom");
81
+ const inspectSymbol = /* @__PURE__ */ Symbol.for("nodejs.util.inspect.custom");
83
82
  function addStringify(buffer) {
84
83
  if (!isDebugMode) return buffer;
85
84
  if ("toJSON" in buffer || inspectSymbol in buffer) return buffer;
@@ -90,6 +89,60 @@ function addStringify(buffer) {
90
89
  }
91
90
  addStringify(MIN_KEY);
92
91
  addStringify(MAX_KEY);
92
+ function keyStep(key) {
93
+ if (isMinKey(key)) return { key, step: 1 };
94
+ if (isMaxKey(key)) return { key, step: -1 };
95
+ const l = key.length - 1;
96
+ let newKey;
97
+ let step;
98
+ switch (key[l]) {
99
+ case 0:
100
+ newKey = key.slice(0, l);
101
+ addStringify(newKey);
102
+ step = 1;
103
+ break;
104
+ case 255:
105
+ newKey = key.slice(0, l);
106
+ addStringify(newKey);
107
+ newKey[l - 1]++;
108
+ step = -1;
109
+ break;
110
+ default:
111
+ newKey = key;
112
+ step = 0;
113
+ }
114
+ return { key: newKey, step };
115
+ }
116
+ function keyBefore(key) {
117
+ if (isMinKey(key) || isMaxKey(key)) return key;
118
+ const l = key.length - 1;
119
+ let newKey;
120
+ if (key[l] === 0) {
121
+ newKey = key.slice(0, l);
122
+ } else {
123
+ newKey = new Uint8Array(l + 2);
124
+ newKey.set(key, 0);
125
+ newKey[l]--;
126
+ newKey[l + 1] = 255;
127
+ }
128
+ addStringify(newKey);
129
+ return newKey;
130
+ }
131
+ function keyAfter(key) {
132
+ if (isMaxKey(key)) return key;
133
+ const l = key.length - 1;
134
+ let newKey;
135
+ if (key[l] === 255) {
136
+ newKey = key.slice(0, l);
137
+ newKey[l - 1]++;
138
+ } else {
139
+ newKey = new Uint8Array(l + 2);
140
+ newKey.set(key, 0);
141
+ newKey[l + 1] = 0;
142
+ }
143
+ addStringify(newKey);
144
+ return newKey;
145
+ }
93
146
  function TwosComplement(view) {
94
147
  const lo = -view.getUint32(4) >>> 0;
95
148
  const carry = lo ? 0 : -1;
@@ -261,60 +314,6 @@ function decode$4(buffer) {
261
314
  }
262
315
  return stack[0][0];
263
316
  }
264
- function keyStep(key) {
265
- if (isMinKey(key)) return { key, step: 1 };
266
- if (isMaxKey(key)) return { key, step: -1 };
267
- const l = key.length - 1;
268
- let newKey;
269
- let step;
270
- switch (key[l]) {
271
- case 0:
272
- newKey = key.slice(0, l);
273
- addStringify(newKey);
274
- step = 1;
275
- break;
276
- case 255:
277
- newKey = key.slice(0, l);
278
- addStringify(newKey);
279
- newKey[l - 1]++;
280
- step = -1;
281
- break;
282
- default:
283
- newKey = key;
284
- step = 0;
285
- }
286
- return { key: newKey, step };
287
- }
288
- function keyBefore(key) {
289
- if (isMinKey(key) || isMaxKey(key)) return key;
290
- const l = key.length - 1;
291
- let newKey;
292
- if (key[l] === 0) {
293
- newKey = key.slice(0, l);
294
- } else {
295
- newKey = new Uint8Array(l + 2);
296
- newKey.set(key, 0);
297
- newKey[l]--;
298
- newKey[l + 1] = 255;
299
- }
300
- addStringify(newKey);
301
- return newKey;
302
- }
303
- function keyAfter(key) {
304
- if (isMaxKey(key)) return key;
305
- const l = key.length - 1;
306
- let newKey;
307
- if (key[l] === 255) {
308
- newKey = key.slice(0, l);
309
- newKey[l - 1]++;
310
- } else {
311
- newKey = new Uint8Array(l + 2);
312
- newKey.set(key, 0);
313
- newKey[l + 1] = 0;
314
- }
315
- addStringify(newKey);
316
- return newKey;
317
- }
318
317
  function decodeBound(bound) {
319
318
  const { key, step } = keyStep(bound);
320
319
  if (isMinKey(key) || isMaxKey(key)) return { step };
@@ -389,51 +388,6 @@ function decode$3(node) {
389
388
  }
390
389
  return args;
391
390
  }
392
- const PATH_SEPARATOR = ".";
393
- function encode$2(path) {
394
- if (typeof path === "string") {
395
- if (!path.length || path === PATH_SEPARATOR) return [];
396
- path = path.split(PATH_SEPARATOR);
397
- }
398
- if (!Array.isArray(path)) {
399
- throw Error(`encodePath.invalid:${JSON.stringify(path)}`);
400
- }
401
- function encodeSegment(seg) {
402
- if (ArrayBuffer.isView(seg)) return seg;
403
- const node = encode$3(seg);
404
- if (node.end) return node;
405
- return node.key;
406
- }
407
- if (isPlainObject(path[path.length - 1])) {
408
- const [page, filter] = splitArgs(path[path.length - 1]);
409
- if (page) path = path.slice(0, -1).concat([filter || MIN_KEY]);
410
- }
411
- return path.map(encodeSegment);
412
- }
413
- function decode$2(path) {
414
- if (!Array.isArray(path)) {
415
- throw Error(`decodePath.invalid:${JSON.stringify(path)}`);
416
- }
417
- return path.map((key) => decode$3({ key }));
418
- }
419
- function splitRef($ref) {
420
- if (!Array.isArray($ref)) return [];
421
- const tail = $ref[$ref.length - 1];
422
- if (!isPlainObject(tail)) return [];
423
- return splitArgs(tail);
424
- }
425
- let customAlphabet = (alphabet, defaultSize = 21) => {
426
- return (size = defaultSize) => {
427
- let id2 = "";
428
- let i = size | 0;
429
- while (i--) {
430
- id2 += alphabet[Math.random() * alphabet.length | 0];
431
- }
432
- return id2;
433
- };
434
- };
435
- const alpha = "-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz";
436
- const id = customAlphabet(alpha, 20);
437
391
  function findFirst(children, target, first, last) {
438
392
  return find(
439
393
  children,
@@ -458,7 +412,7 @@ function isBranch(node) {
458
412
  return node && typeof node.children !== "undefined";
459
413
  }
460
414
  function isPrefix(node) {
461
- return node == null ? void 0 : node.prefix;
415
+ return node?.prefix;
462
416
  }
463
417
  function isLink(node) {
464
418
  return node && typeof node.path !== "undefined";
@@ -516,6 +470,30 @@ function clone(node) {
516
470
  if (node.children) copy.children = node.children.map((child) => clone(child));
517
471
  return copy;
518
472
  }
473
+ function finalize(graph, query, version = Date.now()) {
474
+ let result = [{ key: MIN_KEY, end: MAX_KEY, version: 0 }];
475
+ merge(result, graph);
476
+ if (query) result = slice(result, query).known || [];
477
+ if (version !== false) result = setVersion(result, version, true);
478
+ return result;
479
+ }
480
+ function getKnown(graph, version = 0) {
481
+ const query = [];
482
+ for (const { key, end, children } of graph) {
483
+ const node = { key, version };
484
+ if (end) {
485
+ if (cmp(end, key) !== 0) node.end = end;
486
+ node.value = 1;
487
+ }
488
+ if (children) {
489
+ node.children = getKnown(children);
490
+ } else {
491
+ node.value = 1;
492
+ }
493
+ query.push(node);
494
+ }
495
+ return query;
496
+ }
519
497
  function merge(current, changes) {
520
498
  let index = 0;
521
499
  if (typeof changes === "undefined") return current;
@@ -595,7 +573,7 @@ function getNewer(node, base) {
595
573
  }
596
574
  return node.version >= version ? node : null;
597
575
  }
598
- const IS_VAL = Symbol("IS_VAL");
576
+ const IS_VAL = /* @__PURE__ */ Symbol("IS_VAL");
599
577
  function makeNode(seg, props2) {
600
578
  if (ArrayBuffer.isView(seg)) return { key: seg, ...props2 };
601
579
  return { ...seg, ...props2 };
@@ -658,164 +636,40 @@ function remove(children, path) {
658
636
  const filteredNode = filteredChildren.length ? { ...node, children: filteredChildren } : [];
659
637
  return children.slice(0, ix).concat(filteredNode, children.slice(ix + 1));
660
638
  }
661
- class Result {
662
- constructor(root) {
663
- this.root = root || this;
664
- }
665
- addKnown(node) {
666
- this.known = this.known || [];
667
- merge(this.known, [node]);
668
- }
669
- addUnknown(node) {
670
- this.unknown = this.unknown || [];
671
- this.unknown.push(node);
672
- }
673
- addLinked(children) {
674
- if (this.root !== this) return this.root.addLinked(children);
675
- this.linked = this.linked || [];
676
- add(this.linked, children);
639
+ function setVersion(graph, version, onlyIfZero = false) {
640
+ for (const node of graph) {
641
+ if (!(onlyIfZero && node.version)) node.version = version;
642
+ if (node.children) setVersion(node.children, version, onlyIfZero);
677
643
  }
644
+ return graph;
678
645
  }
679
- function slice(graph, query, root) {
680
- const result = new Result(root);
681
- let currentQuery = query;
682
- while (currentQuery) {
683
- let index = 0;
684
- for (const queryNode of currentQuery) {
685
- if (isRange(queryNode)) {
686
- sliceRange(graph, queryNode, result);
687
- } else {
688
- const key = queryNode.key;
689
- index = findFirst(graph, key);
690
- sliceNode(graph[index], queryNode, result);
691
- }
692
- }
693
- currentQuery = root ? void 0 : result.linked;
694
- delete result.linked;
646
+ function sieve(current, changes, result = []) {
647
+ let index = 0;
648
+ for (const change of changes) {
649
+ index = isRange(change) ? insertRange(current, change, result, index) : insertNode(current, change, result, index);
695
650
  }
696
- delete result.root;
697
651
  return result;
698
652
  }
699
- function sliceNode(graph, query, result) {
700
- const { key, version } = query;
701
- const { root } = result;
702
- if (!graph || cmp(graph.key, key) > 0 || isOlder(graph, version)) {
703
- result.addUnknown(query);
704
- } else if (isRange(graph)) {
705
- if (isBranch(query)) {
706
- const { known } = slice(
707
- [{ key: MIN_KEY, end: MAX_KEY, version: graph.version }],
708
- query.children
709
- );
710
- result.addKnown({ key, version: graph.version, children: known });
711
- } else {
712
- result.addKnown({ key, end: key, version: graph.version });
713
- }
714
- } else if (isBranch(graph) && isBranch(query)) {
715
- const { known, unknown } = slice(graph.children, query.children, root);
716
- if (known) result.addKnown({ ...graph, children: known });
717
- if (unknown) result.addUnknown({ ...query, children: unknown });
718
- } else if (isLink(graph)) {
719
- result.addKnown(graph);
720
- if (graph.prefix && isRange(query)) {
721
- result.addLinked(wrap([query], graph.path, version, true));
722
- } else {
723
- result.addLinked(
724
- wrap(
725
- query.children || query.value,
726
- graph.path,
727
- version,
728
- graph.prefix || query.prefix
729
- )
730
- );
731
- }
732
- } else if (isBranch(graph)) {
733
- result.addKnown(graph);
734
- } else if (isBranch(query)) {
735
- const { known } = slice(
736
- [{ key: MIN_KEY, end: MAX_KEY, version: graph.version }],
737
- query.children
738
- );
739
- result.addKnown({ key, version: graph.version, children: known });
740
- } else {
741
- result.addKnown(graph);
742
- }
743
- }
744
- function sliceRange(graph, query, result) {
745
- let { key, end, limit = Number.POSITIVE_INFINITY, version } = query;
746
- const step = cmp(key, end) < 0 ? 1 : -1;
747
- if (isMinKey(graph[0].key) && graph[0].prefix && graph[0].children) {
748
- const { known, unknown } = slice(graph[0].children, [query], result.root);
749
- if (known) result.addKnown({ ...graph[0], children: known });
750
- if (unknown) result.addUnknown({ ...query[0], children: unknown });
751
- return;
752
- }
753
- if (cmp(key, end) < 0) {
754
- for (let i = findFirst(graph, key); cmp(key, end) <= 0 && limit > 0; i++) {
755
- const node = graph[i];
756
- if (!node || cmp(key, node.key) < 0 || isOlder(node, version)) break;
757
- if (isRange(node)) {
758
- result.addKnown(getOverlap(node, key, end));
759
- } else {
760
- sliceNode(node, { ...query, key }, result);
761
- limit--;
762
- }
763
- key = keyAfter(node.end || node.key);
764
- }
765
- } else {
766
- for (let i = findLast(graph, key) - 1; cmp(key, end) >= 0 && limit > 0; i--) {
767
- const node = graph[i];
768
- if (!node || cmp(key, node.end || node.key) > 0 || isOlder(node, version))
769
- break;
770
- if (isRange(node)) {
771
- result.addKnown(getOverlap(node, end, key));
772
- } else {
773
- sliceNode(node, { ...query, key }, result);
774
- limit--;
775
- }
776
- key = keyBefore(node.key);
777
- }
778
- }
779
- if (limit && (step < 0 ? cmp(key, end) > 0 : cmp(key, end) < 0)) {
780
- const unknown = { ...query, key, end, limit };
781
- result.addUnknown(unknown);
782
- }
783
- }
784
- function getOverlap(node, key, end) {
785
- if (cmp(node.key, key) >= 0 && cmp(node.end, end) <= 0) return node;
786
- return {
787
- ...node,
788
- key: cmp(node.key, key) > 0 ? node.key : key,
789
- end: cmp(node.end, end) < 0 ? node.end : end
790
- };
791
- }
792
- function sieve(current, changes, result = []) {
793
- let index = 0;
794
- for (const change of changes) {
795
- index = isRange(change) ? insertRange(current, change, result, index) : insertNode(current, change, result, index);
796
- }
797
- return result;
798
- }
799
- function insertRange(current, change, result, start = 0) {
800
- const { key, end } = change;
801
- const keyIx = findFirst(current, key, start);
802
- const endIx = findLast(current, end, keyIx);
803
- if (keyIx === endIx && !(current[keyIx] && cmp(current[keyIx].key, end) <= 0 && cmp(current[keyIx].end || current[keyIx].key, key) >= 0)) {
804
- return keyIx;
805
- }
806
- const appliedChange = [];
807
- let currentKey = change.key;
808
- for (let i = keyIx; i < endIx; i++) {
809
- const node = current[i];
810
- if (isRange(node) && node.version >= 0) {
811
- if (cmp(node.key, currentKey) > 0) {
812
- appliedChange.push({
813
- key: currentKey,
814
- end: keyBefore(node.key),
815
- version: change.version
816
- });
817
- }
818
- currentKey = keyAfter(node.end);
653
+ function insertRange(current, change, result, start = 0) {
654
+ const { key, end } = change;
655
+ const keyIx = findFirst(current, key, start);
656
+ const endIx = findLast(current, end, keyIx);
657
+ if (keyIx === endIx && !(current[keyIx] && cmp(current[keyIx].key, end) <= 0 && cmp(current[keyIx].end || current[keyIx].key, key) >= 0)) {
658
+ return keyIx;
659
+ }
660
+ const appliedChange = [];
661
+ let currentKey = change.key;
662
+ for (let i = keyIx; i < endIx; i++) {
663
+ const node = current[i];
664
+ if (isRange(node) && node.version >= 0) {
665
+ if (cmp(node.key, currentKey) > 0) {
666
+ appliedChange.push({
667
+ key: currentKey,
668
+ end: keyBefore(node.key),
669
+ version: change.version
670
+ });
671
+ }
672
+ currentKey = keyAfter(node.end);
819
673
  } else {
820
674
  if (getNewerChange(node, change)) {
821
675
  appliedChange.push({
@@ -930,38 +784,171 @@ function getNewerChange(node, base) {
930
784
  }
931
785
  return node.version >= base.version ? node : null;
932
786
  }
933
- function setVersion(graph, version, onlyIfZero = false) {
934
- for (const node of graph) {
935
- if (!(onlyIfZero && node.version)) node.version = version;
936
- if (node.children) setVersion(node.children, version, onlyIfZero);
787
+ class Result {
788
+ constructor(root) {
789
+ this.root = root || this;
790
+ }
791
+ addKnown(node) {
792
+ this.known = this.known || [];
793
+ merge(this.known, [node]);
794
+ }
795
+ addUnknown(node) {
796
+ this.unknown = this.unknown || [];
797
+ this.unknown.push(node);
798
+ }
799
+ addLinked(children) {
800
+ if (this.root !== this) return this.root.addLinked(children);
801
+ this.linked = this.linked || [];
802
+ add(this.linked, children);
937
803
  }
938
- return graph;
939
804
  }
940
- function getKnown(graph, version = 0) {
941
- const query = [];
942
- for (const { key, end, children } of graph) {
943
- const node = { key, version };
944
- if (end) {
945
- if (cmp(end, key) !== 0) node.end = end;
946
- node.value = 1;
805
+ function slice(graph, query, root) {
806
+ const result = new Result(root);
807
+ let currentQuery = query;
808
+ while (currentQuery) {
809
+ let index = 0;
810
+ for (const queryNode of currentQuery) {
811
+ if (isRange(queryNode)) {
812
+ sliceRange(graph, queryNode, result);
813
+ } else {
814
+ const key = queryNode.key;
815
+ index = findFirst(graph, key);
816
+ sliceNode(graph[index], queryNode, result);
817
+ }
947
818
  }
948
- if (children) {
949
- node.children = getKnown(children);
819
+ currentQuery = root ? void 0 : result.linked;
820
+ delete result.linked;
821
+ }
822
+ delete result.root;
823
+ return result;
824
+ }
825
+ function sliceNode(graph, query, result) {
826
+ const { key, version } = query;
827
+ const { root } = result;
828
+ if (!graph || cmp(graph.key, key) > 0 || isOlder(graph, version)) {
829
+ result.addUnknown(query);
830
+ } else if (isRange(graph)) {
831
+ if (isBranch(query)) {
832
+ const { known } = slice(
833
+ [{ key: MIN_KEY, end: MAX_KEY, version: graph.version }],
834
+ query.children
835
+ );
836
+ result.addKnown({ key, version: graph.version, children: known });
950
837
  } else {
951
- node.value = 1;
838
+ result.addKnown({ key, end: key, version: graph.version });
952
839
  }
953
- query.push(node);
840
+ } else if (isBranch(graph) && isBranch(query)) {
841
+ const { known, unknown } = slice(graph.children, query.children, root);
842
+ if (known) result.addKnown({ ...graph, children: known });
843
+ if (unknown) result.addUnknown({ ...query, children: unknown });
844
+ } else if (isLink(graph)) {
845
+ result.addKnown(graph);
846
+ if (graph.prefix && isRange(query)) {
847
+ result.addLinked(wrap([query], graph.path, version, true));
848
+ } else {
849
+ result.addLinked(
850
+ wrap(
851
+ query.children || query.value,
852
+ graph.path,
853
+ version,
854
+ graph.prefix || query.prefix
855
+ )
856
+ );
857
+ }
858
+ } else if (isBranch(graph)) {
859
+ result.addKnown(graph);
860
+ } else if (isBranch(query)) {
861
+ const { known } = slice(
862
+ [{ key: MIN_KEY, end: MAX_KEY, version: graph.version }],
863
+ query.children
864
+ );
865
+ result.addKnown({ key, version: graph.version, children: known });
866
+ } else {
867
+ result.addKnown(graph);
868
+ }
869
+ }
870
+ function sliceRange(graph, query, result) {
871
+ let { key, end, limit = Number.POSITIVE_INFINITY, version } = query;
872
+ const step = cmp(key, end) < 0 ? 1 : -1;
873
+ if (isMinKey(graph[0].key) && graph[0].prefix && graph[0].children) {
874
+ const { known, unknown } = slice(graph[0].children, [query], result.root);
875
+ if (known) result.addKnown({ ...graph[0], children: known });
876
+ if (unknown) result.addUnknown({ ...query[0], children: unknown });
877
+ return;
878
+ }
879
+ if (cmp(key, end) < 0) {
880
+ for (let i = findFirst(graph, key); cmp(key, end) <= 0 && limit > 0; i++) {
881
+ const node = graph[i];
882
+ if (!node || cmp(key, node.key) < 0 || isOlder(node, version)) break;
883
+ if (isRange(node)) {
884
+ result.addKnown(getOverlap(node, key, end));
885
+ } else {
886
+ sliceNode(node, { ...query, key }, result);
887
+ limit--;
888
+ }
889
+ key = keyAfter(node.end || node.key);
890
+ }
891
+ } else {
892
+ for (let i = findLast(graph, key) - 1; cmp(key, end) >= 0 && limit > 0; i--) {
893
+ const node = graph[i];
894
+ if (!node || cmp(key, node.end || node.key) > 0 || isOlder(node, version))
895
+ break;
896
+ if (isRange(node)) {
897
+ result.addKnown(getOverlap(node, end, key));
898
+ } else {
899
+ sliceNode(node, { ...query, key }, result);
900
+ limit--;
901
+ }
902
+ key = keyBefore(node.key);
903
+ }
904
+ }
905
+ if (limit && (step < 0 ? cmp(key, end) > 0 : cmp(key, end) < 0)) {
906
+ const unknown = { ...query, key, end, limit };
907
+ result.addUnknown(unknown);
908
+ }
909
+ }
910
+ function getOverlap(node, key, end) {
911
+ if (cmp(node.key, key) >= 0 && cmp(node.end, end) <= 0) return node;
912
+ return {
913
+ ...node,
914
+ key: cmp(node.key, key) > 0 ? node.key : key,
915
+ end: cmp(node.end, end) < 0 ? node.end : end
916
+ };
917
+ }
918
+ const PATH_SEPARATOR = ".";
919
+ function encode$2(path) {
920
+ if (typeof path === "string") {
921
+ if (!path.length || path === PATH_SEPARATOR) return [];
922
+ path = path.split(PATH_SEPARATOR);
923
+ }
924
+ if (!Array.isArray(path)) {
925
+ throw Error(`encodePath.invalid:${JSON.stringify(path)}`);
926
+ }
927
+ function encodeSegment(seg) {
928
+ if (ArrayBuffer.isView(seg)) return seg;
929
+ const node = encode$3(seg);
930
+ if (node.end) return node;
931
+ return node.key;
932
+ }
933
+ if (isPlainObject(path[path.length - 1])) {
934
+ const [page, filter] = splitArgs(path[path.length - 1]);
935
+ if (page) path = path.slice(0, -1).concat([filter || MIN_KEY]);
936
+ }
937
+ return path.map(encodeSegment);
938
+ }
939
+ function decode$2(path) {
940
+ if (!Array.isArray(path)) {
941
+ throw Error(`decodePath.invalid:${JSON.stringify(path)}`);
954
942
  }
955
- return query;
943
+ return path.map((key) => decode$3({ key }));
956
944
  }
957
- function finalize(graph, query, version = Date.now()) {
958
- let result = [{ key: MIN_KEY, end: MAX_KEY, version: 0 }];
959
- merge(result, graph);
960
- if (query) result = slice(result, query).known || [];
961
- if (version !== false) result = setVersion(result, version, true);
962
- return result;
945
+ function splitRef($ref) {
946
+ if (!Array.isArray($ref)) return [];
947
+ const tail = $ref[$ref.length - 1];
948
+ if (!isPlainObject(tail)) return [];
949
+ return splitArgs(tail);
963
950
  }
964
- const PRE_CHI_PUT = Symbol("PREFIX_CHILDREN_$PUT");
951
+ const PRE_CHI_PUT = /* @__PURE__ */ Symbol("PREFIX_CHILDREN_$PUT");
965
952
  function decode$1(nodes = [], { isGraph } = {}) {
966
953
  function decodeChildren(nodes2) {
967
954
  let result = [];
@@ -1118,8 +1105,8 @@ function decodeGraph(graph) {
1118
1105
  function decodeQuery(query) {
1119
1106
  return decode$1(query, { isGraph: false });
1120
1107
  }
1121
- const REF = Symbol();
1122
- const PRE = Symbol();
1108
+ const REF = /* @__PURE__ */ Symbol();
1109
+ const PRE = /* @__PURE__ */ Symbol();
1123
1110
  function decorate(rootGraph, rootQuery) {
1124
1111
  function construct(plumGraph, query) {
1125
1112
  if (plumGraph === null) return null;
@@ -1142,7 +1129,7 @@ function decorate(rootGraph, rootQuery) {
1142
1129
  } else if (Array.isArray(query)) {
1143
1130
  let pageKey;
1144
1131
  graph = query.flatMap((item, i) => {
1145
- if (!(item == null ? void 0 : item.$key)) {
1132
+ if (!item?.$key) {
1146
1133
  return construct(descend(plumGraph, i), item);
1147
1134
  }
1148
1135
  const { $key, $chi, ...props2 } = item;
@@ -1261,11 +1248,11 @@ function addPageMeta(graph, args) {
1261
1248
  if (graph.length === count) {
1262
1249
  if ($first) {
1263
1250
  const boundKey = graph[graph.length - 1].$key;
1264
- $page.$until = isDef(boundKey == null ? void 0 : boundKey.$cursor) ? boundKey.$cursor : boundKey;
1251
+ $page.$until = isDef(boundKey?.$cursor) ? boundKey.$cursor : boundKey;
1265
1252
  delete $page.$before;
1266
1253
  } else {
1267
1254
  const boundKey = graph[0].$key;
1268
- $page.$since = isDef(boundKey == null ? void 0 : boundKey.$cursor) ? boundKey.$cursor : boundKey;
1255
+ $page.$since = isDef(boundKey?.$cursor) ? boundKey.$cursor : boundKey;
1269
1256
  delete $page.$after;
1270
1257
  }
1271
1258
  }
@@ -1273,102 +1260,8 @@ function addPageMeta(graph, args) {
1273
1260
  const $next = isDef($page.$before) ? { ...filter, $first: count, $since: $page.$before } : isDef($page.$until) ? { ...filter, $first: count, $after: $page.$until } : null;
1274
1261
  Object.assign(graph, { $page, $next, $prev });
1275
1262
  }
1276
- function getByte(view, offset) {
1277
- return offset < view.byteLength ? view.getUint8(offset) : 0;
1278
- }
1279
- function getChar(string, offset) {
1280
- return offset < string.length ? alpha.indexOf(string[offset]) : 0;
1281
- }
1282
- function encode$1(u8Arr) {
1283
- const { buffer, byteOffset, byteLength } = u8Arr;
1284
- const view = new DataView(buffer, byteOffset, byteLength);
1285
- let str = "";
1286
- for (let i = 0; i < view.byteLength; i += 3) {
1287
- let value = (getByte(view, i) << 16) + (getByte(view, i + 1) << 8) + getByte(view, i + 2);
1288
- let gstr = "";
1289
- for (let j = 0; j < 4; j++) {
1290
- gstr = alpha[value & 63] + gstr;
1291
- value = value >> 6 | 0;
1292
- }
1293
- str += gstr;
1294
- }
1295
- return str.substring(0, Math.ceil(view.byteLength * 4 / 3));
1296
- }
1297
- function decode(string, start = 0) {
1298
- const buffer = new ArrayBuffer(Math.floor((string.length - start) * 3 / 4));
1299
- const view = new DataView(buffer);
1300
- for (let i = start; i < string.length; i += 4) {
1301
- let value = (getChar(string, i) << 18) + (getChar(string, i + 1) << 12) + (getChar(string, i + 2) << 6) + getChar(string, i + 3);
1302
- for (let j = i * 3 / 4 + 2; j >= i * 3 / 4; j--) {
1303
- if (j < view.byteLength) view.setUint8(j, value & 255);
1304
- value = value >> 8 | 0;
1305
- }
1306
- }
1307
- return addStringify(new Uint8Array(buffer));
1308
- }
1309
- const props = [
1310
- "end",
1311
- "version",
1312
- "limit",
1313
- "value",
1314
- "path",
1315
- "prefix",
1316
- "children"
1317
- ];
1318
- function serializeKey(key) {
1319
- if (key[0] === STR) {
1320
- const last = key[key.length - 1];
1321
- if (last !== 0 && last !== 255) {
1322
- return decode$4(key);
1323
- }
1324
- }
1325
- return `\0${encode$1(key)}`;
1326
- }
1327
- function deserializeKey(key) {
1328
- if (key[0] === "\0") return decode(key.slice(1));
1329
- return encode$4(key);
1330
- }
1331
- function pack(children, parentVersion) {
1332
- if (!Array.isArray(children)) return children;
1333
- const array = children.map(
1334
- (node) => props.reduce(
1335
- (array2, prop, i) => {
1336
- if (!(prop in node)) return array2;
1337
- let value = node[prop];
1338
- if (prop === "version" && value === parentVersion) return array2;
1339
- if (prop === "children") value = pack(value, node.version);
1340
- if (prop === "end") value = serializeKey(value);
1341
- if (prop === "path") value = value.map(serializeKey);
1342
- array2[1] |= 1 << i;
1343
- array2.push(value);
1344
- return array2;
1345
- },
1346
- [serializeKey(node.key), 0]
1347
- )
1348
- );
1349
- return array;
1350
- }
1351
- function unpack(children, parentVersion) {
1352
- if (!Array.isArray(children)) return children;
1353
- const node = children.map(
1354
- ([key, type, ...values]) => props.reduce(
1355
- (node2, prop, i) => {
1356
- if (!(type & 1 << i)) return node2;
1357
- let value = values.shift();
1358
- if (prop === "children") value = unpack(value, node2.version);
1359
- if (prop === "end") value = deserializeKey(value);
1360
- if (prop === "path") value = value.map(deserializeKey);
1361
- node2[prop] = value;
1362
- return node2;
1363
- },
1364
- { key: deserializeKey(key), version: parentVersion }
1365
- )
1366
- );
1367
- return node;
1368
- }
1369
- const ROOT_KEY = Symbol();
1370
- function encode(value, { version, isGraph } = {}) {
1371
- var _a;
1263
+ const ROOT_KEY = /* @__PURE__ */ Symbol();
1264
+ function encode$1(value, { version, isGraph } = {}) {
1372
1265
  const links = [];
1373
1266
  function pushLink($ref, $ver, props2, $val, $chi) {
1374
1267
  const [range, _] = splitRef($ref);
@@ -1380,7 +1273,6 @@ function encode(value, { version, isGraph } = {}) {
1380
1273
  }
1381
1274
  const combine = isGraph ? merge : add;
1382
1275
  function makeNode2(object, key, ver, parentPuts = []) {
1383
- var _a2;
1384
1276
  if (!isDef(object)) return;
1385
1277
  const { $key, $ver, $ref, $val, $chi, $put, ...props2 } = object || {};
1386
1278
  if (typeof object === "object" && object && !Array.isArray(object)) {
@@ -1425,7 +1317,7 @@ function encode(value, { version, isGraph } = {}) {
1425
1317
  }
1426
1318
  let putRange = [];
1427
1319
  const prefixPuts = [];
1428
- if (Array.isArray(object) && !isDef($put) && !isDef($val) && !object.some((it) => isDef(it == null ? void 0 : it.$key))) {
1320
+ if (Array.isArray(object) && !isDef($put) && !isDef($val) && !object.some((it) => isDef(it?.$key))) {
1429
1321
  putRange = [encode$3({ $since: 0, $until: Number.POSITIVE_INFINITY })];
1430
1322
  }
1431
1323
  function classifyPut(put) {
@@ -1499,53 +1391,130 @@ function encode(value, { version, isGraph } = {}) {
1499
1391
  }
1500
1392
  if (
1501
1393
  // (key === ROOT_KEY || isDef(node.key)) &&
1502
- ((_a2 = node.children) == null ? void 0 : _a2.length) || isDef(node.end) || isDef(node.value) || isDef(node.path)
1394
+ node.children?.length || isDef(node.end) || isDef(node.value) || isDef(node.path)
1503
1395
  ) {
1504
1396
  return node;
1505
1397
  }
1506
1398
  }
1507
- if (value == null ? void 0 : value.$key) value = [value];
1508
- const result = ((_a = makeNode2(value, ROOT_KEY, version)) == null ? void 0 : _a.children) || [];
1399
+ if (value?.$key) value = [value];
1400
+ const result = makeNode2(value, ROOT_KEY, version)?.children || [];
1509
1401
  while (links.length) {
1510
1402
  combine(result, [links.pop()]);
1511
1403
  }
1512
1404
  return result;
1513
1405
  }
1514
1406
  function encodeGraph(obj, version = Date.now()) {
1515
- const encoded = encode(obj, { version, isGraph: true });
1407
+ const encoded = encode$1(obj, { version, isGraph: true });
1516
1408
  const versioned = setVersion(encoded, version, true);
1517
1409
  return versioned;
1518
1410
  }
1519
1411
  function encodeQuery(obj, version = 0) {
1520
- return encode(obj, { version, isGraph: false });
1412
+ return encode$1(obj, { version, isGraph: false });
1521
1413
  }
1522
- async function* mergeStreams(...streams) {
1523
- const firstValues = (await Promise.all(streams.map((stream2) => stream2.next()))).map((iter) => iter.value);
1524
- if (firstValues.some((value) => typeof value === "undefined")) {
1525
- yield void 0;
1526
- for (const value of firstValues) {
1527
- if (typeof value !== "undefined") yield value;
1414
+ let customAlphabet = (alphabet, defaultSize = 21) => {
1415
+ return (size = defaultSize) => {
1416
+ let id2 = "";
1417
+ let i = size | 0;
1418
+ while (i--) {
1419
+ id2 += alphabet[Math.random() * alphabet.length | 0];
1528
1420
  }
1529
- } else {
1530
- const merged = [];
1531
- for (const value of firstValues) merge(merged, value);
1532
- yield merged;
1421
+ return id2;
1422
+ };
1423
+ };
1424
+ const alpha = "-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz";
1425
+ const id = customAlphabet(alpha, 20);
1426
+ function getByte(view, offset) {
1427
+ return offset < view.byteLength ? view.getUint8(offset) : 0;
1428
+ }
1429
+ function getChar(string, offset) {
1430
+ return offset < string.length ? alpha.indexOf(string[offset]) : 0;
1431
+ }
1432
+ function encode(u8Arr) {
1433
+ const { buffer, byteOffset, byteLength } = u8Arr;
1434
+ const view = new DataView(buffer, byteOffset, byteLength);
1435
+ let str = "";
1436
+ for (let i = 0; i < view.byteLength; i += 3) {
1437
+ let value = (getByte(view, i) << 16) + (getByte(view, i + 1) << 8) + getByte(view, i + 2);
1438
+ let gstr = "";
1439
+ for (let j = 0; j < 4; j++) {
1440
+ gstr = alpha[value & 63] + gstr;
1441
+ value = value >> 6 | 0;
1442
+ }
1443
+ str += gstr;
1533
1444
  }
1534
- yield* mergeIterators(streams);
1445
+ return str.substring(0, Math.ceil(view.byteLength * 4 / 3));
1535
1446
  }
1536
- function makeWatcher() {
1537
- const listeners = /* @__PURE__ */ new Set();
1538
- function write(change) {
1539
- for (const push of listeners) push(change);
1447
+ function decode(string, start = 0) {
1448
+ const buffer = new ArrayBuffer(Math.floor((string.length - start) * 3 / 4));
1449
+ const view = new DataView(buffer);
1450
+ for (let i = start; i < string.length; i += 4) {
1451
+ let value = (getChar(string, i) << 18) + (getChar(string, i + 1) << 12) + (getChar(string, i + 2) << 6) + getChar(string, i + 3);
1452
+ for (let j = i * 3 / 4 + 2; j >= i * 3 / 4; j--) {
1453
+ if (j < view.byteLength) view.setUint8(j, value & 255);
1454
+ value = value >> 8 | 0;
1455
+ }
1540
1456
  }
1541
- function watch(...args) {
1542
- return stream.makeStream((push, _end) => {
1543
- listeners.add(push);
1544
- if (args.length) Promise.resolve(args[0]).then(push);
1545
- return () => listeners.delete(push);
1546
- });
1457
+ return addStringify(new Uint8Array(buffer));
1458
+ }
1459
+ const props = [
1460
+ "end",
1461
+ "version",
1462
+ "limit",
1463
+ "value",
1464
+ "path",
1465
+ "prefix",
1466
+ "children"
1467
+ ];
1468
+ function serializeKey(key) {
1469
+ if (key[0] === STR) {
1470
+ const last = key[key.length - 1];
1471
+ if (last !== 0 && last !== 255) {
1472
+ return decode$4(key);
1473
+ }
1547
1474
  }
1548
- return { write, watch };
1475
+ return `\0${encode(key)}`;
1476
+ }
1477
+ function deserializeKey(key) {
1478
+ if (key[0] === "\0") return decode(key.slice(1));
1479
+ return encode$4(key);
1480
+ }
1481
+ function pack(children, parentVersion) {
1482
+ if (!Array.isArray(children)) return children;
1483
+ const array = children.map(
1484
+ (node) => props.reduce(
1485
+ (array2, prop, i) => {
1486
+ if (!(prop in node)) return array2;
1487
+ let value = node[prop];
1488
+ if (prop === "version" && value === parentVersion) return array2;
1489
+ if (prop === "children") value = pack(value, node.version);
1490
+ if (prop === "end") value = serializeKey(value);
1491
+ if (prop === "path") value = value.map(serializeKey);
1492
+ array2[1] |= 1 << i;
1493
+ array2.push(value);
1494
+ return array2;
1495
+ },
1496
+ [serializeKey(node.key), 0]
1497
+ )
1498
+ );
1499
+ return array;
1500
+ }
1501
+ function unpack(children, parentVersion) {
1502
+ if (!Array.isArray(children)) return children;
1503
+ const node = children.map(
1504
+ ([key, type, ...values]) => props.reduce(
1505
+ (node2, prop, i) => {
1506
+ if (!(type & 1 << i)) return node2;
1507
+ let value = values.shift();
1508
+ if (prop === "children") value = unpack(value, node2.version);
1509
+ if (prop === "end") value = deserializeKey(value);
1510
+ if (prop === "path") value = value.map(deserializeKey);
1511
+ node2[prop] = value;
1512
+ return node2;
1513
+ },
1514
+ { key: deserializeKey(key), version: parentVersion }
1515
+ )
1516
+ );
1517
+ return node;
1549
1518
  }
1550
1519
  function mergeObject(base, change) {
1551
1520
  if (typeof change !== "object" || typeof base !== "object" || !base || !change) {
@@ -1609,12 +1578,40 @@ function unwrapObject(object, path) {
1609
1578
  if (page && !page.$cursor) {
1610
1579
  return object;
1611
1580
  }
1612
- const target = (page == null ? void 0 : page.$cursor) ? { ...filter, $cursor: page.$cursor } : filter;
1581
+ const target = page?.$cursor ? { ...filter, $cursor: page.$cursor } : filter;
1613
1582
  object = object.find(({ $key: $key2 }) => isEqual($key2, target));
1614
1583
  }
1615
1584
  }
1616
1585
  return object;
1617
1586
  }
1587
+ function makeWatcher() {
1588
+ const listeners = /* @__PURE__ */ new Set();
1589
+ function write(change) {
1590
+ for (const push of listeners) push(change);
1591
+ }
1592
+ function watch(...args) {
1593
+ return stream.makeStream((push, _end) => {
1594
+ listeners.add(push);
1595
+ if (args.length) Promise.resolve(args[0]).then(push);
1596
+ return () => listeners.delete(push);
1597
+ });
1598
+ }
1599
+ return { write, watch };
1600
+ }
1601
+ async function* mergeStreams(...streams) {
1602
+ const firstValues = (await Promise.all(streams.map((stream2) => stream2.next()))).map((iter) => iter.value);
1603
+ if (firstValues.some((value) => typeof value === "undefined")) {
1604
+ yield void 0;
1605
+ for (const value of firstValues) {
1606
+ if (typeof value !== "undefined") yield value;
1607
+ }
1608
+ } else {
1609
+ const merged = [];
1610
+ for (const value of firstValues) merge(merged, value);
1611
+ yield merged;
1612
+ }
1613
+ yield* mergeIterators(streams);
1614
+ }
1618
1615
  exports.IS_VAL = IS_VAL;
1619
1616
  exports.MAX_KEY = MAX_KEY;
1620
1617
  exports.MIN_KEY = MIN_KEY;