html-get 2.27.21 → 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 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
- type Pandoc = (format: string, filepath: string) => Promise<string | undefined>
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 = (...args: string[]) => any
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.21",
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)
@@ -129,17 +139,14 @@ const fetch = PCancelable.fn(
129
139
  }
130
140
  } catch (error) {
131
141
  debug('fetch:error', { url, message: error.message || error, reflect })
132
- const finalUrl = error.response ? error.response.url : url
142
+ const answer = {
143
+ url: error.response ? error.response.url : url,
144
+ headers: error.response ? error.response.headers : {},
145
+ statusCode: error.response ? error.response.statusCode : undefined
146
+ }
133
147
  return reflect
134
- ? { isRejected: true, error, url: finalUrl }
135
- : {
136
- url: finalUrl,
137
- html: '',
138
- mode: 'fetch',
139
- headers: error.response ? error.response.headers : {},
140
- statusCode: error.response ? error.response.statusCode : undefined,
141
- redirects
142
- }
148
+ ? { isRejected: true, error, ...answer }
149
+ : { ...answer, html: '', mode: 'fetch', redirects }
143
150
  }
144
151
  }
145
152
  )
@@ -229,11 +236,11 @@ const prerender = PCancelable.fn(
229
236
 
230
237
  return isFetchResRejected
231
238
  ? {
232
- headers: data.headers || {},
239
+ headers: data.headers,
233
240
  html: '',
234
241
  url: data.url,
235
242
  mode: 'prerender',
236
- statusCode: data.error?.response?.statusCode,
243
+ statusCode: data.statusCode,
237
244
  redirects: []
238
245
  }
239
246
  : data
@@ -300,28 +307,28 @@ const whichSync = bin => {
300
307
  return whichCache.get(bin)
301
308
  }
302
309
 
310
+ const spawnOptions = ({ signal, timeout } = {}) => ({ signal, timeout, killSignal: 'SIGKILL' })
311
+
303
312
  // The default mutool runner is wired as a default parameter (evaluated on every
304
313
  // call), so memoizeOne builds it once instead of rebuilding it per request.
305
314
  const defaultMutool = memoizeOne(() => {
306
315
  const mutoolPath = whichSync('mutool')
307
316
  if (!mutoolPath) return
308
- return (...args) => $(`${mutoolPath} draw -q -F html ${args}`)
317
+ return (args, opts) => $(`${mutoolPath} draw -q -F html ${args}`, spawnOptions(opts))
309
318
  })
310
319
 
311
320
  const PANDOC_LIST_FORMATS = ['--list-input-formats']
312
321
 
313
322
  const createPandoc = (pandocPath, inputFormats) => {
314
323
  const supported = new Set(inputFormats.toString().trim().split(/\s+/))
315
- return async (format, filepath) => {
324
+ return async (format, filepath, opts) => {
316
325
  if (!supported.has(format)) return
317
326
  // 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
- ])
327
+ const { stdout } = await $(
328
+ pandocPath,
329
+ [`--from=${format}`, '--to=html', '--standalone', '--embed-resources', filepath],
330
+ spawnOptions(opts)
331
+ )
325
332
  return stdout.trim() ? stdout : undefined
326
333
  }
327
334
  }