@d0paminedriven/fs 7.4.0 → 7.6.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.
Files changed (37) hide show
  1. package/dist/{chunk-XCA34DKZ.js → chunk-3RC3WC7A.js} +2 -2
  2. package/dist/{chunk-IU4MDHYM.js → chunk-4LZQHSBL.js} +15 -13
  3. package/dist/chunk-6R36YN3W.js +386 -0
  4. package/dist/{chunk-KDWGS5GC.js → chunk-CTSJFQMC.js} +2 -2
  5. package/dist/{chunk-7FHBNIB7.js → chunk-I4ITMOIV.js} +2 -2
  6. package/dist/{chunk-Q2VF3BVH.js → chunk-MUFEXR25.js} +2 -2
  7. package/dist/{chunk-KNLXF622.js → chunk-PHTZ7NRL.js} +26 -6
  8. package/dist/{chunk-R63XI7FS.js → chunk-Q6QZ5XEP.js} +1 -1
  9. package/dist/{chunk-TDMPLELZ.js → chunk-QKZIGBLD.js} +2 -2
  10. package/dist/chunk-TUY7Y4DL.js +1335 -0
  11. package/dist/{chunk-FPB77OGJ.js → chunk-ZQRB46YA.js} +3 -3
  12. package/dist/fs/index.d.ts +133 -1
  13. package/dist/fs/index.js +11 -10
  14. package/dist/fs-atomic/index.d.ts +1 -0
  15. package/dist/fs-atomic/index.js +5 -4
  16. package/dist/fs-base/index.d.ts +1 -0
  17. package/dist/fs-base/index.js +4 -3
  18. package/dist/fs-core/index.d.ts +2 -1
  19. package/dist/fs-core/index.js +8 -7
  20. package/dist/fs-fetch/index.d.ts +4 -3
  21. package/dist/fs-fetch/index.js +10 -9
  22. package/dist/fs-size/index.d.ts +1 -0
  23. package/dist/fs-size/index.js +7 -6
  24. package/dist/fs-tmp/index.d.ts +1 -0
  25. package/dist/fs-tmp/index.js +9 -8
  26. package/dist/image/index.d.ts +340 -21
  27. package/dist/image/index.js +3 -2
  28. package/dist/image/workup.d.ts +39 -0
  29. package/dist/image/workup.js +12 -0
  30. package/dist/index.d.ts +1 -0
  31. package/dist/index.js +14 -10
  32. package/dist/mime/index.d.ts +45 -15
  33. package/dist/mime/index.js +1 -1
  34. package/dist/types/index.d.ts +3 -2
  35. package/dist/types/index.js +1 -1
  36. package/package.json +13 -8
  37. package/dist/chunk-C2ANVZLG.js +0 -918
