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.
Files changed (135) hide show
  1. package/dist/_virtual/_rolldown/runtime.js +2 -0
  2. package/dist/adapter/bun/index.d.ts +2 -25
  3. package/dist/adapter/bun/index.js +18 -12
  4. package/dist/adapter/bun/index.mjs +19 -13
  5. package/dist/adapter/bun/router.js +1 -1
  6. package/dist/adapter/bun/router.mjs +1 -1
  7. package/dist/adapter/constants.d.ts +3 -26
  8. package/dist/adapter/index.d.ts +1 -26
  9. package/dist/adapter/index.js +1 -17
  10. package/dist/adapter/index.mjs +1 -17
  11. package/dist/adapter/types.d.ts +1 -0
  12. package/dist/adapter/utils.d.ts +3 -4
  13. package/dist/adapter/utils.js +83 -81
  14. package/dist/adapter/utils.mjs +82 -80
  15. package/dist/adapter/web-standard/handler.js +6 -3
  16. package/dist/adapter/web-standard/handler.mjs +6 -3
  17. package/dist/adapter/web-standard/index.d.ts +2 -26
  18. package/dist/adapter/web-standard/index.js +12 -1
  19. package/dist/adapter/web-standard/index.mjs +12 -1
  20. package/dist/base.d.ts +38 -68
  21. package/dist/base.js +133 -97
  22. package/dist/base.mjs +133 -97
  23. package/dist/compile/aot.d.ts +22 -1
  24. package/dist/compile/aot.js +22 -0
  25. package/dist/compile/aot.mjs +22 -0
  26. package/dist/compile/handler/index.d.ts +2 -2
  27. package/dist/compile/handler/index.js +85 -456
  28. package/dist/compile/handler/index.mjs +86 -457
  29. package/dist/compile/handler/jit.d.ts +35 -0
  30. package/dist/compile/handler/jit.js +445 -0
  31. package/dist/compile/handler/jit.mjs +443 -0
  32. package/dist/compile/handler/params.js +3 -2
  33. package/dist/compile/handler/params.mjs +4 -3
  34. package/dist/compile/handler/reconstruct.d.ts +13 -0
  35. package/dist/compile/handler/reconstruct.js +29 -0
  36. package/dist/compile/handler/reconstruct.mjs +28 -0
  37. package/dist/compile/handler/utils.d.ts +2 -1
  38. package/dist/compile/handler/utils.js +9 -3
  39. package/dist/compile/handler/utils.mjs +9 -4
  40. package/dist/compile/jit-probe.d.ts +19 -0
  41. package/dist/compile/jit-probe.js +38 -0
  42. package/dist/compile/jit-probe.mjs +36 -0
  43. package/dist/cookie/index.d.ts +4 -3
  44. package/dist/cookie/index.js +2 -1
  45. package/dist/cookie/index.mjs +2 -1
  46. package/dist/cookie/serialize.d.ts +6 -0
  47. package/dist/cookie/serialize.js +28 -0
  48. package/dist/cookie/serialize.mjs +27 -0
  49. package/dist/cookie/utils.d.ts +1 -2
  50. package/dist/cookie/utils.js +2 -23
  51. package/dist/cookie/utils.mjs +5 -25
  52. package/dist/handler/error.js +2 -2
  53. package/dist/handler/error.mjs +3 -3
  54. package/dist/handler/fetch.js +49 -42
  55. package/dist/handler/fetch.mjs +50 -43
  56. package/dist/index.d.ts +13 -8
  57. package/dist/index.js +11 -4
  58. package/dist/index.mjs +7 -6
  59. package/dist/parse-query.js +1 -1
  60. package/dist/parse-query.mjs +1 -1
  61. package/dist/plugin/bun.d.ts +10 -0
  62. package/dist/plugin/bun.js +18 -1
  63. package/dist/plugin/bun.mjs +19 -2
  64. package/dist/plugin/core.d.ts +61 -1
  65. package/dist/plugin/core.js +90 -10
  66. package/dist/plugin/core.mjs +90 -12
  67. package/dist/plugin/esbuild.d.ts +10 -0
  68. package/dist/plugin/esbuild.js +18 -1
  69. package/dist/plugin/esbuild.mjs +19 -2
  70. package/dist/plugin/source.d.ts +38 -1
  71. package/dist/plugin/source.js +104 -13
  72. package/dist/plugin/source.mjs +102 -14
  73. package/dist/plugin/vite.d.ts +7 -0
  74. package/dist/plugin/vite.js +22 -1
  75. package/dist/plugin/vite.mjs +23 -2
  76. package/dist/sucrose.js +23 -10
  77. package/dist/sucrose.mjs +23 -10
  78. package/dist/trace.d.ts +1 -1
  79. package/dist/trace.js +4 -6
  80. package/dist/trace.mjs +4 -6
  81. package/dist/type/bridge.d.ts +9 -8
  82. package/dist/type/coerce.d.ts +20 -2
  83. package/dist/type/coerce.js +110 -0
  84. package/dist/type/coerce.mjs +109 -2
  85. package/dist/type/compat.js +4 -3
  86. package/dist/type/compat.mjs +2 -1
  87. package/dist/type/constants.d.ts +1 -1
  88. package/dist/type/elysia/boolean-string.js +1 -1
  89. package/dist/type/elysia/boolean-string.mjs +1 -1
  90. package/dist/type/elysia/file-type.js +1 -1
  91. package/dist/type/elysia/file-type.mjs +1 -1
  92. package/dist/type/elysia/integer-string.js +1 -1
  93. package/dist/type/elysia/integer-string.mjs +1 -1
  94. package/dist/type/exports.js +29 -2059
  95. package/dist/type/exports.mjs +7 -295
  96. package/dist/type/index.d.ts +6 -2
  97. package/dist/type/index.js +4 -0
  98. package/dist/type/index.mjs +3 -1
  99. package/dist/type/types.d.ts +1 -1
  100. package/dist/type/utils.js +4 -4
  101. package/dist/type/utils.mjs +4 -4
  102. package/dist/type/validator/custom-error.d.ts +10 -0
  103. package/dist/type/validator/custom-error.js +113 -0
  104. package/dist/type/validator/custom-error.mjs +110 -0
  105. package/dist/type/validator/default-precompute.d.ts +22 -0
  106. package/dist/type/validator/default-precompute.js +476 -0
  107. package/dist/type/validator/default-precompute.mjs +465 -0
  108. package/dist/type/validator/frozen-check.d.ts +11 -0
  109. package/dist/type/validator/frozen-check.js +19 -0
  110. package/dist/type/validator/frozen-check.mjs +18 -0
  111. package/dist/type/{validator.d.ts → validator/index.d.ts} +7 -20
  112. package/dist/type/validator/index.js +572 -0
  113. package/dist/type/validator/index.mjs +567 -0
  114. package/dist/type/validator/string-codec-aot.d.ts +9 -0
  115. package/dist/type/validator/string-codec-aot.js +106 -0
  116. package/dist/type/validator/string-codec-aot.mjs +102 -0
  117. package/dist/type/validator/validator-cache.d.ts +21 -0
  118. package/dist/type/validator/validator-cache.js +163 -0
  119. package/dist/type/validator/validator-cache.mjs +161 -0
  120. package/dist/types.d.ts +22 -14
  121. package/dist/universal/file.d.ts +1 -1
  122. package/dist/universal/file.js +35 -29
  123. package/dist/universal/file.mjs +35 -29
  124. package/dist/utils.d.ts +12 -1
  125. package/dist/utils.js +89 -45
  126. package/dist/utils.mjs +88 -46
  127. package/dist/validator/index.d.ts +1 -1
  128. package/dist/validator/index.js +15 -2
  129. package/dist/validator/index.mjs +15 -2
  130. package/dist/validator/route.d.ts +0 -1
  131. package/dist/ws/route.js +47 -24
  132. package/dist/ws/route.mjs +48 -25
  133. package/package.json +10 -4
  134. package/dist/type/validator.js +0 -1033
  135. package/dist/type/validator.mjs +0 -1029
