@kalada/core 0.0.0

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 (94) hide show
  1. package/README.md +10 -0
  2. package/THIRD_PARTY_NOTICES.md +42 -0
  3. package/dist/index.cjs +20 -0
  4. package/dist/index.cjs.map +1 -0
  5. package/dist/index.d.cts +2 -0
  6. package/dist/index.d.ts +2 -0
  7. package/dist/index.js +3 -0
  8. package/dist/index.js.map +1 -0
  9. package/dist/kuery-v1/callback-promise.cjs +79 -0
  10. package/dist/kuery-v1/callback-promise.cjs.map +1 -0
  11. package/dist/kuery-v1/callback-promise.d.cts +4 -0
  12. package/dist/kuery-v1/callback-promise.d.ts +4 -0
  13. package/dist/kuery-v1/callback-promise.js +77 -0
  14. package/dist/kuery-v1/callback-promise.js.map +1 -0
  15. package/dist/kuery-v1/canonicalize.cjs +181 -0
  16. package/dist/kuery-v1/canonicalize.cjs.map +1 -0
  17. package/dist/kuery-v1/canonicalize.d.cts +7 -0
  18. package/dist/kuery-v1/canonicalize.d.ts +7 -0
  19. package/dist/kuery-v1/canonicalize.js +178 -0
  20. package/dist/kuery-v1/canonicalize.js.map +1 -0
  21. package/dist/kuery-v1/compile.cjs +53 -0
  22. package/dist/kuery-v1/compile.cjs.map +1 -0
  23. package/dist/kuery-v1/compile.d.cts +10 -0
  24. package/dist/kuery-v1/compile.d.ts +10 -0
  25. package/dist/kuery-v1/compile.js +51 -0
  26. package/dist/kuery-v1/compile.js.map +1 -0
  27. package/dist/kuery-v1/dependencies.cjs +32 -0
  28. package/dist/kuery-v1/dependencies.cjs.map +1 -0
  29. package/dist/kuery-v1/dependencies.d.cts +7 -0
  30. package/dist/kuery-v1/dependencies.d.ts +7 -0
  31. package/dist/kuery-v1/dependencies.js +29 -0
  32. package/dist/kuery-v1/dependencies.js.map +1 -0
  33. package/dist/kuery-v1/evaluate.cjs +174 -0
  34. package/dist/kuery-v1/evaluate.cjs.map +1 -0
  35. package/dist/kuery-v1/evaluate.d.cts +6 -0
  36. package/dist/kuery-v1/evaluate.d.ts +6 -0
  37. package/dist/kuery-v1/evaluate.js +172 -0
  38. package/dist/kuery-v1/evaluate.js.map +1 -0
  39. package/dist/kuery-v1/index.cjs +54 -0
  40. package/dist/kuery-v1/index.cjs.map +1 -0
  41. package/dist/kuery-v1/index.d.cts +8 -0
  42. package/dist/kuery-v1/index.d.ts +8 -0
  43. package/dist/kuery-v1/index.js +9 -0
  44. package/dist/kuery-v1/index.js.map +1 -0
  45. package/dist/kuery-v1/inspect.cjs +167 -0
  46. package/dist/kuery-v1/inspect.cjs.map +1 -0
  47. package/dist/kuery-v1/inspect.d.cts +17 -0
  48. package/dist/kuery-v1/inspect.d.ts +17 -0
  49. package/dist/kuery-v1/inspect.js +160 -0
  50. package/dist/kuery-v1/inspect.js.map +1 -0
  51. package/dist/kuery-v1/json-schema.cjs +108 -0
  52. package/dist/kuery-v1/json-schema.cjs.map +1 -0
  53. package/dist/kuery-v1/json-schema.d.cts +10 -0
  54. package/dist/kuery-v1/json-schema.d.ts +10 -0
  55. package/dist/kuery-v1/json-schema.js +105 -0
  56. package/dist/kuery-v1/json-schema.js.map +1 -0
  57. package/dist/kuery-v1/limits.cjs +35 -0
  58. package/dist/kuery-v1/limits.cjs.map +1 -0
  59. package/dist/kuery-v1/limits.d.cts +6 -0
  60. package/dist/kuery-v1/limits.d.ts +6 -0
  61. package/dist/kuery-v1/limits.js +32 -0
  62. package/dist/kuery-v1/limits.js.map +1 -0
  63. package/dist/kuery-v1/profile.cjs +224 -0
  64. package/dist/kuery-v1/profile.cjs.map +1 -0
  65. package/dist/kuery-v1/profile.d.cts +48 -0
  66. package/dist/kuery-v1/profile.d.ts +48 -0
  67. package/dist/kuery-v1/profile.js +219 -0
  68. package/dist/kuery-v1/profile.js.map +1 -0
  69. package/dist/kuery-v1/result.cjs +44 -0
  70. package/dist/kuery-v1/result.cjs.map +1 -0
  71. package/dist/kuery-v1/result.d.cts +12 -0
  72. package/dist/kuery-v1/result.d.ts +12 -0
  73. package/dist/kuery-v1/result.js +39 -0
  74. package/dist/kuery-v1/result.js.map +1 -0
  75. package/dist/kuery-v1/standard-profile.cjs +173 -0
  76. package/dist/kuery-v1/standard-profile.cjs.map +1 -0
  77. package/dist/kuery-v1/standard-profile.d.cts +7 -0
  78. package/dist/kuery-v1/standard-profile.d.ts +7 -0
  79. package/dist/kuery-v1/standard-profile.js +171 -0
  80. package/dist/kuery-v1/standard-profile.js.map +1 -0
  81. package/dist/kuery-v1/types.cjs +4 -0
  82. package/dist/kuery-v1/types.cjs.map +1 -0
  83. package/dist/kuery-v1/types.d.cts +65 -0
  84. package/dist/kuery-v1/types.d.ts +65 -0
  85. package/dist/kuery-v1/types.js +3 -0
  86. package/dist/kuery-v1/types.js.map +1 -0
  87. package/dist/program-v1.cjs +72 -0
  88. package/dist/program-v1.cjs.map +1 -0
  89. package/dist/program-v1.d.cts +17 -0
  90. package/dist/program-v1.d.ts +17 -0
  91. package/dist/program-v1.js +68 -0
  92. package/dist/program-v1.js.map +1 -0
  93. package/package.json +51 -0
  94. package/provenance/kuery-2.1.0.json +93 -0
