From fa331586d77a48b597e15dab711d20983e58a546 Mon Sep 17 00:00:00 2001 From: Surma Date: Mon, 24 May 2021 17:56:53 +0100 Subject: [PATCH] Handle more input types gracefully --- libsquoosh/src/index.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/libsquoosh/src/index.js b/libsquoosh/src/index.js index 93c1a895..dfd00cc4 100644 --- a/libsquoosh/src/index.js +++ b/libsquoosh/src/index.js @@ -9,7 +9,18 @@ import { autoOptimize } from './auto-optimizer.js'; export { ImagePool, encoders, preprocessors }; async function decodeFile({ file }) { - const buffer = await fsp.readFile(file); + let buffer; + if (ArrayBuffer.isView(file)) { + buffer = Buffer.from(file.buffer); + } else if (file instanceof ArrayBuffer) { + buffer = Buffer.from(file); + } else if (file instanceof Buffer) { + buffer = file; + } else if (typeof file === 'string') { + buffer = await fsp.readFile(file); + } else { + throw Error('Unexpected input type'); + } const firstChunk = buffer.slice(0, 16); const firstChunkString = Array.from(firstChunk) .map((v) => String.fromCodePoint(v))