html-get 2.27.12 → 2.27.14
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/package.json +2 -1
- package/src/html.js +16 -8
- package/src/index.js +46 -28
- package/src/util.js +11 -2
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.27.
|
|
5
|
+
"version": "2.27.14",
|
|
6
6
|
"types": "index.d.ts",
|
|
7
7
|
"main": "src/index.js",
|
|
8
8
|
"bin": {
|
|
@@ -107,6 +107,7 @@
|
|
|
107
107
|
"src"
|
|
108
108
|
],
|
|
109
109
|
"scripts": {
|
|
110
|
+
"benchmark": "node benchmark/index.js",
|
|
110
111
|
"clean": "rm -rf node_modules",
|
|
111
112
|
"contributors": "(npx git-authors-cli && npx finepack && git add package.json && git commit -m 'build: contributors' --no-verify) || true",
|
|
112
113
|
"lint": "standard",
|
package/src/html.js
CHANGED
|
@@ -108,22 +108,30 @@ const rewriteMetaTags = ({ $ }) => {
|
|
|
108
108
|
})
|
|
109
109
|
}
|
|
110
110
|
|
|
111
|
+
const URL_ATTRS_BY_TAG = {}
|
|
112
|
+
|
|
113
|
+
forEach(URL_TAGS, (tagNames, urlAttr) =>
|
|
114
|
+
tagNames.forEach(tagName => (URL_ATTRS_BY_TAG[tagName] ||= []).push(urlAttr))
|
|
115
|
+
)
|
|
116
|
+
|
|
117
|
+
const URL_TAGS_SELECTOR = Object.keys(URL_ATTRS_BY_TAG).join(',')
|
|
118
|
+
|
|
111
119
|
const rewriteHtmlUrls = ({ $, url }) => {
|
|
112
|
-
|
|
113
|
-
$(
|
|
114
|
-
|
|
120
|
+
$(URL_TAGS_SELECTOR).each(function () {
|
|
121
|
+
const el = $(this)
|
|
122
|
+
for (const urlAttr of URL_ATTRS_BY_TAG[this.tagName]) {
|
|
115
123
|
const attr = el.attr(urlAttr)
|
|
116
|
-
if (typeof attr !== 'string')
|
|
124
|
+
if (typeof attr !== 'string') continue
|
|
117
125
|
try {
|
|
118
126
|
const urlObj = new URL(attr, url)
|
|
119
|
-
if (!urlObj.protocol.startsWith('http'))
|
|
127
|
+
if (!urlObj.protocol.startsWith('http')) continue
|
|
120
128
|
if (isLocalAddress(urlObj.hostname)) {
|
|
121
129
|
el.remove()
|
|
122
|
-
|
|
123
|
-
el.attr(urlAttr, urlObj.toString())
|
|
130
|
+
return
|
|
124
131
|
}
|
|
132
|
+
el.attr(urlAttr, urlObj.toString())
|
|
125
133
|
} catch (_) {}
|
|
126
|
-
}
|
|
134
|
+
}
|
|
127
135
|
})
|
|
128
136
|
}
|
|
129
137
|
|
package/src/index.js
CHANGED
|
@@ -57,11 +57,12 @@ const fetch = PCancelable.fn(
|
|
|
57
57
|
const html = await (async () => {
|
|
58
58
|
const contentType = getContentType(res.headers)
|
|
59
59
|
|
|
60
|
-
const officeFormat =
|
|
60
|
+
const officeFormat = getOfficeFormat({ contentType, url: [res.url, url] })
|
|
61
|
+
const convertOffice = officeFormat && (await resolvePandoc(pandoc))
|
|
61
62
|
|
|
62
63
|
// a recognized office file never goes through mutool, even if the
|
|
63
64
|
// response is mislabeled as application/pdf
|
|
64
|
-
if (!
|
|
65
|
+
if (!convertOffice && contentType === 'application/pdf') {
|
|
65
66
|
pdfMeta = await extractSafe(res.body, res.url)
|
|
66
67
|
if (!mutool) return ''
|
|
67
68
|
const file = getTemporalFile(url, 'pdf')
|
|
@@ -86,11 +87,11 @@ const fetch = PCancelable.fn(
|
|
|
86
87
|
return ''
|
|
87
88
|
}
|
|
88
89
|
|
|
89
|
-
if (
|
|
90
|
+
if (convertOffice) {
|
|
90
91
|
const file = getTemporalFile(res.url, officeFormat)
|
|
91
92
|
await writeFile(file.path, res.body)
|
|
92
93
|
try {
|
|
93
|
-
const converted = await
|
|
94
|
+
const converted = await convertOffice(officeFormat, file.path)
|
|
94
95
|
if (converted) return converted
|
|
95
96
|
debug('pandoc:empty', { url: res.url, format: officeFormat })
|
|
96
97
|
} catch (error) {
|
|
@@ -288,45 +289,62 @@ const whichSync = bin => {
|
|
|
288
289
|
return whichCache.get(bin)
|
|
289
290
|
}
|
|
290
291
|
|
|
291
|
-
// The default
|
|
292
|
-
// call), so memoizeOne builds
|
|
293
|
-
// `--list-input-formats` probe and rebuilding the closure per request.
|
|
292
|
+
// The default mutool runner is wired as a default parameter (evaluated on every
|
|
293
|
+
// call), so memoizeOne builds it once instead of rebuilding it per request.
|
|
294
294
|
const defaultMutool = memoizeOne(() => {
|
|
295
295
|
const mutoolPath = whichSync('mutool')
|
|
296
296
|
if (!mutoolPath) return
|
|
297
297
|
return (...args) => $(`${mutoolPath} draw -q -F html ${args}`)
|
|
298
298
|
})
|
|
299
299
|
|
|
300
|
+
const PANDOC_LIST_FORMATS = ['--list-input-formats']
|
|
301
|
+
|
|
302
|
+
const createPandoc = (pandocPath, inputFormats) => {
|
|
303
|
+
const supported = new Set(inputFormats.toString().trim().split(/\s+/))
|
|
304
|
+
return async (format, filepath) => {
|
|
305
|
+
if (!supported.has(format)) return
|
|
306
|
+
// array form keeps `filepath` a single argv entry even if it has spaces
|
|
307
|
+
const { stdout } = await $(pandocPath, [
|
|
308
|
+
`--from=${format}`,
|
|
309
|
+
'--to=html',
|
|
310
|
+
'--standalone',
|
|
311
|
+
'--embed-resources',
|
|
312
|
+
filepath
|
|
313
|
+
])
|
|
314
|
+
return stdout.trim() ? stdout : undefined
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
// a broken/incompatible pandoc can throw while probing (or on the runner); in
|
|
319
|
+
// either case conversion stays disabled instead of breaking the request
|
|
300
320
|
const defaultPandoc = memoizeOne(() => {
|
|
301
321
|
try {
|
|
302
322
|
const pandocPath = whichSync('pandoc')
|
|
303
323
|
if (!pandocPath) return
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
const supported = new Set(
|
|
308
|
-
execFileSync(pandocPath, ['--list-input-formats'], {
|
|
324
|
+
return createPandoc(
|
|
325
|
+
pandocPath,
|
|
326
|
+
execFileSync(pandocPath, PANDOC_LIST_FORMATS, {
|
|
309
327
|
stdio: ['pipe', 'pipe', 'ignore']
|
|
310
328
|
})
|
|
311
|
-
.toString()
|
|
312
|
-
.trim()
|
|
313
|
-
.split(/\s+/)
|
|
314
329
|
)
|
|
315
|
-
return async (format, filepath) => {
|
|
316
|
-
if (!supported.has(format)) return
|
|
317
|
-
// array form keeps `filepath` a single argv entry even if it has spaces
|
|
318
|
-
const { stdout } = await $(pandocPath, [
|
|
319
|
-
`--from=${format}`,
|
|
320
|
-
'--to=html',
|
|
321
|
-
'--standalone',
|
|
322
|
-
'--embed-resources',
|
|
323
|
-
filepath
|
|
324
|
-
])
|
|
325
|
-
return stdout.trim() ? stdout : undefined
|
|
326
|
-
}
|
|
327
330
|
} catch (_) {}
|
|
328
331
|
})
|
|
329
332
|
|
|
333
|
+
// The pandoc probe spawns the binary (~30-100ms). Most processes never fetch an
|
|
334
|
+
// office document, so getHTML defers it to the first one and runs it async.
|
|
335
|
+
const loadDefaultPandoc = memoizeOne(async () => {
|
|
336
|
+
try {
|
|
337
|
+
const pandocPath = whichSync('pandoc')
|
|
338
|
+
if (!pandocPath) return
|
|
339
|
+
const { stdout } = await $(pandocPath, PANDOC_LIST_FORMATS)
|
|
340
|
+
return createPandoc(pandocPath, stdout)
|
|
341
|
+
} catch (_) {}
|
|
342
|
+
})
|
|
343
|
+
|
|
344
|
+
const DEFAULT_PANDOC = Symbol('defaultPandoc')
|
|
345
|
+
|
|
346
|
+
const resolvePandoc = pandoc => (pandoc === DEFAULT_PANDOC ? loadDefaultPandoc() : pandoc)
|
|
347
|
+
|
|
330
348
|
const getContent = PCancelable.fn(
|
|
331
349
|
(
|
|
332
350
|
url,
|
|
@@ -380,7 +398,7 @@ module.exports = PCancelable.fn(
|
|
|
380
398
|
gotOpts,
|
|
381
399
|
headers,
|
|
382
400
|
mutool = defaultMutool(),
|
|
383
|
-
pandoc =
|
|
401
|
+
pandoc = DEFAULT_PANDOC,
|
|
384
402
|
prerender = 'auto',
|
|
385
403
|
puppeteerOpts,
|
|
386
404
|
rewriteHtml = false,
|
package/src/util.js
CHANGED
|
@@ -3,7 +3,10 @@
|
|
|
3
3
|
const NullProtoObj = require('null-prototype-object')
|
|
4
4
|
const { parse } = require('content-type')
|
|
5
5
|
|
|
6
|
-
const
|
|
6
|
+
const CACHE_SIZE = 1000
|
|
7
|
+
|
|
8
|
+
let cache = new NullProtoObj()
|
|
9
|
+
let cacheSize = 0
|
|
7
10
|
|
|
8
11
|
const UNKNOWN_CONTENT_TYPE = { type: undefined, parameters: {} }
|
|
9
12
|
|
|
@@ -17,7 +20,13 @@ const parseContentType = contentType => {
|
|
|
17
20
|
|
|
18
21
|
const contentType = headers => {
|
|
19
22
|
const contentType = headers['content-type']
|
|
20
|
-
|
|
23
|
+
const cached = cache[contentType]
|
|
24
|
+
if (cached !== undefined) return cached
|
|
25
|
+
if (++cacheSize > CACHE_SIZE) {
|
|
26
|
+
cache = new NullProtoObj()
|
|
27
|
+
cacheSize = 1
|
|
28
|
+
}
|
|
29
|
+
return (cache[contentType] = parseContentType(contentType))
|
|
21
30
|
}
|
|
22
31
|
|
|
23
32
|
const getContentType = headers => contentType(headers).type
|