豆豆友情提示:这是一个非官方 GitHub 代理镜像,主要用于网络测试或访问加速。请勿在此进行登录、注册或处理任何敏感信息。进行这些操作请务必访问官方网站 github.com。 Raw 内容也通过此代理提供。
Skip to content

Commit 673f7bc

Browse files
committed
fix(memory): prevent unbounded growth of navigations array in PageCollector
1 parent d082ca4 commit 673f7bc

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

package-lock.json

Lines changed: 20 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/PageCollector.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ export class PageCollector<T> {
6262
collector: (item: T) => void,
6363
) => ListenerMap<PageEvents>;
6464
#listeners = new WeakMap<Page, ListenerMap>();
65-
#maxNavigationSaved = 3;
65+
protected maxNavigationSaved = 3;
66+
protected maxItemsPerNavigation = 5000;
6667

6768
/**
6869
* This maps a Page to a list of navigations with a sub-list
@@ -135,6 +136,9 @@ export class PageCollector<T> {
135136

136137
const navigations = this.storage.get(page) ?? [[]];
137138
navigations[0].push(withId);
139+
if (navigations[0].length > this.maxItemsPerNavigation) {
140+
navigations[0].shift(); // Remove oldest item to prevent memory leak
141+
}
138142
});
139143

140144
listeners['framenavigated'] = (frame: Frame) => {
@@ -159,7 +163,7 @@ export class PageCollector<T> {
159163
}
160164
// Add the latest navigation first
161165
navigations.unshift([]);
162-
navigations.splice(this.#maxNavigationSaved);
166+
navigations.splice(this.maxNavigationSaved);
163167
}
164168

165169
protected cleanupPageDestroyed(page: Page) {
@@ -183,7 +187,7 @@ export class PageCollector<T> {
183187
}
184188

185189
const data: T[] = [];
186-
for (let index = this.#maxNavigationSaved; index >= 0; index--) {
190+
for (let index = this.maxNavigationSaved; index >= 0; index--) {
187191
if (navigations[index]) {
188192
data.push(...navigations[index]);
189193
}
@@ -409,5 +413,6 @@ export class NetworkCollector extends PageCollector<HTTPRequest> {
409413
} else {
410414
navigations.unshift([]);
411415
}
416+
navigations.splice(this.maxNavigationSaved);
412417
}
413418
}

0 commit comments

Comments
 (0)