elysia 2.0.0-beta.2 → 2.0.0-beta.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/adapter/bun/index.d.ts +18 -2
- package/dist/adapter/bun/index.js +467 -110
- package/dist/adapter/bun/index.mjs +467 -112
- package/dist/adapter/types.d.ts +2 -21
- package/dist/adapter/utils.d.ts +14 -3
- package/dist/adapter/utils.js +146 -87
- package/dist/adapter/utils.mjs +148 -90
- package/dist/adapter/web-standard/handler.d.ts +1 -1
- package/dist/adapter/web-standard/handler.js +47 -36
- package/dist/adapter/web-standard/handler.mjs +48 -37
- package/dist/adapter/web-standard/index.js +2 -30
- package/dist/adapter/web-standard/index.mjs +2 -30
- package/dist/adapter/web-standard/utils.js +5 -18
- package/dist/adapter/web-standard/utils.mjs +5 -18
- package/dist/base.d.ts +99 -67
- package/dist/base.js +678 -510
- package/dist/base.mjs +682 -514
- package/dist/compile/aot-capture.d.ts +2 -13
- package/dist/compile/aot-capture.js +26 -69
- package/dist/compile/aot-capture.mjs +28 -67
- package/dist/compile/aot-emit.d.ts +9 -7
- package/dist/compile/aot-emit.js +42 -19
- package/dist/compile/aot-emit.mjs +42 -20
- package/dist/compile/aot.d.ts +17 -19
- package/dist/compile/aot.js +41 -51
- package/dist/compile/aot.mjs +39 -49
- package/dist/compile/handler/descriptor.d.ts +9 -12
- package/dist/compile/handler/descriptor.js +19 -44
- package/dist/compile/handler/descriptor.mjs +20 -42
- package/dist/compile/handler/frozen-validator.d.ts +5 -10
- package/dist/compile/handler/frozen-validator.js +55 -105
- package/dist/compile/handler/frozen-validator.mjs +53 -105
- package/dist/compile/handler/index.d.ts +7 -5
- package/dist/compile/handler/index.js +270 -111
- package/dist/compile/handler/index.mjs +270 -113
- package/dist/compile/handler/jit.d.ts +19 -17
- package/dist/compile/handler/jit.js +222 -149
- package/dist/compile/handler/jit.mjs +224 -152
- package/dist/compile/handler/params.d.ts +1 -4
- package/dist/compile/handler/params.js +10 -5
- package/dist/compile/handler/params.mjs +14 -8
- package/dist/compile/handler/reconstruct.d.ts +2 -2
- package/dist/compile/handler/reconstruct.js +6 -4
- package/dist/compile/handler/reconstruct.mjs +6 -4
- package/dist/compile/handler/static-clone-resolver.d.ts +6 -0
- package/dist/compile/handler/static-clone-resolver.js +8 -0
- package/dist/compile/handler/static-clone-resolver.mjs +7 -0
- package/dist/compile/handler/utils.d.ts +22 -11
- package/dist/compile/handler/utils.js +159 -253
- package/dist/compile/handler/utils.mjs +155 -252
- package/dist/compile/jit-probe.d.ts +2 -2
- package/dist/compile/jit-probe.js +2 -3
- package/dist/compile/jit-probe.mjs +2 -3
- package/dist/compile/lexer.d.ts +7 -4
- package/dist/compile/lexer.js +268 -6
- package/dist/compile/lexer.mjs +268 -4
- package/dist/compile/utils.js +7 -4
- package/dist/compile/utils.mjs +7 -4
- package/dist/compiled.d.ts +0 -1
- package/dist/compiled.js +0 -1
- package/dist/compiled.mjs +0 -1
- package/dist/context.d.ts +14 -4
- package/dist/context.js +17 -32
- package/dist/context.mjs +19 -34
- package/dist/cookie/config.d.ts +7 -2
- package/dist/cookie/config.js +76 -16
- package/dist/cookie/config.mjs +77 -18
- package/dist/cookie/cookie.d.ts +1 -1
- package/dist/cookie/cookie.js +8 -10
- package/dist/cookie/cookie.mjs +8 -10
- package/dist/cookie/crypto.d.ts +8 -7
- package/dist/cookie/crypto.js +51 -40
- package/dist/cookie/crypto.mjs +51 -41
- package/dist/cookie/error.d.ts +1 -2
- package/dist/cookie/error.js +6 -4
- package/dist/cookie/error.mjs +6 -4
- package/dist/cookie/lib.js +7 -2
- package/dist/cookie/lib.mjs +7 -2
- package/dist/cookie/serialize.js +1 -1
- package/dist/cookie/serialize.mjs +1 -1
- package/dist/cookie/types.d.ts +10 -0
- package/dist/cookie/utils.d.ts +4 -8
- package/dist/cookie/utils.js +86 -52
- package/dist/cookie/utils.mjs +89 -54
- package/dist/error.d.ts +113 -103
- package/dist/error.js +181 -117
- package/dist/error.mjs +179 -118
- package/dist/generation.d.ts +2 -2
- package/dist/handler/error.d.ts +26 -15
- package/dist/handler/error.js +77 -96
- package/dist/handler/error.mjs +74 -97
- package/dist/handler/fetch.js +87 -108
- package/dist/handler/fetch.mjs +89 -110
- package/dist/handler/utils.d.ts +12 -6
- package/dist/handler/utils.js +84 -28
- package/dist/handler/utils.mjs +80 -28
- package/dist/index.d.ts +12 -12
- package/dist/index.js +2 -9
- package/dist/index.mjs +1 -1
- package/dist/memory.js +1 -1
- package/dist/memory.mjs +1 -1
- package/dist/package.js +1 -1
- package/dist/package.mjs +1 -1
- package/dist/plugin/aot/bun.d.ts +8 -1
- package/dist/plugin/aot/bun.js +16 -9
- package/dist/plugin/aot/bun.mjs +17 -10
- package/dist/plugin/aot/core.d.ts +52 -60
- package/dist/plugin/aot/core.js +208 -109
- package/dist/plugin/aot/core.mjs +205 -111
- package/dist/plugin/aot/esbuild.js +8 -5
- package/dist/plugin/aot/esbuild.mjs +9 -6
- package/dist/plugin/aot/hooks.d.ts +6 -17
- package/dist/plugin/aot/hooks.js +36 -14
- package/dist/plugin/aot/hooks.mjs +37 -16
- package/dist/plugin/aot/rspack-loader.d.ts +1 -5
- package/dist/plugin/aot/rspack-loader.js +1 -10
- package/dist/plugin/aot/rspack-loader.mjs +1 -10
- package/dist/plugin/aot/rspack.js +2 -5
- package/dist/plugin/aot/rspack.mjs +3 -6
- package/dist/plugin/aot/source.d.ts +6 -42
- package/dist/plugin/aot/source.js +71 -71
- package/dist/plugin/aot/source.mjs +72 -72
- package/dist/plugin/aot/treeshake.js +2 -6
- package/dist/plugin/aot/treeshake.mjs +2 -6
- package/dist/plugin/aot/unplugin.d.ts +1 -4
- package/dist/plugin/aot/unplugin.js +4 -13
- package/dist/plugin/aot/unplugin.mjs +4 -13
- package/dist/plugin/aot/vite.d.ts +2 -6
- package/dist/plugin/aot/worker.js +2 -2
- package/dist/plugin/aot/worker.mjs +2 -2
- package/dist/plugin/auto-head.js +1 -10
- package/dist/plugin/auto-head.mjs +1 -10
- package/dist/plugin/trace.d.ts +2 -1
- package/dist/route-table.d.ts +3 -0
- package/dist/route-table.js +16 -11
- package/dist/route-table.mjs +16 -11
- package/dist/schema-snapshot.d.ts +0 -4
- package/dist/schema-snapshot.js +32 -49
- package/dist/schema-snapshot.mjs +32 -49
- package/dist/sucrose.d.ts +6 -55
- package/dist/sucrose.js +103 -731
- package/dist/sucrose.mjs +102 -724
- package/dist/trace.d.ts +5 -8
- package/dist/trace.js +23 -134
- package/dist/trace.mjs +24 -135
- package/dist/type/bridge-live.d.ts +4 -2
- package/dist/type/bridge-live.js +6 -3
- package/dist/type/bridge-live.mjs +4 -2
- package/dist/type/bridge.d.ts +6 -4
- package/dist/type/bridge.js +17 -9
- package/dist/type/bridge.mjs +5 -3
- package/dist/type/coerce-plan.d.ts +5 -5
- package/dist/type/coerce-plan.js +41 -39
- package/dist/type/coerce-plan.mjs +43 -39
- package/dist/type/coerce.d.ts +3 -7
- package/dist/type/coerce.js +18 -75
- package/dist/type/coerce.mjs +19 -76
- package/dist/type/compat.d.ts +3 -0
- package/dist/type/compat.js +23 -14
- package/dist/type/compat.mjs +20 -10
- package/dist/type/elysia/array-buffer.d.ts +4 -1
- package/dist/type/elysia/array-buffer.js +17 -17
- package/dist/type/elysia/array-buffer.mjs +17 -17
- package/dist/type/elysia/array-string.js +2 -40
- package/dist/type/elysia/array-string.mjs +2 -39
- package/dist/type/elysia/array.js +5 -9
- package/dist/type/elysia/array.mjs +5 -9
- package/dist/type/elysia/boolean-string.js +2 -3
- package/dist/type/elysia/boolean-string.mjs +1 -1
- package/dist/type/elysia/boolean.js +3 -12
- package/dist/type/elysia/boolean.mjs +3 -11
- package/dist/type/elysia/cookie.d.ts +3 -4
- package/dist/type/elysia/cookie.js +5 -15
- package/dist/type/elysia/cookie.mjs +5 -15
- package/dist/type/elysia/date.js +13 -20
- package/dist/type/elysia/date.mjs +10 -16
- package/dist/type/elysia/file-type.d.ts +11 -4
- package/dist/type/elysia/file-type.js +17 -2
- package/dist/type/elysia/file-type.mjs +17 -3
- package/dist/type/elysia/file.d.ts +1 -3
- package/dist/type/elysia/file.js +11 -27
- package/dist/type/elysia/file.mjs +11 -12
- package/dist/type/elysia/files.js +7 -11
- package/dist/type/elysia/files.mjs +7 -10
- package/dist/type/elysia/form.js +2 -3
- package/dist/type/elysia/form.mjs +3 -3
- package/dist/type/elysia/integer-string.d.ts +1 -1
- package/dist/type/elysia/integer-string.js +4 -30
- package/dist/type/elysia/integer-string.mjs +5 -30
- package/dist/type/elysia/integer.js +3 -12
- package/dist/type/elysia/integer.mjs +3 -11
- package/dist/type/elysia/intersect.js +0 -5
- package/dist/type/elysia/intersect.mjs +0 -5
- package/dist/type/elysia/json-string.d.ts +7 -0
- package/dist/type/elysia/json-string.js +47 -0
- package/dist/type/elysia/json-string.mjs +46 -0
- package/dist/type/elysia/keep-config.d.ts +14 -0
- package/dist/type/elysia/keep-config.js +62 -0
- package/dist/type/elysia/keep-config.mjs +53 -0
- package/dist/type/elysia/maybe-empty.js +3 -4
- package/dist/type/elysia/maybe-empty.mjs +1 -1
- package/dist/type/elysia/nullable.js +2 -3
- package/dist/type/elysia/nullable.mjs +1 -1
- package/dist/type/elysia/number.d.ts +2 -1
- package/dist/type/elysia/number.js +19 -11
- package/dist/type/elysia/number.mjs +18 -10
- package/dist/type/elysia/numeric-enum.d.ts +1 -1
- package/dist/type/elysia/numeric-enum.js +4 -5
- package/dist/type/elysia/numeric-enum.mjs +3 -3
- package/dist/type/elysia/numeric.d.ts +4 -2
- package/dist/type/elysia/numeric.js +20 -17
- package/dist/type/elysia/numeric.mjs +19 -16
- package/dist/type/elysia/object-string.js +2 -37
- package/dist/type/elysia/object-string.mjs +2 -36
- package/dist/type/elysia/object.js +5 -9
- package/dist/type/elysia/object.mjs +5 -9
- package/dist/type/elysia/optional.js +8 -8
- package/dist/type/elysia/optional.mjs +7 -7
- package/dist/type/elysia/problem.d.ts +20 -0
- package/dist/type/elysia/problem.js +25 -0
- package/dist/type/elysia/problem.mjs +24 -0
- package/dist/type/elysia/string.js +4 -3
- package/dist/type/elysia/string.mjs +4 -2
- package/dist/type/elysia/uint8-array.d.ts +1 -1
- package/dist/type/elysia/uint8-array.js +5 -20
- package/dist/type/elysia/uint8-array.mjs +5 -19
- package/dist/type/elysia/union-enum.js +7 -8
- package/dist/type/elysia/union-enum.mjs +6 -6
- package/dist/type/elysia/union.js +0 -5
- package/dist/type/elysia/union.mjs +0 -5
- package/dist/type/elysia/utils.d.ts +2 -4
- package/dist/type/elysia/utils.js +10 -21
- package/dist/type/elysia/utils.mjs +7 -15
- package/dist/type/exports.d.ts +4 -2
- package/dist/type/exports.js +23 -2
- package/dist/type/exports.mjs +11 -2
- package/dist/type/index.d.ts +10 -2
- package/dist/type/index.js +62 -15
- package/dist/type/index.mjs +65 -11
- package/dist/type/shared.d.ts +3 -9
- package/dist/type/shared.js +44 -97
- package/dist/type/shared.mjs +43 -97
- package/dist/type/sync-require.d.ts +11 -0
- package/dist/type/sync-require.js +14 -0
- package/dist/type/sync-require.mjs +12 -0
- package/dist/type/typebox-system-lite.d.ts +2 -0
- package/dist/type/typebox-system-lite.js +34 -0
- package/dist/type/typebox-system-lite.mjs +3 -0
- package/dist/type/typebox-system-require.d.ts +7 -0
- package/dist/type/typebox-system-require.js +24 -0
- package/dist/type/typebox-system-require.mjs +23 -0
- package/dist/type/typebox-type-live.d.ts +11 -0
- package/dist/type/typebox-type-live.js +45 -0
- package/dist/type/typebox-type-live.mjs +20 -0
- package/dist/type/typebox-type.d.ts +34 -0
- package/dist/type/typebox-type.js +199 -0
- package/dist/type/typebox-type.mjs +86 -0
- package/dist/type/typebox-value-live.d.ts +11 -0
- package/dist/type/typebox-value-live.js +31 -0
- package/dist/type/typebox-value-live.mjs +13 -0
- package/dist/type/typebox-value-ops.d.ts +4 -0
- package/dist/type/typebox-value-ops.js +90 -0
- package/dist/type/typebox-value-ops.mjs +5 -0
- package/dist/type/typebox-value-require.d.ts +8 -0
- package/dist/type/typebox-value-require.js +28 -0
- package/dist/type/typebox-value-require.mjs +25 -0
- package/dist/type/typebox-value.d.ts +35 -0
- package/dist/type/typebox-value.js +161 -0
- package/dist/type/typebox-value.mjs +74 -0
- package/dist/type/utils.js +1 -10
- package/dist/type/utils.mjs +1 -10
- package/dist/type/validator/clean-safe.d.ts +2 -4
- package/dist/type/validator/clean-safe.js +15 -11
- package/dist/type/validator/clean-safe.mjs +16 -10
- package/dist/type/validator/custom-error.js +12 -16
- package/dist/type/validator/custom-error.mjs +11 -14
- package/dist/type/validator/default-precompute.d.ts +9 -1
- package/dist/type/validator/default-precompute.js +40 -77
- package/dist/type/validator/default-precompute.mjs +37 -74
- package/dist/type/validator/exact-mirror-live.d.ts +8 -0
- package/dist/type/validator/exact-mirror-live.js +22 -0
- package/dist/type/validator/exact-mirror-live.mjs +17 -0
- package/dist/type/validator/exact-mirror-require.d.ts +4 -0
- package/dist/type/validator/exact-mirror-require.js +11 -0
- package/dist/type/validator/exact-mirror-require.mjs +9 -0
- package/dist/type/validator/exact-mirror.js +8 -8
- package/dist/type/validator/exact-mirror.mjs +9 -8
- package/dist/type/validator/frozen-check.js +5 -6
- package/dist/type/validator/frozen-check.mjs +6 -7
- package/dist/type/validator/index.d.ts +1 -0
- package/dist/type/validator/index.js +169 -166
- package/dist/type/validator/index.mjs +162 -158
- package/dist/type/validator/string-codec-aot.d.ts +2 -1
- package/dist/type/validator/string-codec-aot.js +8 -2
- package/dist/type/validator/string-codec-aot.mjs +7 -2
- package/dist/type/validator/validator-cache.d.ts +5 -4
- package/dist/type/validator/validator-cache.js +41 -24
- package/dist/type/validator/validator-cache.mjs +40 -22
- package/dist/types.d.ts +263 -95
- package/dist/universal/constants.d.ts +2 -1
- package/dist/universal/constants.js +3 -1
- package/dist/universal/constants.mjs +2 -1
- package/dist/universal/env.js +2 -3
- package/dist/universal/env.mjs +2 -3
- package/dist/universal/file.d.ts +6 -4
- package/dist/universal/file.js +11 -16
- package/dist/universal/file.mjs +12 -16
- package/dist/universal/index.d.ts +2 -2
- package/dist/universal/is-production.d.ts +1 -2
- package/dist/universal/is-production.js +1 -3
- package/dist/universal/is-production.mjs +2 -3
- package/dist/universal/server.d.ts +0 -272
- package/dist/utils.d.ts +43 -7
- package/dist/utils.js +181 -126
- package/dist/utils.mjs +168 -126
- package/dist/validator/index.d.ts +1 -1
- package/dist/validator/index.js +17 -30
- package/dist/validator/index.mjs +18 -31
- package/dist/validator/route.js +2 -1
- package/dist/validator/route.mjs +2 -1
- package/dist/ws/context.d.ts +8 -1
- package/dist/ws/context.js +68 -44
- package/dist/ws/context.mjs +67 -44
- package/dist/ws/parser.js +10 -20
- package/dist/ws/parser.mjs +10 -20
- package/dist/ws/route.d.ts +2 -2
- package/dist/ws/route.js +249 -141
- package/dist/ws/route.mjs +254 -146
- package/dist/ws/types.d.ts +2 -2
- package/package.json +23 -49
- package/dist/compile/analysis-cache.d.ts +0 -6
- package/dist/compile/analysis-cache.js +0 -12
- package/dist/compile/analysis-cache.mjs +0 -11
- package/dist/cookie/index.d.ts +0 -7
- package/dist/cookie/index.js +0 -13
- package/dist/cookie/index.mjs +0 -7
- package/dist/handler/index.d.ts +0 -2
- package/dist/handler/index.js +0 -5
- package/dist/handler/index.mjs +0 -3
|
@@ -1,10 +1,8 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
-
const require_runtime = require('../../_virtual/_rolldown/runtime.js');
|
|
3
2
|
const require_universal_constants = require('../../universal/constants.js');
|
|
4
3
|
const require_utils = require('../../utils.js');
|
|
5
4
|
const require_type_constants = require('../constants.js');
|
|
6
5
|
const require_type_coerce = require('../coerce.js');
|
|
7
|
-
let typebox_value = require("typebox/value");
|
|
8
6
|
|
|
9
7
|
//#region src/type/validator/validator-cache.ts
|
|
10
8
|
const DEFAULT_CACHE_LIMIT = 1024;
|
|
@@ -46,6 +44,13 @@ function walk(value, key, mode, state) {
|
|
|
46
44
|
if (!state.hasRef && value["$ref"]) state.hasRef = true;
|
|
47
45
|
}
|
|
48
46
|
if (!state.hasFileType && (elyTyp === require_type_constants.ELYSIA_TYPES.File || elyTyp === require_type_constants.ELYSIA_TYPES.Files)) state.hasFileType = true;
|
|
47
|
+
if (!state.hasCodec) {
|
|
48
|
+
const codec = value["~codec"];
|
|
49
|
+
if (codec !== null && typeof codec === "object" && "encode" in codec && "decode" in codec) {
|
|
50
|
+
state.hasCodec = true;
|
|
51
|
+
if (!state.forceKey) state.buildKey = false;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
49
54
|
const toJSON = value.toJSON;
|
|
50
55
|
if (typeof toJSON === "function") value = toJSON.call(value, typeof key === "number" ? "" + key : key);
|
|
51
56
|
}
|
|
@@ -119,17 +124,17 @@ function walkArray(value, mode, state) {
|
|
|
119
124
|
return serialized + "]";
|
|
120
125
|
}
|
|
121
126
|
function computeSchemaMeta(schema, forceKey) {
|
|
122
|
-
const hasCodec = (0, typebox_value.HasCodec)(schema);
|
|
123
127
|
const state = {
|
|
124
128
|
isOpaque: false,
|
|
125
129
|
hasRef: false,
|
|
126
130
|
hasFileType: false,
|
|
131
|
+
hasCodec: false,
|
|
127
132
|
forceKey,
|
|
128
|
-
buildKey:
|
|
133
|
+
buildKey: true,
|
|
129
134
|
depth: 0
|
|
130
135
|
};
|
|
131
136
|
const key = walk(schema, "", NODE, state);
|
|
132
|
-
const special = hasCodec || state.isOpaque;
|
|
137
|
+
const special = state.hasCodec || state.isOpaque;
|
|
133
138
|
return {
|
|
134
139
|
special,
|
|
135
140
|
hasRef: state.hasRef,
|
|
@@ -137,8 +142,8 @@ function computeSchemaMeta(schema, forceKey) {
|
|
|
137
142
|
key: forceKey ? key : special ? "" : key
|
|
138
143
|
};
|
|
139
144
|
}
|
|
140
|
-
const schemaCacheKey = (schema) => computeSchemaMeta(schema, true).key;
|
|
141
145
|
const mayHaveFileType = (schema) => !("~standard" in schema) && TypeBoxValidatorCache.meta(schema).hasFileType;
|
|
146
|
+
let clearWeak;
|
|
142
147
|
var TypeBoxValidatorCache = class TypeBoxValidatorCache {
|
|
143
148
|
static {
|
|
144
149
|
this.EMPTY = require_utils.nullObject();
|
|
@@ -175,17 +180,18 @@ var TypeBoxValidatorCache = class TypeBoxValidatorCache {
|
|
|
175
180
|
constructor(gcTime = DEFAULT_GC_TIME) {
|
|
176
181
|
this.#gcTime = gcTime;
|
|
177
182
|
}
|
|
178
|
-
#scheduleClear() {
|
|
179
|
-
if (require_universal_constants.isCloudflareWorker) return;
|
|
180
|
-
if (this.#gc) clearTimeout(this.#gc);
|
|
181
|
-
this.#gc = setTimeout(() => this.clear(), this.#gcTime);
|
|
182
|
-
this.#gc.unref?.();
|
|
183
|
-
}
|
|
184
183
|
get(schema, coercions = TypeBoxValidatorCache.EMPTY, normalize = "", models) {
|
|
185
184
|
const meta = TypeBoxValidatorCache.meta(schema);
|
|
186
185
|
normalize += TypeBoxValidatorCache.#modelsToken(models, meta.hasRef);
|
|
187
|
-
const
|
|
188
|
-
|
|
186
|
+
const byNormalize = this.#referenceCache.get(schema);
|
|
187
|
+
const refBucket = byNormalize?.get(normalize);
|
|
188
|
+
if (refBucket?.has(coercions)) {
|
|
189
|
+
if (byNormalize.size >= DEFAULT_CACHE_LIMIT) {
|
|
190
|
+
byNormalize.delete(normalize);
|
|
191
|
+
byNormalize.set(normalize, refBucket);
|
|
192
|
+
}
|
|
193
|
+
return refBucket.get(coercions);
|
|
194
|
+
}
|
|
189
195
|
if (meta.special) return;
|
|
190
196
|
const key = meta.key + "\0" + normalize;
|
|
191
197
|
const coercionsCache = this.#cache.get(key);
|
|
@@ -197,35 +203,46 @@ var TypeBoxValidatorCache = class TypeBoxValidatorCache {
|
|
|
197
203
|
if (coercionsCache.has(coercions)) return coercionsCache.get(coercions);
|
|
198
204
|
}
|
|
199
205
|
}
|
|
200
|
-
#
|
|
206
|
+
#setRefBucket(schema, normalize, cache) {
|
|
201
207
|
let byNormalize = this.#referenceCache.get(schema);
|
|
202
208
|
if (!byNormalize) {
|
|
203
209
|
byNormalize = /* @__PURE__ */ new Map();
|
|
204
210
|
this.#referenceCache.set(schema, byNormalize);
|
|
205
211
|
}
|
|
206
|
-
|
|
212
|
+
if (!byNormalize.has(normalize) && byNormalize.size >= DEFAULT_CACHE_LIMIT) require_utils.evictOldestHalf(byNormalize);
|
|
213
|
+
byNormalize.set(normalize, cache);
|
|
207
214
|
}
|
|
208
215
|
set(schema, coercions = TypeBoxValidatorCache.EMPTY, validator, normalize = "", models) {
|
|
209
|
-
|
|
216
|
+
if (!require_universal_constants.isCloudflareWorker) {
|
|
217
|
+
if (this.#gc) clearTimeout(this.#gc);
|
|
218
|
+
this.#gc = setTimeout(clearWeak ??= (() => {
|
|
219
|
+
try {
|
|
220
|
+
return Function("r", "r.deref()?.clear()");
|
|
221
|
+
} catch {
|
|
222
|
+
return (ref) => ref.deref()?.clear();
|
|
223
|
+
}
|
|
224
|
+
})(), this.#gcTime, new WeakRef(this));
|
|
225
|
+
this.#gc.unref?.();
|
|
226
|
+
}
|
|
210
227
|
const meta = TypeBoxValidatorCache.meta(schema);
|
|
211
228
|
normalize += TypeBoxValidatorCache.#modelsToken(models, meta.hasRef);
|
|
212
229
|
if (meta.special) {
|
|
213
230
|
const cache = (/* @__PURE__ */ new WeakMap()).set(coercions, validator);
|
|
214
|
-
this.#
|
|
231
|
+
this.#setRefBucket(schema, normalize, cache);
|
|
215
232
|
return;
|
|
216
233
|
}
|
|
217
234
|
const key = meta.key + "\0" + normalize;
|
|
218
235
|
if (this.#cache.has(key)) {
|
|
219
236
|
const cache = this.#cache.get(key).set(coercions, validator);
|
|
220
|
-
const byNormalize = this.#
|
|
221
|
-
if (byNormalize
|
|
222
|
-
else
|
|
237
|
+
const byNormalize = this.#referenceCache.get(schema);
|
|
238
|
+
if (byNormalize?.has(normalize)) byNormalize.get(normalize).set(coercions, validator);
|
|
239
|
+
else this.#setRefBucket(schema, normalize, cache);
|
|
223
240
|
return;
|
|
224
241
|
}
|
|
225
242
|
if (this.#cache.size >= DEFAULT_CACHE_LIMIT) require_utils.evictOldestHalf(this.#cache);
|
|
226
243
|
const cache = (/* @__PURE__ */ new WeakMap()).set(coercions, validator);
|
|
227
244
|
this.#cache.set(key, cache);
|
|
228
|
-
this.#
|
|
245
|
+
this.#setRefBucket(schema, normalize, cache);
|
|
229
246
|
}
|
|
230
247
|
clear() {
|
|
231
248
|
if (this.#gc) {
|
|
@@ -242,6 +259,6 @@ var TypeBoxValidatorCache = class TypeBoxValidatorCache {
|
|
|
242
259
|
|
|
243
260
|
//#endregion
|
|
244
261
|
exports.TypeBoxValidatorCache = TypeBoxValidatorCache;
|
|
262
|
+
exports.computeSchemaMeta = computeSchemaMeta;
|
|
245
263
|
exports.fnKey = fnKey;
|
|
246
|
-
exports.mayHaveFileType = mayHaveFileType;
|
|
247
|
-
exports.schemaCacheKey = schemaCacheKey;
|
|
264
|
+
exports.mayHaveFileType = mayHaveFileType;
|
|
@@ -2,7 +2,6 @@ import { isCloudflareWorker } from "../../universal/constants.mjs";
|
|
|
2
2
|
import { evictOldestHalf, nullObject } from "../../utils.mjs";
|
|
3
3
|
import { ELYSIA_TYPES } from "../constants.mjs";
|
|
4
4
|
import { deferCoercions } from "../coerce.mjs";
|
|
5
|
-
import { HasCodec } from "typebox/value";
|
|
6
5
|
|
|
7
6
|
//#region src/type/validator/validator-cache.ts
|
|
8
7
|
const DEFAULT_CACHE_LIMIT = 1024;
|
|
@@ -44,6 +43,13 @@ function walk(value, key, mode, state) {
|
|
|
44
43
|
if (!state.hasRef && value["$ref"]) state.hasRef = true;
|
|
45
44
|
}
|
|
46
45
|
if (!state.hasFileType && (elyTyp === ELYSIA_TYPES.File || elyTyp === ELYSIA_TYPES.Files)) state.hasFileType = true;
|
|
46
|
+
if (!state.hasCodec) {
|
|
47
|
+
const codec = value["~codec"];
|
|
48
|
+
if (codec !== null && typeof codec === "object" && "encode" in codec && "decode" in codec) {
|
|
49
|
+
state.hasCodec = true;
|
|
50
|
+
if (!state.forceKey) state.buildKey = false;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
47
53
|
const toJSON = value.toJSON;
|
|
48
54
|
if (typeof toJSON === "function") value = toJSON.call(value, typeof key === "number" ? "" + key : key);
|
|
49
55
|
}
|
|
@@ -117,17 +123,17 @@ function walkArray(value, mode, state) {
|
|
|
117
123
|
return serialized + "]";
|
|
118
124
|
}
|
|
119
125
|
function computeSchemaMeta(schema, forceKey) {
|
|
120
|
-
const hasCodec = HasCodec(schema);
|
|
121
126
|
const state = {
|
|
122
127
|
isOpaque: false,
|
|
123
128
|
hasRef: false,
|
|
124
129
|
hasFileType: false,
|
|
130
|
+
hasCodec: false,
|
|
125
131
|
forceKey,
|
|
126
|
-
buildKey:
|
|
132
|
+
buildKey: true,
|
|
127
133
|
depth: 0
|
|
128
134
|
};
|
|
129
135
|
const key = walk(schema, "", NODE, state);
|
|
130
|
-
const special = hasCodec || state.isOpaque;
|
|
136
|
+
const special = state.hasCodec || state.isOpaque;
|
|
131
137
|
return {
|
|
132
138
|
special,
|
|
133
139
|
hasRef: state.hasRef,
|
|
@@ -135,8 +141,8 @@ function computeSchemaMeta(schema, forceKey) {
|
|
|
135
141
|
key: forceKey ? key : special ? "" : key
|
|
136
142
|
};
|
|
137
143
|
}
|
|
138
|
-
const schemaCacheKey = (schema) => computeSchemaMeta(schema, true).key;
|
|
139
144
|
const mayHaveFileType = (schema) => !("~standard" in schema) && TypeBoxValidatorCache.meta(schema).hasFileType;
|
|
145
|
+
let clearWeak;
|
|
140
146
|
var TypeBoxValidatorCache = class TypeBoxValidatorCache {
|
|
141
147
|
static {
|
|
142
148
|
this.EMPTY = nullObject();
|
|
@@ -173,17 +179,18 @@ var TypeBoxValidatorCache = class TypeBoxValidatorCache {
|
|
|
173
179
|
constructor(gcTime = DEFAULT_GC_TIME) {
|
|
174
180
|
this.#gcTime = gcTime;
|
|
175
181
|
}
|
|
176
|
-
#scheduleClear() {
|
|
177
|
-
if (isCloudflareWorker) return;
|
|
178
|
-
if (this.#gc) clearTimeout(this.#gc);
|
|
179
|
-
this.#gc = setTimeout(() => this.clear(), this.#gcTime);
|
|
180
|
-
this.#gc.unref?.();
|
|
181
|
-
}
|
|
182
182
|
get(schema, coercions = TypeBoxValidatorCache.EMPTY, normalize = "", models) {
|
|
183
183
|
const meta = TypeBoxValidatorCache.meta(schema);
|
|
184
184
|
normalize += TypeBoxValidatorCache.#modelsToken(models, meta.hasRef);
|
|
185
|
-
const
|
|
186
|
-
|
|
185
|
+
const byNormalize = this.#referenceCache.get(schema);
|
|
186
|
+
const refBucket = byNormalize?.get(normalize);
|
|
187
|
+
if (refBucket?.has(coercions)) {
|
|
188
|
+
if (byNormalize.size >= DEFAULT_CACHE_LIMIT) {
|
|
189
|
+
byNormalize.delete(normalize);
|
|
190
|
+
byNormalize.set(normalize, refBucket);
|
|
191
|
+
}
|
|
192
|
+
return refBucket.get(coercions);
|
|
193
|
+
}
|
|
187
194
|
if (meta.special) return;
|
|
188
195
|
const key = meta.key + "\0" + normalize;
|
|
189
196
|
const coercionsCache = this.#cache.get(key);
|
|
@@ -195,35 +202,46 @@ var TypeBoxValidatorCache = class TypeBoxValidatorCache {
|
|
|
195
202
|
if (coercionsCache.has(coercions)) return coercionsCache.get(coercions);
|
|
196
203
|
}
|
|
197
204
|
}
|
|
198
|
-
#
|
|
205
|
+
#setRefBucket(schema, normalize, cache) {
|
|
199
206
|
let byNormalize = this.#referenceCache.get(schema);
|
|
200
207
|
if (!byNormalize) {
|
|
201
208
|
byNormalize = /* @__PURE__ */ new Map();
|
|
202
209
|
this.#referenceCache.set(schema, byNormalize);
|
|
203
210
|
}
|
|
204
|
-
|
|
211
|
+
if (!byNormalize.has(normalize) && byNormalize.size >= DEFAULT_CACHE_LIMIT) evictOldestHalf(byNormalize);
|
|
212
|
+
byNormalize.set(normalize, cache);
|
|
205
213
|
}
|
|
206
214
|
set(schema, coercions = TypeBoxValidatorCache.EMPTY, validator, normalize = "", models) {
|
|
207
|
-
|
|
215
|
+
if (!isCloudflareWorker) {
|
|
216
|
+
if (this.#gc) clearTimeout(this.#gc);
|
|
217
|
+
this.#gc = setTimeout(clearWeak ??= (() => {
|
|
218
|
+
try {
|
|
219
|
+
return Function("r", "r.deref()?.clear()");
|
|
220
|
+
} catch {
|
|
221
|
+
return (ref) => ref.deref()?.clear();
|
|
222
|
+
}
|
|
223
|
+
})(), this.#gcTime, new WeakRef(this));
|
|
224
|
+
this.#gc.unref?.();
|
|
225
|
+
}
|
|
208
226
|
const meta = TypeBoxValidatorCache.meta(schema);
|
|
209
227
|
normalize += TypeBoxValidatorCache.#modelsToken(models, meta.hasRef);
|
|
210
228
|
if (meta.special) {
|
|
211
229
|
const cache = (/* @__PURE__ */ new WeakMap()).set(coercions, validator);
|
|
212
|
-
this.#
|
|
230
|
+
this.#setRefBucket(schema, normalize, cache);
|
|
213
231
|
return;
|
|
214
232
|
}
|
|
215
233
|
const key = meta.key + "\0" + normalize;
|
|
216
234
|
if (this.#cache.has(key)) {
|
|
217
235
|
const cache = this.#cache.get(key).set(coercions, validator);
|
|
218
|
-
const byNormalize = this.#
|
|
219
|
-
if (byNormalize
|
|
220
|
-
else
|
|
236
|
+
const byNormalize = this.#referenceCache.get(schema);
|
|
237
|
+
if (byNormalize?.has(normalize)) byNormalize.get(normalize).set(coercions, validator);
|
|
238
|
+
else this.#setRefBucket(schema, normalize, cache);
|
|
221
239
|
return;
|
|
222
240
|
}
|
|
223
241
|
if (this.#cache.size >= DEFAULT_CACHE_LIMIT) evictOldestHalf(this.#cache);
|
|
224
242
|
const cache = (/* @__PURE__ */ new WeakMap()).set(coercions, validator);
|
|
225
243
|
this.#cache.set(key, cache);
|
|
226
|
-
this.#
|
|
244
|
+
this.#setRefBucket(schema, normalize, cache);
|
|
227
245
|
}
|
|
228
246
|
clear() {
|
|
229
247
|
if (this.#gc) {
|
|
@@ -239,4 +257,4 @@ var TypeBoxValidatorCache = class TypeBoxValidatorCache {
|
|
|
239
257
|
};
|
|
240
258
|
|
|
241
259
|
//#endregion
|
|
242
|
-
export { TypeBoxValidatorCache, fnKey, mayHaveFileType
|
|
260
|
+
export { TypeBoxValidatorCache, computeSchemaMeta, fnKey, mayHaveFileType };
|