| | |
| | | * |
| | | * All file system operations have synchronous, callback, and promise-based |
| | | * forms, and are accessible using both CommonJS syntax and ES6 Modules (ESM). |
| | | * @see [source](https://github.com/nodejs/node/blob/v16.9.0/lib/fs.js) |
| | | * @see [source](https://github.com/nodejs/node/blob/v17.0.0/lib/fs.js) |
| | | */ |
| | | declare module 'fs' { |
| | | import * as stream from 'node:stream'; |
| | |
| | | */ |
| | | readSync(): Dirent | null; |
| | | } |
| | | /** |
| | | * Class: fs.StatWatcher |
| | | * @since v14.3.0, v12.20.0 |
| | | * Extends `EventEmitter` |
| | | * A successful call to {@link watchFile} method will return a new fs.StatWatcher object. |
| | | */ |
| | | export interface StatWatcher extends EventEmitter { |
| | | /** |
| | | * 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 |
| | | * no effect. |
| | | * |
| | | * By default, all `fs.StatWatcher` objects are "ref'ed", making it normally |
| | | * unnecessary to call `watcher.ref()` unless `watcher.unref()` had been |
| | | * called previously. |
| | | * @since v14.3.0, v12.20.0 |
| | | */ |
| | | ref(): this; |
| | | /** |
| | | * When called, the active `fs.StatWatcher` object will not require the Node.js |
| | | * event loop to remain active. If there is no other activity keeping the |
| | | * event loop running, the process may exit before the `fs.StatWatcher` object's |
| | | * callback is invoked. Calling `watcher.unref()` multiple times will have |
| | | * no effect. |
| | | * @since v14.3.0, v12.20.0 |
| | | */ |
| | | unref(): this; |
| | | } |
| | | export interface FSWatcher extends EventEmitter { |
| | | /** |
| | | * Stop watching for changes on the given `fs.FSWatcher`. Once stopped, the `fs.FSWatcher` object is no longer usable. |
| | |
| | | * @since v0.1.93 |
| | | */ |
| | | export class ReadStream extends stream.Readable { |
| | | close(): void; |
| | | close(callback?: (err?: NodeJS.ErrnoException | null) => void): void; |
| | | /** |
| | | * The number of bytes that have been read so far. |
| | | * @since v6.4.0 |
| | |
| | | * callback that will be executed once the `writeStream`is closed. |
| | | * @since v0.9.4 |
| | | */ |
| | | close(): void; |
| | | close(callback?: (err?: NodeJS.ErrnoException | null) => void): void; |
| | | /** |
| | | * The number of bytes written so far. Does not include data that is still queued |
| | | * for writing. |
| | |
| | | function __promisify__(fd: number, options?: StatOptions): Promise<Stats | BigIntStats>; |
| | | } |
| | | /** |
| | | * Synchronous fstat(2) - Get file status. |
| | | * @param fd A file descriptor. |
| | | * Retrieves the `fs.Stats` for the file descriptor. |
| | | * |
| | | * See the POSIX [`fstat(2)`](http://man7.org/linux/man-pages/man2/fstat.2.html) documentation for more detail. |
| | | * @since v0.1.95 |
| | | */ |
| | | export function fstatSync( |
| | | fd: number, |
| | |
| | | } |
| | | ): BigIntStats; |
| | | export function fstatSync(fd: number, options?: StatOptions): Stats | BigIntStats; |
| | | |
| | | /** |
| | | * Retrieves the `fs.Stats` for the symbolic link referred to by the path. |
| | | * 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 |
| | |
| | | */ |
| | | export function writeSync(fd: number, string: string, position?: number | null, encoding?: BufferEncoding | null): number; |
| | | export type ReadPosition = number | bigint; |
| | | export interface ReadSyncOptions { |
| | | /** |
| | | * @default 0 |
| | | */ |
| | | offset?: number | undefined; |
| | | /** |
| | | * @default `length of buffer` |
| | | */ |
| | | length?: number | undefined; |
| | | /** |
| | | * @default null |
| | | */ |
| | | position?: ReadPosition | null | undefined; |
| | | } |
| | | export interface ReadAsyncOptions<TBuffer extends NodeJS.ArrayBufferView> extends ReadSyncOptions { |
| | | buffer?: TBuffer; |
| | | } |
| | | /** |
| | | * Read data from the file specified by `fd`. |
| | | * |
| | |
| | | position: ReadPosition | null, |
| | | callback: (err: NodeJS.ErrnoException | null, bytesRead: number, buffer: TBuffer) => void |
| | | ): void; |
| | | /** |
| | | * Similar to the above `fs.read` function, this version takes an optional `options` object. |
| | | * If not otherwise specified in an `options` object, |
| | | * `buffer` defaults to `Buffer.alloc(16384)`, |
| | | * `offset` defaults to `0`, |
| | | * `length` defaults to `buffer.byteLength`, `- offset` as of Node 17.6.0 |
| | | * `position` defaults to `null` |
| | | * @since v12.17.0, 13.11.0 |
| | | */ |
| | | export function read<TBuffer extends NodeJS.ArrayBufferView>( |
| | | fd: number, |
| | | options: ReadAsyncOptions<TBuffer>, |
| | | callback: (err: NodeJS.ErrnoException | null, bytesRead: number, buffer: TBuffer) => void |
| | | ): void; |
| | | export function read( |
| | | fd: number, |
| | | callback: (err: NodeJS.ErrnoException | null, bytesRead: number, buffer: NodeJS.ArrayBufferView) => void |
| | | ): void; |
| | | export namespace read { |
| | | /** |
| | | * @param fd A file descriptor. |
| | |
| | | bytesRead: number; |
| | | buffer: TBuffer; |
| | | }>; |
| | | } |
| | | export interface ReadSyncOptions { |
| | | /** |
| | | * @default 0 |
| | | */ |
| | | offset?: number | undefined; |
| | | /** |
| | | * @default `length of buffer` |
| | | */ |
| | | length?: number | undefined; |
| | | /** |
| | | * @default null |
| | | */ |
| | | position?: ReadPosition | null | undefined; |
| | | function __promisify__<TBuffer extends NodeJS.ArrayBufferView>( |
| | | fd: number, |
| | | options: ReadAsyncOptions<TBuffer> |
| | | ): Promise<{ |
| | | bytesRead: number; |
| | | buffer: TBuffer; |
| | | }>; |
| | | function __promisify__( |
| | | fd: number |
| | | ): Promise<{ |
| | | bytesRead: number; |
| | | buffer: NodeJS.ArrayBufferView; |
| | | }>; |
| | | } |
| | | /** |
| | | * Returns the number of `bytesRead`. |
| | |
| | | * * the file is renamed and then renamed a second time back to its original name |
| | | * @since v0.1.31 |
| | | */ |
| | | export interface WatchFileOptions { |
| | | bigint?: boolean | undefined; |
| | | persistent?: boolean | undefined; |
| | | interval?: number | undefined; |
| | | } |
| | | /** |
| | | * Watch for changes on `filename`. The callback `listener` will be called each |
| | | * time the file is accessed. |
| | | * |
| | | * The `options` argument may be omitted. If provided, it should be an object. The`options` object may contain a boolean named `persistent` that indicates |
| | | * whether the process should continue to run as long as files are being watched. |
| | | * The `options` object may specify an `interval` property indicating how often the |
| | | * target should be polled in milliseconds. |
| | | * |
| | | * The `listener` gets two arguments the current stat object and the previous |
| | | * stat object: |
| | | * |
| | | * ```js |
| | | * import { watchFile } from 'fs'; |
| | | * |
| | | * watchFile('message.text', (curr, prev) => { |
| | | * console.log(`the current mtime is: ${curr.mtime}`); |
| | | * console.log(`the previous mtime was: ${prev.mtime}`); |
| | | * }); |
| | | * ``` |
| | | * |
| | | * These stat objects are instances of `fs.Stat`. If the `bigint` option is `true`, |
| | | * the numeric values in these objects are specified as `BigInt`s. |
| | | * |
| | | * To be notified when the file was modified, not just accessed, it is necessary |
| | | * to compare `curr.mtimeMs` and `prev.mtimeMs`. |
| | | * |
| | | * When an `fs.watchFile` operation results in an `ENOENT` error, it |
| | | * will invoke the listener once, with all the fields zeroed (or, for dates, the |
| | | * Unix Epoch). If the file is created later on, the listener will be called |
| | | * again, with the latest stat objects. This is a change in functionality since |
| | | * v0.10. |
| | | * |
| | | * 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. |
| | | * |
| | | * When a file being watched by `fs.watchFile()` disappears and reappears, |
| | | * then the contents of `previous` in the second callback event (the file's |
| | | * reappearance) will be the same as the contents of `previous` in the first |
| | | * callback event (its disappearance). |
| | | * |
| | | * This happens when: |
| | | * |
| | | * * the file is deleted, followed by a restore |
| | | * * the file is renamed and then renamed a second time back to its original name |
| | | * @since v0.1.31 |
| | | */ |
| | | export function watchFile( |
| | | filename: PathLike, |
| | | options: |
| | | | { |
| | | persistent?: boolean | undefined; |
| | | interval?: number | undefined; |
| | | } |
| | | | (WatchFileOptions & { |
| | | bigint?: false | undefined; |
| | | }) |
| | | | undefined, |
| | | listener: (curr: Stats, prev: Stats) => void |
| | | ): void; |
| | | ): StatWatcher; |
| | | export function watchFile( |
| | | filename: PathLike, |
| | | options: |
| | | | (WatchFileOptions & { |
| | | bigint: true; |
| | | }) |
| | | | undefined, |
| | | listener: (curr: BigIntStats, prev: BigIntStats) => void |
| | | ): StatWatcher; |
| | | /** |
| | | * Watch for changes on `filename`. The callback `listener` will be called each time the file is accessed. |
| | | * @param filename A path to a file or directory. If a URL is provided, it must use the `file:` protocol. |
| | | */ |
| | | export function watchFile(filename: PathLike, listener: (curr: Stats, prev: Stats) => void): void; |
| | | export function watchFile(filename: PathLike, listener: (curr: Stats, prev: Stats) => void): StatWatcher; |
| | | /** |
| | | * Stop watching for changes on `filename`. If `listener` is specified, only that |
| | | * particular listener is removed. Otherwise, _all_ listeners are removed, |