@dbx-tools/shared-fs

中文文档
0.9.206发布于 2026/11/11Apache-2.0TypeScriptES Module构建来源已验证近 30 天下载 1.3万

Browser-safe filesystem contract and abstract base for rooted storage backends

原始使用文档
文档目录4 节
  1. Quick start
  2. Choose a runtime
  3. Use cache and backend helpers
  4. Package exports

@dbx-tools/shared-fs

Use one filesystem contract across memory, local disk, Unity Catalog volumes, and other rooted storage backends. This browser-safe package provides the portable FileSystem interface, BaseFileSystem, normalized errors, path helpers, caching, and an in-memory adapter.

Quick start

import { MemoryFileSystem } from "@dbx-tools/shared-fs";

const files = new MemoryFileSystem();
await files.writeFile("note.txt", "ready");

The filesystem contract covers:

  • Portable FileSystem interface for read, write, append, copy, move, directory, stat, and existence operations.
  • BaseFileSystem roots accept one or many segments. Objects are FNV-hashed, and true and 1 are stringified. 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/[email protected]/My Notes survive intact.
  • BaseFileSystem lets a new backend implement mostly *At primitives. It provides memoized _init, optional createRoot, toBackendPath, parent creation before mutations, POSIX-only paths, encoding, recursive directory operations, and append, copy, and move fallbacks.
  • Every primitive is invoked through a guard that routes failures to a mapError hook, so an adapter writes no try/catch of its own and cannot return an unnormalized error.
  • MemoryFileSystem in-process adapter for tests and reference implementations.
  • baseFS.mapFileSystemError / baseFS.inferFileSystemErrorCode on @dbx-tools/shared-core error helpers.
  • posixPath helpers that convert roots/joins to /-separated form (posixPath.toPosix and posixPath.join).
  • Typed FileSystemError codes 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, and stat are cached by default. Other methods delegate directly unless selected explicitly. Cache owners expose keys() so directory move/delete operations can invalidate related ancestor and descendant entries. read, invalidate, and keys may be synchronous or asynchronous. Keys use <stable-hash>_<canonical-rooted-backend-path>.

Choose a runtime

Use this package when several backends must implement the same API. Browser code can use the contracts, path helpers, cache decorator, and MemoryFileSystem. Node hosts provide concrete adapters such as @dbx-tools/fs, including LocalFileSystem.

Use 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.

Package exports

ExportRole
FileSystemPortable filesystem contract
BaseFileSystemAbstract base over *At primitives with memoized _init
baseFS.normalizeFileSystemRootJoin root segments (stringify / FNV-hash)
FileSystemRootInputroot option: one or many non-null segments
MemoryFileSystemIn-memory adapter (tests / reference)
memoryFSThe module behind it, if a subclass needs its internals
baseFS.mapFileSystemErrorWrap backend failures into FileSystemError
baseFS.inferFileSystemErrorCodeInfer a code from HTTP status / message tokens
posixPathPOSIX root/join/normalize helpers for namespace paths
posixPath.isHomeRelativePath~ / ~/... detection, shared by every adapter
posixPath.expandHomeExpand ~ against a home, with a pluggable join
posixPath.toRelativeSegmentStrip leading ~ / separators so input joins under a base
FileSystemErrorTyped filesystem failure
FileEntry / FileStat / options typesShared data types

版本与发布记录

@dbx-tools/shared-fs 共发布 285 个版本,首次发布于 2026/07/31;过去 12 个月发布了 285 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.9.206latest2026/11/11今天
0.9.2052026/11/111 天前
0.9.2042026/11/101 天前
0.9.2032026/11/074 天前
0.9.2022026/11/065 天前
0.9.2012026/11/056 天前

关于 @dbx-tools/shared-fs 的常见问题

@dbx-tools/shared-fs 是什么?

Browser-safe filesystem contract and abstract base for rooted storage backends

如何安装 @dbx-tools/shared-fs?

在项目目录运行 npm install @dbx-tools/shared-fs,或使用 pnpm add / yarn add / bun add。

@dbx-tools/shared-fs 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@dbx-tools/shared-fs 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@dbx-tools/shared-fs 可以商用吗?

@dbx-tools/shared-fs 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@dbx-tools/shared-fs 的下载量有多少?

根据 npm 官方下载统计,@dbx-tools/shared-fs 在 2026/10/12 – 2026/11/10 这 30 天内被下载 12,581 次,日均约 419 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@dbx-tools/shared-fs 还在维护吗?

最新版本 0.9.206 发布于 2026/11/11(今天);至今共发布 285 个版本,其中过去 12 个月发布了 285 个;首次发布于 2026/07/31。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。