2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-03-20 14:05:13 -04:00
|
|
|
|
|
|
|
// deno-lint-ignore-file
|
|
|
|
|
2024-01-10 17:37:25 -05:00
|
|
|
import { internals } from "ext:core/mod.js";
|
2023-03-20 14:05:13 -04:00
|
|
|
const requireImpl = internals.requireImpl;
|
2024-01-10 17:37:25 -05:00
|
|
|
|
2023-07-19 04:30:04 -04:00
|
|
|
import { nodeGlobals } from "ext:deno_node/00_globals.js";
|
2023-07-02 14:19:30 -04:00
|
|
|
import "node:module";
|
2023-03-20 14:05:13 -04:00
|
|
|
|
|
|
|
let initialized = false;
|
|
|
|
|
|
|
|
function initialize(
|
|
|
|
usesLocalNodeModulesDir,
|
|
|
|
argv0,
|
2024-03-08 11:31:11 -05:00
|
|
|
runningOnMainThread,
|
2024-03-13 15:52:53 -04:00
|
|
|
workerId,
|
2024-03-11 18:18:03 -04:00
|
|
|
maybeWorkerMetadata,
|
2024-03-22 16:49:07 -04:00
|
|
|
warmup = false,
|
2023-03-20 14:05:13 -04:00
|
|
|
) {
|
2024-03-22 16:49:07 -04:00
|
|
|
if (!warmup) {
|
|
|
|
if (initialized) {
|
|
|
|
throw Error("Node runtime already initialized");
|
|
|
|
}
|
|
|
|
initialized = true;
|
|
|
|
if (usesLocalNodeModulesDir) {
|
|
|
|
requireImpl.setUsesLocalNodeModulesDir();
|
|
|
|
}
|
2023-03-20 14:05:13 -04:00
|
|
|
|
2024-03-22 16:49:07 -04:00
|
|
|
// FIXME(bartlomieju): not nice to depend on `Deno` namespace here
|
|
|
|
// but it's the only way to get `args` and `version` and this point.
|
2024-04-29 05:36:53 -04:00
|
|
|
internals.__bootstrapNodeProcess(
|
|
|
|
argv0,
|
|
|
|
Deno.args,
|
|
|
|
Deno.version,
|
|
|
|
Deno.env.get("NODE_DEBUG") ?? "",
|
|
|
|
);
|
2024-03-22 16:49:07 -04:00
|
|
|
internals.__initWorkerThreads(
|
|
|
|
runningOnMainThread,
|
|
|
|
workerId,
|
|
|
|
maybeWorkerMetadata,
|
|
|
|
);
|
|
|
|
internals.__setupChildProcessIpcChannel();
|
|
|
|
// `Deno[Deno.internal].requireImpl` will be unreachable after this line.
|
|
|
|
delete internals.requireImpl;
|
|
|
|
} else {
|
|
|
|
// Warm up the process module
|
2024-04-29 05:36:53 -04:00
|
|
|
internals.__bootstrapNodeProcess(
|
|
|
|
undefined,
|
|
|
|
undefined,
|
|
|
|
undefined,
|
|
|
|
undefined,
|
|
|
|
true,
|
|
|
|
);
|
2024-03-22 16:49:07 -04:00
|
|
|
}
|
2023-03-20 14:05:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function loadCjsModule(moduleName, isMain, inspectBrk) {
|
|
|
|
if (inspectBrk) {
|
|
|
|
requireImpl.setInspectBrk();
|
|
|
|
}
|
|
|
|
requireImpl.Module._load(moduleName, null, { main: isMain });
|
|
|
|
}
|
|
|
|
|
2023-12-13 05:14:16 -05:00
|
|
|
globalThis.nodeBootstrap = initialize;
|
|
|
|
|
2023-03-20 14:05:13 -04:00
|
|
|
internals.node = {
|
|
|
|
initialize,
|
|
|
|
loadCjsModule,
|
|
|
|
};
|
2024-03-22 16:49:07 -04:00
|
|
|
|
|
|
|
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;
|