Native file system operations for Bare. The API closely follows that of the Node.js fs module.
npm i bare-fs
const fs = require('bare-fs')
const fd = await fs.open('hello.txt')
const buffer = Buffer.alloc(1024)
try {
const length = await fs.read(fd, buffer)
console.log('Read', length, 'bytes')
} finally {
await fs.close(fd)
}Open a file, returning a file descriptor. flags defaults to 'r' and mode defaults to 0o666. flags may be a string such as 'r', 'w', 'a', 'r+', etc., or a numeric combination of fs.constants flags.
Callback version of fs.open().
Synchronous version of fs.open().
Close a file descriptor.
Callback version of fs.close().
Synchronous version of fs.close().
Check whether the file at filepath is accessible. mode defaults to fs.constants.F_OK.
Callback version of fs.access().
Synchronous version of fs.access().
Check whether a file exists at filepath. Returns true if the file is accessible, false otherwise.
Callback version of fs.exists().
Synchronous version of fs.exists().
Read from a file descriptor into buffer. offset defaults to 0, len defaults to buffer.byteLength - offset, and pos defaults to -1 (current position). Returns the number of bytes read.
Callback version of fs.read().
Synchronous version of fs.read().
Read from a file descriptor into an array of buffers. pos defaults to -1.
Callback version of fs.readv().
Synchronous version of fs.readv().
Write data to a file descriptor. When data is a string, the signature is fs.write(fd, data[, pos[, encoding]]) where encoding defaults to 'utf8'. Returns the number of bytes written.
Callback version of fs.write().
Synchronous version of fs.write().
Write an array of buffers to a file descriptor. pos defaults to -1.
Callback version of fs.writev().
Synchronous version of fs.writev().
Get the status of a file. Returns a Stats object.
Callback version of fs.stat().
Synchronous version of fs.stat().
Like fs.stat(), but if filepath is a symbolic link, the link itself is statted, not the file it refers to.
Callback version of fs.lstat().
Synchronous version of fs.lstat().
Get the status of a file by its file descriptor. Returns a Stats object.
Callback version of fs.fstat().
Synchronous version of fs.fstat().
Truncate a file to len bytes. len defaults to 0.
Callback version of fs.ftruncate().
Synchronous version of fs.ftruncate().
Change the permissions of a file. mode may be a numeric mode or a string that will be parsed as octal.
Callback version of fs.chmod().
Synchronous version of fs.chmod().
Change the permissions of a file by its file descriptor.
Callback version of fs.fchmod().
Synchronous version of fs.fchmod().
Change the access and modification times of a file. Times may be numbers (seconds since epoch) or Date objects.
Callback version of fs.utimes().
Synchronous version of fs.utimes().
Create a directory at filepath.
Options include:
options = {
mode: 0o777,
recursive: false
}If opts is a number, it is treated as the mode. When recursive is true, parent directories are created as needed.
Callback version of fs.mkdir().
Synchronous version of fs.mkdir().
Remove an empty directory.
Callback version of fs.rmdir().
Synchronous version of fs.rmdir().
Remove a file or directory at filepath.
Options include:
options = {
force: false,
recursive: false
}When recursive is true, directories are removed along with their contents. When force is true, no error is thrown if filepath does not exist.
Callback version of fs.rm().
Synchronous version of fs.rm().
Remove a file.
Callback version of fs.unlink().
Synchronous version of fs.unlink().
Rename a file from src to dst.
Callback version of fs.rename().
Synchronous version of fs.rename().
Copy a file from src to dst. mode is an optional bitmask created from fs.constants.COPYFILE_EXCL, fs.constants.COPYFILE_FICLONE, or fs.constants.COPYFILE_FICLONE_FORCE.
Callback version of fs.copyFile().
Synchronous version of fs.copyFile().
Copy a file or directory from src to dst.
Options include:
options = {
recursive: false
}Set recursive to true to copy directories and their contents. Files are copied preserving their permissions.
Callback version of fs.cp().
Synchronous version of fs.cp().
Resolve the real path of filepath, expanding all symbolic links.
Options include:
options = {
encoding: 'utf8'
}Set encoding to 'buffer' to receive the result as a Buffer.
Callback version of fs.realpath().
Synchronous version of fs.realpath().
Read the target of a symbolic link.
Options include:
options = {
encoding: 'utf8'
}Callback version of fs.readlink().
Synchronous version of fs.readlink().
Create a symbolic link at filepath pointing to target. type may be 'file', 'dir', or 'junction' (Windows only) or a numeric flag. On Windows, if type is not provided, it is inferred from the target.
Callback version of fs.symlink().
Synchronous version of fs.symlink().
Open a directory for iteration. Returns a Dir object.
Options include:
options = {
encoding: 'utf8',
bufferSize: 32
}Callback version of fs.opendir().
Synchronous version of fs.opendir().
Read the contents of a directory. Returns an array of filenames or, if withFileTypes is true, an array of Dirent objects.
Options include:
options = {
encoding: 'utf8',
withFileTypes: false
}Callback version of fs.readdir().
Synchronous version of fs.readdir().
Read the entire contents of a file. Returns a Buffer by default, or a string if an encoding is specified.
Options include:
options = {
encoding: 'buffer',
flag: 'r'
}Callback version of fs.readFile().
Synchronous version of fs.readFile().
Write data to a file, replacing it if it already exists.
Options include:
options = {
encoding: 'utf8',
flag: 'w',
mode: 0o666
}Callback version of fs.writeFile().
Synchronous version of fs.writeFile().
Append data to a file, creating it if it does not exist. Accepts the same options as fs.writeFile() but defaults to the 'a' flag.
Callback version of fs.appendFile().
Synchronous version of fs.appendFile().
Watch a file or directory for changes. Returns a Watcher object. The callback, if provided, is called with (eventType, filename) on each change.
Options include:
options = {
persistent: true,
recursive: false,
encoding: 'utf8'
}Create a readable stream for a file. Returns a ReadStream.
Options include:
options = {
fd: -1,
flags: 'r',
mode: 0o666,
start: 0,
end: Infinity
}If fd is provided, path may be null and the stream reads from the given file descriptor.
Create a writable stream for a file. Returns a WriteStream.
Options include:
options = {
fd: -1,
flags: 'w',
mode: 0o666
}If fd is provided, path may be null and the stream writes to the given file descriptor.
An object containing file system constants. See fs/constants for the full list. Commonly used constants include:
fs.constants.O_RDONLY,fs.constants.O_WRONLY,fs.constants.O_RDWR— file access flagsfs.constants.O_CREAT,fs.constants.O_TRUNC,fs.constants.O_APPEND— file creation flagsfs.constants.F_OK,fs.constants.R_OK,fs.constants.W_OK,fs.constants.X_OK— file accessibility flagsfs.constants.S_IFMT,fs.constants.S_IFREG,fs.constants.S_IFDIR,fs.constants.S_IFLNK— file type flagsfs.constants.COPYFILE_EXCL,fs.constants.COPYFILE_FICLONE,fs.constants.COPYFILE_FICLONE_FORCE— copy flags
Returned by fs.stat(), fs.lstat(), and fs.fstat().
The device identifier.
The file mode (type and permissions).
The number of hard links.
The user identifier of the file owner.
The group identifier of the file owner.
The device identifier for special files.
The file system block size for I/O operations.
The inode number.
The size of the file in bytes.
The number of 512-byte blocks allocated.
The access time in milliseconds since the epoch.
The modification time in milliseconds since the epoch.
The change time in milliseconds since the epoch.
The creation time in milliseconds since the epoch.
The access time as a Date object.
The modification time as a Date object.
The change time as a Date object.
The creation time as a Date object.
Returns true if the file is a directory.
Returns true if the file is a regular file.
Returns true if the file is a block device.
Returns true if the file is a character device.
Returns true if the file is a FIFO (named pipe).
Returns true if the file is a symbolic link. Only meaningful when using fs.lstat().
Returns true if the file is a socket.
Returned by fs.opendir(). Supports both synchronous and asynchronous iteration.
The path of the directory.
Read the next directory entry. Returns a Dirent or null when all entries have been read.
Callback version of dir.read().
Synchronous version of dir.read().
Close the directory handle.
Callback version of dir.close().
Synchronous version of dir.close().
Represents a directory entry, returned when iterating a Dir or using fs.readdir() with withFileTypes: true.
The path of the parent directory.
The name of the directory entry, as a string or Buffer depending on the encoding.
The numeric type of the directory entry.
Returns true if the entry is a regular file.
Returns true if the entry is a directory.
Returns true if the entry is a symbolic link.
Returns true if the entry is a FIFO.
Returns true if the entry is a socket.
Returns true if the entry is a character device.
Returns true if the entry is a block device.
A readable stream for file data, created by fs.createReadStream(). Extends Readable from https://github.com/holepunchto/bare-stream.
The file path, or null if opened by file descriptor.
The underlying file descriptor.
The flags the file was opened with.
The mode the file was opened with.
A writable stream for file data, created by fs.createWriteStream(). Extends Writable from https://github.com/holepunchto/bare-stream.
The file path, or null if opened by file descriptor.
The underlying file descriptor.
The flags the file was opened with.
The mode the file was opened with.
Watches for file system changes, created by fs.watch(). Extends EventEmitter from https://github.com/holepunchto/bare-events.
Stop watching for changes.
Prevent the event loop from exiting while the watcher is active.
Allow the event loop to exit even if the watcher is still active.
Emitted with (eventType, filename) when a change is detected. eventType is either 'rename' or 'change'.
Emitted with (err) when an error occurs.
Emitted when the watcher is closed.
Returned by require('bare-fs/promises').open(). Provides an object-oriented API for working with file descriptors.
Close the file handle.
Read from the file into buffer.
Read from the file into an array of buffers.
Write data to the file.
Write an array of buffers to the file.
Get the status of the file.
Change the permissions of the file.
Create a readable stream for the file.
Create a writable stream for the file.
Emitted when the file handle is closed.
Apache-2.0