html-get 2.28.1 → 2.28.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.d.ts +2 -0
- package/package.json +1 -1
- package/src/index.js +43 -0
package/index.d.ts
CHANGED
|
@@ -16,6 +16,8 @@ declare namespace htmlGet {
|
|
|
16
16
|
statusCode: number | undefined
|
|
17
17
|
/** Redirect history */
|
|
18
18
|
redirects: Array<{ statusCode: number; url: string }>
|
|
19
|
+
/** Set when the body stopped arriving (a timeout or a dropped connection): `html` is what arrived before it */
|
|
20
|
+
partial?: true
|
|
19
21
|
/** Statistics about the request */
|
|
20
22
|
stats: {
|
|
21
23
|
mode: 'fetch' | 'prerender'
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "html-get",
|
|
3
3
|
"description": "Get the HTML from any website, fine-tuned for correction & speed",
|
|
4
4
|
"homepage": "https://nicedoc.com/microlinkhq/html-get",
|
|
5
|
-
"version": "2.28.
|
|
5
|
+
"version": "2.28.2",
|
|
6
6
|
"types": "index.d.ts",
|
|
7
7
|
"main": "src/index.js",
|
|
8
8
|
"bin": {
|
package/src/index.js
CHANGED
|
@@ -9,6 +9,7 @@ const PCancelable = require('p-cancelable')
|
|
|
9
9
|
const { AbortError } = require('p-retry')
|
|
10
10
|
const htmlEncode = require('html-encode')
|
|
11
11
|
const got = require('@kikobeats/got')
|
|
12
|
+
const { isResponseOk } = require('@kikobeats/got/dist/source/core/utils/is-response-ok')
|
|
12
13
|
const crypto = require('crypto')
|
|
13
14
|
const $ = require('tinyspawn')
|
|
14
15
|
const path = require('path')
|
|
@@ -43,6 +44,42 @@ const inheritRequestFragment = (options, response) => {
|
|
|
43
44
|
if (!options.url.href.includes('#')) options.url.hash = new URL(response.url).hash
|
|
44
45
|
}
|
|
45
46
|
|
|
47
|
+
const HTML_DOCUMENT_TYPES = ['text/html', 'application/xhtml+xml']
|
|
48
|
+
|
|
49
|
+
const resolvesWithBody = res => isResponseOk(res) || !res.request.options.throwHttpErrors
|
|
50
|
+
|
|
51
|
+
const resolvesWithHtml = res =>
|
|
52
|
+
HTML_DOCUMENT_TYPES.includes(getContentType(res.headers)) && resolvesWithBody(res)
|
|
53
|
+
|
|
54
|
+
const recordHtmlBody = req => {
|
|
55
|
+
let chunks = []
|
|
56
|
+
req.on('request', () => {
|
|
57
|
+
chunks = []
|
|
58
|
+
})
|
|
59
|
+
req.on('response', res => {
|
|
60
|
+
if (resolvesWithHtml(res)) {
|
|
61
|
+
res.on('data', chunk => chunks.push(chunk))
|
|
62
|
+
}
|
|
63
|
+
})
|
|
64
|
+
return () => Buffer.concat(chunks)
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const isBodyCut = error => error instanceof got.TimeoutError || error instanceof got.ReadError
|
|
68
|
+
|
|
69
|
+
const readCutHtml = async (error, readHtmlBody, toEncode) => {
|
|
70
|
+
if (!isBodyCut(error)) return
|
|
71
|
+
const body = readHtmlBody()
|
|
72
|
+
if (body.length === 0) return
|
|
73
|
+
try {
|
|
74
|
+
return await toEncode(body, error.response.headers['content-type'])
|
|
75
|
+
} catch (encodeError) {
|
|
76
|
+
debug('fetch:partial:error', {
|
|
77
|
+
url: error.response.url,
|
|
78
|
+
message: encodeError?.message || encodeError
|
|
79
|
+
})
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
46
83
|
const fetch = PCancelable.fn(
|
|
47
84
|
async (
|
|
48
85
|
url,
|
|
@@ -100,6 +137,7 @@ const fetch = PCancelable.fn(
|
|
|
100
137
|
|
|
101
138
|
const redirects = []
|
|
102
139
|
req.on('redirect', res => redirects.push({ statusCode: res.statusCode, url: res.url }))
|
|
140
|
+
const readHtmlBody = recordHtmlBody(req)
|
|
103
141
|
|
|
104
142
|
try {
|
|
105
143
|
const res = await req
|
|
@@ -194,6 +232,11 @@ const fetch = PCancelable.fn(
|
|
|
194
232
|
headers: error.response ? error.response.headers : {},
|
|
195
233
|
statusCode: error.response ? error.response.statusCode : undefined
|
|
196
234
|
}
|
|
235
|
+
const cutHtml = await readCutHtml(error, readHtmlBody, toEncode)
|
|
236
|
+
if (cutHtml) {
|
|
237
|
+
debug('fetch:partial', { url, bytes: Buffer.byteLength(cutHtml), reflect })
|
|
238
|
+
return { ...answer, html: cutHtml, partial: true, mode: 'fetch', redirects }
|
|
239
|
+
}
|
|
197
240
|
return reflect
|
|
198
241
|
? { isRejected: true, error, ...answer }
|
|
199
242
|
: { ...answer, html: '', mode: 'fetch', redirects }
|