豆豆友情提示:这是一个非官方 GitHub 代理镜像,主要用于网络测试或访问加速。请勿在此进行登录、注册或处理任何敏感信息。进行这些操作请务必访问官方网站 github.com。 Raw 内容也通过此代理提供。
Skip to content

Commit 4aa2419

Browse files
committed
build: bundle devtools frontend (#656)
1 parent 94b899c commit 4aa2419

File tree

14 files changed

+180
-79
lines changed

14 files changed

+180
-79
lines changed

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@
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+
<<<<<<< HEAD
2223
"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\"",
24+
=======
25+
"test:only": "npm run build && npm run test:only:no-build",
26+
>>>>>>> 8e5e779 (build: bundle devtools frontend (#656))
2327
"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\"",
2428
"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\"",
2529
"prepare": "node --experimental-strip-types scripts/prepare.ts",

src/DevToolsConnectionAdapter.ts

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

7+
<<<<<<< HEAD
78
import type {CDPConnection as devtools} from './third_party/index.js';
9+
=======
10+
>>>>>>> 8e5e779 (build: bundle devtools frontend (#656))
811
import type * as puppeteer from './third_party/index.js';
12+
import type {DevTools} from './third_party/index.js';
913
import {CDPSessionEvent} from './third_party/index.js';
1014

1115
/**
@@ -16,9 +20,11 @@ import {CDPSessionEvent} from './third_party/index.js';
1620
* We don't have to recursively listen for 'sessionattached' as the "root" CDP session sees all child session attached
1721
* events, regardless how deeply nested they are.
1822
*/
19-
export class PuppeteerDevToolsConnection implements devtools.CDPConnection {
23+
export class PuppeteerDevToolsConnection
24+
implements DevTools.CDPConnection.CDPConnection
25+
{
2026
readonly #connection: puppeteer.Connection;
21-
readonly #observers = new Set<devtools.CDPConnectionObserver>();
27+
readonly #observers = new Set<DevTools.CDPConnection.CDPConnectionObserver>();
2228
readonly #sessionEventHandlers = new Map<
2329
string,
2430
puppeteer.Handler<unknown>
@@ -39,11 +45,14 @@ export class PuppeteerDevToolsConnection implements devtools.CDPConnection {
3945
this.#startForwardingCdpEvents(session);
4046
}
4147

42-
send<T extends devtools.Command>(
48+
send<T extends DevTools.CDPConnection.Command>(
4349
method: T,
44-
params: devtools.CommandParams<T>,
50+
params: DevTools.CDPConnection.CommandParams<T>,
4551
sessionId: string | undefined,
46-
): Promise<{result: devtools.CommandResult<T>} | {error: devtools.CDPError}> {
52+
): Promise<
53+
| {result: DevTools.CDPConnection.CommandResult<T>}
54+
| {error: DevTools.CDPConnection.CDPError}
55+
> {
4756
if (sessionId === undefined) {
4857
throw new Error(
4958
'Attempting to send on the root session. This must not happen',
@@ -62,11 +71,11 @@ export class PuppeteerDevToolsConnection implements devtools.CDPConnection {
6271
/* eslint-enable @typescript-eslint/no-explicit-any */
6372
}
6473

65-
observe(observer: devtools.CDPConnectionObserver): void {
74+
observe(observer: DevTools.CDPConnection.CDPConnectionObserver): void {
6675
this.#observers.add(observer);
6776
}
6877

69-
unobserve(observer: devtools.CDPConnectionObserver): void {
78+
unobserve(observer: DevTools.CDPConnection.CDPConnectionObserver): void {
7079
this.#observers.delete(observer);
7180
}
7281

@@ -98,7 +107,7 @@ export class PuppeteerDevToolsConnection implements devtools.CDPConnection {
98107
) {
99108
this.#observers.forEach(observer =>
100109
observer.onEvent({
101-
method: type as devtools.Event,
110+
method: type as DevTools.CDPConnection.Event,
102111
sessionId,
103112
params: event,
104113
}),

src/DevtoolsUtils.ts

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +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+
<<<<<<< HEAD
1112
import {
1213
type Issue,
1314
type AggregatedIssue,
@@ -22,6 +23,9 @@ import {
2223
Common,
2324
I18n,
2425
} from './third_party/index.js';
26+
=======
27+
import {DevTools} from './third_party/index.js';
28+
>>>>>>> 8e5e779 (build: bundle devtools frontend (#656))
2529
import type {
2630
Browser,
2731
Page,
@@ -87,14 +91,14 @@ function normalizeUrl(url: string): string {
8791
* A mock implementation of an issues manager that only implements the methods
8892
* that are actually used by the IssuesAggregator
8993
*/
90-
export class FakeIssuesManager extends Common.ObjectWrapper
91-
.ObjectWrapper<IssuesManagerEventTypes> {
92-
issues(): Issue[] {
94+
export class FakeIssuesManager extends DevTools.Common.ObjectWrapper
95+
.ObjectWrapper<DevTools.IssuesManagerEventTypes> {
96+
issues(): DevTools.Issue[] {
9397
return [];
9498
}
9599
}
96100

97-
export function mapIssueToMessageObject(issue: AggregatedIssue) {
101+
export function mapIssueToMessageObject(issue: DevTools.AggregatedIssue) {
98102
const count = issue.getAggregatedIssuesCount();
99103
const markdownDescription = issue.getDescription();
100104
const filename = markdownDescription?.file;
@@ -113,12 +117,14 @@ export function mapIssueToMessageObject(issue: AggregatedIssue) {
113117
let title: string | null;
114118

115119
try {
116-
processedMarkdown = MarkdownIssueDescription.substitutePlaceholders(
117-
rawMarkdown,
118-
markdownDescription.substitutions,
119-
);
120-
const markdownAst = Marked.Marked.lexer(processedMarkdown);
121-
title = MarkdownIssueDescription.findTitleFromMarkdownAst(markdownAst);
120+
processedMarkdown =
121+
DevTools.MarkdownIssueDescription.substitutePlaceholders(
122+
rawMarkdown,
123+
markdownDescription.substitutions,
124+
);
125+
const markdownAst = DevTools.Marked.Marked.lexer(processedMarkdown);
126+
title =
127+
DevTools.MarkdownIssueDescription.findTitleFromMarkdownAst(markdownAst);
122128
} catch {
123129
logger('error parsing markdown for issue ' + issue.code());
124130
return null;
@@ -137,22 +143,22 @@ export function mapIssueToMessageObject(issue: AggregatedIssue) {
137143
}
138144

139145
// DevTools CDP errors can get noisy.
140-
ProtocolClient.InspectorBackend.test.suppressRequestErrors = true;
146+
DevTools.ProtocolClient.InspectorBackend.test.suppressRequestErrors = true;
141147

142-
I18n.DevToolsLocale.DevToolsLocale.instance({
148+
DevTools.I18n.DevToolsLocale.DevToolsLocale.instance({
143149
create: true,
144150
data: {
145151
navigatorLanguage: 'en-US',
146152
settingLanguage: 'en-US',
147153
lookupClosestDevToolsLocale: l => l,
148154
},
149155
});
150-
I18n.i18n.registerLocaleDataForTest('en-US', {});
156+
DevTools.I18n.i18n.registerLocaleDataForTest('en-US', {});
151157

152158
export interface TargetUniverse {
153159
/** The DevTools target corresponding to the puppeteer Page */
154-
target: Target;
155-
universe: Foundation.Universe.Universe;
160+
target: DevTools.SDKTarget;
161+
universe: DevTools.Foundation.Universe.Universe;
156162
}
157163
export type TargetUniverseFactoryFn = (page: Page) => Promise<TargetUniverse>;
158164

@@ -231,22 +237,23 @@ export class UniverseManager {
231237
}
232238

233239
const DEFAULT_FACTORY: TargetUniverseFactoryFn = async (page: Page) => {
234-
const settingStorage = new Common.Settings.SettingsStorage({});
235-
const universe = new Foundation.Universe.Universe({
240+
const settingStorage = new DevTools.Common.Settings.SettingsStorage({});
241+
const universe = new DevTools.Foundation.Universe.Universe({
236242
settingsCreationOptions: {
237243
syncedStorage: settingStorage,
238244
globalStorage: settingStorage,
239245
localStorage: settingStorage,
240-
settingRegistrations: Common.SettingRegistration.getRegisteredSettings(),
246+
settingRegistrations:
247+
DevTools.Common.SettingRegistration.getRegisteredSettings(),
241248
},
242-
overrideAutoStartModels: new Set([DebuggerModel]),
249+
overrideAutoStartModels: new Set([DevTools.DebuggerModel]),
243250
});
244251

245252
const session = await page.createCDPSession();
246253
const connection = new PuppeteerDevToolsConnection(session);
247254

248-
const targetManager = universe.context.get(TargetManager);
249-
targetManager.observeModels(DebuggerModel, SKIP_ALL_PAUSES);
255+
const targetManager = universe.context.get(DevTools.TargetManager);
256+
targetManager.observeModels(DevTools.DebuggerModel, SKIP_ALL_PAUSES);
250257

251258
const target = targetManager.createTarget(
252259
'main',
@@ -266,7 +273,7 @@ const DEFAULT_FACTORY: TargetUniverseFactoryFn = async (page: Page) => {
266273
// sent. This means DevTools can still pause, step and do whatever. We just won't
267274
// see the `Debugger.paused`/`Debugger.resumed` events on the MCP side.
268275
const SKIP_ALL_PAUSES = {
269-
modelAdded(model: DebuggerModel): void {
276+
modelAdded(model: DevTools.DebuggerModel): void {
270277
void model.agent.invoke_setSkipAllPauses({skip: true});
271278
},
272279

src/McpContext.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import type {ListenerMap} from './PageCollector.js';
1313
import {NetworkCollector, ConsoleCollector} from './PageCollector.js';
1414
import {type AggregatedIssue} from './third_party/index.js';
1515
import {Locator} from './third_party/index.js';
16+
import type {DevTools} from './third_party/index.js';
1617
import type {
1718
Browser,
1819
ConsoleMessage,
@@ -221,18 +222,20 @@ export class McpContext implements Context {
221222

222223
getConsoleData(
223224
includePreservedMessages?: boolean,
224-
): Array<ConsoleMessage | Error | AggregatedIssue> {
225+
): Array<ConsoleMessage | Error | DevTools.AggregatedIssue> {
225226
const page = this.getSelectedPage();
226227
return this.#consoleCollector.getData(page, includePreservedMessages);
227228
}
228229

229230
getConsoleMessageStableId(
230-
message: ConsoleMessage | Error | AggregatedIssue,
231+
message: ConsoleMessage | Error | DevTools.AggregatedIssue,
231232
): number {
232233
return this.#consoleCollector.getIdForResource(message);
233234
}
234235

235-
getConsoleMessageById(id: number): ConsoleMessage | Error | AggregatedIssue {
236+
getConsoleMessageById(
237+
id: number,
238+
): ConsoleMessage | Error | DevTools.AggregatedIssue {
236239
return this.#consoleCollector.getById(this.getSelectedPage(), id);
237240
}
238241

src/McpResponse.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ import {
1919
} from './formatters/networkFormatter.js';
2020
import {formatSnapshotNode} from './formatters/snapshotFormatter.js';
2121
import type {McpContext} from './McpContext.js';
22+
<<<<<<< HEAD
2223
import {AggregatedIssue} from './third_party/index.js';
24+
=======
25+
import {DevTools} from './third_party/index.js';
26+
>>>>>>> 8e5e779 (build: bundle devtools frontend (#656))
2327
import type {
2428
ConsoleMessage,
2529
ImageContent,
@@ -250,7 +254,7 @@ export class McpResponse implements Response {
250254
}),
251255
),
252256
};
253-
} else if (message instanceof AggregatedIssue) {
257+
} else if (message instanceof DevTools.AggregatedIssue) {
254258
const mappedIssueMessage = mapIssueToMessageObject(message);
255259
if (!mappedIssueMessage)
256260
throw new Error(
@@ -282,7 +286,7 @@ export class McpResponse implements Response {
282286
if ('type' in message) {
283287
return normalizedTypes.has(message.type());
284288
}
285-
if (message instanceof AggregatedIssue) {
289+
if (message instanceof DevTools.AggregatedIssue) {
286290
return normalizedTypes.has('issue');
287291
}
288292
return normalizedTypes.has('error');
@@ -312,7 +316,7 @@ export class McpResponse implements Response {
312316
),
313317
};
314318
}
315-
if (item instanceof AggregatedIssue) {
319+
if (item instanceof DevTools.AggregatedIssue) {
316320
const mappedIssueMessage = mapIssueToMessageObject(item);
317321
if (!mappedIssueMessage) return null;
318322
return {

src/PageCollector.ts

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
createIssuesFromProtocolIssue,
2121
IssueAggregator,
2222
} from './third_party/index.js';
23+
import {DevTools} from './third_party/index.js';
2324
import {
2425
type Browser,
2526
type Frame,
@@ -30,7 +31,7 @@ import {
3031
} from './third_party/index.js';
3132

3233
interface PageEvents extends PuppeteerPageEvents {
33-
issue: AggregatedIssue;
34+
issue: DevTools.AggregatedIssue;
3435
}
3536

3637
export type ListenerMap<EventMap extends PageEvents = PageEvents> = {
@@ -218,7 +219,7 @@ export class PageCollector<T> {
218219
}
219220

220221
export class ConsoleCollector extends PageCollector<
221-
ConsoleMessage | Error | AggregatedIssue
222+
ConsoleMessage | Error | DevTools.AggregatedIssue
222223
> {
223224
#subscribedPages = new WeakMap<Page, PageIssueSubscriber>();
224225

@@ -240,9 +241,9 @@ export class ConsoleCollector extends PageCollector<
240241

241242
class PageIssueSubscriber {
242243
#issueManager = new FakeIssuesManager();
243-
#issueAggregator = new IssueAggregator(this.#issueManager);
244+
#issueAggregator = new DevTools.IssueAggregator(this.#issueManager);
244245
#seenKeys = new Set<string>();
245-
#seenIssues = new Set<AggregatedIssue>();
246+
#seenIssues = new Set<DevTools.AggregatedIssue>();
246247
#page: Page;
247248
#session: CDPSession;
248249

@@ -256,14 +257,13 @@ class PageIssueSubscriber {
256257
this.#issueManager = new FakeIssuesManager();
257258
if (this.#issueAggregator) {
258259
this.#issueAggregator.removeEventListener(
259-
IssueAggregatorEvents.AGGREGATED_ISSUE_UPDATED,
260+
DevTools.IssueAggregatorEvents.AGGREGATED_ISSUE_UPDATED,
260261
this.#onAggregatedissue,
261262
);
262263
}
263-
this.#issueAggregator = new IssueAggregator(this.#issueManager);
264-
264+
this.#issueAggregator = new DevTools.IssueAggregator(this.#issueManager);
265265
this.#issueAggregator.addEventListener(
266-
IssueAggregatorEvents.AGGREGATED_ISSUE_UPDATED,
266+
DevTools.IssueAggregatorEvents.AGGREGATED_ISSUE_UPDATED,
267267
this.#onAggregatedissue,
268268
);
269269
}
@@ -286,7 +286,7 @@ class PageIssueSubscriber {
286286
this.#session.off('Audits.issueAdded', this.#onIssueAdded);
287287
if (this.#issueAggregator) {
288288
this.#issueAggregator.removeEventListener(
289-
IssueAggregatorEvents.AGGREGATED_ISSUE_UPDATED,
289+
DevTools.IssueAggregatorEvents.AGGREGATED_ISSUE_UPDATED,
290290
this.#onAggregatedissue,
291291
);
292292
}
@@ -296,7 +296,7 @@ class PageIssueSubscriber {
296296
}
297297

298298
#onAggregatedissue = (
299-
event: Common.EventTarget.EventTargetEvent<AggregatedIssue>,
299+
event: DevTools.Common.EventTarget.EventTargetEvent<DevTools.AggregatedIssue>,
300300
) => {
301301
if (this.#seenIssues.has(event.data)) {
302302
return;
@@ -319,9 +319,11 @@ class PageIssueSubscriber {
319319
#onIssueAdded = (data: Protocol.Audits.IssueAddedEvent) => {
320320
try {
321321
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];
322+
const issue = DevTools.createIssuesFromProtocolIssue(
323+
null,
324+
// @ts-expect-error Protocol types diverge.
325+
inspectorIssue,
326+
)[0];
325327
if (!issue) {
326328
logger('No issue mapping for for the issue: ', inspectorIssue.code);
327329
return;
@@ -333,7 +335,7 @@ class PageIssueSubscriber {
333335
}
334336
this.#seenKeys.add(primaryKey);
335337
this.#issueManager.dispatchEventToListeners(
336-
IssuesManagerEvents.ISSUE_ADDED,
338+
DevTools.IssuesManagerEvents.ISSUE_ADDED,
337339
{
338340
issue,
339341
// @ts-expect-error We don't care that issues model is null

0 commit comments

Comments
 (0)