chrome-devtools-mcp 1.7.0 → 1.8.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 (67) hide show
  1. package/README.md +48 -14
  2. package/build/src/McpContext.js +60 -24
  3. package/build/src/McpPage.js +5 -3
  4. package/build/src/McpResponse.js +22 -4
  5. package/build/src/ToolHandler.js +66 -12
  6. package/build/src/bin/chrome-devtools-mcp-main.js +2 -2
  7. package/build/src/bin/chrome-devtools.js +29 -30
  8. package/build/src/browser.js +3 -1
  9. package/build/src/{PageCollector.js → collectors/PageCollector.js} +7 -7
  10. package/build/src/{ServiceWorkerCollector.js → collectors/ServiceWorkerCollector.js} +1 -1
  11. package/build/src/{bin/chrome-devtools-cli-options.js → config/cli-options.js} +378 -23
  12. package/build/src/{bin/chrome-devtools-mcp-cli-options.js → config/mcp-options.js} +73 -8
  13. package/build/src/daemon/client.js +11 -2
  14. package/build/src/daemon/daemon.js +2 -2
  15. package/build/src/devtools/DevtoolsUtils.js +2 -14
  16. package/build/src/formatters/ConsoleFormatter.js +43 -10
  17. package/build/src/formatters/HeapSnapshotFormatter.js +10 -2
  18. package/build/src/formatters/IssueFormatter.js +1 -1
  19. package/build/src/formatters/NetworkFormatter.js +7 -7
  20. package/build/src/index.js +33 -5
  21. package/build/src/{HeapSnapshotManager.js → processors/HeapSnapshotManager.js} +21 -5
  22. package/build/src/{trace-processing/parse.js → processors/PerformanceTrace.js} +1 -1
  23. package/build/src/telemetry/ClearcutLogger.js +13 -1
  24. package/build/src/telemetry/flagUtils.js +4 -3
  25. package/build/src/telemetry/types.js +4 -0
  26. package/build/src/third_party/THIRD_PARTY_NOTICES +6 -63
  27. package/build/src/third_party/bundled-packages.json +4 -4
  28. package/build/src/third_party/devtools-heap-snapshot-worker.js +274 -42
  29. package/build/src/third_party/index.js +35378 -35538
  30. package/build/src/third_party/issue-descriptions/connectionAllowlistEmbeddingRequirementNotSatisfied.md +26 -0
  31. package/build/src/third_party/issue-descriptions/connectionAllowlistIFrameAttributeLoosensEmbeddingRequirement.md +19 -0
  32. package/build/src/third_party/issue-descriptions/connectionAllowlistInvalidAllowConnectionAllowlistFrom.md +21 -0
  33. package/build/src/third_party/issue-descriptions/federatedAuthRequestAccountsBlockedByConnectionAllowlist.md +1 -0
  34. package/build/src/third_party/issue-descriptions/federatedAuthRequestConfigBlockedByConnectionAllowlist.md +1 -0
  35. package/build/src/third_party/issue-descriptions/federatedAuthRequestIdTokenBlockedByConnectionAllowlist.md +1 -0
  36. package/build/src/third_party/issue-descriptions/federatedAuthRequestWellKnownBlockedByConnectionAllowlist.md +1 -0
  37. package/build/src/tools/categories.js +3 -0
  38. package/build/src/tools/console.js +9 -3
  39. package/build/src/tools/emulation.js +2 -2
  40. package/build/src/tools/extensions.js +7 -5
  41. package/build/src/tools/input.js +26 -16
  42. package/build/src/tools/lighthouse.js +3 -1
  43. package/build/src/tools/memory.js +112 -16
  44. package/build/src/tools/network.js +6 -3
  45. package/build/src/tools/pages.js +12 -16
  46. package/build/src/tools/performance.js +15 -25
  47. package/build/src/tools/pwa.js +128 -0
  48. package/build/src/tools/screencast.js +6 -4
  49. package/build/src/tools/screenshot.js +42 -9
  50. package/build/src/tools/script.js +28 -8
  51. package/build/src/tools/slim/tools.js +3 -3
  52. package/build/src/tools/snapshot.js +5 -3
  53. package/build/src/tools/thirdPartyDeveloper.js +2 -2
  54. package/build/src/tools/tools.js +2 -0
  55. package/build/src/tools/webmcp.js +2 -2
  56. package/build/src/{WaitForHelper.js → utils/WaitForHelper.js} +63 -38
  57. package/build/src/utils/bytes.js +105 -0
  58. package/build/src/utils/logger.js +25 -11
  59. package/build/src/version.js +1 -1
  60. package/package.json +20 -12
  61. package/skills/a11y-debugging/SKILL.md +2 -0
  62. package/skills/chrome-devtools/SKILL.md +5 -4
  63. package/skills/chrome-devtools-cli/SKILL.md +109 -64
  64. package/skills/debug-optimize-lcp/SKILL.md +9 -9
  65. package/skills/memory-leak-debugging/SKILL.md +2 -2
  66. package/skills/troubleshooting/SKILL.md +1 -0
  67. package/build/src/utils/string.js +0 -37
