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/polyfills/01_require.js | |
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/polyfills/01_require.js')
-rw-r--r-- | ext/node/polyfills/01_require.js | 16 |
1 files changed, 6 insertions, 10 deletions
diff --git a/ext/node/polyfills/01_require.js b/ext/node/polyfills/01_require.js index 3ca4ab428..696c1ef5c 100644 --- a/ext/node/polyfills/01_require.js +++ b/ext/node/polyfills/01_require.js @@ -40,7 +40,6 @@ const { Error, TypeError, } = primordials; -import { nodeGlobalThis } from "ext:deno_node/00_globals.js"; import _httpAgent from "ext:deno_node/_http_agent.mjs"; import _httpOutgoing from "ext:deno_node/_http_outgoing.ts"; import _streamDuplex from "ext:deno_node/internal/streams/duplex.mjs"; @@ -342,7 +341,7 @@ function tryPackage(requestPath, exts, isMain, originalPath) { err.requestPath = originalPath; throw err; } else { - nodeGlobalThis.process.emitWarning( + process.emitWarning( `Invalid 'main' field in '${packageJsonPath}' of '${pkg}'. ` + "Please either fix that or report it to the module author", "DeprecationWarning", @@ -414,7 +413,7 @@ function getExportsForCircularRequire(module) { } function emitCircularRequireWarning(prop) { - nodeGlobalThis.process.emitWarning( + process.emitWarning( `Accessing non-existent property '${String(prop)}' of module exports ` + "inside circular dependency", ); @@ -704,7 +703,7 @@ Module._load = function (request, parent, isMain) { const module = cachedModule || new Module(filename, parent); if (isMain) { - nodeGlobalThis.process.mainModule = module; + process.mainModule = module; mainModule = module; module.id = "."; } @@ -913,9 +912,7 @@ Module.prototype.require = function (id) { }; 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, console, global, process, setImmediate, setInterval, setTimeout, performance} = globalThis; var window = undefined; (function () {", + "(function (exports, require, module, __filename, __dirname) { (function () {", "\n}).call(this); })", ]; Module.wrap = function (script) { @@ -950,7 +947,7 @@ function wrapSafe( const wrapper = Module.wrap(content); const [f, err] = core.evalContext(wrapper, `file://${filename}`); if (err) { - if (nodeGlobalThis.process.mainModule === cjsModuleInstance) { + if (process.mainModule === cjsModuleInstance) { enrichCJSError(err.thrown); } if (isEsmSyntaxError(err.thrown)) { @@ -988,7 +985,6 @@ Module.prototype._compile = function (content, filename) { this, filename, dirname, - nodeGlobalThis, ); if (requireDepth === 0) { statCache = null; @@ -1050,7 +1046,7 @@ Module._extensions[".node"] = function (module, filename) { if (filename.endsWith("fsevents.node")) { throw new Error("Using fsevents module is currently not supported"); } - module.exports = ops.op_napi_open(filename, nodeGlobalThis); + module.exports = ops.op_napi_open(filename, globalThis); }; function createRequireFromPath(filename) { |