elysia 2.0.0-exp.33 → 2.0.0-exp.35

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.
@@ -0,0 +1,7 @@
1
+ import { CaptureImpl } from "./aot.js";
2
+
3
+ //#region src/compile/aot-capture.d.ts
4
+ declare const impl: CaptureImpl;
5
+ declare function installCaptureImpl(): void;
6
+ //#endregion
7
+ export { impl as captureImplementation, installCaptureImpl };
@@ -0,0 +1,178 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
+ const require_runtime = require('../_virtual/_rolldown/runtime.js');
3
+ const require_compile_aot = require('./aot.js');
4
+ const require_compile_handler_frozen_validator = require('./handler/frozen-validator.js');
5
+ const require_type_coerce = require('../type/coerce.js');
6
+ const require_type_validator_default_precompute = require('../type/validator/default-precompute.js');
7
+ const require_type_validator_frozen_check = require('../type/validator/frozen-check.js');
8
+ const require_type_validator_custom_error = require('../type/validator/custom-error.js');
9
+ const require_type_validator_string_codec_aot = require('../type/validator/string-codec-aot.js');
10
+ const require_type_validator_index = require('../type/validator/index.js');
11
+ let typebox_schema = require("typebox/schema");
12
+ let exact_mirror = require("exact-mirror");
13
+ exact_mirror = require_runtime.__toESM(exact_mirror);
14
+
15
+ //#region src/compile/aot-capture.ts
16
+ function sourceOnlyValidator(schema) {
17
+ const buildResult = (0, typebox_schema.Build)(schema);
18
+ let full;
19
+ return new Proxy({}, { get(_, prop) {
20
+ if (prop === "buildResult") return buildResult;
21
+ const f = full ??= (0, typebox_schema.Compile)(schema);
22
+ const value = f[prop];
23
+ return typeof value === "function" ? value.bind(f) : value;
24
+ } });
25
+ }
26
+ function maybeCapture(args) {
27
+ const { aot, slot, hasRef, originalSchema, schema, hasCodec, hasDefault, coerces, normalize, buildResult } = args;
28
+ if (hasCodec && !hasRef && coerces && normalize !== false && normalize !== "typebox") {
29
+ const plan = require_type_coerce.captureCoercePlan(originalSchema, schema);
30
+ if (plan && require_type_validator_index.externalsShape(require_type_coerce.buildCoercedFromPlan(originalSchema, plan)) === require_type_validator_index.externalsShape(schema)) require_compile_aot.Capture.set({
31
+ method: aot.method,
32
+ path: aot.path,
33
+ slot
34
+ }, { coercePlan: plan });
35
+ }
36
+ const defaultFastPathCapture = {
37
+ precomputeSafe: void 0,
38
+ precomputedDefault: void 0,
39
+ precomputeNull: void 0,
40
+ precomputedObjectDefault: void 0,
41
+ defaultCloner: void 0,
42
+ objectDefaultMerger: void 0
43
+ };
44
+ if (hasDefault) {
45
+ const defaults = require_type_validator_default_precompute.verifyPreallocatableDefault(schema);
46
+ if (defaults) {
47
+ defaultFastPathCapture.precomputeSafe = true;
48
+ defaultFastPathCapture.precomputedDefault = defaults.pd;
49
+ defaultFastPathCapture.precomputeNull = defaults.pn;
50
+ defaultFastPathCapture.precomputedObjectDefault = defaults.pod;
51
+ defaultFastPathCapture.defaultCloner = defaults.pd !== void 0 ? require_type_validator_default_precompute.buildDefaultClonerSource(defaults.pd) : void 0;
52
+ defaultFastPathCapture.objectDefaultMerger = defaults.ms ?? (defaults.pod !== void 0 ? require_type_validator_default_precompute.buildObjectDefaultMergeSource(defaults.pod) : void 0);
53
+ }
54
+ }
55
+ require_compile_aot.Capture.set({
56
+ method: aot.method,
57
+ path: aot.path,
58
+ slot
59
+ }, defaultFastPathCapture);
60
+ const customErrors = require_type_validator_custom_error.captureCustomErrors(schema);
61
+ if (customErrors) require_compile_aot.Capture.set({
62
+ method: aot.method,
63
+ path: aot.path,
64
+ slot
65
+ }, { customErrors });
66
+ const innerCodecs = require_type_validator_string_codec_aot.captureStringCodecEntries(schema, args.sanitize);
67
+ if (innerCodecs) require_compile_aot.Capture.set({
68
+ method: aot.method,
69
+ path: aot.path,
70
+ slot
71
+ }, { innerCodecs });
72
+ const cf = require_type_validator_frozen_check.buildFrozenCheck(buildResult, schema);
73
+ if (!cf) return;
74
+ require_compile_aot.Capture.set({
75
+ method: aot.method,
76
+ path: aot.path,
77
+ slot
78
+ }, {
79
+ ...cf,
80
+ async: buildResult.external.variables.some(require_type_validator_index.isAsyncPredicate),
81
+ hasDefault,
82
+ hasCodec,
83
+ hasRef
84
+ });
85
+ }
86
+ function captureMirror(schema, aot, slot, sanitize) {
87
+ try {
88
+ const emitted = (0, exact_mirror.default)(schema, {
89
+ Compile: typebox_schema.Compile,
90
+ sanitize,
91
+ emit: true
92
+ });
93
+ if (typeof emitted?.source === "string") {
94
+ const ext = emitted.externals;
95
+ if (!ext) require_compile_aot.Capture.set({
96
+ method: aot.method,
97
+ path: aot.path,
98
+ slot
99
+ }, { mirror: {
100
+ source: emitted.source,
101
+ hasExternals: false
102
+ } });
103
+ else if (ext.unions && !ext.hof) {
104
+ const u = require_compile_aot.Capture.mirrorUnions(schema, ext.unions);
105
+ if (u) require_compile_aot.Capture.set({
106
+ method: aot.method,
107
+ path: aot.path,
108
+ slot
109
+ }, { mirror: {
110
+ source: emitted.source,
111
+ hasExternals: true,
112
+ u
113
+ } });
114
+ }
115
+ }
116
+ } catch {}
117
+ }
118
+ function captureCodecMirror(schema, aot, slot, sanitize, dir) {
119
+ const dirOpt = dir === "decode" ? { decode: true } : { encode: true };
120
+ try {
121
+ const emitted = (0, exact_mirror.default)(schema, {
122
+ Compile: typebox_schema.Compile,
123
+ sanitize,
124
+ ...dirOpt,
125
+ emit: true
126
+ });
127
+ if (typeof emitted?.source === "string") {
128
+ const ext = emitted.externals;
129
+ if (ext?.codecs && !ext.hof && require_compile_aot.Capture.mirrorCodecs(schema, ext.codecs, dir)) {
130
+ let u;
131
+ let freezable = true;
132
+ if (ext.unions && ext.unions.length) {
133
+ u = require_compile_aot.Capture.mirrorUnions(schema, ext.unions);
134
+ if (!u) freezable = false;
135
+ }
136
+ if (freezable) {
137
+ const mirror = {
138
+ source: emitted.source,
139
+ hasExternals: true,
140
+ u
141
+ };
142
+ require_compile_aot.Capture.set({
143
+ method: aot.method,
144
+ path: aot.path,
145
+ slot
146
+ }, dir === "decode" ? { decodeMirror: mirror } : { encodeMirror: mirror });
147
+ }
148
+ }
149
+ }
150
+ } catch {}
151
+ }
152
+ function captureBridgeFree(aot, slot, rawSchema) {
153
+ const captured = require_compile_aot.Capture.get({
154
+ method: aot.method,
155
+ path: aot.path,
156
+ slot
157
+ });
158
+ if (captured) require_compile_aot.Capture.set({
159
+ method: aot.method,
160
+ path: aot.path,
161
+ slot
162
+ }, { bridgeFree: require_compile_handler_frozen_validator.isCapturedBridgeFree(captured, rawSchema, captured.coercePlan && typeof rawSchema !== "string" ? require_type_coerce.buildCoercedFromPlan(rawSchema, captured.coercePlan) : rawSchema) });
163
+ }
164
+ const impl = {
165
+ sourceOnlyValidator,
166
+ maybeCapture,
167
+ captureMirror,
168
+ captureCodecMirror,
169
+ captureBridgeFree
170
+ };
171
+ function installCaptureImpl() {
172
+ require_compile_aot.setCaptureImpl(impl);
173
+ }
174
+ installCaptureImpl();
175
+
176
+ //#endregion
177
+ exports.captureImplementation = impl;
178
+ exports.installCaptureImpl = installCaptureImpl;
@@ -0,0 +1,174 @@
1
+ import { Capture, setCaptureImpl } from "./aot.mjs";
2
+ import { isCapturedBridgeFree } from "./handler/frozen-validator.mjs";
3
+ import { buildCoercedFromPlan, captureCoercePlan } from "../type/coerce.mjs";
4
+ import { buildDefaultClonerSource, buildObjectDefaultMergeSource, verifyPreallocatableDefault } from "../type/validator/default-precompute.mjs";
5
+ import { buildFrozenCheck } from "../type/validator/frozen-check.mjs";
6
+ import { captureCustomErrors } from "../type/validator/custom-error.mjs";
7
+ import { captureStringCodecEntries } from "../type/validator/string-codec-aot.mjs";
8
+ import { externalsShape, isAsyncPredicate } from "../type/validator/index.mjs";
9
+ import { Build, Compile } from "typebox/schema";
10
+ import createMirror from "exact-mirror";
11
+
12
+ //#region src/compile/aot-capture.ts
13
+ function sourceOnlyValidator(schema) {
14
+ const buildResult = Build(schema);
15
+ let full;
16
+ return new Proxy({}, { get(_, prop) {
17
+ if (prop === "buildResult") return buildResult;
18
+ const f = full ??= Compile(schema);
19
+ const value = f[prop];
20
+ return typeof value === "function" ? value.bind(f) : value;
21
+ } });
22
+ }
23
+ function maybeCapture(args) {
24
+ const { aot, slot, hasRef, originalSchema, schema, hasCodec, hasDefault, coerces, normalize, buildResult } = args;
25
+ if (hasCodec && !hasRef && coerces && normalize !== false && normalize !== "typebox") {
26
+ const plan = captureCoercePlan(originalSchema, schema);
27
+ if (plan && externalsShape(buildCoercedFromPlan(originalSchema, plan)) === externalsShape(schema)) Capture.set({
28
+ method: aot.method,
29
+ path: aot.path,
30
+ slot
31
+ }, { coercePlan: plan });
32
+ }
33
+ const defaultFastPathCapture = {
34
+ precomputeSafe: void 0,
35
+ precomputedDefault: void 0,
36
+ precomputeNull: void 0,
37
+ precomputedObjectDefault: void 0,
38
+ defaultCloner: void 0,
39
+ objectDefaultMerger: void 0
40
+ };
41
+ if (hasDefault) {
42
+ const defaults = verifyPreallocatableDefault(schema);
43
+ if (defaults) {
44
+ defaultFastPathCapture.precomputeSafe = true;
45
+ defaultFastPathCapture.precomputedDefault = defaults.pd;
46
+ defaultFastPathCapture.precomputeNull = defaults.pn;
47
+ defaultFastPathCapture.precomputedObjectDefault = defaults.pod;
48
+ defaultFastPathCapture.defaultCloner = defaults.pd !== void 0 ? buildDefaultClonerSource(defaults.pd) : void 0;
49
+ defaultFastPathCapture.objectDefaultMerger = defaults.ms ?? (defaults.pod !== void 0 ? buildObjectDefaultMergeSource(defaults.pod) : void 0);
50
+ }
51
+ }
52
+ Capture.set({
53
+ method: aot.method,
54
+ path: aot.path,
55
+ slot
56
+ }, defaultFastPathCapture);
57
+ const customErrors = captureCustomErrors(schema);
58
+ if (customErrors) Capture.set({
59
+ method: aot.method,
60
+ path: aot.path,
61
+ slot
62
+ }, { customErrors });
63
+ const innerCodecs = captureStringCodecEntries(schema, args.sanitize);
64
+ if (innerCodecs) Capture.set({
65
+ method: aot.method,
66
+ path: aot.path,
67
+ slot
68
+ }, { innerCodecs });
69
+ const cf = buildFrozenCheck(buildResult, schema);
70
+ if (!cf) return;
71
+ Capture.set({
72
+ method: aot.method,
73
+ path: aot.path,
74
+ slot
75
+ }, {
76
+ ...cf,
77
+ async: buildResult.external.variables.some(isAsyncPredicate),
78
+ hasDefault,
79
+ hasCodec,
80
+ hasRef
81
+ });
82
+ }
83
+ function captureMirror(schema, aot, slot, sanitize) {
84
+ try {
85
+ const emitted = createMirror(schema, {
86
+ Compile,
87
+ sanitize,
88
+ emit: true
89
+ });
90
+ if (typeof emitted?.source === "string") {
91
+ const ext = emitted.externals;
92
+ if (!ext) Capture.set({
93
+ method: aot.method,
94
+ path: aot.path,
95
+ slot
96
+ }, { mirror: {
97
+ source: emitted.source,
98
+ hasExternals: false
99
+ } });
100
+ else if (ext.unions && !ext.hof) {
101
+ const u = Capture.mirrorUnions(schema, ext.unions);
102
+ if (u) Capture.set({
103
+ method: aot.method,
104
+ path: aot.path,
105
+ slot
106
+ }, { mirror: {
107
+ source: emitted.source,
108
+ hasExternals: true,
109
+ u
110
+ } });
111
+ }
112
+ }
113
+ } catch {}
114
+ }
115
+ function captureCodecMirror(schema, aot, slot, sanitize, dir) {
116
+ const dirOpt = dir === "decode" ? { decode: true } : { encode: true };
117
+ try {
118
+ const emitted = createMirror(schema, {
119
+ Compile,
120
+ sanitize,
121
+ ...dirOpt,
122
+ emit: true
123
+ });
124
+ if (typeof emitted?.source === "string") {
125
+ const ext = emitted.externals;
126
+ if (ext?.codecs && !ext.hof && Capture.mirrorCodecs(schema, ext.codecs, dir)) {
127
+ let u;
128
+ let freezable = true;
129
+ if (ext.unions && ext.unions.length) {
130
+ u = Capture.mirrorUnions(schema, ext.unions);
131
+ if (!u) freezable = false;
132
+ }
133
+ if (freezable) {
134
+ const mirror = {
135
+ source: emitted.source,
136
+ hasExternals: true,
137
+ u
138
+ };
139
+ Capture.set({
140
+ method: aot.method,
141
+ path: aot.path,
142
+ slot
143
+ }, dir === "decode" ? { decodeMirror: mirror } : { encodeMirror: mirror });
144
+ }
145
+ }
146
+ }
147
+ } catch {}
148
+ }
149
+ function captureBridgeFree(aot, slot, rawSchema) {
150
+ const captured = Capture.get({
151
+ method: aot.method,
152
+ path: aot.path,
153
+ slot
154
+ });
155
+ if (captured) Capture.set({
156
+ method: aot.method,
157
+ path: aot.path,
158
+ slot
159
+ }, { bridgeFree: isCapturedBridgeFree(captured, rawSchema, captured.coercePlan && typeof rawSchema !== "string" ? buildCoercedFromPlan(rawSchema, captured.coercePlan) : rawSchema) });
160
+ }
161
+ const impl = {
162
+ sourceOnlyValidator,
163
+ maybeCapture,
164
+ captureMirror,
165
+ captureCodecMirror,
166
+ captureBridgeFree
167
+ };
168
+ function installCaptureImpl() {
169
+ setCaptureImpl(impl);
170
+ }
171
+ installCaptureImpl();
172
+
173
+ //#endregion
174
+ export { impl as captureImplementation, installCaptureImpl };
@@ -213,19 +213,60 @@ declare function captureSet(loc: {
213
213
  path: string;
214
214
  slot: ValidatorSlot;
215
215
  }, partial: Partial<CapturedValidator>): void;
