Hyperlinkv0.8.0-beta.28

FileSystem

FileSystem.Fileinterfaceeffect/FileSystem.ts:1126
File

Interface representing an open file handle.

Details

Provides low-level file operations including reading, writing, seeking, and retrieving file information. File handles are automatically managed within scoped operations to ensure proper cleanup.

Example (Working with file handles)

import { Console, Effect, FileSystem } from "effect"

const program = Effect.gen(function*() {
  const fs = yield* FileSystem.FileSystem

  // Open a file and work with the handle
  yield* Effect.scoped(
    Effect.gen(function*() {
      const file = yield* fs.open("./data.txt", { flag: "r+" })

      // Get file information
      const stats = yield* file.stat
      yield* Console.log(`File size: ${stats.size} bytes`)

      // Read from specific position
      yield* file.seek(10, "start")
      const buffer = new Uint8Array(5)
      const bytesRead = yield* file.read(buffer)
      yield* Console.log(`Read ${bytesRead} bytes:`, buffer)

      // Write data
      const data = new TextEncoder().encode("Hello")
      yield* file.write(data)
      yield* file.sync // Flush to disk
    })
  )
})
file
export interface File {
  readonly [FileTypeId]: typeof FileTypeId
  readonly fd: File.Descriptor
  readonly stat: Effect.Effect<File.Info, PlatformError>
  readonly seek: (offset: SizeInput, from: SeekMode) => Effect.Effect<void>
  readonly sync: Effect.Effect<void, PlatformError>
  readonly read: (buffer: Uint8Array) => Effect.Effect<Size, PlatformError>
  readonly readAlloc: (size: SizeInput) => Effect.Effect<Option.Option<Uint8Array>, PlatformError>
  readonly truncate: (length?: SizeInput) => Effect.Effect<void, PlatformError>
  readonly write: (buffer: Uint8Array) => Effect.Effect<Size, PlatformError>
  readonly writeAll: (buffer: Uint8Array) => Effect.Effect<void, PlatformError>
}

/**
 * Namespace containing types associated with open file handles, including file
 * descriptors, entry kinds, and stat information.
 *
 * @since 4.0.0
 */
export declare namespace File {
  /**
   * Branded type for file descriptors.
   *
   * **Details**
   *
   * File descriptors are numeric handles used by the operating system
   * to identify open files. The branded type ensures type safety.
   *
   * @category file
   * @since 4.0.0
   */
  export type Descriptor = Brand.Branded<number, "FileDescriptor">

  /**
   * Enumeration of possible file system entry types.
   *
   * **Details**
   *
   * Represents the different types of entries that can exist in a file system,
   * from regular files to special device files and symbolic links.
   *
   * @category file
   * @since 4.0.0
   */
  export type Type =
    | "File"
    | "Directory"
    | "SymbolicLink"
    | "BlockDevice"
    | "CharacterDevice"
    | "FIFO"
    | "Socket"
    | "Unknown"

  /**
   * Comprehensive file information structure.
   *
   * **Details**
   *
   * Contains metadata about a file or directory including type, timestamps,
   * permissions, and size information. This structure is returned by file
   * stat operations.
   *
   * **Example** (Inspecting file information)
   *
   * ```ts
   * import { Effect, FileSystem, Option } from "effect"
   *
   * const program = Effect.gen(function*() {
   *   const fs = yield* FileSystem.FileSystem
   *
   *   const path = yield* fs.makeTempFile({ prefix: "info-" })
   *   yield* fs.writeFileString(path, "hello")
   *
   *   const info: FileSystem.File.Info = yield* fs.stat(path)
   *
   *   console.log(`File type: ${info.type}`) // "File type: File"
   *   console.log(`File size: ${info.size} bytes`) // "File size: 5 bytes"
   *   console.log(`Mode: ${info.mode.toString(8)}`) // Octal permissions
   *
   *   // Handle optional timestamps without inventing a fallback date
   *   const modified = Option.match(info.mtime, {
   *     onNone: () => "unavailable",
   *     onSome: (mtime) => mtime.toISOString()
   *   })
   *   console.log(`Modified: ${modified}`)
   *
   *   // Check if it's a regular file
   *   if (info.type === "File") {
   *     console.log("Processing regular file...") // "Processing regular file..."
   *   }
   *
   *   yield* fs.remove(path)
   * })
   * ```
   *
   * @category file
   * @since 4.0.0
   */
  export interface Info {
    readonly type: Type
    readonly mtime: Option.Option<Date>
    readonly atime: Option.Option<Date>
    readonly birthtime: Option.Option<Date>
    readonly dev: number
    readonly ino: Option.Option<number>
    readonly mode: number
    readonly nlink: Option.Option<number>
    readonly uid: Option.Option<number>
    readonly gid: Option.Option<number>
    readonly rdev: Option.Option<number>
    readonly size: Size
    readonly blksize: Option.Option<Size>
    readonly blocks: Option.Option<number>
  }
}
Referenced by 4 symbols