@zenfs/dom 0.0.3 → 0.0.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.
@@ -1,6 +1,5 @@
1
- /// <reference types="node" resolution-mode="require"/>
2
1
  import { Cred } from '@zenfs/core/cred.js';
3
- import { File, FileFlag, PreloadFile } from '@zenfs/core/file.js';
2
+ import { FileFlag, PreloadFile } from '@zenfs/core/file.js';
4
3
  import { BaseFileSystem, FileSystemMetadata } from '@zenfs/core/filesystem.js';
5
4
  import { Stats } from '@zenfs/core/stats.js';
6
5
  import { type BackendOptions } from '@zenfs/core/backends/backend.js';
@@ -15,8 +14,8 @@ declare global {
15
14
  interface FileSystemAccessFileSystemOptions {
16
15
  handle: FileSystemDirectoryHandle;
17
16
  }
18
- export declare class FileSystemAccessFile extends PreloadFile<FileSystemAccessFileSystem> implements File {
19
- constructor(_fs: FileSystemAccessFileSystem, _path: string, _flag: FileFlag, _stat: Stats, contents?: Buffer);
17
+ export declare class FileSystemAccessFile extends PreloadFile<FileSystemAccessFileSystem> {
18
+ constructor(_fs: FileSystemAccessFileSystem, _path: string, _flag: FileFlag, _stat: Stats, contents?: Uint8Array);
20
19
  sync(): Promise<void>;
21
20
  close(): Promise<void>;
22
21
  }
@@ -28,13 +27,13 @@ export declare class FileSystemAccessFileSystem extends BaseFileSystem {
28
27
  private _handles;
29
28
  constructor({ handle }: FileSystemAccessFileSystemOptions);
30
29
  get metadata(): FileSystemMetadata;
31
- _sync(p: string, data: Buffer, stats: Stats, cred: Cred): Promise<void>;
30
+ _sync(p: string, data: Uint8Array, stats: Stats, cred: Cred): Promise<void>;
32
31
  rename(oldPath: string, newPath: string, cred: Cred): Promise<void>;
33
- writeFile(fname: string, data: any, encoding: string | null, flag: FileFlag, mode: number, cred: Cred, createFile?: boolean): Promise<void>;
34
- createFile(p: string, flag: FileFlag, mode: number, cred: Cred): Promise<File>;
32
+ writeFile(fname: string, data: Uint8Array, flag: FileFlag, mode: number, cred: Cred, createFile?: boolean): Promise<void>;
33
+ createFile(p: string, flag: FileFlag, mode: number, cred: Cred): Promise<FileSystemAccessFile>;
35
34
  stat(path: string, cred: Cred): Promise<Stats>;
36
35
  exists(p: string, cred: Cred): Promise<boolean>;
37
- openFile(path: string, flags: FileFlag, cred: Cred): Promise<File>;
36
+ openFile(path: string, flags: FileFlag, cred: Cred): Promise<FileSystemAccessFile>;
38
37
  unlink(path: string, cred: Cred): Promise<void>;
39
38
  rmdir(path: string, cred: Cred): Promise<void>;
40
39
  mkdir(p: string, mode: any, cred: Cred): Promise<void>;
@@ -1,19 +1,3 @@
1
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
- return new (P || (P = Promise))(function (resolve, reject) {
4
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
- step((generator = generator.apply(thisArg, _arguments || [])).next());
8
- });
9
- };
10
- var __asyncValues = (this && this.__asyncValues) || function (o) {
11
- if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
12
- var m = o[Symbol.asyncIterator], i;
13
- return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
14
- function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
15
- function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
16
- };
17
1
  var _a;
18
2
  import { basename, dirname, join } from '@zenfs/core/emulation/path.js';
19
3
  import { ApiError, ErrorCode } from '@zenfs/core/ApiError.js';
@@ -22,7 +6,6 @@ import { FileFlag, PreloadFile } from '@zenfs/core/file.js';
22
6
  import { BaseFileSystem } from '@zenfs/core/filesystem.js';
23
7
  import { Stats, FileType } from '@zenfs/core/stats.js';
24
8
  import { CreateBackend } from '@zenfs/core/backends/backend.js';
25
- import { Buffer } from 'buffer';
26
9
  const handleError = (path = '', error) => {
27
10
  if (error.name === 'NotFoundError') {
28
11
  throw ApiError.ENOENT(path);
@@ -33,18 +16,14 @@ export class FileSystemAccessFile extends PreloadFile {
33
16
  constructor(_fs, _path, _flag, _stat, contents) {
34
17
  super(_fs, _path, _flag, _stat, contents);
35
18
  }
36
- sync() {
37
- return __awaiter(this, void 0, void 0, function* () {
38
- if (this.isDirty()) {
39
- yield this._fs._sync(this.getPath(), this.getBuffer(), this.getStats(), Cred.Root);
40
- this.resetDirty();
41
- }
42
- });
19
+ async sync() {
20
+ if (this.isDirty()) {
21
+ await this._fs._sync(this.getPath(), this.getBuffer(), this.getStats(), Cred.Root);
22
+ this.resetDirty();
23
+ }
43
24
  }
44
- close() {
45
- return __awaiter(this, void 0, void 0, function* () {
46
- yield this.sync();
47
- });
25
+ async close() {
26
+ await this.sync();
48
27
  }
49
28
  }
50
29
  export class FileSystemAccessFileSystem extends BaseFileSystem {
@@ -57,204 +36,170 @@ export class FileSystemAccessFileSystem extends BaseFileSystem {
57
36
  this._handles.set('/', handle);
58
37
  }
59
38
  get metadata() {
60
- return Object.assign(Object.assign({}, super.metadata), { name: _a.Name });
61
- }
62
- _sync(p, data, stats, cred) {
63
- return __awaiter(this, void 0, void 0, function* () {
64
- const currentStats = yield this.stat(p, cred);
65
- if (stats.mtime !== currentStats.mtime) {
66
- yield this.writeFile(p, data, null, FileFlag.getFileFlag('w'), currentStats.mode, cred);
67
- }
68
- });
69
- }
70
- rename(oldPath, newPath, cred) {
71
- return __awaiter(this, void 0, void 0, function* () {
72
- try {
73
- const handle = yield this.getHandle(oldPath);
74
- if (handle instanceof FileSystemDirectoryHandle) {
75
- const files = yield this.readdir(oldPath, cred);
76
- yield this.mkdir(newPath, 'wx', cred);
77
- if (files.length === 0) {
78
- yield this.unlink(oldPath, cred);
79
- }
80
- else {
81
- for (const file of files) {
82
- yield this.rename(join(oldPath, file), join(newPath, file), cred);
83
- yield this.unlink(oldPath, cred);
84
- }
85
- }
39
+ return {
40
+ ...super.metadata,
41
+ name: _a.Name,
42
+ };
43
+ }
44
+ async _sync(p, data, stats, cred) {
45
+ const currentStats = await this.stat(p, cred);
46
+ if (stats.mtime !== currentStats.mtime) {
47
+ await this.writeFile(p, data, FileFlag.getFileFlag('w'), currentStats.mode, cred);
48
+ }
49
+ }
50
+ async rename(oldPath, newPath, cred) {
51
+ try {
52
+ const handle = await this.getHandle(oldPath);
53
+ if (handle instanceof FileSystemDirectoryHandle) {
54
+ const files = await this.readdir(oldPath, cred);
55
+ await this.mkdir(newPath, 'wx', cred);
56
+ if (files.length === 0) {
57
+ await this.unlink(oldPath, cred);
86
58
  }
87
- if (handle instanceof FileSystemFileHandle) {
88
- const oldFile = yield handle.getFile(), destFolder = yield this.getHandle(dirname(newPath));
89
- if (destFolder instanceof FileSystemDirectoryHandle) {
90
- const newFile = yield destFolder.getFileHandle(basename(newPath), { create: true });
91
- const writable = yield newFile.createWritable();
92
- const buffer = yield oldFile.arrayBuffer();
93
- yield writable.write(buffer);
94
- writable.close();
95
- yield this.unlink(oldPath, cred);
59
+ else {
60
+ for (const file of files) {
61
+ await this.rename(join(oldPath, file), join(newPath, file), cred);
62
+ await this.unlink(oldPath, cred);
96
63
  }
97
64
  }
98
65
  }
99
- catch (err) {
100
- handleError(oldPath, err);
101
- }
102
- });
103
- }
104
- writeFile(fname, data, encoding, flag, mode, cred, createFile) {
105
- return __awaiter(this, void 0, void 0, function* () {
106
- const handle = yield this.getHandle(dirname(fname));
107
- if (handle instanceof FileSystemDirectoryHandle) {
108
- const file = yield handle.getFileHandle(basename(fname), { create: true });
109
- const writable = yield file.createWritable();
110
- yield writable.write(data);
111
- yield writable.close();
112
- //return createFile ? this.newFile(fname, flag, data) : undefined;
113
- }
114
- });
115
- }
116
- createFile(p, flag, mode, cred) {
117
- return __awaiter(this, void 0, void 0, function* () {
118
- yield this.writeFile(p, Buffer.alloc(0), null, flag, mode, cred, true);
119
- return this.openFile(p, flag, cred);
120
- });
121
- }
122
- stat(path, cred) {
123
- return __awaiter(this, void 0, void 0, function* () {
124
- const handle = yield this.getHandle(path);
125
- if (!handle) {
126
- throw ApiError.FileError(ErrorCode.EINVAL, path);
127
- }
128
- if (handle instanceof FileSystemDirectoryHandle) {
129
- return new Stats(FileType.DIRECTORY, 4096);
130
- }
131
66
  if (handle instanceof FileSystemFileHandle) {
132
- const { lastModified, size } = yield handle.getFile();
133
- return new Stats(FileType.FILE, size, undefined, undefined, lastModified);
67
+ const oldFile = await handle.getFile(), destFolder = await this.getHandle(dirname(newPath));
68
+ if (destFolder instanceof FileSystemDirectoryHandle) {
69
+ const newFile = await destFolder.getFileHandle(basename(newPath), { create: true });
70
+ const writable = await newFile.createWritable();
71
+ const buffer = await oldFile.arrayBuffer();
72
+ await writable.write(buffer);
73
+ writable.close();
74
+ await this.unlink(oldPath, cred);
75
+ }
134
76
  }
135
- });
136
- }
137
- exists(p, cred) {
138
- return __awaiter(this, void 0, void 0, function* () {
77
+ }
78
+ catch (err) {
79
+ handleError(oldPath, err);
80
+ }
81
+ }
82
+ async writeFile(fname, data, flag, mode, cred, createFile) {
83
+ const handle = await this.getHandle(dirname(fname));
84
+ if (handle instanceof FileSystemDirectoryHandle) {
85
+ const file = await handle.getFileHandle(basename(fname), { create: true });
86
+ const writable = await file.createWritable();
87
+ await writable.write(data);
88
+ await writable.close();
89
+ //return createFile ? this.newFile(fname, flag, data) : undefined;
90
+ }
91
+ }
92
+ async createFile(p, flag, mode, cred) {
93
+ await this.writeFile(p, new Uint8Array(), flag, mode, cred, true);
94
+ return this.openFile(p, flag, cred);
95
+ }
96
+ async stat(path, cred) {
97
+ const handle = await this.getHandle(path);
98
+ if (!handle) {
99
+ throw ApiError.FileError(ErrorCode.EINVAL, path);
100
+ }
101
+ if (handle instanceof FileSystemDirectoryHandle) {
102
+ return new Stats(FileType.DIRECTORY, 4096);
103
+ }
104
+ if (handle instanceof FileSystemFileHandle) {
105
+ const { lastModified, size } = await handle.getFile();
106
+ return new Stats(FileType.FILE, size, undefined, undefined, lastModified);
107
+ }
108
+ }
109
+ async exists(p, cred) {
110
+ try {
111
+ await this.getHandle(p);
112
+ return true;
113
+ }
114
+ catch (e) {
115
+ return false;
116
+ }
117
+ }
118
+ async openFile(path, flags, cred) {
119
+ const handle = await this.getHandle(path);
120
+ if (handle instanceof FileSystemFileHandle) {
121
+ const file = await handle.getFile();
122
+ const buffer = await file.arrayBuffer();
123
+ return this.newFile(path, flags, buffer, file.size, file.lastModified);
124
+ }
125
+ }
126
+ async unlink(path, cred) {
127
+ const handle = await this.getHandle(dirname(path));
128
+ if (handle instanceof FileSystemDirectoryHandle) {
139
129
  try {
140
- yield this.getHandle(p);
141
- return true;
130
+ await handle.removeEntry(basename(path), { recursive: true });
142
131
  }
143
132
  catch (e) {
144
- return false;
145
- }
146
- });
147
- }
148
- openFile(path, flags, cred) {
149
- return __awaiter(this, void 0, void 0, function* () {
150
- const handle = yield this.getHandle(path);
151
- if (handle instanceof FileSystemFileHandle) {
152
- const file = yield handle.getFile();
153
- const buffer = yield file.arrayBuffer();
154
- return this.newFile(path, flags, buffer, file.size, file.lastModified);
155
- }
156
- });
133
+ handleError(path, e);
134
+ }
135
+ }
136
+ }
137
+ async rmdir(path, cred) {
138
+ return this.unlink(path, cred);
139
+ }
140
+ async mkdir(p, mode, cred) {
141
+ const overwrite = mode && mode.flag && mode.flag.includes('w') && !mode.flag.includes('x');
142
+ const existingHandle = await this.getHandle(p);
143
+ if (existingHandle && !overwrite) {
144
+ throw ApiError.EEXIST(p);
145
+ }
146
+ const handle = await this.getHandle(dirname(p));
147
+ if (handle instanceof FileSystemDirectoryHandle) {
148
+ await handle.getDirectoryHandle(basename(p), { create: true });
149
+ }
150
+ }
151
+ async readdir(path, cred) {
152
+ const handle = await this.getHandle(path);
153
+ if (!(handle instanceof FileSystemDirectoryHandle)) {
154
+ throw ApiError.ENOTDIR(path);
155
+ }
156
+ const _keys = [];
157
+ for await (const key of handle.keys()) {
158
+ _keys.push(join(path, key));
159
+ }
160
+ return _keys;
157
161
  }
158
- unlink(path, cred) {
159
- return __awaiter(this, void 0, void 0, function* () {
160
- const handle = yield this.getHandle(dirname(path));
161
- if (handle instanceof FileSystemDirectoryHandle) {
162
- try {
163
- yield handle.removeEntry(basename(path), { recursive: true });
164
- }
165
- catch (e) {
166
- handleError(path, e);
162
+ newFile(path, flag, data, size, lastModified) {
163
+ return new FileSystemAccessFile(this, path, flag, new Stats(FileType.FILE, size || 0, undefined, undefined, lastModified || new Date().getTime()), new Uint8Array(data));
164
+ }
165
+ async getHandle(path) {
166
+ if (this._handles.has(path)) {
167
+ return this._handles.get(path);
168
+ }
169
+ let walkedPath = '/';
170
+ const [, ...pathParts] = path.split('/');
171
+ const getHandleParts = async ([pathPart, ...remainingPathParts]) => {
172
+ const walkingPath = join(walkedPath, pathPart);
173
+ const continueWalk = (handle) => {
174
+ walkedPath = walkingPath;
175
+ this._handles.set(walkedPath, handle);
176
+ if (remainingPathParts.length === 0) {
177
+ return this._handles.get(path);
167
178
  }
168
- }
169
- });
170
- }
171
- rmdir(path, cred) {
172
- return __awaiter(this, void 0, void 0, function* () {
173
- return this.unlink(path, cred);
174
- });
175
- }
176
- mkdir(p, mode, cred) {
177
- return __awaiter(this, void 0, void 0, function* () {
178
- const overwrite = mode && mode.flag && mode.flag.includes('w') && !mode.flag.includes('x');
179
- const existingHandle = yield this.getHandle(p);
180
- if (existingHandle && !overwrite) {
181
- throw ApiError.EEXIST(p);
182
- }
183
- const handle = yield this.getHandle(dirname(p));
184
- if (handle instanceof FileSystemDirectoryHandle) {
185
- yield handle.getDirectoryHandle(basename(p), { create: true });
186
- }
187
- });
188
- }
189
- readdir(path, cred) {
190
- var _b, e_1, _c, _d;
191
- return __awaiter(this, void 0, void 0, function* () {
192
- const handle = yield this.getHandle(path);
193
- if (!(handle instanceof FileSystemDirectoryHandle)) {
194
- throw ApiError.ENOTDIR(path);
195
- }
196
- const _keys = [];
179
+ getHandleParts(remainingPathParts);
180
+ };
181
+ const handle = this._handles.get(walkedPath);
197
182
  try {
198
- for (var _e = true, _f = __asyncValues(handle.keys()), _g; _g = yield _f.next(), _b = _g.done, !_b; _e = true) {
199
- _d = _g.value;
200
- _e = false;
201
- const key = _d;
202
- _keys.push(join(path, key));
203
- }
183
+ return await continueWalk(await handle.getDirectoryHandle(pathPart));
204
184
  }
205
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
206
- finally {
207
- try {
208
- if (!_e && !_b && (_c = _f.return)) yield _c.call(_f);
209
- }
210
- finally { if (e_1) throw e_1.error; }
211
- }
212
- return _keys;
213
- });
214
- }
215
- newFile(path, flag, data, size, lastModified) {
216
- return new FileSystemAccessFile(this, path, flag, new Stats(FileType.FILE, size || 0, undefined, undefined, lastModified || new Date().getTime()), Buffer.from(data));
217
- }
218
- getHandle(path) {
219
- return __awaiter(this, void 0, void 0, function* () {
220
- if (this._handles.has(path)) {
221
- return this._handles.get(path);
222
- }
223
- let walkedPath = '/';
224
- const [, ...pathParts] = path.split('/');
225
- const getHandleParts = ([pathPart, ...remainingPathParts]) => __awaiter(this, void 0, void 0, function* () {
226
- const walkingPath = join(walkedPath, pathPart);
227
- const continueWalk = (handle) => {
228
- walkedPath = walkingPath;
229
- this._handles.set(walkedPath, handle);
230
- if (remainingPathParts.length === 0) {
231
- return this._handles.get(path);
185
+ catch (error) {
186
+ if (error.name === 'TypeMismatchError') {
187
+ try {
188
+ return await continueWalk(await handle.getFileHandle(pathPart));
232
189
  }
233
- getHandleParts(remainingPathParts);
234
- };
235
- const handle = this._handles.get(walkedPath);
236
- try {
237
- return yield continueWalk(yield handle.getDirectoryHandle(pathPart));
238
- }
239
- catch (error) {
240
- if (error.name === 'TypeMismatchError') {
241
- try {
242
- return yield continueWalk(yield handle.getFileHandle(pathPart));
243
- }
244
- catch (err) {
245
- handleError(walkingPath, err);
246
- }
247
- }
248
- else if (error.message === 'Name is not allowed.') {
249
- throw new ApiError(ErrorCode.ENOENT, error.message, walkingPath);
250
- }
251
- else {
252
- handleError(walkingPath, error);
190
+ catch (err) {
191
+ handleError(walkingPath, err);
253
192
  }
254
193
  }
255
- });
256
- yield getHandleParts(pathParts);
257
- });
194
+ else if (error.message === 'Name is not allowed.') {
195
+ throw new ApiError(ErrorCode.ENOENT, error.message, walkingPath);
196
+ }
197
+ else {
198
+ handleError(walkingPath, error);
199
+ }
200
+ }
201
+ };
202
+ await getHandleParts(pathParts);
258
203
  }
259
204
  }
260
205
  _a = FileSystemAccessFileSystem;
@@ -1,6 +1,5 @@
1
- /// <reference types="node" resolution-mode="require"/>
2
- import { BaseFileSystem, FileContents, FileSystemMetadata } from '@zenfs/core/filesystem.js';
3
- import { File, FileFlag } from '@zenfs/core/file.js';
1
+ import { BaseFileSystem, FileSystemMetadata } from '@zenfs/core/filesystem.js';
2
+ import { FileFlag, NoSyncFile } from '@zenfs/core/file.js';
4
3
  import { Stats } from '@zenfs/core/stats.js';
5
4
  import { Cred } from '@zenfs/core/cred.js';
6
5
  import { type BackendOptions } from '@zenfs/core/backends/backend.js';
@@ -58,24 +57,22 @@ export declare class HTTPRequest extends BaseFileSystem {
58
57
  static isAvailable(): boolean;
59
58
  readonly prefixUrl: string;
60
59
  private _index;
61
- private _requestFileInternal;
62
- private _requestFileSizeInternal;
63
60
  constructor({ index, baseUrl }: HTTPRequest.Options);
64
61
  get metadata(): FileSystemMetadata;
65
62
  empty(): void;
66
63
  /**
67
64
  * Special HTTPFS function: Preload the given file into the index.
68
- * @param [String] path
69
- * @param [ZenFS.Buffer] buffer
65
+ * @param path
66
+ * @param buffer
70
67
  */
71
- preloadFile(path: string, buffer: Buffer): void;
68
+ preloadFile(path: string, buffer: Uint8Array): void;
72
69
  stat(path: string, cred: Cred): Promise<Stats>;
73
- open(path: string, flags: FileFlag, mode: number, cred: Cred): Promise<File>;
70
+ open(path: string, flags: FileFlag, mode: number, cred: Cred): Promise<NoSyncFile<this>>;
74
71
  readdir(path: string, cred: Cred): Promise<string[]>;
75
72
  /**
76
73
  * We have the entire file as a buffer; optimize readFile.
77
74
  */
78
- readFile(fname: string, encoding: BufferEncoding, flag: FileFlag, cred: Cred): Promise<FileContents>;
75
+ readFile(fname: string, flag: FileFlag, cred: Cred): Promise<Uint8Array>;
79
76
  private _getHTTPPath;
80
77
  /**
81
78
  * Asynchronously download the given file.