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