interface CacheFs {
    existsSync: ((path) => boolean);
    readFile: {
        (path, options?): Promise<Buffer>;
        (path, options): Promise<string>;
        (path, options?): Promise<string | Buffer>;
    };
    readFileSync: {
        (path, options?): NonSharedBuffer;
        (path, options): string;
        (path, options?): string | NonSharedBuffer;
    };
    mkdir(dir): Promise<string | void>;
    stat(f): Promise<{
        mtime: Date;
    }>;
    writeFile(f, d): Promise<void>;
}

Properties

existsSync: ((path) => boolean)

Type declaration

    • (path): boolean
    • Returns true if the path exists, false otherwise.

      For detailed information, see the documentation of the asynchronous version of this API: exists.

      fs.exists() is deprecated, but fs.existsSync() is not. The callback parameter to fs.exists() accepts parameters that are inconsistent with other Node.js callbacks. fs.existsSync() does not use a callback.

      import { existsSync } from 'node:fs';

      if (existsSync('/etc/passwd'))
      console.log('The path exists.');

      Parameters

      Returns boolean

      Since

      v0.1.21

readFile: {
    (path, options?): Promise<Buffer>;
    (path, options): Promise<string>;
    (path, options?): Promise<string | Buffer>;
}

Type declaration

    • (path, options?): Promise<Buffer>
    • Asynchronously reads the entire contents of a file.

      If no encoding is specified (using options.encoding), the data is returned as a Buffer object. Otherwise, the data will be a string.

      If options is a string, then it specifies the encoding.

      When the path is a directory, the behavior of fsPromises.readFile() is platform-specific. On macOS, Linux, and Windows, the promise will be rejected with an error. On FreeBSD, a representation of the directory's contents will be returned.

      An example of reading a package.json file located in the same directory of the running code:

      import { readFile } from 'node:fs/promises';
      try {
      const filePath = new URL('./package.json', import.meta.url);
      const contents = await readFile(filePath, { encoding: 'utf8' });
      console.log(contents);
      } catch (err) {
      console.error(err.message);
      }

      It is possible to abort an ongoing readFile using an AbortSignal. If a request is aborted the promise returned is rejected with an AbortError:

      import { readFile } from 'node:fs/promises';

      try {
      const controller = new AbortController();
      const { signal } = controller;
      const promise = readFile(fileName, { signal });

      // Abort the request before the promise settles.
      controller.abort();

      await promise;
      } catch (err) {
      // When a request is aborted - err is an AbortError
      console.error(err);
      }

      Aborting an ongoing request does not abort individual operating system requests but rather the internal buffering fs.readFile performs.

      Any specified FileHandle has to support reading.

      Parameters

      Returns Promise<Buffer>

      Fulfills with the contents of the file.

      Since

      v10.0.0

    • (path, options): Promise<string>
    • Asynchronously reads the entire contents of a file.

      Parameters

      • path: FileHandle | PathLike

        A path to a file. If a URL is provided, it must use the file: protocol. If a FileHandle is provided, the underlying file will not be closed automatically.

      • options: BufferEncoding | {
            encoding: BufferEncoding;
            flag?: OpenMode | undefined;
        } & Abortable

        An object that may contain an optional flag. If a flag is not provided, it defaults to 'r'.

      Returns Promise<string>

    • (path, options?): Promise<string | Buffer>
    • Asynchronously reads the entire contents of a file.

      Parameters

      • path: FileHandle | PathLike

        A path to a file. If a URL is provided, it must use the file: protocol. If a FileHandle is provided, the underlying file will not be closed automatically.

      • Optional options: null | BufferEncoding | ObjectEncodingOptions & Abortable & {
            flag?: OpenMode | undefined;
        }

        An object that may contain an optional flag. If a flag is not provided, it defaults to 'r'.

      Returns Promise<string | Buffer>

readFileSync: {
    (path, options?): NonSharedBuffer;
    (path, options): string;
    (path, options?): string | NonSharedBuffer;
}

Type declaration

    • (path, options?): NonSharedBuffer
    • Returns the contents of the path.

      For detailed information, see the documentation of the asynchronous version of this API: readFile.

      If the encoding option is specified then this function returns a string. Otherwise it returns a buffer.

      Similar to readFile, when the path is a directory, the behavior of fs.readFileSync() is platform-specific.

      import { readFileSync } from 'node:fs';

      // macOS, Linux, and Windows
      readFileSync('<directory>');
      // => [Error: EISDIR: illegal operation on a directory, read <directory>]

      // FreeBSD
      readFileSync('<directory>'); // => <data>

      Parameters

      • path: PathOrFileDescriptor

        filename or file descriptor

      • Optional options: null | {
            encoding?: null;
            flag?: string;
        }

      Returns NonSharedBuffer

      Since

      v0.1.8

    • (path, options): string
    • Synchronously reads the entire contents of a file.

      Parameters

      • path: PathOrFileDescriptor

        A path to a file. If a URL is provided, it must use the file: protocol. If a file descriptor is provided, the underlying file will not be closed automatically.

      • options: BufferEncoding | {
            encoding: BufferEncoding;
            flag?: string;
        }

        Either the encoding for the result, or an object that contains the encoding and an optional flag. If a flag is not provided, it defaults to 'r'.

      Returns string

    • (path, options?): string | NonSharedBuffer
    • Synchronously reads the entire contents of a file.

      Parameters

      • path: PathOrFileDescriptor

        A path to a file. If a URL is provided, it must use the file: protocol. If a file descriptor is provided, the underlying file will not be closed automatically.

      • Optional options: null | BufferEncoding | ObjectEncodingOptions & {
            flag?: string;
        }

        Either the encoding for the result, or an object that contains the encoding and an optional flag. If a flag is not provided, it defaults to 'r'.

      Returns string | NonSharedBuffer

Methods

  • Parameters

    • dir: string

    Returns Promise<string | void>

  • Parameters

    • f: string

    Returns Promise<{
        mtime: Date;
    }>

  • Parameters

    • f: string
    • d: any

    Returns Promise<void>