diff options
author | Luca Casonato <hello@lcas.dev> | 2023-07-19 10:30:04 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-07-19 10:30:04 +0200 |
commit | e511022c7445cc22193edb1626c77d9674935425 (patch) | |
tree | 521b30eac14cd19a506c9cdfa52cde1da7211dcf /ext/node/lib.rs | |
parent | bf4e99cbd77087706e7ea7034bd90079c2218e2b (diff) |
feat(ext/node): properly segregate node globals (#19307)
Code run within Deno-mode and Node-mode should have access to a
slightly different set of globals. Previously this was done through a
compile time code-transform for Node-mode, but this is not ideal and has
many edge cases, for example Node's globalThis having a different
identity than Deno's globalThis.
This commit makes the `globalThis` of the entire runtime a semi-proxy.
This proxy returns a different set of globals depending on the caller's
mode. This is not a full proxy, because it is shadowed by "real"
properties on globalThis. This is done to avoid the overhead of a full
proxy for all globalThis operations.
The globals between Deno-mode and Node-mode are now properly segregated.
This means that code running in Deno-mode will not have access to Node's
globals, and vice versa. Deleting a managed global in Deno-mode will
NOT delete the corresponding global in Node-mode, and vice versa.
---------
Co-authored-by: Bartek IwaĆczuk <biwanczuk@gmail.com>
Co-authored-by: Aapo Alasuutari <aapo.alasuutari@gmail.com>
Diffstat (limited to 'ext/node/lib.rs')
-rw-r--r-- | ext/node/lib.rs | 59 |
1 files changed, 50 insertions, 9 deletions
diff --git a/ext/node/lib.rs b/ext/node/lib.rs index 3698e5376..89ee87cd3 100644 --- a/ext/node/lib.rs +++ b/ext/node/lib.rs @@ -13,6 +13,7 @@ use deno_core::serde_v8; use deno_core::url::Url; #[allow(unused_imports)] use deno_core::v8; +use deno_core::v8::ExternalReference; use deno_core::JsRuntime; use deno_core::ModuleSpecifier; use deno_fs::sync::MaybeSend; @@ -25,6 +26,7 @@ use once_cell::sync::Lazy; pub mod analyze; pub mod errors; +mod global; mod ops; mod package_json; mod path; @@ -41,6 +43,9 @@ pub use resolution::NodeResolution; pub use resolution::NodeResolutionMode; pub use resolution::NodeResolver; +use crate::global::global_object_middleware; +use crate::global::global_template_middleware; + pub trait NodePermissions { fn check_net_url( &mut self, @@ -112,8 +117,6 @@ pub trait NpmResolver: std::fmt::Debug + MaybeSend + MaybeSync { ) -> Result<(), AnyError>; } -pub const NODE_GLOBAL_THIS_NAME: &str = env!("NODE_GLOBAL_THIS_NAME"); - pub static NODE_ENV_VAR_ALLOWLIST: Lazy<HashSet<String>> = Lazy::new(|| { // The full list of environment variables supported by Node.js is available // at https://nodejs.org/api/cli.html#environment-variables @@ -510,7 +513,49 @@ deno_core::extension!(deno_node, npm_resolver, ))) } - } + }, + global_template_middleware = global_template_middleware, + global_object_middleware = global_object_middleware, + customizer = |ext: &mut deno_core::ExtensionBuilder| { + let mut external_references = Vec::with_capacity(7); + + global::GETTER_MAP_FN.with(|getter| { + external_references.push(ExternalReference { + named_getter: *getter, + }); + }); + global::SETTER_MAP_FN.with(|setter| { + external_references.push(ExternalReference { + named_setter: *setter, + }); + }); + global::QUERY_MAP_FN.with(|query| { + external_references.push(ExternalReference { + named_getter: *query, + }); + }); + global::DELETER_MAP_FN.with(|deleter| { + external_references.push(ExternalReference { + named_getter: *deleter, + },); + }); + global::ENUMERATOR_MAP_FN.with(|enumerator| { + external_references.push(ExternalReference { + enumerator: *enumerator, + }); + }); + global::DEFINER_MAP_FN.with(|definer| { + external_references.push(ExternalReference { + named_definer: *definer, + }); + }); + global::DESCRIPTOR_MAP_FN.with(|descriptor| { + external_references.push(ExternalReference { + named_getter: *descriptor, + }); + }); + ext.external_references(external_references); + }, ); pub fn initialize_runtime( @@ -524,16 +569,12 @@ pub fn initialize_runtime( "undefined".to_string() }; let source_code = format!( - r#"(function loadBuiltinNodeModules(nodeGlobalThisName, usesLocalNodeModulesDir, argv0) {{ + r#"(function loadBuiltinNodeModules(usesLocalNodeModulesDir, argv0) {{ Deno[Deno.internal].node.initialize( - nodeGlobalThisName, usesLocalNodeModulesDir, argv0 ); - // Make the nodeGlobalThisName unconfigurable here. - Object.defineProperty(globalThis, nodeGlobalThisName, {{ configurable: false }}); - }})('{}', {}, {});"#, - NODE_GLOBAL_THIS_NAME, uses_local_node_modules_dir, argv0 + }})({uses_local_node_modules_dir}, {argv0});"#, ); js_runtime.execute_script(located_script_name!(), source_code.into())?; |