vvvfs 0.0.1 → 0.0.3

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vvvfs",
3
- "version": "0.0.1",
3
+ "version": "0.0.3",
4
4
  "description": "一个使用IndexDB实现的虚拟文件系统",
5
5
  "keywords": [
6
6
  "Virtual",
package/index.js DELETED
@@ -1,142 +0,0 @@
1
- const { Dexie } = require('dexie');
2
- // globalThis.Dexie = Dexie;
3
-
4
- (async function () {
5
- globalThis.VVVFS = class {
6
- constructor(name = 'vvvfs', options = {}) {
7
- this.options = options;
8
- this.db = new Dexie(name);
9
- this.db.version(1).stores({
10
- files: '++id, name, path, type, file, mode, readonly, hidden'
11
- });
12
- }
13
- async createUser(username, password) {
14
- if (await this.exists(username)) throw new Error('User already exists');
15
- await this.mkdirs("/home/" + username);
16
- }
17
- async write(path, file) {
18
- if (!(file instanceof File)) throw new Error('file must be a File object');
19
- if (path.endsWith('/')) throw new Error('Path cannot end with a slash');
20
- if (!await this.exists(path)) await this.createFile(path);
21
- const fileEntry = await this.db.files.where('path').equals(path).first();
22
- if (fileEntry.readonly) throw new Error('File is read-only');
23
- await this.db.files.update(path, { file });
24
- }
25
- async mkdirs(path) {
26
- if (path.endsWith('/')) path = path.slice(0, -1);
27
- const exists = await this.exists(path);
28
- console.log('exists', exists);
29
- if (exists) return;
30
- const dirs = path.split('/').filter(Boolean);
31
- console.log('mkdirs', dirs);
32
- for (let i = 0; i < dirs.length; i++) {
33
- if (dirs[i] === '') continue;
34
- const dir = "/" + dirs.slice(0, i + 1).join('/');
35
- if (await this.exists(dir)) continue;
36
- console.log('mkdir', dir);
37
- await this.db.files.add({
38
- name: dirs[i],
39
- path: dir,
40
- type: 'directory',
41
- file: new File([], dir),
42
- mode: 0o755,
43
- readonly: false,
44
- hidden: false
45
- });
46
- }
47
- }
48
- async createFile(path) {
49
- if (path.endsWith('/')) throw new Error('Path cannot end with a slash');
50
- const dir = path.split('/').slice(0, -1).join('/');
51
- console.log('createFile', path, dir);
52
- if (dir) await this.mkdirs(dir);
53
- const name = path.split('/').slice(-1)[0];
54
- console.log('createFile', name);
55
- await this.db.files.add({
56
- name,
57
- path,
58
- type: 'file',
59
- file: new File([], path),
60
- mode: 0o644,
61
- readonly: false,
62
- hidden: false
63
- });
64
- }
65
- async exists(path) {
66
- const file = await this.db.files.where('path').equals(path).first();
67
- return !!file;
68
- }
69
- async isDirectory(path) {
70
- const file = await this.db.files.where('path').equals(path).first();
71
- return file && file.type === 'directory';
72
- }
73
- async isFile(path) {
74
- const file = await this.db.files.where('path').equals(path).first();
75
- return file && file.type === 'file';
76
- }
77
- async read(path) {
78
- const file = await this.db.files.where('path').equals(path).first();
79
- return file;
80
- }
81
- async delete(path) {
82
- await this.db.files.where('path').equals(path).delete();
83
- }
84
- async list(path) {
85
- const files = await this.db.files.where('path').startsWith(path).toArray();
86
- return files;
87
- }
88
- async chmod(path, mode) {
89
- await this.db.files.update(path, { mode });
90
- }
91
- async rename(oldPath, newPath) {
92
- const file = await this.db.files.where('path').equals(oldPath).first();
93
- if (!file) throw new Error('File not found');
94
- if (await this.exists(newPath)) throw new Error('File already exists');
95
- await this.db.files.where('path').equals(oldPath).delete();
96
- console.log('rename', oldPath, newPath);
97
- const name = newPath.split('/').slice(-1)[0];
98
- console.log('rename', name);
99
- await this.db.files.add({
100
- ...file,
101
- name,
102
- path: newPath
103
- });
104
- }
105
- async reset() {
106
- await this.db.files.clear();
107
- }
108
- async move(oldPath, newPath) {
109
- await this.rename(oldPath, newPath);
110
- }
111
- async copy(oldPath, newPath) {
112
- const file = await this.db.files.where('path').equals(oldPath).first();
113
- if (!file) throw new Error('File not found');
114
- if (await this.exists(newPath)) throw new Error('File already exists');
115
- await this.db.files.add({
116
- ...file,
117
- path: newPath
118
- });
119
- }
120
- async chown(path, owner, group) {
121
- await this.db.files.update(path, { owner, group });
122
- }
123
- async stat(path) {
124
- const file = await this.db.files.where('path').equals(path).first();
125
- return file;
126
- }
127
- async isReadonly(path) {
128
- const file = await this.db.files.where('path').equals(path).first();
129
- return file && file.readonly;
130
- }
131
- async setReadonly(path, readonly) {
132
- await this.db.files.update(path, { readonly });
133
- }
134
- async isHidden(path) {
135
- const file = await this.db.files.where('path').equals(path).first();
136
- return file && file.hidden;
137
- }
138
- async setHidden(path, hidden) {
139
- await this.db.files.update(path, { hidden });
140
- }
141
- };
142
- })();