diff --git a/lib/.eslintrc.yaml b/lib/.eslintrc.yaml index a7555a802ad95c..3911b421be1356 100644 --- a/lib/.eslintrc.yaml +++ b/lib/.eslintrc.yaml @@ -25,6 +25,10 @@ rules: message: "Please use `require('internal/errors').hideStackFrames()` instead." - selector: "AssignmentExpression:matches([left.name='prepareStackTrace'], [left.property.name='prepareStackTrace'])" message: "Use 'overrideStackTrace' from 'lib/internal/errors.js' instead of 'Error.prepareStackTrace'." + no-restricted-globals: + - error + - name: globalThis + message: "Use `const { globalThis } = primordials;` instead of the global." # Custom rules in tools/eslint-rules node-core/lowercase-name-for-primitive: error node-core/non-ascii-character: error diff --git a/lib/internal/modules/esm/loader.js b/lib/internal/modules/esm/loader.js index 14e66dcb9fdf62..8b2c89ba130aa7 100644 --- a/lib/internal/modules/esm/loader.js +++ b/lib/internal/modules/esm/loader.js @@ -9,6 +9,7 @@ const { RegExpPrototypeExec, SafeWeakMap, StringPrototypeStartsWith, + globalThis, } = primordials; const { diff --git a/lib/internal/per_context/primordials.js b/lib/internal/per_context/primordials.js index 2c5ce73007bb9d..a1e9f3ae305e32 100644 --- a/lib/internal/per_context/primordials.js +++ b/lib/internal/per_context/primordials.js @@ -131,6 +131,14 @@ function copyPrototype(src, dest, prefix) { } } +// Create copies of configurable value properties of the global object +[ + 'globalThis', +].forEach((name) => { + // eslint-disable-next-line no-restricted-globals + primordials[name] = globalThis[name]; +}); + // Create copies of URI handling functions [ decodeURI, diff --git a/lib/internal/process/execution.js b/lib/internal/process/execution.js index 314b8b0a03c161..e370770643ca6f 100644 --- a/lib/internal/process/execution.js +++ b/lib/internal/process/execution.js @@ -1,5 +1,9 @@ 'use strict'; +const { + globalThis, +} = primordials; + const path = require('path'); const { diff --git a/lib/internal/test/binding.js b/lib/internal/test/binding.js index 063b9b5c900aad..d00ef89a16111b 100644 --- a/lib/internal/test/binding.js +++ b/lib/internal/test/binding.js @@ -1,5 +1,9 @@ 'use strict'; +const { + globalThis, +} = primordials; + process.emitWarning( 'These APIs are for internal testing only. Do not use them.', 'internal/test/binding'); diff --git a/lib/internal/v8_prof_polyfill.js b/lib/internal/v8_prof_polyfill.js index c4140bab54168c..2b6b90c58d1147 100644 --- a/lib/internal/v8_prof_polyfill.js +++ b/lib/internal/v8_prof_polyfill.js @@ -72,6 +72,7 @@ function read(fileName) { } const quit = process.exit; // Polyfill "readline()". +// eslint-disable-next-line no-restricted-globals const logFile = globalThis.arguments[globalThis.arguments.length - 1]; try { fs.accessSync(logFile); @@ -161,6 +162,7 @@ function macCppfiltNm(out) { }); } +// eslint-disable-next-line no-restricted-globals Object.assign(globalThis, { os, print,