lighthouse 12.1.0-dev.20240630 → 12.1.0-dev.20240702

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (115) hide show
  1. package/core/audits/byte-efficiency/byte-efficiency-audit.d.ts +5 -7
  2. package/core/audits/byte-efficiency/byte-efficiency-audit.js +3 -6
  3. package/core/audits/byte-efficiency/render-blocking-resources.d.ts +3 -7
  4. package/core/audits/byte-efficiency/render-blocking-resources.js +10 -16
  5. package/core/audits/dobetterweb/uses-http2.d.ts +4 -6
  6. package/core/audits/dobetterweb/uses-http2.js +2 -5
  7. package/core/audits/long-tasks.d.ts +6 -6
  8. package/core/audits/long-tasks.js +4 -4
  9. package/core/audits/prioritize-lcp-image.d.ts +1 -1
  10. package/core/audits/uses-rel-preload.d.ts +4 -3
  11. package/core/audits/uses-rel-preload.js +8 -6
  12. package/core/computed/critical-request-chains.d.ts +4 -3
  13. package/core/computed/critical-request-chains.js +5 -4
  14. package/core/computed/document-urls.js +1 -1
  15. package/core/computed/load-simulator.d.ts +3 -4
  16. package/core/computed/load-simulator.js +1 -1
  17. package/core/computed/main-resource.js +1 -1
  18. package/core/computed/metrics/lantern-first-contentful-paint.d.ts +2 -4
  19. package/core/computed/metrics/lantern-first-contentful-paint.js +1 -3
  20. package/core/computed/metrics/lantern-interactive.d.ts +2 -4
  21. package/core/computed/metrics/lantern-interactive.js +1 -3
  22. package/core/computed/metrics/lantern-largest-contentful-paint.d.ts +2 -4
  23. package/core/computed/metrics/lantern-largest-contentful-paint.js +1 -3
  24. package/core/computed/metrics/lantern-max-potential-fid.d.ts +2 -4
  25. package/core/computed/metrics/lantern-max-potential-fid.js +1 -3
  26. package/core/computed/metrics/lantern-metric.d.ts +8 -8
  27. package/core/computed/metrics/lantern-metric.js +1 -1
  28. package/core/computed/metrics/lantern-speed-index.d.ts +2 -4
  29. package/core/computed/metrics/lantern-speed-index.js +1 -3
  30. package/core/computed/metrics/lantern-total-blocking-time.d.ts +2 -4
  31. package/core/computed/metrics/lantern-total-blocking-time.js +1 -3
  32. package/core/computed/metrics/total-blocking-time.js +1 -1
  33. package/core/computed/network-analysis.js +1 -1
  34. package/core/computed/page-dependency-graph.d.ts +3 -6
  35. package/core/computed/page-dependency-graph.js +3 -4
  36. package/core/computed/tbt-impact-tasks.js +2 -2
  37. package/core/lib/asset-saver.js +1 -1
  38. package/core/lib/lantern/lantern.d.ts +2 -19
  39. package/core/lib/lantern/lantern.js +2 -45
  40. package/core/lib/lantern-trace-saver.d.ts +4 -6
  41. package/core/lib/lantern-trace-saver.js +2 -3
  42. package/core/lib/navigation-error.js +2 -2
  43. package/core/lib/network-recorder.js +1 -1
  44. package/core/lib/network-request.d.ts +4 -4
  45. package/core/lib/network-request.js +2 -2
  46. package/package.json +2 -2
  47. package/tsconfig.json +0 -11
  48. package/types/artifacts.d.ts +2 -2
  49. package/types/gatherer.d.ts +4 -5
  50. package/core/lib/lantern/BaseNode.d.ts +0 -163
  51. package/core/lib/lantern/BaseNode.js +0 -369
  52. package/core/lib/lantern/BaseNode.test.js +0 -385
  53. package/core/lib/lantern/CpuNode.d.ts +0 -43
  54. package/core/lib/lantern/CpuNode.js +0 -99
  55. package/core/lib/lantern/LanternError.d.ts +0 -8
  56. package/core/lib/lantern/LanternError.js +0 -9
  57. package/core/lib/lantern/Metric.d.ts +0 -69
  58. package/core/lib/lantern/Metric.js +0 -137
  59. package/core/lib/lantern/NetworkNode.d.ts +0 -49
  60. package/core/lib/lantern/NetworkNode.js +0 -136
  61. package/core/lib/lantern/PageDependencyGraph.d.ts +0 -82
  62. package/core/lib/lantern/PageDependencyGraph.js +0 -610
  63. package/core/lib/lantern/PageDependencyGraph.test.js +0 -654
  64. package/core/lib/lantern/TBTUtils.d.ts +0 -44
  65. package/core/lib/lantern/TBTUtils.js +0 -76
  66. package/core/lib/lantern/TBTUtils.test.d.ts +0 -2
  67. package/core/lib/lantern/TBTUtils.test.js +0 -130
  68. package/core/lib/lantern/TraceEngineComputationData.d.ts +0 -25
  69. package/core/lib/lantern/TraceEngineComputationData.js +0 -466
  70. package/core/lib/lantern/metrics/FirstContentfulPaint.d.ts +0 -79
  71. package/core/lib/lantern/metrics/FirstContentfulPaint.js +0 -198
  72. package/core/lib/lantern/metrics/FirstContentfulPaint.test.js +0 -54
  73. package/core/lib/lantern/metrics/Interactive.d.ts +0 -20
  74. package/core/lib/lantern/metrics/Interactive.js +0 -109
  75. package/core/lib/lantern/metrics/Interactive.test.js +0 -56
  76. package/core/lib/lantern/metrics/LargestContentfulPaint.d.ts +0 -19
  77. package/core/lib/lantern/metrics/LargestContentfulPaint.js +0 -105
  78. package/core/lib/lantern/metrics/LargestContentfulPaint.test.js +0 -42
  79. package/core/lib/lantern/metrics/MaxPotentialFID.d.ts +0 -24
  80. package/core/lib/lantern/metrics/MaxPotentialFID.js +0 -91
  81. package/core/lib/lantern/metrics/MetricTestUtils.d.ts +0 -19
  82. package/core/lib/lantern/metrics/MetricTestUtils.js +0 -48
  83. package/core/lib/lantern/metrics/SpeedIndex.d.ts +0 -32
  84. package/core/lib/lantern/metrics/SpeedIndex.js +0 -142
  85. package/core/lib/lantern/metrics/SpeedIndex.test.js +0 -83
  86. package/core/lib/lantern/metrics/TotalBlockingTime.d.ts +0 -25
  87. package/core/lib/lantern/metrics/TotalBlockingTime.js +0 -125
  88. package/core/lib/lantern/metrics/__snapshots__/FirstContentfulPaint.test.js.snap +0 -11
  89. package/core/lib/lantern/metrics/__snapshots__/Interactive.test.js.snap +0 -17
  90. package/core/lib/lantern/metrics/metrics.d.ts +0 -9
  91. package/core/lib/lantern/metrics/metrics.js +0 -16
  92. package/core/lib/lantern/simulation/ConnectionPool.d.ts +0 -48
  93. package/core/lib/lantern/simulation/ConnectionPool.js +0 -152
  94. package/core/lib/lantern/simulation/ConnectionPool.test.js +0 -195
  95. package/core/lib/lantern/simulation/Constants.d.ts +0 -52
  96. package/core/lib/lantern/simulation/Constants.js +0 -48
  97. package/core/lib/lantern/simulation/DNSCache.d.ts +0 -42
  98. package/core/lib/lantern/simulation/DNSCache.js +0 -74
  99. package/core/lib/lantern/simulation/DNSCache.test.js +0 -72
  100. package/core/lib/lantern/simulation/NetworkAnalyzer.d.ts +0 -209
  101. package/core/lib/lantern/simulation/NetworkAnalyzer.js +0 -597
  102. package/core/lib/lantern/simulation/NetworkAnalyzer.test.js +0 -475
  103. package/core/lib/lantern/simulation/SimulationTimingMap.d.ts +0 -153
  104. package/core/lib/lantern/simulation/SimulationTimingMap.js +0 -219
  105. package/core/lib/lantern/simulation/Simulator.d.ts +0 -157
  106. package/core/lib/lantern/simulation/Simulator.js +0 -593
  107. package/core/lib/lantern/simulation/Simulator.test.js +0 -434
  108. package/core/lib/lantern/simulation/TCPConnection.test.d.ts +0 -2
  109. package/core/lib/lantern/simulation/TCPConnection.test.js +0 -374
  110. package/core/lib/lantern/simulation/TcpConnection.d.ts +0 -89
  111. package/core/lib/lantern/simulation/TcpConnection.js +0 -230
  112. package/core/lib/lantern/simulation/simulation.d.ts +0 -21
  113. package/core/lib/lantern/simulation/simulation.js +0 -28
  114. package/core/lib/lantern/types/lantern.d.ts +0 -256
  115. package/core/lib/lantern/types/lantern.js +0 -7
