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

Commit 778c04f

Browse files
committed
fix(memory): prevent unbounded growth of navigations array in PageCollector
1 parent 8c2a7fc commit 778c04f

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

src/PageCollector.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ 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;
6666

6767
/**
6868
* This maps a Page to a list of navigations with a sub-list
@@ -159,7 +159,7 @@ export class PageCollector<T> {
159159
}
160160
// Add the latest navigation first
161161
navigations.unshift([]);
162-
navigations.splice(this.#maxNavigationSaved);
162+
navigations.splice(this.maxNavigationSaved);
163163
}
164164

165165
protected cleanupPageDestroyed(page: Page) {
@@ -183,7 +183,7 @@ export class PageCollector<T> {
183183
}
184184

185185
const data: T[] = [];
186-
for (let index = this.#maxNavigationSaved; index >= 0; index--) {
186+
for (let index = this.maxNavigationSaved; index >= 0; index--) {
187187
if (navigations[index]) {
188188
data.push(...navigations[index]);
189189
}
@@ -409,5 +409,6 @@ export class NetworkCollector extends PageCollector<HTTPRequest> {
409409
} else {
410410
navigations.unshift([]);
411411
}
412+
navigations.splice(this.maxNavigationSaved);
412413
}
413414
}

tests/PageCollector.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,26 @@ describe('NetworkCollector', () => {
284284
page.emit('request', request);
285285
assert.equal(collector.getData(page, true).length, 3);
286286
});
287+
288+
it('should not grow beyond maxNavigationSaved', async () => {
289+
const collector = new NetworkCollector(browser);
290+
await collector.init([page]);
291+
292+
// Simulate 5 navigations (maxNavigationSaved is 3)
293+
for (let i = 0; i < 5; i++) {
294+
const req = getMockRequest({
295+
url: `http://example.com/nav${i}`,
296+
isNavigationRequest: true,
297+
frame: mainFrame,
298+
});
299+
page.emit('request', req);
300+
page.emit('framenavigated', mainFrame);
301+
}
302+
303+
// We expect 4 arrays in navigations (current + 3 saved)
304+
// Each navigation has 1 request, so total should be 4
305+
assert.equal(collector.getData(page, true).length, 4);
306+
});
287307
});
288308

289309
describe('ConsoleCollector', () => {

0 commit comments

Comments
 (0)