@@ -0,0 +1,19 @@
1
+ //#region src/compile/jit-probe.d.ts
2
+ /** A handler-JIT entry point the frozen replay can trip over. */
3
+ type JITProbeReason = 'sucrose' | 'handler:new-function';
4
+ interface JITProbeResult {
5
+ /** True when the frozen replay never touched handler JIT. */
6
+ stubbable: boolean;
7
+ /** `sucrose` + the handler `new Function` codegen is unused. */
8
+ jit: boolean;
9
+ count: number;
10
+ reasons: JITProbeReason[];
11
+ }
12
+ declare abstract class JITProbe {
13
+ static isProbing(): boolean;
14
+ static record(reason: JITProbeReason): void;
15
+ static begin(): void;
16
+ static end(): JITProbeResult;
17
+ }
18
+ //#endregion
19
+ export { JITProbe, JITProbeReason, JITProbeResult };
@@ -0,0 +1,38 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
+
3
+ //#region src/compile/jit-probe.ts
4
+ let armed = false;
5
+ let count = 0;
6
+ const reasons = /* @__PURE__ */ new Set();
7
+ const HANDLER_REASONS = new Set(["sucrose", "handler:new-function"]);
8
+ var JITProbe = class {
9
+ static isProbing() {
10
+ return armed;
11
+ }
12
+ static record(reason) {
13
+ if (!armed) return;
14
+ count++;
15
+ reasons.add(reason);
16
+ }
17
+ static begin() {
18
+ armed = true;
19
+ count = 0;
20
+ reasons.clear();
21
+ }
22
+ static end() {
23
+ armed = false;
24
+ const fired = [...reasons];
25
+ const result = {
26
+ stubbable: count === 0,
27
+ jit: !fired.some((r) => HANDLER_REASONS.has(r)),
28
+ count,
29
+ reasons: fired
30
+ };
31
+ count = 0;
32
+ reasons.clear();
33
+ return result;
34
+ }
35
+ };
36
+
37
+ //#endregion
38
+ exports.JITProbe = JITProbe;
@@ -0,0 +1,36 @@
1
+ //#region src/compile/jit-probe.ts
2
+ let armed = false;
3
+ let count = 0;
4
+ const reasons = /* @__PURE__ */ new Set();
5
+ const HANDLER_REASONS = new Set(["sucrose", "handler:new-function"]);
6
+ var JITProbe = class {
7
+ static isProbing() {
8
+ return armed;
9
+ }
10
+ static record(reason) {
11
+ if (!armed) return;
12
+ count++;
13
+ reasons.add(reason);
14
+ }
15
+ static begin() {
16
+ armed = true;
17
+ count = 0;
18
+ reasons.clear();
19
+ }
20
+ static end() {
21
+ armed = false;
22
+ const fired = [...reasons];
23
+ const result = {
24
+ stubbable: count === 0,
25
+ jit: !fired.some((r) => HANDLER_REASONS.has(r)),
26
+ count,
27
+ reasons: fired
28
+ };
29
+ count = 0;
30
+ reasons.clear();
31
+ return result;
32
+ }
33
+ };
34
+
35
+ //#endregion
36
+ export { JITProbe };
@@ -1,4 +1,5 @@
1
- import { CookieOptions } from "./types.js";
1
+ import { BaseCookie, CookieOptions } from "./types.js";
2
2
  import { Cookie } from "./cookie.js";
