1 | /**
|
2 | * The `fs` module enables interacting with the file system in a
|
3 | * way modeled on standard POSIX functions.
|
4 | *
|
5 | * To use the promise-based APIs:
|
6 | *
|
7 | * ```js
|
8 | * import * as fs from 'fs/promises';
|
9 | * ```
|
10 | *
|
11 | * To use the callback and sync APIs:
|
12 | *
|
13 | * ```js
|
14 | * import * as fs from 'fs';
|
15 | * ```
|
16 | *
|
17 | * All file system operations have synchronous, callback, and promise-based
|
18 | * forms, and are accessible using both CommonJS syntax and ES6 Modules (ESM).
|
19 | * @see [source](https://github.com/nodejs/node/blob/v17.0.0/lib/fs.js)
|
20 | */
|
21 | declare module 'fs' {
|
22 | import * as stream from 'node:stream';
|
23 | import { Abortable, EventEmitter } from 'node:events';
|
24 | import { URL } from 'node:url';
|
25 | import * as promises from 'node:fs/promises';
|
26 | export { promises };
|
27 | /**
|
28 | * Valid types for path values in "fs".
|
29 | */
|
30 | export type PathLike = string | Buffer | URL;
|
31 | export type PathOrFileDescriptor = PathLike | number;
|
32 | export type TimeLike = string | number | Date;
|
33 | export type NoParamCallback = (err: NodeJS.ErrnoException | null) => void;
|
34 | export type BufferEncodingOption =
|
35 | | 'buffer'
|
36 | | {
|
37 | encoding: 'buffer';
|
38 | };
|
39 | export interface ObjectEncodingOptions {
|
40 | encoding?: BufferEncoding | null | undefined;
|
41 | }
|
42 | export type EncodingOption = ObjectEncodingOptions | BufferEncoding | undefined | null;
|
43 | export type OpenMode = number | string;
|
44 | export type Mode = number | string;
|
45 | export interface StatsBase<T> {
|
46 | isFile(): boolean;
|
47 | isDirectory(): boolean;
|
48 | isBlockDevice(): boolean;
|
49 | isCharacterDevice(): boolean;
|
50 | isSymbolicLink(): boolean;
|
51 | isFIFO(): boolean;
|
52 | isSocket(): boolean;
|
53 | dev: T;
|
54 | ino: T;
|
55 | mode: T;
|
56 | nlink: T;
|
57 | uid: T;
|
58 | gid: T;
|
59 | rdev: T;
|
60 | size: T;
|
61 | blksize: T;
|
62 | blocks: T;
|
63 | atimeMs: T;
|
64 | mtimeMs: T;
|
65 | ctimeMs: T;
|
66 | birthtimeMs: T;
|
67 | atime: Date;
|
68 | mtime: Date;
|
69 | ctime: Date;
|
70 | birthtime: Date;
|
71 | }
|
72 | export interface Stats extends StatsBase<number> {}
|
73 | /**
|
74 | * A `fs.Stats` object provides information about a file.
|
75 | *
|
76 | * Objects returned from {@link stat}, {@link lstat} and {@link fstat} and
|
77 | * their synchronous counterparts are of this type.
|
78 | * If `bigint` in the `options` passed to those methods is true, the numeric values
|
79 | * will be `bigint` instead of `number`, and the object will contain additional
|
80 | * nanosecond-precision properties suffixed with `Ns`.
|
81 | *
|
82 | * ```console
|
83 | * Stats {
|
84 | * dev: 2114,
|
85 | * ino: 48064969,
|
86 | * mode: 33188,
|
87 | * nlink: 1,
|
88 | * uid: 85,
|
89 | * gid: 100,
|
90 | * rdev: 0,
|
91 | * size: 527,
|
92 | * blksize: 4096,
|
93 | * blocks: 8,
|
94 | * atimeMs: 1318289051000.1,
|
95 | * mtimeMs: 1318289051000.1,
|
96 | * ctimeMs: 1318289051000.1,
|
97 | * birthtimeMs: 1318289051000.1,
|
98 | * atime: Mon, 10 Oct 2011 23:24:11 GMT,
|
99 | * mtime: Mon, 10 Oct 2011 23:24:11 GMT,
|
100 | * ctime: Mon, 10 Oct 2011 23:24:11 GMT,
|
101 | * birthtime: Mon, 10 Oct 2011 23:24:11 GMT }
|
102 | * ```
|
103 | *
|
104 | * `bigint` version:
|
105 | *
|
106 | * ```console
|
107 | * BigIntStats {
|
108 | * dev: 2114n,
|
109 | * ino: 48064969n,
|
110 | * mode: 33188n,
|
111 | * nlink: 1n,
|
112 | * uid: 85n,
|
113 | * gid: 100n,
|
114 | * rdev: 0n,
|
115 | * size: 527n,
|
116 | * blksize: 4096n,
|
117 | * blocks: 8n,
|
118 | * atimeMs: 1318289051000n,
|
119 | * mtimeMs: 1318289051000n,
|
120 | * ctimeMs: 1318289051000n,
|
121 | * birthtimeMs: 1318289051000n,
|
122 | * atimeNs: 1318289051000000000n,
|
123 | * mtimeNs: 1318289051000000000n,
|
124 | * ctimeNs: 1318289051000000000n,
|
125 | * birthtimeNs: 1318289051000000000n,
|
126 | * atime: Mon, 10 Oct 2011 23:24:11 GMT,
|
127 | * mtime: Mon, 10 Oct 2011 23:24:11 GMT,
|
128 | * ctime: Mon, 10 Oct 2011 23:24:11 GMT,
|
129 | * birthtime: Mon, 10 Oct 2011 23:24:11 GMT }
|
130 | * ```
|
131 | * @since v0.1.21
|
132 | */
|
133 | export class Stats {}
|
134 | /**
|
135 | * A representation of a directory entry, which can be a file or a subdirectory
|
136 | * within the directory, as returned by reading from an `fs.Dir`. The
|
137 | * directory entry is a combination of the file name and file type pairs.
|
138 | *
|
139 | * Additionally, when {@link readdir} or {@link readdirSync} is called with
|
140 | * the `withFileTypes` option set to `true`, the resulting array is filled with `fs.Dirent` objects, rather than strings or `Buffer` s.
|
141 | * @since v10.10.0
|
142 | */
|
143 | export class Dirent {
|
144 | /**
|
145 | * Returns `true` if the `fs.Dirent` object describes a regular file.
|
146 | * @since v10.10.0
|
147 | */
|
148 | isFile(): boolean;
|
149 | /**
|
150 | * Returns `true` if the `fs.Dirent` object describes a file system
|
151 | * directory.
|
152 | * @since v10.10.0
|
153 | */
|
154 | isDirectory(): boolean;
|
155 | /**
|
156 | * Returns `true` if the `fs.Dirent` object describes a block device.
|
157 | * @since v10.10.0
|
158 | */
|
159 | isBlockDevice(): boolean;
|
160 | /**
|
161 | * Returns `true` if the `fs.Dirent` object describes a character device.
|
162 | * @since v10.10.0
|
163 | */
|
164 | isCharacterDevice(): boolean;
|
165 | /**
|
166 | * Returns `true` if the `fs.Dirent` object describes a symbolic link.
|
167 | * @since v10.10.0
|
168 | */
|
169 | isSymbolicLink(): boolean;
|
170 | /**
|
171 | * Returns `true` if the `fs.Dirent` object describes a first-in-first-out
|
172 | * (FIFO) pipe.
|
173 | * @since v10.10.0
|
174 | */
|
175 | isFIFO(): boolean;
|
176 | /**
|
177 | * Returns `true` if the `fs.Dirent` object describes a socket.
|
178 | * @since v10.10.0
|
179 | */
|
180 | isSocket(): boolean;
|
181 | /**
|
182 | * The file name that this `fs.Dirent` object refers to. The type of this
|
183 | * value is determined by the `options.encoding` passed to {@link readdir} or {@link readdirSync}.
|
184 | * @since v10.10.0
|
185 | */
|
186 | name: string;
|
187 | }
|
188 | /**
|
189 | * A class representing a directory stream.
|
190 | *
|
191 | * Created by {@link opendir}, {@link opendirSync}, or `fsPromises.opendir()`.
|
192 | *
|
193 | * ```js
|
194 | * import { opendir } from 'fs/promises';
|
195 | *
|
196 | * try {
|
197 | * const dir = await opendir('./');
|
198 | * for await (const dirent of dir)
|
199 | * console.log(dirent.name);
|
200 | * } catch (err) {
|
201 | * console.error(err);
|
202 | * }
|
203 | * ```
|
204 | *
|
205 | * When using the async iterator, the `fs.Dir` object will be automatically
|
206 | * closed after the iterator exits.
|
207 | * @since v12.12.0
|
208 | */
|
209 | export class Dir implements AsyncIterable<Dirent> {
|
210 | /**
|
211 | * The read-only path of this directory as was provided to {@link opendir},{@link opendirSync}, or `fsPromises.opendir()`.
|
212 | * @since v12.12.0
|
213 | */
|
214 | readonly path: string;
|
215 | /**
|
216 | * Asynchronously iterates over the directory via `readdir(3)` until all entries have been read.
|
217 | */
|
218 | [Symbol.asyncIterator](): AsyncIterableIterator<Dirent>;
|
219 | /**
|
220 | * Asynchronously close the directory's underlying resource handle.
|
221 | * Subsequent reads will result in errors.
|
222 | *
|
223 | * A promise is returned that will be resolved after the resource has been
|
224 | * closed.
|
225 | * @since v12.12.0
|
226 | */
|
227 | close(): Promise<void>;
|
228 | close(cb: NoParamCallback): void;
|
229 | /**
|
230 | * Synchronously close the directory's underlying resource handle.
|
231 | * Subsequent reads will result in errors.
|
232 | * @since v12.12.0
|
233 | */
|
234 | closeSync(): void;
|
235 | /**
|
236 | * Asynchronously read the next directory entry via [`readdir(3)`](http://man7.org/linux/man-pages/man3/readdir.3.html) as an `fs.Dirent`.
|
237 | *
|
238 | * A promise is returned that will be resolved with an `fs.Dirent`, or `null`if there are no more directory entries to read.
|
239 | *
|
240 | * Directory entries returned by this function are in no particular order as
|
241 | * provided by the operating system's underlying directory mechanisms.
|
242 | * Entries added or removed while iterating over the directory might not be
|
243 | * included in the iteration results.
|
244 | * @since v12.12.0
|
245 | * @return containing {fs.Dirent|null}
|
246 | */
|
247 | read(): Promise<Dirent | null>;
|
248 | read(cb: (err: NodeJS.ErrnoException | null, dirEnt: Dirent | null) => void): void;
|
249 | /**
|
250 | * Synchronously read the next directory entry as an `fs.Dirent`. See the
|
251 | * POSIX [`readdir(3)`](http://man7.org/linux/man-pages/man3/readdir.3.html) documentation for more detail.
|
252 | *
|
253 | * If there are no more directory entries to read, `null` will be returned.
|
254 | *
|
255 | * Directory entries returned by this function are in no particular order as
|
256 | * provided by the operating system's underlying directory mechanisms.
|
257 | * Entries added or removed while iterating over the directory might not be
|
258 | * included in the iteration results.
|
259 | * @since v12.12.0
|
260 | */
|
261 | readSync(): Dirent | null;
|
262 | }
|
263 | /**
|
264 | * Class: fs.StatWatcher
|
265 | * @since v14.3.0, v12.20.0
|
266 | * Extends `EventEmitter`
|
267 | * A successful call to {@link watchFile} method will return a new fs.StatWatcher object.
|
268 | */
|
269 | export interface StatWatcher extends EventEmitter {
|
270 | /**
|
271 | * When called, requests that the Node.js event loop _not_ exit so long as the `fs.StatWatcher` is active. Calling `watcher.ref()` multiple times will have
|
272 | * no effect.
|
273 | *
|
274 | * By default, all `fs.StatWatcher` objects are "ref'ed", making it normally
|
275 | * unnecessary to call `watcher.ref()` unless `watcher.unref()` had been
|
276 | * called previously.
|
277 | * @since v14.3.0, v12.20.0
|
278 | */
|
279 | ref(): this;
|
280 | /**
|
281 | * When called, the active `fs.StatWatcher` object will not require the Node.js
|
282 | * event loop to remain active. If there is no other activity keeping the
|
283 | * event loop running, the process may exit before the `fs.StatWatcher` object's
|
284 | * callback is invoked. Calling `watcher.unref()` multiple times will have
|
285 | * no effect.
|
286 | * @since v14.3.0, v12.20.0
|
287 | */
|
288 | unref(): this;
|
289 | }
|
290 | export interface FSWatcher extends EventEmitter {
|
291 | /**
|
292 | * Stop watching for changes on the given `fs.FSWatcher`. Once stopped, the `fs.FSWatcher` object is no longer usable.
|
293 | * @since v0.5.8
|
294 | */
|
295 | close(): void;
|
296 | /**
|
297 | * events.EventEmitter
|
298 | * 1. change
|
299 | * 2. error
|
300 | */
|
301 | addListener(event: string, listener: (...args: any[]) => void): this;
|
302 | addListener(event: 'change', listener: (eventType: string, filename: string | Buffer) => void): this;
|
303 | addListener(event: 'error', listener: (error: Error) => void): this;
|
304 | addListener(event: 'close', listener: () => void): this;
|
305 | on(event: string, listener: (...args: any[]) => void): this;
|
306 | on(event: 'change', listener: (eventType: string, filename: string | Buffer) => void): this;
|
307 | on(event: 'error', listener: (error: Error) => void): this;
|
308 | on(event: 'close', listener: () => void): this;
|
309 | once(event: string, listener: (...args: any[]) => void): this;
|
310 | once(event: 'change', listener: (eventType: string, filename: string | Buffer) => void): this;
|
311 | once(event: 'error', listener: (error: Error) => void): this;
|
312 | once(event: 'close', listener: () => void): this;
|
313 | prependListener(event: string, listener: (...args: any[]) => void): this;
|
314 | prependListener(event: 'change', listener: (eventType: string, filename: string | Buffer) => void): this;
|
315 | prependListener(event: 'error', listener: (error: Error) => void): this;
|
316 | prependListener(event: 'close', listener: () => void): this;
|
317 | prependOnceListener(event: string, listener: (...args: any[]) => void): this;
|
318 | prependOnceListener(event: 'change', listener: (eventType: string, filename: string | Buffer) => void): this;
|
319 | prependOnceListener(event: 'error', listener: (error: Error) => void): this;
|
320 | prependOnceListener(event: 'close', listener: () => void): this;
|
321 | }
|
322 | /**
|
323 | * Instances of `fs.ReadStream` are created and returned using the {@link createReadStream} function.
|
324 | * @since v0.1.93
|
325 | */
|
326 | export class ReadStream extends stream.Readable {
|
327 | close(callback?: (err?: NodeJS.ErrnoException | null) => void): void;
|
328 | /**
|
329 | * The number of bytes that have been read so far.
|
330 | * @since v6.4.0
|
331 | */
|
332 | bytesRead: number;
|
333 | /**
|
334 | * The path to the file the stream is reading from as specified in the first
|
335 | * argument to `fs.createReadStream()`. If `path` is passed as a string, then`readStream.path` will be a string. If `path` is passed as a `Buffer`, then`readStream.path` will be a
|
336 | * `Buffer`. If `fd` is specified, then`readStream.path` will be `undefined`.
|
337 | * @since v0.1.93
|
338 | */
|
339 | path: string | Buffer;
|
340 | /**
|
341 | * This property is `true` if the underlying file has not been opened yet,
|
342 | * i.e. before the `'ready'` event is emitted.
|
343 | * @since v11.2.0, v10.16.0
|
344 | */
|
345 | pending: boolean;
|
346 | /**
|
347 | * events.EventEmitter
|
348 | * 1. open
|
349 | * 2. close
|
350 | * 3. ready
|
351 | */
|
352 | addListener(event: 'close', listener: () => void): this;
|
353 | addListener(event: 'data', listener: (chunk: Buffer | string) => void): this;
|
354 | addListener(event: 'end', listener: () => void): this;
|
355 | addListener(event: 'error', listener: (err: Error) => void): this;
|
356 | addListener(event: 'open', listener: (fd: number) => void): this;
|
357 | addListener(event: 'pause', listener: () => void): this;
|
358 | addListener(event: 'readable', listener: () => void): this;
|
359 | addListener(event: 'ready', listener: () => void): this;
|
360 | addListener(event: 'resume', listener: () => void): this;
|
361 | addListener(event: string | symbol, listener: (...args: any[]) => void): this;
|
362 | on(event: 'close', listener: () => void): this;
|
363 | on(event: 'data', listener: (chunk: Buffer | string) => void): this;
|
364 | on(event: 'end', listener: () => void): this;
|
365 | on(event: 'error', listener: (err: Error) => void): this;
|
366 | on(event: 'open', listener: (fd: number) => void): this;
|
367 | on(event: 'pause', listener: () => void): this;
|
368 | on(event: 'readable', listener: () => void): this;
|
369 | on(event: 'ready', listener: () => void): this;
|
370 | on(event: 'resume', listener: () => void): this;
|
371 | on(event: string | symbol, listener: (...args: any[]) => void): this;
|
372 | once(event: 'close', listener: () => void): this;
|
373 | once(event: 'data', listener: (chunk: Buffer | string) => void): this;
|
374 | once(event: 'end', listener: () => void): this;
|
375 | once(event: 'error', listener: (err: Error) => void): this;
|
376 | once(event: 'open', listener: (fd: number) => void): this;
|
377 | once(event: 'pause', listener: () => void): this;
|
378 | once(event: 'readable', listener: () => void): this;
|
379 | once(event: 'ready', listener: () => void): this;
|
380 | once(event: 'resume', listener: () => void): this;
|
381 | once(event: string | symbol, listener: (...args: any[]) => void): this;
|
382 | prependListener(event: 'close', listener: () => void): this;
|
383 | prependListener(event: 'data', listener: (chunk: Buffer | string) => void): this;
|
384 | prependListener(event: 'end', listener: () => void): this;
|
385 | prependListener(event: 'error', listener: (err: Error) => void): this;
|
386 | prependListener(event: 'open', listener: (fd: number) => void): this;
|
387 | prependListener(event: 'pause', listener: () => void): this;
|
388 | prependListener(event: 'readable', listener: () => void): this;
|
389 | prependListener(event: 'ready', listener: () => void): this;
|
390 | prependListener(event: 'resume', listener: () => void): this;
|
391 | prependListener(event: string | symbol, listener: (...args: any[]) => void): this;
|
392 | prependOnceListener(event: 'close', listener: () => void): this;
|
393 | prependOnceListener(event: 'data', listener: (chunk: Buffer | string) => void): this;
|
394 | prependOnceListener(event: 'end', listener: () => void): this;
|
395 | prependOnceListener(event: 'error', listener: (err: Error) => void): this;
|
396 | prependOnceListener(event: 'open', listener: (fd: number) => void): this;
|
397 | prependOnceListener(event: 'pause', listener: () => void): this;
|
398 | prependOnceListener(event: 'readable', listener: () => void): this;
|
399 | prependOnceListener(event: 'ready', listener: () => void): this;
|
400 | prependOnceListener(event: 'resume', listener: () => void): this;
|
401 | prependOnceListener(event: string | symbol, listener: (...args: any[]) => void): this;
|
402 | }
|
403 | /**
|
404 | * * Extends `stream.Writable`
|
405 | *
|
406 | * Instances of `fs.WriteStream` are created and returned using the {@link createWriteStream} function.
|
407 | * @since v0.1.93
|
408 | */
|
409 | export class WriteStream extends stream.Writable {
|
410 | /**
|
411 | * Closes `writeStream`. Optionally accepts a
|
412 | * callback that will be executed once the `writeStream`is closed.
|
413 | * @since v0.9.4
|
414 | */
|
415 | close(callback?: (err?: NodeJS.ErrnoException | null) => void): void;
|
416 | /**
|
417 | * The number of bytes written so far. Does not include data that is still queued
|
418 | * for writing.
|
419 | * @since v0.4.7
|
420 | */
|
421 | bytesWritten: number;
|
422 | /**
|
423 | * The path to the file the stream is writing to as specified in the first
|
424 | * argument to {@link createWriteStream}. If `path` is passed as a string, then`writeStream.path` will be a string. If `path` is passed as a `Buffer`, then`writeStream.path` will be a
|
425 | * `Buffer`.
|
426 | * @since v0.1.93
|
427 | */
|
428 | path: string | Buffer;
|
429 | /**
|
430 | * This property is `true` if the underlying file has not been opened yet,
|
431 | * i.e. before the `'ready'` event is emitted.
|
432 | * @since v11.2.0
|
433 | */
|
434 | pending: boolean;
|
435 | /**
|
436 | * events.EventEmitter
|
437 | * 1. open
|
438 | * 2. close
|
439 | * 3. ready
|
440 | */
|
441 | addListener(event: 'close', listener: () => void): this;
|
442 | addListener(event: 'drain', listener: () => void): this;
|
443 | addListener(event: 'error', listener: (err: Error) => void): this;
|
444 | addListener(event: 'finish', listener: () => void): this;
|
445 | addListener(event: 'open', listener: (fd: number) => void): this;
|
446 | addListener(event: 'pipe', listener: (src: stream.Readable) => void): this;
|
447 | addListener(event: 'ready', listener: () => void): this;
|
448 | addListener(event: 'unpipe', listener: (src: stream.Readable) => void): this;
|
449 | addListener(event: string | symbol, listener: (...args: any[]) => void): this;
|
450 | on(event: 'close', listener: () => void): this;
|
451 | on(event: 'drain', listener: () => void): this;
|
452 | on(event: 'error', listener: (err: Error) => void): this;
|
453 | on(event: 'finish', listener: () => void): this;
|
454 | on(event: 'open', listener: (fd: number) => void): this;
|
455 | on(event: 'pipe', listener: (src: stream.Readable) => void): this;
|
456 | on(event: 'ready', listener: () => void): this;
|
457 | on(event: 'unpipe', listener: (src: stream.Readable) => void): this;
|
458 | on(event: string | symbol, listener: (...args: any[]) => void): this;
|
459 | once(event: 'close', listener: () => void): this;
|
460 | once(event: 'drain', listener: () => void): this;
|
461 | once(event: 'error', listener: (err: Error) => void): this;
|
462 | once(event: 'finish', listener: () => void): this;
|
463 | once(event: 'open', listener: (fd: number) => void): this;
|
464 | once(event: 'pipe', listener: (src: stream.Readable) => void): this;
|
465 | once(event: 'ready', listener: () => void): this;
|
466 | once(event: 'unpipe', listener: (src: stream.Readable) => void): this;
|
467 | once(event: string | symbol, listener: (...args: any[]) => void): this;
|
468 | prependListener(event: 'close', listener: () => void): this;
|
469 | prependListener(event: 'drain', listener: () => void): this;
|
470 | prependListener(event: 'error', listener: (err: Error) => void): this;
|
471 | prependListener(event: 'finish', listener: () => void): this;
|
472 | prependListener(event: 'open', listener: (fd: number) => void): this;
|
473 | prependListener(event: 'pipe', listener: (src: stream.Readable) => void): this;
|
474 | prependListener(event: 'ready', listener: () => void): this;
|
475 | prependListener(event: 'unpipe', listener: (src: stream.Readable) => void): this;
|
476 | prependListener(event: string | symbol, listener: (...args: any[]) => void): this;
|
477 | prependOnceListener(event: 'close', listener: () => void): this;
|
478 | prependOnceListener(event: 'drain', listener: () => void): this;
|
479 | prependOnceListener(event: 'error', listener: (err: Error) => void): this;
|
480 | prependOnceListener(event: 'finish', listener: () => void): this;
|
481 | prependOnceListener(event: 'open', listener: (fd: number) => void): this;
|
482 | prependOnceListener(event: 'pipe', listener: (src: stream.Readable) => void): this;
|
483 | prependOnceListener(event: 'ready', listener: () => void): this;
|
484 | prependOnceListener(event: 'unpipe', listener: (src: stream.Readable) => void): this;
|
485 | prependOnceListener(event: string | symbol, listener: (...args: any[]) => void): this;
|
486 | }
|
487 | /**
|
488 | * Asynchronously rename file at `oldPath` to the pathname provided
|
489 | * as `newPath`. In the case that `newPath` already exists, it will
|
490 | * be overwritten. If there is a directory at `newPath`, an error will
|
491 | * be raised instead. No arguments other than a possible exception are
|
492 | * given to the completion callback.
|
493 | *
|
494 | * See also: [`rename(2)`](http://man7.org/linux/man-pages/man2/rename.2.html).
|
495 | *
|
496 | * ```js
|
497 | * import { rename } from 'fs';
|
498 | *
|
499 | * rename('oldFile.txt', 'newFile.txt', (err) => {
|
500 | * if (err) throw err;
|
501 | * console.log('Rename complete!');
|
502 | * });
|
503 | * ```
|
504 | * @since v0.0.2
|
505 | */
|
506 | export function rename(oldPath: PathLike, newPath: PathLike, callback: NoParamCallback): void;
|
507 | export namespace rename {
|
508 | /**
|
509 | * Asynchronous rename(2) - Change the name or location of a file or directory.
|
510 | * @param oldPath A path to a file. If a URL is provided, it must use the `file:` protocol.
|
511 | * URL support is _experimental_.
|
512 | * @param newPath A path to a file. If a URL is provided, it must use the `file:` protocol.
|
513 | * URL support is _experimental_.
|
514 | */
|
515 | function __promisify__(oldPath: PathLike, newPath: PathLike): Promise<void>;
|
516 | }
|
517 | /**
|
518 | * Renames the file from `oldPath` to `newPath`. Returns `undefined`.
|
519 | *
|
520 | * See the POSIX [`rename(2)`](http://man7.org/linux/man-pages/man2/rename.2.html) documentation for more details.
|
521 | * @since v0.1.21
|
522 | */
|
523 | export function renameSync(oldPath: PathLike, newPath: PathLike): void;
|
524 | /**
|
525 | * Truncates the file. No arguments other than a possible exception are
|
526 | * given to the completion callback. A file descriptor can also be passed as the
|
527 | * first argument. In this case, `fs.ftruncate()` is called.
|
528 | *
|
529 | * ```js
|
530 | * import { truncate } from 'fs';
|
531 | * // Assuming that 'path/file.txt' is a regular file.
|
532 | * truncate('path/file.txt', (err) => {
|
533 | * if (err) throw err;
|
534 | * console.log('path/file.txt was truncated');
|
535 | * });
|
536 | * ```
|
537 | *
|
538 | * Passing a file descriptor is deprecated and may result in an error being thrown
|
539 | * in the future.
|
540 | *
|
541 | * See the POSIX [`truncate(2)`](http://man7.org/linux/man-pages/man2/truncate.2.html) documentation for more details.
|
542 | * @since v0.8.6
|
543 | * @param [len=0]
|
544 | */
|
545 | export function truncate(path: PathLike, len: number | undefined | null, callback: NoParamCallback): void;
|
546 | /**
|
547 | * Asynchronous truncate(2) - Truncate a file to a specified length.
|
548 | * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
|
549 | */
|
550 | export function truncate(path: PathLike, callback: NoParamCallback): void;
|
551 | export namespace truncate {
|
552 | /**
|
553 | * Asynchronous truncate(2) - Truncate a file to a specified length.
|
554 | * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
|
555 | * @param len If not specified, defaults to `0`.
|
556 | */
|
557 | function __promisify__(path: PathLike, len?: number | null): Promise<void>;
|
558 | }
|
559 | /**
|
560 | * Truncates the file. Returns `undefined`. A file descriptor can also be
|
561 | * passed as the first argument. In this case, `fs.ftruncateSync()` is called.
|
562 | *
|
563 | * Passing a file descriptor is deprecated and may result in an error being thrown
|
564 | * in the future.
|
565 | * @since v0.8.6
|
566 | * @param [len=0]
|
567 | */
|
568 | export function truncateSync(path: PathLike, len?: number | null): void;
|
569 | /**
|
570 | * Truncates the file descriptor. No arguments other than a possible exception are
|
571 | * given to the completion callback.
|
572 | *
|
573 | * See the POSIX [`ftruncate(2)`](http://man7.org/linux/man-pages/man2/ftruncate.2.html) documentation for more detail.
|
574 | *
|
575 | * If the file referred to by the file descriptor was larger than `len` bytes, only
|
576 | * the first `len` bytes will be retained in the file.
|
577 | *
|
578 | * For example, the following program retains only the first four bytes of the
|
579 | * file:
|
580 | *
|
581 | * ```js
|
582 | * import { open, close, ftruncate } from 'fs';
|
583 | *
|
584 | * function closeFd(fd) {
|
585 | * close(fd, (err) => {
|
586 | * if (err) throw err;
|
587 | * });
|
588 | * }
|
589 | *
|
590 | * open('temp.txt', 'r+', (err, fd) => {
|
591 | * if (err) throw err;
|
592 | *
|
593 | * try {
|
594 | * ftruncate(fd, 4, (err) => {
|
595 | * closeFd(fd);
|
596 | * if (err) throw err;
|
597 | * });
|
598 | * } catch (err) {
|
599 | * closeFd(fd);
|
600 | * if (err) throw err;
|
601 | * }
|
602 | * });
|
603 | * ```
|
604 | *
|
605 | * If the file previously was shorter than `len` bytes, it is extended, and the
|
606 | * extended part is filled with null bytes (`'\0'`):
|
607 | *
|
608 | * If `len` is negative then `0` will be used.
|
609 | * @since v0.8.6
|
610 | * @param [len=0]
|
611 | */
|
612 | export function ftruncate(fd: number, len: number | undefined | null, callback: NoParamCallback): void;
|
613 | /**
|
614 | * Asynchronous ftruncate(2) - Truncate a file to a specified length.
|
615 | * @param fd A file descriptor.
|
616 | */
|
617 | export function ftruncate(fd: number, callback: NoParamCallback): void;
|
618 | export namespace ftruncate {
|
619 | /**
|
620 | * Asynchronous ftruncate(2) - Truncate a file to a specified length.
|
621 | * @param fd A file descriptor.
|
622 | * @param len If not specified, defaults to `0`.
|
623 | */
|
624 | function __promisify__(fd: number, len?: number | null): Promise<void>;
|
625 | }
|
626 | /**
|
627 | * Truncates the file descriptor. Returns `undefined`.
|
628 | *
|
629 | * For detailed information, see the documentation of the asynchronous version of
|
630 | * this API: {@link ftruncate}.
|
631 | * @since v0.8.6
|
632 | * @param [len=0]
|
633 | */
|
634 | export function ftruncateSync(fd: number, len?: number | null): void;
|
635 | /**
|
636 | * Asynchronously changes owner and group of a file. No arguments other than a
|
637 | * possible exception are given to the completion callback.
|
638 | *
|
639 | * See the POSIX [`chown(2)`](http://man7.org/linux/man-pages/man2/chown.2.html) documentation for more detail.
|
640 | * @since v0.1.97
|
641 | */
|
642 | export function chown(path: PathLike, uid: number, gid: number, callback: NoParamCallback): void;
|
643 | export namespace chown {
|
644 | /**
|
645 | * Asynchronous chown(2) - Change ownership of a file.
|
646 | * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
|
647 | */
|
648 | function __promisify__(path: PathLike, uid: number, gid: number): Promise<void>;
|
649 | }
|
650 | /**
|
651 | * Synchronously changes owner and group of a file. Returns `undefined`.
|
652 | * This is the synchronous version of {@link chown}.
|
653 | *
|
654 | * See the POSIX [`chown(2)`](http://man7.org/linux/man-pages/man2/chown.2.html) documentation for more detail.
|
655 | * @since v0.1.97
|
656 | */
|
657 | export function chownSync(path: PathLike, uid: number, gid: number): void;
|
658 | /**
|
659 | * Sets the owner of the file. No arguments other than a possible exception are
|
660 | * given to the completion callback.
|
661 | *
|
662 | * See the POSIX [`fchown(2)`](http://man7.org/linux/man-pages/man2/fchown.2.html) documentation for more detail.
|
663 | * @since v0.4.7
|
664 | */
|
665 | export function fchown(fd: number, uid: number, gid: number, callback: NoParamCallback): void;
|
666 | export namespace fchown {
|
667 | /**
|
668 | * Asynchronous fchown(2) - Change ownership of a file.
|
669 | * @param fd A file descriptor.
|
670 | */
|
671 | function __promisify__(fd: number, uid: number, gid: number): Promise<void>;
|
672 | }
|
673 | /**
|
674 | * Sets the owner of the file. Returns `undefined`.
|
675 | *
|
676 | * See the POSIX [`fchown(2)`](http://man7.org/linux/man-pages/man2/fchown.2.html) documentation for more detail.
|
677 | * @since v0.4.7
|
678 | * @param uid The file's new owner's user id.
|
679 | * @param gid The file's new group's group id.
|
680 | */
|
681 | export function fchownSync(fd: number, uid: number, gid: number): void;
|
682 | /**
|
683 | * Set the owner of the symbolic link. No arguments other than a possible
|
684 | * exception are given to the completion callback.
|
685 | *
|
686 | * See the POSIX [`lchown(2)`](http://man7.org/linux/man-pages/man2/lchown.2.html) documentation for more detail.
|
687 | */
|
688 | export function lchown(path: PathLike, uid: number, gid: number, callback: NoParamCallback): void;
|
689 | export namespace lchown {
|
690 | /**
|
691 | * Asynchronous lchown(2) - Change ownership of a file. Does not dereference symbolic links.
|
692 | * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
|
693 | */
|
694 | function __promisify__(path: PathLike, uid: number, gid: number): Promise<void>;
|
695 | }
|
696 | /**
|
697 | * Set the owner for the path. Returns `undefined`.
|
698 | *
|
699 | * See the POSIX [`lchown(2)`](http://man7.org/linux/man-pages/man2/lchown.2.html) documentation for more details.
|
700 | * @param uid The file's new owner's user id.
|
701 | * @param gid The file's new group's group id.
|
702 | */
|
703 | export function lchownSync(path: PathLike, uid: number, gid: number): void;
|
704 | /**
|
705 | * Changes the access and modification times of a file in the same way as {@link utimes}, with the difference that if the path refers to a symbolic
|
706 | * link, then the link is not dereferenced: instead, the timestamps of the
|
707 | * symbolic link itself are changed.
|
708 | *
|
709 | * No arguments other than a possible exception are given to the completion
|
710 | * callback.
|
711 | * @since v14.5.0, v12.19.0
|
712 | */
|
713 | export function lutimes(path: PathLike, atime: TimeLike, mtime: TimeLike, callback: NoParamCallback): void;
|
714 | export namespace lutimes {
|
715 | /**
|
716 | * Changes the access and modification times of a file in the same way as `fsPromises.utimes()`,
|
717 | * with the difference that if the path refers to a symbolic link, then the link is not
|
718 | * dereferenced: instead, the timestamps of the symbolic link itself are changed.
|
719 | * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
|
720 | * @param atime The last access time. If a string is provided, it will be coerced to number.
|
721 | * @param mtime The last modified time. If a string is provided, it will be coerced to number.
|
722 | */
|
723 | function __promisify__(path: PathLike, atime: TimeLike, mtime: TimeLike): Promise<void>;
|
724 | }
|
725 | /**
|
726 | * Change the file system timestamps of the symbolic link referenced by `path`.
|
727 | * Returns `undefined`, or throws an exception when parameters are incorrect or
|
728 | * the operation fails. This is the synchronous version of {@link lutimes}.
|
729 | * @since v14.5.0, v12.19.0
|
730 | */
|
731 | export function lutimesSync(path: PathLike, atime: TimeLike, mtime: TimeLike): void;
|
732 | /**
|
733 | * Asynchronously changes the permissions of a file. No arguments other than a
|
734 | * possible exception are given to the completion callback.
|
735 | *
|
736 | * See the POSIX [`chmod(2)`](http://man7.org/linux/man-pages/man2/chmod.2.html) documentation for more detail.
|
737 | *
|
738 | * ```js
|
739 | * import { chmod } from 'fs';
|
740 | *
|
741 | * chmod('my_file.txt', 0o775, (err) => {
|
742 | * if (err) throw err;
|
743 | * console.log('The permissions for file "my_file.txt" have been changed!');
|
744 | * });
|
745 | * ```
|
746 | * @since v0.1.30
|
747 | */
|
748 | export function chmod(path: PathLike, mode: Mode, callback: NoParamCallback): void;
|
749 | export namespace chmod {
|
750 | /**
|
751 | * Asynchronous chmod(2) - Change permissions of a file.
|
752 | * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
|
753 | * @param mode A file mode. If a string is passed, it is parsed as an octal integer.
|
754 | */
|
755 | function __promisify__(path: PathLike, mode: Mode): Promise<void>;
|
756 | }
|
757 | /**
|
758 | * For detailed information, see the documentation of the asynchronous version of
|
759 | * this API: {@link chmod}.
|
760 | *
|
761 | * See the POSIX [`chmod(2)`](http://man7.org/linux/man-pages/man2/chmod.2.html) documentation for more detail.
|
762 | * @since v0.6.7
|
763 | */
|
764 | export function chmodSync(path: PathLike, mode: Mode): void;
|
765 | /**
|
766 | * Sets the permissions on the file. No arguments other than a possible exception
|
767 | * are given to the completion callback.
|
768 | *
|
769 | * See the POSIX [`fchmod(2)`](http://man7.org/linux/man-pages/man2/fchmod.2.html) documentation for more detail.
|
770 | * @since v0.4.7
|
771 | */
|
772 | export function fchmod(fd: number, mode: Mode, callback: NoParamCallback): void;
|
773 | export namespace fchmod {
|
774 | /**
|
775 | * Asynchronous fchmod(2) - Change permissions of a file.
|
776 | * @param fd A file descriptor.
|
777 | * @param mode A file mode. If a string is passed, it is parsed as an octal integer.
|
778 | */
|
779 | function __promisify__(fd: number, mode: Mode): Promise<void>;
|
780 | }
|
781 | /**
|
782 | * Sets the permissions on the file. Returns `undefined`.
|
783 | *
|
784 | * See the POSIX [`fchmod(2)`](http://man7.org/linux/man-pages/man2/fchmod.2.html) documentation for more detail.
|
785 | * @since v0.4.7
|
786 | */
|
787 | export function fchmodSync(fd: number, mode: Mode): void;
|
788 | /**
|
789 | * Changes the permissions on a symbolic link. No arguments other than a possible
|
790 | * exception are given to the completion callback.
|
791 | *
|
792 | * This method is only implemented on macOS.
|
793 | *
|
794 | * See the POSIX [`lchmod(2)`](https://www.freebsd.org/cgi/man.cgi?query=lchmod&sektion=2) documentation for more detail.
|
795 | * @deprecated Since v0.4.7
|
796 | */
|
797 | export function lchmod(path: PathLike, mode: Mode, callback: NoParamCallback): void;
|
798 | /** @deprecated */
|
799 | export namespace lchmod {
|
800 | /**
|
801 | * Asynchronous lchmod(2) - Change permissions of a file. Does not dereference symbolic links.
|
802 | * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
|
803 | * @param mode A file mode. If a string is passed, it is parsed as an octal integer.
|
804 | */
|
805 | function __promisify__(path: PathLike, mode: Mode): Promise<void>;
|
806 | }
|
807 | /**
|
808 | * Changes the permissions on a symbolic link. Returns `undefined`.
|
809 | *
|
810 | * This method is only implemented on macOS.
|
811 | *
|
812 | * See the POSIX [`lchmod(2)`](https://www.freebsd.org/cgi/man.cgi?query=lchmod&sektion=2) documentation for more detail.
|
813 | * @deprecated Since v0.4.7
|
814 | */
|
815 | export function lchmodSync(path: PathLike, mode: Mode): void;
|
816 | /**
|
817 | * Asynchronous [`stat(2)`](http://man7.org/linux/man-pages/man2/stat.2.html). The callback gets two arguments `(err, stats)` where`stats` is an `fs.Stats` object.
|
818 | *
|
819 | * In case of an error, the `err.code` will be one of `Common System Errors`.
|
820 | *
|
821 | * Using `fs.stat()` to check for the existence of a file before calling`fs.open()`, `fs.readFile()` or `fs.writeFile()` is not recommended.
|
822 | * Instead, user code should open/read/write the file directly and handle the
|
823 | * error raised if the file is not available.
|
824 | *
|
825 | * To check if a file exists without manipulating it afterwards, {@link access} is recommended.
|
826 | *
|
827 | * For example, given the following directory structure:
|
828 | *
|
829 | * ```text
|
830 | * - txtDir
|
831 | * -- file.txt
|
832 | * - app.js
|
833 | * ```
|
834 | *
|
835 | * The next program will check for the stats of the given paths:
|
836 | *
|
837 | * ```js
|
838 | * import { stat } from 'fs';
|
839 | *
|
840 | * const pathsToCheck = ['./txtDir', './txtDir/file.txt'];
|
841 | *
|
842 | * for (let i = 0; i < pathsToCheck.length; i++) {
|
843 | * stat(pathsToCheck[i], (err, stats) => {
|
844 | * console.log(stats.isDirectory());
|
845 | * console.log(stats);
|
846 | * });
|
847 | * }
|
848 | * ```
|
849 | *
|
850 | * The resulting output will resemble:
|
851 | *
|
852 | * ```console
|
853 | * true
|
854 | * Stats {
|
855 | * dev: 16777220,
|
856 | * mode: 16877,
|
857 | * nlink: 3,
|
858 | * uid: 501,
|
859 | * gid: 20,
|
860 | * rdev: 0,
|
861 | * blksize: 4096,
|
862 | * ino: 14214262,
|
863 | * size: 96,
|
864 | * blocks: 0,
|
865 | * atimeMs: 1561174653071.963,
|
866 | * mtimeMs: 1561174614583.3518,
|
867 | * ctimeMs: 1561174626623.5366,
|
868 | * birthtimeMs: 1561174126937.2893,
|
869 | * atime: 2019-06-22T03:37:33.072Z,
|
870 | * mtime: 2019-06-22T03:36:54.583Z,
|
871 | * ctime: 2019-06-22T03:37:06.624Z,
|
872 | * birthtime: 2019-06-22T03:28:46.937Z
|
873 | * }
|
874 | * false
|
875 | * Stats {
|
876 | * dev: 16777220,
|
877 | * mode: 33188,
|
878 | * nlink: 1,
|
879 | * uid: 501,
|
880 | * gid: 20,
|
881 | * rdev: 0,
|
882 | * blksize: 4096,
|
883 | * ino: 14214074,
|
884 | * size: 8,
|
885 | * blocks: 8,
|
886 | * atimeMs: 1561174616618.8555,
|
887 | * mtimeMs: 1561174614584,
|
888 | * ctimeMs: 1561174614583.8145,
|
889 | * birthtimeMs: 1561174007710.7478,
|
890 | * atime: 2019-06-22T03:36:56.619Z,
|
891 | * mtime: 2019-06-22T03:36:54.584Z,
|
892 | * ctime: 2019-06-22T03:36:54.584Z,
|
893 | * birthtime: 2019-06-22T03:26:47.711Z
|
894 | * }
|
895 | * ```
|
896 | * @since v0.0.2
|
897 | */
|
898 | export function stat(path: PathLike, callback: (err: NodeJS.ErrnoException | null, stats: Stats) => void): void;
|
899 | export function stat(
|
900 | path: PathLike,
|
901 | options:
|
902 | | (StatOptions & {
|
903 | bigint?: false | undefined;
|
904 | })
|
905 | | undefined,
|
906 | callback: (err: NodeJS.ErrnoException | null, stats: Stats) => void
|
907 | ): void;
|
908 | export function stat(
|
909 | path: PathLike,
|
910 | options: StatOptions & {
|
911 | bigint: true;
|
912 | },
|
913 | callback: (err: NodeJS.ErrnoException | null, stats: BigIntStats) => void
|
914 | ): void;
|
915 | export function stat(path: PathLike, options: StatOptions | undefined, callback: (err: NodeJS.ErrnoException | null, stats: Stats | BigIntStats) => void): void;
|
916 | export namespace stat {
|
917 | /**
|
918 | * Asynchronous stat(2) - Get file status.
|
919 | * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
|
920 | */
|
921 | function __promisify__(
|
922 | path: PathLike,
|
923 | options?: StatOptions & {
|
924 | bigint?: false | undefined;
|
925 | }
|
926 | ): Promise<Stats>;
|
927 | function __promisify__(
|
928 | path: PathLike,
|
929 | options: StatOptions & {
|
930 | bigint: true;
|
931 | }
|
932 | ): Promise<BigIntStats>;
|
933 | function __promisify__(path: PathLike, options?: StatOptions): Promise<Stats | BigIntStats>;
|
934 | }
|
935 | export interface StatSyncFn extends Function {
|
936 | (path: PathLike, options?: undefined): Stats;
|
937 | (
|
938 | path: PathLike,
|
939 | options?: StatSyncOptions & {
|
940 | bigint?: false | undefined;
|
941 | throwIfNoEntry: false;
|
942 | }
|
943 | ): Stats | undefined;
|
944 | (
|
945 | path: PathLike,
|
946 | options: StatSyncOptions & {
|
947 | bigint: true;
|
948 | throwIfNoEntry: false;
|
949 | }
|
950 | ): BigIntStats | undefined;
|
951 | (
|
952 | path: PathLike,
|
953 | options?: StatSyncOptions & {
|
954 | bigint?: false | undefined;
|
955 | }
|
956 | ): Stats;
|
957 | (
|
958 | path: PathLike,
|
959 | options: StatSyncOptions & {
|
960 | bigint: true;
|
961 | }
|
962 | ): BigIntStats;
|
963 | (
|
964 | path: PathLike,
|
965 | options: StatSyncOptions & {
|
966 | bigint: boolean;
|
967 | throwIfNoEntry?: false | undefined;
|
968 | }
|
969 | ): Stats | BigIntStats;
|
970 | (path: PathLike, options?: StatSyncOptions): Stats | BigIntStats | undefined;
|
971 | }
|
972 | /**
|
973 | * Synchronous stat(2) - Get file status.
|
974 | * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
|
975 | */
|
976 | export const statSync: StatSyncFn;
|
977 | /**
|
978 | * Invokes the callback with the `fs.Stats` for the file descriptor.
|
979 | *
|
980 | * See the POSIX [`fstat(2)`](http://man7.org/linux/man-pages/man2/fstat.2.html) documentation for more detail.
|
981 | * @since v0.1.95
|
982 | */
|
983 | export function fstat(fd: number, callback: (err: NodeJS.ErrnoException | null, stats: Stats) => void): void;
|
984 | export function fstat(
|
985 | fd: number,
|
986 | options:
|
987 | | (StatOptions & {
|
988 | bigint?: false | undefined;
|
989 | })
|
990 | | undefined,
|
991 | callback: (err: NodeJS.ErrnoException | null, stats: Stats) => void
|
992 | ): void;
|
993 | export function fstat(
|
994 | fd: number,
|
995 | options: StatOptions & {
|
996 | bigint: true;
|
997 | },
|
998 | callback: (err: NodeJS.ErrnoException | null, stats: BigIntStats) => void
|
999 | ): void;
|
1000 | export function fstat(fd: number, options: StatOptions | undefined, callback: (err: NodeJS.ErrnoException | null, stats: Stats | BigIntStats) => void): void;
|
1001 | export namespace fstat {
|
1002 | /**
|
1003 | * Asynchronous fstat(2) - Get file status.
|
1004 | * @param fd A file descriptor.
|
1005 | */
|
1006 | function __promisify__(
|
1007 | fd: number,
|
1008 | options?: StatOptions & {
|
1009 | bigint?: false | undefined;
|
1010 | }
|
1011 | ): Promise<Stats>;
|
1012 | function __promisify__(
|
1013 | fd: number,
|
1014 | options: StatOptions & {
|
1015 | bigint: true;
|
1016 | }
|
1017 | ): Promise<BigIntStats>;
|
1018 | function __promisify__(fd: number, options?: StatOptions): Promise<Stats | BigIntStats>;
|
1019 | }
|
1020 | /**
|
1021 | * Retrieves the `fs.Stats` for the file descriptor.
|
1022 | *
|
1023 | * See the POSIX [`fstat(2)`](http://man7.org/linux/man-pages/man2/fstat.2.html) documentation for more detail.
|
1024 | * @since v0.1.95
|
1025 | */
|
1026 | export function fstatSync(
|
1027 | fd: number,
|
1028 | options?: StatOptions & {
|
1029 | bigint?: false | undefined;
|
1030 | }
|
1031 | ): Stats;
|
1032 | export function fstatSync(
|
1033 | fd: number,
|
1034 | options: StatOptions & {
|
1035 | bigint: true;
|
1036 | }
|
1037 | ): BigIntStats;
|
1038 | export function fstatSync(fd: number, options?: StatOptions): Stats | BigIntStats;
|
1039 | /**
|
1040 | * Retrieves the `fs.Stats` for the symbolic link referred to by the path.
|
1041 | * The callback gets two arguments `(err, stats)` where `stats` is a `fs.Stats` object. `lstat()` is identical to `stat()`, except that if `path` is a symbolic
|
1042 | * link, then the link itself is stat-ed, not the file that it refers to.
|
1043 | *
|
1044 | * See the POSIX [`lstat(2)`](http://man7.org/linux/man-pages/man2/lstat.2.html) documentation for more details.
|
1045 | * @since v0.1.30
|
1046 | */
|
1047 | export function lstat(path: PathLike, callback: (err: NodeJS.ErrnoException | null, stats: Stats) => void): void;
|
1048 | export function lstat(
|
1049 | path: PathLike,
|
1050 | options:
|
1051 | | (StatOptions & {
|
1052 | bigint?: false | undefined;
|
1053 | })
|
1054 | | undefined,
|
1055 | callback: (err: NodeJS.ErrnoException | null, stats: Stats) => void
|
1056 | ): void;
|
1057 | export function lstat(
|
1058 | path: PathLike,
|
1059 | options: StatOptions & {
|
1060 | bigint: true;
|
1061 | },
|
1062 | callback: (err: NodeJS.ErrnoException | null, stats: BigIntStats) => void
|
1063 | ): void;
|
1064 | export function lstat(path: PathLike, options: StatOptions | undefined, callback: (err: NodeJS.ErrnoException | null, stats: Stats | BigIntStats) => void): void;
|
1065 | export namespace lstat {
|
1066 | /**
|
1067 | * Asynchronous lstat(2) - Get file status. Does not dereference symbolic links.
|
1068 | * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
|
1069 | */
|
1070 | function __promisify__(
|
1071 | path: PathLike,
|
1072 | options?: StatOptions & {
|
1073 | bigint?: false | undefined;
|
1074 | }
|
1075 | ): Promise<Stats>;
|
1076 | function __promisify__(
|
1077 | path: PathLike,
|
1078 | options: StatOptions & {
|
1079 | bigint: true;
|
1080 | }
|
1081 | ): Promise<BigIntStats>;
|
1082 | function __promisify__(path: PathLike, options?: StatOptions): Promise<Stats | BigIntStats>;
|
1083 | }
|
1084 | /**
|
1085 | * Synchronous lstat(2) - Get file status. Does not dereference symbolic links.
|
1086 | * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
|
1087 | */
|
1088 | export const lstatSync: StatSyncFn;
|
1089 | /**
|
1090 | * 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. No arguments other than
|
1091 | * a possible
|
1092 | * exception are given to the completion callback.
|
1093 | * @since v0.1.31
|
1094 | */
|
1095 | export function link(existingPath: PathLike, newPath: PathLike, callback: NoParamCallback): void;
|
1096 | export namespace link {
|
1097 | /**
|
1098 | * Asynchronous link(2) - Create a new link (also known as a hard link) to an existing file.
|
1099 | * @param existingPath A path to a file. If a URL is provided, it must use the `file:` protocol.
|
1100 | * @param newPath A path to a file. If a URL is provided, it must use the `file:` protocol.
|
1101 | */
|
1102 | function __promisify__(existingPath: PathLike, newPath: PathLike): Promise<void>;
|
1103 | }
|
1104 | /**
|
1105 | * 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. Returns `undefined`.
|
1106 | * @since v0.1.31
|
1107 | */
|
1108 | export function linkSync(existingPath: PathLike, newPath: PathLike): void;
|
1109 | /**
|
1110 | * Creates the link called `path` pointing to `target`. No arguments other than a
|
1111 | * possible exception are given to the completion callback.
|
1112 | *
|
1113 | * See the POSIX [`symlink(2)`](http://man7.org/linux/man-pages/man2/symlink.2.html) documentation for more details.
|
1114 | *
|
1115 | * The `type` argument is only available on Windows and ignored on other platforms.
|
1116 | * It can be set to `'dir'`, `'file'`, or `'junction'`. If the `type` argument is
|
1117 | * not set, Node.js will autodetect `target` type and use `'file'` or `'dir'`. If
|
1118 | * the `target` does not exist, `'file'` will be used. Windows junction points
|
1119 | * require the destination path to be absolute. When using `'junction'`, the`target` argument will automatically be normalized to absolute path.
|
1120 | *
|
1121 | * Relative targets are relative to the link’s parent directory.
|
1122 | *
|
1123 | * ```js
|
1124 | * import { symlink } from 'fs';
|
1125 | *
|
1126 | * symlink('./mew', './example/mewtwo', callback);
|
1127 | * ```
|
1128 | *
|
1129 | * The above example creates a symbolic link `mewtwo` in the `example` which points
|
1130 | * to `mew` in the same directory:
|
1131 | *
|
1132 | * ```bash
|
1133 | * $ tree example/
|
1134 | * example/
|
1135 | * ├── mew
|
1136 | * └── mewtwo -> ./mew
|
1137 | * ```
|
1138 | * @since v0.1.31
|
1139 | */
|
1140 | export function symlink(target: PathLike, path: PathLike, type: symlink.Type | undefined | null, callback: NoParamCallback): void;
|
1141 | /**
|
1142 | * Asynchronous symlink(2) - Create a new symbolic link to an existing file.
|
1143 | * @param target A path to an existing file. If a URL is provided, it must use the `file:` protocol.
|
1144 | * @param path A path to the new symlink. If a URL is provided, it must use the `file:` protocol.
|
1145 | */
|
1146 | export function symlink(target: PathLike, path: PathLike, callback: NoParamCallback): void;
|
1147 | export namespace symlink {
|
1148 | /**
|
1149 | * Asynchronous symlink(2) - Create a new symbolic link to an existing file.
|
1150 | * @param target A path to an existing file. If a URL is provided, it must use the `file:` protocol.
|
1151 | * @param path A path to the new symlink. If a URL is provided, it must use the `file:` protocol.
|
1152 | * @param type May be set to `'dir'`, `'file'`, or `'junction'` (default is `'file'`) and is only available on Windows (ignored on other platforms).
|
1153 | * When using `'junction'`, the `target` argument will automatically be normalized to an absolute path.
|
1154 | */
|
1155 | function __promisify__(target: PathLike, path: PathLike, type?: string | null): Promise<void>;
|
1156 | type Type = 'dir' | 'file' | 'junction';
|
1157 | }
|
1158 | /**
|
1159 | * Returns `undefined`.
|
1160 | *
|
1161 | * For detailed information, see the documentation of the asynchronous version of
|
1162 | * this API: {@link symlink}.
|
1163 | * @since v0.1.31
|
1164 | */
|
1165 | export function symlinkSync(target: PathLike, path: PathLike, type?: symlink.Type | null): void;
|
1166 | /**
|
1167 | * Reads the contents of the symbolic link referred to by `path`. The callback gets
|
1168 | * two arguments `(err, linkString)`.
|
1169 | *
|
1170 | * See the POSIX [`readlink(2)`](http://man7.org/linux/man-pages/man2/readlink.2.html) documentation for more details.
|
1171 | *
|
1172 | * The optional `options` argument can be a string specifying an encoding, or an
|
1173 | * object with an `encoding` property specifying the character encoding to use for
|
1174 | * the link path passed to the callback. If the `encoding` is set to `'buffer'`,
|
1175 | * the link path returned will be passed as a `Buffer` object.
|
1176 | * @since v0.1.31
|
1177 | */
|
1178 | export function readlink(path: PathLike, options: EncodingOption, callback: (err: NodeJS.ErrnoException | null, linkString: string) => void): void;
|
1179 | /**
|
1180 | * Asynchronous readlink(2) - read value of a symbolic link.
|
1181 | * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
|
1182 | * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used.
|
1183 | */
|
1184 | export function readlink(path: PathLike, options: BufferEncodingOption, callback: (err: NodeJS.ErrnoException | null, linkString: Buffer) => void): void;
|
1185 | /**
|
1186 | * Asynchronous readlink(2) - read value of a symbolic link.
|
1187 | * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
|
1188 | * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used.
|
1189 | */
|
1190 | export function readlink(path: PathLike, options: EncodingOption, callback: (err: NodeJS.ErrnoException | null, linkString: string | Buffer) => void): void;
|
1191 | /**
|
1192 | * Asynchronous readlink(2) - read value of a symbolic link.
|
1193 | * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
|
1194 | */
|
1195 | export function readlink(path: PathLike, callback: (err: NodeJS.ErrnoException | null, linkString: string) => void): void;
|
1196 | export namespace readlink {
|
1197 | /**
|
1198 | * Asynchronous readlink(2) - read value of a symbolic link.
|
1199 | * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
|
1200 | * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used.
|
1201 | */
|
1202 | function __promisify__(path: PathLike, options?: EncodingOption): Promise<string>;
|
1203 | /**
|
1204 | * Asynchronous readlink(2) - read value of a symbolic link.
|
1205 | * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
|
1206 | * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used.
|
1207 | */
|
1208 | function __promisify__(path: PathLike, options: BufferEncodingOption): Promise<Buffer>;
|
1209 | /**
|
1210 | * Asynchronous readlink(2) - read value of a symbolic link.
|
1211 | * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
|
1212 | * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used.
|
1213 | */
|
1214 | function __promisify__(path: PathLike, options?: EncodingOption): Promise<string | Buffer>;
|
1215 | }
|
1216 | /**
|
1217 | * Returns the symbolic link's string value.
|
1218 | *
|
1219 | * See the POSIX [`readlink(2)`](http://man7.org/linux/man-pages/man2/readlink.2.html) documentation for more details.
|
1220 | *
|
1221 | * The optional `options` argument can be a string specifying an encoding, or an
|
1222 | * object with an `encoding` property specifying the character encoding to use for
|
1223 | * the link path returned. If the `encoding` is set to `'buffer'`,
|
1224 | * the link path returned will be passed as a `Buffer` object.
|
1225 | * @since v0.1.31
|
1226 | */
|
1227 | export function readlinkSync(path: PathLike, options?: EncodingOption): string;
|
1228 | /**
|
1229 | * Synchronous readlink(2) - read value of a symbolic link.
|
1230 | * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
|
1231 | * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used.
|
1232 | */
|
1233 | export function readlinkSync(path: PathLike, options: BufferEncodingOption): Buffer;
|
1234 | /**
|
1235 | * Synchronous readlink(2) - read value of a symbolic link.
|
1236 | * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
|
1237 | * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used.
|
1238 | */
|
1239 | export function readlinkSync(path: PathLike, options?: EncodingOption): string | Buffer;
|
1240 | /**
|
1241 | * Asynchronously computes the canonical pathname by resolving `.`, `..` and
|
1242 | * symbolic links.
|
1243 | *
|
1244 | * A canonical pathname is not necessarily unique. Hard links and bind mounts can
|
1245 | * expose a file system entity through many pathnames.
|
1246 | *
|
1247 | * This function behaves like [`realpath(3)`](http://man7.org/linux/man-pages/man3/realpath.3.html), with some exceptions:
|
1248 | *
|
1249 | * 1. No case conversion is performed on case-insensitive file systems.
|
1250 | * 2. The maximum number of symbolic links is platform-independent and generally
|
1251 | * (much) higher than what the native [`realpath(3)`](http://man7.org/linux/man-pages/man3/realpath.3.html) implementation supports.
|
1252 | *
|
1253 | * The `callback` gets two arguments `(err, resolvedPath)`. May use `process.cwd`to resolve relative paths.
|
1254 | *
|
1255 | * Only paths that can be converted to UTF8 strings are supported.
|
1256 | *
|
1257 | * The optional `options` argument can be a string specifying an encoding, or an
|
1258 | * object with an `encoding` property specifying the character encoding to use for
|
1259 | * the path passed to the callback. If the `encoding` is set to `'buffer'`,
|
1260 | * the path returned will be passed as a `Buffer` object.
|
1261 | *
|
1262 | * If `path` resolves to a socket or a pipe, the function will return a system
|
1263 | * dependent name for that object.
|
1264 | * @since v0.1.31
|
1265 | */
|
1266 | export function realpath(path: PathLike, options: EncodingOption, callback: (err: NodeJS.ErrnoException | null, resolvedPath: string) => void): void;
|
1267 | /**
|
1268 | * Asynchronous realpath(3) - return the canonicalized absolute pathname.
|
1269 | * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
|
1270 | * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used.
|
1271 | */
|
1272 | export function realpath(path: PathLike, options: BufferEncodingOption, callback: (err: NodeJS.ErrnoException | null, resolvedPath: Buffer) => void): void;
|
1273 | /**
|
1274 | * Asynchronous realpath(3) - return the canonicalized absolute pathname.
|
1275 | * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
|
1276 | * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used.
|
1277 | */
|
1278 | export function realpath(path: PathLike, options: EncodingOption, callback: (err: NodeJS.ErrnoException | null, resolvedPath: string | Buffer) => void): void;
|
1279 | /**
|
1280 | * Asynchronous realpath(3) - return the canonicalized absolute pathname.
|
1281 | * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
|
1282 | */
|
1283 | export function realpath(path: PathLike, callback: (err: NodeJS.ErrnoException | null, resolvedPath: string) => void): void;
|
1284 | export namespace realpath {
|
1285 | /**
|
1286 | * Asynchronous realpath(3) - return the canonicalized absolute pathname.
|
1287 | * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
|
1288 | * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used.
|
1289 | */
|
1290 | function __promisify__(path: PathLike, options?: EncodingOption): Promise<string>;
|
1291 | /**
|
1292 | * Asynchronous realpath(3) - return the canonicalized absolute pathname.
|
1293 | * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
|
1294 | * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used.
|
1295 | */
|
1296 | function __promisify__(path: PathLike, options: BufferEncodingOption): Promise<Buffer>;
|
1297 | /**
|
1298 | * Asynchronous realpath(3) - return the canonicalized absolute pathname.
|
1299 | * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
|
1300 | * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used.
|
1301 | */
|
1302 | function __promisify__(path: PathLike, options?: EncodingOption): Promise<string | Buffer>;
|
1303 | /**
|
1304 | * Asynchronous [`realpath(3)`](http://man7.org/linux/man-pages/man3/realpath.3.html).
|
1305 | *
|
1306 | * The `callback` gets two arguments `(err, resolvedPath)`.
|
1307 | *
|
1308 | * Only paths that can be converted to UTF8 strings are supported.
|
1309 | *
|
1310 | * The optional `options` argument can be a string specifying an encoding, or an
|
1311 | * object with an `encoding` property specifying the character encoding to use for
|
1312 | * the path passed to the callback. If the `encoding` is set to `'buffer'`,
|
1313 | * the path returned will be passed as a `Buffer` object.
|
1314 | *
|
1315 | * On Linux, when Node.js is linked against musl libc, the procfs file system must
|
1316 | * be mounted on `/proc` in order for this function to work. Glibc does not have
|
1317 | * this restriction.
|
1318 | * @since v9.2.0
|
1319 | */
|
1320 | function native(path: PathLike, options: EncodingOption, callback: (err: NodeJS.ErrnoException | null, resolvedPath: string) => void): void;
|
1321 | function native(path: PathLike, options: BufferEncodingOption, callback: (err: NodeJS.ErrnoException | null, resolvedPath: Buffer) => void): void;
|
1322 | function native(path: PathLike, options: EncodingOption, callback: (err: NodeJS.ErrnoException | null, resolvedPath: string | Buffer) => void): void;
|
1323 | function native(path: PathLike, callback: (err: NodeJS.ErrnoException | null, resolvedPath: string) => void): void;
|
1324 | }
|
1325 | /**
|
1326 | * Returns the resolved pathname.
|
1327 | *
|
1328 | * For detailed information, see the documentation of the asynchronous version of
|
1329 | * this API: {@link realpath}.
|
1330 | * @since v0.1.31
|
1331 | */
|
1332 | export function realpathSync(path: PathLike, options?: EncodingOption): string;
|
1333 | /**
|
1334 | * Synchronous realpath(3) - return the canonicalized absolute pathname.
|
1335 | * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
|
1336 | * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used.
|
1337 | */
|
1338 | export function realpathSync(path: PathLike, options: BufferEncodingOption): Buffer;
|
1339 | /**
|
1340 | * Synchronous realpath(3) - return the canonicalized absolute pathname.
|
1341 | * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
|
1342 | * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used.
|
1343 | */
|
1344 | export function realpathSync(path: PathLike, options?: EncodingOption): string | Buffer;
|
1345 | export namespace realpathSync {
|
1346 | function native(path: PathLike, options?: EncodingOption): string;
|
1347 | function native(path: PathLike, options: BufferEncodingOption): Buffer;
|
1348 | function native(path: PathLike, options?: EncodingOption): string | Buffer;
|
1349 | }
|
1350 | /**
|
1351 | * Asynchronously removes a file or symbolic link. No arguments other than a
|
1352 | * possible exception are given to the completion callback.
|
1353 | *
|
1354 | * ```js
|
1355 | * import { unlink } from 'fs';
|
1356 | * // Assuming that 'path/file.txt' is a regular file.
|
1357 | * unlink('path/file.txt', (err) => {
|
1358 | * if (err) throw err;
|
1359 | * console.log('path/file.txt was deleted');
|
1360 | * });
|
1361 | * ```
|
1362 | *
|
1363 | * `fs.unlink()` will not work on a directory, empty or otherwise. To remove a
|
1364 | * directory, use {@link rmdir}.
|
1365 | *
|
1366 | * See the POSIX [`unlink(2)`](http://man7.org/linux/man-pages/man2/unlink.2.html) documentation for more details.
|
1367 | * @since v0.0.2
|
1368 | */
|
1369 | export function unlink(path: PathLike, callback: NoParamCallback): void;
|
1370 | export namespace unlink {
|
1371 | /**
|
1372 | * Asynchronous unlink(2) - delete a name and possibly the file it refers to.
|
1373 | * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
|
1374 | */
|
1375 | function __promisify__(path: PathLike): Promise<void>;
|
1376 | }
|
1377 | /**
|
1378 | * Synchronous [`unlink(2)`](http://man7.org/linux/man-pages/man2/unlink.2.html). Returns `undefined`.
|
1379 | * @since v0.1.21
|
1380 | */
|
1381 | export function unlinkSync(path: PathLike): void;
|
1382 | export interface RmDirOptions {
|
1383 | /**
|
1384 | * If an `EBUSY`, `EMFILE`, `ENFILE`, `ENOTEMPTY`, or
|
1385 | * `EPERM` error is encountered, Node.js will retry the operation with a linear
|
1386 | * backoff wait of `retryDelay` ms longer on each try. This option represents the
|
1387 | * number of retries. This option is ignored if the `recursive` option is not
|
1388 | * `true`.
|
1389 | * @default 0
|
1390 | */
|
1391 | maxRetries?: number | undefined;
|
1392 | /**
|
1393 | * @deprecated since v14.14.0 In future versions of Node.js and will trigger a warning
|
1394 | * `fs.rmdir(path, { recursive: true })` will throw if `path` does not exist or is a file.
|
1395 | * Use `fs.rm(path, { recursive: true, force: true })` instead.
|
1396 | *
|
1397 | * If `true`, perform a recursive directory removal. In
|
1398 | * recursive mode soperations are retried on failure.
|
1399 | * @default false
|
1400 | */
|
1401 | recursive?: boolean | undefined;
|
1402 | /**
|
1403 | * The amount of time in milliseconds to wait between retries.
|
1404 | * This option is ignored if the `recursive` option is not `true`.
|
1405 | * @default 100
|
1406 | */
|
1407 | retryDelay?: number | undefined;
|
1408 | }
|
1409 | /**
|
1410 | * Asynchronous [`rmdir(2)`](http://man7.org/linux/man-pages/man2/rmdir.2.html). No arguments other than a possible exception are given
|
1411 | * to the completion callback.
|
1412 | *
|
1413 | * Using `fs.rmdir()` on a file (not a directory) results in an `ENOENT` error on
|
1414 | * Windows and an `ENOTDIR` error on POSIX.
|
1415 | *
|
1416 | * To get a behavior similar to the `rm -rf` Unix command, use {@link rm} with options `{ recursive: true, force: true }`.
|
1417 | * @since v0.0.2
|
1418 | */
|
1419 | export function rmdir(path: PathLike, callback: NoParamCallback): void;
|
1420 | export function rmdir(path: PathLike, options: RmDirOptions, callback: NoParamCallback): void;
|
1421 | export namespace rmdir {
|
1422 | /**
|
1423 | * Asynchronous rmdir(2) - delete a directory.
|
1424 | * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
|
1425 | */
|
1426 | function __promisify__(path: PathLike, options?: RmDirOptions): Promise<void>;
|
1427 | }
|
1428 | /**
|
1429 | * Synchronous [`rmdir(2)`](http://man7.org/linux/man-pages/man2/rmdir.2.html). Returns `undefined`.
|
1430 | *
|
1431 | * Using `fs.rmdirSync()` on a file (not a directory) results in an `ENOENT` error
|
1432 | * on Windows and an `ENOTDIR` error on POSIX.
|
1433 | *
|
1434 | * To get a behavior similar to the `rm -rf` Unix command, use {@link rmSync} with options `{ recursive: true, force: true }`.
|
1435 | * @since v0.1.21
|
1436 | */
|
1437 | export function rmdirSync(path: PathLike, options?: RmDirOptions): void;
|
1438 | export interface RmOptions {
|
1439 | /**
|
1440 | * When `true`, exceptions will be ignored if `path` does not exist.
|
1441 | * @default false
|
1442 | */
|
1443 | force?: boolean | undefined;
|
1444 | /**
|
1445 | * If an `EBUSY`, `EMFILE`, `ENFILE`, `ENOTEMPTY`, or
|
1446 | * `EPERM` error is encountered, Node.js will retry the operation with a linear
|
1447 | * backoff wait of `retryDelay` ms longer on each try. This option represents the
|
1448 | * number of retries. This option is ignored if the `recursive` option is not
|
1449 | * `true`.
|
1450 | * @default 0
|
1451 | */
|
1452 | maxRetries?: number | undefined;
|
1453 | /**
|
1454 | * If `true`, perform a recursive directory removal. In
|
1455 | * recursive mode, operations are retried on failure.
|
1456 | * @default false
|
1457 | */
|
1458 | recursive?: boolean | undefined;
|
1459 | /**
|
1460 | * The amount of time in milliseconds to wait between retries.
|
1461 | * This option is ignored if the `recursive` option is not `true`.
|
1462 | * @default 100
|
1463 | */
|
1464 | retryDelay?: number | undefined;
|
1465 | }
|
1466 | /**
|
1467 | * Asynchronously removes files and directories (modeled on the standard POSIX `rm`utility). No arguments other than a possible exception are given to the
|
1468 | * completion callback.
|
1469 | * @since v14.14.0
|
1470 | */
|
1471 | export function rm(path: PathLike, callback: NoParamCallback): void;
|
1472 | export function rm(path: PathLike, options: RmOptions, callback: NoParamCallback): void;
|
1473 | export namespace rm {
|
1474 | /**
|
1475 | * Asynchronously removes files and directories (modeled on the standard POSIX `rm` utility).
|
1476 | */
|
1477 | function __promisify__(path: PathLike, options?: RmOptions): Promise<void>;
|
1478 | }
|
1479 | /**
|
1480 | * Synchronously removes files and directories (modeled on the standard POSIX `rm`utility). Returns `undefined`.
|
1481 | * @since v14.14.0
|
1482 | */
|
1483 | export function rmSync(path: PathLike, options?: RmOptions): void;
|
1484 | export interface MakeDirectoryOptions {
|
1485 | /**
|
1486 | * Indicates whether parent folders should be created.
|
1487 | * If a folder was created, the path to the first created folder will be returned.
|
1488 | * @default false
|
1489 | */
|
1490 | recursive?: boolean | undefined;
|
1491 | /**
|
1492 | * A file mode. If a string is passed, it is parsed as an octal integer. If not specified
|
1493 | * @default 0o777
|
1494 | */
|
1495 | mode?: Mode | undefined;
|
1496 | }
|
1497 | /**
|
1498 | * Asynchronously creates a directory.
|
1499 | *
|
1500 | * The callback is given a possible exception and, if `recursive` is `true`, the
|
1501 | * first directory path created, `(err[, path])`.`path` can still be `undefined` when `recursive` is `true`, if no directory was
|
1502 | * created.
|
1503 | *
|
1504 | * The optional `options` argument can be an integer specifying `mode` (permission
|
1505 | * and sticky bits), or an object with a `mode` property and a `recursive`property indicating whether parent directories should be created. Calling`fs.mkdir()` when `path` is a directory that
|
1506 | * exists results in an error only
|
1507 | * when `recursive` is false.
|
1508 | *
|
1509 | * ```js
|
1510 | * import { mkdir } from 'fs';
|
1511 | *
|
1512 | * // Creates /tmp/a/apple, regardless of whether `/tmp` and /tmp/a exist.
|
1513 | * mkdir('/tmp/a/apple', { recursive: true }, (err) => {
|
1514 | * if (err) throw err;
|
1515 | * });
|
1516 | * ```
|
1517 | *
|
1518 | * On Windows, using `fs.mkdir()` on the root directory even with recursion will
|
1519 | * result in an error:
|
1520 | *
|
1521 | * ```js
|
1522 | * import { mkdir } from 'fs';
|
1523 | *
|
1524 | * mkdir('/', { recursive: true }, (err) => {
|
1525 | * // => [Error: EPERM: operation not permitted, mkdir 'C:\']
|
1526 | * });
|
1527 | * ```
|
1528 | *
|
1529 | * See the POSIX [`mkdir(2)`](http://man7.org/linux/man-pages/man2/mkdir.2.html) documentation for more details.
|
1530 | * @since v0.1.8
|
1531 | */
|
1532 | export function mkdir(
|
1533 | path: PathLike,
|
1534 | options: MakeDirectoryOptions & {
|
1535 | recursive: true;
|
1536 | },
|
1537 | callback: (err: NodeJS.ErrnoException | null, path?: string) => void
|
1538 | ): void;
|
1539 | /**
|
1540 | * Asynchronous mkdir(2) - create a directory.
|
1541 | * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
|
1542 | * @param options Either the file mode, or an object optionally specifying the file mode and whether parent folders
|
1543 | * should be created. If a string is passed, it is parsed as an octal integer. If not specified, defaults to `0o777`.
|
1544 | */
|
1545 | export function mkdir(
|
1546 | path: PathLike,
|
1547 | options:
|
1548 | | Mode
|
1549 | | (MakeDirectoryOptions & {
|
1550 | recursive?: false | undefined;
|
1551 | })
|
1552 | | null
|
1553 | | undefined,
|
1554 | callback: NoParamCallback
|
1555 | ): void;
|
1556 | /**
|
1557 | * Asynchronous mkdir(2) - create a directory.
|
1558 | * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
|
1559 | * @param options Either the file mode, or an object optionally specifying the file mode and whether parent folders
|
1560 | * should be created. If a string is passed, it is parsed as an octal integer. If not specified, defaults to `0o777`.
|
1561 | */
|
1562 | export function mkdir(path: PathLike, options: Mode | MakeDirectoryOptions | null | undefined, callback: (err: NodeJS.ErrnoException | null, path?: string) => void): void;
|
1563 | /**
|
1564 | * Asynchronous mkdir(2) - create a directory with a mode of `0o777`.
|
1565 | * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
|
1566 | */
|
1567 | export function mkdir(path: PathLike, callback: NoParamCallback): void;
|
1568 | export namespace mkdir {
|
1569 | /**
|
1570 | * Asynchronous mkdir(2) - create a directory.
|
1571 | * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
|
1572 | * @param options Either the file mode, or an object optionally specifying the file mode and whether parent folders
|
1573 | * should be created. If a string is passed, it is parsed as an octal integer. If not specified, defaults to `0o777`.
|
1574 | */
|
1575 | function __promisify__(
|
1576 | path: PathLike,
|
1577 | options: MakeDirectoryOptions & {
|
1578 | recursive: true;
|
1579 | }
|
1580 | ): Promise<string | undefined>;
|
1581 | /**
|
1582 | * Asynchronous mkdir(2) - create a directory.
|
1583 | * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
|
1584 | * @param options Either the file mode, or an object optionally specifying the file mode and whether parent folders
|
1585 | * should be created. If a string is passed, it is parsed as an octal integer. If not specified, defaults to `0o777`.
|
1586 | */
|
1587 | function __promisify__(
|
1588 | path: PathLike,
|
1589 | options?:
|
1590 | | Mode
|
1591 | | (MakeDirectoryOptions & {
|
1592 | recursive?: false | undefined;
|
1593 | })
|
1594 | | null
|
1595 | ): Promise<void>;
|
1596 | /**
|
1597 | * Asynchronous mkdir(2) - create a directory.
|
1598 | * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
|
1599 | * @param options Either the file mode, or an object optionally specifying the file mode and whether parent folders
|
1600 | * should be created. If a string is passed, it is parsed as an octal integer. If not specified, defaults to `0o777`.
|
1601 | */
|
1602 | function __promisify__(path: PathLike, options?: Mode | MakeDirectoryOptions | null): Promise<string | undefined>;
|
1603 | }
|
1604 | /**
|
1605 | * Synchronously creates a directory. Returns `undefined`, or if `recursive` is`true`, the first directory path created.
|
1606 | * This is the synchronous version of {@link mkdir}.
|
1607 | *
|
1608 | * See the POSIX [`mkdir(2)`](http://man7.org/linux/man-pages/man2/mkdir.2.html) documentation for more details.
|
1609 | * @since v0.1.21
|
1610 | */
|
1611 | export function mkdirSync(
|
1612 | path: PathLike,
|
1613 | options: MakeDirectoryOptions & {
|
1614 | recursive: true;
|
1615 | }
|
1616 | ): string | undefined;
|
1617 | /**
|
1618 | * Synchronous mkdir(2) - create a directory.
|
1619 | * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
|
1620 | * @param options Either the file mode, or an object optionally specifying the file mode and whether parent folders
|
1621 | * should be created. If a string is passed, it is parsed as an octal integer. If not specified, defaults to `0o777`.
|
1622 | */
|
1623 | export function mkdirSync(
|
1624 | path: PathLike,
|
1625 | options?:
|
1626 | | Mode
|
1627 | | (MakeDirectoryOptions & {
|
1628 | recursive?: false | undefined;
|
1629 | })
|
1630 | | null
|
1631 | ): void;
|
1632 | /**
|
1633 | * Synchronous mkdir(2) - create a directory.
|
1634 | * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
|
1635 | * @param options Either the file mode, or an object optionally specifying the file mode and whether parent folders
|
1636 | * should be created. If a string is passed, it is parsed as an octal integer. If not specified, defaults to `0o777`.
|
1637 | */
|
1638 | export function mkdirSync(path: PathLike, options?: Mode | MakeDirectoryOptions | null): string | undefined;
|
1639 | /**
|
1640 | * Creates a unique temporary directory.
|
1641 | *
|
1642 | * Generates six random characters to be appended behind a required`prefix` to create a unique temporary directory. Due to platform
|
1643 | * inconsistencies, avoid trailing `X` characters in `prefix`. Some platforms,
|
1644 | * notably the BSDs, can return more than six random characters, and replace
|
1645 | * trailing `X` characters in `prefix` with random characters.
|
1646 | *
|
1647 | * The created directory path is passed as a string to the callback's second
|
1648 | * parameter.
|
1649 | *
|
1650 | * The optional `options` argument can be a string specifying an encoding, or an
|
1651 | * object with an `encoding` property specifying the character encoding to use.
|
1652 | *
|
1653 | * ```js
|
1654 | * import { mkdtemp } from 'fs';
|
1655 | *
|
1656 | * mkdtemp(path.join(os.tmpdir(), 'foo-'), (err, directory) => {
|
1657 | * if (err) throw err;
|
1658 | * console.log(directory);
|
1659 | * // Prints: /tmp/foo-itXde2 or C:\Users\...\AppData\Local\Temp\foo-itXde2
|
1660 | * });
|
1661 | * ```
|
1662 | *
|
1663 | * The `fs.mkdtemp()` method will append the six randomly selected characters
|
1664 | * directly to the `prefix` string. For instance, given a directory `/tmp`, if the
|
1665 | * intention is to create a temporary directory _within_`/tmp`, the `prefix`must end with a trailing platform-specific path separator
|
1666 | * (`require('path').sep`).
|
1667 | *
|
1668 | * ```js
|
1669 | * import { tmpdir } from 'os';
|
1670 | * import { mkdtemp } from 'fs';
|
1671 | *
|
1672 | * // The parent directory for the new temporary directory
|
1673 | * const tmpDir = tmpdir();
|
1674 | *
|
1675 | * // This method is *INCORRECT*:
|
1676 | * mkdtemp(tmpDir, (err, directory) => {
|
1677 | * if (err) throw err;
|
1678 | * console.log(directory);
|
1679 | * // Will print something similar to `/tmpabc123`.
|
1680 | * // A new temporary directory is created at the file system root
|
1681 | * // rather than *within* the /tmp directory.
|
1682 | * });
|
1683 | *
|
1684 | * // This method is *CORRECT*:
|
1685 | * import { sep } from 'path';
|
1686 | * mkdtemp(`${tmpDir}${sep}`, (err, directory) => {
|
1687 | * if (err) throw err;
|
1688 | * console.log(directory);
|
1689 | * // Will print something similar to `/tmp/abc123`.
|
1690 | * // A new temporary directory is created within
|
1691 | * // the /tmp directory.
|
1692 | * });
|
1693 | * ```
|
1694 | * @since v5.10.0
|
1695 | */
|
1696 | export function mkdtemp(prefix: string, options: EncodingOption, callback: (err: NodeJS.ErrnoException | null, folder: string) => void): void;
|
1697 | /**
|
1698 | * Asynchronously creates a unique temporary directory.
|
1699 | * Generates six random characters to be appended behind a required prefix to create a unique temporary directory.
|
1700 | * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used.
|
1701 | */
|
1702 | export function mkdtemp(
|
1703 | prefix: string,
|
1704 | options:
|
1705 | | 'buffer'
|
1706 | | {
|
1707 | encoding: 'buffer';
|
1708 | },
|
1709 | callback: (err: NodeJS.ErrnoException | null, folder: Buffer) => void
|
1710 | ): void;
|
1711 | /**
|
1712 | * Asynchronously creates a unique temporary directory.
|
1713 | * Generates six random characters to be appended behind a required prefix to create a unique temporary directory.
|
1714 | * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used.
|
1715 | */
|
1716 | export function mkdtemp(prefix: string, options: EncodingOption, callback: (err: NodeJS.ErrnoException | null, folder: string | Buffer) => void): void;
|
1717 | /**
|
1718 | * Asynchronously creates a unique temporary directory.
|
1719 | * Generates six random characters to be appended behind a required prefix to create a unique temporary directory.
|
1720 | */
|
1721 | export function mkdtemp(prefix: string, callback: (err: NodeJS.ErrnoException | null, folder: string) => void): void;
|
1722 | export namespace mkdtemp {
|
1723 | /**
|
1724 | * Asynchronously creates a unique temporary directory.
|
1725 | * Generates six random characters to be appended behind a required prefix to create a unique temporary directory.
|
1726 | * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used.
|
1727 | */
|
1728 | function __promisify__(prefix: string, options?: EncodingOption): Promise<string>;
|
1729 | /**
|
1730 | * Asynchronously creates a unique temporary directory.
|
1731 | * Generates six random characters to be appended behind a required prefix to create a unique temporary directory.
|
1732 | * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used.
|
1733 | */
|
1734 | function __promisify__(prefix: string, options: BufferEncodingOption): Promise<Buffer>;
|
1735 | /**
|
1736 | * Asynchronously creates a unique temporary directory.
|
1737 | * Generates six random characters to be appended behind a required prefix to create a unique temporary directory.
|
1738 | * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used.
|
1739 | */
|
1740 | function __promisify__(prefix: string, options?: EncodingOption): Promise<string | Buffer>;
|
1741 | }
|
1742 | /**
|
1743 | * Returns the created directory path.
|
1744 | *
|
1745 | * For detailed information, see the documentation of the asynchronous version of
|
1746 | * this API: {@link mkdtemp}.
|
1747 | *
|
1748 | * The optional `options` argument can be a string specifying an encoding, or an
|
1749 | * object with an `encoding` property specifying the character encoding to use.
|
1750 | * @since v5.10.0
|
1751 | */
|
1752 | export function mkdtempSync(prefix: string, options?: EncodingOption): string;
|
1753 | /**
|
1754 | * Synchronously creates a unique temporary directory.
|
1755 | * Generates six random characters to be appended behind a required prefix to create a unique temporary directory.
|
1756 | * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used.
|
1757 | */
|
1758 | export function mkdtempSync(prefix: string, options: BufferEncodingOption): Buffer;
|
1759 | /**
|
1760 | * Synchronously creates a unique temporary directory.
|
1761 | * Generates six random characters to be appended behind a required prefix to create a unique temporary directory.
|
1762 | * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used.
|
1763 | */
|
1764 | export function mkdtempSync(prefix: string, options?: EncodingOption): string | Buffer;
|
1765 | /**
|
1766 | * Reads the contents of a directory. The callback gets two arguments `(err, files)`where `files` is an array of the names of the files in the directory excluding`'.'` and `'..'`.
|
1767 | *
|
1768 | * See the POSIX [`readdir(3)`](http://man7.org/linux/man-pages/man3/readdir.3.html) documentation for more details.
|
1769 | *
|
1770 | * The optional `options` argument can be a string specifying an encoding, or an
|
1771 | * object with an `encoding` property specifying the character encoding to use for
|
1772 | * the filenames passed to the callback. If the `encoding` is set to `'buffer'`,
|
1773 | * the filenames returned will be passed as `Buffer` objects.
|
1774 | *
|
1775 | * If `options.withFileTypes` is set to `true`, the `files` array will contain `fs.Dirent` objects.
|
1776 | * @since v0.1.8
|
1777 | */
|
1778 | export function readdir(
|
1779 | path: PathLike,
|
1780 | options:
|
1781 | | {
|
1782 | encoding: BufferEncoding | null;
|
1783 | withFileTypes?: false | undefined;
|
1784 | }
|
1785 | | BufferEncoding
|
1786 | | undefined
|
1787 | | null,
|
1788 | callback: (err: NodeJS.ErrnoException | null, files: string[]) => void
|
1789 | ): void;
|
1790 | /**
|
1791 | * Asynchronous readdir(3) - read a directory.
|
1792 | * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
|
1793 | * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used.
|
1794 | */
|
1795 | export function readdir(
|
1796 | path: PathLike,
|
1797 | options:
|
1798 | | {
|
1799 | encoding: 'buffer';
|
1800 | withFileTypes?: false | undefined;
|
1801 | }
|
1802 | | 'buffer',
|
1803 | callback: (err: NodeJS.ErrnoException | null, files: Buffer[]) => void
|
1804 | ): void;
|
1805 | /**
|
1806 | * Asynchronous readdir(3) - read a directory.
|
1807 | * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
|
1808 | * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used.
|
1809 | */
|
1810 | export function readdir(
|
1811 | path: PathLike,
|
1812 | options:
|
1813 | | (ObjectEncodingOptions & {
|
1814 | withFileTypes?: false | undefined;
|
1815 | })
|
1816 | | BufferEncoding
|
1817 | | undefined
|
1818 | | null,
|
1819 | callback: (err: NodeJS.ErrnoException | null, files: string[] | Buffer[]) => void
|
1820 | ): void;
|
1821 | /**
|
1822 | * Asynchronous readdir(3) - read a directory.
|
1823 | * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
|
1824 | */
|
1825 | export function readdir(path: PathLike, callback: (err: NodeJS.ErrnoException | null, files: string[]) => void): void;
|
1826 | /**
|
1827 | * Asynchronous readdir(3) - read a directory.
|
1828 | * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
|
1829 | * @param options If called with `withFileTypes: true` the result data will be an array of Dirent.
|
1830 | */
|
1831 | export function readdir(
|
1832 | path: PathLike,
|
1833 | options: ObjectEncodingOptions & {
|
1834 | withFileTypes: true;
|
1835 | },
|
1836 | callback: (err: NodeJS.ErrnoException | null, files: Dirent[]) => void
|
1837 | ): void;
|
1838 | export namespace readdir {
|
1839 | /**
|
1840 | * Asynchronous readdir(3) - read a directory.
|
1841 | * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
|
1842 | * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used.
|
1843 | */
|
1844 | function __promisify__(
|
1845 | path: PathLike,
|
1846 | options?:
|
1847 | | {
|
1848 | encoding: BufferEncoding | null;
|
1849 | withFileTypes?: false | undefined;
|
1850 | }
|
1851 | | BufferEncoding
|
1852 | | null
|
1853 | ): Promise<string[]>;
|
1854 | /**
|
1855 | * Asynchronous readdir(3) - read a directory.
|
1856 | * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
|
1857 | * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used.
|
1858 | */
|
1859 | function __promisify__(
|
1860 | path: PathLike,
|
1861 | options:
|
1862 | | 'buffer'
|
1863 | | {
|
1864 | encoding: 'buffer';
|
1865 | withFileTypes?: false | undefined;
|
1866 | }
|
1867 | ): Promise<Buffer[]>;
|
1868 | /**
|
1869 | * Asynchronous readdir(3) - read a directory.
|
1870 | * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
|
1871 | * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used.
|
1872 | */
|
1873 | function __promisify__(
|
1874 | path: PathLike,
|
1875 | options?:
|
1876 | | (ObjectEncodingOptions & {
|
1877 | withFileTypes?: false | undefined;
|
1878 | })
|
1879 | | BufferEncoding
|
1880 | | null
|
1881 | ): Promise<string[] | Buffer[]>;
|
1882 | /**
|
1883 | * Asynchronous readdir(3) - read a directory.
|
1884 | * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
|
1885 | * @param options If called with `withFileTypes: true` the result data will be an array of Dirent
|
1886 | */
|
1887 | function __promisify__(
|
1888 | path: PathLike,
|
1889 | options: ObjectEncodingOptions & {
|
1890 | withFileTypes: true;
|
1891 | }
|
1892 | ): Promise<Dirent[]>;
|
1893 | }
|
1894 | /**
|
1895 | * Reads the contents of the directory.
|
1896 | *
|
1897 | * See the POSIX [`readdir(3)`](http://man7.org/linux/man-pages/man3/readdir.3.html) documentation for more details.
|
1898 | *
|
1899 | * The optional `options` argument can be a string specifying an encoding, or an
|
1900 | * object with an `encoding` property specifying the character encoding to use for
|
1901 | * the filenames returned. If the `encoding` is set to `'buffer'`,
|
1902 | * the filenames returned will be passed as `Buffer` objects.
|
1903 | *
|
1904 | * If `options.withFileTypes` is set to `true`, the result will contain `fs.Dirent` objects.
|
1905 | * @since v0.1.21
|
1906 | */
|
1907 | export function readdirSync(
|
1908 | path: PathLike,
|
1909 | options?:
|
1910 | | {
|
1911 | encoding: BufferEncoding | null;
|
1912 | withFileTypes?: false | undefined;
|
1913 | }
|
1914 | | BufferEncoding
|
1915 | | null
|
1916 | ): string[];
|
1917 | /**
|
1918 | * Synchronous readdir(3) - read a directory.
|
1919 | * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
|
1920 | * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used.
|
1921 | */
|
1922 | export function readdirSync(
|
1923 | path: PathLike,
|
1924 | options:
|
1925 | | {
|
1926 | encoding: 'buffer';
|
1927 | withFileTypes?: false | undefined;
|
1928 | }
|
1929 | | 'buffer'
|
1930 | ): Buffer[];
|
1931 | /**
|
1932 | * Synchronous readdir(3) - read a directory.
|
1933 | * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
|
1934 | * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used.
|
1935 | */
|
1936 | export function readdirSync(
|
1937 | path: PathLike,
|
1938 | options?:
|
1939 | | (ObjectEncodingOptions & {
|
1940 | withFileTypes?: false | undefined;
|
1941 | })
|
1942 | | BufferEncoding
|
1943 | | null
|
1944 | ): string[] | Buffer[];
|
1945 | /**
|
1946 | * Synchronous readdir(3) - read a directory.
|
1947 | * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
|
1948 | * @param options If called with `withFileTypes: true` the result data will be an array of Dirent.
|
1949 | */
|
1950 | export function readdirSync(
|
1951 | path: PathLike,
|
1952 | options: ObjectEncodingOptions & {
|
1953 | withFileTypes: true;
|
1954 | }
|
1955 | ): Dirent[];
|
1956 | /**
|
1957 | * Closes the file descriptor. No arguments other than a possible exception are
|
1958 | * given to the completion callback.
|
1959 | *
|
1960 | * Calling `fs.close()` on any file descriptor (`fd`) that is currently in use
|
1961 | * through any other `fs` operation may lead to undefined behavior.
|
1962 | *
|
1963 | * See the POSIX [`close(2)`](http://man7.org/linux/man-pages/man2/close.2.html) documentation for more detail.
|
1964 | * @since v0.0.2
|
1965 | */
|
1966 | export function close(fd: number, callback?: NoParamCallback): void;
|
1967 | export namespace close {
|
1968 | /**
|
1969 | * Asynchronous close(2) - close a file descriptor.
|
1970 | * @param fd A file descriptor.
|
1971 | */
|
1972 | function __promisify__(fd: number): Promise<void>;
|
1973 | }
|
1974 | /**
|
1975 | * Closes the file descriptor. Returns `undefined`.
|
1976 | *
|
1977 | * Calling `fs.closeSync()` on any file descriptor (`fd`) that is currently in use
|
1978 | * through any other `fs` operation may lead to undefined behavior.
|
1979 | *
|
1980 | * See the POSIX [`close(2)`](http://man7.org/linux/man-pages/man2/close.2.html) documentation for more detail.
|
1981 | * @since v0.1.21
|
1982 | */
|
1983 | export function closeSync(fd: number): void;
|
1984 | /**
|
1985 | * Asynchronous file open. See the POSIX [`open(2)`](http://man7.org/linux/man-pages/man2/open.2.html) documentation for more details.
|
1986 | *
|
1987 | * `mode` sets the file mode (permission and sticky bits), but only if the file was
|
1988 | * created. On Windows, only the write permission can be manipulated; see {@link chmod}.
|
1989 | *
|
1990 | * The callback gets two arguments `(err, fd)`.
|
1991 | *
|
1992 | * Some characters (`< > : " / \ | ? *`) are reserved under Windows as documented
|
1993 | * by [Naming Files, Paths, and Namespaces](https://docs.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file). Under NTFS, if the filename contains
|
1994 | * 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).
|
1995 | *
|
1996 | * Functions based on `fs.open()` exhibit this behavior as well:`fs.writeFile()`, `fs.readFile()`, etc.
|
1997 | * @since v0.0.2
|
1998 | * @param [flags='r'] See `support of file system `flags``.
|
1999 | * @param [mode=0o666]
|
2000 | */
|
2001 | export function open(path: PathLike, flags: OpenMode, mode: Mode | undefined | null, callback: (err: NodeJS.ErrnoException | null, fd: number) => void): void;
|
2002 | /**
|
2003 | * Asynchronous open(2) - open and possibly create a file. If the file is created, its mode will be `0o666`.
|
2004 | * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
|
2005 | */
|
2006 | export function open(path: PathLike, flags: OpenMode, callback: (err: NodeJS.ErrnoException | null, fd: number) => void): void;
|
2007 | export namespace open {
|
2008 | /**
|
2009 | * Asynchronous open(2) - open and possibly create a file.
|
2010 | * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
|
2011 | * @param mode A file mode. If a string is passed, it is parsed as an octal integer. If not supplied, defaults to `0o666`.
|
2012 | */
|
2013 | function __promisify__(path: PathLike, flags: OpenMode, mode?: Mode | null): Promise<number>;
|
2014 | }
|
2015 | /**
|
2016 | * Returns an integer representing the file descriptor.
|
2017 | *
|
2018 | * For detailed information, see the documentation of the asynchronous version of
|
2019 | * this API: {@link open}.
|
2020 | * @since v0.1.21
|
2021 | * @param [flags='r']
|
2022 | * @param [mode=0o666]
|
2023 | */
|
2024 | export function openSync(path: PathLike, flags: OpenMode, mode?: Mode | null): number;
|
2025 | /**
|
2026 | * Change the file system timestamps of the object referenced by `path`.
|
2027 | *
|
2028 | * The `atime` and `mtime` arguments follow these rules:
|
2029 | *
|
2030 | * * Values can be either numbers representing Unix epoch time in seconds,`Date`s, or a numeric string like `'123456789.0'`.
|
2031 | * * If the value can not be converted to a number, or is `NaN`, `Infinity` or`-Infinity`, an `Error` will be thrown.
|
2032 | * @since v0.4.2
|
2033 | */
|
2034 | export function utimes(path: PathLike, atime: TimeLike, mtime: TimeLike, callback: NoParamCallback): void;
|
2035 | export namespace utimes {
|
2036 | /**
|
2037 | * Asynchronously change file timestamps of the file referenced by the supplied path.
|
2038 | * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
|
2039 | * @param atime The last access time. If a string is provided, it will be coerced to number.
|
2040 | * @param mtime The last modified time. If a string is provided, it will be coerced to number.
|
2041 | */
|
2042 | function __promisify__(path: PathLike, atime: TimeLike, mtime: TimeLike): Promise<void>;
|
2043 | }
|
2044 | /**
|
2045 | * Returns `undefined`.
|
2046 | *
|
2047 | * For detailed information, see the documentation of the asynchronous version of
|
2048 | * this API: {@link utimes}.
|
2049 | * @since v0.4.2
|
2050 | */
|
2051 | export function utimesSync(path: PathLike, atime: TimeLike, mtime: TimeLike): void;
|
2052 | /**
|
2053 | * Change the file system timestamps of the object referenced by the supplied file
|
2054 | * descriptor. See {@link utimes}.
|
2055 | * @since v0.4.2
|
2056 | */
|
2057 | export function futimes(fd: number, atime: TimeLike, mtime: TimeLike, callback: NoParamCallback): void;
|
2058 | export namespace futimes {
|
2059 | /**
|
2060 | * Asynchronously change file timestamps of the file referenced by the supplied file descriptor.
|
2061 | * @param fd A file descriptor.
|
2062 | * @param atime The last access time. If a string is provided, it will be coerced to number.
|
2063 | * @param mtime The last modified time. If a string is provided, it will be coerced to number.
|
2064 | */
|
2065 | function __promisify__(fd: number, atime: TimeLike, mtime: TimeLike): Promise<void>;
|
2066 | }
|
2067 | /**
|
2068 | * Synchronous version of {@link futimes}. Returns `undefined`.
|
2069 | * @since v0.4.2
|
2070 | */
|
2071 | export function futimesSync(fd: number, atime: TimeLike, mtime: TimeLike): void;
|
2072 | /**
|
2073 | * Request that all data for the open file descriptor is flushed to the storage
|
2074 | * device. The specific implementation is operating system and device specific.
|
2075 | * Refer to the POSIX [`fsync(2)`](http://man7.org/linux/man-pages/man2/fsync.2.html) documentation for more detail. No arguments other
|
2076 | * than a possible exception are given to the completion callback.
|
2077 | * @since v0.1.96
|
2078 | */
|
2079 | export function fsync(fd: number, callback: NoParamCallback): void;
|
2080 | export namespace fsync {
|
2081 | /**
|
2082 | * Asynchronous fsync(2) - synchronize a file's in-core state with the underlying storage device.
|
2083 | * @param fd A file descriptor.
|
2084 | */
|
2085 | function __promisify__(fd: number): Promise<void>;
|
2086 | }
|
2087 | /**
|
2088 | * Request that all data for the open file descriptor is flushed to the storage
|
2089 | * device. The specific implementation is operating system and device specific.
|
2090 | * Refer to the POSIX [`fsync(2)`](http://man7.org/linux/man-pages/man2/fsync.2.html) documentation for more detail. Returns `undefined`.
|
2091 | * @since v0.1.96
|
2092 | */
|
2093 | export function fsyncSync(fd: number): void;
|
2094 | /**
|
2095 | * Write `buffer` to the file specified by `fd`. If `buffer` is a normal object, it
|
2096 | * must have an own `toString` function property.
|
2097 | *
|
2098 | * `offset` determines the part of the buffer to be written, and `length` is
|
2099 | * an integer specifying the number of bytes to write.
|
2100 | *
|
2101 | * `position` refers to the offset from the beginning of the file where this data
|
2102 | * should be written. If `typeof position !== 'number'`, the data will be written
|
2103 | * at the current position. See [`pwrite(2)`](http://man7.org/linux/man-pages/man2/pwrite.2.html).
|
2104 | *
|
2105 | * The callback will be given three arguments `(err, bytesWritten, buffer)` where`bytesWritten` specifies how many _bytes_ were written from `buffer`.
|
2106 | *
|
2107 | * If this method is invoked as its `util.promisify()` ed version, it returns
|
2108 | * a promise for an `Object` with `bytesWritten` and `buffer` properties.
|
2109 | *
|
2110 | * It is unsafe to use `fs.write()` multiple times on the same file without waiting
|
2111 | * for the callback. For this scenario, {@link createWriteStream} is
|
2112 | * recommended.
|
2113 | *
|
2114 | * On Linux, positional writes don't work when the file is opened in append mode.
|
2115 | * The kernel ignores the position argument and always appends the data to
|
2116 | * the end of the file.
|
2117 | * @since v0.0.2
|
2118 | */
|
2119 | export function write<TBuffer extends NodeJS.ArrayBufferView>(
|
2120 | fd: number,
|
2121 | buffer: TBuffer,
|
2122 | offset: number | undefined | null,
|
2123 | length: number | undefined | null,
|
2124 | position: number | undefined | null,
|
2125 | callback: (err: NodeJS.ErrnoException | null, written: number, buffer: TBuffer) => void
|
2126 | ): void;
|
2127 | /**
|
2128 | * Asynchronously writes `buffer` to the file referenced by the supplied file descriptor.
|
2129 | * @param fd A file descriptor.
|
2130 | * @param offset The part of the buffer to be written. If not supplied, defaults to `0`.
|
2131 | * @param length The number of bytes to write. If not supplied, defaults to `buffer.length - offset`.
|
2132 | */
|
2133 | export function write<TBuffer extends NodeJS.ArrayBufferView>(
|
2134 | fd: number,
|
2135 | buffer: TBuffer,
|
2136 | offset: number | undefined | null,
|
2137 | length: number | undefined | null,
|
2138 | callback: (err: NodeJS.ErrnoException | null, written: number, buffer: TBuffer) => void
|
2139 | ): void;
|
2140 | /**
|
2141 | * Asynchronously writes `buffer` to the file referenced by the supplied file descriptor.
|
2142 | * @param fd A file descriptor.
|
2143 | * @param offset The part of the buffer to be written. If not supplied, defaults to `0`.
|
2144 | */
|
2145 | export function write<TBuffer extends NodeJS.ArrayBufferView>(
|
2146 | fd: number,
|
2147 | buffer: TBuffer,
|
2148 | offset: number | undefined | null,
|
2149 | callback: (err: NodeJS.ErrnoException | null, written: number, buffer: TBuffer) => void
|
2150 | ): void;
|
2151 | /**
|
2152 | * Asynchronously writes `buffer` to the file referenced by the supplied file descriptor.
|
2153 | * @param fd A file descriptor.
|
2154 | */
|
2155 | export function write<TBuffer extends NodeJS.ArrayBufferView>(fd: number, buffer: TBuffer, callback: (err: NodeJS.ErrnoException | null, written: number, buffer: TBuffer) => void): void;
|
2156 | /**
|
2157 | * Asynchronously writes `string` to the file referenced by the supplied file descriptor.
|
2158 | * @param fd A file descriptor.
|
2159 | * @param string A string to write.
|
2160 | * @param position The offset from the beginning of the file where this data should be written. If not supplied, defaults to the current position.
|
2161 | * @param encoding The expected string encoding.
|
2162 | */
|
2163 | export function write(
|
2164 | fd: number,
|
2165 | string: string,
|
2166 | position: number | undefined | null,
|
2167 | encoding: BufferEncoding | undefined | null,
|
2168 | callback: (err: NodeJS.ErrnoException | null, written: number, str: string) => void
|
2169 | ): void;
|
2170 | /**
|
2171 | * Asynchronously writes `string` to the file referenced by the supplied file descriptor.
|
2172 | * @param fd A file descriptor.
|
2173 | * @param string A string to write.
|
2174 | * @param position The offset from the beginning of the file where this data should be written. If not supplied, defaults to the current position.
|
2175 | */
|
2176 | export function write(fd: number, string: string, position: number | undefined | null, callback: (err: NodeJS.ErrnoException | null, written: number, str: string) => void): void;
|
2177 | /**
|
2178 | * Asynchronously writes `string` to the file referenced by the supplied file descriptor.
|
2179 | * @param fd A file descriptor.
|
2180 | * @param string A string to write.
|
2181 | */
|
2182 | export function write(fd: number, string: string, callback: (err: NodeJS.ErrnoException | null, written: number, str: string) => void): void;
|
2183 | export namespace write {
|
2184 | /**
|
2185 | * Asynchronously writes `buffer` to the file referenced by the supplied file descriptor.
|
2186 | * @param fd A file descriptor.
|
2187 | * @param offset The part of the buffer to be written. If not supplied, defaults to `0`.
|
2188 | * @param length The number of bytes to write. If not supplied, defaults to `buffer.length - offset`.
|
2189 | * @param position The offset from the beginning of the file where this data should be written. If not supplied, defaults to the current position.
|
2190 | */
|
2191 | function __promisify__<TBuffer extends NodeJS.ArrayBufferView>(
|
2192 | fd: number,
|
2193 | buffer?: TBuffer,
|
2194 | offset?: number,
|
2195 | length?: number,
|
2196 | position?: number | null
|
2197 | ): Promise<{
|
2198 | bytesWritten: number;
|
2199 | buffer: TBuffer;
|
2200 | }>;
|
2201 | /**
|
2202 | * Asynchronously writes `string` to the file referenced by the supplied file descriptor.
|
2203 | * @param fd A file descriptor.
|
2204 | * @param string A string to write.
|
2205 | * @param position The offset from the beginning of the file where this data should be written. If not supplied, defaults to the current position.
|
2206 | * @param encoding The expected string encoding.
|
2207 | */
|
2208 | function __promisify__(
|
2209 | fd: number,
|
2210 | string: string,
|
2211 | position?: number | null,
|
2212 | encoding?: BufferEncoding | null
|
2213 | ): Promise<{
|
2214 | bytesWritten: number;
|
2215 | buffer: string;
|
2216 | }>;
|
2217 | }
|
2218 | /**
|
2219 | * If `buffer` is a plain object, it must have an own (not inherited) `toString`function property.
|
2220 | *
|
2221 | * For detailed information, see the documentation of the asynchronous version of
|
2222 | * this API: {@link write}.
|
2223 | * @since v0.1.21
|
2224 | * @return The number of bytes written.
|
2225 | */
|
2226 | export function writeSync(fd: number, buffer: NodeJS.ArrayBufferView, offset?: number | null, length?: number | null, position?: number | null): number;
|
2227 | /**
|
2228 | * Synchronously writes `string` to the file referenced by the supplied file descriptor, returning the number of bytes written.
|
2229 | * @param fd A file descriptor.
|
2230 | * @param string A string to write.
|
2231 | * @param position The offset from the beginning of the file where this data should be written. If not supplied, defaults to the current position.
|
2232 | * @param encoding The expected string encoding.
|
2233 | */
|
2234 | export function writeSync(fd: number, string: string, position?: number | null, encoding?: BufferEncoding | null): number;
|
2235 | export type ReadPosition = number | bigint;
|
2236 | /**
|
2237 | * Read data from the file specified by `fd`.
|
2238 | *
|
2239 | * The callback is given the three arguments, `(err, bytesRead, buffer)`.
|
2240 | *
|
2241 | * If the file is not modified concurrently, the end-of-file is reached when the
|
2242 | * number of bytes read is zero.
|
2243 | *
|
2244 | * If this method is invoked as its `util.promisify()` ed version, it returns
|
2245 | * a promise for an `Object` with `bytesRead` and `buffer` properties.
|
2246 | * @since v0.0.2
|
2247 | * @param buffer The buffer that the data will be written to.
|
2248 | * @param offset The position in `buffer` to write the data to.
|
2249 | * @param length The number of bytes to read.
|
2250 | * @param position Specifies where to begin reading from in the file. If `position` is `null` or `-1 `, data will be read from the current file position, and the file position will be updated. If
|
2251 | * `position` is an integer, the file position will be unchanged.
|
2252 | */
|
2253 | export function read<TBuffer extends NodeJS.ArrayBufferView>(
|
2254 | fd: number,
|
2255 | buffer: TBuffer,
|
2256 | offset: number,
|
2257 | length: number,
|
2258 | position: ReadPosition | null,
|
2259 | callback: (err: NodeJS.ErrnoException | null, bytesRead: number, buffer: TBuffer) => void
|
2260 | ): void;
|
2261 | export namespace read {
|
2262 | /**
|
2263 | * @param fd A file descriptor.
|
2264 | * @param buffer The buffer that the data will be written to.
|
2265 | * @param offset The offset in the buffer at which to start writing.
|
2266 | * @param length The number of bytes to read.
|
2267 | * @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.
|
2268 | */
|
2269 | function __promisify__<TBuffer extends NodeJS.ArrayBufferView>(
|
2270 | fd: number,
|
2271 | buffer: TBuffer,
|
2272 | offset: number,
|
2273 | length: number,
|
2274 | position: number | null
|
2275 | ): Promise<{
|
2276 | bytesRead: number;
|
2277 | buffer: TBuffer;
|
2278 | }>;
|
2279 | }
|
2280 | export interface ReadSyncOptions {
|
2281 | /**
|
2282 | * @default 0
|
2283 | */
|
2284 | offset?: number | undefined;
|
2285 | /**
|
2286 | * @default `length of buffer`
|
2287 | */
|
2288 | length?: number | undefined;
|
2289 | /**
|
2290 | * @default null
|
2291 | */
|
2292 | position?: ReadPosition | null | undefined;
|
2293 | }
|
2294 | /**
|
2295 | * Returns the number of `bytesRead`.
|
2296 | *
|
2297 | * For detailed information, see the documentation of the asynchronous version of
|
2298 | * this API: {@link read}.
|
2299 | * @since v0.1.21
|
2300 | */
|
2301 | export function readSync(fd: number, buffer: NodeJS.ArrayBufferView, offset: number, length: number, position: ReadPosition | null): number;
|
2302 | /**
|
2303 | * Similar to the above `fs.readSync` function, this version takes an optional `options` object.
|
2304 | * If no `options` object is specified, it will default with the above values.
|
2305 | */
|
2306 | export function readSync(fd: number, buffer: NodeJS.ArrayBufferView, opts?: ReadSyncOptions): number;
|
2307 | /**
|
2308 | * Asynchronously reads the entire contents of a file.
|
2309 | *
|
2310 | * ```js
|
2311 | * import { readFile } from 'fs';
|
2312 | *
|
2313 | * readFile('/etc/passwd', (err, data) => {
|
2314 | * if (err) throw err;
|
2315 | * console.log(data);
|
2316 | * });
|
2317 | * ```
|
2318 | *
|
2319 | * The callback is passed two arguments `(err, data)`, where `data` is the
|
2320 | * contents of the file.
|
2321 | *
|
2322 | * If no encoding is specified, then the raw buffer is returned.
|
2323 | *
|
2324 | * If `options` is a string, then it specifies the encoding:
|
2325 | *
|
2326 | * ```js
|
2327 | * import { readFile } from 'fs';
|
2328 | *
|
2329 | * readFile('/etc/passwd', 'utf8', callback);
|
2330 | * ```
|
2331 | *
|
2332 | * When the path is a directory, the behavior of `fs.readFile()` and {@link readFileSync} is platform-specific. On macOS, Linux, and Windows, an
|
2333 | * error will be returned. On FreeBSD, a representation of the directory's contents
|
2334 | * will be returned.
|
2335 | *
|
2336 | * ```js
|
2337 | * import { readFile } from 'fs';
|
2338 | *
|
2339 | * // macOS, Linux, and Windows
|
2340 | * readFile('<directory>', (err, data) => {
|
2341 | * // => [Error: EISDIR: illegal operation on a directory, read <directory>]
|
2342 | * });
|
2343 | *
|
2344 | * // FreeBSD
|
2345 | * readFile('<directory>', (err, data) => {
|
2346 | * // => null, <data>
|
2347 | * });
|
2348 | * ```
|
2349 | *
|
2350 | * It is possible to abort an ongoing request using an `AbortSignal`. If a
|
2351 | * request is aborted the callback is called with an `AbortError`:
|
2352 | *
|
2353 | * ```js
|
2354 | * import { readFile } from 'fs';
|
2355 | *
|
2356 | * const controller = new AbortController();
|
2357 | * const signal = controller.signal;
|
2358 | * readFile(fileInfo[0].name, { signal }, (err, buf) => {
|
2359 | * // ...
|
2360 | * });
|
2361 | * // When you want to abort the request
|
2362 | * controller.abort();
|
2363 | * ```
|
2364 | *
|
2365 | * The `fs.readFile()` function buffers the entire file. To minimize memory costs,
|
2366 | * when possible prefer streaming via `fs.createReadStream()`.
|
2367 | *
|
2368 | * Aborting an ongoing request does not abort individual operating
|
2369 | * system requests but rather the internal buffering `fs.readFile` performs.
|
2370 | * @since v0.1.29
|
2371 | * @param path filename or file descriptor
|
2372 | */
|
2373 | export function readFile(
|
2374 | path: PathOrFileDescriptor,
|
2375 | options:
|
2376 | | ({
|
2377 | encoding?: null | undefined;
|
2378 | flag?: string | undefined;
|
2379 | } & Abortable)
|
2380 | | undefined
|
2381 | | null,
|
2382 | callback: (err: NodeJS.ErrnoException | null, data: Buffer) => void
|
2383 | ): void;
|
2384 | /**
|
2385 | * Asynchronously reads the entire contents of a file.
|
2386 | * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
|
2387 | * If a file descriptor is provided, the underlying file will _not_ be closed automatically.
|
2388 | * @param options Either the encoding for the result, or an object that contains the encoding and an optional flag.
|
2389 | * If a flag is not provided, it defaults to `'r'`.
|
2390 | */
|
2391 | export function readFile(
|
2392 | path: PathOrFileDescriptor,
|
2393 | options:
|
2394 | | ({
|
2395 | encoding: BufferEncoding;
|
2396 | flag?: string | undefined;
|
2397 | } & Abortable)
|
2398 | | BufferEncoding,
|
2399 | callback: (err: NodeJS.ErrnoException | null, data: string) => void
|
2400 | ): void;
|
2401 | /**
|
2402 | * Asynchronously reads the entire contents of a file.
|
2403 | * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
|
2404 | * If a file descriptor is provided, the underlying file will _not_ be closed automatically.
|
2405 | * @param options Either the encoding for the result, or an object that contains the encoding and an optional flag.
|
2406 | * If a flag is not provided, it defaults to `'r'`.
|
2407 | */
|
2408 | export function readFile(
|
2409 | path: PathOrFileDescriptor,
|
2410 | options:
|
2411 | | (ObjectEncodingOptions & {
|
2412 | flag?: string | undefined;
|
2413 | } & Abortable)
|
2414 | | BufferEncoding
|
2415 | | undefined
|
2416 | | null,
|
2417 | callback: (err: NodeJS.ErrnoException | null, data: string | Buffer) => void
|
2418 | ): void;
|
2419 | /**
|
2420 | * Asynchronously reads the entire contents of a file.
|
2421 | * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
|
2422 | * If a file descriptor is provided, the underlying file will _not_ be closed automatically.
|
2423 | */
|
2424 | export function readFile(path: PathOrFileDescriptor, callback: (err: NodeJS.ErrnoException | null, data: Buffer) => void): void;
|
2425 | export namespace readFile {
|
2426 | /**
|
2427 | * Asynchronously reads the entire contents of a file.
|
2428 | * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
|
2429 | * If a file descriptor is provided, the underlying file will _not_ be closed automatically.
|
2430 | * @param options An object that may contain an optional flag.
|
2431 | * If a flag is not provided, it defaults to `'r'`.
|
2432 | */
|
2433 | function __promisify__(
|
2434 | path: PathOrFileDescriptor,
|
2435 | options?: {
|
2436 | encoding?: null | undefined;
|
2437 | flag?: string | undefined;
|
2438 | } | null
|
2439 | ): Promise<Buffer>;
|
2440 | /**
|
2441 | * Asynchronously reads the entire contents of a file.
|
2442 | * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
|
2443 | * URL support is _experimental_.
|
2444 | * If a file descriptor is provided, the underlying file will _not_ be closed automatically.
|
2445 | * @param options Either the encoding for the result, or an object that contains the encoding and an optional flag.
|
2446 | * If a flag is not provided, it defaults to `'r'`.
|
2447 | */
|
2448 | function __promisify__(
|
2449 | path: PathOrFileDescriptor,
|
2450 | options:
|
2451 | | {
|
2452 | encoding: BufferEncoding;
|
2453 | flag?: string | undefined;
|
2454 | }
|
2455 | | BufferEncoding
|
2456 | ): Promise<string>;
|
2457 | /**
|
2458 | * Asynchronously reads the entire contents of a file.
|
2459 | * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
|
2460 | * URL support is _experimental_.
|
2461 | * If a file descriptor is provided, the underlying file will _not_ be closed automatically.
|
2462 | * @param options Either the encoding for the result, or an object that contains the encoding and an optional flag.
|
2463 | * If a flag is not provided, it defaults to `'r'`.
|
2464 | */
|
2465 | function __promisify__(
|
2466 | path: PathOrFileDescriptor,
|
2467 | options?:
|
2468 | | (ObjectEncodingOptions & {
|
2469 | flag?: string | undefined;
|
2470 | })
|
2471 | | BufferEncoding
|
2472 | | null
|
2473 | ): Promise<string | Buffer>;
|
2474 | }
|
2475 | /**
|
2476 | * Returns the contents of the `path`.
|
2477 | *
|
2478 | * For detailed information, see the documentation of the asynchronous version of
|
2479 | * this API: {@link readFile}.
|
2480 | *
|
2481 | * If the `encoding` option is specified then this function returns a
|
2482 | * string. Otherwise it returns a buffer.
|
2483 | *
|
2484 | * Similar to {@link readFile}, when the path is a directory, the behavior of`fs.readFileSync()` is platform-specific.
|
2485 | *
|
2486 | * ```js
|
2487 | * import { readFileSync } from 'fs';
|
2488 | *
|
2489 | * // macOS, Linux, and Windows
|
2490 | * readFileSync('<directory>');
|
2491 | * // => [Error: EISDIR: illegal operation on a directory, read <directory>]
|
2492 | *
|
2493 | * // FreeBSD
|
2494 | * readFileSync('<directory>'); // => <data>
|
2495 | * ```
|
2496 | * @since v0.1.8
|
2497 | * @param path filename or file descriptor
|
2498 | */
|
2499 | export function readFileSync(
|
2500 | path: PathOrFileDescriptor,
|
2501 | options?: {
|
2502 | encoding?: null | undefined;
|
2503 | flag?: string | undefined;
|
2504 | } | null
|
2505 | ): Buffer;
|
2506 | /**
|
2507 | * Synchronously reads the entire contents of a file.
|
2508 | * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
|
2509 | * If a file descriptor is provided, the underlying file will _not_ be closed automatically.
|
2510 | * @param options Either the encoding for the result, or an object that contains the encoding and an optional flag.
|
2511 | * If a flag is not provided, it defaults to `'r'`.
|
2512 | */
|
2513 | export function readFileSync(
|
2514 | path: PathOrFileDescriptor,
|
2515 | options:
|
2516 | | {
|
2517 | encoding: BufferEncoding;
|
2518 | flag?: string | undefined;
|
2519 | }
|
2520 | | BufferEncoding
|
2521 | ): string;
|
2522 | /**
|
2523 | * Synchronously reads the entire contents of a file.
|
2524 | * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
|
2525 | * If a file descriptor is provided, the underlying file will _not_ be closed automatically.
|
2526 | * @param options Either the encoding for the result, or an object that contains the encoding and an optional flag.
|
2527 | * If a flag is not provided, it defaults to `'r'`.
|
2528 | */
|
2529 | export function readFileSync(
|
2530 | path: PathOrFileDescriptor,
|
2531 | options?:
|
2532 | | (ObjectEncodingOptions & {
|
2533 | flag?: string | undefined;
|
2534 | })
|
2535 | | BufferEncoding
|
2536 | | null
|
2537 | ): string | Buffer;
|
2538 | export type WriteFileOptions =
|
2539 | | (ObjectEncodingOptions &
|
2540 | Abortable & {
|
2541 | mode?: Mode | undefined;
|
2542 | flag?: string | undefined;
|
2543 | })
|
2544 | | BufferEncoding
|
2545 | | null;
|
2546 | /**
|
2547 | * When `file` is a filename, asynchronously writes data to the file, replacing the
|
2548 | * file if it already exists. `data` can be a string or a buffer.
|
2549 | *
|
2550 | * When `file` is a file descriptor, the behavior is similar to calling`fs.write()` directly (which is recommended). See the notes below on using
|
2551 | * a file descriptor.
|
2552 | *
|
2553 | * The `encoding` option is ignored if `data` is a buffer.
|
2554 | *
|
2555 | * The `mode` option only affects the newly created file. See {@link open} for more details.
|
2556 | *
|
2557 | * If `data` is a plain object, it must have an own (not inherited) `toString`function property.
|
2558 | *
|
2559 | * ```js
|
2560 | * import { writeFile } from 'fs';
|
2561 | * import { Buffer } from 'buffer';
|
2562 | *
|
2563 | * const data = new Uint8Array(Buffer.from('Hello Node.js'));
|
2564 | * writeFile('message.txt', data, (err) => {
|
2565 | * if (err) throw err;
|
2566 | * console.log('The file has been saved!');
|
2567 | * });
|
2568 | * ```
|
2569 | *
|
2570 | * If `options` is a string, then it specifies the encoding:
|
2571 | *
|
2572 | * ```js
|
2573 | * import { writeFile } from 'fs';
|
2574 | *
|
2575 | * writeFile('message.txt', 'Hello Node.js', 'utf8', callback);
|
2576 | * ```
|
2577 | *
|
2578 | * It is unsafe to use `fs.writeFile()` multiple times on the same file without
|
2579 | * waiting for the callback. For this scenario, {@link createWriteStream} is
|
2580 | * recommended.
|
2581 | *
|
2582 | * Similarly to `fs.readFile` \- `fs.writeFile` is a convenience method that
|
2583 | * performs multiple `write` calls internally to write the buffer passed to it.
|
2584 | * For performance sensitive code consider using {@link createWriteStream}.
|
2585 | *
|
2586 | * It is possible to use an `AbortSignal` to cancel an `fs.writeFile()`.
|
2587 | * Cancelation is "best effort", and some amount of data is likely still
|
2588 | * to be written.
|
2589 | *
|
2590 | * ```js
|
2591 | * import { writeFile } from 'fs';
|
2592 | * import { Buffer } from 'buffer';
|
2593 | *
|
2594 | * const controller = new AbortController();
|
2595 | * const { signal } = controller;
|
2596 | * const data = new Uint8Array(Buffer.from('Hello Node.js'));
|
2597 | * writeFile('message.txt', data, { signal }, (err) => {
|
2598 | * // When a request is aborted - the callback is called with an AbortError
|
2599 | * });
|
2600 | * // When the request should be aborted
|
2601 | * controller.abort();
|
2602 | * ```
|
2603 | *
|
2604 | * Aborting an ongoing request does not abort individual operating
|
2605 | * system requests but rather the internal buffering `fs.writeFile` performs.
|
2606 | * @since v0.1.29
|
2607 | * @param file filename or file descriptor
|
2608 | */
|
2609 | export function writeFile(file: PathOrFileDescriptor, data: string | NodeJS.ArrayBufferView, options: WriteFileOptions, callback: NoParamCallback): void;
|
2610 | /**
|
2611 | * Asynchronously writes data to a file, replacing the file if it already exists.
|
2612 | * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
|
2613 | * If a file descriptor is provided, the underlying file will _not_ be closed automatically.
|
2614 | * @param data The data to write. If something other than a Buffer or Uint8Array is provided, the value is coerced to a string.
|
2615 | */
|
2616 | export function writeFile(path: PathOrFileDescriptor, data: string | NodeJS.ArrayBufferView, callback: NoParamCallback): void;
|
2617 | export namespace writeFile {
|
2618 | /**
|
2619 | * Asynchronously writes data to a file, replacing the file if it already exists.
|
2620 | * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
|
2621 | * URL support is _experimental_.
|
2622 | * If a file descriptor is provided, the underlying file will _not_ be closed automatically.
|
2623 | * @param data The data to write. If something other than a Buffer or Uint8Array is provided, the value is coerced to a string.
|
2624 | * @param options Either the encoding for the file, or an object optionally specifying the encoding, file mode, and flag.
|
2625 | * If `encoding` is not supplied, the default of `'utf8'` is used.
|
2626 | * If `mode` is not supplied, the default of `0o666` is used.
|
2627 | * If `mode` is a string, it is parsed as an octal integer.
|
2628 | * If `flag` is not supplied, the default of `'w'` is used.
|
2629 | */
|
2630 | function __promisify__(path: PathOrFileDescriptor, data: string | NodeJS.ArrayBufferView, options?: WriteFileOptions): Promise<void>;
|
2631 | }
|
2632 | /**
|
2633 | * Returns `undefined`.
|
2634 | *
|
2635 | * If `data` is a plain object, it must have an own (not inherited) `toString`function property.
|
2636 | *
|
2637 | * The `mode` option only affects the newly created file. See {@link open} for more details.
|
2638 | *
|
2639 | * For detailed information, see the documentation of the asynchronous version of
|
2640 | * this API: {@link writeFile}.
|
2641 | * @since v0.1.29
|
2642 | * @param file filename or file descriptor
|
2643 | */
|
2644 | export function writeFileSync(file: PathOrFileDescriptor, data: string | NodeJS.ArrayBufferView, options?: WriteFileOptions): void;
|
2645 | /**
|
2646 | * Asynchronously append data to a file, creating the file if it does not yet
|
2647 | * exist. `data` can be a string or a `Buffer`.
|
2648 | *
|
2649 | * The `mode` option only affects the newly created file. See {@link open} for more details.
|
2650 | *
|
2651 | * ```js
|
2652 | * import { appendFile } from 'fs';
|
2653 | *
|
2654 | * appendFile('message.txt', 'data to append', (err) => {
|
2655 | * if (err) throw err;
|
2656 | * console.log('The "data to append" was appended to file!');
|
2657 | * });
|
2658 | * ```
|
2659 | *
|
2660 | * If `options` is a string, then it specifies the encoding:
|
2661 | *
|
2662 | * ```js
|
2663 | * import { appendFile } from 'fs';
|
2664 | *
|
2665 | * appendFile('message.txt', 'data to append', 'utf8', callback);
|
2666 | * ```
|
2667 | *
|
2668 | * The `path` may be specified as a numeric file descriptor that has been opened
|
2669 | * for appending (using `fs.open()` or `fs.openSync()`). The file descriptor will
|
2670 | * not be closed automatically.
|
2671 | *
|
2672 | * ```js
|
2673 | * import { open, close, appendFile } from 'fs';
|
2674 | *
|
2675 | * function closeFd(fd) {
|
2676 | * close(fd, (err) => {
|
2677 | * if (err) throw err;
|
2678 | * });
|
2679 | * }
|
2680 | *
|
2681 | * open('message.txt', 'a', (err, fd) => {
|
2682 | * if (err) throw err;
|
2683 | *
|
2684 | * try {
|
2685 | * appendFile(fd, 'data to append', 'utf8', (err) => {
|
2686 | * closeFd(fd);
|
2687 | * if (err) throw err;
|
2688 | * });
|
2689 | * } catch (err) {
|
2690 | * closeFd(fd);
|
2691 | * throw err;
|
2692 | * }
|
2693 | * });
|
2694 | * ```
|
2695 | * @since v0.6.7
|
2696 | * @param path filename or file descriptor
|
2697 | */
|
2698 | export function appendFile(path: PathOrFileDescriptor, data: string | Uint8Array, options: WriteFileOptions, callback: NoParamCallback): void;
|
2699 | /**
|
2700 | * Asynchronously append data to a file, creating the file if it does not exist.
|
2701 | * @param file A path to a file. If a URL is provided, it must use the `file:` protocol.
|
2702 | * If a file descriptor is provided, the underlying file will _not_ be closed automatically.
|
2703 | * @param data The data to write. If something other than a Buffer or Uint8Array is provided, the value is coerced to a string.
|
2704 | */
|
2705 | export function appendFile(file: PathOrFileDescriptor, data: string | Uint8Array, callback: NoParamCallback): void;
|
2706 | export namespace appendFile {
|
2707 | /**
|
2708 | * Asynchronously append data to a file, creating the file if it does not exist.
|
2709 | * @param file A path to a file. If a URL is provided, it must use the `file:` protocol.
|
2710 | * URL support is _experimental_.
|
2711 | * If a file descriptor is provided, the underlying file will _not_ be closed automatically.
|
2712 | * @param data The data to write. If something other than a Buffer or Uint8Array is provided, the value is coerced to a string.
|
2713 | * @param options Either the encoding for the file, or an object optionally specifying the encoding, file mode, and flag.
|
2714 | * If `encoding` is not supplied, the default of `'utf8'` is used.
|
2715 | * If `mode` is not supplied, the default of `0o666` is used.
|
2716 | * If `mode` is a string, it is parsed as an octal integer.
|
2717 | * If `flag` is not supplied, the default of `'a'` is used.
|
2718 | */
|
2719 | function __promisify__(file: PathOrFileDescriptor, data: string | Uint8Array, options?: WriteFileOptions): Promise<void>;
|
2720 | }
|
2721 | /**
|
2722 | * Synchronously append data to a file, creating the file if it does not yet
|
2723 | * exist. `data` can be a string or a `Buffer`.
|
2724 | *
|
2725 | * The `mode` option only affects the newly created file. See {@link open} for more details.
|
2726 | *
|
2727 | * ```js
|
2728 | * import { appendFileSync } from 'fs';
|
2729 | *
|
2730 | * try {
|
2731 | * appendFileSync('message.txt', 'data to append');
|
2732 | * console.log('The "data to append" was appended to file!');
|
2733 | * } catch (err) {
|
2734 | * // Handle the error
|
2735 | * }
|
2736 | * ```
|
2737 | *
|
2738 | * If `options` is a string, then it specifies the encoding:
|
2739 | *
|
2740 | * ```js
|
2741 | * import { appendFileSync } from 'fs';
|
2742 | *
|
2743 | * appendFileSync('message.txt', 'data to append', 'utf8');
|
2744 | * ```
|
2745 | *
|
2746 | * The `path` may be specified as a numeric file descriptor that has been opened
|
2747 | * for appending (using `fs.open()` or `fs.openSync()`). The file descriptor will
|
2748 | * not be closed automatically.
|
2749 | *
|
2750 | * ```js
|
2751 | * import { openSync, closeSync, appendFileSync } from 'fs';
|
2752 | *
|
2753 | * let fd;
|
2754 | *
|
2755 | * try {
|
2756 | * fd = openSync('message.txt', 'a');
|
2757 | * appendFileSync(fd, 'data to append', 'utf8');
|
2758 | * } catch (err) {
|
2759 | * // Handle the error
|
2760 | * } finally {
|
2761 | * if (fd !== undefined)
|
2762 | * closeSync(fd);
|
2763 | * }
|
2764 | * ```
|
2765 | * @since v0.6.7
|
2766 | * @param path filename or file descriptor
|
2767 | */
|
2768 | export function appendFileSync(path: PathOrFileDescriptor, data: string | Uint8Array, options?: WriteFileOptions): void;
|
2769 | /**
|
2770 | * Watch for changes on `filename`. The callback `listener` will be called each
|
2771 | * time the file is accessed.
|
2772 | *
|
2773 | * The `options` argument may be omitted. If provided, it should be an object. The`options` object may contain a boolean named `persistent` that indicates
|
2774 | * whether the process should continue to run as long as files are being watched.
|
2775 | * The `options` object may specify an `interval` property indicating how often the
|
2776 | * target should be polled in milliseconds.
|
2777 | *
|
2778 | * The `listener` gets two arguments the current stat object and the previous
|
2779 | * stat object:
|
2780 | *
|
2781 | * ```js
|
2782 | * import { watchFile } from 'fs';
|
2783 | *
|
2784 | * watchFile('message.text', (curr, prev) => {
|
2785 | * console.log(`the current mtime is: ${curr.mtime}`);
|
2786 | * console.log(`the previous mtime was: ${prev.mtime}`);
|
2787 | * });
|
2788 | * ```
|
2789 | *
|
2790 | * These stat objects are instances of `fs.Stat`. If the `bigint` option is `true`,
|
2791 | * the numeric values in these objects are specified as `BigInt`s.
|
2792 | *
|
2793 | * To be notified when the file was modified, not just accessed, it is necessary
|
2794 | * to compare `curr.mtimeMs` and `prev.mtimeMs`.
|
2795 | *
|
2796 | * When an `fs.watchFile` operation results in an `ENOENT` error, it
|
2797 | * will invoke the listener once, with all the fields zeroed (or, for dates, the
|
2798 | * Unix Epoch). If the file is created later on, the listener will be called
|
2799 | * again, with the latest stat objects. This is a change in functionality since
|
2800 | * v0.10.
|
2801 | *
|
2802 | * Using {@link watch} is more efficient than `fs.watchFile` and`fs.unwatchFile`. `fs.watch` should be used instead of `fs.watchFile` and`fs.unwatchFile` when possible.
|
2803 | *
|
2804 | * When a file being watched by `fs.watchFile()` disappears and reappears,
|
2805 | * then the contents of `previous` in the second callback event (the file's
|
2806 | * reappearance) will be the same as the contents of `previous` in the first
|
2807 | * callback event (its disappearance).
|
2808 | *
|
2809 | * This happens when:
|
2810 | *
|
2811 | * * the file is deleted, followed by a restore
|
2812 | * * the file is renamed and then renamed a second time back to its original name
|
2813 | * @since v0.1.31
|
2814 | */
|
2815 | export interface WatchFileOptions {
|
2816 | bigint?: boolean | undefined;
|
2817 | persistent?: boolean | undefined;
|
2818 | interval?: number | undefined;
|
2819 | }
|
2820 | /**
|
2821 | * Watch for changes on `filename`. The callback `listener` will be called each
|
2822 | * time the file is accessed.
|
2823 | *
|
2824 | * The `options` argument may be omitted. If provided, it should be an object. The`options` object may contain a boolean named `persistent` that indicates
|
2825 | * whether the process should continue to run as long as files are being watched.
|
2826 | * The `options` object may specify an `interval` property indicating how often the
|
2827 | * target should be polled in milliseconds.
|
2828 | *
|
2829 | * The `listener` gets two arguments the current stat object and the previous
|
2830 | * stat object:
|
2831 | *
|
2832 | * ```js
|
2833 | * import { watchFile } from 'fs';
|
2834 | *
|
2835 | * watchFile('message.text', (curr, prev) => {
|
2836 | * console.log(`the current mtime is: ${curr.mtime}`);
|
2837 | * console.log(`the previous mtime was: ${prev.mtime}`);
|
2838 | * });
|
2839 | * ```
|
2840 | *
|
2841 | * These stat objects are instances of `fs.Stat`. If the `bigint` option is `true`,
|
2842 | * the numeric values in these objects are specified as `BigInt`s.
|
2843 | *
|
2844 | * To be notified when the file was modified, not just accessed, it is necessary
|
2845 | * to compare `curr.mtimeMs` and `prev.mtimeMs`.
|
2846 | *
|
2847 | * When an `fs.watchFile` operation results in an `ENOENT` error, it
|
2848 | * will invoke the listener once, with all the fields zeroed (or, for dates, the
|
2849 | * Unix Epoch). If the file is created later on, the listener will be called
|
2850 | * again, with the latest stat objects. This is a change in functionality since
|
2851 | * v0.10.
|
2852 | *
|
2853 | * Using {@link watch} is more efficient than `fs.watchFile` and`fs.unwatchFile`. `fs.watch` should be used instead of `fs.watchFile` and`fs.unwatchFile` when possible.
|
2854 | *
|
2855 | * When a file being watched by `fs.watchFile()` disappears and reappears,
|
2856 | * then the contents of `previous` in the second callback event (the file's
|
2857 | * reappearance) will be the same as the contents of `previous` in the first
|
2858 | * callback event (its disappearance).
|
2859 | *
|
2860 | * This happens when:
|
2861 | *
|
2862 | * * the file is deleted, followed by a restore
|
2863 | * * the file is renamed and then renamed a second time back to its original name
|
2864 | * @since v0.1.31
|
2865 | */
|
2866 | export function watchFile(
|
2867 | filename: PathLike,
|
2868 | options:
|
2869 | | (WatchFileOptions & {
|
2870 | bigint?: false | undefined;
|
2871 | })
|
2872 | | undefined,
|
2873 | listener: (curr: Stats, prev: Stats) => void
|
2874 | ): StatWatcher;
|
2875 | export function watchFile(
|
2876 | filename: PathLike,
|
2877 | options:
|
2878 | | (WatchFileOptions & {
|
2879 | bigint: true;
|
2880 | })
|
2881 | | undefined,
|
2882 | listener: (curr: BigIntStats, prev: BigIntStats) => void
|
2883 | ): StatWatcher;
|
2884 | /**
|
2885 | * Watch for changes on `filename`. The callback `listener` will be called each time the file is accessed.
|
2886 | * @param filename A path to a file or directory. If a URL is provided, it must use the `file:` protocol.
|
2887 | */
|
2888 | export function watchFile(filename: PathLike, listener: (curr: Stats, prev: Stats) => void): StatWatcher;
|
2889 | /**
|
2890 | * Stop watching for changes on `filename`. If `listener` is specified, only that
|
2891 | * particular listener is removed. Otherwise, _all_ listeners are removed,
|
2892 | * effectively stopping watching of `filename`.
|
2893 | *
|
2894 | * Calling `fs.unwatchFile()` with a filename that is not being watched is a
|
2895 | * no-op, not an error.
|
2896 | *
|
2897 | * Using {@link watch} is more efficient than `fs.watchFile()` and`fs.unwatchFile()`. `fs.watch()` should be used instead of `fs.watchFile()`and `fs.unwatchFile()` when possible.
|
2898 | * @since v0.1.31
|
2899 | * @param listener Optional, a listener previously attached using `fs.watchFile()`
|
2900 | */
|
2901 | export function unwatchFile(filename: PathLike, listener?: (curr: Stats, prev: Stats) => void): void;
|
2902 | export interface WatchOptions extends Abortable {
|
2903 | encoding?: BufferEncoding | 'buffer' | undefined;
|
2904 | persistent?: boolean | undefined;
|
2905 | recursive?: boolean | undefined;
|
2906 | }
|
2907 | export type WatchEventType = 'rename' | 'change';
|
2908 | export type WatchListener<T> = (event: WatchEventType, filename: T) => void;
|
2909 | /**
|
2910 | * Watch for changes on `filename`, where `filename` is either a file or a
|
2911 | * directory.
|
2912 | *
|
2913 | * The second argument is optional. If `options` is provided as a string, it
|
2914 | * specifies the `encoding`. Otherwise `options` should be passed as an object.
|
2915 | *
|
2916 | * The listener callback gets two arguments `(eventType, filename)`. `eventType`is either `'rename'` or `'change'`, and `filename` is the name of the file
|
2917 | * which triggered the event.
|
2918 | *
|
2919 | * On most platforms, `'rename'` is emitted whenever a filename appears or
|
2920 | * disappears in the directory.
|
2921 | *
|
2922 | * The listener callback is attached to the `'change'` event fired by `fs.FSWatcher`, but it is not the same thing as the `'change'` value of`eventType`.
|
2923 | *
|
2924 | * If a `signal` is passed, aborting the corresponding AbortController will close
|
2925 | * the returned `fs.FSWatcher`.
|
2926 | * @since v0.5.10
|
2927 | * @param listener
|
2928 | */
|
2929 | export function watch(
|
2930 | filename: PathLike,
|
2931 | options:
|
2932 | | (WatchOptions & {
|
2933 | encoding: 'buffer';
|
2934 | })
|
2935 | | 'buffer',
|
2936 | listener?: WatchListener<Buffer>
|
2937 | ): FSWatcher;
|
2938 | /**
|
2939 | * Watch for changes on `filename`, where `filename` is either a file or a directory, returning an `FSWatcher`.
|
2940 | * @param filename A path to a file or directory. If a URL is provided, it must use the `file:` protocol.
|
2941 | * @param options Either the encoding for the filename provided to the listener, or an object optionally specifying encoding, persistent, and recursive options.
|
2942 | * If `encoding` is not supplied, the default of `'utf8'` is used.
|
2943 | * If `persistent` is not supplied, the default of `true` is used.
|
2944 | * If `recursive` is not supplied, the default of `false` is used.
|
2945 | */
|
2946 | export function watch(filename: PathLike, options?: WatchOptions | BufferEncoding | null, listener?: WatchListener<string>): FSWatcher;
|
2947 | /**
|
2948 | * Watch for changes on `filename`, where `filename` is either a file or a directory, returning an `FSWatcher`.
|
2949 | * @param filename A path to a file or directory. If a URL is provided, it must use the `file:` protocol.
|
2950 | * @param options Either the encoding for the filename provided to the listener, or an object optionally specifying encoding, persistent, and recursive options.
|
2951 | * If `encoding` is not supplied, the default of `'utf8'` is used.
|
2952 | * If `persistent` is not supplied, the default of `true` is used.
|
2953 | * If `recursive` is not supplied, the default of `false` is used.
|
2954 | */
|
2955 | export function watch(filename: PathLike, options: WatchOptions | string, listener?: WatchListener<string | Buffer>): FSWatcher;
|
2956 | /**
|
2957 | * Watch for changes on `filename`, where `filename` is either a file or a directory, returning an `FSWatcher`.
|
2958 | * @param filename A path to a file or directory. If a URL is provided, it must use the `file:` protocol.
|
2959 | */
|
2960 | export function watch(filename: PathLike, listener?: WatchListener<string>): FSWatcher;
|
2961 | /**
|
2962 | * Test whether or not the given path exists by checking with the file system.
|
2963 | * Then call the `callback` argument with either true or false:
|
2964 | *
|
2965 | * ```js
|
2966 | * import { exists } from 'fs';
|
2967 | *
|
2968 | * exists('/etc/passwd', (e) => {
|
2969 | * console.log(e ? 'it exists' : 'no passwd!');
|
2970 | * });
|
2971 | * ```
|
2972 | *
|
2973 | * **The parameters for this callback are not consistent with other Node.js**
|
2974 | * **callbacks.** Normally, the first parameter to a Node.js callback is an `err`parameter, optionally followed by other parameters. The `fs.exists()` callback
|
2975 | * has only one boolean parameter. This is one reason `fs.access()` is recommended
|
2976 | * instead of `fs.exists()`.
|
2977 | *
|
2978 | * Using `fs.exists()` to check for the existence of a file before calling`fs.open()`, `fs.readFile()` or `fs.writeFile()` is not recommended. Doing
|
2979 | * so introduces a race condition, since other processes may change the file's
|
2980 | * state between the two calls. Instead, user code should open/read/write the
|
2981 | * file directly and handle the error raised if the file does not exist.
|
2982 | *
|
2983 | * **write (NOT RECOMMENDED)**
|
2984 | *
|
2985 | * ```js
|
2986 | * import { exists, open, close } from 'fs';
|
2987 | *
|
2988 | * exists('myfile', (e) => {
|
2989 | * if (e) {
|
2990 | * console.error('myfile already exists');
|
2991 | * } else {
|
2992 | * open('myfile', 'wx', (err, fd) => {
|
2993 | * if (err) throw err;
|
2994 | *
|
2995 | * try {
|
2996 | * writeMyData(fd);
|
2997 | * } finally {
|
2998 | * close(fd, (err) => {
|
2999 | * if (err) throw err;
|
3000 | * });
|
3001 | * }
|
3002 | * });
|
3003 | * }
|
3004 | * });
|
3005 | * ```
|
3006 | *
|
3007 | * **write (RECOMMENDED)**
|
3008 | *
|
3009 | * ```js
|
3010 | * import { open, close } from 'fs';
|
3011 | * open('myfile', 'wx', (err, fd) => {
|
3012 | * if (err) {
|
3013 | * if (err.code === 'EEXIST') {
|
3014 | * console.error('myfile already exists');
|
3015 | * return;
|
3016 | * }
|
3017 | *
|
3018 | * throw err;
|
3019 | * }
|
3020 | *
|
3021 | * try {
|
3022 | * writeMyData(fd);
|
3023 | * } finally {
|
3024 | * close(fd, (err) => {
|
3025 | * if (err) throw err;
|
3026 | * });
|
3027 | * }
|
3028 | * });
|
3029 | * ```
|
3030 | *
|
3031 | * **read (NOT RECOMMENDED)**
|
3032 | *
|
3033 | * ```js
|
3034 | * import { open, close, exists } from 'fs';
|
3035 | *
|
3036 | * exists('myfile', (e) => {
|
3037 | * if (e) {
|
3038 | * open('myfile', 'r', (err, fd) => {
|
3039 | * if (err) throw err;
|
3040 | *
|
3041 | * try {
|
3042 | * readMyData(fd);
|
3043 | * } finally {
|
3044 | * close(fd, (err) => {
|
3045 | * if (err) throw err;
|
3046 | * });
|
3047 | * }
|
3048 | * });
|
3049 | * } else {
|
3050 | * console.error('myfile does not exist');
|
3051 | * }
|
3052 | * });
|
3053 | * ```
|
3054 | *
|
3055 | * **read (RECOMMENDED)**
|
3056 | *
|
3057 | * ```js
|
3058 | * import { open, close } from 'fs';
|
3059 | *
|
3060 | * open('myfile', 'r', (err, fd) => {
|
3061 | * if (err) {
|
3062 | * if (err.code === 'ENOENT') {
|
3063 | * console.error('myfile does not exist');
|
3064 | * return;
|
3065 | * }
|
3066 | *
|
3067 | * throw err;
|
3068 | * }
|
3069 | *
|
3070 | * try {
|
3071 | * readMyData(fd);
|
3072 | * } finally {
|
3073 | * close(fd, (err) => {
|
3074 | * if (err) throw err;
|
3075 | * });
|
3076 | * }
|
3077 | * });
|
3078 | * ```
|
3079 | *
|
3080 | * The "not recommended" examples above check for existence and then use the
|
3081 | * file; the "recommended" examples are better because they use the file directly
|
3082 | * and handle the error, if any.
|
3083 | *
|
3084 | * In general, check for the existence of a file only if the file won’t be
|
3085 | * used directly, for example when its existence is a signal from another
|
3086 | * process.
|
3087 | * @since v0.0.2
|
3088 | * @deprecated Since v1.0.0 - Use {@link stat} or {@link access} instead.
|
3089 | */
|
3090 | export function exists(path: PathLike, callback: (exists: boolean) => void): void;
|
3091 | /** @deprecated */
|
3092 | export namespace exists {
|
3093 | /**
|
3094 | * @param path A path to a file or directory. If a URL is provided, it must use the `file:` protocol.
|
3095 | * URL support is _experimental_.
|
3096 | */
|
3097 | function __promisify__(path: PathLike): Promise<boolean>;
|
3098 | }
|
3099 | /**
|
3100 | * Returns `true` if the path exists, `false` otherwise.
|
3101 | *
|
3102 | * For detailed information, see the documentation of the asynchronous version of
|
3103 | * this API: {@link exists}.
|
3104 | *
|
3105 | * `fs.exists()` is deprecated, but `fs.existsSync()` is not. The `callback`parameter to `fs.exists()` accepts parameters that are inconsistent with other
|
3106 | * Node.js callbacks. `fs.existsSync()` does not use a callback.
|
3107 | *
|
3108 | * ```js
|
3109 | * import { existsSync } from 'fs';
|
3110 | *
|
3111 | * if (existsSync('/etc/passwd'))
|
3112 | * console.log('The path exists.');
|
3113 | * ```
|
3114 | * @since v0.1.21
|
3115 | */
|
3116 | export function existsSync(path: PathLike): boolean;
|
3117 | export namespace constants {
|
3118 | // File Access Constants
|
3119 | /** Constant for fs.access(). File is visible to the calling process. */
|
3120 | const F_OK: number;
|
3121 | /** Constant for fs.access(). File can be read by the calling process. */
|
3122 | const R_OK: number;
|
3123 | /** Constant for fs.access(). File can be written by the calling process. */
|
3124 | const W_OK: number;
|
3125 | /** Constant for fs.access(). File can be executed by the calling process. */
|
3126 | const X_OK: number;
|
3127 | // File Copy Constants
|
3128 | /** Constant for fs.copyFile. Flag indicating the destination file should not be overwritten if it already exists. */
|
3129 | const COPYFILE_EXCL: number;
|
3130 | /**
|
3131 | * Constant for fs.copyFile. copy operation will attempt to create a copy-on-write reflink.
|
3132 | * If the underlying platform does not support copy-on-write, then a fallback copy mechanism is used.
|
3133 | */
|
3134 | const COPYFILE_FICLONE: number;
|
3135 | /**
|
3136 | * Constant for fs.copyFile. Copy operation will attempt to create a copy-on-write reflink.
|
3137 | * If the underlying platform does not support copy-on-write, then the operation will fail with an error.
|
3138 | */
|
3139 | const COPYFILE_FICLONE_FORCE: number;
|
3140 | // File Open Constants
|
3141 | /** Constant for fs.open(). Flag indicating to open a file for read-only access. */
|
3142 | const O_RDONLY: number;
|
3143 | /** Constant for fs.open(). Flag indicating to open a file for write-only access. */
|
3144 | const O_WRONLY: number;
|
3145 | /** Constant for fs.open(). Flag indicating to open a file for read-write access. */
|
3146 | const O_RDWR: number;
|
3147 | /** Constant for fs.open(). Flag indicating to create the file if it does not already exist. */
|
3148 | const O_CREAT: number;
|
3149 | /** Constant for fs.open(). Flag indicating that opening a file should fail if the O_CREAT flag is set and the file already exists. */
|
3150 | const O_EXCL: number;
|
3151 | /**
|
3152 | * Constant for fs.open(). Flag indicating that if path identifies a terminal device,
|
3153 | * opening the path shall not cause that terminal to become the controlling terminal for the process
|
3154 | * (if the process does not already have one).
|
3155 | */
|
3156 | const O_NOCTTY: number;
|
3157 | /** Constant for fs.open(). Flag indicating that if the file exists and is a regular file, and the file is opened successfully for write access, its length shall be truncated to zero. */
|
3158 | const O_TRUNC: number;
|
3159 | /** Constant for fs.open(). Flag indicating that data will be appended to the end of the file. */
|
3160 | const O_APPEND: number;
|
3161 | /** Constant for fs.open(). Flag indicating that the open should fail if the path is not a directory. */
|
3162 | const O_DIRECTORY: number;
|
3163 | /**
|
3164 | * constant for fs.open().
|
3165 | * Flag indicating reading accesses to the file system will no longer result in
|
3166 | * an update to the atime information associated with the file.
|
3167 | * This flag is available on Linux operating systems only.
|
3168 | */
|
3169 | const O_NOATIME: number;
|
3170 | /** Constant for fs.open(). Flag indicating that the open should fail if the path is a symbolic link. */
|
3171 | const O_NOFOLLOW: number;
|
3172 | /** Constant for fs.open(). Flag indicating that the file is opened for synchronous I/O. */
|
3173 | const O_SYNC: number;
|
3174 | /** Constant for fs.open(). Flag indicating that the file is opened for synchronous I/O with write operations waiting for data integrity. */
|
3175 | const O_DSYNC: number;
|
3176 | /** Constant for fs.open(). Flag indicating to open the symbolic link itself rather than the resource it is pointing to. */
|
3177 | const O_SYMLINK: number;
|
3178 | /** Constant for fs.open(). When set, an attempt will be made to minimize caching effects of file I/O. */
|
3179 | const O_DIRECT: number;
|
3180 | /** Constant for fs.open(). Flag indicating to open the file in nonblocking mode when possible. */
|
3181 | const O_NONBLOCK: number;
|
3182 | // File Type Constants
|
3183 | /** Constant for fs.Stats mode property for determining a file's type. Bit mask used to extract the file type code. */
|
3184 | const S_IFMT: number;
|
3185 | /** Constant for fs.Stats mode property for determining a file's type. File type constant for a regular file. */
|
3186 | const S_IFREG: number;
|
3187 | /** Constant for fs.Stats mode property for determining a file's type. File type constant for a directory. */
|
3188 | const S_IFDIR: number;
|
3189 | /** Constant for fs.Stats mode property for determining a file's type. File type constant for a character-oriented device file. */
|
3190 | const S_IFCHR: number;
|
3191 | /** Constant for fs.Stats mode property for determining a file's type. File type constant for a block-oriented device file. */
|
3192 | const S_IFBLK: number;
|
3193 | /** Constant for fs.Stats mode property for determining a file's type. File type constant for a FIFO/pipe. */
|
3194 | const S_IFIFO: number;
|
3195 | /** Constant for fs.Stats mode property for determining a file's type. File type constant for a symbolic link. */
|
3196 | const S_IFLNK: number;
|
3197 | /** Constant for fs.Stats mode property for determining a file's type. File type constant for a socket. */
|
3198 | const S_IFSOCK: number;
|
3199 | // File Mode Constants
|
3200 | /** Constant for fs.Stats mode property for determining access permissions for a file. File mode indicating readable, writable and executable by owner. */
|
3201 | const S_IRWXU: number;
|
3202 | /** Constant for fs.Stats mode property for determining access permissions for a file. File mode indicating readable by owner. */
|
3203 | const S_IRUSR: number;
|
3204 | /** Constant for fs.Stats mode property for determining access permissions for a file. File mode indicating writable by owner. */
|
3205 | const S_IWUSR: number;
|
3206 | /** Constant for fs.Stats mode property for determining access permissions for a file. File mode indicating executable by owner. */
|
3207 | const S_IXUSR: number;
|
3208 | /** Constant for fs.Stats mode property for determining access permissions for a file. File mode indicating readable, writable and executable by group. */
|
3209 | const S_IRWXG: number;
|
3210 | /** Constant for fs.Stats mode property for determining access permissions for a file. File mode indicating readable by group. */
|
3211 | const S_IRGRP: number;
|
3212 | /** Constant for fs.Stats mode property for determining access permissions for a file. File mode indicating writable by group. */
|
3213 | const S_IWGRP: number;
|
3214 | /** Constant for fs.Stats mode property for determining access permissions for a file. File mode indicating executable by group. */
|
3215 | const S_IXGRP: number;
|
3216 | /** Constant for fs.Stats mode property for determining access permissions for a file. File mode indicating readable, writable and executable by others. */
|
3217 | const S_IRWXO: number;
|
3218 | /** Constant for fs.Stats mode property for determining access permissions for a file. File mode indicating readable by others. */
|
3219 | const S_IROTH: number;
|
3220 | /** Constant for fs.Stats mode property for determining access permissions for a file. File mode indicating writable by others. */
|
3221 | const S_IWOTH: number;
|
3222 | /** Constant for fs.Stats mode property for determining access permissions for a file. File mode indicating executable by others. */
|
3223 | const S_IXOTH: number;
|
3224 | /**
|
3225 | * When set, a memory file mapping is used to access the file. This flag
|
3226 | * is available on Windows operating systems only. On other operating systems,
|
3227 | * this flag is ignored.
|
3228 | */
|
3229 | const UV_FS_O_FILEMAP: number;
|
3230 | }
|
3231 | /**
|
3232 | * Tests a user's permissions for the file or directory specified by `path`.
|
3233 | * The `mode` argument is an optional integer that specifies the accessibility
|
3234 | * checks to be performed. Check `File access constants` for possible values
|
3235 | * of `mode`. It is possible to create a mask consisting of the bitwise OR of
|
3236 | * two or more values (e.g. `fs.constants.W_OK | fs.constants.R_OK`).
|
3237 | *
|
3238 | * The final argument, `callback`, is a callback function that is invoked with
|
3239 | * a possible error argument. If any of the accessibility checks fail, the error
|
3240 | * argument will be an `Error` object. The following examples check if`package.json` exists, and if it is readable or writable.
|
3241 | *
|
3242 | * ```js
|
3243 | * import { access, constants } from 'fs';
|
3244 | *
|
3245 | * const file = 'package.json';
|
3246 | *
|
3247 | * // Check if the file exists in the current directory.
|
3248 | * access(file, constants.F_OK, (err) => {
|
3249 | * console.log(`${file} ${err ? 'does not exist' : 'exists'}`);
|
3250 | * });
|
3251 | *
|
3252 | * // Check if the file is readable.
|
3253 | * access(file, constants.R_OK, (err) => {
|
3254 | * console.log(`${file} ${err ? 'is not readable' : 'is readable'}`);
|
3255 | * });
|
3256 | *
|
3257 | * // Check if the file is writable.
|
3258 | * access(file, constants.W_OK, (err) => {
|
3259 | * console.log(`${file} ${err ? 'is not writable' : 'is writable'}`);
|
3260 | * });
|
3261 | *
|
3262 | * // Check if the file exists in the current directory, and if it is writable.
|
3263 | * access(file, constants.F_OK | constants.W_OK, (err) => {
|
3264 | * if (err) {
|
3265 | * console.error(
|
3266 | * `${file} ${err.code === 'ENOENT' ? 'does not exist' : 'is read-only'}`);
|
3267 | * } else {
|
3268 | * console.log(`${file} exists, and it is writable`);
|
3269 | * }
|
3270 | * });
|
3271 | * ```
|
3272 | *
|
3273 | * Do not use `fs.access()` to check for the accessibility of a file before calling`fs.open()`, `fs.readFile()` or `fs.writeFile()`. Doing
|
3274 | * so introduces a race condition, since other processes may change the file's
|
3275 | * state between the two calls. Instead, user code should open/read/write the
|
3276 | * file directly and handle the error raised if the file is not accessible.
|
3277 | *
|
3278 | * **write (NOT RECOMMENDED)**
|
3279 | *
|
3280 | * ```js
|
3281 | * import { access, open, close } from 'fs';
|
3282 | *
|
3283 | * access('myfile', (err) => {
|
3284 | * if (!err) {
|
3285 | * console.error('myfile already exists');
|
3286 | * return;
|
3287 | * }
|
3288 | *
|
3289 | * open('myfile', 'wx', (err, fd) => {
|
3290 | * if (err) throw err;
|
3291 | *
|
3292 | * try {
|
3293 | * writeMyData(fd);
|
3294 | * } finally {
|
3295 | * close(fd, (err) => {
|
3296 | * if (err) throw err;
|
3297 | * });
|
3298 | * }
|
3299 | * });
|
3300 | * });
|
3301 | * ```
|
3302 | *
|
3303 | * **write (RECOMMENDED)**
|
3304 | *
|
3305 | * ```js
|
3306 | * import { open, close } from 'fs';
|
3307 | *
|
3308 | * open('myfile', 'wx', (err, fd) => {
|
3309 | * if (err) {
|
3310 | * if (err.code === 'EEXIST') {
|
3311 | * console.error('myfile already exists');
|
3312 | * return;
|
3313 | * }
|
3314 | *
|
3315 | * throw err;
|
3316 | * }
|
3317 | *
|
3318 | * try {
|
3319 | * writeMyData(fd);
|
3320 | * } finally {
|
3321 | * close(fd, (err) => {
|
3322 | * if (err) throw err;
|
3323 | * });
|
3324 | * }
|
3325 | * });
|
3326 | * ```
|
3327 | *
|
3328 | * **read (NOT RECOMMENDED)**
|
3329 | *
|
3330 | * ```js
|
3331 | * import { access, open, close } from 'fs';
|
3332 | * access('myfile', (err) => {
|
3333 | * if (err) {
|
3334 | * if (err.code === 'ENOENT') {
|
3335 | * console.error('myfile does not exist');
|
3336 | * return;
|
3337 | * }
|
3338 | *
|
3339 | * throw err;
|
3340 | * }
|
3341 | *
|
3342 | * open('myfile', 'r', (err, fd) => {
|
3343 | * if (err) throw err;
|
3344 | *
|
3345 | * try {
|
3346 | * readMyData(fd);
|
3347 | * } finally {
|
3348 | * close(fd, (err) => {
|
3349 | * if (err) throw err;
|
3350 | * });
|
3351 | * }
|
3352 | * });
|
3353 | * });
|
3354 | * ```
|
3355 | *
|
3356 | * **read (RECOMMENDED)**
|
3357 | *
|
3358 | * ```js
|
3359 | * import { open, close } from 'fs';
|
3360 | *
|
3361 | * open('myfile', 'r', (err, fd) => {
|
3362 | * if (err) {
|
3363 | * if (err.code === 'ENOENT') {
|
3364 | * console.error('myfile does not exist');
|
3365 | * return;
|
3366 | * }
|
3367 | *
|
3368 | * throw err;
|
3369 | * }
|
3370 | *
|
3371 | * try {
|
3372 | * readMyData(fd);
|
3373 | * } finally {
|
3374 | * close(fd, (err) => {
|
3375 | * if (err) throw err;
|
3376 | * });
|
3377 | * }
|
3378 | * });
|
3379 | * ```
|
3380 | *
|
3381 | * The "not recommended" examples above check for accessibility and then use the
|
3382 | * file; the "recommended" examples are better because they use the file directly
|
3383 | * and handle the error, if any.
|
3384 | *
|
3385 | * In general, check for the accessibility of a file only if the file will not be
|
3386 | * used directly, for example when its accessibility is a signal from another
|
3387 | * process.
|
3388 | *
|
3389 | * On Windows, access-control policies (ACLs) on a directory may limit access to
|
3390 | * a file or directory. The `fs.access()` function, however, does not check the
|
3391 | * ACL and therefore may report that a path is accessible even if the ACL restricts
|
3392 | * the user from reading or writing to it.
|
3393 | * @since v0.11.15
|
3394 | * @param [mode=fs.constants.F_OK]
|
3395 | */
|
3396 | export function access(path: PathLike, mode: number | undefined, callback: NoParamCallback): void;
|
3397 | /**
|
3398 | * Asynchronously tests a user's permissions for the file specified by path.
|
3399 | * @param path A path to a file or directory. If a URL is provided, it must use the `file:` protocol.
|
3400 | */
|
3401 | export function access(path: PathLike, callback: NoParamCallback): void;
|
3402 | export namespace access {
|
3403 | /**
|
3404 | * Asynchronously tests a user's permissions for the file specified by path.
|
3405 | * @param path A path to a file or directory. If a URL is provided, it must use the `file:` protocol.
|
3406 | * URL support is _experimental_.
|
3407 | */
|
3408 | function __promisify__(path: PathLike, mode?: number): Promise<void>;
|
3409 | }
|
3410 | /**
|
3411 | * Synchronously tests a user's permissions for the file or directory specified
|
3412 | * by `path`. The `mode` argument is an optional integer that specifies the
|
3413 | * accessibility checks to be performed. Check `File access constants` for
|
3414 | * possible values of `mode`. It is possible to create a mask consisting of
|
3415 | * the bitwise OR of two or more values
|
3416 | * (e.g. `fs.constants.W_OK | fs.constants.R_OK`).
|
3417 | *
|
3418 | * If any of the accessibility checks fail, an `Error` will be thrown. Otherwise,
|
3419 | * the method will return `undefined`.
|
3420 | *
|
3421 | * ```js
|
3422 | * import { accessSync, constants } from 'fs';
|
3423 | *
|
3424 | * try {
|
3425 | * accessSync('etc/passwd', constants.R_OK | constants.W_OK);
|
3426 | * console.log('can read/write');
|
3427 | * } catch (err) {
|
3428 | * console.error('no access!');
|
3429 | * }
|
3430 | * ```
|
3431 | * @since v0.11.15
|
3432 | * @param [mode=fs.constants.F_OK]
|
3433 | */
|
3434 | export function accessSync(path: PathLike, mode?: number): void;
|
3435 | interface StreamOptions {
|
3436 | flags?: string | undefined;
|
3437 | encoding?: BufferEncoding | undefined;
|
3438 | fd?: number | promises.FileHandle | undefined;
|
3439 | mode?: number | undefined;
|
3440 | autoClose?: boolean | undefined;
|
3441 | /**
|
3442 | * @default false
|
3443 | */
|
3444 | emitClose?: boolean | undefined;
|
3445 | start?: number | undefined;
|
3446 | highWaterMark?: number | undefined;
|
3447 | }
|
3448 | interface ReadStreamOptions extends StreamOptions {
|
3449 | end?: number | undefined;
|
3450 | }
|
3451 | /**
|
3452 | * Unlike the 16 kb default `highWaterMark` for a `stream.Readable`, the stream
|
3453 | * returned by this method has a default `highWaterMark` of 64 kb.
|
3454 | *
|
3455 | * `options` can include `start` and `end` values to read a range of bytes from
|
3456 | * the file instead of the entire file. Both `start` and `end` are inclusive and
|
3457 | * start counting at 0, allowed values are in the
|
3458 | * \[0, [`Number.MAX_SAFE_INTEGER`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER)\] range. If `fd` is specified and `start` is
|
3459 | * omitted or `undefined`, `fs.createReadStream()` reads sequentially from the
|
3460 | * current file position. The `encoding` can be any one of those accepted by `Buffer`.
|
3461 | *
|
3462 | * If `fd` is specified, `ReadStream` will ignore the `path` argument and will use
|
3463 | * the specified file descriptor. This means that no `'open'` event will be
|
3464 | * emitted. `fd` should be blocking; non-blocking `fd`s should be passed to `net.Socket`.
|
3465 | *
|
3466 | * If `fd` points to a character device that only supports blocking reads
|
3467 | * (such as keyboard or sound card), read operations do not finish until data is
|
3468 | * available. This can prevent the process from exiting and the stream from
|
3469 | * closing naturally.
|
3470 | *
|
3471 | * By default, the stream will emit a `'close'` event after it has been
|
3472 | * destroyed. Set the `emitClose` option to `false` to change this behavior.
|
3473 | *
|
3474 | * By providing the `fs` option, it is possible to override the corresponding `fs`implementations for `open`, `read`, and `close`. When providing the `fs` option,
|
3475 | * an override for `read` is required. If no `fd` is provided, an override for`open` is also required. If `autoClose` is `true`, an override for `close` is
|
3476 | * also required.
|
3477 | *
|
3478 | * ```js
|
3479 | * import { createReadStream } from 'fs';
|
3480 | *
|
3481 | * // Create a stream from some character device.
|
3482 | * const stream = createReadStream('/dev/input/event0');
|
3483 | * setTimeout(() => {
|
3484 | * stream.close(); // This may not close the stream.
|
3485 | * // Artificially marking end-of-stream, as if the underlying resource had
|
3486 | * // indicated end-of-file by itself, allows the stream to close.
|
3487 | * // This does not cancel pending read operations, and if there is such an
|
3488 | * // operation, the process may still not be able to exit successfully
|
3489 | * // until it finishes.
|
3490 | * stream.push(null);
|
3491 | * stream.read(0);
|
3492 | * }, 100);
|
3493 | * ```
|
3494 | *
|
3495 | * If `autoClose` is false, then the file descriptor won't be closed, even if
|
3496 | * there's an error. It is the application's responsibility to close it and make
|
3497 | * sure there's no file descriptor leak. If `autoClose` is set to true (default
|
3498 | * behavior), on `'error'` or `'end'` the file descriptor will be closed
|
3499 | * automatically.
|
3500 | *
|
3501 | * `mode` sets the file mode (permission and sticky bits), but only if the
|
3502 | * file was created.
|
3503 | *
|
3504 | * An example to read the last 10 bytes of a file which is 100 bytes long:
|
3505 | *
|
3506 | * ```js
|
3507 | * import { createReadStream } from 'fs';
|
3508 | *
|
3509 | * createReadStream('sample.txt', { start: 90, end: 99 });
|
3510 | * ```
|
3511 | *
|
3512 | * If `options` is a string, then it specifies the encoding.
|
3513 | * @since v0.1.31
|
3514 | */
|
3515 | export function createReadStream(path: PathLike, options?: BufferEncoding | ReadStreamOptions): ReadStream;
|
3516 | /**
|
3517 | * `options` may also include a `start` option to allow writing data at some
|
3518 | * position past the beginning of the file, allowed values are in the
|
3519 | * \[0, [`Number.MAX_SAFE_INTEGER`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER)\] range. Modifying a file rather than replacing
|
3520 | * it may require the `flags` option to be set to `r+` rather than the default `w`.
|
3521 | * The `encoding` can be any one of those accepted by `Buffer`.
|
3522 | *
|
3523 | * If `autoClose` is set to true (default behavior) on `'error'` or `'finish'`the file descriptor will be closed automatically. If `autoClose` is false,
|
3524 | * then the file descriptor won't be closed, even if there's an error.
|
3525 | * It is the application's responsibility to close it and make sure there's no
|
3526 | * file descriptor leak.
|
3527 | *
|
3528 | * By default, the stream will emit a `'close'` event after it has been
|
3529 | * destroyed. Set the `emitClose` option to `false` to change this behavior.
|
3530 | *
|
3531 | * By providing the `fs` option it is possible to override the corresponding `fs`implementations for `open`, `write`, `writev` and `close`. Overriding `write()`without `writev()` can reduce
|
3532 | * performance as some optimizations (`_writev()`)
|
3533 | * will be disabled. When providing the `fs` option, overrides for at least one of`write` and `writev` are required. If no `fd` option is supplied, an override
|
3534 | * for `open` is also required. If `autoClose` is `true`, an override for `close`is also required.
|
3535 | *
|
3536 | * Like `fs.ReadStream`, if `fd` is specified, `fs.WriteStream` will ignore the`path` argument and will use the specified file descriptor. This means that no`'open'` event will be
|
3537 | * emitted. `fd` should be blocking; non-blocking `fd`s
|
3538 | * should be passed to `net.Socket`.
|
3539 | *
|
3540 | * If `options` is a string, then it specifies the encoding.
|
3541 | * @since v0.1.31
|
3542 | */
|
3543 | export function createWriteStream(path: PathLike, options?: BufferEncoding | StreamOptions): WriteStream;
|
3544 | /**
|
3545 | * Forces all currently queued I/O operations associated with the file to the
|
3546 | * 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. No arguments other
|
3547 | * than a possible
|
3548 | * exception are given to the completion callback.
|
3549 | * @since v0.1.96
|
3550 | */
|
3551 | export function fdatasync(fd: number, callback: NoParamCallback): void;
|
3552 | export namespace fdatasync {
|
3553 | /**
|
3554 | * Asynchronous fdatasync(2) - synchronize a file's in-core state with storage device.
|
3555 | * @param fd A file descriptor.
|
3556 | */
|
3557 | function __promisify__(fd: number): Promise<void>;
|
3558 | }
|
3559 | /**
|
3560 | * Forces all currently queued I/O operations associated with the file to the
|
3561 | * 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. Returns `undefined`.
|
3562 | * @since v0.1.96
|
3563 | */
|
3564 | export function fdatasyncSync(fd: number): void;
|
3565 | /**
|
3566 | * Asynchronously copies `src` to `dest`. By default, `dest` is overwritten if it
|
3567 | * already exists. No arguments other than a possible exception are given to the
|
3568 | * callback function. Node.js makes no guarantees about the atomicity of the copy
|
3569 | * operation. If an error occurs after the destination file has been opened for
|
3570 | * writing, Node.js will attempt to remove the destination.
|
3571 | *
|
3572 | * `mode` is an optional integer that specifies the behavior
|
3573 | * of the copy operation. It is possible to create a mask consisting of the bitwise
|
3574 | * OR of two or more values (e.g.`fs.constants.COPYFILE_EXCL | fs.constants.COPYFILE_FICLONE`).
|
3575 | *
|
3576 | * * `fs.constants.COPYFILE_EXCL`: The copy operation will fail if `dest` already
|
3577 | * exists.
|
3578 | * * `fs.constants.COPYFILE_FICLONE`: The copy operation will attempt to create a
|
3579 | * copy-on-write reflink. If the platform does not support copy-on-write, then a
|
3580 | * fallback copy mechanism is used.
|
3581 | * * `fs.constants.COPYFILE_FICLONE_FORCE`: The copy operation will attempt to
|
3582 | * create a copy-on-write reflink. If the platform does not support
|
3583 | * copy-on-write, then the operation will fail.
|
3584 | *
|
3585 | * ```js
|
3586 | * import { copyFile, constants } from 'fs';
|
3587 | *
|
3588 | * function callback(err) {
|
3589 | * if (err) throw err;
|
3590 | * console.log('source.txt was copied to destination.txt');
|
3591 | * }
|
3592 | *
|
3593 | * // destination.txt will be created or overwritten by default.
|
3594 | * copyFile('source.txt', 'destination.txt', callback);
|
3595 | *
|
3596 | * // By using COPYFILE_EXCL, the operation will fail if destination.txt exists.
|
3597 | * copyFile('source.txt', 'destination.txt', constants.COPYFILE_EXCL, callback);
|
3598 | * ```
|
3599 | * @since v8.5.0
|
3600 | * @param src source filename to copy
|
3601 | * @param dest destination filename of the copy operation
|
3602 | * @param [mode=0] modifiers for copy operation.
|
3603 | */
|
3604 | export function copyFile(src: PathLike, dest: PathLike, callback: NoParamCallback): void;
|
3605 | export function copyFile(src: PathLike, dest: PathLike, mode: number, callback: NoParamCallback): void;
|
3606 | export namespace copyFile {
|
3607 | function __promisify__(src: PathLike, dst: PathLike, mode?: number): Promise<void>;
|
3608 | }
|
3609 | /**
|
3610 | * Synchronously copies `src` to `dest`. By default, `dest` is overwritten if it
|
3611 | * already exists. Returns `undefined`. Node.js makes no guarantees about the
|
3612 | * atomicity of the copy operation. If an error occurs after the destination file
|
3613 | * has been opened for writing, Node.js will attempt to remove the destination.
|
3614 | *
|
3615 | * `mode` is an optional integer that specifies the behavior
|
3616 | * of the copy operation. It is possible to create a mask consisting of the bitwise
|
3617 | * OR of two or more values (e.g.`fs.constants.COPYFILE_EXCL | fs.constants.COPYFILE_FICLONE`).
|
3618 | *
|
3619 | * * `fs.constants.COPYFILE_EXCL`: The copy operation will fail if `dest` already
|
3620 | * exists.
|
3621 | * * `fs.constants.COPYFILE_FICLONE`: The copy operation will attempt to create a
|
3622 | * copy-on-write reflink. If the platform does not support copy-on-write, then a
|
3623 | * fallback copy mechanism is used.
|
3624 | * * `fs.constants.COPYFILE_FICLONE_FORCE`: The copy operation will attempt to
|
3625 | * create a copy-on-write reflink. If the platform does not support
|
3626 | * copy-on-write, then the operation will fail.
|
3627 | *
|
3628 | * ```js
|
3629 | * import { copyFileSync, constants } from 'fs';
|
3630 | *
|
3631 | * // destination.txt will be created or overwritten by default.
|
3632 | * copyFileSync('source.txt', 'destination.txt');
|
3633 | * console.log('source.txt was copied to destination.txt');
|
3634 | *
|
3635 | * // By using COPYFILE_EXCL, the operation will fail if destination.txt exists.
|
3636 | * copyFileSync('source.txt', 'destination.txt', constants.COPYFILE_EXCL);
|
3637 | * ```
|
3638 | * @since v8.5.0
|
3639 | * @param src source filename to copy
|
3640 | * @param dest destination filename of the copy operation
|
3641 | * @param [mode=0] modifiers for copy operation.
|
3642 | */
|
3643 | export function copyFileSync(src: PathLike, dest: PathLike, mode?: number): void;
|
3644 | /**
|
3645 | * Write an array of `ArrayBufferView`s to the file specified by `fd` using`writev()`.
|
3646 | *
|
3647 | * `position` is the offset from the beginning of the file where this data
|
3648 | * should be written. If `typeof position !== 'number'`, the data will be written
|
3649 | * at the current position.
|
3650 | *
|
3651 | * The callback will be given three arguments: `err`, `bytesWritten`, and`buffers`. `bytesWritten` is how many bytes were written from `buffers`.
|
3652 | *
|
3653 | * If this method is `util.promisify()` ed, it returns a promise for an`Object` with `bytesWritten` and `buffers` properties.
|
3654 | *
|
3655 | * It is unsafe to use `fs.writev()` multiple times on the same file without
|
3656 | * waiting for the callback. For this scenario, use {@link createWriteStream}.
|
3657 | *
|
3658 | * On Linux, positional writes don't work when the file is opened in append mode.
|
3659 | * The kernel ignores the position argument and always appends the data to
|
3660 | * the end of the file.
|
3661 | * @since v12.9.0
|
3662 | */
|
3663 | export function writev(fd: number, buffers: ReadonlyArray<NodeJS.ArrayBufferView>, cb: (err: NodeJS.ErrnoException | null, bytesWritten: number, buffers: NodeJS.ArrayBufferView[]) => void): void;
|
3664 | export function writev(
|
3665 | fd: number,
|
3666 | buffers: ReadonlyArray<NodeJS.ArrayBufferView>,
|
3667 | position: number,
|
3668 | cb: (err: NodeJS.ErrnoException | null, bytesWritten: number, buffers: NodeJS.ArrayBufferView[]) => void
|
3669 | ): void;
|
3670 | export interface WriteVResult {
|
3671 | bytesWritten: number;
|
3672 | buffers: NodeJS.ArrayBufferView[];
|
3673 | }
|
3674 | export namespace writev {
|
3675 | function __promisify__(fd: number, buffers: ReadonlyArray<NodeJS.ArrayBufferView>, position?: number): Promise<WriteVResult>;
|
3676 | }
|
3677 | /**
|
3678 | * For detailed information, see the documentation of the asynchronous version of
|
3679 | * this API: {@link writev}.
|
3680 | * @since v12.9.0
|
3681 | * @return The number of bytes written.
|
3682 | */
|
3683 | export function writevSync(fd: number, buffers: ReadonlyArray<NodeJS.ArrayBufferView>, position?: number): number;
|
3684 | /**
|
3685 | * Read from a file specified by `fd` and write to an array of `ArrayBufferView`s
|
3686 | * using `readv()`.
|
3687 | *
|
3688 | * `position` is the offset from the beginning of the file from where data
|
3689 | * should be read. If `typeof position !== 'number'`, the data will be read
|
3690 | * from the current position.
|
3691 | *
|
3692 | * The callback will be given three arguments: `err`, `bytesRead`, and`buffers`. `bytesRead` is how many bytes were read from the file.
|
3693 | *
|
3694 | * If this method is invoked as its `util.promisify()` ed version, it returns
|
3695 | * a promise for an `Object` with `bytesRead` and `buffers` properties.
|
3696 | * @since v13.13.0, v12.17.0
|
3697 | */
|
3698 | export function readv(fd: number, buffers: ReadonlyArray<NodeJS.ArrayBufferView>, cb: (err: NodeJS.ErrnoException | null, bytesRead: number, buffers: NodeJS.ArrayBufferView[]) => void): void;
|
3699 | export function readv(
|
3700 | fd: number,
|
3701 | buffers: ReadonlyArray<NodeJS.ArrayBufferView>,
|
3702 | position: number,
|
3703 | cb: (err: NodeJS.ErrnoException | null, bytesRead: number, buffers: NodeJS.ArrayBufferView[]) => void
|
3704 | ): void;
|
3705 | export interface ReadVResult {
|
3706 | bytesRead: number;
|
3707 | buffers: NodeJS.ArrayBufferView[];
|
3708 | }
|
3709 | export namespace readv {
|
3710 | function __promisify__(fd: number, buffers: ReadonlyArray<NodeJS.ArrayBufferView>, position?: number): Promise<ReadVResult>;
|
3711 | }
|
3712 | /**
|
3713 | * For detailed information, see the documentation of the asynchronous version of
|
3714 | * this API: {@link readv}.
|
3715 | * @since v13.13.0, v12.17.0
|
3716 | * @return The number of bytes read.
|
3717 | */
|
3718 | export function readvSync(fd: number, buffers: ReadonlyArray<NodeJS.ArrayBufferView>, position?: number): number;
|
3719 | export interface OpenDirOptions {
|
3720 | encoding?: BufferEncoding | undefined;
|
3721 | /**
|
3722 | * Number of directory entries that are buffered
|
3723 | * internally when reading from the directory. Higher values lead to better
|
3724 | * performance but higher memory usage.
|
3725 | * @default 32
|
3726 | */
|
3727 | bufferSize?: number | undefined;
|
3728 | }
|
3729 | /**
|
3730 | * Synchronously open a directory. See [`opendir(3)`](http://man7.org/linux/man-pages/man3/opendir.3.html).
|
3731 | *
|
3732 | * Creates an `fs.Dir`, which contains all further functions for reading from
|
3733 | * and cleaning up the directory.
|
3734 | *
|
3735 | * The `encoding` option sets the encoding for the `path` while opening the
|
3736 | * directory and subsequent read operations.
|
3737 | * @since v12.12.0
|
3738 | */
|
3739 | export function opendirSync(path: PathLike, options?: OpenDirOptions): Dir;
|
3740 | /**
|
3741 | * Asynchronously open a directory. See the POSIX [`opendir(3)`](http://man7.org/linux/man-pages/man3/opendir.3.html) documentation for
|
3742 | * more details.
|
3743 | *
|
3744 | * Creates an `fs.Dir`, which contains all further functions for reading from
|
3745 | * and cleaning up the directory.
|
3746 | *
|
3747 | * The `encoding` option sets the encoding for the `path` while opening the
|
3748 | * directory and subsequent read operations.
|
3749 | * @since v12.12.0
|
3750 | */
|
3751 | export function opendir(path: PathLike, cb: (err: NodeJS.ErrnoException | null, dir: Dir) => void): void;
|
3752 | export function opendir(path: PathLike, options: OpenDirOptions, cb: (err: NodeJS.ErrnoException | null, dir: Dir) => void): void;
|
3753 | export namespace opendir {
|
3754 | function __promisify__(path: PathLike, options?: OpenDirOptions): Promise<Dir>;
|
3755 | }
|
3756 | export interface BigIntStats extends StatsBase<bigint> {
|
3757 | atimeNs: bigint;
|
3758 | mtimeNs: bigint;
|
3759 | ctimeNs: bigint;
|
3760 | birthtimeNs: bigint;
|
3761 | }
|
3762 | export interface BigIntOptions {
|
3763 | bigint: true;
|
3764 | }
|
3765 | export interface StatOptions {
|
3766 | bigint?: boolean | undefined;
|
3767 | }
|
3768 | export interface StatSyncOptions extends StatOptions {
|
3769 | throwIfNoEntry?: boolean | undefined;
|
3770 | }
|
3771 | export interface CopyOptions {
|
3772 | /**
|
3773 | * Dereference symlinks
|
3774 | * @default false
|
3775 | */
|
3776 | dereference?: boolean;
|
3777 | /**
|
3778 | * When `force` is `false`, and the destination
|
3779 | * exists, throw an error.
|
3780 | * @default false
|
3781 | */
|
3782 | errorOnExist?: boolean;
|
3783 | /**
|
3784 | * Function to filter copied files/directories. Return
|
3785 | * `true` to copy the item, `false` to ignore it.
|
3786 | */
|
3787 | filter?(source: string, destination: string): boolean;
|
3788 | /**
|
3789 | * Overwrite existing file or directory. _The copy
|
3790 | * operation will ignore errors if you set this to false and the destination
|
3791 | * exists. Use the `errorOnExist` option to change this behavior.
|
3792 | * @default true
|
3793 | */
|
3794 | force?: boolean;
|
3795 | /**
|
3796 | * When `true` timestamps from `src` will
|
3797 | * be preserved.
|
3798 | * @default false
|
3799 | */
|
3800 | preserveTimestamps?: boolean;
|
3801 | /**
|
3802 | * Copy directories recursively.
|
3803 | * @default false
|
3804 | */
|
3805 | recursive?: boolean;
|
3806 | }
|
3807 | /**
|
3808 | * Asynchronously copies the entire directory structure from `src` to `dest`,
|
3809 | * including subdirectories and files.
|
3810 | *
|
3811 | * When copying a directory to another directory, globs are not supported and
|
3812 | * behavior is similar to `cp dir1/ dir2/`.
|
3813 | * @since v16.7.0
|
3814 | * @experimental
|
3815 | * @param src source path to copy.
|
3816 | * @param dest destination path to copy to.
|
3817 | */
|
3818 | export function cp(source: string, destination: string, callback: (err: NodeJS.ErrnoException | null) => void): void;
|
3819 | export function cp(source: string, destination: string, opts: CopyOptions, callback: (err: NodeJS.ErrnoException | null) => void): void;
|
3820 | /**
|
3821 | * Synchronously copies the entire directory structure from `src` to `dest`,
|
3822 | * including subdirectories and files.
|
3823 | *
|
3824 | * When copying a directory to another directory, globs are not supported and
|
3825 | * behavior is similar to `cp dir1/ dir2/`.
|
3826 | * @since v16.7.0
|
3827 | * @experimental
|
3828 | * @param src source path to copy.
|
3829 | * @param dest destination path to copy to.
|
3830 | */
|
3831 | export function cpSync(source: string, destination: string, opts?: CopyOptions): void;
|
3832 | }
|
3833 | declare module 'node:fs' {
|
3834 | export * from 'fs';
|
3835 | }
|