@graffy/common 0.17.8-alpha.2 → 0.17.8

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