chrome-devtools-mcp 1.10.1 → 1.12.0

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 (103) hide show
  1. package/README.md +72 -20
  2. package/build/src/BrowserManager.js +155 -27
  3. package/build/src/McpContext.js +598 -200
  4. package/build/src/McpPage.js +513 -131
  5. package/build/src/McpResponse.js +291 -116
  6. package/build/src/McpWorker.js +66 -0
  7. package/build/src/SlimMcpResponse.js +5 -1
  8. package/build/src/TextSnapshot.js +40 -20
  9. package/build/src/ToolHandler.js +306 -147
  10. package/build/src/bin/chrome-devtools-mcp-main.js +13 -10
  11. package/build/src/bin/chrome-devtools-mcp.js +1 -1
  12. package/build/src/bin/chrome-devtools.js +34 -44
  13. package/build/src/collectors/PageCollector.js +13 -9
  14. package/build/src/collectors/ServiceWorkerCollector.js +20 -13
  15. package/build/src/collectors/constants.js +7 -0
  16. package/build/src/config/ConfigLocator.js +70 -0
  17. package/build/src/config/ConfigParser.js +159 -0
  18. package/build/src/config/ConfigWatcher.js +94 -0
  19. package/build/src/config/ServerConfig.js +251 -0
  20. package/build/src/config/browser-options.js +15 -17
  21. package/build/src/config/category-options.js +15 -30
  22. package/build/src/config/cli-commands.js +165 -5
  23. package/build/src/config/cli-options.js +441 -58
  24. package/build/src/config/mcp-options.js +212 -264
  25. package/build/src/config/puppeteer-options.js +67 -0
  26. package/build/src/config/tool-options.js +60 -0
  27. package/build/src/daemon/client.js +84 -44
  28. package/build/src/daemon/daemon.js +59 -45
  29. package/build/src/daemon/utils.js +46 -12
  30. package/build/src/devtools/DevToolsCommentBridge.js +71 -11
  31. package/build/src/devtools/DevtoolsUtils.js +52 -15
  32. package/build/src/devtools/McpHostBindingAdapter.js +0 -1
  33. package/build/src/formatters/CommentFormatter.js +4 -13
  34. package/build/src/formatters/ConsoleFormatter.js +29 -2
  35. package/build/src/formatters/CssFormatter.js +3 -2
  36. package/build/src/formatters/HeapSnapshotFormatter.js +111 -57
  37. package/build/src/formatters/IssueFormatter.js +33 -32
  38. package/build/src/formatters/NetworkFormatter.js +62 -22
  39. package/build/src/formatters/RemoteObjectFormatter.js +231 -0
  40. package/build/src/formatters/SnapshotFormatter.js +44 -7
  41. package/build/src/index.js +285 -88
  42. package/build/src/processors/ChunkedTraceParser.js +11 -11
  43. package/build/src/processors/HeapSnapshotManager.js +455 -156
  44. package/build/src/processors/PerformanceTrace.js +9 -9
  45. package/build/src/telemetry/ClearcutLogger.js +1 -1
  46. package/build/src/telemetry/transformation.js +7 -11
  47. package/build/src/telemetry/watchdog/main.js +1 -1
  48. package/build/src/third_party/THIRD_PARTY_NOTICES +399 -689
  49. package/build/src/third_party/bundled-packages.json +7 -8
  50. package/build/src/third_party/cli.js +9239 -0
  51. package/build/src/third_party/devtools-formatter-worker.js +4299 -2517
  52. package/build/src/third_party/devtools-heap-snapshot-worker.js +1298 -22
  53. package/build/src/third_party/index.js +132003 -146852
  54. package/build/src/third_party/issue-descriptions/cookieWarnMetadataGrantRead.md +0 -1
  55. package/build/src/third_party/issue-descriptions/cookieWarnMetadataGrantReadOptOut.md +5 -0
  56. package/build/src/third_party/issue-descriptions/cookieWarnMetadataGrantSet.md +0 -1
  57. package/build/src/third_party/issue-descriptions/cookieWarnMetadataGrantSetOptOut.md +5 -0
  58. package/build/src/third_party/issue-descriptions/deprecation.md +1 -1
  59. package/build/src/third_party/issue-descriptions/genericGeolocationPromptWithoutUserGesture.md +5 -0
  60. package/build/src/third_party/issue-descriptions/sriValidationFailedIntegrityMismatch.md +2 -0
  61. package/build/src/third_party/lighthouse-devtools-mcp-bundle.js +54576 -52174
  62. package/build/src/tools/ToolDefinition.js +102 -12
  63. package/build/src/tools/comments.js +62 -4
  64. package/build/src/tools/console.js +6 -4
  65. package/build/src/tools/css.js +5 -2
  66. package/build/src/tools/emulation.js +22 -8
  67. package/build/src/tools/extensions.js +5 -0
  68. package/build/src/tools/input.js +29 -9
  69. package/build/src/tools/lighthouse.js +136 -12
  70. package/build/src/tools/memory.js +166 -59
  71. package/build/src/tools/network.js +5 -4
  72. package/build/src/tools/pages.js +52 -7
  73. package/build/src/tools/performance.js +28 -7
  74. package/build/src/tools/pwa.js +7 -0
  75. package/build/src/tools/screencast.js +74 -33
  76. package/build/src/tools/screenshot.js +52 -21
  77. package/build/src/tools/script.js +125 -40
  78. package/build/src/tools/slim/tools.js +27 -13
  79. package/build/src/tools/snapshot.js +13 -3
  80. package/build/src/tools/thirdPartyDeveloper.js +9 -5
  81. package/build/src/tools/tools.js +35 -22
  82. package/build/src/tools/webmcp.js +11 -6
  83. package/build/src/types.js +11 -0
  84. package/build/src/utils/TaskRunner.js +136 -0
  85. package/build/src/utils/WaitForHelper.js +15 -2
  86. package/build/src/utils/bytes.js +2 -12
  87. package/build/src/utils/check-for-updates.js +2 -2
  88. package/build/src/utils/disclaimers.js +27 -0
  89. package/build/src/utils/errorHandling.js +20 -0
  90. package/build/src/utils/keyboard.js +14 -2
  91. package/build/src/utils/logger.js +84 -5
  92. package/build/src/utils/resourceType.js +27 -0
  93. package/build/src/utils/text.js +44 -0
  94. package/build/src/utils/timeout.js +14 -0
  95. package/build/src/utils/url.js +140 -3
  96. package/build/src/version.js +1 -1
  97. package/package.json +16 -11
  98. package/skills/a11y-debugging/SKILL.md +7 -12
  99. package/skills/chrome-devtools/SKILL.md +2 -2
  100. package/skills/chrome-devtools-cli/SKILL.md +18 -15
  101. package/skills/cookie-debugging/SKILL.md +8 -5
  102. package/skills/debug-optimize-lcp/SKILL.md +1 -1
  103. package/skills/troubleshooting/SKILL.md +5 -6
