summaryrefslogtreecommitdiff
path: root/ext/node
diff options
context:
space:
mode:
authorBartek IwaƄczuk <biwanczuk@gmail.com>2022-09-08 22:01:48 +0200
committerGitHub <noreply@github.com>2022-09-08 22:01:48 +0200
commit6c179daff07fe60d478142ea86231a34dc9ee1fd (patch)
tree39ff8701af61eec0674827b4ef62de1d4087e407 /ext/node
parent93cbac69e8a53fbb393fc7a2f12654b223cf01ad (diff)
fix(npm): recursive translation of reexports, remove window global in node code (#15806)
Co-authored-by: David Sherret <dsherret@gmail.com>
Diffstat (limited to 'ext/node')
-rw-r--r--ext/node/01_node.js11
-rw-r--r--ext/node/02_require.js2
-rw-r--r--ext/node/Cargo.toml1
-rw-r--r--ext/node/lib.rs11
4 files changed, 23 insertions, 2 deletions
diff --git a/ext/node/01_node.js b/ext/node/01_node.js
index 883932c5d..d33a7c43c 100644
--- a/ext/node/01_node.js
+++ b/ext/node/01_node.js
@@ -10,6 +10,7 @@
ArrayPrototypeFilter,
ObjectEntries,
ObjectCreate,
+ ObjectDefineProperty,
} = window.__bootstrap.primordials;
function assert(cond) {
@@ -82,7 +83,7 @@
const nativeModuleExports = ObjectCreate(null);
const builtinModules = [];
- function initialize(nodeModules) {
+ function initialize(nodeModules, nodeGlobalThisName) {
assert(!initialized);
initialized = true;
for (const [name, exports] of ObjectEntries(nodeModules)) {
@@ -98,6 +99,14 @@
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,
+ });
}
window.__bootstrap.internals = {
diff --git a/ext/node/02_require.js b/ext/node/02_require.js
index 4f2a998b2..582d42194 100644
--- a/ext/node/02_require.js
+++ b/ext/node/02_require.js
@@ -658,7 +658,7 @@
Module.wrapper = [
// We provide the non-standard APIs in the CommonJS wrapper
// to avoid exposing them in global namespace.
- "(function (exports, require, module, __filename, __dirname, globalThis) { const { Buffer, clearImmediate, clearInterval, clearTimeout, global, process, setImmediate, setInterval, setTimeout} = globalThis; (function () {",
+ "(function (exports, require, module, __filename, __dirname, globalThis) { const { Buffer, clearImmediate, clearInterval, clearTimeout, global, process, setImmediate, setInterval, setTimeout} = globalThis; var window = undefined; (function () {",
"\n}).call(this); })",
];
Module.wrap = function (script) {
diff --git a/ext/node/Cargo.toml b/ext/node/Cargo.toml
index 7265902a8..1c4a43e6f 100644
--- a/ext/node/Cargo.toml
+++ b/ext/node/Cargo.toml
@@ -15,6 +15,7 @@ path = "lib.rs"
[dependencies]
deno_core = { version = "0.149.0", path = "../../core" }
+once_cell = "1.12.0"
path-clean = "=0.1.0"
regex = "1"
serde = "1.0.136"
diff --git a/ext/node/lib.rs b/ext/node/lib.rs
index 99df93be5..753a11b5d 100644
--- a/ext/node/lib.rs
+++ b/ext/node/lib.rs
@@ -7,6 +7,7 @@ use deno_core::op;
use deno_core::url::Url;
use deno_core::Extension;
use deno_core::OpState;
+use once_cell::sync::Lazy;
use std::path::Path;
use std::path::PathBuf;
use std::rc::Rc;
@@ -48,6 +49,16 @@ pub trait DenoDirNpmResolver {
pub const MODULE_ES_SHIM: &str = include_str!("./module_es_shim.js");
+pub static NODE_GLOBAL_THIS_NAME: Lazy<String> = Lazy::new(|| {
+ let now = std::time::SystemTime::now();
+ let seconds = now
+ .duration_since(std::time::SystemTime::UNIX_EPOCH)
+ .unwrap()
+ .as_secs();
+ // use a changing variable name to make it hard to depend on this
+ format!("__DENO_NODE_GLOBAL_THIS_{}__", seconds)
+});
+
struct Unstable(pub bool);
pub fn init<P: NodePermissions + 'static>(