toiljs 0.0.116 → 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 (325) hide show
  1. package/CHANGELOG.md +5 -0
  2. package/README.md +16 -7
  3. package/build/backend/.tsbuildinfo +1 -1
  4. package/build/cli/.tsbuildinfo +1 -1
  5. package/build/cli/index.js +2375 -258
  6. package/build/client/.tsbuildinfo +1 -1
  7. package/build/client/dev/devtools.js +1 -1
  8. package/build/client/dev/error-overlay.js +1 -1
  9. package/build/client/routing/mount.js +1 -1
  10. package/build/compiler/.tsbuildinfo +1 -1
  11. package/build/compiler/auth-emails.js +7 -3
  12. package/build/compiler/emails.js +1 -1
  13. package/build/compiler/index.d.ts +1 -0
  14. package/build/compiler/index.js +25 -4
  15. package/build/compiler/pages.js +2 -3
  16. package/build/compiler/prerender.d.ts +3 -8
  17. package/build/compiler/prerender.js +88 -127
  18. package/build/compiler/ssg.js +3 -4
  19. package/build/compiler/template-build.js +3 -4
  20. package/build/compiler/toil-docs.generated.js +6 -5
  21. package/build/compiler/vite.js +3 -6
  22. package/build/devserver/.tsbuildinfo +1 -1
  23. package/build/devserver/db/database.js +7 -3
  24. package/build/devserver/db/types.js +1 -1
  25. package/build/devserver/production.js +4 -3
  26. package/build/devserver/runtime/crypto.d.ts +1 -1
  27. package/build/io/.tsbuildinfo +1 -1
  28. package/build/logger/.tsbuildinfo +1 -1
  29. package/build/shared/.tsbuildinfo +1 -1
  30. package/examples/basic/client/routes/analytics.tsx +7 -5
  31. package/examples/basic/server/tsconfig.json +1 -3
  32. package/package.json +45 -44
  33. package/presets/no-uint8array-tostring.js +120 -153
  34. package/presets/oxlint.d.ts +5 -0
  35. package/presets/oxlint.js +224 -0
  36. package/server/runtime/tsconfig.json +1 -3
  37. package/tsconfig.json +2 -11
  38. package/.github/ISSUE_TEMPLATE/bug_report.md +0 -38
  39. package/.github/ISSUE_TEMPLATE/bug_report.yml +0 -90
  40. package/.github/ISSUE_TEMPLATE/config.yml +0 -8
  41. package/.github/ISSUE_TEMPLATE/feature_request.md +0 -20
  42. package/.github/PULL_REQUEST_TEMPLATE.md +0 -43
  43. package/.github/changelog-config.json +0 -45
  44. package/.github/dependabot.yml +0 -27
  45. package/.github/workflows/ci.yml +0 -222
  46. package/.vscode/settings.json +0 -10
  47. package/docs/README.md +0 -107
  48. package/docs/auth/README.md +0 -104
  49. package/docs/auth/configuration.md +0 -105
  50. package/docs/auth/emails.md +0 -92
  51. package/docs/auth/extending.md +0 -202
  52. package/docs/auth/how-it-works.md +0 -138
  53. package/docs/auth/usage.md +0 -188
  54. package/docs/backend/README.md +0 -143
  55. package/docs/backend/data.md +0 -351
  56. package/docs/backend/rest.md +0 -402
  57. package/docs/backend/rpc.md +0 -226
  58. package/docs/background/README.md +0 -114
  59. package/docs/background/daemons.md +0 -276
  60. package/docs/background/derive.md +0 -179
  61. package/docs/cli/README.md +0 -379
  62. package/docs/concepts/ai-guide.md +0 -132
  63. package/docs/concepts/config.md +0 -416
  64. package/docs/concepts/decorators.md +0 -127
  65. package/docs/concepts/security.md +0 -108
  66. package/docs/concepts/tiers.md +0 -166
  67. package/docs/concepts/types.md +0 -216
  68. package/docs/database/README.md +0 -143
  69. package/docs/database/capacity.md +0 -350
  70. package/docs/database/counters.md +0 -174
  71. package/docs/database/documents.md +0 -277
  72. package/docs/database/events.md +0 -307
  73. package/docs/database/membership.md +0 -246
  74. package/docs/database/setup.md +0 -155
  75. package/docs/database/unique.md +0 -216
  76. package/docs/database/views.md +0 -246
  77. package/docs/frontend/README.md +0 -104
  78. package/docs/frontend/components.md +0 -286
  79. package/docs/frontend/data-fetching.md +0 -260
  80. package/docs/frontend/images.md +0 -148
  81. package/docs/frontend/metadata.md +0 -344
  82. package/docs/frontend/navigation.md +0 -296
  83. package/docs/frontend/rendering.md +0 -236
  84. package/docs/frontend/routing.md +0 -346
  85. package/docs/frontend/scripts.md +0 -118
  86. package/docs/frontend/search.md +0 -191
  87. package/docs/frontend/styling.md +0 -147
  88. package/docs/frontend/toil-global.md +0 -216
  89. package/docs/getting-started/README.md +0 -81
  90. package/docs/getting-started/create-project.md +0 -131
  91. package/docs/getting-started/deploy.md +0 -101
  92. package/docs/getting-started/first-app.md +0 -215
  93. package/docs/getting-started/installation.md +0 -124
  94. package/docs/getting-started/migrating.md +0 -125
  95. package/docs/getting-started/project-structure.md +0 -163
  96. package/docs/introduction/README.md +0 -66
  97. package/docs/introduction/design-principles.md +0 -81
  98. package/docs/introduction/distributed.md +0 -91
  99. package/docs/introduction/how-it-works.md +0 -121
  100. package/docs/introduction/hyperscale.md +0 -70
  101. package/docs/introduction/modern-stack.md +0 -85
  102. package/docs/introduction/vs-other-frameworks.md +0 -64
  103. package/docs/introduction/why-toil.md +0 -99
  104. package/docs/llms.txt +0 -95
  105. package/docs/realtime/README.md +0 -102
  106. package/docs/realtime/channels.md +0 -211
  107. package/docs/realtime/streams.md +0 -369
  108. package/docs/services/README.md +0 -60
  109. package/docs/services/analytics.md +0 -279
  110. package/docs/services/caching.md +0 -175
  111. package/docs/services/cookies.md +0 -235
  112. package/docs/services/crypto.md +0 -209
  113. package/docs/services/email.md +0 -289
  114. package/docs/services/environment.md +0 -141
  115. package/docs/services/ratelimit.md +0 -174
  116. package/docs/services/time.md +0 -85
  117. package/eslint.config.js +0 -57
  118. package/examples/basic/client/routes/docs/[...slug].tsx +0 -12
  119. package/presets/eslint.js +0 -88
  120. package/src/backend/index.ts +0 -204
  121. package/src/cli/configure.ts +0 -363
  122. package/src/cli/create.ts +0 -924
  123. package/src/cli/db.ts +0 -158
  124. package/src/cli/diagnostics.ts +0 -665
  125. package/src/cli/doctor.ts +0 -982
  126. package/src/cli/features.ts +0 -170
  127. package/src/cli/index.ts +0 -326
  128. package/src/cli/notify.ts +0 -120
  129. package/src/cli/proc.ts +0 -56
  130. package/src/cli/ui.ts +0 -169
  131. package/src/cli/update.ts +0 -248
  132. package/src/cli/updates.ts +0 -92
  133. package/src/cli/validate.ts +0 -31
  134. package/src/cli/version-check.ts +0 -125
  135. package/src/client/auth.ts +0 -869
  136. package/src/client/channel/channel.ts +0 -146
  137. package/src/client/components/Form.tsx +0 -65
  138. package/src/client/components/Image.tsx +0 -160
  139. package/src/client/components/Script.tsx +0 -113
  140. package/src/client/components/Slot.tsx +0 -21
  141. package/src/client/dev/devtools.tsx +0 -1089
  142. package/src/client/dev/error-overlay.tsx +0 -225
  143. package/src/client/errors.ts +0 -11
  144. package/src/client/head/head.ts +0 -182
  145. package/src/client/head/metadata.ts +0 -110
  146. package/src/client/index.ts +0 -130
  147. package/src/client/navigation/Link.tsx +0 -108
  148. package/src/client/navigation/NavLink.tsx +0 -86
  149. package/src/client/navigation/navigation.ts +0 -235
  150. package/src/client/navigation/prefetch.ts +0 -169
  151. package/src/client/navigation/scroll.ts +0 -53
  152. package/src/client/routing/Router.tsx +0 -216
  153. package/src/client/routing/action.ts +0 -122
  154. package/src/client/routing/error-boundary.tsx +0 -43
  155. package/src/client/routing/hooks.ts +0 -140
  156. package/src/client/routing/lazy.ts +0 -77
  157. package/src/client/routing/loader.ts +0 -381
  158. package/src/client/routing/match.ts +0 -47
  159. package/src/client/routing/mount.tsx +0 -90
  160. package/src/client/routing/params-context.ts +0 -10
  161. package/src/client/routing/slot-context.ts +0 -7
  162. package/src/client/rpc.ts +0 -97
  163. package/src/client/search/search.ts +0 -189
  164. package/src/client/search/use-page-search.ts +0 -73
  165. package/src/client/ssr/markers.tsx +0 -172
  166. package/src/client/stream/client.ts +0 -237
  167. package/src/client/types.ts +0 -73
  168. package/src/compiler/auth-emails.ts +0 -312
  169. package/src/compiler/config.ts +0 -369
  170. package/src/compiler/docs.ts +0 -127
  171. package/src/compiler/email-preview.ts +0 -314
  172. package/src/compiler/emails.ts +0 -404
  173. package/src/compiler/fonts.ts +0 -89
  174. package/src/compiler/generate.ts +0 -471
  175. package/src/compiler/image-blur.ts +0 -42
  176. package/src/compiler/image-report.ts +0 -88
  177. package/src/compiler/index.ts +0 -1362
  178. package/src/compiler/pages.ts +0 -49
  179. package/src/compiler/plugin.ts +0 -391
  180. package/src/compiler/prerender.ts +0 -263
  181. package/src/compiler/routes.ts +0 -170
  182. package/src/compiler/seo.ts +0 -415
  183. package/src/compiler/sri.ts +0 -144
  184. package/src/compiler/ssg.ts +0 -177
  185. package/src/compiler/ssr-codegen.ts +0 -85
  186. package/src/compiler/template-build.ts +0 -676
  187. package/src/compiler/template.ts +0 -262
  188. package/src/compiler/toil-docs.generated.ts +0 -77
  189. package/src/compiler/vite.ts +0 -245
  190. package/src/devserver/analytics/index.ts +0 -317
  191. package/src/devserver/config/dotenv.ts +0 -102
  192. package/src/devserver/config/env.ts +0 -30
  193. package/src/devserver/config/ratelimit.ts +0 -123
  194. package/src/devserver/daemon/catalog.ts +0 -120
  195. package/src/devserver/daemon/cron.ts +0 -87
  196. package/src/devserver/daemon/host.ts +0 -134
  197. package/src/devserver/daemon/index.ts +0 -369
  198. package/src/devserver/daemon/runtime.ts +0 -48
  199. package/src/devserver/db/catalog.ts +0 -109
  200. package/src/devserver/db/database.ts +0 -1834
  201. package/src/devserver/db/derives.ts +0 -121
  202. package/src/devserver/db/index.ts +0 -20
  203. package/src/devserver/db/routeKinds.ts +0 -191
  204. package/src/devserver/db/types.ts +0 -168
  205. package/src/devserver/email/caps.ts +0 -0
  206. package/src/devserver/email/config.ts +0 -141
  207. package/src/devserver/email/console.ts +0 -348
  208. package/src/devserver/email/index.ts +0 -155
  209. package/src/devserver/email/providers.ts +0 -131
  210. package/src/devserver/email/status.ts +0 -23
  211. package/src/devserver/email/validate.ts +0 -37
  212. package/src/devserver/email/wire.ts +0 -55
  213. package/src/devserver/http/cache.ts +0 -0
  214. package/src/devserver/http/envelope.ts +0 -157
  215. package/src/devserver/http/proxy.ts +0 -172
  216. package/src/devserver/http/runtime.ts +0 -387
  217. package/src/devserver/index.ts +0 -43
  218. package/src/devserver/production-ipc.ts +0 -63
  219. package/src/devserver/production-worker.ts +0 -83
  220. package/src/devserver/production.ts +0 -718
  221. package/src/devserver/runtime/crypto.ts +0 -587
  222. package/src/devserver/runtime/host.ts +0 -427
  223. package/src/devserver/runtime/module.ts +0 -442
  224. package/src/devserver/server.ts +0 -251
  225. package/src/devserver/ssr.ts +0 -208
  226. package/src/devserver/stream/catalog.ts +0 -106
  227. package/src/devserver/stream/host.ts +0 -42
  228. package/src/devserver/stream/index.ts +0 -301
  229. package/src/devserver/stream/manager.ts +0 -167
  230. package/src/devserver/stream/router.ts +0 -101
  231. package/src/devserver/stream/wire.ts +0 -58
  232. package/src/devserver/stream/ws.ts +0 -78
  233. package/src/devserver/wasm/sections.ts +0 -59
  234. package/src/devserver/wasm/surface.ts +0 -115
  235. package/src/io/FastMap.ts +0 -151
  236. package/src/io/FastSet.ts +0 -113
  237. package/src/io/codec.ts +0 -362
  238. package/src/io/index.ts +0 -10
  239. package/src/io/lengths.ts +0 -14
  240. package/src/io/types.ts +0 -19
  241. package/src/logger/index.ts +0 -22
  242. package/src/shared/index.ts +0 -46
  243. package/test/analytics-dev.test.ts +0 -113
  244. package/test/assembly/aspect-shim.ts +0 -33
  245. package/test/assembly/cookie.spec.ts +0 -303
  246. package/test/assembly/example.spec.ts +0 -22
  247. package/test/assembly/ssr.spec.ts +0 -95
  248. package/test/assembly.test.ts +0 -59
  249. package/test/auth-emails.test.ts +0 -118
  250. package/test/built-ssr.test.ts +0 -148
  251. package/test/channel.test.ts +0 -21
  252. package/test/config-host-port.test.ts +0 -59
  253. package/test/configure.test.ts +0 -94
  254. package/test/daemon-build.test.ts +0 -278
  255. package/test/daemon-catalog.test.ts +0 -349
  256. package/test/daemon-emulation.test.ts +0 -256
  257. package/test/db.test.ts +0 -0
  258. package/test/devserver-database.test.ts +0 -1102
  259. package/test/devserver-email.test.ts +0 -306
  260. package/test/devserver-pqauth.test.ts +0 -93
  261. package/test/devserver-secrets.test.ts +0 -63
  262. package/test/devserver.test.ts +0 -259
  263. package/test/doctor.test.ts +0 -298
  264. package/test/dom/Image.test.tsx +0 -144
  265. package/test/dom/Link.test.tsx +0 -67
  266. package/test/dom/NavLink.test.tsx +0 -37
  267. package/test/dom/Script.test.tsx +0 -48
  268. package/test/dom/action.test.tsx +0 -146
  269. package/test/dom/error-overlay.test.tsx +0 -44
  270. package/test/dom/loader.test.tsx +0 -121
  271. package/test/dom/navigation.test.ts +0 -59
  272. package/test/dom/revalidate.test.tsx +0 -38
  273. package/test/dom/route-head.test.tsx +0 -56
  274. package/test/dom/router-loading.test.tsx +0 -44
  275. package/test/dom/scroll.test.ts +0 -56
  276. package/test/dom/slot.test.tsx +0 -131
  277. package/test/dom/use-metadata.test.tsx +0 -58
  278. package/test/dom/view-transitions.test.tsx +0 -53
  279. package/test/dynamic-routing.test.ts +0 -132
  280. package/test/email-console.test.ts +0 -86
  281. package/test/email-preview.test.ts +0 -73
  282. package/test/emails.test.ts +0 -58
  283. package/test/errors.test.ts +0 -21
  284. package/test/example-guestbook.test.ts +0 -123
  285. package/test/features.test.ts +0 -149
  286. package/test/fixtures/bignum-wire/spec.ts +0 -25
  287. package/test/fixtures/daemon-app.ts +0 -65
  288. package/test/fixtures/stream-echo.ts +0 -26
  289. package/test/fixtures/stream-gate.ts +0 -28
  290. package/test/fixtures/stream-trap.ts +0 -18
  291. package/test/fixtures/stream-typed.ts +0 -41
  292. package/test/fonts.test.ts +0 -28
  293. package/test/global-setup.ts +0 -17
  294. package/test/head.test.ts +0 -42
  295. package/test/image-blur.test.ts +0 -50
  296. package/test/io.test.ts +0 -117
  297. package/test/metadata.test.ts +0 -40
  298. package/test/navlink.test.ts +0 -28
  299. package/test/pages.test.ts +0 -105
  300. package/test/placeholder.test.ts +0 -9
  301. package/test/pqauth-e2e.test.ts +0 -216
  302. package/test/pqauth-email.test.ts +0 -801
  303. package/test/prerender.test.ts +0 -116
  304. package/test/prettier-plugin.test.ts +0 -46
  305. package/test/routes.test.ts +0 -76
  306. package/test/rpc-bignum-wire.test.ts +0 -164
  307. package/test/rpc-dispatch.test.ts +0 -132
  308. package/test/rpc-kinds.test.ts +0 -18
  309. package/test/rpc.test.ts +0 -66
  310. package/test/search.test.ts +0 -114
  311. package/test/seo.test.ts +0 -175
  312. package/test/slot-layouts.test.ts +0 -69
  313. package/test/sri.test.ts +0 -129
  314. package/test/ssg.test.ts +0 -36
  315. package/test/ssr-hydration.test.tsx +0 -122
  316. package/test/ssr-render.test.ts +0 -197
  317. package/test/ssr-template.test.tsx +0 -482
  318. package/test/stream-emulation.test.ts +0 -479
  319. package/test/ui.test.ts +0 -40
  320. package/test/update.test.ts +0 -58
  321. package/test/validate.test.ts +0 -42
  322. package/test/version-check.test.ts +0 -130
  323. package/tests/data-parity/generated-parity.ts +0 -99
  324. package/tests/data-parity/parity.ts +0 -80
  325. package/tests/data-parity/spec.ts +0 -46
