Skip to content

Commit c9ae8df

Browse files
test: move back to probe-image-size
1 parent 698ca74 commit c9ae8df

File tree

3 files changed

+194
-45
lines changed

3 files changed

+194
-45
lines changed

package-lock.json

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

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@
8888
"husky": "9.1.7",
8989
"i18next": "23.16.8",
9090
"i18next-fs-backend": "2.6.0",
91-
"image-size": "2.0.1",
9291
"jest": "29.7.0",
92+
"image-size": "2.0.1",
9393
"jest-json-schema-extended": "1.0.1",
9494
"joi": "17.13.3",
9595
"js-yaml": "4.1.0",
@@ -103,6 +103,7 @@
103103
"npm-run-all2": "7.0.2",
104104
"piscina": "4.9.2",
105105
"prettier": "3.4.2",
106+
"probe-image-size": "7.2.3",
106107
"shx": "0.4.0",
107108
"terser": "5.39.0",
108109
"typescript": "5.8.2"

utils/get-image-dimensions.js

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

7-
// Minimum required bytes for common image formats (approximate)
8-
const imageMinBytes = {
9-
jpg: 410,
10-
png: 33,
11-
gif: 14,
12-
webp: 30,
13-
bmp: 26,
14-
default: 256 // Fallback for other image types
15-
};
16-
17-
const probeImage = async url => {
18-
const response = await fetch(url);
19-
if (!response.ok)
20-
throw new Error(`Failed to fetch image: ${response.statusText}`);
8+
// const probeImage = async url => {
9+
// const response = await fetch(url);
10+
// if (!response.ok)
11+
// throw new Error(`Failed to fetch image: ${response.statusText}`);
2112

22-
const chunks = [];
23-
let totalLength = 0;
24-
const reader = response.body;
13+
// const reader = response.body;
14+
// let imgBuffer = Buffer.alloc(0);
15+
// // const maxBufferSize = 3072; // Limit memory usage to 3 KB
16+
// const maxBufferSize = 1024; // Limit memory usage to 1 KB
2517

26-
for await (const chunk of reader) {
27-
chunks.push(chunk);
28-
totalLength += chunk.length;
18+
// for await (const chunk of reader) {
19+
// imgBuffer = Buffer.concat([imgBuffer, chunk]);
2920

30-
try {
31-
const buffer = Buffer.concat(chunks, totalLength);
21+
// try {
22+
// const dimensions = imageSize(imgBuffer);
23+
// if (dimensions.width && dimensions.height) {
24+
// response.body.destroy(); // Stop downloading
25+
// return dimensions;
26+
// }
27+
// } catch (err) {
28+
// // Continue reading if more data is needed
29+
// }
3230

33-
// Try to detect image format (first few bytes)
34-
const { type } = imageSize(buffer);
35-
const minBytes = imageMinBytes[type] || imageMinBytes.default;
31+
// // Keep memory usage low by trimming old bytes
32+
// if (imgBuffer.length > maxBufferSize) {
33+
// imgBuffer = imgBuffer.subarray(imgBuffer.length - maxBufferSize);
34+
// }
35+
// }
3636

37-
// Check buffer length before probing image
38-
if (buffer.length < minBytes) continue;
39-
40-
// Get dimensions
41-
const dimensions = imageSize(buffer);
42-
if (dimensions.width && dimensions.height) {
43-
response.body.destroy(); // Stop downloading
44-
return dimensions;
45-
}
46-
} catch (err) {
47-
// Continue reading if more data is needed
48-
}
49-
}
50-
51-
throw new Error('Could not determine image size');
52-
};
37+
// throw new Error('Could not determine image size');
38+
// };
5339

5440
const getImageDimensions = async (url, description) => {
5541
try {
@@ -65,7 +51,7 @@ const getImageDimensions = async (url, description) => {
6551
let imageDimensions = getCache(url);
6652
if (imageDimensions) return imageDimensions;
6753

68-
const res = await probeImage(url);
54+
const res = await probe(url);
6955
imageDimensions = {
7056
width: res?.width ? res?.width : defaultDimensions.width,
7157
height: res?.height ? res?.height : defaultDimensions.height

0 commit comments

Comments
 (0)