toiljs 0.0.67 → 0.0.68

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 (99) hide show
  1. package/CHANGELOG.md +5 -0
  2. package/README.md +63 -61
  3. package/build/backend/.tsbuildinfo +1 -1
  4. package/build/cli/.tsbuildinfo +1 -1
  5. package/build/cli/index.js +13 -1
  6. package/build/client/.tsbuildinfo +1 -1
  7. package/build/client/index.d.ts +2 -0
  8. package/build/client/index.js +1 -0
  9. package/build/client/rpc.js +21 -1
  10. package/build/client/stream/client.d.ts +11 -0
  11. package/build/client/stream/client.js +59 -0
  12. package/build/compiler/.tsbuildinfo +1 -1
  13. package/build/compiler/config.d.ts +2 -0
  14. package/build/compiler/config.js +9 -7
  15. package/build/compiler/index.d.ts +1 -0
  16. package/build/compiler/index.js +16 -2
  17. package/build/compiler/toil-docs.generated.js +2 -2
  18. package/build/devserver/.tsbuildinfo +1 -1
  19. package/build/devserver/daemon/runtime.d.ts +13 -0
  20. package/build/devserver/daemon/runtime.js +29 -0
  21. package/build/devserver/db/database.d.ts +1 -0
  22. package/build/devserver/db/database.js +10 -0
  23. package/build/devserver/db/derives.d.ts +7 -0
  24. package/build/devserver/db/derives.js +94 -0
  25. package/build/devserver/db/index.d.ts +1 -0
  26. package/build/devserver/db/index.js +1 -0
  27. package/build/devserver/db/types.d.ts +1 -0
  28. package/build/devserver/db/types.js +1 -0
  29. package/build/devserver/http/proxy.d.ts +5 -1
  30. package/build/devserver/http/proxy.js +39 -36
  31. package/build/devserver/http/runtime.d.ts +62 -0
  32. package/build/devserver/http/runtime.js +194 -0
  33. package/build/devserver/index.d.ts +2 -0
  34. package/build/devserver/index.js +1 -0
  35. package/build/devserver/production-ipc.d.ts +50 -0
  36. package/build/devserver/production-ipc.js +21 -0
  37. package/build/devserver/production-worker.d.ts +1 -0
  38. package/build/devserver/production-worker.js +73 -0
  39. package/build/devserver/production.d.ts +35 -0
  40. package/build/devserver/production.js +502 -0
  41. package/build/devserver/runtime/module.d.ts +5 -0
  42. package/build/devserver/runtime/module.js +47 -1
  43. package/build/devserver/server.d.ts +1 -0
  44. package/build/devserver/server.js +32 -145
  45. package/build/devserver/ssr.d.ts +2 -0
  46. package/build/devserver/ssr.js +19 -2
  47. package/build/devserver/stream/catalog.d.ts +20 -0
  48. package/build/devserver/stream/catalog.js +54 -0
  49. package/build/devserver/stream/host.d.ts +9 -0
  50. package/build/devserver/stream/host.js +15 -0
  51. package/build/devserver/stream/index.d.ts +37 -0
  52. package/build/devserver/stream/index.js +220 -0
  53. package/build/devserver/stream/manager.d.ts +34 -0
  54. package/build/devserver/stream/manager.js +103 -0
  55. package/build/devserver/stream/router.d.ts +25 -0
  56. package/build/devserver/stream/router.js +64 -0
  57. package/build/devserver/stream/wire.d.ts +5 -0
  58. package/build/devserver/stream/wire.js +33 -0
  59. package/build/devserver/stream/ws.d.ts +18 -0
  60. package/build/devserver/stream/ws.js +46 -0
  61. package/docs/cli.md +3 -1
  62. package/docs/getting-started.md +7 -7
  63. package/examples/basic/server/routes/Guestbook.ts +38 -13
  64. package/package.json +2 -2
  65. package/src/cli/index.ts +14 -1
  66. package/src/client/index.ts +2 -0
  67. package/src/client/rpc.ts +25 -1
  68. package/src/client/stream/client.ts +107 -0
  69. package/src/compiler/config.ts +15 -7
  70. package/src/compiler/index.ts +24 -5
  71. package/src/compiler/toil-docs.generated.ts +2 -2
  72. package/src/devserver/daemon/runtime.ts +48 -0
  73. package/src/devserver/db/database.ts +14 -0
  74. package/src/devserver/db/derives.ts +121 -0
  75. package/src/devserver/db/index.ts +1 -0
  76. package/src/devserver/db/types.ts +6 -0
  77. package/src/devserver/http/proxy.ts +53 -39
  78. package/src/devserver/http/runtime.ts +287 -0
  79. package/src/devserver/index.ts +2 -0
  80. package/src/devserver/production-ipc.ts +63 -0
  81. package/src/devserver/production-worker.ts +83 -0
  82. package/src/devserver/production.ts +706 -0
  83. package/src/devserver/runtime/module.ts +95 -1
  84. package/src/devserver/server.ts +52 -201
  85. package/src/devserver/ssr.ts +23 -3
  86. package/src/devserver/stream/catalog.ts +106 -0
  87. package/src/devserver/stream/host.ts +42 -0
  88. package/src/devserver/stream/index.ts +308 -0
  89. package/src/devserver/stream/manager.ts +163 -0
  90. package/src/devserver/stream/router.ts +101 -0
  91. package/src/devserver/stream/wire.ts +58 -0
  92. package/src/devserver/stream/ws.ts +76 -0
  93. package/test/built-ssr.test.ts +98 -0
  94. package/test/devserver.test.ts +20 -4
  95. package/test/example-guestbook.test.ts +8 -5
  96. package/test/fixtures/stream-echo.ts +26 -0
  97. package/test/fixtures/stream-gate.ts +24 -0
  98. package/test/fixtures/stream-trap.ts +18 -0
  99. package/test/stream-emulation.test.ts +394 -0
