blob: d9ff9dda743f988c93200a4db061b77001518b19 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
import jsdom from "jsdom";
const { JSDOM } = jsdom;
const windows = Object.create( null );
export async function runJSDOM( url, { reportId, verbose } ) {
const virtualConsole = new jsdom.VirtualConsole();
virtualConsole.sendTo( console );
virtualConsole.removeAllListeners( "clear" );
const { window } = await JSDOM.fromURL( url, {
resources: "usable",
runScripts: "dangerously",
virtualConsole
} );
if ( verbose ) {
console.log( "JSDOM window opened.", reportId );
}
windows[ reportId ] = window;
return new Promise( ( resolve ) => {
window.finish = resolve;
} );
}
export function cleanupJSDOM( reportId, { verbose } ) {
const window = windows[ reportId ];
if ( window ) {
if ( window.finish ) {
window.finish();
}
window.close();
delete windows[ reportId ];
if ( verbose ) {
console.log( "Closed JSDOM window.", reportId );
}
}
}
export function cleanupAllJSDOM( { verbose } ) {
const windowsRemaining = Object.keys( windows ).length;
if ( windowsRemaining ) {
if ( verbose ) {
console.log(
`Cleaning up ${ windowsRemaining } JSDOM window${
windowsRemaining > 1 ? "s" : ""
}...`
);
}
for ( const id in windows ) {
cleanupJSDOM( id, { verbose } );
}
}
}
|