@@ -3,10 +3,96 @@
3
3
  * Copyright 2026 Google LLC
4
4
  * SPDX-License-Identifier: Apache-2.0
5
5
  */
6
+ var __addDisposableResource = (this && this.__addDisposableResource) || function (env, value, async) {
7
+ if (value !== null && value !== void 0) {
8
+ if (typeof value !== "object" && typeof value !== "function") throw new TypeError("Object expected.");
9
+ var dispose, inner;
10
+ if (async) {
11
+ if (!Symbol.asyncDispose) throw new TypeError("Symbol.asyncDispose is not defined.");
12
+ dispose = value[Symbol.asyncDispose];
13
+ }
14
+ if (dispose === void 0) {
15
+ if (!Symbol.dispose) throw new TypeError("Symbol.dispose is not defined.");
16
+ dispose = value[Symbol.dispose];
17
+ if (async) inner = dispose;
18
+ }
19
+ if (typeof dispose !== "function") throw new TypeError("Object not disposable.");
20
+ if (inner) dispose = function() { try { inner.call(this); } catch (e) { return Promise.reject(e); } };
21
+ env.stack.push({ value: value, dispose: dispose, async: async });
22
+ }
23
+ else if (async) {
24
+ env.stack.push({ async: true });
25
+ }
26
+ return value;
27
+ };
28
+ var __disposeResources = (this && this.__disposeResources) || (function (SuppressedError) {
29
+ return function (env) {
30
+ function fail(e) {
31
+ env.error = env.hasError ? new SuppressedError(e, env.error, "An error was suppressed during disposal.") : e;
32
+ env.hasError = true;
33
+ }
34
+ var r, s = 0;
35
+ function next() {
36
+ while (r = env.stack.pop()) {
37
+ try {
38
+ if (!r.async && s === 1) return s = 0, env.stack.push(r), Promise.resolve().then(next);
39
+ if (r.dispose) {
40
+ var result = r.dispose.call(r.value);
41
+ if (r.async) return s |= 2, Promise.resolve(result).then(next, function(e) { fail(e); return next(); });
42
+ }
43
+ else s |= 1;
44
+ }
45
+ catch (e) {
46
+ fail(e);
47
+ }
48
+ }
49
+ if (s === 1) return env.hasError ? Promise.reject(env.error) : Promise.resolve();
50
+ if (env.hasError) throw env.error;
51
+ }
52
+ return next();
53
+ };
54
+ })(typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
55
+ var e = new Error(message);
56
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
57
+ });
6
58
  import fsSync from 'node:fs';
7
59
  import path from 'node:path';
8
60
  import { DevTools } from '../third_party/index.js';
