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