html-get 2.29.0 → 2.30.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
@@ -90,6 +90,24 @@ The time, in milliseconds from the start of the fetch, that a PDF or office docu
90
90
 
91
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
92
 
93
+ ##### maxBodySize
94
+
95
+ Type: `number`<br>
96
+ Default: no limit
97
+
98
+ The most bytes, after decompression, read from a response that is not a PDF or office document. A response whose `content-length` is over it is not read at all, unless it is compressed, since `content-length` then counts compressed bytes. Any other response stops once its decoded body passes the limit.
99
+
100
+ A response over the limit still answers its `url`, `statusCode` and `headers`. An HTML page keeps what was read before the limit, with `partial: true`. An HTML page with nothing read, or in a charset that cannot be decoded, and anything else answer an empty `html`. The `afterResponse` hooks in `gotOpts` do not run for it, because the body never finishes. A retry that an `afterResponse` hook makes with `retryWithMergedOptions` is not limited: got gives the hook no way to watch its body.
101
+
102
+ Without it, a URL that answers a large file (a video, an archive) is held whole in memory: a 300MB `video/mp4` raised the peak RSS by 950MB. With a 32MB limit it raised it by 3MB when the response had a `content-length`, and by 71MB when it did not.
103
+
104
+ ##### maxDocumentSize
105
+
106
+ Type: `number`<br>
107
+ Default: no limit
108
+
109
+ The same limit as [maxBodySize](#maxbodysize), for a PDF or office document, which html-get converts and so reads whole. A response is a document when its `content-type` is `application/pdf`, or when it is an office file and [pandoc](#pandoc) is not turned off. An office file has an office `content-type`, or a URL that ends in an office extension while its `content-type` is neither a web nor a media type: a `/notes.docx` served as `text/html`, `application/xhtml+xml`, `application/json`, `image/*`, `audio/*` or `video/*` is a page, and gets `maxBodySize`. With `pandoc: false` an office file is read as a page, so it gets `maxBodySize`, and so does an office file the fetch that runs alongside [prerender](#prerender) reads, since that fetch converts nothing. The default pandoc counts as on even where the binary is missing: it is found only once the body is read, and the limit must be chosen when the headers arrive. Those are labels the server or the URL chooses, so without this limit anyone can make html-get read a file of any size by sending it as `application/pdf` or by adding `.docx` to its URL. A document over the limit answers an empty `html` and is not converted.
110
+
93
111
  ##### encoding
94
112
 
95
113
  Type: `string`
package/index.d.ts CHANGED
@@ -16,7 +16,7 @@ declare namespace htmlGet {
16
16
  statusCode: number | undefined
17
17
  /** Redirect history */
18
18
  redirects: Array<{ statusCode: number; url: string }>
19
- /** Set when the body stopped arriving (a timeout or a dropped connection): `html` is what arrived before it */
19
+ /** Set when the body stopped arriving (a timeout, a dropped connection or `maxBodySize`): `html` is what arrived before it */
20
20
  partial?: true
21
21
  /** Statistics about the request */
22
22
  stats: {
@@ -58,6 +58,10 @@ declare namespace htmlGet {
58
58
  encoding?: string
59
59
  /** Function that returns a browserless instance (required unless prerender is false) */
60
60
  getBrowserless?: () => Promise<any>
61
+ /** Most bytes, after decompression, read from a response that is not a PDF or office document; unlimited when not finite */
62
+ maxBodySize?: number
63
+ /** Most bytes, after decompression, read from a PDF, or an office document (by content-type or URL extension) while pandoc is not turned off; unlimited when not finite */
64
+ maxDocumentSize?: number
61
65
  /** Function to determine the mode ('fetch' or 'prerender') */
62
66
  getMode?: (url: string, options: { prerender: boolean | 'auto' }) => 'fetch' | 'prerender'
63
67
  /** Function to create temporary files */
@@ -90,6 +94,8 @@ declare namespace htmlGet {
90
94
  | 'getTemporalFile'
91
95
  | 'gotOpts'
92
96
  | 'headers'
97
+ | 'maxBodySize'
98
+ | 'maxDocumentSize'
93
99
  | 'mutool'
94
100
  | 'pandoc'
95
101
  | 'puppeteerOpts'
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.29.0",
5
+ "version": "2.30.1",
6
6
  "types": "index.d.ts",
7
7
  "main": "src/index.js",
8
8
  "bin": {
package/src/index.js CHANGED
@@ -65,17 +65,46 @@ const recordHtmlBody = req => {
65
65
  return () => Buffer.concat(chunks)
66
66
  }
67
67
 
68
+ const isEncoded = headers => {
69
+ const encoding = headers['content-encoding']
70
+ return Boolean(encoding) && encoding.toLowerCase() !== 'identity'
71
+ }
72
+
73
+ const isDocument = (res, url, pandoc) => {
74
+ const contentType = getContentType(res.headers)
75
+ if (contentType === 'application/pdf') return true
76
+ return Boolean(pandoc) && Boolean(getOfficeFormat({ contentType, url: [res.url, url] }))
77
+ }
78
+
79
+ const limitBody = (req, url, { maxBodySize, maxDocumentSize, pandoc }) => {
80
+ const limit = { reached: false }
81
+ const stop = () => {
82
+ limit.reached = true
83
+ req.cancel()
84
+ }
85
+ req.on('response', res => {
86
+ const maxSize = isDocument(res, url, pandoc) ? maxDocumentSize : maxBodySize
87
+ if (!Number.isFinite(maxSize)) return
88
+ const announced = isEncoded(res.headers) ? NaN : getContentLength(res.headers)
89
+ if (announced > maxSize) stop()
90
+ let received = 0
91
+ res.on('data', chunk => {
92
+ received += chunk.length
93
+ if (received > maxSize) stop()
94
+ })
95
+ })
96
+ return limit
97
+ }
98
+
68
99
  const isBodyCut = error => error instanceof got.TimeoutError || error instanceof got.ReadError
69
100
 
70
- const readCutHtml = async (error, readHtmlBody, toEncode) => {
71
- if (!isBodyCut(error)) return
72
- const body = readHtmlBody()
101
+ const encodeReadHtml = async (body, res, toEncode) => {
73
102
  if (body.length === 0) return
74
103
  try {
75
- return await toEncode(body, error.response.headers['content-type'])
104
+ return await toEncode(body, res.headers['content-type'])
76
105
  } catch (encodeError) {
77
106
  debug('fetch:partial:error', {
78
- url: error.response.url,
107
+ url: res.url,
79
108
  message: encodeError?.message || encodeError
80
109
  })
81
110
  }
@@ -87,6 +116,8 @@ const fetch = PCancelable.fn(
87
116
  {
88
117
  documentTimeout,
89
118
  getTemporalFile,
119
+ maxBodySize,
120
+ maxDocumentSize,
90
121
  mutool,
91
122
  pandoc,
92
123
  reflect = false,
@@ -139,6 +170,7 @@ const fetch = PCancelable.fn(
139
170
  const redirects = []
140
171
  req.on('redirect', res => redirects.push({ statusCode: res.statusCode, url: res.url }))
141
172
  const readHtmlBody = recordHtmlBody(req)
173
+ const bodyLimit = limitBody(req, url, { maxBodySize, maxDocumentSize, pandoc })
142
174
 
143
175
  try {
144
176
  const res = await req
@@ -227,18 +259,27 @@ const fetch = PCancelable.fn(
227
259
  redirects
228
260
  }
229
261
  } catch (error) {
230
- debug('fetch:error', { url, message: error.message || error, reflect })
262
+ const limited = bodyLimit.reached
263
+ const response = error.response
264
+ debug(limited ? 'fetch:limit' : 'fetch:error', {
265
+ url,
266
+ message: error.message || error,
267
+ reflect
268
+ })
231
269
  const answer = {
232
- url: error.response ? error.response.url : url,
233
- headers: error.response ? error.response.headers : {},
234
- statusCode: error.response ? error.response.statusCode : undefined
270
+ url: response ? response.url : url,
271
+ headers: response ? response.headers : {},
272
+ statusCode: response ? response.statusCode : undefined
235
273
  }
236
- const cutHtml = await readCutHtml(error, readHtmlBody, toEncode)
237
- if (cutHtml) {
238
- debug('fetch:partial', { url, bytes: Buffer.byteLength(cutHtml), reflect })
239
- return { ...answer, html: cutHtml, partial: true, mode: 'fetch', redirects }
274
+ const readHtml =
275
+ limited || isBodyCut(error)
276
+ ? await encodeReadHtml(readHtmlBody(), response, toEncode)
277
+ : undefined
278
+ if (readHtml) {
279
+ debug('fetch:partial', { url, bytes: Buffer.byteLength(readHtml), reflect })
280
+ return { ...answer, html: readHtml, partial: true, mode: 'fetch', redirects }
240
281
  }
241
- return reflect
282
+ return reflect && !limited
242
283
  ? { isRejected: true, error, ...answer }
243
284
  : { ...answer, html: '', mode: 'fetch', redirects }
244
285
  } finally {
@@ -255,6 +296,8 @@ const prerender = PCancelable.fn(
255
296
  getBrowserless,
256
297
  gotOpts,
257
298
  headers,
299
+ maxBodySize,
300
+ maxDocumentSize,
258
301
  timeout = REQ_TIMEOUT,
259
302
  toEncode,
260
303
  ...opts
@@ -273,6 +316,8 @@ const prerender = PCancelable.fn(
273
316
  toEncode,
274
317
  ...gotOpts,
275
318
  headers,
319
+ maxBodySize,
320
+ maxDocumentSize,
276
321
  timeout
277
322
  })
278
323
  const browserless = await getBrowserless()
@@ -479,6 +524,8 @@ const getContent = PCancelable.fn(
479
524
  getTemporalFile,
480
525
  gotOpts,
481
526
  headers,
527
+ maxBodySize,
528
+ maxDocumentSize,
482
529
  mutool,
483
530
  pandoc,
484
531
  puppeteerOpts,
@@ -491,8 +538,26 @@ const getContent = PCancelable.fn(
491
538
  const isFetchMode = mode === 'fetch'
492
539
 
493
540
  const fetchOpts = isFetchMode
494
- ? { headers, toEncode, mutool, pandoc, getTemporalFile, ...gotOpts, documentTimeout }
495
- : { headers, toEncode, getBrowserless, gotOpts, ...puppeteerOpts }
541
+ ? {
542
+ headers,
543
+ toEncode,
544
+ mutool,
545
+ pandoc,
546
+ getTemporalFile,
547
+ ...gotOpts,
548
+ documentTimeout,
549
+ maxBodySize,
550
+ maxDocumentSize
551
+ }
552
+ : {
553
+ headers,
554
+ toEncode,
555
+ getBrowserless,
556
+ gotOpts,
557
+ maxBodySize,
558
+ maxDocumentSize,
559
+ ...puppeteerOpts
560
+ }
496
561
 
497
562
  const promise = modes[mode](url, fetchOpts)
498
563
  onCancel(() => promise.cancel())
@@ -524,6 +589,8 @@ module.exports = PCancelable.fn(
524
589
  getTemporalFile = defaultGetTemporalFile,
525
590
  gotOpts,
526
591
  headers,
592
+ maxBodySize,
593
+ maxDocumentSize,
527
594
  mutool = defaultMutool(),
528
595
  pandoc = DEFAULT_PANDOC,
529
596
  prerender = 'auto',
@@ -551,6 +618,8 @@ module.exports = PCancelable.fn(
551
618
  getTemporalFile,
552
619
  gotOpts,
553
620
  headers,
621
+ maxBodySize,
622
+ maxDocumentSize,
554
623
  mutool,
555
624
  pandoc,
556
625
  puppeteerOpts,
@@ -572,6 +641,8 @@ module.exports = PCancelable.fn(
572
641
  getTemporalFile,
573
642
  gotOpts,
574
643
  headers,
644
+ maxBodySize,
645
+ maxDocumentSize,
575
646
  mutool,
576
647
  puppeteerOpts,
577
648
  rewriteUrls,