216
- declare function captureGet(loc: {
217
- method: string;
218
- path: string;
219
- slot: ValidatorSlot;
220
- }): CapturedValidator | undefined;
221
216
  declare const Capture: {
222
217
  readonly set: typeof captureSet;
223
- readonly get: typeof captureGet;
218
+ readonly get: (loc: {
219
+ method: string;
220
+ path: string;
221
+ slot: ValidatorSlot;
222
+ }) => CapturedValidator | undefined;
224
223
  readonly handler: typeof captureHandler;
225
224
  readonly mirrorUnions: typeof captureMirrorUnions;
226
225
  readonly mirrorCodecs: typeof captureMirrorCodecs;
227
226
  readonly isCapturing: () => boolean;
228
227
  readonly isAotBuildEnv: () => boolean;
229
228
  };
229
+ interface CaptureImpl {
230
+ /** Source-only TypeBox validator (retains codegen source for the manifest). */
231
+ sourceOnlyValidator(schema: any): any;
232
+ /**
233
+ * Capture the frozen check + defaults + custom-errors + inner-codecs + coerce
234
+ * plan for a validator slot. Mirrors the former `#maybeCapture`.
235
+ */
236
+ maybeCapture(args: {
237
+ aot: {
238
+ method: string;
239
+ path: string;
240
+ };
241
+ slot: ValidatorSlot;
242
+ hasRef: boolean;
243
+ originalSchema: any;
244
+ schema: any;
245
+ hasCodec: boolean;
246
+ hasDefault: boolean;
247
+ coerces: unknown;
248
+ normalize: boolean | 'exactMirror' | 'typebox' | undefined;
249
+ sanitize: unknown;
250
+ buildResult: CheckBuildResult;
251
+ }): void;
252
+ /** Capture the exact-mirror clean emit for a slot (former `#setupMirror`). */
253
+ captureMirror(schema: any, aot: {
254
+ method: string;
255
+ path: string;
256
+ }, slot: ValidatorSlot, sanitize: unknown): void;
257
+ /** Capture the codec decode/encode mirror emit (former `#setupCodecMirror`). */
258
+ captureCodecMirror(schema: any, aot: {
259
+ method: string;
260
+ path: string;
261
+ }, slot: ValidatorSlot, sanitize: unknown, dir: 'decode' | 'encode'): void;
262
+ /** Derive + store the bridge-free marker once every channel is captured. */
263
+ captureBridgeFree(aot: {
264
+ method: string;
265
+ path: string;
266
+ }, slot: ValidatorSlot, rawSchema: unknown): void;
267
+ }
268
+ /** Installed by the AOT build plugin; undefined at runtime. */
269
+ declare let captureImpl: CaptureImpl | undefined;
270
+ declare function setCaptureImpl(impl: CaptureImpl | undefined): void;
230
271
  //#endregion
