@browserless/pdf 10.8.0-beta.0 → 10.8.0-beta.10

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 +28 -6
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.0",
5
+ "version": "10.8.0-beta.10",
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": "e7c845edfd2552f48b8f6f3b70d887ec497955b0"
44
+ "gitHead": "6ba5fc437ff39bd25d971f7eaccdd462a92d09f7"
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,17 +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) {
45
- const { isWhite } = await takeScreenshot({ page, goto, opts })
44
+ const timeout = goto.timeouts.action(goto.timeouts.base(opts.timeout))
45
+ let isWhite = false
46
+ let retry = -1
47
+
48
+ const timePdf = timeSpan()
49
+
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)
65
+ }
66
+ } while (isWhite && timePdf() < timeout)
67
+
46
68
  pdfBuffer = await generatePdf(page)
47
- debug('screenshot', { waitUntil, isWhite, duration: timePdf() })
69
+ debug({ waitUntil, isWhite, retry, timeout, duration: require('pretty-ms')(timePdf()) })
48
70
  }
49
71
  }
50
72