1 | /**
|
2 | * The `fs/promises` API provides asynchronous file system methods that return
|
3 | * promises.
|
4 | *
|
5 | * The promise APIs use the underlying Node.js threadpool to perform file
|
6 | * system operations off the event loop thread. These operations are not
|
7 | * synchronized or threadsafe. Care must be taken when performing multiple
|
8 | * concurrent modifications on the same file or data corruption may occur.
|
9 | * @since v10.0.0
|
10 | */
|
11 | declare module 'fs/promises' {
|
12 | import { Abortable } from 'node:events';
|
13 | import { Stream } from 'node:stream';
|
14 | import {
|
15 | Stats,
|
16 | BigIntStats,
|
17 | StatOptions,
|
18 | WriteVResult,
|
19 | ReadVResult,
|
20 | PathLike,
|
21 | RmDirOptions,
|
22 | RmOptions,
|
23 | MakeDirectoryOptions,
|
24 | Dirent,
|
25 | OpenDirOptions,
|
26 | Dir,
|
27 | ObjectEncodingOptions,
|
28 | BufferEncodingOption,
|
29 | OpenMode,
|
30 | Mode,
|
31 | WatchOptions,
|
32 | WatchEventType,
|
33 | CopyOptions,
|
34 | } from 'node:fs';
|
35 | interface FileChangeInfo<T extends string | Buffer> {
|
36 | eventType: WatchEventType;
|
37 | filename: T;
|
38 | }
|
39 | interface FlagAndOpenMode {
|
40 | mode?: Mode | undefined;
|
41 | flag?: OpenMode | undefined;
|
42 | }
|
43 | interface FileReadResult<T extends ArrayBufferView> {
|
44 | bytesRead: number;
|
45 | buffer: T;
|
46 | }
|
47 | interface FileReadOptions<T extends ArrayBufferView = Buffer> {
|
48 | /**
|
49 | * @default `Buffer.alloc(0xffff)`
|
50 | */
|
51 | buffer?: T;
|
52 | /**
|
53 | * @default 0
|
54 | */
|
55 | offset?: number | null;
|
56 | /**
|
57 | * @default `buffer.byteLength`
|
58 | */
|
59 | length?: number | null;
|
60 | position?: number | null;
|
61 | }
|
62 | // TODO: Add `EventEmitter` close
|
63 | interface FileHandle {
|
64 | /**
|
65 | * The numeric file descriptor managed by the {FileHandle} object.
|
66 | * @since v10.0.0
|
67 | */
|
68 | readonly fd: number;
|
69 | /**
|
70 | * Alias of `filehandle.writeFile()`.
|
71 | *
|
72 | * When operating on file handles, the mode cannot be changed from what it was set
|
73 | * to with `fsPromises.open()`. Therefore, this is equivalent to `filehandle.writeFile()`.
|
74 | * @since v10.0.0
|
75 | * @return Fulfills with `undefined` upon success.
|
76 | */
|
77 | appendFile(data: string | Uint8Array, options?: (ObjectEncodingOptions & FlagAndOpenMode) | BufferEncoding | null): Promise<void>;
|
78 | /**
|
79 | * Changes the ownership of the file. A wrapper for [`chown(2)`](http://man7.org/linux/man-pages/man2/chown.2.html).
|
80 | * @since v10.0.0
|
81 | * @param uid The file's new owner's user id.
|
82 | * @param gid The file's new group's group id.
|
83 | * @return Fulfills with `undefined` upon success.
|
84 | */
|
85 | chown(uid: number, gid: number): Promise<void>;
|
86 | /**
|
87 | * Modifies the permissions on the file. See [`chmod(2)`](http://man7.org/linux/man-pages/man2/chmod.2.html).
|
88 | * @since v10.0.0
|
89 | * @param mode the file mode bit mask.
|
90 | * @return Fulfills with `undefined` upon success.
|
91 | */
|
92 | chmod(mode: Mode): Promise<void>;
|
93 | /**
|
94 | * Forces all currently queued I/O operations associated with the file to the
|
95 | * operating system's synchronized I/O completion state. Refer to the POSIX [`fdatasync(2)`](http://man7.org/linux/man-pages/man2/fdatasync.2.html) documentation for details.
|
96 | *
|
97 | * Unlike `filehandle.sync` this method does not flush modified metadata.
|
98 | * @since v10.0.0
|
99 | * @return Fulfills with `undefined` upon success.
|
100 | */
|
101 | datasync(): Promise<void>;
|
102 | /**
|
103 | * Request that all data for the open file descriptor is flushed to the storage
|
104 | * device. The specific implementation is operating system and device specific.
|
105 | * Refer to the POSIX [`fsync(2)`](http://man7.org/linux/man-pages/man2/fsync.2.html) documentation for more detail.
|
106 | * @since v10.0.0
|
107 | * @return Fufills with `undefined` upon success.
|
108 | */
|
109 | sync(): Promise<void>;
|
110 | /**
|
111 | * Reads data from the file and stores that in the given buffer.
|
112 | *
|
113 | * If the file is not modified concurrently, the end-of-file is reached when the
|
114 | * number of bytes read is zero.
|
115 | * @since v10.0.0
|
116 | * @param buffer A buffer that will be filled with the file data read.
|
117 | * @param offset The location in the buffer at which to start filling.
|
118 | * @param length The number of bytes to read.
|
119 | * @param position The location where to begin reading data from the file. If `null`, data will be read from the current file position, and the position will be updated. If `position` is an
|
120 | * integer, the current file position will remain unchanged.
|
121 | * @return Fulfills upon success with an object with two properties:
|
122 | */
|
123 | read<T extends ArrayBufferView>(buffer: T, offset?: number | null, length?: number | null, position?: number | null): Promise<FileReadResult<T>>;
|
124 | read<T extends ArrayBufferView = Buffer>(options?: FileReadOptions<T>): Promise<FileReadResult<T>>;
|
125 | /**
|
126 | * Asynchronously reads the entire contents of a file.
|
127 | *
|
128 | * If `options` is a string, then it specifies the `encoding`.
|
129 | *
|
130 | * The `FileHandle` has to support reading.
|
131 | *
|
132 | * If one or more `filehandle.read()` calls are made on a file handle and then a`filehandle.readFile()` call is made, the data will be read from the current
|
133 | * position till the end of the file. It doesn't always read from the beginning
|
134 | * of the file.
|
135 | * @since v10.0.0
|
136 | * @return Fulfills upon a successful read with the contents of the file. If no encoding is specified (using `options.encoding`), the data is returned as a {Buffer} object. Otherwise, the
|
137 | * data will be a string.
|
138 | */
|
139 | readFile(
|
140 | options?: {
|
141 | encoding?: null | undefined;
|
142 | flag?: OpenMode | undefined;
|
143 | } | null
|
144 | ): Promise<Buffer>;
|
145 | /**
|
146 | * Asynchronously reads the entire contents of a file. The underlying file will _not_ be closed automatically.
|
147 | * The `FileHandle` must have been opened for reading.
|
148 | * @param options An object that may contain an optional flag.
|
149 | * If a flag is not provided, it defaults to `'r'`.
|
150 | */
|
151 | readFile(
|
152 | options:
|
153 | | {
|
154 | encoding: BufferEncoding;
|
155 | flag?: OpenMode | undefined;
|
156 | }
|
157 | | BufferEncoding
|
158 | ): Promise<string>;
|
159 | /**
|
160 | * Asynchronously reads the entire contents of a file. The underlying file will _not_ be closed automatically.
|
161 | * The `FileHandle` must have been opened for reading.
|
162 | * @param options An object that may contain an optional flag.
|
163 | * If a flag is not provided, it defaults to `'r'`.
|
164 | */
|
165 | readFile(
|
166 | options?:
|
167 | | (ObjectEncodingOptions & {
|
168 | flag?: OpenMode | undefined;
|
169 | })
|
170 | | BufferEncoding
|
171 | | null
|
172 | ): Promise<string | Buffer>;
|
173 | /**
|
174 | * @since v10.0.0
|
175 | * @return Fulfills with an {fs.Stats} for the file.
|
176 | */
|
177 | stat(
|
178 | opts?: StatOptions & {
|
179 | bigint?: false | undefined;
|
180 | }
|
181 | ): Promise<Stats>;
|
182 | stat(
|
183 | opts: StatOptions & {
|
184 | bigint: true;
|
185 | }
|
186 | ): Promise<BigIntStats>;
|
187 | stat(opts?: StatOptions): Promise<Stats | BigIntStats>;
|
188 | /**
|
189 | * Truncates the file.
|
190 | *
|
191 | * If the file was larger than `len` bytes, only the first `len` bytes will be
|
192 | * retained in the file.
|
193 | *
|
194 | * The following example retains only the first four bytes of the file:
|
195 | *
|
196 | * ```js
|
197 | * import { open } from 'fs/promises';
|
198 | *
|
199 | * let filehandle = null;
|
200 | * try {
|
201 | * filehandle = await open('temp.txt', 'r+');
|
202 | * await filehandle.truncate(4);
|
203 | * } finally {
|
204 | * await filehandle?.close();
|
205 | * }
|
206 | * ```
|
207 | *
|
208 | * If the file previously was shorter than `len` bytes, it is extended, and the
|
209 | * extended part is filled with null bytes (`'\0'`):
|
210 | *
|
211 | * If `len` is negative then `0` will be used.
|
212 | * @since v10.0.0
|
213 | * @param [len=0]
|
214 | * @return Fulfills with `undefined` upon success.
|
215 | */
|
216 | truncate(len?: number): Promise<void>;
|
217 | /**
|
218 | * Change the file system timestamps of the object referenced by the `FileHandle` then resolves the promise with no arguments upon success.
|
219 | * @since v10.0.0
|
220 | */
|
221 | utimes(atime: string | number | Date, mtime: string | number | Date): Promise<void>;
|
222 | /**
|
223 | * Asynchronously writes data to a file, replacing the file if it already exists.`data` can be a string, a buffer, an
|
224 | * [AsyncIterable](https://tc39.github.io/ecma262/#sec-asynciterable-interface) or
|
225 | * [Iterable](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterable_protocol) object, or an
|
226 | * object with an own `toString` function
|
227 | * property. The promise is resolved with no arguments upon success.
|
228 | *
|
229 | * If `options` is a string, then it specifies the `encoding`.
|
230 | *
|
231 | * The `FileHandle` has to support writing.
|
232 | *
|
233 | * It is unsafe to use `filehandle.writeFile()` multiple times on the same file
|
234 | * without waiting for the promise to be resolved (or rejected).
|
235 | *
|
236 | * If one or more `filehandle.write()` calls are made on a file handle and then a`filehandle.writeFile()` call is made, the data will be written from the
|
237 | * current position till the end of the file. It doesn't always write from the
|
238 | * beginning of the file.
|
239 | * @since v10.0.0
|
240 | */
|
241 | writeFile(data: string | Uint8Array, options?: (ObjectEncodingOptions & FlagAndOpenMode & Abortable) | BufferEncoding | null): Promise<void>;
|
242 | /**
|
243 | * Write `buffer` to the file.
|
244 | *
|
245 | * If `buffer` is a plain object, it must have an own (not inherited) `toString`function property.
|
246 | *
|
247 | * The promise is resolved with an object containing two properties:
|
248 | *
|
249 | * It is unsafe to use `filehandle.write()` multiple times on the same file
|
250 | * without waiting for the promise to be resolved (or rejected). For this
|
251 | * scenario, use `fs.createWriteStream()`.
|
252 | *
|
253 | * On Linux, positional writes do not work when the file is opened in append mode.
|
254 | * The kernel ignores the position argument and always appends the data to
|
255 | * the end of the file.
|
256 | * @since v10.0.0
|
257 | * @param [offset=0] The start position from within `buffer` where the data to write begins.
|
258 | * @param [length=buffer.byteLength] The number of bytes from `buffer` to write.
|
259 | * @param position The offset from the beginning of the file where the data from `buffer` should be written. If `position` is not a `number`, the data will be written at the current position.
|
260 | * See the POSIX pwrite(2) documentation for more detail.
|
261 | */
|
262 | write<TBuffer extends Uint8Array>(
|
263 | buffer: TBuffer,
|
264 | offset?: number | null,
|
265 | length?: number | null,
|
266 | position?: number | null
|
267 | ): Promise<{
|
268 | bytesWritten: number;
|
269 | buffer: TBuffer;
|
270 | }>;
|
271 | write(
|
272 | data: string,
|
273 | position?: number | null,
|
274 | encoding?: BufferEncoding | null
|
275 | ): Promise<{
|
276 | bytesWritten: number;
|
277 | buffer: string;
|
278 | }>;
|
279 | /**
|
280 | * Write an array of [ArrayBufferView](https://developer.mozilla.org/en-US/docs/Web/API/ArrayBufferView) s to the file.
|
281 | *
|
282 | * The promise is resolved with an object containing a two properties:
|
283 | *
|
284 | * It is unsafe to call `writev()` multiple times on the same file without waiting
|
285 | * for the promise to be resolved (or rejected).
|
286 | *
|
287 | * On Linux, positional writes don't work when the file is opened in append mode.
|
288 | * The kernel ignores the position argument and always appends the data to
|
289 | * the end of the file.
|
290 | * @since v12.9.0
|
291 | * @param position The offset from the beginning of the file where the data from `buffers` should be written. If `position` is not a `number`, the data will be written at the current
|
292 | * position.
|
293 | */
|
294 | writev(buffers: ReadonlyArray<NodeJS.ArrayBufferView>, position?: number): Promise<WriteVResult>;
|
295 | /**
|
296 | * Read from a file and write to an array of [ArrayBufferView](https://developer.mozilla.org/en-US/docs/Web/API/ArrayBufferView) s
|
297 | * @since v13.13.0, v12.17.0
|
298 | * @param position The offset from the beginning of the file where the data should be read from. If `position` is not a `number`, the data will be read from the current position.
|
299 | * @return Fulfills upon success an object containing two properties:
|
300 | */
|
301 | readv(buffers: ReadonlyArray<NodeJS.ArrayBufferView>, position?: number): Promise<ReadVResult>;
|
302 | /**
|
303 | * Closes the file handle after waiting for any pending operation on the handle to
|
304 | * complete.
|
305 | *
|
306 | * ```js
|
307 | * import { open } from 'fs/promises';
|
308 | *
|
309 | * let filehandle;
|
310 | * try {
|
311 | * filehandle = await open('thefile.txt', 'r');
|
312 | * } finally {
|
313 | * await filehandle?.close();
|
314 | * }
|
315 | * ```
|
316 | * @since v10.0.0
|
317 | * @return Fulfills with `undefined` upon success.
|
318 | */
|
319 | close(): Promise<void>;
|
320 | }
|
321 | /**
|
322 | * Tests a user's permissions for the file or directory specified by `path`.
|
323 | * The `mode` argument is an optional integer that specifies the accessibility
|
324 | * checks to be performed. Check `File access constants` for possible values
|
325 | * of `mode`. It is possible to create a mask consisting of the bitwise OR of
|
326 | * two or more values (e.g. `fs.constants.W_OK | fs.constants.R_OK`).
|
327 | *
|
328 | * If the accessibility check is successful, the promise is resolved with no
|
329 | * value. If any of the accessibility checks fail, the promise is rejected
|
330 | * with an [Error](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error) object. The following example checks if the file`/etc/passwd` can be read and
|
331 | * written by the current process.
|
332 | *
|
333 | * ```js
|
334 | * import { access } from 'fs/promises';
|
335 | * import { constants } from 'fs';
|
336 | *
|
337 | * try {
|
338 | * await access('/etc/passwd', constants.R_OK | constants.W_OK);
|
339 | * console.log('can access');
|
340 | * } catch {
|
341 | * console.error('cannot access');
|
342 | * }
|
343 | * ```
|
344 | *
|
345 | * Using `fsPromises.access()` to check for the accessibility of a file before
|
346 | * calling `fsPromises.open()` is not recommended. Doing so introduces a race
|
347 | * condition, since other processes may change the file's state between the two
|
348 | * calls. Instead, user code should open/read/write the file directly and handle
|
349 | * the error raised if the file is not accessible.
|
350 | * @since v10.0.0
|
351 | * @param [mode=fs.constants.F_OK]
|
352 | * @return Fulfills with `undefined` upon success.
|
353 | */
|
354 | function access(path: PathLike, mode?: number): Promise<void>;
|
355 | /**
|
356 | * Asynchronously copies `src` to `dest`. By default, `dest` is overwritten if it
|
357 | * already exists.
|
358 | *
|
359 | * No guarantees are made about the atomicity of the copy operation. If an
|
360 | * error occurs after the destination file has been opened for writing, an attempt
|
361 | * will be made to remove the destination.
|
362 | *
|
363 | * ```js
|
364 | * import { constants } from 'fs';
|
365 | * import { copyFile } from 'fs/promises';
|
366 | *
|
367 | * try {
|
368 | * await copyFile('source.txt', 'destination.txt');
|
369 | * console.log('source.txt was copied to destination.txt');
|
370 | * } catch {
|
371 | * console.log('The file could not be copied');
|
372 | * }
|
373 | *
|
374 | * // By using COPYFILE_EXCL, the operation will fail if destination.txt exists.
|
375 | * try {
|
376 | * await copyFile('source.txt', 'destination.txt', constants.COPYFILE_EXCL);
|
377 | * console.log('source.txt was copied to destination.txt');
|
378 | * } catch {
|
379 | * console.log('The file could not be copied');
|
380 | * }
|
381 | * ```
|
382 | * @since v10.0.0
|
383 | * @param src source filename to copy
|
384 | * @param dest destination filename of the copy operation
|
385 | * @param [mode=0] Optional modifiers that specify the behavior of the copy operation. It is possible to create a mask consisting of the bitwise OR of two or more values (e.g.
|
386 | * `fs.constants.COPYFILE_EXCL | fs.constants.COPYFILE_FICLONE`)
|
387 | * @return Fulfills with `undefined` upon success.
|
388 | */
|
389 | function copyFile(src: PathLike, dest: PathLike, mode?: number): Promise<void>;
|
390 | /**
|
391 | * Opens a `FileHandle`.
|
392 | *
|
393 | * Refer to the POSIX [`open(2)`](http://man7.org/linux/man-pages/man2/open.2.html) documentation for more detail.
|
394 | *
|
395 | * Some characters (`< > : " / \ | ? *`) are reserved under Windows as documented
|
396 | * by [Naming Files, Paths, and Namespaces](https://docs.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file). Under NTFS, if the filename contains
|
397 | * a colon, Node.js will open a file system stream, as described by [this MSDN page](https://docs.microsoft.com/en-us/windows/desktop/FileIO/using-streams).
|
398 | * @since v10.0.0
|
399 | * @param [flags='r'] See `support of file system `flags``.
|
400 | * @param [mode=0o666] Sets the file mode (permission and sticky bits) if the file is created.
|
401 | * @return Fulfills with a {FileHandle} object.
|
402 | */
|
403 | function open(path: PathLike, flags: string | number, mode?: Mode): Promise<FileHandle>;
|
404 | /**
|
405 | * Renames `oldPath` to `newPath`.
|
406 | * @since v10.0.0
|
407 | * @return Fulfills with `undefined` upon success.
|
408 | */
|
409 | function rename(oldPath: PathLike, newPath: PathLike): Promise<void>;
|
410 | /**
|
411 | * Truncates (shortens or extends the length) of the content at `path` to `len`bytes.
|
412 | * @since v10.0.0
|
413 | * @param [len=0]
|
414 | * @return Fulfills with `undefined` upon success.
|
415 | */
|
416 | function truncate(path: PathLike, len?: number): Promise<void>;
|
417 | /**
|
418 | * Removes the directory identified by `path`.
|
419 | *
|
420 | * Using `fsPromises.rmdir()` on a file (not a directory) results in the
|
421 | * promise being rejected with an `ENOENT` error on Windows and an `ENOTDIR`error on POSIX.
|
422 | *
|
423 | * To get a behavior similar to the `rm -rf` Unix command, use `fsPromises.rm()` with options `{ recursive: true, force: true }`.
|
424 | * @since v10.0.0
|
425 | * @return Fulfills with `undefined` upon success.
|
426 | */
|
427 | function rmdir(path: PathLike, options?: RmDirOptions): Promise<void>;
|
428 | /**
|
429 | * Removes files and directories (modeled on the standard POSIX `rm` utility).
|
430 | * @since v14.14.0
|
431 | * @return Fulfills with `undefined` upon success.
|
432 | */
|
433 | function rm(path: PathLike, options?: RmOptions): Promise<void>;
|
434 | /**
|
435 | * Asynchronously creates a directory.
|
436 | *
|
437 | * The optional `options` argument can be an integer specifying `mode` (permission
|
438 | * and sticky bits), or an object with a `mode` property and a `recursive`property indicating whether parent directories should be created. Calling`fsPromises.mkdir()` when `path` is a directory
|
439 | * that exists results in a
|
440 | * rejection only when `recursive` is false.
|
441 | * @since v10.0.0
|
442 | * @return Upon success, fulfills with `undefined` if `recursive` is `false`, or the first directory path created if `recursive` is `true`.
|
443 | */
|
444 | function mkdir(
|
445 | path: PathLike,
|
446 | options: MakeDirectoryOptions & {
|
447 | recursive: true;
|
448 | }
|
449 | ): Promise<string | undefined>;
|
450 | /**
|
451 | * Asynchronous mkdir(2) - create a directory.
|
452 | * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
|
453 | * @param options Either the file mode, or an object optionally specifying the file mode and whether parent folders
|
454 | * should be created. If a string is passed, it is parsed as an octal integer. If not specified, defaults to `0o777`.
|
455 | */
|
456 | function mkdir(
|
457 | path: PathLike,
|
458 | options?:
|
459 | | Mode
|
460 | | (MakeDirectoryOptions & {
|
461 | recursive?: false | undefined;
|
462 | })
|
463 | | null
|
464 | ): Promise<void>;
|
465 | /**
|
466 | * Asynchronous mkdir(2) - create a directory.
|
467 | * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
|
468 | * @param options Either the file mode, or an object optionally specifying the file mode and whether parent folders
|
469 | * should be created. If a string is passed, it is parsed as an octal integer. If not specified, defaults to `0o777`.
|
470 | */
|
471 | function mkdir(path: PathLike, options?: Mode | MakeDirectoryOptions | null): Promise<string | undefined>;
|
472 | /**
|
473 | * Reads the contents of a directory.
|
474 | *
|
475 | * The optional `options` argument can be a string specifying an encoding, or an
|
476 | * object with an `encoding` property specifying the character encoding to use for
|
477 | * the filenames. If the `encoding` is set to `'buffer'`, the filenames returned
|
478 | * will be passed as `Buffer` objects.
|
479 | *
|
480 | * If `options.withFileTypes` is set to `true`, the resolved array will contain `fs.Dirent` objects.
|
481 | *
|
482 | * ```js
|
483 | * import { readdir } from 'fs/promises';
|
484 | *
|
485 | * try {
|
486 | * const files = await readdir(path);
|
487 | * for (const file of files)
|
488 | * console.log(file);
|
489 | * } catch (err) {
|
490 | * console.error(err);
|
491 | * }
|
492 | * ```
|
493 | * @since v10.0.0
|
494 | * @return Fulfills with an array of the names of the files in the directory excluding `'.'` and `'..'`.
|
495 | */
|
496 | function readdir(
|
497 | path: PathLike,
|
498 | options?:
|
499 | | (ObjectEncodingOptions & {
|
500 | withFileTypes?: false | undefined;
|
501 | })
|
502 | | BufferEncoding
|
503 | | null
|
504 | ): Promise<string[]>;
|
505 | /**
|
506 | * Asynchronous readdir(3) - read a directory.
|
507 | * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
|
508 | * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used.
|
509 | */
|
510 | function readdir(
|
511 | path: PathLike,
|
512 | options:
|
513 | | {
|
514 | encoding: 'buffer';
|
515 | withFileTypes?: false | undefined;
|
516 | }
|
517 | | 'buffer'
|
518 | ): Promise<Buffer[]>;
|
519 | /**
|
520 | * Asynchronous readdir(3) - read a directory.
|
521 | * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
|
522 | * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used.
|
523 | */
|
524 | function readdir(
|
525 | path: PathLike,
|
526 | options?:
|
527 | | (ObjectEncodingOptions & {
|
528 | withFileTypes?: false | undefined;
|
529 | })
|
530 | | BufferEncoding
|
531 | | null
|
532 | ): Promise<string[] | Buffer[]>;
|
533 | /**
|
534 | * Asynchronous readdir(3) - read a directory.
|
535 | * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
|
536 | * @param options If called with `withFileTypes: true` the result data will be an array of Dirent.
|
537 | */
|
538 | function readdir(
|
539 | path: PathLike,
|
540 | options: ObjectEncodingOptions & {
|
541 | withFileTypes: true;
|
542 | }
|
543 | ): Promise<Dirent[]>;
|
544 | /**
|
545 | * Reads the contents of the symbolic link referred to by `path`. See the POSIX [`readlink(2)`](http://man7.org/linux/man-pages/man2/readlink.2.html) documentation for more detail. The promise is
|
546 | * resolved with the`linkString` upon success.
|
547 | *
|
548 | * The optional `options` argument can be a string specifying an encoding, or an
|
549 | * object with an `encoding` property specifying the character encoding to use for
|
550 | * the link path returned. If the `encoding` is set to `'buffer'`, the link path
|
551 | * returned will be passed as a `Buffer` object.
|
552 | * @since v10.0.0
|
553 | * @return Fulfills with the `linkString` upon success.
|
554 | */
|
555 | function readlink(path: PathLike, options?: ObjectEncodingOptions | BufferEncoding | null): Promise<string>;
|
556 | /**
|
557 | * Asynchronous readlink(2) - read value of a symbolic link.
|
558 | * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
|
559 | * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used.
|
560 | */
|
561 | function readlink(path: PathLike, options: BufferEncodingOption): Promise<Buffer>;
|
562 | /**
|
563 | * Asynchronous readlink(2) - read value of a symbolic link.
|
564 | * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
|
565 | * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used.
|
566 | */
|
567 | function readlink(path: PathLike, options?: ObjectEncodingOptions | string | null): Promise<string | Buffer>;
|
568 | /**
|
569 | * Creates a symbolic link.
|
570 | *
|
571 | * The `type` argument is only used on Windows platforms and can be one of `'dir'`,`'file'`, or `'junction'`. Windows junction points require the destination path
|
572 | * to be absolute. When using `'junction'`, the `target` argument will
|
573 | * automatically be normalized to absolute path.
|
574 | * @since v10.0.0
|
575 | * @param [type='file']
|
576 | * @return Fulfills with `undefined` upon success.
|
577 | */
|
578 | function symlink(target: PathLike, path: PathLike, type?: string | null): Promise<void>;
|
579 | /**
|
580 | * Equivalent to `fsPromises.stat()` unless `path` refers to a symbolic link,
|
581 | * in which case the link itself is stat-ed, not the file that it refers to.
|
582 | * Refer to the POSIX [`lstat(2)`](http://man7.org/linux/man-pages/man2/lstat.2.html) document for more detail.
|
583 | * @since v10.0.0
|
584 | * @return Fulfills with the {fs.Stats} object for the given symbolic link `path`.
|
585 | */
|
586 | function lstat(
|
587 | path: PathLike,
|
588 | opts?: StatOptions & {
|
589 | bigint?: false | undefined;
|
590 | }
|
591 | ): Promise<Stats>;
|
592 | function lstat(
|
593 | path: PathLike,
|
594 | opts: StatOptions & {
|
595 | bigint: true;
|
596 | }
|
597 | ): Promise<BigIntStats>;
|
598 | function lstat(path: PathLike, opts?: StatOptions): Promise<Stats | BigIntStats>;
|
599 | /**
|
600 | * @since v10.0.0
|
601 | * @return Fulfills with the {fs.Stats} object for the given `path`.
|
602 | */
|
603 | function stat(
|
604 | path: PathLike,
|
605 | opts?: StatOptions & {
|
606 | bigint?: false | undefined;
|
607 | }
|
608 | ): Promise<Stats>;
|
609 | function stat(
|
610 | path: PathLike,
|
611 | opts: StatOptions & {
|
612 | bigint: true;
|
613 | }
|
614 | ): Promise<BigIntStats>;
|
615 | function stat(path: PathLike, opts?: StatOptions): Promise<Stats | BigIntStats>;
|
616 | /**
|
617 | * Creates a new link from the `existingPath` to the `newPath`. See the POSIX [`link(2)`](http://man7.org/linux/man-pages/man2/link.2.html) documentation for more detail.
|
618 | * @since v10.0.0
|
619 | * @return Fulfills with `undefined` upon success.
|
620 | */
|
621 | function link(existingPath: PathLike, newPath: PathLike): Promise<void>;
|
622 | /**
|
623 | * If `path` refers to a symbolic link, then the link is removed without affecting
|
624 | * the file or directory to which that link refers. If the `path` refers to a file
|
625 | * path that is not a symbolic link, the file is deleted. See the POSIX [`unlink(2)`](http://man7.org/linux/man-pages/man2/unlink.2.html) documentation for more detail.
|
626 | * @since v10.0.0
|
627 | * @return Fulfills with `undefined` upon success.
|
628 | */
|
629 | function unlink(path: PathLike): Promise<void>;
|
630 | /**
|
631 | * Changes the permissions of a file.
|
632 | * @since v10.0.0
|
633 | * @return Fulfills with `undefined` upon success.
|
634 | */
|
635 | function chmod(path: PathLike, mode: Mode): Promise<void>;
|
636 | /**
|
637 | * Changes the permissions on a symbolic link.
|
638 | *
|
639 | * This method is only implemented on macOS.
|
640 | * @deprecated Since v10.0.0
|
641 | * @return Fulfills with `undefined` upon success.
|
642 | */
|
643 | function lchmod(path: PathLike, mode: Mode): Promise<void>;
|
644 | /**
|
645 | * Changes the ownership on a symbolic link.
|
646 | * @since v10.0.0
|
647 | * @return Fulfills with `undefined` upon success.
|
648 | */
|
649 | function lchown(path: PathLike, uid: number, gid: number): Promise<void>;
|
650 | /**
|
651 | * Changes the access and modification times of a file in the same way as `fsPromises.utimes()`, with the difference that if the path refers to a
|
652 | * symbolic link, then the link is not dereferenced: instead, the timestamps of
|
653 | * the symbolic link itself are changed.
|
654 | * @since v14.5.0, v12.19.0
|
655 | * @return Fulfills with `undefined` upon success.
|
656 | */
|
657 | function lutimes(path: PathLike, atime: string | number | Date, mtime: string | number | Date): Promise<void>;
|
658 | /**
|
659 | * Changes the ownership of a file.
|
660 | * @since v10.0.0
|
661 | * @return Fulfills with `undefined` upon success.
|
662 | */
|
663 | function chown(path: PathLike, uid: number, gid: number): Promise<void>;
|
664 | /**
|
665 | * Change the file system timestamps of the object referenced by `path`.
|
666 | *
|
667 | * The `atime` and `mtime` arguments follow these rules:
|
668 | *
|
669 | * * Values can be either numbers representing Unix epoch time, `Date`s, or a
|
670 | * numeric string like `'123456789.0'`.
|
671 | * * If the value can not be converted to a number, or is `NaN`, `Infinity` or`-Infinity`, an `Error` will be thrown.
|
672 | * @since v10.0.0
|
673 | * @return Fulfills with `undefined` upon success.
|
674 | */
|
675 | function utimes(path: PathLike, atime: string | number | Date, mtime: string | number | Date): Promise<void>;
|
676 | /**
|
677 | * Determines the actual location of `path` using the same semantics as the`fs.realpath.native()` function.
|
678 | *
|
679 | * Only paths that can be converted to UTF8 strings are supported.
|
680 | *
|
681 | * The optional `options` argument can be a string specifying an encoding, or an
|
682 | * object with an `encoding` property specifying the character encoding to use for
|
683 | * the path. If the `encoding` is set to `'buffer'`, the path returned will be
|
684 | * passed as a `Buffer` object.
|
685 | *
|
686 | * On Linux, when Node.js is linked against musl libc, the procfs file system must
|
687 | * be mounted on `/proc` in order for this function to work. Glibc does not have
|
688 | * this restriction.
|
689 | * @since v10.0.0
|
690 | * @return Fulfills with the resolved path upon success.
|
691 | */
|
692 | function realpath(path: PathLike, options?: ObjectEncodingOptions | BufferEncoding | null): Promise<string>;
|
693 | /**
|
694 | * Asynchronous realpath(3) - return the canonicalized absolute pathname.
|
695 | * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
|
696 | * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used.
|
697 | */
|
698 | function realpath(path: PathLike, options: BufferEncodingOption): Promise<Buffer>;
|
699 | /**
|
700 | * Asynchronous realpath(3) - return the canonicalized absolute pathname.
|
701 | * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
|
702 | * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used.
|
703 | */
|
704 | function realpath(path: PathLike, options?: ObjectEncodingOptions | BufferEncoding | null): Promise<string | Buffer>;
|
705 | /**
|
706 | * Creates a unique temporary directory. A unique directory name is generated by
|
707 | * appending six random characters to the end of the provided `prefix`. Due to
|
708 | * platform inconsistencies, avoid trailing `X` characters in `prefix`. Some
|
709 | * platforms, notably the BSDs, can return more than six random characters, and
|
710 | * replace trailing `X` characters in `prefix` with random characters.
|
711 | *
|
712 | * The optional `options` argument can be a string specifying an encoding, or an
|
713 | * object with an `encoding` property specifying the character encoding to use.
|
714 | *
|
715 | * ```js
|
716 | * import { mkdtemp } from 'fs/promises';
|
717 | *
|
718 | * try {
|
719 | * await mkdtemp(path.join(os.tmpdir(), 'foo-'));
|
720 | * } catch (err) {
|
721 | * console.error(err);
|
722 | * }
|
723 | * ```
|
724 | *
|
725 | * The `fsPromises.mkdtemp()` method will append the six randomly selected
|
726 | * characters directly to the `prefix` string. For instance, given a directory`/tmp`, if the intention is to create a temporary directory _within_`/tmp`, the`prefix` must end with a trailing
|
727 | * platform-specific path separator
|
728 | * (`require('path').sep`).
|
729 | * @since v10.0.0
|
730 | * @return Fulfills with a string containing the filesystem path of the newly created temporary directory.
|
731 | */
|
732 | function mkdtemp(prefix: string, options?: ObjectEncodingOptions | BufferEncoding | null): Promise<string>;
|
733 | /**
|
734 | * Asynchronously creates a unique temporary directory.
|
735 | * Generates six random characters to be appended behind a required `prefix` to create a unique temporary directory.
|
736 | * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used.
|
737 | */
|
738 | function mkdtemp(prefix: string, options: BufferEncodingOption): Promise<Buffer>;
|
739 | /**
|
740 | * Asynchronously creates a unique temporary directory.
|
741 | * Generates six random characters to be appended behind a required `prefix` to create a unique temporary directory.
|
742 | * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used.
|
743 | */
|
744 | function mkdtemp(prefix: string, options?: ObjectEncodingOptions | BufferEncoding | null): Promise<string | Buffer>;
|
745 | /**
|
746 | * Asynchronously writes data to a file, replacing the file if it already exists.`data` can be a string, a `Buffer`, or, an object with an own (not inherited)`toString` function property.
|
747 | *
|
748 | * The `encoding` option is ignored if `data` is a buffer.
|
749 | *
|
750 | * If `options` is a string, then it specifies the encoding.
|
751 | *
|
752 | * The `mode` option only affects the newly created file. See `fs.open()` for more details.
|
753 | *
|
754 | * Any specified `FileHandle` has to support writing.
|
755 | *
|
756 | * It is unsafe to use `fsPromises.writeFile()` multiple times on the same file
|
757 | * without waiting for the promise to be settled.
|
758 | *
|
759 | * Similarly to `fsPromises.readFile` \- `fsPromises.writeFile` is a convenience
|
760 | * method that performs multiple `write` calls internally to write the buffer
|
761 | * passed to it. For performance sensitive code consider using `fs.createWriteStream()`.
|
762 | *
|
763 | * It is possible to use an `AbortSignal` to cancel an `fsPromises.writeFile()`.
|
764 | * Cancelation is "best effort", and some amount of data is likely still
|
765 | * to be written.
|
766 | *
|
767 | * ```js
|
768 | * import { writeFile } from 'fs/promises';
|
769 | * import { Buffer } from 'buffer';
|
770 | *
|
771 | * try {
|
772 | * const controller = new AbortController();
|
773 | * const { signal } = controller;
|
774 | * const data = new Uint8Array(Buffer.from('Hello Node.js'));
|
775 | * const promise = writeFile('message.txt', data, { signal });
|
776 | *
|
777 | * // Abort the request before the promise settles.
|
778 | * controller.abort();
|
779 | *
|
780 | * await promise;
|
781 | * } catch (err) {
|
782 | * // When a request is aborted - err is an AbortError
|
783 | * console.error(err);
|
784 | * }
|
785 | * ```
|
786 | *
|
787 | * Aborting an ongoing request does not abort individual operating
|
788 | * system requests but rather the internal buffering `fs.writeFile` performs.
|
789 | * @since v10.0.0
|
790 | * @param file filename or `FileHandle`
|
791 | * @return Fulfills with `undefined` upon success.
|
792 | */
|
793 | function writeFile(
|
794 | file: PathLike | FileHandle,
|
795 | data: string | NodeJS.ArrayBufferView | Iterable<string | NodeJS.ArrayBufferView> | AsyncIterable<string | NodeJS.ArrayBufferView> | Stream,
|
796 | options?:
|
797 | | (ObjectEncodingOptions & {
|
798 | mode?: Mode | undefined;
|
799 | flag?: OpenMode | undefined;
|
800 | } & Abortable)
|
801 | | BufferEncoding
|
802 | | null
|
803 | ): Promise<void>;
|
804 | /**
|
805 | * Asynchronously append data to a file, creating the file if it does not yet
|
806 | * exist. `data` can be a string or a `Buffer`.
|
807 | *
|
808 | * If `options` is a string, then it specifies the `encoding`.
|
809 | *
|
810 | * The `mode` option only affects the newly created file. See `fs.open()` for more details.
|
811 | *
|
812 | * The `path` may be specified as a `FileHandle` that has been opened
|
813 | * for appending (using `fsPromises.open()`).
|
814 | * @since v10.0.0
|
815 | * @param path filename or {FileHandle}
|
816 | * @return Fulfills with `undefined` upon success.
|
817 | */
|
818 | function appendFile(path: PathLike | FileHandle, data: string | Uint8Array, options?: (ObjectEncodingOptions & FlagAndOpenMode) | BufferEncoding | null): Promise<void>;
|
819 | /**
|
820 | * Asynchronously reads the entire contents of a file.
|
821 | *
|
822 | * If no encoding is specified (using `options.encoding`), the data is returned
|
823 | * as a `Buffer` object. Otherwise, the data will be a string.
|
824 | *
|
825 | * If `options` is a string, then it specifies the encoding.
|
826 | *
|
827 | * When the `path` is a directory, the behavior of `fsPromises.readFile()` is
|
828 | * platform-specific. On macOS, Linux, and Windows, the promise will be rejected
|
829 | * with an error. On FreeBSD, a representation of the directory's contents will be
|
830 | * returned.
|
831 | *
|
832 | * It is possible to abort an ongoing `readFile` using an `AbortSignal`. If a
|
833 | * request is aborted the promise returned is rejected with an `AbortError`:
|
834 | *
|
835 | * ```js
|
836 | * import { readFile } from 'fs/promises';
|
837 | *
|
838 | * try {
|
839 | * const controller = new AbortController();
|
840 | * const { signal } = controller;
|
841 | * const promise = readFile(fileName, { signal });
|
842 | *
|
843 | * // Abort the request before the promise settles.
|
844 | * controller.abort();
|
845 | *
|
846 | * await promise;
|
847 | * } catch (err) {
|
848 | * // When a request is aborted - err is an AbortError
|
849 | * console.error(err);
|
850 | * }
|
851 | * ```
|
852 | *
|
853 | * Aborting an ongoing request does not abort individual operating
|
854 | * system requests but rather the internal buffering `fs.readFile` performs.
|
855 | *
|
856 | * Any specified `FileHandle` has to support reading.
|
857 | * @since v10.0.0
|
858 | * @param path filename or `FileHandle`
|
859 | * @return Fulfills with the contents of the file.
|
860 | */
|
861 | function readFile(
|
862 | path: PathLike | FileHandle,
|
863 | options?:
|
864 | | ({
|
865 | encoding?: null | undefined;
|
866 | flag?: OpenMode | undefined;
|
867 | } & Abortable)
|
868 | | null
|
869 | ): Promise<Buffer>;
|
870 | /**
|
871 | * Asynchronously reads the entire contents of a file.
|
872 | * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
|
873 | * If a `FileHandle` is provided, the underlying file will _not_ be closed automatically.
|
874 | * @param options An object that may contain an optional flag.
|
875 | * If a flag is not provided, it defaults to `'r'`.
|
876 | */
|
877 | function readFile(
|
878 | path: PathLike | FileHandle,
|
879 | options:
|
880 | | ({
|
881 | encoding: BufferEncoding;
|
882 | flag?: OpenMode | undefined;
|
883 | } & Abortable)
|
884 | | BufferEncoding
|
885 | ): Promise<string>;
|
886 | /**
|
887 | * Asynchronously reads the entire contents of a file.
|
888 | * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
|
889 | * If a `FileHandle` is provided, the underlying file will _not_ be closed automatically.
|
890 | * @param options An object that may contain an optional flag.
|
891 | * If a flag is not provided, it defaults to `'r'`.
|
892 | */
|
893 | function readFile(
|
894 | path: PathLike | FileHandle,
|
895 | options?:
|
896 | | (ObjectEncodingOptions &
|
897 | Abortable & {
|
898 | flag?: OpenMode | undefined;
|
899 | })
|
900 | | BufferEncoding
|
901 | | null
|
902 | ): Promise<string | Buffer>;
|
903 | /**
|
904 | * Asynchronously open a directory for iterative scanning. See the POSIX [`opendir(3)`](http://man7.org/linux/man-pages/man3/opendir.3.html) documentation for more detail.
|
905 | *
|
906 | * Creates an `fs.Dir`, which contains all further functions for reading from
|
907 | * and cleaning up the directory.
|
908 | *
|
909 | * The `encoding` option sets the encoding for the `path` while opening the
|
910 | * directory and subsequent read operations.
|
911 | *
|
912 | * Example using async iteration:
|
913 | *
|
914 | * ```js
|
915 | * import { opendir } from 'fs/promises';
|
916 | *
|
917 | * try {
|
918 | * const dir = await opendir('./');
|
919 | * for await (const dirent of dir)
|
920 | * console.log(dirent.name);
|
921 | * } catch (err) {
|
922 | * console.error(err);
|
923 | * }
|
924 | * ```
|
925 | *
|
926 | * When using the async iterator, the `fs.Dir` object will be automatically
|
927 | * closed after the iterator exits.
|
928 | * @since v12.12.0
|
929 | * @return Fulfills with an {fs.Dir}.
|
930 | */
|
931 | function opendir(path: string, options?: OpenDirOptions): Promise<Dir>;
|
932 | /**
|
933 | * Returns an async iterator that watches for changes on `filename`, where `filename`is either a file or a directory.
|
934 | *
|
935 | * ```js
|
936 | * const { watch } = require('fs/promises');
|
937 | *
|
938 | * const ac = new AbortController();
|
939 | * const { signal } = ac;
|
940 | * setTimeout(() => ac.abort(), 10000);
|
941 | *
|
942 | * (async () => {
|
943 | * try {
|
944 | * const watcher = watch(__filename, { signal });
|
945 | * for await (const event of watcher)
|
946 | * console.log(event);
|
947 | * } catch (err) {
|
948 | * if (err.name === 'AbortError')
|
949 | * return;
|
950 | * throw err;
|
951 | * }
|
952 | * })();
|
953 | * ```
|
954 | *
|
955 | * On most platforms, `'rename'` is emitted whenever a filename appears or
|
956 | * disappears in the directory.
|
957 | *
|
958 | * All the `caveats` for `fs.watch()` also apply to `fsPromises.watch()`.
|
959 | * @since v15.9.0
|
960 | * @return of objects with the properties:
|
961 | */
|
962 | function watch(
|
963 | filename: PathLike,
|
964 | options:
|
965 | | (WatchOptions & {
|
966 | encoding: 'buffer';
|
967 | })
|
968 | | 'buffer'
|
969 | ): AsyncIterable<FileChangeInfo<Buffer>>;
|
970 | /**
|
971 | * Watch for changes on `filename`, where `filename` is either a file or a directory, returning an `FSWatcher`.
|
972 | * @param filename A path to a file or directory. If a URL is provided, it must use the `file:` protocol.
|
973 | * @param options Either the encoding for the filename provided to the listener, or an object optionally specifying encoding, persistent, and recursive options.
|
974 | * If `encoding` is not supplied, the default of `'utf8'` is used.
|
975 | * If `persistent` is not supplied, the default of `true` is used.
|
976 | * If `recursive` is not supplied, the default of `false` is used.
|
977 | */
|
978 | function watch(filename: PathLike, options?: WatchOptions | BufferEncoding): AsyncIterable<FileChangeInfo<string>>;
|
979 | /**
|
980 | * Watch for changes on `filename`, where `filename` is either a file or a directory, returning an `FSWatcher`.
|
981 | * @param filename A path to a file or directory. If a URL is provided, it must use the `file:` protocol.
|
982 | * @param options Either the encoding for the filename provided to the listener, or an object optionally specifying encoding, persistent, and recursive options.
|
983 | * If `encoding` is not supplied, the default of `'utf8'` is used.
|
984 | * If `persistent` is not supplied, the default of `true` is used.
|
985 | * If `recursive` is not supplied, the default of `false` is used.
|
986 | */
|
987 | function watch(filename: PathLike, options: WatchOptions | string): AsyncIterable<FileChangeInfo<string>> | AsyncIterable<FileChangeInfo<Buffer>>;
|
988 | /**
|
989 | * Asynchronously copies the entire directory structure from `src` to `dest`,
|
990 | * including subdirectories and files.
|
991 | *
|
992 | * When copying a directory to another directory, globs are not supported and
|
993 | * behavior is similar to `cp dir1/ dir2/`.
|
994 | * @since v16.7.0
|
995 | * @experimental
|
996 | * @param src source path to copy.
|
997 | * @param dest destination path to copy to.
|
998 | * @return Fulfills with `undefined` upon success.
|
999 | */
|
1000 | function cp(source: string, destination: string, opts?: CopyOptions): Promise<void>;
|
1001 | }
|
1002 | declare module 'node:fs/promises' {
|
1003 | export * from 'fs/promises';
|
1004 | }
|