elysia 2.0.0-exp.2 → 2.0.0-exp.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/_virtual/_rolldown/runtime.js +2 -0
- package/dist/adapter/bun/index.d.ts +2 -25
- package/dist/adapter/bun/index.js +18 -12
- package/dist/adapter/bun/index.mjs +19 -13
- package/dist/adapter/bun/router.js +1 -1
- package/dist/adapter/bun/router.mjs +1 -1
- package/dist/adapter/constants.d.ts +3 -26
- package/dist/adapter/index.d.ts +1 -26
- package/dist/adapter/index.js +1 -17
- package/dist/adapter/index.mjs +1 -17
- package/dist/adapter/types.d.ts +1 -0
- package/dist/adapter/utils.d.ts +3 -4
- package/dist/adapter/utils.js +83 -81
- package/dist/adapter/utils.mjs +82 -80
- package/dist/adapter/web-standard/handler.js +6 -3
- package/dist/adapter/web-standard/handler.mjs +6 -3
- package/dist/adapter/web-standard/index.d.ts +2 -26
- package/dist/adapter/web-standard/index.js +12 -1
- package/dist/adapter/web-standard/index.mjs +12 -1
- package/dist/base.d.ts +38 -68
- package/dist/base.js +133 -97
- package/dist/base.mjs +133 -97
- package/dist/compile/aot.d.ts +22 -1
- package/dist/compile/aot.js +22 -0
- package/dist/compile/aot.mjs +22 -0
- package/dist/compile/handler/index.d.ts +2 -2
- package/dist/compile/handler/index.js +85 -456
- package/dist/compile/handler/index.mjs +86 -457
- package/dist/compile/handler/jit.d.ts +35 -0
- package/dist/compile/handler/jit.js +445 -0
- package/dist/compile/handler/jit.mjs +443 -0
- package/dist/compile/handler/params.js +3 -2
- package/dist/compile/handler/params.mjs +4 -3
- package/dist/compile/handler/reconstruct.d.ts +13 -0
- package/dist/compile/handler/reconstruct.js +29 -0
- package/dist/compile/handler/reconstruct.mjs +28 -0
- package/dist/compile/handler/utils.d.ts +2 -1
- package/dist/compile/handler/utils.js +9 -3
- package/dist/compile/handler/utils.mjs +9 -4
- package/dist/compile/jit-probe.d.ts +19 -0
- package/dist/compile/jit-probe.js +38 -0
- package/dist/compile/jit-probe.mjs +36 -0
- package/dist/cookie/index.d.ts +4 -3
- package/dist/cookie/index.js +2 -1
- package/dist/cookie/index.mjs +2 -1
- package/dist/cookie/serialize.d.ts +6 -0
- package/dist/cookie/serialize.js +28 -0
- package/dist/cookie/serialize.mjs +27 -0
- package/dist/cookie/utils.d.ts +1 -2
- package/dist/cookie/utils.js +2 -23
- package/dist/cookie/utils.mjs +5 -25
- package/dist/handler/error.js +2 -2
- package/dist/handler/error.mjs +3 -3
- package/dist/handler/fetch.js +49 -42
- package/dist/handler/fetch.mjs +50 -43
- package/dist/index.d.ts +13 -8
- package/dist/index.js +11 -4
- package/dist/index.mjs +7 -6
- package/dist/parse-query.js +1 -1
- package/dist/parse-query.mjs +1 -1
- package/dist/plugin/bun.d.ts +10 -0
- package/dist/plugin/bun.js +18 -1
- package/dist/plugin/bun.mjs +19 -2
- package/dist/plugin/core.d.ts +61 -1
- package/dist/plugin/core.js +90 -10
- package/dist/plugin/core.mjs +90 -12
- package/dist/plugin/esbuild.d.ts +10 -0
- package/dist/plugin/esbuild.js +18 -1
- package/dist/plugin/esbuild.mjs +19 -2
- package/dist/plugin/source.d.ts +38 -1
- package/dist/plugin/source.js +104 -13
- package/dist/plugin/source.mjs +102 -14
- package/dist/plugin/vite.d.ts +7 -0
- package/dist/plugin/vite.js +22 -1
- package/dist/plugin/vite.mjs +23 -2
- package/dist/sucrose.js +23 -10
- package/dist/sucrose.mjs +23 -10
- package/dist/trace.d.ts +1 -1
- package/dist/trace.js +4 -6
- package/dist/trace.mjs +4 -6
- package/dist/type/bridge.d.ts +9 -8
- package/dist/type/coerce.d.ts +20 -2
- package/dist/type/coerce.js +110 -0
- package/dist/type/coerce.mjs +109 -2
- package/dist/type/compat.js +4 -3
- package/dist/type/compat.mjs +2 -1
- package/dist/type/constants.d.ts +1 -1
- package/dist/type/elysia/boolean-string.js +1 -1
- package/dist/type/elysia/boolean-string.mjs +1 -1
- package/dist/type/elysia/file-type.js +1 -1
- package/dist/type/elysia/file-type.mjs +1 -1
- package/dist/type/elysia/integer-string.js +1 -1
- package/dist/type/elysia/integer-string.mjs +1 -1
- package/dist/type/exports.js +29 -2059
- package/dist/type/exports.mjs +7 -295
- package/dist/type/index.d.ts +6 -2
- package/dist/type/index.js +4 -0
- package/dist/type/index.mjs +3 -1
- package/dist/type/types.d.ts +1 -1
- package/dist/type/utils.js +4 -4
- package/dist/type/utils.mjs +4 -4
- package/dist/type/validator/custom-error.d.ts +10 -0
- package/dist/type/validator/custom-error.js +113 -0
- package/dist/type/validator/custom-error.mjs +110 -0
- package/dist/type/validator/default-precompute.d.ts +22 -0
- package/dist/type/validator/default-precompute.js +476 -0
- package/dist/type/validator/default-precompute.mjs +465 -0
- package/dist/type/validator/frozen-check.d.ts +11 -0
- package/dist/type/validator/frozen-check.js +19 -0
- package/dist/type/validator/frozen-check.mjs +18 -0
- package/dist/type/{validator.d.ts → validator/index.d.ts} +7 -20
- package/dist/type/validator/index.js +572 -0
- package/dist/type/validator/index.mjs +567 -0
- package/dist/type/validator/string-codec-aot.d.ts +9 -0
- package/dist/type/validator/string-codec-aot.js +106 -0
- package/dist/type/validator/string-codec-aot.mjs +102 -0
- package/dist/type/validator/validator-cache.d.ts +21 -0
- package/dist/type/validator/validator-cache.js +163 -0
- package/dist/type/validator/validator-cache.mjs +161 -0
- package/dist/types.d.ts +22 -14
- package/dist/universal/file.d.ts +1 -1
- package/dist/universal/file.js +35 -29
- package/dist/universal/file.mjs +35 -29
- package/dist/utils.d.ts +12 -1
- package/dist/utils.js +89 -45
- package/dist/utils.mjs +88 -46
- package/dist/validator/index.d.ts +1 -1
- package/dist/validator/index.js +15 -2
- package/dist/validator/index.mjs +15 -2
- package/dist/validator/route.d.ts +0 -1
- package/dist/ws/route.js +47 -24
- package/dist/ws/route.mjs +48 -25
- package/package.json +10 -4
- package/dist/type/validator.js +0 -1033
- package/dist/type/validator.mjs +0 -1029
package/dist/universal/file.mjs
CHANGED
|
@@ -1,22 +1,26 @@
|
|
|
1
1
|
import { isBun } from "./constants.mjs";
|
|
2
2
|
|
|
3
3
|
//#region src/universal/file.ts
|
|
4
|
+
const text = "text/";
|
|
5
|
+
const application = "application/";
|
|
6
|
+
const image = "image/";
|
|
7
|
+
const msft365 = "application/vnd.openxmlformats-officedocument.";
|
|
4
8
|
const mime = {
|
|
5
|
-
html:
|
|
6
|
-
htm:
|
|
7
|
-
css:
|
|
8
|
-
js:
|
|
9
|
-
ts:
|
|
10
|
-
json:
|
|
11
|
-
xml:
|
|
12
|
-
jpg:
|
|
13
|
-
jpeg:
|
|
14
|
-
png:
|
|
15
|
-
gif:
|
|
16
|
-
webp:
|
|
17
|
-
avif:
|
|
18
|
-
svg:
|
|
19
|
-
ico:
|
|
9
|
+
html: `${text}html`,
|
|
10
|
+
htm: `${text}html`,
|
|
11
|
+
css: `${text}css`,
|
|
12
|
+
js: `${application}javascript`,
|
|
13
|
+
ts: `${application}typescript`,
|
|
14
|
+
json: `${application}json`,
|
|
15
|
+
xml: `${application}xml`,
|
|
16
|
+
jpg: `${image}jpeg`,
|
|
17
|
+
jpeg: `${image}jpeg`,
|
|
18
|
+
png: `${image}png`,
|
|
19
|
+
gif: `${image}gif`,
|
|
20
|
+
webp: `${image}webp`,
|
|
21
|
+
avif: `${image}avif`,
|
|
22
|
+
svg: `${image}svg+xml`,
|
|
23
|
+
ico: `${image}x-icon`,
|
|
20
24
|
mp4: "video/mp4",
|
|
21
25
|
webm: "video/webm",
|
|
22
26
|
mp3: "audio/mpeg",
|
|
@@ -25,26 +29,28 @@ const mime = {
|
|
|
25
29
|
woff2: "font/woff2",
|
|
26
30
|
ttf: "font/ttf",
|
|
27
31
|
otf: "font/otf",
|
|
28
|
-
pdf:
|
|
29
|
-
txt:
|
|
30
|
-
csv:
|
|
31
|
-
zip:
|
|
32
|
-
doc:
|
|
33
|
-
docx:
|
|
34
|
-
xls:
|
|
35
|
-
xlsx:
|
|
36
|
-
ppt:
|
|
37
|
-
pptx:
|
|
32
|
+
pdf: `${application}pdf`,
|
|
33
|
+
txt: `${text}plain`,
|
|
34
|
+
csv: `${text}csv`,
|
|
35
|
+
zip: `${application}zip`,
|
|
36
|
+
doc: `${application}msword`,
|
|
37
|
+
docx: `${msft365}wordprocessingml.document`,
|
|
38
|
+
xls: `${application}vnd.ms-excel`,
|
|
39
|
+
xlsx: `${msft365}spreadsheetml.sheet`,
|
|
40
|
+
ppt: `${application}vnd.ms-powerpoint`,
|
|
41
|
+
pptx: `${msft365}presentationml.presentation`
|
|
38
42
|
};
|
|
39
|
-
|
|
43
|
+
function getFileExtension(path) {
|
|
40
44
|
const index = path.lastIndexOf(".");
|
|
41
45
|
if (index === -1) return "";
|
|
42
|
-
return path.slice(index + 1);
|
|
43
|
-
}
|
|
46
|
+
return path.slice(index + 1).toLowerCase();
|
|
47
|
+
}
|
|
44
48
|
const file = (path) => new ElysiaFile(path);
|
|
45
49
|
let createReadStream;
|
|
46
50
|
let stat;
|
|
47
|
-
|
|
51
|
+
function warnMissing(name) {
|
|
52
|
+
console.warn(/* @__PURE__ */ new Error(`[Elysia] \`file\` require \`fs${name ? "." + name : ""}\` ${name?.includes(".") ? "module " : ""}which is not available in this environment`));
|
|
53
|
+
}
|
|
48
54
|
var ElysiaFile = class {
|
|
49
55
|
constructor(path) {
|
|
50
56
|
this.path = path;
|
package/dist/utils.d.ts
CHANGED
|
@@ -50,6 +50,12 @@ type ChainNode = {
|
|
|
50
50
|
combine: ChainNode;
|
|
51
51
|
over: ChainNode | undefined;
|
|
52
52
|
};
|
|
53
|
+
/**
|
|
54
|
+
* Walk the chain tail-first into a fresh `Partial<AppHook>`
|
|
55
|
+
*
|
|
56
|
+
* Explicit-stack walk (no recursion) so it works uniformly for linear
|
|
57
|
+
* chains and combine nodes without risking stack overflow on deep chains
|
|
58
|
+
*/
|
|
53
59
|
declare function flattenChain(start: ChainNode | undefined, keep?: (s: EventScope | undefined) => boolean, stopAt?: ChainNode): Partial<AppHook> | undefined;
|
|
54
60
|
declare function flattenChainMemo(root: object, start: ChainNode | undefined): Partial<AppHook> | undefined;
|
|
55
61
|
/**
|
|
@@ -130,5 +136,10 @@ declare function joinPath(base: string, path: string): string;
|
|
|
130
136
|
declare function pushField<K extends keyof any>(target: Record<K, unknown>, key: K, item: unknown, defaultArray?: boolean): void;
|
|
131
137
|
declare const requestId: any;
|
|
132
138
|
declare function replaceUrlPath(url: string, path: string): string;
|
|
139
|
+
declare function clonePlainDecorators<T extends Record<string, unknown>>(source: T, seen?: WeakMap<object, any>): T;
|
|
140
|
+
declare function prefix<T extends string, Models extends Record<string, AnySchema>>(prefix: T, models: Models): { [k in keyof Models as `${T}.${k & string}`]: Models[k] };
|
|
141
|
+
declare namespace prefix {
|
|
142
|
+
var capitalize: <T extends string, Models extends Record<string, AnySchema>>(prefix: T, models: Models) => { [k in keyof Models as `${T}.${Capitalize<k & string>}`]: Models[k] };
|
|
143
|
+
}
|
|
133
144
|
//#endregion
|
|
134
|
-
export { ChainNode, cloneHook, coalesceStandaloneSchemas, constantTimeEqual, createErrorEventHandler, dedupedMergeArray, eventProperties, flattenChain, flattenChainMemo, fnOrigin, fnv1a, form, formToFormData, getLoosePath, hookToGuard, isBlob, isDownwardScope, isEmpty, isLocalScope, isNotEmpty, isRecordNumber, joinPath, mapMethodBack, mergeArray, mergeDeep, mergeHook, mergeResponse, nullObject, pushField, redirect, replaceUrlPath, requestId, schemaProperties, sse };
|
|
145
|
+
export { ChainNode, cloneHook, clonePlainDecorators, coalesceStandaloneSchemas, constantTimeEqual, createErrorEventHandler, dedupedMergeArray, eventProperties, flattenChain, flattenChainMemo, fnOrigin, fnv1a, form, formToFormData, getLoosePath, hookToGuard, isBlob, isDownwardScope, isEmpty, isLocalScope, isNotEmpty, isRecordNumber, joinPath, mapMethodBack, mergeArray, mergeDeep, mergeHook, mergeResponse, nullObject, prefix, pushField, redirect, replaceUrlPath, requestId, schemaProperties, sse };
|
package/dist/utils.js
CHANGED
|
@@ -38,54 +38,50 @@ function fnv1a(str) {
|
|
|
38
38
|
const fnOrigin = /* @__PURE__ */ new WeakMap();
|
|
39
39
|
const isDownwardScope = (s) => s === "plugin" || s === "global";
|
|
40
40
|
const isLocalScope = (s) => s === "local" || s === void 0;
|
|
41
|
-
const
|
|
41
|
+
const flattenNodeStack = [];
|
|
42
|
+
const flattenPhaseStack = [];
|
|
43
|
+
/**
|
|
44
|
+
* Walk the chain tail-first into a fresh `Partial<AppHook>`
|
|
45
|
+
*
|
|
46
|
+
* Explicit-stack walk (no recursion) so it works uniformly for linear
|
|
47
|
+
* chains and combine nodes without risking stack overflow on deep chains
|
|
48
|
+
*/
|
|
42
49
|
function flattenChain(start, keep, stopAt) {
|
|
43
50
|
if (!start || start === stopAt) return;
|
|
44
51
|
const result = nullObject();
|
|
45
|
-
const
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
while (
|
|
52
|
-
const
|
|
53
|
-
if (
|
|
54
|
-
appendInto(result,
|
|
52
|
+
const nodes = flattenNodeStack;
|
|
53
|
+
const phases = flattenPhaseStack;
|
|
54
|
+
nodes.length = 0;
|
|
55
|
+
phases.length = 0;
|
|
56
|
+
nodes.push(start);
|
|
57
|
+
phases.push(0);
|
|
58
|
+
while (nodes.length) {
|
|
59
|
+
const node = nodes.pop();
|
|
60
|
+
if (phases.pop() === 1) {
|
|
61
|
+
appendInto(result, node.added, keep, node.scope);
|
|
55
62
|
continue;
|
|
56
63
|
}
|
|
57
|
-
const node = task.node;
|
|
58
64
|
if (stopAt && node === stopAt) continue;
|
|
59
65
|
if ("combine" in node) {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
node: node.over
|
|
67
|
-
});
|
|
66
|
+
nodes.push(node.combine);
|
|
67
|
+
phases.push(0);
|
|
68
|
+
if (node.over) {
|
|
69
|
+
nodes.push(node.over);
|
|
70
|
+
phases.push(0);
|
|
71
|
+
}
|
|
68
72
|
} else {
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
kind: 0,
|
|
76
|
-
node: node.parent
|
|
77
|
-
});
|
|
73
|
+
nodes.push(node);
|
|
74
|
+
phases.push(1);
|
|
75
|
+
if (node.parent && node.parent !== stopAt) {
|
|
76
|
+
nodes.push(node.parent);
|
|
77
|
+
phases.push(0);
|
|
78
|
+
}
|
|
78
79
|
}
|
|
79
80
|
}
|
|
80
81
|
if (isNotEmpty(result)) return result;
|
|
81
82
|
}
|
|
82
83
|
const flattenChainMemos = /* @__PURE__ */ new WeakMap();
|
|
83
84
|
const emptyFlatten = Object.freeze(nullObject());
|
|
84
|
-
function cloneFlatHook(src) {
|
|
85
|
-
const out = Object.assign(nullObject(), src);
|
|
86
|
-
for (const key in out) if (Array.isArray(out[key])) out[key] = out[key].slice();
|
|
87
|
-
return out;
|
|
88
|
-
}
|
|
89
85
|
function flattenChainMemo(root, start) {
|
|
90
86
|
if (!start) return;
|
|
91
87
|
let perRoot = flattenChainMemos.get(root);
|
|
@@ -99,7 +95,7 @@ function flattenChainMemo(root, start) {
|
|
|
99
95
|
perRoot.set(start, cached);
|
|
100
96
|
}
|
|
101
97
|
if (cached === emptyFlatten) return;
|
|
102
|
-
return
|
|
98
|
+
return cloneHook(cached);
|
|
103
99
|
}
|
|
104
100
|
function appendInto(target, src, keep, nodeScope) {
|
|
105
101
|
if (keep && !keep(nodeScope)) return;
|
|
@@ -107,10 +103,13 @@ function appendInto(target, src, keep, nodeScope) {
|
|
|
107
103
|
const v = src[key];
|
|
108
104
|
if (v === void 0 || v === null) continue;
|
|
109
105
|
if (eventProperties.has(key) || key === "schemas") {
|
|
110
|
-
const arr = Array.isArray(v) ? v : [v];
|
|
111
106
|
const existing = target[key];
|
|
112
|
-
if (
|
|
113
|
-
|
|
107
|
+
if (Array.isArray(v)) if (existing) {
|
|
108
|
+
const arr = existing;
|
|
109
|
+
for (let i = 0; i < v.length; i++) arr.push(v[i]);
|
|
110
|
+
} else target[key] = v.slice();
|
|
111
|
+
else if (existing) existing.push(v);
|
|
112
|
+
else target[key] = [v];
|
|
114
113
|
} else target[key] = v;
|
|
115
114
|
}
|
|
116
115
|
}
|
|
@@ -223,18 +222,28 @@ function mergeArray(a, b, reverse = false) {
|
|
|
223
222
|
a.unshift(b);
|
|
224
223
|
return a;
|
|
225
224
|
}
|
|
226
|
-
if (bIsArray)
|
|
225
|
+
if (bIsArray) {
|
|
226
|
+
const out = new Array(b.length + 1);
|
|
227
|
+
for (let i = 0; i < b.length; i++) out[i] = b[i];
|
|
228
|
+
out[b.length] = a;
|
|
229
|
+
return out;
|
|
230
|
+
}
|
|
227
231
|
return [b, a];
|
|
228
232
|
}
|
|
229
233
|
if (aIsArray && bIsArray) {
|
|
230
|
-
a.push(
|
|
234
|
+
for (let i = 0; i < b.length; i++) a.push(b[i]);
|
|
231
235
|
return a;
|
|
232
236
|
}
|
|
233
237
|
if (aIsArray) {
|
|
234
238
|
a.push(b);
|
|
235
239
|
return a;
|
|
236
240
|
}
|
|
237
|
-
if (bIsArray)
|
|
241
|
+
if (bIsArray) {
|
|
242
|
+
const out = new Array(b.length + 1);
|
|
243
|
+
out[0] = a;
|
|
244
|
+
for (let i = 0; i < b.length; i++) out[i + 1] = b[i];
|
|
245
|
+
return out;
|
|
246
|
+
}
|
|
238
247
|
return [a, b];
|
|
239
248
|
}
|
|
240
249
|
/**
|
|
@@ -365,10 +374,13 @@ const isClass = (v) => typeof v === "function" && isClassRegex.test(v.toString()
|
|
|
365
374
|
function mergeDeep(target, source, skipKeys, override = true, mergeArray = false, seen) {
|
|
366
375
|
if (!isObject(target) || !isObject(source)) return target;
|
|
367
376
|
if (seen?.has(source)) return target;
|
|
368
|
-
for (const
|
|
377
|
+
for (const key in source) {
|
|
378
|
+
if (!Object.hasOwn(source, key)) continue;
|
|
379
|
+
const value = source[key];
|
|
369
380
|
if (skipKeys?.includes(key) || require_constants.dangerousKeys.has(key)) continue;
|
|
370
381
|
if (mergeArray && Array.isArray(value)) {
|
|
371
|
-
|
|
382
|
+
const existing = target[key];
|
|
383
|
+
target[key] = Array.isArray(existing) ? existing.concat(value) : value;
|
|
372
384
|
continue;
|
|
373
385
|
}
|
|
374
386
|
if (!isObject(value) || !(key in target) || isClass(value)) {
|
|
@@ -390,8 +402,11 @@ function mergeDeep(target, source, skipKeys, override = true, mergeArray = false
|
|
|
390
402
|
}
|
|
391
403
|
const isBlob = (value) => value instanceof Blob || value instanceof require_universal_file.ElysiaFile;
|
|
392
404
|
function cloneHook(src) {
|
|
393
|
-
const out =
|
|
394
|
-
for (const key in
|
|
405
|
+
const out = nullObject();
|
|
406
|
+
for (const key in src) {
|
|
407
|
+
const value = src[key];
|
|
408
|
+
out[key] = Array.isArray(value) ? value.slice() : value;
|
|
409
|
+
}
|
|
395
410
|
return out;
|
|
396
411
|
}
|
|
397
412
|
function joinPath(base, path) {
|
|
@@ -414,9 +429,37 @@ function replaceUrlPath(url, path) {
|
|
|
414
429
|
const qs = url.indexOf("?", i);
|
|
415
430
|
return `${url.slice(0, i)}${path.charCodeAt(0) === 47 ? "" : "/"}${path}${qs === -1 ? "" : url.slice(qs)}`;
|
|
416
431
|
}
|
|
432
|
+
function isPlainObject(v) {
|
|
433
|
+
if (!v || typeof v !== "object" || Array.isArray(v)) return false;
|
|
434
|
+
const proto = Object.getPrototypeOf(v);
|
|
435
|
+
return proto === Object.prototype || proto === null;
|
|
436
|
+
}
|
|
437
|
+
function clonePlainDecorators(source, seen = /* @__PURE__ */ new WeakMap()) {
|
|
438
|
+
const existing = seen.get(source);
|
|
439
|
+
if (existing) return existing;
|
|
440
|
+
const out = nullObject();
|
|
441
|
+
seen.set(source, out);
|
|
442
|
+
for (const key in source) {
|
|
443
|
+
const value = source[key];
|
|
444
|
+
out[key] = isPlainObject(value) ? clonePlainDecorators(value, seen) : value;
|
|
445
|
+
}
|
|
446
|
+
return out;
|
|
447
|
+
}
|
|
448
|
+
const capitalize = (s) => s.charAt(0).toUpperCase() + s.slice(1);
|
|
449
|
+
function prefix(prefix, models) {
|
|
450
|
+
const prefixed = nullObject();
|
|
451
|
+
for (const key in models) prefixed[`${prefix}.${key}`] = models[key];
|
|
452
|
+
return prefixed;
|
|
453
|
+
}
|
|
454
|
+
prefix.capitalize = function prefixModelsCapitalize(prefix, models) {
|
|
455
|
+
const prefixed = nullObject();
|
|
456
|
+
for (const key in models) prefixed[`${prefix}.${capitalize(key)}`] = models[key];
|
|
457
|
+
return prefixed;
|
|
458
|
+
};
|
|
417
459
|
|
|
418
460
|
//#endregion
|
|
419
461
|
exports.cloneHook = cloneHook;
|
|
462
|
+
exports.clonePlainDecorators = clonePlainDecorators;
|
|
420
463
|
exports.coalesceStandaloneSchemas = coalesceStandaloneSchemas;
|
|
421
464
|
exports.constantTimeEqual = constantTimeEqual;
|
|
422
465
|
exports.createErrorEventHandler = createErrorEventHandler;
|
|
@@ -443,6 +486,7 @@ exports.mergeDeep = mergeDeep;
|
|
|
443
486
|
exports.mergeHook = mergeHook;
|
|
444
487
|
exports.mergeResponse = mergeResponse;
|
|
445
488
|
exports.nullObject = nullObject;
|
|
489
|
+
exports.prefix = prefix;
|
|
446
490
|
exports.pushField = pushField;
|
|
447
491
|
exports.redirect = redirect;
|
|
448
492
|
exports.replaceUrlPath = replaceUrlPath;
|
package/dist/utils.mjs
CHANGED
|
@@ -37,54 +37,50 @@ function fnv1a(str) {
|
|
|
37
37
|
const fnOrigin = /* @__PURE__ */ new WeakMap();
|
|
38
38
|
const isDownwardScope = (s) => s === "plugin" || s === "global";
|
|
39
39
|
const isLocalScope = (s) => s === "local" || s === void 0;
|
|
40
|
-
const
|
|
40
|
+
const flattenNodeStack = [];
|
|
41
|
+
const flattenPhaseStack = [];
|
|
42
|
+
/**
|
|
43
|
+
* Walk the chain tail-first into a fresh `Partial<AppHook>`
|
|
44
|
+
*
|
|
45
|
+
* Explicit-stack walk (no recursion) so it works uniformly for linear
|
|
46
|
+
* chains and combine nodes without risking stack overflow on deep chains
|
|
47
|
+
*/
|
|
41
48
|
function flattenChain(start, keep, stopAt) {
|
|
42
49
|
if (!start || start === stopAt) return;
|
|
43
50
|
const result = nullObject();
|
|
44
|
-
const
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
while (
|
|
51
|
-
const
|
|
52
|
-
if (
|
|
53
|
-
appendInto(result,
|
|
51
|
+
const nodes = flattenNodeStack;
|
|
52
|
+
const phases = flattenPhaseStack;
|
|
53
|
+
nodes.length = 0;
|
|
54
|
+
phases.length = 0;
|
|
55
|
+
nodes.push(start);
|
|
56
|
+
phases.push(0);
|
|
57
|
+
while (nodes.length) {
|
|
58
|
+
const node = nodes.pop();
|
|
59
|
+
if (phases.pop() === 1) {
|
|
60
|
+
appendInto(result, node.added, keep, node.scope);
|
|
54
61
|
continue;
|
|
55
62
|
}
|
|
56
|
-
const node = task.node;
|
|
57
63
|
if (stopAt && node === stopAt) continue;
|
|
58
64
|
if ("combine" in node) {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
node: node.over
|
|
66
|
-
});
|
|
65
|
+
nodes.push(node.combine);
|
|
66
|
+
phases.push(0);
|
|
67
|
+
if (node.over) {
|
|
68
|
+
nodes.push(node.over);
|
|
69
|
+
phases.push(0);
|
|
70
|
+
}
|
|
67
71
|
} else {
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
kind: 0,
|
|
75
|
-
node: node.parent
|
|
76
|
-
});
|
|
72
|
+
nodes.push(node);
|
|
73
|
+
phases.push(1);
|
|
74
|
+
if (node.parent && node.parent !== stopAt) {
|
|
75
|
+
nodes.push(node.parent);
|
|
76
|
+
phases.push(0);
|
|
77
|
+
}
|
|
77
78
|
}
|
|
78
79
|
}
|
|
79
80
|
if (isNotEmpty(result)) return result;
|
|
80
81
|
}
|
|
81
82
|
const flattenChainMemos = /* @__PURE__ */ new WeakMap();
|
|
82
83
|
const emptyFlatten = Object.freeze(nullObject());
|
|
83
|
-
function cloneFlatHook(src) {
|
|
84
|
-
const out = Object.assign(nullObject(), src);
|
|
85
|
-
for (const key in out) if (Array.isArray(out[key])) out[key] = out[key].slice();
|
|
86
|
-
return out;
|
|
87
|
-
}
|
|
88
84
|
function flattenChainMemo(root, start) {
|
|
89
85
|
if (!start) return;
|
|
90
86
|
let perRoot = flattenChainMemos.get(root);
|
|
@@ -98,7 +94,7 @@ function flattenChainMemo(root, start) {
|
|
|
98
94
|
perRoot.set(start, cached);
|
|
99
95
|
}
|
|
100
96
|
if (cached === emptyFlatten) return;
|
|
101
|
-
return
|
|
97
|
+
return cloneHook(cached);
|
|
102
98
|
}
|
|
103
99
|
function appendInto(target, src, keep, nodeScope) {
|
|
104
100
|
if (keep && !keep(nodeScope)) return;
|
|
@@ -106,10 +102,13 @@ function appendInto(target, src, keep, nodeScope) {
|
|
|
106
102
|
const v = src[key];
|
|
107
103
|
if (v === void 0 || v === null) continue;
|
|
108
104
|
if (eventProperties.has(key) || key === "schemas") {
|
|
109
|
-
const arr = Array.isArray(v) ? v : [v];
|
|
110
105
|
const existing = target[key];
|
|
111
|
-
if (
|
|
112
|
-
|
|
106
|
+
if (Array.isArray(v)) if (existing) {
|
|
107
|
+
const arr = existing;
|
|
108
|
+
for (let i = 0; i < v.length; i++) arr.push(v[i]);
|
|
109
|
+
} else target[key] = v.slice();
|
|
110
|
+
else if (existing) existing.push(v);
|
|
111
|
+
else target[key] = [v];
|
|
113
112
|
} else target[key] = v;
|
|
114
113
|
}
|
|
115
114
|
}
|
|
@@ -222,18 +221,28 @@ function mergeArray(a, b, reverse = false) {
|
|
|
222
221
|
a.unshift(b);
|
|
223
222
|
return a;
|
|
224
223
|
}
|
|
225
|
-
if (bIsArray)
|
|
224
|
+
if (bIsArray) {
|
|
225
|
+
const out = new Array(b.length + 1);
|
|
226
|
+
for (let i = 0; i < b.length; i++) out[i] = b[i];
|
|
227
|
+
out[b.length] = a;
|
|
228
|
+
return out;
|
|
229
|
+
}
|
|
226
230
|
return [b, a];
|
|
227
231
|
}
|
|
228
232
|
if (aIsArray && bIsArray) {
|
|
229
|
-
a.push(
|
|
233
|
+
for (let i = 0; i < b.length; i++) a.push(b[i]);
|
|
230
234
|
return a;
|
|
231
235
|
}
|
|
232
236
|
if (aIsArray) {
|
|
233
237
|
a.push(b);
|
|
234
238
|
return a;
|
|
235
239
|
}
|
|
236
|
-
if (bIsArray)
|
|
240
|
+
if (bIsArray) {
|
|
241
|
+
const out = new Array(b.length + 1);
|
|
242
|
+
out[0] = a;
|
|
243
|
+
for (let i = 0; i < b.length; i++) out[i + 1] = b[i];
|
|
244
|
+
return out;
|
|
245
|
+
}
|
|
237
246
|
return [a, b];
|
|
238
247
|
}
|
|
239
248
|
/**
|
|
@@ -364,10 +373,13 @@ const isClass = (v) => typeof v === "function" && isClassRegex.test(v.toString()
|
|
|
364
373
|
function mergeDeep(target, source, skipKeys, override = true, mergeArray = false, seen) {
|
|
365
374
|
if (!isObject(target) || !isObject(source)) return target;
|
|
366
375
|
if (seen?.has(source)) return target;
|
|
367
|
-
for (const
|
|
376
|
+
for (const key in source) {
|
|
377
|
+
if (!Object.hasOwn(source, key)) continue;
|
|
378
|
+
const value = source[key];
|
|
368
379
|
if (skipKeys?.includes(key) || dangerousKeys.has(key)) continue;
|
|
369
380
|
if (mergeArray && Array.isArray(value)) {
|
|
370
|
-
|
|
381
|
+
const existing = target[key];
|
|
382
|
+
target[key] = Array.isArray(existing) ? existing.concat(value) : value;
|
|
371
383
|
continue;
|
|
372
384
|
}
|
|
373
385
|
if (!isObject(value) || !(key in target) || isClass(value)) {
|
|
@@ -389,8 +401,11 @@ function mergeDeep(target, source, skipKeys, override = true, mergeArray = false
|
|
|
389
401
|
}
|
|
390
402
|
const isBlob = (value) => value instanceof Blob || value instanceof ElysiaFile;
|
|
391
403
|
function cloneHook(src) {
|
|
392
|
-
const out =
|
|
393
|
-
for (const key in
|
|
404
|
+
const out = nullObject();
|
|
405
|
+
for (const key in src) {
|
|
406
|
+
const value = src[key];
|
|
407
|
+
out[key] = Array.isArray(value) ? value.slice() : value;
|
|
408
|
+
}
|
|
394
409
|
return out;
|
|
395
410
|
}
|
|
396
411
|
function joinPath(base, path) {
|
|
@@ -413,6 +428,33 @@ function replaceUrlPath(url, path) {
|
|
|
413
428
|
const qs = url.indexOf("?", i);
|
|
414
429
|
return `${url.slice(0, i)}${path.charCodeAt(0) === 47 ? "" : "/"}${path}${qs === -1 ? "" : url.slice(qs)}`;
|
|
415
430
|
}
|
|
431
|
+
function isPlainObject(v) {
|
|
432
|
+
if (!v || typeof v !== "object" || Array.isArray(v)) return false;
|
|
433
|
+
const proto = Object.getPrototypeOf(v);
|
|
434
|
+
return proto === Object.prototype || proto === null;
|
|
435
|
+
}
|
|
436
|
+
function clonePlainDecorators(source, seen = /* @__PURE__ */ new WeakMap()) {
|
|
437
|
+
const existing = seen.get(source);
|
|
438
|
+
if (existing) return existing;
|
|
439
|
+
const out = nullObject();
|
|
440
|
+
seen.set(source, out);
|
|
441
|
+
for (const key in source) {
|
|
442
|
+
const value = source[key];
|
|
443
|
+
out[key] = isPlainObject(value) ? clonePlainDecorators(value, seen) : value;
|
|
444
|
+
}
|
|
445
|
+
return out;
|
|
446
|
+
}
|
|
447
|
+
const capitalize = (s) => s.charAt(0).toUpperCase() + s.slice(1);
|
|
448
|
+
function prefix(prefix, models) {
|
|
449
|
+
const prefixed = nullObject();
|
|
450
|
+
for (const key in models) prefixed[`${prefix}.${key}`] = models[key];
|
|
451
|
+
return prefixed;
|
|
452
|
+
}
|
|
453
|
+
prefix.capitalize = function prefixModelsCapitalize(prefix, models) {
|
|
454
|
+
const prefixed = nullObject();
|
|
455
|
+
for (const key in models) prefixed[`${prefix}.${capitalize(key)}`] = models[key];
|
|
456
|
+
return prefixed;
|
|
457
|
+
};
|
|
416
458
|
|
|
417
459
|
//#endregion
|
|
418
|
-
export { cloneHook, coalesceStandaloneSchemas, constantTimeEqual, createErrorEventHandler, dedupedMergeArray, eventProperties, flattenChain, flattenChainMemo, fnOrigin, fnv1a, form, formToFormData, getLoosePath, hookToGuard, isBlob, isDownwardScope, isEmpty, isLocalScope, isNotEmpty, isRecordNumber, joinPath, mapMethodBack, mergeArray, mergeDeep, mergeHook, mergeResponse, nullObject, pushField, redirect, replaceUrlPath, requestId, schemaProperties, sse };
|
|
460
|
+
export { cloneHook, clonePlainDecorators, coalesceStandaloneSchemas, constantTimeEqual, createErrorEventHandler, dedupedMergeArray, eventProperties, flattenChain, flattenChainMemo, fnOrigin, fnv1a, form, formToFormData, getLoosePath, hookToGuard, isBlob, isDownwardScope, isEmpty, isLocalScope, isNotEmpty, isRecordNumber, joinPath, mapMethodBack, mergeArray, mergeDeep, mergeHook, mergeResponse, nullObject, prefix, pushField, redirect, replaceUrlPath, requestId, schemaProperties, sse };
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { ElysiaConfig, MaybePromise } from "../types.js";
|
|
2
2
|
import { AnySchema, StandardSchemaV1Like } from "../type/types.js";
|
|
3
|
-
import { ValidatorSlot } from "../compile/aot.js";
|
|
4
3
|
import { CoerceOption } from "../type/coerce.js";
|
|
4
|
+
import { ValidatorSlot } from "../compile/aot.js";
|
|
5
5
|
import { TypeBoxValidator } from "../type/bridge.js";
|
|
6
6
|
import { TSchema } from "typebox/type";
|
|
7
7
|
import { TLocalizedValidationError } from "typebox/error";
|
package/dist/validator/index.js
CHANGED
|
@@ -29,6 +29,18 @@ var Validator = class Validator {
|
|
|
29
29
|
};
|
|
30
30
|
}
|
|
31
31
|
const schema = Validator.reference(name, options?.models);
|
|
32
|
+
if (options?.schemas?.some((v) => typeof v === "string")) {
|
|
33
|
+
const models = options.models;
|
|
34
|
+
options = {
|
|
35
|
+
...options,
|
|
36
|
+
schemas: options.schemas.map((v) => Validator.reference(v, models))
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
if (isCompiledSchema(schema)) {
|
|
40
|
+
const message = "[Elysia] Compiled schema detected. Please pass t.Schema instead.";
|
|
41
|
+
if (require_compile_aot.Capture.isCapturing()) throw new Error(`${message} build plugin cannot serialize a pre-compiled schema.`);
|
|
42
|
+
console.warn(message);
|
|
43
|
+
}
|
|
32
44
|
let isIntersectable = false;
|
|
33
45
|
if (options?.schemas?.length) if ("~kind" in schema && options.schemas.every((v) => "~kind" in v || "~elyAcl" in v)) isIntersectable = true;
|
|
34
46
|
else return new MultiValidator(schema, options);
|
|
@@ -76,6 +88,7 @@ var Validator = class Validator {
|
|
|
76
88
|
tbCache?.clear();
|
|
77
89
|
}
|
|
78
90
|
};
|
|
91
|
+
const isCompiledSchema = (schema) => schema != null && typeof schema.Check === "function" && "buildResult" in schema;
|
|
79
92
|
const isSingleSchema = (schema) => "~kind" in schema || "~elyAcl" in schema || "~standard" in schema;
|
|
80
93
|
const toStatusBased = (schema) => isSingleSchema(schema) ? { 200: schema } : schema;
|
|
81
94
|
var StandardValidator = class extends Validator {
|
|
@@ -159,7 +172,7 @@ var MultiValidator = class extends Validator {
|
|
|
159
172
|
let snapshot;
|
|
160
173
|
for (let i = 0; i < this.schemas.length; i++) {
|
|
161
174
|
const validator = this.schemas[i];
|
|
162
|
-
const result = "~standard" in validator ? validator["~standard"].validate(value).value : require_type_bridge.Decode(validator, value);
|
|
175
|
+
const result = "~standard" in validator ? validator["~standard"].validate(value).value : require_type_bridge.Decode(validator.Type(), value);
|
|
163
176
|
if (snapshot === void 0) snapshot = result;
|
|
164
177
|
else if (Array.isArray(snapshot) && Array.isArray(result)) snapshot.push(...result);
|
|
165
178
|
else if (typeof snapshot === "object" && typeof result === "object") snapshot = Object.assign(snapshot, result);
|
|
@@ -186,7 +199,7 @@ var MultiValidator = class extends Validator {
|
|
|
186
199
|
throw new require_error.ValidationError(type, value, validator.Errors(value));
|
|
187
200
|
}
|
|
188
201
|
else if (!validator.Check(value)) throw new require_error.ValidationError(type, value, validator.Errors(value));
|
|
189
|
-
result = require_type_bridge.Decode(validator, value);
|
|
202
|
+
result = require_type_bridge.Decode(validator.Type(), value);
|
|
190
203
|
}
|
|
191
204
|
if (snapshot === void 0) snapshot = result;
|
|
192
205
|
else if (Array.isArray(snapshot) && Array.isArray(result)) snapshot.push(...result);
|
package/dist/validator/index.mjs
CHANGED
|
@@ -28,6 +28,18 @@ var Validator = class Validator {
|
|
|
28
28
|
};
|
|
29
29
|
}
|
|
30
30
|
const schema = Validator.reference(name, options?.models);
|
|
31
|
+
if (options?.schemas?.some((v) => typeof v === "string")) {
|
|
32
|
+
const models = options.models;
|
|
33
|
+
options = {
|
|
34
|
+
...options,
|
|
35
|
+
schemas: options.schemas.map((v) => Validator.reference(v, models))
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
if (isCompiledSchema(schema)) {
|
|
39
|
+
const message = "[Elysia] Compiled schema detected. Please pass t.Schema instead.";
|
|
40
|
+
if (Capture.isCapturing()) throw new Error(`${message} build plugin cannot serialize a pre-compiled schema.`);
|
|
41
|
+
console.warn(message);
|
|
42
|
+
}
|
|
31
43
|
let isIntersectable = false;
|
|
32
44
|
if (options?.schemas?.length) if ("~kind" in schema && options.schemas.every((v) => "~kind" in v || "~elyAcl" in v)) isIntersectable = true;
|
|
33
45
|
else return new MultiValidator(schema, options);
|
|
@@ -75,6 +87,7 @@ var Validator = class Validator {
|
|
|
75
87
|
tbCache?.clear();
|
|
76
88
|
}
|
|
77
89
|
};
|
|
90
|
+
const isCompiledSchema = (schema) => schema != null && typeof schema.Check === "function" && "buildResult" in schema;
|
|
78
91
|
const isSingleSchema = (schema) => "~kind" in schema || "~elyAcl" in schema || "~standard" in schema;
|
|
79
92
|
const toStatusBased = (schema) => isSingleSchema(schema) ? { 200: schema } : schema;
|
|
80
93
|
var StandardValidator = class extends Validator {
|
|
@@ -158,7 +171,7 @@ var MultiValidator = class extends Validator {
|
|
|
158
171
|
let snapshot;
|
|
159
172
|
for (let i = 0; i < this.schemas.length; i++) {
|
|
160
173
|
const validator = this.schemas[i];
|
|
161
|
-
const result = "~standard" in validator ? validator["~standard"].validate(value).value : Decode(validator, value);
|
|
174
|
+
const result = "~standard" in validator ? validator["~standard"].validate(value).value : Decode(validator.Type(), value);
|
|
162
175
|
if (snapshot === void 0) snapshot = result;
|
|
163
176
|
else if (Array.isArray(snapshot) && Array.isArray(result)) snapshot.push(...result);
|
|
164
177
|
else if (typeof snapshot === "object" && typeof result === "object") snapshot = Object.assign(snapshot, result);
|
|
@@ -185,7 +198,7 @@ var MultiValidator = class extends Validator {
|
|
|
185
198
|
throw new ValidationError(type, value, validator.Errors(value));
|
|
186
199
|
}
|
|
187
200
|
else if (!validator.Check(value)) throw new ValidationError(type, value, validator.Errors(value));
|
|
188
|
-
result = Decode(validator, value);
|
|
201
|
+
result = Decode(validator.Type(), value);
|
|
189
202
|
}
|
|
190
203
|
if (snapshot === void 0) snapshot = result;
|
|
191
204
|
else if (Array.isArray(snapshot) && Array.isArray(result)) snapshot.push(...result);
|