@graffy/common 0.19.1-alpha.1 → 0.19.1

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.
Files changed (85) hide show
  1. package/index.cjs +1673 -0
  2. package/index.mjs +1673 -0
  3. package/package.json +8 -14
  4. package/types/coding/args.d.ts +5 -0
  5. package/types/coding/base64.d.ts +2 -0
  6. package/types/coding/decodeTree.d.ts +2 -0
  7. package/types/coding/encodeTree.d.ts +2 -0
  8. package/{coding/index.js → types/coding/index.d.ts} +4 -4
  9. package/types/coding/number.d.ts +2 -0
  10. package/types/coding/pack.d.ts +2 -0
  11. package/types/coding/path.d.ts +3 -0
  12. package/types/coding/string.d.ts +2 -0
  13. package/types/coding/struct.d.ts +11 -0
  14. package/types/node/find.d.ts +2 -0
  15. package/{node/index.js → types/node/index.d.ts} +0 -1
  16. package/types/node/types.d.ts +6 -0
  17. package/types/object.d.ts +4 -0
  18. package/{ops → types/ops}/finalize.d.ts +1 -1
  19. package/{ops → types/ops}/getKnown.d.ts +4 -1
  20. package/types/ops/merge.d.ts +3 -0
  21. package/types/ops/path.d.ts +6 -0
  22. package/types/ops/sieve.d.ts +3 -0
  23. package/{ops → types/ops}/slice.d.ts +6 -2
  24. package/types/ops/step.d.ts +6 -0
  25. package/types/stream/makeWatcher.d.ts +16 -0
  26. package/types/util.d.ts +15 -0
  27. package/coding/alphabet.js +0 -5
  28. package/coding/args.d.ts +0 -5
  29. package/coding/args.js +0 -93
  30. package/coding/base64.d.ts +0 -2
  31. package/coding/base64.js +0 -41
  32. package/coding/decodeTree.d.ts +0 -2
  33. package/coding/decodeTree.js +0 -197
  34. package/coding/decorate.js +0 -211
  35. package/coding/encodeTree.d.ts +0 -2
  36. package/coding/encodeTree.js +0 -248
  37. package/coding/id.js +0 -4
  38. package/coding/index.d.ts +0 -8
  39. package/coding/number.d.ts +0 -2
  40. package/coding/number.js +0 -39
  41. package/coding/pack.d.ts +0 -2
  42. package/coding/pack.js +0 -71
  43. package/coding/path.d.ts +0 -3
  44. package/coding/path.js +0 -41
  45. package/coding/string.d.ts +0 -2
  46. package/coding/string.js +0 -8
  47. package/coding/struct.d.ts +0 -11
  48. package/coding/struct.js +0 -163
  49. package/index.d.ts +0 -6
  50. package/node/find.d.ts +0 -2
  51. package/node/find.js +0 -14
  52. package/node/index.d.ts +0 -2
  53. package/node/types.d.ts +0 -6
  54. package/node/types.js +0 -18
  55. package/object.d.ts +0 -4
  56. package/object.js +0 -86
  57. package/ops/add.js +0 -64
  58. package/ops/finalize.js +0 -19
  59. package/ops/getKnown.js +0 -24
  60. package/ops/index.d.ts +0 -9
  61. package/ops/merge.d.ts +0 -3
  62. package/ops/merge.js +0 -112
  63. package/ops/path.d.ts +0 -6
  64. package/ops/path.js +0 -91
  65. package/ops/setVersion.js +0 -29
  66. package/ops/sieve.d.ts +0 -3
  67. package/ops/sieve.js +0 -193
  68. package/ops/slice.js +0 -153
  69. package/ops/step.d.ts +0 -6
  70. package/ops/step.js +0 -61
  71. package/stream/index.d.ts +0 -2
  72. package/stream/makeWatcher.d.ts +0 -10
  73. package/stream/makeWatcher.js +0 -19
  74. package/stream/mergeStreams.js +0 -20
  75. package/util.d.ts +0 -15
  76. package/util.js +0 -106
  77. package/{coding → types/coding}/alphabet.d.ts +0 -0
  78. package/{coding → types/coding}/decorate.d.ts +0 -0
  79. package/{coding → types/coding}/id.d.ts +0 -0
  80. package/{index.js → types/index.d.ts} +0 -0
  81. package/{ops → types/ops}/add.d.ts +0 -0
  82. package/{ops/index.js → types/ops/index.d.ts} +1 -1
  83. /package/{ops → types/ops}/setVersion.d.ts +0 -0
  84. /package/{stream/index.js → types/stream/index.d.ts} +0 -0
  85. /package/{stream → types/stream}/mergeStreams.d.ts +0 -0
