elysia 2.0.0-beta.13 → 2.0.0-beta.15

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 (90) hide show
  1. package/dist/adapter/bun/index.d.ts +4 -2
  2. package/dist/adapter/bun/index.js +35 -38
  3. package/dist/adapter/bun/index.mjs +35 -39
  4. package/dist/adapter/utils.js +16 -21
  5. package/dist/adapter/utils.mjs +16 -21
  6. package/dist/base.d.ts +28 -0
  7. package/dist/base.js +109 -59
  8. package/dist/base.mjs +110 -60
  9. package/dist/compile/aot.js +14 -21
  10. package/dist/compile/aot.mjs +14 -21
  11. package/dist/compile/handler/frozen-validator.js +51 -42
  12. package/dist/compile/handler/frozen-validator.mjs +51 -42
  13. package/dist/compile/handler/index.js +57 -50
  14. package/dist/compile/handler/index.mjs +57 -50
  15. package/dist/compile/handler/jit.js +36 -26
  16. package/dist/compile/handler/jit.mjs +37 -27
  17. package/dist/compile/handler/params.js +2 -0
  18. package/dist/compile/handler/params.mjs +3 -1
  19. package/dist/compile/handler/utils.d.ts +2 -2
  20. package/dist/compile/handler/utils.js +115 -111
  21. package/dist/compile/handler/utils.mjs +115 -113
  22. package/dist/context.d.ts +3 -1
  23. package/dist/context.js +2 -0
  24. package/dist/context.mjs +3 -1
  25. package/dist/cookie/config.js +6 -7
  26. package/dist/cookie/config.mjs +6 -7
  27. package/dist/error.d.ts +6 -3
  28. package/dist/error.js +26 -28
  29. package/dist/error.mjs +26 -28
  30. package/dist/handler/fetch.js +2 -1
  31. package/dist/handler/fetch.mjs +3 -2
  32. package/dist/handler/utils.d.ts +3 -1
  33. package/dist/handler/utils.js +19 -1
  34. package/dist/handler/utils.mjs +17 -1
  35. package/dist/index.d.ts +3 -3
  36. package/dist/package.js +1 -1
  37. package/dist/package.mjs +1 -1
  38. package/dist/plugin/aot/core.js +4 -0
  39. package/dist/plugin/aot/core.mjs +4 -0
  40. package/dist/sucrose.js +60 -59
  41. package/dist/sucrose.mjs +60 -59
  42. package/dist/trace.js +6 -9
  43. package/dist/trace.mjs +6 -9
  44. package/dist/type/coerce-plan.js +41 -36
  45. package/dist/type/coerce-plan.mjs +41 -36
  46. package/dist/type/coerce.d.ts +1 -1
  47. package/dist/type/constants.d.ts +1 -1
  48. package/dist/type/elysia/cookie.js +8 -17
  49. package/dist/type/elysia/cookie.mjs +8 -17
  50. package/dist/type/elysia/date.js +6 -7
  51. package/dist/type/elysia/date.mjs +6 -7
  52. package/dist/type/elysia/numeric.js +8 -9
  53. package/dist/type/elysia/numeric.mjs +8 -9
  54. package/dist/type/elysia/problem.d.ts +20 -0
  55. package/dist/type/elysia/problem.js +25 -0
  56. package/dist/type/elysia/problem.mjs +24 -0
  57. package/dist/type/exports.d.ts +3 -2
  58. package/dist/type/exports.js +2 -0
  59. package/dist/type/exports.mjs +2 -1
  60. package/dist/type/index.js +2 -0
  61. package/dist/type/index.mjs +2 -0
  62. package/dist/type/validator/clean-safe.d.ts +1 -2
  63. package/dist/type/validator/clean-safe.js +0 -2
  64. package/dist/type/validator/clean-safe.mjs +1 -2
  65. package/dist/type/validator/custom-error.js +37 -26
  66. package/dist/type/validator/custom-error.mjs +37 -26
  67. package/dist/type/validator/default-precompute.js +24 -26
  68. package/dist/type/validator/default-precompute.mjs +24 -26
  69. package/dist/type/validator/exact-mirror.js +6 -8
  70. package/dist/type/validator/exact-mirror.mjs +6 -8
  71. package/dist/type/validator/index.js +64 -66
  72. package/dist/type/validator/index.mjs +65 -67
  73. package/dist/type/validator/validator-cache.d.ts +2 -2
  74. package/dist/type/validator/validator-cache.js +7 -10
  75. package/dist/type/validator/validator-cache.mjs +6 -9
  76. package/dist/types.d.ts +4 -3
  77. package/dist/universal/file.js +2 -6
  78. package/dist/universal/file.mjs +2 -6
  79. package/dist/utils.d.ts +11 -2
  80. package/dist/utils.js +85 -47
  81. package/dist/utils.mjs +82 -48
  82. package/dist/validator/index.js +14 -18
  83. package/dist/validator/index.mjs +14 -18
  84. package/dist/ws/context.js +9 -11
  85. package/dist/ws/context.mjs +9 -11
  86. package/dist/ws/parser.js +23 -20
  87. package/dist/ws/parser.mjs +23 -20
  88. package/dist/ws/route.js +76 -87
  89. package/dist/ws/route.mjs +76 -87
  90. package/package.json +5 -2
@@ -109,20 +109,19 @@ function containsRefLike(node, seen = /* @__PURE__ */ new WeakSet()) {
109
109
  if (node.$ref !== void 0 || node["~kind"] === "Ref" || node["~kind"] === "This" || node["~kind"] === "Cyclic") return true;
110
110
  return childSchemaSome(node, (child) => containsRefLike(child, seen));
111
111
  }
