1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
// Node.js fs/promises API
// Minimal implementation compatible with Node.js
'use strict';
// eslint-disable-next-line no-unused-expressions
(({ readFile: _readFile, readDir: _readDir, writeFile: _writeFile, appendFile: _appendFile, mkdir: _mkdir, rmdir: _rmdir, unlink: _unlink, rename: _rename, copyFile: _copyFile, stat: _stat, access: _access, rm: _rm, truncate: _truncate, realpath: _realpath, chmod: _chmod, mkdtemp: _mkdtemp, readlink: _readlink, symlink: _symlink, lstat: _lstat, chown: _chown, utimes: _utimes }) => {
// Helper to convert synchronous operations to promises
function promisify(fn, ...args) {
return new Promise((resolve, reject) => {
try {
const result = fn(...args);
resolve(result);
} catch (error) {
reject(error);
}
});
}
/**
* Reads the entire contents of a file.
* @param {string | Buffer | URL} path - filename or file descriptor
* @param {Object | string} options - encoding or options object
* @returns {Promise<string | Buffer>}
*/
function readFile(path, options) {
let encoding = null;
let flag = 'r';
// Handle options parameter
if (typeof options === 'string') {
encoding = options;
} else if (options && typeof options === 'object') {
encoding = options.encoding || null;
flag = options.flag || 'r';
}
return promisify(_readFile, path, encoding);
}
/**
* Reads the contents of a directory.
* @param {string | Buffer | URL} path - path to directory
* @param {Object | string} options - encoding or options object
* @returns {Promise<string[]>}
*/
function readdir(path, options) {
let encoding = 'utf8';
let withFileTypes = false;
// Handle options parameter
if (typeof options === 'string') {
encoding = options;
} else if (options && typeof options === 'object') {
encoding = options.encoding || 'utf8';
withFileTypes = options.withFileTypes || false;
}
if (withFileTypes) {
return Promise.reject(new Error('withFileTypes option is not yet supported'));
}
return promisify(_readDir, path);
}
/**
* Writes data to a file.
* @param {string | Buffer | URL} path - filename
* @param {string | Buffer} data - data to write
* @param {Object | string} options - encoding or options object
* @returns {Promise<void>}
*/
function writeFile(path, data, options) {
// Simply pass the data to the native function
// If it's a string, Rust will handle it
// If it's a Uint8Array, Rust will handle it too
return promisify(_writeFile, path, data);
}
/**
* Appends data to a file, creating the file if it doesn't exist.
* @param {string | Buffer | URL} path - filename
* @param {string | Buffer} data - data to append
* @param {Object | string} options - encoding or options object
* @returns {Promise<void>}
*/
function appendFile(path, data, options) {
return promisify(_appendFile, path, data);
}
/**
* Creates a directory.
* @param {string | Buffer | URL} path - directory path
* @param {Object} options - options object
* @returns {Promise<void>}
*/
function mkdir(path, options) {
return promisify(_mkdir, path, options);
}
/**
* Removes a directory.
* @param {string | Buffer | URL} path - directory path
* @param {Object} options - options object
* @returns {Promise<void>}
*/
function rmdir(path, options) {
return promisify(_rmdir, path, options);
}
/**
* Deletes a file.
* @param {string | Buffer | URL} path - file path
* @returns {Promise<void>}
*/
function unlink(path) {
return promisify(_unlink, path);
}
/**
* Renames a file or directory.
* @param {string | Buffer | URL} oldPath - old path
* @param {string | Buffer | URL} newPath - new path
* @returns {Promise<void>}
*/
function rename(oldPath, newPath) {
return promisify(_rename, oldPath, newPath);
}
/**
* Copies a file.
* @param {string | Buffer | URL} src - source path
* @param {string | Buffer | URL} dest - destination path
* @param {number} mode - copy mode (optional)
* @returns {Promise<void>}
*/
function copyFile(src, dest, mode) {
return promisify(_copyFile, src, dest, mode);
}
/**
* Gets file statistics.
* @param {string | Buffer | URL} path - file path
* @param {Object} options - options object
* @returns {Promise<Stats>}
*/
function stat(path, options) {
return promisify(_stat, path, options);
}
/**
* Tests file accessibility.
* @param {string | Buffer | URL} path - file path
* @param {number} mode - accessibility mode
* @returns {Promise<void>}
*/
function access(path, mode) {
return promisify(_access, path, mode);
}
/**
* Removes files and directories (modern alternative to unlink/rmdir).
* @param {string | Buffer | URL} path - path to remove
* @param {Object} options - options object
* @returns {Promise<void>}
*/
function rm(path, options) {
return promisify(_rm, path, options);
}
/**
* Truncates a file to a specified length.
* @param {string | Buffer | URL} path - file path
* @param {number} len - target length (defaults to 0)
* @returns {Promise<void>}
*/
function truncate(path, len) {
return promisify(_truncate, path, len);
}
/**
* Resolves path to an absolute path.
* @param {string | Buffer | URL} path - path to resolve
* @param {Object} options - options object
* @returns {Promise<string>}
*/
function realpath(path, options) {
return promisify(_realpath, path, options);
}
/**
* Changes file permissions (Unix-like systems only).
* @param {string | Buffer | URL} path - file path
* @param {number} mode - file mode (permissions)
* @returns {Promise<void>}
*/
function chmod(path, mode) {
return promisify(_chmod, path, mode);
}
/**
* Creates a unique temporary directory.
* @param {string} prefix - directory name prefix
* @param {Object | string} options - encoding or options object
* @returns {Promise<string>}
*/
function mkdtemp(prefix, options) {
return promisify(_mkdtemp, prefix, options);
}
/**
* Reads the target of a symbolic link.
* @param {string | Buffer | URL} path - path to symlink
* @param {Object | string} options - encoding or options object
* @returns {Promise<string>}
*/
function readlink(path, options) {
return promisify(_readlink, path, options);
}
/**
* Creates a symbolic link.
* @param {string | Buffer | URL} target - target path
* @param {string | Buffer | URL} path - symlink path
* @param {string} type - type of symlink (optional, for Windows)
* @returns {Promise<void>}
*/
function symlink(target, path, type) {
return promisify(_symlink, target, path, type);
}
/**
* Gets file statistics without following symlinks.
* @param {string | Buffer | URL} path - file path
* @param {Object} options - options object
* @returns {Promise<Stats>}
*/
function lstat(path, options) {
return promisify(_lstat, path, options);
}
/**
* Changes file ownership (Unix-like systems only).
* @param {string | Buffer | URL} path - file path
* @param {number} uid - user ID
* @param {number} gid - group ID
* @returns {Promise<void>}
*/
function chown(path, uid, gid) {
return promisify(_chown, path, uid, gid);
}
/**
* Changes file access and modification times.
* @param {string | Buffer | URL} path - file path
* @param {number | Date} atime - access time
* @param {number | Date} mtime - modification time
* @returns {Promise<void>}
*/
function utimes(path, atime, mtime) {
return promisify(_utimes, path, atime, mtime);
}
// Constants
const constants = {
F_OK: 0, // File exists
R_OK: 4, // File is readable
W_OK: 2, // File is writable
X_OK: 1, // File is executable
};
// Export the fs/promises API
const fsPromises = {
readFile,
readdir,
writeFile,
appendFile,
mkdir,
rmdir,
unlink,
rename,
copyFile,
stat,
access,
rm,
truncate,
realpath,
chmod,
mkdtemp,
readlink,
symlink,
lstat,
chown,
utimes,
constants,
};
// Make it available via import
globalThis.__node_modules = globalThis.__node_modules || {};
globalThis.__node_modules['node:fs/promises'] = fsPromises;
globalThis.__node_modules['fs/promises'] = fsPromises;
});