Repository navigation
Introduce Page.onResponse
#3356
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
base: main
Are you sure you want to change the base?
Changes from all commits
0c99838
b788c1a
e29740b
0c710c6
09fdf4d
6b33a7f
1bd18a4
1f58cc3
8c230f2
cacc01c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| import { Protocol } from "devtools-protocol"; | ||
| import type { Response } from "../../understudy/response.js"; | ||
|
|
||
| /** Metadata tracked for each network request currently in-flight. */ | ||
| export type NetworkRequestInfo = { | ||
|
|
@@ -15,9 +16,10 @@ export type NetworkRequestInfo = { | |
|
|
||
| /** Callback hooks consumers can implement to observe network transitions. */ | ||
| export interface NetworkObserver { | ||
| onRequestStarted(info: NetworkRequestInfo): void; | ||
| onRequestFinished(info: NetworkRequestInfo): void; | ||
| onRequestFailed(info: NetworkRequestInfo): void; | ||
| onRequestStarted?(info: NetworkRequestInfo): void; | ||
| onRequestFinished?(info: NetworkRequestInfo): void; | ||
| onRequestFailed?(info: NetworkRequestInfo): void; | ||
| onResponse?(response: Response): void; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't like a lot of optionals, but better than breaking out |
||
| } | ||
|
|
||
| /** Options for the idle waiter helper. */ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,7 @@ | ||
| import type { Protocol } from "devtools-protocol"; | ||
| import type { CDPSessionLike } from "./cdp.js"; | ||
| import type { Page } from "./page.js"; | ||
| import { Response } from "./response.js"; | ||
| import { | ||
| DEFAULT_IDLE_WAIT, | ||
| IGNORED_RESOURCE_TYPES, | ||
|
|
@@ -23,6 +25,10 @@ import { | |
| * Aggregates network information for all CDP sessions owned by a Page. | ||
| */ | ||
| export class NetworkManager { | ||
| private readonly pendingResponses = new Set<Response>(); | ||
|
|
||
| constructor(private readonly page: Page) {} | ||
|
|
||
| readonly sessions = new Map< | ||
| string, | ||
| { | ||
|
|
@@ -114,6 +120,7 @@ export class NetworkManager { | |
|
|
||
| const onResponse = (evt: Protocol.Network.ResponseReceivedEvent) => { | ||
| if (!evt?.requestId) return; | ||
| this.emitResponse(evt, session); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2: For Prompt for AI agents
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| const url = evt.response?.url ?? ""; | ||
| if (url.startsWith("data:")) finish(evt.requestId); | ||
| }; | ||
|
|
@@ -163,6 +170,7 @@ export class NetworkManager { | |
| const entry = this.sessions.get(sid); | ||
| if (!entry) return; | ||
| entry.detach(); | ||
| this.disposeResponsesForSession(entry.session); | ||
| this.sessions.delete(sid); | ||
|
|
||
| for (const key of this.requests.keys()) { | ||
|
|
@@ -302,30 +310,66 @@ export class NetworkManager { | |
| for (const { detach } of this.sessions.values()) { | ||
| detach(); | ||
| } | ||
| for (const response of this.pendingResponses) response.dispose(); | ||
| this.pendingResponses.clear(); | ||
| this.sessions.clear(); | ||
| this.observers.clear(); | ||
| this.requests.clear(); | ||
| this.documentRequestsByFrame.clear(); | ||
| } | ||
|
|
||
| public subscribeResponse(listener: (response: Response) => void): () => void { | ||
| return this.addObserver({ onResponse: listener }); | ||
| } | ||
|
|
||
| /** Materialize one response for all interested observers; body retrieval stays lazy. */ | ||
| private emitResponse( | ||
| event: Protocol.Network.ResponseReceivedEvent, | ||
| session: CDPSessionLike, | ||
| ): void { | ||
| if (![...this.observers].some((observer) => typeof observer.onResponse === "function")) return; | ||
| const response = new Response({ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2: Prompt for AI agents
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thinking we omit |
||
| page: this.page, | ||
| session, | ||
| requestId: event.requestId, | ||
| frameId: event.frameId, | ||
| loaderId: event.loaderId, | ||
| response: event.response, | ||
| fromServiceWorker: Boolean(event.response.fromServiceWorker), | ||
| }); | ||
| this.pendingResponses.add(response); | ||
| void response.finished().then(() => this.pendingResponses.delete(response)); | ||
| for (const observer of this.observers) { | ||
| observer.onResponse?.(response); | ||
| } | ||
| } | ||
|
|
||
| private disposeResponsesForSession(session: CDPSessionLike): void { | ||
| for (const response of this.pendingResponses) { | ||
| if (response.session !== session) continue; | ||
| response.dispose(new Error("Response session was detached")); | ||
| this.pendingResponses.delete(response); | ||
| } | ||
| } | ||
|
|
||
| /** Fan-out helper when a tracked request starts. */ | ||
| emitStart(info: NetworkRequestInfo): void { | ||
| for (const obs of this.observers) { | ||
| obs.onRequestStarted(info); | ||
| obs.onRequestStarted?.(info); | ||
| } | ||
| } | ||
|
|
||
| /** Fan-out helper when a tracked request completes successfully. */ | ||
| emitFinish(info: NetworkRequestInfo): void { | ||
| for (const obs of this.observers) { | ||
| obs.onRequestFinished(info); | ||
| obs.onRequestFinished?.(info); | ||
| } | ||
| } | ||
|
|
||
| /** Fan-out helper when a tracked request fails mid-flight. */ | ||
| emitFailure(info: NetworkRequestInfo): void { | ||
| for (const obs of this.observers) { | ||
| obs.onRequestFailed(info); | ||
| obs.onRequestFailed?.(info); | ||
| } | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
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.
My understanding is that SDK client can operate over many tabs, which means the chromium-supplied request identifier isn't unique can can collide across tabs. So we made this
ResponseHandleTableto act as a global lookup.Might be just me, but I did find our this custom-created
responseIda bit confusing as a first time developer. At first, I thought it was referring to the chromium-supplied identifiers. It's not a big deal, but I do wonder if we could avoid creating custom IDs and instead use a compoundCDP_SESSION_ID-CDP_NETWORK_REQUEST_ID, which would be globally identifiable and unique across tabs.