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); const data = await processBuffer(inputBuffer, operations, transform.format, transform.quality || 100);
return { return {
data, data: Buffer.from(data),
format: transform.format format: transform.format
} }
} }

View file

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

View file

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