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