@zenfs/dom 1.1.8 → 1.2.0

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/dist/IndexedDB.js CHANGED
@@ -105,6 +105,25 @@ export class IndexedDBStore {
105
105
  return new IndexedDBTransaction(tx, this);
106
106
  }
107
107
  }
108
+ /**
109
+ * Used to memoize the availability test result
110
+ */
111
+ const idbTests = new WeakMap();
112
+ async function testAvailability(idbFactory) {
113
+ if (!(idbFactory instanceof IDBFactory))
114
+ return false;
115
+ try {
116
+ const req = idbFactory.open('__zenfs_test');
117
+ await wrap(req);
118
+ return true;
119
+ }
120
+ catch {
121
+ return false;
122
+ }
123
+ finally {
124
+ idbFactory?.deleteDatabase('__zenfs_test');
125
+ }
126
+ }
108
127
  /**
109
128
  * A file system that uses the IndexedDB key value file system.
110
129
  */
@@ -115,19 +134,11 @@ const _IndexedDB = {
115
134
  idbFactory: { type: 'object', required: false },
116
135
  },
117
136
  async isAvailable({ idbFactory = globalThis.indexedDB }) {
118
- try {
119
- if (!(idbFactory instanceof IDBFactory))
120
- return false;
121
- const req = idbFactory.open('__zenfs_test');
122
- await wrap(req);
123
- return true;
124
- }
125
- catch {
126
- return false;
127
- }
128
- finally {
129
- idbFactory?.deleteDatabase('__zenfs_test');
130
- }
137
+ if (idbTests.has(idbFactory))
138
+ return idbTests.get(idbFactory);
139
+ const result = testAvailability(idbFactory);
140
+ idbTests.set(idbFactory, result);
141
+ return result;
131
142
  },
