vvvfs 0.1.7 → 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 +9 -0
- package/index.d.ts +6 -0
- package/index.ts +70 -0
- package/package.json +2 -1
- package/types/index.d.ts +53 -12
package/README.md
CHANGED
|
@@ -27,6 +27,7 @@ const VVVFS = require("vvvfs"); // CommonJS
|
|
|
27
27
|
<script src="https://cdn.jsdelivr.net/npm/vvvfs@latest/dist/vvvfs.min.js"></script>
|
|
28
28
|
<script src="https://unpkg.com/vvvfs@latest/dist/vvvfs.min.js"></script>
|
|
29
29
|
<!-- 浏览器 -->
|
|
30
|
+
<!-- GitHub Release: https://github.com/IFTC-XLKJ/vvvfs/releases -->
|
|
30
31
|
```
|
|
31
32
|
|
|
32
33
|
3. 初始化
|
|
@@ -95,6 +96,10 @@ const vvvfs = new VVVFS("vvvfs", {
|
|
|
95
96
|
|
|
96
97
|
## 更新日志
|
|
97
98
|
|
|
99
|
+
### 0.1.8
|
|
100
|
+
|
|
101
|
+
- 新增 `writeJsonValue` 和 `readJsonValue` 方法,用于读取和写入JSON值(键支持字符串、数字数组下标以及嵌套路径,如 `["a", "b"]`)
|
|
102
|
+
|
|
98
103
|
### 0.1.7
|
|
99
104
|
|
|
100
105
|
- 新增 `init` 选项,用于是否自动初始化,传入字符串时,则为初始化的用户名,传入true时,则为默认用户名。
|
|
@@ -268,6 +273,10 @@ Using the `VVVFS.File` class:
|
|
|
268
273
|
|
|
269
274
|
## Changelog
|
|
270
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
|
+
|
|
271
280
|
### 0.1.7
|
|
272
281
|
|
|
273
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.
|
package/index.d.ts
ADDED
package/index.ts
CHANGED
|
@@ -170,6 +170,21 @@ class VVVFSFile {
|
|
|
170
170
|
async writeJSON(json: Record<string, unknown>, format: boolean = true) {
|
|
171
171
|
return await this._vvvfs.writeJson(this._path, json, format);
|
|
172
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
|
+
}
|
|
173
188
|
/**
|
|
174
189
|
* 追加文件内容
|
|
175
190
|
* @param file 文件对象
|
|
@@ -828,6 +843,61 @@ class VVVFS {
|
|
|
828
843
|
return await this.append(path, blob);
|
|
829
844
|
}, false);
|
|
830
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
|
+
}
|
|
831
901
|
/**
|
|
832
902
|
* 内部读取(跳过访问检查,供内部方法调用)
|
|
833
903
|
*/
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vvvfs",
|
|
3
|
-
"version": "0.1.
|
|
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
|
|
9
|
-
VVVFS:
|
|
18
|
+
interface Window {
|
|
19
|
+
VVVFS: VVVFSConstructor;
|
|
20
|
+
vvvfs: VVVFSInstance;
|
|
10
21
|
}
|
|
11
22
|
|
|
12
23
|
// 扩展 GlobalThis (针对你在 index.js 里的 globalThis.vvfs 写法)
|
|
13
|
-
|
|
14
|
-
|
|
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
|
-
|
|
38
|
+
export interface FileRecord {
|
|
28
39
|
id?: number;
|
|
29
40
|
name: string;
|
|
30
41
|
path: string;
|
|
@@ -37,7 +48,7 @@ declare interface FileRecord {
|
|
|
37
48
|
* 虚拟文件系统数据库
|
|
38
49
|
* @param files 文件存储数据表
|
|
39
50
|
*/
|
|
40
|
-
|
|
51
|
+
export interface VVVFSDatabase extends Dexie {
|
|
41
52
|
files: Table<FileRecord, number>;
|
|
42
53
|
}
|
|
43
54
|
|
|
@@ -54,7 +65,7 @@ export interface VVVFSOptions {
|
|
|
54
65
|
/**
|
|
55
66
|
* 虚拟文件系统错误类
|
|
56
67
|
*/
|
|
57
|
-
|
|
68
|
+
export class VVVFSError extends Error {
|
|
58
69
|
/**
|
|
59
70
|
* @param type 错误类型
|
|
60
71
|
* @param message 错误信息
|
|
@@ -66,7 +77,7 @@ declare class VVVFSError extends Error {
|
|
|
66
77
|
* 虚拟文件系统文件类
|
|
67
78
|
* @param path 文件路径
|
|
68
79
|
*/
|
|
69
|
-
|
|
80
|
+
export class VVVFSFile {
|
|
70
81
|
/**
|
|
71
82
|
* 获取文件路径
|
|
72
83
|
*/
|
|
@@ -133,6 +144,17 @@ declare class VVVFSFile {
|
|
|
133
144
|
* @param json 文件JSON内容
|
|
134
145
|
*/
|
|
135
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>;
|
|
136
158
|
/**
|
|
137
159
|
* 追加文件内容
|
|
138
160
|
* @param file 文件对象
|
|
@@ -208,7 +230,7 @@ declare class VVVFSFile {
|
|
|
208
230
|
/**
|
|
209
231
|
* path类(类似于NodeJS中的path模块)
|
|
210
232
|
*/
|
|
211
|
-
|
|
233
|
+
export class Path {
|
|
212
234
|
/**
|
|
213
235
|
* 路径分隔符
|
|
214
236
|
*/
|
|
@@ -314,7 +336,7 @@ declare class Path {
|
|
|
314
336
|
* @author IFTC
|
|
315
337
|
* @description 虚拟文件系统
|
|
316
338
|
*/
|
|
317
|
-
|
|
339
|
+
export class VVVFS {
|
|
318
340
|
static defaultDBName: string;
|
|
319
341
|
/**
|
|
320
342
|
* 虚拟文件系统版本
|
|
@@ -400,6 +422,25 @@ declare class VVVFS {
|
|
|
400
422
|
* @param format 是否格式化
|
|
401
423
|
*/
|
|
402
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>;
|
|
403
444
|
/**
|
|
404
445
|
* 追加内容
|
|
405
446
|
* @param path 文件路径
|
|
@@ -497,10 +538,10 @@ declare class VVVFS {
|
|
|
497
538
|
* 解析路径
|
|
498
539
|
* @param path 路径
|
|
499
540
|
*/
|
|
500
|
-
|
|
541
|
+
export function parsePath(path: string): { name: string; parent: string };
|
|
501
542
|
|
|
502
543
|
/**
|
|
503
544
|
* 合并路径
|
|
504
545
|
* @param paths 路径
|
|
505
546
|
*/
|
|
506
|
-
|
|
547
|
+
export function joinPath(...paths: string[]): string;
|