112
- function hasDivergentDefaultBelow(node) {
113
- const ownKey = "default" in node ? canonical(node.default) : void 0;
114
- let bad = false;
115
- const visit = (n) => {
116
- if (!n || typeof n !== "object") return false;
117
- if ("default" in n && (ownKey === void 0 || canonical(n.default) !== ownKey)) return bad = true;
118
- return childSchemaSome(n, visit);
119
- };
120
- childSchemaSome(node, visit);
121
- return bad;
122
- }
123
112
  function structuralPreallocatable(schema, depth = 0) {
124
113
  if (!schema || typeof schema !== "object") return true;
125
- 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);
114
+ 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)) {
115
+ const ownKey = "default" in schema ? canonical(schema.default) : void 0;
116
+ let bad = false;
117
+ const visit = (n) => {
118
+ if (!n || typeof n !== "object") return false;
119
+ if ("default" in n && (ownKey === void 0 || canonical(n.default) !== ownKey)) return bad = true;
120
+ return childSchemaSome(n, visit);
121
+ };
122
+ childSchemaSome(schema, visit);
123
+ return !bad;
124
+ }
126
125
  if (depth > 0 && schema.default === void 0 && hasDefaultBelow(schema)) return false;
127
126
  if (typeof schema.additionalProperties === "object" && subtreeHasDefault(schema.additionalProperties)) return false;
