discourse/public/javascripts/media-optimization-worker.js
Martin Brennan 2942ef375d
DEV: System specs for image optimization in composer (#36060)
Followup 66cee823ab

Introduces a few basic system specs to verify that image optimization is
working as expected when uploading images via the composer. The specs
check
that images are optimized and that the optimization process completes
successfully
for both png and jpg images.

This PR also improves the formatting of optimization logs, including
adding
information about the original image size in bytes
2025-11-19 11:37:15 +10:00

51 lines
1.3 KiB
JavaScript

onmessage = async function (e) {
switch (e.data.type) {
case "compress":
try {
globalThis.debugMode = e.data.settings.debug_mode;
let optimized = await globalThis.optimize(
e.data.file,
e.data.fileName,
e.data.width,
e.data.height,
e.data.originalFileSize,
e.data.settings
);
postMessage(
{
type: "file",
file: optimized,
fileName: e.data.fileName,
fileId: e.data.fileId,
},
[optimized]
);
} catch (error) {
console.error(error);
postMessage({
type: "error",
file: e.data.file,
fileName: e.data.fileName,
fileId: e.data.fileId,
});
}
break;
case "install":
try {
globalThis.document = {}; // webpack expects this to exist
await loadLibs(e.data.settings);
postMessage({ type: "installed" });
} catch (error) {
console.error(error);
postMessage({ type: "installFailed", errorMessage: error.message });
}
break;
default:
logIfDebug(`Sorry, we are out of ${e}.`);
}
};
async function loadLibs(settings) {
importScripts(settings.mediaOptimizationBundle);
}