fix: dev broke with the shift to wasm workers

This commit is contained in:
Tony Sullivan 2022-09-15 12:10:17 -05:00
parent bd7824e8b8
commit d2bfbac91c
3 changed files with 18 additions and 14 deletions

View file

@ -111,7 +111,7 @@ class SquooshService extends BaseSSRService {
const data = await processBuffer(inputBuffer, operations, transform.format, transform.quality || 100);
return {
data,
data: Buffer.from(data),
format: transform.format
}
}

View file

@ -89,7 +89,7 @@ export async function processBuffer(
operations: Operation[],
encoding: OutputFormat,
quality: number
): Promise<Buffer> {
): Promise<Uint8Array> {
// @ts-ignore
const worker = await getWorker()
@ -116,25 +116,29 @@ export async function processBuffer(
switch (encoding) {
case 'avif':
return await worker.dispatchJob({ operation: 'encodeavif', imageData, quality: quality || 100 }) as Buffer;
return await worker.dispatchJob({
operation: 'encodeavif',
imageData,
quality: quality || 100
}) as Uint8Array;
case 'jpeg':
case 'jpg':
return await worker.dispatchJob({
operation: 'encodejpeg',
imageData,
quality: quality || 100,
}) as Buffer;
}) as Uint8Array;
case 'png':
return await worker.dispatchJob({
operation: 'encodepng',
imageData,
}) as Buffer;
}) as Uint8Array;
case 'webp':
return await worker.dispatchJob({
operation: 'encodejpeg',
imageData,
quality: quality || 100,
}) as Buffer;
}) as Uint8Array;
default:
throw Error(`Unsupported encoding format`)
}

View file

@ -72,7 +72,7 @@ export async function resize({ image, width, height }: ResizeOpts) {
export async function encodeJpeg(
image: ImageData,
{ quality }: { quality: number }
): Promise<Buffer> {
): Promise<Uint8Array> {
image = ImageData.from(image)
const e = supportedFormats['mozjpeg']
@ -82,13 +82,13 @@ export async function encodeJpeg(
...e.defaultEncoderOptions,
quality,
})
return Buffer.from(r)
return r
}
export async function encodeWebp(
image: ImageData,
{ quality }: { quality: number }
): Promise<Buffer> {
): Promise<Uint8Array> {
image = ImageData.from(image)
const e = supportedFormats['webp']
@ -98,13 +98,13 @@ export async function encodeWebp(
...e.defaultEncoderOptions,
quality,
})
return Buffer.from(r)
return r
}
export async function encodeAvif(
image: ImageData,
{ quality }: { quality: number }
): Promise<Buffer> {
): Promise<Uint8Array> {
image = ImageData.from(image)
const e = supportedFormats['avif']
@ -117,12 +117,12 @@ export async function encodeAvif(
// so a lower value yields higher quality (0 to 100).
cqLevel: quality === 0 ? val : Math.round(val - (quality / 100) * val),
})
return Buffer.from(r)
return r
}
export async function encodePng(
image: ImageData
): Promise<Buffer> {
): Promise<Uint8Array> {
image = ImageData.from(image)
const e = supportedFormats['oxipng']
@ -131,5 +131,5 @@ export async function encodePng(
const r = await m.encode(image.data, image.width, image.height, {
...e.defaultEncoderOptions,
})
return Buffer.from(r)
return r
}