toiljs 0.0.116 → 0.0.118
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/CHANGELOG.md +14 -0
- package/README.md +16 -7
- package/build/backend/.tsbuildinfo +1 -1
- package/build/cli/.tsbuildinfo +1 -1
- package/build/cli/index.js +2375 -258
- package/build/client/.tsbuildinfo +1 -1
- package/build/client/auth.d.ts +3 -0
- package/build/client/auth.js +19 -10
- package/build/client/dev/devtools.js +1 -1
- package/build/client/dev/error-overlay.js +1 -1
- package/build/client/index.d.ts +1 -1
- package/build/client/index.js +1 -1
- package/build/client/navigation/navigation.js +28 -3
- package/build/client/routing/Router.js +5 -2
- package/build/client/routing/mount.js +1 -1
- package/build/compiler/.tsbuildinfo +1 -1
- package/build/compiler/auth-emails.js +7 -3
- package/build/compiler/emails.js +1 -1
- package/build/compiler/index.d.ts +1 -0
- package/build/compiler/index.js +26 -5
- package/build/compiler/pages.js +2 -3
- package/build/compiler/prerender.d.ts +3 -8
- package/build/compiler/prerender.js +88 -127
- package/build/compiler/ssg.js +3 -4
- package/build/compiler/template-build.js +3 -4
- package/build/compiler/toil-docs.generated.js +8 -7
- package/build/compiler/vite.js +4 -6
- package/build/devserver/.tsbuildinfo +1 -1
- package/build/devserver/db/database.js +7 -3
- package/build/devserver/db/types.js +1 -1
- package/build/devserver/production.js +4 -3
- package/build/devserver/runtime/crypto.d.ts +1 -1
- package/build/io/.tsbuildinfo +1 -1
- package/build/logger/.tsbuildinfo +1 -1
- package/build/shared/.tsbuildinfo +1 -1
- package/examples/basic/client/routes/analytics.tsx +7 -5
- package/examples/basic/client/routes/pq.tsx +2 -1
- package/examples/basic/server/tsconfig.json +1 -3
- package/package.json +45 -44
- package/presets/no-uint8array-tostring.js +120 -153
- package/presets/oxlint.d.ts +5 -0
- package/presets/oxlint.js +224 -0
- package/server/auth/AuthController.ts +53 -14
- package/server/auth/FactorSettings.ts +41 -0
- package/server/auth/SessionEpochs.ts +58 -0
- package/server/auth/SessionRevocations.ts +13 -0
- package/server/globals/auth.ts +27 -2
- package/server/runtime/tsconfig.json +1 -3
- package/tsconfig.json +2 -11
- package/.github/ISSUE_TEMPLATE/bug_report.md +0 -38
- package/.github/ISSUE_TEMPLATE/bug_report.yml +0 -90
- package/.github/ISSUE_TEMPLATE/config.yml +0 -8
- package/.github/ISSUE_TEMPLATE/feature_request.md +0 -20
- package/.github/PULL_REQUEST_TEMPLATE.md +0 -43
- package/.github/changelog-config.json +0 -45
- package/.github/dependabot.yml +0 -27
- package/.github/workflows/ci.yml +0 -222
- package/.vscode/settings.json +0 -10
- package/docs/README.md +0 -107
- package/docs/auth/README.md +0 -104
- package/docs/auth/configuration.md +0 -105
- package/docs/auth/emails.md +0 -92
- package/docs/auth/extending.md +0 -202
- package/docs/auth/how-it-works.md +0 -138
- package/docs/auth/usage.md +0 -188
- package/docs/backend/README.md +0 -143
- package/docs/backend/data.md +0 -351
- package/docs/backend/rest.md +0 -402
- package/docs/backend/rpc.md +0 -226
- package/docs/background/README.md +0 -114
- package/docs/background/daemons.md +0 -276
- package/docs/background/derive.md +0 -179
- package/docs/cli/README.md +0 -379
- package/docs/concepts/ai-guide.md +0 -132
- package/docs/concepts/config.md +0 -416
- package/docs/concepts/decorators.md +0 -127
- package/docs/concepts/security.md +0 -108
- package/docs/concepts/tiers.md +0 -166
- package/docs/concepts/types.md +0 -216
- package/docs/database/README.md +0 -143
- package/docs/database/capacity.md +0 -350
- package/docs/database/counters.md +0 -174
- package/docs/database/documents.md +0 -277
- package/docs/database/events.md +0 -307
- package/docs/database/membership.md +0 -246
- package/docs/database/setup.md +0 -155
- package/docs/database/unique.md +0 -216
- package/docs/database/views.md +0 -246
- package/docs/frontend/README.md +0 -104
- package/docs/frontend/components.md +0 -286
- package/docs/frontend/data-fetching.md +0 -260
- package/docs/frontend/images.md +0 -148
- package/docs/frontend/metadata.md +0 -344
- package/docs/frontend/navigation.md +0 -296
- package/docs/frontend/rendering.md +0 -236
- package/docs/frontend/routing.md +0 -346
- package/docs/frontend/scripts.md +0 -118
- package/docs/frontend/search.md +0 -191
- package/docs/frontend/styling.md +0 -147
- package/docs/frontend/toil-global.md +0 -216
- package/docs/getting-started/README.md +0 -81
- package/docs/getting-started/create-project.md +0 -131
- package/docs/getting-started/deploy.md +0 -101
- package/docs/getting-started/first-app.md +0 -215
- package/docs/getting-started/installation.md +0 -124
- package/docs/getting-started/migrating.md +0 -125
- package/docs/getting-started/project-structure.md +0 -163
- package/docs/introduction/README.md +0 -66
- package/docs/introduction/design-principles.md +0 -81
- package/docs/introduction/distributed.md +0 -91
- package/docs/introduction/how-it-works.md +0 -121
- package/docs/introduction/hyperscale.md +0 -70
- package/docs/introduction/modern-stack.md +0 -85
- package/docs/introduction/vs-other-frameworks.md +0 -64
- package/docs/introduction/why-toil.md +0 -99
- package/docs/llms.txt +0 -95
- package/docs/realtime/README.md +0 -102
- package/docs/realtime/channels.md +0 -211
- package/docs/realtime/streams.md +0 -369
- package/docs/services/README.md +0 -60
- package/docs/services/analytics.md +0 -279
- package/docs/services/caching.md +0 -175
- package/docs/services/cookies.md +0 -235
- package/docs/services/crypto.md +0 -209
- package/docs/services/email.md +0 -289
- package/docs/services/environment.md +0 -141
- package/docs/services/ratelimit.md +0 -174
- package/docs/services/time.md +0 -85
- package/eslint.config.js +0 -57
- package/examples/basic/client/routes/docs/[...slug].tsx +0 -12
- package/presets/eslint.js +0 -88
- package/src/backend/index.ts +0 -204
- package/src/cli/configure.ts +0 -363
- package/src/cli/create.ts +0 -924
- package/src/cli/db.ts +0 -158
- package/src/cli/diagnostics.ts +0 -665
- package/src/cli/doctor.ts +0 -982
- package/src/cli/features.ts +0 -170
- package/src/cli/index.ts +0 -326
- package/src/cli/notify.ts +0 -120
- package/src/cli/proc.ts +0 -56
- package/src/cli/ui.ts +0 -169
- package/src/cli/update.ts +0 -248
- package/src/cli/updates.ts +0 -92
- package/src/cli/validate.ts +0 -31
- package/src/cli/version-check.ts +0 -125
- package/src/client/auth.ts +0 -869
- package/src/client/channel/channel.ts +0 -146
- package/src/client/components/Form.tsx +0 -65
- package/src/client/components/Image.tsx +0 -160
- package/src/client/components/Script.tsx +0 -113
- package/src/client/components/Slot.tsx +0 -21
- package/src/client/dev/devtools.tsx +0 -1089
- package/src/client/dev/error-overlay.tsx +0 -225
- package/src/client/errors.ts +0 -11
- package/src/client/head/head.ts +0 -182
- package/src/client/head/metadata.ts +0 -110
- package/src/client/index.ts +0 -130
- package/src/client/navigation/Link.tsx +0 -108
- package/src/client/navigation/NavLink.tsx +0 -86
- package/src/client/navigation/navigation.ts +0 -235
- package/src/client/navigation/prefetch.ts +0 -169
- package/src/client/navigation/scroll.ts +0 -53
- package/src/client/routing/Router.tsx +0 -216
- package/src/client/routing/action.ts +0 -122
- package/src/client/routing/error-boundary.tsx +0 -43
- package/src/client/routing/hooks.ts +0 -140
- package/src/client/routing/lazy.ts +0 -77
- package/src/client/routing/loader.ts +0 -381
- package/src/client/routing/match.ts +0 -47
- package/src/client/routing/mount.tsx +0 -90
- package/src/client/routing/params-context.ts +0 -10
- package/src/client/routing/slot-context.ts +0 -7
- package/src/client/rpc.ts +0 -97
- package/src/client/search/search.ts +0 -189
- package/src/client/search/use-page-search.ts +0 -73
- package/src/client/ssr/markers.tsx +0 -172
- package/src/client/stream/client.ts +0 -237
- package/src/client/types.ts +0 -73
- package/src/compiler/auth-emails.ts +0 -312
- package/src/compiler/config.ts +0 -369
- package/src/compiler/docs.ts +0 -127
- package/src/compiler/email-preview.ts +0 -314
- package/src/compiler/emails.ts +0 -404
- package/src/compiler/fonts.ts +0 -89
- package/src/compiler/generate.ts +0 -471
- package/src/compiler/image-blur.ts +0 -42
- package/src/compiler/image-report.ts +0 -88
- package/src/compiler/index.ts +0 -1362
- package/src/compiler/pages.ts +0 -49
- package/src/compiler/plugin.ts +0 -391
- package/src/compiler/prerender.ts +0 -263
- package/src/compiler/routes.ts +0 -170
- package/src/compiler/seo.ts +0 -415
- package/src/compiler/sri.ts +0 -144
- package/src/compiler/ssg.ts +0 -177
- package/src/compiler/ssr-codegen.ts +0 -85
- package/src/compiler/template-build.ts +0 -676
- package/src/compiler/template.ts +0 -262
- package/src/compiler/toil-docs.generated.ts +0 -77
- package/src/compiler/vite.ts +0 -245
- package/src/devserver/analytics/index.ts +0 -317
- package/src/devserver/config/dotenv.ts +0 -102
- package/src/devserver/config/env.ts +0 -30
- package/src/devserver/config/ratelimit.ts +0 -123
- package/src/devserver/daemon/catalog.ts +0 -120
- package/src/devserver/daemon/cron.ts +0 -87
- package/src/devserver/daemon/host.ts +0 -134
- package/src/devserver/daemon/index.ts +0 -369
- package/src/devserver/daemon/runtime.ts +0 -48
- package/src/devserver/db/catalog.ts +0 -109
- package/src/devserver/db/database.ts +0 -1834
- package/src/devserver/db/derives.ts +0 -121
- package/src/devserver/db/index.ts +0 -20
- package/src/devserver/db/routeKinds.ts +0 -191
- package/src/devserver/db/types.ts +0 -168
- package/src/devserver/email/caps.ts +0 -0
- package/src/devserver/email/config.ts +0 -141
- package/src/devserver/email/console.ts +0 -348
- package/src/devserver/email/index.ts +0 -155
- package/src/devserver/email/providers.ts +0 -131
- package/src/devserver/email/status.ts +0 -23
- package/src/devserver/email/validate.ts +0 -37
- package/src/devserver/email/wire.ts +0 -55
- package/src/devserver/http/cache.ts +0 -0
- package/src/devserver/http/envelope.ts +0 -157
- package/src/devserver/http/proxy.ts +0 -172
- package/src/devserver/http/runtime.ts +0 -387
- package/src/devserver/index.ts +0 -43
- package/src/devserver/production-ipc.ts +0 -63
- package/src/devserver/production-worker.ts +0 -83
- package/src/devserver/production.ts +0 -718
- package/src/devserver/runtime/crypto.ts +0 -587
- package/src/devserver/runtime/host.ts +0 -427
- package/src/devserver/runtime/module.ts +0 -442
- package/src/devserver/server.ts +0 -251
- package/src/devserver/ssr.ts +0 -208
- package/src/devserver/stream/catalog.ts +0 -106
- package/src/devserver/stream/host.ts +0 -42
- package/src/devserver/stream/index.ts +0 -301
- package/src/devserver/stream/manager.ts +0 -167
- package/src/devserver/stream/router.ts +0 -101
- package/src/devserver/stream/wire.ts +0 -58
- package/src/devserver/stream/ws.ts +0 -78
- package/src/devserver/wasm/sections.ts +0 -59
- package/src/devserver/wasm/surface.ts +0 -115
- package/src/io/FastMap.ts +0 -151
- package/src/io/FastSet.ts +0 -113
- package/src/io/codec.ts +0 -362
- package/src/io/index.ts +0 -10
- package/src/io/lengths.ts +0 -14
- package/src/io/types.ts +0 -19
- package/src/logger/index.ts +0 -22
- package/src/shared/index.ts +0 -46
- package/test/analytics-dev.test.ts +0 -113
- package/test/assembly/aspect-shim.ts +0 -33
- package/test/assembly/cookie.spec.ts +0 -303
- package/test/assembly/example.spec.ts +0 -22
- package/test/assembly/ssr.spec.ts +0 -95
- package/test/assembly.test.ts +0 -59
- package/test/auth-emails.test.ts +0 -118
- package/test/built-ssr.test.ts +0 -148
- package/test/channel.test.ts +0 -21
- package/test/config-host-port.test.ts +0 -59
- package/test/configure.test.ts +0 -94
- package/test/daemon-build.test.ts +0 -278
- package/test/daemon-catalog.test.ts +0 -349
- package/test/daemon-emulation.test.ts +0 -256
- package/test/db.test.ts +0 -0
- package/test/devserver-database.test.ts +0 -1102
- package/test/devserver-email.test.ts +0 -306
- package/test/devserver-pqauth.test.ts +0 -93
- package/test/devserver-secrets.test.ts +0 -63
- package/test/devserver.test.ts +0 -259
- package/test/doctor.test.ts +0 -298
- package/test/dom/Image.test.tsx +0 -144
- package/test/dom/Link.test.tsx +0 -67
- package/test/dom/NavLink.test.tsx +0 -37
- package/test/dom/Script.test.tsx +0 -48
- package/test/dom/action.test.tsx +0 -146
- package/test/dom/error-overlay.test.tsx +0 -44
- package/test/dom/loader.test.tsx +0 -121
- package/test/dom/navigation.test.ts +0 -59
- package/test/dom/revalidate.test.tsx +0 -38
- package/test/dom/route-head.test.tsx +0 -56
- package/test/dom/router-loading.test.tsx +0 -44
- package/test/dom/scroll.test.ts +0 -56
- package/test/dom/slot.test.tsx +0 -131
- package/test/dom/use-metadata.test.tsx +0 -58
- package/test/dom/view-transitions.test.tsx +0 -53
- package/test/dynamic-routing.test.ts +0 -132
- package/test/email-console.test.ts +0 -86
- package/test/email-preview.test.ts +0 -73
- package/test/emails.test.ts +0 -58
- package/test/errors.test.ts +0 -21
- package/test/example-guestbook.test.ts +0 -123
- package/test/features.test.ts +0 -149
- package/test/fixtures/bignum-wire/spec.ts +0 -25
- package/test/fixtures/daemon-app.ts +0 -65
- package/test/fixtures/stream-echo.ts +0 -26
- package/test/fixtures/stream-gate.ts +0 -28
- package/test/fixtures/stream-trap.ts +0 -18
- package/test/fixtures/stream-typed.ts +0 -41
- package/test/fonts.test.ts +0 -28
- package/test/global-setup.ts +0 -17
- package/test/head.test.ts +0 -42
- package/test/image-blur.test.ts +0 -50
- package/test/io.test.ts +0 -117
- package/test/metadata.test.ts +0 -40
- package/test/navlink.test.ts +0 -28
- package/test/pages.test.ts +0 -105
- package/test/placeholder.test.ts +0 -9
- package/test/pqauth-e2e.test.ts +0 -216
- package/test/pqauth-email.test.ts +0 -801
- package/test/prerender.test.ts +0 -116
- package/test/prettier-plugin.test.ts +0 -46
- package/test/routes.test.ts +0 -76
- package/test/rpc-bignum-wire.test.ts +0 -164
- package/test/rpc-dispatch.test.ts +0 -132
- package/test/rpc-kinds.test.ts +0 -18
- package/test/rpc.test.ts +0 -66
- package/test/search.test.ts +0 -114
- package/test/seo.test.ts +0 -175
- package/test/slot-layouts.test.ts +0 -69
- package/test/sri.test.ts +0 -129
- package/test/ssg.test.ts +0 -36
- package/test/ssr-hydration.test.tsx +0 -122
- package/test/ssr-render.test.ts +0 -197
- package/test/ssr-template.test.tsx +0 -482
- package/test/stream-emulation.test.ts +0 -479
- package/test/ui.test.ts +0 -40
- package/test/update.test.ts +0 -58
- package/test/validate.test.ts +0 -42
- package/test/version-check.test.ts +0 -130
- package/tests/data-parity/generated-parity.ts +0 -99
- package/tests/data-parity/parity.ts +0 -80
- package/tests/data-parity/spec.ts +0 -46
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Strict single-recipient check for the guest-supplied `to`, byte-for-byte the
|
|
3
|
-
* edge's (`host/email.rs::valid_recipient`). Rejects header injection
|
|
4
|
-
* (CR/LF/NUL), multiple addresses (comma, semicolon, whitespace, angle
|
|
5
|
-
* brackets, quotes), and the obviously malformed, so a guest can never smuggle a
|
|
6
|
-
* Bcc or a second envelope recipient into the provider call. Exactly one `@`,
|
|
7
|
-
* non-empty local part, dotted domain.
|
|
8
|
-
*/
|
|
9
|
-
const FORBIDDEN = new Set(['\r', '\n', '\0', ',', ';', ' ', '\t', '<', '>', '"']);
|
|
10
|
-
|
|
11
|
-
export function validRecipient(s: string): boolean {
|
|
12
|
-
if (s.length === 0 || Buffer.byteLength(s, 'utf8') > 320) return false;
|
|
13
|
-
for (const ch of s) {
|
|
14
|
-
if (FORBIDDEN.has(ch)) return false;
|
|
15
|
-
}
|
|
16
|
-
const parts = s.split('@');
|
|
17
|
-
if (parts.length !== 2) return false; // not exactly one '@'
|
|
18
|
-
const [local, domain] = parts;
|
|
19
|
-
return (
|
|
20
|
-
local.length > 0 && domain.includes('.') && !domain.startsWith('.') && !domain.endsWith('.')
|
|
21
|
-
);
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Lenient validation of the operator-supplied `from` (`host/email.rs::valid_from`):
|
|
26
|
-
* trusted config, so the only hard requirement is no header injection; the
|
|
27
|
-
* provider validates deliverability.
|
|
28
|
-
*/
|
|
29
|
-
export function validFrom(s: string): boolean {
|
|
30
|
-
return (
|
|
31
|
-
Buffer.byteLength(s, 'utf8') <= 320 &&
|
|
32
|
-
s.includes('@') &&
|
|
33
|
-
!s.includes('\r') &&
|
|
34
|
-
!s.includes('\n') &&
|
|
35
|
-
!s.includes('\0')
|
|
36
|
-
);
|
|
37
|
-
}
|
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Decode the `email_send` request blob the guest writes into wasm memory — the
|
|
3
|
-
* v2 wire format, byte-for-byte the edge's (`email_send_import.rs::parse_request`
|
|
4
|
-
* and `server/globals/email.ts`):
|
|
5
|
-
*
|
|
6
|
-
* u16 to_len | u16 subject_len | u16 purpose_len | u32 body_len | u32 html_len
|
|
7
|
-
* [to][subject][purpose][body][html] (14-byte LE header, then UTF-8 payloads)
|
|
8
|
-
*
|
|
9
|
-
* `html_len == 0` is a plain-text send. Returns `null` on truncation, a length
|
|
10
|
-
* that overruns the blob, trailing garbage, or non-UTF-8 — all guest encode bugs
|
|
11
|
-
* the caller maps to `BadRecipient`.
|
|
12
|
-
*/
|
|
13
|
-
export interface ParsedEmail {
|
|
14
|
-
readonly to: string;
|
|
15
|
-
readonly subject: string;
|
|
16
|
-
readonly purpose: string;
|
|
17
|
-
readonly body: string;
|
|
18
|
-
readonly html: string;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
const HEADER_LEN = 14;
|
|
22
|
-
|
|
23
|
-
export function parseEmailBlob(raw: Buffer): ParsedEmail | null {
|
|
24
|
-
if (raw.length < HEADER_LEN) return null;
|
|
25
|
-
const toLen = raw.readUInt16LE(0);
|
|
26
|
-
const subjectLen = raw.readUInt16LE(2);
|
|
27
|
-
const purposeLen = raw.readUInt16LE(4);
|
|
28
|
-
const bodyLen = raw.readUInt32LE(6);
|
|
29
|
-
const htmlLen = raw.readUInt32LE(10);
|
|
30
|
-
|
|
31
|
-
const total = toLen + subjectLen + purposeLen + bodyLen + htmlLen;
|
|
32
|
-
// Exact fit: the five payloads must consume the rest of the blob precisely.
|
|
33
|
-
if (HEADER_LEN + total !== raw.length) return null;
|
|
34
|
-
|
|
35
|
-
let off = HEADER_LEN;
|
|
36
|
-
const take = (n: number): string | null => {
|
|
37
|
-
const end = off + n;
|
|
38
|
-
const slice = raw.subarray(off, end);
|
|
39
|
-
// Reject invalid UTF-8 (Buffer.toString is lossy, so verify by round-trip).
|
|
40
|
-
const s = slice.toString('utf8');
|
|
41
|
-
if (Buffer.byteLength(s, 'utf8') !== n) return null;
|
|
42
|
-
off = end;
|
|
43
|
-
return s;
|
|
44
|
-
};
|
|
45
|
-
|
|
46
|
-
const to = take(toLen);
|
|
47
|
-
const subject = take(subjectLen);
|
|
48
|
-
const purpose = take(purposeLen);
|
|
49
|
-
const body = take(bodyLen);
|
|
50
|
-
const html = take(htmlLen);
|
|
51
|
-
if (to === null || subject === null || purpose === null || body === null || html === null) {
|
|
52
|
-
return null;
|
|
53
|
-
}
|
|
54
|
-
return { to, subject, purpose, body, html };
|
|
55
|
-
}
|
|
Binary file
|
|
@@ -1,157 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Node-side wire envelope codec for the WASM dev server, byte-for-byte compatible with
|
|
3
|
-
* `server/runtime/envelope.ts` (the ToilScript guest decoder/encoder) and
|
|
4
|
-
* `toil-backend/src/http/envelope.rs` (the production edge).
|
|
5
|
-
*
|
|
6
|
-
* Layout (little-endian, no padding):
|
|
7
|
-
*
|
|
8
|
-
* request:
|
|
9
|
-
* u8 method (0=GET, 1=POST, 2=PUT, 3=DELETE, 4=PATCH, 5=HEAD, 6=OPTIONS)
|
|
10
|
-
* u16 path_len
|
|
11
|
-
* [u8] path
|
|
12
|
-
* u16 n_headers
|
|
13
|
-
* for each header: u16 name_len, u16 val_len, [u8] name, [u8] val
|
|
14
|
-
* u32 body_len
|
|
15
|
-
* [u8] body
|
|
16
|
-
*
|
|
17
|
-
* response: same shape but the first u8+u16 (method + path_len + path)
|
|
18
|
-
* is replaced by `u16 status`.
|
|
19
|
-
*
|
|
20
|
-
* Header names/values and the path must each fit in u16, the body in u32; the
|
|
21
|
-
* encoder throws instead of silently truncating (mirrors `EncodeError` on the
|
|
22
|
-
* edge). The decoder bounds-checks every length field so a malformed guest
|
|
23
|
-
* response can never read past the envelope.
|
|
24
|
-
*/
|
|
25
|
-
|
|
26
|
-
/** Method discriminant matching the on-wire envelope byte (part of the wasm ABI; do not reorder). */
|
|
27
|
-
export const METHOD_CODES: Readonly<Record<string, number>> = {
|
|
28
|
-
GET: 0,
|
|
29
|
-
POST: 1,
|
|
30
|
-
PUT: 2,
|
|
31
|
-
DELETE: 3,
|
|
32
|
-
PATCH: 4,
|
|
33
|
-
HEAD: 5,
|
|
34
|
-
OPTIONS: 6,
|
|
35
|
-
};
|
|
36
|
-
|
|
37
|
-
/** A request to serialize for the guest. `path` includes the query string. */
|
|
38
|
-
export interface EnvelopeRequest {
|
|
39
|
-
readonly method: string;
|
|
40
|
-
readonly path: string;
|
|
41
|
-
/** Full request header list, passed through to the guest. */
|
|
42
|
-
readonly headers: readonly (readonly [string, string])[];
|
|
43
|
-
readonly body: Uint8Array;
|
|
44
|
-
/** The connecting client's IP for the `client_ip` import / `ctx.clientIp()`.
|
|
45
|
-
* The edge uses the unspoofable socket peer; the dev server best-efforts it. */
|
|
46
|
-
readonly clientIp?: string;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/** The decoded guest response envelope. */
|
|
50
|
-
export interface EnvelopeResponse {
|
|
51
|
-
readonly status: number;
|
|
52
|
-
readonly headers: readonly (readonly [string, string])[];
|
|
53
|
-
readonly body: Uint8Array;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
const U16_MAX = 0xffff;
|
|
57
|
-
const U32_MAX = 0xffffffff;
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* Encode `req` into a fresh buffer. Throws on an unsupported method or a field
|
|
61
|
-
* that does not fit its length prefix; callers turn that into a clean 4xx (or
|
|
62
|
-
* route the request past the guest) instead of handing the guest garbage.
|
|
63
|
-
*/
|
|
64
|
-
export function encodeRequestEnvelope(req: EnvelopeRequest): Buffer {
|
|
65
|
-
const method = METHOD_CODES[req.method.toUpperCase()];
|
|
66
|
-
if (method === undefined) throw new Error(`unsupported method: ${req.method}`);
|
|
67
|
-
|
|
68
|
-
const path = Buffer.from(req.path, 'utf8');
|
|
69
|
-
if (path.length > U16_MAX) throw new Error(`path too long: ${String(path.length)} bytes`);
|
|
70
|
-
if (req.headers.length > U16_MAX)
|
|
71
|
-
throw new Error(`too many headers: ${String(req.headers.length)}`);
|
|
72
|
-
if (req.body.length > U32_MAX)
|
|
73
|
-
throw new Error(`body too long: ${String(req.body.length)} bytes`);
|
|
74
|
-
|
|
75
|
-
const headers: { name: Buffer; value: Buffer }[] = [];
|
|
76
|
-
let headersSize = 0;
|
|
77
|
-
for (const [name, value] of req.headers) {
|
|
78
|
-
const n = Buffer.from(name, 'utf8');
|
|
79
|
-
const v = Buffer.from(value, 'utf8');
|
|
80
|
-
if (n.length > U16_MAX || v.length > U16_MAX) throw new Error(`header too long: ${name}`);
|
|
81
|
-
headers.push({ name: n, value: v });
|
|
82
|
-
headersSize += 4 + n.length + v.length;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
const total = 1 + 2 + path.length + 2 + headersSize + 4 + req.body.length;
|
|
86
|
-
const out = Buffer.allocUnsafe(total);
|
|
87
|
-
let cur = 0;
|
|
88
|
-
|
|
89
|
-
out.writeUInt8(method, cur);
|
|
90
|
-
cur += 1;
|
|
91
|
-
out.writeUInt16LE(path.length, cur);
|
|
92
|
-
cur += 2;
|
|
93
|
-
cur += path.copy(out, cur);
|
|
94
|
-
|
|
95
|
-
out.writeUInt16LE(headers.length, cur);
|
|
96
|
-
cur += 2;
|
|
97
|
-
for (const h of headers) {
|
|
98
|
-
out.writeUInt16LE(h.name.length, cur);
|
|
99
|
-
cur += 2;
|
|
100
|
-
out.writeUInt16LE(h.value.length, cur);
|
|
101
|
-
cur += 2;
|
|
102
|
-
cur += h.name.copy(out, cur);
|
|
103
|
-
cur += h.value.copy(out, cur);
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
out.writeUInt32LE(req.body.length, cur);
|
|
107
|
-
cur += 4;
|
|
108
|
-
cur += Buffer.from(req.body.buffer, req.body.byteOffset, req.body.length).copy(out, cur);
|
|
109
|
-
|
|
110
|
-
return out;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
/**
|
|
114
|
-
* Decode the response envelope the guest wrote at `bytes`. Throws on
|
|
115
|
-
* truncation or a length field that overruns the envelope (a guest bug);
|
|
116
|
-
* the dispatcher converts that into a 500.
|
|
117
|
-
*/
|
|
118
|
-
export function decodeResponseEnvelope(bytes: Uint8Array): EnvelopeResponse {
|
|
119
|
-
const view = Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
120
|
-
let cur = 0;
|
|
121
|
-
|
|
122
|
-
const take = (n: number): number => {
|
|
123
|
-
if (cur + n > view.length)
|
|
124
|
-
throw new Error(`response envelope truncated at byte ${String(cur)}`);
|
|
125
|
-
const at = cur;
|
|
126
|
-
cur += n;
|
|
127
|
-
return at;
|
|
128
|
-
};
|
|
129
|
-
|
|
130
|
-
const status = view.readUInt16LE(take(2));
|
|
131
|
-
if (status === 0) throw new Error('response envelope has status 0');
|
|
132
|
-
|
|
133
|
-
const nHeaders = view.readUInt16LE(take(2));
|
|
134
|
-
const headers: (readonly [string, string])[] = [];
|
|
135
|
-
for (let i = 0; i < nHeaders; i++) {
|
|
136
|
-
const nameLen = view.readUInt16LE(take(2));
|
|
137
|
-
const valLen = view.readUInt16LE(take(2));
|
|
138
|
-
const name = view.toString('utf8', take(nameLen), cur);
|
|
139
|
-
const value = view.toString('utf8', take(valLen), cur);
|
|
140
|
-
headers.push([name, value]);
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
const bodyLen = view.readUInt32LE(take(4));
|
|
144
|
-
const bodyStart = take(bodyLen);
|
|
145
|
-
// Copy out of the guest's linear memory so the response outlives the instance.
|
|
146
|
-
const body = Uint8Array.prototype.slice.call(view, bodyStart, cur);
|
|
147
|
-
|
|
148
|
-
return { status, headers, body };
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
/** Unpack the i64 `handle()` returns: high 32 bits = response offset, low 32 bits = byte length. */
|
|
152
|
-
export function unpackHandleResult(packed: bigint): { ptr: number; len: number } {
|
|
153
|
-
return {
|
|
154
|
-
ptr: Number((packed >> 32n) & 0xffffffffn),
|
|
155
|
-
len: Number(packed & 0xffffffffn),
|
|
156
|
-
};
|
|
157
|
-
}
|
|
@@ -1,172 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Transparent proxy from the uWebSockets.js front server to the internal Vite
|
|
3
|
-
* dev server, so every Vite dev feature (module transforms, HMR websocket,
|
|
4
|
-
* `/__toil/*` toolbar endpoints, public assets, SPA fallback) keeps working
|
|
5
|
-
* unchanged behind the WASM dispatcher. HTTP goes through `fetch`, the HMR
|
|
6
|
-
* websocket through Node's built-in `WebSocket` client, both loopback-only.
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
import { type Request, type Response, type Server, type Websocket } from '@dacely/hyper-express';
|
|
10
|
-
|
|
11
|
-
/** Where the internal Vite dev server listens (always loopback). */
|
|
12
|
-
export interface ViteTarget {
|
|
13
|
-
readonly host: string;
|
|
14
|
-
readonly port: number;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Hop-by-hop request headers (RFC 9110 §7.6.1) plus headers `fetch` manages
|
|
19
|
-
* itself; everything else is forwarded verbatim. `accept-encoding` is dropped
|
|
20
|
-
* so Vite answers identity-encoded and bytes can be piped through untouched.
|
|
21
|
-
*/
|
|
22
|
-
const SKIP_REQUEST_HEADERS = new Set([
|
|
23
|
-
'connection',
|
|
24
|
-
'keep-alive',
|
|
25
|
-
'proxy-authenticate',
|
|
26
|
-
'proxy-authorization',
|
|
27
|
-
'te',
|
|
28
|
-
'trailer',
|
|
29
|
-
'transfer-encoding',
|
|
30
|
-
'upgrade',
|
|
31
|
-
'content-length',
|
|
32
|
-
'accept-encoding',
|
|
33
|
-
]);
|
|
34
|
-
|
|
35
|
-
/** Response headers owned by the front server's own HTTP framing. */
|
|
36
|
-
const SKIP_RESPONSE_HEADERS = new Set([
|
|
37
|
-
'connection',
|
|
38
|
-
'keep-alive',
|
|
39
|
-
'content-length',
|
|
40
|
-
'content-encoding',
|
|
41
|
-
'transfer-encoding',
|
|
42
|
-
]);
|
|
43
|
-
|
|
44
|
-
/** Forwards one HTTP request to Vite and streams the answer back. */
|
|
45
|
-
export async function proxyToVite(
|
|
46
|
-
request: Request,
|
|
47
|
-
response: Response,
|
|
48
|
-
target: ViteTarget,
|
|
49
|
-
): Promise<void> {
|
|
50
|
-
const url = `http://${target.host}:${String(target.port)}${request.url}`;
|
|
51
|
-
|
|
52
|
-
const headers = new Headers();
|
|
53
|
-
for (const [name, value] of Object.entries(request.headers)) {
|
|
54
|
-
if (!SKIP_REQUEST_HEADERS.has(name.toLowerCase())) headers.set(name, value);
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
const hasBody = request.method !== 'GET' && request.method !== 'HEAD';
|
|
58
|
-
const body = hasBody ? await request.buffer() : undefined;
|
|
59
|
-
|
|
60
|
-
const upstream = await fetch(url, {
|
|
61
|
-
method: request.method,
|
|
62
|
-
headers,
|
|
63
|
-
body: body && body.length > 0 ? new Uint8Array(body) : undefined,
|
|
64
|
-
// The browser follows redirects itself; pass them through untouched.
|
|
65
|
-
redirect: 'manual',
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
response.status(upstream.status);
|
|
69
|
-
upstream.headers.forEach((value, name) => {
|
|
70
|
-
if (!SKIP_RESPONSE_HEADERS.has(name)) response.header(name, value);
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
// Buffer the full upstream body and send it with a content-length, instead
|
|
74
|
-
// of streaming it. uWS/hyper-express chunked-streaming of a `fetch` body
|
|
75
|
-
// emitted an INVALID chunked encoding in the browser
|
|
76
|
-
// (net::ERR_INVALID_CHUNKED_ENCODING), so client modules failed to load. Dev
|
|
77
|
-
// responses are finite (HMR rides the separate websocket), so buffering is
|
|
78
|
-
// safe and sidesteps the chunked framing entirely.
|
|
79
|
-
const buf = Buffer.from(await upstream.arrayBuffer());
|
|
80
|
-
if (buf.length === 0) {
|
|
81
|
-
response.send();
|
|
82
|
-
return;
|
|
83
|
-
}
|
|
84
|
-
response.send(buf);
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
/**
|
|
88
|
-
* A uWS message as something `WebSocket.send` accepts: text stays a string,
|
|
89
|
-
* binary is copied into a plain `ArrayBuffer`-backed view (a `Buffer` may sit
|
|
90
|
-
* on a shared pool slab, which the WebSocket types reject).
|
|
91
|
-
*/
|
|
92
|
-
function toUpstreamMessage(data: Buffer, binary: boolean): string | Uint8Array<ArrayBuffer> {
|
|
93
|
-
if (!binary) return data.toString('utf8');
|
|
94
|
-
const copy = new Uint8Array(data.length);
|
|
95
|
-
copy.set(data);
|
|
96
|
-
return copy;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
/**
|
|
100
|
-
* Wires a catch-all websocket route that pipes every upgrade (Vite's HMR
|
|
101
|
-
* socket connects to the page origin at `/`, subprotocol `vite-hmr`) to the
|
|
102
|
-
* internal Vite server. Messages are queued until the upstream socket opens,
|
|
103
|
-
* closes propagate in both directions.
|
|
104
|
-
*/
|
|
105
|
-
export function wireWebsocketProxy(app: Server, target: ViteTarget): void {
|
|
106
|
-
app.upgrade('/*', (request: Request, response: Response) => {
|
|
107
|
-
response.upgrade({
|
|
108
|
-
url: request.url,
|
|
109
|
-
protocol: request.headers['sec-websocket-protocol'] ?? '',
|
|
110
|
-
});
|
|
111
|
-
});
|
|
112
|
-
|
|
113
|
-
app.ws(
|
|
114
|
-
'/*',
|
|
115
|
-
{ message_type: 'Buffer', idle_timeout: 120, max_payload_length: 16 * 1024 * 1024 },
|
|
116
|
-
(ws: Websocket) => {
|
|
117
|
-
pipeToVite(ws, target, ws.context as { url: string; protocol: string });
|
|
118
|
-
},
|
|
119
|
-
);
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
/**
|
|
123
|
-
* Pipe ONE upgraded websocket to the internal Vite HMR server. The verbatim body extracted from
|
|
124
|
-
* {@link wireWebsocketProxy} so the dev STREAM router (doc 08 4.1 `wireStreams`) can reuse it for every
|
|
125
|
-
* NON-stream upgrade while it handles `@stream`-route upgrades itself - HMR stays byte-for-byte
|
|
126
|
-
* unchanged. `ctx` is the upgrade context (`{ url, protocol }`) the upgrade handler stamped.
|
|
127
|
-
*/
|
|
128
|
-
export function pipeToVite(
|
|
129
|
-
ws: Websocket,
|
|
130
|
-
target: ViteTarget,
|
|
131
|
-
ctx: { url: string; protocol: string },
|
|
132
|
-
): void {
|
|
133
|
-
const { url, protocol } = ctx;
|
|
134
|
-
const upstream = new WebSocket(
|
|
135
|
-
`ws://${target.host}:${String(target.port)}${url}`,
|
|
136
|
-
protocol ? protocol.split(',').map((p) => p.trim()) : [],
|
|
137
|
-
);
|
|
138
|
-
upstream.binaryType = 'arraybuffer';
|
|
139
|
-
|
|
140
|
-
const pending: (string | Uint8Array<ArrayBuffer>)[] = [];
|
|
141
|
-
let open = false;
|
|
142
|
-
|
|
143
|
-
upstream.onopen = (): void => {
|
|
144
|
-
open = true;
|
|
145
|
-
for (const m of pending) upstream.send(m);
|
|
146
|
-
pending.length = 0;
|
|
147
|
-
};
|
|
148
|
-
upstream.onmessage = (event: MessageEvent): void => {
|
|
149
|
-
if (typeof event.data === 'string') ws.send(event.data);
|
|
150
|
-
else ws.send(Buffer.from(event.data as ArrayBuffer), true);
|
|
151
|
-
};
|
|
152
|
-
upstream.onclose = (event: CloseEvent): void => {
|
|
153
|
-
ws.close(event.code, event.reason);
|
|
154
|
-
};
|
|
155
|
-
upstream.onerror = (): void => {
|
|
156
|
-
ws.close();
|
|
157
|
-
};
|
|
158
|
-
|
|
159
|
-
ws.on('message', (message: Buffer, isBinary: boolean) => {
|
|
160
|
-
const m = toUpstreamMessage(message, isBinary);
|
|
161
|
-
if (open) upstream.send(m);
|
|
162
|
-
else pending.push(m);
|
|
163
|
-
});
|
|
164
|
-
ws.on('close', () => {
|
|
165
|
-
if (
|
|
166
|
-
upstream.readyState === WebSocket.OPEN ||
|
|
167
|
-
upstream.readyState === WebSocket.CONNECTING
|
|
168
|
-
) {
|
|
169
|
-
upstream.close();
|
|
170
|
-
}
|
|
171
|
-
});
|
|
172
|
-
}
|