20
20
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21
21
22
22
'use strict' ;
23
- const process = global . process ; // Some tests tamper with the process global .
23
+ const process = globalThis . process ; // Some tests tamper with the process globalThis .
24
24
25
25
const assert = require ( 'assert' ) ;
26
26
const { exec, execSync, spawn, spawnSync } = require ( 'child_process' ) ;
@@ -266,7 +266,7 @@ function platformTimeout(ms) {
266
266
return ms ;
267
267
}
268
268
269
- let knownGlobals = [
269
+ const knownGlobals = new Set ( [
270
270
AbortController ,
271
271
atob ,
272
272
btoa ,
@@ -278,88 +278,59 @@ let knownGlobals = [
278
278
setInterval ,
279
279
setTimeout ,
280
280
queueMicrotask ,
281
- ] ;
282
-
283
- if ( global . gc ) {
284
- knownGlobals . push ( global . gc ) ;
285
- }
286
-
287
- if ( global . navigator ) {
288
- knownGlobals . push ( global . navigator ) ;
289
- }
290
-
291
- if ( global . Navigator ) {
292
- knownGlobals . push ( global . Navigator ) ;
293
- }
294
-
295
- if ( global . Performance ) {
296
- knownGlobals . push ( global . Performance ) ;
297
- }
298
- if ( global . performance ) {
299
- knownGlobals . push ( global . performance ) ;
300
- }
301
- if ( global . PerformanceMark ) {
302
- knownGlobals . push ( global . PerformanceMark ) ;
303
- }
304
- if ( global . PerformanceMeasure ) {
305
- knownGlobals . push ( global . PerformanceMeasure ) ;
306
- }
307
-
308
- // TODO(@ethan-arrowood): Similar to previous checks, this can be temporary
309
- // until v16.x is EOL. Once all supported versions have structuredClone we
310
- // can add this to the list above instead.
311
- if ( global . structuredClone ) {
312
- knownGlobals . push ( global . structuredClone ) ;
313
- }
314
-
315
- if ( global . EventSource ) {
316
- knownGlobals . push ( EventSource ) ;
317
- }
318
-
319
- if ( global . fetch ) {
320
- knownGlobals . push ( fetch ) ;
321
- }
322
- if ( hasCrypto && global . crypto ) {
323
- knownGlobals . push ( global . crypto ) ;
324
- knownGlobals . push ( global . Crypto ) ;
325
- knownGlobals . push ( global . CryptoKey ) ;
326
- knownGlobals . push ( global . SubtleCrypto ) ;
327
- }
328
- if ( global . CustomEvent ) {
329
- knownGlobals . push ( global . CustomEvent ) ;
330
- }
331
- if ( global . ReadableStream ) {
332
- knownGlobals . push (
333
- global . ReadableStream ,
334
- global . ReadableStreamDefaultReader ,
335
- global . ReadableStreamBYOBReader ,
336
- global . ReadableStreamBYOBRequest ,
337
- global . ReadableByteStreamController ,
338
- global . ReadableStreamDefaultController ,
339
- global . TransformStream ,
340
- global . TransformStreamDefaultController ,
341
- global . WritableStream ,
342
- global . WritableStreamDefaultWriter ,
343
- global . WritableStreamDefaultController ,
344
- global . ByteLengthQueuingStrategy ,
345
- global . CountQueuingStrategy ,
346
- global . TextEncoderStream ,
347
- global . TextDecoderStream ,
348
- global . CompressionStream ,
349
- global . DecompressionStream ,
350
- ) ;
351
- }
281
+ structuredClone ,
282
+ fetch ,
283
+ ] ) ;
284
+
285
+ [ 'gc' ,
286
+ // The following are assumed to be conditionally available in the
287
+ // global object currently. They can likely be added to the fixed
288
+ // set of known globals, however.
289
+ 'navigator' ,
290
+ 'Navigator' ,
291
+ 'performance' ,
292
+ 'Performance' ,
293
+ 'PerformanceMark' ,
294
+ 'PerformanceMeasure' ,
295
+ 'EventSource' ,
296
+ 'CustomEvent' ,
297
+ 'ReadableStream' ,
298
+ 'ReadableStreamDefaultReader' ,
299
+ 'ReadableStreamBYOBReader' ,
300
+ 'ReadableStreamBYOBRequest' ,
301
+ 'ReadableByteStreamController' ,
302
+ 'ReadableStreamDefaultController' ,
303
+ 'TransformStream' ,
304
+ 'TransformStreamDefaultController' ,
305
+ 'WritableStream' ,
306
+ 'WritableStreamDefaultWriter' ,
307
+ 'WritableStreamDefaultController' ,
308
+ 'ByteLengthQueuingStrategy' ,
309
+ 'CountQueuingStrategy' ,
310
+ 'TextEncoderStream' ,
311
+ 'TextDecoderStream' ,
312
+ 'CompressionStream' ,
313
+ 'DecompressionStream' ,
314
+ 'Storage' ,
315
+ 'localStorage' ,
316
+ 'sessionStorage' ,
317
+ ] . forEach ( ( i ) => {
318
+ if ( globalThis [ i ] !== undefined ) {
319
+ knownGlobals . add ( globalThis [ i ] ) ;
320
+ }
321
+ } ) ;
352
322
353
- if ( global . Storage ) {
354
- knownGlobals . push (
355
- global . localStorage ,
356
- global . sessionStorage ,
357
- global . Storage ,
358
- ) ;
323
+ if ( hasCrypto ) {
324
+ knownGlobals . add ( globalThis . crypto ) ;
325
+ knownGlobals . add ( globalThis . Crypto ) ;
326
+ knownGlobals . add ( globalThis . CryptoKey ) ;
327
+ knownGlobals . add ( globalThis . SubtleCrypto ) ;
359
328
}
360
329
361
330
function allowGlobals ( ...allowlist ) {
362
- knownGlobals = knownGlobals . concat ( allowlist ) ;
331
+ for ( const val of allowlist ) {
332
+ knownGlobals . add ( val ) ;
333
+ }
363
334
}
364
335
365
336
if ( process . env . NODE_TEST_KNOWN_GLOBALS !== '0' ) {
@@ -371,10 +342,13 @@ if (process.env.NODE_TEST_KNOWN_GLOBALS !== '0') {
371
342
function leakedGlobals ( ) {
372
343
const leaked = [ ] ;
373
344
374
- for ( const val in global ) {
345
+ for ( const val in globalThis ) {
375
346
// globalThis.crypto is a getter that throws if Node.js was compiled
376
- // without OpenSSL.
377
- if ( val !== 'crypto' && ! knownGlobals . includes ( global [ val ] ) ) {
347
+ // without OpenSSL so we'll skip it if it is not available.
348
+ if ( val === 'crypto' && ! hasCrypto ) {
349
+ continue ;
350
+ }
351
+ if ( ! knownGlobals . has ( globalThis [ val ] ) ) {
378
352
leaked . push ( val ) ;
379
353
}
380
354
}
0 commit comments