@squoosh-kit/resize 0.0.42 → 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/dist/bridge.d.ts +4 -1
- package/dist/bridge.d.ts.map +1 -1
- package/dist/index.browser.mjs +577 -2
- package/dist/index.browser.mjs.map +14 -4
- package/dist/index.bun.js +577 -2
- package/dist/index.bun.js.map +14 -4
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.node.cjs +599 -2
- package/dist/index.node.cjs.map +14 -4
- package/dist/index.node.mjs +577 -2
- package/dist/index.node.mjs.map +14 -4
- package/dist/resize.worker.browser.mjs +503 -2
- package/dist/resize.worker.browser.mjs.map +10 -4
- package/dist/resize.worker.bun.js +503 -2
- package/dist/resize.worker.bun.js.map +10 -4
- package/dist/resize.worker.node.cjs +517 -2
- package/dist/resize.worker.node.cjs.map +10 -4
- package/dist/resize.worker.node.mjs +503 -2
- package/dist/resize.worker.node.mjs.map +10 -4
- 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-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-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-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,579 @@
|
|
|
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
|
+
// ../runtime/src/simd-detector.ts
|
|
252
|
+
var init_simd_detector = () => {};
|
|
253
|
+
|
|
254
|
+
// ../runtime/src/index.ts
|
|
255
|
+
var init_src = __esm(() => {
|
|
256
|
+
init_worker_helper();
|
|
257
|
+
init_simd_detector();
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
// src/validators.ts
|
|
261
|
+
function validateResizeOptions(options) {
|
|
262
|
+
if (typeof options !== "object" || options === null) {
|
|
263
|
+
throw new TypeError("options must be an object");
|
|
264
|
+
}
|
|
265
|
+
const opts = options;
|
|
266
|
+
if (opts.width !== undefined) {
|
|
267
|
+
if (typeof opts.width !== "number" || !Number.isInteger(opts.width) || opts.width <= 0) {
|
|
268
|
+
throw new RangeError(`options.width must be a positive integer, got ${opts.width}`);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
if (opts.height !== undefined) {
|
|
272
|
+
if (typeof opts.height !== "number" || !Number.isInteger(opts.height) || opts.height <= 0) {
|
|
273
|
+
throw new RangeError(`options.height must be a positive integer, got ${opts.height}`);
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
if (opts.method !== undefined) {
|
|
277
|
+
const validMethods = ["triangular", "catrom", "mitchell", "lanczos3"];
|
|
278
|
+
if (!validMethods.includes(opts.method)) {
|
|
279
|
+
throw new TypeError(`options.method must be one of: ${validMethods.join(", ")}, got ${opts.method}`);
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
if (opts.premultiply !== undefined && typeof opts.premultiply !== "boolean") {
|
|
283
|
+
throw new TypeError(`options.premultiply must be boolean, got ${typeof opts.premultiply}`);
|
|
284
|
+
}
|
|
285
|
+
if (opts.linearRGB !== undefined && typeof opts.linearRGB !== "boolean") {
|
|
286
|
+
throw new TypeError(`options.linearRGB must be boolean, got ${typeof opts.linearRGB}`);
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
// wasm/squoosh_resize.js
|
|
291
|
+
function getUint8Memory0() {
|
|
292
|
+
if (cachegetUint8Memory0 === null || cachegetUint8Memory0.buffer !== wasm.memory.buffer) {
|
|
293
|
+
cachegetUint8Memory0 = new Uint8Array(wasm.memory.buffer);
|
|
294
|
+
}
|
|
295
|
+
return cachegetUint8Memory0;
|
|
296
|
+
}
|
|
297
|
+
function passArray8ToWasm0(arg, malloc) {
|
|
298
|
+
const ptr = malloc(arg.length * 1);
|
|
299
|
+
getUint8Memory0().set(arg, ptr / 1);
|
|
300
|
+
WASM_VECTOR_LEN = arg.length;
|
|
301
|
+
return ptr;
|
|
302
|
+
}
|
|
303
|
+
function getInt32Memory0() {
|
|
304
|
+
if (cachegetInt32Memory0 === null || cachegetInt32Memory0.buffer !== wasm.memory.buffer) {
|
|
305
|
+
cachegetInt32Memory0 = new Int32Array(wasm.memory.buffer);
|
|
306
|
+
}
|
|
307
|
+
return cachegetInt32Memory0;
|
|
308
|
+
}
|
|
309
|
+
function getUint8ClampedMemory0() {
|
|
310
|
+
if (cachegetUint8ClampedMemory0 === null || cachegetUint8ClampedMemory0.buffer !== wasm.memory.buffer) {
|
|
311
|
+
cachegetUint8ClampedMemory0 = new Uint8ClampedArray(wasm.memory.buffer);
|
|
312
|
+
}
|
|
313
|
+
return cachegetUint8ClampedMemory0;
|
|
314
|
+
}
|
|
315
|
+
function getClampedArrayU8FromWasm0(ptr, len) {
|
|
316
|
+
return getUint8ClampedMemory0().subarray(ptr / 1, ptr / 1 + len);
|
|
317
|
+
}
|
|
318
|
+
function resize(input_image, input_width, input_height, output_width, output_height, typ_idx, premultiply, color_space_conversion) {
|
|
319
|
+
try {
|
|
320
|
+
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
|
|
321
|
+
var ptr0 = passArray8ToWasm0(input_image, wasm.__wbindgen_malloc);
|
|
322
|
+
var len0 = WASM_VECTOR_LEN;
|
|
323
|
+
wasm.resize(retptr, ptr0, len0, input_width, input_height, output_width, output_height, typ_idx, premultiply, color_space_conversion);
|
|
324
|
+
var r0 = getInt32Memory0()[retptr / 4 + 0];
|
|
325
|
+
var r1 = getInt32Memory0()[retptr / 4 + 1];
|
|
326
|
+
var v1 = getClampedArrayU8FromWasm0(r0, r1).slice();
|
|
327
|
+
wasm.__wbindgen_free(r0, r1 * 1);
|
|
328
|
+
return v1;
|
|
329
|
+
} finally {
|
|
330
|
+
wasm.__wbindgen_add_to_stack_pointer(16);
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
async function load(module, imports) {
|
|
334
|
+
if (typeof Response === "function" && module instanceof Response) {
|
|
335
|
+
if (typeof WebAssembly.instantiateStreaming === "function") {
|
|
336
|
+
try {
|
|
337
|
+
return await WebAssembly.instantiateStreaming(module, imports);
|
|
338
|
+
} catch (e) {
|
|
339
|
+
if (module.headers.get("Content-Type") != "application/wasm") {
|
|
340
|
+
console.warn("`WebAssembly.instantiateStreaming` failed because your server does not serve wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n", e);
|
|
341
|
+
} else {
|
|
342
|
+
throw e;
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
const bytes = await module.arrayBuffer();
|
|
347
|
+
return await WebAssembly.instantiate(bytes, imports);
|
|
348
|
+
} else {
|
|
349
|
+
const instance = await WebAssembly.instantiate(module, imports);
|
|
350
|
+
if (instance instanceof WebAssembly.Instance) {
|
|
351
|
+
return { instance, module };
|
|
352
|
+
} else {
|
|
353
|
+
return instance;
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
async function init(input) {
|
|
358
|
+
if (typeof input === "undefined") {
|
|
359
|
+
input = new URL("squoosh_resize_bg.wasm", import.meta.url);
|
|
360
|
+
}
|
|
361
|
+
const imports = {};
|
|
362
|
+
if (typeof input === "string" || typeof Request === "function" && input instanceof Request || typeof URL === "function" && input instanceof URL) {
|
|
363
|
+
input = fetch(input);
|
|
364
|
+
}
|
|
365
|
+
const { instance, module } = await load(await input, imports);
|
|
366
|
+
wasm = instance.exports;
|
|
367
|
+
init.__wbindgen_wasm_module = module;
|
|
368
|
+
return wasm;
|
|
369
|
+
}
|
|
370
|
+
var wasm, cachegetUint8Memory0 = null, WASM_VECTOR_LEN = 0, cachegetInt32Memory0 = null, cachegetUint8ClampedMemory0 = null, squoosh_resize_default;
|
|
371
|
+
var init_squoosh_resize = __esm(() => {
|
|
372
|
+
squoosh_resize_default = init;
|
|
373
|
+
});
|
|
374
|
+
|
|
375
|
+
// src/resize.worker.ts
|
|
376
|
+
var exports_resize_worker = {};
|
|
377
|
+
__export(exports_resize_worker, {
|
|
378
|
+
resizeClient: () => resizeClient
|
|
379
|
+
});
|
|
380
|
+
async function init2() {
|
|
381
|
+
if (wasmResize) {
|
|
382
|
+
return;
|
|
383
|
+
}
|
|
384
|
+
if (initPromise) {
|
|
385
|
+
return initPromise;
|
|
386
|
+
}
|
|
387
|
+
initPromise = (async () => {
|
|
388
|
+
try {
|
|
389
|
+
let wasmBuffer = null;
|
|
390
|
+
const pathsToTry = [
|
|
391
|
+
new URL("./wasm/squoosh_resize_bg.wasm", import.meta.url).href,
|
|
392
|
+
new URL("../wasm/squoosh_resize_bg.wasm", import.meta.url).href
|
|
393
|
+
];
|
|
394
|
+
let lastError = null;
|
|
395
|
+
for (const path of pathsToTry) {
|
|
396
|
+
try {
|
|
397
|
+
wasmBuffer = await loadWasmBinary(path);
|
|
398
|
+
break;
|
|
399
|
+
} catch (error) {
|
|
400
|
+
lastError = error instanceof Error ? error : new Error(String(error));
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
if (!wasmBuffer) {
|
|
404
|
+
throw lastError || new Error("Could not load WASM binary from any path");
|
|
405
|
+
}
|
|
406
|
+
try {
|
|
407
|
+
await squoosh_resize_default(wasmBuffer);
|
|
408
|
+
} catch (initError) {
|
|
409
|
+
if (initError instanceof Error && (initError.message.includes("SharedArrayBuffer") || initError.message.includes("first argument must be"))) {
|
|
410
|
+
if (typeof SharedArrayBuffer === "undefined") {
|
|
411
|
+
globalThis.SharedArrayBuffer = ArrayBuffer;
|
|
412
|
+
}
|
|
413
|
+
try {
|
|
414
|
+
await squoosh_resize_default(wasmBuffer);
|
|
415
|
+
} catch (retryError) {
|
|
416
|
+
throw new Error(`WASM module initialization failed even with polyfill: ${retryError instanceof Error ? retryError.message : String(retryError)}`);
|
|
417
|
+
}
|
|
418
|
+
} else {
|
|
419
|
+
throw initError;
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
wasmResize = resize;
|
|
423
|
+
} catch (error) {
|
|
424
|
+
initPromise = null;
|
|
425
|
+
throw new Error(`Failed to initialize resize WASM module: ${error instanceof Error ? error.message : String(error)}`);
|
|
426
|
+
}
|
|
427
|
+
})();
|
|
428
|
+
return initPromise;
|
|
429
|
+
}
|
|
430
|
+
async function _resizeCore(image, options) {
|
|
431
|
+
validateImageInput(image);
|
|
432
|
+
validateResizeOptions(options);
|
|
433
|
+
await init2();
|
|
434
|
+
if (!wasmResize) {
|
|
435
|
+
throw new Error("Resize module not initialized");
|
|
436
|
+
}
|
|
437
|
+
const { data, width: inputWidth, height: inputHeight } = image;
|
|
438
|
+
let outputWidth = options.width ?? inputWidth;
|
|
439
|
+
let outputHeight = options.height ?? inputHeight;
|
|
440
|
+
if (options.width && !options.height) {
|
|
441
|
+
outputHeight = Math.max(1, Math.round(inputHeight * options.width / inputWidth));
|
|
442
|
+
} else if (options.height && !options.width) {
|
|
443
|
+
outputWidth = Math.max(1, Math.round(inputWidth * options.height / inputHeight));
|
|
444
|
+
}
|
|
445
|
+
if (outputWidth < 1 || outputHeight < 1) {
|
|
446
|
+
throw new RangeError(`Output dimensions must be at least 1x1, got ${outputWidth}x${outputHeight}`);
|
|
447
|
+
}
|
|
448
|
+
const dataArray = data instanceof Uint8ClampedArray ? new Uint8Array(data.buffer, data.byteOffset, data.length) : new Uint8Array(data.buffer, data.byteOffset, data.length);
|
|
449
|
+
const result = wasmResize(dataArray, inputWidth, inputHeight, outputWidth, outputHeight, getResizeMethod(options), options.premultiply ?? true, options.linearRGB ?? true);
|
|
450
|
+
return {
|
|
451
|
+
data: result,
|
|
452
|
+
width: outputWidth,
|
|
453
|
+
height: outputHeight
|
|
454
|
+
};
|
|
455
|
+
}
|
|
456
|
+
async function resizeClient(image, options, signal) {
|
|
457
|
+
if (signal?.aborted) {
|
|
458
|
+
throw new DOMException("Aborted", "AbortError");
|
|
459
|
+
}
|
|
460
|
+
return _resizeCore(image, options);
|
|
461
|
+
}
|
|
462
|
+
function getResizeMethod(options) {
|
|
463
|
+
const methodMap = {
|
|
464
|
+
triangular: 0,
|
|
465
|
+
catrom: 1,
|
|
466
|
+
mitchell: 2,
|
|
467
|
+
lanczos3: 3
|
|
468
|
+
};
|
|
469
|
+
return methodMap[options?.method ?? "lanczos3"] ?? 3;
|
|
470
|
+
}
|
|
471
|
+
var wasmResize = null, initPromise = null;
|
|
472
|
+
var init_resize_worker = __esm(() => {
|
|
473
|
+
init_src();
|
|
474
|
+
init_squoosh_resize();
|
|
475
|
+
if (typeof self !== "undefined") {
|
|
476
|
+
self.onmessage = async (event) => {
|
|
477
|
+
const data = event.data;
|
|
478
|
+
if (data?.type === "worker:ping") {
|
|
479
|
+
self.postMessage({ type: "worker:ready" });
|
|
480
|
+
return;
|
|
481
|
+
}
|
|
482
|
+
const { id, type, payload } = data;
|
|
483
|
+
const response = { id, ok: false };
|
|
484
|
+
try {
|
|
485
|
+
if (type !== "resize:run") {
|
|
486
|
+
throw new Error(`Unknown message type: ${type}`);
|
|
487
|
+
}
|
|
488
|
+
const resultImage = await _resizeCore(payload.image, payload.options);
|
|
489
|
+
response.ok = true;
|
|
490
|
+
response.data = resultImage;
|
|
491
|
+
self.postMessage(response);
|
|
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 ResizeClientBridge {
|
|
505
|
+
async resize(image, options, signal) {
|
|
506
|
+
const module = await Promise.resolve().then(() => (init_resize_worker(), exports_resize_worker));
|
|
507
|
+
const resizeClient2 = module.resizeClient;
|
|
508
|
+
return resizeClient2(image, options, signal);
|
|
509
|
+
}
|
|
510
|
+
async terminate() {}
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
class ResizeWorkerBridge {
|
|
514
|
+
worker = null;
|
|
515
|
+
workerReady = null;
|
|
516
|
+
options;
|
|
517
|
+
constructor(options) {
|
|
518
|
+
this.options = options;
|
|
519
|
+
}
|
|
520
|
+
async getWorker() {
|
|
521
|
+
if (!this.workerReady) {
|
|
522
|
+
this.workerReady = this.createWorker();
|
|
523
|
+
}
|
|
524
|
+
return this.workerReady;
|
|
525
|
+
}
|
|
526
|
+
async createWorker() {
|
|
527
|
+
this.worker = await createReadyWorker("resize.worker.js", this.options);
|
|
528
|
+
return this.worker;
|
|
529
|
+
}
|
|
530
|
+
async resize(image, options, signal) {
|
|
531
|
+
const worker = await this.getWorker();
|
|
532
|
+
validateImageInput(image);
|
|
533
|
+
try {
|
|
534
|
+
const result = await callWorker(worker, "resize:run", { image, options }, signal);
|
|
535
|
+
return result;
|
|
536
|
+
} catch (error) {
|
|
537
|
+
console.error("Resize error:", error);
|
|
538
|
+
throw error;
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
async terminate() {
|
|
542
|
+
if (this.worker) {
|
|
543
|
+
this.worker.terminate();
|
|
544
|
+
this.worker = null;
|
|
545
|
+
this.workerReady = null;
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
function createBridge(mode, options) {
|
|
550
|
+
if (mode === "worker") {
|
|
551
|
+
return new ResizeWorkerBridge(options);
|
|
552
|
+
}
|
|
553
|
+
return new ResizeClientBridge;
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
// src/index.ts
|
|
557
|
+
var globalClientBridge = null;
|
|
558
|
+
async function resize2(imageData, options, signal) {
|
|
559
|
+
if (!globalClientBridge) {
|
|
560
|
+
globalClientBridge = createBridge("worker");
|
|
561
|
+
}
|
|
562
|
+
return globalClientBridge.resize(imageData, options, signal);
|
|
563
|
+
}
|
|
564
|
+
function createResizer(mode = "worker", options) {
|
|
565
|
+
const bridge = createBridge(mode, options);
|
|
566
|
+
return Object.assign((imageData, options2, signal) => {
|
|
567
|
+
return bridge.resize(imageData, options2, signal);
|
|
568
|
+
}, {
|
|
569
|
+
terminate: async () => {
|
|
570
|
+
await bridge.terminate();
|
|
571
|
+
}
|
|
572
|
+
});
|
|
573
|
+
}
|
|
574
|
+
export {
|
|
575
|
+
resize2 as resize,
|
|
576
|
+
createResizer
|
|
577
|
+
};
|
|
578
|
+
|
|
579
|
+
//# debugId=DA2B705D1DFD0F3F64756E2164756E21
|