toiljs 0.0.115 → 0.0.117

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 (326) hide show
  1. package/CHANGELOG.md +10 -0
  2. package/README.md +16 -7
  3. package/TYPESCRIPT_LAW.md +1946 -2073
  4. package/build/backend/.tsbuildinfo +1 -1
  5. package/build/cli/.tsbuildinfo +1 -1
  6. package/build/cli/index.js +2375 -258
  7. package/build/client/.tsbuildinfo +1 -1
  8. package/build/client/dev/devtools.js +1 -1
  9. package/build/client/dev/error-overlay.js +1 -1
  10. package/build/client/routing/mount.js +1 -1
  11. package/build/compiler/.tsbuildinfo +1 -1
  12. package/build/compiler/auth-emails.js +7 -3
  13. package/build/compiler/emails.js +1 -1
  14. package/build/compiler/index.d.ts +1 -0
  15. package/build/compiler/index.js +25 -4
  16. package/build/compiler/pages.js +2 -3
  17. package/build/compiler/prerender.d.ts +3 -8
  18. package/build/compiler/prerender.js +88 -127
  19. package/build/compiler/ssg.js +3 -4
  20. package/build/compiler/template-build.js +3 -4
  21. package/build/compiler/toil-docs.generated.js +6 -5
  22. package/build/compiler/vite.js +3 -6
  23. package/build/devserver/.tsbuildinfo +1 -1
  24. package/build/devserver/db/database.js +7 -3
  25. package/build/devserver/db/types.js +1 -1
  26. package/build/devserver/production.js +4 -3
  27. package/build/devserver/runtime/crypto.d.ts +1 -1
  28. package/build/io/.tsbuildinfo +1 -1
  29. package/build/logger/.tsbuildinfo +1 -1
  30. package/build/shared/.tsbuildinfo +1 -1
  31. package/examples/basic/client/routes/analytics.tsx +7 -5
  32. package/examples/basic/server/tsconfig.json +1 -3
  33. package/package.json +45 -44
  34. package/presets/no-uint8array-tostring.js +120 -153
  35. package/presets/oxlint.d.ts +5 -0
  36. package/presets/oxlint.js +224 -0
  37. package/server/runtime/tsconfig.json +1 -3
  38. package/tsconfig.json +2 -11
  39. package/.github/ISSUE_TEMPLATE/bug_report.md +0 -38
  40. package/.github/ISSUE_TEMPLATE/bug_report.yml +0 -90
  41. package/.github/ISSUE_TEMPLATE/config.yml +0 -8
  42. package/.github/ISSUE_TEMPLATE/feature_request.md +0 -20
  43. package/.github/PULL_REQUEST_TEMPLATE.md +0 -43
  44. package/.github/changelog-config.json +0 -45
  45. package/.github/dependabot.yml +0 -27
  46. package/.github/workflows/ci.yml +0 -222
  47. package/.vscode/settings.json +0 -10
  48. package/docs/README.md +0 -107
  49. package/docs/auth/README.md +0 -104
  50. package/docs/auth/configuration.md +0 -105
  51. package/docs/auth/emails.md +0 -92
  52. package/docs/auth/extending.md +0 -202
  53. package/docs/auth/how-it-works.md +0 -138
  54. package/docs/auth/usage.md +0 -188
  55. package/docs/backend/README.md +0 -143
  56. package/docs/backend/data.md +0 -351
  57. package/docs/backend/rest.md +0 -402
  58. package/docs/backend/rpc.md +0 -226
  59. package/docs/background/README.md +0 -114
  60. package/docs/background/daemons.md +0 -276
  61. package/docs/background/derive.md +0 -179
  62. package/docs/cli/README.md +0 -379
  63. package/docs/concepts/ai-guide.md +0 -132
  64. package/docs/concepts/config.md +0 -416
  65. package/docs/concepts/decorators.md +0 -127
  66. package/docs/concepts/security.md +0 -108
  67. package/docs/concepts/tiers.md +0 -166
  68. package/docs/concepts/types.md +0 -216
  69. package/docs/database/README.md +0 -143
  70. package/docs/database/capacity.md +0 -350
  71. package/docs/database/counters.md +0 -174
  72. package/docs/database/documents.md +0 -277
  73. package/docs/database/events.md +0 -307
  74. package/docs/database/membership.md +0 -246
  75. package/docs/database/setup.md +0 -155
  76. package/docs/database/unique.md +0 -216
  77. package/docs/database/views.md +0 -246
  78. package/docs/frontend/README.md +0 -104
  79. package/docs/frontend/components.md +0 -286
  80. package/docs/frontend/data-fetching.md +0 -260
  81. package/docs/frontend/images.md +0 -148
  82. package/docs/frontend/metadata.md +0 -344
  83. package/docs/frontend/navigation.md +0 -296
  84. package/docs/frontend/rendering.md +0 -236
  85. package/docs/frontend/routing.md +0 -346
  86. package/docs/frontend/scripts.md +0 -118
  87. package/docs/frontend/search.md +0 -191
  88. package/docs/frontend/styling.md +0 -147
  89. package/docs/frontend/toil-global.md +0 -216
  90. package/docs/getting-started/README.md +0 -81
  91. package/docs/getting-started/create-project.md +0 -131
  92. package/docs/getting-started/deploy.md +0 -101
  93. package/docs/getting-started/first-app.md +0 -215
  94. package/docs/getting-started/installation.md +0 -124
  95. package/docs/getting-started/migrating.md +0 -125
  96. package/docs/getting-started/project-structure.md +0 -163
  97. package/docs/introduction/README.md +0 -66
  98. package/docs/introduction/design-principles.md +0 -81
  99. package/docs/introduction/distributed.md +0 -91
  100. package/docs/introduction/how-it-works.md +0 -121
  101. package/docs/introduction/hyperscale.md +0 -70
  102. package/docs/introduction/modern-stack.md +0 -85
  103. package/docs/introduction/vs-other-frameworks.md +0 -64
  104. package/docs/introduction/why-toil.md +0 -99
  105. package/docs/llms.txt +0 -95
  106. package/docs/realtime/README.md +0 -102
  107. package/docs/realtime/channels.md +0 -211
  108. package/docs/realtime/streams.md +0 -369
  109. package/docs/services/README.md +0 -60
  110. package/docs/services/analytics.md +0 -279
  111. package/docs/services/caching.md +0 -175
  112. package/docs/services/cookies.md +0 -235
  113. package/docs/services/crypto.md +0 -209
  114. package/docs/services/email.md +0 -289
  115. package/docs/services/environment.md +0 -141
  116. package/docs/services/ratelimit.md +0 -174
  117. package/docs/services/time.md +0 -85
  118. package/eslint.config.js +0 -57
  119. package/examples/basic/client/routes/docs/[...slug].tsx +0 -12
  120. package/presets/eslint.js +0 -88
  121. package/src/backend/index.ts +0 -204
  122. package/src/cli/configure.ts +0 -363
  123. package/src/cli/create.ts +0 -924
  124. package/src/cli/db.ts +0 -158
  125. package/src/cli/diagnostics.ts +0 -665
  126. package/src/cli/doctor.ts +0 -982
  127. package/src/cli/features.ts +0 -170
  128. package/src/cli/index.ts +0 -326
  129. package/src/cli/notify.ts +0 -120
  130. package/src/cli/proc.ts +0 -56
  131. package/src/cli/ui.ts +0 -169
  132. package/src/cli/update.ts +0 -248
  133. package/src/cli/updates.ts +0 -92
  134. package/src/cli/validate.ts +0 -31
  135. package/src/cli/version-check.ts +0 -125
  136. package/src/client/auth.ts +0 -869
  137. package/src/client/channel/channel.ts +0 -146
  138. package/src/client/components/Form.tsx +0 -65
  139. package/src/client/components/Image.tsx +0 -160
  140. package/src/client/components/Script.tsx +0 -113
  141. package/src/client/components/Slot.tsx +0 -21
  142. package/src/client/dev/devtools.tsx +0 -1089
  143. package/src/client/dev/error-overlay.tsx +0 -225
  144. package/src/client/errors.ts +0 -11
  145. package/src/client/head/head.ts +0 -182
  146. package/src/client/head/metadata.ts +0 -110
  147. package/src/client/index.ts +0 -130
  148. package/src/client/navigation/Link.tsx +0 -108
  149. package/src/client/navigation/NavLink.tsx +0 -86
  150. package/src/client/navigation/navigation.ts +0 -235
  151. package/src/client/navigation/prefetch.ts +0 -169
  152. package/src/client/navigation/scroll.ts +0 -53
  153. package/src/client/routing/Router.tsx +0 -216
  154. package/src/client/routing/action.ts +0 -122
  155. package/src/client/routing/error-boundary.tsx +0 -43
  156. package/src/client/routing/hooks.ts +0 -140
  157. package/src/client/routing/lazy.ts +0 -77
  158. package/src/client/routing/loader.ts +0 -381
  159. package/src/client/routing/match.ts +0 -47
  160. package/src/client/routing/mount.tsx +0 -90
  161. package/src/client/routing/params-context.ts +0 -10
  162. package/src/client/routing/slot-context.ts +0 -7
  163. package/src/client/rpc.ts +0 -97
  164. package/src/client/search/search.ts +0 -189
  165. package/src/client/search/use-page-search.ts +0 -73
  166. package/src/client/ssr/markers.tsx +0 -172
  167. package/src/client/stream/client.ts +0 -237
  168. package/src/client/types.ts +0 -73
  169. package/src/compiler/auth-emails.ts +0 -312
  170. package/src/compiler/config.ts +0 -369
  171. package/src/compiler/docs.ts +0 -127
  172. package/src/compiler/email-preview.ts +0 -314
  173. package/src/compiler/emails.ts +0 -404
  174. package/src/compiler/fonts.ts +0 -89
  175. package/src/compiler/generate.ts +0 -471
  176. package/src/compiler/image-blur.ts +0 -42
  177. package/src/compiler/image-report.ts +0 -88
  178. package/src/compiler/index.ts +0 -1362
  179. package/src/compiler/pages.ts +0 -49
  180. package/src/compiler/plugin.ts +0 -391
  181. package/src/compiler/prerender.ts +0 -263
  182. package/src/compiler/routes.ts +0 -170
  183. package/src/compiler/seo.ts +0 -415
  184. package/src/compiler/sri.ts +0 -144
  185. package/src/compiler/ssg.ts +0 -177
  186. package/src/compiler/ssr-codegen.ts +0 -85
  187. package/src/compiler/template-build.ts +0 -676
  188. package/src/compiler/template.ts +0 -262
  189. package/src/compiler/toil-docs.generated.ts +0 -77
  190. package/src/compiler/vite.ts +0 -245
  191. package/src/devserver/analytics/index.ts +0 -317
  192. package/src/devserver/config/dotenv.ts +0 -102
  193. package/src/devserver/config/env.ts +0 -30
  194. package/src/devserver/config/ratelimit.ts +0 -123
  195. package/src/devserver/daemon/catalog.ts +0 -120
  196. package/src/devserver/daemon/cron.ts +0 -87
  197. package/src/devserver/daemon/host.ts +0 -134
  198. package/src/devserver/daemon/index.ts +0 -369
  199. package/src/devserver/daemon/runtime.ts +0 -48
  200. package/src/devserver/db/catalog.ts +0 -109
  201. package/src/devserver/db/database.ts +0 -1834
  202. package/src/devserver/db/derives.ts +0 -121
  203. package/src/devserver/db/index.ts +0 -20
  204. package/src/devserver/db/routeKinds.ts +0 -191
  205. package/src/devserver/db/types.ts +0 -168
  206. package/src/devserver/email/caps.ts +0 -0
  207. package/src/devserver/email/config.ts +0 -141
  208. package/src/devserver/email/console.ts +0 -348
  209. package/src/devserver/email/index.ts +0 -155
  210. package/src/devserver/email/providers.ts +0 -131
  211. package/src/devserver/email/status.ts +0 -23
  212. package/src/devserver/email/validate.ts +0 -37
  213. package/src/devserver/email/wire.ts +0 -55
  214. package/src/devserver/http/cache.ts +0 -0
  215. package/src/devserver/http/envelope.ts +0 -157
  216. package/src/devserver/http/proxy.ts +0 -172
  217. package/src/devserver/http/runtime.ts +0 -387
  218. package/src/devserver/index.ts +0 -43
  219. package/src/devserver/production-ipc.ts +0 -63
  220. package/src/devserver/production-worker.ts +0 -83
  221. package/src/devserver/production.ts +0 -718
  222. package/src/devserver/runtime/crypto.ts +0 -587
  223. package/src/devserver/runtime/host.ts +0 -427
  224. package/src/devserver/runtime/module.ts +0 -442
  225. package/src/devserver/server.ts +0 -251
  226. package/src/devserver/ssr.ts +0 -208
  227. package/src/devserver/stream/catalog.ts +0 -106
  228. package/src/devserver/stream/host.ts +0 -42
  229. package/src/devserver/stream/index.ts +0 -301
  230. package/src/devserver/stream/manager.ts +0 -167
  231. package/src/devserver/stream/router.ts +0 -101
  232. package/src/devserver/stream/wire.ts +0 -58
  233. package/src/devserver/stream/ws.ts +0 -78
  234. package/src/devserver/wasm/sections.ts +0 -59
  235. package/src/devserver/wasm/surface.ts +0 -115
  236. package/src/io/FastMap.ts +0 -151
  237. package/src/io/FastSet.ts +0 -113
  238. package/src/io/codec.ts +0 -362
  239. package/src/io/index.ts +0 -10
  240. package/src/io/lengths.ts +0 -14
  241. package/src/io/types.ts +0 -19
  242. package/src/logger/index.ts +0 -22
  243. package/src/shared/index.ts +0 -46
  244. package/test/analytics-dev.test.ts +0 -113
  245. package/test/assembly/aspect-shim.ts +0 -33
  246. package/test/assembly/cookie.spec.ts +0 -303
  247. package/test/assembly/example.spec.ts +0 -22
  248. package/test/assembly/ssr.spec.ts +0 -95
  249. package/test/assembly.test.ts +0 -59
  250. package/test/auth-emails.test.ts +0 -118
  251. package/test/built-ssr.test.ts +0 -148
  252. package/test/channel.test.ts +0 -21
  253. package/test/config-host-port.test.ts +0 -59
  254. package/test/configure.test.ts +0 -94
  255. package/test/daemon-build.test.ts +0 -278
  256. package/test/daemon-catalog.test.ts +0 -349
  257. package/test/daemon-emulation.test.ts +0 -256
  258. package/test/db.test.ts +0 -0
  259. package/test/devserver-database.test.ts +0 -1102
  260. package/test/devserver-email.test.ts +0 -306
  261. package/test/devserver-pqauth.test.ts +0 -93
  262. package/test/devserver-secrets.test.ts +0 -63
  263. package/test/devserver.test.ts +0 -259
  264. package/test/doctor.test.ts +0 -298
  265. package/test/dom/Image.test.tsx +0 -144
  266. package/test/dom/Link.test.tsx +0 -67
  267. package/test/dom/NavLink.test.tsx +0 -37
  268. package/test/dom/Script.test.tsx +0 -48
  269. package/test/dom/action.test.tsx +0 -146
  270. package/test/dom/error-overlay.test.tsx +0 -44
  271. package/test/dom/loader.test.tsx +0 -121
  272. package/test/dom/navigation.test.ts +0 -59
  273. package/test/dom/revalidate.test.tsx +0 -38
  274. package/test/dom/route-head.test.tsx +0 -56
  275. package/test/dom/router-loading.test.tsx +0 -44
  276. package/test/dom/scroll.test.ts +0 -56
  277. package/test/dom/slot.test.tsx +0 -131
  278. package/test/dom/use-metadata.test.tsx +0 -58
  279. package/test/dom/view-transitions.test.tsx +0 -53
  280. package/test/dynamic-routing.test.ts +0 -132
  281. package/test/email-console.test.ts +0 -86
  282. package/test/email-preview.test.ts +0 -73
  283. package/test/emails.test.ts +0 -58
  284. package/test/errors.test.ts +0 -21
  285. package/test/example-guestbook.test.ts +0 -123
  286. package/test/features.test.ts +0 -149
  287. package/test/fixtures/bignum-wire/spec.ts +0 -25
  288. package/test/fixtures/daemon-app.ts +0 -65
  289. package/test/fixtures/stream-echo.ts +0 -26
  290. package/test/fixtures/stream-gate.ts +0 -28
  291. package/test/fixtures/stream-trap.ts +0 -18
  292. package/test/fixtures/stream-typed.ts +0 -41
  293. package/test/fonts.test.ts +0 -28
  294. package/test/global-setup.ts +0 -17
  295. package/test/head.test.ts +0 -42
  296. package/test/image-blur.test.ts +0 -50
  297. package/test/io.test.ts +0 -117
  298. package/test/metadata.test.ts +0 -40
  299. package/test/navlink.test.ts +0 -28
  300. package/test/pages.test.ts +0 -105
  301. package/test/placeholder.test.ts +0 -9
  302. package/test/pqauth-e2e.test.ts +0 -216
  303. package/test/pqauth-email.test.ts +0 -801
  304. package/test/prerender.test.ts +0 -116
  305. package/test/prettier-plugin.test.ts +0 -46
  306. package/test/routes.test.ts +0 -76
  307. package/test/rpc-bignum-wire.test.ts +0 -164
  308. package/test/rpc-dispatch.test.ts +0 -132
  309. package/test/rpc-kinds.test.ts +0 -18
  310. package/test/rpc.test.ts +0 -66
  311. package/test/search.test.ts +0 -114
  312. package/test/seo.test.ts +0 -175
  313. package/test/slot-layouts.test.ts +0 -69
  314. package/test/sri.test.ts +0 -129
  315. package/test/ssg.test.ts +0 -36
  316. package/test/ssr-hydration.test.tsx +0 -122
  317. package/test/ssr-render.test.ts +0 -197
  318. package/test/ssr-template.test.tsx +0 -482
  319. package/test/stream-emulation.test.ts +0 -479
  320. package/test/ui.test.ts +0 -40
  321. package/test/update.test.ts +0 -58
  322. package/test/validate.test.ts +0 -42
  323. package/test/version-check.test.ts +0 -130
  324. package/tests/data-parity/generated-parity.ts +0 -99
  325. package/tests/data-parity/parity.ts +0 -80
  326. package/tests/data-parity/spec.ts +0 -46
