html-get 2.27.17 → 2.27.19

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 +3 -3
  2. package/src/pdf/media.js +21 -6
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.17",
5
+ "version": "2.27.19",
6
6
  "types": "index.d.ts",
7
7
  "main": "src/index.js",
8
8
  "bin": {
@@ -64,7 +64,7 @@
64
64
  "p-retry": "~4.6.0",
65
65
  "tinyspawn": "~1.5.7",
66
66
  "top-sites": "~1.1.224",
67
- "unpdf": "~1.9.0"
67
+ "unpdf": "~2.1.0"
68
68
  },
69
69
  "devDependencies": {
70
70
  "@browserless/test": "latest",
@@ -99,7 +99,7 @@
99
99
  "typescript": "latest"
100
100
  },
101
101
  "engines": {
102
- "node": ">= 22"
102
+ "node": ">= 24"
103
103
  },
104
104
  "files": [
105
105
  "bin",
package/src/pdf/media.js CHANGED
@@ -5,6 +5,11 @@ const { deflateSync } = require('node:zlib')
5
5
  const MIN_SIDE = 16
6
6
  const MAX_PIXELS = 400 * 400
7
7
  const MAX_LOGO_SIDE = 256
8
+ const RGBA_CHANNELS = 4
9
+ const PNG_COLOR_TYPE_RGBA = 6
10
+ const PNG_BIT_DEPTH = 8
11
+ const OPAQUE = 255
12
+ const TRANSPARENT = 0
8
13
 
9
14
  const crcTable = new Uint32Array(256)
10
15
  for (let n = 0; n < 256; n++) {
@@ -30,10 +35,10 @@ const chunk = (type, data) => {
30
35
  return Buffer.concat([length, body, crc])
31
36
  }
32
37
 
33
- /** Encode raw RGB/RGBA from unpdf into a PNG data URI. node:zlib only. */
34
- const toPngDataUri = ({ width, height, channels, data }) => {
38
+ /** Encode the raw RGBA pixels unpdf extracts into a PNG data URI. node:zlib only. */
39
+ const toPngDataUri = ({ width, height, data }) => {
35
40
  const source = Buffer.from(data.buffer, data.byteOffset, data.byteLength)
36
- const stride = width * channels
41
+ const stride = width * RGBA_CHANNELS
37
42
  const raw = Buffer.alloc((stride + 1) * height)
38
43
  for (let y = 0; y < height; y++) {
39
44
  source.copy(raw, y * (stride + 1) + 1, y * stride, y * stride + stride)
@@ -41,8 +46,8 @@ const toPngDataUri = ({ width, height, channels, data }) => {
41
46
  const ihdr = Buffer.alloc(13)
42
47
  ihdr.writeUInt32BE(width, 0)
43
48
  ihdr.writeUInt32BE(height, 4)
44
- ihdr[8] = 8
45
- ihdr[9] = channels === 4 ? 6 : 2
49
+ ihdr[8] = PNG_BIT_DEPTH
50
+ ihdr[9] = PNG_COLOR_TYPE_RGBA
46
51
  const png = Buffer.concat([
47
52
  Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]),
48
53
  chunk('IHDR', ihdr),
@@ -52,6 +57,16 @@ const toPngDataUri = ({ width, height, channels, data }) => {
52
57
  return `data:image/png;base64,${png.toString('base64')}`
53
58
  }
54
59
 
60
+ /** An image mask has no color of its own: unpdf paints it opaque black on transparent. */
61
+ const isStencil = ({ data }) => {
62
+ for (let offset = 0; offset < data.length; offset += RGBA_CHANNELS) {
63
+ if (data[offset] || data[offset + 1] || data[offset + 2]) return false
64
+ const alpha = data[offset + 3]
65
+ if (alpha !== OPAQUE && alpha !== TRANSPARENT) return false
66
+ }
67
+ return true
68
+ }
69
+
55
70
  const usable = images =>
56
71
  images.filter(
57
72
  img =>
@@ -60,7 +75,7 @@ const usable = images =>
60
75
  img.width >= MIN_SIDE &&
61
76
  img.height >= MIN_SIDE &&
62
77
  img.width * img.height <= MAX_PIXELS &&
63
- (img.channels === 3 || img.channels === 4)
78
+ !isStencil(img)
64
79
  )
65
80
 
66
81
  const isLogo = img =>