@d0paminedriven/fs 7.0.1 → 7.2.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/dist/{chunk-QBG7TFHM.js → chunk-6FJJLFJL.js} +2 -2
- package/dist/{chunk-CN45SMBB.js → chunk-GH6MFTV5.js} +27 -12
- package/dist/chunk-KUGIFRTF.js +577 -0
- package/dist/{chunk-G27YIWW7.js → chunk-MRKO3PAA.js} +1 -1
- package/dist/{chunk-IUGBPDQJ.js → chunk-O2P3L3CJ.js} +1 -1
- package/dist/{chunk-CTA73JRX.js → chunk-SYISQ57E.js} +184 -427
- package/dist/fs/index.d.ts +204 -85
- package/dist/fs/index.js +6 -5
- package/dist/image/index.d.ts +88 -0
- package/dist/image/index.js +11 -0
- package/dist/index.d.ts +5 -1
- package/dist/index.js +9 -5
- package/dist/mime/index.d.ts +51 -21
- package/dist/mime/index.js +3 -3
- package/dist/types/index.d.ts +8 -8
- package/dist/types/index.js +1 -1
- package/dist/types/stream.d.ts +9 -0
- package/dist/types/stream.js +1 -0
- package/dist/url/index.js +2 -2
- package/dist/utils/index.js +1 -1
- package/package.json +11 -3
- package/dist/fs/index.cjs +0 -1630
- package/dist/fs/index.d.cts +0 -392
- package/dist/index.cjs +0 -1648
- package/dist/index.d.cts +0 -9
- package/dist/mime/index.cjs +0 -494
- package/dist/mime/index.d.cts +0 -472
- package/dist/types/index.cjs +0 -38
- package/dist/types/index.d.cts +0 -333
- package/dist/url/index.cjs +0 -181
- package/dist/url/index.d.cts +0 -33
- package/dist/utils/index.cjs +0 -157
- package/dist/utils/index.d.cts +0 -25
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
} from "./chunk-
|
|
2
|
+
ImageService
|
|
3
|
+
} from "./chunk-KUGIFRTF.js";
|
|
4
4
|
import {
|
|
5
5
|
unitsObj
|
|
6
|
-
} from "./chunk-
|
|
6
|
+
} from "./chunk-MRKO3PAA.js";
|
|
7
7
|
import {
|
|
8
8
|
__name
|
|
9
9
|
} from "./chunk-7QVYU63E.js";
|
|
@@ -13,12 +13,14 @@ import { execSync } from "child_process";
|
|
|
13
13
|
import fsSync from "fs";
|
|
14
14
|
import fsAsync from "fs/promises";
|
|
15
15
|
import { tmpdir } from "os";
|
|
16
|
+
import { Readable } from "stream";
|
|
16
17
|
import { join, relative, resolve } from "path";
|
|
18
|
+
import { ReadableStream as WebReadableStream } from "stream/web";
|
|
17
19
|
import * as dotenv from "dotenv";
|
|
18
20
|
import expand from "dotenv-expand";
|
|
19
21
|
import sharp from "sharp";
|
|
20
22
|
dotenv.config({ quiet: true });
|
|
21
|
-
var Fs = class extends
|
|
23
|
+
var Fs = class extends ImageService {
|
|
22
24
|
constructor(cwd) {
|
|
23
25
|
super();
|
|
24
26
|
this.cwd = cwd;
|
|
@@ -27,6 +29,122 @@ var Fs = class extends MimeService {
|
|
|
27
29
|
static {
|
|
28
30
|
__name(this, "Fs");
|
|
29
31
|
}
|
|
32
|
+
async getImageSpecs(filePath) {
|
|
33
|
+
const buffer = await this.fileToBufferAsync(filePath);
|
|
34
|
+
return this.getImageSpecsWorkup(buffer);
|
|
35
|
+
}
|
|
36
|
+
async getImageSpecsTmp(target) {
|
|
37
|
+
const buffer = await this.readTmpAsync(target);
|
|
38
|
+
return this.getImageSpecsWorkup(buffer);
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Async version of withWs with atomic write support and progress tracking
|
|
42
|
+
* @param path Target file path
|
|
43
|
+
* @param data Streamable data to write
|
|
44
|
+
* @param options Optional atomic write and abort signal
|
|
45
|
+
* @returns Promise with path and bytes written
|
|
46
|
+
* @example
|
|
47
|
+
* ```ts
|
|
48
|
+
* const fs = new Fs(process.cwd());
|
|
49
|
+
* // Simple async write
|
|
50
|
+
* const result = await fs.asyncWithWs("output.txt", "Hello World");
|
|
51
|
+
* console.log(`Wrote ${result.bytes} bytes to ${result.path}`);
|
|
52
|
+
*
|
|
53
|
+
* // Atomic write with temp file
|
|
54
|
+
* const { path, bytes } = await fs.asyncWithWs(
|
|
55
|
+
* "critical-data.json",
|
|
56
|
+
* JSON.stringify(data),
|
|
57
|
+
* { atomic: true }
|
|
58
|
+
* );
|
|
59
|
+
* ```
|
|
60
|
+
*/
|
|
61
|
+
async asyncWithWs(path, data, options) {
|
|
62
|
+
const { atomic = false, signal: _signal = AbortSignal } = options ?? {};
|
|
63
|
+
let bytes = 0;
|
|
64
|
+
if (!atomic) {
|
|
65
|
+
try {
|
|
66
|
+
if (/\//g.test(path)) {
|
|
67
|
+
return this.generateDirIfDNETmp(this.pathHandler(path), {
|
|
68
|
+
recursive: true
|
|
69
|
+
});
|
|
70
|
+
} else return path;
|
|
71
|
+
} catch (error) {
|
|
72
|
+
console.error(
|
|
73
|
+
`[asyncWithWs non-atomic error]: `.concat(
|
|
74
|
+
error instanceof Error ? error.message : typeof error === "string" ? error : JSON.stringify(error, null, 2)
|
|
75
|
+
)
|
|
76
|
+
);
|
|
77
|
+
} finally {
|
|
78
|
+
const readable = this.toReadable(data);
|
|
79
|
+
const chunks = [];
|
|
80
|
+
for await (const chunk of readable) {
|
|
81
|
+
chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
|
|
82
|
+
}
|
|
83
|
+
const buffer = Buffer.concat(chunks);
|
|
84
|
+
try {
|
|
85
|
+
this.withWs(path, buffer);
|
|
86
|
+
} catch (err) {
|
|
87
|
+
console.error(err);
|
|
88
|
+
} finally {
|
|
89
|
+
return { path, bytes: buffer.length };
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
let tmpName;
|
|
94
|
+
try {
|
|
95
|
+
if (/\//g.test(path)) {
|
|
96
|
+
this.generateDirIfDNETmp(this.pathHandler(path), {
|
|
97
|
+
recursive: true
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
tmpName = this.uniqueTmpName("atomic", "tmp");
|
|
101
|
+
} catch (error) {
|
|
102
|
+
console.error(
|
|
103
|
+
`[asyncWithWs atomic setup error]: `.concat(
|
|
104
|
+
error instanceof Error ? error.message : typeof error === "string" ? error : JSON.stringify(error, null, 2)
|
|
105
|
+
)
|
|
106
|
+
);
|
|
107
|
+
if (tmpName) {
|
|
108
|
+
try {
|
|
109
|
+
this.rmTmpFile(tmpName);
|
|
110
|
+
} catch (err) {
|
|
111
|
+
console.error(err);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
} finally {
|
|
115
|
+
if (tmpName) {
|
|
116
|
+
let newPath = null;
|
|
117
|
+
let newBytes = 0;
|
|
118
|
+
const readable = this.toReadable(data);
|
|
119
|
+
const chunks = [];
|
|
120
|
+
for await (const chunk of readable) {
|
|
121
|
+
chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
|
|
122
|
+
}
|
|
123
|
+
const buffer = Buffer.concat(chunks);
|
|
124
|
+
bytes = buffer.length;
|
|
125
|
+
try {
|
|
126
|
+
await this.writeTmpAsync(tmpName, buffer.toString());
|
|
127
|
+
const tmpPath = resolve(this.tmpDir, tmpName);
|
|
128
|
+
const finalPath = resolve(this.cwd, path);
|
|
129
|
+
await fsAsync.rename(tmpPath, finalPath);
|
|
130
|
+
newPath = path;
|
|
131
|
+
newBytes = bytes;
|
|
132
|
+
} catch (err) {
|
|
133
|
+
console.error(`[asyncWithWs atomic rename error]:`, err);
|
|
134
|
+
if (this.existsTmp(tmpName)) {
|
|
135
|
+
try {
|
|
136
|
+
this.rmTmpFile(tmpName);
|
|
137
|
+
} catch (err2) {
|
|
138
|
+
console.error(err2);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
throw err;
|
|
142
|
+
}
|
|
143
|
+
return { path, bytes, newPath, newBytes };
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
return { path, bytes: 0 };
|
|
147
|
+
}
|
|
30
148
|
get myEnv() {
|
|
31
149
|
return dotenv.config({ processEnv: {} });
|
|
32
150
|
}
|
|
@@ -39,6 +157,26 @@ var Fs = class extends MimeService {
|
|
|
39
157
|
parseDotEnv() {
|
|
40
158
|
return expand.expand(this.myEnv);
|
|
41
159
|
}
|
|
160
|
+
isUint8Array(v) {
|
|
161
|
+
return v instanceof Uint8Array;
|
|
162
|
+
}
|
|
163
|
+
isBuffer(v) {
|
|
164
|
+
return Buffer.isBuffer(v);
|
|
165
|
+
}
|
|
166
|
+
isWebReadableStream(v) {
|
|
167
|
+
return v instanceof WebReadableStream;
|
|
168
|
+
}
|
|
169
|
+
isAsyncIterable(v) {
|
|
170
|
+
return typeof v === "object" && v !== null && Symbol.asyncIterator in v;
|
|
171
|
+
}
|
|
172
|
+
toReadable(data) {
|
|
173
|
+
if (typeof data === "string") return Readable.from([Buffer.from(data)]);
|
|
174
|
+
if (this.isBuffer(data) || this.isUint8Array(data))
|
|
175
|
+
return Readable.from([data]);
|
|
176
|
+
if (this.isWebReadableStream(data)) return Readable.fromWeb(data);
|
|
177
|
+
if (this.isAsyncIterable(data)) return Readable.from(data);
|
|
178
|
+
throw new TypeError("Unsupported data type for toReadable()");
|
|
179
|
+
}
|
|
42
180
|
/**
|
|
43
181
|
*
|
|
44
182
|
* @param target path to the file or directory to check for existence
|
|
@@ -273,7 +411,7 @@ var Fs = class extends MimeService {
|
|
|
273
411
|
return await fsAsync.writeFile(
|
|
274
412
|
relative(this.cwd ?? process.cwd(), path),
|
|
275
413
|
Buffer.from(Buffer.from(data).toJSON().data),
|
|
276
|
-
options
|
|
414
|
+
typeof options === "object" ? { ...options } : options
|
|
277
415
|
);
|
|
278
416
|
}
|
|
279
417
|
}, "writeFileAsync");
|
|
@@ -403,37 +541,14 @@ var Fs = class extends MimeService {
|
|
|
403
541
|
return console.error(err);
|
|
404
542
|
}
|
|
405
543
|
}
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
const readUInt16 = littleEndian ? buffer.readUInt16LE.bind(buffer) : buffer.readUInt16BE.bind(buffer);
|
|
414
|
-
const readUInt32 = littleEndian ? buffer.readUInt32LE.bind(buffer) : buffer.readUInt32BE.bind(buffer);
|
|
415
|
-
if (readUInt16(tiffPos + 2) !== 42)
|
|
416
|
-
return { orientation: null, dateTimeOriginal: null };
|
|
417
|
-
let ifdOffset = readUInt32(tiffPos + 4);
|
|
418
|
-
tiffPos += ifdOffset;
|
|
419
|
-
const numEntries = readUInt16(tiffPos);
|
|
420
|
-
tiffPos += 2;
|
|
421
|
-
let orientation = null;
|
|
422
|
-
let dateTimeOriginal = null;
|
|
423
|
-
for (let i = 0; i < numEntries; i++) {
|
|
424
|
-
const tag = readUInt16(tiffPos);
|
|
425
|
-
const type = readUInt16(tiffPos + 2);
|
|
426
|
-
const count = readUInt32(tiffPos + 4);
|
|
427
|
-
const valueOffset = tiffPos + 8;
|
|
428
|
-
if (tag === 274 && type === 3 && count === 1) {
|
|
429
|
-
orientation = readUInt16(valueOffset);
|
|
430
|
-
} else if (tag === 36867 && type === 2 && count === 20) {
|
|
431
|
-
const offset = readUInt32(valueOffset);
|
|
432
|
-
dateTimeOriginal = buffer.toString("ascii", app1Pos + 2 + offset, app1Pos + 2 + offset + 19).trim();
|
|
433
|
-
}
|
|
434
|
-
tiffPos += 12;
|
|
544
|
+
knownToBlockHead(inputUrl) {
|
|
545
|
+
if (inputUrl.startsWith("https://github.com")) {
|
|
546
|
+
return "GET";
|
|
547
|
+
} else if (inputUrl.startsWith("https://raw.githubusercontent.com")) {
|
|
548
|
+
return "GET";
|
|
549
|
+
} else {
|
|
550
|
+
return "HEAD";
|
|
435
551
|
}
|
|
436
|
-
return { orientation, dateTimeOriginal };
|
|
437
552
|
}
|
|
438
553
|
/**
|
|
439
554
|
*
|
|
@@ -448,17 +563,17 @@ var Fs = class extends MimeService {
|
|
|
448
563
|
* ✅ 'public/assets/image-1.png'
|
|
449
564
|
*
|
|
450
565
|
* @description
|
|
451
|
-
* This method is designed for
|
|
452
|
-
*
|
|
566
|
+
* This method is designed for all files regardless of size, but especially for
|
|
567
|
+
* larger files that may not fit into memory (it intelligently determines the best approach on the fly)
|
|
568
|
+
* For files > 100 MB, it streams the data directly to disk instead of loading it all into memory
|
|
453
569
|
*/
|
|
454
570
|
async fetchRemoteWriteLocalLargeFiles(inputUrl, outputPathI, useDetectedExtension = true) {
|
|
571
|
+
if (!URL.canParse(inputUrl))
|
|
572
|
+
throw new Error(`invalid URL ${inputUrl} is unable to be parsed`);
|
|
455
573
|
const MAX_SAFE_IN_MEMORY_MB = 100;
|
|
456
574
|
try {
|
|
457
|
-
const isGithubRaw = inputUrl.startsWith(
|
|
458
|
-
"https://raw.githubusercontent.com"
|
|
459
|
-
);
|
|
460
575
|
const head = await fetch(inputUrl, {
|
|
461
|
-
method:
|
|
576
|
+
method: this.knownToBlockHead(inputUrl)
|
|
462
577
|
});
|
|
463
578
|
const contentLength = head.headers.get("content-length");
|
|
464
579
|
const fileSizeMb = contentLength ? parseInt(contentLength, 10) / (1024 * 1024) : null;
|
|
@@ -496,391 +611,6 @@ var Fs = class extends MimeService {
|
|
|
496
611
|
console.error(`[fetchRemoteWriteLocalLargeFiles error]:`, err);
|
|
497
612
|
}
|
|
498
613
|
}
|
|
499
|
-
findBox(buffer, type, start = 0, end = buffer.length) {
|
|
500
|
-
let pos = start;
|
|
501
|
-
while (pos < end) {
|
|
502
|
-
let boxSize = buffer.readUInt32BE(pos);
|
|
503
|
-
let boxType = buffer.toString("ascii", pos + 4, pos + 8);
|
|
504
|
-
let hdrSize = 8;
|
|
505
|
-
if (boxSize === 1) {
|
|
506
|
-
boxSize = Number(buffer.readBigUInt64BE(pos + 8));
|
|
507
|
-
hdrSize = 16;
|
|
508
|
-
} else if (boxSize === 0) {
|
|
509
|
-
boxSize = end - pos;
|
|
510
|
-
}
|
|
511
|
-
if (boxType === type) {
|
|
512
|
-
return { pos: pos + hdrSize, size: boxSize - hdrSize };
|
|
513
|
-
}
|
|
514
|
-
pos += boxSize;
|
|
515
|
-
}
|
|
516
|
-
return null;
|
|
517
|
-
}
|
|
518
|
-
async getImageSpecs(filePath) {
|
|
519
|
-
const buffer = await this.fileToBufferAsync(filePath);
|
|
520
|
-
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) {
|
|
521
|
-
if (buffer.readUint32BE(8) !== 13 || buffer?.[12] !== 73 || buffer?.[13] !== 72 || buffer?.[14] !== 68 || buffer?.[15] !== 82) {
|
|
522
|
-
throw new Error("IHDR Chunk of png not found or incorrect.");
|
|
523
|
-
}
|
|
524
|
-
const colorType = buffer[25];
|
|
525
|
-
let colorSpace;
|
|
526
|
-
let hasAlpha = false;
|
|
527
|
-
switch (colorType) {
|
|
528
|
-
case 0:
|
|
529
|
-
colorSpace = "grayscale";
|
|
530
|
-
break;
|
|
531
|
-
case 2:
|
|
532
|
-
colorSpace = "rgb";
|
|
533
|
-
break;
|
|
534
|
-
case 3:
|
|
535
|
-
colorSpace = "indexed";
|
|
536
|
-
break;
|
|
537
|
-
case 4:
|
|
538
|
-
colorSpace = "grayscale-alpha";
|
|
539
|
-
hasAlpha = true;
|
|
540
|
-
break;
|
|
541
|
-
case 6:
|
|
542
|
-
colorSpace = "rgba";
|
|
543
|
-
hasAlpha = true;
|
|
544
|
-
break;
|
|
545
|
-
default:
|
|
546
|
-
colorSpace = "unknown";
|
|
547
|
-
}
|
|
548
|
-
const width = buffer.readUInt32BE(16);
|
|
549
|
-
const height = buffer.readUInt32BE(20);
|
|
550
|
-
let frames = 1;
|
|
551
|
-
let animated = false;
|
|
552
|
-
let iccProfile = null;
|
|
553
|
-
let exifDateTimeOriginal = null;
|
|
554
|
-
let orientation = null;
|
|
555
|
-
let pos = 33;
|
|
556
|
-
while (pos < buffer.length - 12) {
|
|
557
|
-
const chunkLen = buffer.readUInt32BE(pos);
|
|
558
|
-
const chunkType = buffer.toString("ascii", pos + 4, pos + 8);
|
|
559
|
-
const chunkData = pos + 8;
|
|
560
|
-
if (chunkType === "acTL") {
|
|
561
|
-
animated = true;
|
|
562
|
-
frames = buffer.readUInt32BE(chunkData);
|
|
563
|
-
} else if (chunkType === "iCCP") {
|
|
564
|
-
const nameEnd = buffer.indexOf(0, chunkData);
|
|
565
|
-
const profileName = buffer.toString("ascii", chunkData, nameEnd);
|
|
566
|
-
iccProfile = profileName || "embedded";
|
|
567
|
-
} else if (chunkType === "tIME") {
|
|
568
|
-
const month = buffer?.[chunkData + 2], day = buffer?.[chunkData + 3], hour = buffer?.[chunkData + 4], minute = buffer?.[chunkData + 5], second = buffer?.[chunkData + 6];
|
|
569
|
-
if (typeof month !== "undefined" && typeof day !== "undefined" && typeof hour !== "undefined" && typeof minute !== "undefined" && typeof second !== "undefined") {
|
|
570
|
-
const year = buffer.readUInt16BE(chunkData);
|
|
571
|
-
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")}`;
|
|
572
|
-
}
|
|
573
|
-
} else if (chunkType === "IDAT") {
|
|
574
|
-
break;
|
|
575
|
-
}
|
|
576
|
-
pos += 12 + chunkLen;
|
|
577
|
-
}
|
|
578
|
-
return {
|
|
579
|
-
width,
|
|
580
|
-
height,
|
|
581
|
-
format: "png",
|
|
582
|
-
frames,
|
|
583
|
-
animated,
|
|
584
|
-
hasAlpha,
|
|
585
|
-
orientation,
|
|
586
|
-
aspectRatio: width / height,
|
|
587
|
-
colorSpace,
|
|
588
|
-
iccProfile,
|
|
589
|
-
exifDateTimeOriginal
|
|
590
|
-
};
|
|
591
|
-
}
|
|
592
|
-
if (buffer.length >= 10 && buffer[0] === 255 && buffer[1] === 216) {
|
|
593
|
-
let pos = 2, colorSpace = "unknown", hasAlpha = false, width = 0, height = 0, iccProfile = null, orientation = null, exifDateTimeOriginal = null;
|
|
594
|
-
while (pos < buffer.length - 10) {
|
|
595
|
-
if (buffer[pos] !== 255) {
|
|
596
|
-
throw new Error("Invalid JPEG file");
|
|
597
|
-
}
|
|
598
|
-
const marker = buffer[pos + 1];
|
|
599
|
-
if (marker === 218) break;
|
|
600
|
-
const segmentSize = buffer.readUInt16BE(pos + 2);
|
|
601
|
-
if (!marker) {
|
|
602
|
-
throw new Error("no marker for jpeg");
|
|
603
|
-
}
|
|
604
|
-
if (marker >= 192 && marker <= 207 && marker !== 196 && marker !== 200 && marker !== 204) {
|
|
605
|
-
const numComponents = buffer[pos + 4];
|
|
606
|
-
switch (numComponents) {
|
|
607
|
-
case 1:
|
|
608
|
-
colorSpace = "grayscale";
|
|
609
|
-
break;
|
|
610
|
-
case 3:
|
|
611
|
-
colorSpace = "ycbcr";
|
|
612
|
-
break;
|
|
613
|
-
// Most common for RGB JPEGs (stored as YCbCr)
|
|
614
|
-
case 4:
|
|
615
|
-
colorSpace = "ycck";
|
|
616
|
-
break;
|
|
617
|
-
// YCCK for CMYK with alpha-like, but no true alpha
|
|
618
|
-
default:
|
|
619
|
-
colorSpace = "unknown";
|
|
620
|
-
}
|
|
621
|
-
width = buffer.readUInt16BE(pos + 7);
|
|
622
|
-
height = buffer.readUInt16BE(pos + 5);
|
|
623
|
-
} else if (marker === 225) {
|
|
624
|
-
const { orientation: ori, dateTimeOriginal } = this.parseExif(
|
|
625
|
-
buffer,
|
|
626
|
-
pos,
|
|
627
|
-
segmentSize
|
|
628
|
-
);
|
|
629
|
-
orientation = ori;
|
|
630
|
-
exifDateTimeOriginal = dateTimeOriginal;
|
|
631
|
-
} else if (marker === 226) {
|
|
632
|
-
const iccHeader = buffer.toString("ascii", pos + 4, pos + 18);
|
|
633
|
-
if (iccHeader.startsWith("ICC_PROFILE")) {
|
|
634
|
-
iccProfile = "embedded";
|
|
635
|
-
}
|
|
636
|
-
}
|
|
637
|
-
pos += segmentSize + 2;
|
|
638
|
-
}
|
|
639
|
-
if (width === 0) throw new Error("No dimensions found in JPEG file");
|
|
640
|
-
return {
|
|
641
|
-
width,
|
|
642
|
-
height,
|
|
643
|
-
format: "jpeg",
|
|
644
|
-
frames: 1,
|
|
645
|
-
animated: false,
|
|
646
|
-
// JPEG not animated
|
|
647
|
-
hasAlpha,
|
|
648
|
-
orientation,
|
|
649
|
-
aspectRatio: width / height,
|
|
650
|
-
colorSpace,
|
|
651
|
-
iccProfile,
|
|
652
|
-
exifDateTimeOriginal
|
|
653
|
-
};
|
|
654
|
-
}
|
|
655
|
-
const gifHeader = buffer.toString("ascii", 0, 6);
|
|
656
|
-
if (buffer.length >= 10 && (gifHeader === "GIF87a" || gifHeader === "GIF89a")) {
|
|
657
|
-
const width = buffer.readUInt16LE(6), height = buffer.readUInt16LE(8);
|
|
658
|
-
let frames = 0;
|
|
659
|
-
let pos = 13;
|
|
660
|
-
let bufTen = buffer?.[10];
|
|
661
|
-
if (typeof bufTen === "undefined") {
|
|
662
|
-
pos = 13;
|
|
663
|
-
frames = 0;
|
|
664
|
-
} else if (bufTen & 128) pos += 3 << (bufTen & 7) + 1;
|
|
665
|
-
while (pos < buffer.length) {
|
|
666
|
-
if (buffer[pos] === 33) {
|
|
667
|
-
pos += 2;
|
|
668
|
-
let subSize = buffer?.[pos];
|
|
669
|
-
while (subSize && subSize > 0) {
|
|
670
|
-
pos += subSize + 1;
|
|
671
|
-
subSize = buffer?.[pos];
|
|
672
|
-
}
|
|
673
|
-
pos++;
|
|
674
|
-
} else if (buffer?.[pos] && buffer?.[pos] === 44) {
|
|
675
|
-
frames++;
|
|
676
|
-
pos += 10;
|
|
677
|
-
let b = buffer?.[pos - 1];
|
|
678
|
-
if (typeof b !== "undefined" && b & 128)
|
|
679
|
-
pos += 3 << (b & 7) + 1;
|
|
680
|
-
pos++;
|
|
681
|
-
let dataSize = buffer?.[pos];
|
|
682
|
-
while (dataSize && dataSize > 0) {
|
|
683
|
-
pos += dataSize + 1;
|
|
684
|
-
dataSize = buffer[pos];
|
|
685
|
-
}
|
|
686
|
-
pos++;
|
|
687
|
-
} else if (buffer[pos] === 59) {
|
|
688
|
-
break;
|
|
689
|
-
} else {
|
|
690
|
-
pos++;
|
|
691
|
-
}
|
|
692
|
-
}
|
|
693
|
-
return {
|
|
694
|
-
width,
|
|
695
|
-
height,
|
|
696
|
-
format: "gif",
|
|
697
|
-
frames,
|
|
698
|
-
animated: frames > 1,
|
|
699
|
-
hasAlpha: null,
|
|
700
|
-
// GIF transparency is per-pixel binary, not full alpha; set null or true if transparent color exists, but complex
|
|
701
|
-
orientation: null,
|
|
702
|
-
// No orientation in GIF
|
|
703
|
-
aspectRatio: width / height,
|
|
704
|
-
colorSpace: "indexed",
|
|
705
|
-
iccProfile: null,
|
|
706
|
-
// No ICC in GIF
|
|
707
|
-
exifDateTimeOriginal: null
|
|
708
|
-
// No EXIF in GIF
|
|
709
|
-
};
|
|
710
|
-
}
|
|
711
|
-
if (buffer.length >= 26 && buffer?.[0] === 66 && buffer?.[1] === 77) {
|
|
712
|
-
const width = buffer.readInt32LE(18);
|
|
713
|
-
const height = Math.abs(buffer.readInt32LE(22));
|
|
714
|
-
const bitDepth = buffer.readUInt16LE(28);
|
|
715
|
-
let colorSpace = bitDepth <= 8 ? "indexed" : "rgb";
|
|
716
|
-
return {
|
|
717
|
-
width,
|
|
718
|
-
height,
|
|
719
|
-
format: "bmp",
|
|
720
|
-
frames: 1,
|
|
721
|
-
animated: false,
|
|
722
|
-
hasAlpha: null,
|
|
723
|
-
// Some BMP have alpha in 32bpp, check if bitDepth===32 && compression===3 (bitfields with alpha)
|
|
724
|
-
orientation: buffer.readInt32LE(22) < 0 ? 1 : 6,
|
|
725
|
-
// Negative height means top-down (orientation 1), positive bottom-up (like 6, but simplified)
|
|
726
|
-
aspectRatio: width / height,
|
|
727
|
-
colorSpace,
|
|
728
|
-
iccProfile: null,
|
|
729
|
-
// Rare in BMP
|
|
730
|
-
exifDateTimeOriginal: null
|
|
731
|
-
};
|
|
732
|
-
}
|
|
733
|
-
if (buffer.length >= 30 && buffer.toString("ascii", 0, 4) === "RIFF" && buffer.toString("ascii", 8, 12) === "WEBP") {
|
|
734
|
-
const chunkType = buffer.toString("ascii", 12, 16);
|
|
735
|
-
let colorSpace = "unknown";
|
|
736
|
-
let hasAlpha = false;
|
|
737
|
-
let width = 0;
|
|
738
|
-
let height = 0;
|
|
739
|
-
let frames = 1;
|
|
740
|
-
let animated = false;
|
|
741
|
-
let iccProfile = null;
|
|
742
|
-
if (chunkType === "VP8X") {
|
|
743
|
-
const flags = buffer?.[20];
|
|
744
|
-
colorSpace = flags ? flags & 2 ? "rgba" : "rgb" : "unknown";
|
|
745
|
-
hasAlpha = flags ? !!(flags & 2) : false;
|
|
746
|
-
animated = flags ? !!(flags & 1) : false;
|
|
747
|
-
let widthSubtractOne = 0;
|
|
748
|
-
let heightSubtractOne = 0;
|
|
749
|
-
if (buffer?.[24] && buffer?.[25] && buffer?.[26]) {
|
|
750
|
-
widthSubtractOne = buffer[24] | buffer[25] << 8 | buffer[26] << 16;
|
|
751
|
-
}
|
|
752
|
-
if (buffer?.[27] && buffer?.[28] && buffer?.[29]) {
|
|
753
|
-
heightSubtractOne = buffer[27] | buffer[28] << 8 | buffer[29] << 16;
|
|
754
|
-
}
|
|
755
|
-
width = widthSubtractOne + 1;
|
|
756
|
-
height = heightSubtractOne + 1;
|
|
757
|
-
if (animated) {
|
|
758
|
-
let pos = 30;
|
|
759
|
-
frames = 0;
|
|
760
|
-
while (pos < buffer.length - 8) {
|
|
761
|
-
const subChunkType = buffer.toString("ascii", pos, pos + 4);
|
|
762
|
-
const subChunkSize = buffer.readUInt32LE(pos + 4);
|
|
763
|
-
if (subChunkType === "ICCP") {
|
|
764
|
-
iccProfile = "embedded";
|
|
765
|
-
} else if (subChunkType === "ANMF") {
|
|
766
|
-
frames++;
|
|
767
|
-
}
|
|
768
|
-
pos += 8 + subChunkSize + subChunkSize % 2;
|
|
769
|
-
}
|
|
770
|
-
}
|
|
771
|
-
} else if (chunkType === "VP8 ") {
|
|
772
|
-
colorSpace = "rgb";
|
|
773
|
-
hasAlpha = false;
|
|
774
|
-
const dataStart = 20;
|
|
775
|
-
if (buffer?.[dataStart + 3] !== 157 || buffer?.[dataStart + 4] !== 1 || buffer?.[dataStart + 5] !== 42) {
|
|
776
|
-
throw new Error("Invalid VP8 keyframe");
|
|
777
|
-
}
|
|
778
|
-
width = buffer.readUInt16LE(dataStart + 6) & 16383;
|
|
779
|
-
height = buffer.readUInt16LE(dataStart + 8) & 16383;
|
|
780
|
-
} else if (chunkType === "VP8L") {
|
|
781
|
-
const dataStart = 20;
|
|
782
|
-
if (buffer?.[dataStart] !== 47) {
|
|
783
|
-
throw new Error("Invalid VP8L signature");
|
|
784
|
-
}
|
|
785
|
-
const bits = buffer.readUInt32LE(dataStart + 1);
|
|
786
|
-
if (bits >>> 29 !== 0) {
|
|
787
|
-
throw new Error("Invalid VP8L version");
|
|
788
|
-
}
|
|
789
|
-
colorSpace = bits & 1 << 8 ? "rgba" : "rgb";
|
|
790
|
-
hasAlpha = !!(bits & 1 << 8);
|
|
791
|
-
width = 1 + (bits & 16383);
|
|
792
|
-
height = 1 + (bits >> 14 & 16383);
|
|
793
|
-
} else {
|
|
794
|
-
throw new Error("Unsupported WebP chunk");
|
|
795
|
-
}
|
|
796
|
-
return {
|
|
797
|
-
width,
|
|
798
|
-
height,
|
|
799
|
-
format: "webp",
|
|
800
|
-
frames,
|
|
801
|
-
animated,
|
|
802
|
-
hasAlpha,
|
|
803
|
-
orientation: null,
|
|
804
|
-
// No standard orientation in WebP
|
|
805
|
-
aspectRatio: width / height,
|
|
806
|
-
colorSpace,
|
|
807
|
-
iccProfile,
|
|
808
|
-
exifDateTimeOriginal: null
|
|
809
|
-
// Can have EXIF chunk, but rare; add if needed
|
|
810
|
-
};
|
|
811
|
-
}
|
|
812
|
-
if (buffer.length >= 32 && buffer.toString("ascii", 4, 8) === "ftyp") {
|
|
813
|
-
const ftyp = this.findBox(buffer, "ftyp");
|
|
814
|
-
if (!ftyp) throw new Error("Invalid AVIF: No ftyp");
|
|
815
|
-
const brands = buffer.toString("ascii", ftyp.pos, ftyp.pos + ftyp.size);
|
|
816
|
-
const isAvif = brands.includes("avif");
|
|
817
|
-
const isAvis = brands.includes("avis");
|
|
818
|
-
if (!isAvif && !isAvis) throw new Error("Not an AVIF file");
|
|
819
|
-
const meta = this.findBox(buffer, "meta");
|
|
820
|
-
if (!meta) throw new Error("Invalid AVIF: No meta");
|
|
821
|
-
const metaSubStart = meta.pos + 4;
|
|
822
|
-
const metaSubEnd = meta.pos + meta.size;
|
|
823
|
-
const iprp = this.findBox(buffer, "iprp", metaSubStart, metaSubEnd);
|
|
824
|
-
if (!iprp) throw new Error("Invalid AVIF: No iprp");
|
|
825
|
-
const ipco = this.findBox(buffer, "ipco", iprp.pos, iprp.pos + iprp.size);
|
|
826
|
-
if (!ipco) throw new Error("Invalid AVIF: No ipco");
|
|
827
|
-
const ispe = this.findBox(buffer, "ispe", ipco.pos, ipco.pos + ipco.size);
|
|
828
|
-
if (!ispe) throw new Error("Invalid AVIF: No ispe");
|
|
829
|
-
if (buffer[ispe.pos] !== 0) throw new Error("Invalid ispe version");
|
|
830
|
-
const width = buffer.readUInt32BE(ispe.pos + 4);
|
|
831
|
-
const height = buffer.readUInt32BE(ispe.pos + 8);
|
|
832
|
-
let colorSpace = "rgb";
|
|
833
|
-
let hasAlpha = false;
|
|
834
|
-
let iccProfile = null;
|
|
835
|
-
const colr = this.findBox(buffer, "colr", ipco.pos, ipco.pos + ipco.size);
|
|
836
|
-
if (colr) {
|
|
837
|
-
const colrType = buffer.toString("ascii", colr.pos, colr.pos + 4);
|
|
838
|
-
if (colrType === "nclx") {
|
|
839
|
-
const matrix = buffer.readUInt16BE(colr.pos + 6);
|
|
840
|
-
colorSpace = matrix === 2 ? "rgb" : "ycbcr";
|
|
841
|
-
} else if (colrType === "rICC" || colrType === "prof") {
|
|
842
|
-
colorSpace = "unknown";
|
|
843
|
-
iccProfile = "embedded";
|
|
844
|
-
}
|
|
845
|
-
}
|
|
846
|
-
const auxC = this.findBox(buffer, "auxC", ipco.pos, ipco.pos + ipco.size);
|
|
847
|
-
if (auxC && buffer.toString("ascii", auxC.pos, auxC.pos + auxC.size).includes("alpha")) {
|
|
848
|
-
hasAlpha = true;
|
|
849
|
-
if (colorSpace === "rgb" || colorSpace === "ycbcr") {
|
|
850
|
-
colorSpace = colorSpace === "rgb" ? "rgba" : "ycck";
|
|
851
|
-
} else if (colorSpace === "unknown") {
|
|
852
|
-
colorSpace = "grayscale-alpha";
|
|
853
|
-
}
|
|
854
|
-
}
|
|
855
|
-
let frames = 1;
|
|
856
|
-
let animated = isAvis;
|
|
857
|
-
if (animated) {
|
|
858
|
-
const iloc = this.findBox(buffer, "iloc", metaSubStart, metaSubEnd);
|
|
859
|
-
if (iloc) {
|
|
860
|
-
const version = buffer?.[iloc.pos];
|
|
861
|
-
const itemCountPos = iloc.pos + (version ? version < 2 ? 4 : 6 : 4);
|
|
862
|
-
frames = buffer.readUInt16BE(itemCountPos);
|
|
863
|
-
}
|
|
864
|
-
}
|
|
865
|
-
return {
|
|
866
|
-
width,
|
|
867
|
-
height,
|
|
868
|
-
format: "avif",
|
|
869
|
-
frames,
|
|
870
|
-
animated,
|
|
871
|
-
hasAlpha: hasAlpha ? true : null,
|
|
872
|
-
// null if unknown
|
|
873
|
-
orientation: null,
|
|
874
|
-
// Can have 'irot' or 'imir' transforms, but rare
|
|
875
|
-
aspectRatio: width / height,
|
|
876
|
-
colorSpace,
|
|
877
|
-
iccProfile,
|
|
878
|
-
exifDateTimeOriginal: null
|
|
879
|
-
// Can have 'XMP' box, but parse XMP for date if needed
|
|
880
|
-
};
|
|
881
|
-
}
|
|
882
|
-
throw new Error("Unsupported image format or invalid file");
|
|
883
|
-
}
|
|
884
614
|
/**
|
|
885
615
|
*
|
|
886
616
|
* @access package-private
|
|
@@ -907,6 +637,18 @@ var Fs = class extends MimeService {
|
|
|
907
637
|
return await sharp(target).toFormat(format, { quality }).toBuffer();
|
|
908
638
|
}
|
|
909
639
|
}
|
|
640
|
+
existsTmp(path) {
|
|
641
|
+
return this.exists(resolve(this.tmpDir, path));
|
|
642
|
+
}
|
|
643
|
+
mkdirTmp(path, options = { mode: 511, recursive: true }) {
|
|
644
|
+
return this.mkdirSync(resolve(this.tmpDir, path), options);
|
|
645
|
+
}
|
|
646
|
+
generateDirIfDNETmp(path, options) {
|
|
647
|
+
if (this.existsTmp(path)) return;
|
|
648
|
+
else {
|
|
649
|
+
return this.mkdirTmp(path, options);
|
|
650
|
+
}
|
|
651
|
+
}
|
|
910
652
|
/**
|
|
911
653
|
* Write data to a file in the system's tmp directory
|
|
912
654
|
* @param filename The filename (can include subdirectories like "img-probe/file.txt")
|
|
@@ -925,6 +667,21 @@ var Fs = class extends MimeService {
|
|
|
925
667
|
const fullPath = resolve(this.tmpDir, filename);
|
|
926
668
|
Promise.resolve(this.withWs(fullPath, data, options));
|
|
927
669
|
}
|
|
670
|
+
async writeTmpAsync(filename, data, options) {
|
|
671
|
+
const fullPath = resolve(this.tmpDir, filename);
|
|
672
|
+
return await this.writeFileAsync(
|
|
673
|
+
fullPath,
|
|
674
|
+
data,
|
|
675
|
+
typeof options === "object" ? { ...options, mode: 511 } : options
|
|
676
|
+
);
|
|
677
|
+
}
|
|
678
|
+
async readTmpAsync(filename) {
|
|
679
|
+
const fullPath = resolve(this.tmpDir, filename);
|
|
680
|
+
if (!this.existsTmp(filename)) {
|
|
681
|
+
throw new Error(`Tmp file not found: ${filename}`);
|
|
682
|
+
}
|
|
683
|
+
return await this.fileToBufferAsync(fullPath);
|
|
684
|
+
}
|
|
928
685
|
/**
|
|
929
686
|
* Read a file from the tmp directory
|
|
930
687
|
* @param filename The filename to read (can include subdirectories)
|
|
@@ -1135,4 +892,4 @@ var Fs = class extends MimeService {
|
|
|
1135
892
|
export {
|
|
1136
893
|
Fs
|
|
1137
894
|
};
|
|
1138
|
-
//# sourceMappingURL=chunk-
|
|
895
|
+
//# sourceMappingURL=chunk-SYISQ57E.js.map
|