3
- import { createCookieJar, parseCookie, serializeCookie, signCookie, unsignCookie } from "./utils.js";
4
- export { Cookie, type CookieOptions, createCookieJar, parseCookie, serializeCookie, signCookie, unsignCookie };
3
+ import { serializeCookie } from "./serialize.js";
4
+ import { createCookieJar, parseCookie, signCookie, unsignCookie } from "./utils.js";
5
+ export { type BaseCookie, Cookie, type CookieOptions, createCookieJar, parseCookie, serializeCookie, signCookie, unsignCookie };
@@ -1,10 +1,11 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
+ const require_cookie_serialize = require('./serialize.js');
2
3
  const require_cookie_cookie = require('./cookie.js');
3
4
  const require_cookie_utils = require('./utils.js');
4
5
 
5
6
  exports.Cookie = require_cookie_cookie.Cookie;
6
7
  exports.createCookieJar = require_cookie_utils.createCookieJar;
7
8
  exports.parseCookie = require_cookie_utils.parseCookie;
8
- exports.serializeCookie = require_cookie_utils.serializeCookie;
9
+ exports.serializeCookie = require_cookie_serialize.serializeCookie;
9
10
  exports.signCookie = require_cookie_utils.signCookie;
10
11
  exports.unsignCookie = require_cookie_utils.unsignCookie;
