chore: a bit of cleanup and a small perf gain

This commit is contained in:
Tony Sullivan 2022-09-08 09:59:38 -05:00
parent 60f808f723
commit e33c2dbfb7
4 changed files with 10 additions and 11 deletions

View file

@ -9,7 +9,6 @@ import type { SSRImageService, TransformOptions } from '../loaders/index.js';
import { loadLocalImage, loadRemoteImage } from '../utils/images.js';
import { debug, info, LoggerLevel, warn } from '../utils/logger.js';
import { isRemoteImage } from '../utils/paths.js';
import { processBuffer } from '../vendor/squoosh/main.js';
function getTimeStat(timeStart: number, timeEnd: number) {
const buildTime = timeEnd - timeStart;

View file

@ -83,8 +83,8 @@ const resizeInit = () => resize.default(fsp.readFile(pathify(resizeWasm.toString
const rotateWasm = new URL('./rotate/rotate.wasm', import.meta.url)
// Our decoders currently rely on a `ImageData` global.
import ImageData from './image_data.js'
;(global as any).ImageData = ImageData
import ImageData from './image_data.js';
(global as any).ImageData = ImageData
function resizeNameToIndex(
name: 'triangle' | 'catrom' | 'mitchell' | 'lanczos3'

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 | Uint8Array> {
): Promise<Buffer> {
image = ImageData.from(image)
const e = supportedFormats['mozjpeg']
@ -88,7 +88,7 @@ export async function encodeJpeg(
export async function encodeWebp(
image: ImageData,
{ quality }: { quality: number }
): Promise<Buffer | Uint8Array> {
): Promise<Buffer> {
image = ImageData.from(image)
const e = supportedFormats['webp']
@ -104,7 +104,7 @@ export async function encodeWebp(
export async function encodeAvif(
image: ImageData,
{ quality }: { quality: number }
): Promise<Buffer | Uint8Array> {
): Promise<Buffer> {
image = ImageData.from(image)
const e = supportedFormats['avif']
@ -122,7 +122,7 @@ export async function encodeAvif(
export async function encodePng(
image: ImageData
): Promise<Buffer | Uint8Array> {
): Promise<Buffer> {
image = ImageData.from(image)
const e = supportedFormats['oxipng']

View file

@ -33,13 +33,13 @@ export async function processBuffer(
switch (encoding) {
case 'jpeg':
case 'jpg':
return Buffer.from(await worker.encodeJpeg(imageData, { quality }))
return await worker.encodeJpeg(imageData, { quality })
case 'webp':
return Buffer.from(await worker.encodeWebp(imageData, { quality }))
return await worker.encodeWebp(imageData, { quality })
case 'avif':
return Buffer.from(await worker.encodeAvif(imageData, { quality }))
return await worker.encodeAvif(imageData, { quality })
case 'png':
return Buffer.from(await worker.encodePng(imageData))
return await worker.encodePng(imageData)
default:
throw Error(`Unsupported encoding format`)
}