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,567 @@
1
+ import { nullObject } from "../../utils.mjs";
2
+ import { ValidationError } from "../../error.mjs";
3
+ import { Capture, Compiled, EMPTY_EXTERNALS, collectExternals, instantiateFrozenBoth, instantiateFrozenDecodeMirror, instantiateFrozenMirror } from "../../compile/aot.mjs";
4
+ import { Validator as Validator$1 } from "../../validator/index.mjs";
5
+ import { ELYSIA_TYPES } from "../constants.mjs";
6
+ import { isAsyncFunction } from "../../compile/utils.mjs";
7
+ import { applyCoercions, buildCoercedFromPlan, captureCoercePlan, nonAdditionalProperties } from "../coerce.mjs";
8
+ import { hasProperty } from "../utils.mjs";
9
+ import { ASYNC_REFINE, collectFileTypeChecks, takeFileTypeChecks } from "../elysia/file-type.mjs";
10
+ import { applyPrecomputed, buildDefaultClonerSource, buildObjectDefaultMergeSource, createDefaultCloner, createMergerFromSource, createObjectDefaultMerger, verifyPreallocatableDefault } from "./default-precompute.mjs";
11
+ import { buildFrozenCheck } from "./frozen-check.mjs";
12
+ import { buildFindCustomError, captureCustomErrors } from "./custom-error.mjs";
13
+ import { captureStringCodecEntries, reconstructInnerCodecs } from "./string-codec-aot.mjs";
14
+ import { TypeBoxValidatorCache } from "./validator-cache.mjs";
15
+ import { Evaluate, Intersect, Module } from "typebox/type";
16
+ import { Clean, Decode as Decode$1, DecodeUnsafe, Default, Encode, EncodeUnsafe, Errors, HasCodec } from "typebox/value";
17
+ import { Build, Compile } from "typebox/schema";
18
+ import createMirror from "exact-mirror";
19
+
20
+ //#region src/type/validator/index.ts
21
+ const moduleCache = /* @__PURE__ */ new WeakMap();
22
+ function schemaContainsRef(node, seen = /* @__PURE__ */ new WeakSet()) {
23
+ if (!node || typeof node !== "object" || seen.has(node)) return false;
24
+ seen.add(node);
25
+ if (node.$ref) return true;
26
+ const props = node.properties;
27
+ if (props) {
28
+ for (const k in props) if (schemaContainsRef(props[k], seen)) return true;
29
+ }
30
+ const items = node.items;
31
+ if (Array.isArray(items)) {
32
+ for (const it of items) if (schemaContainsRef(it, seen)) return true;
33
+ } else if (items && schemaContainsRef(items, seen)) return true;
34
+ for (const key of [
35
+ "anyOf",
36
+ "allOf",
37
+ "oneOf"
38
+ ]) {
39
+ const arr = node[key];
40
+ if (Array.isArray(arr)) {
41
+ for (const x of arr) if (schemaContainsRef(x, seen)) return true;
42
+ }
43
+ }
44
+ if (node.additionalProperties && typeof node.additionalProperties === "object" && schemaContainsRef(node.additionalProperties, seen)) return true;
45
+ if (node.not && schemaContainsRef(node.not, seen)) return true;
46
+ const pp = node.patternProperties;
47
+ if (pp) {
48
+ for (const k in pp) if (schemaContainsRef(pp[k], seen)) return true;
49
+ }
50
+ return false;
51
+ }
52
+ function divergesFromEvaluate(node, seen) {
53
+ if (!node || typeof node !== "object" || seen.has(node)) return false;
54
+ seen.add(node);
55
+ for (const k in node) {
56
+ if (!Object.hasOwn(node, k)) return true;
57
+ const v = node[k];
58
+ if (typeof v === "object" && v && divergesFromEvaluate(v, seen)) return true;
59
+ }
60
+ return false;
61
+ }
62
+ const SIMPLE_OBJECT_KEYS = new Set([
63
+ "type",
64
+ "properties",
65
+ "required"
66
+ ]);
67
+ function shallowMergeObjects(members) {
68
+ const properties = {};
69
+ let required;
70
+ for (const m of members) {
71
+ if (!m || m["~kind"] !== "Object" || m["~elyTyp"] !== void 0 || !m.properties || divergesFromEvaluate(m, /* @__PURE__ */ new WeakSet())) return null;
72
+ for (const k of Object.keys(m)) if (!SIMPLE_OBJECT_KEYS.has(k)) return null;
73
+ for (const k in m.properties) {
74
+ if (k in properties) return null;
75
+ properties[k] = m.properties[k];
76
+ }
77
+ if (Array.isArray(m.required) && m.required.length) (required ??= []).push(...m.required);
78
+ }
79
+ const out = {
80
+ type: "object",
81
+ properties
82
+ };
83
+ if (required) out.required = required;
84
+ return Object.defineProperty(out, "~kind", {
85
+ value: "Object",
86
+ enumerable: false
87
+ });
88
+ }
89
+ let inlineRefId = 0;
90
+ const isAsyncPredicate = (v) => Array.isArray(v) ? v.some((x) => typeof x.check === "function" ? isAsyncFunction(x.check) || x.check["~elyAsyncRefine"] === true : false) : false;
91
+ async function enforceFileTypeChecks(pending, type, value, schema) {
92
+ const results = await Promise.all(pending.map((x) => x.check));
93
+ for (let i = 0; i < results.length; i++) if (results[i] !== true) throw new ValidationError(type, value, [{
94
+ instancePath: findInstancePath(value, pending[i].file) ?? "",
95
+ message: results[i]
96
+ }], schema);
97
+ }
98
+ function findInstancePath(value, target, path = "") {
99
+ if (value === target) return path;
100
+ if (!value || typeof value !== "object") return;
101
+ if (Array.isArray(value)) {
102
+ for (let i = 0; i < value.length; i++) {
103
+ const found = findInstancePath(value[i], target, `${path}/${i}`);
104
+ if (found !== void 0) return found;
105
+ }
106
+ return;
107
+ }
108
+ for (const key in value) {
109
+ const found = findInstancePath(value[key], target, `${path}/${key}`);
110
+ if (found !== void 0) return found;
111
+ }
112
+ }
113
+ function externalsShape(schema) {
114
+ let out = "";
115
+ for (const e of collectExternals(schema)) out += e instanceof RegExp ? "r" : typeof e === "function" ? "f" : "v";
116
+ return out;
117
+ }
118
+ function sourceOnlyValidator(schema) {
119
+ const buildResult = Build(schema);
120
+ let full;
121
+ return new Proxy({}, { get(_, prop) {
122
+ if (prop === "buildResult") return buildResult;
123
+ const f = full ??= Compile(schema);
124
+ const value = f[prop];
125
+ return typeof value === "function" ? value.bind(f) : value;
126
+ } });
127
+ }
128
+ var TypeBoxValidator = class extends Validator$1 {
129
+ #decodeMirror;
130
+ #encodeMirror;
131
+ #findCustomError;
132
+ #defaultFastPath;
133
+ #noValidate;
134
+ #isForm;
135
+ #hasOptional;
136
+ constructor(schema, options, name, isIntersectable) {
137
+ super();
138
+ this.precomputeSafe = false;
139
+ this.#isForm = false;
140
+ this.#hasOptional = false;
141
+ if (isIntersectable) {
142
+ const members = [schema, ...options.schemas];
143
+ schema = shallowMergeObjects(members) ?? Evaluate(Intersect(members));
144
+ }
145
+ const originalElyTyp = schema?.["~elyTyp"];
146
+ const frozen = options?.aot && options.slot ? Compiled.getValidator(options.aot.method, options.aot.path, options.slot) : void 0;
147
+ let schemaHasRef = false;
148
+ if (name && options?.models) schema = moduleCache.getOrInsertComputed(options.models, () => Module(options.models))[name];
149
+ else if (options?.models && typeof name !== "string") {
150
+ schemaHasRef = frozen ? frozen.r === 1 : schemaContainsRef(schema);
151
+ if (schemaHasRef) {
152
+ const id = `inline@${++inlineRefId}`;
153
+ schema = Module({
154
+ ...options.models,
155
+ [id]: schema
156
+ })[id];
157
+ }
158
+ }
159
+ const isFrozen = this.#reconstruct(options, frozen);
160
+ this.schema = isFrozen && frozen.cp ? buildCoercedFromPlan(schema, frozen.cp) : isFrozen && !this.hasCodec ? schema : applyCoercions(schema, options?.coerces);
161
+ if (options?.normalize === false && options.slot !== "headers" && options.slot !== "cookie") this.schema = nonAdditionalProperties(this.schema);
162
+ if (!isFrozen) {
163
+ const capturing = Capture.isCapturing();
164
+ this.tb = capturing ? sourceOnlyValidator(this.schema) : Compile(this.schema);
165
+ this.hasCodec = HasCodec(this.schema);
166
+ this.isAsync = this.tb.buildResult.external.variables.some(isAsyncPredicate) ?? false;
167
+ this.hasDefault = hasProperty("default", this.schema);
168
+ if (capturing) this.#maybeCapture(options, schemaHasRef, schema);
169
+ else this.#dropCompiledSource();
170
+ }
171
+ if (frozen?.ps === 1) {
172
+ const objectTemplate = frozen.pod !== void 0 ? Object.freeze(frozen.pod) : void 0;
173
+ this.precomputeSafe = true;
174
+ this.#defaultFastPath = {
175
+ value: frozen.pd,
176
+ appliesToNull: frozen.pn === 1,
177
+ objectTemplate,
178
+ clone: frozen.dc,
179
+ merge: frozen.pm
180
+ };
181
+ } else {
182
+ const defaults = this.hasDefault ? verifyPreallocatableDefault(this.schema, Capture.isCapturing()) : void 0;
183
+ if (defaults) {
184
+ this.precomputeSafe = true;
185
+ const objectTemplate = defaults.pod !== void 0 ? Object.freeze(defaults.pod) : void 0;
186
+ this.#defaultFastPath = {
187
+ value: defaults.pd,
188
+ appliesToNull: defaults.pn,
189
+ objectTemplate,
190
+ clone: defaults.pd !== void 0 ? createDefaultCloner(defaults.pd) : void 0,
191
+ merge: defaults.ms ? createMergerFromSource(defaults.ms) : objectTemplate !== void 0 ? createObjectDefaultMerger(objectTemplate) : void 0
192
+ };
193
+ } else {
194
+ this.precomputeSafe = false;
195
+ this.#defaultFastPath = void 0;
196
+ }
197
+ }
198
+ this.#noValidate = originalElyTyp === ELYSIA_TYPES.NoValidate;
199
+ this.#isForm = originalElyTyp === ELYSIA_TYPES.Form;
200
+ this.#hasOptional = !!this.schema?.["~optional"];
201
+ if (frozen?.ic) reconstructInnerCodecs(frozen.ic, this.schema);
202
+ if (isFrozen && frozen.cm) {
203
+ const both = instantiateFrozenBoth(frozen, this.schema, schema);
204
+ this.reconstructedCheck = both.check;
205
+ this.Clean = options?.normalize === false ? void 0 : both.clean;
206
+ } else {
207
+ if (isFrozen) this.reconstructedCheck = frozen.c(frozen.e ? collectExternals(this.schema) : EMPTY_EXTERNALS);
208
+ try {
209
+ this.Clean = options?.normalize === false ? void 0 : options?.normalize === "typebox" ? (value) => Clean(this.schema, value) : this.#setupMirror(schema, options, frozen);
210
+ } catch (error) {
211
+ console.warn("Failed to create exactMirror. Please report the following code to https://github.com/elysiajs/elysia/issues");
212
+ console.warn(schema);
213
+ console.warn(error);
214
+ if (options?.normalize !== false) this.Clean = (value) => Clean(this.schema, value);
215
+ }
216
+ }
217
+ if (this.hasCodec && !this.#isForm && !this.#noValidate && !options?.slot?.startsWith("r") && options?.normalize !== false && options?.normalize !== "typebox") this.#decodeMirror = this.#setupCodecMirror(this.schema, options, frozen, "decode");
218
+ if (this.hasCodec && !this.#isForm && !this.#noValidate && options?.slot?.startsWith("r") && options?.normalize !== false && options?.normalize !== "typebox") this.#encodeMirror = this.#setupCodecMirror(this.schema, options, frozen, "encode");
219
+ if (!this.#noValidate) this.#findCustomError = buildFindCustomError(this.schema, frozen);
220
+ }
221
+ #error(value, type) {
222
+ return new ValidationError(type, value, () => this.Errors(value), this.schema, this.#findCustomError);
223
+ }
224
+ #setupMirror(schema, options, frozen) {
225
+ const aot = options?.aot;
226
+ const slot = options?.slot;
227
+ if (aot && slot && frozen?.m) {
228
+ const m = frozen.m;
229
+ let clean;
230
+ return (value) => {
231
+ if (clean === void 0) try {
232
+ clean = instantiateFrozenMirror(m, schema);
233
+ } catch (error) {
234
+ console.warn("Failed to create exactMirror. Please report the following code to https://github.com/elysiajs/elysia/issues");
235
+ console.warn(schema);
236
+ console.warn(error);
237
+ clean = (v) => v;
238
+ }
239
+ return clean(value);
240
+ };
241
+ }
242
+ if (aot && slot) {
243
+ if (Capture.isCapturing()) try {
244
+ const emitted = createMirror(schema, {
245
+ Compile,
246
+ sanitize: options?.sanitize,
247
+ emit: true
248
+ });
249
+ if (typeof emitted?.source === "string") {
250
+ const ext = emitted.externals;
251
+ if (!ext) Capture.set({
252
+ method: aot.method,
253
+ path: aot.path,
254
+ slot
255
+ }, { mirror: {
256
+ source: emitted.source,
257
+ hasExternals: false
258
+ } });
259
+ else if (ext.unions && !ext.hof) {
260
+ const u = Capture.mirrorUnions(schema, ext.unions);
261
+ if (u) Capture.set({
262
+ method: aot.method,
263
+ path: aot.path,
264
+ slot
265
+ }, { mirror: {
266
+ source: emitted.source,
267
+ hasExternals: true,
268
+ u
269
+ } });
270
+ }
271
+ }
272
+ } catch {}
273
+ }
274
+ return createMirror(schema, {
275
+ Compile,
276
+ sanitize: options?.sanitize
277
+ });
278
+ }
279
+ #setupCodecMirror(schema, options, frozen, dir) {
280
+ const aot = options?.aot;
281
+ const slot = options?.slot;
282
+ const frozenMirror = dir === "decode" ? frozen?.dm : frozen?.em;
283
+ if (aot && slot && frozenMirror) {
284
+ const m = frozenMirror;
285
+ let run;
286
+ return (value) => {
287
+ if (run === void 0) try {
288
+ run = instantiateFrozenDecodeMirror(m, schema, dir);
289
+ } catch {
290
+ run = dir === "decode" ? (v) => {
291
+ const decoded = DecodeUnsafe(nullObject(), schema, v);
292
+ return this.Clean ? this.Clean(decoded) : decoded;
293
+ } : (v) => {
294
+ const out = Encode(schema, v);
295
+ return this.Clean ? this.Clean(out) : out;
296
+ };
297
+ }
298
+ return run(value);
299
+ };
300
+ }
301
+ const dirOpt = dir === "decode" ? { decode: true } : { encode: true };
302
+ const captureSlot = dir === "decode" ? !slot?.startsWith("response") : !!slot?.startsWith("response");
303
+ if (aot && slot && Capture.isCapturing() && captureSlot) try {
304
+ const emitted = createMirror(schema, {
305
+ Compile,
306
+ sanitize: options?.sanitize,
307
+ ...dirOpt,
308
+ emit: true
309
+ });
310
+ if (typeof emitted?.source === "string") {
311
+ const ext = emitted.externals;
312
+ if (ext?.codecs && !ext.hof && Capture.mirrorCodecs(schema, ext.codecs, dir)) {
313
+ let u;
314
+ let freezable = true;
315
+ if (ext.unions && ext.unions.length) {
316
+ u = Capture.mirrorUnions(schema, ext.unions);
317
+ if (!u) freezable = false;
318
+ }
319
+ if (freezable) {
320
+ const mirror = {
321
+ source: emitted.source,
322
+ hasExternals: true,
323
+ u
324
+ };
325
+ Capture.set({
326
+ method: aot.method,
327
+ path: aot.path,
328
+ slot
329
+ }, dir === "decode" ? { decodeMirror: mirror } : { encodeMirror: mirror });
330
+ }
331
+ }
332
+ }
333
+ } catch {}
334
+ try {
335
+ return createMirror(schema, {
336
+ Compile,
337
+ sanitize: options?.sanitize,
338
+ ...dirOpt
339
+ });
340
+ } catch {}
341
+ }
342
+ Check(value) {
343
+ if (this.reconstructedCheck) return this.reconstructedCheck(value);
344
+ return this.tb.Check(value);
345
+ }
346
+ #reconstruct(options, frozen) {
347
+ if (!options?.aot || !options.slot || options.normalize === "typebox") return false;
348
+ if (!frozen?.c && !frozen?.cm) return false;
349
+ this.isAsync = frozen.a === 1;
350
+ this.hasDefault = frozen.d === 1;
351
+ this.hasCodec = frozen.k === 1;
352
+ return true;
353
+ }
354
+ #maybeCapture(options, hasRef, originalSchema) {
355
+ const aot = options?.aot;
356
+ const slot = options?.slot;
357
+ if (!aot || !slot || !Capture.isCapturing()) return;
358
+ if (this.hasCodec && !hasRef && options.coerces && options.normalize !== false && options.normalize !== "typebox") {
359
+ const plan = captureCoercePlan(originalSchema, this.schema);
360
+ if (plan && externalsShape(buildCoercedFromPlan(originalSchema, plan)) === externalsShape(this.schema)) Capture.set({
361
+ method: aot.method,
362
+ path: aot.path,
363
+ slot
364
+ }, { coercePlan: plan });
365
+ }
366
+ const defaultFastPathCapture = {
367
+ precomputeSafe: void 0,
368
+ precomputedDefault: void 0,
369
+ precomputeNull: void 0,
370
+ precomputedObjectDefault: void 0,
371
+ defaultCloner: void 0,
372
+ objectDefaultMerger: void 0
373
+ };
374
+ if (this.hasDefault) {
375
+ const defaults = verifyPreallocatableDefault(this.schema);
376
+ if (defaults) {
377
+ defaultFastPathCapture.precomputeSafe = true;
378
+ defaultFastPathCapture.precomputedDefault = defaults.pd;
379
+ defaultFastPathCapture.precomputeNull = defaults.pn;
380
+ defaultFastPathCapture.precomputedObjectDefault = defaults.pod;
381
+ defaultFastPathCapture.defaultCloner = defaults.pd !== void 0 ? buildDefaultClonerSource(defaults.pd) : void 0;
382
+ defaultFastPathCapture.objectDefaultMerger = defaults.ms ?? (defaults.pod !== void 0 ? buildObjectDefaultMergeSource(defaults.pod) : void 0);
383
+ }
384
+ }
385
+ Capture.set({
386
+ method: aot.method,
387
+ path: aot.path,
388
+ slot
389
+ }, defaultFastPathCapture);
390
+ const customErrors = captureCustomErrors(this.schema);
391
+ if (customErrors) Capture.set({
392
+ method: aot.method,
393
+ path: aot.path,
394
+ slot
395
+ }, { customErrors });
396
+ const innerCodecs = captureStringCodecEntries(this.schema, options?.sanitize);
397
+ if (innerCodecs) Capture.set({
398
+ method: aot.method,
399
+ path: aot.path,
400
+ slot
401
+ }, { innerCodecs });
402
+ const build = this.tb.buildResult;
403
+ const cf = buildFrozenCheck(build, this.schema);
404
+ if (!cf) return;
405
+ Capture.set({
406
+ method: aot.method,
407
+ path: aot.path,
408
+ slot
409
+ }, {
410
+ ...cf,
411
+ async: build.external.variables.some(isAsyncPredicate),
412
+ hasDefault: this.hasDefault,
413
+ hasCodec: this.hasCodec,
414
+ hasRef
415
+ });
416
+ }
417
+ #dropCompiledSource() {
418
+ const tb = this.tb;
419
+ if (!tb) return;
420
+ if (tb.evaluateResult) tb.evaluateResult.code = void 0;
421
+ if (tb.buildResult) tb.buildResult.functions = void 0;
422
+ }
423
+ Errors(value) {
424
+ return Errors(this.schema, value);
425
+ }
426
+ Decode(value) {
427
+ return Decode$1(this.schema, value);
428
+ }
429
+ Encode(value) {
430
+ return this.hasCodec ? Encode(this.schema, value) : value;
431
+ }
432
+ EncodeFrom(value, type) {
433
+ if (this.#isForm) {
434
+ if (!this.#noValidate && !this.Check(value)) throw this.#error(value, type);
435
+ return value;
436
+ }
437
+ if (!this.hasCodec) {
438
+ if (!this.#noValidate && !this.Check(value)) throw this.#error(value, type);
439
+ if (this.Clean) value = this.Clean(value);
440
+ return value;
441
+ }
442
+ try {
443
+ if (this.#encodeMirror) {
444
+ const out = this.#encodeMirror(value);
445
+ if (!this.#noValidate && !this.Check(out)) throw new ValidationError(type, out, () => this.Errors(out), this.schema);
446
+ return out;
447
+ }
448
+ const out = this.#noValidate ? EncodeUnsafe(nullObject(), this.schema, value) : Encode(this.schema, value);
449
+ return this.Clean ? this.Clean(out) : out;
450
+ } catch (e) {
451
+ if (this.#noValidate) return this.Clean ? this.Clean(value) : value;
452
+ if (e instanceof ValidationError) throw e;
453
+ if (e?.error) throw e.error;
454
+ if (e?.status) throw e;
455
+ throw new ValidationError(type, value, () => this.Errors(value), this.schema);
456
+ }
457
+ }
458
+ #markForm(value) {
459
+ if (this.#isForm && value !== null && typeof value === "object" && !("~ely-form" in value)) Object.defineProperty(value, "~ely-form", {
460
+ value: 1,
461
+ configurable: true
462
+ });
463
+ }
464
+ #unmarkForm(value) {
465
+ if (this.#isForm && value !== null && typeof value === "object" && "~ely-form" in value && Object.getOwnPropertyDescriptor(value, "~ely-form")?.configurable) delete value["~ely-form"];
466
+ }
467
+ From(value, type) {
468
+ return this.isAsync ? this.FromAsync(value, type) : this.FromSync(value, type);
469
+ }
470
+ #cloneSharedDefault() {
471
+ const defaults = this.#defaultFastPath;
472
+ const value = defaults.value;
473
+ if (value === null || typeof value !== "object") return value;
474
+ return defaults.clone ? defaults.clone() : structuredClone(value);
475
+ }
476
+ #applyPrecomputedObjectDefault(value) {
477
+ const defaults = this.#defaultFastPath;
478
+ if (defaults.merge) return defaults.merge(value);
479
+ if (Array.isArray(value)) return value;
480
+ return applyPrecomputed(defaults.objectTemplate, value);
481
+ }
482
+ optionalBypass(value) {
483
+ const schema = this.schema;
484
+ if (!schema?.["~optional"]) return;
485
+ if (value === void 0 || value === null) return {
486
+ bypass: true,
487
+ value: schema["~kind"] === "Object" ? nullObject() : value
488
+ };
489
+ if (schema["~kind"] === "Object" && typeof value === "object" && !Array.isArray(value) && Object.keys(value).length === 0) return {
490
+ bypass: true,
491
+ value: nullObject()
492
+ };
493
+ }
494
+ async FromAsync(value, type) {
495
+ if (this.hasDefault) {
496
+ const defaults = this.#defaultFastPath;
497
+ if (defaults) {
498
+ if (value === void 0 || value === null && defaults.appliesToNull) value = this.#cloneSharedDefault();
499
+ else if (value !== null && typeof value === "object" && (defaults.merge !== void 0 || defaults.objectTemplate !== void 0)) value = this.#applyPrecomputedObjectDefault(value);
500
+ } else value = Default(this.schema, value);
501
+ }
502
+ if (this.#hasOptional) {
503
+ const bypass = this.optionalBypass(value);
504
+ if (bypass) return bypass.value;
505
+ }
506
+ if (this.#isForm) this.#markForm(value);
507
+ if (this.hasCodec) {
508
+ if (!this.#noValidate) {
509
+ collectFileTypeChecks();
510
+ const valid = this.Check(value);
511
+ const pendingFile = takeFileTypeChecks();
512
+ if (!valid) throw this.#error(value, type);
513
+ if (pendingFile) await enforceFileTypeChecks(pendingFile, type, value, this.schema);
514
+ }
515
+ if (this.#decodeMirror) value = this.#decodeMirror(value);
516
+ else try {
517
+ value = DecodeUnsafe(nullObject(), this.schema, value);
518
+ } catch (e) {
519
+ if (e instanceof ValidationError) throw e;
520
+ if (e?.error) throw e.error;
521
+ if (e?.status) throw e;
522
+ throw new ValidationError(type, value, () => this.Errors(value), this.schema);
523
+ }
524
+ } else if (!this.#noValidate) {
525
+ collectFileTypeChecks();
526
+ const valid = this.Check(value);
527
+ const pendingFile = takeFileTypeChecks();
528
+ if (!valid) throw this.#error(value, type);
529
+ if (pendingFile) await enforceFileTypeChecks(pendingFile, type, value, this.schema);
530
+ }
531
+ if (this.Clean && !this.#decodeMirror) value = this.Clean(value);
532
+ if (this.#isForm) this.#unmarkForm(value);
533
+ return value;
534
+ }
535
+ FromSync(value, type) {
536
+ if (this.hasDefault) {
537
+ const defaults = this.#defaultFastPath;
538
+ if (defaults) {
539
+ if (value === void 0 || value === null && defaults.appliesToNull) value = this.#cloneSharedDefault();
540
+ else if (value !== null && typeof value === "object" && (defaults.merge !== void 0 || defaults.objectTemplate !== void 0)) value = this.#applyPrecomputedObjectDefault(value);
541
+ } else value = Default(this.schema, value);
542
+ }
543
+ if (this.#hasOptional) {
544
+ const bypass = this.optionalBypass(value);
545
+ if (bypass) return bypass.value;
546
+ }
547
+ if (this.#isForm) this.#markForm(value);
548
+ if (this.hasCodec) {
549
+ if (!this.#noValidate && !this.Check(value)) throw this.#error(value, type);
550
+ if (this.#decodeMirror) value = this.#decodeMirror(value);
551
+ else try {
552
+ value = DecodeUnsafe(nullObject(), this.schema, value);
553
+ } catch (e) {
554
+ if (e instanceof ValidationError) throw e;
555
+ if (e?.error) throw e.error;
556
+ if (e?.status) throw e;
557
+ throw new ValidationError(type, value, () => this.Errors(value), this.schema);
558
+ }
559
+ } else if (!this.#noValidate && !this.Check(value)) throw this.#error(value, type);
560
+ if (this.Clean && !this.#decodeMirror) value = this.Clean(value);
561
+ if (this.#isForm) this.#unmarkForm(value);
562
+ return value;
563
+ }
564
+ };
565
+
566
+ //#endregion
567
+ export { TypeBoxValidator, TypeBoxValidatorCache, shallowMergeObjects };
@@ -0,0 +1,9 @@
1
+ import { CapturedValidator, FrozenValidator } from "../../compile/aot.js";
2
+ import { ValidatorOptions } from "../../validator/index.js";
3
+ import { TSchema } from "typebox/type";
4
+
5
+ //#region src/type/validator/string-codec-aot.d.ts
6
+ declare function reconstructInnerCodecs(ic: NonNullable<FrozenValidator['ic']>, schema: any): void;
7
+ declare function captureStringCodecEntries(schema: TSchema, sanitize: ValidatorOptions['sanitize']): CapturedValidator['innerCodecs'] | undefined;
8
+ //#endregion
9
+ export { captureStringCodecEntries, reconstructInnerCodecs };
@@ -0,0 +1,106 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
+ const require_runtime = require('../../_virtual/_rolldown/runtime.js');
3
+ const require_compile_aot = require('../../compile/aot.js');
4
+ const require_type_constants = require('../constants.js');
5
+ const require_type_utils = require('../utils.js');
6
+ const require_type_validator_frozen_check = require('./frozen-check.js');
7
+ let typebox_schema = require("typebox/schema");
8
+ let exact_mirror = require("exact-mirror");
9
+ exact_mirror = require_runtime.__toESM(exact_mirror);
10
+
11
+ //#region src/type/validator/string-codec-aot.ts
12
+ function collectStringCodecNodes(schema, out = []) {
13
+ if (!schema || typeof schema !== "object") return out;
14
+ const ely = schema["~elyTyp"];
15
+ if (ely === require_type_constants.ELYSIA_TYPES.ObjectString || ely === require_type_constants.ELYSIA_TYPES.ArrayString) {
16
+ const inner = schema.anyOf?.[0];
17
+ const codec = schema.anyOf?.[1];
18
+ if (inner && codec?.["~codec"] && codec["~refine"]) out.push({
19
+ inner,
20
+ codec,
21
+ open: ely === require_type_constants.ELYSIA_TYPES.ObjectString ? 123 : 91
22
+ });
23
+ }
24
+ if (schema.properties) for (const k in schema.properties) collectStringCodecNodes(schema.properties[k], out);
25
+ const items = schema.items;
26
+ if (Array.isArray(items)) for (const it of items) collectStringCodecNodes(it, out);
27
+ else if (items) collectStringCodecNodes(items, out);
28
+ if (Array.isArray(schema.anyOf)) for (const b of schema.anyOf) collectStringCodecNodes(b, out);
29
+ return out;
30
+ }
31
+ function reconstructInnerCodecs(ic, schema) {
32
+ const nodes = collectStringCodecNodes(schema);
33
+ for (let i = nodes.length - 1; i >= 0; i--) {
34
+ const entry = ic[i];
35
+ const node = nodes[i];
36
+ if (!entry || !node) continue;
37
+ const innerSchema = node.inner;
38
+ const innerCheck = entry.c(entry.e ? require_compile_aot.collectExternals(innerSchema) : []);
39
+ const innerMirror = entry.d.x ? require_compile_aot.instantiateFrozenDecodeMirror(entry.d, innerSchema) : entry.d.s;
40
+ const open = entry.o;
41
+ node.codec["~refine"][0].check = (v) => {
42
+ if (v.charCodeAt(0) !== open) return false;
43
+ try {
44
+ return innerCheck(JSON.parse(v));
45
+ } catch {
46
+ return false;
47
+ }
48
+ };
49
+ node.codec["~codec"].decode = (v) => innerMirror(JSON.parse(v));
50
+ }
51
+ }
52
+ function captureInnerCodec(inner, open, sanitize) {
53
+ let cf;
54
+ try {
55
+ cf = require_type_validator_frozen_check.buildFrozenCheck((0, typebox_schema.Build)(inner), inner);
56
+ if (!cf) return;
57
+ } catch {
58
+ return;
59
+ }
60
+ let decode;
61
+ try {
62
+ const emitted = (0, exact_mirror.default)(inner, {
63
+ Compile: typebox_schema.Compile,
64
+ sanitize,
65
+ decode: true,
66
+ emit: true
67
+ });
68
+ if (typeof emitted?.source !== "string") return;
69
+ const ext = emitted.externals;
70
+ if (ext?.hof) return;
71
+ if (ext?.codecs && !require_compile_aot.Capture.mirrorCodecs(inner, ext.codecs)) return;
72
+ let u;
73
+ if (ext?.unions && ext.unions.length) {
74
+ u = require_compile_aot.Capture.mirrorUnions(inner, ext.unions);
75
+ if (!u) return;
76
+ }
77
+ decode = {
78
+ source: emitted.source,
79
+ hasExternals: !!(ext?.codecs || u),
80
+ u
81
+ };
82
+ } catch {
83
+ return;
84
+ }
85
+ if (require_type_utils.hasProperty("default", inner)) return;
86
+ return {
87
+ open,
88
+ ...cf,
89
+ decode
90
+ };
91
+ }
92
+ function captureStringCodecEntries(schema, sanitize) {
93
+ const stringCodecs = collectStringCodecNodes(schema);
94
+ if (!stringCodecs.length) return;
95
+ const entries = [];
96
+ for (const { inner, open } of stringCodecs) {
97
+ const entry = captureInnerCodec(inner, open, sanitize);
98
+ if (!entry) break;
99
+ entries.push(entry);
100
+ }
101
+ return entries.length === stringCodecs.length ? entries : void 0;
102
+ }
103
+
104
+ //#endregion
105
+ exports.captureStringCodecEntries = captureStringCodecEntries;
106
+ exports.reconstructInnerCodecs = reconstructInnerCodecs;