rsnext/packages/next/next-server/server/lib/squoosh/main.ts
Shu Ding 241a916e03
Improve image optimizer to only create 1 worker thread (#23188)
As stated in #23157, this PR merges all the operations into 1 worker thread (`processBuffer` in `impl.ts`) and only pass a list of operation names and args into the worker. This should improve the speed and memory usage of next/image.

Fixes #23157. X-ref: #22925.
2021-03-18 15:51:36 +00:00

25 lines
611 B
TypeScript

import { Worker } from 'jest-worker'
import * as path from 'path'
import { execOnce } from '../../../lib/utils'
import { Operation, Encoding } from './impl'
const getWorker = execOnce(
() =>
new Worker(path.resolve(__dirname, 'impl'), {
enableWorkerThreads: true,
})
)
export { Operation }
export async function processBuffer(
buffer: Buffer,
operations: Operation[],
encoding: Encoding,
quality: number
): Promise<Buffer> {
const worker: typeof import('./impl') = getWorker() as any
return Buffer.from(
await worker.processBuffer(buffer, operations, encoding, quality)
)
}