elysia 2.0.0-beta.12 → 2.0.0-beta.13
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.js +28 -17
- package/dist/adapter/bun/index.mjs +28 -17
- package/dist/adapter/utils.js +1 -0
- package/dist/adapter/utils.mjs +1 -0
- package/dist/adapter/web-standard/handler.js +2 -2
- package/dist/adapter/web-standard/handler.mjs +2 -2
- package/dist/compile/aot-emit.js +1 -1
- package/dist/compile/aot-emit.mjs +1 -1
- package/dist/compile/aot.d.ts +1 -1
- package/dist/compile/aot.js +2 -2
- package/dist/compile/aot.mjs +2 -2
- package/dist/compile/handler/descriptor.d.ts +2 -3
- package/dist/compile/handler/descriptor.js +6 -16
- package/dist/compile/handler/descriptor.mjs +7 -16
- package/dist/compile/handler/index.d.ts +1 -1
- package/dist/compile/handler/index.js +15 -6
- package/dist/compile/handler/index.mjs +15 -6
- package/dist/compile/handler/jit.js +71 -66
- package/dist/compile/handler/jit.mjs +72 -67
- package/dist/compile/handler/params.js +3 -2
- package/dist/compile/handler/params.mjs +4 -3
- 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 +7 -6
- package/dist/compile/handler/utils.js +23 -18
- package/dist/compile/handler/utils.mjs +23 -19
- package/dist/compile/utils.js +7 -4
- package/dist/compile/utils.mjs +7 -4
- package/dist/cookie/lib.js +2 -1
- package/dist/cookie/lib.mjs +2 -1
- package/dist/cookie/utils.d.ts +1 -1
- package/dist/cookie/utils.js +9 -23
- package/dist/cookie/utils.mjs +9 -23
- package/dist/handler/error.js +3 -4
- package/dist/handler/error.mjs +3 -4
- package/dist/handler/fetch.js +17 -13
- package/dist/handler/fetch.mjs +17 -13
- package/dist/package.js +1 -1
- package/dist/package.mjs +1 -1
- package/dist/plugin/aot/core.d.ts +2 -0
- package/dist/plugin/aot/core.js +6 -0
- package/dist/plugin/aot/core.mjs +6 -0
- package/dist/plugin/aot/hooks.js +5 -1
- package/dist/plugin/aot/hooks.mjs +5 -1
- package/dist/trace.js +14 -5
- package/dist/trace.mjs +14 -5
- package/dist/type/constants.d.ts +1 -1
- package/dist/types.d.ts +5 -4
- package/dist/utils.d.ts +0 -1
- package/package.json +4 -4
|
@@ -9,45 +9,44 @@ import { parseQueryFromURL } from "../../parse-query.mjs";
|
|
|
9
9
|
import { Capture } from "../aot.mjs";
|
|
10
10
|
import { resolvedTraceOf, traceCapabilityRequired } from "../../generation.mjs";
|
|
11
11
|
import { buildCookieJar, parseCookieRaw, parseCookieRawDeferred, parseCookieRawLazy, parseCookieRawSigned, parseCookieRawSync, signCookieValues } from "../../cookie/utils.mjs";
|
|
12
|
-
import { isAsyncFunction } from "../utils.mjs";
|
|
13
12
|
import { emptyResponse, finalizeRouteError, forwardError } from "../../handler/utils.mjs";
|
|
14
13
|
import { fallbackResponse } from "../../handler/error.mjs";
|
|
15
|
-
import { armEntryAbort, cloneResponse, cloneStaticValue, getQueryParseChannels, hasRequestBody, mapAfterHandle, mapAfterResponse, mapBeforeHandle, mapError, mapMapResponse, mapTransform, runBeforeHandlePrefix, runBeforeHandlePrefixAsync } from "./utils.mjs";
|
|
14
|
+
import { armEntryAbort, awaitGuard, cloneResponse, cloneStaticValue, getQueryParseChannels, hasRequestBody, mapAfterHandle, mapAfterResponse, mapBeforeHandle, mapError, mapMapResponse, mapTransform, runBeforeHandlePrefix, runBeforeHandlePrefixAsync } from "./utils.mjs";
|
|
16
15
|
import { JITProbe } from "../jit-probe.mjs";
|
|
17
16
|
|
|
18
17
|
//#region src/compile/handler/jit.ts
|
|
19
|
-
const
|
|
18
|
+
const awaitValue = (value, arm = "") => arm ? `await (_av=(${value}),${arm},_av)` : `await ${value}`;
|
|
20
19
|
let captureHeaderShorthand;
|
|
21
20
|
const setCaptureHeaderShorthand = (value) => {
|
|
22
21
|
captureHeaderShorthand = value;
|
|
23
22
|
};
|
|
24
|
-
function builtinParser(adapter, parse, link) {
|
|
23
|
+
function builtinParser(adapter, parse, link, arm) {
|
|
25
24
|
switch (parse) {
|
|
26
25
|
case "formdata":
|
|
27
26
|
case "multipart/form-data":
|
|
28
27
|
link(adapter.formData, "pf");
|
|
29
|
-
return
|
|
28
|
+
return `c.body=${awaitValue("pf(c)", arm)}\n`;
|
|
30
29
|
case "json":
|
|
31
30
|
case "application/json":
|
|
32
31
|
link(adapter.json, "pj");
|
|
33
|
-
return
|
|
32
|
+
return `c.body=${awaitValue("pj(c)", arm)}\n`;
|
|
34
33
|
case "urlencoded":
|
|
35
34
|
case "application/x-www-form-urlencoded":
|
|
36
35
|
link(adapter.urlencoded, "pu");
|
|
37
|
-
return
|
|
36
|
+
return `c.body=${awaitValue("pu(c)", arm)}\n`;
|
|
38
37
|
case "arrayBuffer":
|
|
39
38
|
case "application/octet-stream":
|
|
40
39
|
link(adapter.arrayBuffer, "pa");
|
|
41
|
-
return
|
|
40
|
+
return `c.body=${awaitValue("pa(c)", arm)}\n`;
|
|
42
41
|
case "text":
|
|
43
42
|
case "text/plain":
|
|
44
43
|
link(adapter.text, "pt");
|
|
45
|
-
return
|
|
44
|
+
return `c.body=${awaitValue("pt(c)", arm)}\n`;
|
|
46
45
|
case "none": return "";
|
|
47
46
|
default: throw new Error(`Unsupported content type: ${parse}`);
|
|
48
47
|
}
|
|
49
48
|
}
|
|
50
|
-
function parse(adapter, parsers, bodyVali, hasHeaders, link, report) {
|
|
49
|
+
function parse(adapter, parsers, bodyVali, hasHeaders, link, report, arm) {
|
|
51
50
|
if (parsers && typeof parsers === "function") parsers = [parsers];
|
|
52
51
|
if (typeof parsers === "string" || parsers?.length === 1 && typeof parsers[0] === "string") {
|
|
53
52
|
if (parsers.length === 1) parsers = parsers[0];
|
|
@@ -55,7 +54,7 @@ function parse(adapter, parsers, bodyVali, hasHeaders, link, report) {
|
|
|
55
54
|
const child = report?.resolveChild(builtinName);
|
|
56
55
|
const begin = child ? child.begin : "";
|
|
57
56
|
const end = child ? child.end() : "";
|
|
58
|
-
return begin + builtinParser(adapter, parsers, link) + end;
|
|
57
|
+
return begin + builtinParser(adapter, parsers, link, arm) + end;
|
|
59
58
|
}
|
|
60
59
|
let hasFn = false;
|
|
61
60
|
let hasType = false;
|
|
@@ -76,7 +75,7 @@ if(cti!==-1)ct=ct.slice(0,cti)
|
|
|
76
75
|
const child = report?.resolveChild(parser.name || "anonymous");
|
|
77
76
|
if (i) code += "if(!hasBody){";
|
|
78
77
|
if (child) code += child.begin;
|
|
79
|
-
code +=
|
|
78
|
+
code += `_bp=ho.parse[${i}](c,ct)\n` + awaitGuard(parser, true, "_bp", arm) + `c.body=_bp\n`;
|
|
80
79
|
code += "hasBody=c.body!==undefined\n";
|
|
81
80
|
if (child) code += child.end();
|
|
82
81
|
if (i) code += "}\n";
|
|
@@ -85,7 +84,7 @@ if(cti!==-1)ct=ct.slice(0,cti)
|
|
|
85
84
|
const child = report?.resolveChild(parser);
|
|
86
85
|
if (i) code += "if(!hasBody){\n";
|
|
87
86
|
if (child) code += child.begin;
|
|
88
|
-
code += builtinParser(adapter, parser, link);
|
|
87
|
+
code += builtinParser(adapter, parser, link, arm);
|
|
89
88
|
if (child) code += child.end();
|
|
90
89
|
if (i) code += "}\n";
|
|
91
90
|
break;
|
|
@@ -103,7 +102,8 @@ if(cti!==-1)ct=ct.slice(0,cti)
|
|
|
103
102
|
code += `if(ct&&!(${mediaGuard}))throw new es(415,'Unsupported Media Type')\n`;
|
|
104
103
|
link(ElysiaStatus, "es");
|
|
105
104
|
}
|
|
106
|
-
|
|
105
|
+
const value = `c.body=cj?${awaitValue("pj(c)", arm)}:${awaitValue("pd(c,ce,true)", arm)}\n`;
|
|
106
|
+
code += hasFn ? `if(!hasBody&&${guard}){${begin}${value}${end}}\n` : `if(${guard}){${begin}${value}${end}}\n`;
|
|
107
107
|
if (!bodyVali) link(hasRequestBody, "hb");
|
|
108
108
|
link(adapter.json, "pj");
|
|
109
109
|
link(adapter.default, "pd");
|
|
@@ -140,24 +140,24 @@ const fromArgs = (type, isAsync) => `'${type}'${isAsync ? ",true" : ""}`;
|
|
|
140
140
|
const createInlineHandler = (map, h) => ((c) => {
|
|
141
141
|
const r = h(c);
|
|
142
142
|
if (r instanceof Error) throw r;
|
|
143
|
-
if (r
|
|
143
|
+
if (typeof r?.then === "function") return Promise.resolve(r).then((v) => map(forwardError(v), c.request, true));
|
|
144
144
|
return map(r, c.request, true);
|
|
145
145
|
});
|
|
146
146
|
const createInlineHandlerWithSet = (map, h) => ((c) => {
|
|
147
147
|
const r = h(c);
|
|
148
148
|
if (r instanceof Error) throw r;
|
|
149
|
-
if (r
|
|
149
|
+
if (typeof r?.then === "function") return Promise.resolve(r).then((v) => map(forwardError(v), c.set, c.request, true));
|
|
150
150
|
return map(r, c.set, c.request, true);
|
|
151
151
|
});
|
|
152
152
|
const createInlineHandlerWithDefaultHeaders = (map, h) => ((c) => {
|
|
153
153
|
materializeSetHeaders(c.set);
|
|
154
154
|
const r = h(c);
|
|
155
155
|
if (r instanceof Error) throw r;
|
|
156
|
-
if (r
|
|
156
|
+
if (typeof r?.then === "function") return Promise.resolve(r).then((v) => map(forwardError(v), c.set, c.request, true));
|
|
157
157
|
return map(r, c.set, c.request, true);
|
|
158
158
|
});
|
|
159
159
|
function compileHandlerJit({ method, path, handler, root, errorRoot, hook, adapter, isHandleFunction, isStaticResponse, isPromiseHandler, state }) {
|
|
160
|
-
const { vali, inference, cookieConfig, beforeHandlePrefix, traceHandlers, tracePhases, hasAnyPhase, traceHandleOn, descriptor: { async: isAsync, responseMode, hasBody, bodyValiIsAsync, headersValiIsAsync, paramsValiIsAsync, queryValiIsAsync, cookieValiIsAsync: cookieValidIsAsync, responseValiAsync, hasCookieSign, syncCookieSign, asyncCookieSign, lazyCookieVerify, hasErrorHook, hasAfterResponse, hasBeforeHandle, hasAfterHandle, hasMapResponse, hasResponseValidator, hasTrace, traceCount, hasLifecycleHook,
|
|
160
|
+
const { vali, inference, cookieConfig, beforeHandlePrefix, traceHandlers, tracePhases, hasAnyPhase, traceHandleOn, descriptor: { async: isAsync, responseMode, hasBody, bodyValiIsAsync, headersValiIsAsync, paramsValiIsAsync, queryValiIsAsync, cookieValiIsAsync: cookieValidIsAsync, responseValiAsync, hasCookieSign, syncCookieSign, asyncCookieSign, lazyCookieVerify, hasErrorHook, hasAfterResponse, hasBeforeHandle, hasAfterHandle, hasMapResponse, hasResponseValidator, hasTrace, traceCount, hasLifecycleHook, syncErrorHook, syncAfterResponse } } = state;
|
|
161
161
|
const hasStaticAfterResponse = !!hook?.afterResponse?.length;
|
|
162
162
|
const hasDynamicAfterResponse = !!inference.afterResponse;
|
|
163
163
|
const seenKeys = new Set(["rt", "fre"]);
|
|
@@ -180,15 +180,10 @@ function compileHandlerJit({ method, path, handler, root, errorRoot, hook, adapt
|
|
|
180
180
|
}
|
|
181
181
|
const abortOn = hasLifecycleHook && root["~config"]?.abortSignal !== false;
|
|
182
182
|
const abortPeek = "c['~sig']?.aborted";
|
|
183
|
-
const
|
|
184
|
-
|
|
185
|
-
const
|
|
186
|
-
|
|
187
|
-
return suspended ? abortArm : abortPeek;
|
|
188
|
-
};
|
|
189
|
-
const abortCheck = () => abortOn ? `if(${abortExpression()})return emp.clone()\n` : "";
|
|
190
|
-
const abortChainGuard = () => abortOn ? isAsync ? abortArm : abortExpression() : void 0;
|
|
191
|
-
const abortCatch = abortOn ? `if(${abortArm})return emp.clone()\n` : "";
|
|
183
|
+
const arm = abortOn ? "_as??=(c['~sig']??=c.request.signal)" : "";
|
|
184
|
+
const abortCheck = () => abortOn ? `if(${abortPeek})return emp.clone()\n` : "";
|
|
185
|
+
const abortChainGuard = () => abortOn ? abortPeek : void 0;
|
|
186
|
+
const abortCatch = abortOn ? `if((${arm}).aborted)return emp.clone()\n` : "";
|
|
192
187
|
const phaseOn = (phase) => hasTrace && (tracePhases === null || tracePhases.has(phase));
|
|
193
188
|
const beginTrace = (phase, total, name = phase) => {
|
|
194
189
|
if (!phaseOn(phase)) return "";
|
|
@@ -221,7 +216,7 @@ function compileHandlerJit({ method, path, handler, root, errorRoot, hook, adapt
|
|
|
221
216
|
};
|
|
222
217
|
const needsStaticClone = !isHandleFunction && !isStaticResponse && !isPromiseHandler && (hasAfterHandle || hasMapResponse) && typeof handler === "object" && handler !== null;
|
|
223
218
|
if (needsStaticClone) link(cloneStaticValue, "scl");
|
|
224
|
-
const callHandler = isHandleFunction ?
|
|
219
|
+
const callHandler = isHandleFunction ? `_r=h(c)\n${awaitGuard(handler, isAsync, "_r", arm)}` : isStaticResponse ? `_r=cr(h)\n` : isPromiseHandler ? `_r=h.then(cr)\n` : needsStaticClone ? `_r=scl(h)\n` : `_r=h\n`;
|
|
225
220
|
let code = `${isAsync ? "async " : ""}function route(c){\n`;
|
|
226
221
|
if (abortOn) {
|
|
227
222
|
link(emptyResponse, "emp");
|
|
@@ -278,7 +273,7 @@ function compileHandlerJit({ method, path, handler, root, errorRoot, hook, adapt
|
|
|
278
273
|
if (hasBody) {
|
|
279
274
|
const parseLen = Array.isArray(hook?.parse) ? hook.parse.length : 0;
|
|
280
275
|
if (hasTrace) code += beginTrace("parse", parseLen);
|
|
281
|
-
const parseCode = parse(adapter.parse, hook?.parse, vali?.body, hasHeaders, link, buildReport("parse"));
|
|
276
|
+
const parseCode = parse(adapter.parse, hook?.parse, vali?.body, hasHeaders, link, buildReport("parse"), arm);
|
|
282
277
|
const preserveParseStatus = seenKeys.has("es");
|
|
283
278
|
link(ParseError, "pe");
|
|
284
279
|
code += "try{\n" + parseCode + `}catch(e){${preserveParseStatus ? "if(e instanceof es)throw e\n" : ""}throw new pe(e)}\n`;
|
|
@@ -291,26 +286,34 @@ function compileHandlerJit({ method, path, handler, root, errorRoot, hook, adapt
|
|
|
291
286
|
if (transformLen) {
|
|
292
287
|
link(hook.transform, "tf");
|
|
293
288
|
if (isAsync) code += "let _tf\n";
|
|
294
|
-
code += mapTransform(hook.transform, [
|
|
289
|
+
code += mapTransform(hook.transform, [
|
|
290
|
+
isAsync,
|
|
291
|
+
buildReport("transform"),
|
|
292
|
+
arm
|
|
293
|
+
], abortChainGuard());
|
|
295
294
|
}
|
|
296
295
|
code += endTrace("transform");
|
|
297
296
|
if (transformLen) code += abortCheck();
|
|
298
297
|
}
|
|
299
298
|
if (vali?.body) {
|
|
300
299
|
link(vali, "va");
|
|
301
|
-
|
|
300
|
+
const value = `va.body.From(c.body,${fromArgs("body", bodyValiIsAsync)})`;
|
|
301
|
+
code += `c.body=${bodyValiIsAsync ? awaitValue(value, arm) : value}\n`;
|
|
302
302
|
}
|
|
303
303
|
if (vali?.headers) {
|
|
304
304
|
link(vali, "va");
|
|
305
|
-
|
|
305
|
+
const value = `va.headers.From(c.headers,${fromArgs("headers", !!headersValiIsAsync)})`;
|
|
306
|
+
code += `c.headers=${headersValiIsAsync ? awaitValue(value, arm) : value}\n`;
|
|
306
307
|
}
|
|
307
308
|
if (vali?.params) {
|
|
308
309
|
link(vali, "va");
|
|
309
|
-
|
|
310
|
+
const value = `va.params.From(c.params,${fromArgs("params", !!paramsValiIsAsync)})`;
|
|
311
|
+
code += `c.params=${paramsValiIsAsync ? awaitValue(value, arm) : value}\n`;
|
|
310
312
|
}
|
|
311
313
|
if (vali?.query) {
|
|
312
314
|
link(vali, "va");
|
|
313
|
-
|
|
315
|
+
const value = `va.query.From(c.query,${fromArgs("query", !!queryValiIsAsync)})`;
|
|
316
|
+
code += `c.query=${queryValiIsAsync ? awaitValue(value, arm) : value}\n`;
|
|
314
317
|
}
|
|
315
318
|
if (cookieConfig) {
|
|
316
319
|
link(buildCookieJar, "bcj");
|
|
@@ -335,25 +338,30 @@ function compileHandlerJit({ method, path, handler, root, errorRoot, hook, adapt
|
|
|
335
338
|
code += `let _ck=pcrsg(${cookieHeaderExpr},cc)\n`;
|
|
336
339
|
} else {
|
|
337
340
|
link(parseCookieRaw, "pcr");
|
|
338
|
-
code += `let _ck
|
|
341
|
+
code += `let _ck=${awaitValue(`pcr(${cookieHeaderExpr},cc)`, arm)}\n`;
|
|
339
342
|
}
|
|
340
343
|
if (vali?.cookie) {
|
|
341
344
|
link(vali, "va");
|
|
342
345
|
const cookieIsOptional = !!(hook?.cookie)?.["~optional"];
|
|
343
|
-
const
|
|
346
|
+
const value = `va.cookie.From(_ck,${fromArgs("cookie", !!cookieValidIsAsync)})`;
|
|
347
|
+
const validateExpr = `_ck=${cookieValidIsAsync ? awaitValue(value, arm) : value}\n`;
|
|
344
348
|
if (cookieIsOptional) code += `if(Object.keys(_ck).length){${validateExpr}}\n`;
|
|
345
349
|
else code += validateExpr;
|
|
346
350
|
}
|
|
347
351
|
code += `c.cookie=bcj(c.set,_ck,cc${deferDecode ? ",undefined,1" : ""})\n`;
|
|
348
352
|
}
|
|
349
353
|
}
|
|
350
|
-
const
|
|
354
|
+
const compactEligible = responseMode === "compact";
|
|
351
355
|
const res = adapter.response;
|
|
352
356
|
const responseMap = res.map;
|
|
353
|
-
const
|
|
357
|
+
const responseCompact = compactEligible ? res.compact : void 0;
|
|
358
|
+
const portableCompact = compactEligible && (Capture.isAotBuildEnv() || Capture.isCapturing());
|
|
359
|
+
const hasSet = !compactEligible || !responseCompact && !portableCompact;
|
|
360
|
+
const map = hasSet ? link(responseMap, "rm") ?? "rm" : link(responseCompact, "rc") ?? "rc";
|
|
361
|
+
if (portableCompact) link(responseMap, "rm");
|
|
362
|
+
const mapValue = (value) => portableCompact ? `(rc?rc(${value},c.request,true):rm(${value},c.set,c.request,true))` : hasSet ? `${map}(${value},c.set,c.request,true)` : `${map}(${value},c.request,true)`;
|
|
354
363
|
if (isStaticResponse || isPromiseHandler) link(cloneResponse, "cr");
|
|
355
|
-
const
|
|
356
|
-
const mapReturn = hasSet ? `rm(${handleInstruction},c.set,c.request,true)\n` : `rc(${handleInstruction},c.request,true)\n`;
|
|
364
|
+
const mapReturn = `${mapValue(isHandleFunction ? "h(c)" : isStaticResponse ? "cr(h)" : isPromiseHandler ? "h.then(cr)" : "h")}\n`;
|
|
357
365
|
if (hasStaticAfterResponse) link(hook.afterResponse, "ar");
|
|
358
366
|
const drainTraceStream = traceHandleOn ? `let _ser\nif(_trs){try{for await(const v of _trs){}}catch(_te){_ser=_te}}\n` : "";
|
|
359
367
|
const resolveHandlePostDrain = traceHandleOn ? (() => {
|
|
@@ -369,7 +377,7 @@ function compileHandlerJit({ method, path, handler, root, errorRoot, hook, adapt
|
|
|
369
377
|
const syncScheduleDecl = syncAfterResponse && scheduleAfterResponse ? `function _scf(c,_stl){\n${scheduleAfterResponse}}\n` : "";
|
|
370
378
|
const catchSchedule = dedupSchedule ? `_sc()\n` : syncScheduleDecl ? `_scf(c)\n` : "";
|
|
371
379
|
const freThenSchedule = (arg) => catchSchedule ? `if(c._arf)return fre(rt,c,${arg})\nc._arf=true\nconst _fr=fre(rt,c,${arg})\nreturn typeof _fr?.then==='function'?_fr.then((_v)=>{${catchSchedule}return _v\n}):(${catchSchedule.trim()},_fr)\n` : `return fre(rt,c,${arg})\n`;
|
|
372
|
-
const signPrefix = syncCookieSign ? `scv(c.set.cookie,cc)\n` : asyncCookieSign ? `_sg=scv(c.set.cookie,cc)\nif(_sg)await _sg\n` : "";
|
|
380
|
+
const signPrefix = syncCookieSign ? `scv(c.set.cookie,cc)\n` : asyncCookieSign ? `_sg=scv(c.set.cookie,cc)\nif(_sg){${arm ? `${arm}\n` : ""}await _sg}\n` : "";
|
|
373
381
|
if (syncCookieSign || asyncCookieSign) link(signCookieValues, "scv");
|
|
374
382
|
let factoryHelpers = "";
|
|
375
383
|
if (hasBeforeHandle || hasAfterHandle || hasMapResponse || hasAfterResponse || hasResponseValidator || hasCookieSign || hasTrace) {
|
|
@@ -383,7 +391,7 @@ function compileHandlerJit({ method, path, handler, root, errorRoot, hook, adapt
|
|
|
383
391
|
const rbpAbort = abortOn ? ",1" : "";
|
|
384
392
|
if (isAsync) {
|
|
385
393
|
link(runBeforeHandlePrefixAsync, "rbp");
|
|
386
|
-
code += `tmp
|
|
394
|
+
code += `tmp=${awaitValue(`rbp(bp,c${rbpAbort})`, arm)}\n`;
|
|
387
395
|
} else {
|
|
388
396
|
link(runBeforeHandlePrefix, "rbp");
|
|
389
397
|
code += `tmp=rbp(bp,c${rbpAbort})\n`;
|
|
@@ -394,7 +402,7 @@ function compileHandlerJit({ method, path, handler, root, errorRoot, hook, adapt
|
|
|
394
402
|
link(hook.beforeHandle, "bf");
|
|
395
403
|
const deriveEntries = hook["~deriveEntries"];
|
|
396
404
|
const chainGuard = abortChainGuard();
|
|
397
|
-
const mapped = mapBeforeHandle(hook.beforeHandle, deriveEntries, link, isAsync, buildReport("beforeHandle"), chainGuard);
|
|
405
|
+
const mapped = mapBeforeHandle(hook.beforeHandle, deriveEntries, link, isAsync, buildReport("beforeHandle"), chainGuard, arm);
|
|
398
406
|
code += beforeHandlePrefix ? `if(${chainGuard ? `!${chainGuard}&&` : ""}_r===undefined){\n${mapped}}\n` : mapped;
|
|
399
407
|
}
|
|
400
408
|
}
|
|
@@ -402,9 +410,7 @@ function compileHandlerJit({ method, path, handler, root, errorRoot, hook, adapt
|
|
|
402
410
|
if (hasBeforeHandle) code += abortCheck();
|
|
403
411
|
}
|
|
404
412
|
if (hasAfterResponse || traceHandleOn) link(tee, "tee");
|
|
405
|
-
const
|
|
406
|
-
const teeCount = teeConsumers + 1;
|
|
407
|
-
const teeBlock = teeConsumers > 0 && !syncAfterResponse ? `if(_r&&(_r[Symbol.iterator]||_r[Symbol.asyncIterator])&&typeof _r.next==='function'){\nconst _s=tee(_r,${teeCount})\n_r=_s[0]\n` + (hasAfterResponse ? `_stl=_s[1]\n` : "") + (traceHandleOn ? `_trs=_s[${1 + (hasAfterResponse ? 1 : 0)}]\n` : "") + `}\n` : "";
|
|
413
|
+
const teeBlock = (hasAfterResponse || traceHandleOn) && !syncAfterResponse ? "if(_r&&(_r[Symbol.iterator]||_r[Symbol.asyncIterator])&&typeof _r.next==='function'){\nconst _s=tee(_r,2)\n_r=_s[0]\n" + (traceHandleOn ? `_trs=_s[1]\n` : `_stl=_s[1]\n`) + `}\n` : "";
|
|
408
414
|
if (traceHandleOn) {
|
|
409
415
|
const handleName = handler?.name && typeof handler.name === "string" ? handler.name : "anonymous";
|
|
410
416
|
code += beginTrace("handle", 1, handleName);
|
|
@@ -423,16 +429,13 @@ function compileHandlerJit({ method, path, handler, root, errorRoot, hook, adapt
|
|
|
423
429
|
code += abortCheck();
|
|
424
430
|
if (syncAfterResponse) {
|
|
425
431
|
link(forwardError, "fe");
|
|
426
|
-
factoryHelpers += syncScheduleDecl +
|
|
427
|
-
if(_r
|
|
428
|
-
if(_r&&(_r[Symbol.iterator]||_r[Symbol.asyncIterator])&&typeof _r.next==='function'){
|
|
429
|
-
const _s=tee(_r,${teeCount})\nreturn _fin2(c,_s[0],_s[1])\n}\nreturn _fin2(c,_r,undefined)\n}\nfunction _fin2(c,_r,_stl){\nc.responseValue=_r\n` + (syncScheduleDecl ? `_scf(c,_stl)\n` : scheduleAfterResponse) + `const _m=${hasSet ? `${map}(_r,c.set,c.request,true)` : `${map}(_r,c.request,true)`}\nreturn typeof _m?.then==='function'?Promise.resolve(_m).catch((_e)=>fre(rt,c,_e)):_m\n}\n`;
|
|
430
|
-
code += "if(_r instanceof Promise)return _r.then(fe).then((_v)=>_fin(c,_v)).catch((_e)=>fre(rt,c,_e))\nreturn _fin(c,_r)\n";
|
|
432
|
+
factoryHelpers += syncScheduleDecl + "function _fin(c,_r){\nif(_r instanceof Error)throw _r\nif(_r&&(_r[Symbol.iterator]||_r[Symbol.asyncIterator])&&typeof _r.next==='function'){\nconst _s=tee(_r,2)\nreturn _fin2(c,_s[0],_s[1])\n}\nreturn _fin2(c,_r,undefined)\n}\nfunction _fin2(c,_r,_stl){\nc.responseValue=_r\n" + (syncScheduleDecl ? `_scf(c,_stl)\n` : scheduleAfterResponse) + `const _m=${mapValue("_r")}\nreturn typeof _m?.then==='function'?Promise.resolve(_m).catch((_e)=>fre(rt,c,_e)):_m\n}\n`;
|
|
433
|
+
code += "if(typeof _r?.then==='function')return Promise.resolve(_r).then(fe).then((_v)=>_fin(c,_v)).catch((_e)=>fre(rt,c,_e))\nreturn _fin(c,_r)\n";
|
|
431
434
|
} else {
|
|
432
435
|
code += `if(_r instanceof Error)throw _r\n`;
|
|
433
436
|
if (!isAsync) {
|
|
434
437
|
link(forwardError, "fe");
|
|
435
|
-
code += `else if(_r
|
|
438
|
+
code += `else if(typeof _r?.then==='function')_r=Promise.resolve(_r).then(fe)\n`;
|
|
436
439
|
}
|
|
437
440
|
if (hasAfterHandle || hasMapResponse || hasAfterResponse || hasTrace) code += `c.responseValue=_r\n`;
|
|
438
441
|
if (hasAfterHandle || hasTrace) {
|
|
@@ -440,7 +443,7 @@ const _s=tee(_r,${teeCount})\nreturn _fin2(c,_s[0],_s[1])\n}\nreturn _fin2(c,_r,
|
|
|
440
443
|
code += beginTrace("afterHandle", afLen);
|
|
441
444
|
if (hasAfterHandle) {
|
|
442
445
|
link(hook.afterHandle, "af");
|
|
443
|
-
code += mapAfterHandle(hook.afterHandle, isAsync, buildReport("afterHandle"), abortChainGuard());
|
|
446
|
+
code += mapAfterHandle(hook.afterHandle, isAsync, buildReport("afterHandle"), abortChainGuard(), arm);
|
|
444
447
|
}
|
|
445
448
|
code += endTrace("afterHandle");
|
|
446
449
|
if (hasAfterHandle) code += abortCheck();
|
|
@@ -450,7 +453,7 @@ const _s=tee(_r,${teeCount})\nreturn _fin2(c,_s[0],_s[1])\n}\nreturn _fin2(c,_r,
|
|
|
450
453
|
code += beginTrace("mapResponse", mrLen);
|
|
451
454
|
if (hasMapResponse) {
|
|
452
455
|
link(hook.mapResponse, "mr");
|
|
453
|
-
code += mapMapResponse(hook.mapResponse, isAsync, buildReport("mapResponse"), abortChainGuard());
|
|
456
|
+
code += mapMapResponse(hook.mapResponse, isAsync, buildReport("mapResponse"), abortChainGuard(), arm);
|
|
454
457
|
}
|
|
455
458
|
code += endTrace("mapResponse");
|
|
456
459
|
if (hasMapResponse) code += abortCheck();
|
|
@@ -458,15 +461,16 @@ const _s=tee(_r,${teeCount})\nreturn _fin2(c,_s[0],_s[1])\n}\nreturn _fin2(c,_r,
|
|
|
458
461
|
if (hasResponseValidator) {
|
|
459
462
|
link(vali, "va");
|
|
460
463
|
link(ElysiaStatus, "es");
|
|
461
|
-
const
|
|
464
|
+
const encodeStatus = responseValiAsync ? `(_vr.mayReturnPromise?_vr.From(_r.response,'response',true):_vr.EncodeFrom(_r.response,'response'))` : `_vr.EncodeFrom(_r.response,'response')`;
|
|
465
|
+
const encodeBody = responseValiAsync ? `(_vr.mayReturnPromise?_vr.From(_r,'response',true):_vr.EncodeFrom(_r,'response'))` : `_vr.EncodeFrom(_r,'response')`;
|
|
462
466
|
code += `if(_r instanceof es){
|
|
463
467
|
const _vr=va.response[_r.status]
|
|
464
|
-
if(_vr)_r.response=${
|
|
468
|
+
if(_vr)_r.response=${responseValiAsync ? awaitValue(encodeStatus, arm) : encodeStatus}\n}else if(!(_r instanceof Response)&&!(_r instanceof ReadableStream)&&typeof _r?.next!=='function'){\nconst _vr=va.response[c.set.status??200]\nif(_vr)_r=${responseValiAsync ? awaitValue(encodeBody, arm) : encodeBody}\n}\n`;
|
|
465
469
|
code += abortCheck();
|
|
466
470
|
}
|
|
467
471
|
const deferSchedule = !!schedule;
|
|
468
472
|
code += signPrefix;
|
|
469
|
-
const finalMap =
|
|
473
|
+
const finalMap = mapValue("_r");
|
|
470
474
|
const onMapReject = syncErrorHook ? `(_e)=>_ce(_e,c)` : dedupSchedule ? `(_e)=>{${freThenSchedule("_e")}}` : `(_e)=>fre(rt,c,_e)`;
|
|
471
475
|
if (isAsync) code += deferSchedule ? `const _m=await ${finalMap}\n${schedule}return _m\n` : `return await ${finalMap}\n`;
|
|
472
476
|
else {
|
|
@@ -476,9 +480,9 @@ if(_vr)_r.response=${awaitStr}${responseValiAsync ? `(_vr.mayReturnPromise?_vr.F
|
|
|
476
480
|
}
|
|
477
481
|
} else if (isHandleFunction) {
|
|
478
482
|
if (!isAsync) link(forwardError, "fe");
|
|
479
|
-
const
|
|
480
|
-
code +=
|
|
481
|
-
code += abortCheck() + `if(_r instanceof Error)throw _r\n` + (isAsync ? `return await ${
|
|
483
|
+
const finalMap = mapValue("_r");
|
|
484
|
+
code += `let ${callHandler}`;
|
|
485
|
+
code += abortCheck() + `if(_r instanceof Error)throw _r\n` + (isAsync ? `return await ${finalMap}\n` : syncErrorHook ? `if(typeof _r?.then==='function')_r=Promise.resolve(_r).then(fe)\nconst _m=${finalMap}\nreturn typeof _m?.then==='function'?Promise.resolve(_m).catch((_e)=>_ce(_e,c)):_m\n` : `if(typeof _r?.then==='function')_r=Promise.resolve(_r).then(fe)\nconst _m=${finalMap}\nreturn typeof _m?.then==='function'?Promise.resolve(_m).catch((_e)=>fre(rt,c,_e)):_m\n`);
|
|
482
486
|
} else code += `const _m=${mapReturn.trim()}\nreturn typeof _m?.then==='function'?Promise.resolve(_m).catch((_e)=>fre(rt,c,_e)):_m\n`;
|
|
483
487
|
if (hasErrorHook || hasTrace) {
|
|
484
488
|
let body = "";
|
|
@@ -491,17 +495,18 @@ if(_vr)_r.response=${awaitStr}${responseValiAsync ? `(_vr.mayReturnPromise?_vr.F
|
|
|
491
495
|
link(fallbackResponse, "fbr");
|
|
492
496
|
const allowUnsafeDetail = !!root["~config"]?.allowUnsafeValidationDetails;
|
|
493
497
|
if (allowUnsafeDetail) link(ValidationError, "verr");
|
|
494
|
-
factoryHelpers += `function _em(c,_r){return typeof _r?.then==='function'?Promise.resolve(_r).catch((_e)=>fre(rt,c,_e)):_r}\nfunction _fbm(_r,_s,_c){return ${map}(_r,_s,_c.request,true)}\n${asyncCookieSign ? "async " : ""}function _efb(e,c){\n` + (asyncCookieSign ? `let _sg\n` : ``) + signPrefix + "return _em(c,fbr(c,e,_fbm))\n}\n";
|
|
498
|
+
factoryHelpers += `function _em(c,_r){return typeof _r?.then==='function'?Promise.resolve(_r).catch((_e)=>fre(rt,c,_e)):_r}\nfunction _fbm(_r,_s,_c){return ${map}(_r,_s,_c.request,true)}\n${asyncCookieSign ? "async " : ""}function _efb(e,c){\n` + (asyncCookieSign ? `let _sg${arm ? ",_as" : ""}\n` : ``) + signPrefix + "return _em(c,fbr(c,e,_fbm))\n}\n";
|
|
495
499
|
body += `c.error=e\n` + (allowUnsafeDetail ? `if(e instanceof verr)e.allowUnsafeValidationDetails=true\n` : ``) + `if(e?.status)c.set.status=e.status
|
|
496
500
|
else if(c.set.status===undefined||c.set.status===200)c.set.status=500
|
|
497
501
|
let _r${hasMapResponse ? ",tmp" : ""}\n` + mapError(hook.error, [
|
|
498
502
|
map,
|
|
499
503
|
link,
|
|
500
|
-
|
|
501
|
-
(hasMapResponse ? `c.responseValue=_r\n` + mapMapResponse(hook.mapResponse, isAsync, void 0,
|
|
504
|
+
responseMap,
|
|
505
|
+
(hasMapResponse ? `c.responseValue=_r\n` + mapMapResponse(hook.mapResponse, isAsync, void 0, abortChainGuard(), arm) : "") + endTrace("error") + abortCatch + schedule,
|
|
502
506
|
signPrefix,
|
|
503
|
-
isAsync
|
|
504
|
-
|
|
507
|
+
isAsync,
|
|
508
|
+
arm
|
|
509
|
+
], abortChainGuard()) + endTrace("error") + abortCatch + schedule + `return _efb(e,c)\n`;
|
|
505
510
|
} else body += endTrace("error") + freThenSchedule("e");
|
|
506
511
|
if (syncErrorHook) {
|
|
507
512
|
factoryHelpers += `function _ce(e,c){${abortOn ? "let _as\n" : ""}try{\n${body}}catch(_ee){return fre(rt,c,_ee)}}\n`;
|
|
@@ -509,7 +514,7 @@ let _r${hasMapResponse ? ",tmp" : ""}\n` + mapError(hook.error, [
|
|
|
509
514
|
} else code += `}catch(e){try{\n${body}}catch(_ee){return fre(rt,c,_ee)}}\n`;
|
|
510
515
|
} else code += catchSchedule ? `}catch(e){${freThenSchedule("e")}}\n` : `}catch(e){return fre(rt,c,e)}\n`;
|
|
511
516
|
code += "}";
|
|
512
|
-
code = head + (abortOn && code.includes("_as") ? "let _as\n" : "") + scheduleDecl + code;
|
|
517
|
+
code = head + (abortOn && code.includes("_as") ? "let _as\n" : "") + (code.includes("_av") ? "let _av\n" : "") + scheduleDecl + code;
|
|
513
518
|
if (factoryHelpers) code = `(function(){\n${factoryHelpers}return ${code}})()`;
|
|
514
519
|
const alias = aliasKeys.join(",");
|
|
515
520
|
const fullAlias = alias ? `rt,fre,${alias}` : "rt,fre";
|
|
@@ -521,7 +526,7 @@ let _r${hasMapResponse ? ",tmp" : ""}\n` + mapError(hook.error, [
|
|
|
521
526
|
});
|
|
522
527
|
const isGeneratorHandler = isHandleFunction && handler.constructor.name.endsWith("GeneratorFunction");
|
|
523
528
|
if (!hasTrace && isHandleFunction && !isGeneratorHandler && !inlineUnsafe) {
|
|
524
|
-
if (alias === "rc" || !isAsync && !syncErrorHook && alias === "rc,fe") return createInlineHandler(
|
|
529
|
+
if (alias === "rc" || !isAsync && !syncErrorHook && alias === "rc,fe") return createInlineHandler(responseCompact, handler);
|
|
525
530
|
else if (alias === "rm" || alias === "msh,rm" || !isAsync && !syncErrorHook && (alias === "rm,fe" || alias === "msh,rm,fe")) return responseMode === "set-with-default-headers" && inference.set ? createInlineHandlerWithDefaultHeaders(responseMap, handler) : createInlineHandlerWithSet(responseMap, handler);
|
|
526
531
|
}
|
|
527
532
|
JITProbe.record("handler:new-function");
|
|
@@ -7,6 +7,7 @@ const require_cookie_utils = require('../../cookie/utils.js');
|
|
|
7
7
|
const require_handler_utils = require('../../handler/utils.js');
|
|
8
8
|
const require_handler_error = require('../../handler/error.js');
|
|
9
9
|
const require_compile_handler_utils = require('./utils.js');
|
|
10
|
+
const require_compile_handler_static_clone_resolver = require('./static-clone-resolver.js');
|
|
10
11
|
|
|
11
12
|
//#region src/compile/handler/params.ts
|
|
12
13
|
let _handlerParams;
|
|
@@ -22,7 +23,7 @@ const handlerParams = () => _handlerParams ??= {
|
|
|
22
23
|
qa: (c) => require_compile_handler_utils.getQueryParseChannels(c.vali?.query?.schema)?.array,
|
|
23
24
|
qo: (c) => require_compile_handler_utils.getQueryParseChannels(c.vali?.query?.schema)?.object,
|
|
24
25
|
rm: (c) => c.res.map,
|
|
25
|
-
rc: (c) => c.res.compact
|
|
26
|
+
rc: (c) => c.res.compact,
|
|
26
27
|
rid: () => require_utils.requestId,
|
|
27
28
|
pq: () => require_parse_query.parseQueryFromURL,
|
|
28
29
|
pe: () => require_error.ParseError,
|
|
@@ -34,7 +35,7 @@ const handlerParams = () => _handlerParams ??= {
|
|
|
34
35
|
tee: () => require_adapter_utils.tee,
|
|
35
36
|
msh: () => require_adapter_utils.materializeSetHeaders,
|
|
36
37
|
cr: () => require_compile_handler_utils.cloneResponse,
|
|
37
|
-
scl:
|
|
38
|
+
scl: require_compile_handler_static_clone_resolver.staticCloneResolver,
|
|
38
39
|
pcr: () => require_cookie_utils.parseCookieRaw,
|
|
39
40
|
pcrs: () => require_cookie_utils.parseCookieRawSync,
|
|
40
41
|
pcrsg: () => require_cookie_utils.parseCookieRawSigned,
|
|
@@ -5,7 +5,8 @@ import { parseQueryFromURL } from "../../parse-query.mjs";
|
|
|
5
5
|
import { buildCookieJar, parseCookieRaw, parseCookieRawDeferred, parseCookieRawLazy, parseCookieRawSigned, parseCookieRawSync, signCookieValues } from "../../cookie/utils.mjs";
|
|
6
6
|
import { emptyResponse, finalizeRouteError, forwardError } from "../../handler/utils.mjs";
|
|
7
7
|
import { adoptErrorType, fallbackResponse } from "../../handler/error.mjs";
|
|
8
|
-
import { armEntryAbort, cloneResponse,
|
|
8
|
+
import { armEntryAbort, cloneResponse, getQueryParseChannels, hasRequestBody, replaceDeriveContext, runBeforeHandlePrefix } from "./utils.mjs";
|
|
9
|
+
import { staticCloneResolver } from "./static-clone-resolver.mjs";
|
|
9
10
|
|
|
10
11
|
//#region src/compile/handler/params.ts
|
|
11
12
|
let _handlerParams;
|
|
@@ -21,7 +22,7 @@ const handlerParams = () => _handlerParams ??= {
|
|
|
21
22
|
qa: (c) => getQueryParseChannels(c.vali?.query?.schema)?.array,
|
|
22
23
|
qo: (c) => getQueryParseChannels(c.vali?.query?.schema)?.object,
|
|
23
24
|
rm: (c) => c.res.map,
|
|
24
|
-
rc: (c) => c.res.compact
|
|
25
|
+
rc: (c) => c.res.compact,
|
|
25
26
|
rid: () => requestId,
|
|
26
27
|
pq: () => parseQueryFromURL,
|
|
27
28
|
pe: () => ParseError,
|
|
@@ -33,7 +34,7 @@ const handlerParams = () => _handlerParams ??= {
|
|
|
33
34
|
tee: () => tee,
|
|
34
35
|
msh: () => materializeSetHeaders,
|
|
35
36
|
cr: () => cloneResponse,
|
|
36
|
-
scl:
|
|
37
|
+
scl: staticCloneResolver,
|
|
37
38
|
pcr: () => parseCookieRaw,
|
|
38
39
|
pcrs: () => parseCookieRawSync,
|
|
39
40
|
pcrsg: () => parseCookieRawSigned,
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
+
const require_compile_handler_utils = require('./utils.js');
|
|
3
|
+
|
|
4
|
+
//#region src/compile/handler/static-clone-resolver.ts
|
|
5
|
+
const staticCloneResolver = () => require_compile_handler_utils.cloneStaticValue;
|
|
6
|
+
|
|
7
|
+
//#endregion
|
|
8
|
+
exports.staticCloneResolver = staticCloneResolver;
|
|
@@ -22,24 +22,25 @@ declare function hasRequestBody(request: Request): boolean;
|
|
|
22
22
|
* @see `../../adapter/origin` for the provenance channel
|
|
23
23
|
*/
|
|
24
24
|
declare function armEntryAbort(context: any): any;
|
|
25
|
-
declare const mapTransform: (event: MaybeArray<TransformHandler<any, any, undefined, "local">>, rest?: [isAsync: boolean, report?: TraceReporter | undefined] | undefined, abortGuard?: string) => string;
|
|
25
|
+
declare const mapTransform: (event: MaybeArray<TransformHandler<any, any, undefined, "local">>, rest?: [isAsync: boolean, report?: TraceReporter | undefined, arm?: string | undefined] | undefined, abortGuard?: string) => string;
|
|
26
26
|
declare function replaceDeriveContext(context: any, derivative: any): any;
|
|
27
27
|
declare function deriveModes(hooks: Function[], entries?: readonly DeriveEntry[]): (boolean | undefined)[] | undefined;
|
|
28
|
-
declare function mapBeforeHandle(_hooks: AppHook['beforeHandle'] | AppHook['beforeHandle'][0], derive: readonly DeriveEntry[] | undefined, link: Link, isAsync: boolean, report?: TraceReporter, abortGuard?: string): string;
|
|
28
|
+
declare function mapBeforeHandle(_hooks: AppHook['beforeHandle'] | AppHook['beforeHandle'][0], derive: readonly DeriveEntry[] | undefined, link: Link, isAsync: boolean, report?: TraceReporter, abortGuard?: string, arm?: string): string;
|
|
29
29
|
type BeforeHandleContext = {
|
|
30
30
|
request: Request;
|
|
31
31
|
'~sig'?: AbortSignal;
|
|
32
32
|
};
|
|
33
33
|
declare function runBeforeHandlePrefix(prefix: CompactBeforeHandlePrefix, context: BeforeHandleContext, abort?: 1): any;
|
|
34
34
|
declare function runBeforeHandlePrefixAsync(prefix: CompactBeforeHandlePrefix, context: BeforeHandleContext, abort?: 1): Promise<any>;
|
|
35
|
-
declare const mapAfterHandle: (_hooks: AppHook["afterHandle"] | AppHook["afterHandle"][0], isAsync: boolean, report?: TraceReporter, abortGuard?: string) => string;
|
|
36
|
-
declare const mapMapResponse: (_hooks: AppHook["mapResponse"] | AppHook["mapResponse"][0], isAsync: boolean, report?: TraceReporter, abortGuard?: string) => string;
|
|
35
|
+
declare const mapAfterHandle: (_hooks: AppHook["afterHandle"] | AppHook["afterHandle"][0], isAsync: boolean, report?: TraceReporter, abortGuard?: string, arm?: string) => string;
|
|
36
|
+
declare const mapMapResponse: (_hooks: AppHook["mapResponse"] | AppHook["mapResponse"][0], isAsync: boolean, report?: TraceReporter, abortGuard?: string, arm?: string) => string;
|
|
37
37
|
declare const mapAfterResponse: (event: MaybeArray<AfterResponseHandler<any, any, undefined, "local">>, rest?: [report?: TraceReporter | undefined] | undefined, abortGuard?: string) => string;
|
|
38
|
-
declare const mapError: (event: MaybeArray<ErrorHandler<any, any, any>>, rest?: [map: string, link: Link, mapResponse: (response: unknown, set: Context["set"], ...params: unknown[]) => unknown, schedule: string, sign: string, isAsync: boolean] | undefined, abortGuard?: string) => string;
|
|
38
|
+
declare const mapError: (event: MaybeArray<ErrorHandler<any, any, any>>, rest?: [map: string, link: Link, mapResponse: (response: unknown, set: Context["set"], ...params: unknown[]) => unknown, schedule: string, sign: string, isAsync: boolean, arm?: string | undefined] | undefined, abortGuard?: string) => string;
|
|
39
39
|
interface QueryWalkState {
|
|
40
40
|
array: Record<string, 1> | undefined;
|
|
41
41
|
object: Record<string, 1> | undefined;
|
|
42
42
|
}
|
|
43
43
|
declare function getQueryParseChannels(querySchema: any): QueryWalkState | undefined;
|
|
44
|
+
declare function awaitGuard(fn: Function, isAsync: boolean, target: string, arm?: string): string;
|
|
44
45
|
//#endregion
|
|
45
|
-
export { Link, TraceReporter, armEntryAbort, cloneResponse, cloneStaticValue, deriveModes, emptyResponse, getQueryParseChannels, hasRequestBody, mapAfterHandle, mapAfterResponse, mapBeforeHandle, mapError, mapMapResponse, mapTransform, replaceDeriveContext, runBeforeHandlePrefix, runBeforeHandlePrefixAsync };
|
|
46
|
+
export { Link, TraceReporter, armEntryAbort, awaitGuard, cloneResponse, cloneStaticValue, deriveModes, emptyResponse, getQueryParseChannels, hasRequestBody, mapAfterHandle, mapAfterResponse, mapBeforeHandle, mapError, mapMapResponse, mapTransform, replaceDeriveContext, runBeforeHandlePrefix, runBeforeHandlePrefixAsync };
|
|
@@ -25,7 +25,9 @@ function cloneResponse(r) {
|
|
|
25
25
|
}
|
|
26
26
|
function cloneStaticValue(value) {
|
|
27
27
|
try {
|
|
28
|
-
|
|
28
|
+
const cloned = structuredClone(value);
|
|
29
|
+
if (Object.getPrototypeOf(value) === require_error.ElysiaStatus.prototype) Object.setPrototypeOf(cloned, require_error.ElysiaStatus.prototype);
|
|
30
|
+
return cloned;
|
|
29
31
|
} catch {
|
|
30
32
|
return value;
|
|
31
33
|
}
|
|
@@ -51,10 +53,9 @@ function armEntryAbort(context) {
|
|
|
51
53
|
}
|
|
52
54
|
const trace = (report, fn) => report?.resolveChild(childName(fn)) ?? noTrace;
|
|
53
55
|
const toArray = (v) => Array.isArray(v) ? v : [v];
|
|
54
|
-
const mapTransform = /* @__PURE__ */ map((i, fn, [isAsync, report]) => {
|
|
56
|
+
const mapTransform = /* @__PURE__ */ map((i, fn, [isAsync, report, arm]) => {
|
|
55
57
|
const t = trace(report, fn);
|
|
56
|
-
const
|
|
57
|
-
const call = guard ? `_tf=tf${at(i)}(c)\n${guard}` : `${Await(fn)}tf${at(i)}(c)\n`;
|
|
58
|
+
const call = isAsync ? `_tf=tf${at(i)}(c)\n${awaitGuard(fn, isAsync, "_tf", arm)}` : `tf${at(i)}(c)\n`;
|
|
58
59
|
return t.begin + call + t.end();
|
|
59
60
|
});
|
|
60
61
|
const deriveKeyCache = /* @__PURE__ */ new WeakMap();
|
|
@@ -318,7 +319,7 @@ function skipValue(src, i) {
|
|
|
318
319
|
}
|
|
319
320
|
return -1;
|
|
320
321
|
}
|
|
321
|
-
function mapBeforeHandle(_hooks, derive, link, isAsync, report, abortGuard) {
|
|
322
|
+
function mapBeforeHandle(_hooks, derive, link, isAsync, report, abortGuard, arm) {
|
|
322
323
|
const hooks = toArray(_hooks);
|
|
323
324
|
const modes = deriveModes(hooks, derive);
|
|
324
325
|
let code = "";
|
|
@@ -332,8 +333,8 @@ function mapBeforeHandle(_hooks, derive, link, isAsync, report, abortGuard) {
|
|
|
332
333
|
}
|
|
333
334
|
const t = trace(report, fn);
|
|
334
335
|
code += t.begin;
|
|
335
|
-
code += `tmp
|
|
336
|
-
code += awaitGuard(fn, isAsync, "tmp");
|
|
336
|
+
code += `tmp=bf${at(i)}(c)\n`;
|
|
337
|
+
code += awaitGuard(fn, isAsync, "tmp", arm);
|
|
337
338
|
if (modes?.[i] !== void 0) {
|
|
338
339
|
needsEs = true;
|
|
339
340
|
if (modes[i]) {
|
|
@@ -379,7 +380,7 @@ async function runBeforeHandlePrefixAsync(prefix, context, abort) {
|
|
|
379
380
|
if (!first && abort && context["~sig"]?.aborted) return;
|
|
380
381
|
first = false;
|
|
381
382
|
let result = values[j](context);
|
|
382
|
-
if (result
|
|
383
|
+
if (typeof result?.then === "function") {
|
|
383
384
|
result = await result;
|
|
384
385
|
if (abort) context["~sig"] ??= context.request.signal;
|
|
385
386
|
}
|
|
@@ -387,7 +388,7 @@ async function runBeforeHandlePrefixAsync(prefix, context, abort) {
|
|
|
387
388
|
}
|
|
388
389
|
}
|
|
389
390
|
}
|
|
390
|
-
function mapChainHook(hooks, prefix, isAsync, report, abortGuard) {
|
|
391
|
+
function mapChainHook(hooks, prefix, isAsync, report, abortGuard, arm) {
|
|
391
392
|
let code = "";
|
|
392
393
|
let depth = 0;
|
|
393
394
|
for (let i = 0; i < hooks.length; i++) {
|
|
@@ -398,25 +399,25 @@ function mapChainHook(hooks, prefix, isAsync, report, abortGuard) {
|
|
|
398
399
|
}
|
|
399
400
|
const t = trace(report, fn);
|
|
400
401
|
code += t.begin;
|
|
401
|
-
code += `tmp=${
|
|
402
|
-
code += awaitGuard(fn, isAsync, "tmp");
|
|
402
|
+
code += `tmp=${prefix}${at(i)}(c)\n`;
|
|
403
|
+
code += awaitGuard(fn, isAsync, "tmp", arm);
|
|
403
404
|
code += t.end("tmp");
|
|
404
405
|
}
|
|
405
406
|
code += "}".repeat(depth);
|
|
406
407
|
code += `if(tmp!==undefined)_r=c.responseValue=tmp\n`;
|
|
407
408
|
return code;
|
|
408
409
|
}
|
|
409
|
-
const mapAfterHandle = (_hooks, isAsync, report, abortGuard) => mapChainHook(toArray(_hooks), "af", isAsync, report, abortGuard);
|
|
410
|
-
const mapMapResponse = (_hooks, isAsync, report, abortGuard) => mapChainHook(toArray(_hooks), "mr", isAsync, report, abortGuard);
|
|
410
|
+
const mapAfterHandle = (_hooks, isAsync, report, abortGuard, arm) => mapChainHook(toArray(_hooks), "af", isAsync, report, abortGuard, arm);
|
|
411
|
+
const mapMapResponse = (_hooks, isAsync, report, abortGuard, arm) => mapChainHook(toArray(_hooks), "mr", isAsync, report, abortGuard, arm);
|
|
411
412
|
const mapAfterResponse = /* @__PURE__ */ map((i, fn, [report]) => {
|
|
412
413
|
const t = trace(report, fn);
|
|
413
|
-
const call = require_compile_utils.isAsyncFunction(fn) ? `await ar${at(i)}(c)\n` : `let _ar=ar${at(i)}(c)\nif(_ar
|
|
414
|
+
const call = require_compile_utils.isAsyncFunction(fn) ? `await ar${at(i)}(c)\n` : `let _ar=ar${at(i)}(c)\nif(typeof _ar?.then==='function')await _ar\n`;
|
|
414
415
|
return `try{${t.begin}${call}${t.end()}}catch(_e){${t.end("_e")}console.error(_e)}\n`;
|
|
415
416
|
});
|
|
416
|
-
const mapError = /* @__PURE__ */ map((i, fn, [map, link, mapResponse, schedule, sign, isAsync]) => {
|
|
417
|
+
const mapError = /* @__PURE__ */ map((i, fn, [map, link, mapResponse, schedule, sign, isAsync, arm]) => {
|
|
417
418
|
link(mapResponse, "rm");
|
|
418
419
|
link(require_handler_error.adoptErrorType, "aet");
|
|
419
|
-
return `_r
|
|
420
|
+
return `_r=er${at(i)}(c)\n` + awaitGuard(fn, isAsync, "_r", arm) + "if(_r!==undefined){\nif(_r instanceof Response)c.set.status=_r.status\nelse if(c.set.status===undefined||c.set.status===200)c.set.status=500\n" + schedule + sign + `return _em(c,${map}(aet(_r,e),c.set,c.request,true))\n}\n`;
|
|
420
421
|
});
|
|
421
422
|
function map(map) {
|
|
422
423
|
return function(event, rest, abortGuard) {
|
|
@@ -502,11 +503,15 @@ function getQueryParseChannels(querySchema) {
|
|
|
502
503
|
queryParseChannelsCache.set(querySchema, result);
|
|
503
504
|
return result ?? void 0;
|
|
504
505
|
}
|
|
505
|
-
|
|
506
|
-
|
|
506
|
+
function awaitGuard(fn, isAsync, target, arm = "") {
|
|
507
|
+
if (!isAsync) return "";
|
|
508
|
+
const code = `${arm ? `;${arm}\n` : ""}${target}=await ${target}\n`;
|
|
509
|
+
return require_compile_utils.isAsyncFunction(fn) ? code : `if(typeof ${target}?.then==='function'){${code}}\n`;
|
|
510
|
+
}
|
|
507
511
|
|
|
508
512
|
//#endregion
|
|
509
513
|
exports.armEntryAbort = armEntryAbort;
|
|
514
|
+
exports.awaitGuard = awaitGuard;
|
|
510
515
|
exports.cloneResponse = cloneResponse;
|
|
511
516
|
exports.cloneStaticValue = cloneStaticValue;
|
|
512
517
|
exports.deriveModes = deriveModes;
|