package/package.json CHANGED
@@ -2,29 +2,23 @@
2
2
  "name": "@graffy/common",
3
3
  "description": "Common libraries that used by various Graffy modules.",
4
4
  "author": "aravind (https://github.com/aravindet)",
5
- "version": "0.19.1-alpha.1",
6
- "main": "./cjs/index.js",
5
+ "version": "0.19.1",
6
+ "main": "./index.cjs",
7
7
  "exports": {
8
- ".": {
9
- "import": "./index.js",
10
- "types": "./index.d.ts"
11
- },
12
- "./*": {
13
- "import": "./*.js",
14
- "types": "./*.d.ts"
15
- }
8
+ "import": "./index.mjs",
9
+ "require": "./index.cjs"
16
10
  },
17
- "types": "./index.d.ts",
11
+ "module": "./index.mjs",
12
+ "types": "./types/index.d.ts",
18
13
  "repository": {
19
14
  "type": "git",
20
15
  "url": "git+https://github.com/usegraffy/graffy.git"
21
16
  },
22
17
  "license": "Apache-2.0",
23
18
  "dependencies": {
24
- "debug": "^4.4.3",
25
19
  "lodash": "^4.17.23",
26
- "nanoid": "^5.1.6",
27
- "@graffy/stream": "0.19.1-alpha.1",
20
+ "debug": "^4.4.3",
21
+ "@graffy/stream": "0.19.1",
28
22
  "merge-async-iterators": "^0.2.1"
29
23
  }
30
24
  }
@@ -0,0 +1,5 @@
1
+ export function splitArgs(arg: any): {}[];
2
+ export function encode(arg: any): {
3
+ key: Uint8Array<any>;
4
+ };
5
+ export function decode(node: any): any;
@@ -0,0 +1,2 @@
1
+ export function encode(u8Arr: any): string;
2
+ export function decode(string: any, start?: number): any;
@@ -0,0 +1,2 @@
1
+ export function decodeGraph(graph: any): any;
2
+ export function decodeQuery(query: any): any;
@@ -0,0 +1,2 @@
1
+ export function encodeGraph(obj: any, version?: number): any;
2
+ export function encodeQuery(obj: any, version?: number): any;
@@ -1,8 +1,8 @@
1
- export { decode as decodeArgs, encode as encodeArgs, splitArgs, } from "./args.js";
2
1
  export * from "./decodeTree.js";
3
- export { default as decorate } from "./decorate.js";
4
2
  export * from "./encodeTree.js";
5
- export { default as makeId } from "./id.js";
6
3
  export * from "./pack.js";
7
- export { decode as decodePath, encode as encodePath, splitRef, } from "./path.js";
4
+ export { default as decorate } from "./decorate.js";
5
+ export { default as makeId } from "./id.js";
6
+ export { decode as decodeArgs, encode as encodeArgs, splitArgs } from "./args.js";
7
+ export { decode as decodePath, encode as encodePath, splitRef } from "./path.js";
8
8
  export { decode as decodeValue, encode as encodeValue } from "./struct.js";
