diff options
Diffstat (limited to 'ext/node/polyfills')
-rw-r--r-- | ext/node/polyfills/00_globals.js | 70 | ||||
-rw-r--r-- | ext/node/polyfills/01_require.js | 16 | ||||
-rw-r--r-- | ext/node/polyfills/02_init.js | 14 |
3 files changed, 10 insertions, 90 deletions
diff --git a/ext/node/polyfills/00_globals.js b/ext/node/polyfills/00_globals.js index 9952d86aa..c3f064a3f 100644 --- a/ext/node/polyfills/00_globals.js +++ b/ext/node/polyfills/00_globals.js @@ -2,71 +2,5 @@ // deno-lint-ignore-file -const primordials = globalThis.__bootstrap.primordials; -const { - ArrayPrototypeFilter, - Proxy, - ReflectDefineProperty, - ReflectDeleteProperty, - ReflectGet, - ReflectGetOwnPropertyDescriptor, - ReflectHas, - ReflectOwnKeys, - ReflectSet, - Set, - SetPrototypeHas, -} = primordials; - -const nodeGlobals = {}; -const nodeGlobalThis = new Proxy(globalThis, { - get(target, prop) { - if (ReflectHas(nodeGlobals, prop)) { - return ReflectGet(nodeGlobals, prop); - } else { - return ReflectGet(target, prop); - } - }, - set(target, prop, value) { - if (ReflectHas(nodeGlobals, prop)) { - return ReflectSet(nodeGlobals, prop, value); - } else { - return ReflectSet(target, prop, value); - } - }, - has(target, prop) { - return ReflectHas(nodeGlobals, prop) || ReflectHas(target, prop); - }, - deleteProperty(target, prop) { - const nodeDeleted = ReflectDeleteProperty(nodeGlobals, prop); - const targetDeleted = ReflectDeleteProperty(target, prop); - return nodeDeleted || targetDeleted; - }, - ownKeys(target) { - const targetKeys = ReflectOwnKeys(target); - const nodeGlobalsKeys = ReflectOwnKeys(nodeGlobals); - const nodeGlobalsKeySet = new Set(nodeGlobalsKeys); - return [ - ...ArrayPrototypeFilter( - targetKeys, - (k) => !SetPrototypeHas(nodeGlobalsKeySet, k), - ), - ...nodeGlobalsKeys, - ]; - }, - defineProperty(target, prop, desc) { - if (ReflectHas(nodeGlobals, prop)) { - return ReflectDefineProperty(nodeGlobals, prop, desc); - } else { - return ReflectDefineProperty(target, prop, desc); - } - }, - getOwnPropertyDescriptor(target, prop) { - if (ReflectHas(nodeGlobals, prop)) { - return ReflectGetOwnPropertyDescriptor(nodeGlobals, prop); - } else { - return ReflectGetOwnPropertyDescriptor(target, prop); - } - }, -}); - -export { nodeGlobals, nodeGlobalThis }; +export const denoGlobals = globalThis.__bootstrap.ext_node_denoGlobals; +export const nodeGlobals = globalThis.__bootstrap.ext_node_nodeGlobals; 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) { diff --git a/ext/node/polyfills/02_init.js b/ext/node/polyfills/02_init.js index 8a6e0195f..d73d5d822 100644 --- a/ext/node/polyfills/02_init.js +++ b/ext/node/polyfills/02_init.js @@ -4,15 +4,12 @@ const internals = globalThis.__bootstrap.internals; const requireImpl = internals.requireImpl; -const primordials = globalThis.__bootstrap.primordials; -const { ObjectDefineProperty } = primordials; -import { nodeGlobals, nodeGlobalThis } from "ext:deno_node/00_globals.js"; +import { nodeGlobals } from "ext:deno_node/00_globals.js"; import "node:module"; let initialized = false; function initialize( - nodeGlobalThisName, usesLocalNodeModulesDir, argv0, ) { @@ -29,20 +26,13 @@ function initialize( nodeGlobals.clearInterval = nativeModuleExports["timers"].clearInterval; nodeGlobals.clearTimeout = nativeModuleExports["timers"].clearTimeout; nodeGlobals.console = nativeModuleExports["console"]; - nodeGlobals.global = nodeGlobalThis; + nodeGlobals.global = globalThis; nodeGlobals.process = nativeModuleExports["process"]; nodeGlobals.setImmediate = nativeModuleExports["timers"].setImmediate; nodeGlobals.setInterval = nativeModuleExports["timers"].setInterval; nodeGlobals.setTimeout = nativeModuleExports["timers"].setTimeout; nodeGlobals.performance = nativeModuleExports["perf_hooks"].performance; - // add a hidden global for the esm code to use in order to reliably - // get node's globalThis - ObjectDefineProperty(globalThis, nodeGlobalThisName, { - enumerable: false, - configurable: true, - value: nodeGlobalThis, - }); // FIXME(bartlomieju): not nice to depend on `Deno` namespace here // but it's the only way to get `args` and `version` and this point. internals.__bootstrapNodeProcess(argv0, Deno.args, Deno.version); |