html-get 2.27.23 → 2.28.0
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/README.md +16 -1
- package/index.d.ts +3 -0
- package/package.json +1 -1
- package/src/html.js +1 -0
- package/src/index.js +71 -14
- package/src/pdf/first-image.js +20 -0
package/README.md
CHANGED
|
@@ -81,6 +81,15 @@ The target URL for getting the HTML markup.
|
|
|
81
81
|
|
|
82
82
|
#### options
|
|
83
83
|
|
|
84
|
+
##### documentTimeout
|
|
85
|
+
|
|
86
|
+
Type: `number`<br>
|
|
87
|
+
Default: the fetch timeout (`gotOpts.timeout`)
|
|
88
|
+
|
|
89
|
+
The time, in milliseconds from the start of the fetch, that a PDF or office document may take once its response says it is one. It covers mutool or pandoc, and the page by page image search. It only lengthens the fetch timeout, never shortens it. A non-finite document timeout leaves the deadline unchanged, as does a fetch timeout that is not a number. The download itself stays within the fetch timeout.
|
|
90
|
+
|
|
91
|
+
A caller that keeps the fetch short to retry a blocked page elsewhere can still let a document convert: a 151MB scanned PDF of 688 pages downloads in 2.7s and takes mutool 6.3s, more than a 9.5s fetch timeout leaves.
|
|
92
|
+
|
|
84
93
|
##### encoding
|
|
85
94
|
|
|
86
95
|
Type: `string`
|
|
@@ -107,7 +116,9 @@ It determines the strategy to use based on the `url`, being the possibles values
|
|
|
107
116
|
|
|
108
117
|
Type: `function`
|
|
109
118
|
|
|
110
|
-
It
|
|
119
|
+
It receives the URL and the file extension, and returns `{ path }` for a file html-get writes the fetched PDF or office document to, and the converted HTML of a large PDF. Every call must return a new path: two fetches of the same URL at once would otherwise read each other's file. The default is a random name in `os.tmpdir()`.
|
|
120
|
+
|
|
121
|
+
html-get removes every file it asked for once the fetch ends, whether the conversion succeeded, failed or was canceled.
|
|
111
122
|
|
|
112
123
|
##### gotOpts
|
|
113
124
|
|
|
@@ -132,6 +143,10 @@ It can explicitly disabled passing `false`.
|
|
|
132
143
|
|
|
133
144
|
A custom function receives the arguments and `{ signal, timeout }`: `timeout` is what is left of the fetch timeout, and `signal` aborts when the fetch is canceled. The default runner kills mutool with `SIGKILL` on either.
|
|
134
145
|
|
|
146
|
+
The PDF is converted with `-O preserve-images=no`, because mutool otherwise inlines every image as base64: a 151MB scanned PDF of 688 pages became 232MB of HTML, and parsing it took a 7GB heap. MuPDF 1.19 accepts the option and ignores it; 1.28 honors it (the same PDF is 35MB of HTML).
|
|
147
|
+
|
|
148
|
+
The PDF reader only looks at page one and skips images of more than 400x400 pixels in total, with a side under 16px, or used as stencil masks. When it finds none for `og:image`, mutool draws the PDF one page at a time, up to the first 20, and the first image it inlines in a page becomes `og:image`: the image a metadata reader picked from the inlined HTML before. A custom function receives `<file> <page>` for those calls and must resolve `{ stdout }` with mutool's HTML, whose `<div id="pageN">` tells the search which page it got; output without it ends the search.
|
|
149
|
+
|
|
135
150
|
##### pandoc
|
|
136
151
|
|
|
137
152
|
Type: `function`|`boolean`<br>
|
package/index.d.ts
CHANGED
|
@@ -50,6 +50,8 @@ declare namespace htmlGet {
|
|
|
50
50
|
* Options for html-get
|
|
51
51
|
*/
|
|
52
52
|
interface HtmlGetOptions {
|
|
53
|
+
/** Finite milliseconds from the start of the fetch a PDF or office document may take, when longer than the fetch timeout */
|
|
54
|
+
documentTimeout?: number
|
|
53
55
|
/** Character encoding for HTML (default: 'utf-8') */
|
|
54
56
|
encoding?: string
|
|
55
57
|
/** Function that returns a browserless instance (required unless prerender is false) */
|
|
@@ -81,6 +83,7 @@ declare namespace htmlGet {
|
|
|
81
83
|
interface GetContentOptions
|
|
82
84
|
extends Pick<
|
|
83
85
|
HtmlGetOptions,
|
|
86
|
+
| 'documentTimeout'
|
|
84
87
|
| 'getBrowserless'
|
|
85
88
|
| 'getTemporalFile'
|
|
86
89
|
| 'gotOpts'
|
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.
|
|
5
|
+
"version": "2.28.0",
|
|
6
6
|
"types": "index.d.ts",
|
|
7
7
|
"main": "src/index.js",
|
|
8
8
|
"bin": {
|
package/src/html.js
CHANGED
|
@@ -51,6 +51,7 @@ const addHead = ({ $, url, headers, pdfMeta = {} }) => {
|
|
|
51
51
|
|
|
52
52
|
if (pdfMeta.title) tags.push(metaTag('og:title', pdfMeta.title, 'property'))
|
|
53
53
|
if (pdfMeta.author) tags.push(metaTag('author', pdfMeta.author))
|
|
54
|
+
if (pdfMeta.authors) tags.push(metaTag('authors', pdfMeta.authors))
|
|
54
55
|
if (pdfMeta.description) {
|
|
55
56
|
tags.push(metaTag('description', pdfMeta.description))
|
|
56
57
|
tags.push(metaTag('og:description', pdfMeta.description, 'property'))
|
package/src/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
3
|
const { parseUrl, isMediaUrl, isPdfUrl, memoizeOne } = require('@metascraper/helpers')
|
|
4
|
-
const { readFile, writeFile } = require('fs/promises')
|
|
4
|
+
const { readFile, writeFile, rm } = require('fs/promises')
|
|
5
5
|
const timeSpan = require('@kikobeats/time-span')()
|
|
6
6
|
const debug = require('debug-logfmt')('html-get')
|
|
7
7
|
const { execFileSync } = require('child_process')
|
|
@@ -16,7 +16,8 @@ const os = require('os')
|
|
|
16
16
|
|
|
17
17
|
const { getContentLength, getContentType } = require('./util')
|
|
18
18
|
const { getOfficeFormat, isOfficeUrl } = require('./office')
|
|
19
|
-
const { extractSafe } = require('./pdf')
|
|
19
|
+
const { extractSafe, isPdf } = require('./pdf')
|
|
20
|
+
const { firstImage } = require('./pdf/first-image')
|
|
20
21
|
const autoDomains = require('./auto-domains')
|
|
21
22
|
const addHtml = require('./html')
|
|
22
23
|
|
|
@@ -26,6 +27,16 @@ const ABORT_TYPES = ['image', 'stylesheet', 'font']
|
|
|
26
27
|
|
|
27
28
|
const PDF_SIZE_TRESHOLD = 150 * 1024 // 150kb
|
|
28
29
|
|
|
30
|
+
const WITHOUT_IMAGES = '-O preserve-images=no'
|
|
31
|
+
|
|
32
|
+
const firstImageSafe = async (filepath, url, opts) => {
|
|
33
|
+
try {
|
|
34
|
+
return await firstImage(filepath, opts)
|
|
35
|
+
} catch (error) {
|
|
36
|
+
debug('mutool:image:error', { url, message: error.message || error })
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
29
40
|
// `URL#hash` is empty both when the fragment is missing and when it is
|
|
30
41
|
// empty. An empty fragment is still present on `href`, and it must win.
|
|
31
42
|
const inheritRequestFragment = (options, response) => {
|
|
@@ -35,11 +46,32 @@ const inheritRequestFragment = (options, response) => {
|
|
|
35
46
|
const fetch = PCancelable.fn(
|
|
36
47
|
async (
|
|
37
48
|
url,
|
|
38
|
-
{
|
|
49
|
+
{
|
|
50
|
+
documentTimeout,
|
|
51
|
+
getTemporalFile,
|
|
52
|
+
mutool,
|
|
53
|
+
pandoc,
|
|
54
|
+
reflect = false,
|
|
55
|
+
timeout = REQ_TIMEOUT,
|
|
56
|
+
toEncode,
|
|
57
|
+
...opts
|
|
58
|
+
},
|
|
39
59
|
onCancel
|
|
40
60
|
) => {
|
|
61
|
+
const temporalFiles = []
|
|
62
|
+
const temporalFile = (input, ext) => {
|
|
63
|
+
const file = getTemporalFile(input, ext)
|
|
64
|
+
temporalFiles.push(file)
|
|
65
|
+
return file
|
|
66
|
+
}
|
|
41
67
|
const reqTimeout = reflect ? timeout / 2 : timeout
|
|
42
|
-
const
|
|
68
|
+
const startedAt = Date.now()
|
|
69
|
+
let deadline = startedAt + reqTimeout
|
|
70
|
+
const allowDocumentTime = () => {
|
|
71
|
+
if (Number.isFinite(documentTimeout) && documentTimeout > reqTimeout) {
|
|
72
|
+
deadline = startedAt + documentTimeout
|
|
73
|
+
}
|
|
74
|
+
}
|
|
43
75
|
const conversion = new AbortController()
|
|
44
76
|
const convertOpts = () => ({
|
|
45
77
|
signal: conversion.signal,
|
|
@@ -82,20 +114,31 @@ const fetch = PCancelable.fn(
|
|
|
82
114
|
// a recognized office file never goes through mutool, even if the
|
|
83
115
|
// response is mislabeled as application/pdf
|
|
84
116
|
if (!convertOffice && contentType === 'application/pdf') {
|
|
117
|
+
allowDocumentTime()
|
|
85
118
|
pdfMeta = await extractSafe(res.body, res.url)
|
|
86
119
|
if (!mutool) return ''
|
|
87
|
-
const file =
|
|
120
|
+
const file = temporalFile(url, 'pdf')
|
|
88
121
|
await writeFile(file.path, res.body)
|
|
89
122
|
try {
|
|
90
123
|
let converted
|
|
91
124
|
if (getContentLength(res.headers) > PDF_SIZE_TRESHOLD) {
|
|
92
|
-
const ofile =
|
|
93
|
-
await mutool(
|
|
125
|
+
const ofile = temporalFile(`${url}-pdf`, 'pdf')
|
|
126
|
+
await mutool(`${WITHOUT_IMAGES} -o ${ofile.path} ${file.path}`, convertOpts())
|
|
94
127
|
converted = await readFile(ofile.path, 'utf-8')
|
|
95
128
|
} else {
|
|
96
|
-
converted = (await mutool(file.path
|
|
129
|
+
converted = (await mutool(`${WITHOUT_IMAGES} ${file.path}`, convertOpts())).stdout
|
|
130
|
+
}
|
|
131
|
+
if (converted) {
|
|
132
|
+
if (!pdfMeta?.image && isPdf(res.body)) {
|
|
133
|
+
const image = await firstImageSafe(file.path, res.url, {
|
|
134
|
+
mutool,
|
|
135
|
+
convertOpts,
|
|
136
|
+
signal: conversion.signal
|
|
137
|
+
})
|
|
138
|
+
pdfMeta = { ...pdfMeta, image }
|
|
139
|
+
}
|
|
140
|
+
return converted
|
|
97
141
|
}
|
|
98
|
-
if (converted) return converted
|
|
99
142
|
debug('mutool:empty', { url: res.url })
|
|
100
143
|
} catch (error) {
|
|
101
144
|
debug('mutool:error', {
|
|
@@ -107,7 +150,8 @@ const fetch = PCancelable.fn(
|
|
|
107
150
|
}
|
|
108
151
|
|
|
109
152
|
if (convertOffice) {
|
|
110
|
-
|
|
153
|
+
allowDocumentTime()
|
|
154
|
+
const file = temporalFile(res.url, officeFormat)
|
|
111
155
|
await writeFile(file.path, res.body)
|
|
112
156
|
try {
|
|
113
157
|
const converted = await convertOffice(officeFormat, file.path, convertOpts())
|
|
@@ -147,6 +191,8 @@ const fetch = PCancelable.fn(
|
|
|
147
191
|
return reflect
|
|
148
192
|
? { isRejected: true, error, ...answer }
|
|
149
193
|
: { ...answer, html: '', mode: 'fetch', redirects }
|
|
194
|
+
} finally {
|
|
195
|
+
await removeTemporalFiles(temporalFiles)
|
|
150
196
|
}
|
|
151
197
|
}
|
|
152
198
|
)
|
|
@@ -282,11 +328,19 @@ const defaultGetMode = (url, { prerender }) => {
|
|
|
282
328
|
}
|
|
283
329
|
|
|
284
330
|
const defaultGetTemporalFile = (input, ext) => {
|
|
285
|
-
const
|
|
286
|
-
|
|
287
|
-
return { path: filepath }
|
|
331
|
+
const name = crypto.randomUUID()
|
|
332
|
+
return { path: path.join(os.tmpdir(), ext === undefined ? name : `${name}.${ext}`) }
|
|
288
333
|
}
|
|
289
334
|
|
|
335
|
+
const removeTemporalFiles = files =>
|
|
336
|
+
Promise.all(
|
|
337
|
+
files.map(file =>
|
|
338
|
+
rm(file.path, { force: true }).catch(error =>
|
|
339
|
+
debug('temporal:error', { path: file.path, message: error.message })
|
|
340
|
+
)
|
|
341
|
+
)
|
|
342
|
+
)
|
|
343
|
+
|
|
290
344
|
// Resolve a binary path once and cache it: html-get owns the pandoc/mutool
|
|
291
345
|
// lookup, so both the internal runners and the exposed accessors share a single
|
|
292
346
|
// `which` per binary for the whole process. execFileSync (no shell) is faster
|
|
@@ -368,6 +422,7 @@ const getContent = PCancelable.fn(
|
|
|
368
422
|
url,
|
|
369
423
|
mode,
|
|
370
424
|
{
|
|
425
|
+
documentTimeout,
|
|
371
426
|
getBrowserless,
|
|
372
427
|
getTemporalFile,
|
|
373
428
|
gotOpts,
|
|
@@ -384,7 +439,7 @@ const getContent = PCancelable.fn(
|
|
|
384
439
|
const isFetchMode = mode === 'fetch'
|
|
385
440
|
|
|
386
441
|
const fetchOpts = isFetchMode
|
|
387
|
-
? { headers, toEncode, mutool, pandoc, getTemporalFile, ...gotOpts }
|
|
442
|
+
? { headers, toEncode, mutool, pandoc, getTemporalFile, ...gotOpts, documentTimeout }
|
|
388
443
|
: { headers, toEncode, getBrowserless, gotOpts, ...puppeteerOpts }
|
|
389
444
|
|
|
390
445
|
const promise = modes[mode](url, fetchOpts)
|
|
@@ -409,6 +464,7 @@ module.exports = PCancelable.fn(
|
|
|
409
464
|
async (
|
|
410
465
|
targetUrl,
|
|
411
466
|
{
|
|
467
|
+
documentTimeout,
|
|
412
468
|
encoding = 'utf-8',
|
|
413
469
|
getBrowserless,
|
|
414
470
|
getMode = defaultGetMode,
|
|
@@ -437,6 +493,7 @@ module.exports = PCancelable.fn(
|
|
|
437
493
|
const duration = timeSpan()
|
|
438
494
|
|
|
439
495
|
const promise = getContent(targetUrl, reqMode, {
|
|
496
|
+
documentTimeout,
|
|
440
497
|
getBrowserless,
|
|
441
498
|
getTemporalFile,
|
|
442
499
|
gotOpts,
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const MAX_IMAGE_PAGES = 20
|
|
4
|
+
const IMAGE_SRC = /<img\b[^>]*?\ssrc="(data:[^"]+)"/
|
|
5
|
+
|
|
6
|
+
const imageOf = html => IMAGE_SRC.exec(html)?.[1].replace(/\s+/g, '')
|
|
7
|
+
|
|
8
|
+
const firstImage = async (
|
|
9
|
+
filepath,
|
|
10
|
+
{ mutool, convertOpts, signal, maxPages = MAX_IMAGE_PAGES }
|
|
11
|
+
) => {
|
|
12
|
+
for (let page = 1; page <= maxPages && !signal.aborted; page++) {
|
|
13
|
+
const { stdout } = await mutool(`${filepath} ${page}`, convertOpts())
|
|
14
|
+
if (!stdout.includes(`<div id="page${page}"`)) return
|
|
15
|
+
const image = imageOf(stdout)
|
|
16
|
+
if (image) return image
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
module.exports = { firstImage, MAX_IMAGE_PAGES }
|