@browserless/pdf 10.8.0-beta.1 → 10.8.0-beta.11

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.
Files changed (2) hide show
  1. package/package.json +7 -4
  2. package/src/index.js +23 -15
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@browserless/pdf",
3
3
  "description": "Sensible good defaults for exporting a website as PDF",
4
4
  "homepage": "https://browserless.js.org/#/?id=pdfurl-options",
5
- "version": "10.8.0-beta.1",
5
+ "version": "10.8.0-beta.11",
6
6
  "main": "src",
7
7
  "repository": {
8
8
  "directory": "packages/pdf",
@@ -24,8 +24,11 @@
24
24
  "screen"
25
25
  ],
26
26
  "dependencies": {
27
- "@browserless/goto": "^10.7.13",
28
- "@browserless/screenshot": "^10.8.0-beta.0"
27
+ "@browserless/goto": "^10.8.0-beta.4",
28
+ "@browserless/screenshot": "^10.8.0-beta.8",
29
+ "@kikobeats/time-span": "~1.0.8",
30
+ "debug-logfmt": "~1.4.0",
31
+ "pretty-ms": "~7.0.1"
29
32
  },
30
33
  "engines": {
31
34
  "node": ">= 12"
@@ -38,5 +41,5 @@
38
41
  "test": "exit 0"
39
42
  },
40
43
  "license": "MIT",
41
- "gitHead": "c94f407b23a2ad309f71069c1a3e4a9b74c09fd5"
44
+ "gitHead": "cc1bbedae796643bcf394c198afe1c51646c41d0"
42
45
  }
package/src/index.js CHANGED
@@ -1,9 +1,10 @@
1
1
  'use strict'
2
2
 
3
- const { takeScreenshot } = require('@browserless/screenshot')
3
+ const timeSpan = require('@kikobeats/time-span')({ format: n => Math.round(n) })
4
+ const { isWhiteScreenshot } = require('@browserless/screenshot')
4
5
  const debug = require('debug-logfmt')('browserless:pdf')
6
+ const { setTimeout } = require('node:timers/promises')
5
7
  const createGoto = require('@browserless/goto')
6
- const timeSpan = require('@kikobeats/time-span')({ format: require('pretty-ms') })
7
8
 
8
9
  const getMargin = unit => {
9
10
  if (!unit) return unit
@@ -34,31 +35,38 @@ module.exports = ({ goto, ...gotoOpts } = {}) => {
34
35
  scale
35
36
  })
36
37
 
37
- const timePdf = timeSpan()
38
-
39
38
  if (waitUntil !== 'auto') {
40
39
  await goto(page, { ...opts, url, waitUntil })
41
40
  pdfBuffer = await generatePdf(page)
42
41
  } else {
43
42
  await goto(page, { ...opts, url, waitUntil, waitUntilAuto })
44
43
  async function waitUntilAuto (page) {
44
+ const timeout = goto.timeouts.action(opts.timeout)
45
45
  let isWhite = false
46
- let retryCount = 0
47
- const maxRetries = 3
46
+ let retry = -1
48
47
 
49
- do {
50
- const screenshotResult = await takeScreenshot({ page, goto, opts })
51
- isWhite = screenshotResult.isWhite
48
+ const timePdf = timeSpan()
52
49
 
53
- if (isWhite && retryCount < maxRetries) {
54
- retryCount++
55
- debug('screenshot:retry', { waitUntil, isWhite, retryCount, maxRetries })
56
- await goto.waitUntilAuto(page, { timeout: opts.timeout })
50
+ do {
51
+ ++retry
52
+ const screenshotTime = timeSpan()
53
+ isWhite = await isWhiteScreenshot(
54
+ await page.screenshot({
55
+ ...opts,
56
+ optimizeForSpeed: true,
57
+ type: 'jpeg',
58
+ quality: 30
59
+ })
60
+ )
61
+ debug('screenshot', { waitUntil, isWhite, retry, duration: screenshotTime() })
62
+ if (retry > 0) {
63
+ if (retry === 1) await goto.waitUntilAuto(page, { timeout: opts.timeout })
64
+ else await setTimeout(500)
57
65
  }
58
- } while (isWhite && retryCount < maxRetries)
66
+ } while (isWhite && timePdf() < timeout)
59
67
 
60
68
  pdfBuffer = await generatePdf(page)
61
- debug('screenshot', { waitUntil, isWhite, retryCount, duration: timePdf() })
69
+ debug({ waitUntil, isWhite, retry, timeout, duration: require('pretty-ms')(timePdf()) })
62
70
  }
63
71
  }
64
72