2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2022-08-20 11:31:33 -04:00
|
|
|
|
|
|
|
// deno-lint-ignore-file
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const internals = globalThis.__bootstrap.internals;
|
|
|
|
const primordials = globalThis.__bootstrap.primordials;
|
|
|
|
const {
|
|
|
|
ArrayPrototypePush,
|
|
|
|
ArrayPrototypeFilter,
|
|
|
|
ObjectEntries,
|
|
|
|
ObjectCreate,
|
|
|
|
ObjectDefineProperty,
|
|
|
|
Proxy,
|
|
|
|
ReflectDefineProperty,
|
2023-02-08 19:11:12 -05:00
|
|
|
ReflectDeleteProperty,
|
|
|
|
ReflectGet,
|
2023-02-07 14:22:46 -05:00
|
|
|
ReflectGetOwnPropertyDescriptor,
|
2023-02-08 19:11:12 -05:00
|
|
|
ReflectHas,
|
2023-02-07 14:22:46 -05:00
|
|
|
ReflectOwnKeys,
|
2023-02-08 19:11:12 -05:00
|
|
|
ReflectSet,
|
2023-02-07 14:22:46 -05:00
|
|
|
Set,
|
|
|
|
SetPrototypeHas,
|
|
|
|
} = primordials;
|
2022-08-20 11:31:33 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
function assert(cond) {
|
|
|
|
if (!cond) {
|
|
|
|
throw Error("assert");
|
2022-08-20 11:31:33 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2022-08-20 11:31:33 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
let initialized = false;
|
|
|
|
const nodeGlobals = {};
|
|
|
|
const nodeGlobalThis = new Proxy(globalThis, {
|
2023-02-08 19:11:12 -05:00
|
|
|
get(target, prop) {
|
|
|
|
if (ReflectHas(nodeGlobals, prop)) {
|
|
|
|
return ReflectGet(nodeGlobals, prop);
|
2023-02-07 14:22:46 -05:00
|
|
|
} else {
|
2023-02-08 19:11:12 -05:00
|
|
|
return ReflectGet(target, prop);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
},
|
2023-02-08 19:11:12 -05:00
|
|
|
set(target, prop, value) {
|
|
|
|
if (ReflectHas(nodeGlobals, prop)) {
|
|
|
|
return ReflectSet(nodeGlobals, prop, value);
|
2023-02-07 14:22:46 -05:00
|
|
|
} else {
|
2023-02-08 19:11:12 -05:00
|
|
|
return ReflectSet(target, prop, value);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
},
|
2023-02-08 19:11:12 -05:00
|
|
|
has(target, prop) {
|
|
|
|
return ReflectHas(nodeGlobals, prop) || ReflectHas(target, prop);
|
2023-02-07 14:22:46 -05:00
|
|
|
},
|
2023-02-08 19:11:12 -05:00
|
|
|
deleteProperty(target, prop) {
|
|
|
|
const nodeDeleted = ReflectDeleteProperty(nodeGlobals, prop);
|
|
|
|
const targetDeleted = ReflectDeleteProperty(target, prop);
|
|
|
|
return nodeDeleted || targetDeleted;
|
|
|
|
},
|
|
|
|
ownKeys(target) {
|
|
|
|
const targetKeys = ReflectOwnKeys(target);
|
2023-02-07 14:22:46 -05:00
|
|
|
const nodeGlobalsKeys = ReflectOwnKeys(nodeGlobals);
|
|
|
|
const nodeGlobalsKeySet = new Set(nodeGlobalsKeys);
|
|
|
|
return [
|
|
|
|
...ArrayPrototypeFilter(
|
2023-02-08 19:11:12 -05:00
|
|
|
targetKeys,
|
2023-02-07 14:22:46 -05:00
|
|
|
(k) => !SetPrototypeHas(nodeGlobalsKeySet, k),
|
|
|
|
),
|
|
|
|
...nodeGlobalsKeys,
|
|
|
|
];
|
|
|
|
},
|
2023-02-08 19:11:12 -05:00
|
|
|
defineProperty(target, prop, desc) {
|
|
|
|
if (ReflectHas(nodeGlobals, prop)) {
|
2023-02-07 14:22:46 -05:00
|
|
|
return ReflectDefineProperty(nodeGlobals, prop, desc);
|
|
|
|
} else {
|
2023-02-08 19:11:12 -05:00
|
|
|
return ReflectDefineProperty(target, prop, desc);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
},
|
2023-02-08 19:11:12 -05:00
|
|
|
getOwnPropertyDescriptor(target, prop) {
|
|
|
|
if (ReflectHas(nodeGlobals, prop)) {
|
2023-02-07 14:22:46 -05:00
|
|
|
return ReflectGetOwnPropertyDescriptor(nodeGlobals, prop);
|
|
|
|
} else {
|
2023-02-08 19:11:12 -05:00
|
|
|
return ReflectGetOwnPropertyDescriptor(target, prop);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
2022-09-08 16:01:48 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const nativeModuleExports = ObjectCreate(null);
|
|
|
|
const builtinModules = [];
|
|
|
|
|
|
|
|
function initialize(nodeModules, nodeGlobalThisName) {
|
|
|
|
assert(!initialized);
|
|
|
|
initialized = true;
|
|
|
|
for (const [name, exports] of ObjectEntries(nodeModules)) {
|
|
|
|
nativeModuleExports[name] = exports;
|
|
|
|
ArrayPrototypePush(builtinModules, name);
|
2022-08-20 11:31:33 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
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 = nodeGlobalThis;
|
|
|
|
nodeGlobals.process = nativeModuleExports["process"];
|
|
|
|
nodeGlobals.setImmediate = nativeModuleExports["timers"].setImmediate;
|
|
|
|
nodeGlobals.setInterval = nativeModuleExports["timers"].setInterval;
|
|
|
|
nodeGlobals.setTimeout = nativeModuleExports["timers"].setTimeout;
|
|
|
|
|
|
|
|
// add a hidden global for the esm code to use in order to reliably
|
|
|
|
// get node's globalThis
|
|
|
|
ObjectDefineProperty(globalThis, nodeGlobalThisName, {
|
|
|
|
enumerable: false,
|
|
|
|
writable: false,
|
|
|
|
value: nodeGlobalThis,
|
|
|
|
});
|
2023-02-15 13:44:52 -05:00
|
|
|
// FIXME(bartlomieju): not nice to depend on `Deno` namespace here
|
|
|
|
internals.__bootstrapNodeProcess(Deno.args);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2022-08-20 11:31:33 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
internals.node = {
|
|
|
|
globalThis: nodeGlobalThis,
|
|
|
|
initialize,
|
|
|
|
nativeModuleExports,
|
|
|
|
builtinModules,
|
|
|
|
};
|