# @chunkd/fs 中文文档

规范地址：https://npmcn.com/package/%40chunkd/fs/

版本：11\.5\.3
npm 元数据更新时间：2026-10-23T02:41:21.305Z
中文译文更新时间：2026-11-09T15:37:48.800Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40chunkd/fs
原文对照：https://npmcn.com/package/%40chunkd/fs/?lang=original
项目主页：https://github.com/blacha/chunkd#readme
源码仓库：https://github.com/blacha/chunkd

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：158 kB，79 个文件
- 模块格式：ES Module
- TypeScript 类型：包内自带
- Node.js 要求：\>=16\.0\.0
- 运行依赖：4 个
- 首次发布：2021-09-15T02:07:45.197Z
- 最新版本发布：2026-10-23T02:41:20.932Z
- 近 30 天下载量：2699（2026-10-10 至 2026-11-08，npm 官方下载统计）
- 已发布版本数：41（近 12 个月 7 个）

## 中文简介

文件系统抽象层

## 安装

```sh
npm install @chunkd/fs
```

也可以使用：`pnpm add @chunkd/fs`、`yarn add @chunkd/fs`、`bun add @chunkd/fs`

## README

# @ chunkd/ fs (中文(简体) )

文件系统抽象层

方便本地文件系统或远程ASync应用程序与文件系统如接口工作

-   [文件](https://npmcn.com/package/%40chunkd/fs/src/systems/file) - 读取/写入本地文件系统
-   [内存](https://npmcn.com/package/%40chunkd/fs/src/systems/file) - 作为模拟文件系统的内存
-   [小时](https://npmcn.com/package/%40chunkd/fs/src/systems/http.ts) -HTTP请求到远程网络服务器
-   [自动取款机](https://www.npmjs.com/package/@chunkd/fs-aws) - AWS S3作为远程文件系统

## 使用量

### 基础

读取文本文件

```typescript
import { fsa } from '@chunkd/fs';

const bytes = await fsa.read(fsa.toUrl('../foo.txt'));
```

列出文件夹

```typescript
for await (const url of fsa.list(fsa.toUrl('/'))) {
  const bytes = await fsa.read(url);
}
```

### 多文件系统

注册多个文件系统

```typescript
import { fsa, FsMemory } from '@chunkd/fs';

fsa.register('memory://', new FsMemory());

await fsa.write(fsa.toUrl('memory://foo.txt'), 'Hello World');

// Stream the file from memory into the local file system
await fsa.write(fsa.toUrl('/foo.txt'), fsa.readStream(fsa.toUrl(()'memory://foo.txt')));
```

### 远程文件系统

远程文件系统也可以使用

```typescript
import { fsa, FsHttp } from '@chunkd/fs';
import { FsAwsS3 } from '@chunkd/fs-aws';

fsa.register('https://', new FsHttp());
fsa.register('s3://', new FsAwsS3());

// Stream a file from https://example.com into AWS S3
await fsa.write(fsa.toUrl('s3://example/foo.txt'), fsa.readStream(fsa.toUrl('https://example.com/foo.txt')));
```

### 多个 S3 证书

多个 s3 客户端可以用不同的证书注册,然后用于在 AWS 账户或角色之间流出文件

```typescript
import { fsa } from '@chunkd/fs';
import { FsAwsS3 } from '@chunkd/fs-aws';

fsa.register('s3://bucket-a/', new FsAwsS3(new S3Client({ credentials: bucketA })));
fsa.register('s3://bucket-b/', new FsAwsS3(new S3Client({ credentials: bucketB })));

fsa.write(fsa.toUrl('s3://bucket-a/foo.txt'), fsa.readStream(fsa.toUrl('s3://bucket-b/foo.txt')));
```

### 错误

所有文件系统错误都应该被包入一个 `FsError`

````typescript
try {
  await fsa.read('s3://foo/bar.txt')
} catch(e) {
  if(FsError.is(e)) {
    e.status // Http Status code eg 403
    e.cause // original error
    e.action // `read`
    e.url // `s3://foo/bar.txt`
  }
}

# Interface

All file systems implement then following interface interface

```typescript
export interface FileSystem {
  /** Read a file into a buffer */
  read(location: URL): Promise<Buffer>;
  /** Create a read stream */
  readStream(location: URL): Readable;
  /** Write a file from either a buffer or stream */
  write(location: URL, buffer: Buffer | Readable | string, opts?: Partial<WriteOptions>): Promise<void>;
  /** list all files in path */
  list(location: URL, opt?: ListOptions): AsyncGenerator<URL>;
  /** list all files with file info in path */
  details(location: URL, opt?: ListOptions): AsyncGenerator<FileInfo>;
  /** Get information about the path  */
  head(location: URL): Promise<FileInfo | null>;
  /** Create a chunk source to read chunks out of */
  source(location: URL): Source;
  /** Delete a file from the location */
  delete(location: URL): Promise<void>;
}
````

## 常见问题

### @chunkd/fs 是什么？

文件系统抽象层

### 如何安装 @chunkd/fs？

在项目目录运行 npm install @chunkd/fs，或使用 pnpm add / yarn add / bun add。

### @chunkd/fs 支持 TypeScript 吗？

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

### @chunkd/fs 支持 ESM 还是 CommonJS？

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

### @chunkd/fs 需要什么版本的 Node\.js？

package\.json 的 engines 字段要求 Node\.js \>=16\.0\.0。版本不符时 npm 默认只给出警告，开启 engine-strict 后会拒绝安装。

### @chunkd/fs 可以商用吗？

@chunkd/fs 使用 MIT 许可。宽松许可：允许商业使用、修改和再分发，通常只需在分发时保留原版权与许可声明。以上为概括，不构成法律意见，请以许可原文为准。

### @chunkd/fs 的下载量有多少？

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

### @chunkd/fs 还在维护吗？

最新版本 11\.5\.3 发布于 2026/10/23（17 天前）；至今共发布 41 个版本，其中过去 12 个月发布了 7 个；首次发布于 2021/09/15。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

