@types/node 16.4.2 → 16.4.6

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