@dbx-tools/shared-fs
Browser-safe filesystem contract and abstract base for rooted storage backends.
The package defines the portable filesystem API used by local disk, Databricks,
memory, and other storage backends. BaseFileSystem supplies rooted paths,
common operations, and normalized errors for new adapters.
Quick Start
Section titled “Quick Start”import { MemoryFileSystem } from "@dbx-tools/shared-fs";
const files = new MemoryFileSystem();await files.writeFile("note.txt", "ready");Key features:
- Portable
FileSysteminterface (read/write/append/copy/move, mkdir/rmdir/readdir/stat/exists) BaseFileSystemroot accepts one or many segments ("/path", objects → FNV hash,true/1stringified); strings are split on/and only a component that cannot be a path component (separator, NUL / control character, or..) is FNV-hashed, so real names like/Workspace/Users/me@corp.com/My Notessurvive intactBaseFileSystemso a new backend mostly implements*Atprimitives: memoized_init, optionalcreateRoot,toBackendPath, parent creation before write/append/copy/move, POSIX-only paths, encoding, recursive mkdir/rmdir/readdir, and append/copy/move fallbacks- Every primitive is invoked through a guard that routes failures to a
mapErrorhook, so an adapter writes no try/catch of its own and cannot return an unnormalized error MemoryFileSystemin-process adapter for tests and as a reference implementationbaseFS.mapFileSystemError/baseFS.inferFileSystemErrorCodeon@dbx-tools/shared-coreerrorhelpersposixPathhelpers that convert roots/joins to/-separated form (posixPath.toPosix,posixPath.join, …)- Typed
FileSystemErrorcodes for portable failure handling - Lazy debug logs for every public filesystem operation. Set
LOG_LEVEL=debug; operation metadata is not constructed at normal log levels. cache()read-through decorator with automatic mutation invalidation.exists,readdir, andstatare cached by default; other methods delegate directly unless selected explicitly. Cache owners exposekeys()so directory move/delete operations can invalidate related ancestor and descendant entries.read,invalidate, andkeysmay be synchronous or asynchronous. Keys use<stable-hash>_<canonical-rooted-backend-path>.
Use The Shared Contract
Section titled “Use The Shared Contract”Use this when multiple backends (local disk, object storage, Databricks volumes, in-memory) should share one API. Node hosts implement concrete adapters such as @dbx-tools/fs (LocalFileSystem).
Cache And Backend Helpers
Section titled “Cache And Backend Helpers”import type { FileSystem, FileSystemCache } from "@dbx-tools/shared-fs";import { BaseFileSystem, FileSystemError, fs, MemoryFileSystem, posixPath,} from "@dbx-tools/shared-fs";
const mem = new MemoryFileSystem();await mem.writeFile("note.txt", "hi");const cached = fs.cache(mem, myCache satisfies FileSystemCache);await cached.stat("note.txt");
const selected = fs.cache(mem, myCache, { operations: ["readFile", "readdir"], filter: (operation, path) => operation === "readdir" || path.startsWith("/skills/"),});The default caches exists, readdir, and stat. filter receives the
operation and normalized absolute path, so an owning package can compile its
own glob or authorization policy without adding a path-matcher dependency here.
Modules
Section titled “Modules”| Export | Role |
|---|---|
FileSystem |
Portable filesystem contract |
BaseFileSystem |
Abstract base over *At primitives; memoized _init |
baseFS.normalizeFileSystemRoot |
Join root segments (stringify / FNV-hash) |
FileSystemRootInput |
root option: one or many non-null segments |
MemoryFileSystem |
In-memory adapter (tests / reference) |
memoryFS |
The module behind it, if a subclass needs its internals |
baseFS.mapFileSystemError |
Wrap backend failures into FileSystemError |
baseFS.inferFileSystemErrorCode |
Infer a code from HTTP status / message tokens |
posixPath |
POSIX root/join/normalize helpers for namespace paths |
posixPath.isHomeRelativePath |
~ / ~/... detection, shared by every adapter |
posixPath.expandHome |
Expand ~ against a home, with a pluggable join |
posixPath.toRelativeSegment |
Strip leading ~ / separators so input joins UNDER a base |
FileSystemError |
Typed filesystem failure |
FileEntry / FileStat / options types |
Shared wire shapes |
Adjacent: @dbx-tools/fs (local disk adapter).