opfs-worker 1.1.0 → 1.2.1
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 +19 -6
- package/dist/assets/worker-CLvhwwfc.js.map +1 -0
- package/dist/facade.d.ts +141 -0
- package/dist/facade.d.ts.map +1 -0
- package/dist/{helpers-DS5dyURe.js → helpers-CkNHswLp.js} +547 -573
- package/dist/helpers-CkNHswLp.js.map +1 -0
- package/dist/helpers-TAynP0fb.cjs +4 -0
- package/dist/helpers-TAynP0fb.cjs.map +1 -0
- package/dist/index.cjs +753 -878
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +4 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1016 -932
- package/dist/index.js.map +1 -1
- package/dist/raw.cjs +1 -1
- package/dist/raw.cjs.map +1 -1
- package/dist/raw.d.ts +13 -0
- package/dist/raw.d.ts.map +1 -0
- package/dist/raw.js +187 -148
- package/dist/raw.js.map +1 -1
- package/dist/types.d.ts +7 -3
- package/dist/types.d.ts.map +1 -1
- package/dist/utils/encoder.d.ts +1 -2
- package/dist/utils/encoder.d.ts.map +1 -1
- package/dist/utils/helpers.d.ts +2 -22
- package/dist/utils/helpers.d.ts.map +1 -1
- package/dist/worker.d.ts +22 -32
- package/dist/worker.d.ts.map +1 -1
- package/package.json +3 -3
- package/dist/assets/worker-CvILLJKw.js.map +0 -1
- package/dist/helpers-CKqaiMjI.cjs +0 -4
- package/dist/helpers-CKqaiMjI.cjs.map +0 -1
- package/dist/helpers-DS5dyURe.js.map +0 -1
package/dist/raw.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { expose as
|
|
2
|
-
import { O as o,
|
|
3
|
-
class
|
|
1
|
+
import { expose as x, transfer as u } from "comlink";
|
|
2
|
+
import { O as o, o as m, g as A, n as g, s as F, P as C, w as d, F as D, j as b, h as E, k as p, u as H, y as $, r as v, l as T, C as O, A as S, z as I, B as N, f, x as P } from "./helpers-CkNHswLp.js";
|
|
3
|
+
class _ {
|
|
4
4
|
/** Root directory handle for the file system */
|
|
5
5
|
root;
|
|
6
6
|
/** Map of watched paths and options */
|
|
@@ -14,7 +14,7 @@ class M {
|
|
|
14
14
|
root: "/",
|
|
15
15
|
namespace: "",
|
|
16
16
|
maxFileSize: 50 * 1024 * 1024,
|
|
17
|
-
hashAlgorithm:
|
|
17
|
+
hashAlgorithm: !1,
|
|
18
18
|
broadcastChannel: "opfs-worker"
|
|
19
19
|
};
|
|
20
20
|
/** Map of open file descriptors to their metadata */
|
|
@@ -44,12 +44,12 @@ class M {
|
|
|
44
44
|
if (!this.options.broadcastChannel)
|
|
45
45
|
return;
|
|
46
46
|
const e = t.path;
|
|
47
|
-
if (![...this.watchers.values()].some((r) =>
|
|
47
|
+
if (![...this.watchers.values()].some((r) => m(e, r.pattern) && r.include.some((s) => s && m(e, s)) && !r.exclude.some((s) => s && m(e, s))))
|
|
48
48
|
return;
|
|
49
|
-
let
|
|
49
|
+
let i;
|
|
50
50
|
if (this.options.hashAlgorithm)
|
|
51
51
|
try {
|
|
52
|
-
|
|
52
|
+
i = (await this.stat(e)).hash;
|
|
53
53
|
} catch {
|
|
54
54
|
}
|
|
55
55
|
try {
|
|
@@ -58,7 +58,7 @@ class M {
|
|
|
58
58
|
namespace: this.options.namespace,
|
|
59
59
|
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
60
60
|
...t,
|
|
61
|
-
...
|
|
61
|
+
...i && { hash: i }
|
|
62
62
|
};
|
|
63
63
|
this.broadcastChannel.postMessage(r);
|
|
64
64
|
} catch (r) {
|
|
@@ -76,7 +76,7 @@ class M {
|
|
|
76
76
|
* @throws {OPFSError} If OPFS is not supported in the current browser
|
|
77
77
|
*/
|
|
78
78
|
constructor(t) {
|
|
79
|
-
|
|
79
|
+
A(), t && this.setOptions(t);
|
|
80
80
|
}
|
|
81
81
|
/**
|
|
82
82
|
* Initialize the file system within a given directory
|
|
@@ -101,12 +101,12 @@ class M {
|
|
|
101
101
|
*/
|
|
102
102
|
async mount() {
|
|
103
103
|
const t = this.options.root;
|
|
104
|
-
return this.mountingPromise && await this.mountingPromise, this.mountingPromise = new Promise(async (e,
|
|
104
|
+
return this.mountingPromise && await this.mountingPromise, this.mountingPromise = new Promise(async (e, a) => {
|
|
105
105
|
try {
|
|
106
|
-
const
|
|
107
|
-
this.root = t === "/" ?
|
|
108
|
-
} catch (
|
|
109
|
-
|
|
106
|
+
const i = await navigator.storage.getDirectory();
|
|
107
|
+
this.root = t === "/" ? i : await this.getDirectoryHandle(t, !0, i), e(!0);
|
|
108
|
+
} catch (i) {
|
|
109
|
+
a(new o("Failed to initialize OPFS", "INIT_FAILED", t, i));
|
|
110
110
|
} finally {
|
|
111
111
|
this.mountingPromise = null;
|
|
112
112
|
}
|
|
@@ -123,7 +123,7 @@ class M {
|
|
|
123
123
|
* @param options.broadcastChannel - Custom name for the broadcast channel
|
|
124
124
|
*/
|
|
125
125
|
async setOptions(t) {
|
|
126
|
-
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 =
|
|
126
|
+
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 = g(t.root), this.options.namespace || (this.options.namespace = `opfs-worker:${this.options.root}`), await this.mount());
|
|
127
127
|
}
|
|
128
128
|
/**
|
|
129
129
|
* Get a directory handle from a path
|
|
@@ -143,10 +143,10 @@ class M {
|
|
|
143
143
|
* const docsDir2 = await fs.getDirectoryHandle(['users', 'john', 'documents'], true);
|
|
144
144
|
* ```
|
|
145
145
|
*/
|
|
146
|
-
async getDirectoryHandle(t, e = !1,
|
|
147
|
-
const
|
|
148
|
-
let r =
|
|
149
|
-
for (const s of
|
|
146
|
+
async getDirectoryHandle(t, e = !1, a = this.root) {
|
|
147
|
+
const i = Array.isArray(t) ? t : F(t);
|
|
148
|
+
let r = a;
|
|
149
|
+
for (const s of i)
|
|
150
150
|
r = await r.getDirectoryHandle(s, { create: e });
|
|
151
151
|
return r;
|
|
152
152
|
}
|
|
@@ -169,12 +169,12 @@ class M {
|
|
|
169
169
|
* const fileHandle2 = await fs.getFileHandle(['config', 'settings.json'], true);
|
|
170
170
|
* ```
|
|
171
171
|
*/
|
|
172
|
-
async getFileHandle(t, e = !1,
|
|
173
|
-
const
|
|
174
|
-
if (
|
|
175
|
-
throw new
|
|
176
|
-
const r =
|
|
177
|
-
return (await this.getDirectoryHandle(
|
|
172
|
+
async getFileHandle(t, e = !1, a = this.root) {
|
|
173
|
+
const i = F(t);
|
|
174
|
+
if (i.length === 0)
|
|
175
|
+
throw new C("Path must not be empty", Array.isArray(t) ? t.join("/") : t);
|
|
176
|
+
const r = i.pop();
|
|
177
|
+
return (await this.getDirectoryHandle(i, e, a)).getFileHandle(r, { create: e });
|
|
178
178
|
}
|
|
179
179
|
/**
|
|
180
180
|
* Get a complete index of all files and directories in the file system
|
|
@@ -196,10 +196,10 @@ class M {
|
|
|
196
196
|
* ```
|
|
197
197
|
*/
|
|
198
198
|
async index() {
|
|
199
|
-
const t = /* @__PURE__ */ new Map(), e = async (
|
|
200
|
-
const
|
|
201
|
-
for (const r of
|
|
202
|
-
const s = `${
|
|
199
|
+
const t = /* @__PURE__ */ new Map(), e = async (a) => {
|
|
200
|
+
const i = await this.readDir(a);
|
|
201
|
+
for (const r of i) {
|
|
202
|
+
const s = `${a === "/" ? "" : a}/${r.name}`;
|
|
203
203
|
try {
|
|
204
204
|
const n = await this.stat(s);
|
|
205
205
|
t.set(s, n), n.isDirectory && await e(s);
|
|
@@ -217,71 +217,111 @@ class M {
|
|
|
217
217
|
isDirectory: !0
|
|
218
218
|
}), await e("/"), t;
|
|
219
219
|
}
|
|
220
|
-
|
|
221
|
-
|
|
220
|
+
/**
|
|
221
|
+
* Read a file from the file system
|
|
222
|
+
*
|
|
223
|
+
* Reads the contents of a file and returns it as binary data.
|
|
224
|
+
*
|
|
225
|
+
* @param path - The path to the file to read
|
|
226
|
+
* @returns Promise that resolves to the file contents as Uint8Array
|
|
227
|
+
* @throws {FileNotFoundError} If the file does not exist
|
|
228
|
+
* @throws {OPFSError} If reading the file fails
|
|
229
|
+
*
|
|
230
|
+
* @example
|
|
231
|
+
* ```typescript
|
|
232
|
+
* // Read as binary data
|
|
233
|
+
* const content = await fs.readFile('/config/settings.json');
|
|
234
|
+
*
|
|
235
|
+
* // Read binary file
|
|
236
|
+
* const binaryData = await fs.readFile('/images/logo.png');
|
|
237
|
+
* ```
|
|
238
|
+
*/
|
|
239
|
+
async readFile(t) {
|
|
240
|
+
await this.mount();
|
|
222
241
|
try {
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
242
|
+
return await d(t, "shared", async () => {
|
|
243
|
+
const e = await this.open(t);
|
|
244
|
+
try {
|
|
245
|
+
const { size: a } = await this.fstat(e), i = new Uint8Array(a);
|
|
246
|
+
return a > 0 && await this.read(e, i, 0, a, 0), u(i, [i.buffer]);
|
|
247
|
+
} finally {
|
|
248
|
+
await this.close(e);
|
|
249
|
+
}
|
|
250
|
+
});
|
|
251
|
+
} catch (e) {
|
|
252
|
+
throw new D(t, e);
|
|
227
253
|
}
|
|
228
254
|
}
|
|
229
255
|
/**
|
|
230
256
|
* Write data to a file
|
|
231
257
|
*
|
|
232
|
-
* Creates or overwrites a file with the specified data. If the file already
|
|
258
|
+
* Creates or overwrites a file with the specified binary data. If the file already
|
|
233
259
|
* exists, it will be truncated before writing.
|
|
234
260
|
*
|
|
235
261
|
* @param path - The path to the file to write
|
|
236
|
-
* @param data - The data to write to the file (
|
|
237
|
-
* @param encoding - The encoding to use when writing string data (default: 'utf-8')
|
|
262
|
+
* @param data - The binary data to write to the file (Uint8Array or ArrayBuffer)
|
|
238
263
|
* @returns Promise that resolves when the write operation is complete
|
|
239
264
|
* @throws {OPFSError} If writing the file fails
|
|
240
265
|
*
|
|
241
266
|
* @example
|
|
242
267
|
* ```typescript
|
|
243
|
-
* // Write text data
|
|
244
|
-
* await fs.writeFile('/config/settings.json', JSON.stringify({ theme: 'dark' }));
|
|
245
|
-
*
|
|
246
268
|
* // Write binary data
|
|
247
269
|
* const binaryData = new Uint8Array([1, 2, 3, 4, 5]);
|
|
248
270
|
* await fs.writeFile('/data/binary.dat', binaryData);
|
|
249
271
|
*
|
|
250
|
-
* // Write
|
|
251
|
-
*
|
|
272
|
+
* // Write from ArrayBuffer
|
|
273
|
+
* const arrayBuffer = new ArrayBuffer(10);
|
|
274
|
+
* await fs.writeFile('/data/buffer.dat', arrayBuffer);
|
|
252
275
|
* ```
|
|
253
276
|
*/
|
|
254
|
-
async writeFile(t, e
|
|
277
|
+
async writeFile(t, e) {
|
|
255
278
|
await this.mount();
|
|
256
|
-
const a =
|
|
257
|
-
|
|
279
|
+
const a = e instanceof Uint8Array ? e : new Uint8Array(e);
|
|
280
|
+
await d(t, "exclusive", async () => {
|
|
281
|
+
const i = await this.exists(t), r = await this.open(t, { create: !0, truncate: !0 });
|
|
282
|
+
try {
|
|
283
|
+
await this.write(r, a, 0, a.length, null, !1), await this.fsync(r);
|
|
284
|
+
} finally {
|
|
285
|
+
await this.close(r);
|
|
286
|
+
}
|
|
287
|
+
await this.notifyChange({ path: t, type: i ? "changed" : "added", isDirectory: !1 });
|
|
288
|
+
});
|
|
258
289
|
}
|
|
259
290
|
/**
|
|
260
291
|
* Append data to a file
|
|
261
292
|
*
|
|
262
|
-
* Adds data to the end of an existing file. If the file doesn't exist,
|
|
293
|
+
* Adds binary data to the end of an existing file. If the file doesn't exist,
|
|
263
294
|
* it will be created.
|
|
264
295
|
*
|
|
265
296
|
* @param path - The path to the file to append to
|
|
266
|
-
* @param data - The data to append to the file (
|
|
267
|
-
* @param encoding - The encoding to use when appending string data (default: 'utf-8')
|
|
297
|
+
* @param data - The binary data to append to the file (Uint8Array or ArrayBuffer)
|
|
268
298
|
* @returns Promise that resolves when the append operation is complete
|
|
269
299
|
* @throws {OPFSError} If appending to the file fails
|
|
270
300
|
*
|
|
271
301
|
* @example
|
|
272
302
|
* ```typescript
|
|
273
|
-
* // Append text to a log file
|
|
274
|
-
* await fs.appendFile('/logs/app.log', `[${new Date().toISOString()}] User logged in\n`);
|
|
275
|
-
*
|
|
276
303
|
* // Append binary data
|
|
277
304
|
* const additionalData = new Uint8Array([6, 7, 8]);
|
|
278
305
|
* await fs.appendFile('/data/binary.dat', additionalData);
|
|
306
|
+
*
|
|
307
|
+
* // Append from ArrayBuffer
|
|
308
|
+
* const arrayBuffer = new ArrayBuffer(5);
|
|
309
|
+
* await fs.appendFile('/data/buffer.dat', arrayBuffer);
|
|
279
310
|
* ```
|
|
280
311
|
*/
|
|
281
|
-
async appendFile(t, e
|
|
312
|
+
async appendFile(t, e) {
|
|
282
313
|
await this.mount();
|
|
283
|
-
const a =
|
|
284
|
-
|
|
314
|
+
const a = e instanceof Uint8Array ? e : new Uint8Array(e);
|
|
315
|
+
await d(t, "exclusive", async () => {
|
|
316
|
+
const i = await this.open(t, { create: !0 });
|
|
317
|
+
try {
|
|
318
|
+
const { size: r } = await this.fstat(i);
|
|
319
|
+
await this.write(i, a, 0, a.length, r, !1), await this.fsync(i);
|
|
320
|
+
} finally {
|
|
321
|
+
await this.close(i);
|
|
322
|
+
}
|
|
323
|
+
await this.notifyChange({ path: t, type: "changed", isDirectory: !1 });
|
|
324
|
+
});
|
|
285
325
|
}
|
|
286
326
|
/**
|
|
287
327
|
* Create a directory
|
|
@@ -306,15 +346,15 @@ class M {
|
|
|
306
346
|
*/
|
|
307
347
|
async mkdir(t, e) {
|
|
308
348
|
await this.mount();
|
|
309
|
-
const
|
|
349
|
+
const a = e?.recursive ?? !1, i = F(t);
|
|
310
350
|
let r = this.root;
|
|
311
|
-
for (let s = 0; s <
|
|
312
|
-
const n =
|
|
351
|
+
for (let s = 0; s < i.length; s++) {
|
|
352
|
+
const n = i[s];
|
|
313
353
|
try {
|
|
314
|
-
r = await r.getDirectoryHandle(n, { create:
|
|
354
|
+
r = await r.getDirectoryHandle(n, { create: a || s === i.length - 1 });
|
|
315
355
|
} catch (c) {
|
|
316
356
|
throw c.name === "NotFoundError" ? new o(
|
|
317
|
-
`Parent directory does not exist: ${
|
|
357
|
+
`Parent directory does not exist: ${b(i.slice(0, s + 1))}`,
|
|
318
358
|
"ENOENT",
|
|
319
359
|
void 0,
|
|
320
360
|
c
|
|
@@ -355,9 +395,9 @@ class M {
|
|
|
355
395
|
isFile: !1,
|
|
356
396
|
isDirectory: !0
|
|
357
397
|
};
|
|
358
|
-
const e =
|
|
398
|
+
const e = E(t), a = await this.getDirectoryHandle(p(t), !1), i = this.options.hashAlgorithm !== null;
|
|
359
399
|
try {
|
|
360
|
-
const s = await (await
|
|
400
|
+
const s = await (await a.getFileHandle(e, { create: !1 })).getFile(), n = {
|
|
361
401
|
kind: "file",
|
|
362
402
|
size: s.size,
|
|
363
403
|
mtime: new Date(s.lastModified).toISOString(),
|
|
@@ -365,9 +405,9 @@ class M {
|
|
|
365
405
|
isFile: !0,
|
|
366
406
|
isDirectory: !1
|
|
367
407
|
};
|
|
368
|
-
if (
|
|
408
|
+
if (i && this.options.hashAlgorithm)
|
|
369
409
|
try {
|
|
370
|
-
const c = await
|
|
410
|
+
const c = await H(s, this.options.hashAlgorithm, this.options.maxFileSize);
|
|
371
411
|
n.hash = c;
|
|
372
412
|
} catch (c) {
|
|
373
413
|
console.warn(`Failed to calculate hash for ${t}:`, c);
|
|
@@ -378,7 +418,7 @@ class M {
|
|
|
378
418
|
throw new o("Failed to stat (file)", "STAT_FAILED", void 0, r);
|
|
379
419
|
}
|
|
380
420
|
try {
|
|
381
|
-
return await
|
|
421
|
+
return await a.getDirectoryHandle(e, { create: !1 }), {
|
|
382
422
|
kind: "directory",
|
|
383
423
|
size: 0,
|
|
384
424
|
mtime: (/* @__PURE__ */ new Date(0)).toISOString(),
|
|
@@ -410,17 +450,17 @@ class M {
|
|
|
410
450
|
*/
|
|
411
451
|
async readDir(t) {
|
|
412
452
|
await this.mount();
|
|
413
|
-
const e = await this.getDirectoryHandle(t, !1),
|
|
414
|
-
for await (const [
|
|
453
|
+
const e = await this.getDirectoryHandle(t, !1), a = [];
|
|
454
|
+
for await (const [i, r] of e.entries()) {
|
|
415
455
|
const s = r.kind === "file";
|
|
416
|
-
|
|
417
|
-
name:
|
|
456
|
+
a.push({
|
|
457
|
+
name: i,
|
|
418
458
|
kind: r.kind,
|
|
419
459
|
isFile: s,
|
|
420
460
|
isDirectory: !s
|
|
421
461
|
});
|
|
422
462
|
}
|
|
423
|
-
return
|
|
463
|
+
return a;
|
|
424
464
|
}
|
|
425
465
|
/**
|
|
426
466
|
* Check if a file or directory exists
|
|
@@ -439,23 +479,23 @@ class M {
|
|
|
439
479
|
async exists(t) {
|
|
440
480
|
if (await this.mount(), t === "/")
|
|
441
481
|
return !0;
|
|
442
|
-
const e =
|
|
443
|
-
let
|
|
482
|
+
const e = E(t);
|
|
483
|
+
let a = null;
|
|
444
484
|
try {
|
|
445
|
-
|
|
446
|
-
} catch (
|
|
447
|
-
if (
|
|
448
|
-
throw
|
|
485
|
+
a = await this.getDirectoryHandle(p(t), !1);
|
|
486
|
+
} catch (i) {
|
|
487
|
+
if (a = null, i.name !== "NotFoundError" && i.name !== "TypeMismatchError")
|
|
488
|
+
throw i;
|
|
449
489
|
}
|
|
450
|
-
if (!
|
|
490
|
+
if (!a || !e)
|
|
451
491
|
return !1;
|
|
452
492
|
try {
|
|
453
|
-
return await
|
|
454
|
-
} catch (
|
|
455
|
-
if (
|
|
456
|
-
throw
|
|
493
|
+
return await a.getFileHandle(e, { create: !1 }), !0;
|
|
494
|
+
} catch (i) {
|
|
495
|
+
if (i.name !== "NotFoundError" && i.name !== "TypeMismatchError")
|
|
496
|
+
throw i;
|
|
457
497
|
try {
|
|
458
|
-
return await
|
|
498
|
+
return await a.getDirectoryHandle(e, { create: !1 }), !0;
|
|
459
499
|
} catch (r) {
|
|
460
500
|
if (r.name !== "NotFoundError" && r.name !== "TypeMismatchError")
|
|
461
501
|
throw r;
|
|
@@ -486,9 +526,9 @@ class M {
|
|
|
486
526
|
await this.mount();
|
|
487
527
|
try {
|
|
488
528
|
const e = await this.readDir(t);
|
|
489
|
-
for (const
|
|
490
|
-
const
|
|
491
|
-
await this.remove(
|
|
529
|
+
for (const a of e) {
|
|
530
|
+
const i = `${t === "/" ? "" : t}/${a.name}`;
|
|
531
|
+
await this.remove(i, { recursive: !0 });
|
|
492
532
|
}
|
|
493
533
|
await this.notifyChange({ path: t, type: "changed", isDirectory: !0 });
|
|
494
534
|
} catch (e) {
|
|
@@ -522,8 +562,8 @@ class M {
|
|
|
522
562
|
async remove(t, e) {
|
|
523
563
|
if (await this.mount(), t === "/")
|
|
524
564
|
throw new o("Cannot remove root directory", "EROOT");
|
|
525
|
-
const { recursive:
|
|
526
|
-
await
|
|
565
|
+
const { recursive: a = !1, force: i = !1 } = e || {}, r = await this.getDirectoryHandle(p(t), !1), s = await this.stat(t);
|
|
566
|
+
await $(r, t, { recursive: a, force: i }), await this.notifyChange({ path: t, type: "removed", isDirectory: s.isDirectory });
|
|
527
567
|
}
|
|
528
568
|
/**
|
|
529
569
|
* Resolve a path to an absolute path
|
|
@@ -546,9 +586,9 @@ class M {
|
|
|
546
586
|
async realpath(t) {
|
|
547
587
|
await this.mount();
|
|
548
588
|
try {
|
|
549
|
-
const e =
|
|
589
|
+
const e = v(t);
|
|
550
590
|
if (!await this.exists(e))
|
|
551
|
-
throw new
|
|
591
|
+
throw new D(e);
|
|
552
592
|
return e;
|
|
553
593
|
} catch (e) {
|
|
554
594
|
throw e instanceof o ? e : new o(`Failed to resolve path: ${t}`, "REALPATH_FAILED", void 0, e);
|
|
@@ -576,17 +616,15 @@ class M {
|
|
|
576
616
|
* await fs.rename('/old/path/file.txt', '/new/path/renamed.txt', { overwrite: true });
|
|
577
617
|
* ```
|
|
578
618
|
*/
|
|
579
|
-
async rename(t, e,
|
|
619
|
+
async rename(t, e, a) {
|
|
580
620
|
await this.mount();
|
|
581
621
|
try {
|
|
582
|
-
const
|
|
583
|
-
if (
|
|
584
|
-
throw new F(t);
|
|
585
|
-
if (await this.exists(e) && !a)
|
|
622
|
+
const i = a?.overwrite ?? !1, r = await this.stat(t);
|
|
623
|
+
if (await this.exists(e) && !i)
|
|
586
624
|
throw new o(`Destination already exists: ${e}`, "EEXIST", void 0);
|
|
587
|
-
await this.copy(t, e, { recursive: !0, overwrite:
|
|
588
|
-
} catch (
|
|
589
|
-
throw
|
|
625
|
+
await this.copy(t, e, { recursive: !0, overwrite: i }), await this.remove(t, { recursive: !0 }), await this.notifyChange({ path: t, type: "removed", isDirectory: r.isDirectory }), await this.notifyChange({ path: e, type: "added", isDirectory: r.isDirectory });
|
|
626
|
+
} catch (i) {
|
|
627
|
+
throw i instanceof o ? i : new o(`Failed to rename from ${t} to ${e}`, "RENAME_FAILED", void 0, i);
|
|
590
628
|
}
|
|
591
629
|
}
|
|
592
630
|
/**
|
|
@@ -614,29 +652,29 @@ class M {
|
|
|
614
652
|
* await fs.copy('/source', '/dest', { recursive: true, overwrite: false });
|
|
615
653
|
* ```
|
|
616
654
|
*/
|
|
617
|
-
async copy(t, e,
|
|
655
|
+
async copy(t, e, a) {
|
|
618
656
|
await this.mount();
|
|
619
657
|
try {
|
|
620
|
-
const
|
|
658
|
+
const i = a?.recursive ?? !1, r = a?.overwrite ?? !0;
|
|
621
659
|
if (!await this.exists(t))
|
|
622
660
|
throw new o(`Source does not exist: ${t}`, "ENOENT", void 0);
|
|
623
661
|
if (await this.exists(e) && !r)
|
|
624
662
|
throw new o(`Destination already exists: ${e}`, "EEXIST", void 0);
|
|
625
663
|
if ((await this.stat(t)).isFile) {
|
|
626
|
-
const l = await this.readFile(t
|
|
664
|
+
const l = await this.readFile(t);
|
|
627
665
|
await this.writeFile(e, l);
|
|
628
666
|
} else {
|
|
629
|
-
if (!
|
|
667
|
+
if (!i)
|
|
630
668
|
throw new o(`Cannot copy directory without recursive option: ${t}`, "EISDIR", void 0);
|
|
631
669
|
await this.mkdir(e, { recursive: !0 });
|
|
632
670
|
const l = await this.readDir(t);
|
|
633
671
|
for (const h of l) {
|
|
634
|
-
const
|
|
635
|
-
await this.copy(
|
|
672
|
+
const w = `${t}/${h.name}`, y = `${e}/${h.name}`;
|
|
673
|
+
await this.copy(w, y, { recursive: !0, overwrite: r });
|
|
636
674
|
}
|
|
637
675
|
}
|
|
638
|
-
} catch (
|
|
639
|
-
throw
|
|
676
|
+
} catch (i) {
|
|
677
|
+
throw i instanceof o ? i : new o(`Failed to copy from ${t} to ${e}`, "CP_FAILED", void 0, i);
|
|
640
678
|
}
|
|
641
679
|
}
|
|
642
680
|
/**
|
|
@@ -667,12 +705,12 @@ class M {
|
|
|
667
705
|
async watch(t, e) {
|
|
668
706
|
if (!this.options.broadcastChannel)
|
|
669
707
|
throw new o("This instance is not configured to send events. Please specify options.broadcastChannel to enable watching.", "ENOENT");
|
|
670
|
-
const
|
|
671
|
-
pattern:
|
|
708
|
+
const a = {
|
|
709
|
+
pattern: T(t, e?.recursive ?? !0),
|
|
672
710
|
include: Array.isArray(e?.include) ? e.include : [e?.include ?? "**"],
|
|
673
711
|
exclude: Array.isArray(e?.exclude) ? e.exclude : [e?.exclude ?? ""]
|
|
674
712
|
};
|
|
675
|
-
this.watchers.set(t,
|
|
713
|
+
this.watchers.set(t, a);
|
|
676
714
|
}
|
|
677
715
|
/**
|
|
678
716
|
* Stop watching a previously watched path
|
|
@@ -709,13 +747,13 @@ class M {
|
|
|
709
747
|
*/
|
|
710
748
|
async open(t, e) {
|
|
711
749
|
await this.mount();
|
|
712
|
-
const { create:
|
|
750
|
+
const { create: a = !1, exclusive: i = !1, truncate: r = !1 } = e || {}, s = g(v(t));
|
|
713
751
|
try {
|
|
714
|
-
return
|
|
752
|
+
return a && i ? await d(s, "exclusive", async () => {
|
|
715
753
|
if (await this.exists(s))
|
|
716
754
|
throw new o(`File already exists: ${s}`, "EEXIST", s);
|
|
717
|
-
return this._openFile(s,
|
|
718
|
-
}) : await this._openFile(s,
|
|
755
|
+
return this._openFile(s, a, r);
|
|
756
|
+
}) : await this._openFile(s, a, r);
|
|
719
757
|
} catch (n) {
|
|
720
758
|
throw n instanceof o ? n : new o(`Failed to open file: ${s}`, "OPEN_FAILED", s, n);
|
|
721
759
|
}
|
|
@@ -724,19 +762,19 @@ class M {
|
|
|
724
762
|
* Internal method to open a file (without locking)
|
|
725
763
|
* @private
|
|
726
764
|
*/
|
|
727
|
-
async _openFile(t, e,
|
|
728
|
-
const
|
|
765
|
+
async _openFile(t, e, a) {
|
|
766
|
+
const i = await this.getFileHandle(t, e);
|
|
729
767
|
try {
|
|
730
|
-
await
|
|
768
|
+
await i.getFile();
|
|
731
769
|
} catch (n) {
|
|
732
770
|
throw n.name === "TypeMismatchError" ? new o(`Is a directory: ${t}`, "EISDIR", t) : n;
|
|
733
771
|
}
|
|
734
|
-
const r = await
|
|
735
|
-
|
|
772
|
+
const r = await O(i, t);
|
|
773
|
+
a && (r.truncate(0), r.flush());
|
|
736
774
|
const s = this.nextFd++;
|
|
737
775
|
return this.openFiles.set(s, {
|
|
738
776
|
path: t,
|
|
739
|
-
fileHandle:
|
|
777
|
+
fileHandle: i,
|
|
740
778
|
syncHandle: r,
|
|
741
779
|
position: 0
|
|
742
780
|
}), s;
|
|
@@ -757,7 +795,7 @@ class M {
|
|
|
757
795
|
*/
|
|
758
796
|
async close(t) {
|
|
759
797
|
const e = this._getFileDescriptor(t);
|
|
760
|
-
|
|
798
|
+
S(t, e.syncHandle, e.path), this.openFiles.delete(t);
|
|
761
799
|
}
|
|
762
800
|
/**
|
|
763
801
|
* Read data from a file descriptor
|
|
@@ -783,17 +821,17 @@ class M {
|
|
|
783
821
|
* await fs.close(fd);
|
|
784
822
|
* ```
|
|
785
823
|
*/
|
|
786
|
-
async read(t, e,
|
|
824
|
+
async read(t, e, a, i, r) {
|
|
787
825
|
const s = this._getFileDescriptor(t);
|
|
788
|
-
I(e.length,
|
|
826
|
+
I(e.length, a, i, r);
|
|
789
827
|
try {
|
|
790
|
-
const n = r ?? s.position, c = s.syncHandle.getSize(), { isEOF: l, actualLength: h } =
|
|
828
|
+
const n = r ?? s.position, c = s.syncHandle.getSize(), { isEOF: l, actualLength: h } = N(n, i, c);
|
|
791
829
|
if (l)
|
|
792
|
-
return
|
|
793
|
-
const
|
|
794
|
-
return r
|
|
830
|
+
return u({ bytesRead: 0, buffer: e }, [e.buffer]);
|
|
831
|
+
const w = e.subarray(a, a + h), y = s.syncHandle.read(w, { at: n });
|
|
832
|
+
return r == null && (s.position = n + y), u({ bytesRead: y, buffer: e }, [e.buffer]);
|
|
795
833
|
} catch (n) {
|
|
796
|
-
throw
|
|
834
|
+
throw f("read", t, s.path, n);
|
|
797
835
|
}
|
|
798
836
|
}
|
|
799
837
|
/**
|
|
@@ -804,9 +842,10 @@ class M {
|
|
|
804
842
|
* @param offset - The offset in the buffer to start reading from (default: 0)
|
|
805
843
|
* @param length - The number of bytes to write (default: entire buffer)
|
|
806
844
|
* @param position - The position in the file to write to (null/undefined for current position)
|
|
845
|
+
* @param emitEvent - Whether to emit a change event (default: true)
|
|
807
846
|
* @returns Promise that resolves to the number of bytes written
|
|
808
847
|
* @throws {OPFSError} If the file descriptor is invalid or writing fails
|
|
809
|
-
*
|
|
848
|
+
*
|
|
810
849
|
* @example
|
|
811
850
|
* ```typescript
|
|
812
851
|
* const fd = await fs.open('/data/file.txt', { create: true });
|
|
@@ -816,14 +855,14 @@ class M {
|
|
|
816
855
|
* await fs.close(fd);
|
|
817
856
|
* ```
|
|
818
857
|
*/
|
|
819
|
-
async write(t, e,
|
|
820
|
-
const
|
|
821
|
-
I(e.length,
|
|
858
|
+
async write(t, e, a = 0, i, r, s = !0) {
|
|
859
|
+
const n = this._getFileDescriptor(t), c = i ?? e.length - a;
|
|
860
|
+
I(e.length, a, c, r);
|
|
822
861
|
try {
|
|
823
|
-
const
|
|
824
|
-
return (r == null || r ===
|
|
825
|
-
} catch (
|
|
826
|
-
throw
|
|
862
|
+
const l = r ?? n.position, h = e.subarray(a, a + c), w = n.syncHandle.write(h, { at: l });
|
|
863
|
+
return (r == null || r === n.position) && (n.position = l + w), s && await this.notifyChange({ path: n.path, type: "changed", isDirectory: !1 }), w;
|
|
864
|
+
} catch (l) {
|
|
865
|
+
throw f("write", t, n.path, l);
|
|
827
866
|
}
|
|
828
867
|
}
|
|
829
868
|
/**
|
|
@@ -865,13 +904,13 @@ class M {
|
|
|
865
904
|
* ```
|
|
866
905
|
*/
|
|
867
906
|
async ftruncate(t, e = 0) {
|
|
868
|
-
const
|
|
907
|
+
const a = this._getFileDescriptor(t);
|
|
869
908
|
if (e < 0 || !Number.isInteger(e))
|
|
870
909
|
throw new o("Invalid size", "EINVAL");
|
|
871
910
|
try {
|
|
872
|
-
|
|
873
|
-
} catch (
|
|
874
|
-
throw
|
|
911
|
+
a.syncHandle.truncate(e), a.syncHandle.flush(), a.position > e && (a.position = e), await this.notifyChange({ path: a.path, type: "changed", isDirectory: !1 });
|
|
912
|
+
} catch (i) {
|
|
913
|
+
throw f("truncate", t, a.path, i);
|
|
875
914
|
}
|
|
876
915
|
}
|
|
877
916
|
/**
|
|
@@ -892,8 +931,8 @@ class M {
|
|
|
892
931
|
const e = this._getFileDescriptor(t);
|
|
893
932
|
try {
|
|
894
933
|
e.syncHandle.flush();
|
|
895
|
-
} catch (
|
|
896
|
-
throw
|
|
934
|
+
} catch (a) {
|
|
935
|
+
throw f("sync", t, e.path, a);
|
|
897
936
|
}
|
|
898
937
|
}
|
|
899
938
|
/**
|
|
@@ -905,7 +944,7 @@ class M {
|
|
|
905
944
|
dispose() {
|
|
906
945
|
this.broadcastChannel && (this.broadcastChannel.close(), this.broadcastChannel = null), this.watchers.clear();
|
|
907
946
|
for (const [t, e] of this.openFiles)
|
|
908
|
-
|
|
947
|
+
S(t, e.syncHandle, e.path);
|
|
909
948
|
this.openFiles.clear(), this.nextFd = 1;
|
|
910
949
|
}
|
|
911
950
|
/**
|
|
@@ -940,18 +979,18 @@ class M {
|
|
|
940
979
|
await this.mount();
|
|
941
980
|
try {
|
|
942
981
|
(e?.cleanBefore ?? !1) && await this.clear("/");
|
|
943
|
-
for (const [
|
|
944
|
-
const s =
|
|
982
|
+
for (const [i, r] of t) {
|
|
983
|
+
const s = g(i);
|
|
945
984
|
let n;
|
|
946
|
-
r instanceof Blob ? n = await
|
|
985
|
+
r instanceof Blob ? n = await P(r) : typeof r == "string" ? n = new TextEncoder().encode(r) : n = r, await this.writeFile(s, n);
|
|
947
986
|
}
|
|
948
|
-
} catch (
|
|
949
|
-
throw
|
|
987
|
+
} catch (a) {
|
|
988
|
+
throw a instanceof o ? a : new o("Failed to sync file system", "SYNC_FAILED", void 0, a);
|
|
950
989
|
}
|
|
951
990
|
}
|
|
952
991
|
}
|
|
953
|
-
typeof globalThis < "u" && globalThis.constructor.name === "DedicatedWorkerGlobalScope" &&
|
|
992
|
+
typeof globalThis < "u" && globalThis.constructor.name === "DedicatedWorkerGlobalScope" && x(new _());
|
|
954
993
|
export {
|
|
955
|
-
|
|
994
|
+
_ as OPFSWorker
|
|
956
995
|
};
|
|
957
996
|
//# sourceMappingURL=raw.js.map
|