|
| 1 | +// Flags: --experimental-modules |
| 2 | +/* eslint-disable required-modules */ |
| 3 | + |
| 4 | +import assert from 'assert'; |
| 5 | + |
| 6 | +let knownGlobals = [ |
| 7 | + Buffer, |
| 8 | + clearImmediate, |
| 9 | + clearInterval, |
| 10 | + clearTimeout, |
| 11 | + console, |
| 12 | + constructor, // Enumerable in V8 3.21. |
| 13 | + global, |
| 14 | + process, |
| 15 | + setImmediate, |
| 16 | + setInterval, |
| 17 | + setTimeout |
| 18 | +]; |
| 19 | + |
| 20 | +if (process.env.NODE_TEST_KNOWN_GLOBALS) { |
| 21 | + const knownFromEnv = process.env.NODE_TEST_KNOWN_GLOBALS.split(','); |
| 22 | + allowGlobals(...knownFromEnv); |
| 23 | +} |
| 24 | + |
| 25 | +export function allowGlobals(...whitelist) { |
| 26 | + knownGlobals = knownGlobals.concat(whitelist); |
| 27 | +} |
| 28 | + |
| 29 | +export function leakedGlobals() { |
| 30 | + //add possible expected globals |
| 31 | + if (global.gc) { |
| 32 | + knownGlobals.push(global.gc); |
| 33 | + } |
| 34 | + |
| 35 | + if (global.DTRACE_HTTP_SERVER_RESPONSE) { |
| 36 | + knownGlobals.push(DTRACE_HTTP_SERVER_RESPONSE); |
| 37 | + knownGlobals.push(DTRACE_HTTP_SERVER_REQUEST); |
| 38 | + knownGlobals.push(DTRACE_HTTP_CLIENT_RESPONSE); |
| 39 | + knownGlobals.push(DTRACE_HTTP_CLIENT_REQUEST); |
| 40 | + knownGlobals.push(DTRACE_NET_STREAM_END); |
| 41 | + knownGlobals.push(DTRACE_NET_SERVER_CONNECTION); |
| 42 | + } |
| 43 | + |
| 44 | + if (global.COUNTER_NET_SERVER_CONNECTION) { |
| 45 | + knownGlobals.push(COUNTER_NET_SERVER_CONNECTION); |
| 46 | + knownGlobals.push(COUNTER_NET_SERVER_CONNECTION_CLOSE); |
| 47 | + knownGlobals.push(COUNTER_HTTP_SERVER_REQUEST); |
| 48 | + knownGlobals.push(COUNTER_HTTP_SERVER_RESPONSE); |
| 49 | + knownGlobals.push(COUNTER_HTTP_CLIENT_REQUEST); |
| 50 | + knownGlobals.push(COUNTER_HTTP_CLIENT_RESPONSE); |
| 51 | + } |
| 52 | + |
| 53 | + if (global.LTTNG_HTTP_SERVER_RESPONSE) { |
| 54 | + knownGlobals.push(LTTNG_HTTP_SERVER_RESPONSE); |
| 55 | + knownGlobals.push(LTTNG_HTTP_SERVER_REQUEST); |
| 56 | + knownGlobals.push(LTTNG_HTTP_CLIENT_RESPONSE); |
| 57 | + knownGlobals.push(LTTNG_HTTP_CLIENT_REQUEST); |
| 58 | + knownGlobals.push(LTTNG_NET_STREAM_END); |
| 59 | + knownGlobals.push(LTTNG_NET_SERVER_CONNECTION); |
| 60 | + } |
| 61 | + |
| 62 | + if (global.ArrayBuffer) { |
| 63 | + knownGlobals.push(ArrayBuffer); |
| 64 | + knownGlobals.push(Int8Array); |
| 65 | + knownGlobals.push(Uint8Array); |
| 66 | + knownGlobals.push(Uint8ClampedArray); |
| 67 | + knownGlobals.push(Int16Array); |
| 68 | + knownGlobals.push(Uint16Array); |
| 69 | + knownGlobals.push(Int32Array); |
| 70 | + knownGlobals.push(Uint32Array); |
| 71 | + knownGlobals.push(Float32Array); |
| 72 | + knownGlobals.push(Float64Array); |
| 73 | + knownGlobals.push(DataView); |
| 74 | + } |
| 75 | + |
| 76 | + // Harmony features. |
| 77 | + if (global.Proxy) { |
| 78 | + knownGlobals.push(Proxy); |
| 79 | + } |
| 80 | + |
| 81 | + if (global.Symbol) { |
| 82 | + knownGlobals.push(Symbol); |
| 83 | + } |
| 84 | + |
| 85 | + const leaked = []; |
| 86 | + |
| 87 | + for (const val in global) { |
| 88 | + if (!knownGlobals.includes(global[val])) { |
| 89 | + leaked.push(val); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + if (global.__coverage__) { |
| 94 | + return leaked.filter((varname) => !/^(?:cov_|__cov)/.test(varname)); |
| 95 | + } else { |
| 96 | + return leaked; |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +// Turn this off if the test should not check for global leaks. |
| 101 | +export let globalCheck = true; // eslint-disable-line |
| 102 | + |
| 103 | +process.on('exit', function() { |
| 104 | + if (!globalCheck) return; |
| 105 | + const leaked = leakedGlobals(); |
| 106 | + if (leaked.length > 0) { |
| 107 | + assert.fail(`Unexpected global(s) found: ${leaked.join(', ')}`); |
| 108 | + } |
| 109 | +}); |
0 commit comments