@puzzle-ui/media 0.0.0-stage → 0.17.0
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 +305 -2
- package/ai/instructions.md +70 -0
- package/ai/manifest.json +10 -0
- package/dist/LICENSE +21 -0
- package/dist/adapters-Bon320yF.js +481 -0
- package/dist/adapters-Bon320yF.js.map +1 -0
- package/dist/adapters-C4N2gqSK.d.cts +63 -0
- package/dist/adapters-DXycRsjJ.cjs +492 -0
- package/dist/adapters-DXycRsjJ.cjs.map +1 -0
- package/dist/adapters-DhPviL_C.d.ts +63 -0
- package/dist/browser/index.cjs +200 -0
- package/dist/browser/index.cjs.map +1 -0
- package/dist/browser/index.d.cts +89 -0
- package/dist/browser/index.d.ts +89 -0
- package/dist/browser/index.js +195 -0
- package/dist/browser/index.js.map +1 -0
- package/dist/image-processing-B1lRPpsg.cjs +444 -0
- package/dist/image-processing-B1lRPpsg.cjs.map +1 -0
- package/dist/image-processing-XkoQ8NT7.js +400 -0
- package/dist/image-processing-XkoQ8NT7.js.map +1 -0
- package/dist/image-worker.cjs +36 -0
- package/dist/image-worker.cjs.map +1 -0
- package/dist/image-worker.d.cts +1 -0
- package/dist/image-worker.d.ts +1 -0
- package/dist/image-worker.js +37 -0
- package/dist/image-worker.js.map +1 -0
- package/dist/index-8Seqy-LJ.d.ts +297 -0
- package/dist/index-BP0ikbAS.d.cts +297 -0
- package/dist/index.cjs +38 -0
- package/dist/index.d.cts +4 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +3 -0
- package/dist/media-validation-By6karZu.cjs +361 -0
- package/dist/media-validation-By6karZu.cjs.map +1 -0
- package/dist/media-validation-OyjGVUq3.js +230 -0
- package/dist/media-validation-OyjGVUq3.js.map +1 -0
- package/dist/media.types-dzfu0Nep.d.cts +127 -0
- package/dist/media.types-dzfu0Nep.d.ts +127 -0
- package/dist/processing.types-CPaSB88L.d.ts +238 -0
- package/dist/processing.types-Dd2XT8Qr.d.cts +238 -0
- package/dist/src-8d6s1iWH.js +1286 -0
- package/dist/src-8d6s1iWH.js.map +1 -0
- package/dist/src-C7WU7ovP.cjs +1363 -0
- package/dist/src-C7WU7ovP.cjs.map +1 -0
- package/dist/testing/adapters.cjs +4 -0
- package/dist/testing/adapters.d.cts +2 -0
- package/dist/testing/adapters.d.ts +2 -0
- package/dist/testing/adapters.js +1 -0
- package/dist/testing/index.cjs +264 -0
- package/dist/testing/index.cjs.map +1 -0
- package/dist/testing/index.d.cts +14 -0
- package/dist/testing/index.d.ts +14 -0
- package/dist/testing/index.js +260 -0
- package/dist/testing/index.js.map +1 -0
- package/package.json +84 -4
|
@@ -0,0 +1,400 @@
|
|
|
1
|
+
import { S as UnsupportedMediaError, f as MediaCapabilityError, n as normalizeMediaCrop, u as InvalidMediaFileError, v as MediaProcessingError, x as MediaValidationError } from "./media-validation-OyjGVUq3.js";
|
|
2
|
+
// src/browser/browser-utils.ts
|
|
3
|
+
const throwIfAborted = (signal) => {
|
|
4
|
+
if (!signal?.aborted) return;
|
|
5
|
+
const error = /* @__PURE__ */ new Error("The media operation was aborted.");
|
|
6
|
+
error.name = "AbortError";
|
|
7
|
+
throw error;
|
|
8
|
+
};
|
|
9
|
+
const createCanvasSurface = (width, height) => {
|
|
10
|
+
if (typeof OffscreenCanvas !== "undefined") {
|
|
11
|
+
const canvas = new OffscreenCanvas(width, height);
|
|
12
|
+
const context = canvas.getContext("2d", { alpha: true });
|
|
13
|
+
if (context) return {
|
|
14
|
+
canvas,
|
|
15
|
+
context
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
if (typeof document === "undefined") throw new MediaCapabilityError("browser canvas");
|
|
19
|
+
const canvas = document.createElement("canvas");
|
|
20
|
+
canvas.width = width;
|
|
21
|
+
canvas.height = height;
|
|
22
|
+
const context = canvas.getContext("2d", { alpha: true });
|
|
23
|
+
if (!context) throw new MediaCapabilityError("HTMLCanvasElement 2D context");
|
|
24
|
+
return {
|
|
25
|
+
canvas,
|
|
26
|
+
context
|
|
27
|
+
};
|
|
28
|
+
};
|
|
29
|
+
const encodeCanvas = async (canvas, type, quality, signal) => {
|
|
30
|
+
throwIfAborted(signal);
|
|
31
|
+
try {
|
|
32
|
+
if ("convertToBlob" in canvas) {
|
|
33
|
+
const result = await canvas.convertToBlob({
|
|
34
|
+
type,
|
|
35
|
+
...quality !== void 0 ? { quality } : {}
|
|
36
|
+
});
|
|
37
|
+
throwIfAborted(signal);
|
|
38
|
+
return result;
|
|
39
|
+
}
|
|
40
|
+
const result = await new Promise((resolve, reject) => {
|
|
41
|
+
canvas.toBlob((blob) => {
|
|
42
|
+
if (blob) resolve(blob);
|
|
43
|
+
else reject(new MediaProcessingError("The browser could not encode the canvas."));
|
|
44
|
+
}, type, quality);
|
|
45
|
+
});
|
|
46
|
+
throwIfAborted(signal);
|
|
47
|
+
return result;
|
|
48
|
+
} catch (error) {
|
|
49
|
+
if (error instanceof Error && error.name === "AbortError") throw error;
|
|
50
|
+
if (error instanceof MediaProcessingError) throw error;
|
|
51
|
+
throw new MediaProcessingError("The browser failed to encode media.", {
|
|
52
|
+
cause: error,
|
|
53
|
+
retryable: false
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
const validateBlob = (blob, expectedPrefix) => {
|
|
58
|
+
if (!blob || typeof blob.size !== "number" || !Number.isSafeInteger(blob.size) || blob.size < 1) throw new MediaValidationError("The selected media file must not be empty.");
|
|
59
|
+
if (typeof blob.type !== "string" || !blob.type.toLowerCase().startsWith(expectedPrefix)) throw new MediaValidationError(`The selected file must have a ${expectedPrefix} MIME type.`);
|
|
60
|
+
};
|
|
61
|
+
const emitProgress = (callback, stage, progress) => {
|
|
62
|
+
callback?.({
|
|
63
|
+
stage,
|
|
64
|
+
progress
|
|
65
|
+
});
|
|
66
|
+
};
|
|
67
|
+
const asFileName = (blob) => {
|
|
68
|
+
const candidate = blob;
|
|
69
|
+
return typeof candidate.name === "string" ? candidate.name : "media";
|
|
70
|
+
};
|
|
71
|
+
const replaceFileExtension = (fileName, mimeType) => {
|
|
72
|
+
const extension = mimeType === "image/jpeg" ? "jpg" : mimeType === "image/png" ? "png" : mimeType === "image/webp" ? "webp" : mimeType === "image/avif" ? "avif" : mimeType.split("/")[1]?.split(";")[0] ?? "bin";
|
|
73
|
+
return `${fileName.replace(/\.[^.]+$/, "") || "media"}.${extension}`;
|
|
74
|
+
};
|
|
75
|
+
// src/browser/image-processing.ts
|
|
76
|
+
const isImageOutputFormat = (value) => value === "image/jpeg" || value === "image/png" || value === "image/webp" || value === "image/avif";
|
|
77
|
+
const validateResizeOptions = (options, requireMaximum = true) => {
|
|
78
|
+
if (requireMaximum && options.maxWidth === void 0 && options.maxHeight === void 0) throw new MediaValidationError("maxWidth, maxHeight, or both must be provided.");
|
|
79
|
+
for (const [field, value] of [["maxWidth", options.maxWidth], ["maxHeight", options.maxHeight]]) if (value !== void 0 && (!Number.isInteger(value) || value < 1)) throw new MediaValidationError(`${field} must be a positive integer.`);
|
|
80
|
+
if (options.quality !== void 0 && (!Number.isFinite(options.quality) || options.quality < 0 || options.quality > 1)) throw new MediaValidationError("quality must be a number from 0 to 1.");
|
|
81
|
+
};
|
|
82
|
+
const validateMaxSourceSize = (blob, maximum) => {
|
|
83
|
+
if (maximum !== void 0 && (!Number.isSafeInteger(maximum) || maximum < 1)) throw new MediaValidationError("maxSourceSizeBytes must be a positive safe integer.");
|
|
84
|
+
if (maximum !== void 0 && blob.size > maximum) throw new MediaValidationError(`The image exceeds the configured maximum of ${maximum} bytes.`);
|
|
85
|
+
};
|
|
86
|
+
const decodeWithImageElement = async (blob, signal) => {
|
|
87
|
+
if (typeof document === "undefined" || typeof URL === "undefined" || typeof URL.createObjectURL !== "function") throw new MediaProcessingError("No browser image decoder is available.", { retryable: false });
|
|
88
|
+
const image = document.createElement("img");
|
|
89
|
+
const url = URL.createObjectURL(blob);
|
|
90
|
+
image.decoding = "async";
|
|
91
|
+
return new Promise((resolve, reject) => {
|
|
92
|
+
let settled = false;
|
|
93
|
+
const cleanup = () => {
|
|
94
|
+
image.removeEventListener("load", onLoad);
|
|
95
|
+
image.removeEventListener("error", onError);
|
|
96
|
+
signal?.removeEventListener("abort", onAbort);
|
|
97
|
+
URL.revokeObjectURL(url);
|
|
98
|
+
};
|
|
99
|
+
const fail = (error) => {
|
|
100
|
+
if (settled) return;
|
|
101
|
+
settled = true;
|
|
102
|
+
cleanup();
|
|
103
|
+
reject(error);
|
|
104
|
+
};
|
|
105
|
+
const onLoad = () => {
|
|
106
|
+
if (image.naturalWidth < 1 || image.naturalHeight < 1) {
|
|
107
|
+
fail(new InvalidMediaFileError("The selected image has no valid dimensions."));
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
if (settled) return;
|
|
111
|
+
settled = true;
|
|
112
|
+
cleanup();
|
|
113
|
+
resolve({
|
|
114
|
+
source: image,
|
|
115
|
+
width: image.naturalWidth,
|
|
116
|
+
height: image.naturalHeight,
|
|
117
|
+
close: () => void 0
|
|
118
|
+
});
|
|
119
|
+
};
|
|
120
|
+
const onError = () => fail(new InvalidMediaFileError("The selected image could not be decoded."));
|
|
121
|
+
const onAbort = () => {
|
|
122
|
+
const error = /* @__PURE__ */ new Error("The image decode was aborted.");
|
|
123
|
+
error.name = "AbortError";
|
|
124
|
+
fail(error);
|
|
125
|
+
};
|
|
126
|
+
image.addEventListener("load", onLoad, { once: true });
|
|
127
|
+
image.addEventListener("error", onError, { once: true });
|
|
128
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
129
|
+
if (signal?.aborted) {
|
|
130
|
+
onAbort();
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
image.src = url;
|
|
134
|
+
});
|
|
135
|
+
};
|
|
136
|
+
const decodeImage = async (blob, signal) => {
|
|
137
|
+
throwIfAborted(signal);
|
|
138
|
+
if (typeof createImageBitmap === "function") try {
|
|
139
|
+
const bitmap = await createImageBitmap(blob, { imageOrientation: "from-image" });
|
|
140
|
+
try {
|
|
141
|
+
throwIfAborted(signal);
|
|
142
|
+
} catch (error) {
|
|
143
|
+
bitmap.close();
|
|
144
|
+
throw error;
|
|
145
|
+
}
|
|
146
|
+
return {
|
|
147
|
+
source: bitmap,
|
|
148
|
+
width: bitmap.width,
|
|
149
|
+
height: bitmap.height,
|
|
150
|
+
close: () => bitmap.close()
|
|
151
|
+
};
|
|
152
|
+
} catch (error) {
|
|
153
|
+
if (error instanceof Error && error.name === "AbortError") throw error;
|
|
154
|
+
try {
|
|
155
|
+
const bitmap = await createImageBitmap(blob);
|
|
156
|
+
try {
|
|
157
|
+
throwIfAborted(signal);
|
|
158
|
+
} catch (error) {
|
|
159
|
+
bitmap.close();
|
|
160
|
+
throw error;
|
|
161
|
+
}
|
|
162
|
+
return {
|
|
163
|
+
source: bitmap,
|
|
164
|
+
width: bitmap.width,
|
|
165
|
+
height: bitmap.height,
|
|
166
|
+
close: () => bitmap.close()
|
|
167
|
+
};
|
|
168
|
+
} catch (fallbackError) {
|
|
169
|
+
if (fallbackError instanceof Error && fallbackError.name === "AbortError") throw fallbackError;
|
|
170
|
+
try {
|
|
171
|
+
return await decodeWithImageElement(blob, signal);
|
|
172
|
+
} catch (imageError) {
|
|
173
|
+
if (imageError instanceof Error && imageError.name === "AbortError") throw imageError;
|
|
174
|
+
throw new InvalidMediaFileError("The selected image could not be decoded.", { cause: fallbackError ?? error });
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
return decodeWithImageElement(blob, signal);
|
|
179
|
+
};
|
|
180
|
+
const getOutputSize = (width, height, maxWidth, maxHeight) => {
|
|
181
|
+
const scale = Math.min(1, maxWidth === void 0 ? 1 : maxWidth / width, maxHeight === void 0 ? 1 : maxHeight / height);
|
|
182
|
+
return {
|
|
183
|
+
width: Math.max(1, Math.floor(width * scale)),
|
|
184
|
+
height: Math.max(1, Math.floor(height * scale))
|
|
185
|
+
};
|
|
186
|
+
};
|
|
187
|
+
const getDefaultFormat = (type) => {
|
|
188
|
+
const normalized = type.toLowerCase();
|
|
189
|
+
if (isImageOutputFormat(normalized)) return normalized;
|
|
190
|
+
return "image/png";
|
|
191
|
+
};
|
|
192
|
+
const processImageOnCurrentThread = async (image, options, crop) => {
|
|
193
|
+
validateResizeOptions(options, crop === void 0);
|
|
194
|
+
validateBlob(image, "image/");
|
|
195
|
+
validateMaxSourceSize(image, options.maxSourceSizeBytes);
|
|
196
|
+
throwIfAborted(options.signal);
|
|
197
|
+
const requestedFormat = options.format ?? getDefaultFormat(image.type);
|
|
198
|
+
const fallbackFormat = options.fallbackFormat ?? "image/png";
|
|
199
|
+
if (!isImageOutputFormat(requestedFormat) || !isImageOutputFormat(fallbackFormat)) throw new UnsupportedMediaError("The requested output image format is not supported.");
|
|
200
|
+
emitProgress(options.onProgress, "decode", 0);
|
|
201
|
+
const decoded = await decodeImage(image, options.signal);
|
|
202
|
+
try {
|
|
203
|
+
throwIfAborted(options.signal);
|
|
204
|
+
emitProgress(options.onProgress, "decode", 1);
|
|
205
|
+
const source = crop ? {
|
|
206
|
+
x: crop.x * decoded.width,
|
|
207
|
+
y: crop.y * decoded.height,
|
|
208
|
+
width: crop.width * decoded.width,
|
|
209
|
+
height: crop.height * decoded.height
|
|
210
|
+
} : {
|
|
211
|
+
x: 0,
|
|
212
|
+
y: 0,
|
|
213
|
+
width: decoded.width,
|
|
214
|
+
height: decoded.height
|
|
215
|
+
};
|
|
216
|
+
if (source.width < 1 || source.height < 1) throw new MediaValidationError("The crop must cover at least one source pixel in each dimension.");
|
|
217
|
+
const output = getOutputSize(source.width, source.height, options.maxWidth, options.maxHeight);
|
|
218
|
+
const { canvas, context } = createCanvasSurface(output.width, output.height);
|
|
219
|
+
emitProgress(options.onProgress, "resize", 0);
|
|
220
|
+
context.drawImage(decoded.source, source.x, source.y, source.width, source.height, 0, 0, output.width, output.height);
|
|
221
|
+
throwIfAborted(options.signal);
|
|
222
|
+
emitProgress(options.onProgress, "resize", 1);
|
|
223
|
+
emitProgress(options.onProgress, "encode", 0);
|
|
224
|
+
let blob = await encodeCanvas(canvas, requestedFormat, options.quality, options.signal);
|
|
225
|
+
let usedFallbackFormat = blob.type.toLowerCase() !== requestedFormat;
|
|
226
|
+
if (usedFallbackFormat) {
|
|
227
|
+
if (blob.type.toLowerCase() !== fallbackFormat) blob = await encodeCanvas(canvas, fallbackFormat, options.quality, options.signal);
|
|
228
|
+
if (blob.type.toLowerCase() !== fallbackFormat) throw new UnsupportedMediaError(`The browser cannot encode ${requestedFormat} or fallback ${fallbackFormat}.`);
|
|
229
|
+
}
|
|
230
|
+
throwIfAborted(options.signal);
|
|
231
|
+
emitProgress(options.onProgress, "encode", 1);
|
|
232
|
+
const format = blob.type.toLowerCase();
|
|
233
|
+
return {
|
|
234
|
+
blob,
|
|
235
|
+
fileName: replaceFileExtension(asFileName(image), format),
|
|
236
|
+
width: output.width,
|
|
237
|
+
height: output.height,
|
|
238
|
+
originalWidth: decoded.width,
|
|
239
|
+
originalHeight: decoded.height,
|
|
240
|
+
format,
|
|
241
|
+
size: blob.size,
|
|
242
|
+
resized: output.width !== decoded.width || output.height !== decoded.height,
|
|
243
|
+
usedFallbackFormat
|
|
244
|
+
};
|
|
245
|
+
} finally {
|
|
246
|
+
decoded.close();
|
|
247
|
+
}
|
|
248
|
+
};
|
|
249
|
+
const createImageWorker = () => {
|
|
250
|
+
if (typeof document === "undefined" || typeof Worker === "undefined" || typeof OffscreenCanvas === "undefined" || typeof createImageBitmap !== "function" || typeof import.meta.url !== "string") return null;
|
|
251
|
+
try {
|
|
252
|
+
return new Worker(new URL("./image-worker.js", import.meta.url), {
|
|
253
|
+
type: "module",
|
|
254
|
+
name: "puzzle-media-image"
|
|
255
|
+
});
|
|
256
|
+
} catch (error) {
|
|
257
|
+
console.warn("Image worker could not start; processing on the current thread.", error);
|
|
258
|
+
return null;
|
|
259
|
+
}
|
|
260
|
+
};
|
|
261
|
+
const processImageInWorker = (worker, image, options, crop) => new Promise((resolve, reject) => {
|
|
262
|
+
let ready = false;
|
|
263
|
+
let settled = false;
|
|
264
|
+
const cleanup = () => {
|
|
265
|
+
worker.terminate();
|
|
266
|
+
options.signal?.removeEventListener("abort", onAbort);
|
|
267
|
+
};
|
|
268
|
+
const finish = (action) => {
|
|
269
|
+
if (settled) return;
|
|
270
|
+
settled = true;
|
|
271
|
+
cleanup();
|
|
272
|
+
action();
|
|
273
|
+
};
|
|
274
|
+
const onAbort = () => {
|
|
275
|
+
finish(() => {
|
|
276
|
+
const error = /* @__PURE__ */ new Error("The media operation was aborted.");
|
|
277
|
+
error.name = "AbortError";
|
|
278
|
+
reject(error);
|
|
279
|
+
});
|
|
280
|
+
};
|
|
281
|
+
worker.addEventListener("error", (event) => {
|
|
282
|
+
if (!ready) {
|
|
283
|
+
console.warn("Image worker could not load; processing on the current thread.", event.message);
|
|
284
|
+
finish(() => resolve(null));
|
|
285
|
+
} else finish(() => reject(new MediaProcessingError("The image worker failed.", { cause: event.error })));
|
|
286
|
+
});
|
|
287
|
+
worker.addEventListener("messageerror", () => {
|
|
288
|
+
finish(() => reject(new MediaProcessingError("The image worker sent an unreadable message.")));
|
|
289
|
+
});
|
|
290
|
+
worker.addEventListener("message", (event) => {
|
|
291
|
+
const message = event.data;
|
|
292
|
+
switch (message.type) {
|
|
293
|
+
case "ready": {
|
|
294
|
+
ready = true;
|
|
295
|
+
const request = {
|
|
296
|
+
image,
|
|
297
|
+
options: {
|
|
298
|
+
maxWidth: options.maxWidth,
|
|
299
|
+
maxHeight: options.maxHeight,
|
|
300
|
+
format: options.format,
|
|
301
|
+
fallbackFormat: options.fallbackFormat,
|
|
302
|
+
quality: options.quality,
|
|
303
|
+
maxSourceSizeBytes: options.maxSourceSizeBytes
|
|
304
|
+
},
|
|
305
|
+
...crop ? { crop } : {}
|
|
306
|
+
};
|
|
307
|
+
try {
|
|
308
|
+
worker.postMessage(request);
|
|
309
|
+
} catch (error) {
|
|
310
|
+
finish(() => reject(new MediaProcessingError("The image could not be sent to the worker.", {
|
|
311
|
+
cause: error,
|
|
312
|
+
retryable: false
|
|
313
|
+
})));
|
|
314
|
+
}
|
|
315
|
+
break;
|
|
316
|
+
}
|
|
317
|
+
case "progress":
|
|
318
|
+
try {
|
|
319
|
+
options.onProgress?.(message.progress);
|
|
320
|
+
} catch (error) {
|
|
321
|
+
finish(() => reject(error));
|
|
322
|
+
}
|
|
323
|
+
break;
|
|
324
|
+
case "fallback":
|
|
325
|
+
finish(() => resolve(null));
|
|
326
|
+
break;
|
|
327
|
+
case "result":
|
|
328
|
+
finish(() => resolve(message.result));
|
|
329
|
+
break;
|
|
330
|
+
case "error": finish(() => {
|
|
331
|
+
if (message.code === "VALIDATION") reject(new MediaValidationError(message.message));
|
|
332
|
+
else if (message.code === "UNSUPPORTED_MEDIA") reject(new UnsupportedMediaError(message.message));
|
|
333
|
+
else reject(new MediaProcessingError(message.message, { retryable: message.retryable }));
|
|
334
|
+
});
|
|
335
|
+
}
|
|
336
|
+
});
|
|
337
|
+
options.signal?.addEventListener("abort", onAbort, { once: true });
|
|
338
|
+
if (options.signal?.aborted) onAbort();
|
|
339
|
+
});
|
|
340
|
+
const processImage = async (image, options, crop) => {
|
|
341
|
+
validateResizeOptions(options, crop === void 0);
|
|
342
|
+
validateBlob(image, "image/");
|
|
343
|
+
validateMaxSourceSize(image, options.maxSourceSizeBytes);
|
|
344
|
+
throwIfAborted(options.signal);
|
|
345
|
+
const requestedFormat = options.format ?? getDefaultFormat(image.type);
|
|
346
|
+
const fallbackFormat = options.fallbackFormat ?? "image/png";
|
|
347
|
+
if (!isImageOutputFormat(requestedFormat) || !isImageOutputFormat(fallbackFormat)) throw new UnsupportedMediaError("The requested output image format is not supported.");
|
|
348
|
+
const worker = createImageWorker();
|
|
349
|
+
if (worker) {
|
|
350
|
+
const result = await processImageInWorker(worker, image, options, crop);
|
|
351
|
+
if (result) return result;
|
|
352
|
+
}
|
|
353
|
+
return processImageOnCurrentThread(image, options, crop);
|
|
354
|
+
};
|
|
355
|
+
const resizeImage = async (image, options) => processImage(image, options);
|
|
356
|
+
const cropImage = async (image, options) => processImage(image, options, normalizeMediaCrop(options.crop));
|
|
357
|
+
const createImageVariants = async (image, options) => {
|
|
358
|
+
validateBlob(image, "image/");
|
|
359
|
+
validateMaxSourceSize(image, options.maxSourceSizeBytes);
|
|
360
|
+
if (!Array.isArray(options.profiles) || options.profiles.length === 0) throw new MediaValidationError("At least one named image profile is required.");
|
|
361
|
+
const names = /* @__PURE__ */ new Set();
|
|
362
|
+
for (const profile of options.profiles) {
|
|
363
|
+
if (profile.name.trim() === "" || names.has(profile.name)) throw new MediaValidationError("Image profile names must be non-empty and unique.");
|
|
364
|
+
names.add(profile.name);
|
|
365
|
+
validateResizeOptions(profile);
|
|
366
|
+
}
|
|
367
|
+
const variants = {};
|
|
368
|
+
for (let index = 0; index < options.profiles.length; index += 1) {
|
|
369
|
+
throwIfAborted(options.signal);
|
|
370
|
+
const profile = options.profiles[index];
|
|
371
|
+
if (!profile) continue;
|
|
372
|
+
const result = await resizeImage(image, {
|
|
373
|
+
maxWidth: profile.maxWidth,
|
|
374
|
+
maxHeight: profile.maxHeight,
|
|
375
|
+
format: profile.format ?? options.format,
|
|
376
|
+
fallbackFormat: options.fallbackFormat,
|
|
377
|
+
quality: profile.quality ?? options.quality,
|
|
378
|
+
maxSourceSizeBytes: options.maxSourceSizeBytes,
|
|
379
|
+
signal: options.signal,
|
|
380
|
+
onProgress: (progress) => {
|
|
381
|
+
const segment = 1 / options.profiles.length;
|
|
382
|
+
emitProgress(options.onProgress, progress.stage, (index + progress.progress) * segment);
|
|
383
|
+
}
|
|
384
|
+
});
|
|
385
|
+
Object.defineProperty(variants, profile.name, {
|
|
386
|
+
value: result,
|
|
387
|
+
enumerable: true,
|
|
388
|
+
writable: true,
|
|
389
|
+
configurable: true
|
|
390
|
+
});
|
|
391
|
+
}
|
|
392
|
+
throwIfAborted(options.signal);
|
|
393
|
+
return {
|
|
394
|
+
original: options.retainOriginal ? image : null,
|
|
395
|
+
variants
|
|
396
|
+
};
|
|
397
|
+
};
|
|
398
|
+
export { createCanvasSurface as a, throwIfAborted as c, resizeImage as i, validateBlob as l, cropImage as n, emitProgress as o, processImageOnCurrentThread as r, encodeCanvas as s, createImageVariants as t };
|
|
399
|
+
|
|
400
|
+
//# sourceMappingURL=image-processing-XkoQ8NT7.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"image-processing-XkoQ8NT7.js","names":[],"sources":["../src/browser/browser-utils.ts","../src/browser/image-processing.ts"],"sourcesContent":["import {\n MediaCapabilityError,\n MediaProcessingError,\n MediaValidationError,\n} from \"../errors/media.errors\";\n\nexport type BrowserMediaProgress = {\n readonly stage: \"decode\" | \"resize\" | \"encode\" | \"metadata\" | \"seek\" | \"poster\";\n readonly progress: number;\n};\n\nexport type BrowserMediaProgressCallback = (\n progress: BrowserMediaProgress,\n) => void;\n\nexport type CanvasSurface = {\n readonly canvas: HTMLCanvasElement | OffscreenCanvas;\n readonly context: CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D;\n};\n\nexport const throwIfAborted = (signal?: AbortSignal): void => {\n if (!signal?.aborted) return;\n const error = new Error(\"The media operation was aborted.\");\n error.name = \"AbortError\";\n throw error;\n};\n\nexport const createCanvasSurface = (\n width: number,\n height: number,\n): CanvasSurface => {\n if (typeof OffscreenCanvas !== \"undefined\") {\n const canvas = new OffscreenCanvas(width, height);\n const context = canvas.getContext(\"2d\", { alpha: true });\n if (context) return { canvas, context };\n }\n if (typeof document === \"undefined\") {\n throw new MediaCapabilityError(\"browser canvas\");\n }\n const canvas = document.createElement(\"canvas\");\n canvas.width = width;\n canvas.height = height;\n const context = canvas.getContext(\"2d\", { alpha: true });\n if (!context) {\n throw new MediaCapabilityError(\"HTMLCanvasElement 2D context\");\n }\n return { canvas, context };\n};\n\nexport const encodeCanvas = async (\n canvas: HTMLCanvasElement | OffscreenCanvas,\n type: string,\n quality: number | undefined,\n signal?: AbortSignal,\n): Promise<Blob> => {\n throwIfAborted(signal);\n try {\n if (\"convertToBlob\" in canvas) {\n const result = await canvas.convertToBlob({\n type,\n ...(quality !== undefined ? { quality } : {}),\n });\n throwIfAborted(signal);\n return result;\n }\n const result = await new Promise<Blob>((resolve, reject) => {\n canvas.toBlob(\n (blob) => {\n if (blob) resolve(blob);\n else reject(new MediaProcessingError(\"The browser could not encode the canvas.\"));\n },\n type,\n quality,\n );\n });\n throwIfAborted(signal);\n return result;\n } catch (error) {\n if (error instanceof Error && error.name === \"AbortError\") throw error;\n if (error instanceof MediaProcessingError) throw error;\n throw new MediaProcessingError(\"The browser failed to encode media.\", {\n cause: error,\n retryable: false,\n });\n }\n};\n\nexport const validateBlob = (blob: Blob, expectedPrefix: string): void => {\n if (!blob || typeof blob.size !== \"number\" || !Number.isSafeInteger(blob.size) || blob.size < 1) {\n throw new MediaValidationError(\"The selected media file must not be empty.\");\n }\n if (\n typeof blob.type !== \"string\" ||\n !blob.type.toLowerCase().startsWith(expectedPrefix)\n ) {\n throw new MediaValidationError(\n `The selected file must have a ${expectedPrefix} MIME type.`,\n );\n }\n};\n\nexport const emitProgress = (\n callback: BrowserMediaProgressCallback | undefined,\n stage: BrowserMediaProgress[\"stage\"],\n progress: number,\n): void => {\n callback?.({ stage, progress });\n};\n\nexport const asFileName = (blob: Blob): string => {\n const candidate = blob as Blob & { readonly name?: unknown };\n return typeof candidate.name === \"string\" ? candidate.name : \"media\";\n};\n\nexport const replaceFileExtension = (\n fileName: string,\n mimeType: string,\n): string => {\n const extension =\n mimeType === \"image/jpeg\"\n ? \"jpg\"\n : mimeType === \"image/png\"\n ? \"png\"\n : mimeType === \"image/webp\"\n ? \"webp\"\n : mimeType === \"image/avif\"\n ? \"avif\"\n : mimeType.split(\"/\")[1]?.split(\";\")[0] ?? \"bin\";\n const basename = fileName.replace(/\\.[^.]+$/, \"\") || \"media\";\n return `${basename}.${extension}`;\n};\n","import {\n InvalidMediaFileError,\n MediaProcessingError,\n MediaValidationError,\n UnsupportedMediaError,\n} from \"../errors/media.errors\";\nimport type { MediaCrop } from \"../types/media.types\";\nimport { normalizeMediaCrop } from \"../utils/media-validation\";\nimport {\n asFileName,\n createCanvasSurface,\n emitProgress,\n encodeCanvas,\n replaceFileExtension,\n throwIfAborted,\n validateBlob,\n type BrowserMediaProgressCallback,\n} from \"./browser-utils\";\nimport type { ImageWorkerRequest, ImageWorkerResponse } from \"./image-worker-protocol\";\n\nexport type ImageOutputFormat =\n | \"image/jpeg\"\n | \"image/png\"\n | \"image/webp\"\n | \"image/avif\";\n\nexport type ResizeImageOptions = {\n readonly maxWidth?: number;\n readonly maxHeight?: number;\n readonly format?: ImageOutputFormat;\n readonly fallbackFormat?: ImageOutputFormat;\n readonly quality?: number;\n readonly maxSourceSizeBytes?: number;\n readonly signal?: AbortSignal;\n readonly onProgress?: BrowserMediaProgressCallback;\n};\n\nexport type CropImageOptions = ResizeImageOptions & {\n /** Crop coordinates relative to the original, oriented image. */\n readonly crop: MediaCrop;\n};\n\nexport type ImageProcessingResult = {\n readonly blob: Blob;\n readonly fileName: string;\n readonly width: number;\n readonly height: number;\n readonly originalWidth: number;\n readonly originalHeight: number;\n readonly format: string;\n readonly size: number;\n readonly resized: boolean;\n readonly usedFallbackFormat: boolean;\n};\n\nexport type ImageVariantProfile = {\n readonly name: string;\n readonly maxWidth?: number;\n readonly maxHeight?: number;\n readonly format?: ImageOutputFormat;\n readonly quality?: number;\n};\n\nexport type CreateImageVariantsOptions = {\n readonly profiles: readonly ImageVariantProfile[];\n /** Explicitly retain the input, including any EXIF/GPS metadata it contains. */\n readonly retainOriginal: boolean;\n readonly format?: ImageOutputFormat;\n readonly fallbackFormat?: ImageOutputFormat;\n readonly quality?: number;\n readonly maxSourceSizeBytes?: number;\n readonly signal?: AbortSignal;\n readonly onProgress?: BrowserMediaProgressCallback;\n};\n\nexport type ImageVariantsResult = {\n readonly original: Blob | null;\n readonly variants: Readonly<Record<string, ImageProcessingResult>>;\n};\n\ntype DecodedImage = {\n readonly source: CanvasImageSource;\n readonly width: number;\n readonly height: number;\n close(): void;\n};\n\nconst isImageOutputFormat = (value: string): value is ImageOutputFormat =>\n value === \"image/jpeg\" ||\n value === \"image/png\" ||\n value === \"image/webp\" ||\n value === \"image/avif\";\n\nconst validateResizeOptions = (\n options: Pick<ResizeImageOptions, \"maxWidth\" | \"maxHeight\" | \"quality\">,\n requireMaximum = true,\n): void => {\n if (requireMaximum && options.maxWidth === undefined && options.maxHeight === undefined) {\n throw new MediaValidationError(\"maxWidth, maxHeight, or both must be provided.\");\n }\n for (const [field, value] of [\n [\"maxWidth\", options.maxWidth],\n [\"maxHeight\", options.maxHeight],\n ] as const) {\n if (value !== undefined && (!Number.isInteger(value) || value < 1)) {\n throw new MediaValidationError(`${field} must be a positive integer.`);\n }\n }\n if (\n options.quality !== undefined &&\n (!Number.isFinite(options.quality) ||\n options.quality < 0 ||\n options.quality > 1)\n ) {\n throw new MediaValidationError(\"quality must be a number from 0 to 1.\");\n }\n};\n\nconst validateMaxSourceSize = (\n blob: Blob,\n maximum: number | undefined,\n): void => {\n if (maximum !== undefined && (!Number.isSafeInteger(maximum) || maximum < 1)) {\n throw new MediaValidationError(\"maxSourceSizeBytes must be a positive safe integer.\");\n }\n if (maximum !== undefined && blob.size > maximum) {\n throw new MediaValidationError(\n `The image exceeds the configured maximum of ${maximum} bytes.`,\n );\n }\n};\n\nconst decodeWithImageElement = async (\n blob: Blob,\n signal?: AbortSignal,\n): Promise<DecodedImage> => {\n if (\n typeof document === \"undefined\" ||\n typeof URL === \"undefined\" ||\n typeof URL.createObjectURL !== \"function\"\n ) {\n throw new MediaProcessingError(\"No browser image decoder is available.\", {\n retryable: false,\n });\n }\n const image = document.createElement(\"img\");\n const url = URL.createObjectURL(blob);\n image.decoding = \"async\";\n return new Promise<DecodedImage>((resolve, reject) => {\n let settled = false;\n const cleanup = (): void => {\n image.removeEventListener(\"load\", onLoad);\n image.removeEventListener(\"error\", onError);\n signal?.removeEventListener(\"abort\", onAbort);\n URL.revokeObjectURL(url);\n };\n const fail = (error: Error): void => {\n if (settled) return;\n settled = true;\n cleanup();\n reject(error);\n };\n const onLoad = (): void => {\n if (image.naturalWidth < 1 || image.naturalHeight < 1) {\n fail(new InvalidMediaFileError(\"The selected image has no valid dimensions.\"));\n return;\n }\n if (settled) return;\n settled = true;\n cleanup();\n resolve({\n source: image,\n width: image.naturalWidth,\n height: image.naturalHeight,\n close: () => undefined,\n });\n };\n const onError = (): void =>\n fail(new InvalidMediaFileError(\"The selected image could not be decoded.\"));\n const onAbort = (): void => {\n const error = new Error(\"The image decode was aborted.\");\n error.name = \"AbortError\";\n fail(error);\n };\n image.addEventListener(\"load\", onLoad, { once: true });\n image.addEventListener(\"error\", onError, { once: true });\n signal?.addEventListener(\"abort\", onAbort, { once: true });\n if (signal?.aborted) {\n onAbort();\n return;\n }\n image.src = url;\n });\n};\n\nconst decodeImage = async (\n blob: Blob,\n signal?: AbortSignal,\n): Promise<DecodedImage> => {\n throwIfAborted(signal);\n if (typeof createImageBitmap === \"function\") {\n try {\n const bitmap = await createImageBitmap(blob, {\n imageOrientation: \"from-image\",\n });\n try {\n throwIfAborted(signal);\n } catch (error) {\n bitmap.close();\n throw error;\n }\n return {\n source: bitmap,\n width: bitmap.width,\n height: bitmap.height,\n close: () => bitmap.close(),\n };\n } catch (error) {\n if (error instanceof Error && error.name === \"AbortError\") throw error;\n try {\n const bitmap = await createImageBitmap(blob);\n try {\n throwIfAborted(signal);\n } catch (error) {\n bitmap.close();\n throw error;\n }\n return {\n source: bitmap,\n width: bitmap.width,\n height: bitmap.height,\n close: () => bitmap.close(),\n };\n } catch (fallbackError) {\n if (fallbackError instanceof Error && fallbackError.name === \"AbortError\") {\n throw fallbackError;\n }\n try {\n return await decodeWithImageElement(blob, signal);\n } catch (imageError) {\n if (imageError instanceof Error && imageError.name === \"AbortError\") {\n throw imageError;\n }\n throw new InvalidMediaFileError(\"The selected image could not be decoded.\", {\n cause: fallbackError ?? error,\n });\n }\n }\n }\n }\n return decodeWithImageElement(blob, signal);\n};\n\nconst getOutputSize = (\n width: number,\n height: number,\n maxWidth: number | undefined,\n maxHeight: number | undefined,\n): { readonly width: number; readonly height: number } => {\n const scale = Math.min(\n 1,\n maxWidth === undefined ? 1 : maxWidth / width,\n maxHeight === undefined ? 1 : maxHeight / height,\n );\n return {\n width: Math.max(1, Math.floor(width * scale)),\n height: Math.max(1, Math.floor(height * scale)),\n };\n};\n\nconst getDefaultFormat = (type: string): ImageOutputFormat => {\n const normalized = type.toLowerCase();\n if (isImageOutputFormat(normalized)) return normalized;\n return \"image/png\";\n};\n\nexport const processImageOnCurrentThread = async (\n image: Blob,\n options: ResizeImageOptions,\n crop?: MediaCrop,\n): Promise<ImageProcessingResult> => {\n validateResizeOptions(options, crop === undefined);\n validateBlob(image, \"image/\");\n validateMaxSourceSize(image, options.maxSourceSizeBytes);\n throwIfAborted(options.signal);\n const requestedFormat = options.format ?? getDefaultFormat(image.type);\n const fallbackFormat = options.fallbackFormat ?? \"image/png\";\n if (!isImageOutputFormat(requestedFormat) || !isImageOutputFormat(fallbackFormat)) {\n throw new UnsupportedMediaError(\"The requested output image format is not supported.\");\n }\n emitProgress(options.onProgress, \"decode\", 0);\n const decoded = await decodeImage(image, options.signal);\n try {\n throwIfAborted(options.signal);\n emitProgress(options.onProgress, \"decode\", 1);\n const source = crop\n ? {\n x: crop.x * decoded.width,\n y: crop.y * decoded.height,\n width: crop.width * decoded.width,\n height: crop.height * decoded.height,\n }\n : { x: 0, y: 0, width: decoded.width, height: decoded.height };\n if (source.width < 1 || source.height < 1) {\n throw new MediaValidationError(\"The crop must cover at least one source pixel in each dimension.\");\n }\n const output = getOutputSize(\n source.width,\n source.height,\n options.maxWidth,\n options.maxHeight,\n );\n const { canvas, context } = createCanvasSurface(output.width, output.height);\n emitProgress(options.onProgress, \"resize\", 0);\n context.drawImage(\n decoded.source,\n source.x,\n source.y,\n source.width,\n source.height,\n 0,\n 0,\n output.width,\n output.height,\n );\n throwIfAborted(options.signal);\n emitProgress(options.onProgress, \"resize\", 1);\n emitProgress(options.onProgress, \"encode\", 0);\n let blob = await encodeCanvas(\n canvas,\n requestedFormat,\n options.quality,\n options.signal,\n );\n let usedFallbackFormat = blob.type.toLowerCase() !== requestedFormat;\n if (usedFallbackFormat) {\n if (blob.type.toLowerCase() !== fallbackFormat) {\n blob = await encodeCanvas(\n canvas,\n fallbackFormat,\n options.quality,\n options.signal,\n );\n }\n if (blob.type.toLowerCase() !== fallbackFormat) {\n throw new UnsupportedMediaError(\n `The browser cannot encode ${requestedFormat} or fallback ${fallbackFormat}.`,\n );\n }\n }\n throwIfAborted(options.signal);\n emitProgress(options.onProgress, \"encode\", 1);\n const format = blob.type.toLowerCase();\n return {\n blob,\n fileName: replaceFileExtension(asFileName(image), format),\n width: output.width,\n height: output.height,\n originalWidth: decoded.width,\n originalHeight: decoded.height,\n format,\n size: blob.size,\n resized: output.width !== decoded.width || output.height !== decoded.height,\n usedFallbackFormat,\n };\n } finally {\n decoded.close();\n }\n};\n\nconst createImageWorker = (): Worker | null => {\n if (\n typeof document === \"undefined\" ||\n typeof Worker === \"undefined\" ||\n typeof OffscreenCanvas === \"undefined\" ||\n typeof createImageBitmap !== \"function\" ||\n typeof import.meta.url !== \"string\"\n ) return null;\n\n try {\n return new Worker(new URL(\"./image-worker.js\", import.meta.url), {\n type: \"module\",\n name: \"puzzle-media-image\",\n });\n } catch (error) {\n console.warn(\"Image worker could not start; processing on the current thread.\", error);\n return null;\n }\n};\n\nconst processImageInWorker = (\n worker: Worker,\n image: Blob,\n options: ResizeImageOptions,\n crop?: MediaCrop,\n): Promise<ImageProcessingResult | null> =>\n new Promise((resolve, reject) => {\n let ready = false;\n let settled = false;\n const cleanup = (): void => {\n worker.terminate();\n options.signal?.removeEventListener(\"abort\", onAbort);\n };\n const finish = (action: () => void): void => {\n if (settled) return;\n settled = true;\n cleanup();\n action();\n };\n const onAbort = (): void => {\n finish(() => {\n const error = new Error(\"The media operation was aborted.\");\n error.name = \"AbortError\";\n reject(error);\n });\n };\n worker.addEventListener(\"error\", (event) => {\n if (!ready) {\n console.warn(\"Image worker could not load; processing on the current thread.\", event.message);\n finish(() => resolve(null));\n } else {\n finish(() => reject(new MediaProcessingError(\"The image worker failed.\", {\n cause: event.error,\n })));\n }\n });\n worker.addEventListener(\"messageerror\", () => {\n finish(() => reject(new MediaProcessingError(\"The image worker sent an unreadable message.\")));\n });\n worker.addEventListener(\"message\", (event: MessageEvent<ImageWorkerResponse>) => {\n const message = event.data;\n switch (message.type) {\n case \"ready\": {\n ready = true;\n const request: ImageWorkerRequest = {\n image,\n options: {\n maxWidth: options.maxWidth,\n maxHeight: options.maxHeight,\n format: options.format,\n fallbackFormat: options.fallbackFormat,\n quality: options.quality,\n maxSourceSizeBytes: options.maxSourceSizeBytes,\n },\n ...(crop ? { crop } : {}),\n };\n try {\n worker.postMessage(request);\n } catch (error) {\n finish(() => reject(new MediaProcessingError(\"The image could not be sent to the worker.\", {\n cause: error,\n retryable: false,\n })));\n }\n break;\n }\n case \"progress\":\n try {\n options.onProgress?.(message.progress);\n } catch (error) {\n finish(() => reject(error));\n }\n break;\n case \"fallback\":\n finish(() => resolve(null));\n break;\n case \"result\":\n finish(() => resolve(message.result));\n break;\n case \"error\":\n finish(() => {\n if (message.code === \"VALIDATION\") reject(new MediaValidationError(message.message));\n else if (message.code === \"UNSUPPORTED_MEDIA\") reject(new UnsupportedMediaError(message.message));\n else reject(new MediaProcessingError(message.message, { retryable: message.retryable }));\n });\n break;\n }\n });\n options.signal?.addEventListener(\"abort\", onAbort, { once: true });\n if (options.signal?.aborted) onAbort();\n });\n\nconst processImage = async (\n image: Blob,\n options: ResizeImageOptions,\n crop?: MediaCrop,\n): Promise<ImageProcessingResult> => {\n validateResizeOptions(options, crop === undefined);\n validateBlob(image, \"image/\");\n validateMaxSourceSize(image, options.maxSourceSizeBytes);\n throwIfAborted(options.signal);\n const requestedFormat = options.format ?? getDefaultFormat(image.type);\n const fallbackFormat = options.fallbackFormat ?? \"image/png\";\n if (!isImageOutputFormat(requestedFormat) || !isImageOutputFormat(fallbackFormat)) {\n throw new UnsupportedMediaError(\"The requested output image format is not supported.\");\n }\n const worker = createImageWorker();\n if (worker) {\n const result = await processImageInWorker(worker, image, options, crop);\n if (result) return result;\n }\n return processImageOnCurrentThread(image, options, crop);\n};\n\nexport const resizeImage = async (\n image: Blob,\n options: ResizeImageOptions,\n): Promise<ImageProcessingResult> => processImage(image, options);\n\nexport const cropImage = async (\n image: Blob,\n options: CropImageOptions,\n): Promise<ImageProcessingResult> =>\n processImage(image, options, normalizeMediaCrop(options.crop));\n\nexport const createImageVariants = async (\n image: Blob,\n options: CreateImageVariantsOptions,\n): Promise<ImageVariantsResult> => {\n validateBlob(image, \"image/\");\n validateMaxSourceSize(image, options.maxSourceSizeBytes);\n if (!Array.isArray(options.profiles) || options.profiles.length === 0) {\n throw new MediaValidationError(\"At least one named image profile is required.\");\n }\n const names = new Set<string>();\n for (const profile of options.profiles) {\n if (profile.name.trim() === \"\" || names.has(profile.name)) {\n throw new MediaValidationError(\"Image profile names must be non-empty and unique.\");\n }\n names.add(profile.name);\n validateResizeOptions(profile);\n }\n const variants: Record<string, ImageProcessingResult> = {};\n for (let index = 0; index < options.profiles.length; index += 1) {\n throwIfAborted(options.signal);\n const profile = options.profiles[index];\n if (!profile) continue;\n const result = await resizeImage(image, {\n maxWidth: profile.maxWidth,\n maxHeight: profile.maxHeight,\n format: profile.format ?? options.format,\n fallbackFormat: options.fallbackFormat,\n quality: profile.quality ?? options.quality,\n maxSourceSizeBytes: options.maxSourceSizeBytes,\n signal: options.signal,\n onProgress: (progress) => {\n const segment = 1 / options.profiles.length;\n emitProgress(options.onProgress, progress.stage, (index + progress.progress) * segment);\n },\n });\n Object.defineProperty(variants, profile.name, {\n value: result,\n enumerable: true,\n writable: true,\n configurable: true,\n });\n }\n throwIfAborted(options.signal);\n return {\n original: options.retainOriginal ? image : null,\n variants,\n };\n};\n"],"mappings":";;AAoBA,M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;CACH;CACA,eAAe,QAAQ,MAAM;CAC7B,OAAO;EACL,UAAU,QAAQ,iBAAiB,QAAQ;EAC3C;CACF;AACF"}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
const require_media_validation = require("./media-validation-By6karZu.cjs");
|
|
2
|
+
const require_image_processing = require("./image-processing-B1lRPpsg.cjs");
|
|
3
|
+
// src/browser/image-worker.ts
|
|
4
|
+
const scope = globalThis;
|
|
5
|
+
const respond = (message) => scope.postMessage(message);
|
|
6
|
+
scope.addEventListener("message", (event) => {
|
|
7
|
+
const { image, options, crop } = event.data;
|
|
8
|
+
if (typeof OffscreenCanvas === "undefined" || typeof createImageBitmap !== "function") {
|
|
9
|
+
respond({ type: "fallback" });
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
require_image_processing.processImageOnCurrentThread(image, {
|
|
13
|
+
...options,
|
|
14
|
+
onProgress: (progress) => respond({
|
|
15
|
+
type: "progress",
|
|
16
|
+
progress
|
|
17
|
+
})
|
|
18
|
+
}, crop).then((result) => respond({
|
|
19
|
+
type: "result",
|
|
20
|
+
result
|
|
21
|
+
}), (error) => {
|
|
22
|
+
if (error instanceof require_media_validation.MediaCapabilityError || error instanceof require_media_validation.MediaError && error.code === "INVALID_FILE") {
|
|
23
|
+
respond({ type: "fallback" });
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
respond({
|
|
27
|
+
type: "error",
|
|
28
|
+
code: error instanceof require_media_validation.MediaError ? error.code : "PROCESSING_FAILED",
|
|
29
|
+
message: error instanceof Error ? error.message : "Image processing failed.",
|
|
30
|
+
...error instanceof require_media_validation.MediaProcessingError ? { retryable: error.retryable } : {}
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
respond({ type: "ready" });
|
|
35
|
+
|
|
36
|
+
//# sourceMappingURL=image-worker.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"image-worker.cjs","names":["processImageOnCurrentThread","MediaCapabilityError","MediaError","MediaProcessingError"],"sources":["../src/browser/image-worker.ts"],"sourcesContent":["/// <reference lib=\"webworker\" />\n\nimport {\n MediaCapabilityError,\n MediaError,\n MediaProcessingError,\n} from \"../errors/media.errors\";\nimport { processImageOnCurrentThread } from \"./image-processing\";\nimport type { ImageWorkerRequest, ImageWorkerResponse } from \"./image-worker-protocol\";\n\nconst scope = globalThis as typeof globalThis & DedicatedWorkerGlobalScope;\nconst respond = (message: ImageWorkerResponse): void => scope.postMessage(message);\n\nscope.addEventListener(\"message\", (event: MessageEvent<ImageWorkerRequest>) => {\n const { image, options, crop } = event.data;\n if (typeof OffscreenCanvas === \"undefined\" || typeof createImageBitmap !== \"function\") {\n respond({ type: \"fallback\" });\n return;\n }\n void processImageOnCurrentThread(image, {\n ...options,\n onProgress: (progress) => respond({ type: \"progress\", progress }),\n }, crop).then(\n (result) => respond({ type: \"result\", result }),\n (error: unknown) => {\n if (error instanceof MediaCapabilityError ||\n (error instanceof MediaError && error.code === \"INVALID_FILE\")) {\n respond({ type: \"fallback\" });\n return;\n }\n respond({\n type: \"error\",\n code: error instanceof MediaError ? error.code : \"PROCESSING_FAILED\",\n message: error instanceof Error ? error.message : \"Image processing failed.\",\n ...(error instanceof MediaProcessingError ? { retryable: error.retryable } : {}),\n });\n },\n );\n});\n\nrespond({ type: \"ready\" });\n"],"mappings":";;;AAUA,MAAM,QAAQ;AACd,MAAM,WAAW,YAAuC,MAAM,YAAY,OAAO;AAEjF,MAAM,iBAAiB,YAAY,UAA4C;CAC7E,MAAM,EAAE,OAAO,SAAS,SAAS,MAAM;CACvC,IAAI,OAAO,oBAAoB,eAAe,OAAO,sBAAsB,YAAY;EACrF,QAAQ,EAAE,MAAM,WAAW,CAAC;EAC5B;CACF;CACA,yBAAKA,4BAA4B,OAAO;EACtC,GAAG;EACH,aAAa,aAAa,QAAQ;GAAE,MAAM;GAAY;EAAS,CAAC;CAClE,GAAG,IAAI,CAAC,CAAC,MACN,WAAW,QAAQ;EAAE,MAAM;EAAU;CAAO,CAAC,IAC7C,UAAmB;EAClB,IAAI,iBAAiBC,yBAAAA,wBAChB,iBAAiBC,yBAAAA,cAAc,MAAM,SAAS,gBAAiB;GAClE,QAAQ,EAAE,MAAM,WAAW,CAAC;GAC5B;EACF;EACA,QAAQ;GACN,MAAM;GACN,MAAM,iBAAiBA,yBAAAA,aAAa,MAAM,OAAO;GACjD,SAAS,iBAAiB,QAAQ,MAAM,UAAU;GAClD,GAAI,iBAAiBC,yBAAAA,uBAAuB,EAAE,WAAW,MAAM,UAAU,IAAI,CAAC;EAChF,CAAC;CACH,CACF;AACF,CAAC;AAED,QAAQ,EAAE,MAAM,QAAQ,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { f as MediaCapabilityError, m as MediaError, v as MediaProcessingError } from "./media-validation-OyjGVUq3.js";
|
|
2
|
+
import { r as processImageOnCurrentThread } from "./image-processing-XkoQ8NT7.js";
|
|
3
|
+
// src/browser/image-worker.ts
|
|
4
|
+
const scope = globalThis;
|
|
5
|
+
const respond = (message) => scope.postMessage(message);
|
|
6
|
+
scope.addEventListener("message", (event) => {
|
|
7
|
+
const { image, options, crop } = event.data;
|
|
8
|
+
if (typeof OffscreenCanvas === "undefined" || typeof createImageBitmap !== "function") {
|
|
9
|
+
respond({ type: "fallback" });
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
processImageOnCurrentThread(image, {
|
|
13
|
+
...options,
|
|
14
|
+
onProgress: (progress) => respond({
|
|
15
|
+
type: "progress",
|
|
16
|
+
progress
|
|
17
|
+
})
|
|
18
|
+
}, crop).then((result) => respond({
|
|
19
|
+
type: "result",
|
|
20
|
+
result
|
|
21
|
+
}), (error) => {
|
|
22
|
+
if (error instanceof MediaCapabilityError || error instanceof MediaError && error.code === "INVALID_FILE") {
|
|
23
|
+
respond({ type: "fallback" });
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
respond({
|
|
27
|
+
type: "error",
|
|
28
|
+
code: error instanceof MediaError ? error.code : "PROCESSING_FAILED",
|
|
29
|
+
message: error instanceof Error ? error.message : "Image processing failed.",
|
|
30
|
+
...error instanceof MediaProcessingError ? { retryable: error.retryable } : {}
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
respond({ type: "ready" });
|
|
35
|
+
export {};
|
|
36
|
+
|
|
37
|
+
//# sourceMappingURL=image-worker.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"image-worker.js","names":[],"sources":["../src/browser/image-worker.ts"],"sourcesContent":["/// <reference lib=\"webworker\" />\n\nimport {\n MediaCapabilityError,\n MediaError,\n MediaProcessingError,\n} from \"../errors/media.errors\";\nimport { processImageOnCurrentThread } from \"./image-processing\";\nimport type { ImageWorkerRequest, ImageWorkerResponse } from \"./image-worker-protocol\";\n\nconst scope = globalThis as typeof globalThis & DedicatedWorkerGlobalScope;\nconst respond = (message: ImageWorkerResponse): void => scope.postMessage(message);\n\nscope.addEventListener(\"message\", (event: MessageEvent<ImageWorkerRequest>) => {\n const { image, options, crop } = event.data;\n if (typeof OffscreenCanvas === \"undefined\" || typeof createImageBitmap !== \"function\") {\n respond({ type: \"fallback\" });\n return;\n }\n void processImageOnCurrentThread(image, {\n ...options,\n onProgress: (progress) => respond({ type: \"progress\", progress }),\n }, crop).then(\n (result) => respond({ type: \"result\", result }),\n (error: unknown) => {\n if (error instanceof MediaCapabilityError ||\n (error instanceof MediaError && error.code === \"INVALID_FILE\")) {\n respond({ type: \"fallback\" });\n return;\n }\n respond({\n type: \"error\",\n code: error instanceof MediaError ? error.code : \"PROCESSING_FAILED\",\n message: error instanceof Error ? error.message : \"Image processing failed.\",\n ...(error instanceof MediaProcessingError ? { retryable: error.retryable } : {}),\n });\n },\n );\n});\n\nrespond({ type: \"ready\" });\n"],"mappings":";;;AAUA,MAAM,QAAQ;AACd,MAAM,WAAW,YAAuC,MAAM,YAAY,OAAO;AAEjF,MAAM,iBAAiB,YAAY,UAA4C;CAC7E,MAAM,EAAE,OAAO,SAAS,SAAS,MAAM;CACvC,IAAI,OAAO,oBAAoB,eAAe,OAAO,sBAAsB,YAAY;EACrF,QAAQ,EAAE,MAAM,WAAW,CAAC;EAC5B;CACF;CACA,4BAAiC,OAAO;EACtC,GAAG;EACH,aAAa,aAAa,QAAQ;GAAE,MAAM;GAAY;EAAS,CAAC;CAClE,GAAG,IAAI,CAAC,CAAC,MACN,WAAW,QAAQ;EAAE,MAAM;EAAU;CAAO,CAAC,IAC7C,UAAmB;EAClB,IAAI,iBAAiB,wBAChB,iBAAiB,cAAc,MAAM,SAAS,gBAAiB;GAClE,QAAQ,EAAE,MAAM,WAAW,CAAC;GAC5B;EACF;EACA,QAAQ;GACN,MAAM;GACN,MAAM,iBAAiB,aAAa,MAAM,OAAO;GACjD,SAAS,iBAAiB,QAAQ,MAAM,UAAU;GAClD,GAAI,iBAAiB,uBAAuB,EAAE,WAAW,MAAM,UAAU,IAAI,CAAC;EAChF,CAAC;CACH,CACF;AACF,CAAC;AAED,QAAQ,EAAE,MAAM,QAAQ,CAAC"}
|