toiljs 0.0.59 → 0.0.61
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/.github/workflows/ci.yml +31 -0
- package/CHANGELOG.md +15 -0
- package/build/cli/.tsbuildinfo +1 -1
- package/build/cli/index.js +311 -118
- package/build/client/.tsbuildinfo +1 -1
- package/build/client/index.d.ts +1 -1
- package/build/client/index.js +1 -1
- package/build/client/routing/mount.js +12 -1
- package/build/client/ssr/markers.d.ts +1 -0
- package/build/client/ssr/markers.js +3 -0
- package/build/compiler/.tsbuildinfo +1 -1
- package/build/compiler/config.d.ts +21 -0
- package/build/compiler/config.js +35 -0
- package/build/compiler/docs.d.ts +2 -1
- package/build/compiler/docs.js +33 -304
- package/build/compiler/index.d.ts +13 -0
- package/build/compiler/index.js +113 -21
- package/build/compiler/template-build.d.ts +21 -1
- package/build/compiler/template-build.js +110 -26
- package/build/compiler/toil-docs.generated.d.ts +1 -0
- package/build/compiler/toil-docs.generated.js +20 -0
- package/build/devserver/.tsbuildinfo +1 -1
- package/build/devserver/daemon/catalog.d.ts +26 -0
- package/build/devserver/daemon/catalog.js +48 -0
- package/build/devserver/daemon/cron.d.ts +4 -0
- package/build/devserver/daemon/cron.js +50 -0
- package/build/devserver/daemon/host.d.ts +37 -0
- package/build/devserver/daemon/host.js +94 -0
- package/build/devserver/daemon/index.d.ts +34 -0
- package/build/devserver/daemon/index.js +241 -0
- package/build/devserver/db/catalog.d.ts +2 -0
- package/build/devserver/db/catalog.js +80 -0
- package/build/devserver/db/database.d.ts +80 -0
- package/build/devserver/db/database.js +1032 -0
- package/build/devserver/db/index.d.ts +3 -0
- package/build/devserver/db/index.js +3 -0
- package/build/devserver/db/routeKinds.d.ts +8 -0
- package/build/devserver/db/routeKinds.js +139 -0
- package/build/devserver/db/types.d.ts +121 -0
- package/build/devserver/db/types.js +52 -0
- package/build/devserver/email/index.js +1 -1
- package/build/devserver/index.d.ts +19 -24
- package/build/devserver/index.js +11 -165
- package/build/devserver/mstore/store.d.ts +18 -0
- package/build/devserver/mstore/store.js +82 -0
- package/build/devserver/{host.d.ts → runtime/host.d.ts} +7 -1
- package/build/devserver/{host.js → runtime/host.js} +51 -7
- package/build/devserver/{module.d.ts → runtime/module.d.ts} +2 -1
- package/build/devserver/{module.js → runtime/module.js} +34 -1
- package/build/devserver/server.d.ts +23 -0
- package/build/devserver/server.js +223 -0
- package/build/devserver/ssr.d.ts +25 -0
- package/build/devserver/ssr.js +114 -0
- package/build/devserver/wasm/sections.d.ts +2 -0
- package/build/devserver/wasm/sections.js +42 -0
- package/build/devserver/wasm/surface.d.ts +18 -0
- package/build/devserver/wasm/surface.js +41 -0
- package/docs/README.md +4 -4
- package/docs/auth-todo.md +6 -6
- package/docs/caching.md +5 -5
- package/docs/cli.md +15 -0
- package/docs/client.md +40 -0
- package/docs/crypto.md +4 -4
- package/docs/data.md +6 -6
- package/docs/email.md +28 -28
- package/docs/environment.md +10 -10
- package/docs/index.md +26 -0
- package/docs/ratelimit.md +10 -10
- package/docs/routing.md +2 -2
- package/docs/server.md +61 -0
- package/docs/ssr.md +561 -113
- package/docs/styling.md +22 -0
- package/docs/time.md +3 -3
- package/eslint.config.js +10 -1
- package/examples/basic/client/components/Header.tsx +3 -0
- package/examples/basic/client/routes/features/actions.tsx +0 -2
- package/examples/basic/client/routes/hello.tsx +89 -19
- package/examples/basic/client/styles/main.css +48 -0
- package/examples/basic/server/SsrHelloRender.ts +97 -0
- package/examples/basic/server/main.ts +5 -0
- package/examples/basic/server/migrations/GuestEntry.migration.ts +39 -0
- package/examples/basic/server/streams/Echo.ts +49 -0
- package/package.json +12 -10
- package/scripts/gen-toil-docs.mjs +96 -0
- package/server/runtime/time.ts +3 -3
- package/src/cli/create.ts +40 -3
- package/src/cli/db.ts +158 -0
- package/src/cli/diagnostics.ts +19 -0
- package/src/cli/doctor.ts +20 -0
- package/src/cli/index.ts +10 -0
- package/src/cli/update.ts +58 -0
- package/src/client/index.ts +1 -1
- package/src/client/routing/mount.tsx +18 -2
- package/src/client/ssr/markers.tsx +22 -0
- package/src/compiler/config.ts +88 -2
- package/src/compiler/docs.ts +47 -308
- package/src/compiler/index.ts +236 -32
- package/src/compiler/ssr-codegen.ts +1 -1
- package/src/compiler/template-build.ts +247 -46
- package/src/compiler/toil-docs.generated.ts +26 -0
- package/src/devserver/daemon/catalog.ts +120 -0
- package/src/devserver/daemon/cron.ts +87 -0
- package/src/devserver/daemon/host.ts +224 -0
- package/src/devserver/daemon/index.ts +349 -0
- package/src/devserver/db/catalog.ts +108 -0
- package/src/devserver/db/database.ts +1633 -0
- package/src/devserver/db/index.ts +18 -0
- package/src/devserver/db/routeKinds.ts +147 -0
- package/src/devserver/db/types.ts +139 -0
- package/src/devserver/email/index.ts +1 -1
- package/src/devserver/index.ts +31 -287
- package/src/devserver/mstore/store.ts +121 -0
- package/src/devserver/{host.ts → runtime/host.ts} +98 -7
- package/src/devserver/{module.ts → runtime/module.ts} +47 -1
- package/src/devserver/server.ts +393 -0
- package/src/devserver/ssr.ts +166 -0
- package/src/devserver/wasm/sections.ts +59 -0
- package/src/devserver/wasm/surface.ts +88 -0
- package/test/daemon-build.test.ts +198 -0
- package/test/daemon-catalog.test.ts +265 -0
- package/test/daemon-emulation.test.ts +216 -0
- package/test/db.test.ts +0 -0
- package/test/devserver-database.test.ts +510 -14
- package/test/devserver-pqauth.test.ts +1 -1
- package/test/devserver-secrets.test.ts +5 -1
- package/test/doctor.test.ts +13 -0
- package/test/email-preview.test.ts +6 -1
- package/test/example-guestbook.test.ts +43 -1
- package/test/fixtures/daemon-app.ts +56 -0
- package/test/global-setup.ts +17 -0
- package/test/pqauth-e2e.test.ts +1 -1
- package/test/ssr-render.test.ts +94 -27
- package/test/ssr-template.test.tsx +44 -1
- package/vitest.config.ts +3 -0
- package/build/devserver/database.d.ts +0 -8
- package/build/devserver/database.js +0 -418
- package/src/devserver/database.ts +0 -618
- /package/build/devserver/{dotenv.d.ts → config/dotenv.d.ts} +0 -0
- /package/build/devserver/{dotenv.js → config/dotenv.js} +0 -0
- /package/build/devserver/{env.d.ts → config/env.d.ts} +0 -0
- /package/build/devserver/{env.js → config/env.js} +0 -0
- /package/build/devserver/{ratelimit.d.ts → config/ratelimit.d.ts} +0 -0
- /package/build/devserver/{ratelimit.js → config/ratelimit.js} +0 -0
- /package/build/devserver/{cache.d.ts → http/cache.d.ts} +0 -0
- /package/build/devserver/{cache.js → http/cache.js} +0 -0
- /package/build/devserver/{envelope.d.ts → http/envelope.d.ts} +0 -0
- /package/build/devserver/{envelope.js → http/envelope.js} +0 -0
- /package/build/devserver/{proxy.d.ts → http/proxy.d.ts} +0 -0
- /package/build/devserver/{proxy.js → http/proxy.js} +0 -0
- /package/build/devserver/{crypto.d.ts → runtime/crypto.d.ts} +0 -0
- /package/build/devserver/{crypto.js → runtime/crypto.js} +0 -0
- /package/src/devserver/{dotenv.ts → config/dotenv.ts} +0 -0
- /package/src/devserver/{env.ts → config/env.ts} +0 -0
- /package/src/devserver/{ratelimit.ts → config/ratelimit.ts} +0 -0
- /package/src/devserver/{cache.ts → http/cache.ts} +0 -0
- /package/src/devserver/{envelope.ts → http/envelope.ts} +0 -0
- /package/src/devserver/{proxy.ts → http/proxy.ts} +0 -0
- /package/src/devserver/{crypto.ts → runtime/crypto.ts} +0 -0
package/src/compiler/docs.ts
CHANGED
|
@@ -6,6 +6,52 @@
|
|
|
6
6
|
*/
|
|
7
7
|
import fs from 'node:fs';
|
|
8
8
|
import path from 'node:path';
|
|
9
|
+
import { TOIL_DOCS } from './toil-docs.generated.js';
|
|
10
|
+
|
|
11
|
+
export { TOIL_DOCS };
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* One-line summaries for the pointer files' bullet list, keyed by `.toil/docs/` filename.
|
|
15
|
+
* A doc with no entry here still appears in the list (using its `# ` heading as the label),
|
|
16
|
+
* so adding a `docs/*.md` is picked up automatically without re-listing it by hand.
|
|
17
|
+
*/
|
|
18
|
+
const DOC_SUMMARIES: Record<string, string> = {
|
|
19
|
+
'index.md': 'overview and project layout',
|
|
20
|
+
'getting-started.md': 'the two halves, build/dev, and the request lifecycle',
|
|
21
|
+
'routing.md': 'file-based routing, nested layouts, loading / error files',
|
|
22
|
+
'client.md': 'the `Toil` global, Link / NavLink, router hooks',
|
|
23
|
+
'styling.md': 'CSS / Sass / Less / Stylus / Tailwind (via `toiljs configure`)',
|
|
24
|
+
'server.md': 'the toilscript server target, `@data` / `@remote` / `@rest`',
|
|
25
|
+
'ssr.md': 'server-side rendering (`ssr = true`, hole markers, the server `render`)',
|
|
26
|
+
'rpc.md': '`@service` / `@remote` and the generated typed client',
|
|
27
|
+
'data.md': 'the `@data` decorator and the binary codec',
|
|
28
|
+
'caching.md': 'the `@cache` decorator and `Response.cache(...)`',
|
|
29
|
+
'ratelimit.md': 'the `@ratelimit` decorator',
|
|
30
|
+
'auth.md': '`@auth` guards, `@user`, sessions, the client half',
|
|
31
|
+
'environment.md': '`Environment.get` / `getSecure` config + secrets',
|
|
32
|
+
'email.md': '`EmailService`, templates, provider config',
|
|
33
|
+
'cookies.md': 'the `Cookie` builder, `SecureCookies`, signing / encryption',
|
|
34
|
+
'crypto.md': 'the synchronous `crypto` global and `crypto.subtle`',
|
|
35
|
+
'time.md': '`Time.nowMillis()` / `Time.nowSeconds()`',
|
|
36
|
+
'cli.md': 'toiljs CLI commands',
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
/** First-level `# ` heading of a Markdown doc, used as a fallback bullet label. */
|
|
40
|
+
function docTitle(content: string): string {
|
|
41
|
+
const m = content.match(/^#\s+(.+)$/m);
|
|
42
|
+
return m ? m[1].trim() : '';
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* The pointer files' bullet list, derived from the generated `TOIL_DOCS` map so it never
|
|
47
|
+
* drifts from the docs actually written into `.toil/docs/` (no second hand-kept list).
|
|
48
|
+
*/
|
|
49
|
+
const DOC_POINTERS = Object.keys(TOIL_DOCS)
|
|
50
|
+
.map((name) => {
|
|
51
|
+
const summary = DOC_SUMMARIES[name] ?? docTitle(TOIL_DOCS[name]);
|
|
52
|
+
return `- \`.toil/docs/${name}\`${summary ? `, ${summary}` : ''}`;
|
|
53
|
+
})
|
|
54
|
+
.join('\n');
|
|
9
55
|
|
|
10
56
|
/** Shared body for the per-tool pointer files. */
|
|
11
57
|
const POINTER_BODY = `# toiljs, AI assistant guide
|
|
@@ -16,13 +62,7 @@ routing, and a toilscript→WebAssembly server).
|
|
|
16
62
|
**Before editing this project, read the generated documentation in \`.toil/docs/\`.** It describes
|
|
17
63
|
the conventions you must follow:
|
|
18
64
|
|
|
19
|
-
|
|
20
|
-
- \`.toil/docs/routing.md\`, file-based routing, nested layouts, loading / error files
|
|
21
|
-
- \`.toil/docs/client.md\`, the \`Toil\` global, Link / NavLink, router hooks
|
|
22
|
-
- \`.toil/docs/styling.md\`, CSS / Sass / Less / Stylus / Tailwind (via \`toiljs configure\`)
|
|
23
|
-
- \`.toil/docs/server.md\`, the toilscript server target
|
|
24
|
-
- \`.toil/docs/ssr.md\`, server-side rendering (\`ssr = true\`, hole markers, the server \`render\`)
|
|
25
|
-
- \`.toil/docs/cli.md\`, toiljs CLI commands
|
|
65
|
+
${DOC_POINTERS}
|
|
26
66
|
|
|
27
67
|
\`.toil/docs/\` is regenerated by toiljs; do not edit it by hand. This pointer file is yours to edit.
|
|
28
68
|
`;
|
|
@@ -68,307 +108,6 @@ export function aiHelperFiles(ids: readonly string[]): Record<string, string> {
|
|
|
68
108
|
return out;
|
|
69
109
|
}
|
|
70
110
|
|
|
71
|
-
/** The framework docs written into `.toil/docs/` (placeholders for now), keyed by filename. */
|
|
72
|
-
/** A doc file's contents from an array of lines (single-quoted, so backticks/`${}` stay literal). */
|
|
73
|
-
function doc(lines: string[]): string {
|
|
74
|
-
return lines.join('\n') + '\n';
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
export const TOIL_DOCS: Record<string, string> = {
|
|
78
|
-
'index.md': doc([
|
|
79
|
-
'# toiljs',
|
|
80
|
-
'',
|
|
81
|
-
'A full-stack React framework: a Vite-bundled client SPA with file-based routing, plus a',
|
|
82
|
-
'toilscript-to-WebAssembly server target.',
|
|
83
|
-
'',
|
|
84
|
-
'## Project layout',
|
|
85
|
-
'',
|
|
86
|
-
'- `client/`, the app: `routes/` (file-based), `layout.tsx`, `components/`, `styles/`,',
|
|
87
|
-
' `public/`, and `toil.tsx` (the entry that calls `Toil.mount`).',
|
|
88
|
-
'- `server/`, the toilscript → WASM target (`@main` entry), compiled by `toilscript`.',
|
|
89
|
-
' `@data`/`@remote`/`@service` here generate the typed client `Server` API (see `server.md`).',
|
|
90
|
-
'- `toil.config.ts`, configuration via `defineConfig` (`toiljs.config.ts` also works).',
|
|
91
|
-
'- Generated, gitignored, do not edit: `.toil/` (working dir), `toil-env.d.ts` (ambient',
|
|
92
|
-
' globals), `toil-routes.d.ts` (typed routes), `shared/server.ts` (the typed RPC module,',
|
|
93
|
-
' emitted by the server build; import `@data` classes from `shared/server`).',
|
|
94
|
-
'',
|
|
95
|
-
'## Key ideas',
|
|
96
|
-
'',
|
|
97
|
-
'- `Toil` is a native global (no import): `Toil.Link`, `Toil.useRouter`, `Toil.useLoaderData`,',
|
|
98
|
-
' etc. The IO classes (`FastMap`, `FastSet`, `DataWriter`, `DataReader`), `parseError`, and the',
|
|
99
|
-
' generated `Server` RPC surface are globals too.',
|
|
100
|
-
'- Scripts: `npm run dev` (HMR), `npm run build` (→ `build/client` + `build/server`),',
|
|
101
|
-
' `npm start` (self-host the build).',
|
|
102
|
-
'',
|
|
103
|
-
'See `routing.md`, `client.md`, `styling.md`, `server.md`, `ssr.md`, `cli.md`.',
|
|
104
|
-
]),
|
|
105
|
-
'routing.md': doc([
|
|
106
|
-
'# Routing',
|
|
107
|
-
'',
|
|
108
|
-
'File-based, under `client/routes/`. A file path maps to a URL.',
|
|
109
|
-
'',
|
|
110
|
-
'## Route files',
|
|
111
|
-
'',
|
|
112
|
-
'- `index.tsx` → `/`, `about.tsx` → `/about`, `blog/index.tsx` → `/blog`',
|
|
113
|
-
'- `[id].tsx` → dynamic `/:id`, read with `Toil.useParams<{ id: string }>()`',
|
|
114
|
-
'- `[...slug].tsx` → catch-all (1+ segments); `[[...slug]].tsx` → optional catch-all (0+)',
|
|
115
|
-
'- `(group)/` → route group: groups files / scopes a layout, adds no URL segment',
|
|
116
|
-
'',
|
|
117
|
-
'## Special files',
|
|
118
|
-
'',
|
|
119
|
-
'- `layout.tsx`, wraps the routes beneath it. Root `client/layout.tsx` wraps everything;',
|
|
120
|
-
' nested `routes/**/layout.tsx` compose inside it.',
|
|
121
|
-
'- `loading.tsx`, Suspense fallback shown while a route (chunk + loader) loads',
|
|
122
|
-
'- `error.tsx`, error boundary; receives `{ error, reset }` (`Toil.RouteErrorProps`)',
|
|
123
|
-
'- `client/404.tsx`, shown when no route matches',
|
|
124
|
-
'',
|
|
125
|
-
'## Data loaders',
|
|
126
|
-
'',
|
|
127
|
-
'Export `loader` from a route; it runs on navigation, in parallel with the chunk, and the',
|
|
128
|
-
'page suspends until it resolves (so `loading.tsx` shows). Read it with `useLoaderData`:',
|
|
129
|
-
'',
|
|
130
|
-
' export const loader = async ({ params, searchParams }: Toil.LoaderArgs) => {',
|
|
131
|
-
' return fetch(`/api/post/${params.id}`).then((r) => r.json());',
|
|
132
|
-
' };',
|
|
133
|
-
' export default function Post() {',
|
|
134
|
-
' const post = Toil.useLoaderData<{ title: string }>();',
|
|
135
|
-
' return <h1>{post.title}</h1>;',
|
|
136
|
-
' }',
|
|
137
|
-
'',
|
|
138
|
-
'`Toil.useRouter().refresh()` re-runs loaders.',
|
|
139
|
-
'',
|
|
140
|
-
'## Server rendering',
|
|
141
|
-
'',
|
|
142
|
-
'Add `export const ssr = true` to render a route on the server (real first-paint HTML + SEO,',
|
|
143
|
-
'then hydration) with near-static speed. See `ssr.md`.',
|
|
144
|
-
'',
|
|
145
|
-
'## Navigation',
|
|
146
|
-
'',
|
|
147
|
-
'- `Toil.Link` / `Toil.NavLink` (adds an active class) / `Toil.useRouter()`',
|
|
148
|
-
' (push / replace / back / forward / refresh / prefetch)',
|
|
149
|
-
'- `Toil.useParams`, `usePathname`, `useSearchParams`, `useNavigationPending`',
|
|
150
|
-
'- Hrefs are type-checked against your routes, a typo is a compile error.',
|
|
151
|
-
]),
|
|
152
|
-
'client.md': doc([
|
|
153
|
-
'# Client runtime',
|
|
154
|
-
'',
|
|
155
|
-
'Everything is on the `Toil` global, no imports needed in route files.',
|
|
156
|
-
'',
|
|
157
|
-
'## Entry',
|
|
158
|
-
'',
|
|
159
|
-
'`client/toil.tsx` imports the route table + global styles and mounts the app:',
|
|
160
|
-
'',
|
|
161
|
-
' import { routes, layout, notFound } from "toiljs/routes";',
|
|
162
|
-
' import "./styles/main.css";',
|
|
163
|
-
' Toil.mount(routes, layout, notFound);',
|
|
164
|
-
'',
|
|
165
|
-
'## API (on `Toil`)',
|
|
166
|
-
'',
|
|
167
|
-
'- Components: `Link`, `NavLink`, `Head`',
|
|
168
|
-
'- Navigation: `navigate`, `useRouter`, `useNavigate`',
|
|
169
|
-
'- Location: `usePathname`, `useSearchParams`, `useParams`, `useNavigationPending`',
|
|
170
|
-
'- Data: `useLoaderData` (see `routing.md`)',
|
|
171
|
-
'- Head: `useHead`, `useTitle`, `<Head>`, set the `<title>` / meta per route',
|
|
172
|
-
'- Realtime: `useChannel`, `connectChannel` (WebSocket to the backend at `/_toil`)',
|
|
173
|
-
'- IO globals (no `Toil.` prefix): `FastMap`, `FastSet`, `DataWriter`, `DataReader`',
|
|
174
|
-
'- `parseError(err)` global: message from an unknown caught value (handy in `catch`)',
|
|
175
|
-
'- `Server` global: the typed RPC surface generated from the server (see `server.md`)',
|
|
176
|
-
'- `Server.REST.<controller>.<route>(args)`: a working, typed `fetch` client for your',
|
|
177
|
-
' `@rest` controllers, e.g. `await Server.REST.todos.getTodo({ params: { id } })` or',
|
|
178
|
-
' `await Server.REST.todos.add({ body: new AddTodo("milk") })`. `args` is',
|
|
179
|
-
' `{ params?, body?, query?, headers? }`; returns are typed (`@data` classes are parsed for',
|
|
180
|
-
' you). The REST client attaches when you import from `shared/server`.',
|
|
181
|
-
'',
|
|
182
|
-
'## Head example',
|
|
183
|
-
'',
|
|
184
|
-
' Toil.useHead({',
|
|
185
|
-
' title: "Blog",',
|
|
186
|
-
' titleTemplate: "%s, MyApp",',
|
|
187
|
-
' meta: [{ name: "description", content: "..." }],',
|
|
188
|
-
' });',
|
|
189
|
-
]),
|
|
190
|
-
'styling.md': doc([
|
|
191
|
-
'# Styling',
|
|
192
|
-
'',
|
|
193
|
-
'The app imports one stylesheet from `client/toil.tsx` (e.g. `./styles/main.css`).',
|
|
194
|
-
'',
|
|
195
|
-
'## Preprocessors & Tailwind',
|
|
196
|
-
'',
|
|
197
|
-
'Pick a CSS preprocessor (none / Sass / Less / Stylus) and optionally Tailwind at',
|
|
198
|
-
'`toiljs create`, or change it later on an existing project:',
|
|
199
|
-
'',
|
|
200
|
-
' toiljs configure # interactive',
|
|
201
|
-
' toiljs configure --tailwind # add Tailwind',
|
|
202
|
-
' toiljs configure --style sass # switch preprocessor',
|
|
203
|
-
'',
|
|
204
|
-
'`configure` installs/removes the right packages and rewrites the imports. Tailwind lives',
|
|
205
|
-
'in its own `styles/tailwind.css` (`@import "tailwindcss";`).',
|
|
206
|
-
'',
|
|
207
|
-
'## Imports',
|
|
208
|
-
'',
|
|
209
|
-
'`.css` / `.scss` / `.sass` / `.less` / `.styl` and image imports (`.svg`, `.png`, …) are',
|
|
210
|
-
'typed via `toil-env.d.ts`.',
|
|
211
|
-
]),
|
|
212
|
-
'server.md': doc([
|
|
213
|
-
'# Server (toilscript → WebAssembly)',
|
|
214
|
-
'',
|
|
215
|
-
'`server/` is the toilscript source, compiled to WebAssembly by `toilscript`.',
|
|
216
|
-
'',
|
|
217
|
-
'- `server/main.ts`, the `@main` entry, exported as the WASM `main`.',
|
|
218
|
-
'- `server/index.ts`, your functions.',
|
|
219
|
-
'- `server/tsconfig.json`, extends `toilscript/std/assembly.json` (AssemblyScript/toilscript',
|
|
220
|
-
' globals like `i32`, not the DOM), so editors resolve server types correctly.',
|
|
221
|
-
'- `npm run build:server` (or `npm run build`) emits `build/server/release.wasm` and',
|
|
222
|
-
' regenerates `shared/server.ts` (the typed client RPC module).',
|
|
223
|
-
'',
|
|
224
|
-
'## Typed RPC (`@data` / `@remote` / `@service`)',
|
|
225
|
-
'',
|
|
226
|
-
'Tag server code and the build generates a typed client `Server` surface:',
|
|
227
|
-
'',
|
|
228
|
-
'- `@data class X {}`, a serializable struct. Generates a client class with the same fields',
|
|
229
|
-
' plus `encode`/`decode`; construct it on the client: `import { X } from "shared/server"`.',
|
|
230
|
-
'- `@remote function f(a: T): R`, a client-callable endpoint, becomes `Server.f(a)`.',
|
|
231
|
-
'- `@service class S { @remote m(...) {} }`, namespaces methods: `Server.s.m(...)`.',
|
|
232
|
-
'',
|
|
233
|
-
'On the client, `Server` is a global (no import) and fully typed; every call is async',
|
|
234
|
-
'(`Promise<R>`). Inputs/outputs are scalars, arrays, or `@data` classes, both directions.',
|
|
235
|
-
'',
|
|
236
|
-
'Note: the client↔server transport is not wired yet, so calling a `Server` method throws',
|
|
237
|
-
'until it lands; the typed surface + codec are generated and ready.',
|
|
238
|
-
'',
|
|
239
|
-
'## HTTP REST (`@rest` / `@route`)',
|
|
240
|
-
'',
|
|
241
|
-
'Tag a class `@rest` and its methods with a verb to expose a real HTTP API. Unlike RPC,',
|
|
242
|
-
'the generated client is working `fetch` code (it is just HTTP).',
|
|
243
|
-
'',
|
|
244
|
-
'- `@rest("api") class Todos {}`, mounts the controller at `/api` (bare `@rest` → `/`).',
|
|
245
|
-
'- `@get("/todos/:id")` / `@post` / `@put` / `@del` / `@patch` / `@head` / `@options`, verb',
|
|
246
|
-
' shortcuts; or `@route({ method: Methods.GET, path: "/todos", stream: DataStream.JSON })`.',
|
|
247
|
-
'- A method takes an optional `@data` body + an optional `ctx: RouteContext` (path params via',
|
|
248
|
-
' `ctx.param("id")`, `ctx.query(...)`, `ctx.header(...)`). It returns either a `@data` type,',
|
|
249
|
-
' which the compiler encodes per `stream` (`DataStream.JSON` default, or `DataStream.Binary`,',
|
|
250
|
-
' lossless for large `u64`/bignum), or a `Response` for full control - custom status and',
|
|
251
|
-
' headers, e.g. `Response.json(value.toJSON().toString()).setHeader("cache-control", "no-store")`',
|
|
252
|
-
' or `Response.notFound()`. (The editor sees the compiler-injected `@data` `toJSON`/`encode`',
|
|
253
|
-
' members via the toilscript plugin, so serializing into a `Response` is editor-clean.)',
|
|
254
|
-
'',
|
|
255
|
-
'Each `@rest` class self-registers; dispatch them from your handler - it composes, it never',
|
|
256
|
-
'takes over `handle()`:',
|
|
257
|
-
'',
|
|
258
|
-
'```ts',
|
|
259
|
-
'import { ToilHandler, Request, Response, Rest } from "toiljs/server/runtime";',
|
|
260
|
-
'export class App extends ToilHandler {',
|
|
261
|
-
' public handle(req: Request): Response {',
|
|
262
|
-
' const hit = Rest.dispatch(req); // try every @rest controller',
|
|
263
|
-
' if (hit != null) return hit;',
|
|
264
|
-
' return Response.notFound(); // your own logic / static fallback',
|
|
265
|
-
' }',
|
|
266
|
-
'}',
|
|
267
|
-
'```',
|
|
268
|
-
'',
|
|
269
|
-
'For a REST-only project, `Server.handler = () => new RestHandler()` does the same with no',
|
|
270
|
-
'boilerplate. On the client: `Server.REST.todos.getTodo({ params: { id } })` (see client.md).',
|
|
271
|
-
]),
|
|
272
|
-
'ssr.md': doc([
|
|
273
|
-
'# Server-side rendering (SSR)',
|
|
274
|
-
'',
|
|
275
|
-
'Opt a route into SSR with `export const ssr = true`. toiljs renders the page ONCE at build',
|
|
276
|
-
'time into a template with holes; at request time the server fills only the dynamic holes and',
|
|
277
|
-
'serves the page, then the browser hydrates it in place. The page is never re-rendered per',
|
|
278
|
-
'request, so an SSR route is served about as fast as a static file while still delivering real',
|
|
279
|
-
'first-paint HTML and SEO.',
|
|
280
|
-
'',
|
|
281
|
-
'## Marking the holes',
|
|
282
|
-
'',
|
|
283
|
-
'Wrap the dynamic bits of the page in hole markers from `toiljs/client`. They are transparent',
|
|
284
|
-
'in the browser (they just render their children); only the build and the server treat them',
|
|
285
|
-
'specially, so the same component is your normal client UI.',
|
|
286
|
-
'',
|
|
287
|
-
'- `<Hole id="name">{value}</Hole>`, a text hole (the value is HTML-escaped for you).',
|
|
288
|
-
'- `<RawHtml id="bio" html={s} />`, a raw-HTML block (you own sanitisation, like',
|
|
289
|
-
' `dangerouslySetInnerHTML`).',
|
|
290
|
-
'- `<Repeat id="rows" each={items}>{(item) => <li>...</li>}</Repeat>`, a repeated region (a',
|
|
291
|
-
' list); the row markup is captured once and stamped per item.',
|
|
292
|
-
'- `<Island>{...}</Island>`, a client-only escape hatch: empty in the server HTML, rendered',
|
|
293
|
-
' after hydration (so it gets no first paint or SEO). Put router-hook-driven or otherwise',
|
|
294
|
-
' non-server-safe bits here.',
|
|
295
|
-
'',
|
|
296
|
-
' import { Hole, Repeat, RawHtml, useLoaderData } from "toiljs/client";',
|
|
297
|
-
' export const ssr = true;',
|
|
298
|
-
' export const loader = ({ params }: Toil.LoaderArgs) => loadProfile(params.name);',
|
|
299
|
-
' export default function Profile() {',
|
|
300
|
-
' const d = useLoaderData<typeof loader>();',
|
|
301
|
-
' return (',
|
|
302
|
-
' <main>',
|
|
303
|
-
' <h1>@<Hole id="username">{d.username}</Hole></h1>',
|
|
304
|
-
' <RawHtml id="bio" html={d.bioHtml} />',
|
|
305
|
-
' <ul>',
|
|
306
|
-
' <Repeat id="posts" each={d.posts}>',
|
|
307
|
-
' {(p) => <li><Hole id="title">{p.title}</Hole></li>}',
|
|
308
|
-
' </Repeat>',
|
|
309
|
-
' </ul>',
|
|
310
|
-
' </main>',
|
|
311
|
-
' );',
|
|
312
|
-
' }',
|
|
313
|
-
'',
|
|
314
|
-
'## The server render',
|
|
315
|
-
'',
|
|
316
|
-
'For each SSR route the build emits a typed `Slot` enum and a coherence hash (a',
|
|
317
|
-
'`<route>.slots.ts` module). In the server you write a `render(req)` that fills each slot from',
|
|
318
|
-
'the request and data, using the `SlotValues` API, and register it with `Ssr`:',
|
|
319
|
-
'',
|
|
320
|
-
'```ts',
|
|
321
|
-
'import { Request, SlotValues, HtmlBuilder, Ssr } from "toiljs/server/runtime";',
|
|
322
|
-
'import { Slot, HASH } from "./profile.slots";',
|
|
323
|
-
'',
|
|
324
|
-
'function renderProfile(req: Request): SlotValues | null {',
|
|
325
|
-
' if (!req.path.startsWith("/u/")) return null; // not this route',
|
|
326
|
-
' const v = new SlotValues(HASH);',
|
|
327
|
-
' v.setText(Slot.username, usernameFor(req)); // escaped',
|
|
328
|
-
' v.setRaw(Slot.bio, bioHtml); // verbatim',
|
|
329
|
-
' const rows = new HtmlBuilder();',
|
|
330
|
-
' for (let i = 0; i < posts.length; i++) rows.raw("<li>").text(posts[i]).raw("</li>");',
|
|
331
|
-
' v.setRepeat(Slot.posts, rows); // stamped rows',
|
|
332
|
-
' return v;',
|
|
333
|
-
'}',
|
|
334
|
-
'Ssr.register(renderProfile);',
|
|
335
|
-
'```',
|
|
336
|
-
'',
|
|
337
|
-
'`SlotValues`: `setText` (escaped), `setRaw` (verbatim), `setAttr` (attribute value),',
|
|
338
|
-
'`setRepeat` (a stamped `HtmlBuilder`), plus `setHeader` and `setStatus`. The hole values you',
|
|
339
|
-
'return are filled into the template and the page is served; the browser then hydrates against',
|
|
340
|
-
'the same data, so the markup matches with no client re-render.',
|
|
341
|
-
'',
|
|
342
|
-
'## Rules of thumb',
|
|
343
|
-
'',
|
|
344
|
-
'- An SSR route (and the layouts above it) must render under static markup. Use the hole',
|
|
345
|
-
' markers and `useLoaderData`; move anything that needs router hooks or browser-only APIs into',
|
|
346
|
-
' an `<Island>`. A route that cannot render this way is skipped at build (with a warning) and',
|
|
347
|
-
' simply falls back to normal client rendering.',
|
|
348
|
-
'- Hole values are HTML-escaped exactly as React escapes them, so hydration is byte-for-byte',
|
|
349
|
-
" clean. Keep a repeat row's structure the same across items (only the leaf hole values vary).",
|
|
350
|
-
'- Build output for an SSR route lands in `build/client/_ssr/` (the template + its manifest)',
|
|
351
|
-
' alongside the generated `Slot` module; routes without `ssr = true` are unaffected.',
|
|
352
|
-
]),
|
|
353
|
-
'cli.md': doc([
|
|
354
|
-
'# CLI',
|
|
355
|
-
'',
|
|
356
|
-
'- `toiljs create [name]`, scaffold a project. Flags: `--template app|minimal`,',
|
|
357
|
-
' `--style css|sass|less|stylus`, `--tailwind`, `--no-ai`, `-y`/`--yes`.',
|
|
358
|
-
'- `toiljs dev`, dev server with HMR (`--port`, `--root`). With a `toilconfig.json` it builds',
|
|
359
|
-
' the server first, then rebuilds it whenever a `server/` file changes (regenerating',
|
|
360
|
-
' `shared/server.ts`, which Vite HMRs into the client); client-only edits just HMR the client.',
|
|
361
|
-
'- `toiljs build`, production build. With a `toilconfig.json` it builds the server (toilscript,',
|
|
362
|
-
' regenerating `shared/server.ts`) first, then the client → `build/client`. `--server` builds',
|
|
363
|
-
' only the server. Every `server/` file declaring a surface (`@data`/`@rest`/...) is compiled.',
|
|
364
|
-
'- `toiljs start`, self-host the built app (hyper-express) with a `/_toil` WebSocket channel.',
|
|
365
|
-
'- `toiljs configure`, toggle styling features on an existing project (see `styling.md`).',
|
|
366
|
-
'- `toiljs doctor`, diagnose project setup (`--json` for CI). `--fix` auto-wires the typed-RPC',
|
|
367
|
-
' setup (build scripts, tsconfig `shared` + alias, `.gitignore`, toilscript version, and the',
|
|
368
|
-
' toilscript prettier plugin) so an existing project upgrades in one command.',
|
|
369
|
-
]),
|
|
370
|
-
};
|
|
371
|
-
|
|
372
111
|
/** Writes the framework docs into `<toilDir>/docs/`. Called by `generate` each dev/build. */
|
|
373
112
|
export function writeDocs(toilDir: string): void {
|
|
374
113
|
const dir = path.join(toilDir, 'docs');
|