@@ -1,4 +1,5 @@
1
+ import { serializeCookie } from "./serialize.mjs";
1
2
  import { Cookie } from "./cookie.mjs";
2
- import { createCookieJar, parseCookie, serializeCookie, signCookie, unsignCookie } from "./utils.mjs";
3
+ import { createCookieJar, parseCookie, signCookie, unsignCookie } from "./utils.mjs";
3
4
 
4
5
  export { Cookie, createCookieJar, parseCookie, serializeCookie, signCookie, unsignCookie };
@@ -0,0 +1,6 @@
1
+ import { Context } from "../context.js";
2
+
3
+ //#region src/cookie/serialize.d.ts
4
+ declare function serializeCookie(cookies: Context['set']['cookie']): string | string[] | undefined;
5
+ //#endregion
6
+ export { serializeCookie };
@@ -0,0 +1,28 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
+ const require_utils = require('../utils.js');
3
+ const require_cookie_lib = require('./lib.js');
4
+
5
+ //#region src/cookie/serialize.ts
6
+ function serializeCookie(cookies) {
7
+ if (!cookies || !require_utils.isNotEmpty(cookies)) return;
8
+ let set;
9
+ let isArray = false;
10
+ for (const key in cookies) {
11
+ if (!key) continue;
12
+ const property = cookies[key];
13
+ if (!property) continue;
14
+ const value = property.value;
15
+ if (value === void 0 || value === null) continue;
16
+ const v = require_cookie_lib.serialize(key, value, property);
17
+ if (set) if (isArray) set.push(v);
18
+ else {
19
+ set = [set, v];
20
+ isArray = true;
21
+ }
22
+ else set = v;
23
+ }
24
+ return set;
25
+ }
26
+
27
+ //#endregion
28
+ exports.serializeCookie = serializeCookie;
@@ -0,0 +1,27 @@
1
+ import { isNotEmpty } from "../utils.mjs";
2
+ import { serialize } from "./lib.mjs";
3
+
4
+ //#region src/cookie/serialize.ts
5
+ function serializeCookie(cookies) {
6
+ if (!cookies || !isNotEmpty(cookies)) return;
7
+ let set;
8
+ let isArray = false;
9
+ for (const key in cookies) {
10
+ if (!key) continue;
11
+ const property = cookies[key];
12
+ if (!property) continue;
13
+ const value = property.value;
14
+ if (value === void 0 || value === null) continue;
15
+ const v = serialize(key, value, property);
16
+ if (set) if (isArray) set.push(v);
17
+ else {
18
+ set = [set, v];
19
+ isArray = true;
20
+ }
21
+ else set = v;
22
+ }
23
+ return set;
24
+ }
25
+
26
+ //#endregion
27
+ export { serializeCookie };
@@ -12,8 +12,7 @@ declare function parseCookieRawSync(cookieString: string | null | undefined, _co
12
12
  declare function parseCookieRaw(cookieString: string | null | undefined, config: CompiledCookieConfig): Promise<Record<string, unknown>>;
13
13
  declare function buildCookieJar(set: Context['set'], raw: Record<string, unknown>, config: CompiledCookieConfig): Record<string, Cookie<unknown>>;
14
14
  declare function signCookieValues(cookies: Context['set']['cookie'] | undefined, config: CompiledCookieConfig): Promise<void> | undefined;
15
- declare function serializeCookie(cookies: Context['set']['cookie']): string | string[] | undefined;
16
15
  declare function signCookie(val: string, secret: string | null): Promise<string>;
17
16
  declare function unsignCookie(input: string, secret: string | null): Promise<string | false>;
18
17
  //#endregion
19
- export { buildCookieJar, createCookieJar, parseCookie, parseCookieRaw, parseCookieRawSync, serializeCookie, signCookie, signCookieValues, unsignCookie };
18
+ export { buildCookieJar, createCookieJar, parseCookie, parseCookieRaw, parseCookieRawSync, signCookie, signCookieValues, unsignCookie };
@@ -3,8 +3,8 @@ const require_runtime = require('../_virtual/_rolldown/runtime.js');
3
3
  const require_constants = require('../constants.js');
4
4
  const require_utils = require('../utils.js');
5
5
  const require_cookie_lib = require('./lib.js');
6
- const require_cookie_cookie = require('./cookie.js');
7
6
  const require_error = require('../error.js');
7
+ const require_cookie_cookie = require('./cookie.js');
8
8
  const require_cookie_config = require('./config.js');
9
9
  let deuri = require("deuri");
10
10
 
@@ -104,7 +104,7 @@ function signCookieValues(cookies, config) {
104
104
  if (value === void 0 || value === null) continue;
105
105
  if (typeof value === "object") value = JSON.stringify(value);
106
106
  else if (typeof value !== "string") value = value + "";
107
- const secret = Array.isArray(r.secrets) ? r.secrets.find((s) => s !== null) ?? null : r.secrets;
107
+ const secret = Array.isArray(r.secrets) ? r.secrets[0] ?? null : r.secrets;
108
108
  if (secret === null) throw new TypeError(`Cookie field "${key}" is signed but no \`secrets\` is provided.`);
109
109
  (pending ??= []).push([
110
110
  property,
@@ -121,26 +121,6 @@ async function signPending(pending) {
121
121
  property.value = await signCookie(value, secret);
122
122
  }
123
123
  }
124
- function serializeCookie(cookies) {
125
- if (!cookies || !require_utils.isNotEmpty(cookies)) return;
126
- let set;
127
- let isArray = false;
128
- for (const key in cookies) {
129
- if (!key) continue;
130
- const property = cookies[key];
131
- if (!property) continue;
132
- const value = property.value;
133
- if (value === void 0 || value === null) continue;
134
- const v = require_cookie_lib.serialize(key, value, property);
135
- if (set) if (isArray) set.push(v);
136
- else {
137
- set = [set, v];
138
- isArray = true;
139
- }
140
- else set = v;
141
- }
142
- return set;
143
- }
144
124
  const removeTrailingEquals = /=+$/g;
145
125
  const algorithm = {
146
126
  name: "HMAC",
@@ -184,7 +164,6 @@ exports.createCookieJar = createCookieJar;
184
164
  exports.parseCookie = parseCookie;
185
165
  exports.parseCookieRaw = parseCookieRaw;
186
166
  exports.parseCookieRawSync = parseCookieRawSync;
187
- exports.serializeCookie = serializeCookie;
188
167
  exports.signCookie = signCookie;
189
168
  exports.signCookieValues = signCookieValues;
190
169
  exports.unsignCookie = unsignCookie;
@@ -1,8 +1,8 @@
1
1
  import { dangerousKeys } from "../constants.mjs";
2
- import { constantTimeEqual, isNotEmpty, nullObject } from "../utils.mjs";
3
- import { parse, serialize } from "./lib.mjs";
4
- import { Cookie } from "./cookie.mjs";
2
+ import { constantTimeEqual, nullObject } from "../utils.mjs";
3
+ import { parse } from "./lib.mjs";
5
4
  import { InvalidCookieSignature } from "../error.mjs";
5
+ import { Cookie } from "./cookie.mjs";
6
6
  import { compileCookieConfig, isCookieSigned } from "./config.mjs";
7
7
  import { decodeComponent } from "deuri";
8
8
 
@@ -102,7 +102,7 @@ function signCookieValues(cookies, config) {
102
102
  if (value === void 0 || value === null) continue;
103
103
  if (typeof value === "object") value = JSON.stringify(value);
104
104
  else if (typeof value !== "string") value = value + "";
105
- const secret = Array.isArray(r.secrets) ? r.secrets.find((s) => s !== null) ?? null : r.secrets;
105
+ const secret = Array.isArray(r.secrets) ? r.secrets[0] ?? null : r.secrets;
106
106
  if (secret === null) throw new TypeError(`Cookie field "${key}" is signed but no \`secrets\` is provided.`);
107
107
  (pending ??= []).push([
108
108
  property,
@@ -119,26 +119,6 @@ async function signPending(pending) {
119
119
  property.value = await signCookie(value, secret);
120
120
  }
121
121
  }
122
- function serializeCookie(cookies) {
123
- if (!cookies || !isNotEmpty(cookies)) return;
124
- let set;
125
- let isArray = false;
126
- for (const key in cookies) {
127
- if (!key) continue;
128
- const property = cookies[key];
129
- if (!property) continue;
130
- const value = property.value;
131
- if (value === void 0 || value === null) continue;
132
- const v = serialize(key, value, property);
133
- if (set) if (isArray) set.push(v);
134
- else {
135
- set = [set, v];
136
- isArray = true;
137
- }
138
- else set = v;
139
- }
140
- return set;
141
- }
142
122
  const removeTrailingEquals = /=+$/g;
143
123
  const algorithm = {
144
124
  name: "HMAC",
@@ -177,4 +157,4 @@ async function unsignCookie(input, secret) {
177
157
  }
178
158
 
179
159
  //#endregion
180
- export { buildCookieJar, createCookieJar, parseCookie, parseCookieRaw, parseCookieRawSync, serializeCookie, signCookie, signCookieValues, unsignCookie };
160
+ export { buildCookieJar, createCookieJar, parseCookie, parseCookieRaw, parseCookieRawSync, signCookie, signCookieValues, unsignCookie };
@@ -18,10 +18,10 @@ function fallbackResponse(context, error, mapResponse, defaultError) {
18
18
  return fallbackErrorResponse(context, error, mapResponse, defaultError);
19
19
  }
20
20
  function fallbackErrorResponse(context, error, mapResponse, defaultError) {
21
- if (error?.status) return mapResponse(error.response !== void 0 ? error.response : error.message ?? "", context.set, context);
21
+ if (error?.status) return mapResponse(error.response !== void 0 ? error.response : require_error.isProduction() && error.status >= 500 ? "Internal Server Error" : error.message ?? "", context.set, context);
22
22
  if (error?.message != null) {
23
23
  if (context.set.status === void 0 || context.set.status === 200) context.set.status = 500;
24
- return mapResponse(error.message, context.set, context);
24
+ return mapResponse(require_error.isProduction() ? "Internal Server Error" : error.message, context.set, context);
25
25
  }
26
26
  return defaultError ? defaultError.clone() : getDefaultError();
27
27
  }
@@ -1,4 +1,4 @@
1
- import { ElysiaStatus, ValidationError } from "../error.mjs";
1
+ import { ElysiaStatus, ValidationError, isProduction } from "../error.mjs";
2
2
  import { parseQueryFromURL } from "../parse-query.mjs";
3
3
  import { cachedResponse, getAsyncIndexes } from "./utils.mjs";
4
4
 
@@ -17,10 +17,10 @@ function fallbackResponse(context, error, mapResponse, defaultError) {
17
17
  return fallbackErrorResponse(context, error, mapResponse, defaultError);
18
18
  }
19
19
  function fallbackErrorResponse(context, error, mapResponse, defaultError) {
20
- if (error?.status) return mapResponse(error.response !== void 0 ? error.response : error.message ?? "", context.set, context);
20
+ if (error?.status) return mapResponse(error.response !== void 0 ? error.response : isProduction() && error.status >= 500 ? "Internal Server Error" : error.message ?? "", context.set, context);
21
21
  if (error?.message != null) {
22
22
  if (context.set.status === void 0 || context.set.status === 200) context.set.status = 500;
23
- return mapResponse(error.message, context.set, context);
23
+ return mapResponse(isProduction() ? "Internal Server Error" : error.message, context.set, context);
24
24
  }
25
25
  return defaultError ? defaultError.clone() : getDefaultError();
26
26
  }
@@ -1,13 +1,13 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
2
  const require_runtime = require('../_virtual/_rolldown/runtime.js');
3
3
  const require_utils = require('../utils.js');
4
- const require_error = require('../error.js');
5
4
  const require_adapter_utils = require('../adapter/utils.js');
5
+ const require_error = require('../error.js');
6
6
  const require_context = require('../context.js');
7
- const require_adapter_constants = require('../adapter/constants.js');
8
7
  const require_handler_utils = require('./utils.js');
9
- const require_handler_error = require('./error.js');
10
8
  const require_trace = require('../trace.js');
9
+ const require_adapter_constants = require('../adapter/constants.js');
10
+ const require_handler_error = require('./error.js');
11
11
  let deuri = require("deuri");
12
12
 
13
13
  //#region src/handler/fetch.ts
@@ -30,11 +30,17 @@ const decodeParams = (params) => {
30
30
  }
31
31
  return params;
32
32
  };
33
- const catchError = (context, handleError, afterResponse) => (error) => {
33
+ function finalizeError(context, handleError, afterResponse, error) {
34
34
  const resp = handleError(context, error);
35
- afterResponse?.(context);
35
+ if (!afterResponse) return resp;
36
+ if (resp instanceof Promise) return resp.then((r) => {
37
+ afterResponse(context);
38
+ return r;
39
+ });
40
+ afterResponse(context);
36
41
  return resp;
37
- };
42
+ }
43
+ const catchError = (context, handleError, afterResponse) => (error) => finalizeError(context, handleError, afterResponse, error);
38
44
  function findRoute(context, request, map, router, hasError, handleError, afterResponse, strictPath, hasWS) {
39
45
  const path = context.path;
40
46
  if (hasWS) {
@@ -52,17 +58,23 @@ function findRoute(context, request, map, router, hasError, handleError, afterRe
52
58
  return r instanceof Promise ? r.catch(catchError(context, handleError, afterResponse)) : r;
53
59
  }
54
60
  }
55
- } else {
56
- const handler = map[request.method]?.[path] ?? map["*"]?.[path];
61
+ }
62
+ {
63
+ const methodMap = map[request.method];
64
+ let handler = methodMap?.[path];
65
+ if (!handler) if (!strictPath && path.length > 1 && path.charCodeAt(path.length - 1) === 47) {
66
+ const loose = path.slice(0, -1);
67
+ handler = methodMap?.[loose];
68
+ if (!handler) {
69
+ const anyMap = map["*"];
70
+ handler = anyMap?.[path] ?? anyMap?.[loose];
71
+ }
72
+ } else handler = map["*"]?.[path];
57
73
  if (handler) {
58
74
  const r = handler(context);
59
75
  return r instanceof Promise ? r.catch(catchError(context, handleError, afterResponse)) : r;
60
76
  }
61
- let found = router?.find(request.method, path) ?? router?.find("*", path);
62
- if (!found && !strictPath) {
63
- const loose = require_utils.getLoosePath(path);
64
- if (loose !== path) found = router?.find(request.method, loose) ?? router?.find("*", loose);
65
- }
77
+ const found = router?.find(request.method, path) ?? router?.find("*", path);
66
78
  if (found) {
67
79
  context.params = decodeParams(found.params);
68
80
  const r = found.store(context);
@@ -140,25 +152,27 @@ function createFetchHandler(app) {
140
152
  const trace = new Array(traceLength);
141
153
  for (let i = 0; i < traceLength; i++) trace[i] = tracerFactories[i](context);
142
154
  context.trace = trace;
143
- const requestReports = trace.map((c) => c.request({
155
+ const requestReports = new Array(traceLength);
156
+ for (let i = 0; i < traceLength; i++) requestReports[i] = trace[i].request({
144
157
  id: context.rid,
145
158
  event: "request",
146
159
  name: "request",
147
160
  begin: performance.now(),
148
161
  total: onRequests.length
149
- }));
162
+ });
150
163
  try {
151
164
  for (let i = 0; i < onRequests.length; i++) {
152
- const endReports = requestReports.map((r) => r.resolveChild?.shift?.()?.({
165
+ const endReports = new Array(traceLength);
166
+ for (let j = 0; j < traceLength; j++) endReports[j] = requestReports[j].resolveChild?.shift?.()?.({
153
167
  id: context.rid,
154
168
  event: "request",
155
169
  name: onRequests[i].name || "anonymous",
156
170
  begin: performance.now()
157
- }));
171
+ });
158
172
  const result = asyncIndexes?.[i] ? await onRequests[i](context) : onRequests[i](context);
159
173
  for (let i = 0; i < traceLength; i++) endReports[i]?.();
160
174
  if (result !== void 0) {
161
- requestReports.forEach((r) => r.resolve());
175
+ for (let j = 0; j < traceLength; j++) requestReports[j].resolve();
162
176
  const response = mapResponse(result, context.set);
163
177
  afterResponse?.(context);
164
178
  return response;
@@ -168,9 +182,7 @@ function createFetchHandler(app) {
168
182
  return await findRoute(context, request, map, router, hasError, handleError, afterResponse, strictPath, hasWS);
169
183
  } catch (error) {
170
184
  for (let i = 0; i < traceLength; i++) requestReports[i].resolve(error);
171
- const r = handleError(context, error);
172
- afterResponse?.(context);
173
- return r;
185
+ return finalizeError(context, handleError, afterResponse, error);
174
186
  }
175
187
  };
176
188
  }
@@ -192,9 +204,7 @@ function createFetchHandler(app) {
192
204
  }
193
205
  return findRoute(context, request, map, router, hasError, handleError, afterResponse, strictPath, hasWS);
194
206
  } catch (error) {
195
- const r = handleError(context, error);
196
- afterResponse?.(context);
197
- return r;
207
+ return finalizeError(context, handleError, afterResponse, error);
198
208
  }
199
209
  };
200
210
  return (request) => {
@@ -212,7 +222,7 @@ function createFetchHandler(app) {
212
222
  }
213
223
  return findRoute(context, request, map, router, hasError, handleError, afterResponse, strictPath, hasWS);
214
224
  } catch (error) {
215
- return handleError(context, error);
225
+ return finalizeError(context, handleError, afterResponse, error);
216
226
  }
217
227
  };
218
228
  }
@@ -236,38 +246,35 @@ function createFetchHandler(app) {
236
246
  return r instanceof Promise ? r.catch(catchError(context, handleError, afterResponse)) : r;
237
247
  }
238
248
  } catch (error) {
239
- const r = handleError(context, error);
240
- afterResponse?.(context);
241
- return r;
249
+ return finalizeError(context, handleError, afterResponse, error);
242
250
  }
243
251
  }
244
252
  }
245
- const handler = map[request.method]?.[path] ?? map["*"]?.[path];
253
+ const methodMap = map[request.method];
254
+ let handler = methodMap?.[path];
246
255
  try {
256
+ if (!handler) if (!strictPath && path.length > 1 && path.charCodeAt(path.length - 1) === 47) {
257
+ const loose = path.slice(0, -1);
258
+ handler = methodMap?.[loose];
259
+ if (!handler) {
260
+ const anyMap = map["*"];
261
+ handler = anyMap?.[path] ?? anyMap?.[loose];
262
+ }
263
+ } else handler = map["*"]?.[path];
247
264
  if (handler) {
248
265
  const r = handler(context);
249
266
  return r instanceof Promise ? r.catch(catchError(context, handleError, afterResponse)) : r;
250
267
  }
251
- let result = router?.find(request.method, path) ?? router?.find("*", path);
252
- if (!result && !strictPath) {
253
- const loose = require_utils.getLoosePath(path);
254
- if (loose !== path) result = router?.find(request.method, loose) ?? router?.find("*", loose);
255
- }
268
+ const result = router?.find(request.method, path) ?? router?.find("*", path);
256
269
  if (result) {
257
270
  context.params = decodeParams(result.params);
258
271
  const r = result.store(context);
259
272
  return r instanceof Promise ? r.catch(catchError(context, handleError, afterResponse)) : r;
260
273
  }
261
274
  } catch (error) {
262
- const r = handleError(context, error);
263
- afterResponse?.(context);
264
- return r;
265
- }
266
- if (hasError) {
267
- const r = handleError(context, new require_error.NotFound());
268
- afterResponse?.(context);
269
- return r;
275
+ return finalizeError(context, handleError, afterResponse, error);
270
276
  }
277
+ if (hasError) return finalizeError(context, handleError, afterResponse, new require_error.NotFound());
271
278
  afterResponse?.(context, 404);
272
279
  return notFound(context);
273
280
  };