chunk-integrations-coverage.99c020eb.mjs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import { importModule } from 'local-pkg';
  2. /*
  3. How it works:
  4. `this.#head` is an instance of `Node` which keeps track of its current value and nests another instance of `Node` that keeps the value that comes after it. When a value is provided to `.enqueue()`, the code needs to iterate through `this.#head`, going deeper and deeper to find the last value. However, iterating through every single item is slow. This problem is solved by saving a reference to the last value as `this.#tail` so that it can reference it to add a new value.
  5. */
  6. class Node {
  7. value;
  8. next;
  9. constructor(value) {
  10. this.value = value;
  11. }
  12. }
  13. class Queue {
  14. #head;
  15. #tail;
  16. #size;
  17. constructor() {
  18. this.clear();
  19. }
  20. enqueue(value) {
  21. const node = new Node(value);
  22. if (this.#head) {
  23. this.#tail.next = node;
  24. this.#tail = node;
  25. } else {
  26. this.#head = node;
  27. this.#tail = node;
  28. }
  29. this.#size++;
  30. }
  31. dequeue() {
  32. const current = this.#head;
  33. if (!current) {
  34. return;
  35. }
  36. this.#head = this.#head.next;
  37. this.#size--;
  38. return current.value;
  39. }
  40. clear() {
  41. this.#head = undefined;
  42. this.#tail = undefined;
  43. this.#size = 0;
  44. }
  45. get size() {
  46. return this.#size;
  47. }
  48. * [Symbol.iterator]() {
  49. let current = this.#head;
  50. while (current) {
  51. yield current.value;
  52. current = current.next;
  53. }
  54. }
  55. }
  56. function pLimit(concurrency) {
  57. if (!((Number.isInteger(concurrency) || concurrency === Number.POSITIVE_INFINITY) && concurrency > 0)) {
  58. throw new TypeError('Expected `concurrency` to be a number from 1 and up');
  59. }
  60. const queue = new Queue();
  61. let activeCount = 0;
  62. const next = () => {
  63. activeCount--;
  64. if (queue.size > 0) {
  65. queue.dequeue()();
  66. }
  67. };
  68. const run = async (fn, resolve, args) => {
  69. activeCount++;
  70. const result = (async () => fn(...args))();
  71. resolve(result);
  72. try {
  73. await result;
  74. } catch {}
  75. next();
  76. };
  77. const enqueue = (fn, resolve, args) => {
  78. queue.enqueue(run.bind(undefined, fn, resolve, args));
  79. (async () => {
  80. // This function needs to wait until the next microtask before comparing
  81. // `activeCount` to `concurrency`, because `activeCount` is updated asynchronously
  82. // when the run function is dequeued and called. The comparison in the if-statement
  83. // needs to happen asynchronously as well to get an up-to-date value for `activeCount`.
  84. await Promise.resolve();
  85. if (activeCount < concurrency && queue.size > 0) {
  86. queue.dequeue()();
  87. }
  88. })();
  89. };
  90. const generator = (fn, ...args) => new Promise(resolve => {
  91. enqueue(fn, resolve, args);
  92. });
  93. Object.defineProperties(generator, {
  94. activeCount: {
  95. get: () => activeCount,
  96. },
  97. pendingCount: {
  98. get: () => queue.size,
  99. },
  100. clearQueue: {
  101. value: () => {
  102. queue.clear();
  103. },
  104. },
  105. });
  106. return generator;
  107. }
  108. const CoverageProviderMap = {
  109. c8: "@vitest/coverage-c8",
  110. istanbul: "@vitest/coverage-istanbul"
  111. };
  112. async function resolveCoverageProvider(provider) {
  113. if (typeof provider === "string") {
  114. const pkg = CoverageProviderMap[provider];
  115. if (!pkg)
  116. throw new Error(`Unknown coverage provider: ${provider}`);
  117. return await importModule(pkg);
  118. } else {
  119. return provider;
  120. }
  121. }
  122. async function getCoverageProvider(options) {
  123. if ((options == null ? void 0 : options.enabled) && (options == null ? void 0 : options.provider)) {
  124. const { getProvider } = await resolveCoverageProvider(options.provider);
  125. return await getProvider();
  126. }
  127. return null;
  128. }
  129. async function takeCoverageInsideWorker(options) {
  130. if (options.enabled && options.provider) {
  131. const { takeCoverage } = await resolveCoverageProvider(options.provider);
  132. return await (takeCoverage == null ? void 0 : takeCoverage());
  133. }
  134. }
  135. export { CoverageProviderMap as C, getCoverageProvider as g, pLimit as p, takeCoverageInsideWorker as t };