@@ -1,442 +0,0 @@
1
- /**
2
- * Loads the ToilScript-compiled server wasm and dispatches request envelopes
3
- * into it, the dev-mode equivalent of the edge's fresh dispatcher
4
- * (`toil-backend/src/wasm/dispatcher/fresh.rs`): one fresh instance per
5
- * request, so guest state can never leak between requests and a trapped
6
- * instance is simply dropped. Instantiation of a compiled module is
7
- * microseconds, irrelevant next to dev-mode I/O.
8
- *
9
- * The compiled module is cached and transparently recompiled when the wasm
10
- * file's mtime changes (the `toiljs dev` watcher rebuilds it via toilscript on
11
- * server-source edits), which is the server-side hot reload.
12
- */
13
-
14
- import fs from 'node:fs';
15
-
16
- import {
17
- commitDeriveCheckpoints,
18
- DbFunctionKind,
19
- type DeriveEntry,
20
- derivesForWrites,
21
- parseDerives,
22
- persistDb,
23
- setDbCatalog,
24
- } from '../db/index.js';
25
- import {
26
- parseRouteKinds,
27
- parseRpcKinds,
28
- routeKindForRequest,
29
- rpcKindForId,
30
- type RouteKindEntry,
31
- type RpcKindEntry,
32
- } from '../db/routeKinds.js';
33
- import {
34
- decodeResponseEnvelope,
35
- encodeRequestEnvelope,
36
- type EnvelopeRequest,
37
- unpackHandleResult,
38
- } from '../http/envelope.js';
39
- import { buildHostImports, freshDispatchState, type MemoryRef } from './host.js';
40
-
41
- export { WasmAbortError } from './host.js';
42
-
43
- /**
44
- * Marker header the server runtime puts on its fallback 404 (no `@rest` route
45
- * matched and no custom handler produced a response). The dev server strips it
46
- * and falls through to Vite; a deliberate `Response.notFound()` does not carry
47
- * it and is sent to the client as-is. Mirrors `TOIL_UNHANDLED_HEADER` in
48
- * `server/runtime/response.ts`.
49
- */
50
- export const UNHANDLED_HEADER = 'x-toil-unhandled';
51
-
52
- const WASM_PAGE = 65536;
53
-
54
- function dbKindForHttpMethod(method: string): DbFunctionKind {
55
- switch (method.toUpperCase()) {
56
- case 'GET':
57
- case 'HEAD':
58
- case 'OPTIONS':
59
- return DbFunctionKind.Query;
60
- case 'POST':
61
- case 'PUT':
62
- case 'PATCH':
63
- case 'DELETE':
64
- default:
65
- return DbFunctionKind.Action;
66
- }
67
- }
68
-
69
- export function dbFunctionKindForRequest(
70
- routes: readonly RouteKindEntry[],
71
- method: string,
72
- path: string,
73
- ): DbFunctionKind {
74
- const routeKind = routeKindForRequest(routes, method, path);
75
- return routeKind === DbFunctionKind.Query ? DbFunctionKind.Query : dbKindForHttpMethod(method);
76
- }
77
-
78
- /** The shaped outcome of one guest dispatch. */
79
- export interface WasmDispatchResult {
80
- readonly status: number;
81
- readonly headers: readonly (readonly [string, string])[];
82
- readonly body: Uint8Array;
83
- /** Path from a guest `respond_file` call; when set it replaces `body`. */
84
- readonly sendfile: string | null;
85
- /** True when the guest reported "no route matched" (the {@link UNHANDLED_HEADER} marker). */
86
- readonly unhandled: boolean;
87
- }
88
-
89
- interface HandleExports {
90
- readonly memory: WebAssembly.Memory;
91
- readonly handle: (reqOfs: number, reqLen: number) => bigint;
92
- }
93
-
94
- /** A `@database` with `@derive` methods exports `derive_run` (optional: absent
95
- * when the program declares no derive). See toilscript `injectDeriveHandler`. */
96
- interface DeriveExports {
97
- readonly memory: WebAssembly.Memory;
98
- readonly derive_run?: (deriveId: number) => bigint;
99
- }
100
-
101
- /** Host functions the dev server provides under `env` (see `host.ts`). */
102
- const PROVIDED_IMPORTS = new Set([
103
- 'abort',
104
- 'set_status',
105
- 'set_header',
106
- 'respond_file',
107
- 'thread_spawn',
108
- 'Date.now',
109
- 'client_ip',
110
- 'ratelimit_check',
111
- 'email_send',
112
- 'email_send_detached',
113
- 'env_get',
114
- 'env_get_secure',
115
- // Per-domain analytics (see ./analytics/index.ts).
116
- 'analytics_read',
117
- 'analytics_series',
118
- 'analytics_list_sites',
119
- // Web Crypto host functions (see ./crypto.ts).
120
- 'crypto.fill_random',
121
- 'crypto.random_uuid',
122
- 'crypto.take_result',
123
- 'crypto.digest',
124
- 'crypto.import_key',
125
- 'crypto.export_key',
126
- 'crypto.encrypt',
127
- 'crypto.decrypt',
128
- 'crypto.sign',
129
- 'crypto.verify',
130
- 'crypto.derive_bits',
131
- 'crypto.mldsa_verify',
132
- 'crypto.mlkem_decapsulate',
133
- 'crypto.voprf_evaluate',
134
- // ToilDB data API (see ./database.ts). Backed by ScyllaDB on the production
135
- // edge; backs the auth example's accounts + login challenges in dev.
136
- 'data.resolve_collection',
137
- 'data.get',
138
- 'data.get_many',
139
- 'data.exists',
140
- 'data.create',
141
- 'data.patch',
142
- 'data.upsert',
143
- 'data.delete',
144
- 'data.get_delete',
145
- 'data.unique_lookup',
146
- 'data.unique_claim',
147
- 'data.unique_release',
148
- 'data.view_get',
149
- 'data.view_publish',
150
- 'data.membership_contains',
151
- 'data.membership_add',
152
- 'data.membership_remove',
153
- 'data.membership_list',
154
- 'data.counter_get',
155
- 'data.counter_add',
156
- 'data.append',
157
- 'data.append_once',
158
- 'data.enqueue',
159
- 'data.latest',
160
- 'data.events_since',
161
- 'data.capacity_set_total',
162
- 'data.capacity_available',
163
- 'data.capacity_reserve',
164
- 'data.capacity_confirm',
165
- 'data.capacity_cancel',
166
- 'data.take_result',
167
- 'data.result_schema_version',
168
- 'data.write_allowed',
169
- ]);
170
-
171
- export class WasmServerModule {
172
- private module: WebAssembly.Module | null = null;
173
- private loadedMtimeMs = -1;
174
- private routeKinds: readonly RouteKindEntry[] = [];
175
- private rpcKinds: readonly RpcKindEntry[] = [];
176
- private derives: readonly DeriveEntry[] = [];
177
- // Set when a (re)compile loaded a module with @derive methods; the first
178
- // dispatch afterward rebuilds every materialized view from its sources.
179
- private derivesDirty = false;
180
-
181
- constructor(private readonly wasmPath: string) {}
182
-
183
- /** Whether a compiled module is currently available to dispatch into. */
184
- get available(): boolean {
185
- return this.module !== null;
186
- }
187
-
188
- /**
189
- * (Re)compile when the wasm file appeared or changed since the last load.
190
- * Returns `true` when a (re)compile happened, `false` when the cached
191
- * module is still current or the file is missing (`available` tells the
192
- * caller which).
193
- */
194
- refresh(): boolean {
195
- let mtimeMs: number;
196
- try {
197
- mtimeMs = fs.statSync(this.wasmPath).mtimeMs;
198
- } catch {
199
- this.module = null;
200
- this.routeKinds = [];
201
- this.rpcKinds = [];
202
- this.derives = [];
203
- this.derivesDirty = false;
204
- this.loadedMtimeMs = -1;
205
- return false;
206
- }
207
- if (this.module !== null && mtimeMs === this.loadedMtimeMs) return false;
208
-
209
- const bytes = fs.readFileSync(this.wasmPath);
210
- const module = new WebAssembly.Module(bytes);
211
- this.assertImportSurface(module);
212
- this.assertExportSurface(module);
213
- // Refresh collection -> current schema_version so writes stamp the live layout;
214
- // after a @data type evolves + rebuild, old on-disk rows now look out of date.
215
- setDbCatalog(bytes);
216
- this.routeKinds = parseRouteKinds(bytes);
217
- this.rpcKinds = parseRpcKinds(bytes);
218
- this.derives = parseDerives(bytes);
219
- this.module = module;
220
- this.loadedMtimeMs = mtimeMs;
221
- // Rebuild materialized views from their sources on the first dispatch
222
- // (after persistence is configured), so a freshly-loaded box serves
223
- // up-to-date views even after an out-of-band change to the source data.
224
- this.derivesDirty = this.derives.length > 0;
225
- return true;
226
- }
227
-
228
- /**
229
- * Run one request through a fresh guest instance. Throws on a guest trap
230
- * (including ToilScript `abort`), a malformed response envelope, or a
231
- * missing module; the caller shapes those into a 500.
232
- */
233
- dispatch(req: EnvelopeRequest): WasmDispatchResult {
234
- if (this.module === null) throw new Error(`server wasm not loaded (${this.wasmPath})`);
235
-
236
- // First dispatch after a (re)load: materialize views from their sources
237
- // before serving the request, so reads see fresh views.
238
- this.rebuildDerivedViewsIfStale();
239
-
240
- const envelope = encodeRequestEnvelope(req);
241
-
242
- const ref: MemoryRef = { memory: null };
243
- const state = freshDispatchState();
244
- state.clientIp = req.clientIp ?? '';
245
- // Match the edge DB gate: the HTTP method is the baseline authority, and `toildb.route_kinds`
246
- // can only tighten a mutating route to query. The reserved /__toil_rpc endpoint is the inverse -
247
- // a @remote defaults to read-only (Query); only an @action @remote (in rpc_kinds) may write.
248
- const rpcPath = req.path.split('?')[0] ?? req.path;
249
- const rpcMethod = req.method.toUpperCase();
250
- const rpcMutating =
251
- rpcMethod === 'POST' || rpcMethod === 'PUT' || rpcMethod === 'PATCH' || rpcMethod === 'DELETE';
252
- if (rpcPath === '/__toil_rpc' && rpcMutating) {
253
- const idHeader = req.headers.find(([n]) => n.toLowerCase() === 'dacely-rpc')?.[1];
254
- // Strict u32 parse, mirroring the host's `v.parse::<u32>()`: reject trailing garbage/whitespace
255
- // and out-of-range ids so a malformed header falls through to read-only Query, exactly as prod.
256
- const id = idHeader !== undefined && /^\d+$/.test(idHeader) ? Number(idHeader) : NaN;
257
- state.db.functionKind =
258
- Number.isInteger(id) && id <= 0xffffffff
259
- ? rpcKindForId(this.rpcKinds, id)
260
- : DbFunctionKind.Query;
261
- } else {
262
- state.db.functionKind = dbFunctionKindForRequest(this.routeKinds, req.method, req.path);
263
- }
264
- const instance = new WebAssembly.Instance(this.module, buildHostImports(ref, state));
265
- const exports = instance.exports as unknown as HandleExports;
266
- ref.memory = exports.memory;
267
-
268
- // The edge writes the envelope at offset 0, which only holds for tiny
269
- // requests: ToilScript static data starts at offset 1024, so anything
270
- // larger would corrupt the guest. `handle(req_ofs, req_len)` takes the
271
- // offset as a parameter, so we stay ABI-compatible and write past the
272
- // current end of linear memory instead (grown to fit). The guest heap
273
- // grows upward from its data section and copies the envelope into
274
- // managed objects on decode, well before it could reach this region.
275
- const reqOfs = exports.memory.buffer.byteLength;
276
- exports.memory.grow(Math.ceil(envelope.length / WASM_PAGE) || 1);
277
- Buffer.from(exports.memory.buffer).set(envelope, reqOfs);
278
-
279
- const packed = exports.handle(reqOfs, envelope.length);
280
- const { ptr, len } = unpackHandleResult(packed);
281
-
282
- // Same bounds validation as the edge: never trust the guest's pointer.
283
- const memSize = exports.memory.buffer.byteLength;
284
- if (len > memSize || ptr + len > memSize)
285
- throw new Error(
286
- `guest returned an out-of-bounds response: ptr=${String(ptr)} len=${String(len)}`,
287
- );
288
-
289
- const resp = decodeResponseEnvelope(new Uint8Array(exports.memory.buffer, ptr, len));
290
-
291
- // Merge the imperative host-import state on top of the envelope (a
292
- // toiljs guest answers fully in-band, so this is usually a no-op).
293
- const headers: (readonly [string, string])[] = [...resp.headers, ...state.headers];
294
- const status = state.status ?? resp.status;
295
-
296
- const unhandled = headers.some(([n]) => n.toLowerCase() === UNHANDLED_HEADER);
297
-
298
- // Materialize: re-run any @derive whose @database had a source
299
- // collection written this request, under FunctionKind=Derive, so its
300
- // view.publish lands BEFORE the single persistDb() below flushes both the
301
- // request's writes and the derive's view to disk. A read writes nothing,
302
- // so this is a no-op for GETs.
303
- this.runAffectedDerives(state.db.writtenCollections);
304
-
305
- // Flush any DB writes this request made to disk, so dev data survives a
306
- // restart (and a crash never loses an already-served write).
307
- persistDb();
308
-
309
- return {
310
- status,
311
- headers: headers.filter(([n]) => n.toLowerCase() !== UNHANDLED_HEADER),
312
- body: resp.body,
313
- sendfile: state.sendfile,
314
- unhandled,
315
- };
316
- }
317
-
318
- /**
319
- * Run one request through the guest `render` entrypoint (edge SSR). Returns
320
- * the raw values-envelope bytes (status + template_hash + headers + slot
321
- * values) that the edge splices against the template manifest. Throws if
322
- * the module has no `render` export. Mirrors {@link dispatch}'s fresh-
323
- * instance + grow-and-write-envelope contract.
324
- */
325
- dispatchRender(req: EnvelopeRequest): Uint8Array {
326
- if (this.module === null) throw new Error(`server wasm not loaded (${this.wasmPath})`);
327
-
328
- const envelope = encodeRequestEnvelope(req);
329
- const ref: MemoryRef = { memory: null };
330
- const state = freshDispatchState();
331
- state.clientIp = req.clientIp ?? '';
332
- state.db.functionKind = DbFunctionKind.Query;
333
- const instance = new WebAssembly.Instance(this.module, buildHostImports(ref, state));
334
- const exports = instance.exports as unknown as HandleExports & {
335
- render?: (reqOfs: number, reqLen: number) => bigint;
336
- };
337
- if (typeof exports.render !== 'function')
338
- throw new Error(`guest wasm has no 'render' export (${this.wasmPath})`);
339
- ref.memory = exports.memory;
340
-
341
- const reqOfs = exports.memory.buffer.byteLength;
342
- exports.memory.grow(Math.ceil(envelope.length / WASM_PAGE) || 1);
343
- Buffer.from(exports.memory.buffer).set(envelope, reqOfs);
344
-
345
- const packed = exports.render(reqOfs, envelope.length);
346
- const { ptr, len } = unpackHandleResult(packed);
347
-
348
- const memSize = exports.memory.buffer.byteLength;
349
- if (len > memSize || ptr + len > memSize)
350
- throw new Error(
351
- `guest returned out-of-bounds values: ptr=${String(ptr)} len=${String(len)}`,
352
- );
353
- // Copy out of the (about-to-be-dropped) instance's memory.
354
- return new Uint8Array(exports.memory.buffer, ptr, len).slice();
355
- }
356
-
357
- /**
358
- * After a mutating dispatch, re-run each `@derive` whose `@database` had a
359
- * source collection written, on a fresh instance under FunctionKind=Derive.
360
- * Each derive recomputes + `view.publish`es its materialized view (a later
361
- * Query route reads it via the non-scan `view.get`). A trapped derive is
362
- * logged and skipped: the request has already succeeded and the next write
363
- * re-derives, so a stale view is the worst case. Mirrors the edge runner,
364
- * which folds events into the view off the request path; in single-process
365
- * dev, running it synchronously-after-the-write is observably equivalent.
366
- */
367
- private runAffectedDerives(written: ReadonlySet<string>): void {
368
- if (this.module === null) return;
369
- for (const derive of derivesForWrites(this.derives, written)) {
370
- try {
371
- this.runDerive(derive.deriveId);
372
- } catch (err) {
373
- console.error(`[toil] derive ${derive.dbName}#${derive.methodName} failed:`, err);
374
- }
375
- }
376
- }
377
-
378
- /**
379
- * On the first dispatch after a (re)compile, rebuild every materialized view
380
- * from its sources (server start, hot-reload, or an out-of-band change to the
381
- * persisted source data). Runs once per load; ongoing writes are materialized
382
- * incrementally by {@link runAffectedDerives}. Mirrors the edge rebuilding a
383
- * view from its event log when a box first comes up.
384
- */
385
- private rebuildDerivedViewsIfStale(): void {
386
- if (!this.derivesDirty) return;
387
- this.derivesDirty = false;
388
- for (const derive of this.derives) {
389
- try {
390
- this.runDerive(derive.deriveId);
391
- } catch (err) {
392
- console.error(
393
- `[toil] derive ${derive.dbName}#${derive.methodName} failed on load:`,
394
- err,
395
- );
396
- }
397
- }
398
- }
399
-
400
- /** One derive invocation: a fresh instance under Derive kind, calling the
401
- * synthesized `derive_run(derive_id)` export (writes flow to the shared dev
402
- * store via the `data.*` imports; the caller's persistDb() flushes them). */
403
- private runDerive(deriveId: number): void {
404
- if (this.module === null) return;
405
- const ref: MemoryRef = { memory: null };
406
- const state = freshDispatchState();
407
- state.db.functionKind = DbFunctionKind.Derive;
408
- state.db.deriveId = deriveId;
409
- const instance = new WebAssembly.Instance(this.module, buildHostImports(ref, state));
410
- const exports = instance.exports as unknown as DeriveExports;
411
- ref.memory = exports.memory;
412
- if (typeof exports.derive_run !== 'function') return;
413
- exports.derive_run(deriveId);
414
- // Publish-then-persist: derive_run returned (its `view.publish`es landed synchronously in the dev
415
- // store), so commit the `events.since` cursor advances staged this run. A trapped fold throws above
416
- // and never reaches here, leaving the durable cursor for a re-read on the next trigger.
417
- commitDeriveCheckpoints(state.db);
418
- }
419
-
420
- /** Fail instantiation up front, with names, when the guest needs imports we do not provide. */
421
- private assertImportSurface(module: WebAssembly.Module): void {
422
- const missing = WebAssembly.Module.imports(module)
423
- .filter(
424
- (i) =>
425
- i.kind === 'function' && (i.module !== 'env' || !PROVIDED_IMPORTS.has(i.name)),
426
- )
427
- .map((i) => `${i.module}.${i.name}`);
428
- if (missing.length > 0)
429
- throw new Error(
430
- `server wasm imports unsupported host functions: ${missing.join(', ')}`,
431
- );
432
- }
433
-
434
- /** The dispatcher needs the `handle` entrypoint and the exported linear memory. */
435
- private assertExportSurface(module: WebAssembly.Module): void {
436
- const names = new Set(WebAssembly.Module.exports(module).map((e) => e.name));
437
- for (const required of ['handle', 'memory']) {
438
- if (!names.has(required))
439
- throw new Error(`server wasm does not export \`${required}\``);
440
- }
441
- }
442
- }
@@ -1,251 +0,0 @@
1
- /**
2
- * The toiljs WASM dev server: a uWebSockets.js front (via @dacely/hyper-express,
3
- * the same stack as `toiljs/backend`) that dispatches HTTP requests into the
4
- * ToilScript-compiled server wasm exactly like the production edge does, and
5
- * proxies everything the server does not claim to an internal Vite dev server,
6
- * so dev keeps 100% of Vite's behavior (HMR, transforms, toolbar endpoints,
7
- * public assets, SPA fallback).
8
- *
9
- * Request flow:
10
- *
11
- * browser ── uWS :port ──► wasm `handle()` (fresh instance, envelope ABI)
12
- * │ │
13
- * │ └─ "unhandled" marker (no route matched)
14
- * ▼ │
15
- * Vite dev server (loopback) ◄──────────────┘
16
- *
17
- * Dev intentionally skips the edge's metering, gas, pooling and snapshot-reset
18
- * machinery; the ABI (envelope layout, `handle(ofs, len) -> i64`, host import
19
- * surface, trap isolation) is identical so a server that runs here runs there.
20
- */
21
-
22
- import path from 'node:path';
23
-
24
- import { Server } from '@dacely/hyper-express';
25
- import pc from 'picocolors';
26
-
27
- import type { EmailBackendConfig } from 'toiljs/shared';
28
-
29
- import type { ResolvedDaemonConfig } from './daemon/host.js';
30
- import { startDaemonRuntime } from './daemon/runtime.js';
31
- import { configureDbPersistence } from './db/index.js';
32
- import { initEmailService } from './email/index.js';
33
- import { proxyToVite, type ViteTarget, wireWebsocketProxy } from './http/proxy.js';
34
- import {
35
- assembleRouteSsr,
36
- dispatchWasmRequest,
37
- installRuntimeErrorHandler,
38
- isDispatchableMethod,
39
- runtimeServerOptions,
40
- sendSsr,
41
- } from './http/runtime.js';
42
- import { WasmServerModule } from './runtime/module.js';
43
- import { StreamRouter } from './stream/router.js';
44
- import { streamEmulationEnabled, wireStreams } from './stream/wire.js';
45
- import { buildSsrRoutes, type DevSsrTemplate, pathnameOf } from './ssr.js';
46
-
47
- /**
48
- * Paths that are Vite's own by construction; skipping the wasm round-trip for
49
- * them keeps the hot path of module serving untouched. Everything else is
50
- * offered to the server first (it answers or yields via the unhandled marker).
51
- */
52
- const VITE_PREFIXES = ['/@', '/node_modules/', '/__toil/'];
53
-
54
- /** Options for {@link startDevServer}. */
55
- export interface DevServerOptions {
56
- /** Project root; `respond_file` paths resolve against it (and may not escape it). */
57
- readonly root: string;
58
- /** Public listening port (the one the browser opens). */
59
- readonly port: number;
60
- /** Bind host. Default `127.0.0.1`. */
61
- readonly host?: string;
62
- /** Absolute path to the ToilScript server wasm (toilconfig `targets.release.outFile`). */
63
- readonly wasmFile: string;
64
- /**
65
- * Absolute path to the cold daemon artifact (`release-cold.wasm`). When present and the cold
66
- * artifact declares a daemon surface, the dev daemon emulator drives its `@scheduled` tasks
67
- * (per `nodeMode`). Omit for a project with no `@daemon` (the file is never built).
68
- */
69
- readonly coldWasmFile?: string;
70
- /**
71
- * Absolute path to the stream artifact (`release-stream.wasm`). When present and this dev process
72
- * serves streams (`nodeMode` regional/continental/all), the dev stream router (doc 08 4.1) serves
73
- * `@stream`-route WebSocket upgrades. Omit for a project with no `@stream` (the file is never built).
74
- */
75
- readonly streamWasmFile?: string;
76
- /** Which layer the dev process emulates (gates the daemon + stream emulators). Default `all`. */
77
- readonly nodeMode?: string;
78
- /** Daemon (L4) config mirror (drives the dev scheduler's budgets/caps). */
79
- readonly daemon?: ResolvedDaemonConfig;
80
- /** The internal Vite dev server to proxy unclaimed traffic to. */
81
- readonly vite: ViteTarget;
82
- /** Max request body bytes. Default 8 MB. */
83
- readonly maxBodyLength?: number;
84
- /**
85
- * The `toil.config.ts` `server.email` section (non-secret). When set (and the
86
- * API key is in `.env.secrets`), `EmailService.send` really sends in dev;
87
- * otherwise it stays a log-only mock. See `./email`.
88
- */
89
- readonly email?: EmailBackendConfig;
90
- /**
91
- * Edge-SSR templates (one per `ssr = true` route), extracted at dev startup
92
- * against the live dev shell. When a GET/HEAD matches a route the dev server
93
- * runs the guest `render`, splices the values, and serves the SSR HTML (same
94
- * path as the prod edge). Omit / empty for a project with no SSR route.
95
- */
96
- readonly ssrTemplates?: readonly DevSsrTemplate[];
97
- }
98
-
99
- /** A running dev server. */
100
- export interface RunningDevServer {
101
- readonly port: number;
102
- readonly host: string;
103
- /** Gracefully shuts the front server down (the Vite server is owned by the caller). */
104
- close(): Promise<void>;
105
- }
106
-
107
- /** True for requests that belong to Vite by construction (never offered to the wasm). */
108
- function isViteInternal(url: string): boolean {
109
- return VITE_PREFIXES.some((p) => url.startsWith(p));
110
- }
111
-
112
- /**
113
- * Starts the front server. The caller owns the Vite dev server (start it on a
114
- * loopback port first) and the toilscript rebuild watcher; this watches only
115
- * the wasm artifact and hot-swaps the compiled module when it changes.
116
- */
117
- export async function startDevServer(options: DevServerOptions): Promise<RunningDevServer> {
118
- const host = options.host ?? '127.0.0.1';
119
- const root = path.resolve(options.root);
120
-
121
- // Wire the email service from toil.config `server.email` + `.env.secrets`
122
- // (TOIL_EMAIL_*). Configured -> real sends; otherwise the import stays a
123
- // log-only mock. A partial-but-invalid config logs why it stayed off.
124
- const emailInit = initEmailService(root, options.email);
125
- if (emailInit.service !== null) {
126
- process.stdout.write(pc.dim(` ✉ email enabled: ${emailInit.note}`) + '\n');
127
- } else if (emailInit.note !== null) {
128
- process.stdout.write(pc.yellow(' ! ') + pc.dim(`email off: ${emailInit.note}`) + '\n');
129
- }
130
-
131
- const module = new WasmServerModule(options.wasmFile);
132
-
133
- // Edge-SSR routes (extracted against the live dev shell at startup). When a
134
- // GET/HEAD matches one, the dev server runs the guest `render`, splices the
135
- // values into the template, and serves the SSR HTML (prod-edge parity).
136
- const ssrRoutes = buildSsrRoutes(options.ssrTemplates ?? []);
137
- if (ssrRoutes.length > 0) {
138
- process.stdout.write(
139
- pc.dim(` edge SSR: ${String(ssrRoutes.length)} route(s) served server-side`) + '\n',
140
- );
141
- }
142
-
143
- // Persist dev DB data under the project's .toil/ so records, events, and their
144
- // schema_versions survive restarts (delete .toil/devdata.json to reset). Only
145
- // the running dev server persists; tests that construct WasmServerModule
146
- // directly stay purely in-memory.
147
- configureDbPersistence(path.join(root, '.toil', 'devdata.json'));
148
-
149
- let warnedMissing = false;
150
- let loadedOnce = false;
151
- const refresh = (): void => {
152
- try {
153
- if (module.refresh() && loadedOnce) {
154
- process.stdout.write(pc.green(' ✓ ') + pc.dim('server module reloaded') + '\n');
155
- }
156
- loadedOnce ||= module.available;
157
- } catch (e) {
158
- process.stdout.write(pc.red(` ✗ server wasm failed to load: ${String(e)}`) + '\n');
159
- }
160
- if (!module.available && !warnedMissing) {
161
- warnedMissing = true;
162
- process.stdout.write(
163
- pc.yellow(' ! ') +
164
- pc.dim(`server wasm not found at ${options.wasmFile}; serving client only`) +
165
- '\n',
166
- );
167
- }
168
- };
169
- refresh();
170
-
171
- const app = new Server(runtimeServerOptions(options));
172
- installRuntimeErrorHandler(app);
173
-
174
- const nodeMode = options.nodeMode ?? 'all';
175
-
176
- // Dev STREAM (L2/L3) emulation (doc 08 4.1): when a stream artifact is built AND this dev process
177
- // serves streams, route `@stream`-route WebSocket upgrades to the resident-box stream router and
178
- // proxy everything else (Vite HMR) upstream; otherwise the plain Vite proxy (existing behaviour).
179
- if (options.streamWasmFile !== undefined && streamEmulationEnabled(nodeMode)) {
180
- wireStreams(app, options.vite, new StreamRouter(options.streamWasmFile));
181
- } else {
182
- wireWebsocketProxy(app, options.vite);
183
- }
184
-
185
- const daemon = startDaemonRuntime({
186
- coldWasmFile: options.coldWasmFile,
187
- nodeMode,
188
- daemon: options.daemon,
189
- });
190
-
191
- app.any('/*', async (request, response) => {
192
- response.removeHeader('uWebSockets');
193
-
194
- const dispatchable = !isViteInternal(request.url) && isDispatchableMethod(request.method);
195
- if (dispatchable) refresh();
196
-
197
- if (dispatchable && module.available) {
198
- const dispatch = await dispatchWasmRequest({
199
- module,
200
- request,
201
- response,
202
- root,
203
- cacheHost: request.headers.host ?? 'dev',
204
- serverHeader: 'toil-dev',
205
- errorPrefix: '✗',
206
- });
207
- if (dispatch.handled) {
208
- return;
209
- }
210
-
211
- // Edge SSR: handle() did not claim this path; if it matches an
212
- // `ssr = true` route, run the guest `render`, splice the values, and
213
- // serve the server-rendered HTML. A fail-safe envelope (no renderer
214
- // matched / malformed) returns null, so we fall through to Vite (the
215
- // route then client-renders, same as before).
216
- if ((request.method === 'GET' || request.method === 'HEAD') && ssrRoutes.length > 0) {
217
- const route = ssrRoutes.find((r) => r.test(pathnameOf(request.url)));
218
- if (route) {
219
- try {
220
- // A static route (its template has no holes -> no slots) needs no guest
221
- // render: serve the prerendered template directly so it paints instantly
222
- // instead of falling through to a (blank-until-JS) client render. Dynamic
223
- // routes run the guest `render` and splice its values in.
224
- const out = assembleRouteSsr(route, module, dispatch.envelopeReq);
225
- if (out !== null) {
226
- sendSsr(response, out, request.method === 'HEAD', 'toil-dev');
227
- return;
228
- }
229
- } catch (e) {
230
- process.stdout.write(
231
- pc.red(` ✗ SSR ${request.path}: ${String(e)}`) + '\n',
232
- );
233
- }
234
- }
235
- }
236
- }
237
-
238
- await proxyToVite(request, response, options.vite);
239
- });
240
-
241
- await app.listen(options.port, host);
242
-
243
- return {
244
- port: options.port,
245
- host,
246
- close: async (): Promise<void> => {
247
- daemon?.close();
248
- await app.shutdown();
249
- },
250
- };
251
- }