Repository navigation
Keep the status of a request the browser answered and then cancelled #662
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
vishnuv688
wants to merge
2
commits into
main
Choose a base branch
from
fix/bidi-started-response-status
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| '@wdio/devtools-service': patch | ||
| '@wdio/selenium-devtools': patch | ||
| '@wdio/nightwatch-devtools': patch | ||
| --- | ||
|
|
||
| A request the browser answered and then gave up on keeps the status it was answered with. Chrome stops loading a script whose response is a 404 and reports the request as `net::ERR_ABORTED`, so a missing script was listed as status 0 with no response headers. It is now listed as 404 with its response headers, with `net::ERR_ABORTED` as the error. A request that fails before any response, such as a blocked beacon, is still listed as status 0 with the error. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| /** | ||
| * 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 | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| import type { NetworkRequest } from '@wdio/devtools-shared' | ||
| import { arrayHeadersToObject } from './bidi-headers.js' | ||
| import { errorMessage } from './error.js' | ||
| import { getRequestType } from './net.js' | ||
| import { networkRowId } from './network-row-id.js' | ||
|
|
||
| interface ResponseStartedEvent { | ||
| redirectCount?: number | ||
| request?: { request?: string } | ||
| id?: string | ||
| response?: { | ||
| status?: number | ||
| statusText?: string | ||
| headers?: unknown | ||
| mimeType?: string | ||
| } | ||
| } | ||
|
|
||
| /** What `network.responseStarted` reported for a hop: kept until a fetch error | ||
| * closes that hop, because Chrome cancels a script body once its status is an | ||
| * error and BiDi then sends `fetchError`, not `responseCompleted`. */ | ||
| export interface StartedResponse { | ||
| status: number | ||
| statusText?: string | ||
| responseHeaders?: Record<string, string> | ||
| mimeType?: string | ||
| } | ||
|
|
||
| /** Started responses kept per session: a fetch error follows its response | ||
| * within milliseconds, so only the latest few can still be claimed. */ | ||
| const MAX_STARTED_RESPONSES = 256 | ||
|
|
||
| /** Keep a hop's started response, whether or not its request row exists yet: | ||
| * Chrome can deliver `responseStarted` before `beforeRequestSent`. */ | ||
| export function handleBidiResponseStarted( | ||
| rawEvent: unknown, | ||
| started: Map<string, StartedResponse>, | ||
| log: (level: 'warn', message: string) => void | ||
| ): void { | ||
| const event = rawEvent as ResponseStartedEvent | ||
| try { | ||
| const requestId = String(event?.request?.request ?? event?.id ?? '') | ||
| const status = Number(event?.response?.status) | ||
| if (!requestId || !status) { | ||
| return | ||
| } | ||
| started.set(networkRowId(requestId, event?.redirectCount), { | ||
| status, | ||
| statusText: event?.response?.statusText, | ||
| responseHeaders: arrayHeadersToObject(event?.response?.headers), | ||
| mimeType: event?.response?.mimeType | ||
| }) | ||
| if (started.size > MAX_STARTED_RESPONSES) { | ||
| started.delete(started.keys().next().value as string) | ||
| } | ||
| } catch (err) { | ||
| log('warn', `responseStarted threw: ${errorMessage(err)}`) | ||
| } | ||
| } | ||
|
|
||
| /** The status, headers and type a failed hop closes with: what its started | ||
| * response reported, else status 0 with the browser's error text. */ | ||
| export function failedRowFields( | ||
| url: string, | ||
| errorText: string, | ||
| answered: StartedResponse | undefined | ||
| ): Pick<NetworkRequest, 'status' | 'statusText' | 'error' | 'responseHeaders'> & | ||
| Partial<Pick<NetworkRequest, 'type'>> { | ||
| if (!answered) { | ||
| return { status: 0, statusText: errorText, error: errorText } | ||
| } | ||
| return { | ||
| status: answered.status, | ||
| statusText: answered.statusText ?? errorText, | ||
| error: errorText, | ||
| ...(answered.responseHeaders | ||
| ? { responseHeaders: answered.responseHeaders } | ||
| : {}), | ||
| type: getRequestType(url, answered.mimeType) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| /** 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. */ | ||
| export interface FetchTimings { | ||
| requestTime?: number | ||
| responseEnd?: number | ||
| } | ||
|
|
||
| const isFiniteNumber = (value: unknown): value is number => | ||
| typeof value === 'number' && Number.isFinite(value) | ||
|
|
||
| /** 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. */ | ||
| export 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. */ | ||
| export function wallTimeOf(value: unknown): { wallTime?: number } { | ||
| return isFiniteNumber(value) ? { wallTime: Number(value) } : {} | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Keeping the response status now hides browser cancellations after trace export.
networkRequestToHardropserror, andharToNetworkRequestdoes not restore it. A cancelled 404 script loses itsnet::ERR_ABORTEDreason. A response answered with 200 and then cancelled looks successful and no longer produces an OpenTelemetryrequest.failedevent.Preserve
errorthrough trace export and import, and make the telemetry check honor it.