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