elysia 2.0.0-exp.50 → 2.0.0-exp.51

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 (72) hide show
  1. package/dist/adapter/bun/index.js +24 -2
  2. package/dist/adapter/bun/index.mjs +24 -2
  3. package/dist/adapter/origin.d.ts +59 -0
  4. package/dist/adapter/origin.js +60 -0
  5. package/dist/adapter/origin.mjs +58 -0
  6. package/dist/base.js +283 -273
  7. package/dist/base.mjs +284 -274
  8. package/dist/compile/analysis-cache.js +0 -2
  9. package/dist/compile/analysis-cache.mjs +0 -2
  10. package/dist/compile/aot.js +2 -1
  11. package/dist/compile/aot.mjs +2 -1
  12. package/dist/compile/handler/descriptor.d.ts +3 -6
  13. package/dist/compile/handler/descriptor.js +5 -7
  14. package/dist/compile/handler/descriptor.mjs +5 -7
  15. package/dist/compile/handler/index.js +16 -9
  16. package/dist/compile/handler/index.mjs +18 -11
  17. package/dist/compile/handler/jit.d.ts +3 -1
  18. package/dist/compile/handler/jit.js +47 -35
  19. package/dist/compile/handler/jit.mjs +47 -36
  20. package/dist/compile/handler/utils.d.ts +3 -2
  21. package/dist/compile/handler/utils.js +9 -5
  22. package/dist/compile/handler/utils.mjs +9 -5
  23. package/dist/compile/lexer.d.ts +1 -1
  24. package/dist/cookie/crypto.js +15 -10
  25. package/dist/cookie/crypto.mjs +15 -10
  26. package/dist/handler/fetch.js +29 -6
  27. package/dist/handler/fetch.mjs +29 -6
  28. package/dist/package.js +1 -1
  29. package/dist/package.mjs +1 -1
  30. package/dist/plugin/aot/core.js +1 -1
  31. package/dist/plugin/aot/core.mjs +1 -1
  32. package/dist/route-table.d.ts +1 -1
  33. package/dist/route-table.js +3 -3
  34. package/dist/route-table.mjs +3 -3
  35. package/dist/schema-snapshot.d.ts +4 -0
  36. package/dist/schema-snapshot.js +193 -33
  37. package/dist/schema-snapshot.mjs +194 -33
  38. package/dist/sucrose.js +64 -44
  39. package/dist/sucrose.mjs +64 -44
  40. package/dist/type/bridge-live.d.ts +2 -2
  41. package/dist/type/bridge-live.js +1 -0
  42. package/dist/type/bridge-live.mjs +2 -2
  43. package/dist/type/bridge.d.ts +10 -8
  44. package/dist/type/bridge.js +8 -0
  45. package/dist/type/bridge.mjs +3 -1
  46. package/dist/type/compat.js +1 -0
  47. package/dist/type/compat.mjs +2 -1
  48. package/dist/type/constants.d.ts +1 -1
  49. package/dist/type/elysia/boolean-string.js +2 -1
  50. package/dist/type/elysia/boolean-string.mjs +2 -1
  51. package/dist/type/elysia/integer-string.js +4 -2
  52. package/dist/type/elysia/integer-string.mjs +4 -2
  53. package/dist/type/elysia/numeric.js +29 -3
  54. package/dist/type/elysia/numeric.mjs +29 -3
  55. package/dist/type/shared.d.ts +10 -1
  56. package/dist/type/shared.js +18 -0
  57. package/dist/type/shared.mjs +17 -1
  58. package/dist/type/utils.js +10 -1
  59. package/dist/type/utils.mjs +10 -1
  60. package/dist/type/validator/index.d.ts +3 -3
  61. package/dist/type/validator/index.js +5 -4
  62. package/dist/type/validator/index.mjs +7 -7
  63. package/dist/type/validator/validator-cache.d.ts +11 -5
  64. package/dist/type/validator/validator-cache.js +153 -53
  65. package/dist/type/validator/validator-cache.mjs +150 -53
  66. package/dist/types.d.ts +11 -1
  67. package/dist/utils.d.ts +9 -5
  68. package/dist/utils.js +47 -43
  69. package/dist/utils.mjs +47 -43
  70. package/dist/validator/route.js +2 -1
  71. package/dist/validator/route.mjs +3 -2
  72. package/package.json +1 -1
@@ -6,8 +6,30 @@ const require_adapter_web_standard_index = require('../web-standard/index.js');
6
6
  const require_generation = require('../../generation.js');
7
7
  const require_compile_handler_index = require('../../compile/handler/index.js');
8
8
  const require_route_table = require('../../route-table.js');
9
+ const require_adapter_origin = require('../origin.js');
9
10
 
10
11
  //#region src/adapter/bun/index.ts
