Skip to content

Commit 71098b0

Browse files
authored
refactor(tools): import logpoints without the browser bundle (#3525)
Refs #3427 Lazy loading part 9 of 11, stacked on #3524. The server still loads `third_party/browser.js` at startup through `index.ts`, `McpContext` and `McpResponse`, until a follow-up PR loads it on the first tool call. ## What moved - The `set_logpoints` schema description uses `LOGPOINT_FORMAT`, so a lazy import is not enough. The browser-free parts of `LogpointManager.ts` move unchanged to a new `src/utils/logpoint.ts`: `LOGPOINT_FORMAT`, `parseLogpoint()`, `LogpointError` and the `LogpointOptions`, `Logpoint` and `GeneratedLocation` interfaces. - `LogpointManager.ts`, `McpPage.ts`, `formatters/LogpointFormatter.ts`, `tools/ToolDefinition.ts`, `tools/logpoints.ts` and the tests import them from there. There are no re-exports. Adds `tools/logpoints.js` to the light entries in `tests/browser-imports.test.ts`. ## Modules now in both bundles None. ## Test plan - `npm run test tests/browser-imports.test.ts tests/tools/logpoints.test.ts tests/formatters/LogpointFormatter.test.ts tests/LogpointManager.test.ts tests/McpPage.test.ts`. - `npm run format` and `npm run build`. - `tests/browser-imports.test.ts` fails without the source change and passes with it.
1 parent ba76587 commit 71098b0

11 files changed

Lines changed: 119 additions & 106 deletions

File tree

β€Žsrc/BUILD.gnβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ ts_library("mcp_lib") {
268268
"utils/id.ts",
269269
"utils/keyboard.ts",
270270
"utils/logger.ts",
271+
"utils/logpoint.ts",
271272
"utils/network.ts",
272273
"utils/pagination.ts",
273274
"utils/polyfill.ts",

β€Žsrc/LogpointManager.tsβ€Ž

Lines changed: 6 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -11,68 +11,12 @@ import {
1111
waitForSourceMaps,
1212
} from './devtools/DevtoolsUtils.js';
1313
import {DevTools} from './third_party/browser.js';
14-
15-
export interface LogpointOptions {
16-
/**
17-
* The URL of a script as served to the browser, or the URL of an original
18-
* source file from a source map. Matched exactly or as a path suffix.
19-
*/
20-
url: string;
21-
/** 1-based line number in the coordinates of `url`. */
22-
lineNumber: number;
23-
/** Optional 1-based column number. */
24-
columnNumber?: number;
25-
/** Logged like the arguments of `console.log()`. */
26-
expression: string;
27-
}
28-
29-
/**
30-
* A location in a script as served to the browser, 1-based like the
31-
* locations reported by the tools.
32-
*/
33-
export interface GeneratedLocation {
34-
url: string;
35-
lineNumber: number;
36-
columnNumber: number;
37-
}
38-
39-
export interface Logpoint {
40-
/**
41-
* The full URL of the script or original source the logpoint was set in.
42-
* DevTools normalizes locations, so this can be the original source file
43-
* of a location that was given in a generated script.
44-
*/
45-
url: string;
46-
lineNumber: number;
47-
columnNumber?: number;
48-
expression: string;
49-
/**
50-
* Whether `url` is an original source file from a source map rather than
51-
* a script as served to the browser.
52-
*/
53-
sourceMapped: boolean;
54-
/**
55-
* The locations in the scripts served to the browser the logpoint is
56-
* currently set at. Empty while no parsed script contains the location,
57-
* for example right after a reload before the script was parsed again.
58-
*/
59-
generatedLocations: GeneratedLocation[];
60-
}
61-
62-
/**
63-
* Thrown by {@link LogpointManager.setLogpoints} when some locations could
64-
* not be bound. The other logpoints were applied; `logpoints` holds the
65-
* resulting state.
66-
*/
67-
export class LogpointError extends Error {
68-
readonly logpoints: Logpoint[];
69-
70-
constructor(message: string, logpoints: Logpoint[]) {
71-
super(message);
72-
this.name = 'LogpointError';
73-
this.logpoints = logpoints;
74-
}
75-
}
14+
import {
15+
type GeneratedLocation,
16+
type Logpoint,
17+
LogpointError,
18+
type LogpointOptions,
19+
} from './utils/logpoint.js';
7620

7721
type Universe = DevTools.Foundation.Universe.Universe;
7822
type Breakpoint = DevTools.BreakpointManager.Breakpoint;
@@ -85,38 +29,6 @@ interface ResolvedLogpoint {
8529
key: string;
8630
}
8731

88-
export const LOGPOINT_FORMAT =
89-
'<url>:<lineNumber>[:<columnNumber>] <expression>';
90-
91-
/**
92-
* Parses a logpoint given as `<url>:<lineNumber>[:<columnNumber>] <expression>`.
93-
* The url may itself contain colons (for example a port), so the location is
94-
* the shortest prefix that ends in one or two numbers followed by whitespace.
95-
*/
96-
export function parseLogpoint(text: string): LogpointOptions {
97-
const match = /^(\S+?):(\d+)(?::(\d+))?\s+(\S.*)$/s.exec(text.trim());
98-
if (!match) {
99-
throw new Error(
100-
`Invalid logpoint "${text}". Expected the format ${LOGPOINT_FORMAT}, for example "src/app.ts:42 'total', cart.total".`,
101-
);
102-
}
103-
const [, url, lineNumber, columnNumber, expression] = match;
104-
const logpoint: LogpointOptions = {
105-
url,
106-
lineNumber: Number(lineNumber),
107-
expression,
108-
};
109-
if (columnNumber !== undefined) {
110-
logpoint.columnNumber = Number(columnNumber);
111-
}
112-
if (logpoint.lineNumber < 1 || logpoint.columnNumber === 0) {
113-
throw new Error(
114-
`Invalid logpoint "${text}": line and column numbers are 1-based.`,
115-
);
116-
}
117-
return logpoint;
118-
}
119-
12032
/**
12133
* Checks that the expression parses as `console.log()` arguments. V8 is used
12234
* for parsing both here and in the browser, so an expression that is rejected

β€Žsrc/McpPage.tsβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ import {
1616
type ListenerMap,
1717
type UncaughtError,
1818
} from './collectors/PageCollector.js';
19-
import type {Logpoint, LogpointOptions} from './LogpointManager.js';
2019
import {LogpointManager} from './LogpointManager.js';
20+
import type {Logpoint, LogpointOptions} from './utils/logpoint.js';
2121
import {TextSnapshot} from './TextSnapshot.js';
2222
import {
2323
NetworkOverrideManager,

β€Žsrc/formatters/LogpointFormatter.tsβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

7-
import type {GeneratedLocation, Logpoint} from '../LogpointManager.js';
7+
import type {GeneratedLocation, Logpoint} from '../utils/logpoint.js';
88

99
export interface StructuredLogpoint {
1010
url: string;

β€Žsrc/tools/ToolDefinition.tsβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import type {
1515
HeapQueryOptions,
1616
} from '../processors/HeapSnapshotManager.js';
1717
import type {McpPage} from '../McpPage.js';
18-
import type {Logpoint, LogpointOptions} from '../LogpointManager.js';
18+
import type {Logpoint, LogpointOptions} from '../utils/logpoint.js';
1919
import type {DevToolsCommentBridge} from '../devtools/DevToolsCommentBridge.js';
2020
import type {CssFormatterOptions} from '../formatters/CssFormatter.js';
2121
import type {

β€Žsrc/tools/logpoints.tsβ€Ž

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
*/
66

77
import {LogpointFormatter} from '../formatters/LogpointFormatter.js';
8+
import {zod} from '../third_party/index.js';
89
import {
910
LOGPOINT_FORMAT,
1011
LogpointError,
1112
parseLogpoint,
12-
} from '../LogpointManager.js';
13-
import {zod} from '../third_party/index.js';
13+
} from '../utils/logpoint.js';
1414

1515
import {ToolCategory} from './categories.js';
1616
import {definePageTool, type PageToolFactory} from './ToolDefinition.js';

β€Žsrc/utils/logpoint.tsβ€Ž

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/**
2+
* @license
3+
* Copyright 2026 Google LLC
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
export interface LogpointOptions {
8+
/**
9+
* The URL of a script as served to the browser, or the URL of an original
10+
* source file from a source map. Matched exactly or as a path suffix.
11+
*/
12+
url: string;
13+
/** 1-based line number in the coordinates of `url`. */
14+
lineNumber: number;
15+
/** Optional 1-based column number. */
16+
columnNumber?: number;
17+
/** Logged like the arguments of `console.log()`. */
18+
expression: string;
19+
}
20+
21+
/**
22+
* A location in a script as served to the browser, 1-based like the
23+
* locations reported by the tools.
24+
*/
25+
export interface GeneratedLocation {
26+
url: string;
27+
lineNumber: number;
28+
columnNumber: number;
29+
}
30+
31+
export interface Logpoint {
32+
/**
33+
* The full URL of the script or original source the logpoint was set in.
34+
* DevTools normalizes locations, so this can be the original source file
35+
* of a location that was given in a generated script.
36+
*/
37+
url: string;
38+
lineNumber: number;
39+
columnNumber?: number;
40+
expression: string;
41+
/**
42+
* Whether `url` is an original source file from a source map rather than
43+
* a script as served to the browser.
44+
*/
45+
sourceMapped: boolean;
46+
/**
47+
* The locations in the scripts served to the browser the logpoint is
48+
* currently set at. Empty while no parsed script contains the location,
49+
* for example right after a reload before the script was parsed again.
50+
*/
51+
generatedLocations: GeneratedLocation[];
52+
}
53+
54+
/**
55+
* Thrown by {@link LogpointManager.setLogpoints} when some locations could
56+
* not be bound. The other logpoints were applied; `logpoints` holds the
57+
* resulting state.
58+
*/
59+
export class LogpointError extends Error {
60+
readonly logpoints: Logpoint[];
61+
62+
constructor(message: string, logpoints: Logpoint[]) {
63+
super(message);
64+
this.name = 'LogpointError';
65+
this.logpoints = logpoints;
66+
}
67+
}
68+
69+
export const LOGPOINT_FORMAT =
70+
'<url>:<lineNumber>[:<columnNumber>] <expression>';
71+
72+
/**
73+
* Parses a logpoint given as `<url>:<lineNumber>[:<columnNumber>] <expression>`.
74+
* The url may itself contain colons (for example a port), so the location is
75+
* the shortest prefix that ends in one or two numbers followed by whitespace.
76+
*/
77+
export function parseLogpoint(text: string): LogpointOptions {
78+
const match = /^(\S+?):(\d+)(?::(\d+))?\s+(\S.*)$/s.exec(text.trim());
79+
if (!match) {
80+
throw new Error(
81+
`Invalid logpoint "${text}". Expected the format ${LOGPOINT_FORMAT}, for example "src/app.ts:42 'total', cart.total".`,
82+
);
83+
}
84+
const [, url, lineNumber, columnNumber, expression] = match;
85+
const logpoint: LogpointOptions = {
86+
url,
87+
lineNumber: Number(lineNumber),
88+
expression,
89+
};
90+
if (columnNumber !== undefined) {
91+
logpoint.columnNumber = Number(columnNumber);
92+
}
93+
if (logpoint.lineNumber < 1 || logpoint.columnNumber === 0) {
94+
throw new Error(
95+
`Invalid logpoint "${text}": line and column numbers are 1-based.`,
96+
);
97+
}
98+
return logpoint;
99+
}

