Skip to content

Commit bf34a49

Browse files
jasnelladuh95
authored andcommitted
test: make common/index slightly less node.js specific
* s/global/globalThis * clean up knownGlobals a bit, make it a Set instead of an array and condense a bit. PR-URL: #56712 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent ef2ed71 commit bf34a49

File tree

1 file changed

+57
-83
lines changed

1 file changed

+57
-83
lines changed

test/common/index.js

+57-83
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

2222
'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.
2424

2525
const assert = require('assert');
2626
const { exec, execSync, spawn, spawnSync } = require('child_process');
@@ -266,7 +266,7 @@ function platformTimeout(ms) {
266266
return ms;
267267
}
268268

269-
let knownGlobals = [
269+
const knownGlobals = new Set([
270270
AbortController,
271271
atob,
272272
btoa,
@@ -278,88 +278,59 @@ let knownGlobals = [
278278
setInterval,
279279
setTimeout,
280280
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+
});
352322

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);
359328
}
360329

361330
function allowGlobals(...allowlist) {
362-
knownGlobals = knownGlobals.concat(allowlist);
331+
for (const val of allowlist) {
332+
knownGlobals.add(val);
333+
}
363334
}
364335

365336
if (process.env.NODE_TEST_KNOWN_GLOBALS !== '0') {
@@ -371,10 +342,13 @@ if (process.env.NODE_TEST_KNOWN_GLOBALS !== '0') {
371342
function leakedGlobals() {
372343
const leaked = [];
373344

374-
for (const val in global) {
345+
for (const val in globalThis) {
375346
// 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])) {
378352
leaked.push(val);
379353
}
380354
}

0 commit comments

Comments
 (0)