diff --git a/ext/node/01_node.js b/ext/node/01_node.js index d33a7c43c26213..79d3503caff5da 100644 --- a/ext/node/01_node.js +++ b/ext/node/01_node.js @@ -11,6 +11,12 @@ ObjectEntries, ObjectCreate, ObjectDefineProperty, + Proxy, + ReflectDefineProperty, + ReflectGetOwnPropertyDescriptor, + ReflectOwnKeys, + Set, + SetPrototypeHas, } = window.__bootstrap.primordials; function assert(cond) { @@ -50,29 +56,29 @@ return success; }, ownKeys(_target) { - const globalThisKeys = Reflect.ownKeys(globalThis); - const nodeGlobalsKeys = Reflect.ownKeys(nodeGlobals); + const globalThisKeys = ReflectOwnKeys(globalThis); + const nodeGlobalsKeys = ReflectOwnKeys(nodeGlobals); const nodeGlobalsKeySet = new Set(nodeGlobalsKeys); return [ ...ArrayPrototypeFilter( globalThisKeys, - (k) => !nodeGlobalsKeySet.has(k), + (k) => !SetPrototypeHas(nodeGlobalsKeySet, k), ), ...nodeGlobalsKeys, ]; }, defineProperty(_target, prop, desc) { if (prop in nodeGlobals) { - return Reflect.defineProperty(nodeGlobals, prop, desc); + return ReflectDefineProperty(nodeGlobals, prop, desc); } else { - return Reflect.defineProperty(globalThis, prop, desc); + return ReflectDefineProperty(globalThis, prop, desc); } }, getOwnPropertyDescriptor(_target, prop) { if (prop in nodeGlobals) { - return Reflect.getOwnPropertyDescriptor(nodeGlobals, prop); + return ReflectGetOwnPropertyDescriptor(nodeGlobals, prop); } else { - return Reflect.getOwnPropertyDescriptor(globalThis, prop); + return ReflectGetOwnPropertyDescriptor(globalThis, prop); } }, has(_target, prop) { diff --git a/ext/node/02_require.js b/ext/node/02_require.js index 578d8e873927d3..0a3248eb8e54bd 100644 --- a/ext/node/02_require.js +++ b/ext/node/02_require.js @@ -20,16 +20,23 @@ ObjectKeys, ObjectPrototype, ObjectCreate, + Proxy, SafeMap, SafeWeakMap, + SafeArrayIterator, JSONParse, + String, StringPrototypeEndsWith, StringPrototypeIndexOf, + StringPrototypeIncludes, StringPrototypeMatch, StringPrototypeSlice, + StringPrototypeSplit, StringPrototypeStartsWith, StringPrototypeCharCodeAt, RegExpPrototypeTest, + Error, + TypeError, } = window.__bootstrap.primordials; const core = window.Deno.core; const ops = core.ops; @@ -254,9 +261,9 @@ Module.builtinModules = node.builtinModules; - Module._extensions = Object.create(null); - Module._cache = Object.create(null); - Module._pathCache = Object.create(null); + Module._extensions = ObjectCreate(null); + Module._cache = ObjectCreate(null); + Module._pathCache = ObjectCreate(null); let modulePaths = []; Module.globalPaths = modulePaths; @@ -402,7 +409,7 @@ parent.filename, ); if (denoDirPath) { - paths.push(denoDirPath); + ArrayPrototypePush(paths, denoDirPath); } } const lookupPathsResult = ops.op_require_resolve_lookup_paths( @@ -411,7 +418,7 @@ parent?.filename ?? "", ); if (lookupPathsResult) { - paths.push(...lookupPathsResult); + ArrayPrototypePush(paths, ...new SafeArrayIterator(lookupPathsResult)); } return paths; }; @@ -668,10 +675,11 @@ function enrichCJSError(error) { if (error instanceof SyntaxError) { if ( - error.message.includes( + StringPrototypeIncludes( + error.message, "Cannot use import statement outside a module", ) || - error.message.includes("Unexpected token 'export'") + StringPrototypeIncludes(error.message, "Unexpected token 'export'") ) { console.error( 'To load an ES module, set "type": "module" in the package.json or use ' + @@ -745,8 +753,8 @@ }; function stripBOM(content) { - if (content.charCodeAt(0) === 0xfeff) { - content = content.slice(1); + if (StringPrototypeCharCodeAt(content, 0) === 0xfeff) { + content = StringPrototypeSlice(content, 1); } return content; } @@ -865,13 +873,13 @@ /** @param specifier {string} */ function packageSpecifierSubPath(specifier) { - let parts = specifier.split("/"); - if (parts[0].startsWith("@")) { - parts = parts.slice(2); + let parts = StringPrototypeSplit(specifier, "/"); + if (StringPrototypeStartsWith(parts[0], "@")) { + parts = ArrayPrototypeSlice(parts, 2); } else { - parts = parts.slice(1); + parts = ArrayPrototypeSlice(parts, 1); } - return parts.join("/"); + return ArrayPrototypeJoin(parts, "/"); } window.__bootstrap.internals = {