UNPKG

4.41 kBJavaScriptView Raw
1'use strict';
2
3// Load modules
4
5
6// Declare internals
7
8const internals = {};
9
10
11exports.detect = function (customGlobals) {
12
13 const whitelist = {
14 _labScriptRun: true, // Lab global to detect script executions
15
16 // Enumerable globals
17 setTimeout: true,
18 setInterval: true,
19 setImmediate: true,
20 clearTimeout: true,
21 clearInterval: true,
22 clearImmediate: true,
23 console: true,
24 Buffer: true,
25 process: true,
26 global: true,
27 GLOBAL: true,
28 root: true,
29 constructor: true,
30 ArrayBuffer: true,
31 Int8Array: true,
32 Uint8Array: true,
33 Uint8ClampedArray: true,
34 Int16Array: true,
35 Uint16Array: true,
36 Int32Array: true,
37 Uint32Array: true,
38 Float32Array: true,
39 Float64Array: true,
40 DataView: true,
41 __$$labCov: true,
42 gc: true,
43
44 // Non-Enumerable globals
45 Array: true,
46 isNaN: true,
47 ReferenceError: true,
48 Number: true,
49 RangeError: true,
50 EvalError: true,
51 Function: true,
52 isFinite: true,
53 Object: true,
54 undefined: true,
55 Date: true,
56 SyntaxError: true,
57 String: true,
58 eval: true,
59 parseFloat: true,
60 unescape: true,
61 Error: true,
62 encodeURI: true,
63 NaN: true,
64 RegExp: true,
65 encodeURIComponent: true,
66 Math: true,
67 decodeURI: true,
68 parseInt: true,
69 Infinity: true,
70 escape: true,
71 decodeURIComponent: true,
72 JSON: true,
73 TypeError: true,
74 URIError: true,
75 Boolean: true,
76 Intl: true,
77 Map: true,
78 Promise: true,
79 Set: true,
80 Symbol: true,
81 WeakMap: true,
82 WeakSet: true
83 };
84
85 if (customGlobals) {
86 for (let i = 0; i < customGlobals.length; ++i) {
87 whitelist[customGlobals[i]] = true;
88 }
89 }
90
91 // $lab:coverage:off$
92 if (global.Atomics) {
93 whitelist.Atomics = true;
94 }
95 // $lab:coverage:on$
96
97 if (global.Proxy) {
98 whitelist.Proxy = true;
99 }
100
101 if (global.Reflect) {
102 whitelist.Reflect = true;
103 }
104
105 // $lab:coverage:off$
106 if (global.SharedArrayBuffer) {
107 whitelist.SharedArrayBuffer = true;
108 }
109 // $lab:coverage:on$
110
111 // $lab:coverage:off$
112 if (global.WebAssembly) {
113 // $lab:coverage:on$
114 whitelist.WebAssembly = true;
115 }
116
117 // $lab:coverage:off$
118 if (global.URL) {
119 whitelist.URL = true;
120 whitelist.URLSearchParams = true;
121 }
122 // $lab:coverage:on$
123
124 // $lab:coverage:off$
125 if (global.TextEncoder) {
126 whitelist.TextEncoder = true;
127 }
128 // $lab:coverage:on$
129
130 // $lab:coverage:off$
131 if (global.TextDecoder) {
132 whitelist.TextDecoder = true;
133 }
134 // $lab:coverage:on$
135
136 // $lab:coverage:off$
137 if (global.BigInt) {
138 whitelist.BigInt = true;
139 whitelist.BigUint64Array = true;
140 whitelist.BigInt64Array = true;
141 }
142 // $lab:coverage:on$
143
144 if (global.DTRACE_HTTP_SERVER_RESPONSE) {
145 whitelist.DTRACE_HTTP_SERVER_RESPONSE = true;
146 whitelist.DTRACE_HTTP_SERVER_REQUEST = true;
147 whitelist.DTRACE_HTTP_CLIENT_RESPONSE = true;
148 whitelist.DTRACE_HTTP_CLIENT_REQUEST = true;
149 whitelist.DTRACE_NET_STREAM_END = true;
150 whitelist.DTRACE_NET_SERVER_CONNECTION = true;
151 whitelist.DTRACE_NET_SOCKET_READ = true;
152 whitelist.DTRACE_NET_SOCKET_WRITE = true;
153 }
154
155 if (global.COUNTER_NET_SERVER_CONNECTION) {
156 whitelist.COUNTER_NET_SERVER_CONNECTION = true;
157 whitelist.COUNTER_NET_SERVER_CONNECTION_CLOSE = true;
158 whitelist.COUNTER_HTTP_SERVER_REQUEST = true;
159 whitelist.COUNTER_HTTP_SERVER_RESPONSE = true;
160 whitelist.COUNTER_HTTP_CLIENT_REQUEST = true;
161 whitelist.COUNTER_HTTP_CLIENT_RESPONSE = true;
162 }
163
164 // $lab:coverage:off$
165 if ('queueMicrotask' in global) {
166 whitelist.queueMicrotask = true;
167 }
168 // $lab:coverage:on$
169
170 const leaks = [];
171 const globals = Object.getOwnPropertyNames(global);
172 for (let i = 0; i < globals.length; ++i) {
173 if (!whitelist[globals[i]] &&
174 global[globals[i]] !== global) {
175
176 leaks.push(globals[i]);
177 }
178 }
179
180 return leaks;
181};