Skip to content

Commit

Permalink
fix: [].push stack overflow if image data too big
Browse files Browse the repository at this point in the history
  • Loading branch information
luwes committed Dec 13, 2024
1 parent 1380d26 commit 0443adb
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion blurup.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { imageDimensionsFromStream } from 'image-dimensions';
import { imageDimensionsFromData } from 'image-dimensions';

const defaultOptions = {
type: 'webp',
Expand Down Expand Up @@ -84,6 +84,19 @@ async function getSourceImageDimensions(imageURL, fetchOptions) {
return imageDimensionsFromStream(response.body);
}

async function imageDimensionsFromStream(stream) {
let chunks = [];

for await (const chunk of stream) {
chunks = [...chunks, ...chunk];

const dimensions = imageDimensionsFromData(new Uint8Array(chunks));
if (dimensions) {
return dimensions;
}
}
}

async function getTinyImageDataURL(imageURL, fetchOptions) {
const response = await fetch(imageURL, fetchOptions);
validateResponse(imageURL, response);
Expand Down

0 comments on commit 0443adb

Please sign in to comment.