elysia 2.0.0-exp.34 → 2.0.0-exp.36
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/base.d.ts +34 -6
- package/dist/base.js +16 -3
- package/dist/base.mjs +17 -4
- package/dist/compile/aot-capture.d.ts +7 -0
- package/dist/compile/aot-capture.js +185 -0
- package/dist/compile/aot-capture.mjs +181 -0
- package/dist/compile/aot-reconstruct.d.ts +54 -0
- package/dist/compile/aot-reconstruct.js +249 -0
- package/dist/compile/aot-reconstruct.mjs +234 -0
- package/dist/compile/aot.d.ts +72 -55
- package/dist/compile/aot.js +37 -222
- package/dist/compile/aot.mjs +30 -212
- package/dist/compile/handler/frozen-validator.js +5 -5
- package/dist/compile/handler/frozen-validator.mjs +6 -6
- package/dist/compile/handler/jit.js +1 -1
- package/dist/compile/handler/jit.mjs +1 -1
- package/dist/compile/handler/utils.d.ts +5 -2
- package/dist/compile/handler/utils.js +63 -5
- package/dist/compile/handler/utils.mjs +61 -5
- package/dist/compile/lexer.d.ts +1 -1
- package/dist/plugin/core.d.ts +35 -5
- package/dist/plugin/core.js +6 -3
- package/dist/plugin/core.mjs +6 -4
- package/dist/plugin/source.d.ts +9 -0
- package/dist/plugin/source.js +16 -11
- package/dist/plugin/source.mjs +6 -1
- package/dist/type/constants.d.ts +1 -1
- package/dist/type/validator/custom-error.js +1 -1
- package/dist/type/validator/custom-error.mjs +2 -2
- package/dist/type/validator/frozen-check.js +3 -3
- package/dist/type/validator/frozen-check.mjs +1 -1
- package/dist/type/validator/index.d.ts +3 -2
- package/dist/type/validator/index.js +24 -164
- package/dist/type/validator/index.mjs +29 -170
- package/dist/type/validator/string-codec-aot.d.ts +2 -2
- package/dist/type/validator/string-codec-aot.js +5 -6
- package/dist/type/validator/string-codec-aot.mjs +4 -4
- package/dist/types.d.ts +1 -1
- package/dist/utils.d.ts +5 -1
- package/dist/utils.js +6 -0
- package/dist/utils.mjs +4 -1
- package/dist/ws/route.js +6 -3
- package/dist/ws/route.mjs +8 -5
- package/package.json +6 -1
package/dist/base.d.ts
CHANGED
|
@@ -53,6 +53,7 @@ declare class Elysia<const in out BasePath extends string = '', const in out Sco
|
|
|
53
53
|
};
|
|
54
54
|
'~hasWS'?: boolean;
|
|
55
55
|
'~hasDynamicWS'?: boolean;
|
|
56
|
+
'~hasTrace'?: boolean;
|
|
56
57
|
'~scopeChild'?: boolean;
|
|
57
58
|
'~scopeChildren'?: AnyElysia[];
|
|
58
59
|
constructor(config?: ElysiaConfig<BasePath, Scope>);
|
|
@@ -203,12 +204,39 @@ declare class Elysia<const in out BasePath extends string = '', const in out Sco
|
|
|
203
204
|
derive<const Derivative extends Record<string, unknown> | ElysiaStatus<any, any, any> | void>(scope: 'local', transform: (context: Context<HookContextSchema<Metadata, Ephemeral, Volatile, BasePath>, HookContextSingleton<Singleton, Ephemeral, Volatile>>) => MaybePromise<Derivative>): LocalHookReturn<BasePath, Scope, Singleton, Definitions, Metadata, Routes, Ephemeral, Volatile, ExtractErrorFromHandle<Derivative>, ExcludeElysiaResponse<Derivative>>;
|
|
204
205
|
derive<const Derivative extends Record<string, unknown> | ElysiaStatus<any, any, any> | void>(scope: 'plugin', transform: (context: LifecycleContext<HookContextSchema<Metadata, Ephemeral, Volatile, BasePath>, HookContextSingleton<Singleton, Ephemeral, Volatile>, undefined, 'plugin'>) => MaybePromise<Derivative>): PluginHookReturn<BasePath, Scope, Singleton, Definitions, Metadata, Routes, Ephemeral, Volatile, ExtractErrorFromHandle<Derivative>, ExcludeElysiaResponse<Derivative>>;
|
|
205
206
|
derive<const Derivative extends Record<string, unknown> | ElysiaStatus<any, any, any> | void>(scope: 'global', transform: (context: LifecycleContext<HookContextSchema<Metadata, Ephemeral, Volatile, BasePath>, HookContextSingleton<Singleton, Ephemeral, Volatile>, undefined, 'global'>) => MaybePromise<Derivative>): GlobalHookReturn<BasePath, Scope, Singleton, Definitions, Metadata, Routes, Ephemeral, Volatile, ExtractErrorFromHandle<Derivative>, ExcludeElysiaResponse<Derivative>>;
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
207
|
+
mapDerive<const Derivative extends Record<string, unknown> | ElysiaStatus<any, any, any> | void>(transform: (context: Context<HookContextSchema<Metadata, Ephemeral, Volatile, BasePath>, HookContextSingleton<Singleton, Ephemeral, Volatile>>) => MaybePromise<Derivative>): Elysia<BasePath, Scope, Singleton, Definitions, Metadata, Routes, Ephemeral, {
|
|
208
|
+
derive: ExcludeElysiaResponse<Derivative>;
|
|
209
|
+
schema: Volatile['schema'];
|
|
210
|
+
schemas: Volatile['schemas'];
|
|
211
|
+
response: UnionResponseStatus<Volatile['response'], ExtractErrorFromHandle<Derivative>>;
|
|
212
|
+
error: Volatile['error'];
|
|
213
|
+
}>;
|
|
214
|
+
mapDerive<const Derivative extends Record<string, unknown> | ElysiaStatus<any, any, any> | void>(scope: 'local', transform: (context: Context<HookContextSchema<Metadata, Ephemeral, Volatile, BasePath>, HookContextSingleton<Singleton, Ephemeral, Volatile>>) => MaybePromise<Derivative>): Elysia<BasePath, Scope, Singleton, Definitions, Metadata, Routes, Ephemeral, {
|
|
215
|
+
derive: ExcludeElysiaResponse<Derivative>;
|
|
216
|
+
schema: Volatile['schema'];
|
|
217
|
+
schemas: Volatile['schemas'];
|
|
218
|
+
response: UnionResponseStatus<Volatile['response'], ExtractErrorFromHandle<Derivative>>;
|
|
219
|
+
error: Volatile['error'];
|
|
220
|
+
}>;
|
|
221
|
+
mapDerive<const Derivative extends Record<string, unknown> | ElysiaStatus<any, any, any> | void>(scope: 'plugin', transform: (context: LifecycleContext<HookContextSchema<Metadata, Ephemeral, Volatile, BasePath>, HookContextSingleton<Singleton, Ephemeral, Volatile>, undefined, 'plugin'>) => MaybePromise<Derivative>): Elysia<BasePath, Scope, Singleton, Definitions, Metadata, Routes, {
|
|
222
|
+
derive: ExcludeElysiaResponse<Derivative>;
|
|
223
|
+
schema: Ephemeral['schema'];
|
|
224
|
+
schemas: Ephemeral['schemas'];
|
|
225
|
+
response: UnionResponseStatus<Ephemeral['response'], ExtractErrorFromHandle<Derivative>>;
|
|
226
|
+
error: Ephemeral['error'];
|
|
227
|
+
}, Volatile>;
|
|
228
|
+
mapDerive<const Derivative extends Record<string, unknown> | ElysiaStatus<any, any, any> | void>(scope: 'global', transform: (context: LifecycleContext<HookContextSchema<Metadata, Ephemeral, Volatile, BasePath>, HookContextSingleton<Singleton, Ephemeral, Volatile>, undefined, 'global'>) => MaybePromise<Derivative>): Elysia<BasePath, Scope, {
|
|
229
|
+
decorator: Singleton['decorator'];
|
|
230
|
+
store: Singleton['store'];
|
|
231
|
+
derive: ExcludeElysiaResponse<Derivative>;
|
|
232
|
+
}, Definitions, {
|
|
233
|
+
schema: Metadata['schema'];
|
|
234
|
+
schemas: Metadata['schemas'];
|
|
235
|
+
macro: Metadata['macro'];
|
|
236
|
+
macroFn: Metadata['macroFn'];
|
|
237
|
+
parser: Metadata['parser'];
|
|
238
|
+
response: UnionResponseStatus<Metadata['response'], ExtractErrorFromHandle<Derivative>>;
|
|
239
|
+
}, Routes, Ephemeral, Volatile>;
|
|
212
240
|
afterHandle<const Handler extends MaybeArray<AfterHandler<HookContextSchema<Metadata, Ephemeral, Volatile, BasePath>, HookContextSingleton<Singleton, Ephemeral, Volatile>>>>(fn: Handler): LocalHookReturn<BasePath, Scope, Singleton, Definitions, Metadata, Routes, Ephemeral, Volatile, ElysiaHandlerToResponseSchemaAmbiguous<Handler>>;
|
|
213
241
|
afterHandle<const Handler extends MaybeArray<AfterHandler<HookContextSchema<Metadata, Ephemeral, Volatile, BasePath>, HookContextSingleton<Singleton, Ephemeral, Volatile>>>>(scope: 'local', fn: Handler): LocalHookReturn<BasePath, Scope, Singleton, Definitions, Metadata, Routes, Ephemeral, Volatile, ElysiaHandlerToResponseSchemaAmbiguous<Handler>>;
|
|
214
242
|
afterHandle<const Handler extends MaybeArray<AfterHandler<HookContextSchema<Metadata, Ephemeral, Volatile, BasePath>, HookContextSingleton<Singleton, Ephemeral, Volatile>, undefined, 'plugin'>>>(scope: 'plugin', fn: Handler): PluginHookReturn<BasePath, Scope, Singleton, Definitions, Metadata, Routes, Ephemeral, Volatile, ElysiaHandlerToResponseSchemaAmbiguous<Handler>>;
|
package/dist/base.js
CHANGED
|
@@ -143,6 +143,7 @@ var Elysia = class Elysia {
|
|
|
143
143
|
#on(type, fn, scope = this["~config"]?.as) {
|
|
144
144
|
const added = require_utils.nullObject();
|
|
145
145
|
added[type] = fn;
|
|
146
|
+
if (type === "trace") this["~hasTrace"] = true;
|
|
146
147
|
this["~hookChain"] = {
|
|
147
148
|
added,
|
|
148
149
|
parent: this["~hookChain"],
|
|
@@ -217,6 +218,17 @@ var Elysia = class Elysia {
|
|
|
217
218
|
}
|
|
218
219
|
return result;
|
|
219
220
|
}
|
|
221
|
+
mapDerive(scopeOrFn, fn) {
|
|
222
|
+
const result = this.#onBranch("beforeHandle", scopeOrFn, fn);
|
|
223
|
+
const node = this["~hookChain"];
|
|
224
|
+
if (node?.added) {
|
|
225
|
+
const d = fn ?? scopeOrFn;
|
|
226
|
+
const entries = node.added["~deriveEntries"] ??= [];
|
|
227
|
+
if (Array.isArray(d)) for (const f of d) entries.push(require_utils.mapDeriveEntry(f));
|
|
228
|
+
else entries.push(require_utils.mapDeriveEntry(d));
|
|
229
|
+
}
|
|
230
|
+
return result;
|
|
231
|
+
}
|
|
220
232
|
afterHandle(scopeOrFn, fn) {
|
|
221
233
|
return this.#onBranch("afterHandle", scopeOrFn, fn);
|
|
222
234
|
}
|
|
@@ -368,6 +380,7 @@ var Elysia = class Elysia {
|
|
|
368
380
|
}
|
|
369
381
|
hook = promoted;
|
|
370
382
|
}
|
|
383
|
+
if (hook.trace) this["~hasTrace"] = true;
|
|
371
384
|
this["~hookChain"] = {
|
|
372
385
|
added: hook,
|
|
373
386
|
parent: this["~hookChain"],
|
|
@@ -548,6 +561,7 @@ var Elysia = class Elysia {
|
|
|
548
561
|
if (addedByThisCall) for (const h of addedByThisCall) this.#childrenHash.delete(h);
|
|
549
562
|
throw new Error(`[Elysia] Macro "${macroName}" can be only define once`);
|
|
550
563
|
}
|
|
564
|
+
if (app["~hasTrace"]) this["~hasTrace"] = true;
|
|
551
565
|
if (app.#history) {
|
|
552
566
|
if (app["~hasWS"]) this["~hasWS"] = true;
|
|
553
567
|
const history = this.#history ??= [];
|
|
@@ -682,10 +696,10 @@ var Elysia = class Elysia {
|
|
|
682
696
|
continue;
|
|
683
697
|
}
|
|
684
698
|
if (key === "~deriveEntries") {
|
|
685
|
-
const entries = added[
|
|
699
|
+
const entries = added[key];
|
|
686
700
|
if (!entries) continue;
|
|
687
701
|
const target = isGlobal ? globalEvents ??= require_utils.nullObject() : pluginEvents ??= require_utils.nullObject();
|
|
688
|
-
const list = target[
|
|
702
|
+
const list = target[key] ??= [];
|
|
689
703
|
for (let j = 0; j < entries.length; j++) list.push(entries[j]);
|
|
690
704
|
continue;
|
|
691
705
|
}
|
|
@@ -1256,7 +1270,6 @@ var Elysia = class Elysia {
|
|
|
1256
1270
|
return r;
|
|
1257
1271
|
}
|
|
1258
1272
|
};
|
|
1259
|
-
Elysia.prototype.mapDerive = Elysia.prototype.derive;
|
|
1260
1273
|
|
|
1261
1274
|
//#endregion
|
|
1262
1275
|
exports.Elysia = Elysia;
|
package/dist/base.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { isBun } from "./universal/constants.mjs";
|
|
2
2
|
import { MethodMap, isDynamicRegex, needEncodeRegex } from "./constants.mjs";
|
|
3
|
-
import { clonePlainDecorators, clonePlainDeep, coalesceStandaloneSchemas, createErrorEventHandler, eventProperties, fnOrigin, fnv1a, getLoosePath, guardNonPlainLeaves, hookToGuard, invalidateMacroEpoch, isEmpty, isNotEmpty, isRecordNumber, joinPath, macroOrigin, mapMethodBack, mergeDeep, mergeResponse, nullObject, pushField, schemaProperties, serializeMacroSeed } from "./utils.mjs";
|
|
3
|
+
import { clonePlainDecorators, clonePlainDeep, coalesceStandaloneSchemas, createErrorEventHandler, eventProperties, fnOrigin, fnv1a, getLoosePath, guardNonPlainLeaves, hookToGuard, invalidateMacroEpoch, isEmpty, isNotEmpty, isRecordNumber, joinPath, macroOrigin, mapDeriveEntry, mapMethodBack, mergeDeep, mergeResponse, nullObject, pushField, schemaProperties, serializeMacroSeed } from "./utils.mjs";
|
|
4
4
|
import { Ref } from "./type/bridge.mjs";
|
|
5
5
|
import { Capture } from "./compile/aot.mjs";
|
|
6
6
|
import { compileHandler, composeRouteHook, localMacroRoot, resolveLocalHook } from "./compile/handler/index.mjs";
|
|
@@ -140,6 +140,7 @@ var Elysia = class Elysia {
|
|
|
140
140
|
#on(type, fn, scope = this["~config"]?.as) {
|
|
141
141
|
const added = nullObject();
|
|
142
142
|
added[type] = fn;
|
|
143
|
+
if (type === "trace") this["~hasTrace"] = true;
|
|
143
144
|
this["~hookChain"] = {
|
|
144
145
|
added,
|
|
145
146
|
parent: this["~hookChain"],
|
|
@@ -214,6 +215,17 @@ var Elysia = class Elysia {
|
|
|
214
215
|
}
|
|
215
216
|
return result;
|
|
216
217
|
}
|
|
218
|
+
mapDerive(scopeOrFn, fn) {
|
|
219
|
+
const result = this.#onBranch("beforeHandle", scopeOrFn, fn);
|
|
220
|
+
const node = this["~hookChain"];
|
|
221
|
+
if (node?.added) {
|
|
222
|
+
const d = fn ?? scopeOrFn;
|
|
223
|
+
const entries = node.added["~deriveEntries"] ??= [];
|
|
224
|
+
if (Array.isArray(d)) for (const f of d) entries.push(mapDeriveEntry(f));
|
|
225
|
+
else entries.push(mapDeriveEntry(d));
|
|
226
|
+
}
|
|
227
|
+
return result;
|
|
228
|
+
}
|
|
217
229
|
afterHandle(scopeOrFn, fn) {
|
|
218
230
|
return this.#onBranch("afterHandle", scopeOrFn, fn);
|
|
219
231
|
}
|
|
@@ -365,6 +377,7 @@ var Elysia = class Elysia {
|
|
|
365
377
|
}
|
|
366
378
|
hook = promoted;
|
|
367
379
|
}
|
|
380
|
+
if (hook.trace) this["~hasTrace"] = true;
|
|
368
381
|
this["~hookChain"] = {
|
|
369
382
|
added: hook,
|
|
370
383
|
parent: this["~hookChain"],
|
|
@@ -545,6 +558,7 @@ var Elysia = class Elysia {
|
|
|
545
558
|
if (addedByThisCall) for (const h of addedByThisCall) this.#childrenHash.delete(h);
|
|
546
559
|
throw new Error(`[Elysia] Macro "${macroName}" can be only define once`);
|
|
547
560
|
}
|
|
561
|
+
if (app["~hasTrace"]) this["~hasTrace"] = true;
|
|
548
562
|
if (app.#history) {
|
|
549
563
|
if (app["~hasWS"]) this["~hasWS"] = true;
|
|
550
564
|
const history = this.#history ??= [];
|
|
@@ -679,10 +693,10 @@ var Elysia = class Elysia {
|
|
|
679
693
|
continue;
|
|
680
694
|
}
|
|
681
695
|
if (key === "~deriveEntries") {
|
|
682
|
-
const entries = added[
|
|
696
|
+
const entries = added[key];
|
|
683
697
|
if (!entries) continue;
|
|
684
698
|
const target = isGlobal ? globalEvents ??= nullObject() : pluginEvents ??= nullObject();
|
|
685
|
-
const list = target[
|
|
699
|
+
const list = target[key] ??= [];
|
|
686
700
|
for (let j = 0; j < entries.length; j++) list.push(entries[j]);
|
|
687
701
|
continue;
|
|
688
702
|
}
|
|
@@ -1253,7 +1267,6 @@ var Elysia = class Elysia {
|
|
|
1253
1267
|
return r;
|
|
1254
1268
|
}
|
|
1255
1269
|
};
|
|
1256
|
-
Elysia.prototype.mapDerive = Elysia.prototype.derive;
|
|
1257
1270
|
|
|
1258
1271
|
//#endregion
|
|
1259
1272
|
export { Elysia };
|
|
@@ -0,0 +1,185 @@
|
|
|
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_compile_aot_reconstruct = require('./aot-reconstruct.js');
|
|
8
|
+
const require_type_validator_frozen_check = require('../type/validator/frozen-check.js');
|
|
9
|
+
const require_type_validator_custom_error = require('../type/validator/custom-error.js');
|
|
10
|
+
const require_type_validator_index = require('../type/validator/index.js');
|
|
11
|
+
const require_type_validator_string_codec_aot = require('../type/validator/string-codec-aot.js');
|
|
12
|
+
let typebox_schema = require("typebox/schema");
|
|
13
|
+
let exact_mirror = require("exact-mirror");
|
|
14
|
+
exact_mirror = require_runtime.__toESM(exact_mirror);
|
|
15
|
+
|
|
16
|
+
//#region src/compile/aot-capture.ts
|
|
17
|
+
function externalsShape(schema) {
|
|
18
|
+
let out = "";
|
|
19
|
+
for (const e of require_compile_aot_reconstruct.collectExternals(schema)) out += e instanceof RegExp ? "r" : typeof e === "function" ? "f" : "v";
|
|
20
|
+
return out;
|
|
21
|
+
}
|
|
22
|
+
function sourceOnlyValidator(schema) {
|
|
23
|
+
const buildResult = (0, typebox_schema.Build)(schema);
|
|
24
|
+
let full;
|
|
25
|
+
return new Proxy({}, { get(_, prop) {
|
|
26
|
+
if (prop === "buildResult") return buildResult;
|
|
27
|
+
const f = full ??= (0, typebox_schema.Compile)(schema);
|
|
28
|
+
const value = f[prop];
|
|
29
|
+
return typeof value === "function" ? value.bind(f) : value;
|
|
30
|
+
} });
|
|
31
|
+
}
|
|
32
|
+
function maybeCapture(args) {
|
|
33
|
+
const { aot, slot, hasRef, originalSchema, schema, hasCodec, hasDefault, coerces, normalize, buildResult } = args;
|
|
34
|
+
if (hasCodec && !hasRef && coerces && normalize !== false && normalize !== "typebox") {
|
|
35
|
+
const plan = require_type_coerce.captureCoercePlan(originalSchema, schema);
|
|
36
|
+
if (plan && externalsShape(require_type_coerce.buildCoercedFromPlan(originalSchema, plan)) === externalsShape(schema)) require_compile_aot.Capture.set({
|
|
37
|
+
method: aot.method,
|
|
38
|
+
path: aot.path,
|
|
39
|
+
slot
|
|
40
|
+
}, { coercePlan: plan });
|
|
41
|
+
}
|
|
42
|
+
const defaultFastPathCapture = {
|
|
43
|
+
precomputeSafe: void 0,
|
|
44
|
+
precomputedDefault: void 0,
|
|
45
|
+
precomputeNull: void 0,
|
|
46
|
+
precomputedObjectDefault: void 0,
|
|
47
|
+
defaultCloner: void 0,
|
|
48
|
+
objectDefaultMerger: void 0
|
|
49
|
+
};
|
|
50
|
+
if (hasDefault) {
|
|
51
|
+
const defaults = require_type_validator_default_precompute.verifyPreallocatableDefault(schema);
|
|
52
|
+
if (defaults) {
|
|
53
|
+
defaultFastPathCapture.precomputeSafe = true;
|
|
54
|
+
defaultFastPathCapture.precomputedDefault = defaults.pd;
|
|
55
|
+
defaultFastPathCapture.precomputeNull = defaults.pn;
|
|
56
|
+
defaultFastPathCapture.precomputedObjectDefault = defaults.pod;
|
|
57
|
+
defaultFastPathCapture.defaultCloner = defaults.pd !== void 0 ? require_type_validator_default_precompute.buildDefaultClonerSource(defaults.pd) : void 0;
|
|
58
|
+
defaultFastPathCapture.objectDefaultMerger = defaults.ms ?? (defaults.pod !== void 0 ? require_type_validator_default_precompute.buildObjectDefaultMergeSource(defaults.pod) : void 0);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
require_compile_aot.Capture.set({
|
|
62
|
+
method: aot.method,
|
|
63
|
+
path: aot.path,
|
|
64
|
+
slot
|
|
65
|
+
}, defaultFastPathCapture);
|
|
66
|
+
const customErrors = require_type_validator_custom_error.captureCustomErrors(schema);
|
|
67
|
+
if (customErrors) require_compile_aot.Capture.set({
|
|
68
|
+
method: aot.method,
|
|
69
|
+
path: aot.path,
|
|
70
|
+
slot
|
|
71
|
+
}, { customErrors });
|
|
72
|
+
const innerCodecs = require_type_validator_string_codec_aot.captureStringCodecEntries(schema, args.sanitize);
|
|
73
|
+
if (innerCodecs) require_compile_aot.Capture.set({
|
|
74
|
+
method: aot.method,
|
|
75
|
+
path: aot.path,
|
|
76
|
+
slot
|
|
77
|
+
}, { innerCodecs });
|
|
78
|
+
const cf = require_type_validator_frozen_check.buildFrozenCheck(buildResult, schema);
|
|
79
|
+
if (!cf) return;
|
|
80
|
+
require_compile_aot.Capture.set({
|
|
81
|
+
method: aot.method,
|
|
82
|
+
path: aot.path,
|
|
83
|
+
slot
|
|
84
|
+
}, {
|
|
85
|
+
...cf,
|
|
86
|
+
async: buildResult.external.variables.some(require_type_validator_index.isAsyncPredicate),
|
|
87
|
+
hasDefault,
|
|
88
|
+
hasCodec,
|
|
89
|
+
hasRef
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
function captureMirror(schema, aot, slot, sanitize) {
|
|
93
|
+
try {
|
|
94
|
+
const emitted = (0, exact_mirror.default)(schema, {
|
|
95
|
+
Compile: typebox_schema.Compile,
|
|
96
|
+
sanitize,
|
|
97
|
+
emit: true
|
|
98
|
+
});
|
|
99
|
+
if (typeof emitted?.source === "string") {
|
|
100
|
+
const ext = emitted.externals;
|
|
101
|
+
if (!ext) require_compile_aot.Capture.set({
|
|
102
|
+
method: aot.method,
|
|
103
|
+
path: aot.path,
|
|
104
|
+
slot
|
|
105
|
+
}, { mirror: {
|
|
106
|
+
source: emitted.source,
|
|
107
|
+
hasExternals: false
|
|
108
|
+
} });
|
|
109
|
+
else if (ext.unions && !ext.hof) {
|
|
110
|
+
const u = require_compile_aot_reconstruct.captureMirrorUnions(schema, ext.unions);
|
|
111
|
+
if (u) require_compile_aot.Capture.set({
|
|
112
|
+
method: aot.method,
|
|
113
|
+
path: aot.path,
|
|
114
|
+
slot
|
|
115
|
+
}, { mirror: {
|
|
116
|
+
source: emitted.source,
|
|
117
|
+
hasExternals: true,
|
|
118
|
+
u
|
|
119
|
+
} });
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
} catch {}
|
|
123
|
+
}
|
|
124
|
+
function captureCodecMirror(schema, aot, slot, sanitize, dir) {
|
|
125
|
+
const dirOpt = dir === "decode" ? { decode: true } : { encode: true };
|
|
126
|
+
try {
|
|
127
|
+
const emitted = (0, exact_mirror.default)(schema, {
|
|
128
|
+
Compile: typebox_schema.Compile,
|
|
129
|
+
sanitize,
|
|
130
|
+
...dirOpt,
|
|
131
|
+
emit: true
|
|
132
|
+
});
|
|
133
|
+
if (typeof emitted?.source === "string") {
|
|
134
|
+
const ext = emitted.externals;
|
|
135
|
+
if (ext?.codecs && !ext.hof && require_compile_aot_reconstruct.captureMirrorCodecs(schema, ext.codecs, dir)) {
|
|
136
|
+
let u;
|
|
137
|
+
let freezable = true;
|
|
138
|
+
if (ext.unions && ext.unions.length) {
|
|
139
|
+
u = require_compile_aot_reconstruct.captureMirrorUnions(schema, ext.unions);
|
|
140
|
+
if (!u) freezable = false;
|
|
141
|
+
}
|
|
142
|
+
if (freezable) {
|
|
143
|
+
const mirror = {
|
|
144
|
+
source: emitted.source,
|
|
145
|
+
hasExternals: true,
|
|
146
|
+
u
|
|
147
|
+
};
|
|
148
|
+
require_compile_aot.Capture.set({
|
|
149
|
+
method: aot.method,
|
|
150
|
+
path: aot.path,
|
|
151
|
+
slot
|
|
152
|
+
}, dir === "decode" ? { decodeMirror: mirror } : { encodeMirror: mirror });
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
} catch {}
|
|
157
|
+
}
|
|
158
|
+
function captureBridgeFree(aot, slot, rawSchema) {
|
|
159
|
+
const captured = require_compile_aot.Capture.get({
|
|
160
|
+
method: aot.method,
|
|
161
|
+
path: aot.path,
|
|
162
|
+
slot
|
|
163
|
+
});
|
|
164
|
+
if (captured) require_compile_aot.Capture.set({
|
|
165
|
+
method: aot.method,
|
|
166
|
+
path: aot.path,
|
|
167
|
+
slot
|
|
168
|
+
}, { bridgeFree: require_compile_handler_frozen_validator.isCapturedBridgeFree(captured, rawSchema, captured.coercePlan && typeof rawSchema !== "string" ? require_type_coerce.buildCoercedFromPlan(rawSchema, captured.coercePlan) : rawSchema) });
|
|
169
|
+
}
|
|
170
|
+
const impl = {
|
|
171
|
+
sourceOnlyValidator,
|
|
172
|
+
maybeCapture,
|
|
173
|
+
captureMirror,
|
|
174
|
+
captureCodecMirror,
|
|
175
|
+
captureBridgeFree
|
|
176
|
+
};
|
|
177
|
+
function installCaptureImpl() {
|
|
178
|
+
require_compile_aot.setCaptureImpl(impl);
|
|
179
|
+
require_compile_aot_reconstruct.installReconstructImpl();
|
|
180
|
+
}
|
|
181
|
+
installCaptureImpl();
|
|
182
|
+
|
|
183
|
+
//#endregion
|
|
184
|
+
exports.captureImplementation = impl;
|
|
185
|
+
exports.installCaptureImpl = installCaptureImpl;
|
|
@@ -0,0 +1,181 @@
|
|
|
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 { captureMirrorCodecs, captureMirrorUnions, collectExternals, installReconstructImpl } from "./aot-reconstruct.mjs";
|
|
6
|
+
import { buildFrozenCheck } from "../type/validator/frozen-check.mjs";
|
|
7
|
+
import { captureCustomErrors } from "../type/validator/custom-error.mjs";
|
|
8
|
+
import { isAsyncPredicate } from "../type/validator/index.mjs";
|
|
9
|
+
import { captureStringCodecEntries } from "../type/validator/string-codec-aot.mjs";
|
|
10
|
+
import { Build, Compile } from "typebox/schema";
|
|
11
|
+
import createMirror from "exact-mirror";
|
|
12
|
+
|
|
13
|
+
//#region src/compile/aot-capture.ts
|
|
14
|
+
function externalsShape(schema) {
|
|
15
|
+
let out = "";
|
|
16
|
+
for (const e of collectExternals(schema)) out += e instanceof RegExp ? "r" : typeof e === "function" ? "f" : "v";
|
|
17
|
+
return out;
|
|
18
|
+
}
|
|
19
|
+
function sourceOnlyValidator(schema) {
|
|
20
|
+
const buildResult = Build(schema);
|
|
21
|
+
let full;
|
|
22
|
+
return new Proxy({}, { get(_, prop) {
|
|
23
|
+
if (prop === "buildResult") return buildResult;
|
|
24
|
+
const f = full ??= Compile(schema);
|
|
25
|
+
const value = f[prop];
|
|
26
|
+
return typeof value === "function" ? value.bind(f) : value;
|
|
27
|
+
} });
|
|
28
|
+
}
|
|
29
|
+
function maybeCapture(args) {
|
|
30
|
+
const { aot, slot, hasRef, originalSchema, schema, hasCodec, hasDefault, coerces, normalize, buildResult } = args;
|
|
31
|
+
if (hasCodec && !hasRef && coerces && normalize !== false && normalize !== "typebox") {
|
|
32
|
+
const plan = captureCoercePlan(originalSchema, schema);
|
|
33
|
+
if (plan && externalsShape(buildCoercedFromPlan(originalSchema, plan)) === externalsShape(schema)) Capture.set({
|
|
34
|
+
method: aot.method,
|
|
35
|
+
path: aot.path,
|
|
36
|
+
slot
|
|
37
|
+
}, { coercePlan: plan });
|
|
38
|
+
}
|
|
39
|
+
const defaultFastPathCapture = {
|
|
40
|
+
precomputeSafe: void 0,
|
|
41
|
+
precomputedDefault: void 0,
|
|
42
|
+
precomputeNull: void 0,
|
|
43
|
+
precomputedObjectDefault: void 0,
|
|
44
|
+
defaultCloner: void 0,
|
|
45
|
+
objectDefaultMerger: void 0
|
|
46
|
+
};
|
|
47
|
+
if (hasDefault) {
|
|
48
|
+
const defaults = verifyPreallocatableDefault(schema);
|
|
49
|
+
if (defaults) {
|
|
50
|
+
defaultFastPathCapture.precomputeSafe = true;
|
|
51
|
+
defaultFastPathCapture.precomputedDefault = defaults.pd;
|
|
52
|
+
defaultFastPathCapture.precomputeNull = defaults.pn;
|
|
53
|
+
defaultFastPathCapture.precomputedObjectDefault = defaults.pod;
|
|
54
|
+
defaultFastPathCapture.defaultCloner = defaults.pd !== void 0 ? buildDefaultClonerSource(defaults.pd) : void 0;
|
|
55
|
+
defaultFastPathCapture.objectDefaultMerger = defaults.ms ?? (defaults.pod !== void 0 ? buildObjectDefaultMergeSource(defaults.pod) : void 0);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
Capture.set({
|
|
59
|
+
method: aot.method,
|
|
60
|
+
path: aot.path,
|
|
61
|
+
slot
|
|
62
|
+
}, defaultFastPathCapture);
|
|
63
|
+
const customErrors = captureCustomErrors(schema);
|
|
64
|
+
if (customErrors) Capture.set({
|
|
65
|
+
method: aot.method,
|
|
66
|
+
path: aot.path,
|
|
67
|
+
slot
|
|
68
|
+
}, { customErrors });
|
|
69
|
+
const innerCodecs = captureStringCodecEntries(schema, args.sanitize);
|
|
70
|
+
if (innerCodecs) Capture.set({
|
|
71
|
+
method: aot.method,
|
|
72
|
+
path: aot.path,
|
|
73
|
+
slot
|
|
74
|
+
}, { innerCodecs });
|
|
75
|
+
const cf = buildFrozenCheck(buildResult, schema);
|
|
76
|
+
if (!cf) return;
|
|
77
|
+
Capture.set({
|
|
78
|
+
method: aot.method,
|
|
79
|
+
path: aot.path,
|
|
80
|
+
slot
|
|
81
|
+
}, {
|
|
82
|
+
...cf,
|
|
83
|
+
async: buildResult.external.variables.some(isAsyncPredicate),
|
|
84
|
+
hasDefault,
|
|
85
|
+
hasCodec,
|
|
86
|
+
hasRef
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
function captureMirror(schema, aot, slot, sanitize) {
|
|
90
|
+
try {
|
|
91
|
+
const emitted = createMirror(schema, {
|
|
92
|
+
Compile,
|
|
93
|
+
sanitize,
|
|
94
|
+
emit: true
|
|
95
|
+
});
|
|
96
|
+
if (typeof emitted?.source === "string") {
|
|
97
|
+
const ext = emitted.externals;
|
|
98
|
+
if (!ext) Capture.set({
|
|
99
|
+
method: aot.method,
|
|
100
|
+
path: aot.path,
|
|
101
|
+
slot
|
|
102
|
+
}, { mirror: {
|
|
103
|
+
source: emitted.source,
|
|
104
|
+
hasExternals: false
|
|
105
|
+
} });
|
|
106
|
+
else if (ext.unions && !ext.hof) {
|
|
107
|
+
const u = captureMirrorUnions(schema, ext.unions);
|
|
108
|
+
if (u) Capture.set({
|
|
109
|
+
method: aot.method,
|
|
110
|
+
path: aot.path,
|
|
111
|
+
slot
|
|
112
|
+
}, { mirror: {
|
|
113
|
+
source: emitted.source,
|
|
114
|
+
hasExternals: true,
|
|
115
|
+
u
|
|
116
|
+
} });
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
} catch {}
|
|
120
|
+
}
|
|
121
|
+
function captureCodecMirror(schema, aot, slot, sanitize, dir) {
|
|
122
|
+
const dirOpt = dir === "decode" ? { decode: true } : { encode: true };
|
|
123
|
+
try {
|
|
124
|
+
const emitted = createMirror(schema, {
|
|
125
|
+
Compile,
|
|
126
|
+
sanitize,
|
|
127
|
+
...dirOpt,
|
|
128
|
+
emit: true
|
|
129
|
+
});
|
|
130
|
+
if (typeof emitted?.source === "string") {
|
|
131
|
+
const ext = emitted.externals;
|
|
132
|
+
if (ext?.codecs && !ext.hof && captureMirrorCodecs(schema, ext.codecs, dir)) {
|
|
133
|
+
let u;
|
|
134
|
+
let freezable = true;
|
|
135
|
+
if (ext.unions && ext.unions.length) {
|
|
136
|
+
u = captureMirrorUnions(schema, ext.unions);
|
|
137
|
+
if (!u) freezable = false;
|
|
138
|
+
}
|
|
139
|
+
if (freezable) {
|
|
140
|
+
const mirror = {
|
|
141
|
+
source: emitted.source,
|
|
142
|
+
hasExternals: true,
|
|
143
|
+
u
|
|
144
|
+
};
|
|
145
|
+
Capture.set({
|
|
146
|
+
method: aot.method,
|
|
147
|
+
path: aot.path,
|
|
148
|
+
slot
|
|
149
|
+
}, dir === "decode" ? { decodeMirror: mirror } : { encodeMirror: mirror });
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
} catch {}
|
|
154
|
+
}
|
|
155
|
+
function captureBridgeFree(aot, slot, rawSchema) {
|
|
156
|
+
const captured = Capture.get({
|
|
157
|
+
method: aot.method,
|
|
158
|
+
path: aot.path,
|
|
159
|
+
slot
|
|
160
|
+
});
|
|
161
|
+
if (captured) Capture.set({
|
|
162
|
+
method: aot.method,
|
|
163
|
+
path: aot.path,
|
|
164
|
+
slot
|
|
165
|
+
}, { bridgeFree: isCapturedBridgeFree(captured, rawSchema, captured.coercePlan && typeof rawSchema !== "string" ? buildCoercedFromPlan(rawSchema, captured.coercePlan) : rawSchema) });
|
|
166
|
+
}
|
|
167
|
+
const impl = {
|
|
168
|
+
sourceOnlyValidator,
|
|
169
|
+
maybeCapture,
|
|
170
|
+
captureMirror,
|
|
171
|
+
captureCodecMirror,
|
|
172
|
+
captureBridgeFree
|
|
173
|
+
};
|
|
174
|
+
function installCaptureImpl() {
|
|
175
|
+
setCaptureImpl(impl);
|
|
176
|
+
installReconstructImpl();
|
|
177
|
+
}
|
|
178
|
+
installCaptureImpl();
|
|
179
|
+
|
|
180
|
+
//#endregion
|
|
181
|
+
export { impl as captureImplementation, installCaptureImpl };
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { CheckBuildResult, FrozenMirror, FrozenValidator, ReconstructImpl, StringCodecNode } from "./aot.js";
|
|
2
|
+
|
|
3
|
+
//#region src/compile/aot-reconstruct.d.ts
|
|
4
|
+
declare function reconstructCheck(build: CheckBuildResult): {
|
|
5
|
+
defs: string;
|
|
6
|
+
value: string;
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* Rebuild TypeBox `External[]` for `schema` in `BuildSchema` traversal order:
|
|
10
|
+
* object (patternProperties → properties) → array (items) → string (pattern) →
|
|
11
|
+
* guard → const → enum → not/allOf/anyOf/oneOf → refine LAST.
|
|
12
|
+
*/
|
|
13
|
+
declare function collectExternals(schema: any, out?: unknown[]): unknown[];
|
|
14
|
+
declare function externalsMatch(a: unknown[], b: unknown[]): boolean;
|
|
15
|
+
declare const Source: {
|
|
16
|
+
readonly checkFactory: (identifier: string, code: string) => string;
|
|
17
|
+
readonly checkCode: (defs: string, value: string) => string;
|
|
18
|
+
readonly handlerFactory: (alias: string, code: string) => string;
|
|
19
|
+
readonly mirrorFactory: (source: string, hasExternals: boolean) => string;
|
|
20
|
+
readonly bothFactory: (identifier: string, checkDefs: string, checkValue: string, mirrorSource: string, mirrorHasExternals: boolean) => string;
|
|
21
|
+
};
|
|
22
|
+
declare function collectMirrorUnions(schema: any, out?: unknown[][]): unknown[][];
|
|
23
|
+
declare function instantiateFrozenMirror(frozen: FrozenMirror, schema: unknown): (value: unknown) => unknown;
|
|
24
|
+
declare function instantiateFrozenDecodeMirror(frozen: FrozenMirror, schema: unknown, dir?: 'decode' | 'encode'): (value: unknown) => unknown;
|
|
25
|
+
declare const instantiateFrozenEncodeMirror: (frozen: FrozenMirror, schema: unknown) => ((value: unknown) => unknown);
|
|
26
|
+
declare function instantiateFrozenBoth(frozen: FrozenValidator, checkSchema: unknown, mirrorSchema: unknown): {
|
|
27
|
+
check?: (value: unknown) => boolean;
|
|
28
|
+
clean?: (value: unknown) => unknown;
|
|
29
|
+
};
|
|
30
|
+
declare function collectStringCodecNodes(schema: any, out?: StringCodecNode[]): StringCodecNode[];
|
|
31
|
+
/**
|
|
32
|
+
* Overwrite every ObjectString/ArrayString node's `~refine[0].check` and
|
|
33
|
+
* `~codec.decode` with the baked `ic` closures. After this runs, the node no
|
|
34
|
+
* longer calls the constructor's live `typebox/value` Check/Decode
|
|
35
|
+
*
|
|
36
|
+
* Iterate in reverse so nested codecs reconstruct bottom-up.
|
|
37
|
+
*/
|
|
38
|
+
declare function reconstructInnerCodecs(ic: NonNullable<FrozenValidator['ic']>, schema: any): void;
|
|
39
|
+
/**
|
|
40
|
+
* verify that mirror unions can be reconstructed in build time
|
|
41
|
+
*
|
|
42
|
+
* return undefined if not reconstructable
|
|
43
|
+
* `truthUnions` is `mir.externals.unions` (compiled branches).
|
|
44
|
+
*/
|
|
45
|
+
declare function captureMirrorUnions(schema: unknown, truthUnions: any[][]): {
|
|
46
|
+
identifier: string;
|
|
47
|
+
code: string;
|
|
48
|
+
}[][] | undefined;
|
|
49
|
+
declare function captureMirrorCodecs(schema: unknown, truthCodecs: Function[], dir?: 'decode' | 'encode'): boolean;
|
|
50
|
+
/** Runtime reconstruction table the generated manifest module registers. */
|
|
51
|
+
declare const Reconstruct: ReconstructImpl;
|
|
52
|
+
declare function installReconstructImpl(): void;
|
|
53
|
+
//#endregion
|
|
54
|
+
export { Reconstruct, Source, captureMirrorCodecs, captureMirrorUnions, collectExternals, collectMirrorUnions, collectStringCodecNodes, externalsMatch, installReconstructImpl, instantiateFrozenBoth, instantiateFrozenDecodeMirror, instantiateFrozenEncodeMirror, instantiateFrozenMirror, reconstructCheck, reconstructInnerCodecs };
|