Skip to content

Commit 647fd9c

Browse files
committed
Bug Fixes: The page is not closed when there is an error in the crawlPage API
1 parent 2fc2afb commit 647fd9c

File tree

4 files changed

+41
-27
lines changed

4 files changed

+41
-27
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# [v4.0.1](https://github.com/coder-hxl/x-crawl/compare/v4.0.0...v4.0.1) (2023-03-30)
2+
3+
### 🐞 Bug Fixes
4+
5+
- The page is not closed when there is an error in the crawlPage API
6+
17
# [v4.0.0](https://github.com/coder-hxl/x-crawl/compare/v3.3.0...v4.0.0) (2023-03-27)
28

39
### 🚨 Breaking Changes

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"private": true,
33
"name": "x-crawl",
4-
"version": "4.0.0",
4+
"version": "4.0.1",
55
"author": "coderHXL",
66
"description": "x-crawl is a flexible nodejs crawler library.",
77
"license": "MIT",

publish/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "x-crawl",
3-
"version": "4.0.0",
3+
"version": "4.0.1",
44
"author": "coderHXL",
55
"description": "x-crawl is a flexible nodejs crawler library.",
66
"license": "MIT",

src/api.ts

+33-25
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import fs from 'node:fs'
22
import { writeFile } from 'node:fs/promises'
33
import path from 'node:path'
4-
import puppeteer, { Browser, Protocol } from 'puppeteer'
4+
import puppeteer, { Browser, HTTPResponse, Page, Protocol } from 'puppeteer'
55

66
import { useBatchCrawlHandleByMode } from './batchCrawlHandle'
77
import { request } from './request'
@@ -166,35 +166,43 @@ export function createCrawlPage(baseConfig: LoaderXCrawlBaseConfig) {
166166
}
167167

168168
async function crawlPageHandle(handleConfig: CrawlBaseConfigV1) {
169-
const page = await browser!.newPage()
170-
await page.setViewport({ width: 1280, height: 1024 })
169+
let page: Page | null = null
170+
let httpResponse: HTTPResponse | null = null
171+
172+
try {
173+
page = await browser!.newPage()
174+
await page.setViewport({ width: 1280, height: 1024 })
175+
176+
if (handleConfig.proxy) {
177+
await browser!.createIncognitoBrowserContext({
178+
proxyServer: handleConfig.proxy
179+
})
180+
} else {
181+
await browser!.createIncognitoBrowserContext({
182+
proxyServer: undefined
183+
})
184+
}
171185

172-
if (handleConfig.proxy) {
173-
await browser!.createIncognitoBrowserContext({
174-
proxyServer: handleConfig.proxy
175-
})
176-
} else {
177-
await browser!.createIncognitoBrowserContext({
178-
proxyServer: undefined
179-
})
180-
}
186+
if (handleConfig.headers) {
187+
await page.setExtraHTTPHeaders(
188+
handleConfig.headers as any as Record<string, string>
189+
)
190+
}
181191

182-
if (handleConfig.headers) {
183-
await page.setExtraHTTPHeaders(
184-
handleConfig.headers as any as Record<string, string>
185-
)
186-
}
192+
if (handleConfig.cookies) {
193+
await page.setCookie(
194+
...parseCrawlPageCookies(handleConfig.url, handleConfig.cookies)
195+
)
196+
}
187197

188-
if (handleConfig.cookies) {
189-
await page.setCookie(
190-
...parseCrawlPageCookies(handleConfig.url, handleConfig.cookies)
191-
)
198+
httpResponse = await page.goto(handleConfig.url, {
199+
timeout: handleConfig.timeout
200+
})
201+
} catch (error) {
202+
await page?.close()
203+
throw error
192204
}
193205

194-
const httpResponse = await page.goto(handleConfig.url, {
195-
timeout: handleConfig.timeout
196-
})
197-
198206
return { httpResponse, browser: browser!, page }
199207
}
200208

0 commit comments

Comments
 (0)