zen-fs-webdav 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 zen-fs-webdav
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,261 @@
1
+ # zen-fs-webdav
2
+
3
+ 一个简单、现代的 WebDAV 客户端库,提供类似文件系统的 API。
4
+
5
+ ## 特性
6
+
7
+ - 🚀 简单易用的 API,类似于 Node.js 的 fs 模块
8
+ - 🔄 支持所有基本的 WebDAV 操作(读取、写入、复制、移动等)
9
+ - 🔒 支持基本认证和令牌认证
10
+ - 📁 支持目录操作和递归操作
11
+ - 💪 完全使用 TypeScript 编写,提供完整的类型定义
12
+ - 🌐 同时支持浏览器和 Node.js 环境
13
+
14
+ ## 安装
15
+
16
+ ```bash
17
+ npm install zen-fs-webdav
18
+ ```
19
+
20
+ 或者使用 yarn:
21
+
22
+ ```bash
23
+ yarn add zen-fs-webdav
24
+ ```
25
+
26
+ ## 快速开始
27
+
28
+ ```typescript
29
+ import { WebDAVFileSystem } from 'zen-fs-webdav';
30
+
31
+ // 创建 WebDAV 客户端实例
32
+ const fs = createWebDAVFileSystem({
33
+ baseUrl: 'https://example.com/webdav',
34
+ username: 'user',
35
+ password: 'pass',
36
+ });
37
+
38
+ // 读取文件
39
+ const content = await fs.readFile('/path/to/file.txt');
40
+ console.log(content);
41
+
42
+ // 写入文件
43
+ await fs.writeFile('/path/to/newfile.txt', 'Hello, WebDAV!');
44
+
45
+ // 列出目录内容
46
+ const files = await fs.readdir('/path/to/directory');
47
+ console.log(files);
48
+ ```
49
+
50
+ ## API 参考
51
+
52
+ ### 创建实例
53
+
54
+ ```typescript
55
+ const fs = new WebDAVFileSystem(options);
56
+ ```
57
+
58
+ #### 选项
59
+
60
+ - `baseUrl`: WebDAV 服务器的基础 URL
61
+ - `username`: 用户名(可选,用于基本认证)
62
+ - `password`: 密码(可选,用于基本认证)
63
+ - `token`: 认证令牌(可选,用于令牌认证)
64
+ - `headers`: 自定义请求头(可选)
65
+ - `fetch`: 自定义 fetch 函数(可选)
66
+
67
+ ### 文件操作
68
+
69
+ #### 检查文件是否存在
70
+
71
+ ```typescript
72
+ const exists = await fs.exists('/path/to/file.txt');
73
+ ```
74
+
75
+ #### 读取文件
76
+
77
+ ```typescript
78
+ // 读取为文本
79
+ const text = await fs.readFile('/path/to/file.txt');
80
+
81
+ // 读取为二进制数据
82
+ const binary = await fs.readFile('/path/to/file.bin', { responseType: 'arraybuffer' });
83
+ ```
84
+
85
+ #### 写入文件
86
+
87
+ ```typescript
88
+ // 写入文本
89
+ await fs.writeFile('/path/to/file.txt', 'Hello, WebDAV!');
90
+
91
+ // 写入二进制数据
92
+ const buffer = new ArrayBuffer(10);
93
+ await fs.writeFile('/path/to/file.bin', buffer);
94
+
95
+ // 指定内容类型
96
+ await fs.writeFile('/path/to/file.json', '{"key": "value"}', { contentType: 'application/json' });
97
+ ```
98
+
99
+ #### 删除文件
100
+
101
+ ```typescript
102
+ await fs.unlink('/path/to/file.txt');
103
+ ```
104
+
105
+ #### 获取文件信息
106
+
107
+ ```typescript
108
+ const stats = await fs.stat('/path/to/file.txt');
109
+ console.log(stats.size);
110
+ console.log(stats.lastModified);
111
+ console.log(stats.isDirectory);
112
+ ```
113
+
114
+ ### 目录操作
115
+
116
+ #### 创建目录
117
+
118
+ ```typescript
119
+ // 创建单个目录
120
+ await fs.mkdir('/path/to/directory');
121
+
122
+ // 递归创建目录
123
+ await fs.mkdir('/path/to/nested/directory', { recursive: true });
124
+ ```
125
+
126
+ #### 列出目录内容
127
+
128
+ ```typescript
129
+ // 列出顶层内容
130
+ const files = await fs.readdir('/path/to/directory');
131
+
132
+ // 递归列出所有内容
133
+ const allFiles = await fs.readdir('/path/to/directory', { depth: 'infinity' });
134
+ ```
135
+
136
+ #### 删除目录
137
+
138
+ ```typescript
139
+ // 删除空目录
140
+ await fs.rmdir('/path/to/directory');
141
+
142
+ // 递归删除目录及其内容
143
+ await fs.rmdir('/path/to/directory', { recursive: true });
144
+ ```
145
+
146
+ ### 复制和移动
147
+
148
+ #### 复制文件或目录
149
+
150
+ ```typescript
151
+ // 复制文件
152
+ await fs.copy('/source/file.txt', '/destination/file.txt');
153
+
154
+ // 复制目录
155
+ await fs.copy('/source/directory', '/destination/directory', { recursive: true });
156
+ ```
157
+
158
+ #### 移动文件或目录
159
+
160
+ ```typescript
161
+ await fs.move('/source/file.txt', '/destination/file.txt');
162
+ ```
163
+
164
+ #### 重命名文件或目录
165
+
166
+ ```typescript
167
+ await fs.rename('/path/to/oldname.txt', 'newname.txt');
168
+ ```
169
+
170
+ ### 流操作
171
+
172
+ #### 创建可读流
173
+
174
+ ```typescript
175
+ const readStream = await fs.createReadStream('/path/to/file.txt');
176
+ // 使用流...
177
+ ```
178
+
179
+ #### 创建可写流
180
+
181
+ ```typescript
182
+ const writeStream = fs.createWriteStream('/path/to/file.txt');
183
+ // 使用流...
184
+ writeStream.write('Hello');
185
+ writeStream.write(' WebDAV!');
186
+ await writeStream.close();
187
+ ```
188
+
189
+ ## 错误处理
190
+
191
+ 所有方法在失败时都会抛出 `WebDAVError` 异常:
192
+
193
+ ```typescript
194
+ try {
195
+ await fs.readFile('/path/to/nonexistent.txt');
196
+ } catch (error) {
197
+ if (error.name === 'WebDAVError') {
198
+ console.error(`WebDAV 错误: ${error.message}, 状态码: ${error.status}`);
199
+ } else {
200
+ console.error(`其他错误: ${error}`);
201
+ }
202
+ }
203
+ ```
204
+
205
+ ## 浏览器兼容性
206
+
207
+ 该库使用现代 Web API,如 `fetch` 和 `ReadableStream`/`WritableStream`。确保你的目标浏览器支持这些 API,或者使用适当的 polyfill。
208
+
209
+ ## 项目结构
210
+
211
+ ```
212
+ ├── .eslintrc.js - ESLint 配置文件
213
+ ├── .eslintrc.json - ESLint 配置文件
214
+ ├── .gitignore - Git 忽略规则
215
+ ├── .npmignore - NPM 发布忽略规则
216
+ ├── .prettierrc - Prettier 配置
217
+ ├── .prettierrc.js - Prettier 配置文件
218
+ ├── CHANGELOG.md - 变更日志
219
+ ├── CONTRIBUTING.md - 贡献指南
220
+ ├── coverage/ - 测试覆盖率报告
221
+ ├── docs/ - 文档
222
+ │ └── api.md - API 文档
223
+ ├── examples/ - 示例代码
224
+ │ ├── basic-usage.js - 基础使用示例(JS)
225
+ │ ├── basic-usage.ts - 基础使用示例(TS)
226
+ │ ├── browser-example.html - 浏览器示例
227
+ │ ├── browser-usage.ts - 浏览器使用示例
228
+ │ ├── node-example.js - Node.js 示例
229
+ │ ├── node-usage.ts - Node.js 使用示例
230
+ ├── jest.config.js - Jest 配置
231
+ ├── jest.setup.js - Jest 设置
232
+ ├── LICENSE - 许可证文件
233
+ ├── package.json - 项目配置
234
+ ├── package-lock.json - 依赖锁定文件
235
+ ├── prompt.md - 提示文档
236
+ ├── README.md - 项目说明文档
237
+ ├── rollup.config.js - Rollup 配置
238
+ ├── src/ - 源代码目录
239
+ │ ├── constants.ts - 常量定义
240
+ │ ├── errors.ts - 错误处理
241
+ │ ├── index.ts - 主入口文件
242
+ │ ├── types.ts - 类型定义
243
+ │ ├── ui/ - UI 相关代码
244
+ │ ├── utils.ts - 工具函数
245
+ │ ├── webdav-fs.ts - WebDAV 文件系统实现
246
+ │ ├── webdav.ts - WebDAV 核心功能
247
+ │ ├── WebDAVFileSystem.ts - WebDAV 文件系统类
248
+ │ └── __tests__/ - 测试代码
249
+ │ ├── errors.test.ts - 错误测试
250
+ │ ├── integration.test.ts - 集成测试
251
+ │ ├── utils.test.ts - 工具函数测试
252
+ │ ├── webdav-fs.test.ts - 文件系统测试
253
+ │ └── WebDAVFileSystem.test.ts - 文件系统类测试
254
+ ├── tsconfig.json - TypeScript 配置
255
+ ├── tsup.config.ts - Tsup 构建配置
256
+ └── yarn.lock - Yarn 依赖锁定文件
257
+ ```
258
+
259
+ ## 许可证
260
+
261
+ MIT
@@ -0,0 +1,278 @@
1
+ /**
2
+ * WebDAV 文件系统配置选项
3
+ */
4
+ interface WebDAVOptions {
5
+ /**
6
+ * WebDAV 服务器的基础 URL
7
+ */
8
+ baseUrl: string;
9
+ /**
10
+ * 认证用户名(可选)
11
+ */
12
+ username?: string;
13
+ /**
14
+ * 认证密码(可选)
15
+ */
16
+ password?: string;
17
+ /**
18
+ * 认证令牌(可选,如果提供则优先使用)
19
+ */
20
+ token?: string;
21
+ /**
22
+ * 自定义请求头(可选)
23
+ */
24
+ headers?: Record<string, string>;
25
+ /**
26
+ * 请求超时时间,单位毫秒(可选,默认 30000)
27
+ */
28
+ timeout?: number;
29
+ }
30
+ /**
31
+ * WebDAV 请求选项
32
+ */
33
+ interface WebDAVRequestOptions {
34
+ /**
35
+ * 请求方法
36
+ */
37
+ method: string;
38
+ /**
39
+ * 请求头
40
+ */
41
+ headers?: Record<string, string>;
42
+ /**
43
+ * 请求体
44
+ */
45
+ body?: string | ArrayBuffer | null;
46
+ /**
47
+ * 请求超时时间,单位毫秒
48
+ */
49
+ timeout?: number;
50
+ }
51
+ /**
52
+ * 文件或目录条目
53
+ */
54
+ interface FileEntry {
55
+ /**
56
+ * 文件或目录名称
57
+ */
58
+ name: string;
59
+ /**
60
+ * 是否为目录
61
+ */
62
+ isDirectory: boolean;
63
+ /**
64
+ * 文件大小(字节)
65
+ */
66
+ size?: number;
67
+ /**
68
+ * 最后修改时间
69
+ */
70
+ lastModified?: Date;
71
+ /**
72
+ * 创建时间
73
+ */
74
+ createdAt?: Date;
75
+ /**
76
+ * 文件或目录的完整路径
77
+ */
78
+ path: string;
79
+ }
80
+ /**
81
+ * 文件或目录统计信息
82
+ */
83
+ interface Stats {
84
+ /**
85
+ * 是否为目录
86
+ */
87
+ isDirectory: boolean;
88
+ /**
89
+ * 是否为文件
90
+ */
91
+ isFile: boolean;
92
+ /**
93
+ * 文件大小(字节)
94
+ */
95
+ size: number;
96
+ /**
97
+ * 最后修改时间
98
+ */
99
+ lastModified?: Date;
100
+ /**
101
+ * 创建时间
102
+ */
103
+ createdAt?: Date;
104
+ /**
105
+ * 文件或目录名称
106
+ */
107
+ name: string;
108
+ /**
109
+ * 文件或目录的完整路径
110
+ */
111
+ path: string;
112
+ /**
113
+ * 文件的 MIME 类型
114
+ */
115
+ mimeType?: string;
116
+ /**
117
+ * 文件的 ETag
118
+ */
119
+ etag?: string;
120
+ }
121
+ /**
122
+ * 读取文件选项
123
+ */
124
+ interface ReadFileOptions {
125
+ /**
126
+ * 响应类型
127
+ */
128
+ responseType?: 'text' | 'arraybuffer';
129
+ /**
130
+ * 编码(仅当 responseType 为 'text' 时有效)
131
+ */
132
+ encoding?: string;
133
+ headers?: Record<string, string>;
134
+ }
135
+ /**
136
+ * 写入文件选项
137
+ */
138
+ interface WriteFileOptions {
139
+ /**
140
+ * 是否覆盖现有文件
141
+ */
142
+ overwrite?: boolean;
143
+ /**
144
+ * 文件的 MIME 类型
145
+ */
146
+ contentType?: string;
147
+ }
148
+ /**
149
+ * 创建目录选项
150
+ */
151
+ interface MkdirOptions {
152
+ /**
153
+ * 是否递归创建目录
154
+ */
155
+ recursive?: boolean;
156
+ }
157
+ /**
158
+ * 删除目录选项
159
+ */
160
+ interface RmdirOptions {
161
+ /**
162
+ * 是否递归删除目录及其内容
163
+ */
164
+ recursive?: boolean;
165
+ }
166
+ /**
167
+ * 读取目录选项
168
+ */
169
+ interface ReaddirOptions {
170
+ /**
171
+ * 是否包含详细信息
172
+ */
173
+ withFileTypes?: boolean;
174
+ /**
175
+ * 深度,1 表示只列出当前目录,大于 1 表示递归列出子目录
176
+ */
177
+ depth?: number;
178
+ includeHidden?: boolean;
179
+ headers?: Record<string, string>;
180
+ recursive?: boolean;
181
+ }
182
+ /**
183
+ * 复制选项
184
+ */
185
+ interface CopyOptions {
186
+ /**
187
+ * 是否覆盖目标位置的现有文件
188
+ */
189
+ overwrite?: boolean;
190
+ /**
191
+ * 是否递归复制目录
192
+ */
193
+ recursive?: boolean;
194
+ }
195
+ /**
196
+ * 移动选项
197
+ */
198
+ interface MoveOptions {
199
+ /**
200
+ * 是否覆盖目标位置的现有文件
201
+ */
202
+ overwrite?: boolean;
203
+ }
204
+ /**
205
+ * WebDAV操作结果
206
+ */
207
+ interface WebDAVResult {
208
+ /**
209
+ * 操作是否成功
210
+ */
211
+ success: boolean;
212
+ /**
213
+ * HTTP状态码
214
+ */
215
+ statusCode: number;
216
+ /**
217
+ * 操作结果消息(可选)
218
+ */
219
+ message?: string;
220
+ /**
221
+ * 操作返回的数据(可选)
222
+ */
223
+ data?: unknown;
224
+ }
225
+
226
+ interface WebDAVFileSystem {
227
+ readFile(path: string, options?: ReadFileOptions): Promise<Buffer | string>;
228
+ writeFile(path: string, data: Buffer | string, options?: WriteFileOptions): Promise<WebDAVResult>;
229
+ deleteFile(path: string): Promise<WebDAVResult>;
230
+ readDir(path: string, options?: ReaddirOptions): Promise<Stats[]>;
231
+ mkdir(path: string, options?: MkdirOptions): Promise<void>;
232
+ rm(path: string, options?: {
233
+ recursive?: boolean;
234
+ force?: boolean;
235
+ }): Promise<void>;
236
+ rmdir(path: string, options?: boolean | {
237
+ recursive?: boolean;
238
+ force?: boolean;
239
+ }): Promise<void>;
240
+ stat(path: string): Promise<Stats>;
241
+ exists(path: string): Promise<boolean>;
242
+ copy(source: string, destination: string, overwrite?: boolean): Promise<WebDAVResult>;
243
+ move(source: string, destination: string, overwrite?: boolean): Promise<WebDAVResult>;
244
+ unlink(path: string): Promise<void>;
245
+ }
246
+
247
+ /**
248
+ * 创建 WebDAV 文件系统实例(工厂函数)
249
+ * @param options WebDAV 配置选项
250
+ * @returns WebDAVFileSystem 实例
251
+ */
252
+ declare function createWebDAVFileSystem(options: WebDAVOptions): WebDAVFileSystem;
253
+
254
+ /**
255
+ * WebDAV文件系统库的错误类型定义
256
+ */
257
+ /**
258
+ * WebDAV错误基类
259
+ */
260
+ declare class WebDAVError extends Error {
261
+ /**
262
+ * HTTP状态码
263
+ */
264
+ status?: number;
265
+ /**
266
+ * 原始错误对象(如果有)
267
+ */
268
+ cause?: unknown;
269
+ constructor(message: string, status?: number, cause?: unknown);
270
+ toString(): string;
271
+ static fromResponse(response: {
272
+ status: number;
273
+ statusText: string;
274
+ }, message?: string): WebDAVError;
275
+ static fromError(error: unknown): WebDAVError;
276
+ }
277
+
278
+ export { type CopyOptions, type FileEntry, type MkdirOptions, type MoveOptions, type ReadFileOptions, type ReaddirOptions, type RmdirOptions, type Stats, WebDAVError, type WebDAVFileSystem, type WebDAVOptions, type WebDAVRequestOptions, type WriteFileOptions, createWebDAVFileSystem };