128
127
  if (schema.patternProperties) {
@@ -295,10 +294,6 @@ function mergeCategory(node) {
295
294
  if ((kind === "Array" || node.type === "array") && node.items) return !Array.isArray(node.items) && subtreeHasDefault(node.items) ? "array" : "identity";
296
295
  return "identity";
297
296
  }
298
- function presentExpr(child, isObject, varName) {
299
- if (child === "") return varName;
300
- return isObject ? `(${varName}!==null&&typeof ${varName}==='object'&&!Array.isArray(${varName})?${child}(${varName}):${varName})` : `(Array.isArray(${varName})?${child}(${varName}):${varName})`;
301
- }
302
297
  function mergeExpression(schema, varName, helpers, memo) {
303
298
  const full = Default(schema, void 0);
304
299
  const child = emitMerger(schema, helpers, memo);
@@ -306,9 +301,13 @@ function mergeExpression(schema, varName, helpers, memo) {
306
301
  const absent = full === void 0 ? void 0 : cloneExpression(full);
307
302
  if (full !== void 0 && absent === void 0) return;
308
303
  const isObject = mergeCategory(schema) === "object";
304
+ let present;
305
+ if (child === "") present = varName;
306
+ else if (isObject) present = `(${varName}!==null&&typeof ${varName}==='object'&&!Array.isArray(${varName})?${child}(${varName}):${varName})`;
307
+ else present = `(Array.isArray(${varName})?${child}(${varName}):${varName})`;
309
308
  return {
310
309
  absent,
311
- present: presentExpr(child, isObject, varName),
310
+ present,
312
311
  child,
313
312
  isObject
314
313
  };
@@ -374,14 +373,6 @@ function emitMerger(node, helpers, memo = /* @__PURE__ */ new WeakMap()) {
374
373
  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}`;
375
374
  return name;
376
375
  }
377
- function buildMergeSource(schema) {
378
- const category = mergeCategory(schema);
379
- if (category !== "object" && category !== "array") return;
380
- const helpers = [];
381
- const root = emitMerger(schema, helpers, /* @__PURE__ */ new WeakMap());
382
- if (!root) return;
383
- return `(function(){${helpers.join(";")};return ${root}})()`;
384
- }
385
376
  let probeImpl;
386
377
  function setDefaultProbeImpl(impl) {
387
378
  probeImpl = impl;
@@ -413,7 +404,14 @@ function verifyPreallocatableDefault(schema, validate = true) {
413
404
  if (containsRefLike(schema)) return;
414
405
  if (!isEmittable(pd, /* @__PURE__ */ new Set(), true) || pod !== void 0 && !isEmittable(pod, /* @__PURE__ */ new Set(), true)) return;
415
406
  const nullDefault = isPrecomputeSafe(schema);
416
- let ms = buildMergeSource(schema);
407
+ let ms;
408
+ const category = mergeCategory(schema);
409
+ if (category !== "object" && category !== "array") ms = void 0;
410
+ else {
411
+ const helpers = [];
412
+ const root = emitMerger(schema, helpers, /* @__PURE__ */ new WeakMap());
413
+ ms = root ? `(function(){${helpers.join(";")};return ${root}})()` : void 0;
414
+ }
417
415
  if (ms !== void 0 && validate && !probe().validateMergeSource(schema, ms)) ms = void 0;
418
416
  if (!nullDefault && ms === void 0) {
419
417
  if (!rootIsObject) return;
@@ -1,14 +1,12 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
2
 
3
3
  //#region src/type/validator/exact-mirror.ts
4
- function loadExactMirror() {
5
- try {
6
- const module = ({}.require ?? globalThis.process?.getBuiltinModule?.("module")?.createRequire(require("url").pathToFileURL(__filename).href))?.("exact-mirror");
7
- const mirror = module?.default ?? module;
8
- return typeof mirror === "function" ? mirror : void 0;
9
- } catch {}
10
- }
11
- let exactMirror = loadExactMirror();
4
+ let exactMirror;
5
+ try {
6
+ const module = ({}.require ?? globalThis.process?.getBuiltinModule?.("module")?.createRequire(require("url").pathToFileURL(__filename).href))?.("exact-mirror");
7
+ const mirror = module?.default ?? module;
8
+ exactMirror = typeof mirror === "function" ? mirror : void 0;
9
+ } catch {}
12
10
  const getExactMirror = () => exactMirror;
13
11
  const setExactMirror = (mirror) => exactMirror = mirror;
14
12
  const exactMirrorRequired = () => /* @__PURE__ */ new Error("exact-mirror is required when using normalize: 'exactMirror' or sanitize. Install it and, if the runtime cannot load CommonJS modules, register it with setupTypebox({ exactMirror }); otherwise use normalize: 'typebox'.");
@@ -1,12 +1,10 @@
1
1
  //#region src/type/validator/exact-mirror.ts
2
- function loadExactMirror() {
3
- try {
4
- const module = (import.meta.require ?? globalThis.process?.getBuiltinModule?.("module")?.createRequire(import.meta.url))?.("exact-mirror");
5
- const mirror = module?.default ?? module;
6
- return typeof mirror === "function" ? mirror : void 0;
7
- } catch {}
8
- }
9
- let exactMirror = loadExactMirror();
2
+ let exactMirror;
3
+ try {
4
+ const module = (import.meta.require ?? globalThis.process?.getBuiltinModule?.("module")?.createRequire(import.meta.url))?.("exact-mirror");
5
+ const mirror = module?.default ?? module;
6
+ exactMirror = typeof mirror === "function" ? mirror : void 0;
7
+ } catch {}
10
8
  const getExactMirror = () => exactMirror;
11
9
  const setExactMirror = (mirror) => exactMirror = mirror;
12
10
  const exactMirrorRequired = () => /* @__PURE__ */ new Error("exact-mirror is required when using normalize: 'exactMirror' or sanitize. Install it and, if the runtime cannot load CommonJS modules, register it with setupTypebox({ exactMirror }); otherwise use normalize: 'typebox'.");
@@ -118,20 +118,6 @@ function collectRefinements(schema, out = /* @__PURE__ */ new Set(), seen = /* @
118
118
  ]) if (Array.isArray(schema[key])) for (const child of schema[key]) collectRefinements(child, out, seen);
119
119
  return out;
120
120
  }
121
- const materializeModels = (models) => {
122
- const out = {};
123
- for (const name in models) {
124
- const schema = models[name];
125
- const value = {};
126
- let source = schema;
127
- while (source && source !== Object.prototype) {
128
- for (const key of Object.getOwnPropertyNames(source)) if (key !== "$id" && !Object.hasOwn(value, key)) Object.defineProperty(value, key, Object.getOwnPropertyDescriptor(source, key));
129
- source = Object.getPrototypeOf(source);
130
- }
131
- out[name] = value;
132
- }
133
- return out;
134
- };
135
121
  function divergesFromEvaluate(node, seen) {
136
122
  if (!node || typeof node !== "object" || seen.has(node)) return false;
137
123
  seen.add(node);
@@ -181,7 +167,6 @@ function schemaMayHaveAsyncRefine(node, seen = /* @__PURE__ */ new WeakSet()) {
181
167
  }
182
168
  return false;
183
169
  }
184
- const schemaHasCustomError = (schema) => require_type_validator_clean_safe.schemaSome(schema, (node) => node.error !== void 0);
185
170
  async function enforceFileTypeChecks(pending, type, value, schema) {
186
171
  const results = await Promise.all(pending.map((x) => x.check));
187
172
  for (let i = 0; i < results.length; i++) if (results[i] !== true) throw new require_error.ValidationError(type, value, [{
@@ -204,24 +189,18 @@ function findInstancePath(value, target, path = "") {
204
189
  if (found !== void 0) return found;
205
190
  }
206
191
  }
207
- function reportSchema(schema) {
192
+ function warnMirrorFailure(schema, error) {
193
+ console.warn("Failed to create exactMirror. Please report the following to https://github.com/elysiajs/elysia/issues");
194
+ let report;
208
195
  try {
209
- return JSON.stringify(schema, (key, value) => key === "secrets" ? "[redacted]" : value);
196
+ report = JSON.stringify(schema, (key, value) => key === "secrets" ? "[redacted]" : value);
210
197
  } catch {
211
- return "[unserializable schema]";
198
+ report = "[unserializable schema]";
212
199
  }
213
- }
214
- function warnMirrorFailure(schema, error) {
215
- console.warn("Failed to create exactMirror. Please report the following code to https://github.com/elysiajs/elysia/issues");
216
- console.warn(reportSchema(schema));
200
+ console.warn(report);
217
201
  console.warn(error);
218
202
  }
219
203
  let warnedMissingMirror = false;
220
- function warnMissingMirror() {
221
- if (warnedMissingMirror) return;
222
- warnedMissingMirror = true;
223
- console.warn("exact-mirror is unavailable, normalization degraded to TypeBox. Install exact-mirror, or bundle it statically with the AOT build plugin ('elysia/plugin/aot') to restore the fast path. Use normalize: 'typebox' to silence this.");
224
- }
225
204
  var TypeBoxValidator = class extends require_validator_index.Validator {
226
205
  #decodeMirror;
227
206
  #encodeMirror;
@@ -252,13 +231,28 @@ var TypeBoxValidator = class extends require_validator_index.Validator {
252
231
  const frozen = options?.aot && options.slot ? require_compile_aot.Compiled.getValidator(options.aot.method, options.aot.path, options.slot, options.app?.["~programId"]) : void 0;
253
232
  let schemaHasRef = false;
254
233
  if (name && options?.models) {
255
- schema = moduleCache.getOrInsertComputed(options.models, () => require_type_typebox_type.Module(materializeModels(options.models)))[name];
234
+ schema = moduleCache.getOrInsertComputed(options.models, () => {
235
+ const module = require_type_typebox_type.Module;
236
+ const models = options.models;
237
+ const out = {};
238
+ for (const modelName in models) {
239
+ const modelSchema = models[modelName];
240
+ const value = {};
241
+ let source = modelSchema;
242
+ while (source && source !== Object.prototype) {
243
+ for (const key of Object.getOwnPropertyNames(source)) if (key !== "$id" && !Object.hasOwn(value, key)) Object.defineProperty(value, key, Object.getOwnPropertyDescriptor(source, key));
244
+ source = Object.getPrototypeOf(source);
245
+ }
246
+ out[modelName] = value;
247
+ }
248
+ return module(out);
249
+ })[name];
256
250
  if (isIntersectable) {
257
251
  const members = [schema, ...options.schemas];
258
252
  schema = shallowMergeObjects(members) ?? require_type_typebox_type.Evaluate(require_type_typebox_type.Intersect(members));
259
253
  }
260
254
  } else if (options?.models && typeof name !== "string") {
261
- schemaHasRef = frozen ? frozen.r === 1 : require_type_validator_clean_safe.schemaContainsRef(schema);
255
+ schemaHasRef = frozen ? frozen.r === 1 : require_type_validator_clean_safe.schemaSome(schema, (n) => !!n.$ref);
262
256
  if (schemaHasRef) {
263
257
  const id = `inline@${++inlineRefId}`;
264
258
  schema = require_type_typebox_type.Module({
@@ -267,14 +261,22 @@ var TypeBoxValidator = class extends require_validator_index.Validator {
267
261
  })[id];
268
262
  }
269
263
  }
270
- const isFrozen = this.#reconstruct(options, frozen);
264
+ let isFrozen;
265
+ if (!options?.aot || !options.slot || options.normalize === "typebox") isFrozen = false;
266
+ else if (!frozen?.c && !frozen?.cm) isFrozen = false;
267
+ else {
268
+ this.isAsync = frozen.a === 1;
269
+ this.hasDefault = frozen.d === 1;
270
+ this.hasCodec = frozen.k === 1;
271
+ isFrozen = true;
272
+ }
271
273
  this.schema = isFrozen && frozen.cp ? require_type_coerce.buildCoercedFromPlan(schema, frozen.cp) : isFrozen && !this.hasCodec ? schema : require_type_coerce.applyCoercions(schema, options?.coerces);
272
274
  if (options?.normalize === false && options.slot !== "headers" && options.slot !== "cookie") this.schema = require_type_shared.nonAdditionalProperties(this.schema);
273
275
  if (!isFrozen) {
274
276
  const capturing = require_compile_aot.Capture.isCapturing();
275
277
  this.hasCodec = require_type_typebox_value.HasCodec(this.schema);
276
278
  this.hasDefault = require_type_utils.hasProperty("default", this.schema);
277
- if (!capturing && !options?.eager && !schemaMayHaveAsyncRefine(this.schema) && !schemaHasCustomError(this.schema)) {
279
+ if (!capturing && !options?.eager && !schemaMayHaveAsyncRefine(this.schema) && !require_type_validator_clean_safe.schemaSome(this.schema, (node) => node.error !== void 0)) {
278
280
  this.#deferred = true;
279
281
  this.isAsync = false;
280
282
  } else {
@@ -341,10 +343,39 @@ var TypeBoxValidator = class extends require_validator_index.Validator {
341
343
  if (isFrozen) this.reconstructedCheck = frozen.c(frozen.e ? require_compile_aot.reconstruct().collectExternals(this.schema) : require_compile_aot.EMPTY_EXTERNALS);
342
344
  if (!require_type_validator_exact_mirror.getExactMirror() && !frozen?.m && !require_type_validator_clean_safe.schemaHasDangerousProperties(this.schema) && options?.normalize !== false && options?.normalize !== "typebox") {
343
345
  if (options?.normalize === "exactMirror" || options?.sanitize) throw require_type_validator_exact_mirror.exactMirrorRequired();
344
- if (options?.normalize === true) warnMissingMirror();
346
+ if (options?.normalize === true && !warnedMissingMirror) {
347
+ warnedMissingMirror = true;
348
+ console.warn("exact-mirror is unavailable, normalization degraded to TypeBox. Install exact-mirror, or bundle it statically with the AOT build plugin ('elysia/plugin/aot') to restore the fast path. Use normalize: 'typebox' to silence this.");
349
+ }
345
350
  }
346
351
  try {
347
- this.Clean = options?.normalize === false ? void 0 : options?.normalize === "typebox" ? (value) => require_type_typebox_value.Clean(this.schema, value) : this.#setupMirror(schema, options, frozen);
352
+ if (options?.normalize === false) this.Clean = void 0;
353
+ else if (options?.normalize === "typebox") this.Clean = (value) => require_type_typebox_value.Clean(this.schema, value);
354
+ else if (require_type_validator_clean_safe.schemaHasDangerousProperties(this.schema)) this.Clean = (value) => require_type_typebox_value.Clean(this.schema, value);
355
+ else {
356
+ const aot = options?.aot;
357
+ const slot = options?.slot;
358
+ if (aot && slot && frozen?.m) {
359
+ const m = frozen.m;
360
+ let clean;
361
+ this.Clean = (value) => {
362
+ if (clean === void 0) try {
363
+ clean = require_compile_aot.reconstruct().instantiateFrozenMirror(m, schema);
364
+ } catch (error) {
365
+ warnMirrorFailure(schema, error);
366
+ clean = (v) => v;
367
+ }
368
+ return clean(value);
369
+ };
370
+ } else {
371
+ if (aot && slot && require_compile_aot.Capture.isCapturing() && require_compile_aot.captureImpl) require_compile_aot.captureImpl.captureMirror(schema, aot, slot, options?.sanitize);
372
+ const createMirror = require_type_validator_exact_mirror.getExactMirror();
373
+ this.Clean = !createMirror ? (value) => require_type_typebox_value.Clean(this.schema, value) : createMirror(schema, {
374
+ Compile: require_type_typebox_value.SchemaCompile,
375
+ sanitize: options?.sanitize
376
+ });
377
+ }
378
+ }
348
379
  } catch (error) {
349
380
  warnMirrorFailure(schema, error);
350
381
  if (options?.normalize !== false) this.Clean = (value) => require_type_typebox_value.Clean(this.schema, value);
@@ -379,31 +410,6 @@ var TypeBoxValidator = class extends require_validator_index.Validator {
379
410
  if (usingPool) pool.active = false;
380
411
  }
381
412
  }
382
- #setupMirror(schema, options, frozen) {
383
- if (require_type_validator_clean_safe.schemaHasDangerousProperties(this.schema)) return (value) => require_type_typebox_value.Clean(this.schema, value);
384
- const aot = options?.aot;
385
- const slot = options?.slot;
386
- if (aot && slot && frozen?.m) {
387
- const m = frozen.m;
388
- let clean;
389
- return (value) => {
390
- if (clean === void 0) try {
391
- clean = require_compile_aot.reconstruct().instantiateFrozenMirror(m, schema);
392
- } catch (error) {
393
- warnMirrorFailure(schema, error);
394
- clean = (v) => v;
395
- }
396
- return clean(value);
397
- };
398
- }
399
- if (aot && slot && require_compile_aot.Capture.isCapturing() && require_compile_aot.captureImpl) require_compile_aot.captureImpl.captureMirror(schema, aot, slot, options?.sanitize);
400
- const createMirror = require_type_validator_exact_mirror.getExactMirror();
401
- if (!createMirror) return (value) => require_type_typebox_value.Clean(this.schema, value);
402
- return createMirror(schema, {
403
- Compile: require_type_typebox_value.SchemaCompile,
404
- sanitize: options?.sanitize
405
- });
406
- }
407
413
  #setupCodecMirror(schema, options, frozen, dir) {
408
414
  if (require_type_validator_clean_safe.schemaHasDangerousProperties(this.schema)) return;
409
415
  const aot = options?.aot;
@@ -482,14 +488,6 @@ var TypeBoxValidator = class extends require_validator_index.Validator {
482
488
  this.#dropCompiledSource();
483
489
  this.#deferred = false;
484
490
  }
485
- #reconstruct(options, frozen) {
486
- if (!options?.aot || !options.slot || options.normalize === "typebox") return false;
487
- if (!frozen?.c && !frozen?.cm) return false;
488
- this.isAsync = frozen.a === 1;
489
- this.hasDefault = frozen.d === 1;
490
- this.hasCodec = frozen.k === 1;
491
- return true;
492
- }
493
491
  #dropCompiledSource() {
494
492
  const tb = this.tb;
495
493
  if (!tb) return;
@@ -4,7 +4,7 @@ import { ValidationError } from "../../error.mjs";
4
4
  import { Capture, Compiled, EMPTY_EXTERNALS, captureImpl, reconstruct } from "../../compile/aot.mjs";
5
5
  import { isPureRefinement, nonAdditionalProperties } from "../shared.mjs";
6
6
  import { collectFileTypeChecks, isAsyncPredicate, takeFileTypeChecks } from "../elysia/file-type.mjs";
7
- import { isFullyClosedObject, schemaContainsRef, schemaHasDangerousProperties, schemaSome } from "./clean-safe.mjs";
7
+ import { isFullyClosedObject, schemaHasDangerousProperties, schemaSome } from "./clean-safe.mjs";
8
8
  import { hasProperty } from "../utils.mjs";
9
9
  import { Validator } from "../../validator/index.mjs";
10
10
  import { Evaluate, Intersect, Module } from "../typebox-type.mjs";
@@ -117,20 +117,6 @@ function collectRefinements(schema, out = /* @__PURE__ */ new Set(), seen = /* @
117
117
  ]) if (Array.isArray(schema[key])) for (const child of schema[key]) collectRefinements(child, out, seen);
118
118
  return out;
119
119
  }
120
- const materializeModels = (models) => {
121
- const out = {};
122
- for (const name in models) {
123
- const schema = models[name];
124
- const value = {};
125
- let source = schema;
126
- while (source && source !== Object.prototype) {
127
- for (const key of Object.getOwnPropertyNames(source)) if (key !== "$id" && !Object.hasOwn(value, key)) Object.defineProperty(value, key, Object.getOwnPropertyDescriptor(source, key));
128
- source = Object.getPrototypeOf(source);
129
- }
130
- out[name] = value;
131
- }
132
- return out;
133
- };
134
120
  function divergesFromEvaluate(node, seen) {
135
121
  if (!node || typeof node !== "object" || seen.has(node)) return false;
136
122
  seen.add(node);
@@ -180,7 +166,6 @@ function schemaMayHaveAsyncRefine(node, seen = /* @__PURE__ */ new WeakSet()) {
180
166
  }
181
167
  return false;
182
168
  }
183
- const schemaHasCustomError = (schema) => schemaSome(schema, (node) => node.error !== void 0);
184
169
  async function enforceFileTypeChecks(pending, type, value, schema) {
185
170
  const results = await Promise.all(pending.map((x) => x.check));
186
171
  for (let i = 0; i < results.length; i++) if (results[i] !== true) throw new ValidationError(type, value, [{
@@ -203,24 +188,18 @@ function findInstancePath(value, target, path = "") {
203
188
  if (found !== void 0) return found;
204
189
  }
205
190
  }
206
- function reportSchema(schema) {
191
+ function warnMirrorFailure(schema, error) {
192
+ console.warn("Failed to create exactMirror. Please report the following to https://github.com/elysiajs/elysia/issues");
193
+ let report;
207
194
  try {
208
- return JSON.stringify(schema, (key, value) => key === "secrets" ? "[redacted]" : value);
195
+ report = JSON.stringify(schema, (key, value) => key === "secrets" ? "[redacted]" : value);
209
196
  } catch {
210
- return "[unserializable schema]";
197
+ report = "[unserializable schema]";
211
198
  }
212
- }
213
- function warnMirrorFailure(schema, error) {
214
- console.warn("Failed to create exactMirror. Please report the following code to https://github.com/elysiajs/elysia/issues");
215
- console.warn(reportSchema(schema));
199
+ console.warn(report);
216
200
  console.warn(error);
217
201
  }
218
202
  let warnedMissingMirror = false;
219
- function warnMissingMirror() {
220
- if (warnedMissingMirror) return;
221
- warnedMissingMirror = true;
222
- console.warn("exact-mirror is unavailable, normalization degraded to TypeBox. Install exact-mirror, or bundle it statically with the AOT build plugin ('elysia/plugin/aot') to restore the fast path. Use normalize: 'typebox' to silence this.");
223
- }
224
203
  var TypeBoxValidator = class extends Validator {
225
204
  #decodeMirror;
226
205
  #encodeMirror;
@@ -251,13 +230,28 @@ var TypeBoxValidator = class extends Validator {
251
230
  const frozen = options?.aot && options.slot ? Compiled.getValidator(options.aot.method, options.aot.path, options.slot, options.app?.["~programId"]) : void 0;
252
231
  let schemaHasRef = false;
253
232
  if (name && options?.models) {
254
- schema = moduleCache.getOrInsertComputed(options.models, () => Module(materializeModels(options.models)))[name];
233
+ schema = moduleCache.getOrInsertComputed(options.models, () => {
234
+ const module = Module;
235
+ const models = options.models;
236
+ const out = {};
237
+ for (const modelName in models) {
238
+ const modelSchema = models[modelName];
239
+ const value = {};
240
+ let source = modelSchema;
241
+ while (source && source !== Object.prototype) {
242
+ for (const key of Object.getOwnPropertyNames(source)) if (key !== "$id" && !Object.hasOwn(value, key)) Object.defineProperty(value, key, Object.getOwnPropertyDescriptor(source, key));
243
+ source = Object.getPrototypeOf(source);
244
+ }
245
+ out[modelName] = value;
246
+ }
247
+ return module(out);
248
+ })[name];
255
249
  if (isIntersectable) {
256
250
  const members = [schema, ...options.schemas];
257
251
  schema = shallowMergeObjects(members) ?? Evaluate(Intersect(members));
258
252
  }
259
253
  } else if (options?.models && typeof name !== "string") {
260
- schemaHasRef = frozen ? frozen.r === 1 : schemaContainsRef(schema);
254
+ schemaHasRef = frozen ? frozen.r === 1 : schemaSome(schema, (n) => !!n.$ref);
261
255
  if (schemaHasRef) {
262
256
  const id = `inline@${++inlineRefId}`;
263
257
  schema = Module({
@@ -266,14 +260,22 @@ var TypeBoxValidator = class extends Validator {
266
260
  })[id];
267
261
  }
268
262
  }
269
- const isFrozen = this.#reconstruct(options, frozen);
263
+ let isFrozen;
264
+ if (!options?.aot || !options.slot || options.normalize === "typebox") isFrozen = false;
265
+ else if (!frozen?.c && !frozen?.cm) isFrozen = false;
266
+ else {
267
+ this.isAsync = frozen.a === 1;
268
+ this.hasDefault = frozen.d === 1;
269
+ this.hasCodec = frozen.k === 1;
270
+ isFrozen = true;
271
+ }
270
272
  this.schema = isFrozen && frozen.cp ? buildCoercedFromPlan(schema, frozen.cp) : isFrozen && !this.hasCodec ? schema : applyCoercions(schema, options?.coerces);
271
273
  if (options?.normalize === false && options.slot !== "headers" && options.slot !== "cookie") this.schema = nonAdditionalProperties(this.schema);
272
274
  if (!isFrozen) {
273
275
  const capturing = Capture.isCapturing();
274
276
  this.hasCodec = HasCodec(this.schema);
275
277
  this.hasDefault = hasProperty("default", this.schema);
276
- if (!capturing && !options?.eager && !schemaMayHaveAsyncRefine(this.schema) && !schemaHasCustomError(this.schema)) {
278
+ if (!capturing && !options?.eager && !schemaMayHaveAsyncRefine(this.schema) && !schemaSome(this.schema, (node) => node.error !== void 0)) {
277
279
  this.#deferred = true;
278
280
  this.isAsync = false;
279
281
  } else {
@@ -340,10 +342,39 @@ var TypeBoxValidator = class extends Validator {
340
342
  if (isFrozen) this.reconstructedCheck = frozen.c(frozen.e ? reconstruct().collectExternals(this.schema) : EMPTY_EXTERNALS);
341
343
  if (!getExactMirror() && !frozen?.m && !schemaHasDangerousProperties(this.schema) && options?.normalize !== false && options?.normalize !== "typebox") {
342
344
  if (options?.normalize === "exactMirror" || options?.sanitize) throw exactMirrorRequired();
343
- if (options?.normalize === true) warnMissingMirror();
345
+ if (options?.normalize === true && !warnedMissingMirror) {
346
+ warnedMissingMirror = true;
347
+ console.warn("exact-mirror is unavailable, normalization degraded to TypeBox. Install exact-mirror, or bundle it statically with the AOT build plugin ('elysia/plugin/aot') to restore the fast path. Use normalize: 'typebox' to silence this.");
348
+ }
344
349
  }
345
350
  try {
346
- this.Clean = options?.normalize === false ? void 0 : options?.normalize === "typebox" ? (value) => Clean(this.schema, value) : this.#setupMirror(schema, options, frozen);
351
+ if (options?.normalize === false) this.Clean = void 0;
352
+ else if (options?.normalize === "typebox") this.Clean = (value) => Clean(this.schema, value);
353
+ else if (schemaHasDangerousProperties(this.schema)) this.Clean = (value) => Clean(this.schema, value);
354
+ else {
355
+ const aot = options?.aot;
356
+ const slot = options?.slot;
357
+ if (aot && slot && frozen?.m) {
358
+ const m = frozen.m;
359
+ let clean;
360
+ this.Clean = (value) => {
361
+ if (clean === void 0) try {
362
+ clean = reconstruct().instantiateFrozenMirror(m, schema);
363
+ } catch (error) {
364
+ warnMirrorFailure(schema, error);
365
+ clean = (v) => v;
366
+ }
367
+ return clean(value);
368
+ };
369
+ } else {
370
+ if (aot && slot && Capture.isCapturing() && captureImpl) captureImpl.captureMirror(schema, aot, slot, options?.sanitize);
371
+ const createMirror = getExactMirror();
372
+ this.Clean = !createMirror ? (value) => Clean(this.schema, value) : createMirror(schema, {
373
+ Compile: SchemaCompile,
374
+ sanitize: options?.sanitize
375
+ });
376
+ }
377
+ }
347
378
  } catch (error) {
348
379
  warnMirrorFailure(schema, error);
349
380
  if (options?.normalize !== false) this.Clean = (value) => Clean(this.schema, value);
@@ -378,31 +409,6 @@ var TypeBoxValidator = class extends Validator {
378
409
  if (usingPool) pool.active = false;
379
410
  }
380
411
  }
381
- #setupMirror(schema, options, frozen) {
382
- if (schemaHasDangerousProperties(this.schema)) return (value) => Clean(this.schema, value);
383
- const aot = options?.aot;
384
- const slot = options?.slot;
385
- if (aot && slot && frozen?.m) {
386
- const m = frozen.m;
387
- let clean;
388
- return (value) => {
389
- if (clean === void 0) try {
390
- clean = reconstruct().instantiateFrozenMirror(m, schema);
391
- } catch (error) {
392
- warnMirrorFailure(schema, error);
393
- clean = (v) => v;
394
- }
395
- return clean(value);
396
- };
397
- }
398
- if (aot && slot && Capture.isCapturing() && captureImpl) captureImpl.captureMirror(schema, aot, slot, options?.sanitize);
399
- const createMirror = getExactMirror();
400
- if (!createMirror) return (value) => Clean(this.schema, value);
401
- return createMirror(schema, {
402
- Compile: SchemaCompile,
403
- sanitize: options?.sanitize
404
- });
405
- }
406
412
  #setupCodecMirror(schema, options, frozen, dir) {
407
413
  if (schemaHasDangerousProperties(this.schema)) return;
408
414
  const aot = options?.aot;
@@ -481,14 +487,6 @@ var TypeBoxValidator = class extends Validator {
481
487
  this.#dropCompiledSource();
482
488
  this.#deferred = false;
483
489
  }
484
- #reconstruct(options, frozen) {
485
- if (!options?.aot || !options.slot || options.normalize === "typebox") return false;
486
- if (!frozen?.c && !frozen?.cm) return false;
487
- this.isAsync = frozen.a === 1;
488
- this.hasDefault = frozen.d === 1;
489
- this.hasCodec = frozen.k === 1;
490
- return true;
491
- }
492
490
  #dropCompiledSource() {
493
491
  const tb = this.tb;
494
492
  if (!tb) return;
@@ -11,7 +11,7 @@ interface SchemaMeta {
11
11
  key: string | undefined;
12
12
  }
13
13
  declare function fnKey(fn: Function): string;
14
- declare const schemaCacheKey: (schema: TSchema) => string | undefined;
14
+ declare function computeSchemaMeta(schema: TSchema, forceKey: boolean): SchemaMeta;
15
15
  declare const mayHaveFileType: (schema: object) => boolean;
16
16
  declare class TypeBoxValidatorCache {
17
17
  #private;
@@ -23,4 +23,4 @@ declare class TypeBoxValidatorCache {
23
23
  clear(): void;
24
24
  }
25
25
  //#endregion
26
- export { SchemaMeta, TypeBoxValidatorCache, fnKey, mayHaveFileType, schemaCacheKey };
26
+ export { SchemaMeta, TypeBoxValidatorCache, computeSchemaMeta, fnKey, mayHaveFileType };
@@ -142,7 +142,6 @@ function computeSchemaMeta(schema, forceKey) {
142
142
  key: forceKey ? key : special ? "" : key
143
143
  };
144
144
  }
145
- const schemaCacheKey = (schema) => computeSchemaMeta(schema, true).key;
146
145
  const mayHaveFileType = (schema) => !("~standard" in schema) && TypeBoxValidatorCache.meta(schema).hasFileType;
147
146
  var TypeBoxValidatorCache = class TypeBoxValidatorCache {
148
147
  static {
@@ -180,12 +179,6 @@ var TypeBoxValidatorCache = class TypeBoxValidatorCache {
180
179
  constructor(gcTime = DEFAULT_GC_TIME) {
181
180
  this.#gcTime = gcTime;
182
181
  }
183
- #scheduleClear() {
184
- if (require_universal_constants.isCloudflareWorker) return;
185
- if (this.#gc) clearTimeout(this.#gc);
186
- this.#gc = setTimeout(() => this.clear(), this.#gcTime);
187
- this.#gc.unref?.();
188
- }
189
182
  get(schema, coercions = TypeBoxValidatorCache.EMPTY, normalize = "", models) {
190
183
  const meta = TypeBoxValidatorCache.meta(schema);
191
184
  normalize += TypeBoxValidatorCache.#modelsToken(models, meta.hasRef);
@@ -219,7 +212,11 @@ var TypeBoxValidatorCache = class TypeBoxValidatorCache {
219
212
  byNormalize.set(normalize, cache);
220
213
  }
221
214
  set(schema, coercions = TypeBoxValidatorCache.EMPTY, validator, normalize = "", models) {
222
- this.#scheduleClear();
215
+ if (!require_universal_constants.isCloudflareWorker) {
216
+ if (this.#gc) clearTimeout(this.#gc);
217
+ this.#gc = setTimeout(() => this.clear(), this.#gcTime);
218
+ this.#gc.unref?.();
219
+ }
223
220
  const meta = TypeBoxValidatorCache.meta(schema);
224
221
  normalize += TypeBoxValidatorCache.#modelsToken(models, meta.hasRef);
225
222
  if (meta.special) {
@@ -255,6 +252,6 @@ var TypeBoxValidatorCache = class TypeBoxValidatorCache {
255
252
 
256
253
  //#endregion
257
254
  exports.TypeBoxValidatorCache = TypeBoxValidatorCache;
255
+ exports.computeSchemaMeta = computeSchemaMeta;
258
256
  exports.fnKey = fnKey;
259
- exports.mayHaveFileType = mayHaveFileType;
260
- exports.schemaCacheKey = schemaCacheKey;
257
+ exports.mayHaveFileType = mayHaveFileType;
@@ -141,7 +141,6 @@ function computeSchemaMeta(schema, forceKey) {
141
141
  key: forceKey ? key : special ? "" : key
142
142
  };
143
143
  }
144
- const schemaCacheKey = (schema) => computeSchemaMeta(schema, true).key;
145
144
  const mayHaveFileType = (schema) => !("~standard" in schema) && TypeBoxValidatorCache.meta(schema).hasFileType;
146
145
  var TypeBoxValidatorCache = class TypeBoxValidatorCache {
147
146
  static {
@@ -179,12 +178,6 @@ var TypeBoxValidatorCache = class TypeBoxValidatorCache {
179
178
  constructor(gcTime = DEFAULT_GC_TIME) {
180
179
  this.#gcTime = gcTime;
181
180
  }
182
- #scheduleClear() {
183
- if (isCloudflareWorker) return;
184
- if (this.#gc) clearTimeout(this.#gc);
185
- this.#gc = setTimeout(() => this.clear(), this.#gcTime);
186
- this.#gc.unref?.();
187
- }
188
181
  get(schema, coercions = TypeBoxValidatorCache.EMPTY, normalize = "", models) {
189
182
  const meta = TypeBoxValidatorCache.meta(schema);
190
183
  normalize += TypeBoxValidatorCache.#modelsToken(models, meta.hasRef);
@@ -218,7 +211,11 @@ var TypeBoxValidatorCache = class TypeBoxValidatorCache {
218
211
  byNormalize.set(normalize, cache);
219
212
  }
220
213
  set(schema, coercions = TypeBoxValidatorCache.EMPTY, validator, normalize = "", models) {
221
- this.#scheduleClear();
214
+ if (!isCloudflareWorker) {
215
+ if (this.#gc) clearTimeout(this.#gc);
216
+ this.#gc = setTimeout(() => this.clear(), this.#gcTime);
217
+ this.#gc.unref?.();
218
+ }
222
219
  const meta = TypeBoxValidatorCache.meta(schema);
223
220
  normalize += TypeBoxValidatorCache.#modelsToken(models, meta.hasRef);
224
221
  if (meta.special) {
@@ -253,4 +250,4 @@ var TypeBoxValidatorCache = class TypeBoxValidatorCache {
253
250
  };
254
251
 
255
252
  //#endregion
256
- export { TypeBoxValidatorCache, fnKey, mayHaveFileType, schemaCacheKey };
253
+ export { TypeBoxValidatorCache, computeSchemaMeta, fnKey, mayHaveFileType };