opfs-worker 0.3.0 → 0.3.2

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/index.js CHANGED
@@ -1,21 +1,21 @@
1
1
  import { wrap as i } from "comlink";
2
- import { D as f, F as d, O as p, b as u, a as y, P as w, c as m, S as g, T as v, e as b, k as F, d as E, l as S, h as P, o as x, f as T, m as C, g as D, j as A, n as O, i as _, r as I, s as U, w as R } from "./helpers-C0nyU6hv.js";
2
+ import { D as d, F as f, O as p, b as u, a as y, P as m, c as w, S as g, T as v, e as b, k as E, d as S, l as F, h as P, o as x, f as D, m as C, g as T, j as A, n as O, i as _, r as I, s as U, w as R } from "./helpers-DxFcNkZe.js";
3
3
  const a = `/**
4
4
  * @license
5
5
  * Copyright 2019 Google LLC
6
6
  * SPDX-License-Identifier: Apache-2.0
7
7
  */
8
- const H = Symbol("Comlink.proxy"), R = Symbol("Comlink.endpoint"), B = Symbol("Comlink.releaseProxy"), A = Symbol("Comlink.finalizer"), E = Symbol("Comlink.thrown"), $ = (e) => typeof e == "object" && e !== null || typeof e == "function", W = {
8
+ const H = Symbol("Comlink.proxy"), R = Symbol("Comlink.endpoint"), B = Symbol("Comlink.releaseProxy"), C = Symbol("Comlink.finalizer"), p = Symbol("Comlink.thrown"), $ = (e) => typeof e == "object" && e !== null || typeof e == "function", W = {
9
9
  canHandle: (e) => $(e) && e[H],
10
10
  serialize(e) {
11
11
  const { port1: t, port2: r } = new MessageChannel();
12
- return I(e, t), [r, [r]];
12
+ return x(e, t), [r, [r]];
13
13
  },
14
14
  deserialize(e) {
15
15
  return e.start(), Y(e);
16
16
  }
17
17
  }, j = {
18
- canHandle: (e) => $(e) && E in e,
18
+ canHandle: (e) => $(e) && p in e,
19
19
  serialize({ value: e }) {
20
20
  let t;
21
21
  return e instanceof Error ? t = {
@@ -40,15 +40,15 @@ function V(e, t) {
40
40
  return !0;
41
41
  return !1;
42
42
  }
43
- function I(e, t = globalThis, r = ["*"]) {
44
- t.addEventListener("message", function n(i) {
45
- if (!i || !i.data)
43
+ function x(e, t = globalThis, r = ["*"]) {
44
+ t.addEventListener("message", function i(n) {
45
+ if (!n || !n.data)
46
46
  return;
47
- if (!V(r, i.origin)) {
48
- console.warn(\`Invalid origin '\${i.origin}' for comlink proxy\`);
47
+ if (!V(r, n.origin)) {
48
+ console.warn(\`Invalid origin '\${n.origin}' for comlink proxy\`);
49
49
  return;
50
50
  }
51
- const { id: a, type: s, path: o } = Object.assign({ path: [] }, i.data), l = (i.data.argumentList || []).map(d);
51
+ const { id: a, type: s, path: o } = Object.assign({ path: [] }, n.data), l = (n.data.argumentList || []).map(d);
52
52
  let h;
53
53
  try {
54
54
  const u = o.slice(0, -1).reduce((f, g) => f[g], e), w = o.reduce((f, g) => f[g], e);
@@ -57,7 +57,7 @@ function I(e, t = globalThis, r = ["*"]) {
57
57
  h = w;
58
58
  break;
59
59
  case "SET":
60
- u[o.slice(-1)[0]] = d(i.data.value), h = !0;
60
+ u[o.slice(-1)[0]] = d(n.data.value), h = !0;
61
61
  break;
62
62
  case "APPLY":
63
63
  h = w.apply(u, l);
@@ -71,7 +71,7 @@ function I(e, t = globalThis, r = ["*"]) {
71
71
  case "ENDPOINT":
72
72
  {
73
73
  const { port1: f, port2: g } = new MessageChannel();
74
- I(e, g), h = J(f, [f]);
74
+ x(e, g), h = J(f, [f]);
75
75
  }
76
76
  break;
77
77
  case "RELEASE":
@@ -81,15 +81,15 @@ function I(e, t = globalThis, r = ["*"]) {
81
81
  return;
82
82
  }
83
83
  } catch (u) {
84
- h = { value: u, [E]: 0 };
84
+ h = { value: u, [p]: 0 };
85
85
  }
86
- Promise.resolve(h).catch((u) => ({ value: u, [E]: 0 })).then((u) => {
87
- const [w, f] = D(u);
88
- t.postMessage(Object.assign(Object.assign({}, w), { id: a }), f), s === "RELEASE" && (t.removeEventListener("message", n), L(t), A in e && typeof e[A] == "function" && e[A]());
86
+ Promise.resolve(h).catch((u) => ({ value: u, [p]: 0 })).then((u) => {
87
+ const [w, f] = A(u);
88
+ t.postMessage(Object.assign(Object.assign({}, w), { id: a }), f), s === "RELEASE" && (t.removeEventListener("message", i), L(t), C in e && typeof e[C] == "function" && e[C]());
89
89
  }).catch((u) => {
90
- const [w, f] = D({
90
+ const [w, f] = A({
91
91
  value: new TypeError("Unserializable return value"),
92
- [E]: 0
92
+ [p]: 0
93
93
  });
94
94
  t.postMessage(Object.assign(Object.assign({}, w), { id: a }), f);
95
95
  });
@@ -103,8 +103,8 @@ function L(e) {
103
103
  }
104
104
  function Y(e, t) {
105
105
  const r = /* @__PURE__ */ new Map();
106
- return e.addEventListener("message", function(i) {
107
- const { data: a } = i;
106
+ return e.addEventListener("message", function(n) {
107
+ const { data: a } = n;
108
108
  if (!a || !a.id)
109
109
  return;
110
110
  const s = r.get(a.id);
@@ -114,9 +114,9 @@ function Y(e, t) {
114
114
  } finally {
115
115
  r.delete(a.id);
116
116
  }
117
- }), x(e, r, [], t);
117
+ }), M(e, r, [], t);
118
118
  }
119
- function p(e) {
119
+ function E(e) {
120
120
  if (e)
121
121
  throw new Error("Proxy has been released and is not useable");
122
122
  }
@@ -127,25 +127,25 @@ function U(e) {
127
127
  L(e);
128
128
  });
129
129
  }
130
- const F = /* @__PURE__ */ new WeakMap(), S = "FinalizationRegistry" in globalThis && new FinalizationRegistry((e) => {
131
- const t = (F.get(e) || 0) - 1;
132
- F.set(e, t), t === 0 && U(e);
130
+ const S = /* @__PURE__ */ new WeakMap(), D = "FinalizationRegistry" in globalThis && new FinalizationRegistry((e) => {
131
+ const t = (S.get(e) || 0) - 1;
132
+ S.set(e, t), t === 0 && U(e);
133
133
  });
134
134
  function X(e, t) {
135
- const r = (F.get(t) || 0) + 1;
136
- F.set(t, r), S && S.register(e, t, e);
135
+ const r = (S.get(t) || 0) + 1;
136
+ S.set(t, r), D && D.register(e, t, e);
137
137
  }
138
138
  function q(e) {
139
- S && S.unregister(e);
139
+ D && D.unregister(e);
140
140
  }
141
- function x(e, t, r = [], n = function() {
141
+ function M(e, t, r = [], i = function() {
142
142
  }) {
143
- let i = !1;
144
- const a = new Proxy(n, {
143
+ let n = !1;
144
+ const a = new Proxy(i, {
145
145
  get(s, o) {
146
- if (p(i), o === B)
146
+ if (E(n), o === B)
147
147
  return () => {
148
- q(a), U(e), t.clear(), i = !0;
148
+ q(a), U(e), t.clear(), n = !0;
149
149
  };
150
150
  if (o === "then") {
151
151
  if (r.length === 0)
@@ -156,11 +156,11 @@ function x(e, t, r = [], n = function() {
156
156
  }).then(d);
157
157
  return l.then.bind(l);
158
158
  }
159
- return x(e, t, [...r, o]);
159
+ return M(e, t, [...r, o]);
160
160
  },
161
161
  set(s, o, l) {
162
- p(i);
163
- const [h, u] = D(l);
162
+ E(n);
163
+ const [h, u] = A(l);
164
164
  return y(e, t, {
165
165
  type: "SET",
166
166
  path: [...r, o].map((w) => w.toString()),
@@ -168,14 +168,14 @@ function x(e, t, r = [], n = function() {
168
168
  }, u).then(d);
169
169
  },
170
170
  apply(s, o, l) {
171
- p(i);
171
+ E(n);
172
172
  const h = r[r.length - 1];
173
173
  if (h === R)
174
174
  return y(e, t, {
175
175
  type: "ENDPOINT"
176
176
  }).then(d);
177
177
  if (h === "bind")
178
- return x(e, t, r.slice(0, -1));
178
+ return M(e, t, r.slice(0, -1));
179
179
  const [u, w] = v(l);
180
180
  return y(e, t, {
181
181
  type: "APPLY",
@@ -184,7 +184,7 @@ function x(e, t, r = [], n = function() {
184
184
  }, w).then(d);
185
185
  },
186
186
  construct(s, o) {
187
- p(i);
187
+ E(n);
188
188
  const [l, h] = v(o);
189
189
  return y(e, t, {
190
190
  type: "CONSTRUCT",
@@ -199,7 +199,7 @@ function K(e) {
199
199
  return Array.prototype.concat.apply([], e);
200
200
  }
201
201
  function v(e) {
202
- const t = e.map(D);
202
+ const t = e.map(A);
203
203
  return [t.map((r) => r[0]), K(t.map((r) => r[1]))];
204
204
  }
205
205
  const _ = /* @__PURE__ */ new WeakMap();
@@ -209,17 +209,17 @@ function J(e, t) {
209
209
  function Q(e) {
210
210
  return Object.assign(e, { [H]: !0 });
211
211
  }
212
- function D(e) {
212
+ function A(e) {
213
213
  for (const [t, r] of z)
214
214
  if (r.canHandle(e)) {
215
- const [n, i] = r.serialize(e);
215
+ const [i, n] = r.serialize(e);
216
216
  return [
217
217
  {
218
218
  type: "HANDLER",
219
219
  name: t,
220
- value: n
220
+ value: i
221
221
  },
222
- i
222
+ n
223
223
  ];
224
224
  }
225
225
  return [
@@ -238,18 +238,18 @@ function d(e) {
238
238
  return e.value;
239
239
  }
240
240
  }
241
- function y(e, t, r, n) {
242
- return new Promise((i) => {
241
+ function y(e, t, r, i) {
242
+ return new Promise((n) => {
243
243
  const a = Z();
244
- t.set(a, i), e.start && e.start(), e.postMessage(Object.assign({ id: a }, r), n);
244
+ t.set(a, n), e.start && e.start(), e.postMessage(Object.assign({ id: a }, r), i);
245
245
  });
246
246
  }
247
247
  function Z() {
248
248
  return new Array(4).fill(0).map(() => Math.floor(Math.random() * Number.MAX_SAFE_INTEGER).toString(16)).join("-");
249
249
  }
250
250
  class c extends Error {
251
- constructor(t, r, n) {
252
- super(t), this.code = r, this.path = n, this.name = "OPFSError";
251
+ constructor(t, r, i) {
252
+ super(t), this.code = r, this.path = i, this.name = "OPFSError";
253
253
  }
254
254
  }
255
255
  class tt extends c {
@@ -257,7 +257,7 @@ class tt extends c {
257
257
  super("OPFS is not supported in this browser", "OPFS_NOT_SUPPORTED");
258
258
  }
259
259
  }
260
- class C extends c {
260
+ class b extends c {
261
261
  constructor() {
262
262
  super("OPFS is not mounted", "OPFS_NOT_MOUNTED");
263
263
  }
@@ -267,7 +267,7 @@ class N extends c {
267
267
  super(t, "INVALID_PATH", r);
268
268
  }
269
269
  }
270
- class b extends c {
270
+ class T extends c {
271
271
  constructor(t) {
272
272
  super(\`File not found: \${t}\`, "FILE_NOT_FOUND", t);
273
273
  }
@@ -323,8 +323,8 @@ function rt(e, t = "utf-8") {
323
323
  function nt(e) {
324
324
  const t = new Uint8Array(e.length * 2);
325
325
  for (let r = 0; r < e.length; r++) {
326
- const n = e.charCodeAt(r);
327
- t[r * 2] = n & 255, t[r * 2 + 1] = n >> 8;
326
+ const i = e.charCodeAt(r);
327
+ t[r * 2] = i & 255, t[r * 2 + 1] = i >> 8;
328
328
  }
329
329
  return t;
330
330
  }
@@ -350,33 +350,33 @@ function ot() {
350
350
  throw new tt();
351
351
  }
352
352
  function m(e) {
353
- return Array.isArray(e) ? e : e.split("/").filter(Boolean);
353
+ return Array.isArray(e) ? e : (e.startsWith("~/") ? e.slice(2) : e).split("/").filter(Boolean);
354
354
  }
355
- function P(e) {
355
+ function I(e) {
356
356
  return typeof e == "string" ? e ?? "/" : \`/\${e.join("/")}\`;
357
357
  }
358
- function T(e) {
358
+ function O(e) {
359
359
  const t = m(e);
360
360
  return t[t.length - 1] || "";
361
361
  }
362
- function O(e) {
362
+ function P(e) {
363
363
  const t = m(e);
364
- return t.pop(), P(t);
364
+ return t.pop(), I(t);
365
365
  }
366
- function M(e) {
367
- return !e || e === "/" ? "/" : e.startsWith("/") ? e : \`/\${e}\`;
366
+ function F(e) {
367
+ return !e || e === "/" ? "/" : e.startsWith("~/") ? \`/\${e.slice(2)}\` : e.startsWith("/") ? e : \`/\${e}\`;
368
368
  }
369
369
  function ct(e) {
370
- const t = m(e), r = [];
371
- for (const n of t)
370
+ const t = F(e), r = m(t), i = [];
371
+ for (const n of r)
372
372
  if (!(n === "." || n === ""))
373
373
  if (n === "..") {
374
- if (r.length === 0)
374
+ if (i.length === 0)
375
375
  continue;
376
- r.pop();
376
+ i.pop();
377
377
  } else
378
- r.push(n);
379
- return P(r);
378
+ i.push(n);
379
+ return I(i);
380
380
  }
381
381
  function lt(e, t = "utf-8") {
382
382
  return typeof e == "string" ? et(e, t) : e instanceof Uint8Array ? e : new Uint8Array(e);
@@ -384,26 +384,26 @@ function lt(e, t = "utf-8") {
384
384
  async function ht(e) {
385
385
  const t = await e.createSyncAccessHandle();
386
386
  try {
387
- const r = t.getSize(), n = new Uint8Array(r);
388
- return t.read(n, { at: 0 }), n;
387
+ const r = t.getSize(), i = new Uint8Array(r);
388
+ return t.read(i, { at: 0 }), i;
389
389
  } finally {
390
390
  t.close();
391
391
  }
392
392
  }
393
- async function k(e, t, r, n = {}) {
394
- let i = null;
393
+ async function k(e, t, r, i = {}) {
394
+ let n = null;
395
395
  try {
396
- i = await e.createSyncAccessHandle();
397
- const a = lt(t, r), s = n.append ? i.getSize() : 0;
398
- i.write(a, { at: s }), n.truncate && !n.append && i.truncate(a.byteLength), i.flush();
396
+ n = await e.createSyncAccessHandle();
397
+ const a = lt(t, r), s = i.append ? n.getSize() : 0;
398
+ n.write(a, { at: s }), i.truncate && !i.append && n.truncate(a.byteLength), n.flush();
399
399
  } catch (a) {
400
400
  console.error(a);
401
- const s = n.append ? "append" : "write";
401
+ const s = i.append ? "append" : "write";
402
402
  throw new c(\`Failed to \${s} file\`, \`\${s.toUpperCase()}_FAILED\`);
403
403
  } finally {
404
- if (i)
404
+ if (n)
405
405
  try {
406
- i.close();
406
+ n.close();
407
407
  } catch {
408
408
  }
409
409
  }
@@ -411,8 +411,8 @@ async function k(e, t, r, n = {}) {
411
411
  async function ut(e, t = "SHA-1", r = 50 * 1024 * 1024) {
412
412
  if (e instanceof File && (e = await e.arrayBuffer()), e.byteLength > r)
413
413
  throw new Error(\`File size \${e.byteLength} bytes exceeds maximum allowed size \${r} bytes\`);
414
- const n = new Uint8Array(e), i = await crypto.subtle.digest(t, n);
415
- return Array.from(new Uint8Array(i)).map((s) => s.toString(16).padStart(2, "0")).join("");
414
+ const i = new Uint8Array(e), n = await crypto.subtle.digest(t, i);
415
+ return Array.from(new Uint8Array(n)).map((s) => s.toString(16).padStart(2, "0")).join("");
416
416
  }
417
417
  async function ft(e) {
418
418
  const t = await e.arrayBuffer();
@@ -453,21 +453,22 @@ class wt {
453
453
  let r;
454
454
  if (this.options.hashAlgorithm && !t.isDirectory && t.type !== "removed")
455
455
  try {
456
- const n = await this.stat(t.path);
457
- n.isFile && n.hash && (r = n.hash);
458
- } catch (n) {
459
- console.warn(\`Failed to calculate hash for \${t.path}:\`, n);
456
+ const i = await this.stat(t.path);
457
+ i.isFile && i.hash && (r = i.hash);
458
+ } catch (i) {
459
+ console.warn(\`Failed to calculate hash for \${t.path}:\`, i);
460
460
  }
461
461
  try {
462
462
  this.broadcastChannel || (this.broadcastChannel = new BroadcastChannel(this.options.broadcastChannel));
463
- const n = {
463
+ const i = {
464
+ root: this.root.name,
464
465
  timestamp: (/* @__PURE__ */ new Date()).toISOString(),
465
466
  ...t,
466
467
  ...r && { hash: r }
467
468
  };
468
- this.broadcastChannel.postMessage(n);
469
- } catch (n) {
470
- console.warn("Failed to send event via BroadcastChannel:", n);
469
+ this.broadcastChannel.postMessage(i);
470
+ } catch (i) {
471
+ console.warn("Failed to send event via BroadcastChannel:", i);
471
472
  }
472
473
  }
473
474
  /**
@@ -504,13 +505,13 @@ class wt {
504
505
  * \`\`\`
505
506
  */
506
507
  async mount(t = "/") {
507
- return this.mountingPromise && await this.mountingPromise, this.mountingPromise = new Promise(async (r, n) => {
508
+ return this.mountingPromise && await this.mountingPromise, this.mountingPromise = new Promise(async (r, i) => {
508
509
  this.root = null;
509
510
  try {
510
- const i = await navigator.storage.getDirectory();
511
- t === "/" ? this.root = i : this.root = await this.getDirectoryHandle(t, !0, i), r(!0);
512
- } catch (i) {
513
- console.error(i), n(new c("Failed to initialize OPFS", "INIT_FAILED"));
511
+ const n = await navigator.storage.getDirectory();
512
+ t === "/" ? this.root = n : this.root = await this.getDirectoryHandle(t, !0, n), r(!0);
513
+ } catch (n) {
514
+ console.error(n), i(new c("Failed to initialize OPFS", "INIT_FAILED"));
514
515
  } finally {
515
516
  this.mountingPromise = null;
516
517
  }
@@ -564,12 +565,12 @@ class wt {
564
565
  * const docsDir2 = await fs.getDirectoryHandle(['users', 'john', 'documents'], true);
565
566
  * \`\`\`
566
567
  */
567
- async getDirectoryHandle(t, r = !1, n = this.root) {
568
- if (!n)
569
- throw new C();
570
- const i = Array.isArray(t) ? t : m(t);
571
- let a = n;
572
- for (const s of i)
568
+ async getDirectoryHandle(t, r = !1, i = this.root) {
569
+ if (!i)
570
+ throw new b();
571
+ const n = Array.isArray(t) ? t : m(t);
572
+ let a = i;
573
+ for (const s of n)
573
574
  a = await a.getDirectoryHandle(s, { create: r });
574
575
  return a;
575
576
  }
@@ -592,14 +593,14 @@ class wt {
592
593
  * const fileHandle2 = await fs.getFileHandle(['config', 'settings.json'], true);
593
594
  * \`\`\`
594
595
  */
595
- async getFileHandle(t, r = !1, n = this.root) {
596
- if (!n)
597
- throw new C();
598
- const i = m(t);
599
- if (i.length === 0)
596
+ async getFileHandle(t, r = !1, i = this.root) {
597
+ if (!i)
598
+ throw new b();
599
+ const n = m(t);
600
+ if (n.length === 0)
600
601
  throw new N("Path must not be empty", Array.isArray(t) ? t.join("/") : t);
601
- const a = i.pop();
602
- return (await this.getDirectoryHandle(i, r, n)).getFileHandle(a, { create: r });
602
+ const a = n.pop();
603
+ return (await this.getDirectoryHandle(n, r, i)).getFileHandle(a, { create: r });
603
604
  }
604
605
  /**
605
606
  * Get a complete index of all files and directories in the file system
@@ -621,10 +622,10 @@ class wt {
621
622
  * \`\`\`
622
623
  */
623
624
  async index() {
624
- const t = /* @__PURE__ */ new Map(), r = async (n) => {
625
- const i = await this.readdir(n, { withFileTypes: !0 });
626
- for (const a of i) {
627
- const s = \`\${n === "/" ? "" : n}/\${a.name}\`;
625
+ const t = /* @__PURE__ */ new Map(), r = async (i) => {
626
+ const n = await this.readDir(i);
627
+ for (const a of n) {
628
+ const s = \`\${i === "/" ? "" : i}/\${a.name}\`;
628
629
  try {
629
630
  const o = await this.stat(s);
630
631
  t.set(s, o), o.isDirectory && await r(s);
@@ -645,10 +646,10 @@ class wt {
645
646
  async readFile(t, r = "utf-8") {
646
647
  await this.ensureMounted();
647
648
  try {
648
- const n = await this.getFileHandle(t, !1), i = await ht(n);
649
- return r === "binary" ? i : rt(i, r);
650
- } catch (n) {
651
- throw console.error(n), new b(t);
649
+ const i = await this.getFileHandle(t, !1), n = await ht(i);
650
+ return r === "binary" ? n : rt(n, r);
651
+ } catch (i) {
652
+ throw console.error(i), new T(t);
652
653
  }
653
654
  }
654
655
  /**
@@ -676,10 +677,10 @@ class wt {
676
677
  * await fs.writeFile('/data/utf16.txt', 'Hello World', 'utf-16le');
677
678
  * \`\`\`
678
679
  */
679
- async writeFile(t, r, n) {
680
+ async writeFile(t, r, i) {
680
681
  await this.ensureMounted();
681
- const i = await this.getFileHandle(t, !0);
682
- await k(i, r, n, { truncate: !0 }), await this.notifyChange({ path: t, type: "changed", isDirectory: !1 });
682
+ const n = await this.getFileHandle(t, !0);
683
+ await k(n, r, i, { truncate: !0 }), await this.notifyChange({ path: t, type: "changed", isDirectory: !1 });
683
684
  }
684
685
  /**
685
686
  * Append data to a file
@@ -703,10 +704,10 @@ class wt {
703
704
  * await fs.appendFile('/data/binary.dat', additionalData);
704
705
  * \`\`\`
705
706
  */
706
- async appendFile(t, r, n) {
707
+ async appendFile(t, r, i) {
707
708
  await this.ensureMounted();
708
- const i = await this.getFileHandle(t, !0);
709
- await k(i, r, n, { append: !0 }), await this.notifyChange({ path: t, type: "changed", isDirectory: !1 });
709
+ const n = await this.getFileHandle(t, !0);
710
+ await k(n, r, i, { append: !0 }), await this.notifyChange({ path: t, type: "changed", isDirectory: !1 });
710
711
  }
711
712
  /**
712
713
  * Create a directory
@@ -731,16 +732,16 @@ class wt {
731
732
  */
732
733
  async mkdir(t, r) {
733
734
  if (await this.ensureMounted(), !this.root)
734
- throw new C();
735
- const n = r?.recursive ?? !1, i = m(t);
735
+ throw new b();
736
+ const i = r?.recursive ?? !1, n = m(t);
736
737
  let a = this.root;
737
- for (let s = 0; s < i.length; s++) {
738
- const o = i[s];
738
+ for (let s = 0; s < n.length; s++) {
739
+ const o = n[s];
739
740
  try {
740
- a = await a.getDirectoryHandle(o, { create: n || s === i.length - 1 });
741
+ a = await a.getDirectoryHandle(o, { create: i || s === n.length - 1 });
741
742
  } catch (l) {
742
743
  throw l.name === "NotFoundError" ? new c(
743
- \`Parent directory does not exist: \${P(i.slice(0, s + 1))}\`,
744
+ \`Parent directory does not exist: \${I(n.slice(0, s + 1))}\`,
744
745
  "ENOENT"
745
746
  ) : l.name === "TypeMismatchError" ? new c(\`Path segment is not a directory: \${o}\`, "ENOTDIR") : new c("Failed to create directory", "MKDIR_FAILED");
746
747
  }
@@ -779,9 +780,9 @@ class wt {
779
780
  isFile: !1,
780
781
  isDirectory: !0
781
782
  };
782
- const r = T(t), n = await this.getDirectoryHandle(O(t), !1), i = this.options.hashAlgorithm !== null;
783
+ const r = O(t), i = await this.getDirectoryHandle(P(t), !1), n = this.options.hashAlgorithm !== null;
783
784
  try {
784
- const s = await (await n.getFileHandle(r, { create: !1 })).getFile(), o = {
785
+ const s = await (await i.getFileHandle(r, { create: !1 })).getFile(), o = {
785
786
  kind: "file",
786
787
  size: s.size,
787
788
  mtime: new Date(s.lastModified).toISOString(),
@@ -789,7 +790,7 @@ class wt {
789
790
  isFile: !0,
790
791
  isDirectory: !1
791
792
  };
792
- if (i && this.options.hashAlgorithm)
793
+ if (n && this.options.hashAlgorithm)
793
794
  try {
794
795
  const l = await ut(s, this.options.hashAlgorithm, this.options.maxFileSize);
795
796
  o.hash = l;
@@ -802,7 +803,7 @@ class wt {
802
803
  throw new c("Failed to stat (file)", "STAT_FAILED");
803
804
  }
804
805
  try {
805
- return await n.getDirectoryHandle(r, { create: !1 }), {
806
+ return await i.getDirectoryHandle(r, { create: !1 }), {
806
807
  kind: "directory",
807
808
  size: 0,
808
809
  mtime: (/* @__PURE__ */ new Date(0)).toISOString(),
@@ -814,27 +815,37 @@ class wt {
814
815
  throw a.name === "NotFoundError" ? new c(\`No such file or directory: \${t}\`, "ENOENT") : new c("Failed to stat (directory)", "STAT_FAILED");
815
816
  }
816
817
  }
817
- async readdir(t, r) {
818
+ /**
819
+ * Read a directory's contents
820
+ *
821
+ * Lists all files and subdirectories within the specified directory.
822
+ *
823
+ * @param path - The path to the directory to read
824
+ * @returns Promise that resolves to an array of detailed file/directory information
825
+ * @throws {OPFSError} If the directory does not exist or cannot be accessed
826
+ *
827
+ * @example
828
+ * \`\`\`typescript
829
+ * // Get detailed information about files and directories
830
+ * const detailed = await fs.readDir('/users/john/documents');
831
+ * detailed.forEach(item => {
832
+ * console.log(\`\${item.name} - \${item.isFile ? 'file' : 'directory'}\`);
833
+ * });
834
+ * \`\`\`
835
+ */
836
+ async readDir(t) {
818
837
  await this.ensureMounted();
819
- const n = r?.withFileTypes ?? !1, i = await this.getDirectoryHandle(t, !1);
820
- if (n) {
821
- const a = [];
822
- for await (const [s, o] of i.entries()) {
823
- const l = o.kind === "file";
824
- a.push({
825
- name: s,
826
- kind: o.kind,
827
- isFile: l,
828
- isDirectory: !l
829
- });
830
- }
831
- return a;
832
- } else {
833
- const a = [];
834
- for await (const [s] of i.entries())
835
- a.push(s);
836
- return a;
838
+ const r = await this.getDirectoryHandle(t, !1), i = [];
839
+ for await (const [n, a] of r.entries()) {
840
+ const s = a.kind === "file";
841
+ i.push({
842
+ name: n,
843
+ kind: a.kind,
844
+ isFile: s,
845
+ isDirectory: !s
846
+ });
837
847
  }
848
+ return i;
838
849
  }
839
850
  /**
840
851
  * Check if a file or directory exists
@@ -853,26 +864,26 @@ class wt {
853
864
  async exists(t) {
854
865
  if (await this.ensureMounted(), t === "/")
855
866
  return !0;
856
- const r = T(t);
857
- let n = null;
867
+ const r = O(t);
868
+ let i = null;
858
869
  try {
859
- n = await this.getDirectoryHandle(O(t), !1);
860
- } catch (i) {
861
- throw (i.name === "NotFoundError" || i.name === "TypeMismatchError") && (n = null), i;
870
+ i = await this.getDirectoryHandle(P(t), !1);
871
+ } catch (n) {
872
+ throw (n.name === "NotFoundError" || n.name === "TypeMismatchError") && (i = null), n;
862
873
  }
863
- if (!n || !r)
874
+ if (!i || !r)
864
875
  return !1;
865
876
  try {
866
- return await n.getFileHandle(r, { create: !1 }), !0;
867
- } catch (i) {
868
- if (i.name !== "NotFoundError" && i.name !== "TypeMismatchError")
869
- throw i;
877
+ return await i.getFileHandle(r, { create: !1 }), !0;
878
+ } catch (n) {
879
+ if (n.name !== "NotFoundError" && n.name !== "TypeMismatchError")
880
+ throw n;
870
881
  }
871
882
  try {
872
- return await n.getDirectoryHandle(r, { create: !1 }), !0;
873
- } catch (i) {
874
- if (i.name !== "NotFoundError" && i.name !== "TypeMismatchError")
875
- throw i;
883
+ return await i.getDirectoryHandle(r, { create: !1 }), !0;
884
+ } catch (n) {
885
+ if (n.name !== "NotFoundError" && n.name !== "TypeMismatchError")
886
+ throw n;
876
887
  }
877
888
  return !1;
878
889
  }
@@ -898,10 +909,10 @@ class wt {
898
909
  async clear(t = "/") {
899
910
  await this.ensureMounted();
900
911
  try {
901
- const r = await this.readdir(t, { withFileTypes: !0 });
902
- for (const n of r) {
903
- const i = \`\${t === "/" ? "" : t}/\${n.name}\`;
904
- await this.remove(i, { recursive: !0 });
912
+ const r = await this.readDir(t);
913
+ for (const i of r) {
914
+ const n = \`\${t === "/" ? "" : t}/\${i.name}\`;
915
+ await this.remove(n, { recursive: !0 });
905
916
  }
906
917
  await this.notifyChange({ path: t, type: "changed", isDirectory: !0 });
907
918
  } catch (r) {
@@ -934,20 +945,20 @@ class wt {
934
945
  */
935
946
  async remove(t, r) {
936
947
  await this.ensureMounted();
937
- const n = r?.recursive ?? !1, i = r?.force ?? !1;
948
+ const i = r?.recursive ?? !1, n = r?.force ?? !1;
938
949
  if (t === "/")
939
950
  throw new c("Cannot remove root directory", "EROOT");
940
- const a = T(t);
951
+ const a = O(t);
941
952
  if (!a)
942
953
  throw new N("Invalid path", t);
943
- const s = await this.getDirectoryHandle(O(t), !1);
954
+ const s = await this.getDirectoryHandle(P(t), !1);
944
955
  try {
945
- await s.removeEntry(a, { recursive: n });
956
+ await s.removeEntry(a, { recursive: i });
946
957
  } catch (o) {
947
958
  if (o.name === "NotFoundError") {
948
- if (!i)
959
+ if (!n)
949
960
  throw new c(\`No such file or directory: \${t}\`, "ENOENT");
950
- } else throw o.name === "InvalidModificationError" ? new c(\`Directory not empty: \${t}. Use recursive option to force removal.\`, "ENOTEMPTY") : o.name === "TypeMismatchError" && !n ? new c(\`Cannot remove directory without recursive option: \${t}\`, "EISDIR") : new c(\`Failed to remove path: \${t}\`, "RM_FAILED");
961
+ } else throw o.name === "InvalidModificationError" ? new c(\`Directory not empty: \${t}. Use recursive option to force removal.\`, "ENOTEMPTY") : o.name === "TypeMismatchError" && !i ? new c(\`Cannot remove directory without recursive option: \${t}\`, "EISDIR") : new c(\`Failed to remove path: \${t}\`, "RM_FAILED");
951
962
  }
952
963
  await this.notifyChange({ path: t, type: "removed", isDirectory: !1 });
953
964
  }
@@ -974,7 +985,7 @@ class wt {
974
985
  try {
975
986
  const r = ct(t);
976
987
  if (!await this.exists(r))
977
- throw new b(r);
988
+ throw new T(r);
978
989
  return r;
979
990
  } catch (r) {
980
991
  throw r instanceof c ? r : new c(\`Failed to resolve path: \${t}\`, "REALPATH_FAILED");
@@ -1000,10 +1011,10 @@ class wt {
1000
1011
  await this.ensureMounted();
1001
1012
  try {
1002
1013
  if (!await this.exists(t))
1003
- throw new b(t);
1014
+ throw new T(t);
1004
1015
  await this.copy(t, r, { recursive: !0 }), await this.remove(t, { recursive: !0 }), await this.notifyChange({ path: t, type: "removed", isDirectory: !1 }), await this.notifyChange({ path: r, type: "added", isDirectory: !1 });
1005
- } catch (n) {
1006
- throw n instanceof c ? n : new c(\`Failed to rename from \${t} to \${r}\`, "RENAME_FAILED");
1016
+ } catch (i) {
1017
+ throw i instanceof c ? i : new c(\`Failed to rename from \${t} to \${r}\`, "RENAME_FAILED");
1007
1018
  }
1008
1019
  }
1009
1020
  /**
@@ -1031,10 +1042,10 @@ class wt {
1031
1042
  * await fs.copy('/source', '/dest', { recursive: true, force: false });
1032
1043
  * \`\`\`
1033
1044
  */
1034
- async copy(t, r, n) {
1045
+ async copy(t, r, i) {
1035
1046
  await this.ensureMounted();
1036
1047
  try {
1037
- const i = n?.recursive ?? !1, a = n?.force ?? !0;
1048
+ const n = i?.recursive ?? !1, a = i?.force ?? !0;
1038
1049
  if (!await this.exists(t))
1039
1050
  throw new c(\`Source does not exist: \${t}\`, "ENOENT");
1040
1051
  if (await this.exists(r) && !a)
@@ -1043,18 +1054,18 @@ class wt {
1043
1054
  const h = await this.readFile(t, "binary");
1044
1055
  await this.writeFile(r, h);
1045
1056
  } else {
1046
- if (!i)
1057
+ if (!n)
1047
1058
  throw new c(\`Cannot copy directory without recursive option: \${t}\`, "EISDIR");
1048
1059
  await this.mkdir(r, { recursive: !0 });
1049
- const h = await this.readdir(t, { withFileTypes: !0 });
1060
+ const h = await this.readDir(t);
1050
1061
  for (const u of h) {
1051
1062
  const w = \`\${t}/\${u.name}\`, f = \`\${r}/\${u.name}\`;
1052
1063
  await this.copy(w, f, { recursive: !0, force: a });
1053
1064
  }
1054
1065
  }
1055
1066
  await this.notifyChange({ path: r, type: "added", isDirectory: !1 });
1056
- } catch (i) {
1057
- throw i instanceof c ? i : new c(\`Failed to copy from \${t} to \${r}\`, "CP_FAILED");
1067
+ } catch (n) {
1068
+ throw n instanceof c ? n : new c(\`Failed to copy from \${t} to \${r}\`, "CP_FAILED");
1058
1069
  }
1059
1070
  }
1060
1071
  /**
@@ -1062,8 +1073,8 @@ class wt {
1062
1073
  */
1063
1074
  async watch(t) {
1064
1075
  await this.ensureMounted();
1065
- const r = M(t), n = await this.buildSnapshot(r);
1066
- this.watchers.set(r, n), this.watchTimer || (this.watchTimer = setInterval(() => {
1076
+ const r = F(t), i = await this.buildSnapshot(r);
1077
+ this.watchers.set(r, i), this.watchTimer || (this.watchTimer = setInterval(() => {
1067
1078
  this.scanWatches();
1068
1079
  }, this.options.watchInterval));
1069
1080
  }
@@ -1071,7 +1082,7 @@ class wt {
1071
1082
  * Stop watching a previously watched path
1072
1083
  */
1073
1084
  unwatch(t) {
1074
- const r = M(t);
1085
+ const r = F(t);
1075
1086
  this.watchers.delete(r), this.watchers.size === 0 && this.watchTimer && (clearInterval(this.watchTimer), this.watchTimer = null);
1076
1087
  }
1077
1088
  /**
@@ -1084,17 +1095,17 @@ class wt {
1084
1095
  this.broadcastChannel && (this.broadcastChannel.close(), this.broadcastChannel = null), this.watchTimer && (clearInterval(this.watchTimer), this.watchTimer = null), this.watchers.clear();
1085
1096
  }
1086
1097
  async buildSnapshot(t) {
1087
- const r = /* @__PURE__ */ new Map(), n = async (i) => {
1088
- const a = await this.stat(i);
1089
- if (r.set(i, a), a.isDirectory) {
1090
- const s = await this.readdir(i, { withFileTypes: !0 });
1098
+ const r = /* @__PURE__ */ new Map(), i = async (n) => {
1099
+ const a = await this.stat(n);
1100
+ if (r.set(n, a), a.isDirectory) {
1101
+ const s = await this.readDir(n);
1091
1102
  for (const o of s) {
1092
- const l = \`\${i === "/" ? "" : i}/\${o.name}\`;
1093
- await n(l);
1103
+ const l = \`\${n === "/" ? "" : n}/\${o.name}\`;
1104
+ await i(l);
1094
1105
  }
1095
1106
  }
1096
1107
  };
1097
- return await n(t), r;
1108
+ return await i(t), r;
1098
1109
  }
1099
1110
  async scanWatches() {
1100
1111
  if (!this.scanning) {
@@ -1102,22 +1113,22 @@ class wt {
1102
1113
  try {
1103
1114
  await Promise.all(
1104
1115
  [...this.watchers.entries()].map(async ([t, r]) => {
1105
- let n;
1116
+ let i;
1106
1117
  try {
1107
- n = await this.buildSnapshot(t);
1118
+ i = await this.buildSnapshot(t);
1108
1119
  } catch {
1109
- n = /* @__PURE__ */ new Map();
1120
+ i = /* @__PURE__ */ new Map();
1110
1121
  }
1111
- for (const [i, a] of n) {
1112
- const s = r.get(i);
1113
- s ? (s.mtime !== a.mtime || s.size !== a.size) && await this.notifyChange({ path: i, type: "changed", isDirectory: a.isDirectory }) : await this.notifyChange({ path: i, type: "added", isDirectory: a.isDirectory });
1122
+ for (const [n, a] of i) {
1123
+ const s = r.get(n);
1124
+ s ? (s.mtime !== a.mtime || s.size !== a.size) && await this.notifyChange({ path: n, type: "changed", isDirectory: a.isDirectory }) : await this.notifyChange({ path: n, type: "added", isDirectory: a.isDirectory });
1114
1125
  }
1115
- for (const i of r.keys())
1116
- if (!n.has(i)) {
1117
- const a = r.get(i);
1118
- await this.notifyChange({ path: i, type: "removed", isDirectory: a?.isDirectory ?? !1 });
1126
+ for (const n of r.keys())
1127
+ if (!i.has(n)) {
1128
+ const a = r.get(n);
1129
+ await this.notifyChange({ path: n, type: "removed", isDirectory: a?.isDirectory ?? !1 });
1119
1130
  }
1120
- this.watchers.set(t, n);
1131
+ this.watchers.set(t, i);
1121
1132
  })
1122
1133
  );
1123
1134
  } finally {
@@ -1157,19 +1168,19 @@ class wt {
1157
1168
  await this.ensureMounted();
1158
1169
  try {
1159
1170
  (r?.cleanBefore ?? !1) && await this.clear("/");
1160
- for (const [i, a] of t) {
1161
- const s = M(i);
1171
+ for (const [n, a] of t) {
1172
+ const s = F(n);
1162
1173
  let o;
1163
1174
  a instanceof Blob ? o = await ft(a) : o = a, await this.writeFile(s, o);
1164
1175
  }
1165
1176
  await this.notifyChange({ path: "/", type: "changed", isDirectory: !0 });
1166
- } catch (n) {
1167
- throw n instanceof c ? n : new c("Failed to sync file system", "SYNC_FAILED");
1177
+ } catch (i) {
1178
+ throw i instanceof c ? i : new c("Failed to sync file system", "SYNC_FAILED");
1168
1179
  }
1169
1180
  }
1170
1181
  }
1171
- typeof self < "u" && self.constructor.name === "DedicatedWorkerGlobalScope" && I(new wt());
1172
- //# sourceMappingURL=worker-DvdTp4yb.js.map
1182
+ typeof self < "u" && self.constructor.name === "DedicatedWorkerGlobalScope" && x(new wt());
1183
+ //# sourceMappingURL=worker-CLK22qZk.js.map
1173
1184
  `, r = typeof self < "u" && self.Blob && new Blob(["URL.revokeObjectURL(import.meta.url);", a], { type: "text/javascript;charset=utf-8" });