9
- import { createIdGenerator, stableIdSymbol, } from '../utils/id.js';
61
+ import { createIdGenerator } from '../utils/id.js';
62
+ function toClassDiff(index, rawDiff) {
63
+ return {
64
+ index,
65
+ className: rawDiff.name,
66
+ addedCount: rawDiff.addedCount,
67
+ removedCount: rawDiff.removedCount,
68
+ countDelta: rawDiff.countDelta,
69
+ addedSize: rawDiff.addedSize,
70
+ removedSize: rawDiff.removedSize,
71
+ sizeDelta: rawDiff.sizeDelta,
72
+ };
73
+ }
74
+ export class HeapSnapshot {
75
+ proxy;
76
+ worker;
77
+ // 1-indexed array where index is the class ID.
78
+ #idToClassKey = [''];
79
+ #classKeyToId = new Map();
80
+ constructor(proxy, worker, aggregates) {
81
+ this.proxy = proxy;
82
+ this.worker = worker;
83
+ for (const key of Object.keys(aggregates)) {
84
+ const id = this.#idToClassKey.length;
85
+ this.#classKeyToId.set(key, id);
86
+ this.#idToClassKey.push(key);
87
+ }
88
+ }
89
+ resolveIdFromClassKey(classKey) {
90
+ return this.#classKeyToId.get(classKey);
91
+ }
92
+ resolveClassKeyFromId(id) {
93
+ return this.#idToClassKey[id];
94
+ }
95
+ }
10
96
  const VALID_EXTENSIONS = ['.heapsnapshot', '.heaptimeline'];
11
97
  function hasValidHeapSnapshotExtension(filePath) {
12
98
  return VALID_EXTENSIONS.some(ext => filePath.endsWith(ext));
@@ -14,24 +100,48 @@ function hasValidHeapSnapshotExtension(filePath) {
14
100
  export class HeapSnapshotManager {
15
101
  #snapshotIdGenerator = createIdGenerator();
16
102
  #snapshots = new Map();
103
+ /**
104
+ * Queue for all heap snapshot work, with one child queue per absolute file
105
+ * path. A read of one file holds the file queue, so reads of different
106
+ * files run at the same time. A read of several files holds this queue.
107
+ * `disposeSnapshot()` holds the file queue, so it waits for in-flight
108
+ * reads of the file, and a read that starts after it loads the file again.
109
+ */
110
+ #taskQueue;
111
+ #taskRunner;
112
+ #fileTaskQueues = new Map();
113
+ constructor(taskRunner) {
114
+ this.#taskRunner = taskRunner;
115
+ this.#taskQueue = taskRunner.createQueue();
116
+ }
117
+ /**
118
+ * Holds the queue of `filePath`, or the heap queue without a path, until
119
+ * the guard is disposed.
120
+ */
121
+ async #scheduleTask(filePath) {
122
+ if (filePath === undefined) {
123
+ return await this.#taskQueue.scheduleTask();
124
+ }
125
+ const absolutePath = path.resolve(filePath);
126
+ let queue = this.#fileTaskQueues.get(absolutePath);
127
+ if (!queue) {
128
+ queue = this.#taskRunner.createQueue(this.#taskQueue);
129
+ this.#fileTaskQueues.set(absolutePath, queue);
130
+ }
131
+ return await queue.scheduleTask();
132
+ }
17
133
  async getSnapshot(filePath) {
18
134
  if (!hasValidHeapSnapshotExtension(filePath)) {
19
135
  throw new Error(`File ${filePath} must have a .heapsnapshot or .heaptimeline extension.`);
20
136
  }
21
137
  const absolutePath = path.resolve(filePath);
22
- const cached = this.#snapshots.get(absolutePath);
23
- if (cached) {
24
- return cached.snapshot;
25
- }
26
- const uid = this.#snapshotIdGenerator();
27
- const { snapshot, worker } = await this.#loadSnapshot(absolutePath, uid);
28
- this.#snapshots.set(absolutePath, {
29
- snapshot,
30
- worker,
31
- idToClassKey: [''],
32
- classKeyToId: new Map(),
33
- });
34
- return snapshot;
138
+ let cached = this.#snapshots.get(absolutePath);
139
+ if (!cached) {
140
+ const uid = this.#snapshotIdGenerator();
141
+ cached = this.#loadSnapshot(absolutePath, uid);
142
+ this.#snapshots.set(absolutePath, cached);
143
+ }
144
+ return await cached;
35
145
  }
36
146
  async #applyNodeFilter(snapshot, filter, filterName, objectId) {
37
147
  if (filterName === 'attributedToSpecificNativeContext') {
@@ -49,114 +159,261 @@ export class HeapSnapshotManager {
49
159
  }
50
160
  }
