happy-opfs
    Preparing search index...

    Function readFile

    Reads the content of a file at the specified path with the specified options.

    The type of the content to read from the file.

    The path of the file to read.

    Optional read options.

    A promise that resolves to an AsyncIOResult containing the file content.

    • Reads the content of a file at the specified path as a File.

      Parameters

      • filePath: string

        The path of the file to read.

      • options: ReadOptions & { encoding: "blob" }

        Read options specifying the 'blob' encoding.

      Returns AsyncIOResult<File>

      A promise that resolves to an AsyncIOResult containing the file content as a File.

      1.0.0

      (await readFile('/path/to/file.txt', { encoding: 'blob' }))
      .inspect(file => console.log(file.name, file.size, file.type));
    • Reads the content of a file at the specified path as a string.

      Parameters

      • filePath: string

        The path of the file to read.

      • options: ReadOptions & { encoding: "utf8" }

        Read options specifying the 'utf8' encoding.

      Returns AsyncIOResult<string>

      A promise that resolves to an AsyncIOResult containing the file content as a string.

      1.0.0

      (await readFile('/path/to/file.txt', { encoding: 'utf8' }))
      .inspect(content => console.log(content));
    • Reads the content of a file at the specified path as a readable stream. Useful for processing large files without loading them entirely into memory.

      Parameters

      • filePath: string

        The path of the file to read.

      • options: ReadOptions & { encoding: "stream" }

        Read options specifying the 'stream' encoding.

      Returns AsyncIOResult<ReadableStream<Uint8Array<ArrayBuffer>>>

      A promise that resolves to an AsyncIOResult containing a ReadableStream<Uint8Array>.

      1.0.0

      readFileSync for synchronous version (bytes only)

      (await readFile('/path/to/large-file.bin', { encoding: 'stream' }))
      .inspect(async stream => {
      const reader = stream.getReader();
      while (true) {
      const { done, value } = await reader.read();
      if (done) break;
      console.log('Received chunk:', value.length, 'bytes');
      }
      });
    • Reads the content of a file at the specified path as a Uint8Array (default).

      Parameters

      • filePath: string

        The path of the file to read.

      • Optionaloptions: ReadOptions & { encoding?: "bytes" }

        Optional read options. Defaults to 'bytes' encoding.

      Returns AsyncIOResult<Uint8Array<ArrayBuffer>>

      A promise that resolves to an AsyncIOResult containing the file content as a Uint8Array.

      1.0.0

      readFileSync for synchronous version

      (await readFile('/path/to/file.bin'))
      .inspect(bytes => console.log('First byte:', bytes[0]));
    • Reads the content of a file at the specified path with the specified options. This overload accepts any ReadOptions and returns the union of all possible content types. Useful when the encoding is determined at runtime.

      Parameters

      • filePath: string

        The path of the file to read.

      • Optionaloptions: ReadOptions

        Optional read options.

      Returns AsyncIOResult<ReadFileContent>

      A promise that resolves to an AsyncIOResult containing the file content.

      1.0.0

      readFileSync for synchronous version

      // When encoding is dynamic
      const encoding = getUserPreference(); // 'utf8' | 'bytes' | ...
      (await readFile('/path/to/file.txt', { encoding }))
      .inspect(content => {
      // content type is ReadFileContent (union type)
      if (typeof content === 'string') {
      console.log('Text:', content);
      } else if (content instanceof Uint8Array) {
      console.log('Bytes:', content.length);
      }
      });