Skip to content
This repository was archived by the owner on Feb 27, 2026. It is now read-only.

Commit 7aa3b5c

Browse files
author
Philipp Giese
committed
fix: correctly remove listeners from page and network
1 parent cf60957 commit 7aa3b5c

1 file changed

Lines changed: 22 additions & 23 deletions

File tree

src/captureNetwork.ts

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ import { Page } from "puppeteer"
33
import { Har } from "har-format"
44

55
// event types to observe
6-
const page_observe = [
6+
const pageEventsToObserve = [
77
"Page.loadEventFired",
88
"Page.domContentEventFired",
99
"Page.frameStartedLoading",
1010
"Page.frameAttached",
1111
"Page.frameScheduledNavigation",
1212
]
1313

14-
const network_observe = [
14+
const networkEventsToObserve = [
1515
"Network.requestWillBeSent",
1616
"Network.requestServedFromCache",
1717
"Network.dataReceived",
@@ -45,10 +45,8 @@ export async function captureNetwork(
4545
captureMimeTypes = ["text/html", "application/json"],
4646
}: CaptureOptions = {}
4747
): Promise<StopFn> {
48-
let inProgress = true
49-
50-
const page_events: PageEvent[] = []
51-
const network_events: NetworkEvent[] = []
48+
const pageEvents: PageEvent[] = []
49+
const networkEvents: NetworkEvent[] = []
5250

5351
const pendingRequests: Promise<void>[] = []
5452

@@ -57,23 +55,19 @@ export async function captureNetwork(
5755
await client.send("Page.enable")
5856
await client.send("Network.enable")
5957

60-
page_observe.forEach((method) => {
61-
client.on(method, (params) => {
62-
if (!inProgress) {
63-
return
64-
}
58+
const pageListeners = pageEventsToObserve.map((method) => {
59+
const callback = (params: any) => {
60+
pageEvents.push({ method, params })
61+
}
6562

66-
page_events.push({ method, params })
67-
})
68-
})
63+
client.on(method, callback)
6964

70-
network_observe.forEach((method) => {
71-
client.on(method, async (params) => {
72-
if (!inProgress) {
73-
return
74-
}
65+
return () => client.off(method, callback)
66+
})
7567

76-
network_events.push({ method, params })
68+
const networkListeners = networkEventsToObserve.map((method) => {
69+
const callback = async (params: any) => {
70+
networkEvents.push({ method, params })
7771

7872
if (!saveResponses || method !== "Network.responseReceived") {
7973
return
@@ -112,16 +106,21 @@ export async function captureNetwork(
112106
} finally {
113107
resolve()
114108
}
115-
})
109+
}
110+
111+
client.on(method, callback)
112+
113+
return () => client.off(method, callback)
116114
})
117115

118116
return async function getHar(): Promise<Har> {
119-
inProgress = false
117+
networkListeners.forEach((stopListening) => stopListening())
118+
pageListeners.forEach((stopListening) => stopListening())
120119

121120
await client.detach()
122121
await Promise.all(pendingRequests)
123122

124-
return harFromMessages(page_events.concat(network_events), {
123+
return harFromMessages(pageEvents.concat(networkEvents), {
125124
includeTextFromResponseBody: saveResponses,
126125
})
127126
}

0 commit comments

Comments
 (0)