Repository navigation
Expand file tree
/
Copy pathbidi.ts
More file actions
520 lines (493 loc) · 16.4 KB
/
Copy pathbidi.ts
File metadata and controls
520 lines (493 loc) · 16.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
import { createRequire } from 'node:module'
import type {
ConsoleLog,
LogLevel,
NetworkRequest
} from '@wdio/devtools-shared'
import {
LOG_SOURCES,
chromeLogLevelToLogLevel,
type LogSource
} from '@wdio/devtools-shared'
import { errorMessage } from './error.js'
import { getRequestType } from './net.js'
/** Generic sinks the BiDi handlers push into; {@link buildBidiSinks} wires them to a capturer. */
export interface BidiHandlerSinks {
pushConsoleLog: (entry: ConsoleLog) => void
pushNetworkRequest: (entry: NetworkRequest) => void
replaceNetworkRequest: (id: string, entry: NetworkRequest) => void
}
/** The capturer state BiDi events land in; `bidiNetworkObserved` is the evidence latch that gates the page's own rows. */
export interface BidiSinkTarget {
consoleLogs: ConsoleLog[]
networkRequests: NetworkRequest[]
sendUpstream(event: string, data: unknown): void
bidiNetworkObserved: boolean
}
/** Route BiDi events into the target's arrays and upstream; `onConsoleEntry` runs before each console entry is kept. */
export function buildBidiSinks(
target: BidiSinkTarget,
onConsoleEntry?: () => void
): BidiHandlerSinks {
return {
pushConsoleLog: (entry) => {
onConsoleEntry?.()
target.consoleLogs.push(entry)
target.sendUpstream('consoleLogs', [entry])
},
pushNetworkRequest: (entry) => {
target.bidiNetworkObserved = true
target.networkRequests.push(entry)
target.sendUpstream('networkRequests', [entry])
},
replaceNetworkRequest: (id, entry) => {
target.bidiNetworkObserved = true
const idx = target.networkRequests.findIndex((r) => r.id === id)
if (idx !== -1) {
target.networkRequests[idx] = entry
} else {
target.networkRequests.push(entry)
}
target.sendUpstream('networkRequests', [entry])
}
}
}
/**
* Resolve a `selenium-webdriver/<subpath>` module from the user's install
* (preferred) or the package's local install (fallback). Returns `null` if
* neither resolves — caller should treat as "BiDi not available on this
* runtime" and degrade gracefully.
*
* Used by both selenium-devtools and (when wired up) nightwatch-devtools —
* both ship selenium-webdriver-style drivers under the hood.
*/
export function loadSeleniumSubmodule<T = unknown>(subpath: string): T | null {
try {
const userRequire = createRequire(`${process.cwd()}/`)
return userRequire(`selenium-webdriver/${subpath}`) as T
} catch {
try {
const localRequire = createRequire(import.meta.url)
return localRequire(`selenium-webdriver/${subpath}`) as T
} catch {
return null
}
}
}
type BidiLogger = (level: 'info' | 'warn', message: string) => void
type InspectorFactory = (driver: unknown) => Promise<unknown>
interface LogInspector {
onConsoleEntry: (cb: (entry: unknown) => void) => Promise<void>
onJavascriptException: (cb: (exc: unknown) => void) => Promise<void>
}
interface NetworkInspector {
beforeRequestSent: (cb: (e: unknown) => void) => Promise<void>
responseCompleted: (cb: (e: unknown) => void) => Promise<void>
fetchError?: (cb: (e: unknown) => void) => Promise<void>
/** The connection the inspector subscribed on, for one without the hook. */
bidi?: {
subscribe: (events: string | string[]) => Promise<unknown>
socket?: {
on: (event: 'message', listener: (raw: unknown) => void) => void
}
}
}
export function handleBidiConsoleEntry(
rawEntry: unknown,
sinks: BidiHandlerSinks,
log: BidiLogger
): void {
const entry = rawEntry as {
level?: string
type?: string
text?: string
message?: string
timestamp?: number
}
try {
const level = (entry?.level ?? entry?.type ?? 'info').toString()
const text = entry?.text ?? entry?.message ?? ''
sinks.pushConsoleLog({
timestamp: Number(entry?.timestamp) || Date.now(),
type: chromeLogLevelToLogLevel(level) as LogLevel,
args: [text],
source: LOG_SOURCES.BROWSER as LogSource
})
} catch (err) {
log('warn', `onConsoleEntry handler threw: ${errorMessage(err)}`)
}
}
export function handleBidiJsException(
rawExc: unknown,
sinks: BidiHandlerSinks,
log: BidiLogger
): void {
const exception = rawExc as { text?: string; message?: string }
try {
const text = exception?.text ?? exception?.message ?? String(rawExc)
const trimmed = String(text).replace(/\s+/g, ' ').slice(0, 200)
log(
'warn',
`🐛 JS error in page: ${trimmed}${String(text).length > 200 ? '…' : ''}`
)
sinks.pushConsoleLog({
timestamp: Date.now(),
type: 'error',
args: [text],
source: LOG_SOURCES.BROWSER as LogSource
})
} catch (err) {
log('warn', `onJavascriptException handler threw: ${errorMessage(err)}`)
}
}
async function attachLogInspector(
driver: unknown,
factory: InspectorFactory,
sinks: BidiHandlerSinks,
log: BidiLogger
): Promise<boolean> {
try {
const inspector = (await factory(driver)) as LogInspector
await inspector.onConsoleEntry((e) => handleBidiConsoleEntry(e, sinks, log))
await inspector.onJavascriptException((e) =>
handleBidiJsException(e, sinks, log)
)
log('info', '✓ BiDi LogInspector attached (console + JS exceptions)')
return true
} catch (err) {
log('warn', `BiDi LogInspector attach failed: ${errorMessage(err)}`)
return false
}
}
/** Subset of WebDriver BiDi `FetchTimingInfo` (selenium exposes these via
* getters). Values are ms offsets from the request baseline `requestTime`
* (usually 0), so `responseEnd - requestTime` is the request duration. */
interface FetchTimings {
requestTime?: number
responseEnd?: number
}
const isFiniteNumber = (value: unknown): value is number =>
typeof value === 'number' && Number.isFinite(value)
interface BeforeRequestSentEvent {
request?: {
request?: string
url?: string
method?: string
headers?: unknown
timings?: FetchTimings
}
id?: string
timestamp?: number
}
interface ResponseCompletedEvent {
request?: { request?: string; timings?: FetchTimings }
id?: string
timestamp?: number
response?: {
status?: number
statusText?: string
headers?: unknown
mimeType?: string
bytesReceived?: number
}
}
/** Request duration (ms) from the browser's FetchTimingInfo, plus a derived
* endTime anchored to `startTime`. The browser timings are immune to BiDi
* events arriving batched in one tick (which collapses the event-level
* `timestamp`/`Date.now()` to a single value and yields 0-duration requests).
* Falls back to the timestamp diff only when timings are unavailable. */
function bidiResponseTiming(
timings: FetchTimings | undefined,
startTime: number,
timestamp?: number
): { endTime: number; time: number } {
if (
timings &&
isFiniteNumber(timings.requestTime) &&
isFiniteNumber(timings.responseEnd) &&
timings.responseEnd > timings.requestTime
) {
const time = Math.round(timings.responseEnd - timings.requestTime)
return { endTime: startTime + time, time }
}
const endTime = Number(timestamp ?? Date.now())
return { endTime, time: Math.max(0, endTime - startTime) }
}
/** The browser-clock start, only when the event carried one. */
function wallTimeOf(value: unknown): { wallTime?: number } {
return isFiniteNumber(value) ? { wallTime: Number(value) } : {}
}
/** Rows a session has seen, by request id: a later event for the same request
* (a redirect hop, a late failure) continues the row rather than adding one. */
const MAX_TRACKED_REQUESTS = 5000
function remember(
rows: Map<string, NetworkRequest>,
id: string,
entry: NetworkRequest
): void {
rows.set(id, entry)
if (rows.size <= MAX_TRACKED_REQUESTS) {
return
}
// A row still open keeps its place: its answer has somewhere to land.
for (const [key, row] of rows) {
if (row.status !== undefined) {
rows.delete(key)
return
}
}
rows.delete(rows.keys().next().value as string)
}
export function handleBidiRequestSent(
rawEvent: unknown,
rows: Map<string, NetworkRequest>,
sinks: BidiHandlerSinks,
log: BidiLogger
): void {
const event = rawEvent as BeforeRequestSentEvent
try {
const requestId = String(event?.request?.request ?? event?.id ?? '')
// selenium-webdriver's NetworkInspector hands every network event to every
// subscriber's callback, so response and failure events reach this
// handler too; taken as requests they pushed or reopened rows.
const shape = rawEvent as { response?: unknown; errorText?: unknown }
if (!requestId || shape?.response || shape?.errorText) {
return
}
const previous = rows.get(requestId)
const entry: NetworkRequest = {
id: requestId,
url: event?.request?.url ?? '',
method: event?.request?.method ?? 'GET',
requestHeaders: arrayHeadersToObject(event?.request?.headers),
// A redirect hop re-sends the request under the same id: one row, which
// keeps the clock of the hop it follows and reopens for the next answer.
timestamp: previous?.timestamp ?? Date.now(),
startTime: previous?.startTime ?? Number(event?.timestamp ?? Date.now()),
...wallTimeOf(previous?.wallTime ?? event?.timestamp),
type: getRequestType(event?.request?.url ?? '')
}
remember(rows, requestId, entry)
if (previous) {
sinks.replaceNetworkRequest(requestId, entry)
} else {
sinks.pushNetworkRequest(entry)
}
} catch (err) {
log('warn', `beforeRequestSent threw: ${errorMessage(err)}`)
}
}
export function handleBidiResponseCompleted(
rawEvent: unknown,
rows: Map<string, NetworkRequest>,
sinks: BidiHandlerSinks,
log: BidiLogger
): void {
const event = rawEvent as ResponseCompletedEvent
try {
const requestId = String(event?.request?.request ?? event?.id ?? '')
const previous = rows.get(requestId)
// A request event delivered here (see handleBidiRequestSent) carries no
// response; finalizing on it emptied the row.
if (!previous || !event?.response) {
return
}
const { endTime, time } = bidiResponseTiming(
event?.request?.timings,
previous.startTime,
event?.timestamp
)
const finalized: NetworkRequest = {
...previous,
status: Number(event?.response?.status) || previous.status,
statusText: event?.response?.statusText ?? previous.statusText,
responseHeaders: arrayHeadersToObject(event?.response?.headers),
type: getRequestType(previous.url, event?.response?.mimeType),
endTime,
time,
size: Number(event?.response?.bytesReceived) || undefined
}
remember(rows, requestId, finalized)
sinks.replaceNetworkRequest(requestId, finalized)
} catch (err) {
log('warn', `responseCompleted threw: ${errorMessage(err)}`)
}
}
interface FetchErrorEvent {
request?: { request?: string; timings?: FetchTimings }
id?: string
timestamp?: number
errorText?: string
}
/** A request the browser gave up on: closed as a failure, as the WDIO service
* closes its own, so it never sits in the Network tab as pending. */
export function handleBidiFetchError(
rawEvent: unknown,
rows: Map<string, NetworkRequest>,
sinks: BidiHandlerSinks,
log: BidiLogger
): void {
const event = rawEvent as FetchErrorEvent
try {
const requestId = String(event?.request?.request ?? event?.id ?? '')
const previous = rows.get(requestId)
// The inspector delivers every network event here too; only a failure
// carries `errorText`.
if (!previous || typeof event?.errorText !== 'string') {
return
}
const { endTime, time } = bidiResponseTiming(
event?.request?.timings,
previous.startTime,
event?.timestamp
)
const errorText = event?.errorText ?? 'Failed'
const finalized: NetworkRequest = {
...previous,
status: 0,
statusText: errorText,
error: errorText,
endTime,
time
}
remember(rows, requestId, finalized)
sinks.replaceNetworkRequest(requestId, finalized)
} catch (err) {
log('warn', `fetchError threw: ${errorMessage(err)}`)
}
}
/** A failed fetch closes its row, or it would stay pending forever: through
* the inspector's own hook, or off the socket for an inspector without one. */
async function subscribeFetchErrors(
inspector: NetworkInspector,
rows: Map<string, NetworkRequest>,
sinks: BidiHandlerSinks,
log: BidiLogger
): Promise<void> {
if (typeof inspector.fetchError === 'function') {
await inspector.fetchError((e) => handleBidiFetchError(e, rows, sinks, log))
return
}
const bidi = inspector.bidi
if (typeof bidi?.subscribe !== 'function' || !bidi.socket?.on) {
return
}
await bidi.subscribe('network.fetchError')
bidi.socket.on('message', (raw) => {
try {
const { method, params } = JSON.parse(String(raw)) as {
method?: string
params?: unknown
}
if (method === 'network.fetchError') {
handleBidiFetchError(params, rows, sinks, log)
}
} catch {
/* a frame that is not JSON is not an event */
}
})
}
export async function attachNetworkInspector(
driver: unknown,
factory: InspectorFactory,
sinks: BidiHandlerSinks,
log: BidiLogger
): Promise<boolean> {
try {
const inspector = (await factory(driver)) as NetworkInspector
const rows = new Map<string, NetworkRequest>()
await inspector.beforeRequestSent((e) =>
handleBidiRequestSent(e, rows, sinks, log)
)
await inspector.responseCompleted((e) =>
handleBidiResponseCompleted(e, rows, sinks, log)
)
try {
await subscribeFetchErrors(inspector, rows, sinks, log)
} catch (err) {
// The inspector is attached either way; only failed requests stay pending.
log('warn', `BiDi network.fetchError unavailable: ${errorMessage(err)}`)
}
log('info', '✓ BiDi NetworkInspector attached (request + response)')
return true
} catch (err) {
log('warn', `BiDi NetworkInspector attach failed: ${errorMessage(err)}`)
return false
}
}
/**
* Attach the selenium-webdriver BiDi LogInspector + NetworkInspector to a
* driver and route their events into the given sinks. Returns `true` when at
* least one inspector connected — caller can disable the equivalent
* script-injection collectors to avoid duplicates.
*
* Tolerant of older / non-BiDi runtimes: if either submodule fails to load
* or the inspector factory throws (driver session doesn't have webSocketUrl
* capability set, etc.), the corresponding stream is silently skipped and
* the function returns false.
*
* @param onLog Optional callback for adapter-side logging. Receives ('info' |
* 'warn', message) on lifecycle events. Default: silent — adapters wire their
* own logger when they want visibility into BiDi attach state.
*/
export async function attachBidiHandlers(
driver: unknown,
sinks: BidiHandlerSinks,
onLog?: (level: 'info' | 'warn', message: string) => void
): Promise<boolean> {
const log: BidiLogger = (level, message) => onLog?.(level, message)
const logFactory =
loadSeleniumSubmodule<InspectorFactory>('bidi/logInspector')
const networkFactory = loadSeleniumSubmodule<InspectorFactory>(
'bidi/networkInspector'
)
let attached = 0
if (typeof logFactory === 'function') {
if (await attachLogInspector(driver, logFactory, sinks, log)) {
attached++
}
} else {
log('info', 'selenium-webdriver/bidi/logInspector not available — skipping')
}
if (typeof networkFactory === 'function') {
if (await attachNetworkInspector(driver, networkFactory, sinks, log)) {
attached++
}
} else {
log(
'info',
'selenium-webdriver/bidi/networkInspector not available — skipping'
)
}
return attached > 0
}
/**
* Flatten BiDi's `Array<{name, value:{value|type}}>` header shape to a
* lowercased `Record<string, string>`. Exported so adapter-side helpers can
* reuse it for their own header normalization.
*/
export function arrayHeadersToObject(
headers: unknown
): Record<string, string> | undefined {
if (!Array.isArray(headers)) {
return undefined
}
const out: Record<string, string> = {}
for (const h of headers as Array<{
name?: string
value?: string | { value?: string; type?: string }
}>) {
const name = String(h?.name ?? '').toLowerCase()
if (!name) {
continue
}
const v = h?.value
out[name] =
typeof v === 'string'
? v
: typeof (v as { value?: string })?.value === 'string'
? (v as { value: string }).value
: JSON.stringify(v ?? '')
}
return out
}