From b794a5590b919dc4b2cac5d6796e5e120f737000 Mon Sep 17 00:00:00 2001 From: Simon Holthausen Date: Thu, 26 Jan 2023 15:57:21 +0100 Subject: [PATCH] quick experiment to see if it's the process communication --- packages/kit/src/utils/fork.js | 71 +++++++++++++++++++--------------- 1 file changed, 39 insertions(+), 32 deletions(-) diff --git a/packages/kit/src/utils/fork.js b/packages/kit/src/utils/fork.js index 0e407fd5bbac..b8c6b23b3dfd 100644 --- a/packages/kit/src/utils/fork.js +++ b/packages/kit/src/utils/fork.js @@ -1,5 +1,8 @@ import { fileURLToPath } from 'node:url'; import child_process from 'node:child_process'; +import { dirname } from 'node:path'; +import { readFileSync, unlinkSync, writeFileSync } from 'node:fs'; +import * as devalue from 'devalue'; /** * Runs a task in a subprocess so any dangling stuff gets killed upon completion. @@ -12,22 +15,30 @@ import child_process from 'node:child_process'; */ export function forked(module, callback) { if (process.env.SVELTEKIT_FORK) { - process.on( - 'message', - /** @param {any} data */ async (data) => { - if (data?.type === 'args' && data.module === module) { - if (process.send) { - process.send({ - type: 'result', - module, - payload: await callback(data.payload) - }); - - process.exit(0); - } + try { + const data = devalue.parse( + readFileSync(fileURLToPath(dirname(module)) + '/__sveltekit_args__', 'utf-8') + ); + unlinkSync(fileURLToPath(dirname(module)) + '/__sveltekit_args__'); + const run = async () => { + try { + const result = await callback(data); + writeFileSync( + fileURLToPath(dirname(module)) + '/__sveltekit_result__', + devalue.stringify(result), + 'utf-8' + ); + process.exit(0); + } catch (err) { + console.error(err); + process.exit(1); } - } - ); + }; + run(); + } catch (e) { + console.error(e); + process.exit(1); + } } /** @@ -38,34 +49,30 @@ export function forked(module, callback) { return new Promise((fulfil, reject) => { const script = fileURLToPath(new URL(module, import.meta.url)); + writeFileSync( + fileURLToPath(dirname(module)) + '/__sveltekit_args__', + devalue.stringify(opts), + 'utf-8' + ); const child = child_process.fork(script, { stdio: 'inherit', env: { SVELTEKIT_FORK: 'true' - }, - serialization: 'advanced' - }); - - child.on( - 'message', - /** @param {any} data */ (data) => { - if (data?.type === 'result' && data.module === module) { - fulfil(data.payload); - } } - ); + }); child.on('exit', (code) => { if (code) { reject(new Error(`Failed with code ${code}`)); + } else { + const result = readFileSync( + fileURLToPath(dirname(module)) + '/__sveltekit_result__', + 'utf-8' + ); + unlinkSync(fileURLToPath(dirname(module)) + '/__sveltekit_result__'); + fulfil(devalue.parse(result)); } }); - - child.send({ - type: 'args', - module, - payload: opts - }); }); };