vvvfs 0.1.4 → 0.1.5

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/README.md CHANGED
@@ -86,6 +86,10 @@ const vvvfs = new VVVFS("vvvfs", {
86
86
 
87
87
  ## 更新日志
88
88
 
89
+ ### 0.1.5
90
+
91
+ - 重新 `lock` 和 `unlock` 方法
92
+
89
93
  ### 0.1.4
90
94
 
91
95
  - 使用 AI 优化代码
package/index.html CHANGED
@@ -9,37 +9,7 @@
9
9
  </head>
10
10
 
11
11
  <body>
12
- <script>
13
- (async () => {
14
- try {
15
- globalThis.vvvfs = new VVVFS();
16
- vvvfs.options.throwError = true;
17
- await vvvfs.reset();
18
- await vvvfs.init("IFTC");
19
- console.log(vvvfs);
20
- vvvfs.watch("/test.txt", (type) => {
21
- console.log(type);
22
- // return true;
23
- });
24
- console.log("创建文件", await vvvfs.createFile("/test.txt"));
25
- console.log("写入文件", await vvvfs.writeText("/test.txt", "Hello World"));
26
- console.log("追加文件", await vvvfs.appendText("/test.txt", " - Appended"));
27
- // await vvvfs.lock("/test.txt");
28
- console.log("文件是否存在", await vvvfs.exists("/test.txt"));
29
- console.log("文件是否存在", await vvvfs.exists("/test2.txt"));
30
- console.log("读取文件", await vvvfs.readText("/test.txt"));
31
- console.log("读取文件块", await vvvfs.readTextChunk("/test.txt", 0, 5));
32
- console.log("复制文件", await vvvfs.copy("/test.txt", "/test2.txt"));
33
- console.log("搜索文件", await vvvfs.search("/", "t"));
34
- console.log("移动文件", await vvvfs.move("/test2.txt", "/test3.txt"));
35
- console.log("删除文件", await vvvfs.delete("/test.txt"));
36
- const file = new VVVFS.File("test.txt");
37
- console.log("写入文件", await file.writeText("Hello World"));
38
- } catch (error) {
39
- console.error(error);
40
- }
41
- })();
42
- </script>
12
+ <script src="index.js"></script>
43
13
  </body>
44
14
 
45
15
  </html>
package/index.js ADDED
@@ -0,0 +1,34 @@
1
+ /// <reference path="./types/index.d.ts" />
2
+ /**
3
+ * @class VVVFS
4
+ * @description VVVFS 虚拟文件系统
5
+ */
6
+ (async () => {
7
+ try {
8
+ globalThis.vvvfs = new VVVFS();
9
+ vvvfs.options.throwError = true;
10
+ await vvvfs.reset();
11
+ await vvvfs.init("IFTC");
12
+ console.log(vvvfs);
13
+ vvvfs.watch("/test.txt", (type) => {
14
+ console.log(type);
15
+ // return true;
16
+ });
17
+ console.log("创建文件", await vvvfs.createFile("/test.txt"));
18
+ console.log("写入文件", await vvvfs.writeText("/test.txt", "Hello World"));
19
+ console.log("追加文件", await vvvfs.appendText("/test.txt", " - Appended"));
20
+ // await vvvfs.lock("/test.txt");
21
+ console.log("文件是否存在", await vvvfs.exists("/test.txt"));
22
+ console.log("文件是否存在", await vvvfs.exists("/test2.txt"));
23
+ console.log("读取文件", await vvvfs.readText("/test.txt"));
24
+ console.log("读取文件块", await vvvfs.readTextChunk("/test.txt", 0, 5));
25
+ console.log("复制文件", await vvvfs.copy("/test.txt", "/test2.txt"));
26
+ console.log("搜索文件", await vvvfs.search("/", "t"));
27
+ console.log("移动文件", await vvvfs.move("/test2.txt", "/test3.txt"));
28
+ console.log("删除文件", await vvvfs.delete("/test.txt"));
29
+ const file = new VVVFS.File("test.txt");
30
+ console.log("写入文件", await file.writeText("Hello World"));
31
+ } catch (error) {
32
+ console.error(error);
33
+ }
34
+ })();
package/index.ts CHANGED
@@ -15,6 +15,7 @@ import mime from "mime";
15
15
  * @param path 文件路径
16
16
  * @param type 文件类型(file或dir)
17
17
  * @param file 文件对象
18
+ * @param locked 是否锁定
18
19
  */
19
20
  export interface FileRecord {
20
21
  id?: number;
@@ -22,6 +23,7 @@ export interface FileRecord {
22
23
  path: string;
23
24
  type: string;
24
25
  file: File | null;
26
+ locked?: boolean;
25
27
  }
26
28
 
27
29
  /**
@@ -264,6 +266,12 @@ class VVVFSFile {
264
266
  async unlock() {
265
267
  return await this._vvvfs.unlock(this._path);
266
268
  }
269
+ /**
270
+ * 判断文件是否已锁定
271
+ */
272
+ async isLocked() {
273
+ return await this._vvvfs.isLocked(this._path);
274
+ }
267
275
  }
268
276
 
269
277
  const version = packageJson.version;
@@ -284,10 +292,6 @@ class VVVFS {
284
292
  * 虚拟文件系统监听器
285
293
  */
286
294
  #watchers: Record<string, Array<(type: string) => Promise<boolean>>> = {};
287
- /**
288
- * 锁定的文件
289
- */
290
- #lockedFiles: Record<string, boolean> = {};
291
295
 
292
296
  /**
293
297
  * 检查文件访问权限(监听器和锁定状态)
@@ -309,7 +313,7 @@ class VVVFS {
309
313
  }
310
314
  }
311
315
  }
312
- if (this.#lockedFiles[path]) {
316
+ if (await this.#isLocked(path)) {
313
317
  if (this.options.throwError) {
314
318
  throw new VVVFSError(
315
319
  operation.charAt(0).toUpperCase() + operation.slice(1),
@@ -321,6 +325,15 @@ class VVVFS {
321
325
  return true;
322
326
  }
323
327
 
328
+ /**
329
+ * 内部判断是否锁定(跳过错误包装)
330
+ */
331
+ async #isLocked(path: string): Promise<boolean> {
332
+ const { name, parent } = parsePath(path);
333
+ const fileRecord = await this.#db.files.where({ name, path: parent }).first();
334
+ return !!fileRecord?.locked;
335
+ }
336
+
324
337
  /**
325
338
  * 错误处理包装器
326
339
  * @param operation 操作名称
@@ -364,6 +377,15 @@ class VVVFS {
364
377
  this.#db.version(1).stores({
365
378
  files: "++id, name, path, type, file, [name+path+type]",
366
379
  });
380
+ this.#db.version(2).stores({
381
+ files: "++id, name, path, type, file, locked, [name+path+type]",
382
+ }).upgrade(async (tx) => {
383
+ await tx.table("files").toCollection().modify((file: FileRecord) => {
384
+ if (file.locked === undefined) {
385
+ file.locked = false;
386
+ }
387
+ });
388
+ });
367
389
  } catch (error) {
368
390
  console.error("创建数据库失败", error);
369
391
  throw new VVVFSError("CreateDatabase", "创建数据库失败");
@@ -384,12 +406,14 @@ class VVVFS {
384
406
  path: "/",
385
407
  type: "dir" as const,
386
408
  file: new File([], name),
409
+ locked: false,
387
410
  }));
388
411
  linuxInitFiles.push({
389
412
  name: user || "root",
390
413
  path: "/home",
391
414
  type: "dir" as const,
392
415
  file: new File([], "home"),
416
+ locked: false,
393
417
  });
394
418
  try {
395
419
  for (const file of linuxInitFiles) {
@@ -411,6 +435,9 @@ class VVVFS {
411
435
  this.#db.version(1).stores({
412
436
  files: "++id, name, path, type, file, [name+path+type]",
413
437
  });
438
+ this.#db.version(2).stores({
439
+ files: "++id, name, path, type, file, locked, [name+path+type]",
440
+ });
414
441
  } catch (error) {
415
442
  console.error("重置数据库失败", error);
416
443
  throw new VVVFSError("ResetDatabase", "重置数据库失败" + error);
@@ -439,6 +466,7 @@ class VVVFS {
439
466
  file: new File([], name, {
440
467
  type: mime.getType(targetPath) || "application/octet-stream",
441
468
  }),
469
+ locked: false,
442
470
  });
443
471
  return true;
444
472
  }, false);
@@ -463,6 +491,7 @@ class VVVFS {
463
491
  path: "/",
464
492
  type: "dir",
465
493
  file: new File([], ""),
494
+ locked: false,
466
495
  });
467
496
  if (name === "") return true;
468
497
  } else {
@@ -474,6 +503,7 @@ class VVVFS {
474
503
  path: parent,
475
504
  type: "dir",
476
505
  file: new File([], name),
506
+ locked: false,
477
507
  });
478
508
  return true;
479
509
  }, false);
@@ -862,6 +892,7 @@ class VVVFS {
862
892
  path: newParent,
863
893
  type: fileRecord.type,
864
894
  file: fileRecord.file,
895
+ locked: false,
865
896
  });
866
897
  return true;
867
898
  }
@@ -922,16 +953,22 @@ class VVVFS {
922
953
  */
923
954
  async lock(path: string) {
924
955
  return this.#withErrorHandling("lock", async () => {
925
- path = joinPath(path);
926
- if (!(await this.exists(path))) {
956
+ const targetPath = joinPath(path);
957
+ if (!(await this.exists(targetPath))) {
927
958
  console.warn("文件不存在");
928
959
  return false;
929
960
  }
930
- if (this.#lockedFiles[path]) {
961
+ if (await this.#isLocked(targetPath)) {
931
962
  console.warn("文件已被锁定");
932
963
  return false;
933
964
  }
934
- this.#lockedFiles[path] = true;
965
+ const { name, parent } = parsePath(targetPath);
966
+ const fileRecord = await this.#db.files.where({ name, path: parent }).first();
967
+ if (!fileRecord?.id) {
968
+ console.warn("文件记录未找到");
969
+ return false;
970
+ }
971
+ await this.#db.files.update(fileRecord.id, { locked: true });
935
972
  return true;
936
973
  }, false);
937
974
  }
@@ -941,19 +978,38 @@ class VVVFS {
941
978
  */
942
979
  async unlock(path: string) {
943
980
  return this.#withErrorHandling("unlock", async () => {
944
- path = joinPath(path);
945
- if (!(await this.exists(path))) {
981
+ const targetPath = joinPath(path);
982
+ if (!(await this.exists(targetPath))) {
946
983
  console.warn("文件不存在");
947
984
  return false;
948
985
  }
949
- if (!this.#lockedFiles[path]) {
986
+ if (!(await this.#isLocked(targetPath))) {
950
987
  console.warn("文件未被锁定");
951
988
  return false;
952
989
  }
953
- delete this.#lockedFiles[path];
990
+ const { name, parent } = parsePath(targetPath);
991
+ const fileRecord = await this.#db.files.where({ name, path: parent }).first();
992
+ if (!fileRecord?.id) {
993
+ console.warn("文件记录未找到");
994
+ return false;
995
+ }
996
+ await this.#db.files.update(fileRecord.id, { locked: false });
954
997
  return true;
955
998
  }, false);
956
999
  }
1000
+ /**
1001
+ * 判断文件是否已锁定
1002
+ * @param path 文件路径
1003
+ */
1004
+ async isLocked(path: string) {
1005
+ return this.#withErrorHandling("isLocked", async () => {
1006
+ const targetPath = joinPath(path);
1007
+ if (!(await this.exists(targetPath))) {
1008
+ return false;
1009
+ }
1010
+ return await this.#isLocked(targetPath);
1011
+ }, false);
1012
+ }
957
1013
  }
958
1014
  /**
959
1015
  * 解析路径
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vvvfs",
3
- "version": "0.1.4",
3
+ "version": "0.1.5",
4
4
  "description": "一个使用IndexDB实现的虚拟文件系统",
5
5
  "keywords": [
6
6
  "Virtual",
@@ -0,0 +1,396 @@
1
+ import { Dexie, Table } from "dexie";
2
+
3
+ /**
4
+ * 虚拟文件系统
5
+ */
6
+ declare global {
7
+ // 扩展 Window 接口
8
+ interface window {
9
+ VVVFS: typeof VVVFS; // 如果 VVFS 是个类,这里可能需要调整,或者直接定义 vvfs 实例
10
+ }
11
+
12
+ // 扩展 GlobalThis (针对你在 index.js 里的 globalThis.vvfs 写法)
13
+ // 注意:GlobalThis 和 Window 往往指向同一个对象,但在 TS 类型中有时需要分别处理
14
+ // var VVVFS: typeof VVVFS;
15
+ }
16
+
17
+
18
+ /**
19
+ * 文件记录
20
+ * @param id 文件id
21
+ * @param name 文件名
22
+ * @param path 文件路径
23
+ * @param type 文件类型(file或dir)
24
+ * @param file 文件对象
25
+ * @param locked 是否锁定
26
+ */
27
+ declare interface FileRecord {
28
+ id?: number;
29
+ name: string;
30
+ path: string;
31
+ type: string;
32
+ file: File | null;
33
+ locked?: boolean;
34
+ }
35
+
36
+ /**
37
+ * 虚拟文件系统数据库
38
+ * @param files 文件存储数据表
39
+ */
40
+ declare interface VVVFSDatabase extends Dexie {
41
+ files: Table<FileRecord, number>;
42
+ }
43
+
44
+ /**
45
+ * 虚拟文件系统配置项
46
+ * @param throwError 是否抛出错误
47
+ */
48
+ declare interface VVVFSOptions {
49
+ throwError: boolean;
50
+ }
51
+
52
+ /**
53
+ * 虚拟文件系统错误类
54
+ */
55
+ declare class VVVFSError extends Error {
56
+ /**
57
+ * @param type 错误类型
58
+ * @param message 错误信息
59
+ */
60
+ constructor(type: string, message: string);
61
+ }
62
+
63
+ /**
64
+ * 虚拟文件系统文件类
65
+ * @param path 文件路径
66
+ */
67
+ declare class VVVFSFile {
68
+ /**
69
+ * 获取文件路径
70
+ */
71
+ get path(): string;
72
+ /**
73
+ * 设置文件路径
74
+ * @param path 文件路径
75
+ */
76
+ set path(path: string);
77
+ /**
78
+ * 设置数据库名称
79
+ * @param dbname 数据库名称
80
+ */
81
+ set dbname(dbname: string);
82
+ /**
83
+ * 获取数据库名称
84
+ */
85
+ get dbname(): string;
86
+ set options(options: VVVFSOptions);
87
+ get options(): VVVFSOptions;
88
+
89
+ /**
90
+ * 构造函数
91
+ * @param paths 文件路径
92
+ */
93
+ constructor(...paths: string[]);
94
+
95
+ /**
96
+ * 读取文件
97
+ */
98
+ read(): Promise<File | null>;
99
+ /**
100
+ * 读取文件文本内容
101
+ */
102
+ readText(): Promise<string | null>;
103
+ /**
104
+ * 读取文件JSON内容
105
+ */
106
+ readJSON(): Promise<Record<string, unknown> | null>;
107
+ /**
108
+ * 读取文件内容
109
+ * @param start 起始位置
110
+ * @param end 结束位置
111
+ */
112
+ readChunk(start: number, end: number): Promise<Blob | null>;
113
+ /**
114
+ * 读取文件内容
115
+ * @param start 起始位置
116
+ * @param end 结束位置
117
+ */
118
+ readTextChunk(start: number, end: number): Promise<string | null>;
119
+ /**
120
+ * 写入文件
121
+ * @param file 文件对象
122
+ */
123
+ write(file: Blob): Promise<boolean>;
124
+ /**
125
+ * 写入文件文本内容
126
+ * @param text 文件文本内容
127
+ */
128
+ writeText(text: string): Promise<boolean>;
129
+ /**
130
+ * 写入文件JSON内容
131
+ * @param json 文件JSON内容
132
+ */
133
+ writeJSON(json: Record<string, unknown>, format?: boolean): Promise<boolean>;
134
+ /**
135
+ * 追加文件内容
136
+ * @param file 文件对象
137
+ */
138
+ append(file: Blob): Promise<boolean>;
139
+ /**
140
+ * 追加文件文本内容
141
+ * @param text 文件文本内容
142
+ */
143
+ appendText(text: string): Promise<boolean>;
144
+ /**
145
+ * 创建文件
146
+ */
147
+ createFile(): Promise<boolean>;
148
+ /**
149
+ * 创建文件夹
150
+ */
151
+ createDir(): Promise<boolean>;
152
+ /**
153
+ * 删除文件
154
+ */
155
+ delete(): Promise<boolean>;
156
+ /**
157
+ * 判断文件是否存在
158
+ */
159
+ exists(): Promise<boolean>;
160
+ /**
161
+ * 获取文件大小
162
+ */
163
+ isFile(): Promise<boolean>;
164
+ /**
165
+ * 判断文件是否是文件夹
166
+ */
167
+ isDir(): Promise<boolean>;
168
+ /**
169
+ * 列出文件
170
+ */
171
+ list(): Promise<string[]>;
172
+ /**
173
+ * 重命名文件
174
+ */
175
+ rename(newName: string): Promise<boolean>;
176
+ /**
177
+ * 移动文件
178
+ */
179
+ move(newPath: string): Promise<boolean>;
180
+ /**
181
+ * 复制文件
182
+ */
183
+ copy(newPath: string): Promise<boolean>;
184
+ /**
185
+ * 搜索文件
186
+ */
187
+ search(query: string): Promise<string[] | null>;
188
+ /**
189
+ * 监听文件
190
+ */
191
+ watch(handler: (type: string) => Promise<boolean>): void;
192
+ /**
193
+ * 锁定文件
194
+ */
195
+ lock(): Promise<boolean>;
196
+ /**
197
+ * 解锁文件
198
+ */
199
+ unlock(): Promise<boolean>;
200
+ /**
201
+ * 判断文件是否已锁定
202
+ */
203
+ isLocked(): Promise<boolean>;
204
+ }
205
+
206
+ /**
207
+ * 虚拟文件系统
208
+ * @author IFTC
209
+ * @description 虚拟文件系统
210
+ */
211
+ declare class VVVFS {
212
+ static defaultDBName: string;
213
+ /**
214
+ * 虚拟文件系统版本
215
+ */
216
+ static readonly version: string;
217
+ /**
218
+ * 虚拟文件系统作者
219
+ */
220
+ static readonly author: string;
221
+ /**
222
+ * 虚拟文件系统文件类
223
+ */
224
+ static File: typeof VVVFSFile;
225
+
226
+ options: VVVFSOptions;
227
+
228
+ /**
229
+ * 创建虚拟文件系统
230
+ * @param name 虚拟文件系统名称
231
+ * @param options 配置项
232
+ */
233
+ constructor(name?: string, options?: VVVFSOptions);
234
+
235
+ /**
236
+ * 初始化虚拟文件系统
237
+ * @description 将Linux的系统初始文件初始化到数据库中
238
+ */
239
+ init(user?: string): Promise<void>;
240
+ /**
241
+ * 重置虚拟文件系统
242
+ */
243
+ reset(): Promise<void>;
244
+ /**
245
+ * 创建文件
246
+ * @param path 文件路径
247
+ */
248
+ createFile(path: string): Promise<boolean>;
249
+ /**
250
+ * 创建目录
251
+ * @param path 目录路径
252
+ */
253
+ createDir(path: string): Promise<boolean>;
254
+ /**
255
+ * 判断文件是否存在
256
+ * @param path 文件路径
257
+ */
258
+ exists(path: string): Promise<boolean>;
259
+ /**
260
+ * 读取文件内容
261
+ * @param path 文件路径
262
+ */
263
+ read(path: string): Promise<File | null>;
264
+ /**
265
+ * 读取文件文本内容
266
+ * @param path 文件路径
267
+ */
268
+ readText(path: string): Promise<string | null>;
269
+ /**
270
+ * 读取JSON内容
271
+ * @param path 文件路径
272
+ */
273
+ readJson(path: string): Promise<Record<string, unknown> | null>;
274
+ /**
275
+ * 写入文件
276
+ * @param path 文件路径
277
+ * @param content 文件内容
278
+ */
279
+ write(path: string, content: Blob): Promise<boolean>;
280
+ /**
281
+ * 写入文本内容
282
+ * @param path 文件路径
283
+ * @param content 文本内容
284
+ */
285
+ writeText(path: string, content: string): Promise<boolean>;
286
+ /**
287
+ * 写入JSON内容
288
+ * @param path 文件路径
289
+ * @param content JSON内容
290
+ * @param format 是否格式化
291
+ */
292
+ writeJson(path: string, content: Record<string, unknown>, format?: boolean): Promise<boolean>;
293
+ /**
294
+ * 追加内容
295
+ * @param path 文件路径
296
+ * @param content 追加内容
297
+ */
298
+ append(path: string, content: Blob): Promise<boolean>;
299
+ /**
300
+ * 追加文本内容
301
+ * @param path 文件路径
302
+ * @param content 追加内容
303
+ */
304
+ appendText(path: string, content: string): Promise<boolean>;
305
+ /**
306
+ * 读取文件内容
307
+ * @param path 文件路径
308
+ * @param start 开始位置
309
+ * @param end 结束位置
310
+ */
311
+ readChunk(path: string, start: number, end: number): Promise<Blob | null>;
312
+ /**
313
+ * 读取文件内容
314
+ * @param path 文件路径
315
+ * @param start 读取开始位置
316
+ * @param end 读取结束位置
317
+ */
318
+ readTextChunk(path: string, start: number, end: number): Promise<string | null>;
319
+ /**
320
+ * 判断是否是文件
321
+ * @param path 文件路径
322
+ */
323
+ isFile(path: string): Promise<boolean>;
324
+ /**
325
+ * 判断是否是目录
326
+ * @param path 文件路径
327
+ */
328
+ isDir(path: string): Promise<boolean>;
329
+ /**
330
+ * 列出目录下的文件
331
+ * @param path 目录路径
332
+ */
333
+ list(path: string): Promise<string[]>;
334
+ /**
335
+ * 重命名文件
336
+ * @param path 文件路径
337
+ * @param newName 新文件名
338
+ */
339
+ rename(path: string, newName: string): Promise<boolean>;
340
+ /**
341
+ * 删除文件
342
+ * @param path 文件路径
343
+ */
344
+ delete(path: string): Promise<boolean>;
345
+ /**
346
+ * 移动文件
347
+ * @param path 文件路径
348
+ * @param newPath 新路径
349
+ */
350
+ move(path: string, newPath: string): Promise<boolean>;
351
+ /**
352
+ * 复制文件
353
+ * @param path 文件路径
354
+ * @param newPath 新路径
355
+ */
356
+ copy(path: string, newPath: string): Promise<boolean>;
357
+ /**
358
+ * 搜索文件
359
+ * @param basePath 基础路径
360
+ * @param query 查询字符串
361
+ */
362
+ search(basePath: string, query: string): Promise<string[] | null>;
363
+ /**
364
+ * 监听文件
365
+ * @param path 文件路径
366
+ * @param handler 监听器
367
+ */
368
+ watch(path: string, handler: (type: string) => Promise<boolean>): void;
369
+ /**
370
+ * 锁定文件
371
+ * @param path 文件路径
372
+ */
373
+ lock(path: string): Promise<boolean>;
374
+ /**
375
+ * 解锁文件
376
+ * @param path 文件路径
377
+ */
378
+ unlock(path: string): Promise<boolean>;
379
+ /**
380
+ * 判断文件是否已锁定
381
+ * @param path 文件路径
382
+ */
383
+ isLocked(path: string): Promise<boolean>;
384
+ }
385
+
386
+ /**
387
+ * 解析路径
388
+ * @param path 路径
389
+ */
390
+ declare function parsePath(path: string): { name: string; parent: string };
391
+
392
+ /**
393
+ * 合并路径
394
+ * @param paths 路径
395
+ */
396
+ declare function joinPath(...paths: string[]): string;