fad3867adb
* WIP: adding a service built on @squoosh/lib * WIP: investigating memory leaks in Squoosh * WIP: vendoring Squoosh to work with our build * chore: a bit of cleanup and a small perf gain * removing a few unused deps * fix: removing temp .only() in sharp test * hooking up the last build steps to copy over .wasm files * removing the duplicated lib/*.wasm files * defaulting to Sharp for the initial @next release * make sure pnpm always runs the postbuild script * removing a few node dependencies * refactor: move the copy .wasm build step out of the SSR bundle * linter fixes * fixing lock file * chore: add TEMP changeset * fix built wasm location for SSG builds * Revert "defaulting to Sharp for the initial @next release" This reverts commit1a8d4f7f60
. * removing sharp dependency * Revert "fix built wasm location for SSG builds" This reverts commit446b80bb53
. * chore: update lockfile * fixing up image tests for the wasm loader * updating the README for squoosh * parallel wasm builds * refactor: a bit of house keeping * perf: allow a thread for each output encoding format * fix: dev broke with the shift to wasm workers * adds a new `astro:build:generated` hook for SSG builds * fix: typo + calling cleanup methods in wasm codecs * adding @astrojs/webapi for the TransformStream polyfill * Revert "adding @astrojs/webapi for the TransformStream polyfill" This reverts commit39e5b845a5
. * perf: using sharp for most of the CI tests * chore: update lockfile * removing hard-coded squoosh imports * fix: adding sharp to rollup externals * test: using dev for the squoosh tests * fix: updating the build output dir for wasm filles in SSG builds * updating the changeset with migration details * Revert "adds a new `astro:build:generated` hook for SSG builds" This reverts commit59b5fec7be
. * nit: adding comments for the wasm file copy * chore: fix eslint warning
125 lines
3.2 KiB
TypeScript
125 lines
3.2 KiB
TypeScript
/* tslint-disable ban-types */
|
|
import { Worker, parentPort } from 'worker_threads';
|
|
import { TransformStream } from 'web-streams-polyfill';
|
|
|
|
function uuid() {
|
|
return Array.from({ length: 16 }, () =>
|
|
Math.floor(Math.random() * 256).toString(16),
|
|
).join('');
|
|
}
|
|
|
|
interface Job<I> {
|
|
msg: I;
|
|
resolve: (result: any) => void;
|
|
reject: (reason: any) => void;
|
|
}
|
|
|
|
export default class WorkerPool<I, O> {
|
|
public numWorkers: number;
|
|
public jobQueue: TransformStream<Job<I>, Job<I>>;
|
|
public workerQueue: TransformStream<Worker, Worker>;
|
|
public done: Promise<void>;
|
|
|
|
constructor(numWorkers: number, workerFile: string) {
|
|
this.numWorkers = numWorkers;
|
|
this.jobQueue = new TransformStream();
|
|
this.workerQueue = new TransformStream();
|
|
|
|
const writer = this.workerQueue.writable.getWriter();
|
|
for (let i = 0; i < numWorkers; i++) {
|
|
writer.write(new Worker(workerFile));
|
|
}
|
|
writer.releaseLock();
|
|
|
|
this.done = this._readLoop();
|
|
}
|
|
|
|
async _readLoop() {
|
|
const reader = this.jobQueue.readable.getReader();
|
|
while (true) {
|
|
const { value, done } = await reader.read();
|
|
if (done) {
|
|
await this._terminateAll();
|
|
return;
|
|
}
|
|
|
|
if (!value) {
|
|
throw new Error('Reader did not return any value');
|
|
}
|
|
|
|
const { msg, resolve, reject } = value;
|
|
const worker = await this._nextWorker();
|
|
this.jobPromise(worker, msg)
|
|
.then((result) => resolve(result))
|
|
.catch((reason) => reject(reason))
|
|
.finally(() => {
|
|
// Return the worker to the pool
|
|
const writer = this.workerQueue.writable.getWriter();
|
|
writer.write(worker);
|
|
writer.releaseLock();
|
|
});
|
|
}
|
|
}
|
|
|
|
async _nextWorker() {
|
|
const reader = this.workerQueue.readable.getReader();
|
|
const { value } = await reader.read();
|
|
reader.releaseLock();
|
|
if (!value) {
|
|
throw new Error('No worker left');
|
|
}
|
|
|
|
return value;
|
|
}
|
|
|
|
async _terminateAll() {
|
|
for (let n = 0; n < this.numWorkers; n++) {
|
|
const worker = await this._nextWorker();
|
|
worker.terminate();
|
|
}
|
|
this.workerQueue.writable.close();
|
|
}
|
|
|
|
async join() {
|
|
this.jobQueue.writable.getWriter().close();
|
|
await this.done;
|
|
}
|
|
|
|
dispatchJob(msg: I): Promise<O> {
|
|
return new Promise((resolve, reject) => {
|
|
const writer = this.jobQueue.writable.getWriter();
|
|
writer.write({ msg, resolve, reject });
|
|
writer.releaseLock();
|
|
});
|
|
}
|
|
|
|
private jobPromise(worker: Worker, msg: I) {
|
|
return new Promise((resolve, reject) => {
|
|
const id = uuid();
|
|
worker.postMessage({ msg, id });
|
|
worker.on('message', function f({ error, result, id: rid }) {
|
|
if (rid !== id) {
|
|
return;
|
|
}
|
|
if (error) {
|
|
reject(error);
|
|
return;
|
|
}
|
|
worker.off('message', f);
|
|
resolve(result);
|
|
});
|
|
});
|
|
}
|
|
|
|
static useThisThreadAsWorker<I, O>(cb: (msg: I) => O) {
|
|
parentPort!.on('message', async (data) => {
|
|
const { msg, id } = data;
|
|
try {
|
|
const result = await cb(msg);
|
|
parentPort!.postMessage({ result, id });
|
|
} catch (e: any) {
|
|
parentPort!.postMessage({ error: e.message, id });
|
|
}
|
|
});
|
|
}
|
|
}
|