@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.
@@ -1,392 +0,0 @@
1
- import expand from 'dotenv-expand';
2
- import sharp from 'sharp';
3
- import { ReadDirOptions, ExecuteCommandProps, MkDirSyncOptions, Unit, SizeOpts, WriteStreamDataShape, WriteStreamOptions, WriteFileAsyncDataUnion, WriteFileAsyncOptions, RmDirOptions } from '../types/index.cjs';
4
- import { MimeService } from '../mime/index.cjs';
5
- import '../url/index.cjs';
6
- import '../utils/index.cjs';
7
- import 'stream';
8
- import 'buffer';
9
-
10
- declare class Fs extends MimeService {
11
- cwd: string;
12
- constructor(cwd: string);
13
- private get myEnv();
14
- get tmpDir(): string;
15
- get tmpDirRelative(): string;
16
- parseDotEnv(): expand.DotenvExpandOutput;
17
- /**
18
- *
19
- * @param target path to the file or directory to check for existence
20
- * @returns true if the file or directory exists, false otherwise
21
- * @example
22
- * ```ts
23
- * const fs = new Fs(process.cwd());
24
- * const exists = fs.exists("my-file.txt");
25
- * console.log(exists); // true if the file exists, false otherwise
26
- * ```
27
- * @description
28
- * This method checks if a file or directory exists at the specified path input
29
- *
30
- * Additional Note:
31
- *
32
- * say you have a directory that begins with a dot, such as `.turbo`;
33
- *
34
- * in this scenario, both `./.turbo` and `.turbo` will return true if the directory exists
35
- *
36
- */
37
- exists<const T extends string>(target: T): boolean;
38
- /**
39
- *
40
- * @param ms milliseconds to wait
41
- * @returns a promise that resolves after the specified number of milliseconds
42
- * @example
43
- * ```ts
44
- * const fs = new Fs(process.cwd());
45
- * await fs.wait(1000); // waits for 1 second
46
- * console.log("Done waiting!");
47
- * ```
48
- *
49
- * Alternatively, you can use it like this:
50
- * ```ts
51
- * const fs = new Fs(process.cwd());
52
- * fs.wait(1000).then(() => { // waits for 1 second
53
- * console.log("Done waiting!");
54
- * // do something after waiting
55
- * });
56
- * ```
57
- *
58
- * @description
59
- * This method is useful for delaying execution in asynchronous code, such as when you need to wait for a file operation to complete or when you want to introduce a delay in a loop.
60
- */
61
- wait<T extends number>(ms: T): Promise<unknown>;
62
- /**
63
- * Handles an array or readonly array of strings and/or Buffers, converting Buffers to strings
64
- * @param arr An array or readonly array of strings and/or Buffers
65
- * @returns An array of strings with Buffers converted to UTF-8 strings.
66
- */
67
- handleBuffStrArrUnion<const T extends (string | Buffer)[] | readonly (string | Buffer)[]>(arr: T): string[];
68
- isRootPathTargeted<const T extends string>(path: T): boolean;
69
- /**
70
- *
71
- * @param path path to the directory relative to the cwd
72
- * @param options options for reading the directory, defaults to recursive
73
- *
74
- * The following two examples are equivalent:
75
- *
76
- * @example
77
- * ```ts
78
- * const fs = new Fs(process.cwd());
79
- * const files = fs.readDir("./my-directory", { recursive: true });
80
- * console.log(files);
81
- * ```
82
- * @example
83
- * ```ts
84
- * const fs = new Fs(process.cwd());
85
- * const files = fs.readDir("my-directory", { recursive: true });
86
- * console.log(files);
87
- * ```
88
- *
89
- * @returns an array of strings representing the contents of the directory
90
- */
91
- readDir<const T extends string>(path: T, options?: ReadDirOptions): string[];
92
- executeCommand: <const T extends string>({ command, ...options }: ExecuteCommandProps<T>) => string;
93
- existsSync<const T extends string>(path: T): boolean;
94
- pathHandler<const T extends string>(path: T): string;
95
- mkdirSync<const T extends string>(path: T, options?: MkDirSyncOptions): string | undefined;
96
- private units;
97
- private u;
98
- autoFileSizeRaw(size: number | bigint): {
99
- value: number;
100
- unit: "B" | "KB" | "MB" | "GB" | "TB" | "PB" | undefined;
101
- };
102
- getSize<const S extends Unit | Lowercase<Unit> | "auto">(size: number | bigint, target: S, opts?: SizeOpts): string | number;
103
- fileSizeMb<const T extends string>(path: T): number;
104
- fileSize<const T extends string, const S extends Unit | Lowercase<Unit> | "auto">(path: T, target: S, opts?: SizeOpts): string | number;
105
- generateDirIfDNE<const T extends string>(path: T, options?: MkDirSyncOptions): string | undefined;
106
- withWs<const T extends string>(path: T, data: WriteStreamDataShape, options?: WriteStreamOptions): string | boolean | undefined;
107
- writeFileAsync: <const T extends string>(path: T, data: WriteFileAsyncDataUnion, options?: WriteFileAsyncOptions) => Promise<string | void>;
108
- unlink<const P extends string>(path: P): Promise<void>;
109
- rmFile<const FP extends string>(filePath: FP): void;
110
- rmdir<const D extends string>(dir: D, options?: RmDirOptions): Promise<void>;
111
- rmDirSync<const D extends string>(dir: D): void;
112
- fileToBuffer: <const T extends string>(path: T) => Buffer<ArrayBuffer>;
113
- fileToBufferAsync: <const T extends string>(path: T) => Promise<Buffer<ArrayBufferLike>>;
114
- dirContainsDir<const From extends string, const To extends string>(readDir: From, targetDir: To, options?: ReadDirOptions): boolean;
115
- assetToBuffer<const T extends string>(path: T): Promise<{
116
- readonly b64encodedData: `data:audio/aac;base64, ${string}` | `data:application/x-abiword;base64, ${string}` | `data:image/aces;base64, ${string}` | `data:image/apng;base64, ${string}` | `data:application/x-freearc;base64, ${string}` | `data:image/avci;base64, ${string}` | `data:image/avif;base64, ${string}` | `data:video/x-msvideo;base64, ${string}` | `data:application/vnd.amazon.ebook;base64, ${string}` | `data:application/octet-stream;base64, ${string}` | `data:multipart/voice-message;base64, ${string}` | `data:image/bmp;base64, ${string}` | `data:application/x-bzip;base64, ${string}` | `data:application/x-bzip2;base64, ${string}` | `data:application/x-cdf;base64, ${string}` | `data:application/node;base64, ${string}` | `data:text/javascript;base64, ${string}` | `data:application/x-csh;base64, ${string}` | `data:text/css;base64, ${string}` | `data:text/csv;base64, ${string}` | `data:application/msword;base64, ${string}` | `data:application/vnd.openxmlformats-officedocument.wordprocessingml.document;base64, ${string}` | `data:image/dpx;base64, ${string}` | `data:image/emf;base64, ${string}` | `data:application/vnd.ms-fontobject;base64, ${string}` | `data:application/epub+zip;base64, ${string}` | `data:image/gif;base64, ${string}` | `data:model/gltf-binary;base64, ${string}` | `data:model/gltf+json;base64, ${string}` | `data:application/gzip;base64, ${string}` | `data:application/x-gzip;base64, ${string}` | `data:haptics/hjif;base64, ${string}` | `data:haptics/hmpg;base64, ${string}` | `data:text/html;base64, ${string}` | `data:image/vnd.microsoft.icon;base64, ${string}` | `data:text/calendar;base64, ${string}` | `data:haptics/ivs;base64, ${string}` | `data:application/java-archive;base64, ${string}` | `data:image/jpeg;base64, ${string}` | `data:application/json;base64, ${string}` | `data:application/ld+json;base64, ${string}` | `data:image/ktx;base64, ${string}` | `data:image/ktx2;base64, ${string}` | `data:application/vnd.apple.mpegurl;base64, ${string}` | `data:audio/mp4;base64, ${string}` | `data:video/mp4;base64, ${string}` | `data:text/markdown;base64, ${string}` | `data:application/x-mdx;base64, ${string}` | `data:audio/midi;base64, ${string}` | `data:audio/x-midi;base64, ${string}` | `data:audio/mpeg;base64, ${string}` | `data:video/mpeg;base64, ${string}` | `data:application/vnd.apple.installer+xml;base64, ${string}` | `data:application/x-ndjson;base64, ${string}` | `data:model/obj;base64, ${string}` | `data:text/plain;base64, ${string}` | `data:application/vnd.oasis.opendocument.presentation;base64, ${string}` | `data:application/vnd.oasis.opendocument.spreadsheet;base64, ${string}` | `data:application/vnd.oasis.opendocument.text;base64, ${string}` | `data:audio/ogg;base64, ${string}` | `data:video/ogg;base64, ${string}` | `data:application/ogg;base64, ${string}` | `data:font/otf;base64, ${string}` | `data:image/png;base64, ${string}` | `data:application/pdf;base64, ${string}` | `data:application/x-httpd-php;base64, ${string}` | `data:application/vnd.apple.pkpass;base64, ${string}` | `data:application/vnd.ms-powerpoint;base64, ${string}` | `data:application/vnd.openxmlformats-officedocument.presentationml.presentation;base64, ${string}` | `data:text/x-python;base64, ${string}` | `data:application/x-python-code;base64, ${string}` | `data:application/vnd.rar;base64, ${string}` | `data:text/rust;base64, ${string}` | `data:application/rtf;base64, ${string}` | `data:application/x-sh;base64, ${string}` | `data:application/sql;base64, ${string}` | `data:text/event-stream;base64, ${string}` | `data:image/svg+xml;base64, ${string}` | `data:application/x-tar;base64, ${string}` | `data:image/tiff;base64, ${string}` | `data:application/toml;base64, ${string}` | `data:text/typescript;base64, ${string}` | `data:video/mp2t;base64, ${string}` | `data:video/vnd.dlna.mpeg-tts;base64, ${string}` | `data:application/font-sfnt;base64, ${string}` | `data:font/ttf;base64, ${string}` | `data:model/vnd.usdz+zip;base64, ${string}` | `data:application/vnd.visio;base64, ${string}` | `data:text/vtt;base64, ${string}` | `data:application/wasm;base64, ${string}` | `data:audio/wav;base64, ${string}` | `data:video/webm;base64, ${string}` | `data:image/webp;base64, ${string}` | `data:font/woff;base64, ${string}` | `data:font/woff2;base64, ${string}` | `data:application/xhtml+xml;base64, ${string}` | `data:application/vnd.ms-excel;base64, ${string}` | `data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64, ${string}` | `data:application/xml;base64, ${string}` | `data:application/vnd.mozilla.xul+xml;base64, ${string}` | `data:application/yaml;base64, ${string}` | `data:application/zip;base64, ${string}` | `data:application/x-zip-compressed;base64, ${string}` | `data:video/3gpp;base64, ${string}` | `data:video/3gpp2;base64, ${string}` | `data:application/x-7z-compressed;base64, ${string}`;
117
- readonly extension: "png" | "jpeg" | "gif" | "bmp" | "webp" | "avif" | "epub" | "ttf" | "gz" | "jar" | "json" | "jsonld" | "doc" | "node" | "js" | "bin" | "obj" | "ogx" | "pdf" | "rtf" | "sql" | "toml" | "azw" | "mpkg" | "m3u8" | "pkpass" | "xul" | "xls" | "eot" | "ppt" | "odp" | "ods" | "odt" | "pptx" | "xlsx" | "docx" | "rar" | "vsd" | "wasm" | "7z" | "abw" | "bz" | "bz2" | "cda" | "csh" | "arc" | "php" | "mdx" | "ndjson" | "pyc" | "sh" | "tar" | "zip" | "xhtml" | "xml" | "yml" | "yaml" | "aac" | "mid" | "m4a" | "mp3" | "opus" | "ogg" | "oga" | "wav" | "midi" | "otf" | "woff" | "woff2" | "hjif" | "hmpg" | "ivs" | "ivt" | "aces" | "apng" | "avci" | "dpx" | "emf" | "jpg" | "ktx" | "ktx2" | "svg" | "tiff" | "tif" | "ico" | "glb" | "gltf" | "usdz" | "ics" | "css" | "csv" | "sse" | "ts" | "rs" | "py" | "txt" | "html" | "htm" | "cjs" | "mjs" | "md" | "vtt" | "3gp" | "3g2" | "mp4" | "m4v" | "mpeg" | "ogv" | "weba" | "avi";
118
- }>;
119
- assetToBufferView<const T extends string>(path: T): Promise<{
120
- readonly b64encodedData: string;
121
- readonly extension: "png" | "jpeg" | "gif" | "bmp" | "webp" | "avif" | "epub" | "ttf" | "gz" | "jar" | "json" | "jsonld" | "doc" | "node" | "js" | "bin" | "obj" | "ogx" | "pdf" | "rtf" | "sql" | "toml" | "azw" | "mpkg" | "m3u8" | "pkpass" | "xul" | "xls" | "eot" | "ppt" | "odp" | "ods" | "odt" | "pptx" | "xlsx" | "docx" | "rar" | "vsd" | "wasm" | "7z" | "abw" | "bz" | "bz2" | "cda" | "csh" | "arc" | "php" | "mdx" | "ndjson" | "pyc" | "sh" | "tar" | "zip" | "xhtml" | "xml" | "yml" | "yaml" | "aac" | "mid" | "m4a" | "mp3" | "opus" | "ogg" | "oga" | "wav" | "midi" | "otf" | "woff" | "woff2" | "hjif" | "hmpg" | "ivs" | "ivt" | "aces" | "apng" | "avci" | "dpx" | "emf" | "jpg" | "ktx" | "ktx2" | "svg" | "tiff" | "tif" | "ico" | "glb" | "gltf" | "usdz" | "ics" | "css" | "csv" | "sse" | "ts" | "rs" | "py" | "txt" | "html" | "htm" | "cjs" | "mjs" | "md" | "vtt" | "3gp" | "3g2" | "mp4" | "m4v" | "mpeg" | "ogv" | "weba" | "avi" | undefined;
122
- }>;
123
- formatHelper<const T extends string>(f: T): string;
124
- cleanDataUrl<const C extends string>(props: C): string;
125
- /**
126
- *
127
- * @param inputUrl remote url to fetch data from
128
- * @param outputPath desired output path relative to the cwd
129
- * @param useDetectedExtension optional, defaults to true
130
- *
131
- * if `useDetectedExtension` is false you must include the file extension in your output path &rarr;
132
- *
133
- * 🚫 'public/assets/image-1'
134
- *
135
- * ✅ 'public/assets/image-1.png'
136
- */
137
- fetchRemoteWriteLocal<const I extends string, const O extends string>(inputUrl: I, outputPath: O, useDetectedExtension?: boolean): Promise<void>;
138
- parseExif(buffer: Buffer, app1Pos: number, _app1Size: number): {
139
- orientation: number | null;
140
- dateTimeOriginal: string | null;
141
- };
142
- /**
143
- *
144
- * @param inputUrl remote url to fetch data from
145
- * @param outputPath desired output path relative to the cwd
146
- * @param useDetectedExtension optional, defaults to true
147
- *
148
- * if `useDetectedExtension` is false you must include the file extension in your output path &rarr;
149
- *
150
- * 🚫 'public/assets/image-1'
151
- *
152
- * ✅ 'public/assets/image-1.png'
153
- *
154
- * @description
155
- * This method is designed for large files that may not fit into memory.
156
- * It streams the data directly to disk instead of loading it all into memory.
157
- */
158
- fetchRemoteWriteLocalLargeFiles<const I extends string, const O extends string>(inputUrl: I, outputPathI: O, useDetectedExtension?: boolean): Promise<void>;
159
- private findBox;
160
- getImageSpecs(filePath: string): Promise<{
161
- width: number;
162
- height: number;
163
- format: "png";
164
- frames: number;
165
- animated: boolean;
166
- hasAlpha: boolean;
167
- orientation: null;
168
- aspectRatio: number;
169
- colorSpace: "unknown" | "rgb" | "rgba" | "grayscale" | "grayscale-alpha" | "indexed";
170
- iccProfile: string | null;
171
- exifDateTimeOriginal: string | null;
172
- } | {
173
- width: number;
174
- height: number;
175
- format: "jpeg";
176
- frames: number;
177
- animated: false;
178
- hasAlpha: false;
179
- orientation: number | null;
180
- aspectRatio: number;
181
- colorSpace: "unknown" | "grayscale" | "ycbcr" | "ycck";
182
- iccProfile: string | null;
183
- exifDateTimeOriginal: string | null;
184
- } | {
185
- width: number;
186
- height: number;
187
- format: "gif";
188
- frames: number;
189
- animated: boolean;
190
- hasAlpha: null;
191
- orientation: null;
192
- aspectRatio: number;
193
- colorSpace: "indexed";
194
- iccProfile: null;
195
- exifDateTimeOriginal: null;
196
- } | {
197
- width: number;
198
- height: number;
199
- format: "bmp";
200
- frames: number;
201
- animated: false;
202
- hasAlpha: null;
203
- orientation: number;
204
- aspectRatio: number;
205
- colorSpace: "rgb" | "indexed";
206
- iccProfile: null;
207
- exifDateTimeOriginal: null;
208
- } | {
209
- width: number;
210
- height: number;
211
- format: "webp";
212
- frames: number;
213
- animated: boolean;
214
- hasAlpha: boolean;
215
- orientation: null;
216
- aspectRatio: number;
217
- colorSpace: "unknown" | "rgb" | "rgba";
218
- iccProfile: string | null;
219
- exifDateTimeOriginal: null;
220
- } | {
221
- width: number;
222
- height: number;
223
- format: "avif";
224
- frames: number;
225
- animated: boolean;
226
- hasAlpha: true | null;
227
- orientation: null;
228
- aspectRatio: number;
229
- colorSpace: "unknown" | "rgb" | "rgba" | "grayscale-alpha" | "ycbcr" | "ycck";
230
- iccProfile: string | null;
231
- exifDateTimeOriginal: null;
232
- }>;
233
- /**
234
- *
235
- * @access package-private
236
- *
237
- * work in progress, for internal package maintainer use only
238
- */
239
- imageTransform<const F extends "webp" | "avif" | "jpg" | "png" | "tif" | "tiff" | "jp2" | "jpeg">({ format, target, quality, tint, resize }: {
240
- format: F;
241
- target: Buffer<ArrayBuffer>;
242
- quality?: number;
243
- tint?: string | {
244
- r?: number | undefined;
245
- g?: number | undefined;
246
- b?: number | undefined;
247
- alpha?: number | undefined;
248
- };
249
- resize?: {
250
- widthOrOptions?: number | sharp.ResizeOptions | null;
251
- height?: number | null;
252
- options?: sharp.ResizeOptions;
253
- };
254
- }): Promise<Buffer<ArrayBufferLike>>;
255
- /**
256
- * Write data to a file in the system's tmp directory
257
- * @param filename The filename (can include subdirectories like "img-probe/file.txt")
258
- * @param data The data to write
259
- * @param options Optional write stream options
260
- * @returns The full path to the written file
261
- * @example
262
- * ```ts
263
- * const fs = new Fs(process.cwd());
264
- * const tmpPath = fs.writeTmp("session-data.json", JSON.stringify(data));
265
- * // Or with a subdirectory:
266
- * fs.writeTmp("img-probe/output.png", imageBuffer);
267
- * ```
268
- */
269
- writeTmp<const F extends string>(filename: F, data: WriteStreamDataShape, options?: WriteStreamOptions): void;
270
- /**
271
- * Read a file from the tmp directory
272
- * @param filename The filename to read (can include subdirectories)
273
- * @returns Buffer containing the file data
274
- * @example
275
- * ```ts
276
- * const fs = new Fs(process.cwd());
277
- * const data = fs.readTmp("session-data.json");
278
- * const parsed = JSON.parse(data.toString("utf-8"));
279
- * ```
280
- */
281
- readTmp<const F extends string>(filename: F): Buffer<ArrayBuffer>;
282
- /**
283
- * Scan the tmp directory for files matching a pattern
284
- * @param pattern Optional pattern to filter files (e.g., "img-probe")
285
- * @param options Options for directory reading (defaults to non-recursive to avoid permission issues)
286
- * @returns Array of matching file paths relative to tmp directory
287
- * @example
288
- * ```ts
289
- * const fs = new Fs(process.cwd());
290
- * // Get all img-probe files
291
- * const imgFiles = fs.scanTmp("img-probe");
292
- * // Get all files in tmp (non-recursive)
293
- * const allFiles = fs.scanTmp();
294
- * ```
295
- */
296
- scanTmp(pattern?: string | RegExp, options?: ReadDirOptions): string[];
297
- /**
298
- * Extract specific files from tmp directory to a target location
299
- * @param pattern Pattern to match files (e.g., "img-probe")
300
- * @param targetDir Target directory to extract files to
301
- * @param options Options for extraction behavior
302
- * @returns Array of extracted file paths
303
- * @example
304
- * ```ts
305
- * const fs = new Fs(process.cwd());
306
- * // Extract all img-probe files to ./output
307
- * const extracted = fs.extractFromTmp("img-probe", "./output");
308
- * // Extract with custom behavior
309
- * fs.extractFromTmp("session-", "./sessions", {
310
- * preserveStructure: true,
311
- * cleanupAfter: true
312
- * });
313
- * ```
314
- */
315
- extractFromTmp(pattern: string, targetDir: string, options?: {
316
- preserveStructure?: boolean;
317
- cleanupAfter?: boolean;
318
- overwrite?: boolean;
319
- }): string[];
320
- /**
321
- * Clean up tmp files matching a pattern
322
- * @param pattern Pattern to match files for deletion
323
- * @param maxAge Optional max age in milliseconds (only delete files older than this)
324
- * @returns Number of files removed
325
- * @example
326
- * ```ts
327
- * const fs = new Fs(process.cwd());
328
- * // Remove all img-probe files
329
- * fs.cleanupTmp("img-probe");
330
- * // Remove session files older than 1 hour
331
- * fs.cleanupTmp("session-", 60 * 60 * 1000);
332
- * ```
333
- */
334
- cleanupTmp(pattern: string, maxAge?: number): number;
335
- rmTmpFile<const V extends string>(filename: V): void;
336
- /**
337
- * Generate a unique tmp filename with optional prefix
338
- * @param prefix Optional prefix for the filename
339
- * @param extension Optional file extension (without dot)
340
- * @returns Unique filename
341
- * @example
342
- * ```ts
343
- * const fs = new Fs(process.cwd());
344
- * const filename = fs.uniqueTmpName("img-probe", "png");
345
- * // Returns: "img-probe-1701234567890-a3b2c1.png"
346
- * ```
347
- */
348
- uniqueTmpName(prefix?: string, extension?: string): string;
349
- /**
350
- * Async generator for cleaning tmp files with batched removal
351
- * @param pattern Pattern to match files for deletion
352
- * @param batchSize Number of files to remove per batch (default 10)
353
- * @yields Progress information during cleanup
354
- * @returns Final cleanup summary
355
- * @example
356
- * ```ts
357
- * const fs = new Fs(process.cwd());
358
- * // Clean up with progress tracking
359
- * for await (const progress of fs.cleanTmpGenerator("session-", 25)) {
360
- * console.log(progress);
361
- * }
362
- * ```
363
- */
364
- cleanTmpGenerator(pattern: string | RegExp, batchSize?: number): AsyncGenerator<{
365
- action: "removing";
366
- batch: string[];
367
- batchNumber: number;
368
- totalBatches: number;
369
- totalFound: number;
370
- removed: number;
371
- batchSize?: undefined;
372
- totalRemoved?: undefined;
373
- remaining?: undefined;
374
- } | {
375
- action: "batch-complete";
376
- batchNumber: number;
377
- batchSize: number;
378
- totalRemoved: number;
379
- remaining: number;
380
- batch?: undefined;
381
- totalBatches?: undefined;
382
- totalFound?: undefined;
383
- removed?: undefined;
384
- }, {
385
- action: "complete";
386
- totalRemoved: number;
387
- totalBatches: number;
388
- pattern: string;
389
- }, unknown>;
390
- }
391
-
392
- export = Fs;