opfs-worker 0.3.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/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,21 @@ 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
462
  timestamp: (/* @__PURE__ */ new Date()).toISOString(),
463
463
  ...t,
464
464
  ...r && { hash: r }
465
465
  };
466
- this.broadcastChannel.postMessage(n);
467
- } catch (n) {
468
- console.warn("Failed to send event via BroadcastChannel:", n);
466
+ this.broadcastChannel.postMessage(i);
467
+ } catch (i) {
468
+ console.warn("Failed to send event via BroadcastChannel:", i);
469
469
  }
470
470
  }
471
471
  /**
@@ -502,13 +502,13 @@ class wt {
502
502
  * \`\`\`
503
503
  */
504
504
  async mount(t = "/") {
505
- return this.mountingPromise && await this.mountingPromise, this.mountingPromise = new Promise(async (r, n) => {
505
+ return this.mountingPromise && await this.mountingPromise, this.mountingPromise = new Promise(async (r, i) => {
506
506
  this.root = null;
507
507
  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"));
508
+ const n = await navigator.storage.getDirectory();
509
+ t === "/" ? this.root = n : this.root = await this.getDirectoryHandle(t, !0, n), r(!0);
510
+ } catch (n) {
511
+ console.error(n), i(new c("Failed to initialize OPFS", "INIT_FAILED"));
512
512
  } finally {
513
513
  this.mountingPromise = null;
514
514
  }
@@ -562,12 +562,12 @@ class wt {
562
562
  * const docsDir2 = await fs.getDirectoryHandle(['users', 'john', 'documents'], true);
563
563
  * \`\`\`
564
564
  */
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)
565
+ async getDirectoryHandle(t, r = !1, i = this.root) {
566
+ if (!i)
567
+ throw new b();
568
+ const n = Array.isArray(t) ? t : m(t);
569
+ let a = i;
570
+ for (const s of n)
571
571
  a = await a.getDirectoryHandle(s, { create: r });
572
572
  return a;
573
573
  }
@@ -590,14 +590,14 @@ class wt {
590
590
  * const fileHandle2 = await fs.getFileHandle(['config', 'settings.json'], true);
591
591
  * \`\`\`
592
592
  */
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)
593
+ async getFileHandle(t, r = !1, i = this.root) {
594
+ if (!i)
595
+ throw new b();
596
+ const n = m(t);
597
+ if (n.length === 0)
598
598
  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 });
599
+ const a = n.pop();
600
+ return (await this.getDirectoryHandle(n, r, i)).getFileHandle(a, { create: r });
601
601
  }
602
602
  /**
603
603
  * Get a complete index of all files and directories in the file system
@@ -619,10 +619,10 @@ class wt {
619
619
  * \`\`\`
620
620
  */
621
621
  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}\`;
622
+ const t = /* @__PURE__ */ new Map(), r = async (i) => {
623
+ const n = await this.readDir(i);
624
+ for (const a of n) {
625
+ const s = \`\${i === "/" ? "" : i}/\${a.name}\`;
626
626
  try {
627
627
  const o = await this.stat(s);
628
628
  t.set(s, o), o.isDirectory && await r(s);
@@ -643,10 +643,10 @@ class wt {
643
643
  async readFile(t, r = "utf-8") {
644
644
  await this.ensureMounted();
645
645
  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);
646
+ const i = await this.getFileHandle(t, !1), n = await ht(i);
647
+ return r === "binary" ? n : rt(n, r);
648
+ } catch (i) {
649
+ throw console.error(i), new T(t);
650
650
  }
651
651
  }
652
652
  /**
@@ -674,10 +674,10 @@ class wt {
674
674
  * await fs.writeFile('/data/utf16.txt', 'Hello World', 'utf-16le');
675
675
  * \`\`\`
676
676
  */
677
- async writeFile(t, r, n) {
677
+ async writeFile(t, r, i) {
678
678
  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 });
679
+ const n = await this.getFileHandle(t, !0);
680
+ await k(n, r, i, { truncate: !0 }), await this.notifyChange({ path: t, type: "changed", isDirectory: !1 });
681
681
  }
682
682
  /**
683
683
  * Append data to a file
@@ -701,10 +701,10 @@ class wt {
701
701
  * await fs.appendFile('/data/binary.dat', additionalData);
702
702
  * \`\`\`
703
703
  */
704
- async appendFile(t, r, n) {
704
+ async appendFile(t, r, i) {
705
705
  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 });
706
+ const n = await this.getFileHandle(t, !0);
707
+ await k(n, r, i, { append: !0 }), await this.notifyChange({ path: t, type: "changed", isDirectory: !1 });
708
708
  }
709
709
  /**
710
710
  * Create a directory
@@ -729,16 +729,16 @@ class wt {
729
729
  */
730
730
  async mkdir(t, r) {
731
731
  if (await this.ensureMounted(), !this.root)
732
- throw new C();
733
- const n = r?.recursive ?? !1, i = m(t);
732
+ throw new b();
733
+ const i = r?.recursive ?? !1, n = m(t);
734
734
  let a = this.root;
735
- for (let s = 0; s < i.length; s++) {
736
- const o = i[s];
735
+ for (let s = 0; s < n.length; s++) {
736
+ const o = n[s];
737
737
  try {
738
- a = await a.getDirectoryHandle(o, { create: n || s === i.length - 1 });
738
+ a = await a.getDirectoryHandle(o, { create: i || s === n.length - 1 });
739
739
  } catch (l) {
740
740
  throw l.name === "NotFoundError" ? new c(
741
- \`Parent directory does not exist: \${P(i.slice(0, s + 1))}\`,
741
+ \`Parent directory does not exist: \${I(n.slice(0, s + 1))}\`,
742
742
  "ENOENT"
743
743
  ) : l.name === "TypeMismatchError" ? new c(\`Path segment is not a directory: \${o}\`, "ENOTDIR") : new c("Failed to create directory", "MKDIR_FAILED");
744
744
  }
@@ -777,9 +777,9 @@ class wt {
777
777
  isFile: !1,
778
778
  isDirectory: !0
779
779
  };
780
- const r = T(t), n = await this.getDirectoryHandle(O(t), !1), i = this.options.hashAlgorithm !== null;
780
+ const r = O(t), i = await this.getDirectoryHandle(P(t), !1), n = this.options.hashAlgorithm !== null;
781
781
  try {
782
- const s = await (await n.getFileHandle(r, { create: !1 })).getFile(), o = {
782
+ const s = await (await i.getFileHandle(r, { create: !1 })).getFile(), o = {
783
783
  kind: "file",
784
784
  size: s.size,
785
785
  mtime: new Date(s.lastModified).toISOString(),
@@ -787,7 +787,7 @@ class wt {
787
787
  isFile: !0,
788
788
  isDirectory: !1
789
789
  };
790
- if (i && this.options.hashAlgorithm)
790
+ if (n && this.options.hashAlgorithm)
791
791
  try {
792
792
  const l = await ut(s, this.options.hashAlgorithm, this.options.maxFileSize);
793
793
  o.hash = l;
@@ -800,7 +800,7 @@ class wt {
800
800
  throw new c("Failed to stat (file)", "STAT_FAILED");
801
801
  }
802
802
  try {
803
- return await n.getDirectoryHandle(r, { create: !1 }), {
803
+ return await i.getDirectoryHandle(r, { create: !1 }), {
804
804
  kind: "directory",
805
805
  size: 0,
806
806
  mtime: (/* @__PURE__ */ new Date(0)).toISOString(),
@@ -812,27 +812,37 @@ class wt {
812
812
  throw a.name === "NotFoundError" ? new c(\`No such file or directory: \${t}\`, "ENOENT") : new c("Failed to stat (directory)", "STAT_FAILED");
813
813
  }
814
814
  }
815
- async readdir(t, r) {
815
+ /**
816
+ * Read a directory's contents
817
+ *
818
+ * Lists all files and subdirectories within the specified directory.
819
+ *
820
+ * @param path - The path to the directory to read
821
+ * @returns Promise that resolves to an array of detailed file/directory information
822
+ * @throws {OPFSError} If the directory does not exist or cannot be accessed
823
+ *
824
+ * @example
825
+ * \`\`\`typescript
826
+ * // Get detailed information about files and directories
827
+ * const detailed = await fs.readDir('/users/john/documents');
828
+ * detailed.forEach(item => {
829
+ * console.log(\`\${item.name} - \${item.isFile ? 'file' : 'directory'}\`);
830
+ * });
831
+ * \`\`\`
832
+ */
833
+ async readDir(t) {
816
834
  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;
835
+ const r = await this.getDirectoryHandle(t, !1), i = [];
836
+ for await (const [n, a] of r.entries()) {
837
+ const s = a.kind === "file";
838
+ i.push({
839
+ name: n,
840
+ kind: a.kind,
841
+ isFile: s,
842
+ isDirectory: !s
843
+ });
835
844
  }
845
+ return i;
836
846
  }
837
847
  /**
838
848
  * Check if a file or directory exists
@@ -851,26 +861,26 @@ class wt {
851
861
  async exists(t) {
852
862
  if (await this.ensureMounted(), t === "/")
853
863
  return !0;
854
- const r = T(t);
855
- let n = null;
864
+ const r = O(t);
865
+ let i = null;
856
866
  try {
857
- n = await this.getDirectoryHandle(O(t), !1);
858
- } catch (i) {
859
- throw (i.name === "NotFoundError" || i.name === "TypeMismatchError") && (n = null), i;
867
+ i = await this.getDirectoryHandle(P(t), !1);
868
+ } catch (n) {
869
+ throw (n.name === "NotFoundError" || n.name === "TypeMismatchError") && (i = null), n;
860
870
  }
861
- if (!n || !r)
871
+ if (!i || !r)
862
872
  return !1;
863
873
  try {
864
- return await n.getFileHandle(r, { create: !1 }), !0;
865
- } catch (i) {
866
- if (i.name !== "NotFoundError" && i.name !== "TypeMismatchError")
867
- throw i;
874
+ return await i.getFileHandle(r, { create: !1 }), !0;
875
+ } catch (n) {
876
+ if (n.name !== "NotFoundError" && n.name !== "TypeMismatchError")
877
+ throw n;
868
878
  }
869
879
  try {
870
- return await n.getDirectoryHandle(r, { create: !1 }), !0;
871
- } catch (i) {
872
- if (i.name !== "NotFoundError" && i.name !== "TypeMismatchError")
873
- throw i;
880
+ return await i.getDirectoryHandle(r, { create: !1 }), !0;
881
+ } catch (n) {
882
+ if (n.name !== "NotFoundError" && n.name !== "TypeMismatchError")
883
+ throw n;
874
884
  }
875
885
  return !1;
876
886
  }
@@ -896,10 +906,10 @@ class wt {
896
906
  async clear(t = "/") {
897
907
  await this.ensureMounted();
898
908
  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 });
909
+ const r = await this.readDir(t);
910
+ for (const i of r) {
911
+ const n = \`\${t === "/" ? "" : t}/\${i.name}\`;
912
+ await this.remove(n, { recursive: !0 });
903
913
  }
904
914
  await this.notifyChange({ path: t, type: "changed", isDirectory: !0 });
905
915
  } catch (r) {
@@ -932,20 +942,20 @@ class wt {
932
942
  */
933
943
  async remove(t, r) {
934
944
  await this.ensureMounted();
935
- const n = r?.recursive ?? !1, i = r?.force ?? !1;
945
+ const i = r?.recursive ?? !1, n = r?.force ?? !1;
936
946
  if (t === "/")
937
947
  throw new c("Cannot remove root directory", "EROOT");
938
- const a = T(t);
948
+ const a = O(t);
939
949
  if (!a)
940
950
  throw new N("Invalid path", t);
941
- const s = await this.getDirectoryHandle(O(t), !1);
951
+ const s = await this.getDirectoryHandle(P(t), !1);
942
952
  try {
943
- await s.removeEntry(a, { recursive: n });
953
+ await s.removeEntry(a, { recursive: i });
944
954
  } catch (o) {
945
955
  if (o.name === "NotFoundError") {
946
- if (!i)
956
+ if (!n)
947
957
  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");
958
+ } 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
959
  }
950
960
  await this.notifyChange({ path: t, type: "removed", isDirectory: !1 });
951
961
  }
@@ -972,7 +982,7 @@ class wt {
972
982
  try {
973
983
  const r = ct(t);
974
984
  if (!await this.exists(r))
975
- throw new b(r);
985
+ throw new T(r);
976
986
  return r;
977
987
  } catch (r) {
978
988
  throw r instanceof c ? r : new c(\`Failed to resolve path: \${t}\`, "REALPATH_FAILED");
@@ -998,10 +1008,10 @@ class wt {
998
1008
  await this.ensureMounted();
999
1009
  try {
1000
1010
  if (!await this.exists(t))
1001
- throw new b(t);
1011
+ throw new T(t);
1002
1012
  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");
1013
+ } catch (i) {
1014
+ throw i instanceof c ? i : new c(\`Failed to rename from \${t} to \${r}\`, "RENAME_FAILED");
1005
1015
  }
1006
1016
  }
1007
1017
  /**
@@ -1029,10 +1039,10 @@ class wt {
1029
1039
  * await fs.copy('/source', '/dest', { recursive: true, force: false });
1030
1040
  * \`\`\`
1031
1041
  */
1032
- async copy(t, r, n) {
1042
+ async copy(t, r, i) {
1033
1043
  await this.ensureMounted();
1034
1044
  try {
1035
- const i = n?.recursive ?? !1, a = n?.force ?? !0;
1045
+ const n = i?.recursive ?? !1, a = i?.force ?? !0;
1036
1046
  if (!await this.exists(t))
1037
1047
  throw new c(\`Source does not exist: \${t}\`, "ENOENT");
1038
1048
  if (await this.exists(r) && !a)
@@ -1041,18 +1051,18 @@ class wt {
1041
1051
  const h = await this.readFile(t, "binary");
1042
1052
  await this.writeFile(r, h);
1043
1053
  } else {
1044
- if (!i)
1054
+ if (!n)
1045
1055
  throw new c(\`Cannot copy directory without recursive option: \${t}\`, "EISDIR");
1046
1056
  await this.mkdir(r, { recursive: !0 });
1047
- const h = await this.readdir(t, { withFileTypes: !0 });
1057
+ const h = await this.readDir(t);
1048
1058
  for (const u of h) {
1049
1059
  const w = \`\${t}/\${u.name}\`, f = \`\${r}/\${u.name}\`;
1050
1060
  await this.copy(w, f, { recursive: !0, force: a });
1051
1061
  }
1052
1062
  }
1053
1063
  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");
1064
+ } catch (n) {
1065
+ throw n instanceof c ? n : new c(\`Failed to copy from \${t} to \${r}\`, "CP_FAILED");
1056
1066
  }
1057
1067
  }
1058
1068
  /**
@@ -1060,8 +1070,8 @@ class wt {
1060
1070
  */
1061
1071
  async watch(t) {
1062
1072
  await this.ensureMounted();
1063
- const r = M(t), n = await this.buildSnapshot(r);
1064
- this.watchers.set(r, n), this.watchTimer || (this.watchTimer = setInterval(() => {
1073
+ const r = F(t), i = await this.buildSnapshot(r);
1074
+ this.watchers.set(r, i), this.watchTimer || (this.watchTimer = setInterval(() => {
1065
1075
  this.scanWatches();
1066
1076
  }, this.options.watchInterval));
1067
1077
  }
@@ -1069,7 +1079,7 @@ class wt {
1069
1079
  * Stop watching a previously watched path
1070
1080
  */
1071
1081
  unwatch(t) {
1072
- const r = M(t);
1082
+ const r = F(t);
1073
1083
  this.watchers.delete(r), this.watchers.size === 0 && this.watchTimer && (clearInterval(this.watchTimer), this.watchTimer = null);
1074
1084
  }
1075
1085
  /**
@@ -1082,17 +1092,17 @@ class wt {
1082
1092
  this.broadcastChannel && (this.broadcastChannel.close(), this.broadcastChannel = null), this.watchTimer && (clearInterval(this.watchTimer), this.watchTimer = null), this.watchers.clear();
1083
1093
  }
1084
1094
  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 });
1095
+ const r = /* @__PURE__ */ new Map(), i = async (n) => {
1096
+ const a = await this.stat(n);
1097
+ if (r.set(n, a), a.isDirectory) {
1098
+ const s = await this.readDir(n);
1089
1099
  for (const o of s) {
1090
- const l = \`\${i === "/" ? "" : i}/\${o.name}\`;
1091
- await n(l);
1100
+ const l = \`\${n === "/" ? "" : n}/\${o.name}\`;
1101
+ await i(l);
1092
1102
  }
1093
1103
  }
1094
1104
  };
1095
- return await n(t), r;
1105
+ return await i(t), r;
1096
1106
  }
1097
1107
  async scanWatches() {
1098
1108
  if (!this.scanning) {
@@ -1100,22 +1110,22 @@ class wt {
1100
1110
  try {
1101
1111
  await Promise.all(
1102
1112
  [...this.watchers.entries()].map(async ([t, r]) => {
1103
- let n;
1113
+ let i;
1104
1114
  try {
1105
- n = await this.buildSnapshot(t);
1115
+ i = await this.buildSnapshot(t);
1106
1116
  } catch {
1107
- n = /* @__PURE__ */ new Map();
1117
+ i = /* @__PURE__ */ new Map();
1108
1118
  }
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 });
1119
+ for (const [n, a] of i) {
1120
+ const s = r.get(n);
1121
+ 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
1122
  }
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 });
1123
+ for (const n of r.keys())
1124
+ if (!i.has(n)) {
1125
+ const a = r.get(n);
1126
+ await this.notifyChange({ path: n, type: "removed", isDirectory: a?.isDirectory ?? !1 });
1117
1127
  }
1118
- this.watchers.set(t, n);
1128
+ this.watchers.set(t, i);
1119
1129
  })
1120
1130
  );
1121
1131
  } finally {
@@ -1155,18 +1165,18 @@ class wt {
1155
1165
  await this.ensureMounted();
1156
1166
  try {
1157
1167
  (r?.cleanBefore ?? !1) && await this.clear("/");
1158
- for (const [i, a] of t) {
1159
- const s = M(i);
1168
+ for (const [n, a] of t) {
1169
+ const s = F(n);
1160
1170
  let o;
1161
1171
  a instanceof Blob ? o = await ft(a) : o = a, await this.writeFile(s, o);
1162
1172
  }
1163
1173
  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");
1174
+ } catch (i) {
1175
+ throw i instanceof c ? i : new c("Failed to sync file system", "SYNC_FAILED");
1166
1176
  }
1167
1177
  }
1168
1178
  }
1169
- typeof self < "u" && self.constructor.name === "DedicatedWorkerGlobalScope" && I(new wt());
1170
- //# sourceMappingURL=worker-DvdTp4yb.js.map
1179
+ typeof self < "u" && self.constructor.name === "DedicatedWorkerGlobalScope" && x(new wt());
1180
+ //# sourceMappingURL=worker-DilNsKoO.js.map
1171
1181
  `,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
1182
  //# sourceMappingURL=index.cjs.map