Skip to content
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

fix: ensure proper browser closure #18404

Merged
merged 1 commit into from
Feb 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/routes/aip/journal-pupp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const handler = async (ctx) => {
false
);

browser.close();
await browser.close();

return {
title: jrnlName,
Expand Down
2 changes: 1 addition & 1 deletion lib/routes/alternativeto/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const puppeteerGet = (url, cache) =>
waitUntil: 'domcontentloaded',
});
const html = await page.evaluate(() => document.documentElement.innerHTML);
browser.close();
await browser.close();
return html;
});

Expand Down
2 changes: 1 addition & 1 deletion lib/routes/apkpure/versions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ async function handler(ctx) {
});

const r = await page.evaluate(() => document.documentElement.innerHTML);
browser.close();
await browser.close();

const $ = load(r);
const img = new URL($('.ver-top img').attr('src'));
Expand Down
2 changes: 1 addition & 1 deletion lib/routes/colamanga/manga.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ async function handler(ctx: Context) {
});

const response = await page.content();
browser.close();
await browser.close();

const $ = load(response);

Expand Down
2 changes: 1 addition & 1 deletion lib/routes/douyin/live.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ async function handler(ctx) {
await page.goto(pageUrl, {
waitUntil: 'networkidle2',
});
browser.close();
await browser.close();

return roomInfo;
},
Expand Down
2 changes: 1 addition & 1 deletion lib/routes/douyin/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ async function handler(ctx) {
waitUntil: 'networkidle2',
});

browser.close();
await browser.close();

if (!postData) {
throw new Error('Empty post data. The request may be filtered by WAF.');
Expand Down
2 changes: 1 addition & 1 deletion lib/routes/fortnite/news.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ async function handler(ctx) {
});

await page.close();
browser.close();
await browser.close();

const { blogList: list } = data;
const items = await Promise.all(
Expand Down
2 changes: 1 addition & 1 deletion lib/routes/gov/customs/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ async function handler(ctx) {
)
);

browser.close();
await browser.close();

return {
title: `中国海关-${channelName}`,
Expand Down
2 changes: 1 addition & 1 deletion lib/routes/gov/pbc/goutongjiaoliu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ async function handler() {
)
);

browser.close();
await browser.close();

return {
title: '中国人民银行 - 沟通交流',
Expand Down
2 changes: 1 addition & 1 deletion lib/routes/gov/pbc/trade-announcement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ async function handler() {
)
);

browser.close();
await browser.close();

return {
title: '中国人民银行 - 货币政策司公开市场交易公告',
Expand Down
2 changes: 1 addition & 1 deletion lib/routes/hitcon/zeroday.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ async function handler(ctx: Context): Promise<Data> {
});

const response = await page.evaluate(() => document.documentElement.innerHTML);
browser.close();
await browser.close();

const $ = load(response);
const items: DataItem[] = $('.zdui-strip-list>li')
Expand Down
4 changes: 2 additions & 2 deletions lib/routes/hottoys/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ async function handler() {
waitUntil: 'domcontentloaded',
});
const response = await page.content();
page.close();
await page.close();
const $ = load(response);
const items = $('li.productListItem')
.toArray()
Expand All @@ -54,7 +54,7 @@ async function handler() {
guid: a.attr('href'),
};
});
browser.close();
await browser.close();
return {
title: 'Hot Toys New Products',
link: baseUrl,
Expand Down
2 changes: 1 addition & 1 deletion lib/routes/ielts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ async function handler() {
await page.waitForSelector('div.container');

const html = await page.evaluate(() => document.documentElement.innerHTML);
browser.close();
await browser.close();
return html;
},
config.cache.routeExpire,
Expand Down
2 changes: 1 addition & 1 deletion lib/routes/iqiyi/video.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ async function handler(ctx) {
config.cache.routeExpire,
false
);
browser.close();
await browser.close();

return data;
}
2 changes: 1 addition & 1 deletion lib/routes/njust/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ async function getContent(url, pptr = false) {
const content = await page.content();
return content;
} finally {
browser.close();
await browser.close();
}
} else {
const response = await got(url);
Expand Down
2 changes: 1 addition & 1 deletion lib/routes/nytimes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ async function handler(ctx) {
})
);

browser.close();
await browser.close();

return {
title,
Expand Down
2 changes: 1 addition & 1 deletion lib/routes/oceanengine/arithmetic-index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ async function handler(ctx) {
});
await page.goto('https://trendinsight.oceanengine.com/arithmetic-index');
const res = await getMultiKeywordHotTrend(page, keyword, start_date, end_date, channel);
browser.close();
await browser.close();

const rawData = JSON.parse(res).data;
const data = decrypt(rawData).hot_list[0];
Expand Down
2 changes: 1 addition & 1 deletion lib/routes/parliament.uk/commonslibrary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ async function handler(ctx) {
description: $(article).find('p').last().text().trim(),
pubDate: timezone($(article).find('.card__date time').attr('datetime')),
}));
browser.close();
await browser.close();
return {
title: `parliament - lordslibrary - ${topic}`,
link: url,
Expand Down
2 changes: 1 addition & 1 deletion lib/routes/parliament.uk/lordslibrary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ async function handler(ctx) {
description: $(article).find('p').last().text().trim(),
pubDate: timezone($(article).find('.card__date time').attr('datetime')),
}));
browser.close();
await browser.close();
return {
title: `parliament - lordslibrary - ${topic}`,
link: url,
Expand Down
2 changes: 1 addition & 1 deletion lib/routes/pincong/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const puppeteerGet = (url, cache) =>
waitUntil: 'domcontentloaded',
});
const html = await page.evaluate(() => document.documentElement.innerHTML);
browser.close();
await browser.close();
return html;
});

