mirror of
https://github.com/GoogleChromeLabs/squoosh.git
synced 2025-11-13 09:17:20 +00:00
Handle more input types gracefully
This commit is contained in:
@@ -9,7 +9,18 @@ import { autoOptimize } from './auto-optimizer.js';
|
|||||||
export { ImagePool, encoders, preprocessors };
|
export { ImagePool, encoders, preprocessors };
|
||||||
|
|
||||||
async function decodeFile({ file }) {
|
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 firstChunk = buffer.slice(0, 16);
|
||||||
const firstChunkString = Array.from(firstChunk)
|
const firstChunkString = Array.from(firstChunk)
|
||||||
.map((v) => String.fromCodePoint(v))
|
.map((v) => String.fromCodePoint(v))
|
||||||
|
|||||||
Reference in New Issue
Block a user