@@ -3,8 +3,9 @@
3
3
  * Copyright 2026 Google LLC
4
4
  * SPDX-License-Identifier: Apache-2.0
5
5
  */
6
- import { toSnakeCase } from '../utils/string.js';
6
+ import { DevTools } from '../third_party/index.js';
7
7
  import { stripUnderscoreBeforeNumber } from './transformation.js';
8
+ const { StringUtilities } = DevTools.Platform;
8
9
  /**
9
10
  * For enums, log the value as uppercase.
10
11
  * We're going to have an enum for such flags with choices represented
@@ -30,7 +31,7 @@ export function computeFlagUsage(args, options) {
30
31
  const usage = {};
31
32
  for (const [flagName, config] of Object.entries(options)) {
32
33
  const value = args[flagName];
33
- const snakeCaseName = stripUnderscoreBeforeNumber(toSnakeCase(flagName));
34
+ const snakeCaseName = stripUnderscoreBeforeNumber(StringUtilities.toSnakeCase(flagName));
34
35
  // If there isn't a default value provided for the flag,
35
36
  // we're going to log whether it's present on the args user
36
37
  // provided or not. If there is a default value, we only log presence
@@ -59,7 +60,7 @@ export function computeFlagUsage(args, options) {
59
60
  export function getPossibleFlagMetrics(options) {
60
61
  const metrics = [];
61
62
  for (const [flagName, config] of Object.entries(options)) {
62
- const snakeCaseName = stripUnderscoreBeforeNumber(toSnakeCase(flagName));
63
+ const snakeCaseName = stripUnderscoreBeforeNumber(StringUtilities.toSnakeCase(flagName));
63
64
  // _present is always a possible metric
64
65
  metrics.push({
65
66
  name: `${snakeCaseName}_present`,
@@ -20,6 +20,10 @@ export var McpClient;
20
20
  McpClient[McpClient["MCP_CLIENT_OPENCLAW"] = 5] = "MCP_CLIENT_OPENCLAW";
21
21
  McpClient[McpClient["MCP_CLIENT_CODEX"] = 6] = "MCP_CLIENT_CODEX";
22
22
  McpClient[McpClient["MCP_CLIENT_ANTIGRAVITY"] = 7] = "MCP_CLIENT_ANTIGRAVITY";
23
+ McpClient[McpClient["MCP_CLIENT_GROK"] = 8] = "MCP_CLIENT_GROK";
24
+ McpClient[McpClient["MCP_CLIENT_OPENCODE"] = 9] = "MCP_CLIENT_OPENCODE";
25
+ McpClient[McpClient["MCP_CLIENT_CLAUDE_DESKTOP"] = 10] = "MCP_CLIENT_CLAUDE_DESKTOP";
26
+ McpClient[McpClient["MCP_CLIENT_GITHUB_COPILOT"] = 11] = "MCP_CLIENT_GITHUB_COPILOT";
23
27
  McpClient[McpClient["MCP_CLIENT_OTHER"] = 3] = "MCP_CLIENT_OTHER";
24
28
  })(McpClient || (McpClient = {}));
25
29
  // IPC types for messages between the main process and the
@@ -1,6 +1,6 @@
1
1
  Name: core-js
2
2
  URL: https://core-js.io
3
- Version: 3.49.0
3
+ Version: 3.50.0
4
4
  License: MIT
5
5
 
6
6
  Copyright (c) 2013–2025 Denis Pushkarev (zloirock.ru)
@@ -317,65 +317,6 @@ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
317
317
  IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
318
318
 
319
319
 
320
- -------------------- DEPENDENCY DIVIDER --------------------
321
-
322
- Name: debug
323
- URL: git://github.com/debug-js/debug.git
324
- Version: 4.4.3
325
- License: MIT
326
-
327
- (The MIT License)
328
-
329
- Copyright (c) 2014-2017 TJ Holowaychuk <tj@vision-media.ca>
330
- Copyright (c) 2018-2021 Josh Junon
331
-
332
- Permission is hereby granted, free of charge, to any person obtaining a copy of this software
333
- and associated documentation files (the 'Software'), to deal in the Software without restriction,
334
- including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
335
- and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
336
- subject to the following conditions:
337
-
338
- The above copyright notice and this permission notice shall be included in all copies or substantial
339
- portions of the Software.
340
-
341
- THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
342
- LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
343
- IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
344
- WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
345
- SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
346
-
347
-
348
-
349
- -------------------- DEPENDENCY DIVIDER --------------------
350
-
351
- Name: ms
352
- URL: vercel/ms
353
- Version: 2.1.3
354
- License: MIT
355
-
356
- The MIT License (MIT)
357
-
358
- Copyright (c) 2020 Vercel, Inc.
359
-
360
- Permission is hereby granted, free of charge, to any person obtaining a copy
361
- of this software and associated documentation files (the "Software"), to deal
362
- in the Software without restriction, including without limitation the rights
363
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
364
- copies of the Software, and to permit persons to whom the Software is
365
- furnished to do so, subject to the following conditions:
366
-
367
- The above copyright notice and this permission notice shall be included in all
368
- copies or substantial portions of the Software.
369
-
370
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
371
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
372
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
373
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
374
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
375
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
376
- SOFTWARE.
377
-
378
-
379
320
  -------------------- DEPENDENCY DIVIDER --------------------
380
321
 
381
322
  Name: zod
@@ -804,14 +745,14 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
804
745
 
805
746
  Name: puppeteer-core
806
747
  URL: https://github.com/puppeteer/puppeteer/tree/main/packages/puppeteer-core
807
- Version: 25.5.0
748
+ Version: 25.8.0
808
749
  License: Apache-2.0
809
750
 
810
751
  -------------------- DEPENDENCY DIVIDER --------------------
811
752
 
812
753
  Name: @puppeteer/browsers
813
754
  URL: https://github.com/puppeteer/puppeteer/tree/main/packages/browsers
814
- Version: 3.1.0
755
+ Version: 3.2.1
815
756
  License: Apache-2.0
816
757
 
817
758
  -------------------- DEPENDENCY DIVIDER --------------------
@@ -852,7 +793,9 @@ License: MIT
852
793
 
853
794
  -------------------- DEPENDENCY DIVIDER --------------------
854
795
 
855
- Name: chrome-devtools-frontend
796
+ Name: devtools-frontend
797
+ URL: https://github.com/ChromeDevTools/devtools-frontend
798
+ Version: 6fdb53e0f7a5cb1ebf16e40c45439a5c8007d9be
856
799
  License: Apache-2.0
857
800
 
858
801
  // Copyright 2014 The Chromium Authors
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "@modelcontextprotocol/sdk": "1.30.0",
3
- "core-js": "3.49.0",
4
- "debug": "4.4.3",
5
- "lighthouse": "13.4.0",
3
+ "core-js": "3.50.0",
4
+ "lighthouse": "13.4.1",
6
5
  "semver": "7.8.5",
7
6
  "yargs": "18.1.0",
8
- "puppeteer-core": "25.5.0"
7
+ "puppeteer-core": "25.8.0",
8
+ "https://github.com/ChromeDevTools/devtools-frontend": "6fdb53e0f7a5cb1ebf16e40c45439a5c8007d9be"
9
9
  }
@@ -5661,15 +5661,21 @@ class HeapSnapshotWorkerProxy extends ObjectWrapper {
5661
5661
  }
5662
5662
  evaluateForTest(script, callback) {
5663
5663
  const callId = this.nextCallId++;
5664
- this.callbacks.set(callId, callback);
5664
+ this.callbacks.set(callId, (error, result) => {
5665
+ callback(error, result);
5666
+ });
5665
5667
  this.postMessage({ callId, disposition: 'evaluateForTest', source: script });
5666
5668
  }
5667
5669
  callFactoryMethod(callback, objectId, methodName, proxyConstructor, transfer, ...methodArguments) {
5668
5670
  const callId = this.nextCallId++;
5669
5671
  const newObjectId = this.nextObjectId++;
5670
5672
  if (callback) {
5671
- this.callbacks.set(callId, remoteResult => {
5672
- callback(remoteResult ? new proxyConstructor(this, newObjectId) : null);
5673
+ this.callbacks.set(callId, (error, remoteResult) => {
5674
+ if (error) {
5675
+ callback(error);
5676
+ return;
5677
+ }
5678
+ callback(undefined, remoteResult ? new proxyConstructor(this, newObjectId) : undefined);
5673
5679
  });
5674
5680
  this.postMessage({
5675
5681
  callId,
@@ -5694,7 +5700,9 @@ class HeapSnapshotWorkerProxy extends ObjectWrapper {
5694
5700
  callMethod(callback, objectId, methodName, ...methodArguments) {
5695
5701
  const callId = this.nextCallId++;
5696
5702
  if (callback) {
5697
- this.callbacks.set(callId, callback);
5703
+ this.callbacks.set(callId, (error, result) => {
5704
+ callback(error, result);
5705
+ });
5698
5706
  }
5699
5707
  this.postMessage({
5700
5708
  callId,
@@ -5725,8 +5733,15 @@ class HeapSnapshotWorkerProxy extends ObjectWrapper {
5725
5733
  }
5726
5734
  setupForSecondaryInit(port) {
5727
5735
  const callId = this.nextCallId++;
5728
- const done = new Promise(resolve => {
5729
- this.callbacks.set(callId, resolve);
5736
+ const done = new Promise((resolve, reject) => {
5737
+ this.callbacks.set(callId, error => {
5738
+ if (error) {
5739
+ reject(new Error(error));
5740
+ }
5741
+ else {
5742
+ resolve();
5743
+ }
5744
+ });
5730
5745
  });
5731
5746
  this.postMessage({
5732
5747
  callId,
@@ -5746,15 +5761,17 @@ class HeapSnapshotWorkerProxy extends ObjectWrapper {
5746
5761
  if (data.error) {
5747
5762
  this.#console.error(`An error occurred when a call to method '${data.errorMethodName}' was requested`);
5748
5763
  this.#console.error(data['errorCallStack']);
5749
- this.callbacks.delete(data.callId);
5750
- return;
5751
5764
  }
5752
5765
  const callback = this.callbacks.get(data.callId);
5753
5766
  if (!callback) {
5754
5767
  return;
5755
5768
  }
5756
5769
  this.callbacks.delete(data.callId);
5757
- callback(data.result);
5770
+ if (data.error) {
5771
+ callback(data.error);
5772
+ return;
5773
+ }
5774
+ callback(undefined, data.result);
5758
5775
  }
5759
5776
  postMessage(message, transfer) {
5760
5777
  this.worker.postMessage(message, transfer);
@@ -5774,10 +5791,27 @@ class HeapSnapshotProxyObject {
5774
5791
  return this.worker.callFactoryMethod(null, String(this.objectId), methodName, proxyConstructor, [], ...args);
5775
5792
  }
5776
5793
  callFactoryMethodPromise(methodName, proxyConstructor, transfer, ...args) {
5777
- return new Promise(resolve => this.worker.callFactoryMethod(resolve, String(this.objectId), methodName, proxyConstructor, transfer, ...args));
5794
+ return new Promise((resolve, reject) => this.worker.callFactoryMethod((error, result) => {
5795
+ if (error) {
5796
+ reject(new Error(error));
5797
+ }
5798
+ else if (result) {
5799
+ resolve(result);
5800
+ }
5801
+ else {
5802
+ reject(new Error(`Failed to create ${proxyConstructor.name}`));
5803
+ }
5804
+ }, String(this.objectId), methodName, proxyConstructor, transfer, ...args));
5778
5805
  }
5779
5806
  callMethodPromise(methodName, ...args) {
5780
- return new Promise(resolve => this.worker.callMethod(resolve, String(this.objectId), methodName, ...args));
5807
+ return new Promise((resolve, reject) => this.worker.callMethod((error, result) => {
5808
+ if (error) {
5809
+ reject(new Error(error));
5810
+ }
5811
+ else {
5812
+ resolve(result);
5813
+ }
5814
+ }, String(this.objectId), methodName, ...args));
5781
5815
  }
5782
5816
  }
5783
5817
  class HeapSnapshotLoaderProxy extends HeapSnapshotProxyObject {
@@ -5796,12 +5830,16 @@ class HeapSnapshotLoaderProxy extends HeapSnapshotProxyObject {
5796
5830
  const secondWorker = new HeapSnapshotWorkerProxy(() => { }, this.worker.console, this.worker.workerUrl);
5797
5831
  const channel = new MessageChannel();
5798
5832
  await secondWorker.setupForSecondaryInit(channel.port2);
5799
- const snapshotProxy = await this.callFactoryMethodPromise('buildSnapshot', HeapSnapshotProxy, [channel.port1]);
5800
- secondWorker.dispose();
5801
- this.dispose();
5802
- snapshotProxy.setProfileUid(this.profileUid);
5803
- await snapshotProxy.updateStaticData();
5804
- this.snapshotReceivedCallback(snapshotProxy);
5833
+ try {
5834
+ const snapshotProxy = await this.callFactoryMethodPromise('buildSnapshot', HeapSnapshotProxy, [channel.port1]);
5835
+ snapshotProxy.setProfileUid(this.profileUid);
5836
+ await snapshotProxy.updateStaticData();
5837
+ this.snapshotReceivedCallback(snapshotProxy);
5838
+ }
5839
+ finally {
5840
+ secondWorker.dispose();
5841
+ this.dispose();
5842
+ }
5805
5843
  }
5806
5844
  }
5807
5845
  class HeapSnapshotProxy extends HeapSnapshotProxyObject {
@@ -5820,6 +5858,9 @@ class HeapSnapshotProxy extends HeapSnapshotProxyObject {
5820
5858
  getNativeContextSizes() {
5821
5859
  return this.callMethodPromise('getNativeContextSizes');
5822
5860
  }
5861
+ getRetainedByContextSummary() {
5862
+ return this.callMethodPromise('getRetainedByContextSummary');
5863
+ }
5823
5864
  aggregatesWithFilter(filter) {
5824
5865
  return this.callMethodPromise('aggregatesWithFilter', filter);
5825
5866
  }
@@ -5841,8 +5882,8 @@ class HeapSnapshotProxy extends HeapSnapshotProxyObject {
5841
5882
  getObjectInfo(nodeIndex) {
5842
5883
  return this.callMethodPromise('getObjectInfo', nodeIndex);
5843
5884
  }
5844
- createEdgesProvider(nodeIndex) {
5845
- return this.callFactoryMethod('createEdgesProvider', HeapSnapshotProviderProxy, nodeIndex);
5885
+ createEdgesProvider(nodeIndex, options) {
5886
+ return this.callFactoryMethod('createEdgesProvider', HeapSnapshotProviderProxy, nodeIndex, options);
5846
5887
  }
5847
5888
  createRetainingEdgesProvider(nodeIndex) {
5848
5889
  return this.callFactoryMethod('createRetainingEdgesProvider', HeapSnapshotProviderProxy, nodeIndex);
@@ -5859,6 +5900,9 @@ class HeapSnapshotProxy extends HeapSnapshotProxyObject {
5859
5900
  createNodesProviderForClass(classKey, nodeFilter) {
5860
5901
  return this.callFactoryMethod('createNodesProviderForClass', HeapSnapshotProviderProxy, classKey, nodeFilter);
5861
5902
  }
5903
+ queryObjects(queryOptions) {
5904
+ return this.callFactoryMethod('queryObjects', HeapSnapshotProviderProxy, queryOptions);
5905
+ }
5862
5906
  allocationTracesTops() {
5863
5907
  return this.callMethodPromise('allocationTracesTops');
5864
5908
  }
@@ -6586,21 +6630,10 @@ class HeapSnapshotNode {
6586
6630
  return false;
6587
6631
  }
6588
6632
  #detachednessAndClassIndex() {
6589
- const { snapshot, nodeIndex } = this;
6590
- const nodeDetachednessAndClassIndexOffset = snapshot.nodeDetachednessAndClassIndexOffset;
6591
- return nodeDetachednessAndClassIndexOffset !== -1 ?
6592
- snapshot.nodes.getValue(nodeIndex + nodeDetachednessAndClassIndexOffset) :
6593
- snapshot.detachednessAndClassIndexArray[nodeIndex / snapshot.nodeFieldCount];
6633
+ return this.snapshot.detachednessAndClassIndexArray[this.nodeIndex / this.snapshot.nodeFieldCount];
6594
6634
  }
6595
6635
  #setDetachednessAndClassIndex(value) {
6596
- const { snapshot, nodeIndex } = this;
6597
- const nodeDetachednessAndClassIndexOffset = snapshot.nodeDetachednessAndClassIndexOffset;
6598
- if (nodeDetachednessAndClassIndexOffset !== -1) {
6599
- snapshot.nodes.setValue(nodeIndex + nodeDetachednessAndClassIndexOffset, value);
6600
- }
6601
- else {
6602
- snapshot.detachednessAndClassIndexArray[nodeIndex / snapshot.nodeFieldCount] = value;
6603
- }
6636
+ this.snapshot.detachednessAndClassIndexArray[this.nodeIndex / this.snapshot.nodeFieldCount] = value;
6604
6637
  }
6605
6638
  detachedness() {
6606
6639
  return this.#detachednessAndClassIndex() & BITMASK_FOR_DOM_LINK_STATE;
@@ -6995,6 +7028,7 @@ class HeapSnapshot {
6995
7028
  this.#progress.updateStatus('Building retainers…');
6996
7029
  const resultsFromSecondWorker = this.startInitStep1InSecondThread(secondWorker);
6997
7030
  this.#progress.updateStatus('Propagating DOM state…');
7031
+ this.initDetachednessAndClassIndex();
6998
7032
  this.propagateDOMState();
6999
7033
  this.#progress.updateStatus('Calculating node flags…');
7000
7034
  this.calculateFlags();
@@ -8030,9 +8064,6 @@ class HeapSnapshot {
8030
8064
  }
8031
8065
  calculateObjectNames() {
8032
8066
  const { nodes, nodeCount, nodeNameOffset, nodeNativeType, nodeHiddenType, nodeObjectType, nodeCodeType, nodeClosureType, nodeRegExpType, } = this;
8033
- if (this.nodeDetachednessAndClassIndexOffset === -1) {
8034
- this.detachednessAndClassIndexArray = new Uint32Array(nodeCount);
8035
- }
8036
8067
  const stringTable = new Map();
8037
8068
  const getIndexForString = (s) => {
8038
8069
  let index = stringTable.get(s);
@@ -8243,6 +8274,43 @@ class HeapSnapshot {
8243
8274
  getNativeContextSizes() {
8244
8275
  return this.#nativeContextSizes;
8245
8276
  }
8277
+ getRetainedByContextSummary() {
8278
+ const isRetainedByContext = this.createNamedFilter('objectsRetainedByContexts');
8279
+ let contextCount = 0;
8280
+ let retainedByContextSize = 0;
8281
+ let retainedByContextCount = 0;
8282
+ let notRetainedByContextSize = 0;
8283
+ let notRetainedByContextCount = 0;
8284
+ const node = this.rootNode();
8285
+ const { nodes, nodeFieldCount, nodeSelfSizeOffset: selfSizeOffset } = this;
8286
+ const nodesLength = nodes.length;
8287
+ for (let nodeIndex = 0; nodeIndex < nodesLength; nodeIndex += nodeFieldCount) {
8288
+ const selfSize = nodes.getValue(nodeIndex + selfSizeOffset);
8289
+ if (!selfSize) {
8290
+ continue;
8291
+ }
8292
+ node.nodeIndex = nodeIndex;
8293
+ if (isRetainedByContext(node)) {
8294
+ retainedByContextCount++;
8295
+ retainedByContextSize += selfSize;
8296
+ }
8297
+ else {
8298
+ notRetainedByContextCount++;
8299
+ notRetainedByContextSize += selfSize;
8300
+ }
8301
+ if (this.isContextObject(node)) {
8302
+ contextCount++;
8303
+ }
8304
+ }
8305
+ return {
8306
+ contextCount,
8307
+ retainedByContextSize,
8308
+ retainedByContextCount,
8309
+ notRetainedByContextSize,
8310
+ notRetainedByContextCount,
8311
+ totalSize: retainedByContextSize + notRetainedByContextSize,
8312
+ };
8313
+ }
8246
8314
  nodeNativeContext(nodeIndex) {
8247
8315
  const ordinal = nodeIndex / this.nodeFieldCount;
8248
8316
  const nativeContextOrdinal = this.nodeNativeContextAttribution[ordinal];
@@ -8431,6 +8499,28 @@ class HeapSnapshot {
8431
8499
  }
8432
8500
  return null;
8433
8501
  }
8502
+ initDetachednessAndClassIndex() {
8503
+ this.detachednessAndClassIndexArray = new Uint32Array(this.nodeCount);
8504
+ if (this.nodeDetachednessAndClassIndexOffset !== -1) {
8505
+ const { nodeFieldCount, nodeDetachednessAndClassIndexOffset: offset } = this;
8506
+ for (let i = 0; i < this.nodeCount; ++i) {
8507
+ this.detachednessAndClassIndexArray[i] = Number(this.nodes.getValue(i * nodeFieldCount + offset));
8508
+ }
8509
+ }
8510
+ else {
8511
+ const node = this.rootNode();
8512
+ const nodesLength = this.nodes.length;
8513
+ const { nodeFieldCount, nodeNativeType, nodeTypeOffset } = this;
8514
+ for (let nodeIndex = 0; nodeIndex < nodesLength; nodeIndex += nodeFieldCount) {
8515
+ if (this.nodes.getValue(nodeIndex + nodeTypeOffset) === nodeNativeType) {
8516
+ node.nodeIndex = nodeIndex;
8517
+ if (node.name().startsWith('Detached ')) {
8518
+ node.setDetachedness(2 );
8519
+ }
8520
+ }
8521
+ }
8522
+ }
8523
+ }
8434
8524
  propagateDOMState() {
8435
8525
  if (this.nodeDetachednessAndClassIndexOffset === -1) {
8436
8526
  return;
@@ -8658,11 +8748,54 @@ class HeapSnapshot {
8658
8748
  }
8659
8749
  return null;
8660
8750
  }
8661
- createEdgesProvider(nodeIndex) {
8751
+ createEdgesProvider(nodeIndex, options) {
8662
8752
  const node = this.createNode(nodeIndex);
8663
- const filter = this.containmentEdgesFilter();
8753
+ const defaultFilter = this.containmentEdgesFilter();
8754
+ const minRetainedSize = options?.minRetainedSize;
8755
+ const excludePrimitives = options?.excludePrimitives ?? false;
8756
+ let filter = defaultFilter;
8757
+ if (minRetainedSize !== undefined || excludePrimitives) {
8758
+ filter = (edge) => {
8759
+ if (defaultFilter && !defaultFilter(edge)) {
8760
+ return false;
8761
+ }
8762
+ const targetNode = edge.node();
8763
+ if (minRetainedSize !== undefined && targetNode.retainedSize() < minRetainedSize) {
8764
+ return false;
8765
+ }
8766
+ if (excludePrimitives) {
8767
+ const rawType = targetNode.rawType();
8768
+ if (rawType === this.nodeNumberType) {
8769
+ return false;
8770
+ }
8771
+ if (rawType === this.nodeNativeType) {
8772
+ const targetName = targetNode.rawName();
8773
+ if (targetName === 'undefined' || targetName === 'null' || targetName === 'true' ||
8774
+ targetName === 'false') {
8775
+ return false;
8776
+ }
8777
+ }
8778
+ }
8779
+ return true;
8780
+ };
8781
+ }
8664
8782
  const indexProvider = new HeapSnapshotEdgeIndexProvider(this);
8665
- return new HeapSnapshotEdgesProvider(this, filter, node.edges(), indexProvider);
8783
+ const provider = new HeapSnapshotEdgesProvider(this, filter, node.edges(), indexProvider);
8784
+ if (options?.sortBy) {
8785
+ const sortBy = options.sortBy;
8786
+ let comparator;
8787
+ if (sortBy === 'selfSize') {
8788
+ comparator = new ComparatorConfig('selfSize', false, '!edgeName', true);
8789
+ }
8790
+ else if (sortBy === 'name') {
8791
+ comparator = new ComparatorConfig('!edgeName', true, 'retainedSize', false);
8792
+ }
8793
+ else {
8794
+ comparator = new ComparatorConfig('retainedSize', false, '!edgeName', true);
8795
+ }
8796
+ provider.sortAndRewind(comparator);
8797
+ }
8798
+ return provider;
8666
8799
  }
8667
8800
  createEdgesProviderForTest(nodeIndex, filter) {
8668
8801
  const node = this.createNode(nodeIndex);
@@ -8819,6 +8952,85 @@ class HeapSnapshot {
8819
8952
  createNodesProviderForClass(classKey, nodeFilter) {
8820
8953
  return new HeapSnapshotNodesProvider(this, this.aggregatesWithFilter(nodeFilter)[classKey].idxs);
8821
8954
  }
8955
+ queryObjects(queryOptions) {
8956
+ const { nodes, nodeFieldCount, retainedSizes } = this;
8957
+ const nodesLength = nodes.length;
8958
+ const matchingIndexes = [];
8959
+ const classNamePattern = queryOptions.className ? new RegExp(queryOptions.className, 'i') : null;
8960
+ const propertyNamePattern = queryOptions.propertyName ? new RegExp(queryOptions.propertyName, 'i') : null;
8961
+ const targetNodeType = queryOptions.nodeType ? queryOptions.nodeType.toLowerCase() : null;
8962
+ const node = this.rootNode();
8963
+ for (let nodeIndex = 0, ordinal = 0; nodeIndex < nodesLength; nodeIndex += nodeFieldCount, ordinal++) {
8964
+ node.nodeIndex = nodeIndex;
8965
+ if (queryOptions.minSelfSize !== undefined && node.selfSize() < queryOptions.minSelfSize) {
8966
+ continue;
8967
+ }
8968
+ if (queryOptions.maxSelfSize !== undefined && node.selfSize() > queryOptions.maxSelfSize) {
8969
+ continue;
8970
+ }
8971
+ const retainedSize = retainedSizes[ordinal];
8972
+ if (queryOptions.minRetainedSize !== undefined && retainedSize < queryOptions.minRetainedSize) {
8973
+ continue;
8974
+ }
8975
+ if (queryOptions.maxRetainedSize !== undefined && retainedSize > queryOptions.maxRetainedSize) {
8976
+ continue;
8977
+ }
8978
+ if (queryOptions.isDetached !== undefined) {
8979
+ const isDetached = node.detachedness() === 2 ;
8980
+ if (isDetached !== queryOptions.isDetached) {
8981
+ continue;
8982
+ }
8983
+ }
8984
+ if (classNamePattern) {
8985
+ const name = node.name();
8986
+ if (!classNamePattern.test(name)) {
8987
+ continue;
8988
+ }
8989
+ }
8990
+ if (targetNodeType) {
8991
+ const typeStr = node.type();
8992
+ if (typeStr.toLowerCase() !== targetNodeType) {
8993
+ continue;
8994
+ }
8995
+ }
8996
+ if (propertyNamePattern) {
8997
+ let propMatch = false;
8998
+ for (const iter = node.edges(); iter.hasNext(); iter.next()) {
8999
+ if (propertyNamePattern.test(iter.edge.name())) {
9000
+ propMatch = true;
9001
+ break;
9002
+ }
9003
+ }
9004
+ if (!propMatch) {
9005
+ continue;
9006
+ }
9007
+ }
9008
+ matchingIndexes.push(nodeIndex);
9009
+ }
9010
+ const sortBy = queryOptions.sortBy ?? 'retainedSize';
9011
+ if (sortBy === 'retainedSize') {
9012
+ matchingIndexes.sort((a, b) => retainedSizes[b / nodeFieldCount] - retainedSizes[a / nodeFieldCount]);
9013
+ }
9014
+ else if (sortBy === 'selfSize') {
9015
+ matchingIndexes.sort((a, b) => {
9016
+ node.nodeIndex = b;
9017
+ const sizeB = node.selfSize();
9018
+ node.nodeIndex = a;
9019
+ const sizeA = node.selfSize();
9020
+ return sizeB - sizeA;
9021
+ });
9022
+ }
9023
+ else if (sortBy === 'id') {
9024
+ matchingIndexes.sort((a, b) => {
9025
+ node.nodeIndex = b;
9026
+ const idB = node.id();
9027
+ node.nodeIndex = a;
9028
+ const idA = node.id();
9029
+ return idA - idB;
9030
+ });
9031
+ }
9032
+ return new HeapSnapshotNodesProvider(this, matchingIndexes);
9033
+ }
8822
9034
  maxJsNodeId() {
8823
9035
  const nodeFieldCount = this.nodeFieldCount;
8824
9036
  const nodes = this.nodes;
@@ -9326,7 +9538,7 @@ class JSHeapSnapshot extends HeapSnapshot {
9326
9538
  continue;
9327
9539
  }
9328
9540
  node.nodeIndex = nodeIndex;
9329
- if (node.name().startsWith('Detached ')) {
9541
+ if (node.detachedness() === 2 ) {
9330
9542
  this.flags[ordinal] |= flag;
9331
9543
  }
9332
9544
  }
@@ -9895,6 +10107,7 @@ class HeapSnapshotLoader {
9895
10107
  this.#dataCallback = null;
9896
10108
  this.#done = false;
9897
10109
  this.parsingComplete = this.#parseInput();
10110
+ this.parsingComplete.catch(() => { });
9898
10111
  }
9899
10112
  dispose() {
9900
10113
  this.#reset();
@@ -9907,6 +10120,7 @@ class HeapSnapshotLoader {
9907
10120
  this.#done = true;
9908
10121
  if (this.#dataCallback) {
9909
10122
  this.#dataCallback('');
10123
+ this.#dataCallback = null;
9910
10124
  }
9911
10125
  }
9912
10126
  async buildSnapshot(secondWorker) {
@@ -9974,6 +10188,9 @@ class HeapSnapshotLoader {
9974
10188
  this.#snapshot.strings = JSON.parse(this.#json);
9975
10189
  }
9976
10190
  write(chunk) {
10191
+ if (!chunk) {
10192
+ return;
10193
+ }
9977
10194
  this.#buffer.push(chunk);
9978
10195
  if (!this.#dataCallback) {
9979
10196
  return;
@@ -9985,6 +10202,9 @@ class HeapSnapshotLoader {
9985
10202
  if (this.#buffer.length > 0) {
9986
10203
  return Promise.resolve(this.#buffer.shift());
9987
10204
  }
10205
+ if (this.#done) {
10206
+ return Promise.resolve('');
10207
+ }
9988
10208
  const { promise, resolve } = Promise.withResolvers();
9989
10209
  this.#dataCallback = resolve;
9990
10210
  return promise;
@@ -9996,7 +10216,11 @@ class HeapSnapshotLoader {
9996
10216
  return pos;
9997
10217
  }
9998
10218
  startIndex = this.#json.length - token.length + 1;
9999
- this.#json += await this.#fetchChunk();
10219
+ const chunk = await this.#fetchChunk();
10220
+ if (!chunk) {
10221
+ throw new Error(`Token ${token} not found (unexpected end of input)`);
10222
+ }
10223
+ this.#json += chunk;
10000
10224
  }
10001
10225
  }
10002
10226
  async #parseArray(name, title, length) {
@@ -10013,7 +10237,11 @@ class HeapSnapshotLoader {
10013
10237
  else {
10014
10238
  this.#progress.updateStatus(title);
10015
10239
  }
10016
- this.#json += await this.#fetchChunk();
10240
+ const chunk = await this.#fetchChunk();
10241
+ if (!chunk) {
10242
+ throw new Error(`Unexpected end of input while ${title}`);
10243
+ }
10244
+ this.#json += chunk;
10017
10245
  }
10018
10246
  const result = this.#array;
10019
10247
  this.#array = null;
@@ -10036,7 +10264,11 @@ class HeapSnapshotLoader {
10036
10264
  });
10037
10265
  jsonTokenizer.write(json);
10038
10266
  while (!jsonTokenizerDone) {
10039
- jsonTokenizer.write(await this.#fetchChunk());
10267
+ const chunk = await this.#fetchChunk();
10268
+ if (!chunk) {
10269
+ throw new Error('Unexpected end of input while loading snapshot info');
10270
+ }
10271
+ jsonTokenizer.write(chunk);
10040
10272
  }
10041
10273
  this.#snapshot = this.#snapshot || {};
10042
10274
  const nodes = await this.#parseArray('"nodes"', 'Loading nodes… {PH1}%', this.#snapshot.snapshot.meta.node_fields.length * this.#snapshot.snapshot.node_count);