opfs-worker 2.0.0 → 2.1.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/README.md +62 -439
- package/dist/BaseOPFS-BPw9AIQ-.cjs +4 -0
- package/dist/BaseOPFS-BPw9AIQ-.cjs.map +1 -0
- package/dist/BaseOPFS-Cylz6ijk.js +2036 -0
- package/dist/BaseOPFS-Cylz6ijk.js.map +1 -0
- package/dist/OPFSAsync-BKSAJJeh.js +124 -0
- package/dist/OPFSAsync-BKSAJJeh.js.map +1 -0
- package/dist/OPFSAsync-s2P7y3sw.cjs +2 -0
- package/dist/OPFSAsync-s2P7y3sw.cjs.map +1 -0
- package/dist/OPFSFacade-DePnNRgt.cjs +2 -0
- package/dist/OPFSFacade-DePnNRgt.cjs.map +1 -0
- package/dist/OPFSFacade-XohWlHuu.js +281 -0
- package/dist/OPFSFacade-XohWlHuu.js.map +1 -0
- package/dist/OPFSSync-C7KBepe_.cjs +2 -0
- package/dist/OPFSSync-C7KBepe_.cjs.map +1 -0
- package/dist/OPFSSync-CsSQkpk5.js +208 -0
- package/dist/OPFSSync-CsSQkpk5.js.map +1 -0
- package/dist/assets/dedicated.worker-nY3tK4zi.js.map +1 -0
- package/dist/{OPFSWorker.d.ts → core/BaseOPFS.d.ts} +34 -304
- package/dist/core/BaseOPFS.d.ts.map +1 -0
- package/dist/core/OPFSAsync.d.ts +55 -0
- package/dist/core/OPFSAsync.d.ts.map +1 -0
- package/dist/core/OPFSSync.d.ts +54 -0
- package/dist/core/OPFSSync.d.ts.map +1 -0
- package/dist/createOPFSAsync-BwKDmHsy.cjs +2 -0
- package/dist/createOPFSAsync-BwKDmHsy.cjs.map +1 -0
- package/dist/createOPFSAsync-Y4Mg6qte.js +10 -0
- package/dist/createOPFSAsync-Y4Mg6qte.js.map +1 -0
- package/dist/createOPFSDedicated-CsnV6MLP.js +2333 -0
- package/dist/createOPFSDedicated-CsnV6MLP.js.map +1 -0
- package/dist/createOPFSDedicated-pKTRWLdz.cjs +2283 -0
- package/dist/createOPFSDedicated-pKTRWLdz.cjs.map +1 -0
- package/dist/createOPFSShared-CGdd__PH.js +23 -0
- package/dist/createOPFSShared-CGdd__PH.js.map +1 -0
- package/dist/createOPFSShared-CskROt7U.cjs +2 -0
- package/dist/createOPFSShared-CskROt7U.cjs.map +1 -0
- package/dist/dedicated.worker.js +2281 -0
- package/dist/dedicated.worker.js.map +1 -0
- package/dist/{OPFSFacade.d.ts → facade/OPFSFacade.d.ts} +31 -23
- package/dist/facade/OPFSFacade.d.ts.map +1 -0
- package/dist/facade/createOPFSAsync.d.ts +16 -0
- package/dist/facade/createOPFSAsync.d.ts.map +1 -0
- package/dist/facade/createOPFSDedicated.d.ts +17 -0
- package/dist/facade/createOPFSDedicated.d.ts.map +1 -0
- package/dist/facade/createOPFSShared.d.ts +24 -0
- package/dist/facade/createOPFSShared.d.ts.map +1 -0
- package/dist/index.async.cjs +2 -0
- package/dist/index.async.cjs.map +1 -0
- package/dist/index.async.d.ts +10 -0
- package/dist/index.async.d.ts.map +1 -0
- package/dist/index.async.js +55 -0
- package/dist/index.async.js.map +1 -0
- package/dist/index.cjs +1 -2477
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +17 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +61 -2828
- package/dist/index.js.map +1 -1
- package/dist/index.pure.cjs +1 -1
- package/dist/index.pure.cjs.map +1 -1
- package/dist/index.pure.d.ts +3 -1
- package/dist/index.pure.d.ts.map +1 -1
- package/dist/index.pure.js +50 -1053
- package/dist/index.pure.js.map +1 -1
- package/dist/index.sharedworker.cjs +2 -0
- package/dist/index.sharedworker.cjs.map +1 -0
- package/dist/index.sharedworker.d.ts +11 -0
- package/dist/index.sharedworker.d.ts.map +1 -0
- package/dist/index.sharedworker.js +55 -0
- package/dist/index.sharedworker.js.map +1 -0
- package/dist/index.sync.cjs +2 -0
- package/dist/index.sync.cjs.map +1 -0
- package/dist/index.sync.d.ts +11 -0
- package/dist/index.sync.d.ts.map +1 -0
- package/dist/index.sync.js +55 -0
- package/dist/index.sync.js.map +1 -0
- package/dist/shared.worker.js +2166 -0
- package/dist/shared.worker.js.map +1 -0
- package/dist/types.d.ts +9 -4
- package/dist/types.d.ts.map +1 -1
- package/dist/worker/createDedicatedWorker.d.ts +30 -0
- package/dist/worker/createDedicatedWorker.d.ts.map +1 -0
- package/dist/worker/createSharedWorker.d.ts +37 -0
- package/dist/worker/createSharedWorker.d.ts.map +1 -0
- package/dist/worker/dedicated.worker.d.ts +2 -0
- package/dist/worker/dedicated.worker.d.ts.map +1 -0
- package/dist/worker/shared.worker.d.ts +2 -0
- package/dist/worker/shared.worker.d.ts.map +1 -0
- package/docs/README.md +30 -0
- package/docs/api/backend.md +36 -0
- package/docs/api/create.md +82 -0
- package/docs/api/facade.md +52 -0
- package/docs/choosing-a-mode.md +41 -0
- package/docs/file-descriptors.md +8 -9
- package/docs/guides/async.md +36 -0
- package/docs/guides/dedicated.md +76 -0
- package/docs/guides/hashing.md +34 -0
- package/docs/guides/pure.md +46 -0
- package/docs/guides/sharedworker.md +57 -0
- package/docs/guides/streaming.md +51 -0
- package/docs/guides/watching.md +35 -0
- package/docs/migration.md +41 -0
- package/docs/types.md +20 -6
- package/package.json +42 -3
- package/src/{OPFSWorker.ts → core/BaseOPFS.ts} +54 -647
- package/src/core/OPFSAsync.ts +248 -0
- package/src/core/OPFSSync.ts +460 -0
- package/src/{OPFSFacade.ts → facade/OPFSFacade.ts} +83 -54
- package/src/facade/createOPFSAsync.ts +22 -0
- package/src/facade/createOPFSDedicated.ts +23 -0
- package/src/facade/createOPFSShared.ts +30 -0
- package/src/index.async.ts +11 -0
- package/src/index.pure.ts +3 -2
- package/src/index.sharedworker.ts +12 -0
- package/src/index.sync.ts +12 -0
- package/src/index.ts +37 -8
- package/src/types.ts +8 -4
- package/src/worker/createDedicatedWorker.ts +74 -0
- package/src/worker/createSharedWorker.ts +67 -0
- package/src/worker/dedicated.worker.ts +13 -0
- package/src/worker/shared.worker.ts +17 -0
- package/dist/OPFSFacade.d.ts.map +0 -1
- package/dist/OPFSWorker.d.ts.map +0 -1
- package/dist/assets/worker.entry-DUlEoroc.js.map +0 -1
- package/dist/createOPFSWorker.d.ts +0 -17
- package/dist/createOPFSWorker.d.ts.map +0 -1
- package/dist/helpers-DNj8ZoMu.cjs +0 -4
- package/dist/helpers-DNj8ZoMu.cjs.map +0 -1
- package/dist/helpers-WY2jfbOT.js +0 -1427
- package/dist/helpers-WY2jfbOT.js.map +0 -1
- package/dist/worker.entry.d.ts +0 -2
- package/dist/worker.entry.d.ts.map +0 -1
- package/docs/api-reference.md +0 -815
- package/src/createOPFSWorker.ts +0 -57
- package/src/worker.entry.ts +0 -6
package/dist/index.pure.js
CHANGED
|
@@ -1,1056 +1,53 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
class V {
|
|
5
|
-
/** Root directory handle for the file system */
|
|
6
|
-
root;
|
|
7
|
-
/** Map of watched paths and options */
|
|
8
|
-
watchers = /* @__PURE__ */ new Map();
|
|
9
|
-
/** Promise to prevent concurrent mount operations */
|
|
10
|
-
mountingPromise = null;
|
|
11
|
-
/** BroadcastChannel instance for sending events */
|
|
12
|
-
broadcastChannel = null;
|
|
13
|
-
/** Configuration options */
|
|
14
|
-
options = {
|
|
15
|
-
root: "/",
|
|
16
|
-
namespace: "",
|
|
17
|
-
maxFileSize: 50 * 1024 * 1024,
|
|
18
|
-
hashAlgorithm: "etag",
|
|
19
|
-
broadcastChannel: "opfs-worker"
|
|
20
|
-
};
|
|
21
|
-
/** Shared sync handles per path (OPFS allows only one handle per file) */
|
|
22
|
-
openHandles = /* @__PURE__ */ new Map();
|
|
23
|
-
/** Map of open file descriptors to their metadata */
|
|
24
|
-
openFiles = /* @__PURE__ */ new Map();
|
|
25
|
-
/** Next available file descriptor number */
|
|
26
|
-
nextFd = 1;
|
|
27
|
-
/**
|
|
28
|
-
* Get file info by descriptor with validation
|
|
29
|
-
* @private
|
|
30
|
-
*/
|
|
31
|
-
_getFileDescriptor(t) {
|
|
32
|
-
const e = this.openFiles.get(t);
|
|
33
|
-
if (!e)
|
|
34
|
-
throw new C("descriptor", `Invalid file descriptor: ${t}`);
|
|
35
|
-
return e;
|
|
36
|
-
}
|
|
37
|
-
/**
|
|
38
|
-
* Notify about internal changes to the file system
|
|
39
|
-
*
|
|
40
|
-
* This method is called by internal operations to notify clients about
|
|
41
|
-
* changes, even when no specific paths are being watched.
|
|
42
|
-
*
|
|
43
|
-
* @param event - The event describing the change
|
|
44
|
-
*/
|
|
45
|
-
async notifyChange(t) {
|
|
46
|
-
if (!this.options.broadcastChannel)
|
|
47
|
-
return;
|
|
48
|
-
const e = t.path;
|
|
49
|
-
if (![...this.watchers.values()].some((i) => g(e, i.pattern) && i.include.some((s) => s && g(e, s)) && !i.exclude.some((s) => s && g(e, s))))
|
|
50
|
-
return;
|
|
51
|
-
let a;
|
|
52
|
-
if (this.options.hashAlgorithm)
|
|
53
|
-
try {
|
|
54
|
-
a = (await this.stat(e)).hash;
|
|
55
|
-
} catch {
|
|
56
|
-
}
|
|
57
|
-
try {
|
|
58
|
-
this.broadcastChannel || (this.broadcastChannel = new BroadcastChannel(this.options.broadcastChannel));
|
|
59
|
-
const i = {
|
|
60
|
-
namespace: this.options.namespace,
|
|
61
|
-
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
62
|
-
...t,
|
|
63
|
-
...a && { hash: a }
|
|
64
|
-
};
|
|
65
|
-
this.broadcastChannel.postMessage(i);
|
|
66
|
-
} catch (i) {
|
|
67
|
-
console.warn("Failed to send event via BroadcastChannel:", i);
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
/**
|
|
71
|
-
* Creates a new OPFSWorker instance
|
|
72
|
-
*
|
|
73
|
-
* @param options - Optional configuration options
|
|
74
|
-
* @param options.root - Root path for the file system (default: '/')
|
|
75
|
-
* @param options.watchInterval - Polling interval in milliseconds for file watching
|
|
76
|
-
* @param options.hashAlgorithm - Hash algorithm for file hashing
|
|
77
|
-
* @param options.maxFileSize - Maximum file size for hashing in bytes (default: 50MB)
|
|
78
|
-
* @throws {OPFSError} If OPFS is not supported in the current browser
|
|
79
|
-
*/
|
|
80
|
-
constructor(t) {
|
|
81
|
-
O(), t && this.setOptions(t);
|
|
82
|
-
}
|
|
83
|
-
/**
|
|
84
|
-
* Initialize the file system within a given directory
|
|
85
|
-
*
|
|
86
|
-
* This method sets up the root directory for all subsequent operations.
|
|
87
|
-
* If no root is specified, it will use the OPFS root directory.
|
|
88
|
-
*
|
|
89
|
-
* @returns Promise that resolves to true if initialization was successful
|
|
90
|
-
* @throws {OPFSError} If initialization fails
|
|
91
|
-
*
|
|
92
|
-
* @example
|
|
93
|
-
* ```typescript
|
|
94
|
-
* const fs = new OPFSWorker();
|
|
95
|
-
*
|
|
96
|
-
* // Use OPFS root (default)
|
|
97
|
-
* await fs.mount();
|
|
98
|
-
*
|
|
99
|
-
* // Use custom directory
|
|
100
|
-
* await fs.mount('/my-app');
|
|
101
|
-
* ```
|
|
102
|
-
*/
|
|
103
|
-
async mount() {
|
|
104
|
-
const t = this.options.root;
|
|
105
|
-
return this.mountingPromise && await this.mountingPromise, this.mountingPromise = new Promise(async (e, r) => {
|
|
106
|
-
try {
|
|
107
|
-
const a = await navigator.storage.getDirectory();
|
|
108
|
-
this.root = t === "/" ? a : await this.getDirectoryHandle(t, !0, a), e(!0);
|
|
109
|
-
} catch (a) {
|
|
110
|
-
r(new P(t, a));
|
|
111
|
-
} finally {
|
|
112
|
-
this.mountingPromise = null;
|
|
113
|
-
}
|
|
114
|
-
}), this.mountingPromise;
|
|
115
|
-
}
|
|
116
|
-
/**
|
|
117
|
-
* Update configuration options
|
|
118
|
-
*
|
|
119
|
-
* @param options - Configuration options to update
|
|
120
|
-
* @param options.root - Root path for the file system
|
|
121
|
-
* @param options.watchInterval - Polling interval in milliseconds for file watching
|
|
122
|
-
* @param options.hashAlgorithm - Hash algorithm for file hashing
|
|
123
|
-
* @param options.maxFileSize - Maximum file size for hashing in bytes
|
|
124
|
-
* @param options.broadcastChannel - Custom name for the broadcast channel
|
|
125
|
-
*/
|
|
126
|
-
async setOptions(t) {
|
|
127
|
-
t.hashAlgorithm !== void 0 && (this.options.hashAlgorithm = t.hashAlgorithm), t.maxFileSize !== void 0 && (this.options.maxFileSize = t.maxFileSize), t.broadcastChannel !== void 0 && (this.broadcastChannel && this.options.broadcastChannel !== t.broadcastChannel && (this.broadcastChannel.close(), this.broadcastChannel = null), this.options.broadcastChannel = t.broadcastChannel), t.namespace && (this.options.namespace = t.namespace), t.root !== void 0 && (this.options.root = p(t.root), this.options.namespace || (this.options.namespace = `opfs-worker:${this.options.root}`), await this.mount());
|
|
128
|
-
}
|
|
129
|
-
/**
|
|
130
|
-
* Get a directory handle from a path
|
|
131
|
-
*
|
|
132
|
-
* Navigates through the directory structure to find or create a directory
|
|
133
|
-
* at the specified path.
|
|
134
|
-
*
|
|
135
|
-
* @param path - The path to the directory (string or array of segments)
|
|
136
|
-
* @param create - Whether to create the directory if it doesn't exist (default: false)
|
|
137
|
-
* @param from - The directory to start from (default: root directory)
|
|
138
|
-
* @returns Promise that resolves to the directory handle
|
|
139
|
-
* @throws {OPFSError} If the directory cannot be accessed or created
|
|
140
|
-
*
|
|
141
|
-
* @example
|
|
142
|
-
* ```typescript
|
|
143
|
-
* const docsDir = await fs.getDirectoryHandle('/users/john/documents', true);
|
|
144
|
-
* const docsDir2 = await fs.getDirectoryHandle(['users', 'john', 'documents'], true);
|
|
145
|
-
* ```
|
|
146
|
-
*/
|
|
147
|
-
async getDirectoryHandle(t, e = !1, r = this.root) {
|
|
148
|
-
const a = Array.isArray(t) ? t : D(t);
|
|
149
|
-
let i = r;
|
|
150
|
-
for (const s of a)
|
|
151
|
-
i = await i.getDirectoryHandle(s, { create: e });
|
|
152
|
-
return i;
|
|
153
|
-
}
|
|
154
|
-
/**
|
|
155
|
-
* Get a file handle from a path
|
|
156
|
-
*
|
|
157
|
-
* Navigates to the parent directory and retrieves or creates a file handle
|
|
158
|
-
* for the specified file path.
|
|
159
|
-
*
|
|
160
|
-
* @param path - The path to the file (string or array of segments)
|
|
161
|
-
* @param create - Whether to create the file if it doesn't exist (default: false)
|
|
162
|
-
* @param _from - The directory to start from (default: root directory)
|
|
163
|
-
* @returns Promise that resolves to the file handle
|
|
164
|
-
* @throws {PathError} If the path is empty
|
|
165
|
-
* @throws {OPFSError} If the file cannot be accessed or created
|
|
166
|
-
*
|
|
167
|
-
* @example
|
|
168
|
-
* ```typescript
|
|
169
|
-
* const fileHandle = await fs.getFileHandle('/config/settings.json', true);
|
|
170
|
-
* const fileHandle2 = await fs.getFileHandle(['config', 'settings.json'], true);
|
|
171
|
-
* ```
|
|
172
|
-
*/
|
|
173
|
-
async getFileHandle(t, e = !1, r = this.root) {
|
|
174
|
-
const a = D(t);
|
|
175
|
-
if (a.length === 0)
|
|
176
|
-
throw new z("Path must not be empty", Array.isArray(t) ? t.join("/") : t);
|
|
177
|
-
const i = a.pop();
|
|
178
|
-
return (await this.getDirectoryHandle(a, e, r)).getFileHandle(i, { create: e });
|
|
179
|
-
}
|
|
180
|
-
/**
|
|
181
|
-
* Get a complete index of all files and directories in the file system
|
|
182
|
-
*
|
|
183
|
-
* This method recursively traverses the entire file system and returns
|
|
184
|
-
* a Map containing FileStat objects for every file and directory.
|
|
185
|
-
*
|
|
186
|
-
* @returns Promise that resolves to a Map of paths to FileStat objects
|
|
187
|
-
* @throws {OPFSError} If the file system is not mounted
|
|
188
|
-
*
|
|
189
|
-
* @example
|
|
190
|
-
* ```typescript
|
|
191
|
-
* const index = await fs.index();
|
|
192
|
-
* const fileStats = index.get('/data/config.json');
|
|
193
|
-
* if (fileStats) {
|
|
194
|
-
* console.log(`File size: ${fileStats.size} bytes`);
|
|
195
|
-
* if (fileStats.hash) console.log(`Hash: ${fileStats.hash}`);
|
|
196
|
-
* }
|
|
197
|
-
* ```
|
|
198
|
-
*/
|
|
199
|
-
async index() {
|
|
200
|
-
const t = /* @__PURE__ */ new Map(), e = async (r) => {
|
|
201
|
-
const a = await this.readDir(r);
|
|
202
|
-
for (const i of a) {
|
|
203
|
-
const s = `${r === "/" ? "" : r}/${i.name}`;
|
|
204
|
-
try {
|
|
205
|
-
const n = await this.stat(s);
|
|
206
|
-
t.set(s, n), n.isDirectory && await e(s);
|
|
207
|
-
} catch (n) {
|
|
208
|
-
console.warn(`Skipping broken entry: ${s}`, n);
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
};
|
|
212
|
-
return t.set("/", {
|
|
213
|
-
kind: "directory",
|
|
214
|
-
size: 0,
|
|
215
|
-
mtime: (/* @__PURE__ */ new Date(0)).toISOString(),
|
|
216
|
-
ctime: (/* @__PURE__ */ new Date(0)).toISOString(),
|
|
217
|
-
isFile: !1,
|
|
218
|
-
isDirectory: !0
|
|
219
|
-
}), await e("/"), t;
|
|
220
|
-
}
|
|
221
|
-
/**
|
|
222
|
-
* Read a file from the file system
|
|
223
|
-
*
|
|
224
|
-
* Reads the contents of a file and returns it as binary data.
|
|
225
|
-
*
|
|
226
|
-
* @param path - The path to the file to read
|
|
227
|
-
* @returns Promise that resolves to the file contents as Uint8Array
|
|
228
|
-
* @throws {FileNotFoundError} If the file does not exist
|
|
229
|
-
* @throws {OPFSError} If reading the file fails
|
|
230
|
-
*
|
|
231
|
-
* @example
|
|
232
|
-
* ```typescript
|
|
233
|
-
* // Read as binary data
|
|
234
|
-
* const content = await fs.readFile('/config/settings.json');
|
|
235
|
-
*
|
|
236
|
-
* // Read binary file
|
|
237
|
-
* const binaryData = await fs.readFile('/images/logo.png');
|
|
238
|
-
* ```
|
|
239
|
-
*/
|
|
240
|
-
async readFile(t) {
|
|
241
|
-
await this.mount();
|
|
242
|
-
try {
|
|
243
|
-
return await u(t, async () => {
|
|
244
|
-
const e = await this.open(t);
|
|
245
|
-
try {
|
|
246
|
-
const { size: r } = await this.fstat(e), a = new Uint8Array(r);
|
|
247
|
-
return r > 0 && await this.read(e, a, 0, r, 0), x(a, [a.buffer]);
|
|
248
|
-
} finally {
|
|
249
|
-
await this.close(e);
|
|
250
|
-
}
|
|
251
|
-
});
|
|
252
|
-
} catch (e) {
|
|
253
|
-
throw e instanceof w ? e : h(e, { path: t, isDirectory: !1 });
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
/**
|
|
257
|
-
* Write data to a file
|
|
258
|
-
*
|
|
259
|
-
* Creates or overwrites a file with the specified binary data. If the file already
|
|
260
|
-
* exists, it will be truncated before writing.
|
|
261
|
-
*
|
|
262
|
-
* @param path - The path to the file to write
|
|
263
|
-
* @param data - The binary data to write to the file (Uint8Array or ArrayBuffer)
|
|
264
|
-
* @returns Promise that resolves when the write operation is complete
|
|
265
|
-
* @throws {OPFSError} If writing the file fails
|
|
266
|
-
*
|
|
267
|
-
* @example
|
|
268
|
-
* ```typescript
|
|
269
|
-
* // Write binary data
|
|
270
|
-
* const binaryData = new Uint8Array([1, 2, 3, 4, 5]);
|
|
271
|
-
* await fs.writeFile('/data/binary.dat', binaryData);
|
|
272
|
-
*
|
|
273
|
-
* // Write from ArrayBuffer
|
|
274
|
-
* const arrayBuffer = new ArrayBuffer(10);
|
|
275
|
-
* await fs.writeFile('/data/buffer.dat', arrayBuffer);
|
|
276
|
-
* ```
|
|
277
|
-
*/
|
|
278
|
-
async writeFile(t, e) {
|
|
279
|
-
await this.mount();
|
|
280
|
-
const r = e instanceof Uint8Array ? e : new Uint8Array(e);
|
|
281
|
-
await u(t, async () => {
|
|
282
|
-
const a = await this.exists(t), i = await this.open(t, { create: !0, truncate: !0 });
|
|
283
|
-
try {
|
|
284
|
-
await this.write(i, r, 0, r.length, null, !1), await this.fsync(i);
|
|
285
|
-
} finally {
|
|
286
|
-
await this.close(i);
|
|
287
|
-
}
|
|
288
|
-
await this.notifyChange({ path: t, type: a ? l.Changed : l.Added, isDirectory: !1 });
|
|
289
|
-
});
|
|
290
|
-
}
|
|
291
|
-
/**
|
|
292
|
-
* Append data to a file
|
|
293
|
-
*
|
|
294
|
-
* Adds binary data to the end of an existing file. If the file doesn't exist,
|
|
295
|
-
* it will be created.
|
|
296
|
-
*
|
|
297
|
-
* @param path - The path to the file to append to
|
|
298
|
-
* @param data - The binary data to append to the file (Uint8Array or ArrayBuffer)
|
|
299
|
-
* @returns Promise that resolves when the append operation is complete
|
|
300
|
-
* @throws {OPFSError} If appending to the file fails
|
|
301
|
-
*
|
|
302
|
-
* @example
|
|
303
|
-
* ```typescript
|
|
304
|
-
* // Append binary data
|
|
305
|
-
* const additionalData = new Uint8Array([6, 7, 8]);
|
|
306
|
-
* await fs.appendFile('/data/binary.dat', additionalData);
|
|
307
|
-
*
|
|
308
|
-
* // Append from ArrayBuffer
|
|
309
|
-
* const arrayBuffer = new ArrayBuffer(5);
|
|
310
|
-
* await fs.appendFile('/data/buffer.dat', arrayBuffer);
|
|
311
|
-
* ```
|
|
312
|
-
*/
|
|
313
|
-
async appendFile(t, e) {
|
|
314
|
-
await this.mount();
|
|
315
|
-
const r = e instanceof Uint8Array ? e : new Uint8Array(e);
|
|
316
|
-
await u(t, async () => {
|
|
317
|
-
const a = await this.open(t, { create: !0 });
|
|
318
|
-
try {
|
|
319
|
-
const { size: i } = await this.fstat(a);
|
|
320
|
-
await this.write(a, r, 0, r.length, i, !1), await this.fsync(a);
|
|
321
|
-
} finally {
|
|
322
|
-
await this.close(a);
|
|
323
|
-
}
|
|
324
|
-
await this.notifyChange({ path: t, type: l.Changed, isDirectory: !1 });
|
|
325
|
-
});
|
|
326
|
-
}
|
|
327
|
-
/**
|
|
328
|
-
* Create a directory
|
|
329
|
-
*
|
|
330
|
-
* Creates a new directory at the specified path. If the recursive option
|
|
331
|
-
* is enabled, parent directories will be created as needed.
|
|
332
|
-
*
|
|
333
|
-
* @param path - The path where the directory should be created
|
|
334
|
-
* @param options - Options for directory creation
|
|
335
|
-
* @param options.recursive - Whether to create parent directories if they don't exist (default: false)
|
|
336
|
-
* @returns Promise that resolves when the directory is created
|
|
337
|
-
* @throws {OPFSError} If the directory cannot be created
|
|
338
|
-
*
|
|
339
|
-
* @example
|
|
340
|
-
* ```typescript
|
|
341
|
-
* // Create a single directory
|
|
342
|
-
* await fs.mkdir('/users/john');
|
|
343
|
-
*
|
|
344
|
-
* // Create nested directories
|
|
345
|
-
* await fs.mkdir('/users/john/documents/projects', { recursive: true });
|
|
346
|
-
* ```
|
|
347
|
-
*/
|
|
348
|
-
async mkdir(t, e) {
|
|
349
|
-
await this.mount();
|
|
350
|
-
const r = e?.recursive ?? !1, a = D(t);
|
|
351
|
-
let i = this.root;
|
|
352
|
-
for (let s = 0; s < a.length; s++) {
|
|
353
|
-
const n = a[s];
|
|
354
|
-
try {
|
|
355
|
-
i = await i.getDirectoryHandle(n, { create: r || s === a.length - 1 });
|
|
356
|
-
} catch (o) {
|
|
357
|
-
throw o.name === "NotFoundError" ? h(o, {
|
|
358
|
-
path: k(a.slice(0, s + 1)),
|
|
359
|
-
existenceType: "directory"
|
|
360
|
-
}) : o.name === "TypeMismatchError" ? h(o, { path: n, isDirectory: !1 }) : new F("create directory", n, o);
|
|
361
|
-
}
|
|
362
|
-
}
|
|
363
|
-
await this.notifyChange({ path: t, type: l.Added, isDirectory: !0 });
|
|
364
|
-
}
|
|
365
|
-
/**
|
|
366
|
-
* Get file or directory statistics
|
|
367
|
-
*
|
|
368
|
-
* Returns detailed information about a file or directory, including
|
|
369
|
-
* size, modification time, and optionally a hash of the file content.
|
|
370
|
-
*
|
|
371
|
-
* @param path - The path to the file or directory
|
|
372
|
-
* @returns Promise that resolves to FileStat object
|
|
373
|
-
* @throws {OPFSError} If the path does not exist or cannot be accessed
|
|
374
|
-
*
|
|
375
|
-
* @example
|
|
376
|
-
* ```typescript
|
|
377
|
-
* const stats = await fs.stat('/data/config.json');
|
|
378
|
-
* console.log(`File size: ${stats.size} bytes`);
|
|
379
|
-
* console.log(`Last modified: ${stats.mtime}`);
|
|
380
|
-
*
|
|
381
|
-
* // If hashing is enabled, hash will be included
|
|
382
|
-
* if (stats.hash) {
|
|
383
|
-
* console.log(`Hash: ${stats.hash}`);
|
|
384
|
-
* }
|
|
385
|
-
* ```
|
|
386
|
-
*/
|
|
387
|
-
async stat(t) {
|
|
388
|
-
if (await this.mount(), t === "/")
|
|
389
|
-
return {
|
|
390
|
-
kind: "directory",
|
|
391
|
-
size: 0,
|
|
392
|
-
mtime: (/* @__PURE__ */ new Date(0)).toISOString(),
|
|
393
|
-
ctime: (/* @__PURE__ */ new Date(0)).toISOString(),
|
|
394
|
-
isFile: !1,
|
|
395
|
-
isDirectory: !0
|
|
396
|
-
};
|
|
397
|
-
const e = v(t);
|
|
398
|
-
let r;
|
|
399
|
-
try {
|
|
400
|
-
r = await this.getDirectoryHandle(E(t), !1);
|
|
401
|
-
const a = this.options.hashAlgorithm, s = await (await r.getFileHandle(e, { create: !1 })).getFile(), n = {
|
|
402
|
-
kind: "file",
|
|
403
|
-
size: s.size,
|
|
404
|
-
mtime: new Date(s.lastModified).toISOString(),
|
|
405
|
-
ctime: new Date(s.lastModified).toISOString(),
|
|
406
|
-
isFile: !0,
|
|
407
|
-
isDirectory: !1
|
|
408
|
-
};
|
|
409
|
-
if (a === "etag")
|
|
410
|
-
n.hash = `${s.lastModified.toString(36)}-${s.size.toString(36)}`;
|
|
411
|
-
else if (typeof a == "string")
|
|
412
|
-
try {
|
|
413
|
-
const o = await M(s, a, this.options.maxFileSize);
|
|
414
|
-
n.hash = o;
|
|
415
|
-
} catch (o) {
|
|
416
|
-
console.warn(`Failed to calculate hash for ${t}:`, o);
|
|
417
|
-
}
|
|
418
|
-
return n;
|
|
419
|
-
} catch (a) {
|
|
420
|
-
if (a.name === "NotFoundError")
|
|
421
|
-
throw new S("file", t, a);
|
|
422
|
-
if (a.name !== "TypeMismatchError")
|
|
423
|
-
throw new F("stat", t, a);
|
|
424
|
-
}
|
|
425
|
-
try {
|
|
426
|
-
return await r.getDirectoryHandle(e, { create: !1 }), {
|
|
427
|
-
kind: "directory",
|
|
428
|
-
size: 0,
|
|
429
|
-
mtime: (/* @__PURE__ */ new Date(0)).toISOString(),
|
|
430
|
-
ctime: (/* @__PURE__ */ new Date(0)).toISOString(),
|
|
431
|
-
isFile: !1,
|
|
432
|
-
isDirectory: !0
|
|
433
|
-
};
|
|
434
|
-
} catch (a) {
|
|
435
|
-
throw new F("stat", t, a);
|
|
436
|
-
}
|
|
437
|
-
}
|
|
438
|
-
/**
|
|
439
|
-
* Read a directory's contents
|
|
440
|
-
*
|
|
441
|
-
* Lists all files and subdirectories within the specified directory.
|
|
442
|
-
*
|
|
443
|
-
* @param path - The path to the directory to read
|
|
444
|
-
* @returns Promise that resolves to an array of detailed file/directory information
|
|
445
|
-
* @throws {OPFSError} If the directory does not exist or cannot be accessed
|
|
446
|
-
*
|
|
447
|
-
* @example
|
|
448
|
-
* ```typescript
|
|
449
|
-
* // Get detailed information about files and directories
|
|
450
|
-
* const detailed = await fs.readDir('/users/john/documents');
|
|
451
|
-
* detailed.forEach(item => {
|
|
452
|
-
* console.log(`${item.name} - ${item.isFile ? 'file' : 'directory'}`);
|
|
453
|
-
* });
|
|
454
|
-
* ```
|
|
455
|
-
*/
|
|
456
|
-
async readDir(t) {
|
|
457
|
-
await this.mount();
|
|
458
|
-
const e = await this.getDirectoryHandle(t, !1), r = [];
|
|
459
|
-
for await (const [a, i] of e.entries()) {
|
|
460
|
-
const s = i.kind === "file";
|
|
461
|
-
r.push({
|
|
462
|
-
name: a,
|
|
463
|
-
kind: i.kind,
|
|
464
|
-
isFile: s,
|
|
465
|
-
isDirectory: !s
|
|
466
|
-
});
|
|
467
|
-
}
|
|
468
|
-
return r;
|
|
469
|
-
}
|
|
470
|
-
/**
|
|
471
|
-
* Check if a file or directory exists
|
|
472
|
-
*
|
|
473
|
-
* Verifies if a file or directory exists at the specified path.
|
|
474
|
-
*
|
|
475
|
-
* @param path - The path to check
|
|
476
|
-
* @returns Promise that resolves to true if the file or directory exists, false otherwise
|
|
477
|
-
*
|
|
478
|
-
* @example
|
|
479
|
-
* ```typescript
|
|
480
|
-
* const exists = await fs.exists('/config/settings.json');
|
|
481
|
-
* console.log(`File exists: ${exists}`);
|
|
482
|
-
* ```
|
|
483
|
-
*/
|
|
484
|
-
async exists(t) {
|
|
485
|
-
if (await this.mount(), t === "/")
|
|
486
|
-
return !0;
|
|
487
|
-
const e = v(t);
|
|
488
|
-
let r = null;
|
|
489
|
-
try {
|
|
490
|
-
r = await this.getDirectoryHandle(E(t), !1);
|
|
491
|
-
} catch (a) {
|
|
492
|
-
if (r = null, a.name !== "NotFoundError" && a.name !== "TypeMismatchError")
|
|
493
|
-
throw a;
|
|
494
|
-
}
|
|
495
|
-
if (!r || !e)
|
|
496
|
-
return !1;
|
|
497
|
-
try {
|
|
498
|
-
return await r.getFileHandle(e, { create: !1 }), !0;
|
|
499
|
-
} catch (a) {
|
|
500
|
-
if (a.name !== "NotFoundError" && a.name !== "TypeMismatchError")
|
|
501
|
-
throw a;
|
|
502
|
-
try {
|
|
503
|
-
return await r.getDirectoryHandle(e, { create: !1 }), !0;
|
|
504
|
-
} catch (i) {
|
|
505
|
-
if (i.name !== "NotFoundError" && i.name !== "TypeMismatchError")
|
|
506
|
-
throw i;
|
|
507
|
-
return !1;
|
|
508
|
-
}
|
|
509
|
-
}
|
|
510
|
-
}
|
|
511
|
-
/**
|
|
512
|
-
* Clear all contents of a directory without removing the directory itself
|
|
513
|
-
*
|
|
514
|
-
* Removes all files and subdirectories within the specified directory,
|
|
515
|
-
* but keeps the directory itself.
|
|
516
|
-
*
|
|
517
|
-
* @param path - The path to the directory to clear (default: '/')
|
|
518
|
-
* @returns Promise that resolves when all contents are removed
|
|
519
|
-
* @throws {OPFSError} If the operation fails
|
|
520
|
-
*
|
|
521
|
-
* @example
|
|
522
|
-
* ```typescript
|
|
523
|
-
* // Clear root directory contents
|
|
524
|
-
* await fs.clear('/');
|
|
525
|
-
*
|
|
526
|
-
* // Clear specific directory contents
|
|
527
|
-
* await fs.clear('/data');
|
|
528
|
-
* ```
|
|
529
|
-
*/
|
|
530
|
-
async clear(t = "/") {
|
|
531
|
-
await this.mount();
|
|
532
|
-
try {
|
|
533
|
-
const e = await this.readDir(t);
|
|
534
|
-
for (const r of e) {
|
|
535
|
-
const a = `${t === "/" ? "" : t}/${r.name}`;
|
|
536
|
-
await this.remove(a, { recursive: !0 });
|
|
537
|
-
}
|
|
538
|
-
await this.notifyChange({ path: t, type: l.Changed, isDirectory: !0 });
|
|
539
|
-
} catch (e) {
|
|
540
|
-
throw e instanceof w ? e : h(e, { path: t, isDirectory: !0 });
|
|
541
|
-
}
|
|
542
|
-
}
|
|
543
|
-
/**
|
|
544
|
-
* Remove files and directories
|
|
545
|
-
*
|
|
546
|
-
* Removes files and directories. Similar to Node.js fs.rm().
|
|
547
|
-
*
|
|
548
|
-
* @param path - The path to remove
|
|
549
|
-
* @param options - Options for removal
|
|
550
|
-
* @param options.recursive - Whether to remove directories and their contents recursively (default: false)
|
|
551
|
-
* @param options.force - Whether to ignore errors if the path doesn't exist (default: false)
|
|
552
|
-
* @returns Promise that resolves when the removal is complete
|
|
553
|
-
* @throws {OPFSError} If the removal fails
|
|
554
|
-
*
|
|
555
|
-
* @example
|
|
556
|
-
* ```typescript
|
|
557
|
-
* // Remove a file
|
|
558
|
-
* await fs.rm('/path/to/file.txt');
|
|
559
|
-
*
|
|
560
|
-
* // Remove a directory and all its contents
|
|
561
|
-
* await fs.rm('/path/to/directory', { recursive: true });
|
|
562
|
-
*
|
|
563
|
-
* // Remove with force (ignore if doesn't exist)
|
|
564
|
-
* await fs.rm('/maybe/exists', { force: true });
|
|
565
|
-
* ```
|
|
566
|
-
*/
|
|
567
|
-
async remove(t, e) {
|
|
568
|
-
if (await this.mount(), t === "/")
|
|
569
|
-
throw new T("EROOT", t);
|
|
570
|
-
const { recursive: r = !1, force: a = !1 } = e || {}, i = await this.getDirectoryHandle(E(t), !1), s = await this.stat(t);
|
|
571
|
-
await $(i, t, { recursive: r, force: a }), await this.notifyChange({ path: t, type: l.Removed, isDirectory: s.isDirectory });
|
|
572
|
-
}
|
|
573
|
-
/**
|
|
574
|
-
* Resolve a path to an absolute path
|
|
575
|
-
*
|
|
576
|
-
* Resolves relative paths and normalizes path segments (like '..' and '.').
|
|
577
|
-
* Similar to Node.js fs.realpath() but without symlink resolution since OPFS doesn't support symlinks.
|
|
578
|
-
*
|
|
579
|
-
* @param path - The path to resolve
|
|
580
|
-
* @returns Promise that resolves to the absolute normalized path
|
|
581
|
-
* @throws {FileNotFoundError} If the path does not exist
|
|
582
|
-
* @throws {OPFSError} If path resolution fails
|
|
583
|
-
*
|
|
584
|
-
* @example
|
|
585
|
-
* ```typescript
|
|
586
|
-
* // Resolve relative path
|
|
587
|
-
* const absolute = await fs.realpath('./config/../data/file.txt');
|
|
588
|
-
* console.log(absolute); // '/data/file.txt'
|
|
589
|
-
* ```
|
|
590
|
-
*/
|
|
591
|
-
async realpath(t) {
|
|
592
|
-
await this.mount();
|
|
593
|
-
try {
|
|
594
|
-
const e = b(t);
|
|
595
|
-
if (!await this.exists(e))
|
|
596
|
-
throw new S("file", e);
|
|
597
|
-
return e;
|
|
598
|
-
} catch (e) {
|
|
599
|
-
throw e instanceof w ? e : h(e, { path: t });
|
|
600
|
-
}
|
|
601
|
-
}
|
|
602
|
-
/**
|
|
603
|
-
* Rename a file or directory
|
|
604
|
-
*
|
|
605
|
-
* Changes the name of a file or directory. If the target path already exists,
|
|
606
|
-
* it will be replaced only if overwrite option is enabled.
|
|
607
|
-
*
|
|
608
|
-
* @param oldPath - The current path of the file or directory
|
|
609
|
-
* @param newPath - The new path for the file or directory
|
|
610
|
-
* @param options - Options for renaming
|
|
611
|
-
* @param options.overwrite - Whether to overwrite existing files (default: false)
|
|
612
|
-
* @returns Promise that resolves when the rename operation is complete
|
|
613
|
-
* @throws {OPFSError} If the rename operation fails
|
|
614
|
-
*
|
|
615
|
-
* @example
|
|
616
|
-
* ```typescript
|
|
617
|
-
* // Basic rename (fails if target exists)
|
|
618
|
-
* await fs.rename('/old/path/file.txt', '/new/path/renamed.txt');
|
|
619
|
-
*
|
|
620
|
-
* // Rename with overwrite
|
|
621
|
-
* await fs.rename('/old/path/file.txt', '/new/path/renamed.txt', { overwrite: true });
|
|
622
|
-
* ```
|
|
623
|
-
*/
|
|
624
|
-
async rename(t, e, r) {
|
|
625
|
-
await this.mount();
|
|
626
|
-
try {
|
|
627
|
-
const a = r?.overwrite ?? !1, i = await this.stat(t);
|
|
628
|
-
if (await this.exists(e) && !a)
|
|
629
|
-
throw new H(e);
|
|
630
|
-
await this.copy(t, e, { recursive: !0, overwrite: a }), await this.remove(t, { recursive: !0 }), await this.notifyChange({ path: t, type: l.Removed, isDirectory: i.isDirectory }), await this.notifyChange({ path: e, type: l.Added, isDirectory: i.isDirectory });
|
|
631
|
-
} catch (a) {
|
|
632
|
-
throw a instanceof w ? a : h(a, { path: t });
|
|
633
|
-
}
|
|
634
|
-
}
|
|
635
|
-
/**
|
|
636
|
-
* Copy files and directories
|
|
637
|
-
*
|
|
638
|
-
* Copies files and directories. Similar to Node.js fs.cp().
|
|
639
|
-
*
|
|
640
|
-
* @param source - The source path to copy from
|
|
641
|
-
* @param destination - The destination path to copy to
|
|
642
|
-
* @param options - Options for copying
|
|
643
|
-
* @param options.recursive - Whether to copy directories recursively (default: false)
|
|
644
|
-
* @param options.overwrite - Whether to overwrite existing files (default: true)
|
|
645
|
-
* @returns Promise that resolves when the copy operation is complete
|
|
646
|
-
* @throws {OPFSError} If the copy operation fails
|
|
647
|
-
*
|
|
648
|
-
* @example
|
|
649
|
-
* ```typescript
|
|
650
|
-
* // Copy a file
|
|
651
|
-
* await fs.copy('/source/file.txt', '/dest/file.txt');
|
|
652
|
-
*
|
|
653
|
-
* // Copy a directory and all its contents
|
|
654
|
-
* await fs.copy('/source/dir', '/dest/dir', { recursive: true });
|
|
655
|
-
*
|
|
656
|
-
* // Copy without overwriting existing files
|
|
657
|
-
* await fs.copy('/source', '/dest', { recursive: true, overwrite: false });
|
|
658
|
-
* ```
|
|
659
|
-
*/
|
|
660
|
-
async copy(t, e, r) {
|
|
661
|
-
await this.mount();
|
|
662
|
-
try {
|
|
663
|
-
const a = r?.recursive ?? !1, i = r?.overwrite ?? !0;
|
|
664
|
-
if (!await this.exists(t))
|
|
665
|
-
throw new S("source", t);
|
|
666
|
-
if (await this.exists(e) && !i)
|
|
667
|
-
throw new H(e);
|
|
668
|
-
if ((await this.stat(t)).isFile) {
|
|
669
|
-
const c = await this.readFile(t);
|
|
670
|
-
await this.writeFile(e, c);
|
|
671
|
-
} else {
|
|
672
|
-
if (!a)
|
|
673
|
-
throw new B("directory", t);
|
|
674
|
-
await this.mkdir(e, { recursive: !0 });
|
|
675
|
-
const c = await this.readDir(t);
|
|
676
|
-
for (const y of c) {
|
|
677
|
-
const f = `${t}/${y.name}`, d = `${e}/${y.name}`;
|
|
678
|
-
await this.copy(f, d, { recursive: !0, overwrite: i });
|
|
679
|
-
}
|
|
680
|
-
}
|
|
681
|
-
} catch (a) {
|
|
682
|
-
throw a instanceof w ? a : h(a, { path: t });
|
|
683
|
-
}
|
|
684
|
-
}
|
|
685
|
-
/**
|
|
686
|
-
* Start watching a file or directory for changes
|
|
687
|
-
*
|
|
688
|
-
* @param path - The path to watch (minimatch syntax allowed)
|
|
689
|
-
* @param options - Watch options
|
|
690
|
-
* @param options.recursive - Whether to watch recursively (default: true)
|
|
691
|
-
* @param options.exclude - Glob pattern(s) to exclude (minimatch).
|
|
692
|
-
* @returns Promise that resolves when watching starts
|
|
693
|
-
*
|
|
694
|
-
* @example
|
|
695
|
-
* ```typescript
|
|
696
|
-
* // Watch entire directory tree recursively (default)
|
|
697
|
-
* await fs.watch('/data');
|
|
698
|
-
*
|
|
699
|
-
* // Watch only immediate children (shallow)
|
|
700
|
-
* await fs.watch('/data', { recursive: false });
|
|
701
|
-
*
|
|
702
|
-
* // Watch a single file
|
|
703
|
-
* await fs.watch('/config.json', { recursive: false });
|
|
704
|
-
*
|
|
705
|
-
* // Watch all json files but not in dist directory
|
|
706
|
-
* await fs.watch('/**\/*.json', { recursive: false, exclude: ['dist/**'] });
|
|
707
|
-
*
|
|
708
|
-
* ```
|
|
709
|
-
*/
|
|
710
|
-
async watch(t, e) {
|
|
711
|
-
if (!this.options.broadcastChannel)
|
|
712
|
-
throw new N("This instance is not configured to send events. Please specify options.broadcastChannel to enable watching.");
|
|
713
|
-
const r = {
|
|
714
|
-
pattern: _(t, e?.recursive ?? !0),
|
|
715
|
-
include: Array.isArray(e?.include) ? e.include : [e?.include ?? "**"],
|
|
716
|
-
exclude: Array.isArray(e?.exclude) ? e.exclude : [e?.exclude ?? ""]
|
|
717
|
-
};
|
|
718
|
-
this.watchers.set(t, r);
|
|
719
|
-
}
|
|
720
|
-
/**
|
|
721
|
-
* Stop watching a previously watched path
|
|
722
|
-
*/
|
|
723
|
-
unwatch(t) {
|
|
724
|
-
this.watchers.delete(t);
|
|
725
|
-
}
|
|
726
|
-
/**
|
|
727
|
-
* Open a file and return a file descriptor
|
|
728
|
-
*
|
|
729
|
-
* @param path - The path to the file to open
|
|
730
|
-
* @param options - Options for opening the file
|
|
731
|
-
* @param options.create - Whether to create the file if it doesn't exist (default: false)
|
|
732
|
-
* @param options.exclusive - If true and create is true, fails if file already exists (default: false)
|
|
733
|
-
* Note: This is best-effort in OPFS, not fully atomic due to browser limitations
|
|
734
|
-
* @param options.truncate - Whether to truncate the file to zero length (default: false)
|
|
735
|
-
* @returns Promise that resolves to a file descriptor number
|
|
736
|
-
* @throws {OPFSError} If opening the file fails
|
|
737
|
-
*
|
|
738
|
-
* @example
|
|
739
|
-
* ```typescript
|
|
740
|
-
* // Open existing file for reading
|
|
741
|
-
* const fd = await fs.open('/data/config.json');
|
|
742
|
-
*
|
|
743
|
-
* // Create new file for writing
|
|
744
|
-
* const fd = await fs.open('/data/new.txt', { create: true });
|
|
745
|
-
*
|
|
746
|
-
* // Create file exclusively (fails if exists)
|
|
747
|
-
* const fd = await fs.open('/data/unique.txt', { create: true, exclusive: true });
|
|
748
|
-
*
|
|
749
|
-
* // Open and truncate file
|
|
750
|
-
* const fd = await fs.open('/data/log.txt', { create: true, truncate: true });
|
|
751
|
-
* ```
|
|
752
|
-
*/
|
|
753
|
-
async open(t, e) {
|
|
754
|
-
await this.mount();
|
|
755
|
-
const { create: r = !1, exclusive: a = !1, truncate: i = !1 } = e || {}, s = p(b(t));
|
|
756
|
-
try {
|
|
757
|
-
return r && a ? await u(s, async () => {
|
|
758
|
-
if (await this.exists(s))
|
|
759
|
-
throw new H(s);
|
|
760
|
-
return this._openFile(s, r, i);
|
|
761
|
-
}) : await this._openFile(s, r, i);
|
|
762
|
-
} catch (n) {
|
|
763
|
-
if (n instanceof w)
|
|
764
|
-
throw n;
|
|
765
|
-
const o = n && n.name === "TypeMismatchError";
|
|
766
|
-
throw h(n, {
|
|
767
|
-
path: s,
|
|
768
|
-
isDirectory: !!o
|
|
769
|
-
});
|
|
770
|
-
}
|
|
771
|
-
}
|
|
772
|
-
/**
|
|
773
|
-
* Internal method to open a file (without locking)
|
|
774
|
-
*
|
|
775
|
-
* Multiple FDs for the same path share one sync access handle (OPFS limit),
|
|
776
|
-
* with independent per-FD positions — similar to Node.js.
|
|
777
|
-
* @private
|
|
778
|
-
*/
|
|
779
|
-
async _openFile(t, e, r) {
|
|
780
|
-
let a = this.openHandles.get(t);
|
|
781
|
-
if (!a) {
|
|
782
|
-
const s = await this.getFileHandle(t, e);
|
|
783
|
-
try {
|
|
784
|
-
await s.getFile();
|
|
785
|
-
} catch (o) {
|
|
786
|
-
throw h(o, { path: t, isDirectory: !0 });
|
|
787
|
-
}
|
|
788
|
-
const n = await R(s, t);
|
|
789
|
-
a = { fileHandle: s, syncHandle: n, refCount: 0 }, this.openHandles.set(t, a);
|
|
790
|
-
}
|
|
791
|
-
a.refCount++, r && (a.syncHandle.truncate(0), a.syncHandle.flush());
|
|
792
|
-
const i = this.nextFd++;
|
|
793
|
-
return this.openFiles.set(i, {
|
|
794
|
-
path: t,
|
|
795
|
-
fileHandle: a.fileHandle,
|
|
796
|
-
syncHandle: a.syncHandle,
|
|
797
|
-
position: 0
|
|
798
|
-
}), i;
|
|
799
|
-
}
|
|
800
|
-
/**
|
|
801
|
-
* Close a file descriptor
|
|
802
|
-
*
|
|
803
|
-
* @param fd - The file descriptor to close
|
|
804
|
-
* @returns Promise that resolves when the file descriptor is closed
|
|
805
|
-
* @throws {OPFSError} If the file descriptor is invalid or closing fails
|
|
806
|
-
*
|
|
807
|
-
* @example
|
|
808
|
-
* ```typescript
|
|
809
|
-
* const fd = await fs.open('/data/file.txt');
|
|
810
|
-
* // ... use the file descriptor ...
|
|
811
|
-
* await fs.close(fd);
|
|
812
|
-
* ```
|
|
813
|
-
*/
|
|
814
|
-
async close(t) {
|
|
815
|
-
const e = this._getFileDescriptor(t);
|
|
816
|
-
this.openFiles.delete(t);
|
|
817
|
-
const r = this.openHandles.get(e.path);
|
|
818
|
-
r && (r.refCount--, r.refCount <= 0 && (A(t, r.syncHandle, e.path), this.openHandles.delete(e.path)));
|
|
819
|
-
}
|
|
820
|
-
/**
|
|
821
|
-
* Read data from a file descriptor
|
|
822
|
-
*
|
|
823
|
-
* @param fd - The file descriptor to read from
|
|
824
|
-
* @param buffer - The buffer to read data into
|
|
825
|
-
* @param offset - The offset in the buffer to start writing at
|
|
826
|
-
* @param length - The number of bytes to read
|
|
827
|
-
* @param position - The position in the file to read from (null for current position)
|
|
828
|
-
* @returns Promise that resolves to the number of bytes read and the modified buffer
|
|
829
|
-
* @throws {OPFSError} If the file descriptor is invalid or reading fails
|
|
830
|
-
*
|
|
831
|
-
* @note This method uses Comlink.transfer() to efficiently pass the buffer as a Transferable Object,
|
|
832
|
-
* ensuring zero-copy performance across Web Worker boundaries.
|
|
833
|
-
*
|
|
834
|
-
* @example
|
|
835
|
-
* ```typescript
|
|
836
|
-
* const fd = await fs.open('/data/file.txt');
|
|
837
|
-
* const buffer = new Uint8Array(1024);
|
|
838
|
-
* const { bytesRead, buffer: modifiedBuffer } = await fs.read(fd, buffer, 0, 1024, null);
|
|
839
|
-
* console.log(`Read ${bytesRead} bytes`);
|
|
840
|
-
* // Use modifiedBuffer which contains the actual data
|
|
841
|
-
* await fs.close(fd);
|
|
842
|
-
* ```
|
|
843
|
-
*/
|
|
844
|
-
async read(t, e, r, a, i) {
|
|
845
|
-
const s = this._getFileDescriptor(t);
|
|
846
|
-
I(e.length, r, a, i);
|
|
847
|
-
try {
|
|
848
|
-
const n = i ?? s.position, o = s.syncHandle.getSize(), { isEOF: c, actualLength: y } = L(n, a, o);
|
|
849
|
-
if (c)
|
|
850
|
-
return x({ bytesRead: 0, buffer: e }, [e.buffer]);
|
|
851
|
-
const f = e.subarray(r, r + y), d = s.syncHandle.read(f, { at: n });
|
|
852
|
-
return i == null && (s.position = n + d), x({ bytesRead: d, buffer: e }, [e.buffer]);
|
|
853
|
-
} catch (n) {
|
|
854
|
-
throw m("read", t, s.path, n);
|
|
855
|
-
}
|
|
856
|
-
}
|
|
857
|
-
/**
|
|
858
|
-
* Write data to a file descriptor
|
|
859
|
-
*
|
|
860
|
-
* @param fd - The file descriptor to write to
|
|
861
|
-
* @param buffer - The buffer containing data to write
|
|
862
|
-
* @param offset - The offset in the buffer to start reading from (default: 0)
|
|
863
|
-
* @param length - The number of bytes to write (default: entire buffer)
|
|
864
|
-
* @param position - The position in the file to write to (null/undefined for current position)
|
|
865
|
-
* @param emitEvent - Whether to emit a change event (default: true)
|
|
866
|
-
* @returns Promise that resolves to the number of bytes written
|
|
867
|
-
* @throws {OPFSError} If the file descriptor is invalid or writing fails
|
|
868
|
-
*
|
|
869
|
-
* @example
|
|
870
|
-
* ```typescript
|
|
871
|
-
* const fd = await fs.open('/data/file.txt', { create: true });
|
|
872
|
-
* const data = new TextEncoder().encode('Hello, World!');
|
|
873
|
-
* const bytesWritten = await fs.write(fd, data, 0, data.length, null);
|
|
874
|
-
* console.log(`Wrote ${bytesWritten} bytes`);
|
|
875
|
-
* await fs.close(fd);
|
|
876
|
-
* ```
|
|
877
|
-
*/
|
|
878
|
-
async write(t, e, r = 0, a, i, s = !0) {
|
|
879
|
-
const n = this._getFileDescriptor(t), o = a ?? e.length - r;
|
|
880
|
-
I(e.length, r, o, i);
|
|
881
|
-
try {
|
|
882
|
-
const c = i ?? n.position, y = e.subarray(r, r + o), f = n.syncHandle.write(y, { at: c });
|
|
883
|
-
return (i == null || i === n.position) && (n.position = c + f), s && await this.notifyChange({ path: n.path, type: l.Changed, isDirectory: !1 }), f;
|
|
884
|
-
} catch (c) {
|
|
885
|
-
throw m("write", t, n.path, c);
|
|
886
|
-
}
|
|
887
|
-
}
|
|
888
|
-
/**
|
|
889
|
-
* Get file status information by file descriptor
|
|
890
|
-
*
|
|
891
|
-
* @param fd - The file descriptor
|
|
892
|
-
* @returns Promise that resolves to FileStat object
|
|
893
|
-
* @throws {OPFSError} If the file descriptor is invalid
|
|
894
|
-
*
|
|
895
|
-
* @example
|
|
896
|
-
* ```typescript
|
|
897
|
-
* const fd = await fs.open('/data/file.txt');
|
|
898
|
-
* const stats = await fs.fstat(fd);
|
|
899
|
-
* console.log(`File size: ${stats.size} bytes`);
|
|
900
|
-
* console.log(`Last modified: ${stats.mtime}`);
|
|
901
|
-
*
|
|
902
|
-
* // If hashing is enabled, hash will be included
|
|
903
|
-
* if (stats.hash) {
|
|
904
|
-
* console.log(`Hash: ${stats.hash}`);
|
|
905
|
-
* }
|
|
906
|
-
* ```
|
|
907
|
-
*/
|
|
908
|
-
async fstat(t) {
|
|
909
|
-
const e = this._getFileDescriptor(t);
|
|
910
|
-
return this.stat(e.path);
|
|
911
|
-
}
|
|
912
|
-
/**
|
|
913
|
-
* Truncate file to specified size
|
|
914
|
-
*
|
|
915
|
-
* @param fd - The file descriptor
|
|
916
|
-
* @param size - The new size of the file (default: 0)
|
|
917
|
-
* @returns Promise that resolves when truncation is complete
|
|
918
|
-
* @throws {OPFSError} If the file descriptor is invalid or truncation fails
|
|
919
|
-
*
|
|
920
|
-
* @example
|
|
921
|
-
* ```typescript
|
|
922
|
-
* const fd = await fs.open('/data/file.txt', { create: true });
|
|
923
|
-
* await fs.truncate(fd, 100); // Truncate to 100 bytes
|
|
924
|
-
* ```
|
|
925
|
-
*/
|
|
926
|
-
async ftruncate(t, e = 0) {
|
|
927
|
-
const r = this._getFileDescriptor(t);
|
|
928
|
-
if (e < 0 || !Number.isInteger(e))
|
|
929
|
-
throw new C("argument", "Invalid size");
|
|
930
|
-
try {
|
|
931
|
-
r.syncHandle.truncate(e), r.syncHandle.flush(), r.position > e && (r.position = e), await this.notifyChange({ path: r.path, type: l.Changed, isDirectory: !1 });
|
|
932
|
-
} catch (a) {
|
|
933
|
-
throw m("truncate", t, r.path, a);
|
|
934
|
-
}
|
|
935
|
-
}
|
|
936
|
-
/**
|
|
937
|
-
* Synchronize file data to storage (fsync equivalent)
|
|
938
|
-
*
|
|
939
|
-
* @param fd - The file descriptor
|
|
940
|
-
* @returns Promise that resolves when synchronization is complete
|
|
941
|
-
* @throws {OPFSError} If the file descriptor is invalid or sync fails
|
|
942
|
-
*
|
|
943
|
-
* @example
|
|
944
|
-
* ```typescript
|
|
945
|
-
* const fd = await fs.open('/data/file.txt', { create: true });
|
|
946
|
-
* await fs.write(fd, data);
|
|
947
|
-
* await fs.fsync(fd); // Ensure data is written to storage
|
|
948
|
-
* ```
|
|
949
|
-
*/
|
|
950
|
-
async fsync(t) {
|
|
951
|
-
const e = this._getFileDescriptor(t);
|
|
952
|
-
try {
|
|
953
|
-
e.syncHandle.flush();
|
|
954
|
-
} catch (r) {
|
|
955
|
-
throw m("sync", t, e.path, r);
|
|
956
|
-
}
|
|
957
|
-
}
|
|
958
|
-
/**
|
|
959
|
-
* Dispose of resources and clean up the file system instance
|
|
960
|
-
*
|
|
961
|
-
* This method should be called when the file system instance is no longer needed
|
|
962
|
-
* to properly clean up resources like the broadcast channel and watch timers.
|
|
963
|
-
*/
|
|
964
|
-
dispose() {
|
|
965
|
-
this.broadcastChannel && (this.broadcastChannel.close(), this.broadcastChannel = null), this.watchers.clear();
|
|
966
|
-
for (const [t, e] of this.openHandles)
|
|
967
|
-
A(-1, e.syncHandle, t);
|
|
968
|
-
this.openHandles.clear(), this.openFiles.clear(), this.nextFd = 1;
|
|
969
|
-
}
|
|
970
|
-
/**
|
|
971
|
-
* Synchronize the file system with external data
|
|
972
|
-
*
|
|
973
|
-
* Syncs the file system with an array of entries containing paths and data.
|
|
974
|
-
* This is useful for importing data from external sources or syncing with remote data.
|
|
975
|
-
*
|
|
976
|
-
* @param entries - Array of [path, data] tuples to sync
|
|
977
|
-
* @returns Promise that resolves when synchronization is complete
|
|
978
|
-
* @throws {OPFSError} If the synchronization fails
|
|
979
|
-
*
|
|
980
|
-
* @example
|
|
981
|
-
* ```typescript
|
|
982
|
-
* // Sync with external data
|
|
983
|
-
* const entries: [string, string | Uint8Array | Blob][] = [
|
|
984
|
-
* ['/config.json', JSON.stringify({ theme: 'dark' })],
|
|
985
|
-
* ['/data/binary.dat', new Uint8Array([1, 2, 3, 4])],
|
|
986
|
-
* ['/upload.txt', new Blob(['file content'], { type: 'text/plain' })]
|
|
987
|
-
* ];
|
|
988
|
-
*
|
|
989
|
-
* // Sync without clearing existing files
|
|
990
|
-
* await fs.sync(entries);
|
|
991
|
-
*
|
|
992
|
-
* // Clean file system and then sync
|
|
993
|
-
* await fs.sync(entries, { cleanBefore: true });
|
|
994
|
-
* ```
|
|
995
|
-
*/
|
|
996
|
-
async createIndex(t) {
|
|
997
|
-
await this.mount();
|
|
998
|
-
try {
|
|
999
|
-
for (const [e, r] of t) {
|
|
1000
|
-
const a = p(e);
|
|
1001
|
-
let i;
|
|
1002
|
-
r instanceof Blob ? i = await U(r) : typeof r == "string" ? i = new TextEncoder().encode(r) : i = r, await this.writeFile(a, i);
|
|
1003
|
-
}
|
|
1004
|
-
} catch (e) {
|
|
1005
|
-
throw e instanceof w ? e : h(e);
|
|
1006
|
-
}
|
|
1007
|
-
}
|
|
1008
|
-
}
|
|
1
|
+
import { A as e, K as s, B as o, D as t, E as i, F as n, g as E, c, I as l, f as S, O as d, a as p, d as m, e as h, P as F, b as O, S as y, V as P, W as f, k as u, x, v as B, H as A, i as v, y as I, u as N, h as b, J as g, N as z, l as D, M as H, t as L, L as T, q as k, j as M, m as R, p as W, o as j, n as q, z as w, r as C, G as V, s as _, C as G, w as J } from "./BaseOPFS-Cylz6ijk.js";
|
|
2
|
+
import { O as U } from "./OPFSSync-CsSQkpk5.js";
|
|
3
|
+
import { O as Y } from "./OPFSAsync-BKSAJJeh.js";
|
|
1009
4
|
export {
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
5
|
+
e as AlreadyExistsError,
|
|
6
|
+
s as BINARY_FILE_EXTENSIONS,
|
|
7
|
+
o as BaseOPFS,
|
|
8
|
+
t as DirectoryOperationError,
|
|
9
|
+
i as ExistenceError,
|
|
10
|
+
n as FileBusyError,
|
|
11
|
+
E as FileSystemOperationError,
|
|
12
|
+
c as FileTypeError,
|
|
13
|
+
l as IOError,
|
|
14
|
+
S as InitializationFailedError,
|
|
15
|
+
Y as OPFSAsync,
|
|
16
|
+
d as OPFSError,
|
|
17
|
+
p as OPFSNotSupportedError,
|
|
18
|
+
U as OPFSSync,
|
|
19
|
+
m as OperationAbortedError,
|
|
20
|
+
h as OperationNotSupportedError,
|
|
21
|
+
F as PathError,
|
|
22
|
+
O as PermissionError,
|
|
23
|
+
y as StorageError,
|
|
24
|
+
P as ValidationError,
|
|
25
|
+
f as WatchEventType,
|
|
26
|
+
u as basename,
|
|
27
|
+
x as buffersEqual,
|
|
28
|
+
B as calculateFileHash,
|
|
29
|
+
A as calculateReadLength,
|
|
30
|
+
v as checkOPFSSupport,
|
|
31
|
+
I as convertBlobToUint8Array,
|
|
32
|
+
N as createBuffer,
|
|
33
|
+
b as createFDError,
|
|
34
|
+
g as createSyncHandleSafe,
|
|
35
|
+
z as decodeBuffer,
|
|
36
|
+
D as dirname,
|
|
37
|
+
H as encodeString,
|
|
38
|
+
L as extname,
|
|
39
|
+
T as isBinaryFileExtension,
|
|
40
|
+
k as isPathExcluded,
|
|
41
|
+
M as joinPath,
|
|
42
|
+
R as mapDomError,
|
|
43
|
+
W as matchMinimatch,
|
|
44
|
+
j as normalizeMinimatch,
|
|
45
|
+
q as normalizePath,
|
|
46
|
+
w as removeEntry,
|
|
47
|
+
C as resolvePath,
|
|
48
|
+
V as safeCloseSyncHandle,
|
|
49
|
+
_ as splitPath,
|
|
50
|
+
G as validateReadWriteArgs,
|
|
51
|
+
J as withLock
|
|
1055
52
|
};
|
|
1056
53
|
//# sourceMappingURL=index.pure.js.map
|