@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.
- package/README.md +10 -0
- package/THIRD_PARTY_NOTICES.md +42 -0
- package/dist/index.cjs +20 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +2 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -0
- package/dist/kuery-v1/callback-promise.cjs +79 -0
- package/dist/kuery-v1/callback-promise.cjs.map +1 -0
- package/dist/kuery-v1/callback-promise.d.cts +4 -0
- package/dist/kuery-v1/callback-promise.d.ts +4 -0
- package/dist/kuery-v1/callback-promise.js +77 -0
- package/dist/kuery-v1/callback-promise.js.map +1 -0
- package/dist/kuery-v1/canonicalize.cjs +181 -0
- package/dist/kuery-v1/canonicalize.cjs.map +1 -0
- package/dist/kuery-v1/canonicalize.d.cts +7 -0
- package/dist/kuery-v1/canonicalize.d.ts +7 -0
- package/dist/kuery-v1/canonicalize.js +178 -0
- package/dist/kuery-v1/canonicalize.js.map +1 -0
- package/dist/kuery-v1/compile.cjs +53 -0
- package/dist/kuery-v1/compile.cjs.map +1 -0
- package/dist/kuery-v1/compile.d.cts +10 -0
- package/dist/kuery-v1/compile.d.ts +10 -0
- package/dist/kuery-v1/compile.js +51 -0
- package/dist/kuery-v1/compile.js.map +1 -0
- package/dist/kuery-v1/dependencies.cjs +32 -0
- package/dist/kuery-v1/dependencies.cjs.map +1 -0
- package/dist/kuery-v1/dependencies.d.cts +7 -0
- package/dist/kuery-v1/dependencies.d.ts +7 -0
- package/dist/kuery-v1/dependencies.js +29 -0
- package/dist/kuery-v1/dependencies.js.map +1 -0
- package/dist/kuery-v1/evaluate.cjs +174 -0
- package/dist/kuery-v1/evaluate.cjs.map +1 -0
- package/dist/kuery-v1/evaluate.d.cts +6 -0
- package/dist/kuery-v1/evaluate.d.ts +6 -0
- package/dist/kuery-v1/evaluate.js +172 -0
- package/dist/kuery-v1/evaluate.js.map +1 -0
- package/dist/kuery-v1/index.cjs +54 -0
- package/dist/kuery-v1/index.cjs.map +1 -0
- package/dist/kuery-v1/index.d.cts +8 -0
- package/dist/kuery-v1/index.d.ts +8 -0
- package/dist/kuery-v1/index.js +9 -0
- package/dist/kuery-v1/index.js.map +1 -0
- package/dist/kuery-v1/inspect.cjs +167 -0
- package/dist/kuery-v1/inspect.cjs.map +1 -0
- package/dist/kuery-v1/inspect.d.cts +17 -0
- package/dist/kuery-v1/inspect.d.ts +17 -0
- package/dist/kuery-v1/inspect.js +160 -0
- package/dist/kuery-v1/inspect.js.map +1 -0
- package/dist/kuery-v1/json-schema.cjs +108 -0
- package/dist/kuery-v1/json-schema.cjs.map +1 -0
- package/dist/kuery-v1/json-schema.d.cts +10 -0
- package/dist/kuery-v1/json-schema.d.ts +10 -0
- package/dist/kuery-v1/json-schema.js +105 -0
- package/dist/kuery-v1/json-schema.js.map +1 -0
- package/dist/kuery-v1/limits.cjs +35 -0
- package/dist/kuery-v1/limits.cjs.map +1 -0
- package/dist/kuery-v1/limits.d.cts +6 -0
- package/dist/kuery-v1/limits.d.ts +6 -0
- package/dist/kuery-v1/limits.js +32 -0
- package/dist/kuery-v1/limits.js.map +1 -0
- package/dist/kuery-v1/profile.cjs +224 -0
- package/dist/kuery-v1/profile.cjs.map +1 -0
- package/dist/kuery-v1/profile.d.cts +48 -0
- package/dist/kuery-v1/profile.d.ts +48 -0
- package/dist/kuery-v1/profile.js +219 -0
- package/dist/kuery-v1/profile.js.map +1 -0
- package/dist/kuery-v1/result.cjs +44 -0
- package/dist/kuery-v1/result.cjs.map +1 -0
- package/dist/kuery-v1/result.d.cts +12 -0
- package/dist/kuery-v1/result.d.ts +12 -0
- package/dist/kuery-v1/result.js +39 -0
- package/dist/kuery-v1/result.js.map +1 -0
- package/dist/kuery-v1/standard-profile.cjs +173 -0
- package/dist/kuery-v1/standard-profile.cjs.map +1 -0
- package/dist/kuery-v1/standard-profile.d.cts +7 -0
- package/dist/kuery-v1/standard-profile.d.ts +7 -0
- package/dist/kuery-v1/standard-profile.js +171 -0
- package/dist/kuery-v1/standard-profile.js.map +1 -0
- package/dist/kuery-v1/types.cjs +4 -0
- package/dist/kuery-v1/types.cjs.map +1 -0
- package/dist/kuery-v1/types.d.cts +65 -0
- package/dist/kuery-v1/types.d.ts +65 -0
- package/dist/kuery-v1/types.js +3 -0
- package/dist/kuery-v1/types.js.map +1 -0
- package/dist/program-v1.cjs +72 -0
- package/dist/program-v1.cjs.map +1 -0
- package/dist/program-v1.d.cts +17 -0
- package/dist/program-v1.d.ts +17 -0
- package/dist/program-v1.js +68 -0
- package/dist/program-v1.js.map +1 -0
- package/package.json +51 -0
- package/provenance/kuery-2.1.0.json +93 -0
package/README.md
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# `@kalada/core`
|
|
2
|
+
|
|
3
|
+
This dependency-free package owns Kalada's canonical, versioned runtime contracts. The root
|
|
4
|
+
exports the four-field `KaladaProgramV1` envelope, its strict canonicalizer, and Kuery-expression
|
|
5
|
+
adapters. It intentionally does not export a compiler, evaluator, profile, parser, effects, or
|
|
6
|
+
scheduling semantics.
|
|
7
|
+
|
|
8
|
+
The exact Kuery 2.1 strict-expression compatibility API is available only from
|
|
9
|
+
`@kalada/core/kuery-v1`. Provenance is recorded in the shipped `provenance/kuery-2.1.0.json`, and
|
|
10
|
+
third-party license declarations are reproduced in `THIRD_PARTY_NOTICES.md`.
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# Third-party notices
|
|
2
|
+
|
|
3
|
+
`@kalada/core/kuery-v1` contains code derived from Kuery 2.1.0. Kuery's package metadata declares
|
|
4
|
+
the ISC license, while the release repository's `LICENSE` file contains the MIT text below.
|
|
5
|
+
Because these two upstream declarations differ, Kalada conservatively reproduces both notices.
|
|
6
|
+
This does not alter Kalada's own MIT license or package metadata.
|
|
7
|
+
|
|
8
|
+
## Kuery package metadata — ISC
|
|
9
|
+
|
|
10
|
+
Copyright (c) 2015 surikaterna
|
|
11
|
+
|
|
12
|
+
Permission to use, copy, modify, and/or distribute this software for any purpose with or without
|
|
13
|
+
fee is hereby granted, provided that the above copyright notice and this permission notice appear
|
|
14
|
+
in all copies.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
|
|
17
|
+
SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
19
|
+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
|
|
20
|
+
NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
|
|
21
|
+
OF THIS SOFTWARE.
|
|
22
|
+
|
|
23
|
+
## Kuery release repository `LICENSE` — MIT
|
|
24
|
+
|
|
25
|
+
The MIT License (MIT)
|
|
26
|
+
|
|
27
|
+
Copyright (c) 2015 surikaterna
|
|
28
|
+
|
|
29
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
|
|
30
|
+
associated documentation files (the "Software"), to deal in the Software without restriction,
|
|
31
|
+
including without limitation the rights to use, copy, modify, merge, publish, distribute,
|
|
32
|
+
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
|
|
33
|
+
furnished to do so, subject to the following conditions:
|
|
34
|
+
|
|
35
|
+
The above copyright notice and this permission notice shall be included in all copies or
|
|
36
|
+
substantial portions of the Software.
|
|
37
|
+
|
|
38
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
|
|
39
|
+
BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
40
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
41
|
+
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
42
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var programV1_js = require('./program-v1.js');
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
Object.defineProperty(exports, "canonicalizeKaladaProgramV1", {
|
|
8
|
+
enumerable: true,
|
|
9
|
+
get: function () { return programV1_js.canonicalizeKaladaProgramV1; }
|
|
10
|
+
});
|
|
11
|
+
Object.defineProperty(exports, "fromKueryExpression", {
|
|
12
|
+
enumerable: true,
|
|
13
|
+
get: function () { return programV1_js.fromKueryExpression; }
|
|
14
|
+
});
|
|
15
|
+
Object.defineProperty(exports, "toKueryExpression", {
|
|
16
|
+
enumerable: true,
|
|
17
|
+
get: function () { return programV1_js.toKueryExpression; }
|
|
18
|
+
});
|
|
19
|
+
//# sourceMappingURL=index.cjs.map
|
|
20
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":"","file":"index.cjs","sourcesContent":[]}
|
package/dist/index.d.cts
ADDED
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":"","file":"index.js","sourcesContent":[]}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const apply = Reflect.apply;
|
|
4
|
+
const getDescriptor = Object.getOwnPropertyDescriptor;
|
|
5
|
+
const getPrototype = Object.getPrototypeOf;
|
|
6
|
+
const functionToString = Function.prototype.toString;
|
|
7
|
+
const promiseThen = Promise.prototype.then;
|
|
8
|
+
const promiseSource = apply(functionToString, Promise, []);
|
|
9
|
+
const thenSource = apply(functionToString, promiseThen, []);
|
|
10
|
+
const speciesGetter = getDescriptor(Promise, Symbol.species)?.get;
|
|
11
|
+
const speciesSource = speciesGetter ? apply(functionToString, speciesGetter, []) : "";
|
|
12
|
+
function rejectCallbackPromise(value) {
|
|
13
|
+
const shape = promiseShape(value);
|
|
14
|
+
if (shape === "none") return false;
|
|
15
|
+
if (shape === "safe") consumeRejection(value);
|
|
16
|
+
return true;
|
|
17
|
+
}
|
|
18
|
+
function promiseShape(value) {
|
|
19
|
+
if (typeof value !== "object" || value === null) return "none";
|
|
20
|
+
let prototype;
|
|
21
|
+
try {
|
|
22
|
+
prototype = getPrototype(value);
|
|
23
|
+
} catch {
|
|
24
|
+
return "none";
|
|
25
|
+
}
|
|
26
|
+
for (let depth = 0; prototype && depth < 4; depth += 1) {
|
|
27
|
+
const prototypeShape = inspectPromisePrototype(prototype);
|
|
28
|
+
if (prototypeShape !== "none") return inspectInstance(value, depth, prototypeShape);
|
|
29
|
+
try {
|
|
30
|
+
prototype = getPrototype(prototype);
|
|
31
|
+
} catch {
|
|
32
|
+
return "none";
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return "none";
|
|
36
|
+
}
|
|
37
|
+
function inspectPromisePrototype(prototype) {
|
|
38
|
+
try {
|
|
39
|
+
const then = getDescriptor(prototype, "then");
|
|
40
|
+
if (!isNativeFunction(then, thenSource)) return "none";
|
|
41
|
+
const constructorDescriptor = getDescriptor(prototype, "constructor");
|
|
42
|
+
if (!constructorDescriptor || !("value" in constructorDescriptor) || typeof constructorDescriptor.value !== "function")
|
|
43
|
+
return "suspicious";
|
|
44
|
+
const ownPrototype = getDescriptor(constructorDescriptor.value, "prototype");
|
|
45
|
+
if (!ownPrototype || !("value" in ownPrototype) || ownPrototype.value !== prototype)
|
|
46
|
+
return "suspicious";
|
|
47
|
+
const species = getDescriptor(constructorDescriptor.value, Symbol.species);
|
|
48
|
+
if (!species?.get || species.set !== void 0) return "suspicious";
|
|
49
|
+
const constructorNative = apply(functionToString, constructorDescriptor.value, []) === promiseSource;
|
|
50
|
+
const speciesNative = apply(functionToString, species.get, []) === speciesSource;
|
|
51
|
+
return constructorNative && speciesNative ? "safe" : "suspicious";
|
|
52
|
+
} catch {
|
|
53
|
+
return "suspicious";
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
function isNativeFunction(descriptor, source) {
|
|
57
|
+
return Boolean(
|
|
58
|
+
descriptor && "value" in descriptor && typeof descriptor.value === "function" && apply(functionToString, descriptor.value, []) === source
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
function inspectInstance(value, depth, prototypeShape) {
|
|
62
|
+
if (depth !== 0 || prototypeShape !== "safe") return "suspicious";
|
|
63
|
+
try {
|
|
64
|
+
const hasOverrides = getDescriptor(value, "constructor") !== void 0 || getDescriptor(value, "then") !== void 0 || getDescriptor(value, Symbol.species) !== void 0;
|
|
65
|
+
return hasOverrides ? "suspicious" : "safe";
|
|
66
|
+
} catch {
|
|
67
|
+
return "suspicious";
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
function consumeRejection(value) {
|
|
71
|
+
try {
|
|
72
|
+
void apply(promiseThen, value, [void 0, () => void 0]);
|
|
73
|
+
} catch {
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
exports.rejectCallbackPromise = rejectCallbackPromise;
|
|
78
|
+
//# sourceMappingURL=callback-promise.cjs.map
|
|
79
|
+
//# sourceMappingURL=callback-promise.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/kuery-v1/callback-promise.ts"],"names":[],"mappings":";;AAAA,MAAM,QAAQ,OAAA,CAAQ,KAAA;AACtB,MAAM,gBAAgB,MAAA,CAA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SAAS,gBAAA,CAAiB,YAA4C,MAAA,EAAyB;AAC7F,EAAA,OAAO,OAAA;AAAA,IACL,UAAA,IACE,OAAA,IAAW,UAAA,IACX,OAAO,UAAA,CAAW,KAAA,KAAU,UAAA,IAC5B,KAAA,CAAM,gBAAA,EAAkB,UAAA,CAAW,KAAA,EAAO,EAAE,CAAA,KAAM;AAAA,GACtD;AACF;AAEA,SAAS,eAAA,CAAgB,KAAA,EAAe,KAAA,EAAe,cAAA,EAA4C;AACjG,EAAA,IAAI,KAAA,KAAU,CAAA,IAAK,cAAA,KAAmB,MAAA,EAAQ,OAAO,YAAA;AACrD,EAAA,IAAI;AACF,IAAA,MAAM,YAAA,GACJ,aAAA,CAAc,KAAA,EAAO,aAAa,MAAM,KAAA,CAAA,IACxC,aAAA,CAAc,KAAA,EAAO,MAAM,MAAM,KAAA,CAAA,IACjC,aAAA,CAAc,KAAA,EAAO,MAAA,CAAO,OAAO,CAAA,KAAM,KAAA,CAAA;AAC3C,IAAA,OAAO,eAAe,YAAA,GAAe,MAAA;AAAA,EACvC,CAAA,CAAA,MAAQ;AACN,IAAA,OAAO,YAAA;AAAA,EACT;AACF;AAEA,SAAS,iBAAiB,KAAA,EAAqB;AAC7C,EAAA,IAAI;AACF,IAAA,KAAK,MAAM,WAAA,EAAa,KAAA,EAAO,CAAC,KAAA,CAAA,EAAW,MAAM,MAAS,CAAC,CAAA;AAAA,EAC7D,CAAA,CAAA,MAAQ;AAAA,EAER;AACF","file":"callback-promise.cjs","sourcesContent":["const apply = Reflect.apply;\nconst getDescriptor = Object.getOwnPropertyDescriptor;\nconst getPrototype = Object.getPrototypeOf;\nconst functionToString = Function.prototype.toString;\nconst promiseThen = Promise.prototype.then;\nconst promiseSource = apply(functionToString, Promise, []);\nconst thenSource = apply(functionToString, promiseThen, []);\nconst speciesGetter = getDescriptor(Promise, Symbol.species)?.get;\nconst speciesSource = speciesGetter ? apply(functionToString, speciesGetter, []) : \"\";\n\ntype PromiseShape = \"safe\" | \"suspicious\" | \"none\";\n\n/** Detect trusted native Promises without reading user-controlled properties. */\nexport function rejectCallbackPromise(value: unknown): boolean {\n const shape = promiseShape(value);\n if (shape === \"none\") return false;\n if (shape === \"safe\") consumeRejection(value as object);\n return true;\n}\n\nfunction promiseShape(value: unknown): PromiseShape {\n if (typeof value !== \"object\" || value === null) return \"none\";\n let prototype: object | null;\n try {\n prototype = getPrototype(value);\n } catch {\n return \"none\";\n }\n for (let depth = 0; prototype && depth < 4; depth += 1) {\n const prototypeShape = inspectPromisePrototype(prototype);\n if (prototypeShape !== \"none\") return inspectInstance(value, depth, prototypeShape);\n try {\n prototype = getPrototype(prototype);\n } catch {\n return \"none\";\n }\n }\n return \"none\";\n}\n\nfunction inspectPromisePrototype(prototype: object): PromiseShape {\n try {\n const then = getDescriptor(prototype, \"then\");\n if (!isNativeFunction(then, thenSource)) return \"none\";\n const constructorDescriptor = getDescriptor(prototype, \"constructor\");\n if (\n !constructorDescriptor ||\n !(\"value\" in constructorDescriptor) ||\n typeof constructorDescriptor.value !== \"function\"\n )\n return \"suspicious\";\n const ownPrototype = getDescriptor(constructorDescriptor.value, \"prototype\");\n if (!ownPrototype || !(\"value\" in ownPrototype) || ownPrototype.value !== prototype)\n return \"suspicious\";\n const species = getDescriptor(constructorDescriptor.value, Symbol.species);\n if (!species?.get || species.set !== undefined) return \"suspicious\";\n const constructorNative =\n apply(functionToString, constructorDescriptor.value, []) === promiseSource;\n const speciesNative = apply(functionToString, species.get, []) === speciesSource;\n return constructorNative && speciesNative ? \"safe\" : \"suspicious\";\n } catch {\n return \"suspicious\";\n }\n}\n\nfunction isNativeFunction(descriptor: PropertyDescriptor | undefined, source: string): boolean {\n return Boolean(\n descriptor &&\n \"value\" in descriptor &&\n typeof descriptor.value === \"function\" &&\n apply(functionToString, descriptor.value, []) === source,\n );\n}\n\nfunction inspectInstance(value: object, depth: number, prototypeShape: PromiseShape): PromiseShape {\n if (depth !== 0 || prototypeShape !== \"safe\") return \"suspicious\";\n try {\n const hasOverrides =\n getDescriptor(value, \"constructor\") !== undefined ||\n getDescriptor(value, \"then\") !== undefined ||\n getDescriptor(value, Symbol.species) !== undefined;\n return hasOverrides ? \"suspicious\" : \"safe\";\n } catch {\n return \"suspicious\";\n }\n}\n\nfunction consumeRejection(value: object): void {\n try {\n void apply(promiseThen, value, [undefined, () => undefined]);\n } catch {\n // A failed brand check remains an unsupported Promise-shaped callback result.\n }\n}\n"]}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
const apply = Reflect.apply;
|
|
2
|
+
const getDescriptor = Object.getOwnPropertyDescriptor;
|
|
3
|
+
const getPrototype = Object.getPrototypeOf;
|
|
4
|
+
const functionToString = Function.prototype.toString;
|
|
5
|
+
const promiseThen = Promise.prototype.then;
|
|
6
|
+
const promiseSource = apply(functionToString, Promise, []);
|
|
7
|
+
const thenSource = apply(functionToString, promiseThen, []);
|
|
8
|
+
const speciesGetter = getDescriptor(Promise, Symbol.species)?.get;
|
|
9
|
+
const speciesSource = speciesGetter ? apply(functionToString, speciesGetter, []) : "";
|
|
10
|
+
function rejectCallbackPromise(value) {
|
|
11
|
+
const shape = promiseShape(value);
|
|
12
|
+
if (shape === "none") return false;
|
|
13
|
+
if (shape === "safe") consumeRejection(value);
|
|
14
|
+
return true;
|
|
15
|
+
}
|
|
16
|
+
function promiseShape(value) {
|
|
17
|
+
if (typeof value !== "object" || value === null) return "none";
|
|
18
|
+
let prototype;
|
|
19
|
+
try {
|
|
20
|
+
prototype = getPrototype(value);
|
|
21
|
+
} catch {
|
|
22
|
+
return "none";
|
|
23
|
+
}
|
|
24
|
+
for (let depth = 0; prototype && depth < 4; depth += 1) {
|
|
25
|
+
const prototypeShape = inspectPromisePrototype(prototype);
|
|
26
|
+
if (prototypeShape !== "none") return inspectInstance(value, depth, prototypeShape);
|
|
27
|
+
try {
|
|
28
|
+
prototype = getPrototype(prototype);
|
|
29
|
+
} catch {
|
|
30
|
+
return "none";
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return "none";
|
|
34
|
+
}
|
|
35
|
+
function inspectPromisePrototype(prototype) {
|
|
36
|
+
try {
|
|
37
|
+
const then = getDescriptor(prototype, "then");
|
|
38
|
+
if (!isNativeFunction(then, thenSource)) return "none";
|
|
39
|
+
const constructorDescriptor = getDescriptor(prototype, "constructor");
|
|
40
|
+
if (!constructorDescriptor || !("value" in constructorDescriptor) || typeof constructorDescriptor.value !== "function")
|
|
41
|
+
return "suspicious";
|
|
42
|
+
const ownPrototype = getDescriptor(constructorDescriptor.value, "prototype");
|
|
43
|
+
if (!ownPrototype || !("value" in ownPrototype) || ownPrototype.value !== prototype)
|
|
44
|
+
return "suspicious";
|
|
45
|
+
const species = getDescriptor(constructorDescriptor.value, Symbol.species);
|
|
46
|
+
if (!species?.get || species.set !== void 0) return "suspicious";
|
|
47
|
+
const constructorNative = apply(functionToString, constructorDescriptor.value, []) === promiseSource;
|
|
48
|
+
const speciesNative = apply(functionToString, species.get, []) === speciesSource;
|
|
49
|
+
return constructorNative && speciesNative ? "safe" : "suspicious";
|
|
50
|
+
} catch {
|
|
51
|
+
return "suspicious";
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
function isNativeFunction(descriptor, source) {
|
|
55
|
+
return Boolean(
|
|
56
|
+
descriptor && "value" in descriptor && typeof descriptor.value === "function" && apply(functionToString, descriptor.value, []) === source
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
function inspectInstance(value, depth, prototypeShape) {
|
|
60
|
+
if (depth !== 0 || prototypeShape !== "safe") return "suspicious";
|
|
61
|
+
try {
|
|
62
|
+
const hasOverrides = getDescriptor(value, "constructor") !== void 0 || getDescriptor(value, "then") !== void 0 || getDescriptor(value, Symbol.species) !== void 0;
|
|
63
|
+
return hasOverrides ? "suspicious" : "safe";
|
|
64
|
+
} catch {
|
|
65
|
+
return "suspicious";
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
function consumeRejection(value) {
|
|
69
|
+
try {
|
|
70
|
+
void apply(promiseThen, value, [void 0, () => void 0]);
|
|
71
|
+
} catch {
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export { rejectCallbackPromise };
|
|
76
|
+
//# sourceMappingURL=callback-promise.js.map
|
|
77
|
+
//# sourceMappingURL=callback-promise.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/kuery-v1/callback-promise.ts"],"names":[],"mappings":"AAAA,MAAM,QAAQ,OAAA,CAAQ,KAAA;AACtB,MAAM,gBAAgB,MAAA,CAAO,wBAAA;AAC7B,MAAM,eAAe,MAAA,CAAO,cAAA;AAC5B,MAAM,gBAAA,GAAmB,SAAS,SAAA,CAAU,QAAA;AAC5C,MAAM,WAAA,GAAc,QAAQ,SAAA,CAAU,IAAA;AACtC,MAAM,aAAA,GAAgB,KAAA,CAAM,gBAAA,EAAkB,OAAA,EAAS,EAAE,CAAA;AACzD,MAAM,UAAA,GAAa,KAAA,CAAM,gBAAA,EAAkB,WAAA,EAAa,EAAE,CAAA;AAC1D,MAAM,aAAA,GAAgB,aAAA,CAAc,OAAA,EAAS,MAAA,CAAO,OAAO,CAAA,EAAG,GAAA;AAC9D,MAAM,gBAAgB,aAAA,GAAgB,KAAA,CAAM,kBAAkB,aAAA,EAAe,EAAE,CAAA,GAAI,EAAA;AAK5E,SAAS,sBAAsB,KAAA,EAAyB;AAC7D,EAAA,MAAM,KAAA,GAAQ,aAAa,KAAK,CAAA;AAChC,EAAA,IAAI,KAAA,KAAU,QAAQ,OAAO,KAAA;AAC7B,EAAA,IAAI,KAAA,KAAU,MAAA,EAAQ,gBAAA,CAAiB,KAAe,CAAA;AACtD,EAAA,OAAO,IAAA;AACT;AAEA,SAAS,aAAa,KAAA,EAA8B;AAClD,EAAA,IAAI,OAAO,KAAA,KAAU,QAAA,IAAY,KAAA,KAAU,MAAM,OAAO,MAAA;AACxD,EAAA,IAAI,SAAA;AACJ,EAAA,IAAI;AACF,IAAA,SAAA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AAA,CAAA,MAAQ;AAAA,EAER;AACF","file":"callback-promise.js","sourcesContent":["const apply = Reflect.apply;\nconst getDescriptor = Object.getOwnPropertyDescriptor;\nconst getPrototype = Object.getPrototypeOf;\nconst functionToString = Function.prototype.toString;\nconst promiseThen = Promise.prototype.then;\nconst promiseSource = apply(functionToString, Promise, []);\nconst thenSource = apply(functionToString, promiseThen, []);\nconst speciesGetter = getDescriptor(Promise, Symbol.species)?.get;\nconst speciesSource = speciesGetter ? apply(functionToString, speciesGetter, []) : \"\";\n\ntype PromiseShape = \"safe\" | \"suspicious\" | \"none\";\n\n/** Detect trusted native Promises without reading user-controlled properties. */\nexport function rejectCallbackPromise(value: unknown): boolean {\n const shape = promiseShape(value);\n if (shape === \"none\") return false;\n if (shape === \"safe\") consumeRejection(value as object);\n return true;\n}\n\nfunction promiseShape(value: unknown): PromiseShape {\n if (typeof value !== \"object\" || value === null) return \"none\";\n let prototype: object | null;\n try {\n prototype = getPrototype(value);\n } catch {\n return \"none\";\n }\n for (let depth = 0; prototype && depth < 4; depth += 1) {\n const prototypeShape = inspectPromisePrototype(prototype);\n if (prototypeShape !== \"none\") return inspectInstance(value, depth, prototypeShape);\n try {\n prototype = getPrototype(prototype);\n } catch {\n return \"none\";\n }\n }\n return \"none\";\n}\n\nfunction inspectPromisePrototype(prototype: object): PromiseShape {\n try {\n const then = getDescriptor(prototype, \"then\");\n if (!isNativeFunction(then, thenSource)) return \"none\";\n const constructorDescriptor = getDescriptor(prototype, \"constructor\");\n if (\n !constructorDescriptor ||\n !(\"value\" in constructorDescriptor) ||\n typeof constructorDescriptor.value !== \"function\"\n )\n return \"suspicious\";\n const ownPrototype = getDescriptor(constructorDescriptor.value, \"prototype\");\n if (!ownPrototype || !(\"value\" in ownPrototype) || ownPrototype.value !== prototype)\n return \"suspicious\";\n const species = getDescriptor(constructorDescriptor.value, Symbol.species);\n if (!species?.get || species.set !== undefined) return \"suspicious\";\n const constructorNative =\n apply(functionToString, constructorDescriptor.value, []) === promiseSource;\n const speciesNative = apply(functionToString, species.get, []) === speciesSource;\n return constructorNative && speciesNative ? \"safe\" : \"suspicious\";\n } catch {\n return \"suspicious\";\n }\n}\n\nfunction isNativeFunction(descriptor: PropertyDescriptor | undefined, source: string): boolean {\n return Boolean(\n descriptor &&\n \"value\" in descriptor &&\n typeof descriptor.value === \"function\" &&\n apply(functionToString, descriptor.value, []) === source,\n );\n}\n\nfunction inspectInstance(value: object, depth: number, prototypeShape: PromiseShape): PromiseShape {\n if (depth !== 0 || prototypeShape !== \"safe\") return \"suspicious\";\n try {\n const hasOverrides =\n getDescriptor(value, \"constructor\") !== undefined ||\n getDescriptor(value, \"then\") !== undefined ||\n getDescriptor(value, Symbol.species) !== undefined;\n return hasOverrides ? \"suspicious\" : \"safe\";\n } catch {\n return \"suspicious\";\n }\n}\n\nfunction consumeRejection(value: object): void {\n try {\n void apply(promiseThen, value, [undefined, () => undefined]);\n } catch {\n // A failed brand check remains an unsupported Promise-shaped callback result.\n }\n}\n"]}
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var inspect_js = require('./inspect.js');
|
|
4
|
+
var limits_js = require('./limits.js');
|
|
5
|
+
var result_js = require('./result.js');
|
|
6
|
+
|
|
7
|
+
const NODE_KEYS = Object.freeze({
|
|
8
|
+
literal: /* @__PURE__ */ new Set(["kind", "value"]),
|
|
9
|
+
ref: /* @__PURE__ */ new Set(["kind", "ref"]),
|
|
10
|
+
op: /* @__PURE__ */ new Set(["kind", "op", "args"])
|
|
11
|
+
});
|
|
12
|
+
const ALL_NODE_KEYS = /* @__PURE__ */ new Set(["kind", "value", "ref", "op", "args"]);
|
|
13
|
+
function canonicalizeExpression(input, options = {}) {
|
|
14
|
+
let limits;
|
|
15
|
+
try {
|
|
16
|
+
limits = limits_js.resolveLimits(options.limits);
|
|
17
|
+
} catch {
|
|
18
|
+
return result_js.failure("EXPRESSION_LIMIT_EXCEEDED", []);
|
|
19
|
+
}
|
|
20
|
+
const state = {
|
|
21
|
+
limits,
|
|
22
|
+
options,
|
|
23
|
+
maxDepth: limits.maxDepth,
|
|
24
|
+
maxNodes: limits.maxNodes,
|
|
25
|
+
maxStringLength: limits.maxStringLength,
|
|
26
|
+
nodes: 0,
|
|
27
|
+
active: /* @__PURE__ */ new WeakSet()
|
|
28
|
+
};
|
|
29
|
+
try {
|
|
30
|
+
return result_js.success(canonicalizeNode(input, [], 0, state));
|
|
31
|
+
} catch (error) {
|
|
32
|
+
const problem = error instanceof result_js.ExpressionFailure ? error : new result_js.ExpressionFailure("EXPRESSION_INVALID_INPUT", []);
|
|
33
|
+
return result_js.failure(problem.code, problem.expressionPath);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
function canonicalizeNode(input, path, depth, state) {
|
|
37
|
+
countNode(path, depth, state);
|
|
38
|
+
if (typeof input !== "object" || input === null)
|
|
39
|
+
throw new result_js.ExpressionFailure("EXPRESSION_INVALID_INPUT", path);
|
|
40
|
+
if (state.active.has(input)) throw new result_js.ExpressionFailure("EXPRESSION_INVALID_INPUT", path);
|
|
41
|
+
state.active.add(input);
|
|
42
|
+
try {
|
|
43
|
+
const initial = inspect_js.dataProperties(input, path, ALL_NODE_KEYS, 3);
|
|
44
|
+
const kind = initial.kind;
|
|
45
|
+
if (kind !== "literal" && kind !== "ref" && kind !== "op") {
|
|
46
|
+
throw new result_js.ExpressionFailure("EXPRESSION_INVALID_INPUT", [...path, "kind"]);
|
|
47
|
+
}
|
|
48
|
+
const properties = selectNodeProperties(initial, path, NODE_KEYS[kind]);
|
|
49
|
+
if (kind === "literal") return canonicalLiteral(properties, path, depth, state);
|
|
50
|
+
if (kind === "ref") return canonicalReference(properties, path, depth, state);
|
|
51
|
+
return canonicalOperator(properties, path, depth, state);
|
|
52
|
+
} finally {
|
|
53
|
+
state.active.delete(input);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
function selectNodeProperties(properties, path, allowed) {
|
|
57
|
+
for (const key of Object.keys(properties)) {
|
|
58
|
+
if (!allowed.has(key)) throw new result_js.ExpressionFailure("EXPRESSION_INVALID_INPUT", [...path, key]);
|
|
59
|
+
}
|
|
60
|
+
return properties;
|
|
61
|
+
}
|
|
62
|
+
function countNode(path, depth, state) {
|
|
63
|
+
state.nodes += 1;
|
|
64
|
+
if (depth > state.limits.maxDepth || state.nodes > state.limits.maxNodes) {
|
|
65
|
+
throw new result_js.ExpressionFailure("EXPRESSION_LIMIT_EXCEEDED", path);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
function canonicalLiteral(properties, path, depth, state) {
|
|
69
|
+
if (!("value" in properties))
|
|
70
|
+
throw new result_js.ExpressionFailure("EXPRESSION_INVALID_INPUT", [...path, "value"]);
|
|
71
|
+
const value = inspect_js.cloneJson(properties.value, [...path, "value"], depth + 1, state);
|
|
72
|
+
return Object.freeze({ kind: "literal", value });
|
|
73
|
+
}
|
|
74
|
+
function canonicalReference(properties, path, depth, state) {
|
|
75
|
+
if (!("ref" in properties))
|
|
76
|
+
throw new result_js.ExpressionFailure("EXPRESSION_INVALID_REFERENCE", [...path, "ref"]);
|
|
77
|
+
const refPath = [...path, "ref"];
|
|
78
|
+
const raw = properties.ref;
|
|
79
|
+
const codec = state.options.reference;
|
|
80
|
+
if (!codec && typeof raw === "string" && inspect_js.exceedsStringLimit(raw, state.limits.maxReferenceLength)) {
|
|
81
|
+
throw new result_js.ExpressionFailure("EXPRESSION_LIMIT_EXCEEDED", refPath);
|
|
82
|
+
}
|
|
83
|
+
const cloned = cloneReference(raw, refPath, depth + 1, state);
|
|
84
|
+
if (codec ? !safeValidate(codec.validate, cloned) : !defaultReference(cloned)) {
|
|
85
|
+
throw new result_js.ExpressionFailure("EXPRESSION_INVALID_REFERENCE", refPath);
|
|
86
|
+
}
|
|
87
|
+
if (referenceExceedsLength(cloned, state.limits.maxReferenceLength)) {
|
|
88
|
+
throw new result_js.ExpressionFailure("EXPRESSION_LIMIT_EXCEEDED", refPath);
|
|
89
|
+
}
|
|
90
|
+
if (!codec?.canonicalize) return Object.freeze({ kind: "ref", ref: cloned });
|
|
91
|
+
const replacement = canonicalReferenceValue(cloned, codec.canonicalize, refPath);
|
|
92
|
+
const output = cloneReference(replacement, refPath, depth + 1, state);
|
|
93
|
+
if (!safeValidate(codec.validate, output))
|
|
94
|
+
throw new result_js.ExpressionFailure("EXPRESSION_INVALID_REFERENCE", refPath);
|
|
95
|
+
if (referenceExceedsLength(output, state.limits.maxReferenceLength)) {
|
|
96
|
+
throw new result_js.ExpressionFailure("EXPRESSION_LIMIT_EXCEEDED", refPath);
|
|
97
|
+
}
|
|
98
|
+
return Object.freeze({ kind: "ref", ref: output });
|
|
99
|
+
}
|
|
100
|
+
function cloneReference(input, path, depth, state) {
|
|
101
|
+
try {
|
|
102
|
+
return inspect_js.cloneJson(input, path, depth, state);
|
|
103
|
+
} catch (error) {
|
|
104
|
+
if (error instanceof result_js.ExpressionFailure && error.code === "EXPRESSION_LIMIT_EXCEEDED")
|
|
105
|
+
throw error;
|
|
106
|
+
throw new result_js.ExpressionFailure("EXPRESSION_INVALID_REFERENCE", path);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
function safeValidate(validate, input) {
|
|
110
|
+
try {
|
|
111
|
+
return validate(input) === true;
|
|
112
|
+
} catch {
|
|
113
|
+
return false;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
function defaultReference(input) {
|
|
117
|
+
return typeof input === "string" && input.length > 0;
|
|
118
|
+
}
|
|
119
|
+
function canonicalReferenceValue(reference, canonicalize, path) {
|
|
120
|
+
try {
|
|
121
|
+
return canonicalize(reference);
|
|
122
|
+
} catch {
|
|
123
|
+
throw new result_js.ExpressionFailure("EXPRESSION_INVALID_REFERENCE", path);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
function canonicalOperator(properties, path, depth, state) {
|
|
127
|
+
if (typeof properties.op !== "string" || !Array.isArray(properties.args)) {
|
|
128
|
+
throw new result_js.ExpressionFailure("EXPRESSION_INVALID_INPUT", path);
|
|
129
|
+
}
|
|
130
|
+
if (properties.op.length === 0 || inspect_js.exceedsStringLimit(properties.op, state.limits.maxStringLength)) {
|
|
131
|
+
throw new result_js.ExpressionFailure("EXPRESSION_LIMIT_EXCEEDED", [...path, "op"]);
|
|
132
|
+
}
|
|
133
|
+
const args = canonicalArgs(properties.args, path, depth, state);
|
|
134
|
+
return Object.freeze({ kind: "op", op: properties.op, args });
|
|
135
|
+
}
|
|
136
|
+
function canonicalArgs(input, path, depth, state) {
|
|
137
|
+
const argsPath = [...path, "args"];
|
|
138
|
+
const length = inspect_js.arrayLength(input, argsPath);
|
|
139
|
+
if (length > state.limits.maxArgs || length > state.maxNodes - state.nodes) {
|
|
140
|
+
throw new result_js.ExpressionFailure("EXPRESSION_LIMIT_EXCEEDED", argsPath);
|
|
141
|
+
}
|
|
142
|
+
validateArrayKeys(input, argsPath, length);
|
|
143
|
+
const output = [];
|
|
144
|
+
for (let index = 0; index < length; index += 1) {
|
|
145
|
+
const descriptor = arrayElement(input, index, argsPath);
|
|
146
|
+
output.push(canonicalizeNode(descriptor.value, [...argsPath, index], depth + 1, state));
|
|
147
|
+
}
|
|
148
|
+
return Object.freeze(output);
|
|
149
|
+
}
|
|
150
|
+
function validateArrayKeys(input, path, length) {
|
|
151
|
+
try {
|
|
152
|
+
const keys = Reflect.ownKeys(input);
|
|
153
|
+
if (keys.length !== length + 1) throw new result_js.ExpressionFailure("EXPRESSION_INVALID_INPUT", path);
|
|
154
|
+
} catch (error) {
|
|
155
|
+
if (error instanceof result_js.ExpressionFailure) throw error;
|
|
156
|
+
throw new result_js.ExpressionFailure("EXPRESSION_INVALID_INPUT", path);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
function arrayElement(input, index, path) {
|
|
160
|
+
try {
|
|
161
|
+
const descriptor = Object.getOwnPropertyDescriptor(input, String(index));
|
|
162
|
+
if (!descriptor || !("value" in descriptor) || !descriptor.enumerable) {
|
|
163
|
+
throw new result_js.ExpressionFailure("EXPRESSION_INVALID_INPUT", [...path, index]);
|
|
164
|
+
}
|
|
165
|
+
return descriptor;
|
|
166
|
+
} catch (error) {
|
|
167
|
+
if (error instanceof result_js.ExpressionFailure) throw error;
|
|
168
|
+
throw new result_js.ExpressionFailure("EXPRESSION_INVALID_INPUT", [...path, index]);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
function referenceIdentity(reference) {
|
|
172
|
+
return inspect_js.stableJson(reference);
|
|
173
|
+
}
|
|
174
|
+
function referenceExceedsLength(reference, maxLength) {
|
|
175
|
+
return typeof reference === "string" ? inspect_js.exceedsStringLimit(reference, maxLength) : inspect_js.stableJson(reference).length > maxLength;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
exports.canonicalizeExpression = canonicalizeExpression;
|
|
179
|
+
exports.referenceIdentity = referenceIdentity;
|
|
180
|
+
//# sourceMappingURL=canonicalize.cjs.map
|
|
181
|
+
//# sourceMappingURL=canonicalize.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/kuery-v1/canonicalize.ts"],"names":["resolveLimits","failure","success","ExpressionFailure","dataProperties","cloneJson","exceedsStringLimit","arrayLength","stableJson"],"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,E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,CAAW,SAAS,CAAA,CAAE,MAAA,GAAS,SAAA;AACrC","file":"canonicalize.cjs","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,7 @@
|
|
|
1
|
+
import { JsonValue, CanonicalizeExpressionOptions, Result, ValueExpression } from './types.cjs';
|
|
2
|
+
|
|
3
|
+
/** Validate unknown input and return a deeply frozen, canonical expression snapshot. */
|
|
4
|
+
declare function canonicalizeExpression<R extends JsonValue = string>(input: unknown, options?: CanonicalizeExpressionOptions<R>): Result<ValueExpression<R>>;
|
|
5
|
+
declare function referenceIdentity(reference: JsonValue): string;
|
|
6
|
+
|
|
7
|
+
export { canonicalizeExpression, referenceIdentity };
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { JsonValue, CanonicalizeExpressionOptions, Result, ValueExpression } from './types.js';
|
|
2
|
+
|
|
3
|
+
/** Validate unknown input and return a deeply frozen, canonical expression snapshot. */
|
|
4
|
+
declare function canonicalizeExpression<R extends JsonValue = string>(input: unknown, options?: CanonicalizeExpressionOptions<R>): Result<ValueExpression<R>>;
|
|
5
|
+
declare function referenceIdentity(reference: JsonValue): string;
|
|
6
|
+
|
|
7
|
+
export { canonicalizeExpression, referenceIdentity };
|