@@ -0,0 +1,706 @@
1
+ /**
2
+ * Built-app self-host server: static assets + ToilScript wasm dispatch + edge
3
+ * SSR template assembly. This is the production counterpart to the dev front
4
+ * server, except the fallback is the built client directory instead of Vite.
5
+ */
6
+ import { fork, type ChildProcess } from 'node:child_process';
7
+ import fs from 'node:fs';
8
+ import { availableParallelism } from 'node:os';
9
+ import path from 'node:path';
10
+ import { fileURLToPath } from 'node:url';
11
+
12
+ import {
13
+ type MiddlewareNext,
14
+ type Request,
15
+ type Response,
16
+ Server,
17
+ type Websocket,
18
+ } from '@dacely/hyper-express';
19
+ import pc from 'picocolors';
20
+
21
+ import type { EmailBackendConfig } from 'toiljs/shared';
22
+
23
+ import type { ResolvedDaemonConfig } from './daemon/host.js';
24
+ import { startDaemonRuntime } from './daemon/runtime.js';
25
+ import { configureDbPersistence } from './db/index.js';
26
+ import { initEmailService } from './email/index.js';
27
+ import {
28
+ assembleRouteSsr,
29
+ dispatchEnvelopeRequest,
30
+ installRuntimeErrorHandler,
31
+ isDispatchableMethod,
32
+ prepareSsrResponse,
33
+ prepareWasmResponse,
34
+ resolveStaticFile,
35
+ runtimeServerOptions,
36
+ toEnvelopeRequest,
37
+ } from './http/runtime.js';
38
+ import {
39
+ decodeBody,
40
+ encodeBody,
41
+ isWorkerToPrimaryMessage,
42
+ type ThreadedReply,
43
+ type ThreadedRequest,
44
+ type WorkerToPrimaryMessage,
45
+ } from './production-ipc.js';
46
+ import { WasmServerModule } from './runtime/module.js';
47
+ import { buildSsrRoutes, type DevSsrTemplate, pathnameOf, type SsrRoute } from './ssr.js';
48
+
49
+ const WS_MAX_PAYLOAD_LENGTH = 1024 * 1024;
50
+ const WS_IDLE_TIMEOUT = 120;
51
+ const WS_MAX_BACKPRESSURE = 1024 * 1024 * 2;
52
+
53
+ const CORS_METHODS = 'GET, POST, OPTIONS, PUT, PATCH, DELETE';
54
+ const CORS_HEADERS = 'X-Requested-With, content-type';
55
+
56
+ export interface BuiltServerOptions {
57
+ /** Project root; wasm sendfile paths and local data resolve here. */
58
+ readonly root: string;
59
+ /** Built client directory, usually `<root>/build/client`. */
60
+ readonly staticRoot: string;
61
+ /** Built server wasm, usually `<root>/build/server/release.wasm`. */
62
+ readonly wasmFile?: string;
63
+ /** Built cold daemon wasm, usually `<root>/build/server/release-cold.wasm`. */
64
+ readonly coldWasmFile?: string;
65
+ /** Which layer the self-host process emulates. Default `all`. */
66
+ readonly nodeMode?: string;
67
+ /** Daemon (L4) config mirror used by the self-host daemon runtime. */
68
+ readonly daemon?: ResolvedDaemonConfig;
69
+ /** Listening port. Default `3000`. */
70
+ readonly port?: number;
71
+ /** Bind host. Default `127.0.0.1`. */
72
+ readonly host?: string;
73
+ /** Extra origins allowed to open the WebSocket channel. */
74
+ readonly allowedOrigins?: readonly string[];
75
+ /** WebSocket channel path. Default `/_toil`. */
76
+ readonly wsPath?: string;
77
+ /** Send permissive CORS headers + handle preflight. Default `true`. */
78
+ readonly cors?: boolean;
79
+ /** Max request body bytes. Default 8 MB. */
80
+ readonly maxBodyLength?: number;
81
+ /** Optional self-host email config, secrets still come from env/.env.secrets. */
82
+ readonly email?: EmailBackendConfig;
83
+ /**
84
+ * Number of production HTTP workers for `toiljs start`. Default `auto`
85
+ * (`os.availableParallelism()`). Set `1` to run a single in-process server.
86
+ */
87
+ readonly threads?: number | 'auto';
88
+ }
89
+
90
+ export interface RunningBuiltServer {
91
+ readonly port: number;
92
+ readonly host: string;
93
+ readonly wsPath: string;
94
+ broadcast(message: string): void;
95
+ clientCount(): number;
96
+ close(): Promise<void>;
97
+ }
98
+
99
+ interface TemplateIndexEntry {
100
+ route: string;
101
+ name: string;
102
+ hash?: string;
103
+ }
104
+
105
+ interface BuiltServerPaths {
106
+ readonly port: number;
107
+ readonly host: string;
108
+ readonly wsPath: string;
109
+ readonly cors: boolean;
110
+ readonly projectRoot: string;
111
+ readonly staticRoot: string;
112
+ readonly indexHtml: string;
113
+ }
114
+
115
+ interface BuiltRuntime {
116
+ readonly projectRoot: string;
117
+ readonly module: WasmServerModule | null;
118
+ readonly ssrRoutes: readonly SsrRoute[];
119
+ }
120
+
121
+ interface BuiltHttpRuntimeOptions {
122
+ readonly onClientCount?: (count: number) => void;
123
+ }
124
+
125
+ type DynamicHandler = (request: Request, response: Response) => Promise<boolean>;
126
+
127
+ function resolveBuiltServerPaths(options: BuiltServerOptions): BuiltServerPaths {
128
+ const port = options.port ?? 3000;
129
+ const host = options.host ?? '127.0.0.1';
130
+ const wsPath = options.wsPath ?? '/_toil';
131
+ const staticRoot = path.resolve(options.staticRoot);
132
+ const indexHtml = path.join(staticRoot, 'index.html');
133
+ if (!fs.existsSync(indexHtml)) {
134
+ throw new Error(`No build found in ${staticRoot}. Run \`toiljs build\` first.`);
135
+ }
136
+ return {
137
+ port,
138
+ host,
139
+ wsPath,
140
+ cors: options.cors ?? true,
141
+ projectRoot: path.resolve(options.root),
142
+ staticRoot,
143
+ indexHtml,
144
+ };
145
+ }
146
+
147
+ function resolveThreadCount(threads: BuiltServerOptions['threads']): number {
148
+ const raw = process.env.TOILJS_THREADS ?? threads;
149
+ if (raw === undefined || raw === 'auto') return Math.max(1, availableParallelism());
150
+ const n = typeof raw === 'number' ? raw : Number.parseInt(raw, 10);
151
+ if (!Number.isFinite(n)) return Math.max(1, availableParallelism());
152
+ return Math.max(1, Math.min(128, Math.floor(n)));
153
+ }
154
+
155
+ function headerValue(
156
+ headers: readonly (readonly [string, string])[],
157
+ name: string,
158
+ ): string | undefined {
159
+ const lower = name.toLowerCase();
160
+ return headers.find(([k]) => k.toLowerCase() === lower)?.[1];
161
+ }
162
+
163
+ function textReply(status: number, body: string): ThreadedReply {
164
+ return {
165
+ kind: 'response',
166
+ status,
167
+ headers: [['content-type', 'text/plain; charset=utf-8']],
168
+ body: encodeBody(new TextEncoder().encode(body)),
169
+ sendfile: null,
170
+ };
171
+ }
172
+
173
+ function preparedReply(out: {
174
+ readonly status: number;
175
+ readonly headers: readonly (readonly [string, string])[];
176
+ readonly body: Uint8Array;
177
+ readonly sendfile: string | null;
178
+ }): ThreadedReply {
179
+ return {
180
+ kind: 'response',
181
+ status: out.status,
182
+ headers: out.headers,
183
+ body: encodeBody(out.body),
184
+ sendfile: out.sendfile,
185
+ };
186
+ }
187
+
188
+ function sendThreadedReply(response: Response, reply: ThreadedReply): boolean {
189
+ if (reply.kind === 'fallback') return false;
190
+ response.status(reply.status);
191
+ for (const [name, value] of reply.headers) response.header(name, value);
192
+ if (reply.sendfile !== null) {
193
+ response.sendFile(reply.sendfile);
194
+ return true;
195
+ }
196
+ const body = decodeBody(reply.body);
197
+ response.send(Buffer.from(body.buffer, body.byteOffset, body.length));
198
+ return true;
199
+ }
200
+
201
+ function isWsOriginAllowed(
202
+ origin: string | undefined,
203
+ hostHeader: string | undefined,
204
+ allowed: readonly string[] | undefined,
205
+ ): boolean {
206
+ if (!origin) return true;
207
+ if (allowed?.includes(origin)) return true;
208
+ try {
209
+ return new URL(origin).host === hostHeader;
210
+ } catch {
211
+ return false;
212
+ }
213
+ }
214
+
215
+ function parseSlotsManifest(
216
+ slotsFile: string,
217
+ ): { entries: { id: number; offset: number }[]; hash: Uint8Array } | null {
218
+ const bin = fs.readFileSync(slotsFile);
219
+ if (bin.length < 46) return null;
220
+ if (bin.subarray(0, 4).toString('ascii') !== 'TSLT') return null;
221
+ const n = bin.readUInt16LE(44);
222
+ if (bin.length < 46 + n * 8) return null;
223
+ const entries: { id: number; offset: number }[] = [];
224
+ let o = 46;
225
+ for (let i = 0; i < n; i++) {
226
+ entries.push({ offset: bin.readUInt32LE(o), id: bin.readUInt16LE(o + 4) });
227
+ o += 8;
228
+ }
229
+ return { entries, hash: Buffer.from(bin.subarray(12, 44)) };
230
+ }
231
+
232
+ function isTemplateIndexEntry(value: unknown): value is TemplateIndexEntry {
233
+ if (typeof value !== 'object' || value === null) return false;
234
+ const v = value as Record<string, unknown>;
235
+ return typeof v.route === 'string' && typeof v.name === 'string';
236
+ }
237
+
238
+ /** Loads built SSR templates from `<staticRoot>/_ssr/templates.json`. */
239
+ export function loadBuiltSsrTemplates(staticRoot: string): DevSsrTemplate[] {
240
+ const ssrDir = path.join(staticRoot, '_ssr');
241
+ const indexFile = path.join(ssrDir, 'templates.json');
242
+ if (!fs.existsSync(indexFile)) return [];
243
+ let index: unknown;
244
+ try {
245
+ index = JSON.parse(fs.readFileSync(indexFile, 'utf8'));
246
+ } catch {
247
+ return [];
248
+ }
249
+ if (!Array.isArray(index)) return [];
250
+
251
+ const out: DevSsrTemplate[] = [];
252
+ for (const item of index) {
253
+ if (!isTemplateIndexEntry(item)) continue;
254
+ if (!/^[A-Za-z0-9_]+$/.test(item.name)) continue;
255
+ const tmplFile = path.join(ssrDir, `${item.name}.tmpl`);
256
+ const slotsFile = path.join(ssrDir, `${item.name}.slots`);
257
+ if (!fs.existsSync(tmplFile) || !fs.existsSync(slotsFile)) continue;
258
+ const parsed = parseSlotsManifest(slotsFile);
259
+ if (parsed === null) continue;
260
+ out.push({
261
+ pattern: item.route,
262
+ name: item.name,
263
+ tmpl: fs.readFileSync(tmplFile),
264
+ entries: parsed.entries,
265
+ hash: parsed.hash,
266
+ });
267
+ }
268
+ return out;
269
+ }
270
+
271
+ function createBuiltRuntime(options: BuiltServerOptions, paths: BuiltServerPaths): BuiltRuntime {
272
+ const emailInit = initEmailService(paths.projectRoot, options.email);
273
+ if (emailInit.service !== null) {
274
+ process.stdout.write(pc.dim(` email enabled: ${emailInit.note}`) + '\n');
275
+ } else if (emailInit.note !== null) {
276
+ process.stdout.write(pc.yellow(' ! ') + pc.dim(`email off: ${emailInit.note}`) + '\n');
277
+ }
278
+
279
+ const module =
280
+ options.wasmFile !== undefined && fs.existsSync(options.wasmFile)
281
+ ? new WasmServerModule(options.wasmFile)
282
+ : null;
283
+ if (module !== null) {
284
+ try {
285
+ module.refresh();
286
+ } catch (e) {
287
+ process.stdout.write(pc.red(` x server wasm failed to load: ${String(e)}`) + '\n');
288
+ }
289
+ }
290
+
291
+ const ssrRoutes = buildSsrRoutes(loadBuiltSsrTemplates(paths.staticRoot));
292
+ if (ssrRoutes.length > 0) {
293
+ process.stdout.write(
294
+ pc.dim(` edge SSR: ${String(ssrRoutes.length)} route(s) served server-side`) + '\n',
295
+ );
296
+ }
297
+
298
+ configureDbPersistence(path.join(paths.projectRoot, '.toil', 'devdata.json'));
299
+ return { projectRoot: paths.projectRoot, module, ssrRoutes };
300
+ }
301
+
302
+ function startBuiltDaemon(
303
+ options: BuiltServerOptions,
304
+ paths: BuiltServerPaths,
305
+ ): { close(): void } | null {
306
+ configureDbPersistence(path.join(paths.projectRoot, '.toil', 'devdata.json'));
307
+ return startDaemonRuntime({
308
+ coldWasmFile: options.coldWasmFile,
309
+ nodeMode: options.nodeMode,
310
+ daemon: options.daemon,
311
+ });
312
+ }
313
+
314
+ async function handleBuiltRuntimeRequest(
315
+ runtime: BuiltRuntime,
316
+ request: ThreadedRequest,
317
+ ): Promise<ThreadedReply> {
318
+ const envelopeReq = {
319
+ method: request.method,
320
+ path: request.url,
321
+ headers: request.headers,
322
+ body: decodeBody(request.body),
323
+ clientIp: request.clientIp,
324
+ };
325
+ const dispatchable = isDispatchableMethod(request.method);
326
+ if (dispatchable && runtime.module !== null && runtime.module.available) {
327
+ const hasAuth =
328
+ headerValue(request.headers, 'cookie') !== undefined ||
329
+ headerValue(request.headers, 'authorization') !== undefined;
330
+ try {
331
+ const dispatch = dispatchEnvelopeRequest({
332
+ module: runtime.module,
333
+ envelopeReq,
334
+ method: request.method,
335
+ url: request.url,
336
+ cacheHost: headerValue(request.headers, 'host') ?? 'self-host',
337
+ hasAuth,
338
+ });
339
+ if (dispatch.result !== null) {
340
+ return preparedReply(
341
+ prepareWasmResponse(runtime.projectRoot, dispatch.result, 'toil'),
342
+ );
343
+ }
344
+ } catch (e) {
345
+ process.stdout.write(
346
+ pc.red(` x ${request.method} ${request.path} server error: ${String(e)}`) + '\n',
347
+ );
348
+ return textReply(500, 'internal error\n');
349
+ }
350
+ }
351
+
352
+ if (request.method === 'GET' || request.method === 'HEAD') {
353
+ const route = runtime.ssrRoutes.find((r) => r.test(pathnameOf(request.url)));
354
+ if (route !== undefined) {
355
+ try {
356
+ const out = assembleRouteSsr(route, runtime.module, envelopeReq);
357
+ if (out !== null) {
358
+ return preparedReply(
359
+ prepareSsrResponse(out, request.method === 'HEAD', 'toil'),
360
+ );
361
+ }
362
+ } catch (e) {
363
+ process.stdout.write(pc.red(` x SSR ${request.path}: ${String(e)}`) + '\n');
364
+ return textReply(500, 'internal error\n');
365
+ }
366
+ }
367
+ return { kind: 'fallback' };
368
+ }
369
+
370
+ return textReply(404, 'not found\n');
371
+ }
372
+
373
+ async function startBuiltHttpServer(
374
+ options: BuiltServerOptions,
375
+ paths: BuiltServerPaths,
376
+ dynamicHandler: DynamicHandler,
377
+ runtimeOptions: BuiltHttpRuntimeOptions = {},
378
+ ): Promise<RunningBuiltServer> {
379
+ const cors = paths.cors;
380
+
381
+ const app = new Server(runtimeServerOptions(options));
382
+
383
+ const clients = new Set<Websocket>();
384
+
385
+ installRuntimeErrorHandler(app);
386
+
387
+ if (cors) {
388
+ app.use((request: Request, response: Response, next: MiddlewareNext) => {
389
+ if (request.method !== 'OPTIONS') {
390
+ response.setHeader('Access-Control-Allow-Origin', '*');
391
+ response.setHeader('Access-Control-Allow-Methods', CORS_METHODS);
392
+ response.setHeader('Access-Control-Allow-Headers', CORS_HEADERS);
393
+ }
394
+ response.removeHeader('uWebSockets');
395
+ next();
396
+ });
397
+ app.options('/*', (_request: Request, response: Response) => {
398
+ response.setHeader('Access-Control-Allow-Origin', '*');
399
+ response.setHeader('Access-Control-Allow-Methods', CORS_METHODS);
400
+ response.setHeader('Access-Control-Allow-Headers', CORS_HEADERS);
401
+ response.setHeader('Access-Control-Max-Age', '86400');
402
+ response.status(204).send();
403
+ });
404
+ }
405
+
406
+ app.ws(
407
+ paths.wsPath,
408
+ {
409
+ message_type: 'String',
410
+ max_payload_length: WS_MAX_PAYLOAD_LENGTH,
411
+ idle_timeout: WS_IDLE_TIMEOUT,
412
+ max_backpressure: WS_MAX_BACKPRESSURE,
413
+ },
414
+ (ws) => {
415
+ clients.add(ws);
416
+ runtimeOptions.onClientCount?.(clients.size);
417
+ ws.send(JSON.stringify({ type: 'connected', clients: clients.size }));
418
+ ws.on('message', (message: string) => {
419
+ for (const client of clients) client.send(message);
420
+ });
421
+ ws.on('drain', () => {});
422
+ ws.on('close', () => {
423
+ clients.delete(ws);
424
+ runtimeOptions.onClientCount?.(clients.size);
425
+ });
426
+ },
427
+ );
428
+
429
+ app.upgrade(paths.wsPath, (request: Request, response: Response) => {
430
+ if (
431
+ !isWsOriginAllowed(request.headers.origin, request.headers.host, options.allowedOrigins)
432
+ ) {
433
+ response.status(403).send();
434
+ return;
435
+ }
436
+ response.upgrade({});
437
+ });
438
+
439
+ app.any('/*', async (request: Request, response: Response) => {
440
+ response.removeHeader('uWebSockets');
441
+
442
+ if (request.method === 'GET' || request.method === 'HEAD') {
443
+ const file = resolveStaticFile(paths.staticRoot, request.path);
444
+ if (file !== null) {
445
+ response.sendFile(file);
446
+ return;
447
+ }
448
+ }
449
+
450
+ if (await dynamicHandler(request, response)) return;
451
+
452
+ if (request.method === 'GET' || request.method === 'HEAD') {
453
+ response.sendFile(paths.indexHtml);
454
+ return;
455
+ }
456
+
457
+ response.status(404).send('not found\n');
458
+ });
459
+
460
+ await app.listen(paths.port, paths.host);
461
+
462
+ return {
463
+ port: paths.port,
464
+ host: paths.host,
465
+ wsPath: paths.wsPath,
466
+ broadcast: (message: string): void => {
467
+ for (const client of clients) client.send(message);
468
+ },
469
+ clientCount: (): number => clients.size,
470
+ close: async (): Promise<void> => {
471
+ await app.shutdown();
472
+ },
473
+ };
474
+ }
475
+
476
+ async function toThreadedRequest(request: Request, id: number): Promise<ThreadedRequest> {
477
+ const envelopeReq = await toEnvelopeRequest(request);
478
+ return {
479
+ id,
480
+ method: request.method,
481
+ url: request.url,
482
+ path: request.path,
483
+ headers: envelopeReq.headers,
484
+ body: encodeBody(envelopeReq.body),
485
+ clientIp: envelopeReq.clientIp ?? '127.0.0.1',
486
+ };
487
+ }
488
+
489
+ async function startBuiltServerSingle(options: BuiltServerOptions): Promise<RunningBuiltServer> {
490
+ const paths = resolveBuiltServerPaths(options);
491
+ const runtime = createBuiltRuntime(options, paths);
492
+ const daemon = startBuiltDaemon(options, paths);
493
+ try {
494
+ const server = await startBuiltHttpServer(options, paths, async (request, response) => {
495
+ const reply = await handleBuiltRuntimeRequest(
496
+ runtime,
497
+ await toThreadedRequest(request, 0),
498
+ );
499
+ return sendThreadedReply(response, reply);
500
+ });
501
+ return {
502
+ ...server,
503
+ close: async (): Promise<void> => {
504
+ daemon?.close();
505
+ await server.close();
506
+ },
507
+ };
508
+ } catch (e) {
509
+ daemon?.close();
510
+ throw e;
511
+ }
512
+ }
513
+
514
+ export interface BuiltServerWorkerController {
515
+ request(request: ThreadedRequest): Promise<ThreadedReply>;
516
+ clientCount(count: number): void;
517
+ }
518
+
519
+ export async function startBuiltServerWorker(
520
+ options: BuiltServerOptions,
521
+ controller: BuiltServerWorkerController,
522
+ ): Promise<RunningBuiltServer> {
523
+ const paths = resolveBuiltServerPaths(options);
524
+ let nextRequestId = 1;
525
+ return startBuiltHttpServer(
526
+ { ...options, threads: 1 },
527
+ paths,
528
+ async (request, response) => {
529
+ const reply = await controller.request(
530
+ await toThreadedRequest(request, nextRequestId++),
531
+ );
532
+ return sendThreadedReply(response, reply);
533
+ },
534
+ { onClientCount: (count) => controller.clientCount(count) },
535
+ );
536
+ }
537
+
538
+ function sendToWorker(worker: ChildProcess, message: object): void {
539
+ try {
540
+ worker.send?.(message);
541
+ } catch {
542
+ // Worker is already gone; the exit handler will respawn or close it.
543
+ }
544
+ }
545
+
546
+ function stopWorker(worker: ChildProcess): Promise<void> {
547
+ return new Promise((resolve) => {
548
+ if (worker.exitCode !== null || worker.signalCode !== null) {
549
+ resolve();
550
+ return;
551
+ }
552
+ const hard = setTimeout(() => {
553
+ try {
554
+ worker.kill('SIGTERM');
555
+ } catch {
556
+ // already closed
557
+ }
558
+ resolve();
559
+ }, 1500);
560
+ hard.unref();
561
+ worker.once('exit', () => {
562
+ clearTimeout(hard);
563
+ resolve();
564
+ });
565
+ sendToWorker(worker, { toil: 'shutdown' });
566
+ });
567
+ }
568
+
569
+ async function startThreadedBuiltServer(
570
+ options: BuiltServerOptions,
571
+ threads: number,
572
+ ): Promise<RunningBuiltServer> {
573
+ const paths = resolveBuiltServerPaths(options);
574
+ const runtime = createBuiltRuntime(options, paths);
575
+ const daemon = startBuiltDaemon(options, paths);
576
+ const workerScript = fileURLToPath(new URL('./production-worker.js', import.meta.url));
577
+ const workers = new Map<number, ChildProcess>();
578
+ const clientCounts = new Map<number, number>();
579
+ let closing = false;
580
+
581
+ const handleMessage = (worker: ChildProcess, message: WorkerToPrimaryMessage): void => {
582
+ switch (message.toil) {
583
+ case 'clientCount':
584
+ clientCounts.set(message.workerId, message.count);
585
+ return;
586
+ case 'request':
587
+ void handleBuiltRuntimeRequest(runtime, message.request)
588
+ .then((reply) =>
589
+ sendToWorker(worker, {
590
+ toil: 'reply',
591
+ id: message.request.id,
592
+ reply,
593
+ }),
594
+ )
595
+ .catch((e: unknown) =>
596
+ sendToWorker(worker, {
597
+ toil: 'reply',
598
+ id: message.request.id,
599
+ reply: textReply(500, `internal error: ${String(e)}\n`),
600
+ }),
601
+ );
602
+ return;
603
+ case 'ready':
604
+ return;
605
+ }
606
+ };
607
+
608
+ const spawnWorker = (workerId: number, initial: boolean): Promise<void> =>
609
+ new Promise((resolve, reject) => {
610
+ const worker = fork(workerScript, [], {
611
+ stdio: ['inherit', 'inherit', 'inherit', 'ipc'],
612
+ env: process.env,
613
+ });
614
+ workers.set(workerId, worker);
615
+ clientCounts.set(workerId, 0);
616
+
617
+ let ready = false;
618
+ const failInitial = (error: Error): void => {
619
+ if (!initial || ready) return;
620
+ reject(error);
621
+ };
622
+
623
+ worker.on('message', (value: unknown) => {
624
+ if (!isWorkerToPrimaryMessage(value)) return;
625
+ if (value.toil === 'ready') {
626
+ ready = true;
627
+ resolve();
628
+ return;
629
+ }
630
+ handleMessage(worker, value);
631
+ });
632
+ worker.once('error', (error) => {
633
+ failInitial(error);
634
+ });
635
+ worker.once('exit', (code, signal) => {
636
+ workers.delete(workerId);
637
+ clientCounts.delete(workerId);
638
+ if (closing) return;
639
+ if (!ready) {
640
+ failInitial(
641
+ new Error(
642
+ `production worker ${String(workerId)} exited before listening (code ${String(code)}, signal ${String(signal)})`,
643
+ ),
644
+ );
645
+ return;
646
+ }
647
+ void spawnWorker(workerId, false).catch((e: unknown) => {
648
+ process.stdout.write(
649
+ pc.red(
650
+ ` x production worker ${String(workerId)} restart failed: ${String(e)}`,
651
+ ) + '\n',
652
+ );
653
+ });
654
+ });
655
+ sendToWorker(worker, {
656
+ toil: 'start',
657
+ workerId,
658
+ options: { ...options, threads: 1 },
659
+ });
660
+ });
661
+
662
+ try {
663
+ await Promise.all(Array.from({ length: threads }, (_, i) => spawnWorker(i + 1, true)));
664
+ } catch (e) {
665
+ closing = true;
666
+ daemon?.close();
667
+ await Promise.all([...workers.values()].map((worker) => stopWorker(worker)));
668
+ throw e;
669
+ }
670
+
671
+ process.stdout.write(pc.dim(` production threads: ${String(threads)} HTTP workers`) + '\n');
672
+
673
+ return {
674
+ port: paths.port,
675
+ host: paths.host,
676
+ wsPath: paths.wsPath,
677
+ broadcast: (message: string): void => {
678
+ for (const worker of workers.values()) {
679
+ sendToWorker(worker, { toil: 'broadcast', message });
680
+ }
681
+ },
682
+ clientCount: (): number => {
683
+ let total = 0;
684
+ for (const count of clientCounts.values()) total += count;
685
+ return total;
686
+ },
687
+ close: async (): Promise<void> => {
688
+ closing = true;
689
+ daemon?.close();
690
+ await Promise.all([...workers.values()].map((worker) => stopWorker(worker)));
691
+ },
692
+ };
693
+ }
694
+
695
+ /**
696
+ * Starts a built toil app. Requests are served in this order:
697
+ * 1. concrete static files from `staticRoot`,
698
+ * 2. wasm `handle()` for API/server routes,
699
+ * 3. wasm `render()` + `_ssr` template assembly for SSR routes,
700
+ * 4. SPA fallback to `index.html`.
701
+ */
702
+ export async function startBuiltServer(options: BuiltServerOptions): Promise<RunningBuiltServer> {
703
+ const threads = resolveThreadCount(options.threads);
704
+ if (threads <= 1) return startBuiltServerSingle(options);
705
+ return startThreadedBuiltServer(options, threads);
706
+ }