2023-03-20 14:05:13 -04:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
|
|
|
|
|
|
|
// deno-lint-ignore-file
|
|
|
|
|
|
|
|
const internals = globalThis.__bootstrap.internals;
|
|
|
|
const requireImpl = internals.requireImpl;
|
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
|
|
|
|
2023-08-15 15:36:36 -04:00
|
|
|
globalThis.nodeBootstrap = function (usesLocalNodeModulesDir, argv0) {
|
|
|
|
initialize(usesLocalNodeModulesDir, argv0);
|
|
|
|
};
|
|
|
|
|
2023-03-20 14:05:13 -04:00
|
|
|
let initialized = false;
|
|
|
|
|
|
|
|
function initialize(
|
|
|
|
usesLocalNodeModulesDir,
|
|
|
|
argv0,
|
|
|
|
) {
|
|
|
|
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"];
|
2023-07-19 04:30:04 -04:00
|
|
|
nodeGlobals.global = globalThis;
|
2023-03-20 14:05:13 -04:00
|
|
|
nodeGlobals.process = nativeModuleExports["process"];
|
|
|
|
nodeGlobals.setImmediate = nativeModuleExports["timers"].setImmediate;
|
|
|
|
nodeGlobals.setInterval = nativeModuleExports["timers"].setInterval;
|
|
|
|
nodeGlobals.setTimeout = nativeModuleExports["timers"].setTimeout;
|
2023-05-15 23:16:24 -04:00
|
|
|
nodeGlobals.performance = nativeModuleExports["perf_hooks"].performance;
|
2023-03-20 14:05:13 -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.
|
|
|
|
internals.__bootstrapNodeProcess(argv0, Deno.args, Deno.version);
|
2023-05-23 14:56:29 -04:00
|
|
|
internals.__initWorkerThreads();
|
2023-03-20 14:05:13 -04:00
|
|
|
// `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 });
|
|
|
|
}
|
|
|
|
|
|
|
|
internals.node = {
|
|
|
|
initialize,
|
|
|
|
loadCjsModule,
|
|
|
|
};
|