@d0paminedriven/fs 7.2.0 → 7.4.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 +127 -51
- package/dist/{chunk-O2P3L3CJ.js → chunk-6HHKXBIM.js} +5 -2
- package/dist/chunk-7FHBNIB7.js +240 -0
- package/dist/{chunk-KUGIFRTF.js → chunk-C2ANVZLG.js} +345 -4
- package/dist/chunk-FPB77OGJ.js +75 -0
- package/dist/chunk-IU4MDHYM.js +75 -0
- package/dist/chunk-KDWGS5GC.js +69 -0
- package/dist/{chunk-GH6MFTV5.js → chunk-KNLXF622.js} +2 -2
- package/dist/chunk-Q2VF3BVH.js +165 -0
- package/dist/{chunk-MRKO3PAA.js → chunk-R63XI7FS.js} +1 -1
- package/dist/chunk-TDMPLELZ.js +367 -0
- package/dist/{chunk-6FJJLFJL.js → chunk-UW52DMHT.js} +2 -2
- package/dist/chunk-XCA34DKZ.js +107 -0
- package/dist/chunk-Z2WZLZC4.js +75 -0
- package/dist/fs/index.d.ts +21 -476
- package/dist/fs/index.js +13 -6
- package/dist/fs-atomic/index.d.ts +38 -0
- package/dist/fs-atomic/index.js +14 -0
- package/dist/fs-base/index.d.ts +112 -0
- package/dist/fs-base/index.js +13 -0
- package/dist/fs-core/index.d.ts +24 -0
- package/dist/fs-core/index.js +17 -0
- package/dist/fs-fetch/index.d.ts +64 -0
- package/dist/fs-fetch/index.js +19 -0
- package/dist/fs-size/index.d.ts +30 -0
- package/dist/fs-size/index.js +16 -0
- package/dist/fs-tmp/index.d.ts +194 -0
- package/dist/fs-tmp/index.js +18 -0
- package/dist/image/index.d.ts +36 -2
- package/dist/image/index.js +5 -4
- package/dist/index.d.ts +11 -4
- package/dist/index.js +36 -8
- package/dist/ld/index.d.ts +14 -0
- package/dist/ld/index.js +8 -0
- package/dist/mime/index.d.ts +5 -4
- package/dist/mime/index.js +4 -3
- package/dist/types/index.d.ts +13 -13
- package/dist/types/index.js +1 -1
- package/dist/types/stream.d.ts +2 -1
- package/dist/url/index.d.ts +1 -0
- package/dist/url/index.js +3 -2
- package/dist/utils/index.d.ts +2 -1
- package/dist/utils/index.js +2 -1
- package/package.json +32 -4
- package/dist/chunk-SYISQ57E.js +0 -895
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import {
|
|
2
|
+
FsTmp
|
|
3
|
+
} from "./chunk-TDMPLELZ.js";
|
|
4
|
+
import {
|
|
5
|
+
__name
|
|
6
|
+
} from "./chunk-7QVYU63E.js";
|
|
7
|
+
|
|
8
|
+
// src/fs-fetch/index.ts
|
|
9
|
+
import fsSync from "fs";
|
|
10
|
+
import { relative } from "path";
|
|
11
|
+
var FsFetch = class extends FsTmp {
|
|
12
|
+
constructor(cwd) {
|
|
13
|
+
super(cwd ??= process.cwd());
|
|
14
|
+
this.cwd = cwd;
|
|
15
|
+
}
|
|
16
|
+
static {
|
|
17
|
+
__name(this, "FsFetch");
|
|
18
|
+
}
|
|
19
|
+
async assetToBuffer(path) {
|
|
20
|
+
const [fetcher] = await Promise.all([
|
|
21
|
+
fetch(path).then((t) => t.arrayBuffer())
|
|
22
|
+
]);
|
|
23
|
+
const mime = this.getMimeTypeForPath(path);
|
|
24
|
+
const b64encodedData = `data:${mime};base64, ${Buffer.from(fetcher).toString("base64")}`;
|
|
25
|
+
const extension = this.assetType(path) ?? "txt";
|
|
26
|
+
return {
|
|
27
|
+
b64encodedData,
|
|
28
|
+
extension
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
async assetToBufferView(path) {
|
|
32
|
+
const response = await fetch(path, { method: "GET" });
|
|
33
|
+
if (!response.ok) {
|
|
34
|
+
throw new Error(`Failed to fetch asset: ${response.statusText}`);
|
|
35
|
+
}
|
|
36
|
+
const reader = response.body?.getReader();
|
|
37
|
+
if (reader) {
|
|
38
|
+
const chunks = Array.of();
|
|
39
|
+
while (true) {
|
|
40
|
+
const { done, value } = await reader.read();
|
|
41
|
+
if (done) break;
|
|
42
|
+
if (value) chunks.push(value);
|
|
43
|
+
}
|
|
44
|
+
const totalLength = chunks.reduce((acc, chunk) => acc + chunk.length, 0);
|
|
45
|
+
const completeBuffer = new Uint8Array(totalLength);
|
|
46
|
+
let offset = 0;
|
|
47
|
+
for (const chunk of chunks) {
|
|
48
|
+
completeBuffer.set(chunk, offset);
|
|
49
|
+
offset += chunk.length;
|
|
50
|
+
}
|
|
51
|
+
const mime = this.getMimeTypeForPath(path);
|
|
52
|
+
const b64encodedData = `data:${mime};base64, ${Buffer.from(completeBuffer).toString("base64")}`;
|
|
53
|
+
const extension = this.assetType(path);
|
|
54
|
+
return { b64encodedData, extension };
|
|
55
|
+
} else {
|
|
56
|
+
const arrayBuffer = await response.arrayBuffer();
|
|
57
|
+
const mime = this.getMimeTypeForPath(path);
|
|
58
|
+
const b64encodedData = `data:${mime};base64, ${Buffer.from(arrayBuffer).toString("base64")}`;
|
|
59
|
+
const extension = this.assetType(path);
|
|
60
|
+
return { b64encodedData, extension };
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
*
|
|
65
|
+
* @param inputUrl remote url to fetch data from
|
|
66
|
+
* @param outputPath desired output path relative to the cwd
|
|
67
|
+
* @param useDetectedExtension optional, defaults to true
|
|
68
|
+
*
|
|
69
|
+
* if `useDetectedExtension` is false you must include the file extension in your output path →
|
|
70
|
+
*
|
|
71
|
+
* 🚫 'public/assets/image-1'
|
|
72
|
+
*
|
|
73
|
+
* ✅ 'public/assets/image-1.png'
|
|
74
|
+
*/
|
|
75
|
+
async fetchRemoteWriteLocal(inputUrl, outputPath, useDetectedExtension = true) {
|
|
76
|
+
try {
|
|
77
|
+
const result = await this.assetToBufferView(inputUrl);
|
|
78
|
+
const cleanData = this.cleanDataUrl(result.b64encodedData);
|
|
79
|
+
const formattedPath = useDetectedExtension ? `${outputPath}.${result.extension}` : outputPath;
|
|
80
|
+
if (/\./g.test(formattedPath) === false) {
|
|
81
|
+
throw new Error(
|
|
82
|
+
"either add false as the third argument in `fetchRemoteWriteLocal` (input, output, use-detected-file-extension) or provide an output path without a file extension"
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
this.withWs(formattedPath, Buffer.from(cleanData, "base64"));
|
|
86
|
+
} catch (err) {
|
|
87
|
+
return console.error(err);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
knownToBlockHead(inputUrl) {
|
|
91
|
+
if (inputUrl.startsWith("https://github.com")) {
|
|
92
|
+
return "GET";
|
|
93
|
+
} else if (inputUrl.startsWith("https://raw.githubusercontent.com")) {
|
|
94
|
+
return "GET";
|
|
95
|
+
} else {
|
|
96
|
+
return "HEAD";
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
*
|
|
101
|
+
* @param inputUrl remote url to fetch data from
|
|
102
|
+
* @param outputPath desired output path relative to the cwd
|
|
103
|
+
* @param useDetectedExtension optional, defaults to true
|
|
104
|
+
*
|
|
105
|
+
* if `useDetectedExtension` is false you must include the file extension in your output path →
|
|
106
|
+
*
|
|
107
|
+
* 🚫 'public/assets/image-1'
|
|
108
|
+
*
|
|
109
|
+
* ✅ 'public/assets/image-1.png'
|
|
110
|
+
*
|
|
111
|
+
* @description
|
|
112
|
+
* This method is designed for all files regardless of size, but especially for
|
|
113
|
+
* larger files that may not fit into memory (it intelligently determines the best approach on the fly)
|
|
114
|
+
* For files > 100 MB, it streams the data directly to disk instead of loading it all into memory
|
|
115
|
+
*/
|
|
116
|
+
async fetchRemoteWriteLocalLargeFiles(inputUrl, outputPathI, useDetectedExtension = true) {
|
|
117
|
+
if (!URL.canParse(inputUrl))
|
|
118
|
+
throw new Error(`invalid URL ${inputUrl} is unable to be parsed`);
|
|
119
|
+
const MAX_SAFE_IN_MEMORY_MB = 100;
|
|
120
|
+
try {
|
|
121
|
+
const head = await fetch(inputUrl, {
|
|
122
|
+
method: this.knownToBlockHead(inputUrl)
|
|
123
|
+
});
|
|
124
|
+
const contentLength = head.headers.get("content-length");
|
|
125
|
+
const fileSizeMb = contentLength ? parseInt(contentLength, 10) / (1024 * 1024) : null;
|
|
126
|
+
let outputPath = outputPathI;
|
|
127
|
+
const assetTypeViaUrl = this.assetType(inputUrl);
|
|
128
|
+
const contentType = head.headers.get("content-type") ?? "application/octet-stream";
|
|
129
|
+
const formattedPath = useDetectedExtension ? `${outputPath}.${typeof assetTypeViaUrl === "undefined" ? this.toExtObj[contentType][0] : assetTypeViaUrl}` : outputPath;
|
|
130
|
+
this.generateDirIfDNE(this.pathHandler(formattedPath), {
|
|
131
|
+
recursive: true
|
|
132
|
+
});
|
|
133
|
+
if (fileSizeMb !== null && fileSizeMb > MAX_SAFE_IN_MEMORY_MB) {
|
|
134
|
+
const res = await fetch(inputUrl);
|
|
135
|
+
if (!res.ok || !res.body) {
|
|
136
|
+
throw new Error(`Failed to fetch asset: ${res.statusText}`);
|
|
137
|
+
}
|
|
138
|
+
const writeStream = fsSync.createWriteStream(
|
|
139
|
+
relative(this.cwd, formattedPath)
|
|
140
|
+
);
|
|
141
|
+
const reader = res.body.getReader();
|
|
142
|
+
const pump = /* @__PURE__ */ __name(async () => {
|
|
143
|
+
while (true) {
|
|
144
|
+
const { done, value } = await reader.read();
|
|
145
|
+
if (done) break;
|
|
146
|
+
writeStream.write(value);
|
|
147
|
+
}
|
|
148
|
+
writeStream.end();
|
|
149
|
+
}, "pump");
|
|
150
|
+
await pump();
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
const result = await this.assetToBufferView(inputUrl);
|
|
154
|
+
const cleanedData = this.cleanDataUrl(result.b64encodedData);
|
|
155
|
+
this.withWs(formattedPath, Buffer.from(cleanedData, "base64"));
|
|
156
|
+
} catch (err) {
|
|
157
|
+
console.error(`[fetchRemoteWriteLocalLargeFiles error]:`, err);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
export {
|
|
163
|
+
FsFetch
|
|
164
|
+
};
|
|
165
|
+
//# sourceMappingURL=chunk-Q2VF3BVH.js.map
|
|
@@ -0,0 +1,367 @@
|
|
|
1
|
+
import {
|
|
2
|
+
FsCore
|
|
3
|
+
} from "./chunk-KDWGS5GC.js";
|
|
4
|
+
import {
|
|
5
|
+
__name
|
|
6
|
+
} from "./chunk-7QVYU63E.js";
|
|
7
|
+
|
|
8
|
+
// src/fs-tmp/index.ts
|
|
9
|
+
import fsSync from "fs";
|
|
10
|
+
import fsAsync from "fs/promises";
|
|
11
|
+
import { resolve } from "path";
|
|
12
|
+
var FsTmp = class extends FsCore {
|
|
13
|
+
constructor(cwd) {
|
|
14
|
+
super(cwd ??= process.cwd());
|
|
15
|
+
this.cwd = cwd;
|
|
16
|
+
}
|
|
17
|
+
static {
|
|
18
|
+
__name(this, "FsTmp");
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Write data to a file in the system's tmp directory
|
|
22
|
+
* @param filename The filename (can include subdirectories like "img-probe/file.txt")
|
|
23
|
+
* @param data The data to write
|
|
24
|
+
* @param options Optional write stream options
|
|
25
|
+
* @returns The full path to the written file
|
|
26
|
+
* @example
|
|
27
|
+
* ```ts
|
|
28
|
+
* const fs = new Fs(process.cwd());
|
|
29
|
+
* const tmpPath = fs.writeTmp("session-data.json", JSON.stringify(data));
|
|
30
|
+
* // Or with a subdirectory:
|
|
31
|
+
* fs.writeTmp("img-probe/output.png", imageBuffer);
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
writeTmp(filename, data, options) {
|
|
35
|
+
const fullPath = resolve(this.tmpDir, filename);
|
|
36
|
+
Promise.resolve(this.withWs(fullPath, data, options));
|
|
37
|
+
}
|
|
38
|
+
async writeTmpAsync(filename, data, options = {}) {
|
|
39
|
+
const fullPath = resolve(this.tmpDir, filename);
|
|
40
|
+
return await this.writeFileAsync(
|
|
41
|
+
fullPath,
|
|
42
|
+
data,
|
|
43
|
+
typeof options === "object" ? { ...options, mode: 511 } : options
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
async readTmpAsync(filename) {
|
|
47
|
+
const fullPath = resolve(this.tmpDir, filename);
|
|
48
|
+
if (!this.existsTmp(filename)) {
|
|
49
|
+
throw new Error(`Tmp file not found: ${filename}`);
|
|
50
|
+
}
|
|
51
|
+
return await this.fileToBufferAsync(fullPath);
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Read a file from the tmp directory
|
|
55
|
+
* @param filename The filename to read (can include subdirectories)
|
|
56
|
+
* @returns Buffer containing the file data
|
|
57
|
+
* @example
|
|
58
|
+
* ```ts
|
|
59
|
+
* const fs = new Fs(process.cwd());
|
|
60
|
+
* const data = fs.readTmp("session-data.json");
|
|
61
|
+
* const parsed = JSON.parse(data.toString("utf-8"));
|
|
62
|
+
* ```
|
|
63
|
+
*/
|
|
64
|
+
readTmp(filename) {
|
|
65
|
+
const fullPath = resolve(this.tmpDir, filename);
|
|
66
|
+
if (!this.exists(fullPath)) {
|
|
67
|
+
throw new Error(`Tmp file not found: ${filename}`);
|
|
68
|
+
}
|
|
69
|
+
return this.fileToBuffer(fullPath);
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Scan the tmp directory for files matching a pattern
|
|
73
|
+
* @param pattern Optional pattern to filter files (e.g., "img-probe")
|
|
74
|
+
* @param options Options for directory reading (defaults to non-recursive to avoid permission issues)
|
|
75
|
+
* @returns Array of matching file paths relative to tmp directory
|
|
76
|
+
* @example
|
|
77
|
+
* ```ts
|
|
78
|
+
* const fs = new Fs(process.cwd());
|
|
79
|
+
* // Get all img-probe files
|
|
80
|
+
* const imgFiles = fs.scanTmp("img-probe");
|
|
81
|
+
* // Get all files in tmp (non-recursive)
|
|
82
|
+
* const allFiles = fs.scanTmp();
|
|
83
|
+
* ```
|
|
84
|
+
*/
|
|
85
|
+
scanTmp(pattern, options = {
|
|
86
|
+
recursive: false,
|
|
87
|
+
withFileTypes: false,
|
|
88
|
+
encoding: "utf-8"
|
|
89
|
+
}) {
|
|
90
|
+
const dirs = resolve(this.tmpDir);
|
|
91
|
+
const tmpContents = this.readDir(dirs, options);
|
|
92
|
+
if (!pattern) {
|
|
93
|
+
return tmpContents;
|
|
94
|
+
}
|
|
95
|
+
if (typeof pattern === "string") {
|
|
96
|
+
return tmpContents.filter((t) => t.includes(pattern));
|
|
97
|
+
}
|
|
98
|
+
return tmpContents.filter((path) => {
|
|
99
|
+
return pattern.test(path);
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Extract specific files from tmp directory to a target location
|
|
104
|
+
* @param pattern Pattern to match files (e.g., "img-probe")
|
|
105
|
+
* @param targetDir Target directory to extract files to
|
|
106
|
+
* @param options Options for extraction behavior
|
|
107
|
+
* @returns Array of extracted file paths
|
|
108
|
+
* @example
|
|
109
|
+
* ```ts
|
|
110
|
+
* const fs = new Fs(process.cwd());
|
|
111
|
+
* // Extract all img-probe files to ./output
|
|
112
|
+
* const extracted = fs.extractFromTmp("img-probe", "./output");
|
|
113
|
+
* // Extract with custom behavior
|
|
114
|
+
* fs.extractFromTmp("session-", "./sessions", {
|
|
115
|
+
* preserveStructure: true,
|
|
116
|
+
* cleanupAfter: true
|
|
117
|
+
* });
|
|
118
|
+
* ```
|
|
119
|
+
*/
|
|
120
|
+
extractFromTmp(pattern, targetDir, options) {
|
|
121
|
+
const {
|
|
122
|
+
preserveStructure = false,
|
|
123
|
+
cleanupAfter = false,
|
|
124
|
+
overwrite = true
|
|
125
|
+
} = options ?? {};
|
|
126
|
+
const matchingFiles = this.scanTmp(pattern);
|
|
127
|
+
const extracted = [];
|
|
128
|
+
for (const file of matchingFiles) {
|
|
129
|
+
const sourcePath = resolve(this.tmpDir, file);
|
|
130
|
+
const lastSegment = file.includes("/") ? file.split("/").pop() : file;
|
|
131
|
+
if (!lastSegment) continue;
|
|
132
|
+
const targetPath = preserveStructure ? resolve(targetDir, file) : resolve(targetDir, lastSegment);
|
|
133
|
+
if (this.exists(targetPath) && !overwrite) {
|
|
134
|
+
console.warn(`Skipping existing file: ${targetPath}`);
|
|
135
|
+
continue;
|
|
136
|
+
}
|
|
137
|
+
const data = this.fileToBuffer(sourcePath);
|
|
138
|
+
this.withWs(targetPath, data);
|
|
139
|
+
extracted.push(targetPath);
|
|
140
|
+
if (cleanupAfter) {
|
|
141
|
+
this.rmFile(sourcePath);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
return extracted;
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Clean up tmp files matching a pattern
|
|
148
|
+
* @param pattern Pattern to match files for deletion
|
|
149
|
+
* @param maxAge Optional max age in milliseconds (only delete files older than this)
|
|
150
|
+
* @returns Number of files removed
|
|
151
|
+
* @example
|
|
152
|
+
* ```ts
|
|
153
|
+
* const fs = new Fs(process.cwd());
|
|
154
|
+
* // Remove all img-probe files
|
|
155
|
+
* fs.cleanupTmp("img-probe");
|
|
156
|
+
* // Remove session files older than 1 hour
|
|
157
|
+
* fs.cleanupTmp("session-", 60 * 60 * 1000);
|
|
158
|
+
* ```
|
|
159
|
+
*/
|
|
160
|
+
cleanupTmp(pattern, maxAge) {
|
|
161
|
+
const matchingFiles = this.scanTmp(pattern);
|
|
162
|
+
let removed = 0;
|
|
163
|
+
for (const file of matchingFiles) {
|
|
164
|
+
const fullPath = resolve(this.tmpDir, file);
|
|
165
|
+
try {
|
|
166
|
+
const stats = fsSync.statSync(fullPath);
|
|
167
|
+
if (maxAge) {
|
|
168
|
+
const age = Date.now() - stats.mtimeMs;
|
|
169
|
+
if (age < maxAge) {
|
|
170
|
+
continue;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
if (stats.isDirectory()) {
|
|
174
|
+
this.rmDirSync(fullPath);
|
|
175
|
+
} else {
|
|
176
|
+
this.rmFile(fullPath);
|
|
177
|
+
}
|
|
178
|
+
removed++;
|
|
179
|
+
} catch (err) {
|
|
180
|
+
console.warn(`Failed to remove tmp file ${file}:`, err);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
return removed;
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* Generate a unique tmp filename with optional prefix
|
|
187
|
+
* @param prefix Optional prefix for the filename
|
|
188
|
+
* @param extension Optional file extension (without dot)
|
|
189
|
+
* @returns Unique filename
|
|
190
|
+
* @example
|
|
191
|
+
* ```ts
|
|
192
|
+
* const fs = new Fs(process.cwd());
|
|
193
|
+
* const filename = fs.uniqueTmpName("img-probe", "png");
|
|
194
|
+
* // Returns: "img-probe-1701234567890-a3b2c1.png"
|
|
195
|
+
* ```
|
|
196
|
+
*/
|
|
197
|
+
uniqueTmpName(prefix, extension) {
|
|
198
|
+
const timestamp = Date.now();
|
|
199
|
+
const random = Math.random().toString(36).substring(2, 8);
|
|
200
|
+
const parts = [prefix ?? "tmp", timestamp, random].join("-");
|
|
201
|
+
return extension ? `${parts}.${extension}` : parts;
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Async generator for cleaning tmp files with batched removal
|
|
205
|
+
* @param pattern Pattern to match files for deletion
|
|
206
|
+
* @param batchSize Number of files to remove per batch (default 10)
|
|
207
|
+
* @yields Progress information during cleanup
|
|
208
|
+
* @returns Final cleanup summary
|
|
209
|
+
* @example
|
|
210
|
+
* ```ts
|
|
211
|
+
* const fs = new Fs(process.cwd());
|
|
212
|
+
* // Clean up with progress tracking
|
|
213
|
+
* for await (const progress of fs.cleanTmpGenerator("session-", 25)) {
|
|
214
|
+
* console.log(progress);
|
|
215
|
+
* }
|
|
216
|
+
* ```
|
|
217
|
+
*/
|
|
218
|
+
async *cleanTmpGenerator(pattern, batchSize = 10) {
|
|
219
|
+
const files = this.scanTmp(pattern);
|
|
220
|
+
const batches = await this.arrToArrOfArrs({
|
|
221
|
+
arrToFragment: files,
|
|
222
|
+
arrOfArrsAggregator: [],
|
|
223
|
+
interval: batchSize
|
|
224
|
+
});
|
|
225
|
+
let totalRemoved = 0;
|
|
226
|
+
for (const [index, batch] of batches.entries()) {
|
|
227
|
+
yield {
|
|
228
|
+
action: "removing",
|
|
229
|
+
batch: [...batch],
|
|
230
|
+
batchNumber: index + 1,
|
|
231
|
+
totalBatches: batches.length,
|
|
232
|
+
totalFound: files.length,
|
|
233
|
+
removed: totalRemoved
|
|
234
|
+
};
|
|
235
|
+
for (const file of batch) {
|
|
236
|
+
this.rmTmpFile(file);
|
|
237
|
+
totalRemoved++;
|
|
238
|
+
}
|
|
239
|
+
yield {
|
|
240
|
+
action: "batch-complete",
|
|
241
|
+
batchNumber: index + 1,
|
|
242
|
+
batchSize: batch.length,
|
|
243
|
+
totalRemoved,
|
|
244
|
+
remaining: files.length - totalRemoved
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
return {
|
|
248
|
+
action: "complete",
|
|
249
|
+
totalRemoved,
|
|
250
|
+
totalBatches: batches.length,
|
|
251
|
+
pattern: pattern.toString()
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* Async version of withWs with atomic write support and progress tracking
|
|
256
|
+
* @param path Target file path
|
|
257
|
+
* @param data Streamable data to write
|
|
258
|
+
* @param options Optional atomic write and abort signal
|
|
259
|
+
* @returns Promise with path and bytes written
|
|
260
|
+
* @example
|
|
261
|
+
* ```ts
|
|
262
|
+
* const fs = new Fs(process.cwd());
|
|
263
|
+
* // Simple async write
|
|
264
|
+
* const result = await fs.asyncWithWs("output.txt", "Hello World");
|
|
265
|
+
* console.log(`Wrote ${result.bytes} bytes to ${result.path}`);
|
|
266
|
+
*
|
|
267
|
+
* // Atomic write with temp file
|
|
268
|
+
* const { path, bytes } = await fs.asyncWithWs(
|
|
269
|
+
* "critical-data.json",
|
|
270
|
+
* JSON.stringify(data),
|
|
271
|
+
* { atomic: true }
|
|
272
|
+
* );
|
|
273
|
+
* ```
|
|
274
|
+
*/
|
|
275
|
+
async asyncWithWs(path, data, options) {
|
|
276
|
+
const { atomic = false, signal: _signal = AbortSignal } = options ?? {};
|
|
277
|
+
let bytes = 0;
|
|
278
|
+
if (!atomic) {
|
|
279
|
+
try {
|
|
280
|
+
if (/\//g.test(path)) {
|
|
281
|
+
return this.generateDirIfDNETmp(this.pathHandler(path), {
|
|
282
|
+
recursive: true
|
|
283
|
+
});
|
|
284
|
+
} else return path;
|
|
285
|
+
} catch (error) {
|
|
286
|
+
console.error(
|
|
287
|
+
`[asyncWithWs non-atomic error]: `.concat(
|
|
288
|
+
error instanceof Error ? error.message : typeof error === "string" ? error : JSON.stringify(error, null, 2)
|
|
289
|
+
)
|
|
290
|
+
);
|
|
291
|
+
} finally {
|
|
292
|
+
const readable = this.toReadable(data);
|
|
293
|
+
const chunks = [];
|
|
294
|
+
for await (const chunk of readable) {
|
|
295
|
+
chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
|
|
296
|
+
}
|
|
297
|
+
const buffer = Buffer.concat(chunks);
|
|
298
|
+
try {
|
|
299
|
+
this.withWs(path, buffer);
|
|
300
|
+
} catch (err) {
|
|
301
|
+
console.error(err);
|
|
302
|
+
} finally {
|
|
303
|
+
return { path, bytes: buffer.length };
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
let tmpName;
|
|
308
|
+
try {
|
|
309
|
+
if (/\//g.test(path)) {
|
|
310
|
+
this.generateDirIfDNETmp(this.pathHandler(path), {
|
|
311
|
+
recursive: true
|
|
312
|
+
});
|
|
313
|
+
}
|
|
314
|
+
tmpName = this.uniqueTmpName("atomic", "tmp");
|
|
315
|
+
} catch (error) {
|
|
316
|
+
console.error(
|
|
317
|
+
`[asyncWithWs atomic setup error]: `.concat(
|
|
318
|
+
error instanceof Error ? error.message : typeof error === "string" ? error : JSON.stringify(error, null, 2)
|
|
319
|
+
)
|
|
320
|
+
);
|
|
321
|
+
if (tmpName) {
|
|
322
|
+
try {
|
|
323
|
+
this.rmTmpFile(tmpName);
|
|
324
|
+
} catch (err) {
|
|
325
|
+
console.error(err);
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
} finally {
|
|
329
|
+
if (tmpName) {
|
|
330
|
+
let newPath = null;
|
|
331
|
+
let newBytes = 0;
|
|
332
|
+
const readable = this.toReadable(data);
|
|
333
|
+
const chunks = [];
|
|
334
|
+
for await (const chunk of readable) {
|
|
335
|
+
chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
|
|
336
|
+
}
|
|
337
|
+
const buffer = Buffer.concat(chunks);
|
|
338
|
+
bytes = buffer.length;
|
|
339
|
+
try {
|
|
340
|
+
await this.writeTmpAsync(tmpName, buffer.toString());
|
|
341
|
+
const tmpPath = resolve(this.tmpDir, tmpName);
|
|
342
|
+
const finalPath = resolve(this.cwd, path);
|
|
343
|
+
await fsAsync.rename(tmpPath, finalPath);
|
|
344
|
+
newPath = path;
|
|
345
|
+
newBytes = bytes;
|
|
346
|
+
} catch (err) {
|
|
347
|
+
console.error(`[asyncWithWs atomic rename error]:`, err);
|
|
348
|
+
if (this.existsTmp(tmpName)) {
|
|
349
|
+
try {
|
|
350
|
+
this.rmTmpFile(tmpName);
|
|
351
|
+
} catch (err2) {
|
|
352
|
+
console.error(err2);
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
throw err;
|
|
356
|
+
}
|
|
357
|
+
return { path, bytes, newPath, newBytes };
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
return { path, bytes: 0 };
|
|
361
|
+
}
|
|
362
|
+
};
|
|
363
|
+
|
|
364
|
+
export {
|
|
365
|
+
FsTmp
|
|
366
|
+
};
|
|
367
|
+
//# sourceMappingURL=chunk-TDMPLELZ.js.map
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
UtilsService
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-6HHKXBIM.js";
|
|
4
4
|
import {
|
|
5
5
|
__name
|
|
6
6
|
} from "./chunk-7QVYU63E.js";
|
|
@@ -30,4 +30,4 @@ var UrlService = class extends UtilsService {
|
|
|
30
30
|
export {
|
|
31
31
|
UrlService
|
|
32
32
|
};
|
|
33
|
-
//# sourceMappingURL=chunk-
|
|
33
|
+
//# sourceMappingURL=chunk-UW52DMHT.js.map
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import {
|
|
2
|
+
FsBase
|
|
3
|
+
} from "./chunk-7FHBNIB7.js";
|
|
4
|
+
import {
|
|
5
|
+
__name
|
|
6
|
+
} from "./chunk-7QVYU63E.js";
|
|
7
|
+
|
|
8
|
+
// src/fs-atomic/index.ts
|
|
9
|
+
import fsSync from "fs";
|
|
10
|
+
import fsAsync from "fs/promises";
|
|
11
|
+
import { execSync } from "child_process";
|
|
12
|
+
import { join, relative, resolve } from "path";
|
|
13
|
+
var FsAtomic = class extends FsBase {
|
|
14
|
+
constructor(cwd) {
|
|
15
|
+
super(cwd ??= process.cwd());
|
|
16
|
+
this.cwd = cwd;
|
|
17
|
+
}
|
|
18
|
+
static {
|
|
19
|
+
__name(this, "FsAtomic");
|
|
20
|
+
}
|
|
21
|
+
executeCommand = /* @__PURE__ */ __name(({
|
|
22
|
+
command,
|
|
23
|
+
...options
|
|
24
|
+
}) => Buffer.from(execSync(command, { ...options }).toJSON().data).toString(
|
|
25
|
+
"utf-8"
|
|
26
|
+
), "executeCommand");
|
|
27
|
+
mkdirSync(path, options = { mode: 511, recursive: true }) {
|
|
28
|
+
return fsSync.mkdirSync(relative(this.cwd, path), options);
|
|
29
|
+
}
|
|
30
|
+
generateDirIfDNE(path, options = { mode: 511, recursive: true }) {
|
|
31
|
+
if (this.exists(path)) return;
|
|
32
|
+
else {
|
|
33
|
+
return this.mkdirSync(path, options);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
fileToBuffer = /* @__PURE__ */ __name((path) => Buffer.from(fsSync.readFileSync(relative(this.cwd, path)).toJSON().data), "fileToBuffer");
|
|
37
|
+
fileToBufferAsync = /* @__PURE__ */ __name(async (path) => {
|
|
38
|
+
return await fsAsync.readFile(relative(this.cwd, path));
|
|
39
|
+
}, "fileToBufferAsync");
|
|
40
|
+
dirContainsDir(readDir, targetDir, options) {
|
|
41
|
+
return this.readDir(readDir, options).filter((t) => t.split(".").length === 1).includes(targetDir);
|
|
42
|
+
}
|
|
43
|
+
rmFile(filePath, options = {
|
|
44
|
+
force: true,
|
|
45
|
+
maxRetries: 0,
|
|
46
|
+
recursive: true,
|
|
47
|
+
retryDelay: 100
|
|
48
|
+
}) {
|
|
49
|
+
if (this.exists(filePath)) {
|
|
50
|
+
if (this.isRootPathTargeted(filePath)) {
|
|
51
|
+
fsSync.rmSync(resolve(join(this.cwd, filePath)), options);
|
|
52
|
+
} else fsSync.rmSync(resolve(this.cwd, filePath), options);
|
|
53
|
+
} else return;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Removes files and directories (modeled on the standard POSIX `rm` utility).
|
|
57
|
+
* To get a behavior similar to the `rm -rf` Unix command, use options `{ recursive: true, force: true }`.
|
|
58
|
+
* @return Fulfills with `undefined` upon success.
|
|
59
|
+
*/
|
|
60
|
+
async rmdir(dir, options = {
|
|
61
|
+
maxRetries: 0,
|
|
62
|
+
retryDelay: 100,
|
|
63
|
+
recursive: true,
|
|
64
|
+
force: true
|
|
65
|
+
}) {
|
|
66
|
+
const resolved = relative(this.cwd, dir);
|
|
67
|
+
if (this.exists(dir)) {
|
|
68
|
+
return await fsAsync.rm(relative(this.cwd, dir), options);
|
|
69
|
+
} else {
|
|
70
|
+
throw new Error(`directory ${resolved} does not exist`);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
rmDirSync(dir, options = {
|
|
74
|
+
force: true,
|
|
75
|
+
recursive: true,
|
|
76
|
+
maxRetries: 0,
|
|
77
|
+
retryDelay: 100
|
|
78
|
+
}, callback = (err) => err?.message ? console.error(err.message) : console.error(`directory ${dir} does not exist`)) {
|
|
79
|
+
const resolved = resolve(this.cwd, dir);
|
|
80
|
+
if (this.exists(dir)) {
|
|
81
|
+
return fsSync.rm(resolved, options, callback);
|
|
82
|
+
} else {
|
|
83
|
+
throw new Error(`directory ${resolved} does not exist`);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
existsTmp(path) {
|
|
87
|
+
return this.exists(resolve(this.tmpDir, path));
|
|
88
|
+
}
|
|
89
|
+
mkdirTmp(path, options = { mode: 511, recursive: true }) {
|
|
90
|
+
return this.mkdirSync(resolve(this.tmpDir, path), options);
|
|
91
|
+
}
|
|
92
|
+
generateDirIfDNETmp(path, options) {
|
|
93
|
+
if (this.existsTmp(path)) return;
|
|
94
|
+
else {
|
|
95
|
+
return this.mkdirTmp(path, options);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
rmTmpFile(filename) {
|
|
99
|
+
const tmpPath = resolve(this.tmpDir, filename);
|
|
100
|
+
this.rmFile(tmpPath);
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
export {
|
|
105
|
+
FsAtomic
|
|
106
|
+
};
|
|
107
|
+
//# sourceMappingURL=chunk-XCA34DKZ.js.map
|