elysia 2.0.0-exp.2 → 2.0.0-exp.21
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/dist/_virtual/_rolldown/runtime.js +2 -0
- package/dist/adapter/bun/index.d.ts +2 -25
- package/dist/adapter/bun/index.js +18 -12
- package/dist/adapter/bun/index.mjs +19 -13
- package/dist/adapter/bun/router.js +1 -1
- package/dist/adapter/bun/router.mjs +1 -1
- package/dist/adapter/constants.d.ts +3 -26
- package/dist/adapter/index.d.ts +1 -26
- package/dist/adapter/index.js +1 -17
- package/dist/adapter/index.mjs +1 -17
- package/dist/adapter/types.d.ts +1 -0
- package/dist/adapter/utils.d.ts +3 -4
- package/dist/adapter/utils.js +83 -81
- package/dist/adapter/utils.mjs +82 -80
- package/dist/adapter/web-standard/handler.js +6 -3
- package/dist/adapter/web-standard/handler.mjs +6 -3
- package/dist/adapter/web-standard/index.d.ts +2 -26
- package/dist/adapter/web-standard/index.js +12 -1
- package/dist/adapter/web-standard/index.mjs +12 -1
- package/dist/base.d.ts +38 -68
- package/dist/base.js +133 -97
- package/dist/base.mjs +133 -97
- package/dist/compile/aot.d.ts +22 -1
- package/dist/compile/aot.js +22 -0
- package/dist/compile/aot.mjs +22 -0
- package/dist/compile/handler/index.d.ts +2 -2
- package/dist/compile/handler/index.js +85 -456
- package/dist/compile/handler/index.mjs +86 -457
- package/dist/compile/handler/jit.d.ts +35 -0
- package/dist/compile/handler/jit.js +445 -0
- package/dist/compile/handler/jit.mjs +443 -0
- package/dist/compile/handler/params.js +3 -2
- package/dist/compile/handler/params.mjs +4 -3
- package/dist/compile/handler/reconstruct.d.ts +13 -0
- package/dist/compile/handler/reconstruct.js +29 -0
- package/dist/compile/handler/reconstruct.mjs +28 -0
- package/dist/compile/handler/utils.d.ts +2 -1
- package/dist/compile/handler/utils.js +9 -3
- package/dist/compile/handler/utils.mjs +9 -4
- package/dist/compile/jit-probe.d.ts +19 -0
- package/dist/compile/jit-probe.js +38 -0
- package/dist/compile/jit-probe.mjs +36 -0
- package/dist/cookie/index.d.ts +4 -3
- package/dist/cookie/index.js +2 -1
- package/dist/cookie/index.mjs +2 -1
- package/dist/cookie/serialize.d.ts +6 -0
- package/dist/cookie/serialize.js +28 -0
- package/dist/cookie/serialize.mjs +27 -0
- package/dist/cookie/utils.d.ts +1 -2
- package/dist/cookie/utils.js +2 -23
- package/dist/cookie/utils.mjs +5 -25
- package/dist/handler/error.js +2 -2
- package/dist/handler/error.mjs +3 -3
- package/dist/handler/fetch.js +49 -42
- package/dist/handler/fetch.mjs +50 -43
- package/dist/index.d.ts +13 -8
- package/dist/index.js +11 -4
- package/dist/index.mjs +7 -6
- package/dist/parse-query.js +1 -1
- package/dist/parse-query.mjs +1 -1
- package/dist/plugin/bun.d.ts +10 -0
- package/dist/plugin/bun.js +18 -1
- package/dist/plugin/bun.mjs +19 -2
- package/dist/plugin/core.d.ts +61 -1
- package/dist/plugin/core.js +90 -10
- package/dist/plugin/core.mjs +90 -12
- package/dist/plugin/esbuild.d.ts +10 -0
- package/dist/plugin/esbuild.js +18 -1
- package/dist/plugin/esbuild.mjs +19 -2
- package/dist/plugin/source.d.ts +38 -1
- package/dist/plugin/source.js +104 -13
- package/dist/plugin/source.mjs +102 -14
- package/dist/plugin/vite.d.ts +7 -0
- package/dist/plugin/vite.js +22 -1
- package/dist/plugin/vite.mjs +23 -2
- package/dist/sucrose.js +23 -10
- package/dist/sucrose.mjs +23 -10
- package/dist/trace.d.ts +1 -1
- package/dist/trace.js +4 -6
- package/dist/trace.mjs +4 -6
- package/dist/type/bridge.d.ts +9 -8
- package/dist/type/coerce.d.ts +20 -2
- package/dist/type/coerce.js +110 -0
- package/dist/type/coerce.mjs +109 -2
- package/dist/type/compat.js +4 -3
- package/dist/type/compat.mjs +2 -1
- package/dist/type/constants.d.ts +1 -1
- package/dist/type/elysia/boolean-string.js +1 -1
- package/dist/type/elysia/boolean-string.mjs +1 -1
- package/dist/type/elysia/file-type.js +1 -1
- package/dist/type/elysia/file-type.mjs +1 -1
- package/dist/type/elysia/integer-string.js +1 -1
- package/dist/type/elysia/integer-string.mjs +1 -1
- package/dist/type/exports.js +29 -2059
- package/dist/type/exports.mjs +7 -295
- package/dist/type/index.d.ts +6 -2
- package/dist/type/index.js +4 -0
- package/dist/type/index.mjs +3 -1
- package/dist/type/types.d.ts +1 -1
- package/dist/type/utils.js +4 -4
- package/dist/type/utils.mjs +4 -4
- package/dist/type/validator/custom-error.d.ts +10 -0
- package/dist/type/validator/custom-error.js +113 -0
- package/dist/type/validator/custom-error.mjs +110 -0
- package/dist/type/validator/default-precompute.d.ts +22 -0
- package/dist/type/validator/default-precompute.js +476 -0
- package/dist/type/validator/default-precompute.mjs +465 -0
- package/dist/type/validator/frozen-check.d.ts +11 -0
- package/dist/type/validator/frozen-check.js +19 -0
- package/dist/type/validator/frozen-check.mjs +18 -0
- package/dist/type/{validator.d.ts → validator/index.d.ts} +7 -20
- package/dist/type/validator/index.js +572 -0
- package/dist/type/validator/index.mjs +567 -0
- package/dist/type/validator/string-codec-aot.d.ts +9 -0
- package/dist/type/validator/string-codec-aot.js +106 -0
- package/dist/type/validator/string-codec-aot.mjs +102 -0
- package/dist/type/validator/validator-cache.d.ts +21 -0
- package/dist/type/validator/validator-cache.js +163 -0
- package/dist/type/validator/validator-cache.mjs +161 -0
- package/dist/types.d.ts +22 -14
- package/dist/universal/file.d.ts +1 -1
- package/dist/universal/file.js +35 -29
- package/dist/universal/file.mjs +35 -29
- package/dist/utils.d.ts +12 -1
- package/dist/utils.js +89 -45
- package/dist/utils.mjs +88 -46
- package/dist/validator/index.d.ts +1 -1
- package/dist/validator/index.js +15 -2
- package/dist/validator/index.mjs +15 -2
- package/dist/validator/route.d.ts +0 -1
- package/dist/ws/route.js +47 -24
- package/dist/ws/route.mjs +48 -25
- package/package.json +10 -4
- package/dist/type/validator.js +0 -1033
- package/dist/type/validator.mjs +0 -1029
|
@@ -0,0 +1,465 @@
|
|
|
1
|
+
import { dangerousKeys } from "../../constants.mjs";
|
|
2
|
+
import { nullObject } from "../../utils.mjs";
|
|
3
|
+
import { Default } from "typebox/value";
|
|
4
|
+
|
|
5
|
+
//#region src/type/validator/default-precompute.ts
|
|
6
|
+
function isPrecomputeSafe(schema, depth = 0) {
|
|
7
|
+
if (!schema || typeof schema !== "object") return true;
|
|
8
|
+
const kind = schema["~kind"];
|
|
9
|
+
if (kind === "Union" || kind === "Intersect" || kind === "Ref" || kind === "This" || kind === "Cyclic") return false;
|
|
10
|
+
if (schema["~codec"] || schema["~refine"]) return false;
|
|
11
|
+
if (depth > 0 && (kind === "Object" || schema.type === "object")) {
|
|
12
|
+
if (schema.default === void 0 || nestedOwnDefaultDiverges(schema)) return false;
|
|
13
|
+
}
|
|
14
|
+
if (schema.properties) {
|
|
15
|
+
for (const key in schema.properties) if (Object.hasOwn(schema.properties, key) && !isPrecomputeSafe(schema.properties[key], depth + 1)) return false;
|
|
16
|
+
}
|
|
17
|
+
if (schema.items) {
|
|
18
|
+
if (Array.isArray(schema.items)) {
|
|
19
|
+
for (const v of schema.items) if (!isPrecomputeSafe(v, depth + 1)) return false;
|
|
20
|
+
} else if (!isPrecomputeSafe(schema.items, depth + 1)) return false;
|
|
21
|
+
}
|
|
22
|
+
if (typeof schema.additionalProperties === "object" && !isPrecomputeSafe(schema.additionalProperties, depth + 1)) return false;
|
|
23
|
+
if (schema.patternProperties) {
|
|
24
|
+
for (const key in schema.patternProperties) if (Object.hasOwn(schema.patternProperties, key) && !isPrecomputeSafe(schema.patternProperties[key], depth + 1)) return false;
|
|
25
|
+
}
|
|
26
|
+
return true;
|
|
27
|
+
}
|
|
28
|
+
function nestedOwnDefaultDiverges(objectSchema) {
|
|
29
|
+
const own = objectSchema.default;
|
|
30
|
+
if (own === null || typeof own !== "object" || Array.isArray(own)) return false;
|
|
31
|
+
const props = objectSchema.properties;
|
|
32
|
+
if (!props) return false;
|
|
33
|
+
for (const key in props) {
|
|
34
|
+
const child = props[key];
|
|
35
|
+
if (!child || typeof child !== "object") continue;
|
|
36
|
+
if ("default" in child) {
|
|
37
|
+
if (!(key in own) || canonical(own[key]) !== canonical(child.default)) return true;
|
|
38
|
+
} else if (child["~kind"] === "Object" || child.type === "object") {
|
|
39
|
+
if (key in own) {
|
|
40
|
+
if (nestedOwnDefaultDiverges({
|
|
41
|
+
...child,
|
|
42
|
+
default: own[key]
|
|
43
|
+
})) return true;
|
|
44
|
+
} else if (hasDefaultBelow(child)) return true;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
function applyPrecomputed(defaults, value) {
|
|
50
|
+
const out = nullObject();
|
|
51
|
+
for (const k in defaults) {
|
|
52
|
+
const d = defaults[k];
|
|
53
|
+
const v = value[k];
|
|
54
|
+
if (v === void 0) out[k] = d !== null && typeof d === "object" ? structuredClone(d) : d;
|
|
55
|
+
else if (!Object.hasOwn(value, k)) out[k] = v;
|
|
56
|
+
else out[k] = d;
|
|
57
|
+
}
|
|
58
|
+
for (const k in value) {
|
|
59
|
+
const v = value[k];
|
|
60
|
+
if (v === void 0) continue;
|
|
61
|
+
const d = out[k];
|
|
62
|
+
if (v && typeof v === "object" && !Array.isArray(v) && d && typeof d === "object" && !Array.isArray(d)) out[k] = applyPrecomputed(d, v);
|
|
63
|
+
else out[k] = v;
|
|
64
|
+
}
|
|
65
|
+
return out;
|
|
66
|
+
}
|
|
67
|
+
const subtreeHasDefault = (n) => !!n && typeof n === "object" && ("default" in n || childSchemaSome(n, subtreeHasDefault));
|
|
68
|
+
function childSchemaSome(n, f) {
|
|
69
|
+
if (!n || typeof n !== "object") return false;
|
|
70
|
+
if (n.properties) {
|
|
71
|
+
for (const k in n.properties) if (f(n.properties[k])) return true;
|
|
72
|
+
}
|
|
73
|
+
const items = n.items;
|
|
74
|
+
if (Array.isArray(items)) {
|
|
75
|
+
for (const it of items) if (f(it)) return true;
|
|
76
|
+
} else if (items && f(items)) return true;
|
|
77
|
+
for (const key of [
|
|
78
|
+
"anyOf",
|
|
79
|
+
"allOf",
|
|
80
|
+
"oneOf"
|
|
81
|
+
]) {
|
|
82
|
+
const arr = n[key];
|
|
83
|
+
if (Array.isArray(arr)) {
|
|
84
|
+
for (const s of arr) if (f(s)) return true;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
if (typeof n.additionalProperties === "object" && f(n.additionalProperties)) return true;
|
|
88
|
+
if (n.patternProperties) {
|
|
89
|
+
for (const k in n.patternProperties) if (f(n.patternProperties[k])) return true;
|
|
90
|
+
}
|
|
91
|
+
for (const key of [
|
|
92
|
+
"not",
|
|
93
|
+
"if",
|
|
94
|
+
"then",
|
|
95
|
+
"else"
|
|
96
|
+
]) if (n[key] && f(n[key])) return true;
|
|
97
|
+
return false;
|
|
98
|
+
}
|
|
99
|
+
const hasDefaultBelow = (node) => childSchemaSome(node, subtreeHasDefault);
|
|
100
|
+
function hasUnemittableDefaultValue(node) {
|
|
101
|
+
if (!node || typeof node !== "object") return false;
|
|
102
|
+
if ("default" in node && !isEmittable(node.default, /* @__PURE__ */ new Set(), true)) return true;
|
|
103
|
+
return childSchemaSome(node, hasUnemittableDefaultValue);
|
|
104
|
+
}
|
|
105
|
+
function containsRefLike(node, seen = /* @__PURE__ */ new WeakSet()) {
|
|
106
|
+
if (!node || typeof node !== "object" || seen.has(node)) return false;
|
|
107
|
+
seen.add(node);
|
|
108
|
+
if (node.$ref !== void 0 || node["~kind"] === "Ref" || node["~kind"] === "This" || node["~kind"] === "Cyclic") return true;
|
|
109
|
+
return childSchemaSome(node, (child) => containsRefLike(child, seen));
|
|
110
|
+
}
|
|
111
|
+
function hasDivergentDefaultBelow(node) {
|
|
112
|
+
const ownKey = "default" in node ? canonical(node.default) : void 0;
|
|
113
|
+
let bad = false;
|
|
114
|
+
const visit = (n) => {
|
|
115
|
+
if (!n || typeof n !== "object") return false;
|
|
116
|
+
if ("default" in n && (ownKey === void 0 || canonical(n.default) !== ownKey)) return bad = true;
|
|
117
|
+
return childSchemaSome(n, visit);
|
|
118
|
+
};
|
|
119
|
+
childSchemaSome(node, visit);
|
|
120
|
+
return bad;
|
|
121
|
+
}
|
|
122
|
+
function structuralPreallocatable(schema, depth = 0) {
|
|
123
|
+
if (!schema || typeof schema !== "object") return true;
|
|
124
|
+
if (!((schema["~kind"] === "Object" || schema.type === "object") && !schema["~codec"] && !schema["~refine"] && !Array.isArray(schema.anyOf) && !Array.isArray(schema.allOf) && !Array.isArray(schema.oneOf) && schema.$ref === void 0 && schema.if === void 0 && schema.not === void 0)) return !hasDivergentDefaultBelow(schema);
|
|
125
|
+
if (depth > 0 && schema.default === void 0 && hasDefaultBelow(schema)) return false;
|
|
126
|
+
if (typeof schema.additionalProperties === "object" && subtreeHasDefault(schema.additionalProperties)) return false;
|
|
127
|
+
if (schema.patternProperties) {
|
|
128
|
+
for (const k in schema.patternProperties) if (subtreeHasDefault(schema.patternProperties[k])) return false;
|
|
129
|
+
}
|
|
130
|
+
if (schema.properties) {
|
|
131
|
+
for (const k in schema.properties) if (!structuralPreallocatable(schema.properties[k], depth + 1)) return false;
|
|
132
|
+
}
|
|
133
|
+
return true;
|
|
134
|
+
}
|
|
135
|
+
function emittableObjectKeys(v) {
|
|
136
|
+
if (Object.getOwnPropertySymbols(v).length) return;
|
|
137
|
+
const keys = Object.keys(v);
|
|
138
|
+
if (Object.getOwnPropertyNames(v).length !== keys.length) return;
|
|
139
|
+
for (const k of keys) {
|
|
140
|
+
const descriptor = Object.getOwnPropertyDescriptor(v, k);
|
|
141
|
+
if (!descriptor || descriptor.get || descriptor.set || !descriptor.enumerable) return;
|
|
142
|
+
}
|
|
143
|
+
return keys;
|
|
144
|
+
}
|
|
145
|
+
function isEmittable(v, seen, top = false) {
|
|
146
|
+
if (v === void 0) return top;
|
|
147
|
+
if (v === null) return true;
|
|
148
|
+
const t = typeof v;
|
|
149
|
+
if (t === "number") return Number.isFinite(v) && !Object.is(v, -0);
|
|
150
|
+
if (t === "string" || t === "boolean") return true;
|
|
151
|
+
if (t !== "object") return false;
|
|
152
|
+
if (seen.has(v)) return false;
|
|
153
|
+
seen.add(v);
|
|
154
|
+
let ok = true;
|
|
155
|
+
if (Array.isArray(v)) {
|
|
156
|
+
if (Object.getOwnPropertySymbols(v).length) ok = false;
|
|
157
|
+
for (const x of v) if (!isEmittable(x, seen)) {
|
|
158
|
+
ok = false;
|
|
159
|
+
break;
|
|
160
|
+
}
|
|
161
|
+
} else {
|
|
162
|
+
const proto = Object.getPrototypeOf(v);
|
|
163
|
+
if (proto !== Object.prototype && proto !== null) ok = false;
|
|
164
|
+
else {
|
|
165
|
+
const keys = emittableObjectKeys(v);
|
|
166
|
+
if (!keys) ok = false;
|
|
167
|
+
else for (const k of keys) {
|
|
168
|
+
if (dangerousKeys.has(k)) {
|
|
169
|
+
ok = false;
|
|
170
|
+
break;
|
|
171
|
+
}
|
|
172
|
+
if (!isEmittable(v[k], seen)) {
|
|
173
|
+
ok = false;
|
|
174
|
+
break;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
seen.delete(v);
|
|
180
|
+
return ok;
|
|
181
|
+
}
|
|
182
|
+
function cloneExpression(v) {
|
|
183
|
+
if (v === void 0) return "undefined";
|
|
184
|
+
if (v === null) return "null";
|
|
185
|
+
const t = typeof v;
|
|
186
|
+
if (t === "number" || t === "string" || t === "boolean") return JSON.stringify(v);
|
|
187
|
+
if (t !== "object") return;
|
|
188
|
+
if (Array.isArray(v)) {
|
|
189
|
+
if (Object.getOwnPropertySymbols(v).length) return;
|
|
190
|
+
const values = [];
|
|
191
|
+
for (const x of v) {
|
|
192
|
+
const value = cloneExpression(x);
|
|
193
|
+
if (value === void 0) return;
|
|
194
|
+
values.push(value);
|
|
195
|
+
}
|
|
196
|
+
return "[" + values.join(",") + "]";
|
|
197
|
+
}
|
|
198
|
+
const proto = Object.getPrototypeOf(v);
|
|
199
|
+
if (proto !== Object.prototype && proto !== null) return;
|
|
200
|
+
const keys = emittableObjectKeys(v);
|
|
201
|
+
if (!keys) return;
|
|
202
|
+
const entries = [];
|
|
203
|
+
for (const k of keys) {
|
|
204
|
+
if (dangerousKeys.has(k)) return;
|
|
205
|
+
const value = cloneExpression(v[k]);
|
|
206
|
+
if (value === void 0) return;
|
|
207
|
+
entries.push(JSON.stringify(k) + ":" + value);
|
|
208
|
+
}
|
|
209
|
+
return "{" + entries.join(",") + "}";
|
|
210
|
+
}
|
|
211
|
+
function buildDefaultClonerSource(value) {
|
|
212
|
+
if (!isEmittable(value, /* @__PURE__ */ new Set(), true)) return;
|
|
213
|
+
const expression = cloneExpression(value);
|
|
214
|
+
if (expression === void 0) return;
|
|
215
|
+
return `function(){return ${expression}}`;
|
|
216
|
+
}
|
|
217
|
+
function buildObjectMergeFunction(defaults, helpers) {
|
|
218
|
+
const name = `_m${helpers.length}`;
|
|
219
|
+
helpers.push("");
|
|
220
|
+
const keys = Object.keys(defaults);
|
|
221
|
+
let body = "if(v===null||typeof v!=='object'||Array.isArray(v))return v;const out=Object.create(null);let x;";
|
|
222
|
+
for (const key of keys) {
|
|
223
|
+
const access = `v[${JSON.stringify(key)}]`;
|
|
224
|
+
const write = `out[${JSON.stringify(key)}]`;
|
|
225
|
+
const d = defaults[key];
|
|
226
|
+
const value = cloneExpression(d);
|
|
227
|
+
if (value === void 0) return;
|
|
228
|
+
body += `x=${access};`;
|
|
229
|
+
if (d !== null && typeof d === "object") {
|
|
230
|
+
if (Array.isArray(d)) {
|
|
231
|
+
body += `if(x===undefined)${write}=${value};else ${write}=x;`;
|
|
232
|
+
continue;
|
|
233
|
+
}
|
|
234
|
+
const nested = buildObjectMergeFunction(d, helpers);
|
|
235
|
+
if (!nested) return;
|
|
236
|
+
body += `if(x===undefined)${write}=${value};else if(x&&typeof x==='object'&&!Array.isArray(x))${write}=${nested}(x);else ${write}=x;`;
|
|
237
|
+
} else body += `${write}=x===undefined?${value}:x;`;
|
|
238
|
+
}
|
|
239
|
+
body += "for(const k in v){x=v[k];if(x!==undefined";
|
|
240
|
+
for (const key of keys) body += `&&k!==${JSON.stringify(key)}`;
|
|
241
|
+
body += ")out[k]=x}return out";
|
|
242
|
+
helpers[+name.slice(2)] = `function ${name}(v){${body}}`;
|
|
243
|
+
return name;
|
|
244
|
+
}
|
|
245
|
+
function buildObjectDefaultMergeSource(defaults) {
|
|
246
|
+
if (!isEmittable(defaults, /* @__PURE__ */ new Set(), true)) return;
|
|
247
|
+
const helpers = [];
|
|
248
|
+
const root = buildObjectMergeFunction(defaults, helpers);
|
|
249
|
+
if (!root) return;
|
|
250
|
+
return `(function(){${helpers.join(";")};return ${root}})()`;
|
|
251
|
+
}
|
|
252
|
+
function fromSource(source) {
|
|
253
|
+
try {
|
|
254
|
+
return Function(`return ${source}`)();
|
|
255
|
+
} catch {
|
|
256
|
+
return;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
function createDefaultCloner(value) {
|
|
260
|
+
const source = buildDefaultClonerSource(value);
|
|
261
|
+
return source ? fromSource(source) : void 0;
|
|
262
|
+
}
|
|
263
|
+
function createObjectDefaultMerger(defaults) {
|
|
264
|
+
const source = buildObjectDefaultMergeSource(defaults);
|
|
265
|
+
return source ? fromSource(source) : void 0;
|
|
266
|
+
}
|
|
267
|
+
function createMergerFromSource(source) {
|
|
268
|
+
return fromSource(source);
|
|
269
|
+
}
|
|
270
|
+
function mergeCategory(node) {
|
|
271
|
+
if (!node || typeof node !== "object") return "identity";
|
|
272
|
+
if (!subtreeHasDefault(node)) return "identity";
|
|
273
|
+
if (node["~codec"] || node["~refine"]) return "bail";
|
|
274
|
+
const kind = node["~kind"];
|
|
275
|
+
if (kind === "Union" || kind === "Intersect" || kind === "Ref" || kind === "This" || kind === "Cyclic") return "bail";
|
|
276
|
+
if (kind === "Object" || node.type === "object") return "object";
|
|
277
|
+
if ((kind === "Array" || node.type === "array") && node.items) return !Array.isArray(node.items) && subtreeHasDefault(node.items) ? "array" : "identity";
|
|
278
|
+
return "identity";
|
|
279
|
+
}
|
|
280
|
+
function mergeExpression(schema, varName, helpers) {
|
|
281
|
+
const full = Default(schema, void 0);
|
|
282
|
+
const child = emitMerger(schema, helpers);
|
|
283
|
+
if (child === void 0) return;
|
|
284
|
+
const absent = full === void 0 ? void 0 : cloneExpression(full);
|
|
285
|
+
if (full !== void 0 && absent === void 0) return;
|
|
286
|
+
let present;
|
|
287
|
+
if (child === "") present = varName;
|
|
288
|
+
else present = mergeCategory(schema) === "object" ? `(${varName}!==null&&typeof ${varName}==='object'&&!Array.isArray(${varName})?${child}(${varName}):${varName})` : `(Array.isArray(${varName})?${child}(${varName}):${varName})`;
|
|
289
|
+
return {
|
|
290
|
+
absent,
|
|
291
|
+
present
|
|
292
|
+
};
|
|
293
|
+
}
|
|
294
|
+
function emitMerger(node, helpers) {
|
|
295
|
+
const category = mergeCategory(node);
|
|
296
|
+
if (category === "identity") return "";
|
|
297
|
+
if (category === "bail") return;
|
|
298
|
+
const name = `_d${helpers.length}`;
|
|
299
|
+
helpers.push("");
|
|
300
|
+
if (category === "object") {
|
|
301
|
+
const props = node.properties ?? {};
|
|
302
|
+
const handled = [];
|
|
303
|
+
let body = "if(v===null||typeof v!=='object'||Array.isArray(v))return v;const out=Object.create(null);let x;";
|
|
304
|
+
for (const key in props) {
|
|
305
|
+
if (!Object.hasOwn(props, key)) continue;
|
|
306
|
+
const ps = props[key];
|
|
307
|
+
if (!subtreeHasDefault(ps)) continue;
|
|
308
|
+
if (dangerousKeys.has(key)) return;
|
|
309
|
+
const expr = mergeExpression(ps, "x", helpers);
|
|
310
|
+
if (!expr) return;
|
|
311
|
+
handled.push(key);
|
|
312
|
+
const access = JSON.stringify(key);
|
|
313
|
+
body += `x=v[${access}];`;
|
|
314
|
+
body += expr.absent === void 0 ? `if(x!==undefined)out[${access}]=${expr.present};` : `out[${access}]=x===undefined?${expr.absent}:${expr.present};`;
|
|
315
|
+
}
|
|
316
|
+
if (!handled.length) {
|
|
317
|
+
helpers.pop();
|
|
318
|
+
return "";
|
|
319
|
+
}
|
|
320
|
+
body += "for(const k in v){x=v[k];if(x!==undefined";
|
|
321
|
+
for (const key of handled) body += `&&k!==${JSON.stringify(key)}`;
|
|
322
|
+
body += ")out[k]=x}return out";
|
|
323
|
+
helpers[+name.slice(2)] = `function ${name}(v){${body}}`;
|
|
324
|
+
return name;
|
|
325
|
+
}
|
|
326
|
+
const expr = mergeExpression(node.items, "e", helpers);
|
|
327
|
+
if (!expr) return;
|
|
328
|
+
if (expr.absent === void 0 && expr.present === "e") {
|
|
329
|
+
helpers.pop();
|
|
330
|
+
return "";
|
|
331
|
+
}
|
|
332
|
+
const assign = expr.absent === void 0 ? `e===undefined?undefined:${expr.present}` : `e===undefined?${expr.absent}:${expr.present}`;
|
|
333
|
+
helpers[+name.slice(2)] = `function ${name}(v){if(!Array.isArray(v))return v;const o=new Array(v.length);let e;for(let i=0;i<v.length;i++){e=v[i];o[i]=${assign}}return o}`;
|
|
334
|
+
return name;
|
|
335
|
+
}
|
|
336
|
+
function buildMergeSource(schema) {
|
|
337
|
+
const category = mergeCategory(schema);
|
|
338
|
+
if (category !== "object" && category !== "array") return;
|
|
339
|
+
const helpers = [];
|
|
340
|
+
const root = emitMerger(schema, helpers);
|
|
341
|
+
if (!root) return;
|
|
342
|
+
return `(function(){${helpers.join(";")};return ${root}})()`;
|
|
343
|
+
}
|
|
344
|
+
function emptyContainers(node, depth) {
|
|
345
|
+
if (depth <= 0 || !node || typeof node !== "object") return;
|
|
346
|
+
const kind = node["~kind"];
|
|
347
|
+
if (kind === "Object" || node.type === "object") {
|
|
348
|
+
const out = nullObject();
|
|
349
|
+
const props = node.properties ?? {};
|
|
350
|
+
for (const key in props) if (Object.hasOwn(props, key)) {
|
|
351
|
+
const child = emptyContainers(props[key], depth - 1);
|
|
352
|
+
if (child !== void 0) out[key] = child;
|
|
353
|
+
}
|
|
354
|
+
return out;
|
|
355
|
+
}
|
|
356
|
+
if ((kind === "Array" || node.type === "array") && node.items && !Array.isArray(node.items)) {
|
|
357
|
+
const element = emptyContainers(node.items, depth - 1);
|
|
358
|
+
return element === void 0 ? [] : [element];
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
function* mergeProbes(schema) {
|
|
362
|
+
yield {};
|
|
363
|
+
yield [];
|
|
364
|
+
const filled = emptyContainers(schema, 6);
|
|
365
|
+
if (filled !== void 0) yield filled;
|
|
366
|
+
const kind = schema["~kind"];
|
|
367
|
+
if (kind === "Object" || schema.type === "object") {
|
|
368
|
+
const props = schema.properties ?? {};
|
|
369
|
+
for (const key in props) if (Object.hasOwn(props, key)) {
|
|
370
|
+
yield { [key]: PROBE_SENTINEL };
|
|
371
|
+
const child = emptyContainers(props[key], 6);
|
|
372
|
+
if (child !== void 0) yield { [key]: child };
|
|
373
|
+
}
|
|
374
|
+
} else if ((kind === "Array" || schema.type === "array") && schema.items && !Array.isArray(schema.items)) {
|
|
375
|
+
yield [PROBE_SENTINEL];
|
|
376
|
+
const element = emptyContainers(schema.items, 6);
|
|
377
|
+
if (element !== void 0) yield [element, element];
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
function validateMergeSource(schema, source) {
|
|
381
|
+
const merger = fromSource(source);
|
|
382
|
+
if (!merger) return false;
|
|
383
|
+
for (const probe of mergeProbes(schema)) {
|
|
384
|
+
if (probe === null || typeof probe !== "object") continue;
|
|
385
|
+
let expected;
|
|
386
|
+
try {
|
|
387
|
+
expected = Default(schema, structuredClone(probe));
|
|
388
|
+
} catch {
|
|
389
|
+
return false;
|
|
390
|
+
}
|
|
391
|
+
let actual;
|
|
392
|
+
try {
|
|
393
|
+
actual = merger(structuredClone(probe));
|
|
394
|
+
} catch {
|
|
395
|
+
return false;
|
|
396
|
+
}
|
|
397
|
+
if (canonical(expected) !== canonical(actual)) return false;
|
|
398
|
+
}
|
|
399
|
+
return true;
|
|
400
|
+
}
|
|
401
|
+
const PROBE_SENTINEL = "__elysia_default_probe__";
|
|
402
|
+
function* defaultProbes(pod) {
|
|
403
|
+
if (!pod) return;
|
|
404
|
+
yield {};
|
|
405
|
+
for (const k in pod) {
|
|
406
|
+
yield { [k]: PROBE_SENTINEL };
|
|
407
|
+
const d = pod[k];
|
|
408
|
+
if (d && typeof d === "object" && !Array.isArray(d)) {
|
|
409
|
+
yield { [k]: {} };
|
|
410
|
+
for (const k2 in d) {
|
|
411
|
+
yield { [k]: { [k2]: PROBE_SENTINEL } };
|
|
412
|
+
break;
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
function canonical(v) {
|
|
418
|
+
if (v === void 0) return "\0u";
|
|
419
|
+
if (Object.is(v, -0)) return "-0";
|
|
420
|
+
if (v === null || typeof v !== "object") return JSON.stringify(v) ?? "\0u";
|
|
421
|
+
if (Array.isArray(v)) return "[" + v.map(canonical).join(",") + "]";
|
|
422
|
+
return "{" + Object.keys(v).sort().map((k) => JSON.stringify(k) + ":" + canonical(v[k])).join(",") + "}";
|
|
423
|
+
}
|
|
424
|
+
function verifyPreallocatableDefault(schema, validate = true) {
|
|
425
|
+
if (hasUnemittableDefaultValue(schema)) return;
|
|
426
|
+
let pd;
|
|
427
|
+
let podRaw;
|
|
428
|
+
try {
|
|
429
|
+
pd = Default(schema, void 0);
|
|
430
|
+
podRaw = Default(schema, nullObject());
|
|
431
|
+
} catch {
|
|
432
|
+
return;
|
|
433
|
+
}
|
|
434
|
+
const s = schema;
|
|
435
|
+
const rootIsObject = s.type === "object" || s["~kind"] === "Object";
|
|
436
|
+
const pod = rootIsObject && podRaw && typeof podRaw === "object" && !Array.isArray(podRaw) ? podRaw : void 0;
|
|
437
|
+
if (containsRefLike(schema)) return;
|
|
438
|
+
if (!isEmittable(pd, /* @__PURE__ */ new Set(), true) || pod !== void 0 && !isEmittable(pod, /* @__PURE__ */ new Set(), true)) return;
|
|
439
|
+
const nullDefault = isPrecomputeSafe(schema);
|
|
440
|
+
let ms = buildMergeSource(schema);
|
|
441
|
+
if (ms !== void 0 && validate && !validateMergeSource(schema, ms)) ms = void 0;
|
|
442
|
+
if (!nullDefault && ms === void 0) {
|
|
443
|
+
if (!rootIsObject) return;
|
|
444
|
+
if (!structuralPreallocatable(schema)) return;
|
|
445
|
+
if (validate) for (const probe of defaultProbes(pod)) {
|
|
446
|
+
let expected;
|
|
447
|
+
try {
|
|
448
|
+
expected = Default(schema, structuredClone(probe));
|
|
449
|
+
} catch {
|
|
450
|
+
return;
|
|
451
|
+
}
|
|
452
|
+
const actual = applyPrecomputed(pod, structuredClone(probe));
|
|
453
|
+
if (canonical(expected) !== canonical(actual)) return;
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
return {
|
|
457
|
+
pd,
|
|
458
|
+
pn: nullDefault,
|
|
459
|
+
pod,
|
|
460
|
+
ms
|
|
461
|
+
};
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
//#endregion
|
|
465
|
+
export { applyPrecomputed, buildDefaultClonerSource, buildMergeSource, buildObjectDefaultMergeSource, createDefaultCloner, createMergerFromSource, createObjectDefaultMerger, isEmittable, isPrecomputeSafe, verifyPreallocatableDefault };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { CheckBuildResult } from "../../compile/aot.js";
|
|
2
|
+
|
|
3
|
+
//#region src/type/validator/frozen-check.d.ts
|
|
4
|
+
declare function buildFrozenCheck(build: CheckBuildResult | undefined, node: any): {
|
|
5
|
+
identifier: string;
|
|
6
|
+
checkDefs: string;
|
|
7
|
+
checkValue: string;
|
|
8
|
+
external: boolean;
|
|
9
|
+
} | undefined;
|
|
10
|
+
//#endregion
|
|
11
|
+
export { buildFrozenCheck };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
+
const require_compile_aot = require('../../compile/aot.js');
|
|
3
|
+
|
|
4
|
+
//#region src/type/validator/frozen-check.ts
|
|
5
|
+
function buildFrozenCheck(build, node) {
|
|
6
|
+
if (!build?.functions?.length || !build.entry) return;
|
|
7
|
+
const vars = build.external.variables;
|
|
8
|
+
if (!require_compile_aot.externalsMatch(require_compile_aot.collectExternals(node), vars)) return;
|
|
9
|
+
const cr = require_compile_aot.reconstructCheck(build);
|
|
10
|
+
return {
|
|
11
|
+
identifier: build.external.identifier,
|
|
12
|
+
checkDefs: cr.defs,
|
|
13
|
+
checkValue: cr.value,
|
|
14
|
+
external: vars.length > 0
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
//#endregion
|
|
19
|
+
exports.buildFrozenCheck = buildFrozenCheck;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { collectExternals, externalsMatch, reconstructCheck } from "../../compile/aot.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/type/validator/frozen-check.ts
|
|
4
|
+
function buildFrozenCheck(build, node) {
|
|
5
|
+
if (!build?.functions?.length || !build.entry) return;
|
|
6
|
+
const vars = build.external.variables;
|
|
7
|
+
if (!externalsMatch(collectExternals(node), vars)) return;
|
|
8
|
+
const cr = reconstructCheck(build);
|
|
9
|
+
return {
|
|
10
|
+
identifier: build.external.identifier,
|
|
11
|
+
checkDefs: cr.defs,
|
|
12
|
+
checkValue: cr.value,
|
|
13
|
+
external: vars.length > 0
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
//#endregion
|
|
18
|
+
export { buildFrozenCheck };
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import { MaybePromise } from "
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
1
|
+
import { MaybePromise } from "../../types.js";
|
|
2
|
+
import { Validator as Validator$1, ValidatorOptions } from "../../validator/index.js";
|
|
3
|
+
import { TypeBoxValidatorCache } from "./validator-cache.js";
|
|
4
4
|
import { Static, StaticDecode, StaticEncode, TAny, TSchema } from "typebox/type";
|
|
5
|
-
import * as _$typebox_schema0 from "typebox/schema";
|
|
6
|
-
import { Validator } from "typebox/schema";
|
|
7
5
|
import { TLocalizedValidationError } from "typebox/error";
|
|
6
|
+
import { Validator } from "typebox/schema";
|
|
8
7
|
|
|
9
|
-
//#region src/type/validator.d.ts
|
|
8
|
+
//#region src/type/validator/index.d.ts
|
|
9
|
+
declare function shallowMergeObjects(members: any[]): TSchema | null;
|
|
10
10
|
declare class TypeBoxValidator<const in out T extends TSchema = TAny> extends Validator$1 {
|
|
11
11
|
#private;
|
|
12
12
|
tb?: Validator;
|
|
@@ -27,18 +27,5 @@ declare class TypeBoxValidator<const in out T extends TSchema = TAny> extends Va
|
|
|
27
27
|
FromAsync(value: Static<T>, type?: string): Promise<Static<T>>;
|
|
28
28
|
FromSync(value: Static<T>, type?: string): Static<T>;
|
|
29
29
|
}
|
|
30
|
-
declare class TypeBoxValidatorCache {
|
|
31
|
-
#private;
|
|
32
|
-
private static EMPTY;
|
|
33
|
-
private static ignoreKeys;
|
|
34
|
-
private static fnIds;
|
|
35
|
-
private static nextFnId;
|
|
36
|
-
private static fnKey;
|
|
37
|
-
static ignoreMeta(k: string, v: unknown): any;
|
|
38
|
-
constructor(gcTime: number);
|
|
39
|
-
get(schema: TSchema, coercions?: CoerceOption[] | typeof TypeBoxValidatorCache.EMPTY, normalize?: string): Validator<_$typebox_schema0.XSchema, unknown> | undefined;
|
|
40
|
-
set(schema: TSchema, coercions?: CoerceOption[] | typeof TypeBoxValidatorCache.EMPTY, validator?: Validator, normalize?: string): void;
|
|
41
|
-
clear(): void;
|
|
42
|
-
}
|
|
43
30
|
//#endregion
|
|
44
|
-
export { TypeBoxValidator, TypeBoxValidatorCache };
|
|
31
|
+
export { TypeBoxValidator, TypeBoxValidatorCache, shallowMergeObjects };
|