231
- export { Capture, CapturedHandler, CapturedMirror, CapturedValidator, CheckBuildResult, Compiled, CompiledSnapshot, EMPTY_EXTERNALS, FrozenBothFactory, FrozenCheckFactory, FrozenHandler, FrozenMirror, FrozenMirrorFactory, FrozenValidator, HandlerManifest, Source, StringCodecNode, ValidatorManifest, ValidatorSlot, beginValidatorCapture, collectExternals, collectMirrorUnions, collectStringCodecNodes, endHandlerCapture, endValidatorCapture, externalsMatch, instantiateFrozenBoth, instantiateFrozenDecodeMirror, instantiateFrozenEncodeMirror, instantiateFrozenMirror, reconstructCheck, reconstructInnerCodecs };
272
+ export { Capture, CaptureImpl, CapturedHandler, CapturedMirror, CapturedValidator, CheckBuildResult, Compiled, CompiledSnapshot, EMPTY_EXTERNALS, FrozenBothFactory, FrozenCheckFactory, FrozenHandler, FrozenMirror, FrozenMirrorFactory, FrozenValidator, HandlerManifest, Source, StringCodecNode, ValidatorManifest, ValidatorSlot, beginValidatorCapture, captureImpl, collectExternals, collectMirrorUnions, collectStringCodecNodes, endHandlerCapture, endValidatorCapture, externalsMatch, instantiateFrozenBoth, instantiateFrozenDecodeMirror, instantiateFrozenEncodeMirror, instantiateFrozenMirror, reconstructCheck, reconstructInnerCodecs, setCaptureImpl };
@@ -304,7 +304,9 @@ function captureEntry({ method, path, slot }) {
304
304
  });
