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;
|
|
|
|
|
2024-05-05 10:16:02 -04:00
|
|
|
function initialize(args) {
|
|
|
|
const {
|
|
|
|
usesLocalNodeModulesDir,
|
|
|
|
argv0,
|
|
|
|
runningOnMainThread,
|
|
|
|
workerId,
|
|
|
|
maybeWorkerMetadata,
|
|
|
|
nodeDebug,
|
|
|
|
warmup = false,
|
|
|
|
} = args;
|
2024-03-22 16:49:07 -04:00
|
|
|
if (!warmup) {
|
|
|
|
if (initialized) {
|
2024-08-28 16:40:37 -04:00
|
|
|
throw new Error("Node runtime already initialized");
|
2024-03-22 16:49:07 -04:00
|
|
|
}
|
|
|
|
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,
|
2024-05-05 10:16:02 -04:00
|
|
|
nodeDebug ?? "",
|
2024-04-29 05:36:53 -04:00
|
|
|
);
|
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.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;
|