@squoosh-kit/webp 0.0.41 → 0.0.43
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 +0 -34
- package/dist/bridge.d.ts +4 -1
- package/dist/bridge.d.ts.map +1 -1
- package/dist/index.browser.mjs +575 -2
- package/dist/index.browser.mjs.map +15 -4
- package/dist/index.bun.js +575 -2
- package/dist/index.bun.js.map +15 -4
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.node.cjs +597 -2
- package/dist/index.node.cjs.map +15 -4
- package/dist/index.node.mjs +575 -2
- package/dist/index.node.mjs.map +15 -4
- package/dist/webp.worker.browser.mjs +503 -26
- package/dist/webp.worker.browser.mjs.map +11 -6
- package/dist/webp.worker.bun.js +503 -26
- package/dist/webp.worker.bun.js.map +11 -6
- package/dist/webp.worker.d.ts.map +1 -1
- package/dist/webp.worker.node.cjs +517 -26
- package/dist/webp.worker.node.cjs.map +11 -6
- package/dist/webp.worker.node.mjs +503 -26
- package/dist/webp.worker.node.mjs.map +11 -6
- package/package.json +2 -2
- package/dist/bridge.browser.mjs +0 -4
- package/dist/bridge.browser.mjs.map +0 -13
- package/dist/bridge.bun.js +0 -5
- package/dist/bridge.bun.js.map +0 -13
- package/dist/bridge.node.cjs +0 -3
- package/dist/bridge.node.cjs.map +0 -13
- package/dist/bridge.node.mjs +0 -4
- package/dist/bridge.node.mjs.map +0 -13
- package/dist/chunk-534zkza4.js +0 -27
- package/dist/chunk-534zkza4.js.map +0 -10
- package/dist/chunk-5vdxd0v3.js +0 -4
- package/dist/chunk-5vdxd0v3.js.map +0 -10
- package/dist/chunk-8y42hv65.js +0 -5
- package/dist/chunk-8y42hv65.js.map +0 -9
- package/dist/chunk-bhj61bkd.js +0 -4
- package/dist/chunk-bhj61bkd.js.map +0 -9
- package/dist/chunk-c6cx0d7q.js +0 -4
- package/dist/chunk-c6cx0d7q.js.map +0 -9
- package/dist/chunk-cxqwba64.js +0 -28
- package/dist/chunk-cxqwba64.js.map +0 -10
- package/dist/chunk-djaabg7r.js +0 -3
- package/dist/chunk-djaabg7r.js.map +0 -9
- package/dist/chunk-gw29pvyv.js +0 -3
- package/dist/chunk-gw29pvyv.js.map +0 -10
- package/dist/chunk-q0apy7nk.js +0 -5
- package/dist/chunk-q0apy7nk.js.map +0 -10
- package/dist/chunk-sbhzt3r6.js +0 -27
- package/dist/chunk-sbhzt3r6.js.map +0 -10
- package/dist/chunk-sc3b6r8w.js +0 -27
- package/dist/chunk-sc3b6r8w.js.map +0 -10
- package/dist/chunk-szbj3b6y.js +0 -4
- package/dist/chunk-szbj3b6y.js.map +0 -10
- package/dist/types.browser.mjs +0 -2
- package/dist/types.browser.mjs.map +0 -9
- package/dist/types.bun.js +0 -3
- package/dist/types.bun.js.map +0 -9
- package/dist/types.node.cjs +0 -3
- package/dist/types.node.cjs.map +0 -9
- package/dist/types.node.mjs +0 -2
- package/dist/types.node.mjs.map +0 -9
- package/dist/validators.browser.mjs +0 -4
- package/dist/validators.browser.mjs.map +0 -10
- package/dist/validators.bun.js +0 -5
- package/dist/validators.bun.js.map +0 -10
- package/dist/validators.node.cjs +0 -3
- package/dist/validators.node.cjs.map +0 -10
- package/dist/validators.node.mjs +0 -4
- package/dist/validators.node.mjs.map +0 -10
package/dist/index.bun.js
CHANGED
|
@@ -1,4 +1,577 @@
|
|
|
1
1
|
// @bun
|
|
2
|
-
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __export = (target, all) => {
|
|
4
|
+
for (var name in all)
|
|
5
|
+
__defProp(target, name, {
|
|
6
|
+
get: all[name],
|
|
7
|
+
enumerable: true,
|
|
8
|
+
configurable: true,
|
|
9
|
+
set: (newValue) => all[name] = () => newValue
|
|
10
|
+
});
|
|
11
|
+
};
|
|
12
|
+
var __esm = (fn, res) => () => (fn && (res = fn(fn = 0)), res);
|
|
3
13
|
|
|
4
|
-
|
|
14
|
+
// ../runtime/src/env.ts
|
|
15
|
+
function isBun() {
|
|
16
|
+
return typeof Bun !== "undefined";
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// ../runtime/src/worker-call.ts
|
|
20
|
+
async function callWorker(worker, type, payload, signal, transfer) {
|
|
21
|
+
return new Promise((resolve, reject) => {
|
|
22
|
+
const id = ++requestId;
|
|
23
|
+
if (signal?.aborted) {
|
|
24
|
+
reject(new DOMException("Aborted", "AbortError"));
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
const handleMessage = (event) => {
|
|
28
|
+
const response = event.data;
|
|
29
|
+
if (response.id !== id)
|
|
30
|
+
return;
|
|
31
|
+
cleanup();
|
|
32
|
+
if (response.ok && response.data !== undefined) {
|
|
33
|
+
resolve(response.data);
|
|
34
|
+
} else {
|
|
35
|
+
reject(new Error(response.error || "Unknown worker error"));
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
const handleError = (error) => {
|
|
39
|
+
cleanup();
|
|
40
|
+
reject(new Error(`Worker error: ${error.message}`));
|
|
41
|
+
};
|
|
42
|
+
const handleAbort = () => {
|
|
43
|
+
cleanup();
|
|
44
|
+
reject(new DOMException("Aborted", "AbortError"));
|
|
45
|
+
};
|
|
46
|
+
const cleanup = () => {
|
|
47
|
+
worker.removeEventListener("message", handleMessage);
|
|
48
|
+
worker.removeEventListener("error", handleError);
|
|
49
|
+
signal?.removeEventListener("abort", handleAbort);
|
|
50
|
+
};
|
|
51
|
+
worker.addEventListener("message", handleMessage);
|
|
52
|
+
worker.addEventListener("error", handleError);
|
|
53
|
+
signal?.addEventListener("abort", handleAbort);
|
|
54
|
+
const request = { type, id, payload };
|
|
55
|
+
if (transfer && transfer.length > 0) {
|
|
56
|
+
worker.postMessage(request, transfer);
|
|
57
|
+
} else {
|
|
58
|
+
worker.postMessage(request);
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
var requestId = 0;
|
|
63
|
+
|
|
64
|
+
// ../runtime/src/worker-helper.ts
|
|
65
|
+
function createCodecWorker(workerFilename, options) {
|
|
66
|
+
const normalizedName = workerFilename.endsWith(".js") ? workerFilename : `${workerFilename}.js`;
|
|
67
|
+
const workerMap = {
|
|
68
|
+
"resize.worker.js": {
|
|
69
|
+
package: "@squoosh-kit/resize",
|
|
70
|
+
specifier: "resize.worker.js"
|
|
71
|
+
},
|
|
72
|
+
"webp.worker.js": {
|
|
73
|
+
package: "@squoosh-kit/webp",
|
|
74
|
+
specifier: "webp.worker.js"
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
const workerConfig = workerMap[normalizedName];
|
|
78
|
+
if (!workerConfig) {
|
|
79
|
+
throw new Error(`Unknown worker: ${normalizedName}. ` + `Supported workers: ${Object.keys(workerMap).join(", ")}`);
|
|
80
|
+
}
|
|
81
|
+
try {
|
|
82
|
+
if (typeof window !== "undefined") {
|
|
83
|
+
const packageName = workerConfig.package.split("/")[1];
|
|
84
|
+
const workerFile = normalizedName.replace(".js", ".browser.mjs");
|
|
85
|
+
console.log(`[worker-helper] In browser environment. Trying to create worker:`);
|
|
86
|
+
console.log(`[worker-helper] - Package Name: ${packageName}`);
|
|
87
|
+
console.log(`[worker-helper] - Worker File: ${workerFile}`);
|
|
88
|
+
if (options?.assetPath) {
|
|
89
|
+
const workerUrl = `${window.location.origin}${options.assetPath}${packageName}/${workerFile}`;
|
|
90
|
+
console.log(`[worker-helper] Using provided assetPath. Full Worker URL: ${workerUrl}`);
|
|
91
|
+
try {
|
|
92
|
+
return new Worker(workerUrl, { type: "module" });
|
|
93
|
+
} catch (e) {
|
|
94
|
+
console.error(`[worker-helper] CRITICAL: new Worker() failed synchronously for URL: ${workerUrl}`, e);
|
|
95
|
+
throw e;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
const pathStrategies = [
|
|
99
|
+
`../../${packageName}/dist/${workerFile}`,
|
|
100
|
+
`../../../node_modules/@squoosh-kit/${packageName}/dist/${workerFile}`,
|
|
101
|
+
`../../../${packageName}/dist/${workerFile}`
|
|
102
|
+
];
|
|
103
|
+
let lastError = null;
|
|
104
|
+
for (const relPath of pathStrategies) {
|
|
105
|
+
try {
|
|
106
|
+
const workerUrl = new URL(relPath, import.meta.url);
|
|
107
|
+
console.log(`[worker-helper] Trying path strategy. Full Worker URL: ${workerUrl.href}`);
|
|
108
|
+
const worker = new Worker(workerUrl, {
|
|
109
|
+
type: "module"
|
|
110
|
+
});
|
|
111
|
+
console.log(`[worker-helper] Successfully created worker with URL: ${workerUrl.href}`);
|
|
112
|
+
return worker;
|
|
113
|
+
} catch (error) {
|
|
114
|
+
console.warn(`[worker-helper] Path strategy failed for ${relPath}:`, error);
|
|
115
|
+
lastError = error instanceof Error ? error : new Error(String(error));
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
if (lastError) {
|
|
119
|
+
console.error("[worker-helper] All path strategies failed.", lastError);
|
|
120
|
+
throw lastError;
|
|
121
|
+
}
|
|
122
|
+
throw new Error(`Could not resolve worker ${normalizedName} using any available path strategy`);
|
|
123
|
+
}
|
|
124
|
+
const platformExt = isBun() ? ".bun.js" : ".node.mjs";
|
|
125
|
+
const baseName = normalizedName.replace(".js", "");
|
|
126
|
+
const srcRelPath = workerConfig.package.includes("resize") ? `../../resize/src/${baseName}.ts` : `../../webp/src/${baseName}.ts`;
|
|
127
|
+
try {
|
|
128
|
+
return new Worker(new URL(srcRelPath, import.meta.url), {
|
|
129
|
+
type: "module"
|
|
130
|
+
});
|
|
131
|
+
} catch {
|
|
132
|
+
const distRelPath = workerConfig.package.includes("resize") ? `../../resize/dist/${baseName}.${platformExt.slice(1)}` : `../../webp/dist/${baseName}.${platformExt.slice(1)}`;
|
|
133
|
+
try {
|
|
134
|
+
return new Worker(new URL(distRelPath, import.meta.url), {
|
|
135
|
+
type: "module"
|
|
136
|
+
});
|
|
137
|
+
} catch {
|
|
138
|
+
if (typeof import.meta.resolve === "function") {
|
|
139
|
+
try {
|
|
140
|
+
const resolved = import.meta.resolve(`${workerConfig.package}/${workerConfig.specifier}`);
|
|
141
|
+
return new Worker(resolved, { type: "module" });
|
|
142
|
+
} catch {}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
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.`);
|
|
147
|
+
} catch (error) {
|
|
148
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
149
|
+
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.`);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
function createReadyWorker(workerFilename, options, timeoutMs = 1e4) {
|
|
153
|
+
return new Promise((resolve, reject) => {
|
|
154
|
+
const timeout = setTimeout(() => {
|
|
155
|
+
reject(new Error(`Worker initialization timeout after ${timeoutMs}ms. Worker file: ${workerFilename}`));
|
|
156
|
+
}, timeoutMs);
|
|
157
|
+
let worker;
|
|
158
|
+
try {
|
|
159
|
+
worker = createCodecWorker(workerFilename, options);
|
|
160
|
+
} catch (error) {
|
|
161
|
+
clearTimeout(timeout);
|
|
162
|
+
reject(error);
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
const handleMessage = (event) => {
|
|
166
|
+
if (event.data?.type === "worker:ready") {
|
|
167
|
+
clearTimeout(timeout);
|
|
168
|
+
worker.removeEventListener("message", handleMessage);
|
|
169
|
+
worker.removeEventListener("error", handleError);
|
|
170
|
+
worker.removeEventListener("messageerror", handleMessageError);
|
|
171
|
+
resolve(worker);
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
const handleError = (event) => {
|
|
175
|
+
clearTimeout(timeout);
|
|
176
|
+
worker.removeEventListener("message", handleMessage);
|
|
177
|
+
worker.removeEventListener("error", handleError);
|
|
178
|
+
worker.removeEventListener("messageerror", handleMessageError);
|
|
179
|
+
reject(new Error(`Worker failed to start: ${event?.message || "Unknown error"}. Worker file: ${workerFilename}`));
|
|
180
|
+
};
|
|
181
|
+
const handleMessageError = () => {
|
|
182
|
+
clearTimeout(timeout);
|
|
183
|
+
worker.removeEventListener("message", handleMessage);
|
|
184
|
+
worker.removeEventListener("error", handleError);
|
|
185
|
+
worker.removeEventListener("messageerror", handleMessageError);
|
|
186
|
+
reject(new Error(`Worker message error during initialization. Worker file: ${workerFilename}`));
|
|
187
|
+
};
|
|
188
|
+
worker.addEventListener("message", handleMessage);
|
|
189
|
+
worker.addEventListener("error", handleError);
|
|
190
|
+
worker.addEventListener("messageerror", handleMessageError);
|
|
191
|
+
worker.postMessage({ type: "worker:ping" });
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
var init_worker_helper = () => {};
|
|
195
|
+
|
|
196
|
+
// ../runtime/src/wasm-loader.ts
|
|
197
|
+
async function loadWasmBinary(relativePath, baseUrlOverride) {
|
|
198
|
+
const baseUrl = baseUrlOverride ? typeof baseUrlOverride === "string" ? new URL(".", baseUrlOverride) : new URL(".", baseUrlOverride.href) : new URL(".", import.meta.url);
|
|
199
|
+
const fullUrl = new URL(relativePath, baseUrl);
|
|
200
|
+
console.log(`[WasmLoader] Loading WASM from relative path: ${relativePath}`);
|
|
201
|
+
console.log(`[WasmLoader] Base URL (import.meta.url): ${baseUrl.href}`);
|
|
202
|
+
console.log(`[WasmLoader] Constructed full URL: ${fullUrl.href}`);
|
|
203
|
+
try {
|
|
204
|
+
const response = await fetch(fullUrl.href);
|
|
205
|
+
console.log(`[WasmLoader] Fetch response status for ${fullUrl.href}: ${response.status}`);
|
|
206
|
+
if (!response.ok) {
|
|
207
|
+
const responseText = await response.text();
|
|
208
|
+
console.error(`[WasmLoader] Fetch response text (first 500 chars):`, responseText.substring(0, 500));
|
|
209
|
+
throw new Error(`Failed to fetch WASM module at ${fullUrl.href}: ${response.status} ${response.statusText}`);
|
|
210
|
+
}
|
|
211
|
+
const contentType = response.headers.get("content-type");
|
|
212
|
+
console.log(`[WasmLoader] Response Content-Type: ${contentType}`);
|
|
213
|
+
if (!contentType || !contentType.includes("application/wasm")) {
|
|
214
|
+
console.warn(`[WasmLoader] Warning: WASM module at ${fullUrl.href} served with incorrect MIME type: "${contentType}". Should be "application/wasm".`);
|
|
215
|
+
}
|
|
216
|
+
return await response.arrayBuffer();
|
|
217
|
+
} catch (error) {
|
|
218
|
+
console.error(`[WasmLoader] CRITICAL: Fetching WASM binary from ${fullUrl.href} failed.`, error);
|
|
219
|
+
throw error;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// ../runtime/src/validators.ts
|
|
224
|
+
function validateImageInput(image) {
|
|
225
|
+
if (!image || typeof image !== "object") {
|
|
226
|
+
throw new TypeError("image must be an object");
|
|
227
|
+
}
|
|
228
|
+
const imageObj = image;
|
|
229
|
+
if (!("data" in imageObj)) {
|
|
230
|
+
throw new TypeError("image.data is required");
|
|
231
|
+
}
|
|
232
|
+
const { data } = imageObj;
|
|
233
|
+
if (!(data instanceof Uint8Array || data instanceof Uint8ClampedArray)) {
|
|
234
|
+
throw new TypeError("image.data must be Uint8Array or Uint8ClampedArray");
|
|
235
|
+
}
|
|
236
|
+
if (!("width" in imageObj) || !("height" in imageObj)) {
|
|
237
|
+
throw new TypeError("image.width and image.height are required");
|
|
238
|
+
}
|
|
239
|
+
const { width, height } = imageObj;
|
|
240
|
+
if (typeof width !== "number" || !Number.isInteger(width) || width <= 0) {
|
|
241
|
+
throw new RangeError(`image.width must be a positive integer, got ${width}`);
|
|
242
|
+
}
|
|
243
|
+
if (typeof height !== "number" || !Number.isInteger(height) || height <= 0) {
|
|
244
|
+
throw new RangeError(`image.height must be a positive integer, got ${height}`);
|
|
245
|
+
}
|
|
246
|
+
const expectedSize = width * height * 4;
|
|
247
|
+
if (data.length < expectedSize) {
|
|
248
|
+
throw new RangeError(`image.data too small: ${data.length} bytes, expected at least ${expectedSize} bytes for ${width}x${height} RGBA image`);
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
// ../../node_modules/.bun/wasm-feature-detect@1.8.0/node_modules/wasm-feature-detect/dist/esm/index.js
|
|
253
|
+
var simd = async () => WebAssembly.validate(new Uint8Array([0, 97, 115, 109, 1, 0, 0, 0, 1, 5, 1, 96, 0, 1, 123, 3, 2, 1, 0, 10, 10, 1, 8, 0, 65, 0, 253, 15, 253, 98, 11]));
|
|
254
|
+
|
|
255
|
+
// ../runtime/src/simd-detector.ts
|
|
256
|
+
async function detectSimd() {
|
|
257
|
+
if (simdSupported !== null) {
|
|
258
|
+
return simdSupported;
|
|
259
|
+
}
|
|
260
|
+
if (simdDetectionPromise) {
|
|
261
|
+
return simdDetectionPromise;
|
|
262
|
+
}
|
|
263
|
+
simdDetectionPromise = (async () => {
|
|
264
|
+
try {
|
|
265
|
+
simdSupported = await simd();
|
|
266
|
+
return simdSupported;
|
|
267
|
+
} catch (error) {
|
|
268
|
+
simdSupported = false;
|
|
269
|
+
console.warn("SIMD detection failed, falling back to standard WASM modules:", error);
|
|
270
|
+
return false;
|
|
271
|
+
}
|
|
272
|
+
})();
|
|
273
|
+
return simdDetectionPromise;
|
|
274
|
+
}
|
|
275
|
+
var simdSupported = null, simdDetectionPromise = null;
|
|
276
|
+
var init_simd_detector = () => {};
|
|
277
|
+
|
|
278
|
+
// ../runtime/src/index.ts
|
|
279
|
+
var init_src = __esm(() => {
|
|
280
|
+
init_worker_helper();
|
|
281
|
+
init_simd_detector();
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
// src/validators.ts
|
|
285
|
+
function validateWebpOptions(options) {
|
|
286
|
+
if (options === undefined) {
|
|
287
|
+
return;
|
|
288
|
+
}
|
|
289
|
+
if (typeof options !== "object" || options === null) {
|
|
290
|
+
throw new TypeError("options must be an object or undefined");
|
|
291
|
+
}
|
|
292
|
+
const opts = options;
|
|
293
|
+
if ("quality" in opts && opts.quality !== undefined) {
|
|
294
|
+
const quality = opts.quality;
|
|
295
|
+
if (Number.isNaN(quality)) {
|
|
296
|
+
throw new RangeError("quality must be a valid number in the range 0-100, got NaN");
|
|
297
|
+
}
|
|
298
|
+
if (typeof quality !== "number") {
|
|
299
|
+
throw new TypeError("quality must be a number");
|
|
300
|
+
}
|
|
301
|
+
if (!Number.isInteger(quality)) {
|
|
302
|
+
throw new RangeError("quality must be an integer in the range 0-100, got floating point");
|
|
303
|
+
}
|
|
304
|
+
if (quality < 0 || quality > 100) {
|
|
305
|
+
throw new RangeError(`quality must be in the range 0-100, got ${quality}`);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
// src/webp.worker.ts
|
|
311
|
+
var exports_webp_worker = {};
|
|
312
|
+
__export(exports_webp_worker, {
|
|
313
|
+
webpEncodeClient: () => webpEncodeClient
|
|
314
|
+
});
|
|
315
|
+
async function loadWebPModule() {
|
|
316
|
+
if (cachedModule) {
|
|
317
|
+
return cachedModule;
|
|
318
|
+
}
|
|
319
|
+
const simdSupported2 = await detectSimd();
|
|
320
|
+
const modulePath = simdSupported2 ? "webp/webp_enc_simd.js" : "webp/webp_enc.js";
|
|
321
|
+
try {
|
|
322
|
+
console.log("[WebP Worker] Initializing. SIMD support:", simdSupported2);
|
|
323
|
+
console.log(`[WebP Worker] Attempting to import module from path: ${modulePath}`);
|
|
324
|
+
const globalSelf = typeof self !== "undefined" ? self : globalThis;
|
|
325
|
+
if (!globalSelf.location) {
|
|
326
|
+
globalSelf.location = {
|
|
327
|
+
href: import.meta.url
|
|
328
|
+
};
|
|
329
|
+
}
|
|
330
|
+
if (typeof self === "undefined" && typeof globalThis !== "undefined") {
|
|
331
|
+
globalThis.self = globalThis;
|
|
332
|
+
}
|
|
333
|
+
let moduleFactory;
|
|
334
|
+
const pathsToTry = ["./wasm/" + modulePath, "../wasm/" + modulePath];
|
|
335
|
+
let lastError = null;
|
|
336
|
+
for (const importPath of pathsToTry) {
|
|
337
|
+
try {
|
|
338
|
+
moduleFactory = (await import(importPath)).default;
|
|
339
|
+
console.log(`[WebP Worker] Successfully loaded module from: ${importPath}`);
|
|
340
|
+
break;
|
|
341
|
+
} catch (error) {
|
|
342
|
+
lastError = error instanceof Error ? error : new Error(String(error));
|
|
343
|
+
console.warn(`[WebP Worker] Failed to load from ${importPath}, trying next path...`);
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
if (!moduleFactory) {
|
|
347
|
+
throw lastError || new Error("Could not load WebP module from any path");
|
|
348
|
+
}
|
|
349
|
+
console.log("[WebP Worker] Module factory loaded successfully.");
|
|
350
|
+
const wasmPathsToTry = simdSupported2 ? ["./wasm/webp/webp_enc_simd.wasm", "../wasm/webp/webp_enc_simd.wasm"] : ["./wasm/webp/webp_enc.wasm", "../wasm/webp/webp_enc.wasm"];
|
|
351
|
+
console.log(`[WebP Worker] Preparing to load WASM binary. Will try paths: ${wasmPathsToTry.join(", ")}`);
|
|
352
|
+
const initModuleWithBinary = async (moduleFactory2, wasmPaths) => {
|
|
353
|
+
const workerBaseUrl = new URL(".", import.meta.url);
|
|
354
|
+
let lastError2 = null;
|
|
355
|
+
for (const wasmPath of wasmPaths) {
|
|
356
|
+
try {
|
|
357
|
+
console.log(`[WebP Worker] Calling loadWasmBinary with path: ${wasmPath}`);
|
|
358
|
+
const wasmBinary = await loadWasmBinary(wasmPath, workerBaseUrl);
|
|
359
|
+
console.log(`[WebP Worker] Successfully fetched WASM binary from ${wasmPath}. Size: ${wasmBinary.byteLength} bytes.`);
|
|
360
|
+
const globalSelf2 = typeof self !== "undefined" ? self : globalThis;
|
|
361
|
+
if (!globalSelf2.location) {
|
|
362
|
+
globalSelf2.location = {
|
|
363
|
+
href: import.meta.url
|
|
364
|
+
};
|
|
365
|
+
}
|
|
366
|
+
if (typeof self === "undefined" && typeof globalThis !== "undefined") {
|
|
367
|
+
globalThis.self = globalThis;
|
|
368
|
+
}
|
|
369
|
+
return await moduleFactory2({
|
|
370
|
+
noInitialRun: true,
|
|
371
|
+
wasmBinary
|
|
372
|
+
});
|
|
373
|
+
} catch (err) {
|
|
374
|
+
lastError2 = err instanceof Error ? err : new Error(String(err));
|
|
375
|
+
console.warn(`[WebP Worker] Failed to load WASM from ${wasmPath}, trying next path...`);
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
throw lastError2 || new Error("Could not load WASM binary from any of the attempted paths");
|
|
379
|
+
};
|
|
380
|
+
cachedModule = await initModuleWithBinary(moduleFactory, wasmPathsToTry);
|
|
381
|
+
console.log("[WebP Worker] WebP module initialized successfully.");
|
|
382
|
+
return cachedModule;
|
|
383
|
+
} catch (err) {
|
|
384
|
+
console.error(`[WebP Worker] CRITICAL: Failed to load WebP module from path: ${modulePath}`, err);
|
|
385
|
+
throw err;
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
function createEncodeOptions(options) {
|
|
389
|
+
return {
|
|
390
|
+
quality: options?.quality ?? 75,
|
|
391
|
+
target_size: 0,
|
|
392
|
+
target_PSNR: 0,
|
|
393
|
+
method: options?.method ?? 4,
|
|
394
|
+
sns_strength: options?.sns_strength ?? 50,
|
|
395
|
+
filter_strength: options?.filter_strength ?? 60,
|
|
396
|
+
filter_sharpness: options?.filter_sharpness ?? 0,
|
|
397
|
+
filter_type: options?.filter_type ?? 1,
|
|
398
|
+
partitions: options?.partitions ?? 0,
|
|
399
|
+
segments: options?.segments ?? 4,
|
|
400
|
+
pass: options?.pass ?? 1,
|
|
401
|
+
show_compressed: 0,
|
|
402
|
+
preprocessing: options?.preprocessing ?? 0,
|
|
403
|
+
autofilter: options?.autofilter ?? 0,
|
|
404
|
+
partition_limit: 0,
|
|
405
|
+
alpha_compression: options?.alpha_compression ?? 1,
|
|
406
|
+
alpha_filtering: options?.alpha_filtering ?? 1,
|
|
407
|
+
alpha_quality: options?.alpha_quality ?? options?.quality ?? 75,
|
|
408
|
+
lossless: options?.lossless ? 1 : 0,
|
|
409
|
+
exact: options?.exact ?? 0,
|
|
410
|
+
image_hint: options?.image_hint ?? 0,
|
|
411
|
+
emulate_jpeg_size: 0,
|
|
412
|
+
thread_level: 0,
|
|
413
|
+
low_memory: 0,
|
|
414
|
+
near_lossless: options?.near_lossless ?? options?.quality ?? 75,
|
|
415
|
+
use_delta_palette: 0,
|
|
416
|
+
use_sharp_yuv: options?.use_sharp_yuv ?? 0
|
|
417
|
+
};
|
|
418
|
+
}
|
|
419
|
+
async function webpEncodeClient(image, options, signal) {
|
|
420
|
+
validateImageInput(image);
|
|
421
|
+
validateWebpOptions(options);
|
|
422
|
+
if (signal?.aborted) {
|
|
423
|
+
throw new DOMException("Aborted", "AbortError");
|
|
424
|
+
}
|
|
425
|
+
const width = image.width;
|
|
426
|
+
const height = image.height;
|
|
427
|
+
const data = image.data;
|
|
428
|
+
if (!(data instanceof Uint8Array) && !(data instanceof Uint8ClampedArray)) {
|
|
429
|
+
throw new Error("Image data must be Uint8Array or Uint8ClampedArray");
|
|
430
|
+
}
|
|
431
|
+
const t0 = performance.now();
|
|
432
|
+
const module = await loadWebPModule();
|
|
433
|
+
const t1 = performance.now();
|
|
434
|
+
if (typeof console !== "undefined" && console.log) {
|
|
435
|
+
console.log(`[WebP] module loading took ${(t1 - t0).toFixed(2)}ms`);
|
|
436
|
+
console.log(`[WebP] Module type: ${cachedModule && "encode" in cachedModule ? "Ready" : "Unknown"}`);
|
|
437
|
+
}
|
|
438
|
+
if (signal?.aborted) {
|
|
439
|
+
throw new DOMException("Aborted", "AbortError");
|
|
440
|
+
}
|
|
441
|
+
const encodeOptions = createEncodeOptions(options);
|
|
442
|
+
const dataArray = data instanceof Uint8ClampedArray ? new Uint8Array(data.buffer, data.byteOffset, data.length) : new Uint8Array(data.buffer, data.byteOffset, data.length);
|
|
443
|
+
if (typeof console !== "undefined" && console.log) {
|
|
444
|
+
console.log(`[WebP] encode options:`, encodeOptions);
|
|
445
|
+
}
|
|
446
|
+
const t2 = performance.now();
|
|
447
|
+
const result = module.encode(dataArray, width, height, encodeOptions);
|
|
448
|
+
const t3 = performance.now();
|
|
449
|
+
if (typeof console !== "undefined" && console.log) {
|
|
450
|
+
console.log(`[WebP] actual encoding took ${(t3 - t2).toFixed(2)}ms`);
|
|
451
|
+
}
|
|
452
|
+
if (signal?.aborted) {
|
|
453
|
+
throw new DOMException("Aborted", "AbortError");
|
|
454
|
+
}
|
|
455
|
+
if (!result) {
|
|
456
|
+
throw new Error("WebP encoding failed");
|
|
457
|
+
}
|
|
458
|
+
return result;
|
|
459
|
+
}
|
|
460
|
+
var cachedModule = null;
|
|
461
|
+
var init_webp_worker = __esm(() => {
|
|
462
|
+
init_src();
|
|
463
|
+
if (typeof self !== "undefined") {
|
|
464
|
+
self.onmessage = async (event) => {
|
|
465
|
+
const data = event.data;
|
|
466
|
+
if (data?.type === "worker:ping") {
|
|
467
|
+
self.postMessage({ type: "worker:ready" });
|
|
468
|
+
return;
|
|
469
|
+
}
|
|
470
|
+
const request = data;
|
|
471
|
+
const response = {
|
|
472
|
+
id: request.id,
|
|
473
|
+
ok: false
|
|
474
|
+
};
|
|
475
|
+
try {
|
|
476
|
+
if (request.type === "webp:encode") {
|
|
477
|
+
const { image, options } = request.payload;
|
|
478
|
+
const t0 = performance.now();
|
|
479
|
+
const controller = new AbortController;
|
|
480
|
+
const result = await webpEncodeClient(image, options, controller.signal);
|
|
481
|
+
const t1 = performance.now();
|
|
482
|
+
if (typeof console !== "undefined" && console.log) {
|
|
483
|
+
console.log(`[WebP Worker] encode took ${(t1 - t0).toFixed(2)}ms`);
|
|
484
|
+
}
|
|
485
|
+
response.ok = true;
|
|
486
|
+
response.data = result;
|
|
487
|
+
self.postMessage(response);
|
|
488
|
+
} else {
|
|
489
|
+
response.error = `Unknown message type: ${request.type}`;
|
|
490
|
+
self.postMessage(response);
|
|
491
|
+
}
|
|
492
|
+
} catch (error) {
|
|
493
|
+
response.error = error instanceof Error ? error.message : String(error);
|
|
494
|
+
self.postMessage(response);
|
|
495
|
+
}
|
|
496
|
+
};
|
|
497
|
+
}
|
|
498
|
+
});
|
|
499
|
+
|
|
500
|
+
// src/bridge.ts
|
|
501
|
+
init_src();
|
|
502
|
+
init_src();
|
|
503
|
+
|
|
504
|
+
class WebpClientBridge {
|
|
505
|
+
async encode(image, options, signal) {
|
|
506
|
+
const module = await Promise.resolve().then(() => (init_webp_worker(), exports_webp_worker));
|
|
507
|
+
const webpEncodeClient2 = module.webpEncodeClient;
|
|
508
|
+
return webpEncodeClient2(image, options, signal);
|
|
509
|
+
}
|
|
510
|
+
async terminate() {}
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
class WebpWorkerBridge {
|
|
514
|
+
worker = null;
|
|
515
|
+
workerReady = null;
|
|
516
|
+
options;
|
|
517
|
+
constructor(options) {
|
|
518
|
+
console.log("[webp/bridge] WebpWorkerBridge constructor called with options:", options);
|
|
519
|
+
this.options = options;
|
|
520
|
+
}
|
|
521
|
+
async getWorker() {
|
|
522
|
+
if (!this.workerReady) {
|
|
523
|
+
this.workerReady = this.createWorker();
|
|
524
|
+
}
|
|
525
|
+
return this.workerReady;
|
|
526
|
+
}
|
|
527
|
+
async createWorker() {
|
|
528
|
+
console.log("[webp/bridge] createWorker called. Creating ready worker...");
|
|
529
|
+
this.worker = await createReadyWorker("webp.worker.js", this.options);
|
|
530
|
+
console.log("[webp/bridge] createWorker: Ready worker created successfully.");
|
|
531
|
+
return this.worker;
|
|
532
|
+
}
|
|
533
|
+
async encode(image, options, signal) {
|
|
534
|
+
const worker = await this.getWorker();
|
|
535
|
+
validateImageInput(image);
|
|
536
|
+
return callWorker(worker, "webp:encode", { image, options }, signal);
|
|
537
|
+
}
|
|
538
|
+
async terminate() {
|
|
539
|
+
if (this.worker) {
|
|
540
|
+
this.worker.terminate();
|
|
541
|
+
this.worker = null;
|
|
542
|
+
this.workerReady = null;
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
function createBridge(mode, options) {
|
|
547
|
+
console.log(`[webp/bridge] createBridge called with mode: ${mode}`);
|
|
548
|
+
if (mode === "worker") {
|
|
549
|
+
return new WebpWorkerBridge(options);
|
|
550
|
+
}
|
|
551
|
+
return new WebpClientBridge;
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
// src/index.ts
|
|
555
|
+
var globalClientBridge = null;
|
|
556
|
+
async function encode(imageData, options, signal) {
|
|
557
|
+
if (!globalClientBridge) {
|
|
558
|
+
globalClientBridge = createBridge("worker");
|
|
559
|
+
}
|
|
560
|
+
return globalClientBridge.encode(imageData, options, signal);
|
|
561
|
+
}
|
|
562
|
+
function createWebpEncoder(mode = "worker", options) {
|
|
563
|
+
const bridge = createBridge(mode, options);
|
|
564
|
+
return Object.assign((imageData, options2, signal) => {
|
|
565
|
+
return bridge.encode(imageData, options2, signal);
|
|
566
|
+
}, {
|
|
567
|
+
terminate: async () => {
|
|
568
|
+
await bridge.terminate();
|
|
569
|
+
}
|
|
570
|
+
});
|
|
571
|
+
}
|
|
572
|
+
export {
|
|
573
|
+
encode,
|
|
574
|
+
createWebpEncoder
|
|
575
|
+
};
|
|
576
|
+
|
|
577
|
+
//# debugId=775534E2BAE731C164756E2164756E21
|