305
305
  return e;
306
306
  }
307
+ const aotActivationError = /* @__PURE__ */ new Error("Elysia AOT capture module is not activated.");
307
308
  function beginValidatorCapture() {
309
+ if (captureImpl === void 0) throw aotActivationError;
308
310
  capture = /* @__PURE__ */ new Map();
309
311
  }
310
312
  function endValidatorCapture() {
@@ -327,13 +329,18 @@ function captureSet(loc, partial) {
327
329
  const e = captureEntry(loc);
328
330
  if (e) Object.assign(e, partial);
329
331
  }
330
- function captureGet(loc) {
331
- return capture?.get(`${loc.method}_${loc.path}_${loc.slot}`);
332
- }
332
+ const captureGet = (loc) => capture?.get(`${loc.method}_${loc.path}_${loc.slot}`);
333
333
  const isAotBuildEnv = () => !!require_universal_env.env.ELYSIA_AOT_BUILD;
334
334
  const isValidatorCapturing = () => {
335
- if (capture !== void 0) return true;
336
- return isAotBuildEnv();
335
+ if (capture !== void 0) {
336
+ if (captureImpl === void 0) throw aotActivationError;
337
+ return true;
338
+ }
339
+ if (isAotBuildEnv()) {
340
+ if (captureImpl === void 0) throw aotActivationError;
341
+ return true;
342
+ }
343
+ return false;
337
344
  };
338
345
  const Capture = {
339
346
  set: captureSet,
@@ -344,6 +351,11 @@ const Capture = {
344
351
  isCapturing: isValidatorCapturing,
345
352
  isAotBuildEnv
346
353
  };
354
+ /** Installed by the AOT build plugin; undefined at runtime. */
355
+ let captureImpl;
356
+ function setCaptureImpl(impl) {
357
+ captureImpl = impl;
358
+ }
347
359
 
348
360
  //#endregion
349
361
  exports.Capture = Capture;
@@ -351,6 +363,12 @@ exports.Compiled = Compiled;
351
363
  exports.EMPTY_EXTERNALS = EMPTY_EXTERNALS;
352
364
  exports.Source = Source;
353
365
  exports.beginValidatorCapture = beginValidatorCapture;
366
+ Object.defineProperty(exports, 'captureImpl', {
367
+ enumerable: true,
368
+ get: function () {
369
+ return captureImpl;
370
+ }
371
+ });
354
372
  exports.collectExternals = collectExternals;
355
373
  exports.collectMirrorUnions = collectMirrorUnions;
356
374
  exports.collectStringCodecNodes = collectStringCodecNodes;
@@ -362,4 +380,5 @@ exports.instantiateFrozenDecodeMirror = instantiateFrozenDecodeMirror;
362
380
  exports.instantiateFrozenEncodeMirror = instantiateFrozenEncodeMirror;
363
381
  exports.instantiateFrozenMirror = instantiateFrozenMirror;
364
382
  exports.reconstructCheck = reconstructCheck;
365
- exports.reconstructInnerCodecs = reconstructInnerCodecs;
383
+ exports.reconstructInnerCodecs = reconstructInnerCodecs;
384
+ exports.setCaptureImpl = setCaptureImpl;
@@ -303,7 +303,9 @@ function captureEntry({ method, path, slot }) {
303
303
  });
304
304
  return e;
305
305
  }
