Skip to content

Commit 76dc661

Browse files
bartlomiejuclaude
andauthored
refactor: remove Node/Deno global proxy and simplify CJS wrapper (#33122)
## Summary - Remove the v8 named property handler proxy in `ext/node/global.rs` that intercepted `process` and `window` global accesses based on Node/Deno context - Now that Node.js timers are the default (#33118), the only remaining managed globals were `process` (already set directly on `globalThis`) and `window` (never populated in `denoGlobals`), making the proxy a no-op - Simplify the CJS module wrapper to match Node.js — no longer destructures globals from `Deno[Deno.internal].nodeGlobals` since they're all on `globalThis` - Remove the `nodeGlobals` bag from `02_init.js` and `99_main.js` entirely --------- Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 2c76f45 commit 76dc661

8 files changed

Lines changed: 8 additions & 550 deletions

File tree

ext/node/global.rs

Lines changed: 0 additions & 483 deletions
This file was deleted.

ext/node/lib.rs

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ use node_resolver::errors::PackageJsonLoadError;
2929

3030
extern crate libz_sys as zlib;
3131

32-
mod global;
3332
pub mod ops;
3433

3534
use deno_dotenv::parse_env_content_hook;
@@ -44,10 +43,6 @@ pub use ops::vm::VM_CONTEXT_INDEX;
4443
pub use ops::vm::create_v8_context;
4544
pub use ops::vm::init_global_template;
4645

47-
pub use crate::global::GlobalsStorage;
48-
use crate::global::global_object_middleware;
49-
use crate::global::global_template_middleware;
50-
5146
pub fn is_builtin_node_module(module_name: &str) -> bool {
5247
DenoIsBuiltInNodeModuleChecker.is_builtin_node_module(module_name)
5348
}
@@ -374,7 +369,6 @@ deno_core::extension!(deno_node,
374369
esm_entry_point = "ext:deno_node/02_init.js",
375370
esm = [
376371
dir "polyfills",
377-
"00_globals.js",
378372
"02_init.js",
379373
"_events.mjs",
380374
"internal/fs/promises.ts",
@@ -616,8 +610,6 @@ deno_core::extension!(deno_node,
616610
}
617611

618612
},
619-
global_template_middleware = global_template_middleware,
620-
global_object_middleware = global_object_middleware,
621613
customizer = |ext: &mut deno_core::Extension| {
622614
let external_references = [
623615
vm::QUERY_MAP_FN.with(|query| {
@@ -692,41 +684,6 @@ deno_core::extension!(deno_node,
692684
}
693685
}),
694686

695-
global::GETTER_MAP_FN.with(|getter| {
696-
ExternalReference {
697-
named_getter: *getter,
698-
}
699-
}),
700-
global::SETTER_MAP_FN.with(|setter| {
701-
ExternalReference {
702-
named_setter: *setter,
703-
}
704-
}),
705-
global::QUERY_MAP_FN.with(|query| {
706-
ExternalReference {
707-
named_query: *query,
708-
}
709-
}),
710-
global::DELETER_MAP_FN.with(|deleter| {
711-
ExternalReference {
712-
named_deleter: *deleter,
713-
}
714-
}),
715-
global::ENUMERATOR_MAP_FN.with(|enumerator| {
716-
ExternalReference {
717-
enumerator: *enumerator,
718-
}
719-
}),
720-
global::DEFINER_MAP_FN.with(|definer| {
721-
ExternalReference {
722-
named_definer: *definer,
723-
}
724-
}),
725-
global::DESCRIPTOR_MAP_FN.with(|descriptor| {
726-
ExternalReference {
727-
named_getter: *descriptor,
728-
}
729-
}),
730687
];
731688

732689
ext.external_references.to_mut().extend(external_references);

ext/node/ops/vm.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,11 @@ pub fn init_global_template<'a>(
628628

629629
// Using thread_local! to get around compiler bug.
630630
//
631-
// See NOTE in ext/node/global.rs#L12
631+
// NOTE(bartlomieju): somehow calling `.map_fn_to()` multiple times on a
632+
// function returns two different pointers. That shouldn't be the case as
633+
// `.map_fn_to()` creates a thin wrapper that is a pure function.
634+
// @piscisaureus suggests it might be a bug in Rust compiler; so for now we
635+
// just create and store these mapped functions per-thread.
632636
thread_local! {
633637
pub static QUERY_MAP_FN: v8::NamedPropertyQueryCallback = property_query.map_fn_to();
634638
pub static GETTER_MAP_FN: v8::NamedPropertyGetterCallback = property_getter.map_fn_to();

ext/node/polyfills/00_globals.js

Lines changed: 0 additions & 6 deletions
This file was deleted.

ext/node/polyfills/01_require.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -962,14 +962,9 @@ Module.prototype.require = function (id) {
962962
}
963963
};
964964

965-
// The module wrapper looks slightly different to Node. Instead of using one
966-
// wrapper function, we use two. The first one exists to performance optimize
967-
// access to magic node globals, like `Buffer`. The second one is the actual
968-
// wrapper function we run the users code in. The only observable difference is
969-
// that in Deno `arguments.callee` is not null.
970965
const wrapper = [
971-
`(function (exports, require, module, __filename, __dirname) { var { Buffer, clearImmediate, global, process, setImmediate } = Deno[Deno.internal].nodeGlobals; (() => {`,
972-
"\n})(); })",
966+
"(function (exports, require, module, __filename, __dirname) { ",
967+
"\n});",
973968
];
974969

975970
export let wrap = function (script) {

ext/node/polyfills/02_init.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import { core, internals } from "ext:core/mod.js";
66
const requireImpl = internals.requireImpl;
77

88
import { op_stream_base_register_state } from "ext:core/ops";
9-
import { nodeGlobals } from "ext:deno_node/00_globals.js";
109
import {
1110
kStreamBaseField,
1211
streamBaseState,
@@ -151,12 +150,6 @@ internals.node = {
151150
};
152151

153152
const nativeModuleExports = requireImpl.nativeModuleExports;
154-
nodeGlobals.Buffer = nativeModuleExports["buffer"].Buffer;
155-
nodeGlobals.clearImmediate = nativeModuleExports["timers"].clearImmediate;
156-
nodeGlobals.global = globalThis;
157-
nodeGlobals.process = nativeModuleExports["process"];
158-
nodeGlobals.setImmediate = nativeModuleExports["timers"].setImmediate;
159-
160153
nativeModuleExports["internal/console/constructor"].bindStreamsLazy(
161154
nativeModuleExports["console"],
162155
nativeModuleExports["process"],

runtime/js/99_main.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ import {
9393
} from "ext:runtime/98_global_scope_worker.js";
9494
import { SymbolMetadata } from "ext:deno_web/00_infra.js";
9595
import { bootstrap as bootstrapOtel } from "ext:deno_telemetry/telemetry.ts";
96-
import { nodeGlobals } from "ext:deno_node/00_globals.js";
9796

9897
// deno-lint-ignore prefer-primordials
9998
if (Symbol.metadata) {
@@ -561,7 +560,7 @@ function removeImportedOps() {
561560
// FIXME(bartlomieju): temporarily add whole `Deno.core` to
562561
// `Deno[Deno.internal]` namespace. It should be removed and only necessary
563562
// methods should be left there.
564-
ObjectAssign(internals, { core, nodeGlobals: { ...nodeGlobals } });
563+
ObjectAssign(internals, { core });
565564
const internalSymbol = Symbol("Deno.internal");
566565
const finalDenoNs = {
567566
internal: internalSymbol,

tools/core_import_map.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,6 @@
554554
"ext:deno_node/_util/std_fmt_colors.ts": "../ext/node/polyfills/_util/std_fmt_colors.ts",
555555
"ext:deno_node/_utils.ts": "../ext/node/polyfills/_utils.ts",
556556
"ext:deno_node/_zlib_binding.mjs": "../ext/node/polyfills/_zlib_binding.mjs",
557-
"ext:deno_node/00_globals.js": "../ext/node/polyfills/00_globals.js",
558557
"ext:deno_node/inspector.ts": "../ext/node/polyfills/inspector.ts",
559558
"ext:deno_node/internal_binding/_libuv_winerror.ts": "../ext/node/polyfills/internal_binding/_libuv_winerror.ts",
560559
"ext:deno_node/internal_binding/_listen.ts": "../ext/node/polyfills/internal_binding/_listen.ts",

0 commit comments

Comments
 (0)