HEX
Server: nginx/1.18.0
System: Linux test-ipsremont 5.4.0-214-generic #234-Ubuntu SMP Fri Mar 14 23:50:27 UTC 2025 x86_64
User: ips (1000)
PHP: 8.0.30
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: //var/www/quadcode.com/node_modules/@sveltejs/kit/src/utils/fork.js
import { fileURLToPath } from 'node:url';
import { Worker, parentPort } from 'node:worker_threads';

/**
 * Runs a task in a subprocess so any dangling stuff gets killed upon completion.
 * The subprocess needs to be the file `forked` is called in, and `forked` needs to be called eagerly at the top level.
 * @template T
 * @template U
 * @param {string} module `import.meta.url` of the file
 * @param {(opts: T) => U} callback The function that is invoked in the subprocess
 * @returns {(opts: T) => Promise<U>} A function that when called starts the subprocess
 */
export function forked(module, callback) {
	if (process.env.SVELTEKIT_FORK && parentPort) {
		parentPort.on(
			'message',
			/** @param {any} data */ async (data) => {
				if (data?.type === 'args' && data.module === module) {
					parentPort?.postMessage({
						type: 'result',
						module,
						payload: await callback(data.payload)
					});
				}
			}
		);

		parentPort.postMessage({ type: 'ready', module });
	}

	/**
	 * @param {T} opts
	 * @returns {Promise<U>}
	 */
	const fn = function (opts) {
		return new Promise((fulfil, reject) => {
			const worker = new Worker(fileURLToPath(module), {
				env: {
					...process.env,
					SVELTEKIT_FORK: 'true'
				}
			});

			worker.on(
				'message',
				/** @param {any} data */ (data) => {
					if (data?.type === 'ready' && data.module === module) {
						worker.postMessage({
							type: 'args',
							module,
							payload: opts
						});
					}

					if (data?.type === 'result' && data.module === module) {
						worker.terminate();
						fulfil(data.payload);
					}
				}
			);

			worker.on('exit', (code) => {
				if (code) {
					reject(new Error(`Failed with code ${code}`));
				}
			});
		});
	};

	return fn;
}