306
+ const aotActivationError = /* @__PURE__ */ new Error("Elysia AOT capture module is not activated.");
306
307
  function beginValidatorCapture() {
308
+ if (captureImpl === void 0) throw aotActivationError;
307
309
  capture = /* @__PURE__ */ new Map();
308
310
  }
309
311
  function endValidatorCapture() {
@@ -326,13 +328,18 @@ function captureSet(loc, partial) {
326
328
  const e = captureEntry(loc);
327
329
  if (e) Object.assign(e, partial);
328
330
  }
329
- function captureGet(loc) {
330
- return capture?.get(`${loc.method}_${loc.path}_${loc.slot}`);
331
- }
331
+ const captureGet = (loc) => capture?.get(`${loc.method}_${loc.path}_${loc.slot}`);
332
332
  const isAotBuildEnv = () => !!env.ELYSIA_AOT_BUILD;
333
333
  const isValidatorCapturing = () => {
334
- if (capture !== void 0) return true;
335
- return isAotBuildEnv();
334
+ if (capture !== void 0) {
335
+ if (captureImpl === void 0) throw aotActivationError;
336
+ return true;
337
+ }
338
+ if (isAotBuildEnv()) {
339
+ if (captureImpl === void 0) throw aotActivationError;
340
+ return true;
341
+ }
342
+ return false;
336
343
  };
337
344
  const Capture = {
338
345
  set: captureSet,
@@ -343,6 +350,11 @@ const Capture = {
343
350
  isCapturing: isValidatorCapturing,
344
351
  isAotBuildEnv
345
352
  };
353
+ /** Installed by the AOT build plugin; undefined at runtime. */
354
+ let captureImpl;
355
+ function setCaptureImpl(impl) {
356
+ captureImpl = impl;
357
+ }
346
358
 
347
359
  //#endregion
348
- export { Capture, Compiled, EMPTY_EXTERNALS, Source, beginValidatorCapture, collectExternals, collectMirrorUnions, collectStringCodecNodes, endHandlerCapture, endValidatorCapture, externalsMatch, instantiateFrozenBoth, instantiateFrozenDecodeMirror, instantiateFrozenEncodeMirror, instantiateFrozenMirror, reconstructCheck, reconstructInnerCodecs };
360
+ export { Capture, Compiled, EMPTY_EXTERNALS, Source, beginValidatorCapture, captureImpl, collectExternals, collectMirrorUnions, collectStringCodecNodes, endHandlerCapture, endValidatorCapture, externalsMatch, instantiateFrozenBoth, instantiateFrozenDecodeMirror, instantiateFrozenEncodeMirror, instantiateFrozenMirror, reconstructCheck, reconstructInnerCodecs, setCaptureImpl };
@@ -165,6 +165,8 @@ function compileHandlerJit({ method, path, handler, root, hook, adapter, buildVa
165
165
  const traceHandlers = hook?.trace ?? void 0;
166
166
  const hasTrace = !!traceHandlers?.length;
167
167
  const traceCount = hasTrace ? traceHandlers.length : 0;
168
+ const hasLifecycleHook = parseLength > 0 || !!hook?.transform?.length || hasBeforeHandle || hasAfterHandle || hasMapResponse || hasErrorHook || hasAfterResponse;
169
+ const abortCheck = hasLifecycleHook ? `if(aborted)return emp.clone()\n` : "";
168
170
  const tracePhases = hasTrace ? require_trace.unionTracePhases(traceHandlers) : /* @__PURE__ */ new Set();
169
171
  const phaseOn = (phase) => hasTrace && (tracePhases === null || tracePhases.has(phase));
170
172
  const hasAnyPhase = hasTrace && (tracePhases === null || tracePhases.size > 0);
@@ -217,6 +219,10 @@ function compileHandlerJit({ method, path, handler, root, hook, adapter, buildVa
217
219
  const syncAfterResponse = hasAfterResponse && !isAsync && !hasTrace && !hasErrorHook;
218
220
  const callHandler = isHandleFunction ? callHandlerSyncOnAsync ? `_r=h(c)\nif(_r instanceof Promise)_r=await _r\n` : `_r=${isAsync ? "await " : ""}h(c)\n` : isStaticResponse ? `_r=cr(h)\n` : isPromiseHandler ? `_r=h.then(cr)\n` : `_r=h\n`;
219
221
  let code = `${isAsync ? "async " : ""}function route(c){\n`;
222
+ if (hasLifecycleHook) {
223
+ link(require_handler_utils.emptyResponse, "emp");
224
+ code += "let aborted=c.request.signal.aborted\nif(!aborted)c.request.signal.addEventListener('abort',()=>{aborted=true},{once:true})\n" + abortCheck;
225
+ }
220
226
  if ((hasAfterResponse || hasTrace) && !syncAfterResponse) code += "let _stl\n";
221
227
  if (asyncCookieSign) code += "let _sg\n";
222
228
  if (hasTrace) {
@@ -265,6 +271,7 @@ function compileHandlerJit({ method, path, handler, root, hook, adapter, buildVa
265
271
  link(require_error.ParseError, "pe");
266
272
  code += "try{\n" + parseCode + "}catch(e){throw new pe(e)}\n";
267
273
  if (hasTrace) code += endTrace("parse");
274
+ if (hasLifecycleHook) code += abortCheck;
268
275
  } else if (hasTrace) code += beginTrace("parse", 0) + endTrace("parse");
269
276
  if (hook?.transform?.length || hasTrace) {
270
277
  const transformLen = hook?.transform?.length ?? 0;
@@ -272,9 +279,10 @@ function compileHandlerJit({ method, path, handler, root, hook, adapter, buildVa
272
279
  if (transformLen) {
273
280
  link(hook.transform, "tf");
274
281
  if (isAsync) code += "let _tf\n";
275
- code += require_compile_handler_utils.mapTransform(hook.transform, [isAsync, buildReport("transform")]);
282
+ code += require_compile_handler_utils.mapTransform(hook.transform, [isAsync, buildReport("transform")], "aborted");
276
283
  }
277
284
  code += endTrace("transform");
285
+ if (transformLen) code += abortCheck;
278
286
  }
279
287
  if (vali?.body) {
280
288
  link(vali, "va");
@@ -344,9 +352,10 @@ function compileHandlerJit({ method, path, handler, root, hook, adapter, buildVa
344
352
  if (hasBeforeHandle) {
345
353
  link(hook.beforeHandle, "bf");
346
354
  const deriveEntries = hook["~deriveEntries"];
347
- code += require_compile_handler_utils.mapBeforeHandle(hook.beforeHandle, deriveEntries?.length ? new Set(deriveEntries) : void 0, link, isAsync, buildReport("beforeHandle"));
355
+ code += require_compile_handler_utils.mapBeforeHandle(hook.beforeHandle, deriveEntries?.length ? new Set(deriveEntries) : void 0, link, isAsync, buildReport("beforeHandle"), "aborted");
348
356
  }
349
357
  code += endTrace("beforeHandle");
358
+ if (hasBeforeHandle) code += abortCheck;
350
359
  }
351
360
  if (hasAfterResponse || traceHandleOn) link(require_adapter_utils.tee, "tee");
352
361
  const teeConsumers = (hasAfterResponse ? 1 : 0) + (traceHandleOn ? 1 : 0);
@@ -367,6 +376,7 @@ function compileHandlerJit({ method, path, handler, root, hook, adapter, buildVa
367
376
  code += `}\n`;
368
377
  } else if (hasBeforeHandle) code += `if(_r===undefined){\n${callHandler}${teeBlock}}\n`;
369
378
  else code += callHandler + teeBlock;
379
+ if (hasLifecycleHook) code += abortCheck;
370
380
  if (syncAfterResponse) {
371
381
  link(require_handler_utils.forwardError, "fe");
372
382
  factoryHelpers += `function _fin(c,_r){
@@ -386,18 +396,20 @@ const _s=tee(_r,${teeCount})\nreturn _fin2(c,_s[0],_s[1])\n}\nreturn _fin2(c,_r,
386
396
  code += beginTrace("afterHandle", afLen);
387
397
  if (hasAfterHandle) {
388
398
  link(hook.afterHandle, "af");
389
- code += require_compile_handler_utils.mapAfterHandle(hook.afterHandle, isAsync, buildReport("afterHandle"));
399
+ code += require_compile_handler_utils.mapAfterHandle(hook.afterHandle, isAsync, buildReport("afterHandle"), "aborted");
390
400
  }
391
401
  code += endTrace("afterHandle");
402
+ if (hasAfterHandle) code += abortCheck;
392
403
  }
393
404
  if (hasMapResponse || hasTrace) {
394
405
  const mrLen = hook?.mapResponse?.length ?? 0;
395
406
  code += beginTrace("mapResponse", mrLen);
396
407
  if (hasMapResponse) {
397
408
  link(hook.mapResponse, "mr");
398
- code += require_compile_handler_utils.mapMapResponse(hook.mapResponse, isAsync, buildReport("mapResponse"));
409
+ code += require_compile_handler_utils.mapMapResponse(hook.mapResponse, isAsync, buildReport("mapResponse"), "aborted");
399
410
  }
400
411
  code += endTrace("mapResponse");
412
+ if (hasMapResponse) code += abortCheck;
401
413
  }
402
414
  if (hasResponseValidator) {
403
415
  link(vali, "va");
@@ -406,17 +418,18 @@ const _s=tee(_r,${teeCount})\nreturn _fin2(c,_s[0],_s[1])\n}\nreturn _fin2(c,_r,
406
418
  code += `if(_r instanceof es){
407
419
  const _vr=va.response[_r.code]
408
420
  if(_vr)_r.response=${awaitStr}_vr.EncodeFrom(_r.response,'response')\n}else if(!(_r instanceof Response)&&!(_r instanceof ReadableStream)&&typeof _r?.next!=='function'){\nconst _vr=va.response[c.set.status??200]\nif(_vr)_r=${awaitStr}_vr.EncodeFrom(_r,'response')\n}\n`;
421
+ if (hasLifecycleHook) code += abortCheck;
409
422
  }
410
423
  code += schedule;
411
424
  code += signPrefix;
412
425
  const finalMap = hasSet ? `${map}(_r,c.set,c.request)` : `${map}(_r,c.request)`;
413
- if (syncErrorHook) code += `if(_r instanceof Promise)return ${finalMap}.catch((_e)=>_ce(_e,c))\n`;
426
+ if (syncErrorHook) code += `if(_r instanceof Promise)return ${finalMap}.catch((_e)=>_ce(_e,c,aborted))\n`;
414
427
  code += `return ${finalMap}\n`;
415
428
  }
416
429
  } else if (isHandleFunction) {
417
430
  if (!isAsync) link(require_handler_utils.forwardError, "fe");
418
431
  const mapArgs = hasSet ? "c.set,c.request" : "c.request";
419
- code += (callHandlerSyncOnAsync ? `let _r=h(c)\nif(_r instanceof Promise)_r=await _r\n` : `let _r=${isAsync ? "await " : ""}h(c)\n`) + `if(_r instanceof Error)throw _r\n` + (isAsync ? `return ${map}(_r,${mapArgs})\n` : syncErrorHook ? `if(_r instanceof Promise)return ${map}(_r.then(fe),${mapArgs}).catch((_e)=>_ce(_e,c))\nreturn ${map}(_r,${mapArgs})\n` : `if(_r instanceof Promise)_r=_r.then(fe)\nreturn ${map}(_r,${mapArgs})\n`);
432
+ code += (callHandlerSyncOnAsync ? `let _r=h(c)\nif(_r instanceof Promise)_r=await _r\n` : `let _r=${isAsync ? "await " : ""}h(c)\n`) + abortCheck + `if(_r instanceof Error)throw _r\n` + (isAsync ? `return ${map}(_r,${mapArgs})\n` : syncErrorHook ? `if(_r instanceof Promise)return ${map}(_r.then(fe),${mapArgs}).catch((_e)=>_ce(_e,c,aborted))\nreturn ${map}(_r,${mapArgs})\n` : `if(_r instanceof Promise)_r=_r.then(fe)\nreturn ${map}(_r,${mapArgs})\n`);
420
433
  } else code += `return ${mapReturn}`;
421
434
  if (hasErrorHook || hasTrace) {
422
435
  let body = "";
@@ -438,9 +451,9 @@ let _r${hasMapResponse ? ",tmp" : ""}\n` + require_compile_handler_utils.mapErro
438
451
  map,
439
452
  link,
440
453
  res.map,
441
- (hasMapResponse ? `c.responseValue=_r\n` + require_compile_handler_utils.mapMapResponse(hook.mapResponse, isAsync, void 0) : "") + endTrace("error") + schedule,
454
+ (hasMapResponse ? `c.responseValue=_r\n` + require_compile_handler_utils.mapMapResponse(hook.mapResponse, isAsync, void 0, "aborted") : "") + endTrace("error") + abortCheck + schedule,
442
455
  signPrefix
443
- ]) + endTrace("error") + schedule + `if(typeof e?.toResponse==='function')try{
456
+ ], "aborted") + endTrace("error") + abortCheck + schedule + `if(typeof e?.toResponse==='function')try{
444
457
  const _er=e.toResponse()
445
458
  if(_er instanceof Promise)return _er.then(${asyncCookieSign ? "async " : ""}(_v)=>{if(_v instanceof Response){${signPrefix}return ${map}(_v,c.set,c.request)}return _efb(e,c)},()=>_efb(e,c))\nif(_er instanceof Response){${signPrefix}return ${map}(_er,c.set,c.request)}\n}catch{}\nreturn _efb(e,c)\n`;
446
459
  } else {
@@ -448,8 +461,8 @@ if(_er instanceof Promise)return _er.then(${asyncCookieSign ? "async " : ""}(_v)
448
461
  body += `throw e\n`;
449
462
  }
450
463
  if (syncErrorHook) {
451
- factoryHelpers += `function _ce(e,c){\n${body}}\n`;
452
- code += `}catch(e){return _ce(e,c)}\n`;
464
+ factoryHelpers += `function _ce(e,c,aborted){\n${body}}\n`;
465
+ code += `}catch(e){return _ce(e,c,aborted)}\n`;
453
466
  } else code += `}catch(e){\n${body}}\n`;
454
467
  }
455
468
  code += "}";