Both operations stream with bounded memory. Installation requires an existing parent directory on a filesystem that supports hard links. It checks the staged bytes and leaves an existing destination in place. An exact match is reused.
POSIX opens the final component without following links. On Windows, the path is checked before opening and compared with the descriptor before and after reading, since Windows has no atomic no-follow open here. The caller is responsible for containment and control of the parent directories.
Keep a job from running twice at once, across processes on one machine, with a lock:
ts
import { acquireLock } from 'jg-fs';const lock = await acquireLock('.locks/nightly-report', { metadata: { command: 'report' } }); // LOCK_BUSY while another process holds ittry { await buildNightlyReport();} finally { await lock.release();}
The lock is a directory holding an owner.json that names the process, with the metadata it’s given. A lock is never taken over by itself, even when its owner’s process is gone, as after a crash, because only you know whether the owner left work half done: acquireLock throws LOCK_BUSY with reason: 'owner-gone'. Check what the owner left, then take the lock over from exactly that owner:
recoverFrom takes the lock over only from that owner, on this host, once its process is gone. Use locks on local filesystems only: a lock is never taken over from another host.
Write a file only when it still holds what you read, such as a config another program may edit, and check that no directory on the way to a path inside a root is a symbolic link or a junction:
ts
import { assertNoLinksBelow, readBoundedFile, writeFileAtomicIfUnchanged } from 'jg-fs';const before = await readBoundedFile('/srv/repo/.npmrc', 65_536);await assertNoLinksBelow('/srv/repo', '/srv/repo/.npmrc'); // PATH_NOT_CANONICAL for a link on the wayawait writeFileAtomicIfUnchanged('/srv/repo/.npmrc', `${before}save-exact=true\n`, before); // FILE_CHANGED if edited meanwhile
writeJsonFile ends what it stringifies with a line feed, as text files do.
Keep keys, tokens, and sessions private to the user who owns them:
ts
import { createPrivateDirectory, readPrivateFile, writePrivateFile } from 'jg-fs';await createPrivateDirectory(configDirectory); // 0o700; DIRECTORY_NOT_PRIVATE if it exists and isn'tawait writePrivateFile(`${configDirectory}/token.json`, token, { rootPath: configDirectory }); // atomic, 0o600const saved = await readPrivateFile(`${configDirectory}/token.json`, { maxBytes: 16 * 1024, rootPath: configDirectory,});
A private file is a regular file, never a symbolic link, with no symbolic link anywhere in its path (PATH_NOT_CANONICAL), and without group or other permissions (FILE_NOT_PRIVATE). The checks run on the open file, so the content read is the content checked. A shared file is never overwritten, and permissions are never changed for you: fix them, or remove the file. writePrivateFile with isExclusive creates the file and throws FILE_ALREADY_EXISTS when anything is already at the path.
On Windows, Node.js reports only a file’s read-only attribute as its mode, so the permission checks are skipped there. A private file on Windows depends on its directory’s access control list, such as a directory inside the user’s profile.
Failures throw Exceptions from jg-exception, with the codes in ERRORS.
Removing a stuck lock
A lock left in a state no process can safely take over stays busy, and LOCK_BUSY’s data.reason says why:
held: the owner named in owner.json is still running. Wait, or stop that process.
owner-gone: the owner’s process is gone, as after a crash. Read it with readLockOwner, check what it left, and pass it as acquireLock’s recoverFrom.
other-host: the owner runs on another host, which this host can’t check.
ownerless: owner.json is missing or invalid, such as after a crash right after the directory was created. A lock being created or released at that moment looks the same, so try again first.
recovery-in-progress: a recovery directory is inside, so another process is taking the lock over, or crashed while doing so.
unexpected-entries: the directory holds something besides owner.json, such as a .tmp file left by an interrupted write.
Before deleting a stuck lock’s directory by hand, confirm that no process is using it: check the pid and hostname in owner.json, when there is one. Then delete the whole directory.
Generated from TSDoc
API reference
Every export of [email protected], 96 in all, with its signature, its errors, and an example.
Acquires a lock that only one process at a time can hold, such as one guarding a script that mustn't run twice at once. The lock is a directory at path, created with mode 0o700, holding an owner.json, mode 0o600, that names the process, with the metadata it's given, such as the command it runs.
A lock that already exists is never taken over by itself, even when its owner's process is gone, because only the caller knows whether that owner left work half done. It fails with LOCK_BUSY, whose data.reason says why: held, owner-gone, other-host, ownerless, recovery-in-progress, or unexpected-entries. To take over from an owner whose process is gone, read it with readLockOwner, check what it left, and pass it as recoverFrom: the lock is taken over only from that exact owner, on this host, when its process is gone and the directory holds nothing but owner.json. A lock left behind by a crash at the wrong moment stays busy until it's removed by hand, as the README describes.
Use it on local filesystems only. POSIX modes don't apply on Windows, and a lock is never taken over from another host.
What to write about the owner, and the dead owner to take the lock over from.
Defaults to {}.
Returns
Promise<IFileLock>
A promise that resolves to the lock, released with release().
Throws
LOCK_BUSYThe lock '<path>' is busy.
NOT_A_DIRECTORYThe path '<path>' is not a directory.
LOCK_ACQUISITION_FAILEDThe lock '<path>' could not be acquired.
Example
import { acquireLock, readLockOwner, writeFileAtomic } from 'jg-fs';const lock = await acquireLock('.locks/nightly-report', { metadata: { command: 'report' } });try { await writeFileAtomic('reports/nightly.json', JSON.stringify({ generatedAt: Date.now() }));} finally { await lock.release();}// after a crash: take the lock over from the owner whose process is gone, once its work is checkedconst deadOwner = await readLockOwner('.locks/nightly-report');if (deadOwner !== null) { await acquireLock('.locks/nightly-report', { recoverFrom: deadOwner });}
Checks that no directory between a root and a path is a symbolic link, or, on Windows, a junction, such as before writing a file inside a repository that someone else may have changed. It reads each directory strictly below the root, down to the path's parent, without following it, and stops at the first that doesn't exist, which a write would create. The root itself and the path's last component aren't checked.
Checks that a directory is private to its owner: a directory that isn't a symbolic link, with no symbolic link anywhere in its path, and, on POSIX, without group or other permissions. On Windows, the permission check is skipped, as for files.
Checks that a file is private to its owner, such as a key or a session file before it's trusted: a regular file that isn't a symbolic link, with no symbolic link anywhere in its path, and, on POSIX, without group or other permissions. With rootPath, it must be strictly inside that directory; with maxBytes, no larger. The checks run on the open file, so a file swapped in between them is caught.
On Windows, Node.js reports only a file's read-only attribute as its mode, so the permission check is skipped there: a private file on Windows depends on its directory's access control list.
Creates a directory, and any missing parents, with 0o700 permissions, and checks that it's private, as assertPrivateDirectory does. An existing directory that isn't private throws: its permissions are never changed silently.
Lists a directory's elements grouped by kind: directories, files (filtered by extension), and symbolic links, each group sorted. Entries removed while the directory is read are left out, and at most 32 entries are read at once.
A promise that resolves to the path's details, or null when it doesn't exist.
Example
import { getPathElement } from 'jg-fs';const element = await getPathElement('logo.png');element?.size; // the size in bytes, or undefined when the path doesn't exist
Computes the size and SHA-256 digest of one regular file with bounded memory. Rejects detectable changes to the file or its path during the read. POSIX opens the last component atomically without following links. Windows checks the path before opening and compares it with the opened descriptor before and after reading. The caller owns containment of parent directories. Cancellation rejects with the caller's abort reason.
Installs the expected file by linking an exclusively staged and verified copy to the destination. A matching destination is reused. Other occupants remain in place. The parent directory must exist, be controlled by the caller, and support hard links. Cancellation before installation preserves the destination and rejects with the caller's reason. File identity and link checks are the same as for hashFile.
Checks whether a path is a root path or nested inside it. The check is lexical: both paths are resolved without touching the filesystem, and symbolic links aren't followed. Sibling paths that share a prefix, such as /data/app and /data/application, aren't nested, and neither is a path on another drive on Windows.
Reads a regular file of at most maxBytes bytes. It never follows a symbolic link in the path's last component, though links in parent directories are followed, so callers that need containment combine it with isPathWithin. It reads at most maxBytes + 1 bytes, even when the file grows while it's read.
Whether to include the contents of subdirectories.
Defaults to false.
Returns
Promise<string[]>
A promise that resolves to the content paths.
Throws
NON_EXISTENT_DIRECTORYif the path doesn't exist
NOT_A_DIRECTORYif the path is not a directory
Example
import { readDirectory } from 'jg-fs';await readDirectory('dist'); // ['dist/index.js', 'dist/assets']await readDirectory('dist', true); // includes the contents of dist/assets
Reads and parses a JSON file, and validates it with the schema when there is one. Without a schema, the value is unknown, or the type you pass. With maxBytes, it reads the file as readBoundedFile does, for a file whose size isn't yours to decide.
A promise that resolves to the owner, with its metadata, or null when there's no lock, or its owner.json is missing or invalid, as after a crash while it was being acquired.
Example
import { readLockOwner } from 'jg-fs';const owner = await readLockOwner('.locks/nightly-report');owner?.metadata?.['command']; // what the owner wrote about itself, such as 'report'
Reads a file after checking that it's private, as assertPrivateFile does, through the same open file, so the content read is the content checked. It reads at most maxBytes bytes, even when the file grows while it's read.
Writes a file atomically: readers see either the previous content or the complete new content, never a partial write. The data goes to a temporary file in the same directory, which is flushed to disk and renamed over the path; on POSIX, the directory is flushed too. An explicit mode wins; without one, a replaced file keeps its permission bits and a new file gets the platform default. The temporary file is never more widely readable than the final mode. When writing fails before the rename, the temporary file is removed and the destination is untouched. While Windows refuses the rename because another process, such as an antivirus scanner, holds the file, it tries again for about 4 seconds. A symbolic link at the path is replaced by the file.
The permission bits of the written file, such as 0o600.
Returns
Promise<void>
A promise that resolves once the file is written and flushed.
Example
import { writeFileAtomic } from 'jg-fs';const state = { step: 2 };await writeFileAtomic('state.json', JSON.stringify(state)); // readers never see a partial fileawait writeFileAtomic('secrets.env', 'TOKEN=abc\n', 0o600); // only the owner can read it
Writes a file atomically, as writeFileAtomic does, only when it still holds what the caller read, such as a config file another program may have edited since: just before the rename, it reads the file again, without following a link, and compares its bytes with expected. It throws FILE_CHANGED, leaving the file as it is, when they differ, when the path is now a symbolic link or not a regular file, or when expected is null, for a file that didn't exist, and something now exists at the path. A change made between that read and the rename itself, a short window, isn't detected. An explicit mode wins; without one, a replaced file keeps its permission bits and a new file gets the platform default. The temporary file's permissions are set before writing.
Writes a JSON file atomically, as writeFileAtomic does, so a crash or a full disk never leaves a truncated file that won't parse. A symbolic link at the path keeps pointing at the file, whose target is replaced. A string is written as given; anything else is stringified.
Writes a file that only its owner can read and write, such as a token or a session. The file's directory must have no symbolic link in its path, and with rootPath, the file must be strictly inside that directory.
By default, it replaces the file atomically with 0o600 permissions, through a temporary file that's never more widely readable; a file already at the path must be private, as assertPrivateFile checks, so a symbolic link or a shared file is never overwritten. With isExclusive, it creates the file and fails when anything is already at the path.
what to write about the owner in owner.json, such as the command it runs, returned on the owner readLockOwner gives; it must fit owner.json's 4 KiB with the rest of the owner
recoverFromproperty
IFileLockOwner
the owner, as readLockOwner gave it, whose lock to take over once its process is gone
type IReadJsonFileOptions<T> = { schema?: ISchema<T>; maxBytes?: number;};
Members
schemaproperty
ISchema<T>
validates the parsed content, such as a Zod schema. Its errors propagate unchanged
maxBytesproperty
number
the largest file accepted, in bytes, for a file whose size isn't yours to decide, such as one a user uploaded. With it, the file is read as readBoundedFile reads it
Acquires a lock that only one process at a time can hold, such as one guarding a script that mustn't run twice at once. The lock is a directory at path, created with mode 0o700, holding an owner.json, mode 0o600, that names the process, with the metadata it's given, such as the command it runs.
A lock that already exists is never taken over by itself, even when its owner's process is gone, because only the caller knows whether that owner left work half done. It fails with LOCK_BUSY, whose data.reason says why: held, owner-gone, other-host, ownerless, recovery-in-progress, or unexpected-entries. To take over from an owner whose process is gone, read it with readLockOwner, check what it left, and pass it as recoverFrom: the lock is taken over only from that exact owner, on this host, when its process is gone and the directory holds nothing but owner.json. A lock left behind by a crash at the wrong moment stays busy until it's removed by hand, as the README describes.
Use it on local filesystems only. POSIX modes don't apply on Windows, and a lock is never taken over from another host.
What to write about the owner, and the dead owner to take the lock over from.
Defaults to {}.
Returns
IFileLock
The lock, released with release().
Throws
LOCK_BUSYThe lock '<path>' is busy.
NOT_A_DIRECTORYThe path '<path>' is not a directory.
LOCK_ACQUISITION_FAILEDThe lock '<path>' could not be acquired.
Example
import { acquireLock, readLockOwner, writeFileAtomic } from 'jg-fs/sync';const lock = acquireLock('.locks/nightly-report', { metadata: { command: 'report' } });try { writeFileAtomic('reports/nightly.json', JSON.stringify({ generatedAt: Date.now() }));} finally { lock.release();}// after a crash: take the lock over from the owner whose process is gone, once its work is checkedconst deadOwner = readLockOwner('.locks/nightly-report');if (deadOwner !== null) { acquireLock('.locks/nightly-report', { recoverFrom: deadOwner });}
Checks that no directory between a root and a path is a symbolic link, or, on Windows, a junction, such as before writing a file inside a repository that someone else may have changed. It reads each directory strictly below the root, down to the path's parent, without following it, and stops at the first that doesn't exist, which a write would create. The root itself and the path's last component aren't checked.
Checks that a directory is private to its owner: a directory that isn't a symbolic link, with no symbolic link anywhere in its path, and, on POSIX, without group or other permissions. On Windows, the permission check is skipped, as for files.
Signature
assertPrivateDirectory(path: string): void
Parameters
pathstring
The directory's path.
Throws
NON_EXISTENT_DIRECTORYif the path doesn't exist
NOT_A_DIRECTORYif the path is a symbolic link or isn't a directory
PATH_NOT_CANONICALif the path goes through a symbolic link
DIRECTORY_NOT_PRIVATEif other users can access the directory
Example
import { assertPrivateDirectory } from 'jg-fs/sync';assertPrivateDirectory('/home/ada/.config/app');
Checks that a file is private to its owner, such as a key or a session file before it's trusted: a regular file that isn't a symbolic link, with no symbolic link anywhere in its path, and, on POSIX, without group or other permissions. With rootPath, it must be strictly inside that directory; with maxBytes, no larger. The checks run on the open file, so a file swapped in between them is caught.
On Windows, Node.js reports only a file's read-only attribute as its mode, so the permission check is skipped there: a private file on Windows depends on its directory's access control list.
Whether to delete an existing directory and its contents first.
Throws
DIRECTORY_ALREADY_EXISTSif something other than a directory exists at the path
Example
import { createDirectory } from 'jg-fs/sync';createDirectory('dist/assets'); // creates missing parents toocreateDirectory('dist', true); // replaces it with an empty directory
Creates a directory, and any missing parents, with 0o700 permissions, and checks that it's private, as assertPrivateDirectory does. An existing directory that isn't private throws: its permissions are never changed silently.
Signature
createPrivateDirectory(path: string): void
Parameters
pathstring
The directory's path.
Throws
NOT_A_DIRECTORYif the path exists and isn't a directory
PATH_NOT_CANONICALif the path goes through a symbolic link
DIRECTORY_NOT_PRIVATEif other users can access the directory
Example
import { createPrivateDirectory } from 'jg-fs/sync';createPrivateDirectory('/home/ada/.config/app/sessions');
Lists a directory's elements grouped by kind: directories, files (filtered by extension), and symbolic links, each group sorted. Entries removed while the directory is read are left out.
Reads a path's details without following a symbolic link at the path.
Signature
getPathElement(path: string): IPathElement | null
Parameters
pathstring
The path.
Returns
IPathElement | null
The path's details, or null when it doesn't exist.
Example
import { getPathElement } from 'jg-fs/sync';const element = getPathElement('logo.png');element?.size; // the size in bytes, or undefined when the path doesn't exist
Checks whether a path is a root path or nested inside it. The check is lexical: both paths are resolved without touching the filesystem, and symbolic links aren't followed. Sibling paths that share a prefix, such as /data/app and /data/application, aren't nested, and neither is a path on another drive on Windows.
Reads a regular file of at most maxBytes bytes. It never follows a symbolic link in the path's last component, though links in parent directories are followed, so callers that need containment combine it with isPathWithin. It reads at most maxBytes + 1 bytes, even when the file grows while it's read.
Whether to include the contents of subdirectories.
Defaults to false.
Returns
string[]
The content paths.
Throws
NON_EXISTENT_DIRECTORYif the path doesn't exist
NOT_A_DIRECTORYif the path is not a directory
Example
import { readDirectory } from 'jg-fs/sync';readDirectory('dist'); // ['dist/index.js', 'dist/assets']readDirectory('dist', true); // includes the contents of dist/assets
Reads and parses a JSON file, and validates it with the schema when there is one. Without a schema, the value is unknown, or the type you pass. With maxBytes, it reads the file as readBoundedFile does, for a file whose size isn't yours to decide.
Signature
readJsonFile<T = unknown>(path: string, options?: IReadJsonFileOptions<T>): T
Parameters
pathstring
The file's path.
options?IReadJsonFileOptions<T>
The schema, such as a Zod schema, whose errors propagate unchanged, and the largest file accepted, in bytes.
Defaults to {}.
Returns
T
The parsed value.
Throws
NON_EXISTENT_FILEif the path doesn't exist
NOT_A_FILEif the path is not a file
FILE_TOO_LARGEif the file holds more than maxBytes bytes
FILE_CONTENT_IS_EMPTY_OR_INVALIDif the file's JSON content cannot be parsed
Example 1
import { readJsonFile } from 'jg-fs/sync';const config = readJsonFile('config.json', { schema: Config }); // Config's type
Example 2
import { readJsonFile } from 'jg-fs/sync';const value = readJsonFile('data.json'); // unknown
The owner, with its metadata, or null when there's no lock, or its owner.json is missing or invalid, as after a crash while it was being acquired.
Example
import { readLockOwner } from 'jg-fs/sync';const owner = readLockOwner('.locks/nightly-report');owner?.metadata?.['command']; // what the owner wrote about itself, such as 'report'
Reads a file after checking that it's private, as assertPrivateFile does, through the same open file, so the content read is the content checked. It reads at most maxBytes bytes, even when the file grows while it's read.
Writes a file atomically: readers see either the previous content or the complete new content, never a partial write. The data goes to a temporary file in the same directory, which is flushed to disk and renamed over the path; on POSIX, the directory is flushed too. An explicit mode wins; without one, a replaced file keeps its permission bits and a new file gets the platform default. The temporary file is never more widely readable than the final mode. When writing fails before the rename, the temporary file is removed and the destination is untouched. While Windows refuses the rename because another process, such as an antivirus scanner, holds the file, it tries again for about 4 seconds. A symbolic link at the path is replaced by the file.
The permission bits of the written file, such as 0o600.
Example
import { writeFileAtomic } from 'jg-fs/sync';const state = { step: 2 };writeFileAtomic('state.json', JSON.stringify(state)); // readers never see a partial filewriteFileAtomic('secrets.env', 'TOKEN=abc\n', 0o600); // only the owner can read it
Writes a file atomically, as writeFileAtomic does, only when it still holds what the caller read, such as a config file another program may have edited since: just before the rename, it reads the file again, without following a link, and compares its bytes with expected. It throws FILE_CHANGED, leaving the file as it is, when they differ, when the path is now a symbolic link or not a regular file, or when expected is null, for a file that didn't exist, and something now exists at the path. A change made between that read and the rename itself, a short window, isn't detected. An explicit mode wins; without one, a replaced file keeps its permission bits and a new file gets the platform default. The temporary file's permissions are set before writing.
Writes a JSON file atomically, as writeFileAtomic does, so a crash or a full disk never leaves a truncated file that won't parse. A symbolic link at the path keeps pointing at the file, whose target is replaced. A string is written as given; anything else is stringified.
Writes a file that only its owner can read and write, such as a token or a session. The file's directory must have no symbolic link in its path, and with rootPath, the file must be strictly inside that directory.
By default, it replaces the file atomically with 0o600 permissions, through a temporary file that's never more widely readable; a file already at the path must be private, as assertPrivateFile checks, so a symbolic link or a shared file is never overwritten. With isExclusive, it creates the file and fails when anything is already at the path.
what to write about the owner in owner.json, such as the command it runs, returned on the owner readLockOwner gives; it must fit owner.json's 4 KiB with the rest of the owner
recoverFromproperty
IFileLockOwner
the owner, as readLockOwner gave it, whose lock to take over once its process is gone
type IReadJsonFileOptions<T> = { schema?: ISchema<T>; maxBytes?: number;};
Members
schemaproperty
ISchema<T>
validates the parsed content, such as a Zod schema. Its errors propagate unchanged
maxBytesproperty
number
the largest file accepted, in bytes, for a file whose size isn't yours to decide, such as one a user uploaded. With it, the file is read as readBoundedFile reads it