vvvfs 0.1.6 → 0.1.8

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
@@ -2,6 +2,8 @@
2
2
 
3
3
  一个使用Dexie.js,基于IndexedDB的轻量、简单的浏览器中的Linux虚拟文件系统
4
4
 
5
+ GitHub仓库: https://github.com/IFTC-XLKJ/vvvfs
6
+
5
7
  ## 快速开始
6
8
 
7
9
  1. 安装依赖
@@ -25,6 +27,7 @@ const VVVFS = require("vvvfs"); // CommonJS
25
27
  <script src="https://cdn.jsdelivr.net/npm/vvvfs@latest/dist/vvvfs.min.js"></script>
26
28
  <script src="https://unpkg.com/vvvfs@latest/dist/vvvfs.min.js"></script>
27
29
  <!-- 浏览器 -->
30
+ <!-- GitHub Release: https://github.com/IFTC-XLKJ/vvvfs/releases -->
28
31
  ```
29
32
 
30
33
  3. 初始化
@@ -32,6 +35,7 @@ const VVVFS = require("vvvfs"); // CommonJS
32
35
  ```javascript
33
36
  const vvvfs = new VVVFS("vvvfs", {
34
37
  throwError: true, // 设置为true后,操作文件失败时,不会返回false,而是会抛出错误
38
+ init: "IFTC", // 自动初始化,用户名为 IFTC
35
39
  }); // 创建一个名为vvvfs的虚拟文件系统
36
40
  ```
37
41
 
@@ -92,6 +96,14 @@ const vvvfs = new VVVFS("vvvfs", {
92
96
 
93
97
  ## 更新日志
94
98
 
99
+ ### 0.1.8
100
+
101
+ - 新增 `writeJsonValue` 和 `readJsonValue` 方法,用于读取和写入JSON值(键支持字符串、数字数组下标以及嵌套路径,如 `["a", "b"]`)
102
+
103
+ ### 0.1.7
104
+
105
+ - 新增 `init` 选项,用于是否自动初始化,传入字符串时,则为初始化的用户名,传入true时,则为默认用户名。
106
+
95
107
  ### 0.1.6
96
108
 
97
109
  - 新增 `VVVFS.path` ,用法与 NodeJS 的 `path` 模块类似。
@@ -168,6 +180,8 @@ const vvvfs = new VVVFS("vvvfs", {
168
180
 
169
181
  A lightweight and simple Linux virtual file system in the browser, based on IndexedDB, built with Dexie.js
170
182
 
183
+ GitHub repository: https://github.com/IFTC-XLKJ/vvvfs
184
+
171
185
  ## Quick Start
172
186
 
173
187
  1. Install the dependency
@@ -198,6 +212,7 @@ const VVVFS = require("vvvfs"); // CommonJS
198
212
  ```javascript
199
213
  const vvvfs = new VVVFS("vvvfs", {
200
214
  throwError: true, // When set to true, failed file operations throw an error instead of returning false
215
+ init: "IFTC", // Automatically initialize with the username IFTC
201
216
  }); // Create a virtual file system named vvvfs
