@seanhogg/builderforce-webcontainers 2026.10.8 → 2026.10.9
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/dist/assets.js +3 -3
- package/dist/cdnModules.d.ts +16 -0
- package/dist/cdnModules.d.ts.map +1 -0
- package/dist/cdnModules.js +25 -0
- package/dist/cdnModules.js.map +1 -0
- package/dist/componentCompilers.d.ts +5 -5
- package/dist/componentCompilers.d.ts.map +1 -1
- package/dist/componentCompilers.js +5 -23
- package/dist/componentCompilers.js.map +1 -1
- package/dist/host.d.ts +11 -3
- package/dist/host.d.ts.map +1 -1
- package/dist/host.js +10 -15
- package/dist/host.js.map +1 -1
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/node/childRelay.d.ts +22 -0
- package/dist/node/childRelay.d.ts.map +1 -0
- package/dist/node/childRelay.js +54 -0
- package/dist/node/childRelay.js.map +1 -0
- package/dist/node/processHost.d.ts.map +1 -1
- package/dist/node/processHost.js +18 -32
- package/dist/node/processHost.js.map +1 -1
- package/dist/node/protocol.d.ts +30 -7
- package/dist/node/protocol.d.ts.map +1 -1
- package/dist/node/protocol.js.map +1 -1
- package/dist/node/worker.js +135 -132
- package/dist/node/workerProcess.d.ts.map +1 -1
- package/dist/node/workerProcess.js +31 -4
- package/dist/node/workerProcess.js.map +1 -1
- package/dist/previewUpdates.d.ts +10 -0
- package/dist/previewUpdates.d.ts.map +1 -0
- package/dist/previewUpdates.js +27 -0
- package/dist/previewUpdates.js.map +1 -0
- package/dist/process-worker.js +135 -132
- package/dist/protocol.d.ts +2 -2
- package/dist/protocol.d.ts.map +1 -1
- package/dist/protocol.js +2 -2
- package/dist/protocol.js.map +1 -1
- package/dist/relay.html +1 -1
- package/dist/styleCompilers.d.ts +19 -0
- package/dist/styleCompilers.d.ts.map +1 -0
- package/dist/styleCompilers.js +22 -0
- package/dist/styleCompilers.js.map +1 -0
- package/dist/sw.js +1 -1
- package/dist/transport.d.ts +3 -2
- package/dist/transport.d.ts.map +1 -1
- package/dist/transport.js +4 -4
- package/dist/transport.js.map +1 -1
- package/dist/worker.d.ts +6 -2
- package/dist/worker.d.ts.map +1 -1
- package/dist/worker.js +5 -5
- package/dist/worker.js.map +1 -1
- package/package.json +2 -2
- package/src/cdnModules.ts +34 -0
- package/src/componentCompilers.ts +8 -32
- package/src/host.ts +21 -18
- package/src/index.ts +3 -0
- package/src/node/childRelay.ts +63 -0
- package/src/node/processHost.ts +23 -31
- package/src/node/protocol.ts +24 -8
- package/src/node/workerProcess.ts +36 -7
- package/src/previewUpdates.ts +28 -0
- package/src/protocol.ts +2 -2
- package/src/relay.ts +6 -5
- package/src/styleCompilers.ts +45 -0
- package/src/transport.ts +7 -6
- package/src/worker.ts +5 -5
package/src/node/processHost.ts
CHANGED
|
@@ -15,6 +15,7 @@ import {
|
|
|
15
15
|
PortRegistry,
|
|
16
16
|
ServerEvents,
|
|
17
17
|
allocatePid,
|
|
18
|
+
createIpcEndpoint,
|
|
18
19
|
runArguments,
|
|
19
20
|
textResponse,
|
|
20
21
|
toRuntimeProcess,
|
|
@@ -26,11 +27,13 @@ import {
|
|
|
26
27
|
type HttpStreamResponse,
|
|
27
28
|
type ProcessOptions,
|
|
28
29
|
type RuntimeProcess,
|
|
30
|
+
type StdioChunk,
|
|
29
31
|
type VirtualFileSystem,
|
|
30
32
|
} from '@seanhogg/builderforce-webcontainers-core';
|
|
31
33
|
import type { FsOp, HostToWorker, WorkerToHost } from './protocol.js';
|
|
32
34
|
import { portPath } from './protocol.js';
|
|
33
35
|
import { ResponseReceiver, sendResponse } from './responseWire.js';
|
|
36
|
+
import { ChildRelay, isChildMessage } from './childRelay.js';
|
|
34
37
|
|
|
35
38
|
/** The part of `Worker` the host uses — a real Worker, or an in-thread fake in tests. */
|
|
36
39
|
export interface WorkerLike {
|
|
@@ -74,12 +77,15 @@ interface Proc {
|
|
|
74
77
|
pid: number;
|
|
75
78
|
info: Omit<ProcessInfo, 'ports'>;
|
|
76
79
|
worker: WorkerLike;
|
|
77
|
-
stdout: Set<(chunk:
|
|
78
|
-
stderr: Set<(chunk:
|
|
80
|
+
stdout: Set<(chunk: StdioChunk) => void>;
|
|
81
|
+
stderr: Set<(chunk: StdioChunk) => void>;
|
|
79
82
|
exited: boolean;
|
|
80
83
|
resolveExit(code: number): void;
|
|
81
84
|
registrations: Map<number, { close(): void }>;
|
|
82
|
-
children
|
|
85
|
+
/** The children its Worker spawned, relayed back to it. */
|
|
86
|
+
children: ChildRelay;
|
|
87
|
+
/** Its message channel to whoever spawned it with `ipc`: the parent's side. */
|
|
88
|
+
ipc?: ReturnType<typeof createIpcEndpoint>;
|
|
83
89
|
/** Responses its servers are streaming back to requests forwarded here. */
|
|
84
90
|
responses: ResponseReceiver;
|
|
85
91
|
/** Its loopback requests being answered, by the worker's request id: cancel functions. */
|
|
@@ -143,12 +149,16 @@ export class ProcessHost {
|
|
|
143
149
|
resolveExit(code);
|
|
144
150
|
},
|
|
145
151
|
registrations: new Map(),
|
|
146
|
-
children: new
|
|
152
|
+
children: new ChildRelay((childCommand, childArgs, childOptions) => {
|
|
153
|
+
this.flushOutboxes(); // the child must see every write made so far
|
|
154
|
+
return this.spawnChild(childCommand, childArgs, childOptions);
|
|
155
|
+
}, (message) => this.post(proc, message)),
|
|
147
156
|
responses: new ResponseReceiver(),
|
|
148
157
|
loopbacks: new Map(),
|
|
149
158
|
sockets: new Map(),
|
|
150
159
|
outbox: [],
|
|
151
160
|
};
|
|
161
|
+
if (options.ipc) proc.ipc = createIpcEndpoint((data) => this.post(proc, { type: 'ipc', data }), () => this.post(proc, { type: 'ipc-disconnect' }));
|
|
152
162
|
this.procs.add(proc);
|
|
153
163
|
worker.onmessage = (event) => this.onWorkerMessage(proc, event.data as WorkerToHost);
|
|
154
164
|
worker.onerror = (event) => {
|
|
@@ -164,6 +174,8 @@ export class ProcessHost {
|
|
|
164
174
|
cwd: options.cwd ?? '/',
|
|
165
175
|
env: { ...DEFAULT_ENV, ...this.options.env, ...options.env },
|
|
166
176
|
terminal: options.terminal,
|
|
177
|
+
ipc: options.ipc,
|
|
178
|
+
thread: options.thread,
|
|
167
179
|
files: fs.list().map((path) => [path, fs.readFile(path)!]),
|
|
168
180
|
dirs: fs.explicitDirectories(),
|
|
169
181
|
ports: this.ports.ports(),
|
|
@@ -183,6 +195,7 @@ export class ProcessHost {
|
|
|
183
195
|
kill: (signal = 'SIGTERM') => this.kill(proc, signal),
|
|
184
196
|
resize: (cols, rows) => this.post(proc, { type: 'resize', cols, rows }),
|
|
185
197
|
exit,
|
|
198
|
+
ipc: proc.ipc?.endpoint,
|
|
186
199
|
};
|
|
187
200
|
}
|
|
188
201
|
|
|
@@ -239,6 +252,7 @@ export class ProcessHost {
|
|
|
239
252
|
proc.loopbacks.clear();
|
|
240
253
|
for (const socket of proc.sockets.values()) socket.close();
|
|
241
254
|
proc.sockets.clear();
|
|
255
|
+
proc.ipc?.closed();
|
|
242
256
|
this.procs.delete(proc);
|
|
243
257
|
queueMicrotask(() => proc.worker.terminate());
|
|
244
258
|
}
|
|
@@ -266,6 +280,7 @@ export class ProcessHost {
|
|
|
266
280
|
}
|
|
267
281
|
|
|
268
282
|
private onWorkerMessage(proc: Proc, message: WorkerToHost): void {
|
|
283
|
+
if (isChildMessage(message)) return proc.children.receive(message);
|
|
269
284
|
switch (message.type) {
|
|
270
285
|
case 'stdout':
|
|
271
286
|
for (const l of proc.stdout) l(message.data);
|
|
@@ -289,33 +304,10 @@ export class ProcessHost {
|
|
|
289
304
|
}
|
|
290
305
|
return;
|
|
291
306
|
}
|
|
292
|
-
case '
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
child = this.spawnChild(message.command, message.args, { cwd: message.cwd, env: message.env, terminal: message.terminal });
|
|
297
|
-
} catch (error) {
|
|
298
|
-
return this.post(proc, { type: 'child-error', childId: message.childId, code: (error as { code?: string }).code ?? 'EFAIL', message: (error as Error).message });
|
|
299
|
-
}
|
|
300
|
-
proc.children.set(message.childId, child);
|
|
301
|
-
child.onStdout((data) => this.post(proc, { type: 'child-out', childId: message.childId, stream: 'stdout', data }));
|
|
302
|
-
child.onStderr((data) => this.post(proc, { type: 'child-out', childId: message.childId, stream: 'stderr', data }));
|
|
303
|
-
void child.exit.then((code) => {
|
|
304
|
-
proc.children.delete(message.childId);
|
|
305
|
-
this.post(proc, { type: 'child-exit', childId: message.childId, code });
|
|
306
|
-
});
|
|
307
|
-
return;
|
|
308
|
-
}
|
|
309
|
-
case 'child-stdin': {
|
|
310
|
-
const child = proc.children.get(message.childId);
|
|
311
|
-
if (message.data === null) child?.closeStdin();
|
|
312
|
-
else child?.write(message.data);
|
|
313
|
-
return;
|
|
314
|
-
}
|
|
315
|
-
case 'child-kill':
|
|
316
|
-
return proc.children.get(message.childId)?.kill(message.signal);
|
|
317
|
-
case 'child-resize':
|
|
318
|
-
return proc.children.get(message.childId)?.resize?.(message.cols, message.rows);
|
|
307
|
+
case 'ipc':
|
|
308
|
+
return proc.ipc?.receive(message.data);
|
|
309
|
+
case 'ipc-disconnect':
|
|
310
|
+
return proc.ipc?.closed();
|
|
319
311
|
case 'listen': {
|
|
320
312
|
try {
|
|
321
313
|
const registration = this.ports.listen(
|
package/src/node/protocol.ts
CHANGED
|
@@ -9,8 +9,12 @@
|
|
|
9
9
|
* worker they answer a `loopback` (ids the worker allocated). `request-cancel`
|
|
10
10
|
* goes the other way: the reader hung up. Upgraded connections (WebSockets)
|
|
11
11
|
* are opened by the page (`socket-open`) and carry raw bytes both ways.
|
|
12
|
+
*
|
|
13
|
+
* A message channel (fork, worker threads) is `ipc` / `ipc-disconnect` between a
|
|
14
|
+
* process and the page (its parent's side), and `child-ipc` /
|
|
15
|
+
* `child-ipc-disconnect` between a parent process and the page (its child's side).
|
|
12
16
|
*/
|
|
13
|
-
import type { FileContent, HttpRequestData, HttpResponseHead } from '@seanhogg/builderforce-webcontainers-core';
|
|
17
|
+
import type { FileContent, HttpRequestData, HttpResponseHead, StdioChunk, ThreadInfo } from '@seanhogg/builderforce-webcontainers-core';
|
|
14
18
|
|
|
15
19
|
export type FsOp = { op: 'write'; path: string; content: FileContent } | { op: 'remove'; path: string } | { op: 'mkdir'; path: string };
|
|
16
20
|
|
|
@@ -22,6 +26,9 @@ export interface StartMessage {
|
|
|
22
26
|
cwd: string;
|
|
23
27
|
env: Record<string, string>;
|
|
24
28
|
terminal?: { cols: number; rows: number };
|
|
29
|
+
/** A message channel to the parent is open (`process.send`, a thread's `parentPort`). */
|
|
30
|
+
ipc?: boolean;
|
|
31
|
+
thread?: ThreadInfo;
|
|
25
32
|
files: Array<[string, FileContent]>;
|
|
26
33
|
dirs: string[];
|
|
27
34
|
/** Ports already listening anywhere in the runtime (so `listen` can fail fast with EADDRINUSE). */
|
|
@@ -44,30 +51,38 @@ export type ResponseMessage =
|
|
|
44
51
|
/** Raw bytes of an upgraded connection, either way; ids allocated by the page. */
|
|
45
52
|
export type SocketMessage = { type: 'socket-data'; connId: number; data: Uint8Array } | { type: 'socket-close'; connId: number };
|
|
46
53
|
|
|
54
|
+
/** Message-channel traffic: to/from this process's parent (`ipc`), or one of its children (`child-ipc`). */
|
|
55
|
+
export type IpcMessage =
|
|
56
|
+
| { type: 'ipc'; data: unknown }
|
|
57
|
+
| { type: 'ipc-disconnect' }
|
|
58
|
+
| { type: 'child-ipc'; childId: number; data: unknown }
|
|
59
|
+
| { type: 'child-ipc-disconnect'; childId: number };
|
|
60
|
+
|
|
47
61
|
/** page → worker */
|
|
48
62
|
export type HostToWorker =
|
|
49
63
|
| StartMessage
|
|
50
|
-
| { type: 'stdin'; data:
|
|
64
|
+
| { type: 'stdin'; data: StdioChunk | null }
|
|
51
65
|
| { type: 'kill'; signal: string }
|
|
52
66
|
| { type: 'resize'; cols: number; rows: number }
|
|
53
67
|
| { type: 'fs'; ops: FsOp[] }
|
|
54
68
|
| { type: 'ports'; open: number[]; closed: number[] }
|
|
55
|
-
| { type: 'child-out'; childId: number; stream: 'stdout' | 'stderr'; data:
|
|
69
|
+
| { type: 'child-out'; childId: number; stream: 'stdout' | 'stderr'; data: StdioChunk }
|
|
56
70
|
| { type: 'child-exit'; childId: number; code: number }
|
|
57
71
|
| { type: 'child-error'; childId: number; code: string; message: string }
|
|
58
72
|
| { type: 'http-request'; reqId: number; port: number; request: HttpRequestData }
|
|
59
73
|
| { type: 'request-cancel'; reqId: number }
|
|
60
74
|
| { type: 'socket-open'; connId: number; port: number; request: HttpRequestData }
|
|
61
75
|
| ResponseMessage
|
|
62
|
-
| SocketMessage
|
|
76
|
+
| SocketMessage
|
|
77
|
+
| IpcMessage;
|
|
63
78
|
|
|
64
79
|
/** worker → page */
|
|
65
80
|
export type WorkerToHost =
|
|
66
|
-
| { type: 'stdout' | 'stderr'; data:
|
|
81
|
+
| { type: 'stdout' | 'stderr'; data: StdioChunk }
|
|
67
82
|
| { type: 'exit'; code: number }
|
|
68
83
|
| { type: 'fs'; ops: FsOp[] }
|
|
69
|
-
| { type: 'spawn'; childId: number; command: string; args: string[]; cwd?: string; env?: Record<string, string>; terminal?: { cols: number; rows: number } }
|
|
70
|
-
| { type: 'child-stdin'; childId: number; data:
|
|
84
|
+
| { type: 'spawn'; childId: number; command: string; args: string[]; cwd?: string; env?: Record<string, string>; terminal?: { cols: number; rows: number }; ipc?: boolean; thread?: ThreadInfo }
|
|
85
|
+
| { type: 'child-stdin'; childId: number; data: StdioChunk | null }
|
|
71
86
|
| { type: 'child-kill'; childId: number; signal: string }
|
|
72
87
|
| { type: 'child-resize'; childId: number; cols: number; rows: number }
|
|
73
88
|
| { type: 'listen'; port: number }
|
|
@@ -76,7 +91,8 @@ export type WorkerToHost =
|
|
|
76
91
|
| { type: 'request-cancel'; reqId: number }
|
|
77
92
|
| { type: 'listening'; port: number; url: string }
|
|
78
93
|
| ResponseMessage
|
|
79
|
-
| SocketMessage
|
|
94
|
+
| SocketMessage
|
|
95
|
+
| IpcMessage;
|
|
80
96
|
|
|
81
97
|
/** The preview-relative URL a virtual server on `port` is reachable at. */
|
|
82
98
|
export function portPath(previewBase: string, port: number): string {
|
|
@@ -11,6 +11,7 @@ import {
|
|
|
11
11
|
VirtualFileSystem,
|
|
12
12
|
commandNotFound,
|
|
13
13
|
createChannelPair,
|
|
14
|
+
createIpcEndpoint,
|
|
14
15
|
defaultPrograms,
|
|
15
16
|
resolveCommand,
|
|
16
17
|
runProgram,
|
|
@@ -20,6 +21,8 @@ import {
|
|
|
20
21
|
type FsChange,
|
|
21
22
|
type HttpHandler,
|
|
22
23
|
type PackageCache,
|
|
24
|
+
type ProcessOptions,
|
|
25
|
+
type StdioChunk,
|
|
23
26
|
type System,
|
|
24
27
|
type UpgradeHandler,
|
|
25
28
|
} from '@seanhogg/builderforce-webcontainers-core';
|
|
@@ -43,7 +46,9 @@ export function createWorkerProcess(deps: WorkerProcessDeps): { receive(message:
|
|
|
43
46
|
const fs = new VirtualFileSystem();
|
|
44
47
|
const handlers = new Map<number, { handler: HttpHandler; upgrade?: UpgradeHandler }>();
|
|
45
48
|
const knownPorts = new Set<number>();
|
|
46
|
-
const children = new Map<number, { stdout: Set<(
|
|
49
|
+
const children = new Map<number, { stdout: Set<(chunk: StdioChunk) => void>; stderr: Set<(chunk: StdioChunk) => void>; exit: (code: number) => void; ipc?: ReturnType<typeof createIpcEndpoint> }>();
|
|
50
|
+
/** This process's channel to its parent, when it was spawned with `ipc`. */
|
|
51
|
+
let parentChannel: ReturnType<typeof createIpcEndpoint> | undefined;
|
|
47
52
|
/** Responses to this process's loopback requests, streaming in from the page. */
|
|
48
53
|
const loopbacks = new ResponseReceiver();
|
|
49
54
|
/** Responses this process's servers are streaming to the page, by the page's request id. */
|
|
@@ -89,15 +94,18 @@ export function createWorkerProcess(deps: WorkerProcessDeps): { receive(message:
|
|
|
89
94
|
}
|
|
90
95
|
};
|
|
91
96
|
|
|
92
|
-
function remoteChild(command: string, args: string[], options:
|
|
97
|
+
function remoteChild(command: string, args: string[], options: ProcessOptions = {}): ChildHandle {
|
|
93
98
|
flush(); // the child must see every write made so far
|
|
94
99
|
const childId = nextChild++;
|
|
95
|
-
const stdout = new Set<(
|
|
96
|
-
const stderr = new Set<(
|
|
100
|
+
const stdout = new Set<(chunk: StdioChunk) => void>();
|
|
101
|
+
const stderr = new Set<(chunk: StdioChunk) => void>();
|
|
97
102
|
let resolveExit!: (code: number) => void;
|
|
98
103
|
const exit = new Promise<number>((resolve) => (resolveExit = resolve));
|
|
99
|
-
|
|
100
|
-
|
|
104
|
+
const ipc = options.ipc
|
|
105
|
+
? createIpcEndpoint((data) => deps.post({ type: 'child-ipc', childId, data }), () => deps.post({ type: 'child-ipc-disconnect', childId }))
|
|
106
|
+
: undefined;
|
|
107
|
+
children.set(childId, { stdout, stderr, exit: resolveExit, ipc });
|
|
108
|
+
deps.post({ type: 'spawn', childId, command, args, cwd: options.cwd, env: options.env, terminal: options.terminal, ipc: options.ipc, thread: options.thread });
|
|
101
109
|
return {
|
|
102
110
|
pid: childId,
|
|
103
111
|
onStdout: (l) => (stdout.add(l), () => stdout.delete(l)),
|
|
@@ -107,6 +115,7 @@ export function createWorkerProcess(deps: WorkerProcessDeps): { receive(message:
|
|
|
107
115
|
kill: (signal = 'SIGTERM') => deps.post({ type: 'child-kill', childId, signal }),
|
|
108
116
|
resize: (cols, rows) => deps.post({ type: 'child-resize', childId, cols, rows }),
|
|
109
117
|
exit,
|
|
118
|
+
ipc: ipc?.endpoint,
|
|
110
119
|
};
|
|
111
120
|
}
|
|
112
121
|
|
|
@@ -169,7 +178,18 @@ export function createWorkerProcess(deps: WorkerProcessDeps): { receive(message:
|
|
|
169
178
|
deps.post({ type: 'exit', code: 127 });
|
|
170
179
|
return;
|
|
171
180
|
}
|
|
172
|
-
|
|
181
|
+
if (message.ipc) parentChannel = createIpcEndpoint((data) => deps.post({ type: 'ipc', data }), () => deps.post({ type: 'ipc-disconnect' }));
|
|
182
|
+
process = runProgram({
|
|
183
|
+
program: resolved.program,
|
|
184
|
+
args: resolved.args,
|
|
185
|
+
system,
|
|
186
|
+
cwd: message.cwd,
|
|
187
|
+
env: message.env,
|
|
188
|
+
terminal: message.terminal,
|
|
189
|
+
pid: message.pid,
|
|
190
|
+
thread: message.thread,
|
|
191
|
+
channel: parentChannel?.endpoint,
|
|
192
|
+
});
|
|
173
193
|
process.onStdout((data) => deps.post({ type: 'stdout', data }));
|
|
174
194
|
process.onStderr((data) => deps.post({ type: 'stderr', data }));
|
|
175
195
|
void process.exit.then((code) => {
|
|
@@ -201,9 +221,18 @@ export function createWorkerProcess(deps: WorkerProcessDeps): { receive(message:
|
|
|
201
221
|
for (const l of children.get(message.childId)?.[message.stream] ?? []) l(message.data);
|
|
202
222
|
return;
|
|
203
223
|
case 'child-exit':
|
|
224
|
+
children.get(message.childId)?.ipc?.closed();
|
|
204
225
|
children.get(message.childId)?.exit(message.code);
|
|
205
226
|
children.delete(message.childId);
|
|
206
227
|
return;
|
|
228
|
+
case 'child-ipc':
|
|
229
|
+
return children.get(message.childId)?.ipc?.receive(message.data);
|
|
230
|
+
case 'child-ipc-disconnect':
|
|
231
|
+
return children.get(message.childId)?.ipc?.closed();
|
|
232
|
+
case 'ipc':
|
|
233
|
+
return parentChannel?.receive(message.data);
|
|
234
|
+
case 'ipc-disconnect':
|
|
235
|
+
return parentChannel?.closed();
|
|
207
236
|
case 'child-error': {
|
|
208
237
|
const child = children.get(message.childId);
|
|
209
238
|
for (const l of child?.stderr ?? []) l(`${message.message}\n`);
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { DevServer, FsChange, PreviewUpdate, VirtualFileSystem } from '@seanhogg/builderforce-webcontainers-core';
|
|
2
|
+
|
|
3
|
+
/** Edits arrive in bursts (a save, an agent writing several files): one update per burst. */
|
|
4
|
+
export const UPDATE_DEBOUNCE_MS = 120;
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Watch the files and tell open previews what each burst of edits means — a
|
|
8
|
+
* style swap, components re-rendered in place, or a reload — as the dev server
|
|
9
|
+
* plans it. Returns the stop function.
|
|
10
|
+
*/
|
|
11
|
+
export function watchForPreviewUpdates(fs: VirtualFileSystem, server: DevServer, send: (update: PreviewUpdate) => void): () => void {
|
|
12
|
+
let pending: FsChange[] = [];
|
|
13
|
+
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
14
|
+
const stop = fs.watch((change) => {
|
|
15
|
+
pending.push(change);
|
|
16
|
+
clearTimeout(timer);
|
|
17
|
+
timer = setTimeout(() => {
|
|
18
|
+
const burst = pending;
|
|
19
|
+
pending = [];
|
|
20
|
+
const update = server.planUpdate(burst);
|
|
21
|
+
if (update) send(update);
|
|
22
|
+
}, UPDATE_DEBOUNCE_MS);
|
|
23
|
+
});
|
|
24
|
+
return () => {
|
|
25
|
+
clearTimeout(timer);
|
|
26
|
+
stop();
|
|
27
|
+
};
|
|
28
|
+
}
|
package/src/protocol.ts
CHANGED
|
@@ -28,8 +28,8 @@ export const RELAY_READY = 'bfwc:relay-ready';
|
|
|
28
28
|
export const RELAY_ATTACH = 'bfwc:relay-attach';
|
|
29
29
|
/** relay → host: the worker asked for `id` again. */
|
|
30
30
|
export const RELAY_REATTACH = 'bfwc:relay-reattach';
|
|
31
|
-
/** host → relay:
|
|
32
|
-
export const
|
|
31
|
+
/** host → relay: tell every frame showing the preview at `base` what changed (`update`; absent = reload). */
|
|
32
|
+
export const RELAY_UPDATE = 'bfwc:relay-reload';
|
|
33
33
|
/** host → relay, with a MessagePort: start a process worker on the preview origin, bridged over the port. */
|
|
34
34
|
export const RELAY_SPAWN = 'bfwc:relay-spawn';
|
|
35
35
|
|
package/src/relay.ts
CHANGED
|
@@ -7,9 +7,10 @@
|
|
|
7
7
|
* Whoever may frame this page may drive it, so the server that hosts relay.html
|
|
8
8
|
* must restrict that with `Content-Security-Policy: frame-ancestors <host origins>`.
|
|
9
9
|
*/
|
|
10
|
-
import { ATTACH, REATTACH, RELAY_ATTACH, RELAY_READY, RELAY_REATTACH,
|
|
10
|
+
import { ATTACH, REATTACH, RELAY_ATTACH, RELAY_READY, RELAY_REATTACH, RELAY_SPAWN, RELAY_UPDATE } from './protocol.js';
|
|
11
11
|
import { bridgeWorker, RELAY_PROCESS_WORKER } from './relayWorker.js';
|
|
12
|
-
import {
|
|
12
|
+
import type { PreviewUpdate } from '@seanhogg/builderforce-webcontainers-core';
|
|
13
|
+
import { broadcastUpdate, waitForActive } from './worker.js';
|
|
13
14
|
|
|
14
15
|
const host = window.parent;
|
|
15
16
|
|
|
@@ -25,11 +26,11 @@ async function start(): Promise<void> {
|
|
|
25
26
|
|
|
26
27
|
addEventListener('message', (event) => {
|
|
27
28
|
if (event.source !== host) return;
|
|
28
|
-
const data = event.data as { type?: string; id?: unknown; base?: unknown } | null;
|
|
29
|
+
const data = event.data as { type?: string; id?: unknown; base?: unknown; update?: PreviewUpdate } | null;
|
|
29
30
|
if (data?.type === RELAY_ATTACH && typeof data.id === 'string' && event.ports[0]) {
|
|
30
31
|
registration.active?.postMessage({ type: ATTACH, id: data.id }, [event.ports[0]]);
|
|
31
|
-
} else if (data?.type ===
|
|
32
|
-
|
|
32
|
+
} else if (data?.type === RELAY_UPDATE && typeof data.base === 'string') {
|
|
33
|
+
broadcastUpdate(data.base, data.update ?? { type: 'reload' });
|
|
33
34
|
} else if (data?.type === RELAY_SPAWN && event.ports[0]) {
|
|
34
35
|
// Process code runs HERE, on the preview origin, never on the host's.
|
|
35
36
|
bridgeWorker(event.ports[0], new Worker(new URL(`./${RELAY_PROCESS_WORKER}`, location.href), { name: 'bfwc-process' }));
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createLessCompiler,
|
|
3
|
+
createSassCompiler,
|
|
4
|
+
type LessLike,
|
|
5
|
+
type SassLike,
|
|
6
|
+
type StyleCompiler,
|
|
7
|
+
type StyleCompilers,
|
|
8
|
+
} from '@seanhogg/builderforce-webcontainers-core';
|
|
9
|
+
import { memoizedLoader, nativeImport, rangeOf, type ImportModule } from './cdnModules.js';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Sass and Less, fetched from a CDN the first time a project has a `.scss`,
|
|
13
|
+
* `.sass` or `.less` file (or a `<style lang>` block), pinned to the project's own
|
|
14
|
+
* range. Both official packages ship pure-JS builds that run in a page.
|
|
15
|
+
*/
|
|
16
|
+
export interface CdnStyleCompilerOptions {
|
|
17
|
+
/** The ES module URL of `sass` for a range. */
|
|
18
|
+
sassUrl?: (range: string) => string;
|
|
19
|
+
/** The ES module URL of `less` for a range. */
|
|
20
|
+
lessUrl?: (range: string) => string;
|
|
21
|
+
/** Loads a module by URL. Default: native `import()`. */
|
|
22
|
+
importModule?: ImportModule;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export const defaultSassUrl = (range: string) => `https://esm.sh/sass@${range}`;
|
|
26
|
+
export const defaultLessUrl = (range: string) => `https://esm.sh/less@${range}`;
|
|
27
|
+
|
|
28
|
+
/** A CommonJS build arrives as the default export; an ES build as the namespace. */
|
|
29
|
+
const moduleApi = <T>(module: unknown, probe: string): T => {
|
|
30
|
+
const namespace = module as Record<string, unknown>;
|
|
31
|
+
return (probe in namespace ? namespace : namespace.default) as T;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export function createCdnStyleCompilers(options: CdnStyleCompilerOptions = {}): StyleCompilers {
|
|
35
|
+
const load = memoizedLoader<StyleCompiler>(options.importModule ?? nativeImport);
|
|
36
|
+
return (language, dependencies) => {
|
|
37
|
+
if (language === 'less') {
|
|
38
|
+
const url = (options.lessUrl ?? defaultLessUrl)(rangeOf(dependencies, 'less', '4'));
|
|
39
|
+
return load(url, (module) => createLessCompiler(moduleApi<LessLike>(module, 'render')));
|
|
40
|
+
}
|
|
41
|
+
const range = rangeOf(dependencies, 'sass', rangeOf(dependencies, 'sass-embedded', '1'));
|
|
42
|
+
const url = (options.sassUrl ?? defaultSassUrl)(range);
|
|
43
|
+
return load(url, (module) => createSassCompiler(moduleApi<SassLike>(module, 'compileStringAsync')));
|
|
44
|
+
};
|
|
45
|
+
}
|
package/src/transport.ts
CHANGED
|
@@ -16,12 +16,13 @@ import {
|
|
|
16
16
|
RELAY_ATTACH,
|
|
17
17
|
RELAY_READY,
|
|
18
18
|
RELAY_REATTACH,
|
|
19
|
-
RELAY_RELOAD,
|
|
20
19
|
RELAY_SPAWN,
|
|
20
|
+
RELAY_UPDATE,
|
|
21
21
|
} from './protocol.js';
|
|
22
|
+
import type { PreviewUpdate } from '@seanhogg/builderforce-webcontainers-core';
|
|
22
23
|
import type { WorkerLike } from './node/processHost.js';
|
|
23
24
|
import { workerOverPort } from './relayWorker.js';
|
|
24
|
-
import {
|
|
25
|
+
import { broadcastUpdate, waitForActive } from './worker.js';
|
|
25
26
|
|
|
26
27
|
export interface PreviewTransport {
|
|
27
28
|
/** The origin previews are served from. */
|
|
@@ -32,8 +33,8 @@ export interface PreviewTransport {
|
|
|
32
33
|
attach(id: string, port: MessagePort): void;
|
|
33
34
|
/** The worker restarted and wants `id` again. */
|
|
34
35
|
onReattach(listener: (id: string) => void): () => void;
|
|
35
|
-
/**
|
|
36
|
-
|
|
36
|
+
/** Tell every frame showing the preview at `base` what changed: a hot update, or reload. */
|
|
37
|
+
send(base: string, update: PreviewUpdate): void;
|
|
37
38
|
/**
|
|
38
39
|
* Start a process worker on the preview origin. Only the relay has one to
|
|
39
40
|
* offer; same-origin mode runs processes on the host's origin, which is the
|
|
@@ -69,7 +70,7 @@ export async function sameOriginTransport(serviceWorkerUrl: string, scopeOption?
|
|
|
69
70
|
listeners.add(listener);
|
|
70
71
|
return () => listeners.delete(listener);
|
|
71
72
|
},
|
|
72
|
-
|
|
73
|
+
send: broadcastUpdate,
|
|
73
74
|
dispose() {
|
|
74
75
|
container.removeEventListener('message', onWorkerMessage);
|
|
75
76
|
listeners.clear();
|
|
@@ -126,7 +127,7 @@ export async function relayTransport(relayUrl: string): Promise<PreviewTransport
|
|
|
126
127
|
listeners.add(listener);
|
|
127
128
|
return () => listeners.delete(listener);
|
|
128
129
|
},
|
|
129
|
-
|
|
130
|
+
send: (base, update) => post({ type: RELAY_UPDATE, base, update }),
|
|
130
131
|
createWorker() {
|
|
131
132
|
const channel = new MessageChannel();
|
|
132
133
|
post({ type: RELAY_SPAWN }, [channel.port2]);
|
package/src/worker.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Small helpers shared by the page-side transports and the relay page.
|
|
3
3
|
*/
|
|
4
|
-
import {
|
|
4
|
+
import { updateChannelName, type PreviewUpdate } from '@seanhogg/builderforce-webcontainers-core';
|
|
5
5
|
|
|
6
6
|
/** Resolve once the registration has an active worker. */
|
|
7
7
|
export function waitForActive(registration: ServiceWorkerRegistration): Promise<ServiceWorker> {
|
|
@@ -16,10 +16,10 @@ export function waitForActive(registration: ServiceWorkerRegistration): Promise<
|
|
|
16
16
|
});
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
-
/**
|
|
20
|
-
export function
|
|
19
|
+
/** Tell every frame (on this origin) showing the preview served at `base` what changed. */
|
|
20
|
+
export function broadcastUpdate(base: string, update: PreviewUpdate): void {
|
|
21
21
|
if (typeof BroadcastChannel === 'undefined') return;
|
|
22
|
-
const channel = new BroadcastChannel(
|
|
23
|
-
channel.postMessage(
|
|
22
|
+
const channel = new BroadcastChannel(updateChannelName(base));
|
|
23
|
+
channel.postMessage(update);
|
|
24
24
|
channel.close();
|
|
25
25
|
}
|