@@ -1,163 +0,0 @@
1
- /**
2
- * A union of all types derived from BaseNode, allowing type check discrimination
3
- * based on `node.type`. If a new node type is created, it should be added here.
4
- */
5
- export type Node<T = any> = import('./CpuNode.js').CPUNode<T> | import('./NetworkNode.js').NetworkNode<T>;
6
- /**
7
- * @license
8
- * Copyright 2017 Google LLC
9
- * SPDX-License-Identifier: Apache-2.0
10
- */
11
- /**
12
- * A union of all types derived from BaseNode, allowing type check discrimination
13
- * based on `node.type`. If a new node type is created, it should be added here.
14
- * @template [T=any]
15
- * @typedef {import('./CpuNode.js').CPUNode<T> | import('./NetworkNode.js').NetworkNode<T>} Node
16
- */
17
- /**
18
- * @fileoverview This class encapsulates logic for handling resources and tasks used to model the
19
- * execution dependency graph of the page. A node has a unique identifier and can depend on other
20
- * nodes/be depended on. The construction of the graph maintains some important invariants that are
21
- * inherent to the model:
22
- *
23
- * 1. The graph is a DAG, there are no cycles.
24
- * 2. There is always a root node upon which all other nodes eventually depend.
25
- *
26
- * This allows particular optimizations in this class so that we do no need to check for cycles as
27
- * these methods are called and we can always start traversal at the root node.
28
- */
29
- /**
30
- * @template [T=any]
31
- */
32
- export class BaseNode<T = any> {
33
- static TYPES: {
34
- NETWORK: 'network';
35
- CPU: 'cpu';
36
- };
37
- /**
38
- * Returns whether the given node has a cycle in its dependent graph by performing a DFS.
39
- * @param {Node} node
40
- * @param {'dependents'|'dependencies'|'both'} [direction]
41
- * @return {boolean}
42
- */
43
- static hasCycle(node: Node, direction?: "dependents" | "dependencies" | "both" | undefined): boolean;
44
- /**
45
- * @param {string} id
46
- */
47
- constructor(id: string);
48
- _id: string;
49
- _isMainDocument: boolean;
50
- /** @type {Node[]} */
51
- _dependents: Node[];
52
- /** @type {Node[]} */
53
- _dependencies: Node[];
54
- /**
55
- * @return {string}
56
- */
57
- get id(): string;
58
- /**
59
- * @return {typeof BaseNode.TYPES[keyof typeof BaseNode.TYPES]}
60
- */
61
- get type(): "network" | "cpu";
62
- /**
63
- * In microseconds
64
- * @return {number}
65
- */
66
- get startTime(): number;
67
- /**
68
- * In microseconds
69
- * @return {number}
70
- */
71
- get endTime(): number;
72
- /**
73
- * @param {boolean} value
74
- */
75
- setIsMainDocument(value: boolean): void;
76
- /**
77
- * @return {boolean}
78
- */
79
- isMainDocument(): boolean;
80
- /**
81
- * @return {Node[]}
82
- */
83
- getDependents(): Node[];
84
- /**
85
- * @return {number}
86
- */
87
- getNumberOfDependents(): number;
88
- /**
89
- * @return {Node[]}
90
- */
91
- getDependencies(): Node[];
92
- /**
93
- * @return {number}
94
- */
95
- getNumberOfDependencies(): number;
96
- /**
97
- * @return {Node}
98
- */
99
- getRootNode(): Node;
100
- /**
101
- * @param {Node} node
102
- */
103
- addDependent(node: Node): void;
104
- /**
105
- * @param {Node} node
106
- */
107
- addDependency(node: Node): void;
108
- /**
109
- * @param {Node} node
110
- */
111
- removeDependent(node: Node): void;
112
- /**
113
- * @param {Node} node
114
- */
115
- removeDependency(node: Node): void;
116
- removeAllDependencies(): void;
117
- /**
118
- * Computes whether the given node is anywhere in the dependency graph of this node.
119
- * While this method can prevent cycles, it walks the graph and should be used sparingly.
120
- * Nodes are always considered dependent on themselves for the purposes of cycle detection.
121
- * @param {BaseNode} node
122
- * @return {boolean}
123
- */
124
- isDependentOn(node: BaseNode): boolean;
125
- /**
126
- * Clones the node's information without adding any dependencies/dependents.
127
- * @return {Node}
128
- */
129
- cloneWithoutRelationships(): Node;
130
- /**
131
- * Clones the entire graph connected to this node filtered by the optional predicate. If a node is
132
- * included by the predicate, all nodes along the paths between the node and the root will be included. If the
133
- * node this was called on is not included in the resulting filtered graph, the method will throw.
134
- * @param {function(Node):boolean} [predicate]
135
- * @return {Node}
136
- */
137
- cloneWithRelationships(predicate?: ((arg0: Node) => boolean) | undefined): Node;
138
- /**
139
- * Traverses all connected nodes in BFS order, calling `callback` exactly once
140
- * on each. `traversalPath` is the shortest (though not necessarily unique)
141
- * path from `node` to the root of the iteration.
142
- *
143
- * The `getNextNodes` function takes a visited node and returns which nodes to
144
- * visit next. It defaults to returning the node's dependents.
145
- * @param {(node: Node<T>, traversalPath: Node<T>[]) => void} callback
146
- * @param {function(Node<T>): Node<T>[]} [getNextNodes]
147
- */
148
- traverse(callback: (node: Node<T>, traversalPath: Node<T>[]) => void, getNextNodes?: ((arg0: Node<T>) => Node<T>[]) | undefined): void;
149
- /**
150
- * @see BaseNode.traverse
151
- * @param {function(Node): Node[]} [getNextNodes]
152
- */
153
- traverseGenerator(getNextNodes?: ((arg0: Node) => Node[]) | undefined): Generator<{
154
- node: Node<any>;
155
- traversalPath: Node<any>[];
156
- }, void, unknown>;
157
- /**
158
- * @param {Node} node
159
- * @return {boolean}
160
- */
161
- canDependOn(node: Node): boolean;
162
- }
163
- //# sourceMappingURL=BaseNode.d.ts.map
@@ -1,369 +0,0 @@
1
- /**
2
- * @license
3
- * Copyright 2017 Google LLC
4
- * SPDX-License-Identifier: Apache-2.0
5
- */
6
-
7
- /**
8
- * A union of all types derived from BaseNode, allowing type check discrimination
9
- * based on `node.type`. If a new node type is created, it should be added here.
10
- * @template [T=any]
11
- * @typedef {import('./CpuNode.js').CPUNode<T> | import('./NetworkNode.js').NetworkNode<T>} Node
12
- */
13
-
14
- /**
15
- * @fileoverview This class encapsulates logic for handling resources and tasks used to model the
16
- * execution dependency graph of the page. A node has a unique identifier and can depend on other
17
- * nodes/be depended on. The construction of the graph maintains some important invariants that are
18
- * inherent to the model:
19
- *
20
- * 1. The graph is a DAG, there are no cycles.
21
- * 2. There is always a root node upon which all other nodes eventually depend.
22
- *
23
- * This allows particular optimizations in this class so that we do no need to check for cycles as
24
- * these methods are called and we can always start traversal at the root node.
25
- */
26
-
27
- /**
28
- * @template [T=any]
29
- */
30
- class BaseNode {
31
- static TYPES = /** @type {{NETWORK: 'network', CPU: 'cpu'}} */({
32
- NETWORK: 'network',
33
- CPU: 'cpu',
34
- });
35
-
36
- /**
37
- * @param {string} id
38
- */
39
- constructor(id) {
40
- this._id = id;
41
- this._isMainDocument = false;
42
- /** @type {Node[]} */
43
- this._dependents = [];
44
- /** @type {Node[]} */
45
- this._dependencies = [];
46
- }
47
-
48
- /**
49
- * @return {string}
50
- */
51
- get id() {
52
- return this._id;
53
- }
54
-
55
- /**
56
- * @return {typeof BaseNode.TYPES[keyof typeof BaseNode.TYPES]}
57
- */
58
- get type() {
59
- throw new Error('Unimplemented');
60
- }
61
-
62
- /**
63
- * In microseconds
64
- * @return {number}
65
- */
66
- get startTime() {
67
- throw new Error('Unimplemented');
68
- }
69
-
70
- /**
71
- * In microseconds
72
- * @return {number}
73
- */
74
- get endTime() {
75
- throw new Error('Unimplemented');
76
- }
77
-
78
- /**
79
- * @param {boolean} value
80
- */
81
- setIsMainDocument(value) {
82
- this._isMainDocument = value;
83
- }
84
-
85
- /**
86
- * @return {boolean}
87
- */
88
- isMainDocument() {
89
- return this._isMainDocument;
90
- }
91
-
92
- /**
93
- * @return {Node[]}
94
- */
95
- getDependents() {
96
- return this._dependents.slice();
97
- }
98
-
99
- /**
100
- * @return {number}
101
- */
102
- getNumberOfDependents() {
103
- return this._dependents.length;
104
- }
105
-
106
- /**
107
- * @return {Node[]}
108
- */
109
- getDependencies() {
110
- return this._dependencies.slice();
111
- }
112
-
113
- /**
114
- * @return {number}
115
- */
116
- getNumberOfDependencies() {
117
- return this._dependencies.length;
118
- }
119
-
120
- /**
121
- * @return {Node}
122
- */
123
- getRootNode() {
124
- let rootNode = /** @type {Node} */ (/** @type {BaseNode} */ (this));
125
- while (rootNode._dependencies.length) {
126
- rootNode = rootNode._dependencies[0];
127
- }
128
-
129
- return rootNode;
130
- }
131
-
132
- /**
133
- * @param {Node} node
134
- */
135
- addDependent(node) {
136
- node.addDependency(/** @type {Node} */ (/** @type {BaseNode} */ (this)));
137
- }
138
-
139
- /**
140
- * @param {Node} node
141
- */
142
- addDependency(node) {
143
- // @ts-expect-error - in checkJs, ts doesn't know that CPUNode and NetworkNode *are* BaseNodes.
144
- if (node === this) throw new Error('Cannot add dependency on itself');
145
-
146
- if (this._dependencies.includes(node)) {
147
- return;
148
- }
149
-
150
- node._dependents.push(/** @type {Node} */ (/** @type {BaseNode} */ (this)));
151
- this._dependencies.push(node);
152
- }
153
-
154
- /**
155
- * @param {Node} node
156
- */
157
- removeDependent(node) {
158
- node.removeDependency(/** @type {Node} */ (/** @type {BaseNode} */ (this)));
159
- }
160
-
161
- /**
162
- * @param {Node} node
163
- */
164
- removeDependency(node) {
165
- if (!this._dependencies.includes(node)) {
166
- return;
167
- }
168
-
169
- const thisIndex = node._dependents.indexOf(/** @type {Node} */ (/** @type {BaseNode} */(this)));
170
- node._dependents.splice(thisIndex, 1);
171
- this._dependencies.splice(this._dependencies.indexOf(node), 1);
172
- }
173
-
174
- removeAllDependencies() {
175
- for (const node of this._dependencies.slice()) {
176
- this.removeDependency(node);
177
- }
178
- }
179
-
180
- /**
181
- * Computes whether the given node is anywhere in the dependency graph of this node.
182
- * While this method can prevent cycles, it walks the graph and should be used sparingly.
183
- * Nodes are always considered dependent on themselves for the purposes of cycle detection.
184
- * @param {BaseNode} node
185
- * @return {boolean}
186
- */
187
- isDependentOn(node) {
188
- let isDependentOnNode = false;
189
- this.traverse(currentNode => {
190
- if (isDependentOnNode) return;
191
- isDependentOnNode = currentNode === node;
192
- }, currentNode => {
193
- // If we've already found the dependency, don't traverse further.
194
- if (isDependentOnNode) return [];
195
- // Otherwise, traverse the dependencies.
196
- return currentNode.getDependencies();
197
- });
198
-
199
- return isDependentOnNode;
200
- }
201
-
202
- /**
203
- * Clones the node's information without adding any dependencies/dependents.
204
- * @return {Node}
205
- */
206
- cloneWithoutRelationships() {
207
- const node = /** @type {Node} */ (new BaseNode(this.id));
208
- node.setIsMainDocument(this._isMainDocument);
209
- return node;
210
- }
211
-
212
- /**
213
- * Clones the entire graph connected to this node filtered by the optional predicate. If a node is
214
- * included by the predicate, all nodes along the paths between the node and the root will be included. If the
215
- * node this was called on is not included in the resulting filtered graph, the method will throw.
216
- * @param {function(Node):boolean} [predicate]
217
- * @return {Node}
218
- */
219
- cloneWithRelationships(predicate) {
220
- const rootNode = this.getRootNode();
221
-
222
- /** @type {Map<string, Node>} */
223
- const idsToIncludedClones = new Map();
224
-
225
- // Walk down dependents.
226
- rootNode.traverse(node => {
227
- if (idsToIncludedClones.has(node.id)) return;
228
-
229
- if (predicate === undefined) {
230
- // No condition for entry, so clone every node.
231
- idsToIncludedClones.set(node.id, node.cloneWithoutRelationships());
232
- return;
233
- }
234
-
235
- if (predicate(node)) {
236
- // Node included, so walk back up dependencies, cloning nodes from here back to the root.
237
- node.traverse(
238
- node => idsToIncludedClones.set(node.id, node.cloneWithoutRelationships()),
239
- // Dependencies already cloned have already cloned ancestors, so no need to visit again.
240
- node => node._dependencies.filter(parent => !idsToIncludedClones.has(parent.id))
241
- );
242
- }
243
- });
244
-
245
- // Copy dependencies between nodes.
246
- rootNode.traverse(originalNode => {
247
- const clonedNode = idsToIncludedClones.get(originalNode.id);
248
- if (!clonedNode) return;
249
-
250
- for (const dependency of originalNode._dependencies) {
251
- const clonedDependency = idsToIncludedClones.get(dependency.id);
252
- if (!clonedDependency) throw new Error('Dependency somehow not cloned');
253
- clonedNode.addDependency(clonedDependency);
254
- }
255
- });
256
-
257
- const clonedThisNode = idsToIncludedClones.get(this.id);
258
- if (!clonedThisNode) throw new Error('Cloned graph missing node');
259
- return clonedThisNode;
260
- }
261
-
262
- /**
263
- * Traverses all connected nodes in BFS order, calling `callback` exactly once
264
- * on each. `traversalPath` is the shortest (though not necessarily unique)
265
- * path from `node` to the root of the iteration.
266
- *
267
- * The `getNextNodes` function takes a visited node and returns which nodes to
268
- * visit next. It defaults to returning the node's dependents.
269
- * @param {(node: Node<T>, traversalPath: Node<T>[]) => void} callback
270
- * @param {function(Node<T>): Node<T>[]} [getNextNodes]
271
- */
272
- traverse(callback, getNextNodes) {
273
- for (const {node, traversalPath} of this.traverseGenerator(getNextNodes)) {
274
- callback(node, traversalPath);
275
- }
276
- }
277
-
278
- /**
279
- * @see BaseNode.traverse
280
- * @param {function(Node): Node[]} [getNextNodes]
281
- */
282
- * traverseGenerator(getNextNodes) {
283
- if (!getNextNodes) {
284
- getNextNodes = node => node.getDependents();
285
- }
286
-
287
- /** @type {Node[][]} */
288
- // @ts-expect-error - only traverses graphs of Node, so force tsc to treat `this` as one
289
- const queue = [[this]];
290
- const visited = new Set([this.id]);
291
-
292
- while (queue.length) {
293
- /** @type {Node[]} */
294
- // @ts-expect-error - queue has length so it's guaranteed to have an item
295
- const traversalPath = queue.shift();
296
- const node = traversalPath[0];
297
- yield {node, traversalPath};
298
-
299
- for (const nextNode of getNextNodes(node)) {
300
- if (visited.has(nextNode.id)) continue;
301
- visited.add(nextNode.id);
302
-
303
- queue.push([nextNode, ...traversalPath]);
304
- }
305
- }
306
- }
307
-
308
- /**
309
- * Returns whether the given node has a cycle in its dependent graph by performing a DFS.
310
- * @param {Node} node
311
- * @param {'dependents'|'dependencies'|'both'} [direction]
312
- * @return {boolean}
313
- */
314
- static hasCycle(node, direction = 'both') {
315
- // Checking 'both' is the default entrypoint to recursively check both directions
316
- if (direction === 'both') {
317
- return BaseNode.hasCycle(node, 'dependents') || BaseNode.hasCycle(node, 'dependencies');
318
- }
319
-
320
- const visited = new Set();
321
- /** @type {Node[]} */
322
- const currentPath = [];
323
- const toVisit = [node];
324
- const depthAdded = new Map([[node, 0]]);
325
-
326
- // Keep going while we have nodes to visit in the stack
327
- while (toVisit.length) {
328
- // Get the last node in the stack (DFS uses stack, not queue)
329
- /** @type {Node} */
330
- // @ts-expect-error - toVisit has length so it's guaranteed to have an item
331
- const currentNode = toVisit.pop();
332
-
333
- // We've hit a cycle if the node we're visiting is in our current dependency path
334
- if (currentPath.includes(currentNode)) return true;
335
- // If we've already visited the node, no need to revisit it
336
- if (visited.has(currentNode)) continue;
337
-
338
- // Since we're visiting this node, clear out any nodes in our path that we had to backtrack
339
- // @ts-expect-error
340
- while (currentPath.length > depthAdded.get(currentNode)) currentPath.pop();
341
-
342
- // Update our data structures to reflect that we're adding this node to our path
343
- visited.add(currentNode);
344
- currentPath.push(currentNode);
345
-
346
- // Add all of its dependents to our toVisit stack
347
- const nodesToExplore = direction === 'dependents' ?
348
- currentNode._dependents :
349
- currentNode._dependencies;
350
- for (const nextNode of nodesToExplore) {
351
- if (toVisit.includes(nextNode)) continue;
352
- toVisit.push(nextNode);
353
- depthAdded.set(nextNode, currentPath.length);
354
- }
355
- }
356
-
357
- return false;
358
- }
359
-
360
- /**
361
- * @param {Node} node
362
- * @return {boolean}
363
- */
364
- canDependOn(node) {
365
- return node.startTime <= this.startTime;
366
- }
367
- }
368
-
369
- export {BaseNode};