html-get 2.27.22 → 2.27.23
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 +4 -0
- package/index.d.ts +13 -2
- package/package.json +1 -1
- package/src/index.js +22 -12
package/README.md
CHANGED
|
@@ -130,6 +130,8 @@ It returns a function that receives that executes [mutool](https://mupdf.com/) b
|
|
|
130
130
|
|
|
131
131
|
It can explicitly disabled passing `false`.
|
|
132
132
|
|
|
133
|
+
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
|
+
|
|
133
135
|
##### pandoc
|
|
134
136
|
|
|
135
137
|
Type: `function`|`boolean`<br>
|
|
@@ -141,6 +143,8 @@ The format is resolved from the response content-type, falling back to the URL e
|
|
|
141
143
|
|
|
142
144
|
It can be explicitly disabled passing `false`.
|
|
143
145
|
|
|
146
|
+
A custom function receives `(format, filepath, { signal, timeout })`, with the same meaning as for `mutool`. The default runner kills pandoc with `SIGKILL` on either, so a conversion never outlives its fetch: pandoc has no limit of its own, and a 4.5MB spreadsheet once took it 34s and 10.9GB.
|
|
147
|
+
|
|
144
148
|
##### prerender
|
|
145
149
|
|
|
146
150
|
Type: `boolean`|`string`<br>
|
package/index.d.ts
CHANGED
|
@@ -31,9 +31,20 @@ declare namespace htmlGet {
|
|
|
31
31
|
$: CheerioAPI
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
-
|
|
34
|
+
interface ConvertOptions {
|
|
35
|
+
/** Aborted when the fetch is canceled; the default runners kill the process */
|
|
36
|
+
signal: AbortSignal
|
|
37
|
+
/** Milliseconds left of the fetch timeout, undefined when it is not a number; the default runners kill the process after it */
|
|
38
|
+
timeout?: number
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
type Pandoc = (
|
|
42
|
+
format: string,
|
|
43
|
+
filepath: string,
|
|
44
|
+
options?: ConvertOptions
|
|
45
|
+
) => Promise<string | undefined>
|
|
35
46
|
|
|
36
|
-
type Mutool = (
|
|
47
|
+
type Mutool = (args: string, options?: ConvertOptions) => any
|
|
37
48
|
|
|
38
49
|
/**
|
|
39
50
|
* Options for html-get
|
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.23",
|
|
6
6
|
"types": "index.d.ts",
|
|
7
7
|
"main": "src/index.js",
|
|
8
8
|
"bin": {
|
package/src/index.js
CHANGED
|
@@ -39,6 +39,14 @@ const fetch = PCancelable.fn(
|
|
|
39
39
|
onCancel
|
|
40
40
|
) => {
|
|
41
41
|
const reqTimeout = reflect ? timeout / 2 : timeout
|
|
42
|
+
const deadline = Date.now() + reqTimeout
|
|
43
|
+
const conversion = new AbortController()
|
|
44
|
+
const convertOpts = () => ({
|
|
45
|
+
signal: conversion.signal,
|
|
46
|
+
timeout: Number.isFinite(deadline)
|
|
47
|
+
? Math.max(Math.floor(deadline - Date.now()), 1)
|
|
48
|
+
: undefined
|
|
49
|
+
})
|
|
42
50
|
|
|
43
51
|
const req = got(url, {
|
|
44
52
|
...opts,
|
|
@@ -55,6 +63,7 @@ const fetch = PCancelable.fn(
|
|
|
55
63
|
onCancel(() => {
|
|
56
64
|
debug('fetch:cancel', { url, reflect })
|
|
57
65
|
req.cancel()
|
|
66
|
+
conversion.abort()
|
|
58
67
|
})
|
|
59
68
|
|
|
60
69
|
const redirects = []
|
|
@@ -81,10 +90,10 @@ const fetch = PCancelable.fn(
|
|
|
81
90
|
let converted
|
|
82
91
|
if (getContentLength(res.headers) > PDF_SIZE_TRESHOLD) {
|
|
83
92
|
const ofile = getTemporalFile(`${url}-pdf`, 'pdf')
|
|
84
|
-
await mutool(`-o ${ofile.path} ${file.path}
|
|
93
|
+
await mutool(`-o ${ofile.path} ${file.path}`, convertOpts())
|
|
85
94
|
converted = await readFile(ofile.path, 'utf-8')
|
|
86
95
|
} else {
|
|
87
|
-
converted = (await mutool(file.path)).stdout
|
|
96
|
+
converted = (await mutool(file.path, convertOpts())).stdout
|
|
88
97
|
}
|
|
89
98
|
if (converted) return converted
|
|
90
99
|
debug('mutool:empty', { url: res.url })
|
|
@@ -101,7 +110,7 @@ const fetch = PCancelable.fn(
|
|
|
101
110
|
const file = getTemporalFile(res.url, officeFormat)
|
|
102
111
|
await writeFile(file.path, res.body)
|
|
103
112
|
try {
|
|
104
|
-
const converted = await convertOffice(officeFormat, file.path)
|
|
113
|
+
const converted = await convertOffice(officeFormat, file.path, convertOpts())
|
|
105
114
|
if (converted) return converted
|
|
106
115
|
debug('pandoc:empty', { url: res.url, format: officeFormat })
|
|
107
116
|
} catch (error) {
|
|
@@ -111,6 +120,7 @@ const fetch = PCancelable.fn(
|
|
|
111
120
|
message: error.message || error
|
|
112
121
|
})
|
|
113
122
|
}
|
|
123
|
+
if (conversion.signal.aborted) return ''
|
|
114
124
|
}
|
|
115
125
|
|
|
116
126
|
return contentType === 'text/html' || !isMediaUrl(url)
|
|
@@ -297,28 +307,28 @@ const whichSync = bin => {
|
|
|
297
307
|
return whichCache.get(bin)
|
|
298
308
|
}
|
|
299
309
|
|
|
310
|
+
const spawnOptions = ({ signal, timeout } = {}) => ({ signal, timeout, killSignal: 'SIGKILL' })
|
|
311
|
+
|
|
300
312
|
// The default mutool runner is wired as a default parameter (evaluated on every
|
|
301
313
|
// call), so memoizeOne builds it once instead of rebuilding it per request.
|
|
302
314
|
const defaultMutool = memoizeOne(() => {
|
|
303
315
|
const mutoolPath = whichSync('mutool')
|
|
304
316
|
if (!mutoolPath) return
|
|
305
|
-
return (
|
|
317
|
+
return (args, opts) => $(`${mutoolPath} draw -q -F html ${args}`, spawnOptions(opts))
|
|
306
318
|
})
|
|
307
319
|
|
|
308
320
|
const PANDOC_LIST_FORMATS = ['--list-input-formats']
|
|
309
321
|
|
|
310
322
|
const createPandoc = (pandocPath, inputFormats) => {
|
|
311
323
|
const supported = new Set(inputFormats.toString().trim().split(/\s+/))
|
|
312
|
-
return async (format, filepath) => {
|
|
324
|
+
return async (format, filepath, opts) => {
|
|
313
325
|
if (!supported.has(format)) return
|
|
314
326
|
// array form keeps `filepath` a single argv entry even if it has spaces
|
|
315
|
-
const { stdout } = await $(
|
|
316
|
-
|
|
317
|
-
'--to=html',
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
filepath
|
|
321
|
-
])
|
|
327
|
+
const { stdout } = await $(
|
|
328
|
+
pandocPath,
|
|
329
|
+
[`--from=${format}`, '--to=html', '--standalone', '--embed-resources', filepath],
|
|
330
|
+
spawnOptions(opts)
|
|
331
|
+
)
|
|
322
332
|
return stdout.trim() ? stdout : undefined
|
|
323
333
|
}
|
|
324
334
|
}
|