mirror of
https://github.com/denoland/deno.git
synced 2024-11-23 15:16:54 -05:00
5a91a065b8
This PR implements the Node child_process IPC functionality in Deno on Unix systems. For `fd > 2` a duplex unix pipe is set up between the parent and child processes. Currently implements data passing via the channel in the JSON serialization format.
58 lines
2 KiB
JavaScript
58 lines
2 KiB
JavaScript
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
|
|
|
// deno-lint-ignore-file
|
|
|
|
const internals = globalThis.__bootstrap.internals;
|
|
const requireImpl = internals.requireImpl;
|
|
import { nodeGlobals } from "ext:deno_node/00_globals.js";
|
|
import "node:module";
|
|
|
|
let initialized = false;
|
|
|
|
function initialize(
|
|
usesLocalNodeModulesDir,
|
|
argv0,
|
|
ipcFd,
|
|
) {
|
|
if (initialized) {
|
|
throw Error("Node runtime already initialized");
|
|
}
|
|
initialized = true;
|
|
if (usesLocalNodeModulesDir) {
|
|
requireImpl.setUsesLocalNodeModulesDir();
|
|
}
|
|
const nativeModuleExports = requireImpl.nativeModuleExports;
|
|
nodeGlobals.Buffer = nativeModuleExports["buffer"].Buffer;
|
|
nodeGlobals.clearImmediate = nativeModuleExports["timers"].clearImmediate;
|
|
nodeGlobals.clearInterval = nativeModuleExports["timers"].clearInterval;
|
|
nodeGlobals.clearTimeout = nativeModuleExports["timers"].clearTimeout;
|
|
nodeGlobals.console = nativeModuleExports["console"];
|
|
nodeGlobals.global = globalThis;
|
|
nodeGlobals.process = nativeModuleExports["process"];
|
|
nodeGlobals.setImmediate = nativeModuleExports["timers"].setImmediate;
|
|
nodeGlobals.setInterval = nativeModuleExports["timers"].setInterval;
|
|
nodeGlobals.setTimeout = nativeModuleExports["timers"].setTimeout;
|
|
nodeGlobals.performance = nativeModuleExports["perf_hooks"].performance;
|
|
|
|
// FIXME(bartlomieju): not nice to depend on `Deno` namespace here
|
|
// but it's the only way to get `args` and `version` and this point.
|
|
internals.__bootstrapNodeProcess(argv0, Deno.args, Deno.version);
|
|
internals.__initWorkerThreads();
|
|
internals.__setupChildProcessIpcChannel(ipcFd);
|
|
// `Deno[Deno.internal].requireImpl` will be unreachable after this line.
|
|
delete internals.requireImpl;
|
|
}
|
|
|
|
function loadCjsModule(moduleName, isMain, inspectBrk) {
|
|
if (inspectBrk) {
|
|
requireImpl.setInspectBrk();
|
|
}
|
|
requireImpl.Module._load(moduleName, null, { main: isMain });
|
|
}
|
|
|
|
globalThis.nodeBootstrap = initialize;
|
|
|
|
internals.node = {
|
|
initialize,
|
|
loadCjsModule,
|
|
};
|