1174
1185
  function o(t) {
1175
1186
  let n;
@@ -1197,25 +1208,25 @@ function c(t) {
1197
1208
  return t && n.setOptions(t), n;
1198
1209
  }
1199
1210
  export {
1200
- f as DirectoryNotFoundError,
1201
- d as FileNotFoundError,
1211
+ d as DirectoryNotFoundError,
1212
+ f as FileNotFoundError,
1202
1213
  p as OPFSError,
1203
1214
  u as OPFSNotMountedError,
1204
1215
  y as OPFSNotSupportedError,
1205
- w as PathError,
1206
- m as PermissionError,
1216
+ m as PathError,
1217
+ w as PermissionError,
1207
1218
  g as StorageError,
1208
1219
  v as TimeoutError,
1209
1220
  b as basename,
1210
- F as calculateFileHash,
1211
- E as checkOPFSSupport,
1212
- S as convertBlobToUint8Array,
1221
+ E as calculateFileHash,
1222
+ S as checkOPFSSupport,
1223
+ F as convertBlobToUint8Array,
1213
1224
  P as createBuffer,
1214
1225
  c as createWorker,
1215
1226
  x as decodeBuffer,
1216
- T as dirname,
1227
+ D as dirname,
1217
1228
  C as encodeString,
1218
- D as extname,
1229
+ T as extname,
1219
1230
  A as joinPath,
1220
1231
  O as normalizePath,
1221
1232
  _ as readFileData,