UNPKG

33.2 kBTypeScriptView Raw
1declare module 'fs/promises' {
2 import { Abortable } from 'events';
3 import { Stream } from 'stream';
4 import {
5 Stats,
6 BigIntStats,
7 StatOptions,
8 WriteVResult,
9 ReadVResult,
10 PathLike,
11 RmDirOptions,
12 RmOptions,
13 MakeDirectoryOptions,
14 Dirent,
15 OpenDirOptions,
16 Dir,
17 BaseEncodingOptions,
18 BufferEncodingOption,
19 OpenMode,
20 Mode,
21 WatchOptions,
22 } from 'fs';
23
24 interface FileHandle {
25 /**
26 * Gets the file descriptor for this file handle.
27 */
28 readonly fd: number;
29
30 /**
31 * Asynchronously append data to a file, creating the file if it does not exist. The underlying file will _not_ be closed automatically.
32 * The `FileHandle` must have been opened for appending.
33 * @param data The data to write. If something other than a `Buffer` or `Uint8Array` is provided, the value is coerced to a string.
34 * @param options Either the encoding for the file, or an object optionally specifying the encoding, file mode, and flag.
35 * If `encoding` is not supplied, the default of `'utf8'` is used.
36 * If `mode` is not supplied, the default of `0o666` is used.
37 * If `mode` is a string, it is parsed as an octal integer.
38 * If `flag` is not supplied, the default of `'a'` is used.
39 */
40 appendFile(data: string | Uint8Array, options?: BaseEncodingOptions & { mode?: Mode, flag?: OpenMode } | BufferEncoding | null): Promise<void>;
41
42 /**
43 * Asynchronous fchown(2) - Change ownership of a file.
44 */
45 chown(uid: number, gid: number): Promise<void>;
46
47 /**
48 * Asynchronous fchmod(2) - Change permissions of a file.
49 * @param mode A file mode. If a string is passed, it is parsed as an octal integer.
50 */
51 chmod(mode: Mode): Promise<void>;
52
53 /**
54 * Asynchronous fdatasync(2) - synchronize a file's in-core state with storage device.
55 */
56 datasync(): Promise<void>;
57
58 /**
59 * Asynchronous fsync(2) - synchronize a file's in-core state with the underlying storage device.
60 */
61 sync(): Promise<void>;
62
63 /**
64 * Asynchronously reads data from the file.
65 * The `FileHandle` must have been opened for reading.
66 * @param buffer The buffer that the data will be written to.
67 * @param offset The offset in the buffer at which to start writing.
68 * @param length The number of bytes to read.
69 * @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.
70 */
71 read<TBuffer extends Uint8Array>(buffer: TBuffer, offset?: number | null, length?: number | null, position?: number | null): Promise<{ bytesRead: number, buffer: TBuffer }>;
72
73 /**
74 * Asynchronously reads the entire contents of a file. The underlying file will _not_ be closed automatically.
75 * The `FileHandle` must have been opened for reading.
76 * @param options An object that may contain an optional flag.
77 * If a flag is not provided, it defaults to `'r'`.
78 */
79 readFile(options?: { encoding?: null, flag?: OpenMode } | null): Promise<Buffer>;
80
81 /**
82 * Asynchronously reads the entire contents of a file. The underlying file will _not_ be closed automatically.
83 * The `FileHandle` must have been opened for reading.
84 * @param options An object that may contain an optional flag.
85 * If a flag is not provided, it defaults to `'r'`.
86 */
87 readFile(options: { encoding: BufferEncoding, flag?: OpenMode } | BufferEncoding): Promise<string>;
88
89 /**
90 * Asynchronously reads the entire contents of a file. The underlying file will _not_ be closed automatically.
91 * The `FileHandle` must have been opened for reading.
92 * @param options An object that may contain an optional flag.
93 * If a flag is not provided, it defaults to `'r'`.
94 */
95 readFile(options?: BaseEncodingOptions & { flag?: OpenMode } | BufferEncoding | null): Promise<string | Buffer>;
96
97 /**
98 * Asynchronous fstat(2) - Get file status.
99 */
100 stat(opts?: StatOptions & { bigint?: false }): Promise<Stats>;
101 stat(opts: StatOptions & { bigint: true }): Promise<BigIntStats>;
102 stat(opts?: StatOptions): Promise<Stats | BigIntStats>;
103
104 /**
105 * Asynchronous ftruncate(2) - Truncate a file to a specified length.
106 * @param len If not specified, defaults to `0`.
107 */
108 truncate(len?: number): Promise<void>;
109
110 /**
111 * Asynchronously change file timestamps of the file.
112 * @param atime The last access time. If a string is provided, it will be coerced to number.
113 * @param mtime The last modified time. If a string is provided, it will be coerced to number.
114 */
115 utimes(atime: string | number | Date, mtime: string | number | Date): Promise<void>;
116
117 /**
118 * Asynchronously writes `buffer` to the file.
119 * The `FileHandle` must have been opened for writing.
120 * @param buffer The buffer that the data will be written to.
121 * @param offset The part of the buffer to be written. If not supplied, defaults to `0`.
122 * @param length The number of bytes to write. If not supplied, defaults to `buffer.length - offset`.
123 * @param position The offset from the beginning of the file where this data should be written. If not supplied, defaults to the current position.
124 */
125 write<TBuffer extends Uint8Array>(buffer: TBuffer, offset?: number | null, length?: number | null, position?: number | null): Promise<{ bytesWritten: number, buffer: TBuffer }>;
126
127 /**
128 * Asynchronously writes `string` to the file.
129 * The `FileHandle` must have been opened for writing.
130 * It is unsafe to call `write()` multiple times on the same file without waiting for the `Promise`
131 * to be resolved (or rejected). For this scenario, `fs.createWriteStream` is strongly recommended.
132 * @param string A string to write.
133 * @param position The offset from the beginning of the file where this data should be written. If not supplied, defaults to the current position.
134 * @param encoding The expected string encoding.
135 */
136 write(data: string | Uint8Array, position?: number | null, encoding?: BufferEncoding | null): Promise<{ bytesWritten: number, buffer: string }>;
137
138 /**
139 * Asynchronously writes data to a file, replacing the file if it already exists. The underlying file will _not_ be closed automatically.
140 * The `FileHandle` must have been opened for writing.
141 * It is unsafe to call `writeFile()` multiple times on the same file without waiting for the `Promise` to be resolved (or rejected).
142 * @param data The data to write. If something other than a `Buffer` or `Uint8Array` is provided, the value is coerced to a string.
143 * @param options Either the encoding for the file, or an object optionally specifying the encoding, file mode, and flag.
144 * If `encoding` is not supplied, the default of `'utf8'` is used.
145 * If `mode` is not supplied, the default of `0o666` is used.
146 * If `mode` is a string, it is parsed as an octal integer.
147 * If `flag` is not supplied, the default of `'w'` is used.
148 */
149 writeFile(data: string | Uint8Array, options?: BaseEncodingOptions & { mode?: Mode, flag?: OpenMode } & Abortable | BufferEncoding | null): Promise<void>;
150
151 /**
152 * See `fs.writev` promisified version.
153 */
154 writev(buffers: ReadonlyArray<NodeJS.ArrayBufferView>, position?: number): Promise<WriteVResult>;
155
156 /**
157 * See `fs.readv` promisified version.
158 */
159 readv(buffers: ReadonlyArray<NodeJS.ArrayBufferView>, position?: number): Promise<ReadVResult>;
160
161 /**
162 * Asynchronous close(2) - close a `FileHandle`.
163 */
164 close(): Promise<void>;
165 }
166
167 /**
168 * Asynchronously tests a user's permissions for the file specified by path.
169 * @param path A path to a file or directory. If a URL is provided, it must use the `file:` protocol.
170 * URL support is _experimental_.
171 */
172 function access(path: PathLike, mode?: number): Promise<void>;
173
174 /**
175 * Asynchronously copies `src` to `dest`. By default, `dest` is overwritten if it already exists.
176 * Node.js makes no guarantees about the atomicity of the copy operation.
177 * If an error occurs after the destination file has been opened for writing, Node.js will attempt
178 * to remove the destination.
179 * @param src A path to the source file.
180 * @param dest A path to the destination file.
181 * @param flags An optional integer that specifies the behavior of the copy operation. The only
182 * supported flag is `fs.constants.COPYFILE_EXCL`, which causes the copy operation to fail if
183 * `dest` already exists.
184 */
185 function copyFile(src: PathLike, dest: PathLike, flags?: number): Promise<void>;
186
187 /**
188 * Asynchronous open(2) - open and possibly create a file.
189 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
190 * @param mode A file mode. If a string is passed, it is parsed as an octal integer. If not
191 * supplied, defaults to `0o666`.
192 */
193 function open(path: PathLike, flags: string | number, mode?: Mode): Promise<FileHandle>;
194
195 /**
196 * Asynchronously reads data from the file referenced by the supplied `FileHandle`.
197 * @param handle A `FileHandle`.
198 * @param buffer The buffer that the data will be written to.
199 * @param offset The offset in the buffer at which to start writing.
200 * @param length The number of bytes to read.
201 * @param position The offset from the beginning of the file from which data should be read. If
202 * `null`, data will be read from the current position.
203 */
204 function read<TBuffer extends Uint8Array>(
205 handle: FileHandle,
206 buffer: TBuffer,
207 offset?: number | null,
208 length?: number | null,
209 position?: number | null,
210 ): Promise<{ bytesRead: number, buffer: TBuffer }>;
211
212 /**
213 * Asynchronously writes `buffer` to the file referenced by the supplied `FileHandle`.
214 * It is unsafe to call `fsPromises.write()` multiple times on the same file without waiting for the `Promise`
215 * to be resolved (or rejected). For this scenario, `fs.createWriteStream` is strongly recommended.
216 * @param handle A `FileHandle`.
217 * @param buffer The buffer that the data will be written to.
218 * @param offset The part of the buffer to be written. If not supplied, defaults to `0`.
219 * @param length The number of bytes to write. If not supplied, defaults to `buffer.length - offset`.
220 * @param position The offset from the beginning of the file where this data should be written. If not supplied, defaults to the current position.
221 */
222 function write<TBuffer extends Uint8Array>(
223 handle: FileHandle,
224 buffer: TBuffer,
225 offset?: number | null,
226 length?: number | null, position?: number | null): Promise<{ bytesWritten: number, buffer: TBuffer }>;
227
228 /**
229 * Asynchronously writes `string` to the file referenced by the supplied `FileHandle`.
230 * It is unsafe to call `fsPromises.write()` multiple times on the same file without waiting for the `Promise`
231 * to be resolved (or rejected). For this scenario, `fs.createWriteStream` is strongly recommended.
232 * @param handle A `FileHandle`.
233 * @param string A string to write.
234 * @param position The offset from the beginning of the file where this data should be written. If not supplied, defaults to the current position.
235 * @param encoding The expected string encoding.
236 */
237 function write(handle: FileHandle, string: string, position?: number | null, encoding?: BufferEncoding | null): Promise<{ bytesWritten: number, buffer: string }>;
238
239 /**
240 * Asynchronous rename(2) - Change the name or location of a file or directory.
241 * @param oldPath A path to a file. If a URL is provided, it must use the `file:` protocol.
242 * URL support is _experimental_.
243 * @param newPath A path to a file. If a URL is provided, it must use the `file:` protocol.
244 * URL support is _experimental_.
245 */
246 function rename(oldPath: PathLike, newPath: PathLike): Promise<void>;
247
248 /**
249 * Asynchronous truncate(2) - Truncate a file to a specified length.
250 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
251 * @param len If not specified, defaults to `0`.
252 */
253 function truncate(path: PathLike, len?: number): Promise<void>;
254
255 /**
256 * Asynchronous ftruncate(2) - Truncate a file to a specified length.
257 * @param handle A `FileHandle`.
258 * @param len If not specified, defaults to `0`.
259 */
260 function ftruncate(handle: FileHandle, len?: number): Promise<void>;
261
262 /**
263 * Asynchronous rmdir(2) - delete a directory.
264 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
265 */
266 function rmdir(path: PathLike, options?: RmDirOptions): Promise<void>;
267
268 /**
269 * Asynchronously removes files and directories (modeled on the standard POSIX `rm` utility).
270 */
271 function rm(path: PathLike, options?: RmOptions): Promise<void>;
272
273 /**
274 * Asynchronous fdatasync(2) - synchronize a file's in-core state with storage device.
275 * @param handle A `FileHandle`.
276 */
277 function fdatasync(handle: FileHandle): Promise<void>;
278
279 /**
280 * Asynchronous fsync(2) - synchronize a file's in-core state with the underlying storage device.
281 * @param handle A `FileHandle`.
282 */
283 function fsync(handle: FileHandle): Promise<void>;
284
285 /**
286 * Asynchronous mkdir(2) - create a directory.
287 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
288 * @param options Either the file mode, or an object optionally specifying the file mode and whether parent folders
289 * should be created. If a string is passed, it is parsed as an octal integer. If not specified, defaults to `0o777`.
290 */
291 function mkdir(path: PathLike, options: MakeDirectoryOptions & { recursive: true; }): Promise<string | undefined>;
292
293 /**
294 * Asynchronous mkdir(2) - create a directory.
295 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
296 * @param options Either the file mode, or an object optionally specifying the file mode and whether parent folders
297 * should be created. If a string is passed, it is parsed as an octal integer. If not specified, defaults to `0o777`.
298 */
299 function mkdir(path: PathLike, options?: Mode | (MakeDirectoryOptions & { recursive?: false; }) | null): Promise<void>;
300
301 /**
302 * Asynchronous mkdir(2) - create a directory.
303 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
304 * @param options Either the file mode, or an object optionally specifying the file mode and whether parent folders
305 * should be created. If a string is passed, it is parsed as an octal integer. If not specified, defaults to `0o777`.
306 */
307 function mkdir(path: PathLike, options?: Mode | MakeDirectoryOptions | null): Promise<string | undefined>;
308
309 /**
310 * Asynchronous readdir(3) - read a directory.
311 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
312 * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used.
313 */
314 function readdir(path: PathLike, options?: BaseEncodingOptions & { withFileTypes?: false } | BufferEncoding | null): Promise<string[]>;
315
316 /**
317 * Asynchronous readdir(3) - read a directory.
318 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
319 * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used.
320 */
321 function readdir(path: PathLike, options: { encoding: "buffer"; withFileTypes?: false } | "buffer"): Promise<Buffer[]>;
322
323 /**
324 * Asynchronous readdir(3) - read a directory.
325 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
326 * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used.
327 */
328 function readdir(path: PathLike, options?: BaseEncodingOptions & { withFileTypes?: false } | BufferEncoding | null): Promise<string[] | Buffer[]>;
329
330 /**
331 * Asynchronous readdir(3) - read a directory.
332 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
333 * @param options If called with `withFileTypes: true` the result data will be an array of Dirent.
334 */
335 function readdir(path: PathLike, options: BaseEncodingOptions & { withFileTypes: true }): Promise<Dirent[]>;
336
337 /**
338 * Asynchronous readlink(2) - read value of a symbolic link.
339 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
340 * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used.
341 */
342 function readlink(path: PathLike, options?: BaseEncodingOptions | BufferEncoding | null): Promise<string>;
343
344 /**
345 * Asynchronous readlink(2) - read value of a symbolic link.
346 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
347 * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used.
348 */
349 function readlink(path: PathLike, options: BufferEncodingOption): Promise<Buffer>;
350
351 /**
352 * Asynchronous readlink(2) - read value of a symbolic link.
353 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
354 * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used.
355 */
356 function readlink(path: PathLike, options?: BaseEncodingOptions | string | null): Promise<string | Buffer>;
357
358 /**
359 * Asynchronous symlink(2) - Create a new symbolic link to an existing file.
360 * @param target A path to an existing file. If a URL is provided, it must use the `file:` protocol.
361 * @param path A path to the new symlink. If a URL is provided, it must use the `file:` protocol.
362 * @param type May be set to `'dir'`, `'file'`, or `'junction'` (default is `'file'`) and is only available on Windows (ignored on other platforms).
363 * When using `'junction'`, the `target` argument will automatically be normalized to an absolute path.
364 */
365 function symlink(target: PathLike, path: PathLike, type?: string | null): Promise<void>;
366
367 /**
368 * Asynchronous lstat(2) - Get file status. 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 */
371 function lstat(path: PathLike, opts?: StatOptions & { bigint?: false }): Promise<Stats>;
372 function lstat(path: PathLike, opts: StatOptions & { bigint: true }): Promise<BigIntStats>;
373 function lstat(path: PathLike, opts?: StatOptions): Promise<Stats | BigIntStats>;
374
375 /**
376 * Asynchronous stat(2) - Get file status.
377 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
378 */
379 function stat(path: PathLike, opts?: StatOptions & { bigint?: false }): Promise<Stats>;
380 function stat(path: PathLike, opts: StatOptions & { bigint: true }): Promise<BigIntStats>;
381 function stat(path: PathLike, opts?: StatOptions): Promise<Stats | BigIntStats>;
382
383 /**
384 * Asynchronous link(2) - Create a new link (also known as a hard link) to an existing file.
385 * @param existingPath A path to a file. If a URL is provided, it must use the `file:` protocol.
386 * @param newPath A path to a file. If a URL is provided, it must use the `file:` protocol.
387 */
388 function link(existingPath: PathLike, newPath: PathLike): Promise<void>;
389
390 /**
391 * Asynchronous unlink(2) - delete a name and possibly the file it refers to.
392 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
393 */
394 function unlink(path: PathLike): Promise<void>;
395
396 /**
397 * Asynchronous fchmod(2) - Change permissions of a file.
398 * @param handle A `FileHandle`.
399 * @param mode A file mode. If a string is passed, it is parsed as an octal integer.
400 */
401 function fchmod(handle: FileHandle, mode: Mode): Promise<void>;
402
403 /**
404 * Asynchronous chmod(2) - Change permissions of a file.
405 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
406 * @param mode A file mode. If a string is passed, it is parsed as an octal integer.
407 */
408 function chmod(path: PathLike, mode: Mode): Promise<void>;
409
410 /**
411 * Asynchronous lchmod(2) - Change permissions of a file. Does not dereference symbolic links.
412 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
413 * @param mode A file mode. If a string is passed, it is parsed as an octal integer.
414 */
415 function lchmod(path: PathLike, mode: Mode): Promise<void>;
416
417 /**
418 * Asynchronous lchown(2) - Change ownership of a file. Does not dereference symbolic links.
419 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
420 */
421 function lchown(path: PathLike, uid: number, gid: number): Promise<void>;
422
423 /**
424 * Changes the access and modification times of a file in the same way as `fsPromises.utimes()`,
425 * with the difference that if the path refers to a symbolic link, then the link is not
426 * dereferenced: instead, the timestamps of the symbolic link itself are changed.
427 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
428 * @param atime The last access time. If a string is provided, it will be coerced to number.
429 * @param mtime The last modified time. If a string is provided, it will be coerced to number.
430 */
431 function lutimes(path: PathLike, atime: string | number | Date, mtime: string | number | Date): Promise<void>;
432
433 /**
434 * Asynchronous fchown(2) - Change ownership of a file.
435 * @param handle A `FileHandle`.
436 */
437 function fchown(handle: FileHandle, uid: number, gid: number): Promise<void>;
438
439 /**
440 * Asynchronous chown(2) - Change ownership of a file.
441 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
442 */
443 function chown(path: PathLike, uid: number, gid: number): Promise<void>;
444
445 /**
446 * Asynchronously change file timestamps of the file referenced by the supplied path.
447 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
448 * @param atime The last access time. If a string is provided, it will be coerced to number.
449 * @param mtime The last modified time. If a string is provided, it will be coerced to number.
450 */
451 function utimes(path: PathLike, atime: string | number | Date, mtime: string | number | Date): Promise<void>;
452
453 /**
454 * Asynchronously change file timestamps of the file referenced by the supplied `FileHandle`.
455 * @param handle A `FileHandle`.
456 * @param atime The last access time. If a string is provided, it will be coerced to number.
457 * @param mtime The last modified time. If a string is provided, it will be coerced to number.
458 */
459 function futimes(handle: FileHandle, atime: string | number | Date, mtime: string | number | Date): Promise<void>;
460
461 /**
462 * Asynchronous realpath(3) - return the canonicalized absolute pathname.
463 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
464 * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used.
465 */
466 function realpath(path: PathLike, options?: BaseEncodingOptions | BufferEncoding | null): Promise<string>;
467
468 /**
469 * Asynchronous realpath(3) - return the canonicalized absolute pathname.
470 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
471 * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used.
472 */
473 function realpath(path: PathLike, options: BufferEncodingOption): Promise<Buffer>;
474
475 /**
476 * Asynchronous realpath(3) - return the canonicalized absolute pathname.
477 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
478 * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used.
479 */
480 function realpath(path: PathLike, options?: BaseEncodingOptions | BufferEncoding | null): Promise<string | Buffer>;
481
482 /**
483 * Asynchronously creates a unique temporary directory.
484 * Generates six random characters to be appended behind a required `prefix` to create a unique temporary directory.
485 * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used.
486 */
487 function mkdtemp(prefix: string, options?: BaseEncodingOptions | BufferEncoding | null): Promise<string>;
488
489 /**
490 * Asynchronously creates a unique temporary directory.
491 * Generates six random characters to be appended behind a required `prefix` to create a unique temporary directory.
492 * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used.
493 */
494 function mkdtemp(prefix: string, options: BufferEncodingOption): Promise<Buffer>;
495
496 /**
497 * Asynchronously creates a unique temporary directory.
498 * Generates six random characters to be appended behind a required `prefix` to create a unique temporary directory.
499 * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used.
500 */
501 function mkdtemp(prefix: string, options?: BaseEncodingOptions | BufferEncoding | null): Promise<string | Buffer>;
502
503 /**
504 * Asynchronously writes data to a file, replacing the file if it already exists.
505 * It is unsafe to call `fsPromises.writeFile()` multiple times on the same file without waiting for the `Promise` to be resolved (or rejected).
506 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
507 * URL support is _experimental_.
508 * If a `FileHandle` is provided, the underlying file will _not_ be closed automatically.
509 * @param data The data to write. If something other than a `Buffer` or `Uint8Array` is provided, the value is coerced to a string.
510 * @param options Either the encoding for the file, or an object optionally specifying the encoding, file mode, and flag.
511 * If `encoding` is not supplied, the default of `'utf8'` is used.
512 * If `mode` is not supplied, the default of `0o666` is used.
513 * If `mode` is a string, it is parsed as an octal integer.
514 * If `flag` is not supplied, the default of `'w'` is used.
515 */
516 function writeFile(
517 path: PathLike | FileHandle,
518 data: string | NodeJS.ArrayBufferView | Iterable<string | NodeJS.ArrayBufferView> | AsyncIterable<string | NodeJS.ArrayBufferView> | Stream,
519 options?: BaseEncodingOptions & { mode?: Mode, flag?: OpenMode } & Abortable | BufferEncoding | null
520 ): Promise<void>;
521
522 /**
523 * Asynchronously append data to a file, creating the file if it does not exist.
524 * @param file A path to a file. If a URL is provided, it must use the `file:` protocol.
525 * URL support is _experimental_.
526 * If a `FileHandle` is provided, the underlying file will _not_ be closed automatically.
527 * @param data The data to write. If something other than a `Buffer` or `Uint8Array` is provided, the value is coerced to a string.
528 * @param options Either the encoding for the file, or an object optionally specifying the encoding, file mode, and flag.
529 * If `encoding` is not supplied, the default of `'utf8'` is used.
530 * If `mode` is not supplied, the default of `0o666` is used.
531 * If `mode` is a string, it is parsed as an octal integer.
532 * If `flag` is not supplied, the default of `'a'` is used.
533 */
534 function appendFile(path: PathLike | FileHandle, data: string | Uint8Array, options?: BaseEncodingOptions & { mode?: Mode, flag?: OpenMode } | BufferEncoding | null): Promise<void>;
535
536 /**
537 * Asynchronously reads the entire contents of a file.
538 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
539 * If a `FileHandle` is provided, the underlying file will _not_ be closed automatically.
540 * @param options An object that may contain an optional flag.
541 * If a flag is not provided, it defaults to `'r'`.
542 */
543 function readFile(path: PathLike | FileHandle, options?: { encoding?: null, flag?: OpenMode } & Abortable | null): Promise<Buffer>;
544
545 /**
546 * Asynchronously reads the entire contents of a file.
547 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
548 * If a `FileHandle` is provided, the underlying file will _not_ be closed automatically.
549 * @param options An object that may contain an optional flag.
550 * If a flag is not provided, it defaults to `'r'`.
551 */
552 function readFile(path: PathLike | FileHandle, options: { encoding: BufferEncoding, flag?: OpenMode } & Abortable | BufferEncoding): Promise<string>;
553
554 /**
555 * Asynchronously reads the entire contents of a file.
556 * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
557 * If a `FileHandle` is provided, the underlying file will _not_ be closed automatically.
558 * @param options An object that may contain an optional flag.
559 * If a flag is not provided, it defaults to `'r'`.
560 */
561 function readFile(path: PathLike | FileHandle, options?: BaseEncodingOptions & Abortable & { flag?: OpenMode } | BufferEncoding | null): Promise<string | Buffer>;
562
563 function opendir(path: string, options?: OpenDirOptions): Promise<Dir>;
564
565 /**
566 * Watch for changes on `filename`, where `filename` is either a file or a directory, returning an `FSWatcher`.
567 * @param filename A path to a file or directory. If a URL is provided, it must use the `file:` protocol.
568 * @param options Either the encoding for the filename provided to the listener, or an object optionally specifying encoding, persistent, and recursive options.
569 * If `encoding` is not supplied, the default of `'utf8'` is used.
570 * If `persistent` is not supplied, the default of `true` is used.
571 * If `recursive` is not supplied, the default of `false` is used.
572 */
573 function watch(filename: PathLike, options: WatchOptions & { encoding: "buffer" } | "buffer"): AsyncIterable<Buffer>;
574
575 /**
576 * Watch for changes on `filename`, where `filename` is either a file or a directory, returning an `FSWatcher`.
577 * @param filename A path to a file or directory. If a URL is provided, it must use the `file:` protocol.
578 * @param options Either the encoding for the filename provided to the listener, or an object optionally specifying encoding, persistent, and recursive options.
579 * If `encoding` is not supplied, the default of `'utf8'` is used.
580 * If `persistent` is not supplied, the default of `true` is used.
581 * If `recursive` is not supplied, the default of `false` is used.
582 */
583 function watch(
584 filename: PathLike,
585 options?: WatchOptions | BufferEncoding
586 ): AsyncIterable<string>;
587
588 /**
589 * Watch for changes on `filename`, where `filename` is either a file or a directory, returning an `FSWatcher`.
590 * @param filename A path to a file or directory. If a URL is provided, it must use the `file:` protocol.
591 * @param options Either the encoding for the filename provided to the listener, or an object optionally specifying encoding, persistent, and recursive options.
592 * If `encoding` is not supplied, the default of `'utf8'` is used.
593 * If `persistent` is not supplied, the default of `true` is used.
594 * If `recursive` is not supplied, the default of `false` is used.
595 */
596 function watch(filename: PathLike, options: WatchOptions | string): AsyncIterable<string> | AsyncIterable<Buffer>;
597}