132
143
  async create(options) {
133
144
  const db = await createDB(options.storeName || 'zenfs', options.idbFactory);
package/dist/access.d.ts CHANGED
@@ -3,16 +3,32 @@ import { IndexFS } from '@zenfs/core';
3
3
  export interface WebAccessOptions {
4
4
  handle: FileSystemDirectoryHandle;
5
5
  metadata?: string;
6
+ /**
7
+ * If set, disables caching of file system handles.
8
+ * This could significantly degrade lookup performance.
9
+ */
10
+ disableHandleCache?: boolean;
6
11
  }
7
12
  type HKindToType<T extends FileSystemHandleKind> = T extends 'directory' ? FileSystemDirectoryHandle : T extends 'file' ? FileSystemFileHandle : FileSystemHandle;
8
13
  declare const WebAccessFS_base: import("@zenfs/core").Mixin<typeof IndexFS, import("@zenfs/core").AsyncMixin>;
14
+ /**
15
+ * @todo Consider supporting synchronous stuff with `FileSystemFileHandle.createSyncAccessHandle()`\
16
+ * @internal
17
+ */
9
18
  export declare class WebAccessFS extends WebAccessFS_base {
19
+ protected readonly root: FileSystemDirectoryHandle;
20
+ protected readonly disableHandleCache: boolean;
21
+ /**
22
+ * Used to speed up handle lookups.
23
+ * Without this, every lookup would be O(n) on the path length.
24
+ * With the cache, these become O(1) operations.
25
+ */
10
26
  protected _handles: Map<string, FileSystemHandle>;
11
27
  /**
12
28
  * Loads all of the handles.
13
29
  * @internal @hidden
14
30
  */
15
- _loadHandles(path: string, handle: FileSystemDirectoryHandle): Promise<void>;
31
+ protected _loadHandles(path: string, handle: FileSystemDirectoryHandle): Promise<void>;
16
32
  /**
17
33
  * Loads metadata
18
34
  * @internal @hidden
@@ -22,7 +38,7 @@ export declare class WebAccessFS extends WebAccessFS_base {
22
38
  * @hidden
23
39
  */
24
40
  _sync: FileSystem;
25
- constructor(handle: FileSystemDirectoryHandle);
41
+ constructor(root: FileSystemDirectoryHandle, disableHandleCache?: boolean);
26
42
  protected remove(path: string): Promise<void>;
27
43
  protected removeSync(): void;
28
44
  read(path: string, buffer: Uint8Array, offset: number, end: number): Promise<void>;
@@ -33,7 +49,7 @@ export declare class WebAccessFS extends WebAccessFS_base {
33
49
  */
34
50
  writeFile(path: string, data: Uint8Array): Promise<void>;
35
51
  mkdir(path: string, options: CreationOptions): Promise<InodeLike>;
36
- protected get<const T extends FileSystemHandleKind | null>(kind: T | undefined, path: string): T extends FileSystemHandleKind ? HKindToType<T> : FileSystemHandle;
52
+ protected get<const T extends FileSystemHandleKind | null>(kind: T | undefined, path: string): Promise<T extends FileSystemHandleKind ? HKindToType<T> : FileSystemHandle>;
37
53
  }
38
54
  declare const _WebAccess: {
39
55
  readonly name: "WebAccess";
@@ -46,6 +62,10 @@ declare const _WebAccess: {
46
62
  readonly type: "string";
47
63
  readonly required: false;
48
64
  };
65
+ readonly disableHandleCache: {
66
+ readonly type: "boolean";
67
+ readonly required: false;
68
+ };
49
69
  };
50
70
  readonly create: (options: WebAccessOptions) => Promise<WebAccessFS>;
51
71
  };
package/dist/access.js CHANGED
@@ -18,6 +18,10 @@ function isResizable(buffer) {
18
18
  function isKind(handle, kind) {
19
19
  return handle.kind == kind;
20
20
  }
21
+ /**
22
+ * @todo Consider supporting synchronous stuff with `FileSystemFileHandle.createSyncAccessHandle()`\
23
+ * @internal
24
+ */
21
25
  export class WebAccessFS extends Async(IndexFS) {
22
26
  /**
23
27
  * Loads all of the handles.
@@ -36,8 +40,10 @@ export class WebAccessFS extends Async(IndexFS) {
36
40
  * @internal @hidden
37
41
  */
38
42
  async _loadMetadata(metadataPath) {
43
+ this._handles.set('/', this.root);
44
+ await this._loadHandles('/', this.root);
39
45
  if (metadataPath) {
40
- const handle = this.get('file', metadataPath);
46
+ const handle = await this.get('file', metadataPath);
41
47
  const file = await handle.getFile();
42
48
  const raw = await file.text();
43
49
  const data = JSON.parse(raw);
@@ -55,18 +61,26 @@ export class WebAccessFS extends Async(IndexFS) {
55
61
  this.index.set(path, new Inode({ mode: 0o777 | constants.S_IFDIR, size: 0 }));
56
62
  }
57
63
  }
58
- constructor(handle) {
64
+ constructor(root, disableHandleCache = false) {
59
65
  super(0x77656261, 'webaccessfs');
66
+ this.root = root;
67
+ this.disableHandleCache = disableHandleCache;
68
+ /**
69
+ * Used to speed up handle lookups.
70
+ * Without this, every lookup would be O(n) on the path length.
71
+ * With the cache, these become O(1) operations.
72
+ */
60
73
  this._handles = new Map();
61
74
  /**
62
75
  * @hidden
63
76
  */
64
77
  this._sync = InMemory.create({ label: 'accessfs-cache' });
65
78
  this.attributes.set('no_buffer_resize', true);
66
- this._handles.set('/', handle);
79
+ if (disableHandleCache)
80
+ this.attributes.set('no_handle_cache', true);
67
81
  }
68
82
  async remove(path) {
69
- const handle = this.get('directory', dirname(path));
83
+ const handle = await this.get('directory', dirname(path));
70
84
  await handle.removeEntry(basename(path), { recursive: true }).catch(ex => _throw(convertException(ex, path)));
71
85
  }
72
86
  removeSync() {
@@ -75,7 +89,7 @@ export class WebAccessFS extends Async(IndexFS) {
75
89
  async read(path, buffer, offset, end) {
76
90
  if (end <= offset)
77
91
  return;
78
- const handle = this.get('file', path);
92
+ const handle = await this.get('file', path);
79
93
  const file = await handle.getFile();
80
94
  const data = await file.arrayBuffer();
81
95
  if (data.byteLength < end - offset)
@@ -94,12 +108,13 @@ export class WebAccessFS extends Async(IndexFS) {
94
108
  const isDir = (inode.mode & S_IFMT) == S_IFDIR;
95
109
  let handle;
96
110
  try {
97
- handle = this.get(isDir ? 'directory' : 'file', path);
111
+ handle = await this.get(isDir ? 'directory' : 'file', path);
98
112
  }
99
113
  catch {
100
- const parent = this.get('directory', dirname(path));
114
+ const parent = await this.get('directory', dirname(path));
101
115
  handle = await parent[isDir ? 'getDirectoryHandle' : 'getFileHandle'](basename(path), { create: true }).catch((ex) => _throw(convertException(ex, path)));
102
- this._handles.set(path, handle);
116
+ if (!this.disableHandleCache)
117
+ this._handles.set(path, handle);
103
118
  }
104
119
  if (isDir)
105
120
  return;
@@ -129,18 +144,38 @@ export class WebAccessFS extends Async(IndexFS) {
129
144
  }
130
145
  async mkdir(path, options) {
131
146
  const inode = await super.mkdir(path, options);
132
- const handle = this.get('directory', dirname(path));
147
+ const handle = await this.get('directory', dirname(path));
133
148
  const dir = await handle.getDirectoryHandle(basename(path), { create: true }).catch((ex) => _throw(convertException(ex, path)));
134
- this._handles.set(path, dir);
149
+ if (!this.disableHandleCache)
150
+ this._handles.set(path, dir);
135
151
  return inode;
136
152
  }
137
- get(kind = null, path) {
138
- const handle = this._handles.get(path);
139
- if (!handle)
140
- throw withErrno('ENODATA');
141
- if (kind && !isKind(handle, kind))
142
- throw withErrno(kind == 'directory' ? 'ENOTDIR' : 'EISDIR');
143
- return handle;
153
+ async get(kind = null, path) {
154
+ if (!this.disableHandleCache) {
155
+ const handle = this._handles.get(path);
156
+ if (!handle)
157
+ throw withErrno('ENOENT');
158
+ if (kind && !isKind(handle, kind))
159
+ throw withErrno(kind == 'directory' ? 'ENOTDIR' : 'EISDIR');
160
+ return handle;
161
+ }
162
+ if (path == '/')
163
+ return this.root;
164
+ const parts = path.slice(1).split('/');
165
+ let dir = this.root;
166
+ for (let i = 0; i < parts.length - 1; ++i) {
167
+ dir = await dir.getDirectoryHandle(parts[i]).catch(ex => _throw(convertException(ex, path)));
168
+ }
169
+ try {
170
+ const handle = await dir[kind == 'file' ? 'getFileHandle' : 'getDirectoryHandle'](parts.at(-1));
171
+ return handle;
172
+ }
173
+ catch (ex) {
174
+ if (ex.name == 'TypeMismatchError')
175
+ throw withErrno(kind == 'file' ? 'EISDIR' : 'ENOTDIR');
176
+ else
177
+ throw convertException(ex, path);
178
+ }
144
179
  }
145
180
  }
146
181
  const _WebAccess = {
@@ -148,10 +183,10 @@ const _WebAccess = {
148
183
  options: {
149
184
  handle: { type: 'object', required: true },
150
185
  metadata: { type: 'string', required: false },
186
+ disableHandleCache: { type: 'boolean', required: false },
151
187
  },
152
188
  async create(options) {
153
189
  const fs = new WebAccessFS(options.handle);
154
- await fs._loadHandles('/', options.handle);
155
190
  await fs._loadMetadata(options.metadata);
156
191
  return fs;
157
192
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zenfs/dom",
3
- "version": "1.1.8",
3
+ "version": "1.2.0",
4
4
  "description": "DOM backends for ZenFS",
5
5
  "funding": {
6
6
  "type": "individual",