51
161
  async getAggregates(filePath, filterName, objectId) {
52
- const snapshot = await this.getSnapshot(filePath);
53
- const filter = new DevTools.HeapSnapshotModel.HeapSnapshotModel.NodeFilter();
54
- await this.#applyNodeFilter(snapshot, filter, filterName, objectId);
55
- const aggregates = await snapshot.aggregatesWithFilter(filter);
56
- let objectCount = 0;
57
- let totalSelfSize = 0;
58
- for (const [key, aggregate] of Object.entries(aggregates)) {
59
- const id = await this.getOrCreateIdForClassKey(filePath, key);
60
- aggregate[stableIdSymbol] = id;
61
- objectCount += aggregate.count;
62
- totalSelfSize += aggregate.self;
63
- }
64
- return {
65
- aggregates,
66
- objectCount,
67
- totalSelfSize,
68
- };
162
+ const env_1 = { stack: [], error: void 0, hasError: false };
163
+ try {
164
+ const guard = __addDisposableResource(env_1, await this.#scheduleTask(filePath), false);
165
+ const snapshot = await this.getSnapshot(filePath);
166
+ const filter = new DevTools.HeapSnapshotModel.HeapSnapshotModel.NodeFilter();
167
+ await this.#applyNodeFilter(snapshot.proxy, filter, filterName, objectId);
168
+ const rawAggregates = await snapshot.proxy.aggregatesWithFilter(filter);
169
+ let objectCount = 0;
170
+ let totalSelfSize = 0;
171
+ const aggregates = [];
172
+ for (const [key, aggregate] of Object.entries(rawAggregates)) {
173
+ const id = snapshot.resolveIdFromClassKey(key);
174
+ if (id === undefined) {
175
+ throw new Error(`No class ID found for class key ${key}`);
176
+ }
177
+ objectCount += aggregate.count;
178
+ totalSelfSize += aggregate.self;
179
+ aggregates.push({
180
+ id,
181
+ className: aggregate.name,
182
+ count: aggregate.count,
183
+ selfSize: aggregate.self,
184
+ retainedSize: aggregate.maxRet,
185
+ });
186
+ }
187
+ aggregates.sort((a, b) => b.retainedSize - a.retainedSize);
188
+ return {
189
+ aggregates,
190
+ objectCount,
191
+ totalSelfSize,
192
+ };
193
+ }
194
+ catch (e_1) {
195
+ env_1.error = e_1;
196
+ env_1.hasError = true;
197
+ }
198
+ finally {
199
+ __disposeResources(env_1);
200
+ }
69
201
  }
70
202
  async getStats(filePath) {
71
- const snapshot = await this.getSnapshot(filePath);
72
- return await snapshot.getStatistics();
203
+ const env_2 = { stack: [], error: void 0, hasError: false };
204
+ try {
205
+ const guard = __addDisposableResource(env_2, await this.#scheduleTask(filePath), false);
206
+ const snapshot = await this.getSnapshot(filePath);
207
+ return await snapshot.proxy.getStatistics();
208
+ }
209
+ catch (e_2) {
210
+ env_2.error = e_2;
211
+ env_2.hasError = true;
212
+ }
213
+ finally {
214
+ __disposeResources(env_2);
215
+ }
73
216
  }
74
217
  async getStaticData(filePath) {
75
- const snapshot = await this.getSnapshot(filePath);
76
- return snapshot.staticData;
218
+ const env_3 = { stack: [], error: void 0, hasError: false };
219
+ try {
220
+ const guard = __addDisposableResource(env_3, await this.#scheduleTask(filePath), false);
221
+ const snapshot = await this.getSnapshot(filePath);
222
+ return snapshot.proxy.staticData;
223
+ }
224
+ catch (e_3) {
225
+ env_3.error = e_3;
226
+ env_3.hasError = true;
227
+ }
228
+ finally {
229
+ __disposeResources(env_3);
230
+ }
77
231
  }
78
232
  async getNativeContextSizes(filePath) {
79
- const snapshot = await this.getSnapshot(filePath);
80
- return await snapshot.getNativeContextSizes();
233
+ const env_4 = { stack: [], error: void 0, hasError: false };
234
+ try {
235
+ const guard = __addDisposableResource(env_4, await this.#scheduleTask(filePath), false);
236
+ const snapshot = await this.getSnapshot(filePath);
237
+ return await snapshot.proxy.getNativeContextSizes();
238
+ }
239
+ catch (e_4) {
240
+ env_4.error = e_4;
241
+ env_4.hasError = true;
242
+ }
243
+ finally {
244
+ __disposeResources(env_4);
245
+ }
81
246
  }
82
247
  async getRetainedByContextSummary(filePath) {
83
- const snapshot = await this.getSnapshot(filePath);
84
- return await snapshot.getRetainedByContextSummary();
85
- }
86
- async getOrCreateIdForClassKey(filePath, classKey) {
87
- const cached = this.#getCachedSnapshot(filePath);
88
- let id = cached.classKeyToId.get(classKey);
89
- if (!id) {
90
- id = cached.idToClassKey.length;
91
- cached.classKeyToId.set(classKey, id);
92
- cached.idToClassKey.push(classKey);
93
- }
94
- return id;
248
+ const env_5 = { stack: [], error: void 0, hasError: false };
249
+ try {
250
+ const guard = __addDisposableResource(env_5, await this.#scheduleTask(filePath), false);
251
+ const snapshot = await this.getSnapshot(filePath);
252
+ return await snapshot.proxy.getRetainedByContextSummary();
253
+ }
254
+ catch (e_5) {
255
+ env_5.error = e_5;
256
+ env_5.hasError = true;
257
+ }
258
+ finally {
259
+ __disposeResources(env_5);
260
+ }
95
261
  }
96
262
  async getNodesById(filePath, id, filterName, objectId) {
97
- const snapshot = await this.getSnapshot(filePath);
98
- const filter = new DevTools.HeapSnapshotModel.HeapSnapshotModel.NodeFilter();
99
- await this.#applyNodeFilter(snapshot, filter, filterName, objectId);
100
- const className = await this.resolveClassKeyFromId(filePath, id);
101
- if (!className) {
102
- throw new Error(`Class with ID ${id} not found in heap snapshot`);
103
- }
104
- const provider = snapshot.createNodesProviderForClass(className, filter);
105
- return await provider.serializeItemsRange(0, Infinity);
263
+ const env_6 = { stack: [], error: void 0, hasError: false };
264
+ try {
265
+ const guard = __addDisposableResource(env_6, await this.#scheduleTask(filePath), false);
266
+ const snapshot = await this.getSnapshot(filePath);
267
+ const filter = new DevTools.HeapSnapshotModel.HeapSnapshotModel.NodeFilter();
268
+ await this.#applyNodeFilter(snapshot.proxy, filter, filterName, objectId);
269
+ const className = snapshot.resolveClassKeyFromId(id);
270
+ if (!className) {
271
+ throw new Error(`Class with ID ${id} not found in heap snapshot`);
272
+ }
273
+ const provider = snapshot.proxy.createNodesProviderForClass(className, filter);
274
+ return await provider.serializeItemsRange(0, Infinity);
275
+ }
276
+ catch (e_6) {
277
+ env_6.error = e_6;
278
+ env_6.hasError = true;
279
+ }
280
+ finally {
281
+ __disposeResources(env_6);
282
+ }
106
283
  }
107
284
  async getRetainers(filePath, nodeId) {
108
- const snapshot = await this.getSnapshot(filePath);
109
- const nodeIndex = await snapshot.nodeIndexForId(nodeId);
110
- if (nodeIndex === undefined) {
111
- throw new Error(`Node with ID ${nodeId} not found`);
285
+ const env_7 = { stack: [], error: void 0, hasError: false };
286
+ try {
287
+ const guard = __addDisposableResource(env_7, await this.#scheduleTask(filePath), false);
288
+ const snapshot = await this.getSnapshot(filePath);
289
+ const nodeIndex = await snapshot.proxy.nodeIndexForId(nodeId);
290
+ if (nodeIndex === undefined) {
291
+ throw new Error(`Node with ID ${nodeId} not found`);
292
+ }
293
+ const provider = snapshot.proxy.createRetainingEdgesProvider(nodeIndex);
294
+ return await provider.serializeItemsRange(0, Infinity);
295
+ }
296
+ catch (e_7) {
297
+ env_7.error = e_7;
298
+ env_7.hasError = true;
299
+ }
300
+ finally {
301
+ __disposeResources(env_7);
112
302
  }
113
- const provider = snapshot.createRetainingEdgesProvider(nodeIndex);
114
- return await provider.serializeItemsRange(0, Infinity);
115
303
  }
116
304
  async getObjectInfo(filePath, nodeId) {
117
- const snapshot = await this.getSnapshot(filePath);
118
- const nodeIndex = await snapshot.nodeIndexForId(nodeId);
119
- if (nodeIndex === undefined) {
120
- throw new Error(`Node with ID ${nodeId} not found`);
305
+ const env_8 = { stack: [], error: void 0, hasError: false };
306
+ try {
307
+ const guard = __addDisposableResource(env_8, await this.#scheduleTask(filePath), false);
308
+ const snapshot = await this.getSnapshot(filePath);
309
+ const nodeIndex = await snapshot.proxy.nodeIndexForId(nodeId);
310
+ if (nodeIndex === undefined) {
311
+ throw new Error(`Node with ID ${nodeId} not found`);
312
+ }
313
+ return await snapshot.proxy.getObjectInfo(nodeIndex);
314
+ }
315
+ catch (e_8) {
316
+ env_8.error = e_8;
317
+ env_8.hasError = true;
318
+ }
319
+ finally {
320
+ __disposeResources(env_8);
321
+ }
322
+ }
323
+ async analyzeContexts(filePath) {
324
+ const env_9 = { stack: [], error: void 0, hasError: false };
325
+ try {
326
+ const guard = __addDisposableResource(env_9, await this.#scheduleTask(filePath), false);
327
+ const snapshot = await this.getSnapshot(filePath);
328
+ return await snapshot.proxy.analyzeContexts();
329
+ }
330
+ catch (e_9) {
331
+ env_9.error = e_9;
332
+ env_9.hasError = true;
333
+ }
334
+ finally {
335
+ __disposeResources(env_9);
121
336
  }
122
- return await snapshot.getObjectInfo(nodeIndex);
123
337
  }
124
338
  async getRetainingPaths(filePath, nodeId, maxDepth, maxNodes, maxSiblings) {
125
- const snapshot = await this.getSnapshot(filePath);
126
- const nodeIndex = await snapshot.nodeIndexForId(nodeId);
127
- if (nodeIndex === undefined) {
128
- throw new Error(`Node with ID ${nodeId} not found`);
339
+ const env_10 = { stack: [], error: void 0, hasError: false };
340
+ try {
341
+ const guard = __addDisposableResource(env_10, await this.#scheduleTask(filePath), false);
342
+ const snapshot = await this.getSnapshot(filePath);
343
+ const nodeIndex = await snapshot.proxy.nodeIndexForId(nodeId);
344
+ if (nodeIndex === undefined) {
345
+ throw new Error(`Node with ID ${nodeId} not found`);
346
+ }
347
+ return await snapshot.proxy.getRetainingPaths(nodeIndex, maxDepth, maxNodes, maxSiblings);
348
+ }
349
+ catch (e_10) {
350
+ env_10.error = e_10;
351
+ env_10.hasError = true;
352
+ }
353
+ finally {
354
+ __disposeResources(env_10);
129
355
  }
130
- return await snapshot.getRetainingPaths(nodeIndex, maxDepth, maxNodes, maxSiblings);
131
356
  }
132
357
  async getDominatorsOf(filePath, nodeId) {
133
- const snapshot = await this.getSnapshot(filePath);
134
- const nodeIndex = await snapshot.nodeIndexForId(nodeId);
135
- if (nodeIndex === undefined) {
136
- throw new Error(`Node with ID ${nodeId} not found`);
358
+ const env_11 = { stack: [], error: void 0, hasError: false };
359
+ try {
360
+ const guard = __addDisposableResource(env_11, await this.#scheduleTask(filePath), false);
361
+ const snapshot = await this.getSnapshot(filePath);
362
+ const nodeIndex = await snapshot.proxy.nodeIndexForId(nodeId);
363
+ if (nodeIndex === undefined) {
364
+ throw new Error(`Node with ID ${nodeId} not found`);
365
+ }
366
+ return await snapshot.proxy.getDominatorsOf(nodeIndex);
367
+ }
368
+ catch (e_11) {
369
+ env_11.error = e_11;
370
+ env_11.hasError = true;
371
+ }
372
+ finally {
373
+ __disposeResources(env_11);
137
374
  }
138
- return await snapshot.getDominatorsOf(nodeIndex);
139
375
  }
140
376
  async getEdges(filePath, nodeId, options) {
141
- const snapshot = await this.getSnapshot(filePath);
142
- const nodeIndex = await snapshot.nodeIndexForId(nodeId);
143
- if (nodeIndex === undefined) {
144
- throw new Error(`Node with ID ${nodeId} not found`);
377
+ const env_12 = { stack: [], error: void 0, hasError: false };
378
+ try {
379
+ const guard = __addDisposableResource(env_12, await this.#scheduleTask(filePath), false);
380
+ const snapshot = await this.getSnapshot(filePath);
381
+ const nodeIndex = await snapshot.proxy.nodeIndexForId(nodeId);
382
+ if (nodeIndex === undefined) {
383
+ throw new Error(`Node with ID ${nodeId} not found`);
384
+ }
385
+ const provider = snapshot.proxy.createEdgesProvider(nodeIndex, options);
386
+ return await provider.serializeItemsRange(0, Infinity);
387
+ }
388
+ catch (e_12) {
389
+ env_12.error = e_12;
390
+ env_12.hasError = true;
391
+ }
392
+ finally {
393
+ __disposeResources(env_12);
145
394
  }
146
- const provider = snapshot.createEdgesProvider(nodeIndex, options);
147
- return await provider.serializeItemsRange(0, Infinity);
148
395
  }
149
396
  async getClassDiffs(baseFilePath, currentFilePath) {
150
397
  const rawDiffs = await this.#getSortedRawClassDiffs(baseFilePath, currentFilePath);
151
- return rawDiffs.map(rawDiff => ({
152
- className: rawDiff.name,
153
- addedCount: rawDiff.addedCount,
154
- removedCount: rawDiff.removedCount,
155
- countDelta: rawDiff.countDelta,
156
- addedSize: rawDiff.addedSize,
157
- removedSize: rawDiff.removedSize,
158
- sizeDelta: rawDiff.sizeDelta,
159
- }));
398
+ const classDiffs = [];
399
+ const totals = {
400
+ addedCount: 0,
401
+ removedCount: 0,
402
+ countDelta: 0,
403
+ addedSize: 0,
404
+ removedSize: 0,
405
+ sizeDelta: 0,
406
+ };
407
+ for (const [index, rawDiff] of rawDiffs.entries()) {
408
+ classDiffs.push(toClassDiff(index, rawDiff));
409
+ totals.addedCount += rawDiff.addedCount;
410
+ totals.removedCount += rawDiff.removedCount;
411
+ totals.countDelta += rawDiff.countDelta;
412
+ totals.addedSize += rawDiff.addedSize;
413
+ totals.removedSize += rawDiff.removedSize;
414
+ totals.sizeDelta += rawDiff.sizeDelta;
415
+ }
416
+ return { classDiffs, totals };
160
417
  }
161
418
  async getDetailedClassDiff(baseFilePath, currentFilePath, classIndex) {
162
419
  const classDiffs = await this.#getSortedRawClassDiffs(baseFilePath, currentFilePath);
@@ -164,50 +421,53 @@ export class HeapSnapshotManager {
164
421
  if (!rawDiff) {
165
422
  throw new Error(`Invalid classIndex: ${classIndex}. Total classes with changes: ${classDiffs.length}`);
166
423
  }
167
- return {
168
- className: rawDiff.name,
169
- addedCount: rawDiff.addedCount,
170
- removedCount: rawDiff.removedCount,
171
- countDelta: rawDiff.countDelta,
172
- addedSize: rawDiff.addedSize,
173
- removedSize: rawDiff.removedSize,
174
- sizeDelta: rawDiff.sizeDelta,
175
- addedIds: rawDiff.addedIds ?? [],
176
- addedSelfSizes: rawDiff.addedSelfSizes ?? [],
177
- deletedIds: rawDiff.deletedIds ?? [],
178
- deletedSelfSizes: rawDiff.deletedSelfSizes ?? [],
179
- };
180
- }
181
- #getCachedSnapshot(filePath) {
182
- const absolutePath = path.resolve(filePath);
183
- const cached = this.#snapshots.get(absolutePath);
184
- if (!cached) {
185
- throw new Error(`Snapshot not loaded for ${filePath}`);
424
+ const objects = [];
425
+ const addedIds = rawDiff.addedIds ?? [];
426
+ const addedSelfSizes = rawDiff.addedSelfSizes ?? [];
427
+ for (const [i, id] of addedIds.entries()) {
428
+ objects.push({ id, selfSize: addedSelfSizes[i], change: 'added' });
429
+ }
430
+ const deletedIds = rawDiff.deletedIds ?? [];
431
+ const deletedSelfSizes = rawDiff.deletedSelfSizes ?? [];
432
+ for (const [i, id] of deletedIds.entries()) {
433
+ objects.push({ id, selfSize: deletedSelfSizes[i], change: 'deleted' });
186
434
  }
187
- return cached;
435
+ // Largest objects first so that the first page of paginated output shows
436
+ // the most significant changes.
437
+ objects.sort((a, b) => b.selfSize - a.selfSize);
438
+ return { classDiff: toClassDiff(classIndex, rawDiff), objects };
188
439
  }
189
440
  async #getSortedRawClassDiffs(baseFilePath, currentFilePath) {
190
- const baseSnapshot = await this.getSnapshot(baseFilePath);
191
- const currentSnapshot = await this.getSnapshot(currentFilePath);
192
- const interfaceDefinitions = await currentSnapshot.interfaceDefinitions();
193
- const aggregatesForDiff = await baseSnapshot.aggregatesForDiff(interfaceDefinitions);
194
- const baseSnapshotId = baseSnapshot.uid;
195
- if (baseSnapshotId === undefined) {
196
- throw new Error('Base snapshot UID is undefined');
197
- }
198
- // DevTools calculateSnapshotDiff uses the first parameter (baseSnapshotId)
199
- // as a cache key. We pass the unique UID of the base snapshot.
200
- const rawDiffs = await currentSnapshot.calculateSnapshotDiff(baseSnapshotId, aggregatesForDiff);
201
- // Return a filtered and sorted array here to ensure that
202
- // compare_heapsnapshot_summary and compare_heapsnapshot_details agree
203
- // on indices.
204
- return Object.values(rawDiffs)
205
- .filter(diff => diff.addedCount > 0 || diff.removedCount > 0)
206
- .sort((a, b) => b.sizeDelta - a.sizeDelta);
207
- }
208
- async resolveClassKeyFromId(filePath, id) {
209
- const cached = this.#getCachedSnapshot(filePath);
210
- return cached.idToClassKey[id];
441
+ const env_13 = { stack: [], error: void 0, hasError: false };
442
+ try {
443
+ const guard = __addDisposableResource(env_13, await this.#scheduleTask(), false);
444
+ const [baseSnapshot, currentSnapshot] = await Promise.all([
445
+ this.getSnapshot(baseFilePath),
446
+ this.getSnapshot(currentFilePath),
447
+ ]);
448
+ const interfaceDefinitions = await currentSnapshot.proxy.interfaceDefinitions();
449
+ const aggregatesForDiff = await baseSnapshot.proxy.aggregatesForDiff(interfaceDefinitions);
450
+ const baseSnapshotId = baseSnapshot.proxy.uid;
451
+ if (baseSnapshotId === undefined) {
452
+ throw new Error('Base snapshot UID is undefined');
453
+ }
454
+ // DevTools calculateSnapshotDiff uses the first parameter (baseSnapshotId)
455
+ // as a cache key. We pass the unique UID of the base snapshot.
456
+ const rawDiffs = await currentSnapshot.proxy.calculateSnapshotDiff(baseSnapshotId, aggregatesForDiff);
457
+ // Return a filtered and sorted array here to ensure that
458
+ // compare_heapsnapshot_summary and compare_heapsnapshot_details agree
459
+ // on indices.
460
+ return Object.values(rawDiffs)
461
+ .filter(diff => diff.addedCount > 0 || diff.removedCount > 0)
462
+ .sort((a, b) => b.sizeDelta - a.sizeDelta);
463
+ }
464
+ catch (e_13) {
465
+ env_13.error = e_13;
466
+ env_13.hasError = true;
467
+ }
468
+ finally {
469
+ __disposeResources(env_13);
470
+ }
211
471
  }
212
472
  async #loadSnapshot(absolutePath, uid) {
213
473
  const workerProxy = new DevTools.HeapSnapshotModel.HeapSnapshotProxy.HeapSnapshotWorkerProxy(() => {
@@ -227,43 +487,82 @@ export class HeapSnapshotManager {
227
487
  }
228
488
  await loaderProxy.close();
229
489
  const snapshot = await snapshotPromise;
230
- return { snapshot, worker: workerProxy };
490
+ // Assign ids to all aggregates of this snapshot. This ensures ids are available
491
+ // even without calling getAggregates() and deterministic (do not depend on
492
+ // the filter used on the getAggregates() call).
493
+ const aggregates = await snapshot.aggregatesWithFilter(new DevTools.HeapSnapshotModel.HeapSnapshotModel.NodeFilter());
494
+ return new HeapSnapshot(snapshot, workerProxy, aggregates);
231
495
  }
232
496
  catch (error) {
233
- // The worker is created before the read, and a failed load never reaches
234
- // the #snapshots map, so dispose()/disposeAll() can never clean it up.
235
- // Dispose it here to avoid leaking a worker on every failed load (e.g. a
236
- // missing or invalid .heapsnapshot path).
497
+ // Remove the rejected promise from #snapshots and dispose the worker to
498
+ // avoid leaking it on a failed load (e.g. a missing or invalid
499
+ // .heapsnapshot path).
500
+ this.#snapshots.delete(absolutePath);
237
501
  workerProxy.dispose();
238
502
  throw error;
239
503
  }
240
504
  }
241
505
  async getDuplicateStrings(filePath) {
242
- const snapshot = await this.getSnapshot(filePath);
243
- return await snapshot.getDuplicateStrings();
506
+ const env_14 = { stack: [], error: void 0, hasError: false };
507
+ try {
508
+ const guard = __addDisposableResource(env_14, await this.#scheduleTask(filePath), false);
509
+ const snapshot = await this.getSnapshot(filePath);
510
+ return await snapshot.proxy.getDuplicateStrings();
511
+ }
512
+ catch (e_14) {
513
+ env_14.error = e_14;
514
+ env_14.hasError = true;
515
+ }
516
+ finally {
517
+ __disposeResources(env_14);
518
+ }
244
519
  }
245
520
  async queryObjects(filePath, options) {
246
- const snapshot = await this.getSnapshot(filePath);
247
- const provider = snapshot.queryObjects(options);
248
- return await provider.serializeItemsRange(0, Infinity);
521
+ const env_15 = { stack: [], error: void 0, hasError: false };
522
+ try {
523
+ const guard = __addDisposableResource(env_15, await this.#scheduleTask(filePath), false);
524
+ const snapshot = await this.getSnapshot(filePath);
525
+ const provider = snapshot.proxy.queryObjects(options);
526
+ return await provider.serializeItemsRange(0, Infinity);
527
+ }
528
+ catch (e_15) {
529
+ env_15.error = e_15;
530
+ env_15.hasError = true;
531
+ }
532
+ finally {
533
+ __disposeResources(env_15);
534
+ }
249
535
  }
250
536
  hasSnapshots() {
251
537
  return this.#snapshots.size > 0;
252
538
  }
253
- disposeSnapshot(filePath) {
254
- const absolutePath = path.resolve(filePath);
255
- const cached = this.#snapshots.get(absolutePath);
256
- if (cached) {
257
- cached.worker.dispose();
258
- this.#snapshots.delete(absolutePath);
259
- return true;
539
+ async disposeSnapshot(filePath) {
540
+ const env_16 = { stack: [], error: void 0, hasError: false };
541
+ try {
542
+ const guard = __addDisposableResource(env_16, await this.#scheduleTask(filePath), false);
543
+ const absolutePath = path.resolve(filePath);
544
+ const cached = this.#snapshots.get(absolutePath);
545
+ if (cached) {
546
+ this.#snapshots.delete(absolutePath);
547
+ const { worker } = await cached;
548
+ worker.dispose();
549
+ return true;
550
+ }
551
+ return false;
552
+ }
553
+ catch (e_16) {
554
+ env_16.error = e_16;
555
+ env_16.hasError = true;
556
+ }
557
+ finally {
558
+ __disposeResources(env_16);
260
559
  }
261
- return false;
262
560
  }
263
561
  dispose() {
264
- for (const cached of this.#snapshots.values()) {
265
- cached.worker.dispose();
266
- }
562
+ void Promise.allSettled(this.#snapshots.values().map(async (cached) => {
563
+ const { worker } = await cached;
564
+ worker.dispose();
565
+ }));
267
566
  this.#snapshots.clear();
268
567
  }
269
568
  }