202
217
  ```
203
218
 
@@ -258,6 +273,14 @@ Using the `VVVFS.File` class:
258
273
 
259
274
  ## Changelog
260
275
 
276
+ ### 0.1.8
277
+
278
+ - Added the `writeJsonValue` and `readJsonValue` methods for reading and writing JSON values (keys support strings, numeric array indexes, and nested paths such as `["a", "b"]`)
279
+
280
+ ### 0.1.7
281
+
282
+ - Added the `init` option to control automatic initialization: when passed a string, it is used as the username for initialization; when passed `true`, the default username is used.
283
+
261
284
  ### 0.1.6
262
285
 
263
286
  - Added `VVVFS.path`, which works in a similar way to the `path` module in NodeJS.
package/index.d.ts ADDED
@@ -0,0 +1,6 @@
1
+ /**
2
+ * 虚拟文件系统
3
+ * @author IFTC
4
+ * @description 虚拟文件系统
5
+ */
6
+ export * from "./types/index";
package/index.ts CHANGED
@@ -37,9 +37,11 @@ export interface VVVFSDatabase extends Dexie {
37
37
  /**
38
38
  * 虚拟文件系统配置项
39
39
  * @param throwError 是否抛出错误
40
+ * @param init 是否自动初始化,传入字符串时,为用户名,传入true时,使用默认用户名
40
41
  */
41
42
  export interface VVVFSOptions {
42
43
  throwError: boolean;
44
+ init: boolean | string;
43
45
  }
44
46
 
45
47
  /**
@@ -168,6 +170,21 @@ class VVVFSFile {
168
170
  async writeJSON(json: Record<string, unknown>, format: boolean = true) {
169
171
  return await this._vvvfs.writeJson(this._path, json, format);
170
172
  }
173
+ /**
174
+ * 写入文件JSON值
175
+ * @param key 键名
176
+ * @param value 键值
177
+ */
178
+ async writeJsonValue(key: string | number | Array<string | number>, value: any) {
179
+ return await this._vvvfs.writeJsonValue(this._path, key, value);
180
+ }
181
+ /**
182
+ * 读取文件JSON值
183
+ * @param key 键名
184
+ */
185
+ async readJsonValue(key: string | number | Array<string | number>): Promise<any> {
186
+ return await this._vvvfs.readJsonValue(this._path, key);
187
+ }
171
188
  /**
172
189
  * 追加文件内容
173
190
  * @param file 文件对象
@@ -582,6 +599,7 @@ class VVVFS {
582
599
  name?: string,
583
600
  options = {
584
601
  throwError: false,
602
+ init: false,
585
603
  },
586
604
  ) {
587
605
  this.options = options;
@@ -599,6 +617,7 @@ class VVVFS {
599
617
  }
600
618
  });
601
619
  });
620
+ this.options.init ? this.init(this.options.init == true ? "root" : this.options.init) : void 0;
602
621
  } catch (error) {
603
622
  console.error("创建数据库失败", error);
604
623
  throw new VVVFSError("CreateDatabase", "创建数据库失败");
@@ -824,6 +843,61 @@ class VVVFS {
824
843
  return await this.append(path, blob);
825
844
  }, false);
826
845
  }
846
+ /**
847
+ * 写入 JSON 值
848
+ * @param path 文件路径
849
+ * @param key 键名
850
+ * @param value 键值
851
+ * @example await vvvfs.writeJsonValue("/example.json", "a", { b: "example"});
852
+ * @example await vvvfs.writeJsonValue("/example.json", ["a", "b"], "example2");
853
+ * @example await vvvfs.writeJsonValue("/example.json", 0, "example");
854
+ */
855
+ async writeJsonValue(path: string, key: string | number | Array<string | number>, value: any) {
856
+ return this.#withErrorHandling("writeJsonValue", async () => {
857
+ let root: any = (await this.readJson(path)) ?? {};
858
+ const keys = Array.isArray(key) ? key : [key];
859
+ if (keys.length > 0) {
860
+ let json: any = root;
861
+ for (let i = 0; i < keys.length - 1; i++) {
862
+ const currentKey = keys[i];
863
+ const nextKey = keys[i + 1];
864
+ if (
865
+ json[currentKey] === undefined ||
866
+ json[currentKey] === null ||
867
+ typeof json[currentKey] !== "object"
868
+ ) {
869
+ json[currentKey] = typeof nextKey === "number" ? [] : {};
870
+ }
871
+ json = json[currentKey];
872
+ }
873
+ json[keys[keys.length - 1]] = value;
874
+ } else {
875
+ root = value;
876
+ }
877
+ return await this.writeJson(path, root);
878
+ }, false);
879
+ }
880
+ /**
881
+ * 读取 JSON 值
882
+ * @param path 文件路径
883
+ * @param key 键名
884
+ * @example await vvvfs.readJsonValue("/example.json", "a");
885
+ * @example await vvvfs.readJsonValue("/example.json", ["a", "b"]);
886
+ * @example await vvvfs.readJsonValue("/example.json", 0);
887
+ */
888
+ async readJsonValue(path: string, key: string | number | Array<string | number>): Promise<any> {
889
+ return this.#withErrorHandling("readJsonValue", async () => {
890
+ const json = await this.readJson(path);
891
+ if (json === null || json === undefined) return null;
892
+ const keys = Array.isArray(key) ? key : [key];
893
+ let current: any = json;
894
+ for (const keyPart of keys) {
895
+ if (current === null || current === undefined) return null;
896
+ current = current[keyPart];
897
+ }
898
+ return current === undefined ? null : current;
899
+ }, null as any);
900
+ }
827
901
  /**
828
902
  * 内部读取(跳过访问检查,供内部方法调用)
829
903
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vvvfs",
3
- "version": "0.1.6",
3
+ "version": "0.1.8",
4
4
  "description": "一个使用IndexDB实现的虚拟文件系统",
5
5
  "keywords": [
6
6
  "Virtual",
@@ -17,6 +17,7 @@
17
17
  "author": "IFTC",
18
18
  "type": "module",
19
19
  "main": "index.js",
20
+ "types": "index.d.ts",
20
21
  "scripts": {
21
22
  "test": "echo \"Error: no test specified\" && exit 1",
22
23
  "build": "node esbuild.config.js"
package/types/index.d.ts CHANGED
@@ -1,17 +1,28 @@
1
1
  import { Dexie, Table } from "dexie";
2
2
 
3
+ /**
4
+ * VVVFS 类构造签名(避免在内 declare global 中自引用)
5
+ */
6
+ type VVVFSConstructor = typeof VVVFS;
7
+
8
+ /**
9
+ * VVVFS 类实例类型
10
+ */
11
+ type VVVFSInstance = VVVFS;
12
+
3
13
  /**
4
14
  * 虚拟文件系统
5
15
  */
6
16
  declare global {
7
17
  // 扩展 Window 接口
8
- interface window {
9
- VVVFS: typeof VVVFS; // 如果 VVFS 是个类,这里可能需要调整,或者直接定义 vvfs 实例
18
+ interface Window {
19
+ VVVFS: VVVFSConstructor;
20
+ vvvfs: VVVFSInstance;
10
21
  }
11
22
 
12
23
  // 扩展 GlobalThis (针对你在 index.js 里的 globalThis.vvfs 写法)
13
- // 注意:GlobalThis 和 Window 往往指向同一个对象,但在 TS 类型中有时需要分别处理
14
- // var VVVFS: typeof VVVFS;
24
+ var VVVFS: VVVFSConstructor;
25
+ var vvvfs: VVVFSInstance;
15
26
  }
16
27
 
17
28
 
@@ -24,7 +35,7 @@ declare global {
24
35
  * @param file 文件对象
25
36
  * @param locked 是否锁定
26
37
  */
27
- declare interface FileRecord {
38
+ export interface FileRecord {
28
39
  id?: number;
29
40
  name: string;
30
41
  path: string;
@@ -37,22 +48,24 @@ declare interface FileRecord {
37
48
  * 虚拟文件系统数据库
38
49
  * @param files 文件存储数据表
39
50
  */
40
- declare interface VVVFSDatabase extends Dexie {
51
+ export interface VVVFSDatabase extends Dexie {
41
52
  files: Table<FileRecord, number>;
42
53
  }
43
54
 
44
55
  /**
45
56
  * 虚拟文件系统配置项
46
57
  * @param throwError 是否抛出错误
58
+ * @param init 是否自动初始化,传入字符串时,为用户名,传入true时,使用默认用户名
47
59
  */
48
- declare interface VVVFSOptions {
60
+ export interface VVVFSOptions {
49
61
  throwError: boolean;
62
+ init: boolean | string;
50
63
  }
51
64
 
52
65
  /**
53
66
  * 虚拟文件系统错误类
54
67
  */
55
- declare class VVVFSError extends Error {
68
+ export class VVVFSError extends Error {
56
69
  /**
57
70
  * @param type 错误类型
58
71
  * @param message 错误信息
@@ -64,7 +77,7 @@ declare class VVVFSError extends Error {
64
77
  * 虚拟文件系统文件类
65
78
  * @param path 文件路径
66
79
  */
67
- declare class VVVFSFile {
80
+ export class VVVFSFile {
68
81
  /**
69
82
  * 获取文件路径
70
83
  */
@@ -131,6 +144,17 @@ declare class VVVFSFile {
131
144
  * @param json 文件JSON内容
132
145
  */
133
146
  writeJSON(json: Record<string, unknown>, format?: boolean): Promise<boolean>;
147
+ /**
148
+ * 写入文件JSON值
149
+ * @param key 键名
150
+ * @param value 键值
151
+ */
152
+ writeJsonValue(key: string | number | Array<string | number>, value: any): Promise<boolean>;
153
+ /**
154
+ * 读取文件JSON值
155
+ * @param key 键名
156
+ */
157
+ readJsonValue(key: string | number | Array<string | number>): Promise<any>;
134
158
  /**
135
159
  * 追加文件内容
136
160
  * @param file 文件对象
@@ -206,7 +230,7 @@ declare class VVVFSFile {
206
230
  /**
207
231
  * path类(类似于NodeJS中的path模块)
208
232
  */
209
- declare class Path {
233
+ export class Path {
210
234
  /**
211
235
  * 路径分隔符
212
236
  */
@@ -312,7 +336,7 @@ declare class Path {
312
336
  * @author IFTC
313
337
  * @description 虚拟文件系统
314
338
  */
315
- declare class VVVFS {
339
+ export class VVVFS {
316
340
  static defaultDBName: string;
317
341
  /**
318
342
  * 虚拟文件系统版本
@@ -398,6 +422,25 @@ declare class VVVFS {
398
422
  * @param format 是否格式化
399
423
  */
400
424
  writeJson(path: string, content: Record<string, unknown>, format?: boolean): Promise<boolean>;
425
+ /**
426
+ * 写入 JSON 值
427
+ * @param path 文件路径
428
+ * @param key 键名
429
+ * @param value 键值
430
+ * @example await vvvfs.writeJsonValue("/example.json", "a", { b: "example"});
431
+ * @example await vvvfs.writeJsonValue("/example.json", ["a", "b"], "example2");
432
+ * @example await vvvfs.writeJsonValue("/example.json", 0, "example");
433
+ */
434
+ writeJsonValue(path: string, key: string | number | Array<string | number>, value: any): Promise<boolean>;
435
+ /**
436
+ * 读取 JSON 值
437
+ * @param path 文件路径
438
+ * @param key 键名
439
+ * @example await vvvfs.readJsonValue("/example.json", "a");
440
+ * @example await vvvfs.readJsonValue("/example.json", ["a", "b"]);
441
+ * @example await vvvfs.readJsonValue("/example.json", 0);
442
+ */
443
+ readJsonValue(path: string, key: string | number | Array<string | number>): Promise<any>;
401
444
  /**
402
445
  * 追加内容
403
446
  * @param path 文件路径
@@ -495,10 +538,10 @@ declare class VVVFS {
495
538
  * 解析路径
496
539
  * @param path 路径
497
540
  */
498
- declare function parsePath(path: string): { name: string; parent: string };
541
+ export function parsePath(path: string): { name: string; parent: string };
499
542
 
500
543
  /**
501
544
  * 合并路径
502
545
  * @param paths 路径
503
546
  */
504
- declare function joinPath(...paths: string[]): string;
547
+ export function joinPath(...paths: string[]): string;