html-get 2.27.24 → 2.28.1

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
@@ -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`
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.27.24",
5
+ "version": "2.28.1",
6
6
  "types": "index.d.ts",
7
7
  "main": "src/index.js",
8
8
  "bin": {
package/src/html.js CHANGED
@@ -11,7 +11,7 @@ const cheerio = require('cheerio')
11
11
  const { URL } = require('url')
12
12
  const path = require('path')
13
13
 
14
- const { isMime, isUrl, mimeExtension, parseUrl } = require('@metascraper/helpers')
14
+ const { isMime, isUrl, parseUrl } = require('@metascraper/helpers')
15
15
 
16
16
  const { getContentType, getCharset } = require('./util')
17
17
 
@@ -70,6 +70,20 @@ const addHead = ({ $, url, headers, pdfMeta = {} }) => {
70
70
  tags.forEach(tag => head.append(tag))
71
71
  }
72
72
 
73
+ const escapeText = text =>
74
+ String(text ?? '')
75
+ .replace(/&/g, '&amp;')
76
+ .replace(/</g, '&lt;')
77
+
78
+ const isText = contentType =>
79
+ typeof contentType === 'string' &&
80
+ (contentType.endsWith('/json') ||
81
+ contentType.endsWith('+json') ||
82
+ (contentType.startsWith('text/') && contentType !== 'text/html'))
83
+
84
+ const isDocument = ({ html, headers, mode, converted }) =>
85
+ isHTML(html) && (mode === 'prerender' || converted || !isText(getContentType(headers)))
86
+
73
87
  const addBody = ({ url, headers, html }) => {
74
88
  const contentType = getContentType(headers)
75
89
  let element = ''
@@ -80,8 +94,10 @@ const addBody = ({ url, headers, html }) => {
80
94
  element = `<video><source src="${url}" type="${contentType}"></source></video>`
81
95
  } else if (isMime(contentType, 'audio')) {
82
96
  element = `<audio><source src="${url}" type="${contentType}"></source></audio>`
83
- } else if (mimeExtension(contentType) === 'json') {
84
- element = `<pre>${html}</pre>`
97
+ } else if (isText(contentType)) {
98
+ const text = escapeText(html)
99
+ const preserved = text.startsWith('\n') ? `<!-- -->${text}` : text
100
+ element = `<pre>${preserved}</pre>`
85
101
  }
86
102
 
87
103
  return `<!DOCTYPE html><html><head></head><body>${element}</body></html>`
@@ -191,6 +207,8 @@ module.exports = ({
191
207
  html,
192
208
  url,
193
209
  headers = {},
210
+ mode,
211
+ converted,
194
212
  pdfMeta,
195
213
  styles,
196
214
  hide,
@@ -200,7 +218,9 @@ module.exports = ({
200
218
  scripts,
201
219
  modules
202
220
  }) => {
203
- const content = addDocType(isHTML(html) ? html : addBody({ url, headers, html }))
221
+ const content = addDocType(
222
+ isDocument({ html, headers, mode, converted }) ? html : addBody({ url, headers, html })
223
+ )
204
224
 
205
225
  const $ = cheerio.load(content)
206
226
 
package/src/index.js CHANGED
@@ -46,7 +46,16 @@ const inheritRequestFragment = (options, response) => {
46
46
  const fetch = PCancelable.fn(
47
47
  async (
48
48
  url,
49
- { getTemporalFile, mutool, pandoc, reflect = false, timeout = REQ_TIMEOUT, toEncode, ...opts },
49
+ {
50
+ documentTimeout,
51
+ getTemporalFile,
52
+ mutool,
53
+ pandoc,
54
+ reflect = false,
55
+ timeout = REQ_TIMEOUT,
56
+ toEncode,
57
+ ...opts
58
+ },
50
59
  onCancel
51
60
  ) => {
52
61
  const temporalFiles = []
@@ -56,7 +65,13 @@ const fetch = PCancelable.fn(
56
65
  return file
57
66
  }
58
67
  const reqTimeout = reflect ? timeout / 2 : timeout
59
- const deadline = Date.now() + reqTimeout
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
+ }
60
75
  const conversion = new AbortController()
61
76
  const convertOpts = () => ({
62
77
  signal: conversion.signal,
@@ -90,6 +105,7 @@ const fetch = PCancelable.fn(
90
105
  const res = await req
91
106
 
92
107
  let pdfMeta
108
+ let asDocument = false
93
109
  const html = await (async () => {
94
110
  const contentType = getContentType(res.headers)
95
111
 
@@ -99,6 +115,7 @@ const fetch = PCancelable.fn(
99
115
  // a recognized office file never goes through mutool, even if the
100
116
  // response is mislabeled as application/pdf
101
117
  if (!convertOffice && contentType === 'application/pdf') {
118
+ allowDocumentTime()
102
119
  pdfMeta = await extractSafe(res.body, res.url)
103
120
  if (!mutool) return ''
104
121
  const file = temporalFile(url, 'pdf')
@@ -121,6 +138,7 @@ const fetch = PCancelable.fn(
121
138
  })
122
139
  pdfMeta = { ...pdfMeta, image }
123
140
  }
141
+ asDocument = true
124
142
  return converted
125
143
  }
126
144
  debug('mutool:empty', { url: res.url })
@@ -134,11 +152,15 @@ const fetch = PCancelable.fn(
134
152
  }
135
153
 
136
154
  if (convertOffice) {
155
+ allowDocumentTime()
137
156
  const file = temporalFile(res.url, officeFormat)
138
157
  await writeFile(file.path, res.body)
139
158
  try {
140
159
  const converted = await convertOffice(officeFormat, file.path, convertOpts())
141
- if (converted) return converted
160
+ if (converted) {
161
+ asDocument = true
162
+ return converted
163
+ }
142
164
  debug('pandoc:empty', { url: res.url, format: officeFormat })
143
165
  } catch (error) {
144
166
  debug('pandoc:error', {
@@ -159,6 +181,7 @@ const fetch = PCancelable.fn(
159
181
  headers: res.headers,
160
182
  html,
161
183
  pdfMeta,
184
+ converted: asDocument,
162
185
  mode: 'fetch',
163
186
  url: res.url,
164
187
  statusCode: res.statusCode,
@@ -405,6 +428,7 @@ const getContent = PCancelable.fn(
405
428
  url,
406
429
  mode,
407
430
  {
431
+ documentTimeout,
408
432
  getBrowserless,
409
433
  getTemporalFile,
410
434
  gotOpts,
@@ -421,17 +445,18 @@ const getContent = PCancelable.fn(
421
445
  const isFetchMode = mode === 'fetch'
422
446
 
423
447
  const fetchOpts = isFetchMode
424
- ? { headers, toEncode, mutool, pandoc, getTemporalFile, ...gotOpts }
448
+ ? { headers, toEncode, mutool, pandoc, getTemporalFile, ...gotOpts, documentTimeout }
425
449
  : { headers, toEncode, getBrowserless, gotOpts, ...puppeteerOpts }
426
450
 
427
451
  const promise = modes[mode](url, fetchOpts)
428
452
  onCancel(() => promise.cancel())
429
453
 
430
454
  return promise.then(content => {
431
- const { pdfMeta, ...rest } = content
455
+ const { pdfMeta, converted, ...rest } = content
432
456
  const $ = addHtml({
433
457
  ...rest,
434
458
  pdfMeta,
459
+ converted,
435
460
  ...(isFetchMode ? puppeteerOpts : undefined),
436
461
  rewriteUrls,
437
462
  rewriteHtml
@@ -446,6 +471,7 @@ module.exports = PCancelable.fn(
446
471
  async (
447
472
  targetUrl,
448
473
  {
474
+ documentTimeout,
449
475
  encoding = 'utf-8',
450
476
  getBrowserless,
451
477
  getMode = defaultGetMode,
@@ -474,6 +500,7 @@ module.exports = PCancelable.fn(
474
500
  const duration = timeSpan()
475
501
 
476
502
  const promise = getContent(targetUrl, reqMode, {
503
+ documentTimeout,
477
504
  getBrowserless,
478
505
  getTemporalFile,
479
506
  gotOpts,