Expand Down
2 changes: 1 addition & 1 deletion lib/routes/pnas/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ async function handler(ctx) {
)
);

browser.close();
await browser.close();

return {
title: `${$('.banner-widget__content h1').text()} - PNAS`,
Expand Down
4 changes: 2 additions & 2 deletions lib/routes/science/blogs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ async function handler(ctx) {

const response = await page.content();

page.close();
browser.close();
await page.close();
await browser.close();
return response;
},
config.cache.routeExpire,
Expand Down
2 changes: 1 addition & 1 deletion lib/routes/sehuatang/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ async function handler(ctx) {

await Promise.all([page.click('a.enter-btn'), page.waitForNavigation({ waitUntil: 'domcontentloaded' })]);
const response = await page.content();
page.close();
await page.close();

const $ = load(response);

Expand Down
2 changes: 1 addition & 1 deletion lib/routes/spankbang/new-videos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ const handler = async () => {
false
);

browser.close();
await browser.close();

return {
title: data.title,
Expand Down
2 changes: 1 addition & 1 deletion lib/routes/sysu/ygafz.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ async function handler(ctx) {
return jar;
}, cookieJar);

browser.close();
await browser.close();

const $ = load(response);
const list = $('.list-content a')
Expand Down
2 changes: 1 addition & 1 deletion lib/routes/tiktok/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ async function handler(ctx) {
waitUntil: 'networkidle0',
});
const SIGI_STATE = await page.evaluate(() => window.SIGI_STATE);
browser.close();
await browser.close();

const lang = SIGI_STATE.AppContext.lang;
const SharingMetaState = SIGI_STATE.SharingMetaState;
Expand Down
6 changes: 3 additions & 3 deletions lib/routes/uchicago/current.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ async function handler(ctx) {
});
const response = await page.evaluate(() => document.documentElement.innerHTML);
const cookies = await getCookies(page);
page.close();
await page.close();
const $ = load(response);

const list = $('.issue-item__title')
Expand All @@ -68,7 +68,7 @@ async function handler(ctx) {
referer: link,
});
const response = await page.evaluate(() => document.documentElement.innerHTML);
page.close();
await page.close();

const $ = load(response);

Expand All @@ -94,7 +94,7 @@ async function handler(ctx) {
)
);

browser.close();
await browser.close();

return {
title: $('head title').text(),
Expand Down
4 changes: 2 additions & 2 deletions lib/routes/uraaka-joshi/uraaka-joshi-user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ async function handler(ctx) {
page.on('request', (request) => {
request.resourceType() === 'document' || request.resourceType() === 'script' || request.resourceType() === 'fetch' ? request.continue() : request.abort();
});
page.on('requestfinished', (request) => {
page.on('requestfinished', async (request) => {
if (request.url() === link && request.response().status() === 403) {
page.close();
await page.close();
}
});

Expand Down
88 changes: 42 additions & 46 deletions lib/routes/uraaka-joshi/uraaka-joshi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ async function handler() {
page.on('request', (request) => {
request.resourceType() === 'document' || request.resourceType() === 'script' || request.resourceType() === 'fetch' ? request.continue() : request.abort();
});
page.on('requestfinished', (request) => {
page.on('requestfinished', async (request) => {
if (request.url() === link && request.response().status() === 403) {
page.close();
await page.close();
}
});

Expand All @@ -57,49 +57,45 @@ async function handler() {
return {
title,
link,
item:
list &&
list
.map((index, item) => {
item = $(item);

// remove event and styles
item.find('*').removeAttr('onclick');
item.find('*').removeAttr('onerror');
item.find('*').removeAttr('style');

// format account style
const account = item.find('.account-group-link-row');
account.html(account.text());

// extract video tag from its player
item.find('.plyr--video').each((_, player) => {
player = $(player);

const video = player.find('video');
player.replaceWith(video);
const poster = video.attr('data-poster');
video.attr('poster', 'https:' + poster);

const source = video.find('source');
const src = source.attr('src');
source.attr('src', 'https:' + src);
});

// correct src of img tags
item.find('img').each((_, image) => {
const src = $(image).attr('data-src');
$(image).attr('src', 'https:' + src);
});

return {
title: item.find('.account-group').text() + ` - ${title}`,
description: item.html(),
link: item.find('.account-group-link-row').attr('href'),
pubDate: parseDate(item.find('.profile-char').attr('datetime')),
guid: item.find('a.tap-image').attr('data-tweet-id') || item.find('video[class^="js-player-"]').attr('data-tweet-id') || parseDate(item.find('.profile-char').attr('datetime')).getTime(),
};
})
.get(),
item: list.toArray().map((item) => {
item = $(item);

// remove event and styles
item.find('*').removeAttr('onclick');
item.find('*').removeAttr('onerror');
item.find('*').removeAttr('style');

// format account style
const account = item.find('.account-group-link-row');
account.html(account.text());

// extract video tag from its player
item.find('.plyr--video').each((_, player) => {
player = $(player);

const video = player.find('video');
player.replaceWith(video);
const poster = video.attr('data-poster');
video.attr('poster', 'https:' + poster);

const source = video.find('source');
const src = source.attr('src');
source.attr('src', 'https:' + src);
});

// correct src of img tags
item.find('img').each((_, image) => {
const src = $(image).attr('data-src');
$(image).attr('src', 'https:' + src);
});

return {
title: item.find('.account-group').text() + ` - ${title}`,
description: item.html(),
link: item.find('.account-group-link-row').attr('href'),
pubDate: parseDate(item.find('.profile-char').attr('datetime')),
guid: item.find('a.tap-image').attr('data-tweet-id') || item.find('video[class^="js-player-"]').attr('data-tweet-id') || parseDate(item.find('.profile-char').attr('datetime')).getTime(),
};
}),
};
}
Loading