mirror of https://github.com/VERT-sh/VERT.git
52 lines
1.1 KiB
TypeScript
52 lines
1.1 KiB
TypeScript
import Vips from "wasm-vips";
|
|
|
|
const vipsPromise = Vips({
|
|
dynamicLibraries: ["vips-jxl.wasm", "vips-heif.wasm", "vips-resvg.wasm"],
|
|
});
|
|
|
|
vipsPromise
|
|
.then(() => {
|
|
postMessage({ type: "loaded" });
|
|
})
|
|
.catch((error) => {
|
|
postMessage({ type: "error", error });
|
|
});
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
const handleMessage = async (message: any): Promise<any> => {
|
|
const vips = await vipsPromise;
|
|
switch (message.type) {
|
|
case "convert": {
|
|
if (!message.to.startsWith(".")) message.to = `.${message.to}`;
|
|
const image = vips.Image.newFromBuffer(
|
|
await message.input.file.arrayBuffer(),
|
|
`${message.to === ".gif" || message.to === ".webp" ? "[n=-1]" : ""}`,
|
|
);
|
|
const output = image.writeToBuffer(message.to);
|
|
image.delete();
|
|
return {
|
|
type: "finished",
|
|
output: output.buffer,
|
|
};
|
|
}
|
|
}
|
|
};
|
|
|
|
onmessage = async (e) => {
|
|
const message = e.data;
|
|
try {
|
|
const res = await handleMessage(message);
|
|
if (!res) return;
|
|
postMessage({
|
|
...res,
|
|
id: message.id,
|
|
});
|
|
} catch (e) {
|
|
postMessage({
|
|
type: "error",
|
|
error: e,
|
|
id: message.id,
|
|
});
|
|
}
|
|
};
|