Skip to content

Commit 8608476

Browse files
fix: remove probe-image, roll our own solution
1 parent 93cbe32 commit 8608476

File tree

3 files changed

+53
-137
lines changed

3 files changed

+53
-137
lines changed

package-lock.json

+20-133
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
"husky": "9.1.7",
8989
"i18next": "23.16.8",
9090
"i18next-fs-backend": "2.6.0",
91+
"image-size": "2.0.1",
9192
"jest": "29.7.0",
9293
"jest-json-schema-extended": "1.0.1",
9394
"joi": "17.13.3",
@@ -102,7 +103,6 @@
102103
"npm-run-all2": "7.0.2",
103104
"piscina": "4.9.2",
104105
"prettier": "3.4.2",
105-
"probe-image-size": "7.2.3",
106106
"shx": "0.4.0",
107107
"terser": "5.39.0",
108108
"typescript": "5.8.2"

utils/get-image-dimensions.js

+32-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,43 @@
1-
const probe = require('probe-image-size');
1+
const fetch = require('node-fetch');
2+
const { imageSize } = require('image-size');
23
const errorLogger = require('./error-logger');
34
const { getCache, setCache } = require('./cache');
45
const defaultDimensions = { width: 600, height: 400 };
56

7+
async function probeImage(url) {
8+
const response = await fetch(url);
9+
if (!response.ok)
10+
throw new Error(`Failed to fetch image: ${response.statusText}`);
11+
12+
const chunks = [];
13+
const reader = response.body;
14+
15+
for await (const chunk of reader) {
16+
chunks.push(chunk);
17+
18+
try {
19+
// Try to determine image size with available data
20+
const dimensions = imageSize(Buffer.concat(chunks));
21+
if (dimensions.width && dimensions.height) {
22+
response.body.destroy(); // Stop data stream
23+
return dimensions;
24+
}
25+
} catch (err) {
26+
// Keep reading if more data is needed
27+
}
28+
}
29+
30+
throw new Error('Could not determine image size');
31+
}
32+
633
const getImageDimensions = async (url, description) => {
734
try {
35+
if (url.startsWith('data:'))
36+
throw new Error('Data URI images are not supported');
837
let imageDimensions = getCache(url);
938
if (imageDimensions) return imageDimensions;
1039

11-
const res = await probe(url);
40+
const res = await probeImage(url);
1241
imageDimensions = {
1342
width: res?.width ? res?.width : defaultDimensions.width,
1443
height: res?.height ? res?.height : defaultDimensions.height
@@ -17,7 +46,7 @@ const getImageDimensions = async (url, description) => {
1746

1847
return imageDimensions;
1948
} catch (err) {
20-
if (err.statusCode) errorLogger({ type: 'image', name: description }); // Only write HTTP status code errors to log
49+
errorLogger({ type: 'image', name: description });
2150

2251
return defaultDimensions;
2352
}

0 commit comments

Comments
 (0)