@@ -0,0 +1,2 @@
1
+ export function encode(number: any): Uint8Array<ArrayBuffer>;
2
+ export function decode(u8Arr: any): number;
@@ -0,0 +1,2 @@
1
+ export function pack(children: any, parentVersion: any): any;
2
+ export function unpack(children: any, parentVersion: any): any;
@@ -0,0 +1,3 @@
1
+ export function encode(path: any): any;
2
+ export function decode(path: any): any[];
3
+ export function splitRef($ref: any): {}[];
@@ -0,0 +1,2 @@
1
+ export function encode(string: any): Uint8Array<ArrayBuffer>;
2
+ export function decode(u8Arr: any): string;
@@ -0,0 +1,11 @@
1
+ export function encode(value: any): Uint8Array<any>;
2
+ export function decode(buffer: any): any;
3
+ export const END: 0;
4
+ export const NULL: 1;
5
+ export const FALSE: 2;
6
+ export const TRUE: 3;
7
+ export const NUM: 4;
8
+ export const STR: 5;
9
+ export const ARR: 6;
10
+ export const OBJ: 7;
11
+ export const EOK: 127;
@@ -0,0 +1,2 @@
1
+ export function findFirst(children: any, target: any, first: any, last: any): number;
2
+ export function findLast(children: any, end: any, first: any, last: any): number;
@@ -1,3 +1,2 @@
1
- // export { default as find } from './find.ts';
2
1
  export * from "./find.js";
3
2
  export * from "./types.js";
@@ -0,0 +1,6 @@
1
+ export function isRange(node: any): boolean;
2
+ export function isBranch(node: any): boolean;
3
+ export function isPrefix(node: any): any;
4
+ export function isLink(node: any): boolean;
5
+ export function isOlder(node: any, version: any): boolean;
6
+ export function isNewer(node: any, version: any): boolean;
@@ -0,0 +1,4 @@
1
+ export function mergeObject(base: any, change: any): any;
2
+ export function cloneObject(object: any): any;
3
+ export function wrapObject(object: any, path: any): any;
4
+ export function unwrapObject(object: any, path: any): any;
@@ -5,7 +5,7 @@
5
5
  * @param {number | false} version
6
6
  * @returns any
7
7
  */