β€Žtests/LogpointManager.test.tsβ€Ž

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@ import {describe, it} from 'node:test';
1010

1111
import ts from 'typescript';
1212

13-
import type {Logpoint, LogpointOptions} from '../src/LogpointManager.js';
13+
import {validateExpression} from '../src/LogpointManager.js';
14+
import type {McpPage} from '../src/McpPage.js';
1415
import {
16+
type Logpoint,
1517
LogpointError,
18+
type LogpointOptions,
1619
parseLogpoint,
17-
validateExpression,
18-
} from '../src/LogpointManager.js';
19-
import type {McpPage} from '../src/McpPage.js';
20+
} from '../src/utils/logpoint.js';
2021
import {serverHooks} from './server.js';
2122
import {html, waitExecutionFor, withMcpContext} from './utils.js';
2223

β€Žtests/browser-imports.test.tsβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const LIGHT_ENTRIES = [
2020
'tools/emulation.js',
2121
'tools/input.js',
2222
'tools/lighthouse.js',
23+
'tools/logpoints.js',
2324
'tools/performance.js',
2425
'tools/snapshot.js',
2526
'utils/text.js',

β€Žtests/formatters/LogpointFormatter.test.tsβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import {describe, it} from 'node:test';
88

99
import {LogpointFormatter} from '../../src/formatters/LogpointFormatter.js';
10-
import type {Logpoint} from '../../src/LogpointManager.js';
10+
import type {Logpoint} from '../../src/utils/logpoint.js';
1111

1212
describe('LogpointFormatter', () => {
1313
function formatterTest(label: string, logpoints: Logpoint[]) {

0 commit comments

Comments
Β (0)