@squoosh-kit/mozjpeg 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/dist/bridge.d.ts +16 -0
- package/dist/bridge.d.ts.map +1 -0
- package/dist/index.browser.mjs +800 -0
- package/dist/index.browser.mjs.map +20 -0
- package/dist/index.bun.js +801 -0
- package/dist/index.bun.js.map +20 -0
- package/dist/index.d.ts +63 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.node.cjs +828 -0
- package/dist/index.node.cjs.map +20 -0
- package/dist/index.node.mjs +800 -0
- package/dist/index.node.mjs.map +20 -0
- package/dist/mozjpeg.worker.browser.mjs +702 -0
- package/dist/mozjpeg.worker.browser.mjs.map +18 -0
- package/dist/mozjpeg.worker.bun.js +703 -0
- package/dist/mozjpeg.worker.bun.js.map +18 -0
- package/dist/mozjpeg.worker.d.ts +15 -0
- package/dist/mozjpeg.worker.d.ts.map +1 -0
- package/dist/mozjpeg.worker.node.cjs +721 -0
- package/dist/mozjpeg.worker.node.cjs.map +18 -0
- package/dist/mozjpeg.worker.node.mjs +702 -0
- package/dist/mozjpeg.worker.node.mjs.map +18 -0
- package/dist/types.d.ts +7 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/validators.d.ts +2 -0
- package/dist/validators.d.ts.map +1 -0
- package/dist/wasm/mozjpeg-dec/mozjpeg_node_dec.js +16 -0
- package/dist/wasm/mozjpeg-dec/mozjpeg_node_dec.wasm +0 -0
- package/dist/wasm/mozjpeg-enc/mozjpeg_enc.d.ts +37 -0
- package/dist/wasm/mozjpeg-enc/mozjpeg_enc.js +16 -0
- package/dist/wasm/mozjpeg-enc/mozjpeg_enc.wasm +0 -0
- package/dist/wasm/mozjpeg-enc/mozjpeg_node_enc.js +16 -0
- package/dist/wasm/mozjpeg-enc/mozjpeg_node_enc.wasm +0 -0
- package/package.json +51 -0
|
@@ -0,0 +1,828 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
function __accessProp(key) {
|
|
6
|
+
return this[key];
|
|
7
|
+
}
|
|
8
|
+
var __toCommonJS = (from) => {
|
|
9
|
+
var entry = (__moduleCache ??= new WeakMap).get(from), desc;
|
|
10
|
+
if (entry)
|
|
11
|
+
return entry;
|
|
12
|
+
entry = __defProp({}, "__esModule", { value: true });
|
|
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
|
+
}
|
|
21
|
+
__moduleCache.set(from, entry);
|
|
22
|
+
return entry;
|
|
23
|
+
};
|
|
24
|
+
var __moduleCache;
|
|
25
|
+
var __returnValue = (v) => v;
|
|
26
|
+
function __exportSetter(name, newValue) {
|
|
27
|
+
this[name] = __returnValue.bind(null, newValue);
|
|
28
|
+
}
|
|
29
|
+
var __export = (target, all) => {
|
|
30
|
+
for (var name in all)
|
|
31
|
+
__defProp(target, name, {
|
|
32
|
+
get: all[name],
|
|
33
|
+
enumerable: true,
|
|
34
|
+
configurable: true,
|
|
35
|
+
set: __exportSetter.bind(all, name)
|
|
36
|
+
});
|
|
37
|
+
};
|
|
38
|
+
var __esm = (fn, res) => () => (fn && (res = fn(fn = 0)), res);
|
|
39
|
+
|
|
40
|
+
// ../runtime/src/env.ts
|
|
41
|
+
function isBrowser() {
|
|
42
|
+
return typeof window !== "undefined" && typeof document !== "undefined";
|
|
43
|
+
}
|
|
44
|
+
function isBun() {
|
|
45
|
+
return typeof Bun !== "undefined";
|
|
46
|
+
}
|
|
47
|
+
function isNode() {
|
|
48
|
+
return typeof process !== "undefined" && process.versions != null && process.versions.node != null;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// ../runtime/src/worker-call.ts
|
|
52
|
+
async function callWorker(worker, type, payload, signal, transfer) {
|
|
53
|
+
return new Promise((resolve, reject) => {
|
|
54
|
+
const id = ++requestId;
|
|
55
|
+
if (signal?.aborted) {
|
|
56
|
+
reject(new DOMException("Aborted", "AbortError"));
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
const handleMessage = (event) => {
|
|
60
|
+
const response = event.data;
|
|
61
|
+
if (response.id !== id)
|
|
62
|
+
return;
|
|
63
|
+
cleanup();
|
|
64
|
+
if (response.ok && response.data !== undefined) {
|
|
65
|
+
resolve(response.data);
|
|
66
|
+
} else {
|
|
67
|
+
reject(new Error(response.error || "Unknown worker error"));
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
const handleError = (error) => {
|
|
71
|
+
cleanup();
|
|
72
|
+
reject(new Error(`Worker error: ${error.message}`));
|
|
73
|
+
};
|
|
74
|
+
const handleAbort = () => {
|
|
75
|
+
cleanup();
|
|
76
|
+
reject(new DOMException("Aborted", "AbortError"));
|
|
77
|
+
};
|
|
78
|
+
const cleanup = () => {
|
|
79
|
+
worker.removeEventListener("message", handleMessage);
|
|
80
|
+
worker.removeEventListener("error", handleError);
|
|
81
|
+
signal?.removeEventListener("abort", handleAbort);
|
|
82
|
+
};
|
|
83
|
+
worker.addEventListener("message", handleMessage);
|
|
84
|
+
worker.addEventListener("error", handleError);
|
|
85
|
+
signal?.addEventListener("abort", handleAbort);
|
|
86
|
+
const request = { type, id, payload };
|
|
87
|
+
if (transfer && transfer.length > 0) {
|
|
88
|
+
worker.postMessage(request, transfer);
|
|
89
|
+
} else {
|
|
90
|
+
worker.postMessage(request);
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
var requestId = 0;
|
|
95
|
+
|
|
96
|
+
// ../runtime/src/worker-helper.ts
|
|
97
|
+
function createCodecWorker(workerFilename, options) {
|
|
98
|
+
const normalizedName = workerFilename.endsWith(".js") ? workerFilename : `${workerFilename}.js`;
|
|
99
|
+
const workerMap = {
|
|
100
|
+
"resize.worker.js": {
|
|
101
|
+
package: "@squoosh-kit/resize",
|
|
102
|
+
specifier: "resize.worker.js"
|
|
103
|
+
},
|
|
104
|
+
"webp.worker.js": {
|
|
105
|
+
package: "@squoosh-kit/webp",
|
|
106
|
+
specifier: "webp.worker.js"
|
|
107
|
+
},
|
|
108
|
+
"avif.worker.js": {
|
|
109
|
+
package: "@squoosh-kit/avif",
|
|
110
|
+
specifier: "avif.worker.js"
|
|
111
|
+
},
|
|
112
|
+
"mozjpeg.worker.js": {
|
|
113
|
+
package: "@squoosh-kit/mozjpeg",
|
|
114
|
+
specifier: "mozjpeg.worker.js"
|
|
115
|
+
},
|
|
116
|
+
"jxl.worker.js": {
|
|
117
|
+
package: "@squoosh-kit/jxl",
|
|
118
|
+
specifier: "jxl.worker.js"
|
|
119
|
+
},
|
|
120
|
+
"oxipng.worker.js": {
|
|
121
|
+
package: "@squoosh-kit/oxipng",
|
|
122
|
+
specifier: "oxipng.worker.js"
|
|
123
|
+
},
|
|
124
|
+
"png.worker.js": {
|
|
125
|
+
package: "@squoosh-kit/png",
|
|
126
|
+
specifier: "png.worker.js"
|
|
127
|
+
},
|
|
128
|
+
"imagequant.worker.js": {
|
|
129
|
+
package: "@squoosh-kit/imagequant",
|
|
130
|
+
specifier: "imagequant.worker.js"
|
|
131
|
+
},
|
|
132
|
+
"qoi.worker.js": {
|
|
133
|
+
package: "@squoosh-kit/qoi",
|
|
134
|
+
specifier: "qoi.worker.js"
|
|
135
|
+
},
|
|
136
|
+
"wp2.worker.js": {
|
|
137
|
+
package: "@squoosh-kit/wp2",
|
|
138
|
+
specifier: "wp2.worker.js"
|
|
139
|
+
},
|
|
140
|
+
"hqx.worker.js": {
|
|
141
|
+
package: "@squoosh-kit/hqx",
|
|
142
|
+
specifier: "hqx.worker.js"
|
|
143
|
+
},
|
|
144
|
+
"rotate.worker.js": {
|
|
145
|
+
package: "@squoosh-kit/rotate",
|
|
146
|
+
specifier: "rotate.worker.js"
|
|
147
|
+
},
|
|
148
|
+
"visdif.worker.js": {
|
|
149
|
+
package: "@squoosh-kit/visdif",
|
|
150
|
+
specifier: "visdif.worker.js"
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
const workerConfig = workerMap[normalizedName];
|
|
154
|
+
if (!workerConfig) {
|
|
155
|
+
throw new Error(`Unknown worker: ${normalizedName}. ` + `Supported workers: ${Object.keys(workerMap).join(", ")}`);
|
|
156
|
+
}
|
|
157
|
+
try {
|
|
158
|
+
if (typeof window !== "undefined") {
|
|
159
|
+
const packageName = workerConfig.package.split("/")[1];
|
|
160
|
+
const workerFile = normalizedName.replace(".js", ".browser.mjs");
|
|
161
|
+
console.log(`[worker-helper] In browser environment. Trying to create worker:`);
|
|
162
|
+
console.log(`[worker-helper] - Package Name: ${packageName}`);
|
|
163
|
+
console.log(`[worker-helper] - Worker File: ${workerFile}`);
|
|
164
|
+
if (options?.assetPath) {
|
|
165
|
+
let normalizedAssetPath = options.assetPath;
|
|
166
|
+
if (!normalizedAssetPath.startsWith("/")) {
|
|
167
|
+
normalizedAssetPath = "/" + normalizedAssetPath;
|
|
168
|
+
}
|
|
169
|
+
if (normalizedAssetPath.endsWith("/")) {
|
|
170
|
+
normalizedAssetPath = normalizedAssetPath.slice(0, -1);
|
|
171
|
+
}
|
|
172
|
+
const workerPath = `${normalizedAssetPath}/${packageName}/${workerFile}`;
|
|
173
|
+
const workerUrl = new URL(workerPath, window.location.origin).href;
|
|
174
|
+
console.log(`[worker-helper] Using provided assetPath. Full Worker URL: ${workerUrl}`);
|
|
175
|
+
try {
|
|
176
|
+
const worker = new Worker(workerUrl, { type: "module" });
|
|
177
|
+
console.log(`[worker-helper] Successfully created worker with assetPath: ${workerUrl}`);
|
|
178
|
+
return worker;
|
|
179
|
+
} catch (e) {
|
|
180
|
+
console.error(`[worker-helper] Failed to load worker from assetPath URL: ${workerUrl}`, e);
|
|
181
|
+
throw new Error(`Worker failed to load from ${workerUrl}: ${e instanceof Error ? e.message : String(e)}`, { cause: e });
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
const pathStrategies = [
|
|
185
|
+
`../../${packageName}/dist/${workerFile}`,
|
|
186
|
+
`../../../node_modules/@squoosh-kit/${packageName}/dist/${workerFile}`,
|
|
187
|
+
`../../../${packageName}/dist/${workerFile}`
|
|
188
|
+
];
|
|
189
|
+
let lastError = null;
|
|
190
|
+
for (const relPath of pathStrategies) {
|
|
191
|
+
console.log("relPath:", relPath);
|
|
192
|
+
console.log("import.meta.url:", "file:///home/bnowak/repos/squoosh-kit/packages/runtime/src/worker-helper.ts");
|
|
193
|
+
try {
|
|
194
|
+
const workerUrl = new URL(relPath, "file:///home/bnowak/repos/squoosh-kit/packages/runtime/src/worker-helper.ts");
|
|
195
|
+
console.log(`[worker-helper] Trying path strategy. Full Worker URL: ${workerUrl.href}`);
|
|
196
|
+
const worker = new Worker(workerUrl, {
|
|
197
|
+
type: "module"
|
|
198
|
+
});
|
|
199
|
+
console.log(`[worker-helper] Successfully created worker with URL: ${workerUrl.href}`);
|
|
200
|
+
return worker;
|
|
201
|
+
} catch (error) {
|
|
202
|
+
console.warn(`[worker-helper] Path strategy failed for ${relPath}:`, error);
|
|
203
|
+
lastError = error instanceof Error ? error : new Error(String(error));
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
if (lastError) {
|
|
207
|
+
console.error("[worker-helper] All path strategies failed.", lastError);
|
|
208
|
+
throw lastError;
|
|
209
|
+
}
|
|
210
|
+
throw new Error(`Could not resolve worker ${normalizedName} using any available path strategy`);
|
|
211
|
+
}
|
|
212
|
+
const platformExt = isBun() ? ".bun.js" : ".node.mjs";
|
|
213
|
+
const baseName = normalizedName.replace(".js", "");
|
|
214
|
+
const pkgName = workerConfig.package.split("/")[1];
|
|
215
|
+
const srcRelPath = `../../${pkgName}/src/${baseName}.ts`;
|
|
216
|
+
console.log("srcRelPath:", srcRelPath);
|
|
217
|
+
console.log("import.meta.url:", "file:///home/bnowak/repos/squoosh-kit/packages/runtime/src/worker-helper.ts");
|
|
218
|
+
try {
|
|
219
|
+
return new Worker(new URL(srcRelPath, "file:///home/bnowak/repos/squoosh-kit/packages/runtime/src/worker-helper.ts"), {
|
|
220
|
+
type: "module"
|
|
221
|
+
});
|
|
222
|
+
} catch {
|
|
223
|
+
const distRelPath = `../../${pkgName}/dist/${baseName}.${platformExt.slice(1)}`;
|
|
224
|
+
console.log("distRelPath:", distRelPath);
|
|
225
|
+
console.log("import.meta.url:", "file:///home/bnowak/repos/squoosh-kit/packages/runtime/src/worker-helper.ts");
|
|
226
|
+
try {
|
|
227
|
+
return new Worker(new URL(distRelPath, "file:///home/bnowak/repos/squoosh-kit/packages/runtime/src/worker-helper.ts"), {
|
|
228
|
+
type: "module"
|
|
229
|
+
});
|
|
230
|
+
} catch {
|
|
231
|
+
if (typeof import.meta.resolve === "function") {
|
|
232
|
+
try {
|
|
233
|
+
const resolved = import.meta.resolve(`${workerConfig.package}/${workerConfig.specifier}`);
|
|
234
|
+
console.log("resolved:", resolved);
|
|
235
|
+
return new Worker(resolved, { type: "module" });
|
|
236
|
+
} catch {}
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
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.`);
|
|
241
|
+
} catch (error) {
|
|
242
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
243
|
+
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 });
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
function createReadyWorker(workerFilename, options, timeoutMs = 1e4) {
|
|
247
|
+
return new Promise((resolve, reject) => {
|
|
248
|
+
const timeout = setTimeout(() => {
|
|
249
|
+
reject(new Error(`Worker initialization timeout after ${timeoutMs}ms. Worker file: ${workerFilename}`));
|
|
250
|
+
}, timeoutMs);
|
|
251
|
+
let worker;
|
|
252
|
+
try {
|
|
253
|
+
worker = createCodecWorker(workerFilename, options);
|
|
254
|
+
} catch (error) {
|
|
255
|
+
clearTimeout(timeout);
|
|
256
|
+
reject(error);
|
|
257
|
+
return;
|
|
258
|
+
}
|
|
259
|
+
const handleMessage = (event) => {
|
|
260
|
+
if (event.data?.type === "worker:ready") {
|
|
261
|
+
clearTimeout(timeout);
|
|
262
|
+
worker.removeEventListener("message", handleMessage);
|
|
263
|
+
worker.removeEventListener("error", handleError);
|
|
264
|
+
worker.removeEventListener("messageerror", handleMessageError);
|
|
265
|
+
resolve(worker);
|
|
266
|
+
}
|
|
267
|
+
};
|
|
268
|
+
const handleError = (event) => {
|
|
269
|
+
clearTimeout(timeout);
|
|
270
|
+
worker.removeEventListener("message", handleMessage);
|
|
271
|
+
worker.removeEventListener("error", handleError);
|
|
272
|
+
worker.removeEventListener("messageerror", handleMessageError);
|
|
273
|
+
reject(new Error(`Worker failed to start: ${event?.message || "Unknown error"}. Worker file: ${workerFilename}`));
|
|
274
|
+
};
|
|
275
|
+
const handleMessageError = () => {
|
|
276
|
+
clearTimeout(timeout);
|
|
277
|
+
worker.removeEventListener("message", handleMessage);
|
|
278
|
+
worker.removeEventListener("error", handleError);
|
|
279
|
+
worker.removeEventListener("messageerror", handleMessageError);
|
|
280
|
+
reject(new Error(`Worker message error during initialization. Worker file: ${workerFilename}`));
|
|
281
|
+
};
|
|
282
|
+
worker.addEventListener("message", handleMessage);
|
|
283
|
+
worker.addEventListener("error", handleError);
|
|
284
|
+
worker.addEventListener("messageerror", handleMessageError);
|
|
285
|
+
worker.postMessage({ type: "worker:ping" });
|
|
286
|
+
});
|
|
287
|
+
}
|
|
288
|
+
var init_worker_helper = () => {};
|
|
289
|
+
|
|
290
|
+
// ../runtime/src/wasm-loader.ts
|
|
291
|
+
async function loadWasmBinary(relativePath, baseUrlOverride) {
|
|
292
|
+
const baseUrl = baseUrlOverride ? typeof baseUrlOverride === "string" ? new URL(".", baseUrlOverride) : new URL(".", baseUrlOverride.href) : new URL(".", "file:///home/bnowak/repos/squoosh-kit/packages/runtime/src/wasm-loader.ts");
|
|
293
|
+
const fullUrl = new URL(relativePath, baseUrl);
|
|
294
|
+
console.log(`[WasmLoader] Loading WASM from relative path: ${relativePath}`);
|
|
295
|
+
console.log(`[WasmLoader] Base URL (import.meta.url): ${baseUrl.href}`);
|
|
296
|
+
console.log(`[WasmLoader] Constructed full URL: ${fullUrl.href}`);
|
|
297
|
+
try {
|
|
298
|
+
const response = await fetch(fullUrl.href);
|
|
299
|
+
console.log(`[WasmLoader] Fetch response status for ${fullUrl.href}: ${response.status}`);
|
|
300
|
+
if (!response.ok) {
|
|
301
|
+
const responseText = await response.text();
|
|
302
|
+
console.error(`[WasmLoader] Fetch response text (first 500 chars):`, responseText.substring(0, 500));
|
|
303
|
+
throw new Error(`Failed to fetch WASM module at ${fullUrl.href}: ${response.status} ${response.statusText}`);
|
|
304
|
+
}
|
|
305
|
+
const contentType = response.headers.get("content-type");
|
|
306
|
+
console.log(`[WasmLoader] Response Content-Type: ${contentType}`);
|
|
307
|
+
if (!contentType || !contentType.includes("application/wasm")) {
|
|
308
|
+
console.warn(`[WasmLoader] Warning: WASM module at ${fullUrl.href} served with incorrect MIME type: "${contentType}". Should be "application/wasm".`);
|
|
309
|
+
}
|
|
310
|
+
return await response.arrayBuffer();
|
|
311
|
+
} catch (error) {
|
|
312
|
+
console.error(`[WasmLoader] CRITICAL: Fetching WASM binary from ${fullUrl.href} failed.`, error);
|
|
313
|
+
throw error;
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
// ../runtime/src/validators.ts
|
|
318
|
+
function validateImageInput(image) {
|
|
319
|
+
if (!image || typeof image !== "object") {
|
|
320
|
+
throw new TypeError("image must be an object");
|
|
321
|
+
}
|
|
322
|
+
const imageObj = image;
|
|
323
|
+
if (!("data" in imageObj)) {
|
|
324
|
+
throw new TypeError("image.data is required");
|
|
325
|
+
}
|
|
326
|
+
const { data } = imageObj;
|
|
327
|
+
if (!(data instanceof Uint8Array || data instanceof Uint8ClampedArray)) {
|
|
328
|
+
throw new TypeError("image.data must be Uint8Array or Uint8ClampedArray");
|
|
329
|
+
}
|
|
330
|
+
if (!("width" in imageObj) || !("height" in imageObj)) {
|
|
331
|
+
throw new TypeError("image.width and image.height are required");
|
|
332
|
+
}
|
|
333
|
+
const { width, height } = imageObj;
|
|
334
|
+
if (typeof width !== "number" || !Number.isInteger(width) || width <= 0) {
|
|
335
|
+
throw new RangeError(`image.width must be a positive integer, got ${width}`);
|
|
336
|
+
}
|
|
337
|
+
if (typeof height !== "number" || !Number.isInteger(height) || height <= 0) {
|
|
338
|
+
throw new RangeError(`image.height must be a positive integer, got ${height}`);
|
|
339
|
+
}
|
|
340
|
+
const expectedSize = width * height * 4;
|
|
341
|
+
if (data.length < expectedSize) {
|
|
342
|
+
throw new RangeError(`image.data too small: ${data.length} bytes, expected at least ${expectedSize} bytes for ${width}x${height} RGBA image`);
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
// ../runtime/src/simd-detector.ts
|
|
346
|
+
var init_simd_detector = () => {};
|
|
347
|
+
|
|
348
|
+
// ../runtime/src/image-data-polyfill.ts
|
|
349
|
+
function polyfillImageData() {
|
|
350
|
+
if (typeof ImageData === "undefined") {
|
|
351
|
+
globalThis.ImageData = class {
|
|
352
|
+
data;
|
|
353
|
+
width;
|
|
354
|
+
height;
|
|
355
|
+
colorSpace = "srgb";
|
|
356
|
+
constructor(data, width, height) {
|
|
357
|
+
this.data = data;
|
|
358
|
+
this.width = width;
|
|
359
|
+
this.height = height;
|
|
360
|
+
}
|
|
361
|
+
};
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
// ../runtime/src/index.ts
|
|
366
|
+
var init_src = __esm(() => {
|
|
367
|
+
init_worker_helper();
|
|
368
|
+
init_simd_detector();
|
|
369
|
+
});
|
|
370
|
+
|
|
371
|
+
// src/validators.ts
|
|
372
|
+
function validateMozjpegOptions(options) {
|
|
373
|
+
if (options === undefined) {
|
|
374
|
+
return;
|
|
375
|
+
}
|
|
376
|
+
if (typeof options !== "object" || options === null) {
|
|
377
|
+
throw new TypeError("options must be an object or undefined");
|
|
378
|
+
}
|
|
379
|
+
const opts = options;
|
|
380
|
+
if ("quality" in opts && opts.quality !== undefined) {
|
|
381
|
+
const quality = opts.quality;
|
|
382
|
+
if (Number.isNaN(quality)) {
|
|
383
|
+
throw new RangeError("quality must be a valid number in the range 0-100, got NaN");
|
|
384
|
+
}
|
|
385
|
+
if (typeof quality !== "number") {
|
|
386
|
+
throw new TypeError("quality must be a number");
|
|
387
|
+
}
|
|
388
|
+
if (!Number.isFinite(quality) || !Number.isInteger(quality)) {
|
|
389
|
+
throw new RangeError("quality must be an integer in the range 0-100, got floating point");
|
|
390
|
+
}
|
|
391
|
+
if (quality < 0 || quality > 100) {
|
|
392
|
+
throw new RangeError(`quality must be in the range 0-100, got ${quality}`);
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
if ("smoothing" in opts && opts.smoothing !== undefined) {
|
|
396
|
+
const smoothing = opts.smoothing;
|
|
397
|
+
if (Number.isNaN(smoothing)) {
|
|
398
|
+
throw new RangeError("smoothing must be a valid number in the range 0-100, got NaN");
|
|
399
|
+
}
|
|
400
|
+
if (typeof smoothing !== "number") {
|
|
401
|
+
throw new TypeError("smoothing must be a number");
|
|
402
|
+
}
|
|
403
|
+
if (!Number.isFinite(smoothing) || !Number.isInteger(smoothing)) {
|
|
404
|
+
throw new RangeError("smoothing must be an integer in the range 0-100, got floating point");
|
|
405
|
+
}
|
|
406
|
+
if (smoothing < 0 || smoothing > 100) {
|
|
407
|
+
throw new RangeError(`smoothing must be in the range 0-100, got ${smoothing}`);
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
// src/mozjpeg.worker.ts
|
|
413
|
+
var exports_mozjpeg_worker = {};
|
|
414
|
+
__export(exports_mozjpeg_worker, {
|
|
415
|
+
mozjpegEncodeClient: () => mozjpegEncodeClient,
|
|
416
|
+
mozjpegDecodeClient: () => mozjpegDecodeClient
|
|
417
|
+
});
|
|
418
|
+
module.exports = __toCommonJS(exports_mozjpeg_worker);
|
|
419
|
+
async function loadMozjpegModule() {
|
|
420
|
+
if (cachedEncModule)
|
|
421
|
+
return cachedEncModule;
|
|
422
|
+
if (loadEncModulePromise)
|
|
423
|
+
return loadEncModulePromise;
|
|
424
|
+
loadEncModulePromise = (async () => {
|
|
425
|
+
const globalSelf = typeof self !== "undefined" ? self : globalThis;
|
|
426
|
+
if (!globalSelf.location) {
|
|
427
|
+
globalSelf.location = {
|
|
428
|
+
href: "file:///home/bnowak/repos/squoosh-kit/packages/mozjpeg/src/mozjpeg.worker.ts"
|
|
429
|
+
};
|
|
430
|
+
}
|
|
431
|
+
if (typeof self === "undefined" && typeof globalThis !== "undefined") {
|
|
432
|
+
globalThis.self = globalThis;
|
|
433
|
+
}
|
|
434
|
+
const useNode = isBun() || isNode();
|
|
435
|
+
const modulePath = useNode ? "mozjpeg-enc/mozjpeg_node_enc.js" : "mozjpeg-enc/mozjpeg_enc.js";
|
|
436
|
+
try {
|
|
437
|
+
console.log("[MozJPEG Worker] Initializing encoder. Node/Bun:", useNode);
|
|
438
|
+
console.log(`[MozJPEG Worker] Attempting to import encoder module from path: ${modulePath}`);
|
|
439
|
+
let moduleFactory;
|
|
440
|
+
const isSource = "file:///home/bnowak/repos/squoosh-kit/packages/mozjpeg/src/mozjpeg.worker.ts".includes("/src/");
|
|
441
|
+
const pathsToTry = isSource ? ["../wasm/" + modulePath, "./wasm/" + modulePath] : ["./wasm/" + modulePath, "../wasm/" + modulePath];
|
|
442
|
+
let lastError = null;
|
|
443
|
+
for (const importPath of pathsToTry) {
|
|
444
|
+
try {
|
|
445
|
+
moduleFactory = (await import(importPath)).default;
|
|
446
|
+
console.log(`[MozJPEG Worker] Successfully loaded encoder module from: ${importPath}`);
|
|
447
|
+
break;
|
|
448
|
+
} catch (error) {
|
|
449
|
+
lastError = error instanceof Error ? error : new Error(String(error));
|
|
450
|
+
console.warn(`[MozJPEG Worker] Failed to load encoder from ${importPath}, trying next path...`);
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
if (!moduleFactory) {
|
|
454
|
+
throw lastError || new Error("Could not load MozJPEG encoder module from any path");
|
|
455
|
+
}
|
|
456
|
+
console.log("[MozJPEG Worker] Encoder module factory loaded successfully.");
|
|
457
|
+
const wasmFileName = useNode ? "mozjpeg_node_enc.wasm" : "mozjpeg_enc.wasm";
|
|
458
|
+
const wasmPathsToTry = isSource ? [
|
|
459
|
+
`../wasm/mozjpeg-enc/${wasmFileName}`,
|
|
460
|
+
`./wasm/mozjpeg-enc/${wasmFileName}`
|
|
461
|
+
] : [
|
|
462
|
+
`./wasm/mozjpeg-enc/${wasmFileName}`,
|
|
463
|
+
`../wasm/mozjpeg-enc/${wasmFileName}`
|
|
464
|
+
];
|
|
465
|
+
console.log(`[MozJPEG Worker] Preparing to load encoder WASM binary. Will try paths: ${wasmPathsToTry.join(", ")}`);
|
|
466
|
+
const initModuleWithBinary = async (factory, wasmPaths) => {
|
|
467
|
+
const workerBaseUrl = new URL(".", "file:///home/bnowak/repos/squoosh-kit/packages/mozjpeg/src/mozjpeg.worker.ts");
|
|
468
|
+
let lastErr = null;
|
|
469
|
+
for (const wasmPath of wasmPaths) {
|
|
470
|
+
try {
|
|
471
|
+
console.log(`[MozJPEG Worker] Calling loadWasmBinary with path: ${wasmPath}`);
|
|
472
|
+
const wasmBinary = await loadWasmBinary(wasmPath, workerBaseUrl);
|
|
473
|
+
console.log(`[MozJPEG Worker] Successfully fetched encoder WASM binary from ${wasmPath}. Size: ${wasmBinary.byteLength} bytes.`);
|
|
474
|
+
const globalSelf2 = typeof self !== "undefined" ? self : globalThis;
|
|
475
|
+
if (!globalSelf2.location) {
|
|
476
|
+
globalSelf2.location = {
|
|
477
|
+
href: "file:///home/bnowak/repos/squoosh-kit/packages/mozjpeg/src/mozjpeg.worker.ts"
|
|
478
|
+
};
|
|
479
|
+
}
|
|
480
|
+
if (typeof self === "undefined" && typeof globalThis !== "undefined") {
|
|
481
|
+
globalThis.self = globalThis;
|
|
482
|
+
}
|
|
483
|
+
return await factory({
|
|
484
|
+
noInitialRun: true,
|
|
485
|
+
wasmBinary
|
|
486
|
+
});
|
|
487
|
+
} catch (err) {
|
|
488
|
+
lastErr = err instanceof Error ? err : new Error(String(err));
|
|
489
|
+
console.warn(`[MozJPEG Worker] Failed to load encoder WASM from ${wasmPath}, trying next path...`);
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
throw lastErr || new Error("Could not load encoder WASM binary from any of the attempted paths");
|
|
493
|
+
};
|
|
494
|
+
cachedEncModule = await initModuleWithBinary(moduleFactory, wasmPathsToTry);
|
|
495
|
+
console.log("[MozJPEG Worker] MozJPEG encoder module initialized successfully.");
|
|
496
|
+
return cachedEncModule;
|
|
497
|
+
} catch (err) {
|
|
498
|
+
console.error(`[MozJPEG Worker] CRITICAL: Failed to load MozJPEG encoder module from path: ${modulePath}`, err);
|
|
499
|
+
throw err;
|
|
500
|
+
}
|
|
501
|
+
})().catch((err) => {
|
|
502
|
+
loadEncModulePromise = null;
|
|
503
|
+
throw err;
|
|
504
|
+
});
|
|
505
|
+
return loadEncModulePromise;
|
|
506
|
+
}
|
|
507
|
+
async function loadMozjpegNodeDecModule() {
|
|
508
|
+
if (cachedDecModule)
|
|
509
|
+
return cachedDecModule;
|
|
510
|
+
if (loadDecModulePromise)
|
|
511
|
+
return loadDecModulePromise;
|
|
512
|
+
loadDecModulePromise = (async () => {
|
|
513
|
+
const globalSelf = typeof self !== "undefined" ? self : globalThis;
|
|
514
|
+
if (!globalSelf.location) {
|
|
515
|
+
globalSelf.location = {
|
|
516
|
+
href: "file:///home/bnowak/repos/squoosh-kit/packages/mozjpeg/src/mozjpeg.worker.ts"
|
|
517
|
+
};
|
|
518
|
+
}
|
|
519
|
+
if (typeof self === "undefined" && typeof globalThis !== "undefined") {
|
|
520
|
+
globalThis.self = globalThis;
|
|
521
|
+
}
|
|
522
|
+
polyfillImageData();
|
|
523
|
+
const modulePath = "mozjpeg-dec/mozjpeg_node_dec.js";
|
|
524
|
+
try {
|
|
525
|
+
console.log("[MozJPEG Worker] Initializing node decoder...");
|
|
526
|
+
console.log(`[MozJPEG Worker] Attempting to import decoder module from path: ${modulePath}`);
|
|
527
|
+
let moduleFactory;
|
|
528
|
+
const isSource = "file:///home/bnowak/repos/squoosh-kit/packages/mozjpeg/src/mozjpeg.worker.ts".includes("/src/");
|
|
529
|
+
const pathsToTry = isSource ? ["../wasm/" + modulePath, "./wasm/" + modulePath] : ["./wasm/" + modulePath, "../wasm/" + modulePath];
|
|
530
|
+
let lastError = null;
|
|
531
|
+
for (const importPath of pathsToTry) {
|
|
532
|
+
try {
|
|
533
|
+
moduleFactory = (await import(importPath)).default;
|
|
534
|
+
console.log(`[MozJPEG Worker] Successfully loaded decoder module from: ${importPath}`);
|
|
535
|
+
break;
|
|
536
|
+
} catch (error) {
|
|
537
|
+
lastError = error instanceof Error ? error : new Error(String(error));
|
|
538
|
+
console.warn(`[MozJPEG Worker] Failed to load decoder from ${importPath}, trying next path...`);
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
if (!moduleFactory) {
|
|
542
|
+
throw lastError || new Error("Could not load MozJPEG decoder module from any path");
|
|
543
|
+
}
|
|
544
|
+
console.log("[MozJPEG Worker] Decoder module factory loaded successfully.");
|
|
545
|
+
const wasmPathsToTry = isSource ? [
|
|
546
|
+
"../wasm/mozjpeg-dec/mozjpeg_node_dec.wasm",
|
|
547
|
+
"./wasm/mozjpeg-dec/mozjpeg_node_dec.wasm"
|
|
548
|
+
] : [
|
|
549
|
+
"./wasm/mozjpeg-dec/mozjpeg_node_dec.wasm",
|
|
550
|
+
"../wasm/mozjpeg-dec/mozjpeg_node_dec.wasm"
|
|
551
|
+
];
|
|
552
|
+
console.log(`[MozJPEG Worker] Preparing to load decoder WASM binary. Will try paths: ${wasmPathsToTry.join(", ")}`);
|
|
553
|
+
const initDecModuleWithBinary = async (factory, wasmPaths) => {
|
|
554
|
+
const workerBaseUrl = new URL(".", "file:///home/bnowak/repos/squoosh-kit/packages/mozjpeg/src/mozjpeg.worker.ts");
|
|
555
|
+
let lastErr = null;
|
|
556
|
+
for (const wasmPath of wasmPaths) {
|
|
557
|
+
try {
|
|
558
|
+
console.log(`[MozJPEG Worker] Calling loadWasmBinary with path: ${wasmPath}`);
|
|
559
|
+
const wasmBinary = await loadWasmBinary(wasmPath, workerBaseUrl);
|
|
560
|
+
console.log(`[MozJPEG Worker] Successfully fetched decoder WASM binary from ${wasmPath}. Size: ${wasmBinary.byteLength} bytes.`);
|
|
561
|
+
const globalSelf2 = typeof self !== "undefined" ? self : globalThis;
|
|
562
|
+
if (!globalSelf2.location) {
|
|
563
|
+
globalSelf2.location = {
|
|
564
|
+
href: "file:///home/bnowak/repos/squoosh-kit/packages/mozjpeg/src/mozjpeg.worker.ts"
|
|
565
|
+
};
|
|
566
|
+
}
|
|
567
|
+
if (typeof self === "undefined" && typeof globalThis !== "undefined") {
|
|
568
|
+
globalThis.self = globalThis;
|
|
569
|
+
}
|
|
570
|
+
return await factory({
|
|
571
|
+
noInitialRun: true,
|
|
572
|
+
wasmBinary
|
|
573
|
+
});
|
|
574
|
+
} catch (err) {
|
|
575
|
+
lastErr = err instanceof Error ? err : new Error(String(err));
|
|
576
|
+
console.warn(`[MozJPEG Worker] Failed to load decoder WASM from ${wasmPath}, trying next path...`);
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
throw lastErr || new Error("Could not load decoder WASM binary from any of the attempted paths");
|
|
580
|
+
};
|
|
581
|
+
cachedDecModule = await initDecModuleWithBinary(moduleFactory, wasmPathsToTry);
|
|
582
|
+
console.log("[MozJPEG Worker] MozJPEG decoder module initialized successfully.");
|
|
583
|
+
return cachedDecModule;
|
|
584
|
+
} catch (err) {
|
|
585
|
+
console.error("[MozJPEG Worker] CRITICAL: Failed to load MozJPEG decoder module", err);
|
|
586
|
+
throw err;
|
|
587
|
+
}
|
|
588
|
+
})().catch((err) => {
|
|
589
|
+
loadDecModulePromise = null;
|
|
590
|
+
throw err;
|
|
591
|
+
});
|
|
592
|
+
return loadDecModulePromise;
|
|
593
|
+
}
|
|
594
|
+
function createEncodeOptions(options) {
|
|
595
|
+
return {
|
|
596
|
+
quality: options?.quality ?? 75,
|
|
597
|
+
baseline: options?.baseline ?? false,
|
|
598
|
+
arithmetic: options?.arithmetic ?? false,
|
|
599
|
+
progressive: options?.progressive ?? true,
|
|
600
|
+
optimize_coding: options?.optimize_coding ?? true,
|
|
601
|
+
smoothing: options?.smoothing ?? 0,
|
|
602
|
+
color_space: options?.color_space ?? MozJpegColorSpace.YCbCr,
|
|
603
|
+
quant_table: options?.quant_table ?? 3,
|
|
604
|
+
trellis_multipass: options?.trellis_multipass ?? false,
|
|
605
|
+
trellis_opt_zero: options?.trellis_opt_zero ?? false,
|
|
606
|
+
trellis_opt_table: options?.trellis_opt_table ?? false,
|
|
607
|
+
trellis_loops: options?.trellis_loops ?? 1,
|
|
608
|
+
auto_subsample: options?.auto_subsample ?? true,
|
|
609
|
+
chroma_subsample: options?.chroma_subsample ?? 2,
|
|
610
|
+
separate_chroma_quality: options?.separate_chroma_quality ?? false,
|
|
611
|
+
chroma_quality: options?.chroma_quality ?? 75
|
|
612
|
+
};
|
|
613
|
+
}
|
|
614
|
+
async function mozjpegEncodeClient(image, options, signal) {
|
|
615
|
+
validateImageInput(image);
|
|
616
|
+
validateMozjpegOptions(options);
|
|
617
|
+
if (signal?.aborted) {
|
|
618
|
+
throw new DOMException("Aborted", "AbortError");
|
|
619
|
+
}
|
|
620
|
+
const width = image.width;
|
|
621
|
+
const height = image.height;
|
|
622
|
+
const data = image.data;
|
|
623
|
+
if (!(data instanceof Uint8Array) && !(data instanceof Uint8ClampedArray)) {
|
|
624
|
+
throw new Error("Image data must be Uint8Array or Uint8ClampedArray");
|
|
625
|
+
}
|
|
626
|
+
const module2 = await loadMozjpegModule();
|
|
627
|
+
if (signal?.aborted) {
|
|
628
|
+
throw new DOMException("Aborted", "AbortError");
|
|
629
|
+
}
|
|
630
|
+
const encodeOptions = createEncodeOptions(options);
|
|
631
|
+
const dataArray = data instanceof Uint8ClampedArray ? new Uint8Array(data.buffer, data.byteOffset, data.length) : new Uint8Array(data.buffer, data.byteOffset, data.length);
|
|
632
|
+
const result = module2.encode(dataArray, width, height, encodeOptions);
|
|
633
|
+
if (signal?.aborted) {
|
|
634
|
+
throw new DOMException("Aborted", "AbortError");
|
|
635
|
+
}
|
|
636
|
+
if (!result) {
|
|
637
|
+
throw new Error("MozJPEG encoding failed");
|
|
638
|
+
}
|
|
639
|
+
return result;
|
|
640
|
+
}
|
|
641
|
+
async function mozjpegDecodeClient(data, signal) {
|
|
642
|
+
if (isBrowser()) {
|
|
643
|
+
throw new Error("MozJPEG decoding is not supported in browser environments. No browser decoder is available.");
|
|
644
|
+
}
|
|
645
|
+
if (signal?.aborted) {
|
|
646
|
+
throw new DOMException("Aborted", "AbortError");
|
|
647
|
+
}
|
|
648
|
+
const module2 = await loadMozjpegNodeDecModule();
|
|
649
|
+
if (signal?.aborted) {
|
|
650
|
+
throw new DOMException("Aborted", "AbortError");
|
|
651
|
+
}
|
|
652
|
+
const result = module2.decode(data);
|
|
653
|
+
if (!result) {
|
|
654
|
+
throw new Error("MozJPEG decoding failed");
|
|
655
|
+
}
|
|
656
|
+
return result;
|
|
657
|
+
}
|
|
658
|
+
var MozJpegColorSpace, cachedEncModule = null, cachedDecModule = null, loadEncModulePromise = null, loadDecModulePromise = null;
|
|
659
|
+
var init_mozjpeg_worker = __esm(() => {
|
|
660
|
+
init_src();
|
|
661
|
+
MozJpegColorSpace = { GRAYSCALE: 1, RGB: 2, YCbCr: 3 };
|
|
662
|
+
if (typeof self !== "undefined") {
|
|
663
|
+
self.onmessage = async (event) => {
|
|
664
|
+
const data = event.data;
|
|
665
|
+
if (data?.type === "worker:ping") {
|
|
666
|
+
await loadMozjpegModule();
|
|
667
|
+
self.postMessage({ type: "worker:ready" });
|
|
668
|
+
return;
|
|
669
|
+
}
|
|
670
|
+
if (data?.type === "mozjpeg:encode") {
|
|
671
|
+
const request2 = data;
|
|
672
|
+
const response2 = {
|
|
673
|
+
id: request2.id,
|
|
674
|
+
ok: false
|
|
675
|
+
};
|
|
676
|
+
try {
|
|
677
|
+
const { image, options } = request2.payload;
|
|
678
|
+
const result = await mozjpegEncodeClient(image, options);
|
|
679
|
+
response2.ok = true;
|
|
680
|
+
response2.data = result;
|
|
681
|
+
self.postMessage(response2, {
|
|
682
|
+
transfer: [result.buffer]
|
|
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 === "mozjpeg:decode") {
|
|
691
|
+
const request2 = data;
|
|
692
|
+
const response2 = {
|
|
693
|
+
id: request2.id,
|
|
694
|
+
ok: false
|
|
695
|
+
};
|
|
696
|
+
try {
|
|
697
|
+
const result = await mozjpegDecodeClient(request2.payload.data);
|
|
698
|
+
response2.ok = true;
|
|
699
|
+
response2.data = result;
|
|
700
|
+
self.postMessage(response2, {
|
|
701
|
+
transfer: [result.data.buffer]
|
|
702
|
+
});
|
|
703
|
+
} catch (error) {
|
|
704
|
+
response2.error = error instanceof Error ? error.message : String(error);
|
|
705
|
+
self.postMessage(response2);
|
|
706
|
+
}
|
|
707
|
+
return;
|
|
708
|
+
}
|
|
709
|
+
const request = data;
|
|
710
|
+
const response = {
|
|
711
|
+
id: request.id,
|
|
712
|
+
ok: false,
|
|
713
|
+
error: `Unknown message type: ${data?.type}`
|
|
714
|
+
};
|
|
715
|
+
self.postMessage(response);
|
|
716
|
+
};
|
|
717
|
+
}
|
|
718
|
+
});
|
|
719
|
+
|
|
720
|
+
// src/index.ts
|
|
721
|
+
var exports_src = {};
|
|
722
|
+
__export(exports_src, {
|
|
723
|
+
encode: () => encode,
|
|
724
|
+
decode: () => decode,
|
|
725
|
+
createMozjpegEncoder: () => createMozjpegEncoder,
|
|
726
|
+
createMozjpegDecoder: () => createMozjpegDecoder
|
|
727
|
+
});
|
|
728
|
+
module.exports = __toCommonJS(exports_src);
|
|
729
|
+
|
|
730
|
+
// src/bridge.ts
|
|
731
|
+
init_src();
|
|
732
|
+
init_src();
|
|
733
|
+
|
|
734
|
+
class MozjpegClientBridge {
|
|
735
|
+
async encode(image, options, signal) {
|
|
736
|
+
const module2 = await Promise.resolve().then(() => (init_mozjpeg_worker(), exports_mozjpeg_worker));
|
|
737
|
+
const mozjpegEncodeClient2 = module2.mozjpegEncodeClient;
|
|
738
|
+
return mozjpegEncodeClient2(image, options, signal);
|
|
739
|
+
}
|
|
740
|
+
async decode(data, signal) {
|
|
741
|
+
const module2 = await Promise.resolve().then(() => (init_mozjpeg_worker(), exports_mozjpeg_worker));
|
|
742
|
+
const mozjpegDecodeClient2 = module2.mozjpegDecodeClient;
|
|
743
|
+
return mozjpegDecodeClient2(data, signal);
|
|
744
|
+
}
|
|
745
|
+
async terminate() {}
|
|
746
|
+
}
|
|
747
|
+
|
|
748
|
+
class MozjpegWorkerBridge {
|
|
749
|
+
worker = null;
|
|
750
|
+
workerReady = null;
|
|
751
|
+
options;
|
|
752
|
+
constructor(options) {
|
|
753
|
+
console.log("[mozjpeg/bridge] MozjpegWorkerBridge constructor called with options:", options);
|
|
754
|
+
this.options = options;
|
|
755
|
+
}
|
|
756
|
+
async getWorker() {
|
|
757
|
+
if (!this.workerReady) {
|
|
758
|
+
this.workerReady = this.createWorker();
|
|
759
|
+
}
|
|
760
|
+
return this.workerReady;
|
|
761
|
+
}
|
|
762
|
+
async createWorker() {
|
|
763
|
+
console.log("[mozjpeg/bridge] createWorker called. Creating ready worker...");
|
|
764
|
+
this.worker = await createReadyWorker("mozjpeg.worker.js", this.options);
|
|
765
|
+
console.log("[mozjpeg/bridge] createWorker: Ready worker created successfully.");
|
|
766
|
+
return this.worker;
|
|
767
|
+
}
|
|
768
|
+
async encode(image, options, signal) {
|
|
769
|
+
const worker = await this.getWorker();
|
|
770
|
+
validateImageInput(image);
|
|
771
|
+
return callWorker(worker, "mozjpeg:encode", { image, options }, signal);
|
|
772
|
+
}
|
|
773
|
+
async decode(data, signal) {
|
|
774
|
+
const worker = await this.getWorker();
|
|
775
|
+
return callWorker(worker, "mozjpeg:decode", { data }, signal);
|
|
776
|
+
}
|
|
777
|
+
async terminate() {
|
|
778
|
+
if (this.worker) {
|
|
779
|
+
this.worker.terminate();
|
|
780
|
+
this.worker = null;
|
|
781
|
+
this.workerReady = null;
|
|
782
|
+
}
|
|
783
|
+
}
|
|
784
|
+
}
|
|
785
|
+
function createBridge(mode, options) {
|
|
786
|
+
console.log(`[mozjpeg/bridge] createBridge called with mode: ${mode}`);
|
|
787
|
+
if (mode === "worker") {
|
|
788
|
+
return new MozjpegWorkerBridge(options);
|
|
789
|
+
}
|
|
790
|
+
return new MozjpegClientBridge;
|
|
791
|
+
}
|
|
792
|
+
|
|
793
|
+
// src/index.ts
|
|
794
|
+
var globalBridge = null;
|
|
795
|
+
async function encode(imageData, options, signal) {
|
|
796
|
+
if (!globalBridge) {
|
|
797
|
+
globalBridge = createBridge("worker");
|
|
798
|
+
}
|
|
799
|
+
return globalBridge.encode(imageData, options, signal);
|
|
800
|
+
}
|
|
801
|
+
async function decode(data, signal) {
|
|
802
|
+
if (!globalBridge) {
|
|
803
|
+
globalBridge = createBridge("worker");
|
|
804
|
+
}
|
|
805
|
+
return globalBridge.decode(data, signal);
|
|
806
|
+
}
|
|
807
|
+
function createMozjpegEncoder(mode = "worker", options) {
|
|
808
|
+
const bridge = createBridge(mode, options);
|
|
809
|
+
return Object.assign((imageData, encodeOptions, signal) => {
|
|
810
|
+
return bridge.encode(imageData, encodeOptions, signal);
|
|
811
|
+
}, {
|
|
812
|
+
terminate: async () => {
|
|
813
|
+
await bridge.terminate();
|
|
814
|
+
}
|
|
815
|
+
});
|
|
816
|
+
}
|
|
817
|
+
function createMozjpegDecoder(mode = "worker", options) {
|
|
818
|
+
const bridge = createBridge(mode, options);
|
|
819
|
+
return Object.assign((data, signal) => {
|
|
820
|
+
return bridge.decode(data, signal);
|
|
821
|
+
}, {
|
|
822
|
+
terminate: async () => {
|
|
823
|
+
await bridge.terminate();
|
|
824
|
+
}
|
|
825
|
+
});
|
|
826
|
+
}
|
|
827
|
+
|
|
828
|
+
//# debugId=C50C7A2F36D6D0F164756E2164756E21
|