@squoosh-kit/webp 0.1.17 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -4
- package/dist/bridge.d.ts +1 -0
- package/dist/bridge.d.ts.map +1 -1
- package/dist/index.browser.mjs +319 -111
- package/dist/index.browser.mjs.map +9 -9
- package/dist/index.bun.js +319 -111
- package/dist/index.bun.js.map +9 -9
- package/dist/index.d.ts +25 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.node.cjs +332 -118
- package/dist/index.node.cjs.map +9 -9
- package/dist/index.node.mjs +319 -111
- package/dist/index.node.mjs.map +9 -9
- package/dist/webp.worker.browser.mjs +293 -111
- package/dist/webp.worker.browser.mjs.map +7 -7
- package/dist/webp.worker.bun.js +293 -111
- package/dist/webp.worker.bun.js.map +7 -7
- package/dist/webp.worker.d.ts +5 -1
- package/dist/webp.worker.d.ts.map +1 -1
- package/dist/webp.worker.node.cjs +304 -117
- package/dist/webp.worker.node.cjs.map +7 -7
- package/dist/webp.worker.node.mjs +293 -111
- package/dist/webp.worker.node.mjs.map +7 -7
- package/package.json +6 -4
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
var __defProp = Object.defineProperty;
|
|
2
|
+
var __returnValue = (v) => v;
|
|
3
|
+
function __exportSetter(name, newValue) {
|
|
4
|
+
this[name] = __returnValue.bind(null, newValue);
|
|
5
|
+
}
|
|
2
6
|
var __export = (target, all) => {
|
|
3
7
|
for (var name in all)
|
|
4
8
|
__defProp(target, name, {
|
|
5
9
|
get: all[name],
|
|
6
10
|
enumerable: true,
|
|
7
11
|
configurable: true,
|
|
8
|
-
set: (
|
|
12
|
+
set: __exportSetter.bind(all, name)
|
|
9
13
|
});
|
|
10
14
|
};
|
|
11
15
|
var __esm = (fn, res) => () => (fn && (res = fn(fn = 0)), res);
|
|
@@ -71,6 +75,50 @@ function createCodecWorker(workerFilename, options) {
|
|
|
71
75
|
"webp.worker.js": {
|
|
72
76
|
package: "@squoosh-kit/webp",
|
|
73
77
|
specifier: "webp.worker.js"
|
|
78
|
+
},
|
|
79
|
+
"avif.worker.js": {
|
|
80
|
+
package: "@squoosh-kit/avif",
|
|
81
|
+
specifier: "avif.worker.js"
|
|
82
|
+
},
|
|
83
|
+
"mozjpeg.worker.js": {
|
|
84
|
+
package: "@squoosh-kit/mozjpeg",
|
|
85
|
+
specifier: "mozjpeg.worker.js"
|
|
86
|
+
},
|
|
87
|
+
"jxl.worker.js": {
|
|
88
|
+
package: "@squoosh-kit/jxl",
|
|
89
|
+
specifier: "jxl.worker.js"
|
|
90
|
+
},
|
|
91
|
+
"oxipng.worker.js": {
|
|
92
|
+
package: "@squoosh-kit/oxipng",
|
|
93
|
+
specifier: "oxipng.worker.js"
|
|
94
|
+
},
|
|
95
|
+
"png.worker.js": {
|
|
96
|
+
package: "@squoosh-kit/png",
|
|
97
|
+
specifier: "png.worker.js"
|
|
98
|
+
},
|
|
99
|
+
"imagequant.worker.js": {
|
|
100
|
+
package: "@squoosh-kit/imagequant",
|
|
101
|
+
specifier: "imagequant.worker.js"
|
|
102
|
+
},
|
|
103
|
+
"qoi.worker.js": {
|
|
104
|
+
package: "@squoosh-kit/qoi",
|
|
105
|
+
specifier: "qoi.worker.js"
|
|
106
|
+
},
|
|
107
|
+
"wp2.worker.js": {
|
|
108
|
+
package: "@squoosh-kit/wp2",
|
|
109
|
+
specifier: "wp2.worker.js"
|
|
110
|
+
},
|
|
111
|
+
"hqx.worker.js": {
|
|
112
|
+
package: "@squoosh-kit/hqx",
|
|
113
|
+
specifier: "hqx.worker.js"
|
|
114
|
+
},
|
|
115
|
+
"rotate.worker.js": {
|
|
116
|
+
package: "@squoosh-kit/rotate",
|
|
117
|
+
specifier: "rotate.worker.js"
|
|
118
|
+
},
|
|
119
|
+
"visdif.worker.js": {
|
|
120
|
+
package: "@squoosh-kit/visdif",
|
|
121
|
+
specifier: "visdif.worker.js"
|
|
74
122
|
}
|
|
75
123
|
};
|
|
76
124
|
const workerConfig = workerMap[normalizedName];
|
|
@@ -101,7 +149,7 @@ function createCodecWorker(workerFilename, options) {
|
|
|
101
149
|
return worker;
|
|
102
150
|
} catch (e) {
|
|
103
151
|
console.error(`[worker-helper] Failed to load worker from assetPath URL: ${workerUrl}`, e);
|
|
104
|
-
throw new Error(`Worker failed to load from ${workerUrl}: ${e instanceof Error ? e.message : String(e)}
|
|
152
|
+
throw new Error(`Worker failed to load from ${workerUrl}: ${e instanceof Error ? e.message : String(e)}`, { cause: e });
|
|
105
153
|
}
|
|
106
154
|
}
|
|
107
155
|
const pathStrategies = [
|
|
@@ -134,7 +182,8 @@ function createCodecWorker(workerFilename, options) {
|
|
|
134
182
|
}
|
|
135
183
|
const platformExt = isBun() ? ".bun.js" : ".node.mjs";
|
|
136
184
|
const baseName = normalizedName.replace(".js", "");
|
|
137
|
-
const
|
|
185
|
+
const pkgName = workerConfig.package.split("/")[1];
|
|
186
|
+
const srcRelPath = `../../${pkgName}/src/${baseName}.ts`;
|
|
138
187
|
console.log("srcRelPath:", srcRelPath);
|
|
139
188
|
console.log("import.meta.url:", import.meta.url);
|
|
140
189
|
try {
|
|
@@ -142,7 +191,7 @@ function createCodecWorker(workerFilename, options) {
|
|
|
142
191
|
type: "module"
|
|
143
192
|
});
|
|
144
193
|
} catch {
|
|
145
|
-
const distRelPath =
|
|
194
|
+
const distRelPath = `../../${pkgName}/dist/${baseName}.${platformExt.slice(1)}`;
|
|
146
195
|
console.log("distRelPath:", distRelPath);
|
|
147
196
|
console.log("import.meta.url:", import.meta.url);
|
|
148
197
|
try {
|
|
@@ -162,7 +211,7 @@ function createCodecWorker(workerFilename, options) {
|
|
|
162
211
|
throw new Error(`Failed to create worker from ${normalizedName}. ` + `Tried TypeScript source, dist output, and import.meta.resolve. ` + `Ensure the @squoosh-kit/resize and @squoosh-kit/webp packages are installed.`);
|
|
163
212
|
} catch (error) {
|
|
164
213
|
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
165
|
-
throw new Error(`Failed to create worker from ${normalizedName}: ${errorMessage}. ` + `Ensure the @squoosh-kit/resize and @squoosh-kit/webp packages are installed. ` + `If you're using Vite, ensure the worker files are not being optimized as dependencies
|
|
214
|
+
throw new Error(`Failed to create worker from ${normalizedName}: ${errorMessage}. ` + `Ensure the @squoosh-kit/resize and @squoosh-kit/webp packages are installed. ` + `If you're using Vite, ensure the worker files are not being optimized as dependencies.`, { cause: error });
|
|
166
215
|
}
|
|
167
216
|
}
|
|
168
217
|
function createReadyWorker(workerFilename, options, timeoutMs = 1e4) {
|
|
@@ -314,7 +363,7 @@ function validateWebpOptions(options) {
|
|
|
314
363
|
if (typeof quality !== "number") {
|
|
315
364
|
throw new TypeError("quality must be a number");
|
|
316
365
|
}
|
|
317
|
-
if (!Number.isInteger(quality)) {
|
|
366
|
+
if (!Number.isFinite(quality) || !Number.isInteger(quality)) {
|
|
318
367
|
throw new RangeError("quality must be an integer in the range 0-100, got floating point");
|
|
319
368
|
}
|
|
320
369
|
if (quality < 0 || quality > 100) {
|
|
@@ -326,80 +375,95 @@ function validateWebpOptions(options) {
|
|
|
326
375
|
// src/webp.worker.ts
|
|
327
376
|
var exports_webp_worker = {};
|
|
328
377
|
__export(exports_webp_worker, {
|
|
329
|
-
webpEncodeClient: () => webpEncodeClient
|
|
378
|
+
webpEncodeClient: () => webpEncodeClient,
|
|
379
|
+
webpDecodeClient: () => webpDecodeClient
|
|
330
380
|
});
|
|
331
381
|
async function loadWebPModule() {
|
|
332
|
-
if (cachedModule)
|
|
382
|
+
if (cachedModule)
|
|
333
383
|
return cachedModule;
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
globalSelf
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
globalThis.self = globalThis;
|
|
348
|
-
}
|
|
349
|
-
let moduleFactory;
|
|
350
|
-
const pathsToTry = ["./wasm/" + modulePath, "../wasm/" + modulePath];
|
|
351
|
-
let lastError = null;
|
|
352
|
-
for (const importPath of pathsToTry) {
|
|
353
|
-
try {
|
|
354
|
-
moduleFactory = (await import(importPath)).default;
|
|
355
|
-
console.log(`[WebP Worker] Successfully loaded module from: ${importPath}`);
|
|
356
|
-
break;
|
|
357
|
-
} catch (error) {
|
|
358
|
-
lastError = error instanceof Error ? error : new Error(String(error));
|
|
359
|
-
console.warn(`[WebP Worker] Failed to load from ${importPath}, trying next path...`);
|
|
384
|
+
if (loadModulePromise)
|
|
385
|
+
return loadModulePromise;
|
|
386
|
+
loadModulePromise = (async () => {
|
|
387
|
+
const simdSupported2 = await detectSimd();
|
|
388
|
+
const modulePath = simdSupported2 ? "webp/webp_enc_simd.js" : "webp/webp_enc.js";
|
|
389
|
+
try {
|
|
390
|
+
console.log("[WebP Worker] Initializing. SIMD support:", simdSupported2);
|
|
391
|
+
console.log(`[WebP Worker] Attempting to import module from path: ${modulePath}`);
|
|
392
|
+
const globalSelf = typeof self !== "undefined" ? self : globalThis;
|
|
393
|
+
if (!globalSelf.location) {
|
|
394
|
+
globalSelf.location = {
|
|
395
|
+
href: import.meta.url
|
|
396
|
+
};
|
|
360
397
|
}
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
const workerBaseUrl = new URL(".", import.meta.url);
|
|
370
|
-
let lastError2 = null;
|
|
371
|
-
for (const wasmPath of wasmPaths) {
|
|
398
|
+
if (typeof self === "undefined" && typeof globalThis !== "undefined") {
|
|
399
|
+
globalThis.self = globalThis;
|
|
400
|
+
}
|
|
401
|
+
let moduleFactory;
|
|
402
|
+
const isSource = import.meta.url.includes("/src/");
|
|
403
|
+
const pathsToTry = isSource ? ["../wasm/" + modulePath, "./wasm/" + modulePath] : ["./wasm/" + modulePath, "../wasm/" + modulePath];
|
|
404
|
+
let lastError = null;
|
|
405
|
+
for (const importPath of pathsToTry) {
|
|
372
406
|
try {
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
href: import.meta.url
|
|
380
|
-
};
|
|
381
|
-
}
|
|
382
|
-
if (typeof self === "undefined" && typeof globalThis !== "undefined") {
|
|
383
|
-
globalThis.self = globalThis;
|
|
384
|
-
}
|
|
385
|
-
return await moduleFactory2({
|
|
386
|
-
noInitialRun: true,
|
|
387
|
-
wasmBinary
|
|
388
|
-
});
|
|
389
|
-
} catch (err) {
|
|
390
|
-
lastError2 = err instanceof Error ? err : new Error(String(err));
|
|
391
|
-
console.warn(`[WebP Worker] Failed to load WASM from ${wasmPath}, trying next path...`);
|
|
407
|
+
moduleFactory = (await import(importPath)).default;
|
|
408
|
+
console.log(`[WebP Worker] Successfully loaded module from: ${importPath}`);
|
|
409
|
+
break;
|
|
410
|
+
} catch (error) {
|
|
411
|
+
lastError = error instanceof Error ? error : new Error(String(error));
|
|
412
|
+
console.warn(`[WebP Worker] Failed to load from ${importPath}, trying next path...`);
|
|
392
413
|
}
|
|
393
414
|
}
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
415
|
+
if (!moduleFactory) {
|
|
416
|
+
throw lastError || new Error("Could not load WebP module from any path");
|
|
417
|
+
}
|
|
418
|
+
console.log("[WebP Worker] Module factory loaded successfully.");
|
|
419
|
+
const wasmPathsToTry = simdSupported2 ? isSource ? [
|
|
420
|
+
"../wasm/webp/webp_enc_simd.wasm",
|
|
421
|
+
"./wasm/webp/webp_enc_simd.wasm"
|
|
422
|
+
] : [
|
|
423
|
+
"./wasm/webp/webp_enc_simd.wasm",
|
|
424
|
+
"../wasm/webp/webp_enc_simd.wasm"
|
|
425
|
+
] : isSource ? ["../wasm/webp/webp_enc.wasm", "./wasm/webp/webp_enc.wasm"] : ["./wasm/webp/webp_enc.wasm", "../wasm/webp/webp_enc.wasm"];
|
|
426
|
+
console.log(`[WebP Worker] Preparing to load WASM binary. Will try paths: ${wasmPathsToTry.join(", ")}`);
|
|
427
|
+
const initModuleWithBinary = async (moduleFactory2, wasmPaths) => {
|
|
428
|
+
const workerBaseUrl = new URL(".", import.meta.url);
|
|
429
|
+
let lastError2 = null;
|
|
430
|
+
for (const wasmPath of wasmPaths) {
|
|
431
|
+
try {
|
|
432
|
+
console.log(`[WebP Worker] Calling loadWasmBinary with path: ${wasmPath}`);
|
|
433
|
+
const wasmBinary = await loadWasmBinary(wasmPath, workerBaseUrl);
|
|
434
|
+
console.log(`[WebP Worker] Successfully fetched WASM binary from ${wasmPath}. Size: ${wasmBinary.byteLength} bytes.`);
|
|
435
|
+
const globalSelf2 = typeof self !== "undefined" ? self : globalThis;
|
|
436
|
+
if (!globalSelf2.location) {
|
|
437
|
+
globalSelf2.location = {
|
|
438
|
+
href: import.meta.url
|
|
439
|
+
};
|
|
440
|
+
}
|
|
441
|
+
if (typeof self === "undefined" && typeof globalThis !== "undefined") {
|
|
442
|
+
globalThis.self = globalThis;
|
|
443
|
+
}
|
|
444
|
+
return await moduleFactory2({
|
|
445
|
+
noInitialRun: true,
|
|
446
|
+
wasmBinary
|
|
447
|
+
});
|
|
448
|
+
} catch (err) {
|
|
449
|
+
lastError2 = err instanceof Error ? err : new Error(String(err));
|
|
450
|
+
console.warn(`[WebP Worker] Failed to load WASM from ${wasmPath}, trying next path...`);
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
throw lastError2 || new Error("Could not load WASM binary from any of the attempted paths");
|
|
454
|
+
};
|
|
455
|
+
cachedModule = await initModuleWithBinary(moduleFactory, wasmPathsToTry);
|
|
456
|
+
console.log("[WebP Worker] WebP module initialized successfully.");
|
|
457
|
+
return cachedModule;
|
|
458
|
+
} catch (err) {
|
|
459
|
+
console.error(`[WebP Worker] CRITICAL: Failed to load WebP module from path: ${modulePath}`, err);
|
|
460
|
+
throw err;
|
|
461
|
+
}
|
|
462
|
+
})().catch((err) => {
|
|
463
|
+
loadModulePromise = null;
|
|
401
464
|
throw err;
|
|
402
|
-
}
|
|
465
|
+
});
|
|
466
|
+
return loadModulePromise;
|
|
403
467
|
}
|
|
404
468
|
function createEncodeOptions(options) {
|
|
405
469
|
return {
|
|
@@ -444,27 +508,13 @@ async function webpEncodeClient(image, options, signal) {
|
|
|
444
508
|
if (!(data instanceof Uint8Array) && !(data instanceof Uint8ClampedArray)) {
|
|
445
509
|
throw new Error("Image data must be Uint8Array or Uint8ClampedArray");
|
|
446
510
|
}
|
|
447
|
-
const t0 = performance.now();
|
|
448
511
|
const module = await loadWebPModule();
|
|
449
|
-
const t1 = performance.now();
|
|
450
|
-
if (typeof console !== "undefined" && console.log) {
|
|
451
|
-
console.log(`[WebP] module loading took ${(t1 - t0).toFixed(2)}ms`);
|
|
452
|
-
console.log(`[WebP] Module type: ${cachedModule && "encode" in cachedModule ? "Ready" : "Unknown"}`);
|
|
453
|
-
}
|
|
454
512
|
if (signal?.aborted) {
|
|
455
513
|
throw new DOMException("Aborted", "AbortError");
|
|
456
514
|
}
|
|
457
515
|
const encodeOptions = createEncodeOptions(options);
|
|
458
516
|
const dataArray = data instanceof Uint8ClampedArray ? new Uint8Array(data.buffer, data.byteOffset, data.length) : new Uint8Array(data.buffer, data.byteOffset, data.length);
|
|
459
|
-
if (typeof console !== "undefined" && console.log) {
|
|
460
|
-
console.log(`[WebP] encode options:`, encodeOptions);
|
|
461
|
-
}
|
|
462
|
-
const t2 = performance.now();
|
|
463
517
|
const result = module.encode(dataArray, width, height, encodeOptions);
|
|
464
|
-
const t3 = performance.now();
|
|
465
|
-
if (typeof console !== "undefined" && console.log) {
|
|
466
|
-
console.log(`[WebP] actual encoding took ${(t3 - t2).toFixed(2)}ms`);
|
|
467
|
-
}
|
|
468
518
|
if (signal?.aborted) {
|
|
469
519
|
throw new DOMException("Aborted", "AbortError");
|
|
470
520
|
}
|
|
@@ -473,49 +523,181 @@ async function webpEncodeClient(image, options, signal) {
|
|
|
473
523
|
}
|
|
474
524
|
return result;
|
|
475
525
|
}
|
|
476
|
-
|
|
526
|
+
async function loadWebPDecModule() {
|
|
527
|
+
if (cachedDecModule)
|
|
528
|
+
return cachedDecModule;
|
|
529
|
+
if (loadDecModulePromise)
|
|
530
|
+
return loadDecModulePromise;
|
|
531
|
+
loadDecModulePromise = (async () => {
|
|
532
|
+
const modulePath = "webp-dec/webp_dec.js";
|
|
533
|
+
try {
|
|
534
|
+
console.log("[WebP Worker] Initializing dec module...");
|
|
535
|
+
console.log(`[WebP Worker] Attempting to import dec module from path: ${modulePath}`);
|
|
536
|
+
const globalSelf = typeof self !== "undefined" ? self : globalThis;
|
|
537
|
+
if (!globalSelf.location) {
|
|
538
|
+
globalSelf.location = {
|
|
539
|
+
href: import.meta.url
|
|
540
|
+
};
|
|
541
|
+
}
|
|
542
|
+
if (typeof self === "undefined" && typeof globalThis !== "undefined") {
|
|
543
|
+
globalThis.self = globalThis;
|
|
544
|
+
}
|
|
545
|
+
if (typeof ImageData === "undefined") {
|
|
546
|
+
globalThis.ImageData = class {
|
|
547
|
+
data;
|
|
548
|
+
width;
|
|
549
|
+
height;
|
|
550
|
+
colorSpace = "srgb";
|
|
551
|
+
constructor(data, width, height) {
|
|
552
|
+
this.data = data;
|
|
553
|
+
this.width = width;
|
|
554
|
+
this.height = height;
|
|
555
|
+
}
|
|
556
|
+
};
|
|
557
|
+
}
|
|
558
|
+
let moduleFactory;
|
|
559
|
+
const isSource = import.meta.url.includes("/src/");
|
|
560
|
+
const pathsToTry = isSource ? ["../wasm/" + modulePath, "./wasm/" + modulePath] : ["./wasm/" + modulePath, "../wasm/" + modulePath];
|
|
561
|
+
let lastError = null;
|
|
562
|
+
for (const importPath of pathsToTry) {
|
|
563
|
+
try {
|
|
564
|
+
moduleFactory = (await import(importPath)).default;
|
|
565
|
+
console.log(`[WebP Worker] Successfully loaded dec module from: ${importPath}`);
|
|
566
|
+
break;
|
|
567
|
+
} catch (error) {
|
|
568
|
+
lastError = error instanceof Error ? error : new Error(String(error));
|
|
569
|
+
console.warn(`[WebP Worker] Failed to load dec from ${importPath}, trying next path...`);
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
if (!moduleFactory) {
|
|
573
|
+
throw lastError || new Error("Could not load WebP dec module from any path");
|
|
574
|
+
}
|
|
575
|
+
console.log("[WebP Worker] Dec module factory loaded successfully.");
|
|
576
|
+
const wasmPathsToTry = isSource ? ["../wasm/webp-dec/webp_dec.wasm", "./wasm/webp-dec/webp_dec.wasm"] : ["./wasm/webp-dec/webp_dec.wasm", "../wasm/webp-dec/webp_dec.wasm"];
|
|
577
|
+
console.log(`[WebP Worker] Preparing to load dec WASM binary. Will try paths: ${wasmPathsToTry.join(", ")}`);
|
|
578
|
+
const initDecModuleWithBinary = async (moduleFactory2, wasmPaths) => {
|
|
579
|
+
const workerBaseUrl = new URL(".", import.meta.url);
|
|
580
|
+
let lastError2 = null;
|
|
581
|
+
for (const wasmPath of wasmPaths) {
|
|
582
|
+
try {
|
|
583
|
+
console.log(`[WebP Worker] Calling loadWasmBinary with dec path: ${wasmPath}`);
|
|
584
|
+
const wasmBinary = await loadWasmBinary(wasmPath, workerBaseUrl);
|
|
585
|
+
console.log(`[WebP Worker] Successfully fetched dec WASM binary from ${wasmPath}. Size: ${wasmBinary.byteLength} bytes.`);
|
|
586
|
+
const globalSelf2 = typeof self !== "undefined" ? self : globalThis;
|
|
587
|
+
if (!globalSelf2.location) {
|
|
588
|
+
globalSelf2.location = {
|
|
589
|
+
href: import.meta.url
|
|
590
|
+
};
|
|
591
|
+
}
|
|
592
|
+
if (typeof self === "undefined" && typeof globalThis !== "undefined") {
|
|
593
|
+
globalThis.self = globalThis;
|
|
594
|
+
}
|
|
595
|
+
return await moduleFactory2({
|
|
596
|
+
noInitialRun: true,
|
|
597
|
+
wasmBinary
|
|
598
|
+
});
|
|
599
|
+
} catch (err) {
|
|
600
|
+
lastError2 = err instanceof Error ? err : new Error(String(err));
|
|
601
|
+
console.warn(`[WebP Worker] Failed to load dec WASM from ${wasmPath}, trying next path...`);
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
throw lastError2 || new Error("Could not load dec WASM binary from any of the attempted paths");
|
|
605
|
+
};
|
|
606
|
+
cachedDecModule = await initDecModuleWithBinary(moduleFactory, wasmPathsToTry);
|
|
607
|
+
console.log("[WebP Worker] WebP dec module initialized successfully.");
|
|
608
|
+
return cachedDecModule;
|
|
609
|
+
} catch (err) {
|
|
610
|
+
console.error("[WebP Worker] CRITICAL: Failed to load WebP dec module", err);
|
|
611
|
+
throw err;
|
|
612
|
+
}
|
|
613
|
+
})().catch((err) => {
|
|
614
|
+
loadDecModulePromise = null;
|
|
615
|
+
throw err;
|
|
616
|
+
});
|
|
617
|
+
return loadDecModulePromise;
|
|
618
|
+
}
|
|
619
|
+
async function webpDecodeClient(data, signal) {
|
|
620
|
+
if (signal?.aborted) {
|
|
621
|
+
throw new DOMException("Aborted", "AbortError");
|
|
622
|
+
}
|
|
623
|
+
const module = await loadWebPDecModule();
|
|
624
|
+
if (signal?.aborted) {
|
|
625
|
+
throw new DOMException("Aborted", "AbortError");
|
|
626
|
+
}
|
|
627
|
+
const result = module.decode(data);
|
|
628
|
+
if (!result) {
|
|
629
|
+
throw new Error("WebP decoding failed");
|
|
630
|
+
}
|
|
631
|
+
return result;
|
|
632
|
+
}
|
|
633
|
+
var cachedModule = null, loadModulePromise = null, cachedDecModule = null, loadDecModulePromise = null;
|
|
477
634
|
var init_webp_worker = __esm(() => {
|
|
478
635
|
init_src();
|
|
479
636
|
if (typeof self !== "undefined") {
|
|
637
|
+
loadWebPModule().catch(() => {});
|
|
480
638
|
self.onmessage = async (event) => {
|
|
481
639
|
const data = event.data;
|
|
482
640
|
if (data?.type === "worker:ping") {
|
|
641
|
+
await loadWebPModule();
|
|
483
642
|
self.postMessage({ type: "worker:ready" });
|
|
484
643
|
return;
|
|
485
644
|
}
|
|
645
|
+
if (data?.type === "webp:encode") {
|
|
646
|
+
const request2 = data;
|
|
647
|
+
const response2 = {
|
|
648
|
+
id: request2.id,
|
|
649
|
+
ok: false
|
|
650
|
+
};
|
|
651
|
+
try {
|
|
652
|
+
const { image, options } = request2.payload;
|
|
653
|
+
const result = await webpEncodeClient(image, options);
|
|
654
|
+
response2.ok = true;
|
|
655
|
+
response2.data = result;
|
|
656
|
+
const transferBuffer = result.buffer instanceof ArrayBuffer ? result.buffer : result.slice().buffer;
|
|
657
|
+
self.postMessage(response2, {
|
|
658
|
+
transfer: [transferBuffer]
|
|
659
|
+
});
|
|
660
|
+
} catch (error) {
|
|
661
|
+
response2.error = error instanceof Error ? error.message : String(error);
|
|
662
|
+
self.postMessage(response2);
|
|
663
|
+
}
|
|
664
|
+
return;
|
|
665
|
+
}
|
|
666
|
+
if (data?.type === "webp:decode") {
|
|
667
|
+
const request2 = data;
|
|
668
|
+
const response2 = {
|
|
669
|
+
id: request2.id,
|
|
670
|
+
ok: false
|
|
671
|
+
};
|
|
672
|
+
try {
|
|
673
|
+
const result = await webpDecodeClient(request2.payload.data);
|
|
674
|
+
response2.ok = true;
|
|
675
|
+
response2.data = result;
|
|
676
|
+
const transferBuffer = result.data.buffer instanceof ArrayBuffer ? result.data.buffer : result.data.slice().buffer;
|
|
677
|
+
self.postMessage(response2, {
|
|
678
|
+
transfer: [transferBuffer]
|
|
679
|
+
});
|
|
680
|
+
} catch (error) {
|
|
681
|
+
response2.error = error instanceof Error ? error.message : String(error);
|
|
682
|
+
self.postMessage(response2);
|
|
683
|
+
}
|
|
684
|
+
return;
|
|
685
|
+
}
|
|
486
686
|
const request = data;
|
|
487
687
|
const response = {
|
|
488
688
|
id: request.id,
|
|
489
|
-
ok: false
|
|
689
|
+
ok: false,
|
|
690
|
+
error: `Unknown message type: ${data?.type}`
|
|
490
691
|
};
|
|
491
|
-
|
|
492
|
-
if (request.type === "webp:encode") {
|
|
493
|
-
const { image, options } = request.payload;
|
|
494
|
-
const t0 = performance.now();
|
|
495
|
-
const controller = new AbortController;
|
|
496
|
-
const result = await webpEncodeClient(image, options, controller.signal);
|
|
497
|
-
const t1 = performance.now();
|
|
498
|
-
if (typeof console !== "undefined" && console.log) {
|
|
499
|
-
console.log(`[WebP Worker] encode took ${(t1 - t0).toFixed(2)}ms`);
|
|
500
|
-
}
|
|
501
|
-
response.ok = true;
|
|
502
|
-
response.data = result;
|
|
503
|
-
self.postMessage(response);
|
|
504
|
-
} else {
|
|
505
|
-
response.error = `Unknown message type: ${request.type}`;
|
|
506
|
-
self.postMessage(response);
|
|
507
|
-
}
|
|
508
|
-
} catch (error) {
|
|
509
|
-
response.error = error instanceof Error ? error.message : String(error);
|
|
510
|
-
self.postMessage(response);
|
|
511
|
-
}
|
|
692
|
+
self.postMessage(response);
|
|
512
693
|
};
|
|
513
694
|
}
|
|
514
695
|
});
|
|
515
696
|
init_webp_worker();
|
|
516
697
|
|
|
517
698
|
export {
|
|
518
|
-
webpEncodeClient
|
|
699
|
+
webpEncodeClient,
|
|
700
|
+
webpDecodeClient
|
|
519
701
|
};
|
|
520
702
|
|
|
521
|
-
//# debugId=
|
|
703
|
+
//# debugId=85802ED1F6452F3E64756E2164756E21
|