opfs-worker 1.1.0 → 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/raw.js CHANGED
@@ -1,6 +1,6 @@
1
- import { expose as H, transfer as D } from "comlink";
2
- import { O as o, i as y, e as C, n as u, s as m, P as b, E as g, p as A, H as $, F, q as E, j as O, f as v, g as p, t as T, x as N, r as S, h as P, w as _, B as k, z as x, y as I, A as L, d, v as z } from "./helpers-DS5dyURe.js";
3
- class M {
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: null,
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) => y(e, r.pattern) && r.include.some((s) => s && y(e, s)) && !r.exclude.some((s) => s && y(e, s))))
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 a;
49
+ let i;
50
50
  if (this.options.hashAlgorithm)
51
51
  try {
52
- a = (await this.stat(e)).hash;
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
- ...a && { hash: a }
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
- C(), t && this.setOptions(t);
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, i) => {
104
+ return this.mountingPromise && await this.mountingPromise, this.mountingPromise = new Promise(async (e, a) => {
105
105
  try {
106
- const a = await navigator.storage.getDirectory();
107
- this.root = t === "/" ? a : await this.getDirectoryHandle(t, !0, a), e(!0);
108
- } catch (a) {
109
- i(new o("Failed to initialize OPFS", "INIT_FAILED", t, a));
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 = u(t.root), this.options.namespace || (this.options.namespace = `opfs-worker:${this.options.root}`), await this.mount());
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, i = this.root) {
147
- const a = Array.isArray(t) ? t : m(t);
148
- let r = i;
149
- for (const s of a)
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, i = this.root) {
173
- const a = m(t);
174
- if (a.length === 0)
175
- throw new b("Path must not be empty", Array.isArray(t) ? t.join("/") : t);
176
- const r = a.pop();
177
- return (await this.getDirectoryHandle(a, e, i)).getFileHandle(r, { create: e });
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 (i) => {
200
- const a = await this.readDir(i);
201
- for (const r of a) {
202
- const s = `${i === "/" ? "" : i}/${r.name}`;
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
- async readFile(t, e) {
221
- await this.mount(), e || (e = g(t) ? "binary" : "utf-8");
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
- const i = await this.getFileHandle(t, !1, this.root), a = await A(i, t);
224
- return e === "binary" ? a : $(a, e);
225
- } catch (i) {
226
- throw new F(t, i);
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 (string, Uint8Array, or ArrayBuffer)
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 with specific encoding
251
- * await fs.writeFile('/data/utf16.txt', 'Hello World', 'utf-16le');
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, i) {
277
+ async writeFile(t, e) {
255
278
  await this.mount();
256
- const a = await this.exists(t), r = await this.getFileHandle(t, !0);
257
- i || (i = typeof e != "string" || g(t) ? "binary" : "utf-8"), await E(r, e, i, t), a ? await this.notifyChange({ path: t, type: "changed", isDirectory: !1 }) : await this.notifyChange({ path: t, type: "added", isDirectory: !1 });
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 (string, Uint8Array, or ArrayBuffer)
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, i) {
312
+ async appendFile(t, e) {
282
313
  await this.mount();
283
- const a = await this.getFileHandle(t, !0);
284
- i || (i = typeof e != "string" || g(t) ? "binary" : "utf-8"), await E(a, e, i, t, { append: !0 }), await this.notifyChange({ path: t, type: "changed", isDirectory: !1 });
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 i = e?.recursive ?? !1, a = m(t);
349
+ const a = e?.recursive ?? !1, i = F(t);
310
350
  let r = this.root;
311
- for (let s = 0; s < a.length; s++) {
312
- const n = a[s];
351
+ for (let s = 0; s < i.length; s++) {
352
+ const n = i[s];
313
353
  try {
314
- r = await r.getDirectoryHandle(n, { create: i || s === a.length - 1 });
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: ${O(a.slice(0, s + 1))}`,
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 = v(t), i = await this.getDirectoryHandle(p(t), !1), a = this.options.hashAlgorithm !== null;
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 i.getFileHandle(e, { create: !1 })).getFile(), n = {
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 (a && this.options.hashAlgorithm)
408
+ if (i && this.options.hashAlgorithm)
369
409
  try {
370
- const c = await T(s, this.options.hashAlgorithm, this.options.maxFileSize);
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 i.getDirectoryHandle(e, { create: !1 }), {
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), i = [];
414
- for await (const [a, r] of e.entries()) {
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
- i.push({
417
- name: a,
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 i;
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 = v(t);
443
- let i = null;
482
+ const e = E(t);
483
+ let a = null;
444
484
  try {
445
- i = await this.getDirectoryHandle(p(t), !1);
446
- } catch (a) {
447
- if (i = null, a.name !== "NotFoundError" && a.name !== "TypeMismatchError")
448
- throw a;
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 (!i || !e)
490
+ if (!a || !e)
451
491
  return !1;
452
492
  try {
453
- return await i.getFileHandle(e, { create: !1 }), !0;
454
- } catch (a) {
455
- if (a.name !== "NotFoundError" && a.name !== "TypeMismatchError")
456
- throw a;
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 i.getDirectoryHandle(e, { create: !1 }), !0;
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 i of e) {
490
- const a = `${t === "/" ? "" : t}/${i.name}`;
491
- await this.remove(a, { recursive: !0 });
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: i = !1, force: a = !1 } = e || {}, r = await this.getDirectoryHandle(p(t), !1);
526
- await N(r, t, { recursive: i, force: a }), await this.notifyChange({ path: t, type: "removed", isDirectory: !1 });
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 = S(t);
589
+ const e = v(t);
550
590
  if (!await this.exists(e))
551
- throw new F(e);
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, i) {
619
+ async rename(t, e, a) {
580
620
  await this.mount();
581
621
  try {
582
- const a = i?.overwrite ?? !1;
583
- if (!await this.exists(t))
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: a }), await this.remove(t, { recursive: !0 }), await this.notifyChange({ path: t, type: "removed", isDirectory: !1 }), await this.notifyChange({ path: e, type: "added", isDirectory: !1 });
588
- } catch (a) {
589
- throw a instanceof o ? a : new o(`Failed to rename from ${t} to ${e}`, "RENAME_FAILED", void 0, a);
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, i) {
655
+ async copy(t, e, a) {
618
656
  await this.mount();
619
657
  try {
620
- const a = i?.recursive ?? !1, r = i?.overwrite ?? !0;
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, "binary");
664
+ const l = await this.readFile(t);
627
665
  await this.writeFile(e, l);
628
666
  } else {
629
- if (!a)
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 f = `${t}/${h.name}`, w = `${e}/${h.name}`;
635
- await this.copy(f, w, { recursive: !0, overwrite: r });
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 (a) {
639
- throw a instanceof o ? a : new o(`Failed to copy from ${t} to ${e}`, "CP_FAILED", void 0, a);
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 i = {
671
- pattern: P(t, e?.recursive ?? !0),
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, i);
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: i = !1, exclusive: a = !1, truncate: r = !1 } = e || {}, s = u(S(t));
750
+ const { create: a = !1, exclusive: i = !1, truncate: r = !1 } = e || {}, s = g(v(t));
713
751
  try {
714
- return i && a ? await _(s, "exclusive", async () => {
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, i, r);
718
- }) : await this._openFile(s, i, r);
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, i) {
728
- const a = await this.getFileHandle(t, e);
765
+ async _openFile(t, e, a) {
766
+ const i = await this.getFileHandle(t, e);
729
767
  try {
730
- await a.getFile();
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 k(a, t);
735
- i && (r.truncate(0), r.flush());
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: a,
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
- x(t, e.syncHandle, e.path), this.openFiles.delete(t);
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, i, a, r) {
824
+ async read(t, e, a, i, r) {
787
825
  const s = this._getFileDescriptor(t);
788
- I(e.length, i, a, r);
826
+ I(e.length, a, i, r);
789
827
  try {
790
- const n = r ?? s.position, c = s.syncHandle.getSize(), { isEOF: l, actualLength: h } = L(n, a, c);
828
+ const n = r ?? s.position, c = s.syncHandle.getSize(), { isEOF: l, actualLength: h } = N(n, i, c);
791
829
  if (l)
792
- return D({ bytesRead: 0, buffer: e }, [e.buffer]);
793
- const f = e.subarray(i, i + h), w = s.syncHandle.read(f, { at: n });
794
- return r === null && (s.position = n + w), D({ bytesRead: w, buffer: e }, [e.buffer]);
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 d("read", t, s.path, n);
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, i = 0, a, r) {
820
- const s = this._getFileDescriptor(t), n = a ?? e.length - i;
821
- I(e.length, i, n, r);
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 c = r ?? s.position, l = e.subarray(i, i + n), h = s.syncHandle.write(l, { at: c });
824
- return (r == null || r === s.position) && (s.position = c + h), await this.notifyChange({ path: s.path, type: "changed", isDirectory: !1 }), h;
825
- } catch (c) {
826
- throw d("write", t, s.path, c);
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 i = this._getFileDescriptor(t);
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
- i.syncHandle.truncate(e), i.syncHandle.flush(), i.position > e && (i.position = e), await this.notifyChange({ path: i.path, type: "changed", isDirectory: !1 });
873
- } catch (a) {
874
- throw d("truncate", t, i.path, a);
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 (i) {
896
- throw d("sync", t, e.path, i);
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
- x(t, e.syncHandle, e.path);
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 [a, r] of t) {
944
- const s = u(a);
982
+ for (const [i, r] of t) {
983
+ const s = g(i);
945
984
  let n;
946
- r instanceof Blob ? n = await z(r) : n = r, await this.writeFile(s, n);
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 (i) {
949
- throw i instanceof o ? i : new o("Failed to sync file system", "SYNC_FAILED", void 0, i);
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" && H(new M());
992
+ typeof globalThis < "u" && globalThis.constructor.name === "DedicatedWorkerGlobalScope" && x(new _());
954
993
  export {
955
- M as OPFSWorker
994
+ _ as OPFSWorker
956
995
  };
957
996
  //# sourceMappingURL=raw.js.map