8
- export default function finalize(graph: any, query: any, version?: number): {
8
+ export default function finalize(graph: any, query: any, version?: number | false): {
9
9
  key: Uint8Array<ArrayBuffer>;
10
10
  end: Uint8Array<ArrayBuffer>;
11
11
  version: number;
@@ -1 +1,4 @@
1
- export default function getKnown(graph: any, version?: number): any[];
1
+ export default function getKnown(graph: any, version?: number): {
2
+ key: any;
3
+ version: number;
4
+ }[];
@@ -0,0 +1,3 @@
1
+ export default function merge(current: any, changes: any): any;
2
+ export function insertRange(current: any, change: any, start?: number): number;
3
+ export function insertNode(current: any, change: any, start?: number): any;
@@ -0,0 +1,6 @@
1
+ export function wrapValue(value: any, path: any, version?: number): any;
2
+ export function wrap(children: any, path: any, version?: number, prefix?: boolean): any;
3
+ export function unwrap(tree: any, path: any): any;
4
+ export function getNodeValue(node: any): any;
5
+ export function remove(children: any, path: any): any;
6
+ export const IS_VAL: unique symbol;
@@ -0,0 +1,3 @@
1
+ export default function sieve(current: any, changes: any, result?: any[]): any[];
2
+ export function insertRange(current: any, change: any, result: any, start?: number): number;
3
+ export function insertNode(current: any, change: any, result: any, start?: number): any;
@@ -1,9 +1,13 @@
1
+ export default function slice(graph: any, query: any, root: any): Result;
2
+ export function sliceRange(graph: any, query: any, result: any): void;
1
3
  declare class Result {
2
4
  constructor(root: any);
5
+ root: any;
3
6
  addKnown(node: any): void;
7
+ known: any;
4
8
  addUnknown(node: any): void;
9
+ unknown: any;
5
10
  addLinked(children: any): any;
11
+ linked: any;
6
12
  }
7
- export default function slice(graph: any, query: any, root: any): Result;
8
- export declare function sliceRange(graph: any, query: any, result: any): void;
9
13
  export {};
@@ -0,0 +1,6 @@
1
+ export function keyStep(key: any): {
2
+ key: any;
3
+ step: number;
4
+ };
5
+ export function keyBefore(key: any): any;
6
+ export function keyAfter(key: any): any;
@@ -0,0 +1,16 @@
1
+ export default function makeWatcher(): {
2
+ write: (change: any) => void;
3
+ watch: (...args: any[]) => {
4
+ debugId: any;
5
+ next: () => any;
6
+ return(value: any): Promise<{
7
+ value: any;
8
+ done: boolean;
9
+ }>;
10
+ throw(error: any): Promise<{
11
+ value: any;
12
+ done: boolean;
13
+ }>;
14
+ [Symbol.asyncIterator](): /*elided*/ any;
15
+ };
16
+ };
@@ -0,0 +1,15 @@
1
+ export function isMinKey(key: any): boolean;
2
+ export function isMaxKey(key: any): boolean;
3
+ export function err(message: any, { cause, ...args }?: {
4
+ cause?: any;
5
+ }): void;
6
+ export function errIf(message: any, condition: any, args: any): void;
7
+ export function isEmpty(object: any): boolean;
8
+ export function isDef(value: any): boolean;
9
+ export function isPlainObject(arg: any): boolean;
10
+ export function clone(obj: any): any;
11
+ export function cmp(a: any, b: any): 0 | 1 | -1;
12
+ export function find(items: any, compare: any, first?: number, last?: any): number;
13
+ export function addStringify(buffer: any): any;
14
+ export const MIN_KEY: Uint8Array<ArrayBuffer>;
15
+ export const MAX_KEY: Uint8Array<ArrayBuffer>;
@@ -1,5 +0,0 @@
1
- /*
2
- This is just like standard URL-safe Base64, except for using an alphabet
3
- re-ordered for sortability.
4
- */
5
- export default '-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz';
package/coding/args.d.ts DELETED
@@ -1,5 +0,0 @@
1
- export declare function splitArgs(arg: any): {}[];
2
- export declare function encode(arg: any): {
3
- key: Uint8Array<any>;
4
- };
5
- export declare function decode(node: any): any;
package/coding/args.js DELETED
@@ -1,93 +0,0 @@
1
- import { keyAfter, keyBefore, keyStep } from "../ops/step.js";
2
- import { cmp, errIf, isDef, isEmpty, isMaxKey, isMinKey, isPlainObject, MAX_KEY, MIN_KEY, } from "../util.js";
3
- import { decode as decodeValue, encode as encodeValue } from "./struct.js";
4
- function decodeBound(bound) {
5
- const { key, step } = keyStep(bound);
6
- if (isMinKey(key) || isMaxKey(key))
7
- return { step };
8
- const value = decodeValue(key);
9
- return { key: value, step };
10
- }
11
- const pageProps = {
12
- $all: 1,
13
- $first: 1,
14
- $last: 1,
15
- $after: 1,
16
- $before: 1,
17
- $since: 1,
18
- $until: 1,
19
- $cursor: 1,
20
- };
21
- export function splitArgs(arg) {
22
- const page = {};
23
- const filter = {};
24
- for (const p in arg)
25
- (p in pageProps ? page : filter)[p] = arg[p];
26
- return [
27
- isEmpty(page) ? undefined : page,
28
- isEmpty(filter) ? undefined : filter,
29
- ];
30
- }
31
- export function encode(arg) {
32
- if (!isPlainObject(arg))
33
- return { key: encodeValue(arg) };
34
- const [page, filter] = splitArgs(arg);
35
- errIf('page_and_filter', page && filter, arg);
36
- if (!page)
37
- return { key: encodeValue(filter || {}) };
38
- const { $cursor, ...range } = page;
39
- // @ts-expect-error
40
- const { $first, $all, $last, $after, $before, $since, $until } = range;
41
- const hasRange = !isEmpty(range);
42
- errIf('first_and_last', isDef($first) && isDef($last), arg);
43
- errIf('all_and_last', isDef($all) && isDef($last), arg);
44
- errIf('all_and_first', isDef($first) && isDef($all), arg);
45
- errIf('after_and_since', isDef($after) && isDef($since), arg);
46
- errIf('before_and_until', isDef($before) && isDef($until), arg);
47
- errIf('cursor_and_range_arg', isDef($cursor) && hasRange, arg);
48
- let [key, end] = hasRange ? [MIN_KEY, MAX_KEY] : [];
49
- if (isDef($cursor))
50
- key = encodeValue($cursor);
51
- if (isDef($after))
52
- key = keyAfter(encodeValue($after));
53
- if (isDef($before))
54
- end = keyBefore(encodeValue($before));
55
- if (isDef($since))
56
- key = encodeValue($since);
57
- if (isDef($until))
58
- end = encodeValue($until);
59
- if (isDef($last))
60
- [key, end] = [end, key];
61
- const node = { key };
62
- if (isDef(end))
63
- node.end = end;
64
- if ($first || $last)
65
- node.limit = $first || $last;
66
- return node;
67
- }
68
- export function decode(node) {
69
- const { key, end, limit } = node;
70
- errIf('no_key', !isDef(key));
71
- errIf('limit_without_end', isDef(limit) && !isDef(end));
72
- const kParts = decodeBound(key);
73
- if (!isDef(end) || cmp(key, end) === 0)
74
- return kParts.key;
75
- const eParts = decodeBound(end);
76
- const reverse = cmp(key, end) > 0;
77
- const [lower, upper] = reverse ? [eParts, kParts] : [kParts, eParts];
78
- const args = {};
79
- if (limit) {
80
- args[reverse ? '$last' : '$first'] = limit;
81
- }
82
- else if ((isMinKey(key) && isMaxKey(end)) ||
83
- (isMinKey(end) && isMaxKey(key))) {
84
- args.$all = true;
85
- }
86
- if (isDef(lower.key) && !isMinKey(lower.key)) {
87
- args[lower.step === 1 ? '$after' : '$since'] = lower.key;
88
- }
89
- if (isDef(upper.key) && !isMaxKey(upper.key)) {
90
- args[upper.step === -1 ? '$before' : '$until'] = upper.key;
91
- }
92
- return args;
93
- }
@@ -1,2 +0,0 @@
1
- export declare function encode(u8Arr: any): string;
2
- export declare function decode(string: any, start?: number): any;
package/coding/base64.js DELETED
@@ -1,41 +0,0 @@
1
- import { addStringify } from "../util.js";
2
- import alpha from "./alphabet.js";
3
- function getByte(view, offset) {
4
- return offset < view.byteLength ? view.getUint8(offset) : 0;
5
- }
6
- function getChar(string, offset) {
7
- return offset < string.length ? alpha.indexOf(string[offset]) : 0;
8
- }
9
- export function encode(u8Arr) {
10
- const { buffer, byteOffset, byteLength } = u8Arr;
11
- const view = new DataView(buffer, byteOffset, byteLength);
12
- let str = '';
13
- for (let i = 0; i < view.byteLength; i += 3) {
14
- let value = (getByte(view, i) << 16) +
15
- (getByte(view, i + 1) << 8) +
16
- getByte(view, i + 2);
17
- let gstr = '';
18
- for (let j = 0; j < 4; j++) {
19
- gstr = alpha[value & 0x3f] + gstr;
20
- value = (value >> 6) | 0;
21
- }
22
- str += gstr;
23
- }
24
- return str.substring(0, Math.ceil((view.byteLength * 4) / 3));
25
- }
26
- export function decode(string, start = 0) {
27
- const buffer = new ArrayBuffer(Math.floor(((string.length - start) * 3) / 4));
28
- const view = new DataView(buffer);
29
- for (let i = start; i < string.length; i += 4) {
30
- let value = (getChar(string, i) << 18) +
31
- (getChar(string, i + 1) << 12) +
32
- (getChar(string, i + 2) << 6) +
33
- getChar(string, i + 3);
34
- for (let j = (i * 3) / 4 + 2; j >= (i * 3) / 4; j--) {
35
- if (j < view.byteLength)
36
- view.setUint8(j, value & 0xff);
37
- value = (value >> 8) | 0;
38
- }
39
- }
40
- return addStringify(new Uint8Array(buffer));
41
- }
@@ -1,2 +0,0 @@
1
- export declare function decodeGraph(graph: any): any[];
2
- export declare function decodeQuery(query: any): any[];
@@ -1,197 +0,0 @@
1
- import { isBranch, isLink, isPrefix, isRange } from "../node/index.js";
2
- import { keyAfter } from "../ops/index.js";
3
- import { clone, cmp, isDef, isEmpty, isMaxKey, isMinKey } from "../util.js";
4
- import { decode as decodeArgs, splitArgs } from "./args.js";
5
- import { decode as decodePath } from "./path.js";
6
- const PRE_CHI_PUT = Symbol('PREFIX_CHILDREN_$PUT');
7
- /**
8
- @param {any[]} nodes
9
- @param {{ isGraph?: boolean }} options
10
- */
11
- function decode(nodes = [], { isGraph } = {}) {
12
- function decodeChildren(nodes) {
13
- let result = [];
14
- let allStrs = true;
15
- let allNums = true;
16
- //Pushes objects to the result set and updates allStrs and allNums.
17
- function pushResult(...objects) {
18
- for (const object of objects) {
19
- if (isDef(object) && !Number.isInteger(object.$key))
20
- allNums = false;
21
- if (isDef(object) && typeof object.$key !== 'string')
22
- allStrs = false;
23
- }
24
- result.push(...objects);
25
- }
26
- const putRanges = [];
27
- const prefixChildPuts = [];
28
- let lastNode = null;
29
- // Graphs only: Constructs the $put array. Returns true if this is a range
30
- // node that does not require to be added to the results.
31
- function addPutRange({ key, end }) {
32
- if (lastNode) {
33
- if (lastNode.end) {
34
- if (cmp(key, keyAfter(lastNode.end)) === 0) {
35
- lastNode.end = end || key;
36
- return end && cmp(end, key) !== 0;
37
- }
38
- }
39
- else {
40
- if (cmp(key, keyAfter(lastNode.key)) === 0) {
41
- key = lastNode.key;
42
- }
43
- }
44
- }
45
- if (end && cmp(key, end) !== 0) {
46
- lastNode = { key, end };
47
- putRanges.push(lastNode);
48
- return true;
49
- }
50
- lastNode = { key };
51
- return false;
52
- }
53
- for (const node of nodes) {
54
- if (isGraph && addPutRange(node))
55
- continue;
56
- if (isPrefix(node)) {
57
- const decodedChildren = decodePrefixNode(node);
58
- if (PRE_CHI_PUT in decodedChildren) {
59
- prefixChildPuts.push(...decodedChildren[PRE_CHI_PUT]);
60
- }
61
- pushResult(...decodedChildren);
62
- }
63
- else if (isGraph && isRange(node))
64
- pushResult(decodeRangeNode(node));
65
- else if (isBranch(node))
66
- pushResult(decodeBranchNode(node));
67
- else if (isLink(node))
68
- pushResult(decodeLinkNode(node));
69
- else
70
- pushResult(decodeLeafNode(node));
71
- }
72
- // Use a simplified format if all the keys are strings or it's a plain array.
73
- if (allStrs ||
74
- (allNums &&
75
- putRanges.length === 1 &&
76
- cmp(putRanges[0].key, 0) === 0 &&
77
- cmp(putRanges[0].end, Number.POSITIVE_INFINITY) === 0)) {
78
- result = result.reduce((collection, item) => {
79
- if (Array.isArray(item)) {
80
- collection[item.$key] = item;
81
- delete item.$key;
82
- return collection;
83
- }
84
- let { $key, $val } = item;
85
- delete item.$key;
86
- delete item.$val;
87
- if ($val === null) {
88
- $val = { $val };
89
- }
90
- else if (typeof $val === 'object') {
91
- $val = clone($val);
92
- Object.defineProperty($val, '$val', { value: true });
93
- }
94
- // biome-ignore format: tertnary chain
95
- collection[$key] = (isDef($val) ? $val :
96
- !isEmpty(item) || item.$ref || item.$put ? item :
97
- isGraph ? null : true);
98
- return collection;
99
- }, allStrs ? {} : []);
100
- }
101
- if (isGraph && putRanges.length) {
102
- if (isMinKey(putRanges[0].key) && isMaxKey(putRanges[0].end)) {
103
- Object.defineProperty(result, '$put', { value: true });
104
- }
105
- else {
106
- Object.defineProperty(result, '$put', {
107
- value: putRanges
108
- .map((rNode) => decodeArgs(rNode))
109
- .concat(prefixChildPuts),
110
- });
111
- }
112
- }
113
- else if (prefixChildPuts.length) {
114
- Object.defineProperty(result, '$put', { value: prefixChildPuts });
115
- }
116
- return result;
117
- }
118
- function decodePrefixNode(node) {
119
- let args = decodeArgs(node);
120
- if (!args)
121
- args = {};
122
- if (typeof args === 'string') {
123
- throw Error(`decode.unencoded_prefix: ${args}`);
124
- }
125
- if (isLink(node)) {
126
- args.$all = true;
127
- const $ref = decodePath(node.path);
128
- const lastKey = $ref[$ref.length - 1];
129
- if (typeof lastKey === 'string') {
130
- throw Error(`decode.unencoded_prefix_ref: ${node.path}`);
131
- }
132
- lastKey.$all = true;
133
- const linkObject = { $key: args };
134
- Object.defineProperty(linkObject, '$ref', { value: $ref });
135
- return [linkObject];
136
- }
137
- /** @type {any[] & {$put?: any}} */
138
- const children = decodeChildren(node.children);
139
- if (!Array.isArray(children)) {
140
- throw Error(`decode.prefix_without_encoded_child_keys:${node.key}`);
141
- }
142
- for (const child of children) {
143
- if (typeof child.$key === 'string') {
144
- throw Error(`decode.prefix_with_unencoded_child_key:${child.$key}`);
145
- }
146
- if (!splitArgs(child.$key)[0]) {
147
- // splitArgs returns [page, filter]. If page is blank, it indicates
148
- // we have a bare cursor.
149
- child.$key = { $cursor: child.$key };
150
- }
151
- child.$key = { ...args, ...child.$key };
152
- }
153
- if (children.$put === true) {
154
- children[PRE_CHI_PUT] = [{ ...args, $all: true }];
155
- }
156
- else if (Array.isArray(children.$put)) {
157
- children[PRE_CHI_PUT] = children.$put.map((rarg) => ({
158
- ...args,
159
- ...rarg,
160
- }));
161
- }
162
- else if (isDef(children.$put)) {
163
- children[PRE_CHI_PUT] = [{ ...args, ...children.$put }];
164
- }
165
- return children;
166
- }
167
- function decodeBranchNode(node) {
168
- const child = decodeChildren(node.children);
169
- child.$key = decodeArgs(node);
170
- return child;
171
- }
172
- function decodeLeafNode(node) {
173
- const child = isGraph ? { $val: node.value } : {};
174
- child.$key = decodeArgs(node);
175
- return child;
176
- }
177
- /**
178
- Only for graphs;
179
- */
180
- function decodeRangeNode(node) {
181
- if (cmp(node.key, node.end) === 0) {
182
- return { $key: decodeArgs({ key: node.key }) };
183
- }
184
- }
185
- function decodeLinkNode(node) {
186
- const linkObject = { $key: decodeArgs(node) };
187
- Object.defineProperty(linkObject, '$ref', { value: decodePath(node.path) });
188
- return linkObject;
189
- }
190
- return decodeChildren(nodes);
191
- }
192
- export function decodeGraph(graph) {
193
- return decode(graph, { isGraph: true });
194
- }
195
- export function decodeQuery(query) {
196
- return decode(query, { isGraph: false });
197
- }