@browserless/screenshot 10.9.7 → 10.9.8

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/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@browserless/screenshot",
3
3
  "description": "Take a clean screenshot of any website",
4
4
  "homepage": "https://browserless.js.org/#/?id=screenshoturl-options",
5
- "version": "10.9.7",
5
+ "version": "10.9.8",
6
6
  "main": "src/index.js",
7
7
  "author": {
8
8
  "email": "hello@microlink.io",
@@ -28,7 +28,7 @@
28
28
  "screenshot"
29
29
  ],
30
30
  "dependencies": {
31
- "@browserless/goto": "^10.9.7",
31
+ "@browserless/goto": "^10.9.8",
32
32
  "@kikobeats/content-type": "~1.0.1",
33
33
  "@kikobeats/time-span": "~1.0.9",
34
34
  "debug-logfmt": "~1.4.5",
@@ -46,7 +46,7 @@
46
46
  "svg-gradient": "~1.0.3"
47
47
  },
48
48
  "devDependencies": {
49
- "@browserless/test": "^10.9.7",
49
+ "@browserless/test": "^10.9.8",
50
50
  "ava": "5",
51
51
  "cheerio": "latest"
52
52
  },
@@ -67,5 +67,5 @@
67
67
  "timeout": "2m",
68
68
  "workerThreads": false
69
69
  },
70
- "gitHead": "1134ac8b6f7a110e4020042f1b8054af845bdb00"
70
+ "gitHead": "e468dda7fc284d02c2d2eb494e907b1111c7964e"
71
71
  }
@@ -3,20 +3,30 @@
3
3
  const { Jimp } = require('jimp')
4
4
 
5
5
  module.exports = async uint8array => {
6
- const image = await Jimp.fromBuffer(Buffer.from(uint8array))
7
- const firstPixel = image.getPixelColor(0, 0)
8
- const height = image.bitmap.height
9
- const width = image.bitmap.width
6
+ try {
7
+ const image = await Jimp.fromBuffer(Buffer.from(uint8array))
8
+ const firstPixel = image.getPixelColor(0, 0)
9
+ const height = image.bitmap.height
10
+ const width = image.bitmap.width
10
11
 
11
- const samplePercentage = 0.25 // Sample 25% of the image
12
- const sampleSize = Math.floor(width * height * samplePercentage) // Calculate sample size based on percentage
13
- const stepSize = Math.max(1, Math.floor((width * height) / sampleSize)) // Calculate step size based on sample size
12
+ // For 2D grid sampling, calculate stepSize to achieve approximately the target sample percentage.
13
+ // When sampling every 'stepSize' pixels in both dimensions, actual samples = (height/stepSize) * (width/stepSize).
14
+ // To achieve samplePercentage, we need: (h*w)/(stepSize²) samplePercentage*(h*w)
15
+ // Therefore: stepSize ≈ sqrt(1 / samplePercentage)
16
+ const samplePercentage = 0.25 // Sample ~25% of the image
17
+ const stepSize = Math.max(1, Math.ceil(Math.sqrt(1 / samplePercentage)))
14
18
 
15
- for (let i = 0; i < height; i += stepSize) {
16
- for (let j = 0; j < width; j += stepSize) {
17
- if (firstPixel !== image.getPixelColor(j, i)) return false
19
+ for (let i = 0; i < height; i += stepSize) {
20
+ for (let j = 0; j < width; j += stepSize) {
21
+ if (firstPixel !== image.getPixelColor(j, i)) return false
22
+ }
18
23
  }
19
- }
20
24
 
21
- return true
25
+ return true
26
+ } catch (error) {
27
+ if (error.message.includes('maxMemoryUsageInMB')) {
28
+ return false
29
+ }
30
+ throw error
31
+ }
22
32
  }