Skip to content

Commit 657b996

Browse files
authored
build: namespace devtools exports (ChromeDevTools#662)
1 parent 94b899c commit 657b996

File tree

14 files changed

+147
-139
lines changed

14 files changed

+147
-139
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"test:node20": "node --import ./build/tests/setup.js --test-reporter spec --test-force-exit --test build/tests",
2020
"test:no-build": "node --import ./build/tests/setup.js --no-warnings=ExperimentalWarning --experimental-print-required-tla --test-reporter spec --test-force-exit --test \"build/tests/**/*.test.js\"",
2121
"test": "npm run build && npm run test:no-build",
22-
"test:only": "npm run build && node --import ./build/tests/setup.js --no-warnings=ExperimentalWarning --test-reporter spec --test-force-exit --test --test-only \"build/tests/**/*.test.js\"",
22+
"test:only": "npm run build && npm run test:only:no-build",
2323
"test:only:no-build": "node --import ./build/tests/setup.js --no-warnings=ExperimentalWarning --test-reporter spec --test-force-exit --test --test-only \"build/tests/**/*.test.js\"",
2424
"test:update-snapshots": "npm run build && node --import ./build/tests/setup.js --no-warnings=ExperimentalWarning --test-force-exit --test --test-update-snapshots \"build/tests/**/*.test.js\"",
2525
"prepare": "node --experimental-strip-types scripts/prepare.ts",

src/DevToolsConnectionAdapter.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

7-
import type {CDPConnection as devtools} from './third_party/index.js';
87
import type * as puppeteer from './third_party/index.js';
8+
import type {DevTools} from './third_party/index.js';
99
import {CDPSessionEvent} from './third_party/index.js';
1010

1111
/**
@@ -16,9 +16,11 @@ import {CDPSessionEvent} from './third_party/index.js';
1616
* We don't have to recursively listen for 'sessionattached' as the "root" CDP session sees all child session attached
1717
* events, regardless how deeply nested they are.
1818
*/
19-
export class PuppeteerDevToolsConnection implements devtools.CDPConnection {
19+
export class PuppeteerDevToolsConnection
20+
implements DevTools.CDPConnection.CDPConnection
21+
{
2022
readonly #connection: puppeteer.Connection;
21-
readonly #observers = new Set<devtools.CDPConnectionObserver>();
23+
readonly #observers = new Set<DevTools.CDPConnection.CDPConnectionObserver>();
2224
readonly #sessionEventHandlers = new Map<
2325
string,
2426
puppeteer.Handler<unknown>
@@ -39,11 +41,14 @@ export class PuppeteerDevToolsConnection implements devtools.CDPConnection {
3941
this.#startForwardingCdpEvents(session);
4042
}
4143

42-
send<T extends devtools.Command>(
44+
send<T extends DevTools.CDPConnection.Command>(
4345
method: T,
44-
params: devtools.CommandParams<T>,
46+
params: DevTools.CDPConnection.CommandParams<T>,
4547
sessionId: string | undefined,
46-
): Promise<{result: devtools.CommandResult<T>} | {error: devtools.CDPError}> {
48+
): Promise<
49+
| {result: DevTools.CDPConnection.CommandResult<T>}
50+
| {error: DevTools.CDPConnection.CDPError}
51+
> {
4752
if (sessionId === undefined) {
4853
throw new Error(
4954
'Attempting to send on the root session. This must not happen',
@@ -62,11 +67,11 @@ export class PuppeteerDevToolsConnection implements devtools.CDPConnection {
6267
/* eslint-enable @typescript-eslint/no-explicit-any */
6368
}
6469

65-
observe(observer: devtools.CDPConnectionObserver): void {
70+
observe(observer: DevTools.CDPConnection.CDPConnectionObserver): void {
6671
this.#observers.add(observer);
6772
}
6873

69-
unobserve(observer: devtools.CDPConnectionObserver): void {
74+
unobserve(observer: DevTools.CDPConnection.CDPConnectionObserver): void {
7075
this.#observers.delete(observer);
7176
}
7277

@@ -98,7 +103,7 @@ export class PuppeteerDevToolsConnection implements devtools.CDPConnection {
98103
) {
99104
this.#observers.forEach(observer =>
100105
observer.onEvent({
101-
method: type as devtools.Event,
106+
method: type as DevTools.CDPConnection.Event,
102107
sessionId,
103108
params: event,
104109
}),

src/DevtoolsUtils.ts

Lines changed: 26 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,7 @@ import {PuppeteerDevToolsConnection} from './DevToolsConnectionAdapter.js';
88
import {ISSUE_UTILS} from './issue-descriptions.js';
99
import {logger} from './logger.js';
1010
import {Mutex} from './Mutex.js';
11-
import {
12-
type Issue,
13-
type AggregatedIssue,
14-
type IssuesManagerEventTypes,
15-
type SDKTarget as Target,
16-
DebuggerModel,
17-
Foundation,
18-
TargetManager,
19-
MarkdownIssueDescription,
20-
Marked,
21-
ProtocolClient,
22-
Common,
23-
I18n,
24-
} from './third_party/index.js';
11+
import {DevTools} from './third_party/index.js';
2512
import type {
2613
Browser,
2714
Page,
@@ -87,14 +74,14 @@ function normalizeUrl(url: string): string {
8774
* A mock implementation of an issues manager that only implements the methods
8875
* that are actually used by the IssuesAggregator
8976
*/
90-
export class FakeIssuesManager extends Common.ObjectWrapper
91-
.ObjectWrapper<IssuesManagerEventTypes> {
92-
issues(): Issue[] {
77+
export class FakeIssuesManager extends DevTools.Common.ObjectWrapper
78+
.ObjectWrapper<DevTools.IssuesManagerEventTypes> {
79+
issues(): DevTools.Issue[] {
9380
return [];
9481
}
9582
}
9683

97-
export function mapIssueToMessageObject(issue: AggregatedIssue) {
84+
export function mapIssueToMessageObject(issue: DevTools.AggregatedIssue) {
9885
const count = issue.getAggregatedIssuesCount();
9986
const markdownDescription = issue.getDescription();
10087
const filename = markdownDescription?.file;
@@ -113,12 +100,14 @@ export function mapIssueToMessageObject(issue: AggregatedIssue) {
113100
let title: string | null;
114101

115102
try {
116-
processedMarkdown = MarkdownIssueDescription.substitutePlaceholders(
117-
rawMarkdown,
118-
markdownDescription.substitutions,
119-
);
120-
const markdownAst = Marked.Marked.lexer(processedMarkdown);
121-
title = MarkdownIssueDescription.findTitleFromMarkdownAst(markdownAst);
103+
processedMarkdown =
104+
DevTools.MarkdownIssueDescription.substitutePlaceholders(
105+
rawMarkdown,
106+
markdownDescription.substitutions,
107+
);
108+
const markdownAst = DevTools.Marked.Marked.lexer(processedMarkdown);
109+
title =
110+
DevTools.MarkdownIssueDescription.findTitleFromMarkdownAst(markdownAst);
122111
} catch {
123112
logger('error parsing markdown for issue ' + issue.code());
124113
return null;
@@ -137,22 +126,22 @@ export function mapIssueToMessageObject(issue: AggregatedIssue) {
137126
}
138127

139128
// DevTools CDP errors can get noisy.
140-
ProtocolClient.InspectorBackend.test.suppressRequestErrors = true;
129+
DevTools.ProtocolClient.InspectorBackend.test.suppressRequestErrors = true;
141130

142-
I18n.DevToolsLocale.DevToolsLocale.instance({
131+
DevTools.I18n.DevToolsLocale.DevToolsLocale.instance({
143132
create: true,
144133
data: {
145134
navigatorLanguage: 'en-US',
146135
settingLanguage: 'en-US',
147136
lookupClosestDevToolsLocale: l => l,
148137
},
149138
});
150-
I18n.i18n.registerLocaleDataForTest('en-US', {});
139+
DevTools.I18n.i18n.registerLocaleDataForTest('en-US', {});
151140

152141
export interface TargetUniverse {
153142
/** The DevTools target corresponding to the puppeteer Page */
154-
target: Target;
155-
universe: Foundation.Universe.Universe;
143+
target: DevTools.SDKTarget;
144+
universe: DevTools.Foundation.Universe.Universe;
156145
}
157146
export type TargetUniverseFactoryFn = (page: Page) => Promise<TargetUniverse>;
158147

@@ -231,22 +220,23 @@ export class UniverseManager {
231220
}
232221

233222
const DEFAULT_FACTORY: TargetUniverseFactoryFn = async (page: Page) => {
234-
const settingStorage = new Common.Settings.SettingsStorage({});
235-
const universe = new Foundation.Universe.Universe({
223+
const settingStorage = new DevTools.Common.Settings.SettingsStorage({});
224+
const universe = new DevTools.Foundation.Universe.Universe({
236225
settingsCreationOptions: {
237226
syncedStorage: settingStorage,
238227
globalStorage: settingStorage,
239228
localStorage: settingStorage,
240-
settingRegistrations: Common.SettingRegistration.getRegisteredSettings(),
229+
settingRegistrations:
230+
DevTools.Common.SettingRegistration.getRegisteredSettings(),
241231
},
242-
overrideAutoStartModels: new Set([DebuggerModel]),
232+
overrideAutoStartModels: new Set([DevTools.DebuggerModel]),
243233
});
244234

245235
const session = await page.createCDPSession();
246236
const connection = new PuppeteerDevToolsConnection(session);
247237

248-
const targetManager = universe.context.get(TargetManager);
249-
targetManager.observeModels(DebuggerModel, SKIP_ALL_PAUSES);
238+
const targetManager = universe.context.get(DevTools.TargetManager);
239+
targetManager.observeModels(DevTools.DebuggerModel, SKIP_ALL_PAUSES);
250240

251241
const target = targetManager.createTarget(
252242
'main',
@@ -266,7 +256,7 @@ const DEFAULT_FACTORY: TargetUniverseFactoryFn = async (page: Page) => {
266256
// sent. This means DevTools can still pause, step and do whatever. We just won't
267257
// see the `Debugger.paused`/`Debugger.resumed` events on the MCP side.
268258
const SKIP_ALL_PAUSES = {
269-
modelAdded(model: DebuggerModel): void {
259+
modelAdded(model: DevTools.DebuggerModel): void {
270260
void model.agent.invoke_setSkipAllPauses({skip: true});
271261
},
272262

src/McpContext.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import path from 'node:path';
1111
import {extractUrlLikeFromDevToolsTitle, urlsEqual} from './DevtoolsUtils.js';
1212
import type {ListenerMap} from './PageCollector.js';
1313
import {NetworkCollector, ConsoleCollector} from './PageCollector.js';
14-
import {type AggregatedIssue} from './third_party/index.js';
1514
import {Locator} from './third_party/index.js';
15+
import type {DevTools} from './third_party/index.js';
1616
import type {
1717
Browser,
1818
ConsoleMessage,
@@ -221,18 +221,20 @@ export class McpContext implements Context {
221221

222222
getConsoleData(
223223
includePreservedMessages?: boolean,
224-
): Array<ConsoleMessage | Error | AggregatedIssue> {
224+
): Array<ConsoleMessage | Error | DevTools.AggregatedIssue> {
225225
const page = this.getSelectedPage();
226226
return this.#consoleCollector.getData(page, includePreservedMessages);
227227
}
228228

229229
getConsoleMessageStableId(
230-
message: ConsoleMessage | Error | AggregatedIssue,
230+
message: ConsoleMessage | Error | DevTools.AggregatedIssue,
231231
): number {
232232
return this.#consoleCollector.getIdForResource(message);
233233
}
234234

235-
getConsoleMessageById(id: number): ConsoleMessage | Error | AggregatedIssue {
235+
getConsoleMessageById(
236+
id: number,
237+
): ConsoleMessage | Error | DevTools.AggregatedIssue {
236238
return this.#consoleCollector.getById(this.getSelectedPage(), id);
237239
}
238240

src/McpResponse.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {
1919
} from './formatters/networkFormatter.js';
2020
import {formatSnapshotNode} from './formatters/snapshotFormatter.js';
2121
import type {McpContext} from './McpContext.js';
22-
import {AggregatedIssue} from './third_party/index.js';
22+
import {DevTools} from './third_party/index.js';
2323
import type {
2424
ConsoleMessage,
2525
ImageContent,
@@ -250,7 +250,7 @@ export class McpResponse implements Response {
250250
}),
251251
),
252252
};
253-
} else if (message instanceof AggregatedIssue) {
253+
} else if (message instanceof DevTools.AggregatedIssue) {
254254
const mappedIssueMessage = mapIssueToMessageObject(message);
255255
if (!mappedIssueMessage)
256256
throw new Error(
@@ -282,7 +282,7 @@ export class McpResponse implements Response {
282282
if ('type' in message) {
283283
return normalizedTypes.has(message.type());
284284
}
285-
if (message instanceof AggregatedIssue) {
285+
if (message instanceof DevTools.AggregatedIssue) {
286286
return normalizedTypes.has('issue');
287287
}
288288
return normalizedTypes.has('error');
@@ -312,7 +312,7 @@ export class McpResponse implements Response {
312312
),
313313
};
314314
}
315-
if (item instanceof AggregatedIssue) {
315+
if (item instanceof DevTools.AggregatedIssue) {
316316
const mappedIssueMessage = mapIssueToMessageObject(item);
317317
if (!mappedIssueMessage) return null;
318318
return {

src/PageCollector.ts

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,12 @@
77
import {FakeIssuesManager} from './DevtoolsUtils.js';
88
import {logger} from './logger.js';
99
import type {
10+
Target,
1011
CDPSession,
1112
ConsoleMessage,
1213
Protocol,
13-
Target,
14-
Common,
15-
} from './third_party/index.js';
16-
import {
17-
type AggregatedIssue,
18-
IssueAggregatorEvents,
19-
IssuesManagerEvents,
20-
createIssuesFromProtocolIssue,
21-
IssueAggregator,
2214
} from './third_party/index.js';
15+
import {DevTools} from './third_party/index.js';
2316
import {
2417
type Browser,
2518
type Frame,
@@ -30,7 +23,7 @@ import {
3023
} from './third_party/index.js';
3124

3225
interface PageEvents extends PuppeteerPageEvents {
33-
issue: AggregatedIssue;
26+
issue: DevTools.AggregatedIssue;
3427
}
3528

3629
export type ListenerMap<EventMap extends PageEvents = PageEvents> = {
@@ -218,7 +211,7 @@ export class PageCollector<T> {
218211
}
219212

220213
export class ConsoleCollector extends PageCollector<
221-
ConsoleMessage | Error | AggregatedIssue
214+
ConsoleMessage | Error | DevTools.AggregatedIssue
222215
> {
223216
#subscribedPages = new WeakMap<Page, PageIssueSubscriber>();
224217

@@ -240,9 +233,9 @@ export class ConsoleCollector extends PageCollector<
240233

241234
class PageIssueSubscriber {
242235
#issueManager = new FakeIssuesManager();
243-
#issueAggregator = new IssueAggregator(this.#issueManager);
236+
#issueAggregator = new DevTools.IssueAggregator(this.#issueManager);
244237
#seenKeys = new Set<string>();
245-
#seenIssues = new Set<AggregatedIssue>();
238+
#seenIssues = new Set<DevTools.AggregatedIssue>();
246239
#page: Page;
247240
#session: CDPSession;
248241

@@ -256,14 +249,13 @@ class PageIssueSubscriber {
256249
this.#issueManager = new FakeIssuesManager();
257250
if (this.#issueAggregator) {
258251
this.#issueAggregator.removeEventListener(
259-
IssueAggregatorEvents.AGGREGATED_ISSUE_UPDATED,
252+
DevTools.IssueAggregatorEvents.AGGREGATED_ISSUE_UPDATED,
260253
this.#onAggregatedissue,
261254
);
262255
}
263-
this.#issueAggregator = new IssueAggregator(this.#issueManager);
264-
256+
this.#issueAggregator = new DevTools.IssueAggregator(this.#issueManager);
265257
this.#issueAggregator.addEventListener(
266-
IssueAggregatorEvents.AGGREGATED_ISSUE_UPDATED,
258+
DevTools.IssueAggregatorEvents.AGGREGATED_ISSUE_UPDATED,
267259
this.#onAggregatedissue,
268260
);
269261
}
@@ -286,7 +278,7 @@ class PageIssueSubscriber {
286278
this.#session.off('Audits.issueAdded', this.#onIssueAdded);
287279
if (this.#issueAggregator) {
288280
this.#issueAggregator.removeEventListener(
289-
IssueAggregatorEvents.AGGREGATED_ISSUE_UPDATED,
281+
DevTools.IssueAggregatorEvents.AGGREGATED_ISSUE_UPDATED,
290282
this.#onAggregatedissue,
291283
);
292284
}
@@ -296,7 +288,7 @@ class PageIssueSubscriber {
296288
}
297289

298290
#onAggregatedissue = (
299-
event: Common.EventTarget.EventTargetEvent<AggregatedIssue>,
291+
event: DevTools.Common.EventTarget.EventTargetEvent<DevTools.AggregatedIssue>,
300292
) => {
301293
if (this.#seenIssues.has(event.data)) {
302294
return;
@@ -319,9 +311,11 @@ class PageIssueSubscriber {
319311
#onIssueAdded = (data: Protocol.Audits.IssueAddedEvent) => {
320312
try {
321313
const inspectorIssue = data.issue;
322-
// @ts-expect-error Types of protocol from Puppeteer and CDP are
323-
// incomparable for InspectorIssueCode, one is union, other is enum.
324-
const issue = createIssuesFromProtocolIssue(null, inspectorIssue)[0];
314+
const issue = DevTools.createIssuesFromProtocolIssue(
315+
null,
316+
// @ts-expect-error Protocol types diverge.
317+
inspectorIssue,
318+
)[0];
325319
if (!issue) {
326320
logger('No issue mapping for for the issue: ', inspectorIssue.code);
327321
return;
@@ -333,7 +327,7 @@ class PageIssueSubscriber {
333327
}
334328
this.#seenKeys.add(primaryKey);
335329
this.#issueManager.dispatchEventToListeners(
336-
IssuesManagerEvents.ISSUE_ADDED,
330+
DevTools.IssuesManagerEvents.ISSUE_ADDED,
337331
{
338332
issue,
339333
// @ts-expect-error We don't care that issues model is null

0 commit comments

Comments
 (0)