@@ -1,918 +0,0 @@
1
- import {
2
- MimeService
3
- } from "./chunk-KNLXF622.js";
4
- import {
5
- __name
6
- } from "./chunk-7QVYU63E.js";
7
-
8
- // src/image/index.ts
9
- import { createReadStream } from "fs";
10
- import { relative } from "path";
11
- var ImageService = class extends MimeService {
12
- constructor(cwd) {
13
- super();
14
- this.cwd = cwd;
15
- }
16
- static {
17
- __name(this, "ImageService");
18
- }
19
- /**
20
- * Read exactly N bytes from a stream, returning a Buffer
21
- * Closes stream after reading or on error
22
- */
23
- async readBytes(stream, bytes) {
24
- return new Promise((resolve, reject) => {
25
- const chunks = [];
26
- let totalBytes = 0;
27
- const cleanup = /* @__PURE__ */ __name(() => {
28
- stream.removeAllListeners();
29
- stream.destroy();
30
- }, "cleanup");
31
- stream.on("error", (err) => {
32
- cleanup();
33
- reject(err);
34
- });
35
- stream.on("data", (chunk) => {
36
- const remaining = bytes - totalBytes;
37
- if (chunk.length <= remaining) {
38
- chunks.push(chunk);
39
- totalBytes += chunk.length;
40
- } else {
41
- chunks.push(chunk.subarray(0, remaining));
42
- totalBytes = bytes;
43
- }
44
- if (totalBytes >= bytes) {
45
- cleanup();
46
- resolve(Buffer.concat(chunks));
47
- }
48
- });
49
- stream.on("end", () => {
50
- cleanup();
51
- resolve(Buffer.concat(chunks));
52
- });
53
- });
54
- }
55
- /**
56
- * Extract image metadata from a stream, reading only ~4KB for most formats
57
- * Automatically handles PNG, JPEG, GIF, BMP, WebP, and AVIF
58
- */
59
- async extractFromStream(stream, size = 4096) {
60
- const headerSize = size;
61
- const header = await this.readBytes(stream, headerSize);
62
- if (this.isPNG(header)) {
63
- return this.extractPNGFromBuffer(header);
64
- }
65
- if (this.isJPEG(header)) {
66
- return this.extractJPEGFromBuffer(header);
67
- }
68
- if (this.isGIF(header)) {
69
- return this.extractGIFFromBuffer(header);
70
- }
71
- if (this.isBMP(header)) {
72
- return this.extractBMPFromBuffer(header);
73
- }
74
- if (this.isWebP(header)) {
75
- return this.extractWebPFromBuffer(header);
76
- }
77
- if (this.isAVIF(header)) {
78
- return this.extractAVIFFromBuffer(header);
79
- }
80
- throw new Error("Unsupported image format or invalid file");
81
- }
82
- /**
83
- * Extract metadata from file path using streaming (memory efficient)
84
- */
85
- async extractFromPath(filePath, size = 4096) {
86
- const stream = createReadStream(relative(this.cwd, filePath), { highWaterMark: size });
87
- return this.extractFromStream(stream, size);
88
- }
89
- // Format detection helpers
90
- isPNG(buffer) {
91
- return buffer?.length >= 8 && buffer[0] === 137 && buffer[1] === 80 && buffer[2] === 78 && buffer[3] === 71 && buffer[4] === 13 && buffer[5] === 10 && buffer[6] === 26 && buffer[7] === 10;
92
- }
93
- isJPEG(buffer) {
94
- return buffer.length >= 2 && buffer[0] === 255 && buffer[1] === 216;
95
- }
96
- isGIF(buffer) {
97
- const header = buffer.toString("ascii", 0, 6);
98
- return buffer.length >= 6 && (header === "GIF87a" || header === "GIF89a");
99
- }
100
- isBMP(buffer) {
101
- return buffer.length >= 2 && buffer[0] === 66 && buffer[1] === 77;
102
- }
103
- isWebP(buffer) {
104
- return buffer.length >= 12 && buffer.toString("ascii", 0, 4) === "RIFF" && buffer.toString("ascii", 8, 12) === "WEBP";
105
- }
106
- isAVIF(buffer) {
107
- if (buffer.length < 32) return false;
108
- const ftyp = buffer.toString("ascii", 4, 8);
109
- if (ftyp !== "ftyp") return false;
110
- const brands = buffer.toString("ascii", 8, 32);
111
- return brands.includes("avif") || brands.includes("avis");
112
- }
113
- // Extract methods for each format (refactored from getImageSpecsWorkup)
114
- extractPNGFromBuffer(buffer) {
115
- if (!this.isPNG(buffer) || buffer.length < 24) {
116
- throw new Error("Invalid PNG header");
117
- }
118
- const width = buffer.readUInt32BE(16);
119
- const height = buffer.readUInt32BE(20);
120
- const colorType = buffer[25];
121
- let colorSpace;
122
- let hasAlpha = false;
123
- switch (colorType) {
124
- case 0:
125
- colorSpace = "grayscale";
126
- break;
127
- case 2:
128
- colorSpace = "rgb";
129
- break;
130
- case 3:
131
- colorSpace = "indexed";
132
- break;
133
- case 4:
134
- colorSpace = "grayscale-alpha";
135
- hasAlpha = true;
136
- break;
137
- case 6:
138
- colorSpace = "rgba";
139
- hasAlpha = true;
140
- break;
141
- default:
142
- colorSpace = "unknown";
143
- }
144
- return {
145
- width,
146
- height,
147
- format: "png",
148
- frames: 1,
149
- animated: false,
150
- hasAlpha,
151
- orientation: null,
152
- aspectRatio: width / height,
153
- colorSpace,
154
- iccProfile: null,
155
- exifDateTimeOriginal: null
156
- };
157
- }
158
- extractJPEGFromBuffer(buffer) {
159
- if (!this.isJPEG(buffer)) {
160
- throw new Error("Invalid JPEG header");
161
- }
162
- let pos = 2;
163
- let width = 0;
164
- let height = 0;
165
- let colorSpace = "unknown";
166
- while (pos < buffer.length - 10) {
167
- if (buffer[pos] !== 255) break;
168
- const marker = buffer[pos + 1];
169
- if (!marker || marker === 218) break;
170
- const segmentSize = buffer.readUInt16BE(pos + 2);
171
- if (marker && marker >= 192 && marker <= 207 && marker !== 196 && marker !== 200 && marker !== 204) {
172
- const numComponents = buffer[pos + 4];
173
- switch (numComponents) {
174
- case 1:
175
- colorSpace = "grayscale";
176
- break;
177
- case 3:
178
- colorSpace = "ycbcr";
179
- break;
180
- case 4:
181
- colorSpace = "ycck";
182
- break;
183
- default:
184
- colorSpace = "unknown";
185
- }
186
- height = buffer.readUInt16BE(pos + 5);
187
- width = buffer.readUInt16BE(pos + 7);
188
- break;
189
- }
190
- pos += segmentSize + 2;
191
- if (pos >= buffer.length) break;
192
- }
193
- if (width === 0) {
194
- throw new Error("Could not find JPEG dimensions in available data");
195
- }
196
- return {
197
- width,
198
- height,
199
- format: "jpeg",
200
- frames: 1,
201
- animated: false,
202
- hasAlpha: false,
203
- orientation: null,
204
- aspectRatio: width / height,
205
- colorSpace,
206
- iccProfile: null,
207
- exifDateTimeOriginal: null
208
- };
209
- }
210
- extractGIFFromBuffer(buffer) {
211
- if (!this.isGIF(buffer) || buffer.length < 10) {
212
- throw new Error("Invalid GIF header");
213
- }
214
- const width = buffer.readUInt16LE(6);
215
- const height = buffer.readUInt16LE(8);
216
- let potentiallyAnimated = false;
217
- if (buffer.length > 13) {
218
- for (let i = 13; i < Math.min(buffer.length - 2, 100); i++) {
219
- if (buffer[i] === 33 && buffer[i + 1] === 249) {
220
- potentiallyAnimated = true;
221
- break;
222
- }
223
- }
224
- }
225
- return {
226
- width,
227
- height,
228
- format: "gif",
229
- frames: potentiallyAnimated ? 2 : 1,
230
- // Estimate
231
- animated: potentiallyAnimated,
232
- hasAlpha: null,
233
- orientation: null,
234
- aspectRatio: width / height,
235
- colorSpace: "indexed",
236
- iccProfile: null,
237
- exifDateTimeOriginal: null
238
- };
239
- }
240
- extractBMPFromBuffer(buffer) {
241
- if (!this.isBMP(buffer) || buffer.length < 26) {
242
- throw new Error("Invalid BMP header");
243
- }
244
- const width = buffer.readInt32LE(18);
245
- const height = Math.abs(buffer.readInt32LE(22));
246
- const bitDepth = buffer.readUInt16LE(28);
247
- const colorSpace = bitDepth <= 8 ? "indexed" : "rgb";
248
- return {
249
- width,
250
- height,
251
- format: "bmp",
252
- frames: 1,
253
- animated: false,
254
- hasAlpha: null,
255
- orientation: buffer.readInt32LE(22) < 0 ? 1 : 6,
256
- aspectRatio: width / height,
257
- colorSpace,
258
- iccProfile: null,
259
- exifDateTimeOriginal: null
260
- };
261
- }
262
- extractWebPFromBuffer(buffer) {
263
- if (!this.isWebP(buffer) || buffer.length < 30) {
264
- throw new Error("Invalid WebP header");
265
- }
266
- const chunkType = buffer.toString("ascii", 12, 16);
267
- let width = 0;
268
- let height = 0;
269
- let hasAlpha = false;
270
- let animated = false;
271
- let colorSpace = "rgb";
272
- if (chunkType === "VP8X") {
273
- const flags = buffer[20];
274
- if (flags !== void 0) {
275
- hasAlpha = !!(flags & 2);
276
- animated = !!(flags & 1);
277
- colorSpace = hasAlpha ? "rgba" : "rgb";
278
- }
279
- const b24 = buffer[24], b25 = buffer[25], b26 = buffer[26];
280
- const b27 = buffer[27], b28 = buffer[28], b29 = buffer[29];
281
- if (b24 !== void 0 && b25 !== void 0 && b26 !== void 0 && b27 !== void 0 && b28 !== void 0 && b29 !== void 0) {
282
- const widthMinus1 = b24 | b25 << 8 | b26 << 16;
283
- const heightMinus1 = b27 | b28 << 8 | b29 << 16;
284
- width = widthMinus1 + 1;
285
- height = heightMinus1 + 1;
286
- }
287
- } else if (chunkType === "VP8") {
288
- if (buffer.length < 30) throw new Error("VP8 data too short");
289
- width = buffer.readUInt16LE(26) & 16383;
290
- height = buffer.readUInt16LE(28) & 16383;
291
- } else if (chunkType === "VP8L") {
292
- if (buffer.length < 25) throw new Error("VP8L data too short");
293
- const bits = buffer.readUInt32LE(21);
294
- hasAlpha = !!(bits & 1 << 8);
295
- colorSpace = hasAlpha ? "rgba" : "rgb";
296
- width = 1 + (bits & 16383);
297
- height = 1 + (bits >> 14 & 16383);
298
- }
299
- return {
300
- width,
301
- height,
302
- format: "webp",
303
- frames: animated ? 2 : 1,
304
- // Estimate
305
- animated,
306
- hasAlpha,
307
- orientation: null,
308
- aspectRatio: width / height,
309
- colorSpace,
310
- iccProfile: null,
311
- exifDateTimeOriginal: null
312
- };
313
- }
314
- extractAVIFFromBuffer(buffer) {
315
- if (!this.isAVIF(buffer)) {
316
- throw new Error("Invalid AVIF header");
317
- }
318
- let width = 0;
319
- let height = 0;
320
- for (let i = 0; i < buffer.length - 16; i++) {
321
- if (buffer.toString("ascii", i, i + 4) === "ispe") {
322
- if (i + 12 < buffer.length) {
323
- width = buffer.readUInt32BE(i + 8);
324
- height = buffer.readUInt32BE(i + 12);
325
- break;
326
- }
327
- }
328
- }
329
- if (width === 0 || height === 0) {
330
- throw new Error("Could not find AVIF dimensions in available data");
331
- }
332
- return {
333
- width,
334
- height,
335
- format: "avif",
336
- frames: 1,
337
- animated: false,
338
- hasAlpha: null,
339
- orientation: null,
340
- aspectRatio: width / height,
341
- colorSpace: "rgb",
342
- iccProfile: null,
343
- exifDateTimeOriginal: null
344
- };
345
- }
346
- parseExif(buffer, app1Pos, app1Size) {
347
- const exifHeader = buffer.toString("ascii", app1Pos + 2, app1Pos + 8);
348
- if (exifHeader !== "Exif\0\0")
349
- return { orientation: null, dateTimeOriginal: null };
350
- let tiffPos = app1Pos + 8;
351
- const app1End = app1Pos + app1Size;
352
- if (tiffPos + 10 > app1End)
353
- return { orientation: null, dateTimeOriginal: null };
354
- const byteOrder = buffer.toString("ascii", tiffPos, tiffPos + 2);
355
- const littleEndian = byteOrder === "II";
356
- const readUInt16 = littleEndian ? buffer.readUInt16LE.bind(buffer) : buffer.readUInt16BE.bind(buffer);
357
- const readUInt32 = littleEndian ? buffer.readUInt32LE.bind(buffer) : buffer.readUInt32BE.bind(buffer);
358
- if (readUInt16(tiffPos + 2) !== 42)
359
- return { orientation: null, dateTimeOriginal: null };
360
- let ifdOffset = readUInt32(tiffPos + 4);
361
- tiffPos += ifdOffset;
362
- const numEntries = readUInt16(tiffPos);
363
- tiffPos += 2;
364
- let orientation = null;
365
- let dateTimeOriginal = null;
366
- for (let i = 0; i < numEntries; i++) {
367
- const tag = readUInt16(tiffPos), type = readUInt16(tiffPos + 2), count = readUInt32(tiffPos + 4), valueOffset = tiffPos + 8;
368
- if (tag === 274 && type === 3 && count === 1) {
369
- orientation = readUInt16(valueOffset);
370
- } else if (tag === 36867 && type === 2 && count === 20) {
371
- const offset = readUInt32(valueOffset);
372
- if (app1Pos + 2 + offset + 19 > app1End) {
373
- dateTimeOriginal = null;
374
- } else {
375
- dateTimeOriginal = buffer.toString("ascii", app1Pos + 2 + offset, app1Pos + 2 + offset + 19).trim();
376
- }
377
- }
378
- tiffPos += 12;
379
- }
380
- return { orientation, dateTimeOriginal };
381
- }
382
- parseXmpFromAvif(buffer, meta, _ipco) {
383
- const iinf = this.findBox(
384
- buffer,
385
- "iinf",
386
- meta.pos + 4,
387
- meta.pos + meta.size
388
- );
389
- if (!iinf) return null;
390
- let pos = iinf.pos;
391
- const version = buffer[pos++];
392
- const itemCount = version === 0 ? buffer.readUInt16BE(pos) : buffer.readUInt32BE(pos);
393
- pos += version === 0 ? 2 : 4;
394
- let xmpItemId;
395
- for (let i = 0; i < itemCount; i++) {
396
- const infeStart = pos;
397
- const infeSize = buffer.readUInt32BE(pos);
398
- if (buffer.toString("ascii", pos + 4, pos + 8) !== "infe") break;
399
- pos += 8;
400
- const infeVersion = buffer?.[pos++];
401
- if (!infeVersion) return null;
402
- const itemId = infeVersion < 2 ? buffer.readUInt16BE(pos) : buffer.readUInt32BE(pos);
403
- pos += infeVersion < 2 ? 2 : 4;
404
- pos += 2;
405
- const itemType = buffer.toString("ascii", pos, pos + 4);
406
- pos += 4;
407
- let nameEnd = pos;
408
- while (buffer[nameEnd] !== 0 && nameEnd < infeStart + infeSize) nameEnd++;
409
- pos = nameEnd + 1;
410
- if (itemType === "mime") {
411
- let ctEnd = pos;
412
- while (buffer[ctEnd] !== 0 && ctEnd < infeStart + infeSize) ctEnd++;
413
- const contentType = buffer.toString("ascii", pos, ctEnd);
414
- if (contentType === "application/rdf+xml") {
415
- xmpItemId = itemId;
416
- break;
417
- }
418
- pos = ctEnd + 1;
419
- }
420
- pos = infeStart + infeSize;
421
- }
422
- if (!xmpItemId) return null;
423
- const iloc = this.findBox(
424
- buffer,
425
- "iloc",
426
- meta.pos + 4,
427
- meta.pos + meta.size
428
- );
429
- if (!iloc) return null;
430
- pos = iloc.pos;
431
- const ilocVersion = buffer[pos++];
432
- const sizes = buffer?.[pos++];
433
- if (!sizes) return null;
434
- const offsetSize = sizes >> 4 & 15;
435
- const lengthSize = sizes & 15;
436
- let indexSize = 0;
437
- if (ilocVersion === 1 || ilocVersion === 2) {
438
- const indexSizes = buffer?.[pos++];
439
- if (!indexSizes) return null;
440
- indexSize = indexSizes >> 4 & 15;
441
- }
442
- if (!ilocVersion) return null;
443
- let buffpos = buffer?.[pos++];
444
- if (!buffpos) return null;
445
- const baseOffsetSize = ilocVersion < 2 ? 0 : buffpos >> 4 & 15;
446
- const itemCountIloc = ilocVersion < 2 ? buffer.readUInt16BE(pos) : buffer.readUInt32BE(pos);
447
- pos += ilocVersion < 2 ? 2 : 4;
448
- let xmpOffset = 0;
449
- let xmpLength = 0;
450
- for (let i = 0; i < itemCountIloc; i++) {
451
- const curItemId = ilocVersion < 2 ? buffer.readUInt16BE(pos) : buffer.readUInt32BE(pos);
452
- pos += ilocVersion < 2 ? 2 : 4;
453
- if (curItemId !== xmpItemId) {
454
- if (ilocVersion === 1 || ilocVersion === 2) pos += 2;
455
- pos += 2;
456
- pos += baseOffsetSize;
457
- const extentCount2 = buffer.readUInt16BE(pos);
458
- pos += 2;
459
- for (let e = 0; e < extentCount2; e++) {
460
- if (indexSize > 0) pos += indexSize;
461
- pos += offsetSize + lengthSize;
462
- }
463
- continue;
464
- }
465
- if (ilocVersion === 1 || ilocVersion === 2) {
466
- const constructionMethod = buffer.readUInt16BE(pos);
467
- pos += 2;
468
- if (constructionMethod !== 0) return null;
469
- }
470
- pos += 2;
471
- const baseOffset = this.readVarSize(buffer, pos, baseOffsetSize);
472
- pos += baseOffsetSize;
473
- const extentCount = buffer.readUInt16BE(pos);
474
- pos += 2;
475
- if (extentCount !== 1) return null;
476
- if (indexSize > 0) pos += indexSize;
477
- const extentOffset = this.readVarSize(buffer, pos, offsetSize);
478
- pos += offsetSize;
479
- const extentLength = this.readVarSize(buffer, pos, lengthSize);
480
- xmpOffset = baseOffset + extentOffset;
481
- xmpLength = extentLength;
482
- break;
483
- }
484
- if (xmpOffset === 0 || xmpLength === 0 || xmpOffset + xmpLength > buffer.length)
485
- return null;
486
- return buffer.toString("utf8", xmpOffset, xmpOffset + xmpLength);
487
- }
488
- readVarSize(buffer, pos, size) {
489
- if (size === 0) return 0;
490
- if (size === 1 && buffer?.[pos]) return buffer?.[pos];
491
- if (size === 2) return buffer.readUInt16BE(pos);
492
- if (size === 3) {
493
- let pos1 = buffer?.[pos + 1], pos2 = buffer?.[pos + 2];
494
- if (buffer?.[pos] && pos1 && pos2) {
495
- return buffer[pos] << 16 | pos1 << 8 | pos2;
496
- }
497
- throw new Error("Invalid var size");
498
- }
499
- if (size === 4) return buffer.readUInt32BE(pos);
500
- if (size === 8) return Number(buffer.readBigUInt64BE(pos));
501
- throw new Error("Invalid var size");
502
- }
503
- findBox(buffer, type, start = 0, end = buffer.length) {
504
- let pos = start;
505
- while (pos < end) {
506
- let boxSize = buffer.readUInt32BE(pos);
507
- let boxType = buffer.toString("ascii", pos + 4, pos + 8);
508
- let hdrSize = 8;
509
- if (boxSize === 1) {
510
- boxSize = Number(buffer.readBigUInt64BE(pos + 8));
511
- hdrSize = 16;
512
- } else if (boxSize === 0) {
513
- boxSize = end - pos;
514
- }
515
- if (boxType === type) {
516
- return { pos: pos + hdrSize, size: boxSize - hdrSize };
517
- }
518
- pos += boxSize;
519
- }
520
- return null;
521
- }
522
- /**
523
- * Legacy method that processes entire buffer
524
- * Use extractFromStream or extractFromPath for better memory efficiency
525
- */
526
- getImageSpecsWorkup(rawbuffer, size = 8192) {
527
- const MAX_HEADER_SIZE = size;
528
- const buffer = rawbuffer.subarray(0, Math.min(rawbuffer.length, MAX_HEADER_SIZE));
529
- if (buffer?.length >= 24 && buffer?.[0] === 137 && buffer?.[1] === 80 && buffer?.[2] === 78 && buffer?.[3] === 71 && buffer?.[4] === 13 && buffer?.[5] === 10 && buffer?.[6] === 26 && buffer?.[7] === 10) {
530
- if (buffer.readUint32BE(8) !== 13 || buffer?.[12] !== 73 || buffer?.[13] !== 72 || buffer?.[14] !== 68 || buffer?.[15] !== 82) {
531
- throw new Error("IHDR Chunk of png not found or incorrect.");
532
- }
533
- const colorType = buffer[25];
534
- let colorSpace;
535
- let hasAlpha = false;
536
- switch (colorType) {
537
- case 0:
538
- colorSpace = "grayscale";
539
- break;
540
- case 2:
541
- colorSpace = "rgb";
542
- break;
543
- case 3:
544
- colorSpace = "indexed";
545
- break;
546
- case 4:
547
- colorSpace = "grayscale-alpha";
548
- hasAlpha = true;
549
- break;
550
- case 6:
551
- colorSpace = "rgba";
552
- hasAlpha = true;
553
- break;
554
- default:
555
- colorSpace = "unknown";
556
- }
557
- const width = buffer.readUInt32BE(16);
558
- const height = buffer.readUInt32BE(20);
559
- let frames = 1;
560
- let animated = false;
561
- let iccProfile = null;
562
- let exifDateTimeOriginal = null;
563
- let orientation = null;
564
- let pos = 33;
565
- while (pos < buffer.length - 12) {
566
- const chunkLen = buffer.readUInt32BE(pos);
567
- const chunkType = buffer.toString("ascii", pos + 4, pos + 8);
568
- const chunkData = pos + 8;
569
- if (chunkType === "acTL") {
570
- animated = true;
571
- frames = buffer.readUInt32BE(chunkData);
572
- } else if (chunkType === "iCCP") {
573
- const nameEnd = buffer.indexOf(0, chunkData);
574
- const profileName = buffer.toString("ascii", chunkData, nameEnd);
575
- iccProfile = profileName || "embedded";
576
- } else if (chunkType === "tIME") {
577
- const month = buffer?.[chunkData + 2], day = buffer?.[chunkData + 3], hour = buffer?.[chunkData + 4], minute = buffer?.[chunkData + 5], second = buffer?.[chunkData + 6];
578
- if (typeof month !== "undefined" && typeof day !== "undefined" && typeof hour !== "undefined" && typeof minute !== "undefined" && typeof second !== "undefined") {
579
- const year = buffer.readUInt16BE(chunkData);
580
- exifDateTimeOriginal = `${year}:${month.toString().padStart(2, "0")}:${day.toString().padStart(2, "0")} ${hour.toString().padStart(2, "0")}:${minute.toString().padStart(2, "0")}:${second.toString().padStart(2, "0")}`;
581
- }
582
- } else if (chunkType === "IDAT") {
583
- break;
584
- }
585
- pos += 12 + chunkLen;
586
- }
587
- return {
588
- width,
589
- height,
590
- format: "png",
591
- frames,
592
- animated,
593
- hasAlpha,
594
- orientation,
595
- aspectRatio: width / height,
596
- colorSpace,
597
- iccProfile,
598
- exifDateTimeOriginal
599
- };
600
- }
601
- if (buffer.length >= 10 && buffer[0] === 255 && buffer[1] === 216) {
602
- let pos = 2, colorSpace = "unknown", hasAlpha = false, width = 0, height = 0, iccProfile = null, orientation = null, exifDateTimeOriginal = null;
603
- while (pos < buffer.length - 10) {
604
- if (buffer[pos] !== 255) {
605
- throw new Error("Invalid JPEG file");
606
- }
607
- const marker = buffer[pos + 1];
608
- if (marker === 218) break;
609
- const segmentSize = buffer.readUInt16BE(pos + 2);
610
- if (!marker) {
611
- throw new Error("no marker for jpeg");
612
- }
613
- if (marker >= 192 && marker <= 207 && marker !== 196 && marker !== 200 && marker !== 204) {
614
- const numComponents = buffer[pos + 4];
615
- switch (numComponents) {
616
- case 1:
617
- colorSpace = "grayscale";
618
- break;
619
- case 3:
620
- colorSpace = "ycbcr";
621
- break;
622
- // Most common for RGB JPEGs (stored as YCbCr)
623
- case 4:
624
- colorSpace = "ycck";
625
- break;
626
- // YCCK for CMYK with alpha-like, but no true alpha
627
- default:
628
- colorSpace = "unknown";
629
- }
630
- width = buffer.readUInt16BE(pos + 7);
631
- height = buffer.readUInt16BE(pos + 5);
632
- } else if (marker === 225) {
633
- const { orientation: ori, dateTimeOriginal } = this.parseExif(
634
- buffer,
635
- pos,
636
- segmentSize
637
- );
638
- orientation = ori;
639
- exifDateTimeOriginal = dateTimeOriginal;
640
- } else if (marker === 226) {
641
- const iccHeader = buffer.toString("ascii", pos + 4, pos + 18);
642
- if (iccHeader.startsWith("ICC_PROFILE")) {
643
- iccProfile = "embedded";
644
- }
645
- }
646
- pos += segmentSize + 2;
647
- while (pos < buffer.length - 1 && buffer[pos] === 0) {
648
- pos++;
649
- }
650
- }
651
- if (width === 0) throw new Error("No dimensions found in JPEG file");
652
- return {
653
- width,
654
- height,
655
- format: "jpeg",
656
- frames: 1,
657
- animated: false,
658
- // JPEG not animated
659
- hasAlpha,
660
- orientation,
661
- aspectRatio: width / height,
662
- colorSpace,
663
- iccProfile,
664
- exifDateTimeOriginal
665
- };
666
- }
667
- const gifHeader = buffer.toString("ascii", 0, 6);
668
- if (buffer.length >= 10 && (gifHeader === "GIF87a" || gifHeader === "GIF89a")) {
669
- const width = buffer.readUInt16LE(6), height = buffer.readUInt16LE(8);
670
- let frames = 0;
671
- let pos = 13;
672
- let bufTen = buffer?.[10];
673
- if (typeof bufTen === "undefined") {
674
- pos = 13;
675
- frames = 0;
676
- } else if (bufTen & 128) pos += 3 << (bufTen & 7) + 1;
677
- while (pos < buffer.length) {
678
- if (buffer[pos] === 33) {
679
- pos += 2;
680
- let subSize = buffer?.[pos];
681
- while (subSize && subSize > 0) {
682
- pos += subSize + 1;
683
- subSize = buffer?.[pos];
684
- }
685
- pos++;
686
- } else if (buffer?.[pos] && buffer?.[pos] === 44) {
687
- frames++;
688
- pos += 10;
689
- let b = buffer?.[pos - 1];
690
- if (typeof b !== "undefined" && b & 128)
691
- pos += 3 << (b & 7) + 1;
692
- pos++;
693
- let dataSize = buffer?.[pos];
694
- while (dataSize && dataSize > 0) {
695
- pos += dataSize + 1;
696
- dataSize = buffer[pos];
697
- }
698
- pos++;
699
- } else if (buffer[pos] === 59) {
700
- break;
701
- } else {
702
- pos++;
703
- }
704
- }
705
- return {
706
- width,
707
- height,
708
- format: "gif",
709
- frames,
710
- animated: frames > 1,
711
- hasAlpha: null,
712
- // GIF transparency is per-pixel binary, not full alpha; set null or true if transparent color exists, but complex
713
- orientation: null,
714
- // No orientation in GIF
715
- aspectRatio: width / height,
716
- colorSpace: "indexed",
717
- iccProfile: null,
718
- // No ICC in GIF
719
- exifDateTimeOriginal: null
720
- // No EXIF in GIF
721
- };
722
- }
723
- if (buffer.length >= 26 && buffer?.[0] === 66 && buffer?.[1] === 77) {
724
- const width = buffer.readInt32LE(18);
725
- const height = Math.abs(buffer.readInt32LE(22));
726
- const bitDepth = buffer.readUInt16LE(28);
727
- let colorSpace = bitDepth <= 8 ? "indexed" : "rgb";
728
- return {
729
- width,
730
- height,
731
- format: "bmp",
732
- frames: 1,
733
- animated: false,
734
- hasAlpha: null,
735
- // Some BMP have alpha in 32bpp, check if bitDepth===32 && compression===3 (bitfields with alpha)
736
- orientation: buffer.readInt32LE(22) < 0 ? 1 : 6,
737
- // Negative height means top-down (orientation 1), positive bottom-up (like 6, but simplified)
738
- aspectRatio: width / height,
739
- colorSpace,
740
- iccProfile: null,
741
- // Rare in BMP
742
- exifDateTimeOriginal: null
743
- };
744
- }
745
- if (buffer.length >= 30 && buffer.toString("ascii", 0, 4) === "RIFF" && buffer.toString("ascii", 8, 12) === "WEBP") {
746
- const chunkType = buffer.toString("ascii", 12, 16);
747
- let colorSpace = "unknown";
748
- let hasAlpha = false;
749
- let width = 0;
750
- let height = 0;
751
- let frames = 1;
752
- let animated = false;
753
- let iccProfile = null;
754
- if (chunkType === "VP8X") {
755
- const flags = buffer?.[20];
756
- colorSpace = flags ? flags & 2 ? "rgba" : "rgb" : "unknown";
757
- hasAlpha = flags ? !!(flags & 2) : false;
758
- animated = flags ? !!(flags & 1) : false;
759
- let widthSubtractOne = 0;
760
- let heightSubtractOne = 0;
761
- if (buffer?.[24] && buffer?.[25] && buffer?.[26]) {
762
- widthSubtractOne = buffer[24] | buffer[25] << 8 | buffer[26] << 16;
763
- }
764
- if (buffer?.[27] && buffer?.[28] && buffer?.[29]) {
765
- heightSubtractOne = buffer[27] | buffer[28] << 8 | buffer[29] << 16;
766
- }
767
- width = widthSubtractOne + 1;
768
- height = heightSubtractOne + 1;
769
- if (animated) {
770
- let pos = 30;
771
- frames = 0;
772
- while (pos < buffer.length - 8) {
773
- const subChunkType = buffer.toString("ascii", pos, pos + 4);
774
- const subChunkSize = buffer.readUInt32LE(pos + 4);
775
- if (subChunkType === "ICCP") {
776
- iccProfile = "embedded";
777
- } else if (subChunkType === "ANMF") {
778
- frames++;
779
- }
780
- pos += 8 + subChunkSize + subChunkSize % 2;
781
- }
782
- }
783
- } else if (chunkType === "VP8 ") {
784
- colorSpace = "rgb";
785
- hasAlpha = false;
786
- const dataStart = 20;
787
- if (buffer?.[dataStart + 3] !== 157 || buffer?.[dataStart + 4] !== 1 || buffer?.[dataStart + 5] !== 42) {
788
- throw new Error("Invalid VP8 keyframe");
789
- }
790
- width = buffer.readUInt16LE(dataStart + 6) & 16383;
791
- height = buffer.readUInt16LE(dataStart + 8) & 16383;
792
- } else if (chunkType === "VP8L") {
793
- const dataStart = 20;
794
- if (buffer?.[dataStart] !== 47) {
795
- throw new Error("Invalid VP8L signature");
796
- }
797
- const bits = buffer.readUInt32LE(dataStart + 1);
798
- if (bits >>> 29 !== 0) {
799
- throw new Error("Invalid VP8L version");
800
- }
801
- colorSpace = bits & 1 << 8 ? "rgba" : "rgb";
802
- hasAlpha = !!(bits & 1 << 8);
803
- width = 1 + (bits & 16383);
804
- height = 1 + (bits >> 14 & 16383);
805
- } else {
806
- throw new Error("Unsupported WebP chunk");
807
- }
808
- return {
809
- width,
810
- height,
811
- format: "webp",
812
- frames,
813
- animated,
814
- hasAlpha,
815
- orientation: null,
816
- // No standard orientation in WebP
817
- aspectRatio: width / height,
818
- colorSpace,
819
- iccProfile,
820
- exifDateTimeOriginal: null
821
- // Can have EXIF chunk, but rare; add if needed
822
- };
823
- }
824
- if (buffer.length >= 32 && buffer.toString("ascii", 4, 8) === "ftyp") {
825
- const ftyp = this.findBox(buffer, "ftyp");
826
- if (!ftyp) throw new Error("Invalid AVIF: No ftyp");
827
- const brands = buffer.toString("ascii", ftyp.pos, ftyp.pos + ftyp.size);
828
- const isAvif = brands.includes("avif");
829
- const isAvis = brands.includes("avis");
830
- if (!isAvif && !isAvis) throw new Error("Not an AVIF file");
831
- const meta = this.findBox(buffer, "meta");
832
- if (!meta) throw new Error("Invalid AVIF: No meta");
833
- const metaSubStart = meta.pos + 4;
834
- const metaSubEnd = meta.pos + meta.size;
835
- const iprp = this.findBox(buffer, "iprp", metaSubStart, metaSubEnd);
836
- if (!iprp) throw new Error("Invalid AVIF: No iprp");
837
- const ipco = this.findBox(buffer, "ipco", iprp.pos, iprp.pos + iprp.size);
838
- if (!ipco) throw new Error("Invalid AVIF: No ipco");
839
- const ispe = this.findBox(buffer, "ispe", ipco.pos, ipco.pos + ipco.size);
840
- if (!ispe) throw new Error("Invalid AVIF: No ispe");
841
- if (buffer[ispe.pos] !== 0) throw new Error("Invalid ispe version");
842
- const width = buffer.readUInt32BE(ispe.pos + 4);
843
- const height = buffer.readUInt32BE(ispe.pos + 8);
844
- let colorSpace = "rgb";
845
- let hasAlpha = false;
846
- let iccProfile = null;
847
- const colr = this.findBox(buffer, "colr", ipco.pos, ipco.pos + ipco.size);
848
- if (colr) {
849
- const colrType = buffer.toString("ascii", colr.pos, colr.pos + 4);
850
- if (colrType === "nclx") {
851
- const matrix = buffer.readUInt16BE(colr.pos + 6);
852
- colorSpace = matrix === 2 ? "rgb" : "ycbcr";
853
- } else if (colrType === "rICC" || colrType === "prof") {
854
- colorSpace = "unknown";
855
- iccProfile = "embedded";
856
- }
857
- }
858
- const auxC = this.findBox(buffer, "auxC", ipco.pos, ipco.pos + ipco.size);
859
- if (auxC && buffer.toString("ascii", auxC.pos, auxC.pos + auxC.size).includes("alpha")) {
860
- hasAlpha = true;
861
- if (colorSpace === "rgb") {
862
- colorSpace = "rgba";
863
- } else if (colorSpace === "ycbcr") {
864
- colorSpace = "ycck";
865
- } else if (colorSpace === "grayscale" || colorSpace === "unknown") {
866
- colorSpace = "grayscale-alpha";
867
- }
868
- }
869
- const xmpXml = this.parseXmpFromAvif(buffer, meta, ipco);
870
- let exifDateTimeOriginal = null;
871
- if (xmpXml) {
872
- let dateStart = xmpXml.indexOf('xmp:CreateDate="');
873
- if (dateStart === -1)
874
- dateStart = xmpXml.indexOf('photoshop:DateCreated="');
875
- if (dateStart !== -1) {
876
- dateStart += 16;
877
- const dateEnd = xmpXml.indexOf('"', dateStart);
878
- if (dateEnd !== -1) {
879
- exifDateTimeOriginal = xmpXml.substring(dateStart, dateEnd);
880
- }
881
- }
882
- }
883
- let frames = 1;
884
- let animated = isAvis;
885
- if (animated) {
886
- const iloc = this.findBox(buffer, "iloc", metaSubStart, metaSubEnd);
887
- if (iloc) {
888
- const version = buffer?.[iloc.pos] ?? 0;
889
- const itemCountPos = iloc.pos + (version < 2 ? 4 : 6);
890
- let itemCount = buffer?.readUInt16BE(itemCountPos) ?? 0;
891
- if (version >= 2) itemCount = buffer?.readUInt32BE(itemCountPos) ?? 0;
892
- frames = Math.max(1, Math.floor(itemCount / (hasAlpha ? 2 : 1)));
893
- }
894
- }
895
- return {
896
- width,
897
- height,
898
- format: "avif",
899
- frames,
900
- animated,
901
- hasAlpha: hasAlpha ? true : null,
902
- orientation: null,
903
- // Can have 'irot' or 'imir' transforms, but rare
904
- aspectRatio: width / height,
905
- colorSpace,
906
- iccProfile,
907
- exifDateTimeOriginal
908
- // Can have 'XMP' box, but parse XMP for date if needed
909
- };
910
- }
911
- throw new Error("Unsupported image format or invalid file");
912
- }
913
- };
914
-
915
- export {
916
- ImageService
917
- };
918
- //# sourceMappingURL=chunk-C2ANVZLG.js.map