Skip to content

Commit 2e6da94

Browse files
fernandolguevarabenmccannRich-Harris
authored
feat: prerender & analyse in worker rather than subprocess to support Deno (#9919)
* feat(fork): use workers * Create hot-actors-hope.md * Update packages/kit/src/utils/fork.js Co-authored-by: Rich Harris <[email protected]> --------- Co-authored-by: Ben McCann <[email protected]> Co-authored-by: Rich Harris <[email protected]>
1 parent 50acb22 commit 2e6da94

File tree

2 files changed

+22
-22
lines changed

2 files changed

+22
-22
lines changed

.changeset/hot-actors-hope.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@sveltejs/kit": patch
3+
---
4+
5+
feat: prerender in worker rather than subprocess to support Deno

packages/kit/src/utils/fork.js

+17-22
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { fileURLToPath } from 'node:url';
2-
import child_process from 'node:child_process';
2+
import { Worker, isMainThread, parentPort } from 'node:worker_threads';
33

44
/**
55
* Runs a task in a subprocess so any dangling stuff gets killed upon completion.
@@ -11,23 +11,21 @@ import child_process from 'node:child_process';
1111
* @returns {(opts: T) => Promise<U>} A function that when called starts the subprocess
1212
*/
1313
export function forked(module, callback) {
14-
if (process.env.SVELTEKIT_FORK && process.send) {
15-
process.send({ type: 'ready', module });
16-
17-
process.on(
14+
if (!isMainThread && parentPort) {
15+
parentPort.on(
1816
'message',
1917
/** @param {any} data */ async (data) => {
2018
if (data?.type === 'args' && data.module === module) {
21-
if (process.send) {
22-
process.send({
23-
type: 'result',
24-
module,
25-
payload: await callback(data.payload)
26-
});
27-
}
19+
parentPort?.postMessage({
20+
type: 'result',
21+
module,
22+
payload: await callback(data.payload)
23+
});
2824
}
2925
}
3026
);
27+
28+
parentPort.postMessage({ type: 'ready', module });
3129
}
3230

3331
/**
@@ -36,34 +34,31 @@ export function forked(module, callback) {
3634
*/
3735
const fn = function (opts) {
3836
return new Promise((fulfil, reject) => {
39-
const child = child_process.fork(fileURLToPath(module), {
40-
stdio: 'inherit',
37+
const worker = new Worker(fileURLToPath(module), {
4138
env: {
42-
...process.env,
43-
SVELTEKIT_FORK: 'true'
44-
},
45-
serialization: 'advanced'
39+
...process.env
40+
}
4641
});
4742

48-
child.on(
43+
worker.on(
4944
'message',
5045
/** @param {any} data */ (data) => {
5146
if (data?.type === 'ready' && data.module === module) {
52-
child.send({
47+
worker.postMessage({
5348
type: 'args',
5449
module,
5550
payload: opts
5651
});
5752
}
5853

5954
if (data?.type === 'result' && data.module === module) {
60-
child.kill();
55+
worker.terminate();
6156
fulfil(data.payload);
6257
}
6358
}
6459
);
6560

66-
child.on('exit', (code) => {
61+
worker.on('exit', (code) => {
6762
if (code) {
6863
reject(new Error(`Failed with code ${code}`));
6964
}

0 commit comments

Comments
 (0)