/
index.ts
88 lines (71 loc) · 2.64 KB
/
index.ts
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
/// <reference lib="es2021.WeakRef" />
import {promisify} from 'util';
import {setFlagsFromString} from 'v8';
import {runInNewContext} from 'vm';
import {isPrimitive} from 'jest-get-type';
import {format as prettyFormat} from 'pretty-format';
const tick = promisify(setImmediate);
export default class LeakDetector {
private _isReferenceBeingHeld: boolean;
private _finalizationRegistry?: FinalizationRegistry<undefined>;
constructor(value: unknown) {
if (isPrimitive(value)) {
throw new TypeError(
[
'Primitives cannot leak memory.',
`You passed a ${typeof value}: <${prettyFormat(value)}>`,
].join(' '),
);
}
if (globalThis.FinalizationRegistry) {
this._finalizationRegistry = new FinalizationRegistry(() => {
this._isReferenceBeingHeld = false;
});
this._finalizationRegistry.register(value as object, undefined);
} else {
let weak: typeof import('weak-napi');
try {
// eslint-disable-next-line import/no-extraneous-dependencies
weak = require('weak-napi');
} catch (err: any) {
if (!err || err.code !== 'MODULE_NOT_FOUND') {
throw err;
}
throw new Error(
'The leaking detection mechanism requires newer version of node that supports ' +
'FinalizationRegistry, update your node or install the "weak-napi" package' +
'which support current node version as a dependency on your main project.',
);
}
weak(value as object, () => (this._isReferenceBeingHeld = false));
}
this._isReferenceBeingHeld = true;
// Ensure value is not leaked by the closure created by the "weak" callback.
value = null;
}
async isLeaking(): Promise<boolean> {
this._runGarbageCollector();
// wait some ticks to allow GC to run properly, see https://github.com/nodejs/node/issues/34636#issuecomment-669366235
for (let i = 0; i < 10; i++) {
await tick();
}
return this._isReferenceBeingHeld;
}
private _runGarbageCollector() {
// @ts-expect-error: not a function on `globalThis`
const isGarbageCollectorHidden = globalThis.gc == null;
// GC is usually hidden, so we have to expose it before running.
setFlagsFromString('--expose-gc');
runInNewContext('gc')();
// The GC was not initially exposed, so let's hide it again.
if (isGarbageCollectorHidden) {
setFlagsFromString('--no-expose-gc');
}
}
}