Skip to main content
Deno 2 is finally here 🎉️
Learn more
Module

x/ayonli_jsext/fs.ts

A JavaScript extension package for building strong and modern applications.
Latest
import * as ayonliJsext from "https://deno.land/x/ayonli_jsext@v0.9.72/fs.ts";

Universal file system APIs for both server and browser applications.

This module is guaranteed to work in the following environments:

  • Node.js
  • Deno
  • Bun
  • Modern browsers
  • Cloudflare Workers (limited support and experimental)

We can also use the runtime function to check whether the runtime has file system support. When runtime().fsSupport is true, this module should work properly.

In most browsers, this module uses the Origin Private File System. In Chromium browsers, this module can also access the device's local file system via window.showOpenFilePicker() and window.showDirectoryPicker().

This module also provides limited support for Cloudflare Workers, however it requires setting the [site].bucket option in the wrangler.toml file. Only the reading functions are supported, such as readFile and readDir, these functions allow us reading static files in the workers, writing functions is not implemented at the moment. More details about serving static assets in Cloudflare Workers can be found here: Add static assets to an existing Workers project.

Errors:

When a file system operation fails, this module throws an Exception with one of the following names:

  • NotFoundError: The file or directory does not exist.
  • NotAllowedError: The operation is not allowed, such as being blocked by the permission system.
  • AlreadyExistsError: The file or directory already exists.
  • IsDirectoryError: The path is a directory, not a file.
  • NotDirectoryError: The path is a file, not a directory.
  • InvalidOperationError: The operation is not supported, such as trying to copy a directory without the recursive option.
  • BusyError: The file is busy, such as being locked by another program.
  • InterruptedError: The operation is interrupted by the underlying file system.
  • FileTooLargeError: The file is too large, or the file system doesn't have enough space to store the new content.
  • FilesystemLoopError: Too many symbolic links were encountered when resolving the filename.

Other errors may also be thrown by the runtime, such as TypeError.

Variables

Platform-specific end-of-line marker. The value is \r\n in Windows server-side environments, and \n elsewhere.

v
readFileAsStream
deprecated

Functions

Changes the permission of the specified file or directory.

Changes the owner and group of the specified file or directory.

Copies the file or directory (and its contents) from the old location to the new location.

Creates a readable stream for the target file.

Creates a writable stream for the target file.

Ensures the directory exists, creating it (and any parent directory) if not.

Checks if the given path exists.

Obtains the directory handle of the given path.

Obtains the file handle of the given path.

Creates a hard link (or symbolic link) from the source path to the destination path.

Creates a new directory with the given path.

Reads the directory of the given path and iterates its entries.

Reads the content of the given file in bytes.

Reads the file as a File object.

Reads the content of the given file as text.

Returns the destination path of a symbolic link.

Recursively reads the contents of the directory and transform them into a tree structure.

Removes the file or directory of the given path from the file system.

Renames the file or directory from the old path to the new path.

Returns the information of the given file or directory.

Truncates (or extends) the file to reach the specified size. If size is not specified then the entire file contents are truncated.

Changes the access (atime) and modification (mtime) times of the file or directory. Given times are either in seconds (UNIX epoch time) or as Date objects.

Writes the given data to the file.

Writes multiple lines of content to the file.

Interfaces

Options for the copy function.

Information about a directory entry.

Information about a file or directory.

Common options for file system operations.

Options for the getDirHandle function.

Options for the getFileHandle function.

Options for the link function.

Options for the mkdir function.

Options for the readDir function.

Options for file reading functions, such as readFile, readFileAsText and readFileAsFile.

Options for the remove function.

Options for the stat function.

Options for file writing functions, such as writeFile and writeLines.