@d0paminedriven/fs 7.3.0 → 7.5.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 +28 -5
- package/dist/{chunk-2XQ44ZWR.js → chunk-3MPIZJSN.js} +318 -72
- package/dist/{chunk-R63XI7FS.js → chunk-4DZLRFGH.js} +1 -1
- package/dist/{chunk-NHYRCNMW.js → chunk-6UANVLO3.js} +2 -2
- package/dist/{chunk-2IRCHZCF.js → chunk-CJBGTYSB.js} +3 -3
- package/dist/{chunk-LQP2FYMK.js → chunk-DSH3OL5Q.js} +5 -2
- package/dist/{chunk-NOOFSAF4.js → chunk-F57L5MBU.js} +3 -3
- package/dist/{chunk-KOKPGEU4.js → chunk-HQ2FHXLJ.js} +2 -2
- package/dist/{chunk-HPM4IQQR.js → chunk-LYQ3KQSL.js} +18 -10
- package/dist/{chunk-PTXZQBLV.js → chunk-N33N54TN.js} +2 -2
- package/dist/fs/index.d.ts +18 -83
- package/dist/fs/index.js +9 -9
- package/dist/fs-atomic/index.js +3 -3
- package/dist/fs-base/index.d.ts +1 -1
- package/dist/fs-base/index.js +2 -2
- package/dist/fs-core/index.js +6 -6
- package/dist/fs-fetch/index.d.ts +2 -2
- package/dist/fs-fetch/index.js +8 -8
- package/dist/fs-size/index.js +5 -5
- package/dist/fs-tmp/index.js +7 -7
- package/dist/image/index.d.ts +190 -14
- package/dist/image/index.js +1 -1
- package/dist/index.js +9 -9
- package/dist/mime/index.d.ts +2 -2
- package/dist/types/index.d.ts +3 -2
- package/dist/types/index.js +1 -1
- package/dist/types/stream.d.ts +2 -1
- package/package.json +4 -3
|
@@ -8,6 +8,7 @@ import {
|
|
|
8
8
|
// src/image/index.ts
|
|
9
9
|
import { createReadStream } from "fs";
|
|
10
10
|
import { relative } from "path";
|
|
11
|
+
import { inflateSync } from "fflate";
|
|
11
12
|
var ImageService = class extends MimeService {
|
|
12
13
|
constructor(cwd) {
|
|
13
14
|
super();
|
|
@@ -56,8 +57,8 @@ var ImageService = class extends MimeService {
|
|
|
56
57
|
* Extract image metadata from a stream, reading only ~4KB for most formats
|
|
57
58
|
* Automatically handles PNG, JPEG, GIF, BMP, WebP, and AVIF
|
|
58
59
|
*/
|
|
59
|
-
async extractFromStream(stream) {
|
|
60
|
-
const headerSize =
|
|
60
|
+
async extractFromStream(stream, size = 4096) {
|
|
61
|
+
const headerSize = size;
|
|
61
62
|
const header = await this.readBytes(stream, headerSize);
|
|
62
63
|
if (this.isPNG(header)) {
|
|
63
64
|
return this.extractPNGFromBuffer(header);
|
|
@@ -82,9 +83,11 @@ var ImageService = class extends MimeService {
|
|
|
82
83
|
/**
|
|
83
84
|
* Extract metadata from file path using streaming (memory efficient)
|
|
84
85
|
*/
|
|
85
|
-
async extractFromPath(filePath) {
|
|
86
|
-
const stream = createReadStream(relative(this.cwd, filePath), {
|
|
87
|
-
|
|
86
|
+
async extractFromPath(filePath, size = 4096) {
|
|
87
|
+
const stream = createReadStream(relative(this.cwd, filePath), {
|
|
88
|
+
highWaterMark: size
|
|
89
|
+
});
|
|
90
|
+
return this.extractFromStream(stream, size);
|
|
88
91
|
}
|
|
89
92
|
// Format detection helpers
|
|
90
93
|
isPNG(buffer) {
|
|
@@ -118,28 +121,28 @@ var ImageService = class extends MimeService {
|
|
|
118
121
|
const width = buffer.readUInt32BE(16);
|
|
119
122
|
const height = buffer.readUInt32BE(20);
|
|
120
123
|
const colorType = buffer[25];
|
|
121
|
-
let
|
|
124
|
+
let colorModel;
|
|
122
125
|
let hasAlpha = false;
|
|
123
126
|
switch (colorType) {
|
|
124
127
|
case 0:
|
|
125
|
-
|
|
128
|
+
colorModel = "grayscale";
|
|
126
129
|
break;
|
|
127
130
|
case 2:
|
|
128
|
-
|
|
131
|
+
colorModel = "rgb";
|
|
129
132
|
break;
|
|
130
133
|
case 3:
|
|
131
|
-
|
|
134
|
+
colorModel = "indexed";
|
|
132
135
|
break;
|
|
133
136
|
case 4:
|
|
134
|
-
|
|
137
|
+
colorModel = "grayscale-alpha";
|
|
135
138
|
hasAlpha = true;
|
|
136
139
|
break;
|
|
137
140
|
case 6:
|
|
138
|
-
|
|
141
|
+
colorModel = "rgba";
|
|
139
142
|
hasAlpha = true;
|
|
140
143
|
break;
|
|
141
144
|
default:
|
|
142
|
-
|
|
145
|
+
colorModel = "unknown";
|
|
143
146
|
}
|
|
144
147
|
return {
|
|
145
148
|
width,
|
|
@@ -150,7 +153,8 @@ var ImageService = class extends MimeService {
|
|
|
150
153
|
hasAlpha,
|
|
151
154
|
orientation: null,
|
|
152
155
|
aspectRatio: width / height,
|
|
153
|
-
colorSpace,
|
|
156
|
+
colorSpace: "unknown",
|
|
157
|
+
colorModel,
|
|
154
158
|
iccProfile: null,
|
|
155
159
|
exifDateTimeOriginal: null
|
|
156
160
|
};
|
|
@@ -162,7 +166,7 @@ var ImageService = class extends MimeService {
|
|
|
162
166
|
let pos = 2;
|
|
163
167
|
let width = 0;
|
|
164
168
|
let height = 0;
|
|
165
|
-
let
|
|
169
|
+
let colorModel = "unknown";
|
|
166
170
|
while (pos < buffer.length - 10) {
|
|
167
171
|
if (buffer[pos] !== 255) break;
|
|
168
172
|
const marker = buffer[pos + 1];
|
|
@@ -172,16 +176,16 @@ var ImageService = class extends MimeService {
|
|
|
172
176
|
const numComponents = buffer[pos + 4];
|
|
173
177
|
switch (numComponents) {
|
|
174
178
|
case 1:
|
|
175
|
-
|
|
179
|
+
colorModel = "grayscale";
|
|
176
180
|
break;
|
|
177
181
|
case 3:
|
|
178
|
-
|
|
182
|
+
colorModel = "ycbcr";
|
|
179
183
|
break;
|
|
180
184
|
case 4:
|
|
181
|
-
|
|
185
|
+
colorModel = "ycck";
|
|
182
186
|
break;
|
|
183
187
|
default:
|
|
184
|
-
|
|
188
|
+
colorModel = "unknown";
|
|
185
189
|
}
|
|
186
190
|
height = buffer.readUInt16BE(pos + 5);
|
|
187
191
|
width = buffer.readUInt16BE(pos + 7);
|
|
@@ -202,7 +206,8 @@ var ImageService = class extends MimeService {
|
|
|
202
206
|
hasAlpha: false,
|
|
203
207
|
orientation: null,
|
|
204
208
|
aspectRatio: width / height,
|
|
205
|
-
colorSpace,
|
|
209
|
+
colorSpace: "unknown",
|
|
210
|
+
colorModel,
|
|
206
211
|
iccProfile: null,
|
|
207
212
|
exifDateTimeOriginal: null
|
|
208
213
|
};
|
|
@@ -225,6 +230,7 @@ var ImageService = class extends MimeService {
|
|
|
225
230
|
return {
|
|
226
231
|
width,
|
|
227
232
|
height,
|
|
233
|
+
colorModel: "unknown",
|
|
228
234
|
format: "gif",
|
|
229
235
|
frames: potentiallyAnimated ? 2 : 1,
|
|
230
236
|
// Estimate
|
|
@@ -232,7 +238,7 @@ var ImageService = class extends MimeService {
|
|
|
232
238
|
hasAlpha: null,
|
|
233
239
|
orientation: null,
|
|
234
240
|
aspectRatio: width / height,
|
|
235
|
-
colorSpace: "
|
|
241
|
+
colorSpace: "unknown",
|
|
236
242
|
iccProfile: null,
|
|
237
243
|
exifDateTimeOriginal: null
|
|
238
244
|
};
|
|
@@ -244,7 +250,7 @@ var ImageService = class extends MimeService {
|
|
|
244
250
|
const width = buffer.readInt32LE(18);
|
|
245
251
|
const height = Math.abs(buffer.readInt32LE(22));
|
|
246
252
|
const bitDepth = buffer.readUInt16LE(28);
|
|
247
|
-
const
|
|
253
|
+
const colorModel = bitDepth <= 8 ? "indexed" : "rgb";
|
|
248
254
|
return {
|
|
249
255
|
width,
|
|
250
256
|
height,
|
|
@@ -254,7 +260,8 @@ var ImageService = class extends MimeService {
|
|
|
254
260
|
hasAlpha: null,
|
|
255
261
|
orientation: buffer.readInt32LE(22) < 0 ? 1 : 6,
|
|
256
262
|
aspectRatio: width / height,
|
|
257
|
-
colorSpace,
|
|
263
|
+
colorSpace: "unknown",
|
|
264
|
+
colorModel,
|
|
258
265
|
iccProfile: null,
|
|
259
266
|
exifDateTimeOriginal: null
|
|
260
267
|
};
|
|
@@ -268,13 +275,13 @@ var ImageService = class extends MimeService {
|
|
|
268
275
|
let height = 0;
|
|
269
276
|
let hasAlpha = false;
|
|
270
277
|
let animated = false;
|
|
271
|
-
let
|
|
278
|
+
let colorModel = "rgb";
|
|
272
279
|
if (chunkType === "VP8X") {
|
|
273
280
|
const flags = buffer[20];
|
|
274
281
|
if (flags !== void 0) {
|
|
275
282
|
hasAlpha = !!(flags & 2);
|
|
276
283
|
animated = !!(flags & 1);
|
|
277
|
-
|
|
284
|
+
colorModel = hasAlpha ? "rgba" : "rgb";
|
|
278
285
|
}
|
|
279
286
|
const b24 = buffer[24], b25 = buffer[25], b26 = buffer[26];
|
|
280
287
|
const b27 = buffer[27], b28 = buffer[28], b29 = buffer[29];
|
|
@@ -292,12 +299,13 @@ var ImageService = class extends MimeService {
|
|
|
292
299
|
if (buffer.length < 25) throw new Error("VP8L data too short");
|
|
293
300
|
const bits = buffer.readUInt32LE(21);
|
|
294
301
|
hasAlpha = !!(bits & 1 << 8);
|
|
295
|
-
|
|
302
|
+
colorModel = hasAlpha ? "rgba" : "rgb";
|
|
296
303
|
width = 1 + (bits & 16383);
|
|
297
304
|
height = 1 + (bits >> 14 & 16383);
|
|
298
305
|
}
|
|
299
306
|
return {
|
|
300
307
|
width,
|
|
308
|
+
colorModel,
|
|
301
309
|
height,
|
|
302
310
|
format: "webp",
|
|
303
311
|
frames: animated ? 2 : 1,
|
|
@@ -306,7 +314,7 @@ var ImageService = class extends MimeService {
|
|
|
306
314
|
hasAlpha,
|
|
307
315
|
orientation: null,
|
|
308
316
|
aspectRatio: width / height,
|
|
309
|
-
colorSpace,
|
|
317
|
+
colorSpace: "unknown",
|
|
310
318
|
iccProfile: null,
|
|
311
319
|
exifDateTimeOriginal: null
|
|
312
320
|
};
|
|
@@ -338,7 +346,8 @@ var ImageService = class extends MimeService {
|
|
|
338
346
|
hasAlpha: null,
|
|
339
347
|
orientation: null,
|
|
340
348
|
aspectRatio: width / height,
|
|
341
|
-
|
|
349
|
+
colorModel: "rgb",
|
|
350
|
+
colorSpace: "unknown",
|
|
342
351
|
iccProfile: null,
|
|
343
352
|
exifDateTimeOriginal: null
|
|
344
353
|
};
|
|
@@ -519,38 +528,227 @@ var ImageService = class extends MimeService {
|
|
|
519
528
|
}
|
|
520
529
|
return null;
|
|
521
530
|
}
|
|
531
|
+
mapChrmToColorSpace(chrm, fallback) {
|
|
532
|
+
const tol = 100;
|
|
533
|
+
if (Math.abs(chrm.white_x - 31270) < tol && Math.abs(chrm.white_y - 32900) < tol && Math.abs(chrm.red_x - 64e3) < tol && Math.abs(chrm.red_y - 33e3) < tol && Math.abs(chrm.green_x - 3e4) < tol && Math.abs(chrm.green_y - 6e4) < tol && Math.abs(chrm.blue_x - 15e3) < tol && Math.abs(chrm.blue_y - 6e3) < tol)
|
|
534
|
+
return "srgb";
|
|
535
|
+
if (Math.abs(chrm.white_x - 31270) < tol && Math.abs(chrm.white_y - 32900) < tol && Math.abs(chrm.red_x - 64e3) < tol && Math.abs(chrm.red_y - 33e3) < tol && Math.abs(chrm.green_x - 21e3) < tol && Math.abs(chrm.green_y - 71e3) < tol && Math.abs(chrm.blue_x - 15e3) < tol && Math.abs(chrm.blue_y - 6e3) < tol)
|
|
536
|
+
return "adobe_rgb";
|
|
537
|
+
if (Math.abs(chrm.white_x - 31270) < tol && Math.abs(chrm.white_y - 32900) < tol && Math.abs(chrm.red_x - 68e3) < tol && Math.abs(chrm.red_y - 32e3) < tol && Math.abs(chrm.green_x - 26500) < tol && Math.abs(chrm.green_y - 69e3) < tol && Math.abs(chrm.blue_x - 15e3) < tol && Math.abs(chrm.blue_y - 6e3) < tol)
|
|
538
|
+
return "display_p3";
|
|
539
|
+
if (Math.abs(chrm.white_x - 34590) < tol && Math.abs(chrm.white_y - 35850) < tol && // D50 white point
|
|
540
|
+
Math.abs(chrm.red_x - 73470) < tol && Math.abs(chrm.red_y - 26530) < tol && Math.abs(chrm.green_x - 15960) < tol && Math.abs(chrm.green_y - 85040) < tol && Math.abs(chrm.blue_x - 3600) < tol && Math.abs(chrm.blue_y - 1e3) < tol)
|
|
541
|
+
return "prophoto_rgb";
|
|
542
|
+
if (Math.abs(chrm.white_x - 31270) < tol && Math.abs(chrm.white_y - 32900) < tol && Math.abs(chrm.red_x - 70800) < tol && Math.abs(chrm.red_y - 29200) < tol && Math.abs(chrm.green_x - 17e3) < tol && Math.abs(chrm.green_y - 79700) < tol && Math.abs(chrm.blue_x - 13100) < tol && Math.abs(chrm.blue_y - 4600) < tol)
|
|
543
|
+
return "rec2020";
|
|
544
|
+
return fallback;
|
|
545
|
+
}
|
|
546
|
+
xmpToColorSpaceWorkup(space, target) {
|
|
547
|
+
return `<photoshop:${target}>${space}</photoshop:${target}>`;
|
|
548
|
+
}
|
|
549
|
+
mapXmpToColorSpace(xmpText, fallback) {
|
|
550
|
+
const lower = xmpText.toLowerCase();
|
|
551
|
+
if (lower.includes(this.xmpToColorSpaceWorkup("srgb", "iccprofile")))
|
|
552
|
+
return "srgb";
|
|
553
|
+
if (lower.includes(
|
|
554
|
+
this.xmpToColorSpaceWorkup("adobe rgb (1998)", "iccprofile")
|
|
555
|
+
))
|
|
556
|
+
return "adobe_rgb";
|
|
557
|
+
if (lower.includes(this.xmpToColorSpaceWorkup("display p3", "iccprofile")))
|
|
558
|
+
return "display_p3";
|
|
559
|
+
if (lower.includes(this.xmpToColorSpaceWorkup("prophoto rgb", "iccprofile")))
|
|
560
|
+
return "prophoto_rgb";
|
|
561
|
+
if (lower.includes(this.xmpToColorSpaceWorkup("rgb", "colormode")) && lower.includes("adobe rgb"))
|
|
562
|
+
return "adobe_rgb";
|
|
563
|
+
if (lower.includes(this.xmpToColorSpaceWorkup("grayscale", "colormode")))
|
|
564
|
+
return "gray";
|
|
565
|
+
return fallback;
|
|
566
|
+
}
|
|
567
|
+
mapProfileToColorSpace(profileName, fallback) {
|
|
568
|
+
if (!profileName || profileName === "embedded" || profileName.toLowerCase().includes("icc profile"))
|
|
569
|
+
return fallback;
|
|
570
|
+
const lower = profileName.toLowerCase();
|
|
571
|
+
if (lower.includes("srgb") || lower.includes("iec61966") || lower.includes("srgb2014"))
|
|
572
|
+
return "srgb";
|
|
573
|
+
if (lower.includes("display p3") || lower.includes("display-p3") || lower.includes("dci-p3") || lower.includes("p3"))
|
|
574
|
+
return "display_p3";
|
|
575
|
+
if (lower.includes("adobe rgb") || lower.includes("adobergb") || lower.includes("adobe rgb (1998)"))
|
|
576
|
+
return "adobe_rgb";
|
|
577
|
+
if (lower.includes("prophoto rgb") || lower.includes("prophoto") || lower.includes("romm rgb") || lower.includes("romm") || lower.includes("iso 22028"))
|
|
578
|
+
return "prophoto_rgb";
|
|
579
|
+
if (lower.includes("rec2020") || lower.includes("bt.2020") || lower.includes("bt2020") || lower.includes("bt.2100") || lower.includes("itur_2100"))
|
|
580
|
+
return "rec2020";
|
|
581
|
+
if (lower.includes("rec709") || lower.includes("bt.709") || lower.includes("bt709"))
|
|
582
|
+
return "rec709";
|
|
583
|
+
if (lower.includes("cmyk")) return "cmyk";
|
|
584
|
+
if (lower.includes("lab")) return "lab";
|
|
585
|
+
if (lower.includes("xyz")) return "xyz";
|
|
586
|
+
if (lower.includes("gray") || lower.includes("grey") || lower.includes("monochrome") || lower.includes("gamma") || lower.includes("gamma 2.2") || lower.includes("dot gain"))
|
|
587
|
+
return "gray";
|
|
588
|
+
return fallback;
|
|
589
|
+
}
|
|
590
|
+
async extractViaStream(stream, size = 4096) {
|
|
591
|
+
const headerSize = size;
|
|
592
|
+
const header = await this.readBytes(stream, headerSize);
|
|
593
|
+
return this.getImageSpecsWorkup(header, size);
|
|
594
|
+
}
|
|
595
|
+
async extractViaPath(filePath, size = 4096) {
|
|
596
|
+
const stream = createReadStream(relative(this.cwd, filePath), {
|
|
597
|
+
highWaterMark: size
|
|
598
|
+
});
|
|
599
|
+
return await this.extractViaStream(stream, size);
|
|
600
|
+
}
|
|
601
|
+
// Magic bytes for image format detection
|
|
602
|
+
signatures = {
|
|
603
|
+
PNG: [137, 80, 78, 71, 13, 10, 26, 10],
|
|
604
|
+
JPEG: [255, 216, 255],
|
|
605
|
+
GIF87a: [71, 73, 70, 56, 55, 97],
|
|
606
|
+
GIF89a: [71, 73, 70, 56, 57, 97],
|
|
607
|
+
WEBP: [82, 73, 70, 70],
|
|
608
|
+
// RIFF
|
|
609
|
+
BMP: [66, 77],
|
|
610
|
+
TIFF_LE: [73, 73, 42, 0],
|
|
611
|
+
TIFF_BE: [77, 77, 0, 42],
|
|
612
|
+
AVIF: [
|
|
613
|
+
0,
|
|
614
|
+
0,
|
|
615
|
+
0,
|
|
616
|
+
32,
|
|
617
|
+
102,
|
|
618
|
+
116,
|
|
619
|
+
121,
|
|
620
|
+
112,
|
|
621
|
+
97,
|
|
622
|
+
118,
|
|
623
|
+
105,
|
|
624
|
+
102
|
|
625
|
+
],
|
|
626
|
+
HEIC: [102, 116, 121, 112]
|
|
627
|
+
// ftyp at offset 4
|
|
628
|
+
};
|
|
522
629
|
/**
|
|
523
|
-
*
|
|
524
|
-
*
|
|
630
|
+
* Probe image from various sources
|
|
631
|
+
* Handles local Buffer, remote URL, or S3 object
|
|
525
632
|
*/
|
|
526
|
-
|
|
633
|
+
async extractRemote(source, size = 16384, timeout = 5e3) {
|
|
634
|
+
let buffer;
|
|
635
|
+
if (Buffer.isBuffer(source)) {
|
|
636
|
+
buffer = source;
|
|
637
|
+
return this.getImageSpecsWorkup(buffer, size);
|
|
638
|
+
} else {
|
|
639
|
+
buffer = await this.fetchMinimalBuffer(source, size, timeout);
|
|
640
|
+
return this.getImageSpecsWorkup(buffer, size);
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
/**
|
|
644
|
+
* Fetch minimal bytes from remote URL using Range header
|
|
645
|
+
* This is the KEY optimization - only fetch what we need!
|
|
646
|
+
*/
|
|
647
|
+
knownToBlock(inputUrl) {
|
|
648
|
+
if (inputUrl.startsWith("https://github.com")) {
|
|
649
|
+
return "GET";
|
|
650
|
+
} else if (inputUrl.startsWith("https://raw.githubusercontent.com")) {
|
|
651
|
+
return "GET";
|
|
652
|
+
} else {
|
|
653
|
+
return "HEAD";
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
async fetchMinimalBuffer(url, size = 16384, timeout = 5e3) {
|
|
657
|
+
const controller = new AbortController();
|
|
658
|
+
const timeoutId = setTimeout(() => controller.abort(), timeout);
|
|
659
|
+
try {
|
|
660
|
+
const headResponse = await fetch(url, {
|
|
661
|
+
method: this.knownToBlock(url),
|
|
662
|
+
signal: controller.signal
|
|
663
|
+
});
|
|
664
|
+
const acceptsRange = headResponse.headers.get("accept-ranges") === "bytes";
|
|
665
|
+
const contentLength = parseInt(
|
|
666
|
+
headResponse.headers.get("content-length") ?? "0"
|
|
667
|
+
);
|
|
668
|
+
clearTimeout(timeoutId);
|
|
669
|
+
if (acceptsRange) {
|
|
670
|
+
const rangeResponse = await fetch(url, {
|
|
671
|
+
headers: {
|
|
672
|
+
Range: `bytes=0-${size}`
|
|
673
|
+
// First 16KB
|
|
674
|
+
},
|
|
675
|
+
signal: AbortSignal.timeout(timeout)
|
|
676
|
+
});
|
|
677
|
+
if (rangeResponse.status === 206) {
|
|
678
|
+
const arrayBuffer = await rangeResponse.arrayBuffer();
|
|
679
|
+
return Buffer.from(arrayBuffer);
|
|
680
|
+
}
|
|
681
|
+
}
|
|
682
|
+
if (contentLength && contentLength < 1024 * 1024) {
|
|
683
|
+
const response2 = await fetch(url, {
|
|
684
|
+
signal: AbortSignal.timeout(timeout)
|
|
685
|
+
});
|
|
686
|
+
const arrayBuffer = await response2.arrayBuffer();
|
|
687
|
+
return Buffer.from(arrayBuffer);
|
|
688
|
+
}
|
|
689
|
+
const response = await fetch(url, {
|
|
690
|
+
signal: AbortSignal.timeout(timeout)
|
|
691
|
+
});
|
|
692
|
+
if (!response.body) {
|
|
693
|
+
throw new Error("No response body");
|
|
694
|
+
}
|
|
695
|
+
const reader = response.body.getReader();
|
|
696
|
+
const chunks = [];
|
|
697
|
+
let totalBytes = 0;
|
|
698
|
+
const maxBytes = size;
|
|
699
|
+
while (totalBytes < maxBytes) {
|
|
700
|
+
const { done, value } = await reader.read();
|
|
701
|
+
if (done) break;
|
|
702
|
+
const bytesToAdd = Math.min(value.length, maxBytes - totalBytes);
|
|
703
|
+
chunks.push(value.slice(0, bytesToAdd));
|
|
704
|
+
totalBytes += bytesToAdd;
|
|
705
|
+
if (totalBytes >= maxBytes) {
|
|
706
|
+
reader.cancel();
|
|
707
|
+
break;
|
|
708
|
+
}
|
|
709
|
+
}
|
|
710
|
+
return Buffer.concat(chunks);
|
|
711
|
+
} finally {
|
|
712
|
+
clearTimeout(timeoutId);
|
|
713
|
+
}
|
|
714
|
+
}
|
|
715
|
+
getImageSpecsWorkup(rawbuffer, size = 4096) {
|
|
716
|
+
const MAX_HEADER_SIZE = size;
|
|
717
|
+
const buffer = rawbuffer.subarray(
|
|
718
|
+
0,
|
|
719
|
+
Math.min(rawbuffer.length, MAX_HEADER_SIZE)
|
|
720
|
+
);
|
|
527
721
|
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) {
|
|
528
722
|
if (buffer.readUint32BE(8) !== 13 || buffer?.[12] !== 73 || buffer?.[13] !== 72 || buffer?.[14] !== 68 || buffer?.[15] !== 82) {
|
|
529
723
|
throw new Error("IHDR Chunk of png not found or incorrect.");
|
|
530
724
|
}
|
|
531
725
|
const colorType = buffer[25];
|
|
532
|
-
let colorSpace;
|
|
533
|
-
let hasAlpha = false;
|
|
726
|
+
let colorModel = "unknown", colorSpace = "unknown", hasAlpha = false;
|
|
534
727
|
switch (colorType) {
|
|
535
728
|
case 0:
|
|
536
|
-
|
|
729
|
+
colorModel = "grayscale";
|
|
730
|
+
colorSpace = "gray";
|
|
537
731
|
break;
|
|
538
732
|
case 2:
|
|
539
|
-
|
|
733
|
+
colorModel = "rgb";
|
|
734
|
+
colorSpace = "srgb";
|
|
540
735
|
break;
|
|
541
736
|
case 3:
|
|
542
|
-
|
|
737
|
+
colorModel = "indexed";
|
|
738
|
+
colorSpace = "srgb";
|
|
543
739
|
break;
|
|
544
740
|
case 4:
|
|
545
|
-
|
|
741
|
+
colorModel = "grayscale-alpha";
|
|
742
|
+
colorSpace = "gray";
|
|
546
743
|
hasAlpha = true;
|
|
547
744
|
break;
|
|
548
745
|
case 6:
|
|
549
|
-
|
|
746
|
+
colorModel = "rgba";
|
|
747
|
+
colorSpace = "srgb";
|
|
550
748
|
hasAlpha = true;
|
|
551
749
|
break;
|
|
552
750
|
default:
|
|
553
|
-
|
|
751
|
+
colorModel = "unknown";
|
|
554
752
|
}
|
|
555
753
|
const width = buffer.readUInt32BE(16);
|
|
556
754
|
const height = buffer.readUInt32BE(20);
|
|
@@ -562,8 +760,8 @@ var ImageService = class extends MimeService {
|
|
|
562
760
|
let pos = 33;
|
|
563
761
|
while (pos < buffer.length - 12) {
|
|
564
762
|
const chunkLen = buffer.readUInt32BE(pos);
|
|
565
|
-
|
|
566
|
-
|
|
763
|
+
let chunkType = buffer.toString("ascii", pos + 4, pos + 8);
|
|
764
|
+
let chunkData = pos + 8;
|
|
567
765
|
if (chunkType === "acTL") {
|
|
568
766
|
animated = true;
|
|
569
767
|
frames = buffer.readUInt32BE(chunkData);
|
|
@@ -571,10 +769,57 @@ var ImageService = class extends MimeService {
|
|
|
571
769
|
const nameEnd = buffer.indexOf(0, chunkData);
|
|
572
770
|
const profileName = buffer.toString("ascii", chunkData, nameEnd);
|
|
573
771
|
iccProfile = profileName || "embedded";
|
|
772
|
+
colorSpace = this.mapProfileToColorSpace(profileName, colorSpace);
|
|
773
|
+
} else if (chunkType === "sRGB") {
|
|
774
|
+
if (iccProfile === null) {
|
|
775
|
+
iccProfile = "sRGB";
|
|
776
|
+
colorSpace = "srgb";
|
|
777
|
+
}
|
|
778
|
+
} else if (chunkType === "cHRM") {
|
|
779
|
+
if (iccProfile === null && colorModel !== "grayscale" && colorModel !== "grayscale-alpha") {
|
|
780
|
+
const white_x = buffer.readUInt32BE(chunkData), white_y = buffer.readUInt32BE(chunkData + 4), red_x = buffer.readUInt32BE(chunkData + 8), red_y = buffer.readUInt32BE(chunkData + 12), green_x = buffer.readUInt32BE(chunkData + 16), green_y = buffer.readUInt32BE(chunkData + 20), blue_x = buffer.readUInt32BE(chunkData + 24), blue_y = buffer.readUInt32BE(chunkData + 28);
|
|
781
|
+
colorSpace = this.mapChrmToColorSpace(
|
|
782
|
+
{
|
|
783
|
+
white_x,
|
|
784
|
+
white_y,
|
|
785
|
+
red_x,
|
|
786
|
+
red_y,
|
|
787
|
+
green_x,
|
|
788
|
+
green_y,
|
|
789
|
+
blue_x,
|
|
790
|
+
blue_y
|
|
791
|
+
},
|
|
792
|
+
colorSpace
|
|
793
|
+
);
|
|
794
|
+
}
|
|
795
|
+
} else if (chunkType === "iTXt") {
|
|
796
|
+
let offset = chunkData;
|
|
797
|
+
const keywordEnd = buffer.indexOf(0, offset);
|
|
798
|
+
const keyword = buffer.toString("ascii", offset, keywordEnd);
|
|
799
|
+
offset = keywordEnd + 1;
|
|
800
|
+
const compressionFlag = buffer[offset];
|
|
801
|
+
const _compressionMethod = buffer[offset + 1];
|
|
802
|
+
offset += 2;
|
|
803
|
+
const langEnd = buffer.indexOf(0, offset);
|
|
804
|
+
offset = langEnd + 1;
|
|
805
|
+
const transEnd = buffer.indexOf(0, offset);
|
|
806
|
+
offset = transEnd + 1;
|
|
807
|
+
let textBuffer = buffer.subarray(offset, chunkData + chunkLen - 8);
|
|
808
|
+
if (compressionFlag === 1) {
|
|
809
|
+
try {
|
|
810
|
+
textBuffer = Buffer.from(inflateSync(textBuffer));
|
|
811
|
+
} catch (e) {
|
|
812
|
+
console.error("Failed to decompress iTXt:", e);
|
|
813
|
+
}
|
|
814
|
+
}
|
|
815
|
+
const text = textBuffer.toString("utf8");
|
|
816
|
+
if (keyword === "XML:com.adobe.xmp" && !iccProfile) {
|
|
817
|
+
colorSpace = this.mapXmpToColorSpace(text, colorSpace);
|
|
818
|
+
}
|
|
574
819
|
} else if (chunkType === "tIME") {
|
|
575
|
-
|
|
820
|
+
let month = buffer?.[chunkData + 2], day = buffer?.[chunkData + 3], hour = buffer?.[chunkData + 4], minute = buffer?.[chunkData + 5], second = buffer?.[chunkData + 6];
|
|
576
821
|
if (typeof month !== "undefined" && typeof day !== "undefined" && typeof hour !== "undefined" && typeof minute !== "undefined" && typeof second !== "undefined") {
|
|
577
|
-
|
|
822
|
+
let year = buffer.readUInt16BE(chunkData);
|
|
578
823
|
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")}`;
|
|
579
824
|
}
|
|
580
825
|
} else if (chunkType === "IDAT") {
|
|
@@ -585,10 +830,11 @@ var ImageService = class extends MimeService {
|
|
|
585
830
|
return {
|
|
586
831
|
width,
|
|
587
832
|
height,
|
|
588
|
-
format: "png",
|
|
833
|
+
format: animated === true ? "apng" : "png",
|
|
589
834
|
frames,
|
|
590
835
|
animated,
|
|
591
836
|
hasAlpha,
|
|
837
|
+
colorModel,
|
|
592
838
|
orientation,
|
|
593
839
|
aspectRatio: width / height,
|
|
594
840
|
colorSpace,
|
|
@@ -597,7 +843,7 @@ var ImageService = class extends MimeService {
|
|
|
597
843
|
};
|
|
598
844
|
}
|
|
599
845
|
if (buffer.length >= 10 && buffer[0] === 255 && buffer[1] === 216) {
|
|
600
|
-
let pos = 2, colorSpace = "unknown", hasAlpha = false, width = 0, height = 0, iccProfile = null, orientation = null, exifDateTimeOriginal = null;
|
|
846
|
+
let pos = 2, colorSpace = "unknown", colorModel = "unknown", hasAlpha = false, width = 0, height = 0, iccProfile = null, orientation = null, exifDateTimeOriginal = null;
|
|
601
847
|
while (pos < buffer.length - 10) {
|
|
602
848
|
if (buffer[pos] !== 255) {
|
|
603
849
|
throw new Error("Invalid JPEG file");
|
|
@@ -612,18 +858,18 @@ var ImageService = class extends MimeService {
|
|
|
612
858
|
const numComponents = buffer[pos + 4];
|
|
613
859
|
switch (numComponents) {
|
|
614
860
|
case 1:
|
|
615
|
-
|
|
861
|
+
colorModel = "grayscale";
|
|
616
862
|
break;
|
|
617
863
|
case 3:
|
|
618
|
-
|
|
864
|
+
colorModel = "ycbcr";
|
|
619
865
|
break;
|
|
620
866
|
// Most common for RGB JPEGs (stored as YCbCr)
|
|
621
867
|
case 4:
|
|
622
|
-
|
|
868
|
+
colorModel = "ycck";
|
|
623
869
|
break;
|
|
624
870
|
// YCCK for CMYK with alpha-like, but no true alpha
|
|
625
871
|
default:
|
|
626
|
-
|
|
872
|
+
colorModel = "unknown";
|
|
627
873
|
}
|
|
628
874
|
width = buffer.readUInt16BE(pos + 7);
|
|
629
875
|
height = buffer.readUInt16BE(pos + 5);
|
|
@@ -642,6 +888,9 @@ var ImageService = class extends MimeService {
|
|
|
642
888
|
}
|
|
643
889
|
}
|
|
644
890
|
pos += segmentSize + 2;
|
|
891
|
+
while (pos < buffer.length - 1 && buffer[pos] === 0) {
|
|
892
|
+
pos++;
|
|
893
|
+
}
|
|
645
894
|
}
|
|
646
895
|
if (width === 0) throw new Error("No dimensions found in JPEG file");
|
|
647
896
|
return {
|
|
@@ -655,6 +904,7 @@ var ImageService = class extends MimeService {
|
|
|
655
904
|
orientation,
|
|
656
905
|
aspectRatio: width / height,
|
|
657
906
|
colorSpace,
|
|
907
|
+
colorModel,
|
|
658
908
|
iccProfile,
|
|
659
909
|
exifDateTimeOriginal
|
|
660
910
|
};
|
|
@@ -708,7 +958,8 @@ var ImageService = class extends MimeService {
|
|
|
708
958
|
orientation: null,
|
|
709
959
|
// No orientation in GIF
|
|
710
960
|
aspectRatio: width / height,
|
|
711
|
-
|
|
961
|
+
colorModel: "indexed",
|
|
962
|
+
colorSpace: "unknown",
|
|
712
963
|
iccProfile: null,
|
|
713
964
|
// No ICC in GIF
|
|
714
965
|
exifDateTimeOriginal: null
|
|
@@ -719,7 +970,7 @@ var ImageService = class extends MimeService {
|
|
|
719
970
|
const width = buffer.readInt32LE(18);
|
|
720
971
|
const height = Math.abs(buffer.readInt32LE(22));
|
|
721
972
|
const bitDepth = buffer.readUInt16LE(28);
|
|
722
|
-
let
|
|
973
|
+
let colorModel = bitDepth <= 8 ? "indexed" : "rgb";
|
|
723
974
|
return {
|
|
724
975
|
width,
|
|
725
976
|
height,
|
|
@@ -731,7 +982,8 @@ var ImageService = class extends MimeService {
|
|
|
731
982
|
orientation: buffer.readInt32LE(22) < 0 ? 1 : 6,
|
|
732
983
|
// Negative height means top-down (orientation 1), positive bottom-up (like 6, but simplified)
|
|
733
984
|
aspectRatio: width / height,
|
|
734
|
-
|
|
985
|
+
colorModel,
|
|
986
|
+
colorSpace: "unknown",
|
|
735
987
|
iccProfile: null,
|
|
736
988
|
// Rare in BMP
|
|
737
989
|
exifDateTimeOriginal: null
|
|
@@ -739,16 +991,10 @@ var ImageService = class extends MimeService {
|
|
|
739
991
|
}
|
|
740
992
|
if (buffer.length >= 30 && buffer.toString("ascii", 0, 4) === "RIFF" && buffer.toString("ascii", 8, 12) === "WEBP") {
|
|
741
993
|
const chunkType = buffer.toString("ascii", 12, 16);
|
|
742
|
-
let colorSpace = "unknown";
|
|
743
|
-
let hasAlpha = false;
|
|
744
|
-
let width = 0;
|
|
745
|
-
let height = 0;
|
|
746
|
-
let frames = 1;
|
|
747
|
-
let animated = false;
|
|
748
|
-
let iccProfile = null;
|
|
994
|
+
let colorSpace = "unknown", colorModel = "unknown", hasAlpha = false, width = 0, height = 0, frames = 1, animated = false, iccProfile = null;
|
|
749
995
|
if (chunkType === "VP8X") {
|
|
750
996
|
const flags = buffer?.[20];
|
|
751
|
-
|
|
997
|
+
colorModel = flags ? flags & 2 ? "rgba" : "rgb" : "unknown";
|
|
752
998
|
hasAlpha = flags ? !!(flags & 2) : false;
|
|
753
999
|
animated = flags ? !!(flags & 1) : false;
|
|
754
1000
|
let widthSubtractOne = 0;
|
|
@@ -776,7 +1022,7 @@ var ImageService = class extends MimeService {
|
|
|
776
1022
|
}
|
|
777
1023
|
}
|
|
778
1024
|
} else if (chunkType === "VP8 ") {
|
|
779
|
-
|
|
1025
|
+
colorModel = "rgb";
|
|
780
1026
|
hasAlpha = false;
|
|
781
1027
|
const dataStart = 20;
|
|
782
1028
|
if (buffer?.[dataStart + 3] !== 157 || buffer?.[dataStart + 4] !== 1 || buffer?.[dataStart + 5] !== 42) {
|
|
@@ -793,7 +1039,7 @@ var ImageService = class extends MimeService {
|
|
|
793
1039
|
if (bits >>> 29 !== 0) {
|
|
794
1040
|
throw new Error("Invalid VP8L version");
|
|
795
1041
|
}
|
|
796
|
-
|
|
1042
|
+
colorModel = bits & 1 << 8 ? "rgba" : "rgb";
|
|
797
1043
|
hasAlpha = !!(bits & 1 << 8);
|
|
798
1044
|
width = 1 + (bits & 16383);
|
|
799
1045
|
height = 1 + (bits >> 14 & 16383);
|
|
@@ -807,6 +1053,7 @@ var ImageService = class extends MimeService {
|
|
|
807
1053
|
frames,
|
|
808
1054
|
animated,
|
|
809
1055
|
hasAlpha,
|
|
1056
|
+
colorModel,
|
|
810
1057
|
orientation: null,
|
|
811
1058
|
// No standard orientation in WebP
|
|
812
1059
|
aspectRatio: width / height,
|
|
@@ -836,15 +1083,13 @@ var ImageService = class extends MimeService {
|
|
|
836
1083
|
if (buffer[ispe.pos] !== 0) throw new Error("Invalid ispe version");
|
|
837
1084
|
const width = buffer.readUInt32BE(ispe.pos + 4);
|
|
838
1085
|
const height = buffer.readUInt32BE(ispe.pos + 8);
|
|
839
|
-
let colorSpace = "rgb";
|
|
840
|
-
let hasAlpha = false;
|
|
841
|
-
let iccProfile = null;
|
|
1086
|
+
let colorSpace = "rgb", colorModel = "rgb", hasAlpha = false, iccProfile = null;
|
|
842
1087
|
const colr = this.findBox(buffer, "colr", ipco.pos, ipco.pos + ipco.size);
|
|
843
1088
|
if (colr) {
|
|
844
1089
|
const colrType = buffer.toString("ascii", colr.pos, colr.pos + 4);
|
|
845
1090
|
if (colrType === "nclx") {
|
|
846
1091
|
const matrix = buffer.readUInt16BE(colr.pos + 6);
|
|
847
|
-
|
|
1092
|
+
colorModel = matrix === 2 ? "rgb" : "ycbcr";
|
|
848
1093
|
} else if (colrType === "rICC" || colrType === "prof") {
|
|
849
1094
|
colorSpace = "unknown";
|
|
850
1095
|
iccProfile = "embedded";
|
|
@@ -853,12 +1098,12 @@ var ImageService = class extends MimeService {
|
|
|
853
1098
|
const auxC = this.findBox(buffer, "auxC", ipco.pos, ipco.pos + ipco.size);
|
|
854
1099
|
if (auxC && buffer.toString("ascii", auxC.pos, auxC.pos + auxC.size).includes("alpha")) {
|
|
855
1100
|
hasAlpha = true;
|
|
856
|
-
if (
|
|
857
|
-
|
|
858
|
-
} else if (
|
|
859
|
-
|
|
860
|
-
} else if (
|
|
861
|
-
|
|
1101
|
+
if (colorModel === "rgb") {
|
|
1102
|
+
colorModel = "rgba";
|
|
1103
|
+
} else if (colorModel === "ycbcr") {
|
|
1104
|
+
colorModel = "ycck";
|
|
1105
|
+
} else if (colorModel === "grayscale" || colorModel === "unknown") {
|
|
1106
|
+
colorModel = "grayscale-alpha";
|
|
862
1107
|
}
|
|
863
1108
|
}
|
|
864
1109
|
const xmpXml = this.parseXmpFromAvif(buffer, meta, ipco);
|
|
@@ -893,6 +1138,7 @@ var ImageService = class extends MimeService {
|
|
|
893
1138
|
format: "avif",
|
|
894
1139
|
frames,
|
|
895
1140
|
animated,
|
|
1141
|
+
colorModel,
|
|
896
1142
|
hasAlpha: hasAlpha ? true : null,
|
|
897
1143
|
orientation: null,
|
|
898
1144
|
// Can have 'irot' or 'imir' transforms, but rare
|
|
@@ -910,4 +1156,4 @@ var ImageService = class extends MimeService {
|
|
|
910
1156
|
export {
|
|
911
1157
|
ImageService
|
|
912
1158
|
};
|
|
913
|
-
//# sourceMappingURL=chunk-
|
|
1159
|
+
//# sourceMappingURL=chunk-3MPIZJSN.js.map
|