@@ -1,188 +0,0 @@
1
- # Using auth
2
-
3
- ## 1. Enable it
4
-
5
- Either the config flag (canonical) or a one-line import, both do the same thing (the build appends the
6
- framework's auth controller + user shape to your server as compiled entries, so `/auth/*` self-mounts):
7
-
8
- ```ts
9
- // toil.config.ts, canonical
10
- export default defineConfig({ server: { auth: true } });
11
- ```
12
-
13
- ```ts
14
- // server/main.ts, escape hatch (equivalent)
15
- import 'toiljs/server/auth';
16
- ```
17
-
18
- There is also an `AuthService.enable()` no-op you can call for discoverability, but enabling is done by the
19
- flag/import above (it happens at build time). Turn it off by removing the flag/import, with neither, no
20
- `/auth` routes and no accounts collection are generated. It is strictly opt-in.
21
-
22
- ## 2. The client
23
-
24
- `toiljs/client` ships the browser half, it runs the OPRF blinding, Argon2id, ML-DSA keygen, and ML-KEM
25
- encapsulation, and talks to `/auth/*`. You never touch the crypto.
26
-
27
- ```ts
28
- import { Auth } from 'toiljs/client';
29
-
30
- // Create an account. Throws on a taken username or a transport error.
31
- await Auth.register(username, password);
32
-
33
- // Log in. On success the browser holds the signed session cookie; subsequent
34
- // requests to @auth routes are authorized automatically.
35
- await Auth.login(username, password);
36
- ```
37
-
38
- Options (second argument, `AuthOptions`):
39
-
40
- ```ts
41
- await Auth.login(username, password, {
42
- baseUrl: '/auth', // default; change if you mount elsewhere
43
- serverKemPublicKey: MY_KEM_PUB, // REQUIRED in production, pin your deployment's KEM key
44
- });
45
- ```
46
-
47
- > **Production:** the client ships with the DEV KEM public key pinned. A real deployment MUST pass its own
48
- > `serverKemPublicKey` (derived from `AUTH_KEM_SK`). See [Configuration](./configuration.md).
49
-
50
- ## 2b. Client-side: who's logged in, and protecting pages
51
-
52
- `register`/`login` set the session cookies; to *render* login state on the client, use the generated
53
- `getUser()` (emitted from the built-in `@user`). It reads the **readable** `__Secure-toil_user` companion
54
- cookie and returns the typed user or `null`, instant, no network call. It is **display-only**; the server
55
- still enforces access via `@auth`, so never trust it for authorization.
56
-
57
- ```tsx
58
- import { getUser } from 'shared/server'; // generated; typed to the built-in @user
59
-
60
- function Nav() {
61
- const user = getUser(); // { toilUserId, username } | null
62
- return user
63
- ? <span>Hi {user.username} <button onClick={logout}>Log out</button></span>
64
- : <a href="/login">Log in</a>;
65
- }
66
-
67
- async function logout() {
68
- await fetch('/auth/logout', { method: 'POST' });
69
- location.href = '/login'; // cookies are cleared; bounce to login
70
- }
71
- ```
72
-
73
- Gate a whole client route by redirecting when there's no session:
74
-
75
- ```tsx
76
- export default function Dashboard() {
77
- const user = getUser();
78
- if (user == null) { location.href = '/login'; return null; } // not logged in -> to /login
79
- return <main>Welcome, {user.username}</main>;
80
- }
81
- ```
82
-
83
- The authoritative check is still the server: any data this page fetches should come from an `@auth` route,
84
- so a user who forged/deleted the readable cookie sees the redirect OR a `401`, never real data.
85
-
86
- ## 2c. Handling errors
87
-
88
- `Auth.register` / `Auth.login` reject with a message you can show. The important distinguishable cases:
89
-
90
- ```ts
91
- try {
92
- await Auth.register(username, password);
93
- } catch (e) {
94
- const m = String(e);
95
- if (m.includes('already registered')) setError('That username is taken, log in instead.');
96
- else setError('Could not register, try again.');
97
- }
98
-
99
- try {
100
- await Auth.login(username, password);
101
- } catch {
102
- // Wrong password OR unknown user both fail generically (anti-enumeration), one message.
103
- setError('Incorrect username or password.');
104
- }
105
- ```
106
-
107
- - **Username taken** → `register` throws `auth: username already registered (log in instead)` (a
108
- distinguishable case so you can guide the user).
109
- - **Wrong password / unknown user** → `login` throws generically (`auth: request failed`): by design,
110
- the two are indistinguishable, so use ONE "incorrect username or password" message.
111
- - **Rate limited** → after 5 attempts / 60s a `429` surfaces as the same generic throw; back off and tell
112
- the user to wait.
113
-
114
- ## 3. Guard your routes: `@auth`
115
-
116
- Put `@auth` on a route method or a whole `@rest` class. The generated dispatcher checks for a valid signed
117
- session **before** your handler runs and returns `401` otherwise. `@auth` is unchanged by built-in auth,
118
- it's the same decorator you'd use with hand-written auth.
119
-
120
- ```ts
121
- @rest('account')
122
- class AccountApi {
123
- @auth // this route needs a session
124
- @get('/settings')
125
- public settings(): Response { /* … */ }
126
-
127
- @get('/public') // this one is open
128
- public open(): Response { /* … */ }
129
- }
130
-
131
- @auth // …or guard the ENTIRE class
132
- @rest('admin')
133
- class AdminApi { /* every route requires a session */ }
134
- ```
135
-
136
- ## 4. Read the current user
137
-
138
- Inside any handler:
139
-
140
- ```ts
141
- // The typed logged-in user (the built-in `@user`: toilUserId + username), or null.
142
- const user = AuthService.getUser();
143
- if (user != null) {
144
- user.username; // string
145
- user.toilUserId; // Uint8Array(32), the stable id bytes
146
- }
147
-
148
- // The stable identity as a ToilUserId (gate on hasSession() first, see note).
149
- if (AuthService.hasSession()) {
150
- const id = AuthService.userId()!; // ToilUserId
151
- // key your own data on id (see Extending)
152
- }
153
- ```
154
-
155
- > `ToilUserId` overloads `==`, so `AuthService.userId() == null` does not type-check. Gate with
156
- > `AuthService.hasSession()` and then use `userId()!`, or use `getUser()` and null-check that.
157
-
158
- ## 5. The endpoint wire contract
159
-
160
- You normally use the `Auth` client, but the raw endpoints are binary (`DataWriter`/`DataReader`, never
161
- JSON):
162
-
163
- | Route | Request body | Response |
164
- | --- | --- | --- |
165
- | `POST /auth/register/start` | `str(username) bytes(blinded)` | `u8(0) u32(mem) u32(iters) u32(par) bytes(salt) bytes(evaluated)` |
166
- | `POST /auth/register/finish` | `str(username) bytes(pubkey) bytes(proof)` | `u8(status)`, `0` ok, `1` username taken |
167
- | `POST /auth/login/start` | `str(username) bytes(blinded)` | `bytes(cid) str(aud) u32(mem) u32(iters) u32(par) bytes(salt) bytes(nonce) u64(iat) u64(exp) bytes(evaluated)` |
168
- | `POST /auth/login/finish` | `bytes(cid) bytes(ct) bytes(sig)` | `u8(0) bytes(sessionToken) bytes(serverConfirm)` + `Set-Cookie`, or `u8(≠0)` on failure |
169
- | `GET /auth/me` *(`@auth`)* | (none) | `bytes(toilUserId) str(username)` |
170
- | `POST /auth/logout` *(`@auth`)* | (none) | `200` + cookie-clearing `Set-Cookie` |
171
-
172
- Rate limiting: every register/login POST carries `@ratelimit(SlidingWindow, 5, 60)` (5 requests / 60s per
173
- client) out of the box, so brute-force is throttled before it reaches the crypto.
174
-
175
- ## 6. Under `toiljs dev`
176
-
177
- Everything runs locally with **zero setup**: the dev server emulates the ToilDB account/challenge storage
178
- and the ML-DSA/ML-KEM/OPRF host functions in process, and the auth secrets fall back to insecure DEV
179
- values. Register and login span requests (the accounts persist for the dev session). You'll see a warning
180
- that `AUTH_SESSION_SECRET` is unset, that's expected in dev; set it before you deploy (see
181
- [Configuration](./configuration.md)).
182
-
183
- ## 7. `Server.REST.auth.*`
184
-
185
- Because the controller is a normal `@rest('auth')` class, a typed `Server.REST.auth.*` fetch client is
186
- generated for free (`me`, `logout`, and the register/login methods). Use it for `/me` and `/logout`; but
187
- **drive register/login through `toiljs/client` `Auth`**, not the generated client, only the `Auth` helper
188
- runs the required browser-side OPRF/Argon2id/ML-DSA/ML-KEM crypto.
@@ -1,143 +0,0 @@
1
- # Backend overview
2
-
3
- Your backend is TypeScript that toilscript compiles into a small, sandboxed WebAssembly program, which runs on the Dacely edge and answers every request.
4
-
5
- ## What the backend is
6
-
7
- In a toiljs project, everything under `server/` is your backend. You write it in TypeScript, the same language as your frontend. But it does not run in a browser and it does not run in Node. Instead, a compiler called **toilscript** turns it into **WebAssembly** (often shortened to **WASM**): a compact, fast, portable binary format that many kinds of servers can run safely.
8
-
9
- That compiled program is then deployed to the **Dacely edge**. Two pieces of jargon to unpack there:
10
-
11
- - **The edge** means a fleet of servers spread across many cities around the world. When a user makes a request, it is served by the edge node physically closest to them. Close means fast: less distance for the data to travel, so lower latency. You do not pick a region or manage servers; your one compiled backend runs everywhere at once.
12
- - **Sandboxed** means your WASM program runs inside a locked box. It cannot open files, reach the operating system, or make raw network connections on its own. The only way it can touch the outside world is through a small, fixed set of **host functions** that toiljs provides (read the request, build a response, query the database, send an email, and so on). Every one of those calls is metered and bounded, so a buggy or hostile backend cannot crash the node or read another app's data. This is what makes it safe to run thousands of different apps on the same shared edge.
13
-
14
- You never call the WASM boundary by hand. You write normal TypeScript classes and functions, tag them with decorators (like `@rest` or `@service`), and the compiler wires everything up.
15
-
16
- > New to decorators? A **decorator** is the `@name` you write just above a class or method. It attaches meaning to that code without changing what the code does line by line. toiljs uses decorators to say "this class is an HTTP controller" or "this method is callable from the browser." See [Decorators](../concepts/decorators.md).
17
-
18
- ## The request lifecycle
19
-
20
- Here is what happens, end to end, when a browser talks to your backend.
21
-
22
- ```mermaid
23
- flowchart TD
24
- U["User's browser"] -->|"HTTP request"| E["Nearest Dacely edge node"]
25
- E --> S{"Static file<br/>for this path?"}
26
- S -->|"Yes (GET/HEAD)"| F["Serve the file<br/>(HTML, JS, images)"]
27
- S -->|"No"| W["Your compiled backend<br/>(server.wasm handle)"]
28
- W --> H["Your handler runs<br/>and returns a Response"]
29
- H --> E
30
- F --> E
31
- E -->|"HTTP response"| U
32
- ```
33
-
34
- Step by step:
35
-
36
- 1. The request lands on the closest edge node.
37
- 2. The edge first checks whether the path is a **static file** it can serve directly (your built frontend: HTML pages, JavaScript bundles, images). If so, it serves the file and never wakes your code. This is fast and free.
38
- 3. Otherwise the edge hands the request to your compiled backend by calling its single WASM export, `handle`.
39
- 4. Inside, toiljs decodes the raw bytes into a friendly [`Request`](./rest.md#the-request-object) object and calls your handler's `handle(req)` method.
40
- 5. Your handler returns a [`Response`](./rest.md#building-a-response). toiljs encodes it back into bytes.
41
- 6. The edge sends that response to the browser.
42
-
43
- The key mental model: your backend is a pure function of the request. Bytes in, bytes out, one request at a time.
44
-
45
- ## Stateless by default
46
-
47
- A **fresh copy** of your handler serves each request. Any fields you set on a controller do not survive to the next request, and the request might even be served by a different edge node on the other side of the world. This is called being **stateless**.
48
-
49
- That is a feature, not a limitation: it is what lets your backend scale to the whole planet with no coordination. When you need data to persist between requests (a user account, a counter, a list of posts), you store it in the built-in global database, **ToilDB**. See [the database section](../database/README.md).
50
-
51
- ```mermaid
52
- flowchart LR
53
- R1["Request 1"] --> B1["Handler copy A<br/>(fields reset)"]
54
- R2["Request 2"] --> B2["Handler copy B<br/>(fields reset)"]
55
- B1 --> DB[("ToilDB<br/>(shared, persistent)")]
56
- B2 --> DB
57
- ```
58
-
59
- ## The three surfaces
60
-
61
- Your backend can expose three different kinds of endpoint. Each is opted into with a decorator, and each has its own page:
62
-
63
- | Surface | Decorator | What it is | When to use it |
64
- | --- | --- | --- | --- |
65
- | **HTTP REST** | `@rest` + `@get`/`@post`/... | Plain HTTP routes with paths, methods, and status codes. | A public API, webhooks, anything a browser, `curl`, or a third party calls directly. See [REST](./rest.md). |
66
- | **Typed RPC** | `@service` / `@remote` | Server functions your own frontend calls like local async functions, fully type-checked end to end. | Talking from your own React app to your own backend. See [RPC](./rpc.md). |
67
- | **Realtime** | `@stream` | A long-lived connection where the server keeps state per connected client. | Chat, live cursors, notifications, anything push-based. See [Realtime](../realtime/README.md). |
68
-
69
- REST and RPC are the everyday tools. Most apps use both: REST for anything the outside world calls, RPC for your own frontend. They are not exclusive; you can use all three in one project.
70
-
71
- All of these share the same building block for their data: **`@data` classes**, which are typed structs that travel safely between the browser and your WASM backend. See [Data types](./data.md).
72
-
73
- ## Where your handler lives
74
-
75
- REST, RPC, and streams self-register: you tag a class and the compiler adds it to the right dispatcher. A tiny amount of glue lives in `server/main.ts`, which imports your route files and names your top-level handler class. In a typical project you rarely touch `main.ts`; you add route and service files and they are discovered automatically.
76
-
77
- Your handler class extends `ToilHandler` and overrides `handle`. The common pattern is to try the REST dispatcher first, then fall back to your own logic:
78
-
79
- ```ts
80
- import { Method, Request, Response, Rest, ToilHandler } from 'toiljs/server/runtime';
81
-
82
- export class AppHandler extends ToilHandler {
83
- public handle(req: Request): Response {
84
- // Try every @rest controller. Returns the first match, or null.
85
- const hit = Rest.dispatch(req);
86
- if (hit != null) return hit;
87
-
88
- // Your own hand-written endpoints can go here.
89
- if (req.path == '/health') return Response.text('ok\n');
90
-
91
- // "I have no answer for this path": let the edge serve it (a static
92
- // file, the client app) instead of returning a hard 404.
93
- return Response.unhandled();
94
- }
95
- }
96
- ```
97
-
98
- RPC calls (to the reserved path `/__toil_rpc`) are handled by the framework before your `handle` runs, so you do not dispatch them yourself.
99
-
100
- ### Per-request hooks: `onRequestStarted` and `onRequestCompleted`
101
-
102
- `ToilHandler` gives you two optional hooks that run around every request, so you can add cross-cutting logic (logging, timing, metrics) once instead of repeating it in every route, and without re-implementing `handle`:
103
-
104
- - **`onRequestStarted(req: Request): void`** runs just **before** `handle(req)`. Override it for per-request setup: start a timer, log the incoming method and path, read a header you need everywhere.
105
- - **`onRequestCompleted(req: Request, resp: Response): void`** runs just **after** `handle` returns, and it is handed the `Response` that is about to go out. It **also runs when your handler throws**, after the runtime has turned that throw into a `500`, so it is the right place for teardown you always want to happen (record the outcome, stop the timer, emit a metric).
106
-
107
- Both are empty by default, so override only the one you need. The framework calls them around every request; you never call them yourself.
108
-
109
- ```ts
110
- import { Request, Response, Rest, ToilHandler } from 'toiljs/server/runtime';
111
-
112
- export class AppHandler extends ToilHandler {
113
- public handle(req: Request): Response {
114
- const hit = Rest.dispatch(req);
115
- return hit != null ? hit : Response.unhandled();
116
- }
117
-
118
- // Runs before every handle().
119
- public onRequestStarted(req: Request): void {
120
- // per-request setup, e.g. note the path you are about to serve
121
- }
122
-
123
- // Runs after every handle(), including after a throw became a 500.
124
- public onRequestCompleted(req: Request, resp: Response): void {
125
- // always-run teardown: record the outcome, stop a timer, emit a metric
126
- }
127
- }
128
- ```
129
-
130
- If your project is REST-only, you do not even need a custom handler; toiljs ships a ready-made one. See [REST](./rest.md#dispatch-and-the-404-fallback).
131
-
132
- ## Compute tiers
133
-
134
- The request/response backend described here is the default and most common tier, called **L1**. toiljs also has higher tiers for long-lived connections (streams) and scheduled background work (daemons), each compiled into its own WASM artifact from the same project. You opt into a tier just by adding its entry file and surface decorator. For the full picture, see [Compute tiers](../concepts/tiers.md).
135
-
136
- ## Related
137
-
138
- - [HTTP routes (`@rest`)](./rest.md): paths, methods, params, and responses.
139
- - [Typed RPC (`@service`/`@remote`)](./rpc.md): calling the server from your frontend with end-to-end types.
140
- - [Data types (`@data`)](./data.md): the serializable structs everything uses.
141
- - [The database (ToilDB)](../database/README.md): where persistent state lives.
142
- - [Compute tiers](../concepts/tiers.md): L1 request, L2/L3 stream, L4 daemon.
143
- - [Realtime streams](../realtime/README.md): the `@stream` surface.