playwriter 0.0.25 → 0.0.29
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.
- package/bin.js +1 -1
- package/dist/bippy.js +966 -0
- package/dist/{extension/cdp-relay.d.ts → cdp-relay.d.ts} +3 -2
- package/dist/cdp-relay.d.ts.map +1 -0
- package/dist/{extension/cdp-relay.js → cdp-relay.js} +101 -3
- package/dist/cdp-relay.js.map +1 -0
- package/dist/cdp-session.d.ts +1 -1
- package/dist/cdp-session.d.ts.map +1 -1
- package/dist/cdp-session.js +4 -4
- package/dist/cdp-session.js.map +1 -1
- package/dist/cli.d.ts +3 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +71 -0
- package/dist/cli.js.map +1 -0
- package/dist/create-logger.d.ts.map +1 -1
- package/dist/create-logger.js +2 -1
- package/dist/create-logger.js.map +1 -1
- package/dist/debugger-examples-types.d.ts +18 -0
- package/dist/debugger-examples-types.d.ts.map +1 -0
- package/dist/debugger-examples-types.js +2 -0
- package/dist/debugger-examples-types.js.map +1 -0
- package/dist/debugger-examples.d.ts +6 -0
- package/dist/debugger-examples.d.ts.map +1 -0
- package/dist/debugger-examples.js +53 -0
- package/dist/debugger-examples.js.map +1 -0
- package/dist/debugger-examples.ts +66 -0
- package/dist/debugger.d.ts +380 -0
- package/dist/debugger.d.ts.map +1 -0
- package/dist/debugger.js +631 -0
- package/dist/debugger.js.map +1 -0
- package/dist/editor-examples.d.ts +11 -0
- package/dist/editor-examples.d.ts.map +1 -0
- package/dist/editor-examples.js +124 -0
- package/dist/editor-examples.js.map +1 -0
- package/dist/editor.d.ts +203 -0
- package/dist/editor.d.ts.map +1 -0
- package/dist/editor.js +335 -0
- package/dist/editor.js.map +1 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/mcp-client.d.ts +5 -1
- package/dist/mcp-client.d.ts.map +1 -1
- package/dist/mcp-client.js +13 -9
- package/dist/mcp-client.js.map +1 -1
- package/dist/mcp.d.ts +4 -1
- package/dist/mcp.d.ts.map +1 -1
- package/dist/mcp.js +170 -27
- package/dist/mcp.js.map +1 -1
- package/dist/mcp.test.d.ts.map +1 -1
- package/dist/mcp.test.js +886 -182
- package/dist/mcp.test.js.map +1 -1
- package/dist/prompt.md +86 -6
- package/dist/{extension/protocol.d.ts → protocol.d.ts} +1 -1
- package/dist/protocol.d.ts.map +1 -0
- package/dist/protocol.js.map +1 -0
- package/dist/react-source.d.ts +13 -0
- package/dist/react-source.d.ts.map +1 -0
- package/dist/react-source.js +66 -0
- package/dist/react-source.js.map +1 -0
- package/dist/selector-generator.js +7065 -18
- package/dist/start-relay-server.d.ts +4 -2
- package/dist/start-relay-server.d.ts.map +1 -1
- package/dist/start-relay-server.js +3 -3
- package/dist/start-relay-server.js.map +1 -1
- package/dist/styles.d.ts +27 -0
- package/dist/styles.d.ts.map +1 -0
- package/dist/styles.js +232 -0
- package/dist/styles.js.map +1 -0
- package/dist/utils.d.ts +3 -1
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +7 -3
- package/dist/utils.js.map +1 -1
- package/dist/wait-for-page-load.d.ts.map +1 -1
- package/dist/wait-for-page-load.js +3 -2
- package/dist/wait-for-page-load.js.map +1 -1
- package/package.json +5 -2
- package/src/{extension/cdp-relay.ts → cdp-relay.ts} +109 -5
- package/src/cdp-session.ts +4 -4
- package/src/cdp-timing.md +128 -0
- package/src/cli.ts +85 -0
- package/src/create-logger.ts +2 -1
- package/src/debugger-examples-types.ts +10 -0
- package/src/debugger-examples.ts +66 -0
- package/src/debugger.ts +711 -0
- package/src/editor-examples.ts +148 -0
- package/src/editor.ts +389 -0
- package/src/index.ts +1 -1
- package/src/mcp-client.ts +14 -9
- package/src/mcp.test.ts +1053 -196
- package/src/mcp.ts +195 -30
- package/src/prompt.md +86 -6
- package/src/{extension/protocol.ts → protocol.ts} +1 -1
- package/src/react-source.ts +92 -0
- package/src/snapshots/shadcn-ui-accessibility.md +57 -57
- package/src/start-relay-server.ts +3 -3
- package/src/styles.ts +343 -0
- package/src/utils.ts +8 -3
- package/src/wait-for-page-load.ts +3 -2
- package/dist/extension/cdp-relay.d.ts.map +0 -1
- package/dist/extension/cdp-relay.js.map +0 -1
- package/dist/extension/protocol.d.ts.map +0 -1
- package/dist/extension/protocol.js.map +0 -1
- /package/dist/{extension/protocol.js → protocol.js} +0 -0
package/src/debugger.ts
ADDED
|
@@ -0,0 +1,711 @@
|
|
|
1
|
+
import type { CDPSession } from './cdp-session.js'
|
|
2
|
+
import type { Protocol } from 'devtools-protocol'
|
|
3
|
+
|
|
4
|
+
export interface BreakpointInfo {
|
|
5
|
+
id: string
|
|
6
|
+
file: string
|
|
7
|
+
line: number
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface LocationInfo {
|
|
11
|
+
url: string
|
|
12
|
+
lineNumber: number
|
|
13
|
+
columnNumber: number
|
|
14
|
+
callstack: Array<{
|
|
15
|
+
functionName: string
|
|
16
|
+
url: string
|
|
17
|
+
lineNumber: number
|
|
18
|
+
columnNumber: number
|
|
19
|
+
}>
|
|
20
|
+
sourceContext: string
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface EvaluateResult {
|
|
24
|
+
value: unknown
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
export interface ScriptInfo {
|
|
30
|
+
scriptId: string
|
|
31
|
+
url: string
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* A class for debugging JavaScript code via Chrome DevTools Protocol.
|
|
36
|
+
* Works with both Node.js (--inspect) and browser debugging.
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* ```ts
|
|
40
|
+
* const cdp = await getCDPSessionForPage({ page, wsUrl })
|
|
41
|
+
* const dbg = new Debugger({ cdp })
|
|
42
|
+
*
|
|
43
|
+
* await dbg.setBreakpoint({ file: 'https://example.com/app.js', line: 42 })
|
|
44
|
+
* // trigger the code path, then:
|
|
45
|
+
* const location = await dbg.getLocation()
|
|
46
|
+
* const vars = await dbg.inspectLocalVariables()
|
|
47
|
+
* await dbg.resume()
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
export class Debugger {
|
|
51
|
+
private cdp: CDPSession
|
|
52
|
+
private debuggerEnabled = false
|
|
53
|
+
private paused = false
|
|
54
|
+
private currentCallFrames: Protocol.Debugger.CallFrame[] = []
|
|
55
|
+
private breakpoints = new Map<string, BreakpointInfo>()
|
|
56
|
+
private scripts = new Map<string, ScriptInfo>()
|
|
57
|
+
private xhrBreakpoints = new Set<string>()
|
|
58
|
+
private blackboxPatterns: string[] = []
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Creates a new Debugger instance.
|
|
62
|
+
*
|
|
63
|
+
* @param options - Configuration options
|
|
64
|
+
* @param options.cdp - A CDPSession instance for sending CDP commands
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* ```ts
|
|
68
|
+
* const cdp = await getCDPSessionForPage({ page, wsUrl })
|
|
69
|
+
* const dbg = new Debugger({ cdp })
|
|
70
|
+
* ```
|
|
71
|
+
*/
|
|
72
|
+
constructor({ cdp }: { cdp: CDPSession }) {
|
|
73
|
+
this.cdp = cdp
|
|
74
|
+
this.setupEventListeners()
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
private setupEventListeners() {
|
|
78
|
+
this.cdp.on('Debugger.paused', (params) => {
|
|
79
|
+
this.paused = true
|
|
80
|
+
this.currentCallFrames = params.callFrames
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
this.cdp.on('Debugger.resumed', () => {
|
|
84
|
+
this.paused = false
|
|
85
|
+
this.currentCallFrames = []
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
this.cdp.on('Debugger.scriptParsed', (params) => {
|
|
89
|
+
if (params.url && !params.url.startsWith('chrome') && !params.url.startsWith('devtools')) {
|
|
90
|
+
this.scripts.set(params.scriptId, {
|
|
91
|
+
scriptId: params.scriptId,
|
|
92
|
+
url: params.url,
|
|
93
|
+
})
|
|
94
|
+
}
|
|
95
|
+
})
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Enables the debugger and runtime domains. Called automatically by other methods.
|
|
100
|
+
* Also resumes execution if the target was started with --inspect-brk.
|
|
101
|
+
*
|
|
102
|
+
* @example
|
|
103
|
+
* ```ts
|
|
104
|
+
* await dbg.enable()
|
|
105
|
+
* ```
|
|
106
|
+
*/
|
|
107
|
+
async enable(): Promise<void> {
|
|
108
|
+
if (this.debuggerEnabled) {
|
|
109
|
+
return
|
|
110
|
+
}
|
|
111
|
+
await this.cdp.send('Debugger.disable')
|
|
112
|
+
await this.cdp.send('Runtime.disable')
|
|
113
|
+
this.scripts.clear()
|
|
114
|
+
const scriptsReady = new Promise<void>((resolve) => {
|
|
115
|
+
let timeout: ReturnType<typeof setTimeout>
|
|
116
|
+
const listener = () => {
|
|
117
|
+
clearTimeout(timeout)
|
|
118
|
+
timeout = setTimeout(() => {
|
|
119
|
+
this.cdp.off('Debugger.scriptParsed', listener)
|
|
120
|
+
resolve()
|
|
121
|
+
}, 100)
|
|
122
|
+
}
|
|
123
|
+
this.cdp.on('Debugger.scriptParsed', listener)
|
|
124
|
+
timeout = setTimeout(() => {
|
|
125
|
+
this.cdp.off('Debugger.scriptParsed', listener)
|
|
126
|
+
resolve()
|
|
127
|
+
}, 100)
|
|
128
|
+
})
|
|
129
|
+
await this.cdp.send('Debugger.enable')
|
|
130
|
+
await this.cdp.send('Runtime.enable')
|
|
131
|
+
await this.cdp.send('Runtime.runIfWaitingForDebugger')
|
|
132
|
+
await scriptsReady
|
|
133
|
+
this.debuggerEnabled = true
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Sets a breakpoint at a specified URL and line number.
|
|
138
|
+
* Use the URL from listScripts() to find available scripts.
|
|
139
|
+
*
|
|
140
|
+
* @param options - Breakpoint options
|
|
141
|
+
* @param options.file - Script URL (e.g. https://example.com/app.js)
|
|
142
|
+
* @param options.line - Line number (1-based)
|
|
143
|
+
* @param options.condition - Optional JS expression; only pause when it evaluates to true
|
|
144
|
+
* @returns The breakpoint ID for later removal
|
|
145
|
+
*
|
|
146
|
+
* @example
|
|
147
|
+
* ```ts
|
|
148
|
+
* const id = await dbg.setBreakpoint({ file: 'https://example.com/app.js', line: 42 })
|
|
149
|
+
* // later:
|
|
150
|
+
* await dbg.deleteBreakpoint({ breakpointId: id })
|
|
151
|
+
*
|
|
152
|
+
* // Conditional breakpoint - only pause when userId is 123
|
|
153
|
+
* await dbg.setBreakpoint({
|
|
154
|
+
* file: 'https://example.com/app.js',
|
|
155
|
+
* line: 42,
|
|
156
|
+
* condition: 'userId === 123'
|
|
157
|
+
* })
|
|
158
|
+
* ```
|
|
159
|
+
*/
|
|
160
|
+
async setBreakpoint({ file, line, condition }: { file: string; line: number; condition?: string }): Promise<string> {
|
|
161
|
+
await this.enable()
|
|
162
|
+
|
|
163
|
+
const response = await this.cdp.send('Debugger.setBreakpointByUrl', {
|
|
164
|
+
lineNumber: line - 1,
|
|
165
|
+
urlRegex: file.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'),
|
|
166
|
+
columnNumber: 0,
|
|
167
|
+
condition,
|
|
168
|
+
})
|
|
169
|
+
|
|
170
|
+
this.breakpoints.set(response.breakpointId, { id: response.breakpointId, file, line })
|
|
171
|
+
return response.breakpointId
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Removes a breakpoint by its ID.
|
|
176
|
+
*
|
|
177
|
+
* @param options - Options
|
|
178
|
+
* @param options.breakpointId - The breakpoint ID returned by setBreakpoint
|
|
179
|
+
*
|
|
180
|
+
* @example
|
|
181
|
+
* ```ts
|
|
182
|
+
* await dbg.deleteBreakpoint({ breakpointId: 'bp-123' })
|
|
183
|
+
* ```
|
|
184
|
+
*/
|
|
185
|
+
async deleteBreakpoint({ breakpointId }: { breakpointId: string }): Promise<void> {
|
|
186
|
+
await this.enable()
|
|
187
|
+
await this.cdp.send('Debugger.removeBreakpoint', { breakpointId })
|
|
188
|
+
this.breakpoints.delete(breakpointId)
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Returns a list of all active breakpoints set by this debugger instance.
|
|
193
|
+
*
|
|
194
|
+
* @returns Array of breakpoint info objects
|
|
195
|
+
*
|
|
196
|
+
* @example
|
|
197
|
+
* ```ts
|
|
198
|
+
* const breakpoints = dbg.listBreakpoints()
|
|
199
|
+
* // [{ id: 'bp-123', file: 'https://example.com/index.js', line: 42 }]
|
|
200
|
+
* ```
|
|
201
|
+
*/
|
|
202
|
+
listBreakpoints(): BreakpointInfo[] {
|
|
203
|
+
return Array.from(this.breakpoints.values())
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Inspects local variables in the current call frame.
|
|
208
|
+
* Must be paused at a breakpoint. String values over 1000 chars are truncated.
|
|
209
|
+
* Use evaluate() for full control over reading specific values.
|
|
210
|
+
*
|
|
211
|
+
* @returns Record of variable names to values
|
|
212
|
+
* @throws Error if not paused or no active call frames
|
|
213
|
+
*
|
|
214
|
+
* @example
|
|
215
|
+
* ```ts
|
|
216
|
+
* const vars = await dbg.inspectLocalVariables()
|
|
217
|
+
* // { myVar: 'hello', count: 42 }
|
|
218
|
+
* ```
|
|
219
|
+
*/
|
|
220
|
+
async inspectLocalVariables(): Promise<Record<string, unknown>> {
|
|
221
|
+
await this.enable()
|
|
222
|
+
|
|
223
|
+
if (!this.paused || this.currentCallFrames.length === 0) {
|
|
224
|
+
throw new Error('Debugger is not paused at a breakpoint')
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
const frame = this.currentCallFrames[0]
|
|
228
|
+
const result: Record<string, unknown> = {}
|
|
229
|
+
|
|
230
|
+
for (const scopeObj of frame.scopeChain) {
|
|
231
|
+
if (scopeObj.type === 'global') {
|
|
232
|
+
continue
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
if (!scopeObj.object.objectId) {
|
|
236
|
+
continue
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
const objProperties = await this.cdp.send('Runtime.getProperties', {
|
|
240
|
+
objectId: scopeObj.object.objectId,
|
|
241
|
+
ownProperties: true,
|
|
242
|
+
accessorPropertiesOnly: false,
|
|
243
|
+
generatePreview: true,
|
|
244
|
+
})
|
|
245
|
+
|
|
246
|
+
for (const prop of objProperties.result) {
|
|
247
|
+
if (prop.value && prop.configurable) {
|
|
248
|
+
result[prop.name] = this.formatPropertyValue(prop.value)
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
return result
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* Returns global lexical scope variable names.
|
|
258
|
+
*
|
|
259
|
+
* @returns Array of global variable names
|
|
260
|
+
*
|
|
261
|
+
* @example
|
|
262
|
+
* ```ts
|
|
263
|
+
* const globals = await dbg.inspectGlobalVariables()
|
|
264
|
+
* // ['myGlobal', 'CONFIG']
|
|
265
|
+
* ```
|
|
266
|
+
*/
|
|
267
|
+
async inspectGlobalVariables(): Promise<string[]> {
|
|
268
|
+
await this.enable()
|
|
269
|
+
|
|
270
|
+
const response = await this.cdp.send('Runtime.globalLexicalScopeNames', {})
|
|
271
|
+
|
|
272
|
+
return response.names
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* Evaluates a JavaScript expression and returns the result.
|
|
277
|
+
* When paused at a breakpoint, evaluates in the current stack frame scope,
|
|
278
|
+
* allowing access to local variables. Otherwise evaluates in global scope.
|
|
279
|
+
* Values are not truncated, use this for full control over reading specific variables.
|
|
280
|
+
*
|
|
281
|
+
* @param options - Options
|
|
282
|
+
* @param options.expression - JavaScript expression to evaluate
|
|
283
|
+
* @returns The result value
|
|
284
|
+
*
|
|
285
|
+
* @example
|
|
286
|
+
* ```ts
|
|
287
|
+
* // When paused, can access local variables:
|
|
288
|
+
* const result = await dbg.evaluate({ expression: 'localVar + 1' })
|
|
289
|
+
*
|
|
290
|
+
* // Read a large string that would be truncated in inspectLocalVariables:
|
|
291
|
+
* const full = await dbg.evaluate({ expression: 'largeStringVar' })
|
|
292
|
+
* ```
|
|
293
|
+
*/
|
|
294
|
+
async evaluate({ expression }: { expression: string }): Promise<EvaluateResult> {
|
|
295
|
+
await this.enable()
|
|
296
|
+
|
|
297
|
+
const wrappedExpression = `
|
|
298
|
+
try {
|
|
299
|
+
${expression}
|
|
300
|
+
} catch (e) {
|
|
301
|
+
e;
|
|
302
|
+
}
|
|
303
|
+
`
|
|
304
|
+
|
|
305
|
+
let response: Protocol.Debugger.EvaluateOnCallFrameResponse | Protocol.Runtime.EvaluateResponse
|
|
306
|
+
|
|
307
|
+
if (this.paused && this.currentCallFrames.length > 0) {
|
|
308
|
+
const frame = this.currentCallFrames[0]
|
|
309
|
+
response = await this.cdp.send('Debugger.evaluateOnCallFrame', {
|
|
310
|
+
callFrameId: frame.callFrameId,
|
|
311
|
+
expression: wrappedExpression,
|
|
312
|
+
objectGroup: 'console',
|
|
313
|
+
includeCommandLineAPI: true,
|
|
314
|
+
silent: false,
|
|
315
|
+
returnByValue: true,
|
|
316
|
+
generatePreview: true,
|
|
317
|
+
})
|
|
318
|
+
} else {
|
|
319
|
+
response = await this.cdp.send('Runtime.evaluate', {
|
|
320
|
+
expression: wrappedExpression,
|
|
321
|
+
objectGroup: 'console',
|
|
322
|
+
includeCommandLineAPI: true,
|
|
323
|
+
silent: false,
|
|
324
|
+
returnByValue: true,
|
|
325
|
+
generatePreview: true,
|
|
326
|
+
awaitPromise: true,
|
|
327
|
+
})
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
const value = await this.processRemoteObject(response.result)
|
|
331
|
+
|
|
332
|
+
return { value }
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
/**
|
|
336
|
+
* Gets the current execution location when paused at a breakpoint.
|
|
337
|
+
* Includes the call stack and surrounding source code for context.
|
|
338
|
+
*
|
|
339
|
+
* @returns Location info with URL, line number, call stack, and source context
|
|
340
|
+
* @throws Error if debugger is not paused
|
|
341
|
+
*
|
|
342
|
+
* @example
|
|
343
|
+
* ```ts
|
|
344
|
+
* const location = await dbg.getLocation()
|
|
345
|
+
* console.log(location.url) // 'https://example.com/src/index.js'
|
|
346
|
+
* console.log(location.lineNumber) // 42
|
|
347
|
+
* console.log(location.callstack) // [{ functionName: 'handleRequest', ... }]
|
|
348
|
+
* console.log(location.sourceContext)
|
|
349
|
+
* // ' 40: function handleRequest(req) {
|
|
350
|
+
* // 41: const data = req.body
|
|
351
|
+
* // > 42: processData(data)
|
|
352
|
+
* // 43: }'
|
|
353
|
+
* ```
|
|
354
|
+
*/
|
|
355
|
+
async getLocation(): Promise<LocationInfo> {
|
|
356
|
+
await this.enable()
|
|
357
|
+
|
|
358
|
+
if (!this.paused || this.currentCallFrames.length === 0) {
|
|
359
|
+
throw new Error('Debugger is not paused at a breakpoint')
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
const frame = this.currentCallFrames[0]
|
|
363
|
+
const { scriptId, lineNumber, columnNumber } = frame.location
|
|
364
|
+
|
|
365
|
+
const callstack = this.currentCallFrames.map((f) => ({
|
|
366
|
+
functionName: f.functionName || '(anonymous)',
|
|
367
|
+
url: f.url,
|
|
368
|
+
lineNumber: f.location.lineNumber + 1,
|
|
369
|
+
columnNumber: f.location.columnNumber || 0,
|
|
370
|
+
}))
|
|
371
|
+
|
|
372
|
+
let sourceContext = ''
|
|
373
|
+
try {
|
|
374
|
+
const scriptSource = await this.cdp.send('Debugger.getScriptSource', { scriptId })
|
|
375
|
+
const lines = scriptSource.scriptSource.split('\n')
|
|
376
|
+
const startLine = Math.max(0, lineNumber - 3)
|
|
377
|
+
const endLine = Math.min(lines.length - 1, lineNumber + 3)
|
|
378
|
+
|
|
379
|
+
for (let i = startLine; i <= endLine; i++) {
|
|
380
|
+
const prefix = i === lineNumber ? '> ' : ' '
|
|
381
|
+
sourceContext += `${prefix}${i + 1}: ${lines[i]}\n`
|
|
382
|
+
}
|
|
383
|
+
} catch {
|
|
384
|
+
sourceContext = 'Unable to retrieve source code'
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
return {
|
|
388
|
+
url: frame.url,
|
|
389
|
+
lineNumber: lineNumber + 1,
|
|
390
|
+
columnNumber: columnNumber || 0,
|
|
391
|
+
callstack,
|
|
392
|
+
sourceContext,
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
/**
|
|
397
|
+
* Steps over to the next line of code, not entering function calls.
|
|
398
|
+
*
|
|
399
|
+
* @throws Error if debugger is not paused
|
|
400
|
+
*
|
|
401
|
+
* @example
|
|
402
|
+
* ```ts
|
|
403
|
+
* await dbg.stepOver()
|
|
404
|
+
* const newLocation = await dbg.getLocation()
|
|
405
|
+
* ```
|
|
406
|
+
*/
|
|
407
|
+
async stepOver(): Promise<void> {
|
|
408
|
+
await this.enable()
|
|
409
|
+
if (!this.paused) {
|
|
410
|
+
throw new Error('Debugger is not paused')
|
|
411
|
+
}
|
|
412
|
+
await this.cdp.send('Debugger.stepOver')
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
/**
|
|
416
|
+
* Steps into a function call on the current line.
|
|
417
|
+
*
|
|
418
|
+
* @throws Error if debugger is not paused
|
|
419
|
+
*
|
|
420
|
+
* @example
|
|
421
|
+
* ```ts
|
|
422
|
+
* await dbg.stepInto()
|
|
423
|
+
* const location = await dbg.getLocation()
|
|
424
|
+
* // now inside the called function
|
|
425
|
+
* ```
|
|
426
|
+
*/
|
|
427
|
+
async stepInto(): Promise<void> {
|
|
428
|
+
await this.enable()
|
|
429
|
+
if (!this.paused) {
|
|
430
|
+
throw new Error('Debugger is not paused')
|
|
431
|
+
}
|
|
432
|
+
await this.cdp.send('Debugger.stepInto')
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
/**
|
|
436
|
+
* Steps out of the current function, returning to the caller.
|
|
437
|
+
*
|
|
438
|
+
* @throws Error if debugger is not paused
|
|
439
|
+
*
|
|
440
|
+
* @example
|
|
441
|
+
* ```ts
|
|
442
|
+
* await dbg.stepOut()
|
|
443
|
+
* const location = await dbg.getLocation()
|
|
444
|
+
* // back in the calling function
|
|
445
|
+
* ```
|
|
446
|
+
*/
|
|
447
|
+
async stepOut(): Promise<void> {
|
|
448
|
+
await this.enable()
|
|
449
|
+
if (!this.paused) {
|
|
450
|
+
throw new Error('Debugger is not paused')
|
|
451
|
+
}
|
|
452
|
+
await this.cdp.send('Debugger.stepOut')
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
/**
|
|
456
|
+
* Resumes code execution until the next breakpoint or completion.
|
|
457
|
+
*
|
|
458
|
+
* @throws Error if debugger is not paused
|
|
459
|
+
*
|
|
460
|
+
* @example
|
|
461
|
+
* ```ts
|
|
462
|
+
* await dbg.resume()
|
|
463
|
+
* // execution continues
|
|
464
|
+
* ```
|
|
465
|
+
*/
|
|
466
|
+
async resume(): Promise<void> {
|
|
467
|
+
await this.enable()
|
|
468
|
+
if (!this.paused) {
|
|
469
|
+
throw new Error('Debugger is not paused')
|
|
470
|
+
}
|
|
471
|
+
await this.cdp.send('Debugger.resume')
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
/**
|
|
475
|
+
* Returns whether the debugger is currently paused at a breakpoint.
|
|
476
|
+
*
|
|
477
|
+
* @returns true if paused, false otherwise
|
|
478
|
+
*
|
|
479
|
+
* @example
|
|
480
|
+
* ```ts
|
|
481
|
+
* if (dbg.isPaused()) {
|
|
482
|
+
* const vars = await dbg.inspectLocalVariables()
|
|
483
|
+
* }
|
|
484
|
+
* ```
|
|
485
|
+
*/
|
|
486
|
+
isPaused(): boolean {
|
|
487
|
+
return this.paused
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
/**
|
|
491
|
+
* Configures the debugger to pause on exceptions.
|
|
492
|
+
*
|
|
493
|
+
* @param options - Options
|
|
494
|
+
* @param options.state - When to pause: 'none' (never), 'uncaught' (only uncaught), or 'all' (all exceptions)
|
|
495
|
+
*
|
|
496
|
+
* @example
|
|
497
|
+
* ```ts
|
|
498
|
+
* // Pause only on uncaught exceptions
|
|
499
|
+
* await dbg.setPauseOnExceptions({ state: 'uncaught' })
|
|
500
|
+
*
|
|
501
|
+
* // Pause on all exceptions (caught and uncaught)
|
|
502
|
+
* await dbg.setPauseOnExceptions({ state: 'all' })
|
|
503
|
+
*
|
|
504
|
+
* // Disable pausing on exceptions
|
|
505
|
+
* await dbg.setPauseOnExceptions({ state: 'none' })
|
|
506
|
+
* ```
|
|
507
|
+
*/
|
|
508
|
+
async setPauseOnExceptions({ state }: { state: 'none' | 'uncaught' | 'all' }): Promise<void> {
|
|
509
|
+
await this.enable()
|
|
510
|
+
await this.cdp.send('Debugger.setPauseOnExceptions', { state })
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
/**
|
|
514
|
+
* Lists available scripts where breakpoints can be set.
|
|
515
|
+
* Automatically enables the debugger if not already enabled.
|
|
516
|
+
*
|
|
517
|
+
* @param options - Options
|
|
518
|
+
* @param options.search - Optional string to filter scripts by URL (case-insensitive)
|
|
519
|
+
* @returns Array of up to 20 matching scripts with scriptId and url
|
|
520
|
+
*
|
|
521
|
+
* @example
|
|
522
|
+
* ```ts
|
|
523
|
+
* // List all scripts
|
|
524
|
+
* const scripts = await dbg.listScripts()
|
|
525
|
+
* // [{ scriptId: '1', url: 'https://example.com/app.js' }, ...]
|
|
526
|
+
*
|
|
527
|
+
* // Search for specific files
|
|
528
|
+
* const handlers = await dbg.listScripts({ search: 'handler' })
|
|
529
|
+
* // [{ scriptId: '5', url: 'https://example.com/handlers.js' }]
|
|
530
|
+
* ```
|
|
531
|
+
*/
|
|
532
|
+
async listScripts({ search }: { search?: string } = {}): Promise<ScriptInfo[]> {
|
|
533
|
+
await this.enable()
|
|
534
|
+
const scripts = Array.from(this.scripts.values())
|
|
535
|
+
const filtered = search
|
|
536
|
+
? scripts.filter((s) => s.url.toLowerCase().includes(search.toLowerCase()))
|
|
537
|
+
: scripts
|
|
538
|
+
return filtered.slice(0, 20)
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
async setXHRBreakpoint({ url }: { url: string }): Promise<void> {
|
|
542
|
+
await this.enable()
|
|
543
|
+
await this.cdp.send('DOMDebugger.setXHRBreakpoint', { url })
|
|
544
|
+
this.xhrBreakpoints.add(url)
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
async removeXHRBreakpoint({ url }: { url: string }): Promise<void> {
|
|
548
|
+
await this.enable()
|
|
549
|
+
await this.cdp.send('DOMDebugger.removeXHRBreakpoint', { url })
|
|
550
|
+
this.xhrBreakpoints.delete(url)
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
listXHRBreakpoints(): string[] {
|
|
554
|
+
return Array.from(this.xhrBreakpoints)
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
/**
|
|
558
|
+
* Sets regex patterns for scripts to blackbox (skip when stepping).
|
|
559
|
+
* Blackboxed scripts are hidden from the call stack and stepped over automatically.
|
|
560
|
+
* Useful for ignoring framework/library code during debugging.
|
|
561
|
+
*
|
|
562
|
+
* @param options - Options
|
|
563
|
+
* @param options.patterns - Array of regex patterns to match script URLs
|
|
564
|
+
*
|
|
565
|
+
* @example
|
|
566
|
+
* ```ts
|
|
567
|
+
* // Skip all node_modules
|
|
568
|
+
* await dbg.setBlackboxPatterns({ patterns: ['node_modules'] })
|
|
569
|
+
*
|
|
570
|
+
* // Skip React and other frameworks
|
|
571
|
+
* await dbg.setBlackboxPatterns({
|
|
572
|
+
* patterns: [
|
|
573
|
+
* 'node_modules/react',
|
|
574
|
+
* 'node_modules/react-dom',
|
|
575
|
+
* 'node_modules/next',
|
|
576
|
+
* 'webpack://',
|
|
577
|
+
* ]
|
|
578
|
+
* })
|
|
579
|
+
*
|
|
580
|
+
* // Skip all third-party scripts
|
|
581
|
+
* await dbg.setBlackboxPatterns({ patterns: ['^https://cdn\\.'] })
|
|
582
|
+
*
|
|
583
|
+
* // Clear all blackbox patterns
|
|
584
|
+
* await dbg.setBlackboxPatterns({ patterns: [] })
|
|
585
|
+
* ```
|
|
586
|
+
*/
|
|
587
|
+
async setBlackboxPatterns({ patterns }: { patterns: string[] }): Promise<void> {
|
|
588
|
+
await this.enable()
|
|
589
|
+
this.blackboxPatterns = patterns
|
|
590
|
+
await this.cdp.send('Debugger.setBlackboxPatterns', { patterns })
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
/**
|
|
594
|
+
* Adds a single regex pattern to the blackbox list.
|
|
595
|
+
*
|
|
596
|
+
* @param options - Options
|
|
597
|
+
* @param options.pattern - Regex pattern to match script URLs
|
|
598
|
+
*
|
|
599
|
+
* @example
|
|
600
|
+
* ```ts
|
|
601
|
+
* await dbg.addBlackboxPattern({ pattern: 'node_modules/lodash' })
|
|
602
|
+
* await dbg.addBlackboxPattern({ pattern: 'node_modules/axios' })
|
|
603
|
+
* ```
|
|
604
|
+
*/
|
|
605
|
+
async addBlackboxPattern({ pattern }: { pattern: string }): Promise<void> {
|
|
606
|
+
await this.enable()
|
|
607
|
+
if (!this.blackboxPatterns.includes(pattern)) {
|
|
608
|
+
this.blackboxPatterns.push(pattern)
|
|
609
|
+
await this.cdp.send('Debugger.setBlackboxPatterns', { patterns: this.blackboxPatterns })
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
/**
|
|
614
|
+
* Removes a pattern from the blackbox list.
|
|
615
|
+
*
|
|
616
|
+
* @param options - Options
|
|
617
|
+
* @param options.pattern - The exact pattern string to remove
|
|
618
|
+
*/
|
|
619
|
+
async removeBlackboxPattern({ pattern }: { pattern: string }): Promise<void> {
|
|
620
|
+
await this.enable()
|
|
621
|
+
this.blackboxPatterns = this.blackboxPatterns.filter((p) => p !== pattern)
|
|
622
|
+
await this.cdp.send('Debugger.setBlackboxPatterns', { patterns: this.blackboxPatterns })
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
/**
|
|
626
|
+
* Returns the current list of blackbox patterns.
|
|
627
|
+
*/
|
|
628
|
+
listBlackboxPatterns(): string[] {
|
|
629
|
+
return [...this.blackboxPatterns]
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
private truncateValue(value: unknown): unknown {
|
|
633
|
+
if (typeof value === 'string' && value.length > 1000) {
|
|
634
|
+
return value.slice(0, 1000) + `... (${value.length} chars)`
|
|
635
|
+
}
|
|
636
|
+
return value
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
private formatPropertyValue(value: Protocol.Runtime.RemoteObject): unknown {
|
|
640
|
+
if (value.type === 'object' && value.subtype !== 'null') {
|
|
641
|
+
return `[${value.subtype || value.type}]`
|
|
642
|
+
}
|
|
643
|
+
if (value.type === 'function') {
|
|
644
|
+
return '[function]'
|
|
645
|
+
}
|
|
646
|
+
if (value.value !== undefined) {
|
|
647
|
+
return this.truncateValue(value.value)
|
|
648
|
+
}
|
|
649
|
+
return `[${value.type}]`
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
private async processRemoteObject(obj: Protocol.Runtime.RemoteObject): Promise<unknown> {
|
|
653
|
+
if (obj.type === 'undefined') {
|
|
654
|
+
return undefined
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
if (obj.value !== undefined) {
|
|
658
|
+
return obj.value
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
if (obj.type === 'object' && obj.objectId) {
|
|
662
|
+
try {
|
|
663
|
+
const props = await this.cdp.send('Runtime.getProperties', {
|
|
664
|
+
objectId: obj.objectId,
|
|
665
|
+
ownProperties: true,
|
|
666
|
+
accessorPropertiesOnly: false,
|
|
667
|
+
generatePreview: true,
|
|
668
|
+
})
|
|
669
|
+
|
|
670
|
+
const result: Record<string, unknown> = {}
|
|
671
|
+
for (const prop of props.result) {
|
|
672
|
+
if (prop.value) {
|
|
673
|
+
if (prop.value.type === 'object' && prop.value.objectId && prop.value.subtype !== 'null') {
|
|
674
|
+
try {
|
|
675
|
+
const nestedProps = await this.cdp.send('Runtime.getProperties', {
|
|
676
|
+
objectId: prop.value.objectId,
|
|
677
|
+
ownProperties: true,
|
|
678
|
+
accessorPropertiesOnly: false,
|
|
679
|
+
generatePreview: true,
|
|
680
|
+
})
|
|
681
|
+
const nestedObj: Record<string, unknown> = {}
|
|
682
|
+
for (const nestedProp of nestedProps.result) {
|
|
683
|
+
if (nestedProp.value) {
|
|
684
|
+
nestedObj[nestedProp.name] =
|
|
685
|
+
nestedProp.value.value !== undefined
|
|
686
|
+
? nestedProp.value.value
|
|
687
|
+
: nestedProp.value.description || `[${nestedProp.value.subtype || nestedProp.value.type}]`
|
|
688
|
+
}
|
|
689
|
+
}
|
|
690
|
+
result[prop.name] = nestedObj
|
|
691
|
+
} catch {
|
|
692
|
+
result[prop.name] = prop.value.description || `[${prop.value.subtype || prop.value.type}]`
|
|
693
|
+
}
|
|
694
|
+
} else if (prop.value.type === 'function') {
|
|
695
|
+
result[prop.name] = '[function]'
|
|
696
|
+
} else if (prop.value.value !== undefined) {
|
|
697
|
+
result[prop.name] = prop.value.value
|
|
698
|
+
} else {
|
|
699
|
+
result[prop.name] = `[${prop.value.type}]`
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
}
|
|
703
|
+
return result
|
|
704
|
+
} catch {
|
|
705
|
+
return obj.description || `[${obj.subtype || obj.type}]`
|
|
706
|
+
}
|
|
707
|
+
}
|
|
708
|
+
|
|
709
|
+
return obj.description || `[${obj.type}]`
|
|
710
|
+
}
|
|
711
|
+
}
|