opfs-worker 1.2.4 → 1.3.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 A, transfer as m } from "comlink";
2
- import { O as o, o as g, g as C, n as F, s as p, P as b, w as f, F as E, j as H, W as l, h as v, k as D, u as $, y as T, r as S, l as O, C as N, A as I, z as x, B as P, f as u, x as _ } from "./helpers-BwklIUit.js";
3
- class k {
1
+ import { expose as k, transfer as p } from "comlink";
2
+ import { V as E, u as D, n as z, h as P, r as F, s as x, P as M, w as m, E as d, o as $, c as b, j as S, W as h, p as H, q as v, B as T, O as f, m as l, D as _, H as N, x as A, A as C, g as B, t as R, M as U, K as I, J as O, L as W, l as g, G as L } from "./helpers-BuGfPAg0.js";
3
+ class j {
4
4
  /** Root directory handle for the file system */
5
5
  root;
6
6
  /** Map of watched paths and options */
@@ -28,7 +28,7 @@ class k {
28
28
  _getFileDescriptor(t) {
29
29
  const e = this.openFiles.get(t);
30
30
  if (!e)
31
- throw new o(`Invalid file descriptor: ${t}`, "EBADF");
31
+ throw new E("descriptor", `Invalid file descriptor: ${t}`);
32
32
  return e;
33
33
  }
34
34
  /**
@@ -44,7 +44,7 @@ class k {
44
44
  if (!this.options.broadcastChannel)
45
45
  return;
46
46
  const e = t.path;
47
- if (![...this.watchers.values()].some((r) => g(e, r.pattern) && r.include.some((s) => s && g(e, s)) && !r.exclude.some((s) => s && g(e, s))))
47
+ if (![...this.watchers.values()].some((r) => D(e, r.pattern) && r.include.some((s) => s && D(e, s)) && !r.exclude.some((s) => s && D(e, s))))
48
48
  return;
49
49
  let i;
50
50
  if (this.options.hashAlgorithm)
@@ -76,7 +76,7 @@ class k {
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
+ z(), t && this.setOptions(t);
80
80
  }
81
81
  /**
82
82
  * Initialize the file system within a given directory
@@ -106,7 +106,7 @@ class k {
106
106
  const i = await navigator.storage.getDirectory();
107
107
  this.root = t === "/" ? i : await this.getDirectoryHandle(t, !0, i), e(!0);
108
108
  } catch (i) {
109
- a(new o("Failed to initialize OPFS", "INIT_FAILED", t, i));
109
+ a(new P(t, i));
110
110
  } finally {
111
111
  this.mountingPromise = null;
112
112
  }
@@ -144,7 +144,7 @@ class k {
144
144
  * ```
145
145
  */
146
146
  async getDirectoryHandle(t, e = !1, a = this.root) {
147
- const i = Array.isArray(t) ? t : p(t);
147
+ const i = Array.isArray(t) ? t : x(t);
148
148
  let r = a;
149
149
  for (const s of i)
150
150
  r = await r.getDirectoryHandle(s, { create: e });
@@ -170,9 +170,9 @@ class k {
170
170
  * ```
171
171
  */
172
172
  async getFileHandle(t, e = !1, a = this.root) {
173
- const i = p(t);
173
+ const i = x(t);
174
174
  if (i.length === 0)
175
- throw new b("Path must not be empty", Array.isArray(t) ? t.join("/") : t);
175
+ throw new M("Path must not be empty", Array.isArray(t) ? t.join("/") : t);
176
176
  const r = i.pop();
177
177
  return (await this.getDirectoryHandle(i, e, a)).getFileHandle(r, { create: e });
178
178
  }
@@ -239,17 +239,17 @@ class k {
239
239
  async readFile(t) {
240
240
  await this.mount();
241
241
  try {
242
- return await f(t, "shared", async () => {
242
+ return await m(t, "shared", async () => {
243
243
  const e = await this.open(t);
244
244
  try {
245
245
  const { size: a } = await this.fstat(e), i = new Uint8Array(a);
246
- return a > 0 && await this.read(e, i, 0, a, 0), m(i, [i.buffer]);
246
+ return a > 0 && await this.read(e, i, 0, a, 0), p(i, [i.buffer]);
247
247
  } finally {
248
248
  await this.close(e);
249
249
  }
250
250
  });
251
251
  } catch (e) {
252
- throw new E(t, e);
252
+ throw new d("file", t, e);
253
253
  }
254
254
  }
255
255
  /**
@@ -277,14 +277,14 @@ class k {
277
277
  async writeFile(t, e) {
278
278
  await this.mount();
279
279
  const a = e instanceof Uint8Array ? e : new Uint8Array(e);
280
- await f(t, "exclusive", async () => {
280
+ await m(t, "exclusive", async () => {
281
281
  const i = await this.exists(t), r = await this.open(t, { create: !0, truncate: !0 });
282
282
  try {
283
283
  await this.write(r, a, 0, a.length, null, !1), await this.fsync(r);
284
284
  } finally {
285
285
  await this.close(r);
286
286
  }
287
- await this.notifyChange({ path: t, type: i ? l.Changed : l.Added, isDirectory: !1 });
287
+ await this.notifyChange({ path: t, type: i ? h.Changed : h.Added, isDirectory: !1 });
288
288
  });
289
289
  }
290
290
  /**
@@ -312,7 +312,7 @@ class k {
312
312
  async appendFile(t, e) {
313
313
  await this.mount();
314
314
  const a = e instanceof Uint8Array ? e : new Uint8Array(e);
315
- await f(t, "exclusive", async () => {
315
+ await m(t, "exclusive", async () => {
316
316
  const i = await this.open(t, { create: !0 });
317
317
  try {
318
318
  const { size: r } = await this.fstat(i);
@@ -320,7 +320,7 @@ class k {
320
320
  } finally {
321
321
  await this.close(i);
322
322
  }
323
- await this.notifyChange({ path: t, type: l.Changed, isDirectory: !1 });
323
+ await this.notifyChange({ path: t, type: h.Changed, isDirectory: !1 });
324
324
  });
325
325
  }
326
326
  /**
@@ -346,22 +346,17 @@ class k {
346
346
  */
347
347
  async mkdir(t, e) {
348
348
  await this.mount();
349
- const a = e?.recursive ?? !1, i = p(t);
349
+ const a = e?.recursive ?? !1, i = x(t);
350
350
  let r = this.root;
351
351
  for (let s = 0; s < i.length; s++) {
352
352
  const n = i[s];
353
353
  try {
354
354
  r = await r.getDirectoryHandle(n, { create: a || s === i.length - 1 });
355
- } catch (c) {
356
- throw c.name === "NotFoundError" ? new o(
357
- `Parent directory does not exist: ${H(i.slice(0, s + 1))}`,
358
- "ENOENT",
359
- void 0,
360
- c
361
- ) : c.name === "TypeMismatchError" ? new o(`Path segment is not a directory: ${n}`, "ENOTDIR", void 0, c) : new o("Failed to create directory", "MKDIR_FAILED", void 0, c);
355
+ } catch (o) {
356
+ throw o.name === "NotFoundError" ? new d("directory", $(i.slice(0, s + 1)), o) : o.name === "TypeMismatchError" ? new b("directory", "file", n, o) : new S("create directory", n, o);
362
357
  }
363
358
  }
364
- await this.notifyChange({ path: t, type: l.Added, isDirectory: !0 });
359
+ await this.notifyChange({ path: t, type: h.Added, isDirectory: !0 });
365
360
  }
366
361
  /**
367
362
  * Get file or directory statistics
@@ -395,9 +390,11 @@ class k {
395
390
  isFile: !1,
396
391
  isDirectory: !0
397
392
  };
398
- const e = v(t), a = await this.getDirectoryHandle(D(t), !1), i = this.options.hashAlgorithm;
393
+ const e = H(t);
394
+ let a;
399
395
  try {
400
- const s = await (await a.getFileHandle(e, { create: !1 })).getFile(), n = {
396
+ a = await this.getDirectoryHandle(v(t), !1);
397
+ const i = this.options.hashAlgorithm, s = await (await a.getFileHandle(e, { create: !1 })).getFile(), n = {
401
398
  kind: "file",
402
399
  size: s.size,
403
400
  mtime: new Date(s.lastModified).toISOString(),
@@ -409,15 +406,17 @@ class k {
409
406
  n.hash = `${s.lastModified.toString(36)}-${s.size.toString(36)}`;
410
407
  else if (typeof i == "string")
411
408
  try {
412
- const c = await $(s, i, this.options.maxFileSize);
413
- n.hash = c;
414
- } catch (c) {
415
- console.warn(`Failed to calculate hash for ${t}:`, c);
409
+ const o = await T(s, i, this.options.maxFileSize);
410
+ n.hash = o;
411
+ } catch (o) {
412
+ console.warn(`Failed to calculate hash for ${t}:`, o);
416
413
  }
417
414
  return n;
418
- } catch (r) {
419
- if (r.name !== "TypeMismatchError" && r.name !== "NotFoundError")
420
- throw new o("Failed to stat (file)", "STAT_FAILED", void 0, r);
415
+ } catch (i) {
416
+ if (i.name === "NotFoundError")
417
+ throw new d("file", t, i);
418
+ if (i.name !== "TypeMismatchError")
419
+ throw new S("stat", t, i);
421
420
  }
422
421
  try {
423
422
  return await a.getDirectoryHandle(e, { create: !1 }), {
@@ -428,8 +427,8 @@ class k {
428
427
  isFile: !1,
429
428
  isDirectory: !0
430
429
  };
431
- } catch (r) {
432
- throw r.name === "NotFoundError" ? new o(`No such file or directory: ${t}`, "ENOENT", void 0, r) : new o("Failed to stat (directory)", "STAT_FAILED", void 0, r);
430
+ } catch (i) {
431
+ throw new S("stat", t, i);
433
432
  }
434
433
  }
435
434
  /**
@@ -481,10 +480,10 @@ class k {
481
480
  async exists(t) {
482
481
  if (await this.mount(), t === "/")
483
482
  return !0;
484
- const e = v(t);
483
+ const e = H(t);
485
484
  let a = null;
486
485
  try {
487
- a = await this.getDirectoryHandle(D(t), !1);
486
+ a = await this.getDirectoryHandle(v(t), !1);
488
487
  } catch (i) {
489
488
  if (a = null, i.name !== "NotFoundError" && i.name !== "TypeMismatchError")
490
489
  throw i;
@@ -532,9 +531,9 @@ class k {
532
531
  const i = `${t === "/" ? "" : t}/${a.name}`;
533
532
  await this.remove(i, { recursive: !0 });
534
533
  }
535
- await this.notifyChange({ path: t, type: l.Changed, isDirectory: !0 });
534
+ await this.notifyChange({ path: t, type: h.Changed, isDirectory: !0 });
536
535
  } catch (e) {
537
- throw e instanceof o ? e : new o(`Failed to clear directory: ${t}`, "CLEAR_FAILED", void 0, e);
536
+ throw e instanceof f ? e : l(e, { path: t, isDirectory: !0 });
538
537
  }
539
538
  }
540
539
  /**
@@ -563,9 +562,9 @@ class k {
563
562
  */
564
563
  async remove(t, e) {
565
564
  if (await this.mount(), t === "/")
566
- throw new o("Cannot remove root directory", "EROOT");
567
- const { recursive: a = !1, force: i = !1 } = e || {}, r = await this.getDirectoryHandle(D(t), !1), s = await this.stat(t);
568
- await T(r, t, { recursive: a, force: i }), await this.notifyChange({ path: t, type: l.Removed, isDirectory: s.isDirectory });
565
+ throw new _("root", t);
566
+ const { recursive: a = !1, force: i = !1 } = e || {}, r = await this.getDirectoryHandle(v(t), !1), s = await this.stat(t);
567
+ await N(r, t, { recursive: a, force: i }), await this.notifyChange({ path: t, type: h.Removed, isDirectory: s.isDirectory });
569
568
  }
570
569
  /**
571
570
  * Resolve a path to an absolute path
@@ -588,12 +587,12 @@ class k {
588
587
  async realpath(t) {
589
588
  await this.mount();
590
589
  try {
591
- const e = S(t);
590
+ const e = A(t);
592
591
  if (!await this.exists(e))
593
- throw new E(e);
592
+ throw new d("file", e);
594
593
  return e;
595
594
  } catch (e) {
596
- throw e instanceof o ? e : new o(`Failed to resolve path: ${t}`, "REALPATH_FAILED", void 0, e);
595
+ throw e instanceof f ? e : l(e, { path: t });
597
596
  }
598
597
  }
599
598
  /**
@@ -623,10 +622,10 @@ class k {
623
622
  try {
624
623
  const i = a?.overwrite ?? !1, r = await this.stat(t);
625
624
  if (await this.exists(e) && !i)
626
- throw new o(`Destination already exists: ${e}`, "EEXIST", void 0);
627
- await this.copy(t, e, { recursive: !0, overwrite: i }), await this.remove(t, { recursive: !0 }), await this.notifyChange({ path: t, type: l.Removed, isDirectory: r.isDirectory }), await this.notifyChange({ path: e, type: l.Added, isDirectory: r.isDirectory });
625
+ throw new C(e);
626
+ await this.copy(t, e, { recursive: !0, overwrite: i }), await this.remove(t, { recursive: !0 }), await this.notifyChange({ path: t, type: h.Removed, isDirectory: r.isDirectory }), await this.notifyChange({ path: e, type: h.Added, isDirectory: r.isDirectory });
628
627
  } catch (i) {
629
- throw i instanceof o ? i : new o(`Failed to rename from ${t} to ${e}`, "RENAME_FAILED", void 0, i);
628
+ throw i instanceof f ? i : l(i, { path: t });
630
629
  }
631
630
  }
632
631
  /**
@@ -659,24 +658,24 @@ class k {
659
658
  try {
660
659
  const i = a?.recursive ?? !1, r = a?.overwrite ?? !0;
661
660
  if (!await this.exists(t))
662
- throw new o(`Source does not exist: ${t}`, "ENOENT", void 0);
661
+ throw new d("source", t);
663
662
  if (await this.exists(e) && !r)
664
- throw new o(`Destination already exists: ${e}`, "EEXIST", void 0);
663
+ throw new C(e);
665
664
  if ((await this.stat(t)).isFile) {
666
- const h = await this.readFile(t);
667
- await this.writeFile(e, h);
665
+ const c = await this.readFile(t);
666
+ await this.writeFile(e, c);
668
667
  } else {
669
668
  if (!i)
670
- throw new o(`Cannot copy directory without recursive option: ${t}`, "EISDIR", void 0);
669
+ throw new b("file", "directory", t);
671
670
  await this.mkdir(e, { recursive: !0 });
672
- const h = await this.readDir(t);
673
- for (const w of h) {
674
- const d = `${t}/${w.name}`, y = `${e}/${w.name}`;
675
- await this.copy(d, y, { recursive: !0, overwrite: r });
671
+ const c = await this.readDir(t);
672
+ for (const w of c) {
673
+ const y = `${t}/${w.name}`, u = `${e}/${w.name}`;
674
+ await this.copy(y, u, { recursive: !0, overwrite: r });
676
675
  }
677
676
  }
678
677
  } catch (i) {
679
- throw i instanceof o ? i : new o(`Failed to copy from ${t} to ${e}`, "CP_FAILED", void 0, i);
678
+ throw i instanceof f ? i : l(i, { path: t });
680
679
  }
681
680
  }
682
681
  /**
@@ -706,9 +705,9 @@ class k {
706
705
  */
707
706
  async watch(t, e) {
708
707
  if (!this.options.broadcastChannel)
709
- throw new o("This instance is not configured to send events. Please specify options.broadcastChannel to enable watching.", "ENOENT");
708
+ throw new B("This instance is not configured to send events. Please specify options.broadcastChannel to enable watching.");
710
709
  const a = {
711
- pattern: O(t, e?.recursive ?? !0),
710
+ pattern: R(t, e?.recursive ?? !0),
712
711
  include: Array.isArray(e?.include) ? e.include : [e?.include ?? "**"],
713
712
  exclude: Array.isArray(e?.exclude) ? e.exclude : [e?.exclude ?? ""]
714
713
  };
@@ -749,15 +748,15 @@ class k {
749
748
  */
750
749
  async open(t, e) {
751
750
  await this.mount();
752
- const { create: a = !1, exclusive: i = !1, truncate: r = !1 } = e || {}, s = F(S(t));
751
+ const { create: a = !1, exclusive: i = !1, truncate: r = !1 } = e || {}, s = F(A(t));
753
752
  try {
754
- return a && i ? await f(s, "exclusive", async () => {
753
+ return a && i ? await m(s, "exclusive", async () => {
755
754
  if (await this.exists(s))
756
- throw new o(`File already exists: ${s}`, "EEXIST", s);
755
+ throw new C(s);
757
756
  return this._openFile(s, a, r);
758
757
  }) : await this._openFile(s, a, r);
759
758
  } catch (n) {
760
- throw n instanceof o ? n : new o(`Failed to open file: ${s}`, "OPEN_FAILED", s, n);
759
+ throw n instanceof f ? n : l(n, { path: s, isDirectory: !1 });
761
760
  }
762
761
  }
763
762
  /**
@@ -769,9 +768,9 @@ class k {
769
768
  try {
770
769
  await i.getFile();
771
770
  } catch (n) {
772
- throw n.name === "TypeMismatchError" ? new o(`Is a directory: ${t}`, "EISDIR", t) : n;
771
+ throw l(n, { path: t, isDirectory: !0 });
773
772
  }
774
- const r = await N(i, t);
773
+ const r = await U(i, t);
775
774
  a && (r.truncate(0), r.flush());
776
775
  const s = this.nextFd++;
777
776
  return this.openFiles.set(s, {
@@ -825,15 +824,15 @@ class k {
825
824
  */
826
825
  async read(t, e, a, i, r) {
827
826
  const s = this._getFileDescriptor(t);
828
- x(e.length, a, i, r);
827
+ O(e.length, a, i, r);
829
828
  try {
830
- const n = r ?? s.position, c = s.syncHandle.getSize(), { isEOF: h, actualLength: w } = P(n, i, c);
831
- if (h)
832
- return m({ bytesRead: 0, buffer: e }, [e.buffer]);
833
- const d = e.subarray(a, a + w), y = s.syncHandle.read(d, { at: n });
834
- return r == null && (s.position = n + y), m({ bytesRead: y, buffer: e }, [e.buffer]);
829
+ const n = r ?? s.position, o = s.syncHandle.getSize(), { isEOF: c, actualLength: w } = W(n, i, o);
830
+ if (c)
831
+ return p({ bytesRead: 0, buffer: e }, [e.buffer]);
832
+ const y = e.subarray(a, a + w), u = s.syncHandle.read(y, { at: n });
833
+ return r == null && (s.position = n + u), p({ bytesRead: u, buffer: e }, [e.buffer]);
835
834
  } catch (n) {
836
- throw u("read", t, s.path, n);
835
+ throw g("read", t, s.path, n);
837
836
  }
838
837
  }
839
838
  /**
@@ -858,13 +857,13 @@ class k {
858
857
  * ```
859
858
  */
860
859
  async write(t, e, a = 0, i, r, s = !0) {
861
- const n = this._getFileDescriptor(t), c = i ?? e.length - a;
862
- x(e.length, a, c, r);
860
+ const n = this._getFileDescriptor(t), o = i ?? e.length - a;
861
+ O(e.length, a, o, r);
863
862
  try {
864
- const h = r ?? n.position, w = e.subarray(a, a + c), d = n.syncHandle.write(w, { at: h });
865
- return (r == null || r === n.position) && (n.position = h + d), s && await this.notifyChange({ path: n.path, type: l.Changed, isDirectory: !1 }), d;
866
- } catch (h) {
867
- throw u("write", t, n.path, h);
863
+ const c = r ?? n.position, w = e.subarray(a, a + o), y = n.syncHandle.write(w, { at: c });
864
+ return (r == null || r === n.position) && (n.position = c + y), s && await this.notifyChange({ path: n.path, type: h.Changed, isDirectory: !1 }), y;
865
+ } catch (c) {
866
+ throw g("write", t, n.path, c);
868
867
  }
869
868
  }
870
869
  /**
@@ -908,11 +907,11 @@ class k {
908
907
  async ftruncate(t, e = 0) {
909
908
  const a = this._getFileDescriptor(t);
910
909
  if (e < 0 || !Number.isInteger(e))
911
- throw new o("Invalid size", "EINVAL");
910
+ throw new E("argument", "Invalid size");
912
911
  try {
913
- a.syncHandle.truncate(e), a.syncHandle.flush(), a.position > e && (a.position = e), await this.notifyChange({ path: a.path, type: l.Changed, isDirectory: !1 });
912
+ a.syncHandle.truncate(e), a.syncHandle.flush(), a.position > e && (a.position = e), await this.notifyChange({ path: a.path, type: h.Changed, isDirectory: !1 });
914
913
  } catch (i) {
915
- throw u("truncate", t, a.path, i);
914
+ throw g("truncate", t, a.path, i);
916
915
  }
917
916
  }
918
917
  /**
@@ -934,7 +933,7 @@ class k {
934
933
  try {
935
934
  e.syncHandle.flush();
936
935
  } catch (a) {
937
- throw u("sync", t, e.path, a);
936
+ throw g("sync", t, e.path, a);
938
937
  }
939
938
  }
940
939
  /**
@@ -983,15 +982,15 @@ class k {
983
982
  for (const [e, a] of t) {
984
983
  const i = F(e);
985
984
  let r;
986
- a instanceof Blob ? r = await _(a) : typeof a == "string" ? r = new TextEncoder().encode(a) : r = a, await this.writeFile(i, r);
985
+ a instanceof Blob ? r = await L(a) : typeof a == "string" ? r = new TextEncoder().encode(a) : r = a, await this.writeFile(i, r);
987
986
  }
988
987
  } catch (e) {
989
- throw e instanceof o ? e : new o("Failed to sync file system", "SYNC_FAILED", void 0, e);
988
+ throw e instanceof f ? e : l(e);
990
989
  }
991
990
  }
992
991
  }
993
- typeof globalThis < "u" && globalThis.constructor.name === "DedicatedWorkerGlobalScope" && A(new k());
992
+ typeof globalThis < "u" && globalThis.constructor.name === "DedicatedWorkerGlobalScope" && k(new j());
994
993
  export {
995
- k as OPFSWorker
994
+ j as OPFSWorker
996
995
  };
997
996
  //# sourceMappingURL=raw.js.map