@d0paminedriven/fs 6.0.1 → 6.3.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 +322 -64
- package/dist/chunk-C6AZRX7V.js +307 -0
- package/dist/{chunk-QMN5ZCE4.js → chunk-IUGBPDQJ.js} +21 -16
- package/dist/{chunk-4X5DVWVM.js → chunk-MXEE2WS7.js} +1 -1
- package/dist/{chunk-73BXX52B.js → chunk-NGBMZGGU.js} +361 -9
- package/dist/fs/index.cjs +506 -9
- package/dist/fs/index.d.cts +142 -4
- package/dist/fs/index.d.ts +142 -4
- package/dist/fs/index.js +3 -3
- package/dist/index.cjs +528 -24
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +7 -5
- package/dist/mime/index.cjs +152 -5
- package/dist/mime/index.d.cts +243 -11
- package/dist/mime/index.d.ts +243 -11
- package/dist/mime/index.js +5 -3
- package/dist/types/index.d.cts +93 -31
- package/dist/types/index.d.ts +93 -31
- package/dist/types/index.js +1 -1
- package/dist/utils/index.cjs +20 -15
- package/dist/utils/index.d.cts +2 -6
- package/dist/utils/index.d.ts +2 -6
- package/dist/utils/index.js +1 -1
- package/package.json +14 -17
- package/dist/chunk-EHOBCDHE.js +0 -161
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import {
|
|
2
2
|
MimeService
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-C6AZRX7V.js";
|
|
4
4
|
import {
|
|
5
5
|
unitsObj
|
|
6
|
-
} from "./chunk-
|
|
6
|
+
} from "./chunk-MXEE2WS7.js";
|
|
7
7
|
import {
|
|
8
8
|
__name
|
|
9
9
|
} from "./chunk-7QVYU63E.js";
|
|
@@ -12,6 +12,7 @@ import {
|
|
|
12
12
|
import { execSync } from "child_process";
|
|
13
13
|
import fsSync from "fs";
|
|
14
14
|
import fsAsync from "fs/promises";
|
|
15
|
+
import { tmpdir } from "os";
|
|
15
16
|
import { join, relative, resolve } from "path";
|
|
16
17
|
import * as dotenv from "dotenv";
|
|
17
18
|
import expand from "dotenv-expand";
|
|
@@ -21,6 +22,7 @@ var Fs = class extends MimeService {
|
|
|
21
22
|
constructor(cwd) {
|
|
22
23
|
super();
|
|
23
24
|
this.cwd = cwd;
|
|
25
|
+
cwd ??= process.cwd();
|
|
24
26
|
}
|
|
25
27
|
static {
|
|
26
28
|
__name(this, "Fs");
|
|
@@ -28,6 +30,12 @@ var Fs = class extends MimeService {
|
|
|
28
30
|
get myEnv() {
|
|
29
31
|
return dotenv.config({ processEnv: {} });
|
|
30
32
|
}
|
|
33
|
+
get tmpDir() {
|
|
34
|
+
return tmpdir();
|
|
35
|
+
}
|
|
36
|
+
get tmpDirRelative() {
|
|
37
|
+
return relative(this.cwd, tmpdir());
|
|
38
|
+
}
|
|
31
39
|
parseDotEnv() {
|
|
32
40
|
return expand.expand(this.myEnv);
|
|
33
41
|
}
|
|
@@ -141,7 +149,11 @@ var Fs = class extends MimeService {
|
|
|
141
149
|
*
|
|
142
150
|
* @returns an array of strings representing the contents of the directory
|
|
143
151
|
*/
|
|
144
|
-
readDir(path, options = {
|
|
152
|
+
readDir(path, options = {
|
|
153
|
+
recursive: true,
|
|
154
|
+
withFileTypes: false,
|
|
155
|
+
encoding: "utf-8"
|
|
156
|
+
}) {
|
|
145
157
|
if (this.isRootPathTargeted(path)) {
|
|
146
158
|
return this.handleBuffStrArrUnion(
|
|
147
159
|
fsSync.readdirSync(resolve(join(this.cwd, "./")), { ...options })
|
|
@@ -302,6 +314,9 @@ var Fs = class extends MimeService {
|
|
|
302
314
|
fileToBuffer = /* @__PURE__ */ __name((path) => Buffer.from(
|
|
303
315
|
fsSync.readFileSync(relative(this.cwd ?? process.cwd(), path)).toJSON().data
|
|
304
316
|
), "fileToBuffer");
|
|
317
|
+
fileToBufferAsync = /* @__PURE__ */ __name(async (path) => {
|
|
318
|
+
return await fsAsync.readFile(relative(this.cwd, path));
|
|
319
|
+
}, "fileToBufferAsync");
|
|
305
320
|
dirContainsDir(readDir, targetDir, options) {
|
|
306
321
|
return this.readDir(readDir, options).filter((t) => t.split(".").length === 1).includes(targetDir);
|
|
307
322
|
}
|
|
@@ -318,10 +333,11 @@ var Fs = class extends MimeService {
|
|
|
318
333
|
};
|
|
319
334
|
}
|
|
320
335
|
async assetToBufferView(path) {
|
|
321
|
-
const response = await fetch(path);
|
|
336
|
+
const response = await fetch(path, { method: "GET" });
|
|
322
337
|
if (!response.ok) {
|
|
323
338
|
throw new Error(`Failed to fetch asset: ${response.statusText}`);
|
|
324
339
|
}
|
|
340
|
+
console.log(Object.fromEntries(Array.from(response.headers.entries())));
|
|
325
341
|
const reader = response.body?.getReader();
|
|
326
342
|
if (reader) {
|
|
327
343
|
const chunks = Array.of();
|
|
@@ -388,13 +404,37 @@ var Fs = class extends MimeService {
|
|
|
388
404
|
return console.error(err);
|
|
389
405
|
}
|
|
390
406
|
}
|
|
391
|
-
|
|
407
|
+
/**
|
|
408
|
+
*
|
|
409
|
+
* @param inputUrl remote url to fetch data from
|
|
410
|
+
* @param outputPath desired output path relative to the cwd
|
|
411
|
+
* @param useDetectedExtension optional, defaults to true
|
|
412
|
+
*
|
|
413
|
+
* if `useDetectedExtension` is false you must include the file extension in your output path →
|
|
414
|
+
*
|
|
415
|
+
* 🚫 'public/assets/image-1'
|
|
416
|
+
*
|
|
417
|
+
* ✅ 'public/assets/image-1.png'
|
|
418
|
+
*
|
|
419
|
+
* @description
|
|
420
|
+
* This method is designed for large files that may not fit into memory.
|
|
421
|
+
* It streams the data directly to disk instead of loading it all into memory.
|
|
422
|
+
*/
|
|
423
|
+
async fetchRemoteWriteLocalLargeFiles(inputUrl, outputPathI, useDetectedExtension = true) {
|
|
392
424
|
const MAX_SAFE_IN_MEMORY_MB = 100;
|
|
393
425
|
try {
|
|
394
|
-
const
|
|
426
|
+
const isGithubRaw = inputUrl.startsWith(
|
|
427
|
+
"https://raw.githubusercontent.com"
|
|
428
|
+
);
|
|
429
|
+
const head = await fetch(inputUrl, {
|
|
430
|
+
method: isGithubRaw ? "GET" : "HEAD"
|
|
431
|
+
});
|
|
395
432
|
const contentLength = head.headers.get("content-length");
|
|
396
433
|
const fileSizeMb = contentLength ? parseInt(contentLength, 10) / (1024 * 1024) : null;
|
|
397
|
-
|
|
434
|
+
let outputPath = outputPathI;
|
|
435
|
+
const assetTypeViaUrl = this.assetType(inputUrl);
|
|
436
|
+
const contentType = head.headers.get("content-type") ?? "application/octet-stream";
|
|
437
|
+
const formattedPath = useDetectedExtension ? `${outputPath}.${typeof assetTypeViaUrl === "undefined" ? this.toExtObj[contentType][0] : assetTypeViaUrl}` : outputPath;
|
|
398
438
|
this.generateDirIfDNE(this.pathHandler(formattedPath), {
|
|
399
439
|
recursive: true
|
|
400
440
|
});
|
|
@@ -425,6 +465,149 @@ var Fs = class extends MimeService {
|
|
|
425
465
|
console.error(`[fetchRemoteWriteLocalLargeFiles error]:`, err);
|
|
426
466
|
}
|
|
427
467
|
}
|
|
468
|
+
findBox(buffer, type, start = 0, end = buffer.length) {
|
|
469
|
+
let pos = start;
|
|
470
|
+
while (pos < end) {
|
|
471
|
+
let boxSize = buffer.readUInt32BE(pos);
|
|
472
|
+
let boxType = buffer.toString("ascii", pos + 4, pos + 8);
|
|
473
|
+
let hdrSize = 8;
|
|
474
|
+
if (boxSize === 1) {
|
|
475
|
+
boxSize = Number(buffer.readBigUInt64BE(pos + 8));
|
|
476
|
+
hdrSize = 16;
|
|
477
|
+
} else if (boxSize === 0) {
|
|
478
|
+
boxSize = end - pos;
|
|
479
|
+
}
|
|
480
|
+
if (boxType === type) {
|
|
481
|
+
return { pos: pos + hdrSize, size: boxSize - hdrSize };
|
|
482
|
+
}
|
|
483
|
+
pos += boxSize;
|
|
484
|
+
}
|
|
485
|
+
return null;
|
|
486
|
+
}
|
|
487
|
+
async getImageSize(filePath) {
|
|
488
|
+
const buffer = await this.fileToBufferAsync(filePath);
|
|
489
|
+
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) {
|
|
490
|
+
if (buffer.readUint32BE(8) !== 13 || buffer?.[12] !== 73 || buffer?.[13] !== 72 || buffer?.[14] !== 68 || buffer?.[15] !== 82) {
|
|
491
|
+
throw new Error("IHDR Chunk of png not found or incorrect.");
|
|
492
|
+
}
|
|
493
|
+
return {
|
|
494
|
+
width: buffer.readUInt32BE(16),
|
|
495
|
+
height: buffer.readUInt32BE(20),
|
|
496
|
+
type: "png"
|
|
497
|
+
};
|
|
498
|
+
}
|
|
499
|
+
if (buffer.length >= 10 && buffer[0] === 255 && buffer[1] === 216) {
|
|
500
|
+
let pos = 2;
|
|
501
|
+
while (pos < buffer.length - 10) {
|
|
502
|
+
if (buffer[pos] !== 255) {
|
|
503
|
+
throw new Error("Invalid JPEG file");
|
|
504
|
+
}
|
|
505
|
+
const marker = buffer[pos + 1];
|
|
506
|
+
if (marker === 218) break;
|
|
507
|
+
const segmentSize = buffer.readUInt16BE(pos + 2);
|
|
508
|
+
if (!marker) {
|
|
509
|
+
throw new Error("no marker for jpeg");
|
|
510
|
+
}
|
|
511
|
+
if (marker >= 192 && marker <= 207 && marker !== 196 && marker !== 200 && marker !== 204) {
|
|
512
|
+
return {
|
|
513
|
+
width: buffer.readUInt16BE(pos + 7),
|
|
514
|
+
height: buffer.readUInt16BE(pos + 5),
|
|
515
|
+
type: "jpeg"
|
|
516
|
+
};
|
|
517
|
+
}
|
|
518
|
+
pos += segmentSize + 2;
|
|
519
|
+
}
|
|
520
|
+
throw new Error("No dimensions found in JPEG file");
|
|
521
|
+
}
|
|
522
|
+
const gifHeader = buffer.toString("ascii", 0, 6);
|
|
523
|
+
if (buffer.length >= 10 && (gifHeader === "GIF87a" || gifHeader === "GIF89a")) {
|
|
524
|
+
return {
|
|
525
|
+
width: buffer.readUInt16LE(6),
|
|
526
|
+
height: buffer.readUInt16LE(8),
|
|
527
|
+
type: "gif"
|
|
528
|
+
};
|
|
529
|
+
}
|
|
530
|
+
if (buffer.length >= 26 && buffer[0] === 66 && buffer[1] === 77) {
|
|
531
|
+
return {
|
|
532
|
+
width: buffer.readInt32LE(18),
|
|
533
|
+
height: Math.abs(buffer.readInt32LE(22)),
|
|
534
|
+
type: "bmp"
|
|
535
|
+
};
|
|
536
|
+
}
|
|
537
|
+
if (buffer.length >= 30 && buffer.toString("ascii", 0, 4) === "RIFF" && buffer.toString("ascii", 8, 12) === "WEBP") {
|
|
538
|
+
const chunkType = buffer.toString("ascii", 12, 16);
|
|
539
|
+
if (chunkType === "VP8X") {
|
|
540
|
+
let widthSubtractOne = 0;
|
|
541
|
+
let heightSubtractOne = 0;
|
|
542
|
+
if (buffer?.[24] && buffer?.[25] && buffer?.[26]) {
|
|
543
|
+
widthSubtractOne = buffer[24] | buffer[25] << 8 | buffer[26] << 16;
|
|
544
|
+
}
|
|
545
|
+
if (buffer?.[27] && buffer?.[28] && buffer?.[29]) {
|
|
546
|
+
heightSubtractOne = buffer[27] | buffer[28] << 8 | buffer[29] << 16;
|
|
547
|
+
}
|
|
548
|
+
return {
|
|
549
|
+
width: widthSubtractOne + 1,
|
|
550
|
+
height: heightSubtractOne + 1,
|
|
551
|
+
type: "webp"
|
|
552
|
+
};
|
|
553
|
+
} else if (chunkType === "VP8 ") {
|
|
554
|
+
const dataStart = 20;
|
|
555
|
+
if (buffer[dataStart + 3] !== 157 || buffer[dataStart + 4] !== 1 || buffer[dataStart + 5] !== 42) {
|
|
556
|
+
throw new Error("Invalid VP8 keyframe");
|
|
557
|
+
}
|
|
558
|
+
const width = buffer.readUInt16LE(dataStart + 6) & 16383;
|
|
559
|
+
const height = buffer.readUInt16LE(dataStart + 8) & 16383;
|
|
560
|
+
return {
|
|
561
|
+
width,
|
|
562
|
+
height,
|
|
563
|
+
type: "webp"
|
|
564
|
+
};
|
|
565
|
+
} else if (chunkType === "VP8L") {
|
|
566
|
+
const dataStart = 20;
|
|
567
|
+
if (buffer[dataStart] !== 47) {
|
|
568
|
+
throw new Error("Invalid VP8L signature");
|
|
569
|
+
}
|
|
570
|
+
const bits = buffer.readUInt32LE(dataStart + 1);
|
|
571
|
+
if (bits >>> 29 !== 0) {
|
|
572
|
+
throw new Error("Invalid VP8L version");
|
|
573
|
+
}
|
|
574
|
+
const width = 1 + (bits & 16383);
|
|
575
|
+
const height = 1 + (bits >> 14 & 16383);
|
|
576
|
+
return {
|
|
577
|
+
width,
|
|
578
|
+
height,
|
|
579
|
+
type: "webp"
|
|
580
|
+
};
|
|
581
|
+
}
|
|
582
|
+
throw new Error("Unsupported WebP chunk");
|
|
583
|
+
}
|
|
584
|
+
if (buffer.length >= 32 && buffer.toString("ascii", 4, 8) === "ftyp") {
|
|
585
|
+
const ftyp = this.findBox(buffer, "ftyp");
|
|
586
|
+
if (!ftyp) throw new Error("Invalid AVIF: No ftyp");
|
|
587
|
+
const brands = buffer.toString("ascii", ftyp.pos, ftyp.pos + ftyp.size);
|
|
588
|
+
if (!brands.includes("avif") && !brands.includes("avis"))
|
|
589
|
+
throw new Error("Not an AVIF file");
|
|
590
|
+
const meta = this.findBox(buffer, "meta");
|
|
591
|
+
if (!meta) throw new Error("Invalid AVIF: No meta");
|
|
592
|
+
const metaSubStart = meta.pos + 4;
|
|
593
|
+
const metaSubEnd = meta.pos + meta.size;
|
|
594
|
+
const iprp = this.findBox(buffer, "iprp", metaSubStart, metaSubEnd);
|
|
595
|
+
if (!iprp) throw new Error("Invalid AVIF: No iprp");
|
|
596
|
+
const ipco = this.findBox(buffer, "ipco", iprp.pos, iprp.pos + iprp.size);
|
|
597
|
+
if (!ipco) throw new Error("Invalid AVIF: No ipco");
|
|
598
|
+
const ispe = this.findBox(buffer, "ispe", ipco.pos, ipco.pos + ipco.size);
|
|
599
|
+
if (!ispe) throw new Error("Invalid AVIF: No ispe");
|
|
600
|
+
if (buffer[ispe.pos] !== 0) throw new Error("Invalid ispe version");
|
|
601
|
+
const width = buffer.readUInt32BE(ispe.pos + 4);
|
|
602
|
+
const height = buffer.readUInt32BE(ispe.pos + 8);
|
|
603
|
+
return {
|
|
604
|
+
width,
|
|
605
|
+
height,
|
|
606
|
+
type: "avif"
|
|
607
|
+
};
|
|
608
|
+
}
|
|
609
|
+
throw new Error("Unsupported image format or invalid file");
|
|
610
|
+
}
|
|
428
611
|
/**
|
|
429
612
|
*
|
|
430
613
|
* @access package-private
|
|
@@ -447,11 +630,180 @@ var Fs = class extends MimeService {
|
|
|
447
630
|
return await sharp(target).toFormat(format, { quality }).resize(resize.widthOrOptions, resize.height, resize.options).toBuffer();
|
|
448
631
|
} else if (tint && resize) {
|
|
449
632
|
return await sharp(target).toFormat(format, { quality }).tint(tint).resize(resize.widthOrOptions, resize.height, resize.options).toBuffer();
|
|
450
|
-
} else
|
|
633
|
+
} else {
|
|
634
|
+
return await sharp(target).toFormat(format, { quality }).toBuffer();
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
/**
|
|
638
|
+
* Write data to a file in the system's tmp directory
|
|
639
|
+
* @param filename The filename (can include subdirectories like "img-probe/file.txt")
|
|
640
|
+
* @param data The data to write
|
|
641
|
+
* @param options Optional write stream options
|
|
642
|
+
* @returns The full path to the written file
|
|
643
|
+
* @example
|
|
644
|
+
* ```ts
|
|
645
|
+
* const fs = new Fs(process.cwd());
|
|
646
|
+
* const tmpPath = fs.writeTmp("session-data.json", JSON.stringify(data));
|
|
647
|
+
* // Or with a subdirectory:
|
|
648
|
+
* fs.writeTmp("img-probe/output.png", imageBuffer);
|
|
649
|
+
* ```
|
|
650
|
+
*/
|
|
651
|
+
writeTmp(filename, data, options) {
|
|
652
|
+
const fullPath = resolve(this.tmpDir, filename);
|
|
653
|
+
Promise.resolve(this.withWs(fullPath, data, options));
|
|
654
|
+
return fullPath;
|
|
655
|
+
}
|
|
656
|
+
/**
|
|
657
|
+
* Read a file from the tmp directory
|
|
658
|
+
* @param filename The filename to read (can include subdirectories)
|
|
659
|
+
* @returns Buffer containing the file data
|
|
660
|
+
* @example
|
|
661
|
+
* ```ts
|
|
662
|
+
* const fs = new Fs(process.cwd());
|
|
663
|
+
* const data = fs.readTmp("session-data.json");
|
|
664
|
+
* const parsed = JSON.parse(data.toString("utf-8"));
|
|
665
|
+
* ```
|
|
666
|
+
*/
|
|
667
|
+
readTmp(filename) {
|
|
668
|
+
const fullPath = resolve(this.tmpDir, filename);
|
|
669
|
+
if (!this.exists(fullPath)) {
|
|
670
|
+
throw new Error(`Tmp file not found: ${filename}`);
|
|
671
|
+
}
|
|
672
|
+
return this.fileToBuffer(fullPath);
|
|
673
|
+
}
|
|
674
|
+
/**
|
|
675
|
+
* Scan the tmp directory for files matching a pattern
|
|
676
|
+
* @param pattern Optional pattern to filter files (e.g., "img-probe")
|
|
677
|
+
* @param options Options for directory reading (defaults to non-recursive to avoid permission issues)
|
|
678
|
+
* @returns Array of matching file paths relative to tmp directory
|
|
679
|
+
* @example
|
|
680
|
+
* ```ts
|
|
681
|
+
* const fs = new Fs(process.cwd());
|
|
682
|
+
* // Get all img-probe files
|
|
683
|
+
* const imgFiles = fs.scanTmp("img-probe");
|
|
684
|
+
* // Get all files in tmp (non-recursive)
|
|
685
|
+
* const allFiles = fs.scanTmp();
|
|
686
|
+
* ```
|
|
687
|
+
*/
|
|
688
|
+
scanTmp(pattern, options = {
|
|
689
|
+
recursive: false,
|
|
690
|
+
withFileTypes: false,
|
|
691
|
+
encoding: "utf-8"
|
|
692
|
+
}) {
|
|
693
|
+
const dirs = resolve(this.tmpDir);
|
|
694
|
+
const tmpContents = this.readDir(dirs, options);
|
|
695
|
+
if (!pattern) {
|
|
696
|
+
return tmpContents;
|
|
697
|
+
}
|
|
698
|
+
return tmpContents.filter((path) => {
|
|
699
|
+
const filename = path.includes("/") ? path.split("/").pop() ?? path : path;
|
|
700
|
+
return filename.includes(pattern) || path.startsWith(pattern);
|
|
701
|
+
});
|
|
702
|
+
}
|
|
703
|
+
/**
|
|
704
|
+
* Extract specific files from tmp directory to a target location
|
|
705
|
+
* @param pattern Pattern to match files (e.g., "img-probe")
|
|
706
|
+
* @param targetDir Target directory to extract files to
|
|
707
|
+
* @param options Options for extraction behavior
|
|
708
|
+
* @returns Array of extracted file paths
|
|
709
|
+
* @example
|
|
710
|
+
* ```ts
|
|
711
|
+
* const fs = new Fs(process.cwd());
|
|
712
|
+
* // Extract all img-probe files to ./output
|
|
713
|
+
* const extracted = fs.extractFromTmp("img-probe", "./output");
|
|
714
|
+
* // Extract with custom behavior
|
|
715
|
+
* fs.extractFromTmp("session-", "./sessions", {
|
|
716
|
+
* preserveStructure: true,
|
|
717
|
+
* cleanupAfter: true
|
|
718
|
+
* });
|
|
719
|
+
* ```
|
|
720
|
+
*/
|
|
721
|
+
extractFromTmp(pattern, targetDir, options) {
|
|
722
|
+
const {
|
|
723
|
+
preserveStructure = false,
|
|
724
|
+
cleanupAfter = false,
|
|
725
|
+
overwrite = true
|
|
726
|
+
} = options ?? {};
|
|
727
|
+
const matchingFiles = this.scanTmp(pattern);
|
|
728
|
+
const extracted = [];
|
|
729
|
+
for (const file of matchingFiles) {
|
|
730
|
+
const sourcePath = resolve(this.tmpDir, file);
|
|
731
|
+
const lastSegment = file.includes("/") ? file.split("/").pop() : file;
|
|
732
|
+
if (!lastSegment) continue;
|
|
733
|
+
const targetPath = preserveStructure ? resolve(targetDir, file) : resolve(targetDir, lastSegment);
|
|
734
|
+
if (this.exists(targetPath) && !overwrite) {
|
|
735
|
+
console.warn(`Skipping existing file: ${targetPath}`);
|
|
736
|
+
continue;
|
|
737
|
+
}
|
|
738
|
+
const data = this.fileToBuffer(sourcePath);
|
|
739
|
+
this.withWs(targetPath, data);
|
|
740
|
+
extracted.push(targetPath);
|
|
741
|
+
if (cleanupAfter) {
|
|
742
|
+
this.rmFile(sourcePath);
|
|
743
|
+
}
|
|
744
|
+
}
|
|
745
|
+
return extracted;
|
|
746
|
+
}
|
|
747
|
+
/**
|
|
748
|
+
* Clean up tmp files matching a pattern
|
|
749
|
+
* @param pattern Pattern to match files for deletion
|
|
750
|
+
* @param maxAge Optional max age in milliseconds (only delete files older than this)
|
|
751
|
+
* @returns Number of files removed
|
|
752
|
+
* @example
|
|
753
|
+
* ```ts
|
|
754
|
+
* const fs = new Fs(process.cwd());
|
|
755
|
+
* // Remove all img-probe files
|
|
756
|
+
* fs.cleanupTmp("img-probe");
|
|
757
|
+
* // Remove session files older than 1 hour
|
|
758
|
+
* fs.cleanupTmp("session-", 60 * 60 * 1000);
|
|
759
|
+
* ```
|
|
760
|
+
*/
|
|
761
|
+
cleanupTmp(pattern, maxAge) {
|
|
762
|
+
const matchingFiles = this.scanTmp(pattern);
|
|
763
|
+
let removed = 0;
|
|
764
|
+
for (const file of matchingFiles) {
|
|
765
|
+
const fullPath = resolve(this.tmpDir, file);
|
|
766
|
+
try {
|
|
767
|
+
const stats = fsSync.statSync(fullPath);
|
|
768
|
+
if (maxAge) {
|
|
769
|
+
const age = Date.now() - stats.mtimeMs;
|
|
770
|
+
if (age < maxAge) {
|
|
771
|
+
continue;
|
|
772
|
+
}
|
|
773
|
+
}
|
|
774
|
+
if (stats.isDirectory()) {
|
|
775
|
+
this.rmDirSync(fullPath);
|
|
776
|
+
} else {
|
|
777
|
+
this.rmFile(fullPath);
|
|
778
|
+
}
|
|
779
|
+
removed++;
|
|
780
|
+
} catch (err) {
|
|
781
|
+
console.warn(`Failed to remove tmp file ${file}:`, err);
|
|
782
|
+
}
|
|
783
|
+
}
|
|
784
|
+
return removed;
|
|
785
|
+
}
|
|
786
|
+
/**
|
|
787
|
+
* Generate a unique tmp filename with optional prefix
|
|
788
|
+
* @param prefix Optional prefix for the filename
|
|
789
|
+
* @param extension Optional file extension (without dot)
|
|
790
|
+
* @returns Unique filename
|
|
791
|
+
* @example
|
|
792
|
+
* ```ts
|
|
793
|
+
* const fs = new Fs(process.cwd());
|
|
794
|
+
* const filename = fs.uniqueTmpName("img-probe", "png");
|
|
795
|
+
* // Returns: "img-probe-1701234567890-a3b2c1.png"
|
|
796
|
+
* ```
|
|
797
|
+
*/
|
|
798
|
+
uniqueTmpName(prefix, extension) {
|
|
799
|
+
const timestamp = Date.now();
|
|
800
|
+
const random = Math.random().toString(36).substring(2, 8);
|
|
801
|
+
const parts = [prefix ?? "tmp", timestamp, random].join("-");
|
|
802
|
+
return extension ? `${parts}.${extension}` : parts;
|
|
451
803
|
}
|
|
452
804
|
};
|
|
453
805
|
|
|
454
806
|
export {
|
|
455
807
|
Fs
|
|
456
808
|
};
|
|
457
|
-
//# sourceMappingURL=chunk-
|
|
809
|
+
//# sourceMappingURL=chunk-NGBMZGGU.js.map
|