12
+ /**
13
+ * ! This may looks like it would cause race condition, but it is not
14
+ * Bun is single-threaded and synchronous, so the `finally` block will
15
+ * always run before the next request comes in
16
+ * @see ../origin.ts
17
+ *
18
+ * Publish the request `Bun.serve` handed us so `createFetchHandler` can prove,
19
+ * before its first suspension, that it holds the untouched original and may
20
+ * defer materializing `request.signal`
21
+ *
22
+ * `finally` runs on the synchronous return of `fetch` (the returned promise is
23
+ * not awaited), so the slot is live only for the handler's synchronous prologue.
24
+ */
25
+ const withOrigin = (fetch) => (request, server) => {
26
+ require_adapter_origin.origin.request = request;
27
+ try {
28
+ return fetch(request, server);
29
+ } finally {
30
+ require_adapter_origin.origin.request = void 0;
31
+ }
32
+ };
11
33
  const nativeStaticMethods = new Set([
12
34
  "GET",
13
35
  "POST",
@@ -124,7 +146,7 @@ const BunAdapter = require_adapter_index.createAdapter({
124
146
  let built;
125
147
  if (!needsGate) built = build();
126
148
  if (needsGate) serve.fetch = (request, server) => ready.then(() => app.fetch(request, server));
127
- else serve.fetch = built.fetch;
149
+ else serve.fetch = withOrigin(built.fetch);
128
150
  const server = app.server = Bun.serve(serve);
129
151
  const reload = () => {
130
152
  try {
@@ -172,7 +194,7 @@ const BunAdapter = require_adapter_index.createAdapter({
172
194
  const publish = () => {
173
195
  if (app.server !== server) return;
174
196
  built ??= build();
175
- serve.fetch = built.fetch;
197
+ serve.fetch = withOrigin(built.fetch);
176
198
  if (built.websocket) serve.websocket = built.websocket;
177
199
  if (built.routes) serve.routes = built.routes[0];
178
200
  if (needsGate || built.websocket || built.routes) reload();
@@ -5,8 +5,30 @@ import { WebStandardAdapter } from "../web-standard/index.mjs";
5
5
  import { frozenRootOf, resolvedWsOf } from "../../generation.mjs";
6
6
  import { buildNativeStaticResponse } from "../../compile/handler/index.mjs";
7
7
  import { routeRow } from "../../route-table.mjs";
8
+ import { origin } from "../origin.mjs";
8
9
 
9
10
  //#region src/adapter/bun/index.ts
11
+ /**
12
+ * ! This may looks like it would cause race condition, but it is not
13
+ * Bun is single-threaded and synchronous, so the `finally` block will
14
+ * always run before the next request comes in
15
+ * @see ../origin.ts
16
+ *
17
+ * Publish the request `Bun.serve` handed us so `createFetchHandler` can prove,
18
+ * before its first suspension, that it holds the untouched original and may
19
+ * defer materializing `request.signal`
20
+ *
21
+ * `finally` runs on the synchronous return of `fetch` (the returned promise is
22
+ * not awaited), so the slot is live only for the handler's synchronous prologue.
23
+ */
24
+ const withOrigin = (fetch) => (request, server) => {
25
+ origin.request = request;
26
+ try {
27
+ return fetch(request, server);
28
+ } finally {
29
+ origin.request = void 0;
30
+ }
31
+ };
10
32
  const nativeStaticMethods = new Set([
11
33
  "GET",
12
34
  "POST",
@@ -123,7 +145,7 @@ const BunAdapter = createAdapter({
123
145
  let built;
124
146
  if (!needsGate) built = build();
125
147
  if (needsGate) serve.fetch = (request, server) => ready.then(() => app.fetch(request, server));
126
- else serve.fetch = built.fetch;
148
+ else serve.fetch = withOrigin(built.fetch);
127
149
  const server = app.server = Bun.serve(serve);
128
150
  const reload = () => {
129
151
  try {
@@ -171,7 +193,7 @@ const BunAdapter = createAdapter({
171
193
  const publish = () => {
172
194
  if (app.server !== server) return;
173
195
  built ??= build();
174
- serve.fetch = built.fetch;
196
+ serve.fetch = withOrigin(built.fetch);
175
197
  if (built.websocket) serve.websocket = built.websocket;
176
198
  if (built.routes) serve.routes = built.routes[0];
177
199
  if (needsGate || built.websocket || built.routes) reload();
@@ -0,0 +1,59 @@
1
+ //#region src/adapter/origin.d.ts
2
+ /**
3
+ * Provenance channel for deferred abort-signal arming.
4
+ *
5
+ * Bun materializes `Request.signal` lazily (~214ns on the first read), so
6
+ * Elysia only pays for it once the pipeline can actually observe an abort,
7
+ * that is, after it suspends. Deferring that read is only sound for the exact
8
+ * `Request` instance `Bun.serve` handed to `fetch`: any other request
9
+ * (`app.handle`, a non-Bun adapter, or a `.wrap()` HOC that substitutes the
10
+ * request) may carry a user-controlled signal that is already aborted at entry
11
+ * and therefore must be observed synchronously.
12
+ *
13
+ * The Bun adapter publishes the untouched original here for the *synchronous*
14
+ * prologue of `app.fetch` and clears it in `finally`, so the window closes
15
+ * before the first suspension and cannot leak across concurrent requests.
16
+ * A `.wrap()` HOC that replaces the request or defers calling `next()` past a
17
+ * microtask simply misses the window, fetch handler falls back to eager check
18
+ *
19
+ * ## "A single shared slot that every request writes to, so a race condition?"
20
+ *
21
+ * It looks like one, but it can't be. Explained like you're five:
22
+ *
23
+ * JavaScript only ever does ONE thing at a time. When a request comes in, the
24
+ * adapter writes its name on the whiteboard (`origin.request = request`),
25
+ * walks it down the hall (`fetch(request)` the synchronous part, where the
26
+ * one and only identity check happens), and erases the whiteboard (`finally`)
27
+ * all in one uninterruptible turn. The next request's turn CANNOT start
28
+ * until this turn is completely finished, because that is how the event loop
29
+ * works: run-to-completion, no preemption. So request B can never see request
30
+ * A's name on the board, by the time B's turn starts, the board was already
31
+ * erased at the end of A's turn.
32
+ *
33
+ * "But the handler is async!" — the `await`s inside it run LATER, in separate
34
+ * turns. That is fine, because the check already happened: the verdict was
35
+ * copied onto that request's own context (`'~sig'`) during the synchronous
36
+ * part, and the code that runs after an `await` only ever reads the context,
37
+ * never this slot.
38
+ *
39
+ * And if any assumption here ever breaks, a runtime that re-enters, a wrap
40
+ * that delays, a future edit that sneaks an `await` in before the check the
41
+ * comparison simply MISSES and the request falls back to eager arming, which
42
+ * is the exact pre-optimization behavior. Every failure mode makes a request
43
+ * slightly slower, never incorrect
44
+ *
45
+ * Adversarial proof, not just argument: `test/core/abort-race.test.ts` storms
46
+ * concurrent servers, in-process `handle()` calls, request-substituting and
47
+ * `next()`-delaying wraps at this slot and asserts zero misclassification.
48
+ *
49
+ * Never infer the Bun runtime from `fetch`'s second argument: Cloudflare
50
+ * Workers pass `env` there.
51
+ *
52
+ * It's insanely stupid, but it works. As Bun is single-threaded and synchronous
53
+ * so the `finally` block will always run before the next request comes in.
54
+ */
55
+ declare const origin: {
56
+ request: Request | undefined;
57
+ };
58
+ //#endregion
59
+ export { origin };
@@ -0,0 +1,60 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
+
3
+ //#region src/adapter/origin.ts
4
+ /**
5
+ * Provenance channel for deferred abort-signal arming.
6
+ *
7
+ * Bun materializes `Request.signal` lazily (~214ns on the first read), so
8
+ * Elysia only pays for it once the pipeline can actually observe an abort,
9
+ * that is, after it suspends. Deferring that read is only sound for the exact
10
+ * `Request` instance `Bun.serve` handed to `fetch`: any other request
11
+ * (`app.handle`, a non-Bun adapter, or a `.wrap()` HOC that substitutes the
12
+ * request) may carry a user-controlled signal that is already aborted at entry
13
+ * and therefore must be observed synchronously.
14
+ *
15
+ * The Bun adapter publishes the untouched original here for the *synchronous*
16
+ * prologue of `app.fetch` and clears it in `finally`, so the window closes
17
+ * before the first suspension and cannot leak across concurrent requests.
18
+ * A `.wrap()` HOC that replaces the request or defers calling `next()` past a
19
+ * microtask simply misses the window, fetch handler falls back to eager check
20
+ *
21
+ * ## "A single shared slot that every request writes to, so a race condition?"
22
+ *
23
+ * It looks like one, but it can't be. Explained like you're five:
24
+ *
25
+ * JavaScript only ever does ONE thing at a time. When a request comes in, the
26
+ * adapter writes its name on the whiteboard (`origin.request = request`),
27
+ * walks it down the hall (`fetch(request)` the synchronous part, where the
28
+ * one and only identity check happens), and erases the whiteboard (`finally`)
29
+ * all in one uninterruptible turn. The next request's turn CANNOT start
30
+ * until this turn is completely finished, because that is how the event loop
31
+ * works: run-to-completion, no preemption. So request B can never see request
32
+ * A's name on the board, by the time B's turn starts, the board was already
33
+ * erased at the end of A's turn.
34
+ *
35
+ * "But the handler is async!" — the `await`s inside it run LATER, in separate
36
+ * turns. That is fine, because the check already happened: the verdict was
37
+ * copied onto that request's own context (`'~sig'`) during the synchronous
38
+ * part, and the code that runs after an `await` only ever reads the context,
39
+ * never this slot.
40
+ *
41
+ * And if any assumption here ever breaks, a runtime that re-enters, a wrap
42
+ * that delays, a future edit that sneaks an `await` in before the check the
43
+ * comparison simply MISSES and the request falls back to eager arming, which
44
+ * is the exact pre-optimization behavior. Every failure mode makes a request
45
+ * slightly slower, never incorrect
46
+ *
47
+ * Adversarial proof, not just argument: `test/core/abort-race.test.ts` storms
48
+ * concurrent servers, in-process `handle()` calls, request-substituting and
49
+ * `next()`-delaying wraps at this slot and asserts zero misclassification.
50
+ *
51
+ * Never infer the Bun runtime from `fetch`'s second argument: Cloudflare
52
+ * Workers pass `env` there.
53
+ *
54
+ * It's insanely stupid, but it works. As Bun is single-threaded and synchronous
55
+ * so the `finally` block will always run before the next request comes in.
56
+ */
57
+ const origin = { request: void 0 };
58
+
59
+ //#endregion
60
+ exports.origin = origin;
@@ -0,0 +1,58 @@
1
+ //#region src/adapter/origin.ts
2
+ /**
3
+ * Provenance channel for deferred abort-signal arming.
4
+ *
5
+ * Bun materializes `Request.signal` lazily (~214ns on the first read), so
6
+ * Elysia only pays for it once the pipeline can actually observe an abort,
7
+ * that is, after it suspends. Deferring that read is only sound for the exact
8
+ * `Request` instance `Bun.serve` handed to `fetch`: any other request
9
+ * (`app.handle`, a non-Bun adapter, or a `.wrap()` HOC that substitutes the
10
+ * request) may carry a user-controlled signal that is already aborted at entry
11
+ * and therefore must be observed synchronously.
12
+ *
13
+ * The Bun adapter publishes the untouched original here for the *synchronous*
14
+ * prologue of `app.fetch` and clears it in `finally`, so the window closes
15
+ * before the first suspension and cannot leak across concurrent requests.
16
+ * A `.wrap()` HOC that replaces the request or defers calling `next()` past a
17
+ * microtask simply misses the window, fetch handler falls back to eager check
18
+ *
19
+ * ## "A single shared slot that every request writes to, so a race condition?"
20
+ *
21
+ * It looks like one, but it can't be. Explained like you're five:
22
+ *
23
+ * JavaScript only ever does ONE thing at a time. When a request comes in, the
24
+ * adapter writes its name on the whiteboard (`origin.request = request`),
25
+ * walks it down the hall (`fetch(request)` the synchronous part, where the
26
+ * one and only identity check happens), and erases the whiteboard (`finally`)
27
+ * all in one uninterruptible turn. The next request's turn CANNOT start
28
+ * until this turn is completely finished, because that is how the event loop
29
+ * works: run-to-completion, no preemption. So request B can never see request
30
+ * A's name on the board, by the time B's turn starts, the board was already
31
+ * erased at the end of A's turn.
32
+ *
33
+ * "But the handler is async!" — the `await`s inside it run LATER, in separate
34
+ * turns. That is fine, because the check already happened: the verdict was
35
+ * copied onto that request's own context (`'~sig'`) during the synchronous
36
+ * part, and the code that runs after an `await` only ever reads the context,
37
+ * never this slot.
38
+ *
39
+ * And if any assumption here ever breaks, a runtime that re-enters, a wrap
40
+ * that delays, a future edit that sneaks an `await` in before the check the
41
+ * comparison simply MISSES and the request falls back to eager arming, which
42
+ * is the exact pre-optimization behavior. Every failure mode makes a request
43
+ * slightly slower, never incorrect
44
+ *
45
+ * Adversarial proof, not just argument: `test/core/abort-race.test.ts` storms
46
+ * concurrent servers, in-process `handle()` calls, request-substituting and
47
+ * `next()`-delaying wraps at this slot and asserts zero misclassification.
48
+ *
49
+ * Never infer the Bun runtime from `fetch`'s second argument: Cloudflare
50
+ * Workers pass `env` there.
51
+ *
52
+ * It's insanely stupid, but it works. As Bun is single-threaded and synchronous
53
+ * so the `finally` block will always run before the next request comes in.
54
+ */
55
+ const origin = { request: void 0 };
56
+
57
+ //#endregion
58
+ export { origin };