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