happy-opfs
    Preparing search index...

    Function writeFile

    • Writes content to a file at the specified path. Creates the file and parent directories if they don't exist (unless create: false).

      When writing a ReadableStream to a new file, the stream is first written to a temporary file in /tmp, then moved to the target path upon success. This prevents leaving incomplete files if the stream is interrupted. For existing files, writes are performed directly since OPFS's transactional writes preserve the original content on failure.

      Parameters

      • filePath: string

        The absolute path of the file to write to.

      • contents: WriteFileContent

        The content to write (string, ArrayBuffer, TypedArray, Blob, or ReadableStream).

      • Optionaloptions: WriteOptions

        Optional write options.

        Options for writing files, including flags for creation and appending.

        • Optionalappend?: boolean

          Whether to append to the file if it already exists.

          false

        • Optionalcreate?: boolean

          Whether to create the file if it does not exist.

          true

      Returns AsyncVoidIOResult

      A promise that resolves to an AsyncVoidIOResult indicating success or failure.

      1.0.0

      // Write string content
      await writeFile('/path/to/file.txt', 'Hello, World!');

      // Write binary content
      await writeFile('/path/to/file.bin', new Uint8Array([1, 2, 3]));

      // Append to existing file
      await writeFile('/path/to/file.txt', '\nMore content', { append: true });