index.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. function _util() {
  7. const data = require('util');
  8. _util = function () {
  9. return data;
  10. };
  11. return data;
  12. }
  13. function _v() {
  14. const data = require('v8');
  15. _v = function () {
  16. return data;
  17. };
  18. return data;
  19. }
  20. function _vm() {
  21. const data = require('vm');
  22. _vm = function () {
  23. return data;
  24. };
  25. return data;
  26. }
  27. function _jestGetType() {
  28. const data = require('jest-get-type');
  29. _jestGetType = function () {
  30. return data;
  31. };
  32. return data;
  33. }
  34. function _prettyFormat() {
  35. const data = _interopRequireDefault(require('pretty-format'));
  36. _prettyFormat = function () {
  37. return data;
  38. };
  39. return data;
  40. }
  41. function _interopRequireDefault(obj) {
  42. return obj && obj.__esModule ? obj : {default: obj};
  43. }
  44. function _defineProperty(obj, key, value) {
  45. if (key in obj) {
  46. Object.defineProperty(obj, key, {
  47. value: value,
  48. enumerable: true,
  49. configurable: true,
  50. writable: true
  51. });
  52. } else {
  53. obj[key] = value;
  54. }
  55. return obj;
  56. }
  57. const tick = (0, _util().promisify)(setImmediate);
  58. class _default {
  59. constructor(value) {
  60. _defineProperty(this, '_isReferenceBeingHeld', void 0);
  61. if ((0, _jestGetType().isPrimitive)(value)) {
  62. throw new TypeError(
  63. [
  64. 'Primitives cannot leak memory.',
  65. 'You passed a ' +
  66. typeof value +
  67. ': <' +
  68. (0, _prettyFormat().default)(value) +
  69. '>'
  70. ].join(' ')
  71. );
  72. }
  73. let weak;
  74. try {
  75. // eslint-disable-next-line import/no-extraneous-dependencies
  76. weak = require('weak-napi');
  77. } catch (err) {
  78. if (!err || err.code !== 'MODULE_NOT_FOUND') {
  79. throw err;
  80. }
  81. throw new Error(
  82. 'The leaking detection mechanism requires the "weak-napi" package to be installed and work. ' +
  83. 'Please install it as a dependency on your main project'
  84. );
  85. }
  86. weak(value, () => (this._isReferenceBeingHeld = false));
  87. this._isReferenceBeingHeld = true; // Ensure value is not leaked by the closure created by the "weak" callback.
  88. value = null;
  89. }
  90. async isLeaking() {
  91. this._runGarbageCollector(); // wait some ticks to allow GC to run properly, see https://github.com/nodejs/node/issues/34636#issuecomment-669366235
  92. for (let i = 0; i < 10; i++) {
  93. await tick();
  94. }
  95. return this._isReferenceBeingHeld;
  96. }
  97. _runGarbageCollector() {
  98. const isGarbageCollectorHidden = !global.gc; // GC is usually hidden, so we have to expose it before running.
  99. (0, _v().setFlagsFromString)('--expose-gc');
  100. (0, _vm().runInNewContext)('gc')(); // The GC was not initially exposed, so let's hide it again.
  101. if (isGarbageCollectorHidden) {
  102. (0, _v().setFlagsFromString)('--no-expose-gc');
  103. }
  104. }
  105. }
  106. exports.default = _default;