@@ -0,0 +1,178 @@
1
+ import { dataProperties, cloneJson, exceedsStringLimit, arrayLength, stableJson } from './inspect.js';
2
+ import { resolveLimits } from './limits.js';
3
+ import { failure, success, ExpressionFailure } from './result.js';
4
+
5
+ const NODE_KEYS = Object.freeze({
6
+ literal: /* @__PURE__ */ new Set(["kind", "value"]),
7
+ ref: /* @__PURE__ */ new Set(["kind", "ref"]),
8
+ op: /* @__PURE__ */ new Set(["kind", "op", "args"])
9
+ });
10
+ const ALL_NODE_KEYS = /* @__PURE__ */ new Set(["kind", "value", "ref", "op", "args"]);
11
+ function canonicalizeExpression(input, options = {}) {
12
+ let limits;
13
+ try {
14
+ limits = resolveLimits(options.limits);
15
+ } catch {
16
+ return failure("EXPRESSION_LIMIT_EXCEEDED", []);
17
+ }
18
+ const state = {
19
+ limits,
20
+ options,
21
+ maxDepth: limits.maxDepth,
22
+ maxNodes: limits.maxNodes,
23
+ maxStringLength: limits.maxStringLength,
24
+ nodes: 0,
25
+ active: /* @__PURE__ */ new WeakSet()
26
+ };
27
+ try {
28
+ return success(canonicalizeNode(input, [], 0, state));
29
+ } catch (error) {
30
+ const problem = error instanceof ExpressionFailure ? error : new ExpressionFailure("EXPRESSION_INVALID_INPUT", []);
31
+ return failure(problem.code, problem.expressionPath);
32
+ }
33
+ }
34
+ function canonicalizeNode(input, path, depth, state) {
35
+ countNode(path, depth, state);
36
+ if (typeof input !== "object" || input === null)
37
+ throw new ExpressionFailure("EXPRESSION_INVALID_INPUT", path);
38
+ if (state.active.has(input)) throw new ExpressionFailure("EXPRESSION_INVALID_INPUT", path);
39
+ state.active.add(input);
40
+ try {
41
+ const initial = dataProperties(input, path, ALL_NODE_KEYS, 3);
42
+ const kind = initial.kind;
43
+ if (kind !== "literal" && kind !== "ref" && kind !== "op") {
44
+ throw new ExpressionFailure("EXPRESSION_INVALID_INPUT", [...path, "kind"]);
45
+ }
46
+ const properties = selectNodeProperties(initial, path, NODE_KEYS[kind]);
47
+ if (kind === "literal") return canonicalLiteral(properties, path, depth, state);
48
+ if (kind === "ref") return canonicalReference(properties, path, depth, state);
49
+ return canonicalOperator(properties, path, depth, state);
50
+ } finally {
51
+ state.active.delete(input);
52
+ }
53
+ }
54
+ function selectNodeProperties(properties, path, allowed) {
55
+ for (const key of Object.keys(properties)) {
56
+ if (!allowed.has(key)) throw new ExpressionFailure("EXPRESSION_INVALID_INPUT", [...path, key]);
57
+ }
58
+ return properties;
59
+ }
60
+ function countNode(path, depth, state) {
61
+ state.nodes += 1;
62
+ if (depth > state.limits.maxDepth || state.nodes > state.limits.maxNodes) {
63
+ throw new ExpressionFailure("EXPRESSION_LIMIT_EXCEEDED", path);
64
+ }
65
+ }
66
+ function canonicalLiteral(properties, path, depth, state) {
67
+ if (!("value" in properties))
68
+ throw new ExpressionFailure("EXPRESSION_INVALID_INPUT", [...path, "value"]);
69
+ const value = cloneJson(properties.value, [...path, "value"], depth + 1, state);
70
+ return Object.freeze({ kind: "literal", value });
71
+ }
72
+ function canonicalReference(properties, path, depth, state) {
73
+ if (!("ref" in properties))
74
+ throw new ExpressionFailure("EXPRESSION_INVALID_REFERENCE", [...path, "ref"]);
75
+ const refPath = [...path, "ref"];
76
+ const raw = properties.ref;
77
+ const codec = state.options.reference;
78
+ if (!codec && typeof raw === "string" && exceedsStringLimit(raw, state.limits.maxReferenceLength)) {
79
+ throw new ExpressionFailure("EXPRESSION_LIMIT_EXCEEDED", refPath);
80
+ }
81
+ const cloned = cloneReference(raw, refPath, depth + 1, state);
82
+ if (codec ? !safeValidate(codec.validate, cloned) : !defaultReference(cloned)) {
83
+ throw new ExpressionFailure("EXPRESSION_INVALID_REFERENCE", refPath);
84
+ }
85
+ if (referenceExceedsLength(cloned, state.limits.maxReferenceLength)) {
86
+ throw new ExpressionFailure("EXPRESSION_LIMIT_EXCEEDED", refPath);
87
+ }
88
+ if (!codec?.canonicalize) return Object.freeze({ kind: "ref", ref: cloned });
89
+ const replacement = canonicalReferenceValue(cloned, codec.canonicalize, refPath);
90
+ const output = cloneReference(replacement, refPath, depth + 1, state);
91
+ if (!safeValidate(codec.validate, output))
92
+ throw new ExpressionFailure("EXPRESSION_INVALID_REFERENCE", refPath);
93
+ if (referenceExceedsLength(output, state.limits.maxReferenceLength)) {
94
+ throw new ExpressionFailure("EXPRESSION_LIMIT_EXCEEDED", refPath);
95
+ }
96
+ return Object.freeze({ kind: "ref", ref: output });
97
+ }
98
+ function cloneReference(input, path, depth, state) {
99
+ try {
100
+ return cloneJson(input, path, depth, state);
101
+ } catch (error) {
102
+ if (error instanceof ExpressionFailure && error.code === "EXPRESSION_LIMIT_EXCEEDED")
103
+ throw error;
104
+ throw new ExpressionFailure("EXPRESSION_INVALID_REFERENCE", path);
105
+ }
106
+ }
107
+ function safeValidate(validate, input) {
108
+ try {
109
+ return validate(input) === true;
110
+ } catch {
111
+ return false;
112
+ }
113
+ }
114
+ function defaultReference(input) {
115
+ return typeof input === "string" && input.length > 0;
116
+ }
117
+ function canonicalReferenceValue(reference, canonicalize, path) {
118
+ try {
119
+ return canonicalize(reference);
120
+ } catch {
121
+ throw new ExpressionFailure("EXPRESSION_INVALID_REFERENCE", path);
122
+ }
123
+ }
124
+ function canonicalOperator(properties, path, depth, state) {
125
+ if (typeof properties.op !== "string" || !Array.isArray(properties.args)) {
126
+ throw new ExpressionFailure("EXPRESSION_INVALID_INPUT", path);
127
+ }
128
+ if (properties.op.length === 0 || exceedsStringLimit(properties.op, state.limits.maxStringLength)) {
129
+ throw new ExpressionFailure("EXPRESSION_LIMIT_EXCEEDED", [...path, "op"]);
130
+ }
131
+ const args = canonicalArgs(properties.args, path, depth, state);
132
+ return Object.freeze({ kind: "op", op: properties.op, args });
133
+ }
134
+ function canonicalArgs(input, path, depth, state) {
135
+ const argsPath = [...path, "args"];
136
+ const length = arrayLength(input, argsPath);
137
+ if (length > state.limits.maxArgs || length > state.maxNodes - state.nodes) {
138
+ throw new ExpressionFailure("EXPRESSION_LIMIT_EXCEEDED", argsPath);
139
+ }
140
+ validateArrayKeys(input, argsPath, length);
141
+ const output = [];
142
+ for (let index = 0; index < length; index += 1) {
143
+ const descriptor = arrayElement(input, index, argsPath);
144
+ output.push(canonicalizeNode(descriptor.value, [...argsPath, index], depth + 1, state));
145
+ }
146
+ return Object.freeze(output);
147
+ }
148
+ function validateArrayKeys(input, path, length) {
149
+ try {
150
+ const keys = Reflect.ownKeys(input);
151
+ if (keys.length !== length + 1) throw new ExpressionFailure("EXPRESSION_INVALID_INPUT", path);
152
+ } catch (error) {
153
+ if (error instanceof ExpressionFailure) throw error;
154
+ throw new ExpressionFailure("EXPRESSION_INVALID_INPUT", path);
155
+ }
156
+ }
157
+ function arrayElement(input, index, path) {
158
+ try {
159
+ const descriptor = Object.getOwnPropertyDescriptor(input, String(index));
160
+ if (!descriptor || !("value" in descriptor) || !descriptor.enumerable) {
161
+ throw new ExpressionFailure("EXPRESSION_INVALID_INPUT", [...path, index]);
162
+ }
163
+ return descriptor;
164
+ } catch (error) {
165
+ if (error instanceof ExpressionFailure) throw error;
166
+ throw new ExpressionFailure("EXPRESSION_INVALID_INPUT", [...path, index]);
167
+ }
168
+ }
169
+ function referenceIdentity(reference) {
170
+ return stableJson(reference);
171
+ }
172
+ function referenceExceedsLength(reference, maxLength) {
173
+ return typeof reference === "string" ? exceedsStringLimit(reference, maxLength) : stableJson(reference).length > maxLength;
174
+ }
175
+
176
+ export { canonicalizeExpression, referenceIdentity };
177
+ //# sourceMappingURL=canonicalize.js.map
178
+ //# sourceMappingURL=canonicalize.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/kuery-v1/canonicalize.ts"],"names":[],"mappings":";;;;AAmBA,MAAM,SAAA,GAAY,OAAO,MAAA,CAAO;AAAA,EAC9B,yBAAS,IAAI,GAAA,CAAI,CAAC,MAAA,EAAQ,OAAO,CAAC,CAAA;AAAA,EAClC,qBAAK,IAAI,GAAA,CAAI,CAAC,MAAA,EAAQ,KAAK,CAAC,CAAA;AAAA,EAC5B,oBAAI,IAAI,GAAA,CAAI,CAAC,MAAA,EAAQ,IAAA,EAAM,MAAM,CAAC;AACpC,CAAC,CAAA;AACD,MAAM,aAAA,uBAAoB,GAAA,CAAI,CAAC,QAAQ,OAAA,EAAS,KAAA,EAAO,IAAA,EAAM,MAAM,CAAC,CAAA;AAQ7D,SAAS,sBAAA,CACd,KAAA,EACA,OAAA,GAA4C,EAAC,EACjB;AAC5B,EAAA,IAAI,MAAA;AACJ,EAAA,IAAI;AACF,IAAA,MAAA,GAAS,aAAA,CAAc,QAAQ,MAAM,CAAA;AAAA,EACvC,CAAA,CAAA,MAAQ;AACN,IAAA,OAAO,OAAA,CAAQ,2BAAA,EAA6B,EAAE,CAAA;AAAA,EAChD;AACA,EAAA,MAAM,KAAA,GAA2B;AAAA,IAC/B,MAAA;AAAA,IACA,OAAA;AAAA,IACA,UAAU,MAAA,C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AA,MAAM,IAAI,iBAAA,CAAkB,0BAAA,EAA4B,IAAI,CAAA;AAAA,EAC9D;AACF;AAEA,SAAS,YAAA,CACP,KAAA,EACA,KAAA,EACA,IAAA,EACyC;AACzC,EAAA,IAAI;AACF,IAAA,MAAM,aAAa,MAAA,CAAO,wBAAA,CAAyB,KAAA,EAAO,MAAA,CAAO,KAAK,CAAC,CAAA;AACvE,IAAA,IAAI,CAAC,UAAA,IAAc,EAAE,WAAW,UAAA,CAAA,IAAe,CAAC,WAAW,UAAA,EAAY;AACrE,MAAA,MAAM,IAAI,iBAAA,CAAkB,0BAAA,EAA4B,CAAC,GAAG,IAAA,EAAM,KAAK,CAAC,CAAA;AAAA,IAC1E;AACA,IAAA,OAAO,UAAA;AAAA,EACT,SAAS,KAAA,EAAO;AACd,IAAA,IAAI,KAAA,YAAiB,mBAAmB,MAAM,KAAA;AAC9C,IAAA,MAAM,IAAI,iBAAA,CAAkB,0BAAA,EAA4B,CAAC,GAAG,IAAA,EAAM,KAAK,CAAC,CAAA;AAAA,EAC1E;AACF;AAEO,SAAS,kBAAkB,SAAA,EAA8B;AAC9D,EAAA,OAAO,WAAW,SAAS,CAAA;AAC7B;AAEA,SAAS,sBAAA,CAAuB,WAAsB,SAAA,EAA4B;AAChF,EAAA,OAAO,OAAO,SAAA,KAAc,QAAA,GACxB,kBAAA,CAAmB,SAAA,EAAW,SAAS,CAAA,GACvC,UAAA,CAAW,SAAS,CAAA,CAAE,MAAA,GAAS,SAAA;AACrC","file":"canonicalize.js","sourcesContent":["import {\n arrayLength,\n cloneJson,\n dataProperties,\n exceedsStringLimit,\n stableJson,\n type ValidationState,\n} from \"./inspect.js\";\nimport { resolveLimits } from \"./limits.js\";\nimport { ExpressionFailure, failure, success } from \"./result.js\";\nimport type {\n CanonicalizeExpressionOptions,\n ExpressionLimits,\n ExpressionPath,\n JsonValue,\n Result,\n ValueExpression,\n} from \"./types.js\";\n\nconst NODE_KEYS = Object.freeze({\n literal: new Set([\"kind\", \"value\"]),\n ref: new Set([\"kind\", \"ref\"]),\n op: new Set([\"kind\", \"op\", \"args\"]),\n});\nconst ALL_NODE_KEYS = new Set([\"kind\", \"value\", \"ref\", \"op\", \"args\"]);\n\ninterface CanonicalState<R extends JsonValue> extends ValidationState {\n readonly limits: ExpressionLimits;\n readonly options: CanonicalizeExpressionOptions<R>;\n}\n\n/** Validate unknown input and return a deeply frozen, canonical expression snapshot. */\nexport function canonicalizeExpression<R extends JsonValue = string>(\n input: unknown,\n options: CanonicalizeExpressionOptions<R> = {},\n): Result<ValueExpression<R>> {\n let limits: ExpressionLimits;\n try {\n limits = resolveLimits(options.limits);\n } catch {\n return failure(\"EXPRESSION_LIMIT_EXCEEDED\", []);\n }\n const state: CanonicalState<R> = {\n limits,\n options,\n maxDepth: limits.maxDepth,\n maxNodes: limits.maxNodes,\n maxStringLength: limits.maxStringLength,\n nodes: 0,\n active: new WeakSet(),\n };\n try {\n return success(canonicalizeNode(input, [], 0, state));\n } catch (error) {\n const problem =\n error instanceof ExpressionFailure\n ? error\n : new ExpressionFailure(\"EXPRESSION_INVALID_INPUT\", []);\n return failure(problem.code, problem.expressionPath);\n }\n}\n\nfunction canonicalizeNode<R extends JsonValue>(\n input: unknown,\n path: ExpressionPath,\n depth: number,\n state: CanonicalState<R>,\n): ValueExpression<R> {\n countNode(path, depth, state);\n if (typeof input !== \"object\" || input === null)\n throw new ExpressionFailure(\"EXPRESSION_INVALID_INPUT\", path);\n if (state.active.has(input)) throw new ExpressionFailure(\"EXPRESSION_INVALID_INPUT\", path);\n state.active.add(input);\n try {\n const initial = dataProperties(input, path, ALL_NODE_KEYS, 3);\n const kind = initial.kind;\n if (kind !== \"literal\" && kind !== \"ref\" && kind !== \"op\") {\n throw new ExpressionFailure(\"EXPRESSION_INVALID_INPUT\", [...path, \"kind\"]);\n }\n const properties = selectNodeProperties(initial, path, NODE_KEYS[kind]);\n if (kind === \"literal\") return canonicalLiteral(properties, path, depth, state);\n if (kind === \"ref\") return canonicalReference(properties, path, depth, state);\n return canonicalOperator(properties, path, depth, state);\n } finally {\n state.active.delete(input);\n }\n}\n\nfunction selectNodeProperties(\n properties: Readonly<Record<string, unknown>>,\n path: ExpressionPath,\n allowed: ReadonlySet<string>,\n): Readonly<Record<string, unknown>> {\n for (const key of Object.keys(properties)) {\n if (!allowed.has(key)) throw new ExpressionFailure(\"EXPRESSION_INVALID_INPUT\", [...path, key]);\n }\n return properties;\n}\n\nfunction countNode(\n path: ExpressionPath,\n depth: number,\n state: ValidationState & { readonly limits: ExpressionLimits },\n): void {\n state.nodes += 1;\n if (depth > state.limits.maxDepth || state.nodes > state.limits.maxNodes) {\n throw new ExpressionFailure(\"EXPRESSION_LIMIT_EXCEEDED\", path);\n }\n}\n\nfunction canonicalLiteral<R extends JsonValue>(\n properties: Readonly<Record<string, unknown>>,\n path: ExpressionPath,\n depth: number,\n state: CanonicalState<R>,\n): ValueExpression<R> {\n if (!(\"value\" in properties))\n throw new ExpressionFailure(\"EXPRESSION_INVALID_INPUT\", [...path, \"value\"]);\n const value = cloneJson(properties.value, [...path, \"value\"], depth + 1, state);\n return Object.freeze({ kind: \"literal\", value });\n}\n\nfunction canonicalReference<R extends JsonValue>(\n properties: Readonly<Record<string, unknown>>,\n path: ExpressionPath,\n depth: number,\n state: CanonicalState<R>,\n): ValueExpression<R> {\n if (!(\"ref\" in properties))\n throw new ExpressionFailure(\"EXPRESSION_INVALID_REFERENCE\", [...path, \"ref\"]);\n const refPath = [...path, \"ref\"];\n const raw = properties.ref;\n const codec = state.options.reference;\n if (\n !codec &&\n typeof raw === \"string\" &&\n exceedsStringLimit(raw, state.limits.maxReferenceLength)\n ) {\n throw new ExpressionFailure(\"EXPRESSION_LIMIT_EXCEEDED\", refPath);\n }\n const cloned = cloneReference<R>(raw, refPath, depth + 1, state);\n if (codec ? !safeValidate(codec.validate, cloned) : !defaultReference(cloned)) {\n throw new ExpressionFailure(\"EXPRESSION_INVALID_REFERENCE\", refPath);\n }\n if (referenceExceedsLength(cloned, state.limits.maxReferenceLength)) {\n throw new ExpressionFailure(\"EXPRESSION_LIMIT_EXCEEDED\", refPath);\n }\n if (!codec?.canonicalize) return Object.freeze({ kind: \"ref\", ref: cloned });\n const replacement = canonicalReferenceValue(cloned, codec.canonicalize, refPath);\n const output = cloneReference<R>(replacement, refPath, depth + 1, state);\n if (!safeValidate(codec.validate, output))\n throw new ExpressionFailure(\"EXPRESSION_INVALID_REFERENCE\", refPath);\n if (referenceExceedsLength(output, state.limits.maxReferenceLength)) {\n throw new ExpressionFailure(\"EXPRESSION_LIMIT_EXCEEDED\", refPath);\n }\n return Object.freeze({ kind: \"ref\", ref: output });\n}\n\nfunction cloneReference<R extends JsonValue>(\n input: unknown,\n path: ExpressionPath,\n depth: number,\n state: CanonicalState<R>,\n): R {\n try {\n return cloneJson(input, path, depth, state) as R;\n } catch (error) {\n if (error instanceof ExpressionFailure && error.code === \"EXPRESSION_LIMIT_EXCEEDED\")\n throw error;\n throw new ExpressionFailure(\"EXPRESSION_INVALID_REFERENCE\", path);\n }\n}\n\nfunction safeValidate<R>(validate: (input: unknown) => input is R, input: unknown): input is R {\n try {\n return validate(input) === true;\n } catch {\n return false;\n }\n}\n\nfunction defaultReference(input: unknown): input is string {\n return typeof input === \"string\" && input.length > 0;\n}\n\nfunction canonicalReferenceValue<R extends JsonValue>(\n reference: R,\n canonicalize: (reference: R) => R,\n path: ExpressionPath,\n): R {\n try {\n return canonicalize(reference);\n } catch {\n throw new ExpressionFailure(\"EXPRESSION_INVALID_REFERENCE\", path);\n }\n}\n\nfunction canonicalOperator<R extends JsonValue>(\n properties: Readonly<Record<string, unknown>>,\n path: ExpressionPath,\n depth: number,\n state: CanonicalState<R>,\n): ValueExpression<R> {\n if (typeof properties.op !== \"string\" || !Array.isArray(properties.args)) {\n throw new ExpressionFailure(\"EXPRESSION_INVALID_INPUT\", path);\n }\n if (\n properties.op.length === 0 ||\n exceedsStringLimit(properties.op, state.limits.maxStringLength)\n ) {\n throw new ExpressionFailure(\"EXPRESSION_LIMIT_EXCEEDED\", [...path, \"op\"]);\n }\n const args = canonicalArgs(properties.args, path, depth, state);\n return Object.freeze({ kind: \"op\", op: properties.op, args });\n}\n\nfunction canonicalArgs<R extends JsonValue>(\n input: unknown[],\n path: ExpressionPath,\n depth: number,\n state: CanonicalState<R>,\n): readonly ValueExpression<R>[] {\n const argsPath = [...path, \"args\"];\n const length = arrayLength(input, argsPath);\n if (length > state.limits.maxArgs || length > state.maxNodes - state.nodes) {\n throw new ExpressionFailure(\"EXPRESSION_LIMIT_EXCEEDED\", argsPath);\n }\n validateArrayKeys(input, argsPath, length);\n const output: ValueExpression<R>[] = [];\n for (let index = 0; index < length; index += 1) {\n const descriptor = arrayElement(input, index, argsPath);\n output.push(canonicalizeNode(descriptor.value, [...argsPath, index], depth + 1, state));\n }\n return Object.freeze(output);\n}\n\nfunction validateArrayKeys(input: unknown[], path: ExpressionPath, length: number): void {\n try {\n const keys = Reflect.ownKeys(input);\n if (keys.length !== length + 1) throw new ExpressionFailure(\"EXPRESSION_INVALID_INPUT\", path);\n } catch (error) {\n if (error instanceof ExpressionFailure) throw error;\n throw new ExpressionFailure(\"EXPRESSION_INVALID_INPUT\", path);\n }\n}\n\nfunction arrayElement(\n input: unknown[],\n index: number,\n path: ExpressionPath,\n): PropertyDescriptor & { value: unknown } {\n try {\n const descriptor = Object.getOwnPropertyDescriptor(input, String(index));\n if (!descriptor || !(\"value\" in descriptor) || !descriptor.enumerable) {\n throw new ExpressionFailure(\"EXPRESSION_INVALID_INPUT\", [...path, index]);\n }\n return descriptor as PropertyDescriptor & { value: unknown };\n } catch (error) {\n if (error instanceof ExpressionFailure) throw error;\n throw new ExpressionFailure(\"EXPRESSION_INVALID_INPUT\", [...path, index]);\n }\n}\n\nexport function referenceIdentity(reference: JsonValue): string {\n return stableJson(reference);\n}\n\nfunction referenceExceedsLength(reference: JsonValue, maxLength: number): boolean {\n return typeof reference === \"string\"\n ? exceedsStringLimit(reference, maxLength)\n : stableJson(reference).length > maxLength;\n}\n"]}
@@ -0,0 +1,53 @@
1
+ 'use strict';
2
+
3
+ var canonicalize_js = require('./canonicalize.js');
4
+ var dependencies_js = require('./dependencies.js');
5
+ var evaluate_js = require('./evaluate.js');
6
+ var limits_js = require('./limits.js');
7
+ var profile_js = require('./profile.js');
8
+ var result_js = require('./result.js');
9
+
10
+ function compileExpression(input, options) {
11
+ if (!options || !(options.profile instanceof profile_js.ExpressionProfile))
12
+ return result_js.failure("EXPRESSION_INVALID_INPUT", []);
13
+ const canonical = canonicalize_js.canonicalizeExpression(input, options);
14
+ if (!canonical.ok) return canonical;
15
+ const operators = /* @__PURE__ */ new Map();
16
+ const validation = validateOperators(canonical.value, options.profile, [], operators);
17
+ if (!validation.ok) return validation;
18
+ let limits;
19
+ try {
20
+ limits = limits_js.resolveLimits(options.limits);
21
+ } catch {
22
+ return result_js.failure("EXPRESSION_LIMIT_EXCEEDED", []);
23
+ }
24
+ const expression = canonical.value;
25
+ const dependencies = dependencies_js.collectExpressionDependencies(expression);
26
+ const compiled = {
27
+ expression,
28
+ dependencies,
29
+ evaluate: (resolve) => evaluate_js.evaluateCompiled(expression, resolve, operators, limits)
30
+ };
31
+ return result_js.success(Object.freeze(compiled));
32
+ }
33
+ function validateOperators(node, profile, path, output) {
34
+ if (node.kind !== "op") return result_js.success(void 0);
35
+ const operator = profile.get(node.op);
36
+ if (!operator) return result_js.failure("EXPRESSION_UNKNOWN_OPERATOR", [...path, "op"]);
37
+ if (!validArity(operator, node.args.length))
38
+ return result_js.failure("EXPRESSION_INVALID_ARITY", [...path, "args"]);
39
+ output.set(node, operator);
40
+ for (let index = 0; index < node.args.length; index += 1) {
41
+ const result = validateOperators(node.args[index], profile, [...path, "args", index], output);
42
+ if (!result.ok) return result;
43
+ }
44
+ return result_js.success(void 0);
45
+ }
46
+ function validArity(operator, length) {
47
+ if (operator.arity !== void 0) return length === operator.arity;
48
+ return length >= (operator.minArgs ?? 0) && length <= (operator.maxArgs ?? profile_js.MAX_EXPRESSION_OPERATOR_ARGS);
49
+ }
50
+
51
+ exports.compileExpression = compileExpression;
52
+ //# sourceMappingURL=compile.cjs.map
53
+ //# sourceMappingURL=compile.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/kuery-v1/compile.ts"],"names":["ExpressionProfile","failure","canonicalizeExpression","resolveLimits","collectExpressionDependencies","evaluateCompiled","success","MAX_EXPRESSION_OPERATOR_ARGS"],"mappings":";;;;;;;;;AA0BO,SAAS,iBAAA,CACd,OACA,OAAA,EAC+B;AAC/B,EAAA,IAAI,CAAC,OAAA,IAAW,EAAE,OAAA,CAAQ,OAAA,YAAmBA,4BAAA,CAAA;AAC3C,IAAA,OAAOC,iBAAA,CAAQ,0BAAA,EAA4B,EAAE,CAAA;AAC/C,EAAA,MAAM,SAAA,GAAYC,sCAAA,CAA0B,KAAA,EAAO,OAAO,CAAA;AAC1D,EAAA,IAAI,CAAC,SAAA,CAAU,EAAA,EAAI,OAAO,SAAA;AAC1B,EAAA,MAAM,SAAA,uBAAgB,GAAA,EAA4C;AAClE,EAAA,MAAM,UAAA,GAAa,kBAAkB,SAAA,CAAU,KAAA,EAAO,QAAQ,OAAA,EAAS,IAAI,SAAS,CAAA;AACpF,EAAA,IAAI,CAAC,UAAA,CAAW,EAAA,EAAI,OAAO,UAAA;AAC3B,EAAA,IAAI,MAAA;AACJ,EAAA,IAAI;AACF,IAAA,MAAA,GAASC,uBAAA,CAAc,QAAQ,MAAM,CAAA;AAAA,EACvC,CAAA,CAAA,MAAQ;AACN,IAAA,OAAOF,iBAAA,CAAQ,2BAAA,EAA6B,EAAE,CAAA;AAAA,EAChD;AACA,EAAA,MAAM,aAAa,SAAA,CAAU,KAAA;AAC7B,EAAA,MAAM,YAAA,GAAeG,8CAA8B,UAAU,CAAA;AAC7D,EAAA,MAAM,QAAA,GAAkC;AAAA,IACtC,UAAA;AAAA,IACA,YAAA;AAAA,IACA,UAAU,CAAC,OAAA,KAAYC,6BAAiB,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OAAA,IAAW,CAAA,CAAA,IAC/B,MAAA,KAAW,SAAS,OAAA,IAAWC,uCAAA,CAAA;AAEnC","file":"compile.cjs","sourcesContent":["import { canonicalizeExpression } from \"./canonicalize.js\";\nimport { collectExpressionDependencies } from \"./dependencies.js\";\nimport { evaluateCompiled } from \"./evaluate.js\";\nimport { resolveLimits } from \"./limits.js\";\nimport {\n type ExpressionOperator,\n ExpressionProfile,\n MAX_EXPRESSION_OPERATOR_ARGS,\n} from \"./profile.js\";\nimport { failure, success } from \"./result.js\";\nimport type {\n CanonicalizeExpressionOptions,\n CompiledExpression,\n ExpressionLimits,\n ExpressionPath,\n JsonValue,\n Result,\n ValueExpression,\n} from \"./types.js\";\n\nexport interface CompileExpressionOptions<R extends JsonValue>\n extends CanonicalizeExpressionOptions<R> {\n readonly profile: ExpressionProfile;\n}\n\n/** Compile and validate a complete expression against one structurally immutable profile. */\nexport function compileExpression<R extends JsonValue = string>(\n input: unknown,\n options: CompileExpressionOptions<R>,\n): Result<CompiledExpression<R>> {\n if (!options || !(options.profile instanceof ExpressionProfile))\n return failure(\"EXPRESSION_INVALID_INPUT\", []);\n const canonical = canonicalizeExpression<R>(input, options);\n if (!canonical.ok) return canonical;\n const operators = new Map<ValueExpression<R>, ExpressionOperator>();\n const validation = validateOperators(canonical.value, options.profile, [], operators);\n if (!validation.ok) return validation;\n let limits: ExpressionLimits;\n try {\n limits = resolveLimits(options.limits);\n } catch {\n return failure(\"EXPRESSION_LIMIT_EXCEEDED\", []);\n }\n const expression = canonical.value;\n const dependencies = collectExpressionDependencies(expression);\n const compiled: CompiledExpression<R> = {\n expression,\n dependencies,\n evaluate: (resolve) => evaluateCompiled(expression, resolve, operators, limits),\n };\n return success(Object.freeze(compiled));\n}\n\nfunction validateOperators<R extends JsonValue>(\n node: ValueExpression<R>,\n profile: ExpressionProfile,\n path: ExpressionPath,\n output: Map<ValueExpression<R>, ExpressionOperator>,\n): Result<undefined> {\n if (node.kind !== \"op\") return success(undefined);\n const operator = profile.get(node.op);\n if (!operator) return failure(\"EXPRESSION_UNKNOWN_OPERATOR\", [...path, \"op\"]);\n if (!validArity(operator, node.args.length))\n return failure(\"EXPRESSION_INVALID_ARITY\", [...path, \"args\"]);\n output.set(node, operator);\n for (let index = 0; index < node.args.length; index += 1) {\n const result = validateOperators(node.args[index]!, profile, [...path, \"args\", index], output);\n if (!result.ok) return result;\n }\n return success(undefined);\n}\n\nfunction validArity(operator: ExpressionOperator, length: number): boolean {\n if (operator.arity !== undefined) return length === operator.arity;\n return (\n length >= (operator.minArgs ?? 0) &&\n length <= (operator.maxArgs ?? MAX_EXPRESSION_OPERATOR_ARGS)\n );\n}\n"]}
@@ -0,0 +1,10 @@
1
+ import { ExpressionProfile } from './profile.cjs';
2
+ import { JsonValue, CanonicalizeExpressionOptions, Result, CompiledExpression } from './types.cjs';
3
+
4
+ interface CompileExpressionOptions<R extends JsonValue> extends CanonicalizeExpressionOptions<R> {
5
+ readonly profile: ExpressionProfile;
6
+ }
7
+ /** Compile and validate a complete expression against one structurally immutable profile. */
8
+ declare function compileExpression<R extends JsonValue = string>(input: unknown, options: CompileExpressionOptions<R>): Result<CompiledExpression<R>>;
9
+
10
+ export { type CompileExpressionOptions, compileExpression };
@@ -0,0 +1,10 @@
1
+ import { ExpressionProfile } from './profile.js';
2
+ import { JsonValue, CanonicalizeExpressionOptions, Result, CompiledExpression } from './types.js';
3
+
4
+ interface CompileExpressionOptions<R extends JsonValue> extends CanonicalizeExpressionOptions<R> {
5
+ readonly profile: ExpressionProfile;
6
+ }
7
+ /** Compile and validate a complete expression against one structurally immutable profile. */
8
+ declare function compileExpression<R extends JsonValue = string>(input: unknown, options: CompileExpressionOptions<R>): Result<CompiledExpression<R>>;
9
+
10
+ export { type CompileExpressionOptions, compileExpression };
@@ -0,0 +1,51 @@
1
+ import { canonicalizeExpression } from './canonicalize.js';
2
+ import { collectExpressionDependencies } from './dependencies.js';
3
+ import { evaluateCompiled } from './evaluate.js';
4
+ import { resolveLimits } from './limits.js';
5
+ import { ExpressionProfile, MAX_EXPRESSION_OPERATOR_ARGS } from './profile.js';
6
+ import { failure, success } from './result.js';
7
+
8
+ function compileExpression(input, options) {
9
+ if (!options || !(options.profile instanceof ExpressionProfile))
10
+ return failure("EXPRESSION_INVALID_INPUT", []);
11
+ const canonical = canonicalizeExpression(input, options);
12
+ if (!canonical.ok) return canonical;
13
+ const operators = /* @__PURE__ */ new Map();
14
+ const validation = validateOperators(canonical.value, options.profile, [], operators);
15
+ if (!validation.ok) return validation;
16
+ let limits;
17
+ try {
18
+ limits = resolveLimits(options.limits);
19
+ } catch {
20
+ return failure("EXPRESSION_LIMIT_EXCEEDED", []);
21
+ }
22
+ const expression = canonical.value;
23
+ const dependencies = collectExpressionDependencies(expression);
24
+ const compiled = {
25
+ expression,
26
+ dependencies,
27
+ evaluate: (resolve) => evaluateCompiled(expression, resolve, operators, limits)
28
+ };
29
+ return success(Object.freeze(compiled));
30
+ }
31
+ function validateOperators(node, profile, path, output) {
32
+ if (node.kind !== "op") return success(void 0);
33
+ const operator = profile.get(node.op);
34
+ if (!operator) return failure("EXPRESSION_UNKNOWN_OPERATOR", [...path, "op"]);
35
+ if (!validArity(operator, node.args.length))
36
+ return failure("EXPRESSION_INVALID_ARITY", [...path, "args"]);
37
+ output.set(node, operator);
38
+ for (let index = 0; index < node.args.length; index += 1) {
39
+ const result = validateOperators(node.args[index], profile, [...path, "args", index], output);
40
+ if (!result.ok) return result;
41
+ }
42
+ return success(void 0);
43
+ }
44
+ function validArity(operator, length) {
45
+ if (operator.arity !== void 0) return length === operator.arity;
46
+ return length >= (operator.minArgs ?? 0) && length <= (operator.maxArgs ?? MAX_EXPRESSION_OPERATOR_ARGS);
47
+ }
48
+
49
+ export { compileExpression };
50
+ //# sourceMappingURL=compile.js.map
51
+ //# sourceMappingURL=compile.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/kuery-v1/compile.ts"],"names":[],"mappings":";;;;;;;AA0BO,SAAS,iB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,CAAI,IAAA,CAAK,EAAE,CAAA;AACpC,EAAA,IAAI,CAAC,UAAU,OAAO,OAAA,CAAQ,+BAA+B,CAAC,GAAG,IAAA,EAAM,IAAI,CAAC,CAAA;AAC5E,EAAA,IAAI,CAAC,UAAA,CAAW,QAAA,EAAU,IAAA,CAAK,KAAK,MAAM,CAAA;AACxC,IAAA,OAAO,QAAQ,0BAAA,EAA4B,CAAC,GAAG,IAAA,EAAM,MAAM,CAAC,CAAA;AAC9D,EAAA,MAAA,CAAO,GAAA,CAAI,MAAM,QAAQ,CAAA;AACzB,EAAA,KAAA,IAAS,QAAQ,CAAA,EAAG,KAAA,GAAQ,KAAK,IAAA,CAAK,MAAA,EAAQ,SAAS,CAAA,EAAG;AACxD,IAAA,MAAM,MAAA,GAAS,iBAAA,CAAkB,IAAA,CAAK,IAAA,CAAK,KAAK,CAAA,EAAI,OAAA,EAAS,CAAC,GAAG,IAAA,EAAM,MAAA,EAAQ,KAAK,GAAG,MAAM,CAAA;AAC7F,IAAA,IAAI,CAAC,MAAA,CAAO,EAAA,EAAI,OAAO,MAAA;AAAA,EACzB;AACA,EAAA,OAAO,QAAQ,MAAS,CAAA;AAC1B;AAEA,SAAS,UAAA,CAAW,UAA8B,MAAA,EAAyB;AACzE,EAAA,IAAI,QAAA,CAAS,KAAA,KAAU,MAAA,EAAW,OAAO,WAAW,QAAA,CAAS,KAAA;AAC7D,EAAA,OACE,WAAW,QAAA,CAAS,OAAA,IAAW,CAAA,CAAA,IAC/B,MAAA,KAAW,SAAS,OAAA,IAAW,4BAAA,CAAA;AAEnC","file":"compile.js","sourcesContent":["import { canonicalizeExpression } from \"./canonicalize.js\";\nimport { collectExpressionDependencies } from \"./dependencies.js\";\nimport { evaluateCompiled } from \"./evaluate.js\";\nimport { resolveLimits } from \"./limits.js\";\nimport {\n type ExpressionOperator,\n ExpressionProfile,\n MAX_EXPRESSION_OPERATOR_ARGS,\n} from \"./profile.js\";\nimport { failure, success } from \"./result.js\";\nimport type {\n CanonicalizeExpressionOptions,\n CompiledExpression,\n ExpressionLimits,\n ExpressionPath,\n JsonValue,\n Result,\n ValueExpression,\n} from \"./types.js\";\n\nexport interface CompileExpressionOptions<R extends JsonValue>\n extends CanonicalizeExpressionOptions<R> {\n readonly profile: ExpressionProfile;\n}\n\n/** Compile and validate a complete expression against one structurally immutable profile. */\nexport function compileExpression<R extends JsonValue = string>(\n input: unknown,\n options: CompileExpressionOptions<R>,\n): Result<CompiledExpression<R>> {\n if (!options || !(options.profile instanceof ExpressionProfile))\n return failure(\"EXPRESSION_INVALID_INPUT\", []);\n const canonical = canonicalizeExpression<R>(input, options);\n if (!canonical.ok) return canonical;\n const operators = new Map<ValueExpression<R>, ExpressionOperator>();\n const validation = validateOperators(canonical.value, options.profile, [], operators);\n if (!validation.ok) return validation;\n let limits: ExpressionLimits;\n try {\n limits = resolveLimits(options.limits);\n } catch {\n return failure(\"EXPRESSION_LIMIT_EXCEEDED\", []);\n }\n const expression = canonical.value;\n const dependencies = collectExpressionDependencies(expression);\n const compiled: CompiledExpression<R> = {\n expression,\n dependencies,\n evaluate: (resolve) => evaluateCompiled(expression, resolve, operators, limits),\n };\n return success(Object.freeze(compiled));\n}\n\nfunction validateOperators<R extends JsonValue>(\n node: ValueExpression<R>,\n profile: ExpressionProfile,\n path: ExpressionPath,\n output: Map<ValueExpression<R>, ExpressionOperator>,\n): Result<undefined> {\n if (node.kind !== \"op\") return success(undefined);\n const operator = profile.get(node.op);\n if (!operator) return failure(\"EXPRESSION_UNKNOWN_OPERATOR\", [...path, \"op\"]);\n if (!validArity(operator, node.args.length))\n return failure(\"EXPRESSION_INVALID_ARITY\", [...path, \"args\"]);\n output.set(node, operator);\n for (let index = 0; index < node.args.length; index += 1) {\n const result = validateOperators(node.args[index]!, profile, [...path, \"args\", index], output);\n if (!result.ok) return result;\n }\n return success(undefined);\n}\n\nfunction validArity(operator: ExpressionOperator, length: number): boolean {\n if (operator.arity !== undefined) return length === operator.arity;\n return (\n length >= (operator.minArgs ?? 0) &&\n length <= (operator.maxArgs ?? MAX_EXPRESSION_OPERATOR_ARGS)\n );\n}\n"]}
@@ -0,0 +1,32 @@
1
+ 'use strict';
2
+
3
+ var canonicalize_js = require('./canonicalize.js');
4
+ var result_js = require('./result.js');
5
+
6
+ function extractExpressionDependencies(input, options = {}) {
7
+ const canonical = canonicalize_js.canonicalizeExpression(input, options);
8
+ if (!canonical.ok) return canonical;
9
+ return result_js.success(collectExpressionDependencies(canonical.value));
10
+ }
11
+ function collectExpressionDependencies(expression) {
12
+ const dependencies = [];
13
+ const seen = /* @__PURE__ */ new Set();
14
+ const visit = (node) => {
15
+ if (node.kind === "ref") {
16
+ const identity = canonicalize_js.referenceIdentity(node.ref);
17
+ if (!seen.has(identity)) {
18
+ seen.add(identity);
19
+ dependencies.push(node.ref);
20
+ }
21
+ return;
22
+ }
23
+ if (node.kind === "op") node.args.forEach(visit);
24
+ };
25
+ visit(expression);
26
+ return Object.freeze(dependencies);
27
+ }
28
+
29
+ exports.collectExpressionDependencies = collectExpressionDependencies;
30
+ exports.extractExpressionDependencies = extractExpressionDependencies;
31
+ //# sourceMappingURL=dependencies.cjs.map
32
+ //# sourceMappingURL=dependencies.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/kuery-v1/dependencies.ts"],"names":["canonicalizeExpression","success","referenceIdentity"],"mappings":";;;;;AAKO,SAAS,6BAAA,CACd,KAAA,EACA,OAAA,GAA4C,EAAC,EACvB;AACtB,EAAA,MAAM,SAAA,GAAYA,sCAAA,CAA0B,KAAA,EAAO,OAAO,CAAA;AAC1D,EAAA,IAAI,CAAC,SAAA,CAAU,EAAA,EAAI,OAAO,SAAA;AAC1B,EAAA,OAAOC,iBAAA,CAAQ,6BAAA,CAA8B,SAAA,CAAU,KAAK,CAAC,CAAA;AAC/D;AAEO,SAAS,8BACd,UAAA,EACc;AACd,EAAA,MAAM,eAAoB,EAAC;AAC3B,EAAA,MAAM,IAAA,uBAAW,GAAA,EAAY;AAC7B,EAAA,MAAM,KAAA,GAAQ,CAAC,IAAA,KAAmC;AAChD,IAAA,IAAI,IAAA,CAAK,SAAS,KAAA,EAAO;AACvB,MAAA,MAAM,QAAA,GAAWC,iCAAA,CAAkB,IAAA,CAAK,GAAG,CAAA;AAC3C,MAAA,IAAI,CAAC,IAAA,CAAK,GAAA,CAAI,QAAQ,CAAA,EAAG;AACvB,QAAA,IAAA,CAAK,IAAI,QAAQ,CAAA;AACjB,QAAA,YAAA,CAAa,IAAA,CAAK,KAAK,GAAG,CAAA;AAAA,MAC5B;AACA,MAAA;AAAA,IACF;AACA,IAAA,IAAI,KAAK,IAAA,KAAS,IAAA,EAAM,IAAA,CAAK,IAAA,CAAK,QAAQ,KAAK,CAAA;AAAA,EACjD,CAAA;AACA,EAAA,KAAA,CAAM,UAAU,CAAA;AAChB,EAAA,OAAO,MAAA,CAAO,OAAO,YAAY,CAAA;AACnC","file":"dependencies.cjs","sourcesContent":["import { canonicalizeExpression, referenceIdentity } from \"./canonicalize.js\";\nimport { success } from \"./result.js\";\nimport type { CanonicalizeExpressionOptions, JsonValue, Result, ValueExpression } from \"./types.js\";\n\n/** Extract canonical references in first-seen AST order, deduplicated by structural JSON equality. */\nexport function extractExpressionDependencies<R extends JsonValue = string>(\n input: unknown,\n options: CanonicalizeExpressionOptions<R> = {},\n): Result<readonly R[]> {\n const canonical = canonicalizeExpression<R>(input, options);\n if (!canonical.ok) return canonical;\n return success(collectExpressionDependencies(canonical.value));\n}\n\nexport function collectExpressionDependencies<R extends JsonValue>(\n expression: ValueExpression<R>,\n): readonly R[] {\n const dependencies: R[] = [];\n const seen = new Set<string>();\n const visit = (node: ValueExpression<R>): void => {\n if (node.kind === \"ref\") {\n const identity = referenceIdentity(node.ref);\n if (!seen.has(identity)) {\n seen.add(identity);\n dependencies.push(node.ref);\n }\n return;\n }\n if (node.kind === \"op\") node.args.forEach(visit);\n };\n visit(expression);\n return Object.freeze(dependencies);\n}\n"]}
@@ -0,0 +1,7 @@
1
+ import { JsonValue, ValueExpression, CanonicalizeExpressionOptions, Result } from './types.cjs';
2
+
3
+ /** Extract canonical references in first-seen AST order, deduplicated by structural JSON equality. */
4
+ declare function extractExpressionDependencies<R extends JsonValue = string>(input: unknown, options?: CanonicalizeExpressionOptions<R>): Result<readonly R[]>;
5
+ declare function collectExpressionDependencies<R extends JsonValue>(expression: ValueExpression<R>): readonly R[];
6
+
7
+ export { collectExpressionDependencies, extractExpressionDependencies };
@@ -0,0 +1,7 @@
1
+ import { JsonValue, ValueExpression, CanonicalizeExpressionOptions, Result } from './types.js';
2
+
3
+ /** Extract canonical references in first-seen AST order, deduplicated by structural JSON equality. */
4
+ declare function extractExpressionDependencies<R extends JsonValue = string>(input: unknown, options?: CanonicalizeExpressionOptions<R>): Result<readonly R[]>;
5
+ declare function collectExpressionDependencies<R extends JsonValue>(expression: ValueExpression<R>): readonly R[];
6
+
7
+ export { collectExpressionDependencies, extractExpressionDependencies };
@@ -0,0 +1,29 @@
1
+ import { canonicalizeExpression, referenceIdentity } from './canonicalize.js';
2
+ import { success } from './result.js';
3
+
4
+ function extractExpressionDependencies(input, options = {}) {
5
+ const canonical = canonicalizeExpression(input, options);
6
+ if (!canonical.ok) return canonical;
7
+ return success(collectExpressionDependencies(canonical.value));
8
+ }
9
+ function collectExpressionDependencies(expression) {
10
+ const dependencies = [];
11
+ const seen = /* @__PURE__ */ new Set();
12
+ const visit = (node) => {
13
+ if (node.kind === "ref") {
14
+ const identity = referenceIdentity(node.ref);
15
+ if (!seen.has(identity)) {
16
+ seen.add(identity);
17
+ dependencies.push(node.ref);
18
+ }
19
+ return;
20
+ }
21
+ if (node.kind === "op") node.args.forEach(visit);
22
+ };
23
+ visit(expression);
24
+ return Object.freeze(dependencies);
25
+ }
26
+
27
+ export { collectExpressionDependencies, extractExpressionDependencies };
28
+ //# sourceMappingURL=dependencies.js.map
29
+ //# sourceMappingURL=dependencies.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/kuery-v1/dependencies.ts"],"names":[],"mappings":";;;AAKO,SAAS,6BAAA,CACd,KAAA,EACA,OAAA,GAA4C,EAAC,EACvB;AACtB,EAAA,MAAM,SAAA,GAAY,sBAAA,CAA0B,KAAA,EAAO,OAAO,CAAA;AAC1D,EAAA,IAAI,CAAC,SAAA,CAAU,EAAA,EAAI,OAAO,SAAA;AAC1B,EAAA,OAAO,OAAA,CAAQ,6BAAA,CAA8B,SAAA,CAAU,KAAK,CAAC,CAAA;AAC/D;AAEO,SAAS,8BACd,UAAA,EACc;AACd,EAAA,MAAM,eAAoB,EAAC;AAC3B,EAAA,MAAM,IAAA,uBAAW,GAAA,EAAY;AAC7B,EAAA,MAAM,KAAA,GAAQ,CAAC,IAAA,KAAmC;AAChD,IAAA,IAAI,IAAA,CAAK,SAAS,KAAA,EAAO;AACvB,MAAA,MAAM,QAAA,GAAW,iBAAA,CAAkB,IAAA,CAAK,GAAG,CAAA;AAC3C,MAAA,IAAI,CAAC,IAAA,CAAK,GAAA,CAAI,QAAQ,CAAA,EAAG;AACvB,QAAA,IAAA,CAAK,IAAI,QAAQ,CAAA;AACjB,QAAA,YAAA,CAAa,IAAA,CAAK,KAAK,GAAG,CAAA;AAAA,MAC5B;AACA,MAAA;AAAA,IACF;AACA,IAAA,IAAI,KAAK,IAAA,KAAS,IAAA,EAAM,IAAA,CAAK,IAAA,CAAK,QAAQ,KAAK,CAAA;AAAA,EACjD,CAAA;AACA,EAAA,KAAA,CAAM,UAAU,CAAA;AAChB,EAAA,OAAO,MAAA,CAAO,OAAO,YAAY,CAAA;AACnC","file":"dependencies.js","sourcesContent":["import { canonicalizeExpression, referenceIdentity } from \"./canonicalize.js\";\nimport { success } from \"./result.js\";\nimport type { CanonicalizeExpressionOptions, JsonValue, Result, ValueExpression } from \"./types.js\";\n\n/** Extract canonical references in first-seen AST order, deduplicated by structural JSON equality. */\nexport function extractExpressionDependencies<R extends JsonValue = string>(\n input: unknown,\n options: CanonicalizeExpressionOptions<R> = {},\n): Result<readonly R[]> {\n const canonical = canonicalizeExpression<R>(input, options);\n if (!canonical.ok) return canonical;\n return success(collectExpressionDependencies(canonical.value));\n}\n\nexport function collectExpressionDependencies<R extends JsonValue>(\n expression: ValueExpression<R>,\n): readonly R[] {\n const dependencies: R[] = [];\n const seen = new Set<string>();\n const visit = (node: ValueExpression<R>): void => {\n if (node.kind === \"ref\") {\n const identity = referenceIdentity(node.ref);\n if (!seen.has(identity)) {\n seen.add(identity);\n dependencies.push(node.ref);\n }\n return;\n }\n if (node.kind === \"op\") node.args.forEach(visit);\n };\n visit(expression);\n return Object.freeze(dependencies);\n}\n"]}
@@ -0,0 +1,174 @@
1
+ 'use strict';
2
+
3
+ var callbackPromise_js = require('./callback-promise.js');
4
+ var inspect_js = require('./inspect.js');
5
+ var profile_js = require('./profile.js');
6
+ var result_js = require('./result.js');
7
+
8
+ function evaluateCompiled(expression, resolve, operators, limits) {
9
+ const state = { resolve, operators, limits, steps: 0 };
10
+ try {
11
+ const outcome = evaluateNode(expression, [], state);
12
+ if (!outcome.found) {
13
+ return result_js.failure(
14
+ outcome.denied ? "EXPRESSION_REFERENCE_DENIED" : "EXPRESSION_REFERENCE_MISSING",
15
+ outcome.path ?? []
16
+ );
17
+ }
18
+ return result_js.success(outcome.value);
19
+ } catch (error) {
20
+ const problem = error instanceof result_js.ExpressionFailure ? error : new result_js.ExpressionFailure("EXPRESSION_OPERATOR_ERROR", []);
21
+ return result_js.failure(problem.code, problem.expressionPath);
22
+ }
23
+ }
24
+ function evaluateNode(node, path, state) {
25
+ state.steps += 1;
26
+ if (state.steps > state.limits.maxEvaluationSteps) {
27
+ throw new result_js.ExpressionFailure("EXPRESSION_EVALUATION_LIMIT", path);
28
+ }
29
+ if (node.kind === "literal") return { found: true, value: node.value };
30
+ if (node.kind === "ref") return resolveReference(node.ref, path, state.resolve, state.limits);
31
+ const operator = state.operators.get(node);
32
+ if (!operator) throw new result_js.ExpressionFailure("EXPRESSION_UNKNOWN_OPERATOR", [...path, "op"]);
33
+ return evaluateOperator(node, operator, path, state);
34
+ }
35
+ function resolveReference(reference, path, resolve, limits) {
36
+ try {
37
+ const result = resolve(reference);
38
+ if (callbackPromise_js.rejectCallbackPromise(result))
39
+ throw new result_js.ExpressionFailure("EXPRESSION_ASYNC_UNSUPPORTED", path);
40
+ const parsed = parseResolution(result);
41
+ if (!parsed.valid) throw new result_js.ExpressionFailure("EXPRESSION_REFERENCE_ERROR", path);
42
+ if (!parsed.found) return { ...parsed, path };
43
+ return { found: true, value: safeCallbackValue(parsed.value, path, limits) };
44
+ } catch (error) {
45
+ if (error instanceof result_js.ExpressionFailure) throw error;
46
+ throw new result_js.ExpressionFailure("EXPRESSION_REFERENCE_ERROR", path);
47
+ }
48
+ }
49
+ function parseResolution(input) {
50
+ if (typeof input !== "object" || input === null) return { valid: false };
51
+ try {
52
+ const prototype = Object.getPrototypeOf(input);
53
+ if (prototype !== Object.prototype && prototype !== null) return { valid: false };
54
+ const found = Object.getOwnPropertyDescriptor(input, "found");
55
+ if (!found || !("value" in found) || !found.enumerable || typeof found.value !== "boolean")
56
+ return { valid: false };
57
+ const keys = Reflect.ownKeys(input);
58
+ if (!found.value) return parseMissingResolution(input, keys);
59
+ const value = Object.getOwnPropertyDescriptor(input, "value");
60
+ if (keys.length !== 2 || !keys.every((key) => key === "found" || key === "value") || !value || !("value" in value) || !value.enumerable) {
61
+ return { valid: false };
62
+ }
63
+ return { valid: true, found: true, value: value.value };
64
+ } catch {
65
+ return { valid: false };
66
+ }
67
+ }
68
+ function parseMissingResolution(input, keys) {
69
+ if (keys.length === 1 && keys[0] === "found") return { valid: true, found: false };
70
+ if (keys.length !== 2 || !keys.includes("found") || !keys.includes("reason"))
71
+ return { valid: false };
72
+ const reason = Object.getOwnPropertyDescriptor(input, "reason");
73
+ if (!reason || !("value" in reason) || !reason.enumerable) return { valid: false };
74
+ if (reason.value === void 0 || reason.value === "missing")
75
+ return { valid: true, found: false };
76
+ if (reason.value === "denied") return { valid: true, found: false, denied: true };
77
+ return { valid: false };
78
+ }
79
+ function evaluateOperator(node, operator, path, state) {
80
+ const strategy = profile_js.internalProfile.getEvaluationStrategy(operator);
81
+ if (strategy === "exists") return evaluateExists(node.args[0], [...path, "args", 0], state);
82
+ if (strategy === "and" || strategy === "or") return evaluateBoolean(node, strategy, path, state);
83
+ if (strategy === "coalesce") return evaluateCoalesce(node, path, state);
84
+ if (strategy === "if") return evaluateIf(node, path, state);
85
+ const args = [];
86
+ for (let index = 0; index < node.args.length; index += 1) {
87
+ const argPath = [...path, "args", index];
88
+ const outcome = evaluateNode(node.args[index], argPath, state);
89
+ if (!outcome.found) return outcome;
90
+ args.push(outcome.value);
91
+ }
92
+ return { found: true, value: execute(operator, args, path, state.limits) };
93
+ }
94
+ function evaluateExists(argument, path, state) {
95
+ const outcome = evaluateNode(argument, path, state);
96
+ if (outcome.denied)
97
+ throw new result_js.ExpressionFailure("EXPRESSION_REFERENCE_DENIED", outcome.path ?? path);
98
+ return { found: true, value: outcome.found };
99
+ }
100
+ function evaluateBoolean(node, strategy, path, state) {
101
+ const expected = strategy === "and";
102
+ for (let index = 0; index < node.args.length; index += 1) {
103
+ const argPath = [...path, "args", index];
104
+ const outcome = evaluateNode(node.args[index], argPath, state);
105
+ if (!outcome.found) return outcome;
106
+ const value = outcome.value;
107
+ assertType(value, "boolean", argPath);
108
+ if (value !== expected) return { found: true, value: !expected };
109
+ }
110
+ return { found: true, value: expected };
111
+ }
112
+ function evaluateIf(node, path, state) {
113
+ const conditionPath = [...path, "args", 0];
114
+ const condition = evaluateNode(node.args[0], conditionPath, state);
115
+ if (!condition.found) return condition;
116
+ assertType(condition.value, "boolean", conditionPath);
117
+ const selected = condition.value ? 1 : 2;
118
+ return evaluateNode(node.args[selected], [...path, "args", selected], state);
119
+ }
120
+ function evaluateCoalesce(node, path, state) {
121
+ for (let index = 0; index < node.args.length; index += 1) {
122
+ const argPath = [...path, "args", index];
123
+ const outcome = evaluateNode(node.args[index], argPath, state);
124
+ if (outcome.denied)
125
+ throw new result_js.ExpressionFailure("EXPRESSION_REFERENCE_DENIED", outcome.path ?? argPath);
126
+ if (outcome.found && outcome.value !== null) return outcome;
127
+ }
128
+ return { found: true, value: null };
129
+ }
130
+ function execute(operator, args, path, limits) {
131
+ validateInputTypes(operator, args, path);
132
+ try {
133
+ const result = operator.execute(Object.freeze([...args]));
134
+ if (callbackPromise_js.rejectCallbackPromise(result))
135
+ throw new result_js.ExpressionFailure("EXPRESSION_ASYNC_UNSUPPORTED", path);
136
+ const value = safeCallbackValue(result, path, limits);
137
+ if (operator.resultType) assertType(value, operator.resultType, path);
138
+ return value;
139
+ } catch (error) {
140
+ if (error instanceof result_js.ExpressionFailure) throw new result_js.ExpressionFailure(error.code, path);
141
+ throw new result_js.ExpressionFailure("EXPRESSION_OPERATOR_ERROR", path);
142
+ }
143
+ }
144
+ function validateInputTypes(operator, args, path) {
145
+ if (!operator.inputTypes) return;
146
+ args.forEach((value, index) => {
147
+ const expected = operator.inputTypes[index] ?? operator.inputTypes[0];
148
+ if (expected) assertType(value, expected, [...path, "args", index]);
149
+ });
150
+ }
151
+ function assertType(value, expected, path) {
152
+ if (expected === "any") return;
153
+ const actual = value === null ? "null" : Array.isArray(value) ? "array" : typeof value;
154
+ if (actual !== expected) throw new result_js.ExpressionFailure("EXPRESSION_TYPE_MISMATCH", path);
155
+ }
156
+ function safeCallbackValue(input, path, limits) {
157
+ try {
158
+ return inspect_js.cloneJson(input, path, 0, {
159
+ maxDepth: limits.maxDepth,
160
+ maxNodes: limits.maxNodes,
161
+ maxStringLength: limits.maxStringLength,
162
+ nodes: 0,
163
+ active: /* @__PURE__ */ new WeakSet()
164
+ });
165
+ } catch (error) {
166
+ if (error instanceof result_js.ExpressionFailure && error.code === "EXPRESSION_LIMIT_EXCEEDED")
167
+ throw error;
168
+ throw new result_js.ExpressionFailure("EXPRESSION_INVALID_RESULT", path);
169
+ }
170
+ }
171
+
172
+ exports.evaluateCompiled = evaluateCompiled;
173
+ //# sourceMappingURL=evaluate.cjs.map
174
+ //# sourceMappingURL=evaluate.cjs.map