opfs-worker 0.2.4 → 0.2.5

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,258 +1,258 @@
1
1
  import { wrap as i, proxy as o } from "comlink";
2
- import { D as d, F as u, O as p, b as w, a as y, P as m, c as g, S as v, T as E, e as S, k as F, d as b, l as P, h as x, o as T, f as I, m as A, g as O, j as _, n as C, i as D, r as k, s as U, w as R } from "./helpers-Co-qCBmA.js";
2
+ import { D as d, F as p, O as u, b as y, a as w, P as m, c as g, S as v, T as E, e as F, k as S, d as b, l as P, h as T, o as x, f as A, m as O, g as D, j as _, n as I, i as C, r as k, s as U, w as R } from "./helpers-3TGPHd1h.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"), I = Symbol("Comlink.finalizer"), p = Symbol("Comlink.thrown"), $ = (t) => typeof t == "object" && t !== null || typeof t == "function", W = {
9
- canHandle: (t) => $(t) && t[H],
10
- serialize(t) {
11
- const { port1: e, port2: r } = new MessageChannel();
12
- return O(t, e), [r, [r]];
8
+ const H = Symbol("Comlink.proxy"), R = Symbol("Comlink.endpoint"), B = Symbol("Comlink.releaseProxy"), D = Symbol("Comlink.finalizer"), p = Symbol("Comlink.thrown"), $ = (e) => typeof e == "object" && e !== null || typeof e == "function", W = {
9
+ canHandle: (e) => $(e) && e[H],
10
+ serialize(e) {
11
+ const { port1: t, port2: r } = new MessageChannel();
12
+ return P(e, t), [r, [r]];
13
13
  },
14
- deserialize(t) {
15
- return t.start(), Y(t);
14
+ deserialize(e) {
15
+ return e.start(), Y(e);
16
16
  }
17
17
  }, j = {
18
- canHandle: (t) => $(t) && p in t,
19
- serialize({ value: t }) {
20
- let e;
21
- return t instanceof Error ? e = {
18
+ canHandle: (e) => $(e) && p in e,
19
+ serialize({ value: e }) {
20
+ let t;
21
+ return e instanceof Error ? t = {
22
22
  isError: !0,
23
23
  value: {
24
- message: t.message,
25
- name: t.name,
26
- stack: t.stack
24
+ message: e.message,
25
+ name: e.name,
26
+ stack: e.stack
27
27
  }
28
- } : e = { isError: !1, value: t }, [e, []];
28
+ } : t = { isError: !1, value: e }, [t, []];
29
29
  },
30
- deserialize(t) {
31
- throw t.isError ? Object.assign(new Error(t.value.message), t.value) : t.value;
30
+ deserialize(e) {
31
+ throw e.isError ? Object.assign(new Error(e.value.message), e.value) : e.value;
32
32
  }
33
33
  }, U = /* @__PURE__ */ new Map([
34
34
  ["proxy", W],
35
35
  ["throw", j]
36
36
  ]);
37
- function V(t, e) {
38
- for (const r of t)
39
- if (e === r || r === "*" || r instanceof RegExp && r.test(e))
37
+ function V(e, t) {
38
+ for (const r of e)
39
+ if (t === r || r === "*" || r instanceof RegExp && r.test(t))
40
40
  return !0;
41
41
  return !1;
42
42
  }
43
- function O(t, e = globalThis, r = ["*"]) {
44
- e.addEventListener("message", function a(n) {
45
- if (!n || !n.data)
43
+ function P(e, t = globalThis, r = ["*"]) {
44
+ t.addEventListener("message", function n(i) {
45
+ if (!i || !i.data)
46
46
  return;
47
- if (!V(r, n.origin)) {
48
- console.warn(\`Invalid origin '\${n.origin}' for comlink proxy\`);
47
+ if (!V(r, i.origin)) {
48
+ console.warn(\`Invalid origin '\${i.origin}' for comlink proxy\`);
49
49
  return;
50
50
  }
51
- const { id: i, type: o, path: s } = Object.assign({ path: [] }, n.data), l = (n.data.argumentList || []).map(y);
52
- let u;
51
+ const { id: a, type: s, path: o } = Object.assign({ path: [] }, i.data), l = (i.data.argumentList || []).map(y);
52
+ let h;
53
53
  try {
54
- const f = s.slice(0, -1).reduce((h, g) => h[g], t), w = s.reduce((h, g) => h[g], t);
55
- switch (o) {
54
+ const u = o.slice(0, -1).reduce((f, g) => f[g], e), w = o.reduce((f, g) => f[g], e);
55
+ switch (s) {
56
56
  case "GET":
57
- u = w;
57
+ h = w;
58
58
  break;
59
59
  case "SET":
60
- f[s.slice(-1)[0]] = y(n.data.value), u = !0;
60
+ u[o.slice(-1)[0]] = y(i.data.value), h = !0;
61
61
  break;
62
62
  case "APPLY":
63
- u = w.apply(f, l);
63
+ h = w.apply(u, l);
64
64
  break;
65
65
  case "CONSTRUCT":
66
66
  {
67
- const h = new w(...l);
68
- u = Q(h);
67
+ const f = new w(...l);
68
+ h = Q(f);
69
69
  }
70
70
  break;
71
71
  case "ENDPOINT":
72
72
  {
73
- const { port1: h, port2: g } = new MessageChannel();
74
- O(t, g), u = J(h, [h]);
73
+ const { port1: f, port2: g } = new MessageChannel();
74
+ P(e, g), h = J(f, [f]);
75
75
  }
76
76
  break;
77
77
  case "RELEASE":
78
- u = void 0;
78
+ h = void 0;
79
79
  break;
80
80
  default:
81
81
  return;
82
82
  }
83
- } catch (f) {
84
- u = { value: f, [p]: 0 };
83
+ } catch (u) {
84
+ h = { value: u, [p]: 0 };
85
85
  }
86
- Promise.resolve(u).catch((f) => ({ value: f, [p]: 0 })).then((f) => {
87
- const [w, h] = A(f);
88
- e.postMessage(Object.assign(Object.assign({}, w), { id: i }), h), o === "RELEASE" && (e.removeEventListener("message", a), L(e), I in t && typeof t[I] == "function" && t[I]());
89
- }).catch((f) => {
90
- const [w, h] = 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", n), L(t), D in e && typeof e[D] == "function" && e[D]());
89
+ }).catch((u) => {
90
+ const [w, f] = A({
91
91
  value: new TypeError("Unserializable return value"),
92
92
  [p]: 0
93
93
  });
94
- e.postMessage(Object.assign(Object.assign({}, w), { id: i }), h);
94
+ t.postMessage(Object.assign(Object.assign({}, w), { id: a }), f);
95
95
  });
96
- }), e.start && e.start();
96
+ }), t.start && t.start();
97
97
  }
98
- function G(t) {
99
- return t.constructor.name === "MessagePort";
98
+ function G(e) {
99
+ return e.constructor.name === "MessagePort";
100
100
  }
101
- function L(t) {
102
- G(t) && t.close();
101
+ function L(e) {
102
+ G(e) && e.close();
103
103
  }
104
- function Y(t, e) {
104
+ function Y(e, t) {
105
105
  const r = /* @__PURE__ */ new Map();
106
- return t.addEventListener("message", function(n) {
107
- const { data: i } = n;
108
- if (!i || !i.id)
106
+ return e.addEventListener("message", function(i) {
107
+ const { data: a } = i;
108
+ if (!a || !a.id)
109
109
  return;
110
- const o = r.get(i.id);
111
- if (o)
110
+ const s = r.get(a.id);
111
+ if (s)
112
112
  try {
113
- o(i);
113
+ s(a);
114
114
  } finally {
115
- r.delete(i.id);
115
+ r.delete(a.id);
116
116
  }
117
- }), P(t, r, [], e);
117
+ }), I(e, r, [], t);
118
118
  }
119
- function E(t) {
120
- if (t)
119
+ function E(e) {
120
+ if (e)
121
121
  throw new Error("Proxy has been released and is not useable");
122
122
  }
123
- function z(t) {
124
- return d(t, /* @__PURE__ */ new Map(), {
123
+ function z(e) {
124
+ return d(e, /* @__PURE__ */ new Map(), {
125
125
  type: "RELEASE"
126
126
  }).then(() => {
127
- L(t);
127
+ L(e);
128
128
  });
129
129
  }
130
- const F = /* @__PURE__ */ new WeakMap(), S = "FinalizationRegistry" in globalThis && new FinalizationRegistry((t) => {
131
- const e = (F.get(t) || 0) - 1;
132
- F.set(t, e), e === 0 && z(t);
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 && z(e);
133
133
  });
134
- function X(t, e) {
135
- const r = (F.get(e) || 0) + 1;
136
- F.set(e, r), S && S.register(t, e, t);
134
+ function X(e, t) {
135
+ const r = (F.get(t) || 0) + 1;
136
+ F.set(t, r), S && S.register(e, t, e);
137
137
  }
138
- function q(t) {
139
- S && S.unregister(t);
138
+ function q(e) {
139
+ S && S.unregister(e);
140
140
  }
141
- function P(t, e, r = [], a = function() {
141
+ function I(e, t, r = [], n = function() {
142
142
  }) {
143
- let n = !1;
144
- const i = new Proxy(a, {
145
- get(o, s) {
146
- if (E(n), s === B)
143
+ let i = !1;
144
+ const a = new Proxy(n, {
145
+ get(s, o) {
146
+ if (E(i), o === B)
147
147
  return () => {
148
- q(i), z(t), e.clear(), n = !0;
148
+ q(a), z(e), t.clear(), i = !0;
149
149
  };
150
- if (s === "then") {
150
+ if (o === "then") {
151
151
  if (r.length === 0)
152
- return { then: () => i };
153
- const l = d(t, e, {
152
+ return { then: () => a };
153
+ const l = d(e, t, {
154
154
  type: "GET",
155
- path: r.map((u) => u.toString())
155
+ path: r.map((h) => h.toString())
156
156
  }).then(y);
157
157
  return l.then.bind(l);
158
158
  }
159
- return P(t, e, [...r, s]);
159
+ return I(e, t, [...r, o]);
160
160
  },
161
- set(o, s, l) {
162
- E(n);
163
- const [u, f] = A(l);
164
- return d(t, e, {
161
+ set(s, o, l) {
162
+ E(i);
163
+ const [h, u] = A(l);
164
+ return d(e, t, {
165
165
  type: "SET",
166
- path: [...r, s].map((w) => w.toString()),
167
- value: u
168
- }, f).then(y);
166
+ path: [...r, o].map((w) => w.toString()),
167
+ value: h
168
+ }, u).then(y);
169
169
  },
170
- apply(o, s, l) {
171
- E(n);
172
- const u = r[r.length - 1];
173
- if (u === R)
174
- return d(t, e, {
170
+ apply(s, o, l) {
171
+ E(i);
172
+ const h = r[r.length - 1];
173
+ if (h === R)
174
+ return d(e, t, {
175
175
  type: "ENDPOINT"
176
176
  }).then(y);
177
- if (u === "bind")
178
- return P(t, e, r.slice(0, -1));
179
- const [f, w] = k(l);
180
- return d(t, e, {
177
+ if (h === "bind")
178
+ return I(e, t, r.slice(0, -1));
179
+ const [u, w] = N(l);
180
+ return d(e, t, {
181
181
  type: "APPLY",
182
- path: r.map((h) => h.toString()),
183
- argumentList: f
182
+ path: r.map((f) => f.toString()),
183
+ argumentList: u
184
184
  }, w).then(y);
185
185
  },
186
- construct(o, s) {
187
- E(n);
188
- const [l, u] = k(s);
189
- return d(t, e, {
186
+ construct(s, o) {
187
+ E(i);
188
+ const [l, h] = N(o);
189
+ return d(e, t, {
190
190
  type: "CONSTRUCT",
191
- path: r.map((f) => f.toString()),
191
+ path: r.map((u) => u.toString()),
192
192
  argumentList: l
193
- }, u).then(y);
193
+ }, h).then(y);
194
194
  }
195
195
  });
196
- return X(i, t), i;
196
+ return X(a, e), a;
197
197
  }
198
- function K(t) {
199
- return Array.prototype.concat.apply([], t);
198
+ function K(e) {
199
+ return Array.prototype.concat.apply([], e);
200
200
  }
201
- function k(t) {
202
- const e = t.map(A);
203
- return [e.map((r) => r[0]), K(e.map((r) => r[1]))];
201
+ function N(e) {
202
+ const t = e.map(A);
203
+ return [t.map((r) => r[0]), K(t.map((r) => r[1]))];
204
204
  }
205
205
  const _ = /* @__PURE__ */ new WeakMap();
206
- function J(t, e) {
207
- return _.set(t, e), t;
206
+ function J(e, t) {
207
+ return _.set(e, t), e;
208
208
  }
209
- function Q(t) {
210
- return Object.assign(t, { [H]: !0 });
209
+ function Q(e) {
210
+ return Object.assign(e, { [H]: !0 });
211
211
  }
212
- function A(t) {
213
- for (const [e, r] of U)
214
- if (r.canHandle(t)) {
215
- const [a, n] = r.serialize(t);
212
+ function A(e) {
213
+ for (const [t, r] of U)
214
+ if (r.canHandle(e)) {
215
+ const [n, i] = r.serialize(e);
216
216
  return [
217
217
  {
218
218
  type: "HANDLER",
219
- name: e,
220
- value: a
219
+ name: t,
220
+ value: n
221
221
  },
222
- n
222
+ i
223
223
  ];
224
224
  }
225
225
  return [
226
226
  {
227
227
  type: "RAW",
228
- value: t
228
+ value: e
229
229
  },
230
- _.get(t) || []
230
+ _.get(e) || []
231
231
  ];
232
232
  }
233
- function y(t) {
234
- switch (t.type) {
233
+ function y(e) {
234
+ switch (e.type) {
235
235
  case "HANDLER":
236
- return U.get(t.name).deserialize(t.value);
236
+ return U.get(e.name).deserialize(e.value);
237
237
  case "RAW":
238
- return t.value;
238
+ return e.value;
239
239
  }
240
240
  }
241
- function d(t, e, r, a) {
242
- return new Promise((n) => {
243
- const i = Z();
244
- e.set(i, n), t.start && t.start(), t.postMessage(Object.assign({ id: i }, r), a);
241
+ function d(e, t, r, n) {
242
+ return new Promise((i) => {
243
+ const a = Z();
244
+ t.set(a, i), e.start && e.start(), e.postMessage(Object.assign({ id: a }, r), n);
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(e, r, a) {
252
- super(e), this.code = r, this.path = a, this.name = "OPFSError";
251
+ constructor(t, r, n) {
252
+ super(t), this.code = r, this.path = n, this.name = "OPFSError";
253
253
  }
254
254
  }
255
- class ee extends c {
255
+ class tt extends c {
256
256
  constructor() {
257
257
  super("OPFS is not supported in this browser", "OPFS_NOT_SUPPORTED");
258
258
  }
@@ -262,165 +262,166 @@ class T extends c {
262
262
  super("OPFS is not mounted", "OPFS_NOT_MOUNTED");
263
263
  }
264
264
  }
265
- class v extends c {
266
- constructor(e, r) {
267
- super(e, "INVALID_PATH", r);
265
+ class k extends c {
266
+ constructor(t, r) {
267
+ super(t, "INVALID_PATH", r);
268
268
  }
269
269
  }
270
- class D extends c {
271
- constructor(e) {
272
- super(\`File not found: \${e}\`, "FILE_NOT_FOUND", e);
270
+ class O extends c {
271
+ constructor(t) {
272
+ super(\`File not found: \${t}\`, "FILE_NOT_FOUND", t);
273
273
  }
274
274
  }
275
- function te(t, e = "utf-8") {
276
- switch (e) {
275
+ function et(e, t = "utf-8") {
276
+ switch (t) {
277
277
  case "utf8":
278
278
  case "utf-8":
279
- return new TextEncoder().encode(t);
279
+ return new TextEncoder().encode(e);
280
280
  case "utf16le":
281
281
  case "ucs2":
282
282
  case "ucs-2":
283
- return ne(t);
283
+ return nt(e);
284
284
  case "ascii":
285
- return se(t);
285
+ return st(e);
286
286
  case "latin1":
287
- return ie(t);
287
+ return at(e);
288
288
  case "binary":
289
- return Uint8Array.from(t, (r) => r.charCodeAt(0));
289
+ return Uint8Array.from(e, (r) => r.charCodeAt(0));
290
290
  case "base64":
291
- return Uint8Array.from(atob(t), (r) => r.charCodeAt(0));
291
+ return Uint8Array.from(atob(e), (r) => r.charCodeAt(0));
292
292
  case "hex":
293
- if (!/^[\\da-f]+$/i.test(t) || t.length % 2 !== 0)
293
+ if (!/^[\\da-f]+$/i.test(e) || e.length % 2 !== 0)
294
294
  throw new c("Invalid hex string", "INVALID_HEX_FORMAT");
295
- return Uint8Array.from(t.match(/.{1,2}/g).map((r) => parseInt(r, 16)));
295
+ return Uint8Array.from(e.match(/.{1,2}/g).map((r) => parseInt(r, 16)));
296
296
  default:
297
- return console.warn("Encoding not supported, falling back to UTF-8"), new TextEncoder().encode(t);
297
+ return console.warn("Encoding not supported, falling back to UTF-8"), new TextEncoder().encode(e);
298
298
  }
299
299
  }
300
- function re(t, e = "utf-8") {
301
- switch (e) {
300
+ function rt(e, t = "utf-8") {
301
+ switch (t) {
302
302
  case "utf8":
303
303
  case "utf-8":
304
- return new TextDecoder().decode(t);
304
+ return new TextDecoder().decode(e);
305
305
  case "utf16le":
306
306
  case "ucs2":
307
307
  case "ucs-2":
308
- return ae(t);
308
+ return it(e);
309
309
  case "latin1":
310
- return String.fromCharCode(...t);
310
+ return String.fromCharCode(...e);
311
311
  case "binary":
312
- return String.fromCharCode(...t);
312
+ return String.fromCharCode(...e);
313
313
  case "ascii":
314
- return String.fromCharCode(...t.map((r) => r & 127));
314
+ return String.fromCharCode(...e.map((r) => r & 127));
315
315
  case "base64":
316
- return btoa(String.fromCharCode(...t));
316
+ return btoa(String.fromCharCode(...e));
317
317
  case "hex":
318
- return Array.from(t).map((r) => r.toString(16).padStart(2, "0")).join("");
318
+ return Array.from(e).map((r) => r.toString(16).padStart(2, "0")).join("");
319
319
  default:
320
- return console.warn("Unsupported encoding, falling back to UTF-8"), new TextDecoder().decode(t);
320
+ return console.warn("Unsupported encoding, falling back to UTF-8"), new TextDecoder().decode(e);
321
321
  }
322
322
  }
323
- function ne(t) {
324
- const e = new Uint8Array(t.length * 2);
325
- for (let r = 0; r < t.length; r++) {
326
- const a = t.charCodeAt(r);
327
- e[r * 2] = a & 255, e[r * 2 + 1] = a >> 8;
323
+ function nt(e) {
324
+ const t = new Uint8Array(e.length * 2);
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;
328
328
  }
329
- return e;
329
+ return t;
330
330
  }
331
- function ae(t) {
332
- t.length % 2 !== 0 && (console.warn("Invalid UTF-16LE buffer length, truncating last byte"), t = t.slice(0, t.length - 1));
333
- const e = new Uint16Array(t.buffer, t.byteOffset, t.byteLength / 2);
334
- return String.fromCharCode(...e);
331
+ function it(e) {
332
+ e.length % 2 !== 0 && (console.warn("Invalid UTF-16LE buffer length, truncating last byte"), e = e.slice(0, e.length - 1));
333
+ const t = new Uint16Array(e.buffer, e.byteOffset, e.byteLength / 2);
334
+ return String.fromCharCode(...t);
335
335
  }
336
- function ie(t) {
337
- const e = new Uint8Array(t.length);
338
- for (let r = 0; r < t.length; r++)
339
- e[r] = t.charCodeAt(r) & 255;
340
- return e;
336
+ function at(e) {
337
+ const t = new Uint8Array(e.length);
338
+ for (let r = 0; r < e.length; r++)
339
+ t[r] = e.charCodeAt(r) & 255;
340
+ return t;
341
341
  }
342
- function se(t) {
343
- const e = new Uint8Array(t.length);
344
- for (let r = 0; r < t.length; r++)
345
- e[r] = t.charCodeAt(r) & 127;
346
- return e;
342
+ function st(e) {
343
+ const t = new Uint8Array(e.length);
344
+ for (let r = 0; r < e.length; r++)
345
+ t[r] = e.charCodeAt(r) & 127;
346
+ return t;
347
347
  }
348
- function oe() {
348
+ function ot() {
349
349
  if (!("storage" in navigator) || !("getDirectory" in navigator.storage))
350
- throw new ee();
350
+ throw new tt();
351
351
  }
352
- function m(t) {
353
- return Array.isArray(t) ? t : t.split("/").filter(Boolean);
352
+ function m(e) {
353
+ return Array.isArray(e) ? e : e.split("/").filter(Boolean);
354
354
  }
355
- function x(t) {
356
- return typeof t == "string" ? t ?? "/" : \`/\${t.join("/")}\`;
355
+ function x(e) {
356
+ return typeof e == "string" ? e ?? "/" : \`/\${e.join("/")}\`;
357
357
  }
358
- function b(t) {
359
- const e = m(t);
360
- return e[e.length - 1] || "";
358
+ function C(e) {
359
+ const t = m(e);
360
+ return t[t.length - 1] || "";
361
361
  }
362
- function M(t) {
363
- const e = m(t);
364
- return e.pop(), x(e);
362
+ function M(e) {
363
+ const t = m(e);
364
+ return t.pop(), x(t);
365
365
  }
366
- function C(t) {
367
- return !t || t === "/" ? "/" : t.startsWith("/") ? t : \`/\${t}\`;
366
+ function b(e) {
367
+ return !e || e === "/" ? "/" : e.startsWith("/") ? e : \`/\${e}\`;
368
368
  }
369
- function ce(t) {
370
- const e = m(t), r = [];
371
- for (const a of e)
372
- if (!(a === "." || a === ""))
373
- if (a === "..") {
369
+ function ct(e) {
370
+ const t = m(e), r = [];
371
+ for (const n of t)
372
+ if (!(n === "." || n === ""))
373
+ if (n === "..") {
374
374
  if (r.length === 0)
375
375
  continue;
376
376
  r.pop();
377
377
  } else
378
- r.push(a);
378
+ r.push(n);
379
379
  return x(r);
380
380
  }
381
- function le(t, e = "utf-8") {
382
- return typeof t == "string" ? te(t, e) : t instanceof Uint8Array ? t : new Uint8Array(t);
381
+ function lt(e, t = "utf-8") {
382
+ return typeof e == "string" ? et(e, t) : e instanceof Uint8Array ? e : new Uint8Array(e);
383
383
  }
384
- async function ue(t) {
385
- const e = await t.createSyncAccessHandle();
384
+ async function ht(e) {
385
+ const t = await e.createSyncAccessHandle();
386
386
  try {
387
- const r = e.getSize(), a = new Uint8Array(r);
388
- return e.read(a, { at: 0 }), a;
387
+ const r = t.getSize(), n = new Uint8Array(r);
388
+ return t.read(n, { at: 0 }), n;
389
389
  } finally {
390
- e.close();
390
+ t.close();
391
391
  }
392
392
  }
393
- async function N(t, e, r, a = {}) {
394
- let n = null;
393
+ async function v(e, t, r, n = {}) {
394
+ let i = null;
395
395
  try {
396
- n = await t.createSyncAccessHandle();
397
- const i = le(e, r), o = a.append ? n.getSize() : 0;
398
- n.write(i, { at: o }), a.truncate && !a.append && n.truncate(i.byteLength), n.flush();
399
- } catch (i) {
400
- console.error(i);
401
- const o = a.append ? "append" : "write";
402
- throw new c(\`Failed to \${o} file\`, \`\${o.toUpperCase()}_FAILED\`);
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();
399
+ } catch (a) {
400
+ console.error(a);
401
+ const s = n.append ? "append" : "write";
402
+ throw new c(\`Failed to \${s} file\`, \`\${s.toUpperCase()}_FAILED\`);
403
403
  } finally {
404
- if (n)
404
+ if (i)
405
405
  try {
406
- n.close();
406
+ i.close();
407
407
  } catch {
408
408
  }
409
409
  }
410
410
  }
411
- async function fe(t, e = "SHA-1") {
411
+ async function ut(e, t = "SHA-1") {
412
+ e instanceof File && (e = await e.arrayBuffer());
412
413
  try {
413
- const r = new Uint8Array(t), a = await crypto.subtle.digest(e, r);
414
- return Array.from(new Uint8Array(a)).map((i) => i.toString(16).padStart(2, "0")).join("");
414
+ const r = new Uint8Array(e), n = await crypto.subtle.digest(t, r);
415
+ return Array.from(new Uint8Array(n)).map((a) => a.toString(16).padStart(2, "0")).join("");
415
416
  } catch (r) {
416
- throw console.warn(\`Failed to calculate \${e} hash:\`, r), r;
417
+ throw console.warn(\`Failed to calculate \${t} hash:\`, r), r;
417
418
  }
418
419
  }
419
- async function he(t) {
420
- const e = await t.arrayBuffer();
421
- return new Uint8Array(e);
420
+ async function ft(e) {
421
+ const t = await e.arrayBuffer();
422
+ return new Uint8Array(t);
422
423
  }
423
- class we {
424
+ class wt {
424
425
  /** Root directory handle for the file system */
425
426
  root = null;
426
427
  /** Watch event callback */
@@ -435,6 +436,8 @@ class we {
435
436
  scanning = !1;
436
437
  /** Promise to prevent concurrent mount operations */
437
438
  mountingPromise = null;
439
+ /** Hash algorithm for file hashing (null means no hashing) */
440
+ hashAlgorithm = null;
438
441
  /**
439
442
  * Notify about internal changes to the file system
440
443
  *
@@ -444,21 +447,34 @@ class we {
444
447
  * @param path - The path that was changed
445
448
  * @param type - The type of change (create, change, delete)
446
449
  */
447
- notifyInternalChange(e, r) {
448
- this.watchCallback && this.watchCallback({
449
- path: e,
450
- type: r
450
+ async notifyChange(t) {
451
+ if (!this.watchCallback)
452
+ return;
453
+ let r;
454
+ if (this.hashAlgorithm && !t.isDirectory && t.type !== "removed")
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);
460
+ }
461
+ this.watchCallback({
462
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
463
+ ...t,
464
+ ...r && { hash: r }
451
465
  });
452
466
  }
453
467
  /**
454
468
  * Creates a new OPFSFileSystem instance
455
469
  *
456
470
  * @param watchCallback - Optional callback for file change events
457
- * @param watchOptions - Optional configuration for watching
471
+ * @param options - Optional configuration options
472
+ * @param options.watchInterval - Polling interval in milliseconds for file watching
473
+ * @param options.hashAlgorithm - Hash algorithm for file hashing
458
474
  * @throws {OPFSError} If OPFS is not supported in the current browser
459
475
  */
460
- constructor(e, r) {
461
- oe(), e && (this.watchCallback = e, r?.watchInterval && (this.watchInterval = r.watchInterval)), this.mount("/");
476
+ constructor(t, r) {
477
+ ot(), t && (this.watchCallback = t), r && this.setOptions(r), this.mount("/");
462
478
  }
463
479
  /**
464
480
  * Initialize the file system within a given directory
@@ -481,14 +497,14 @@ class we {
481
497
  * await fs.mount('/my-app');
482
498
  * \`\`\`
483
499
  */
484
- async mount(e = "/") {
485
- return this.mountingPromise && await this.mountingPromise, this.mountingPromise = new Promise(async (r, a) => {
500
+ async mount(t = "/") {
501
+ return this.mountingPromise && await this.mountingPromise, this.mountingPromise = new Promise(async (r, n) => {
486
502
  this.root = null;
487
503
  try {
488
- const n = await navigator.storage.getDirectory();
489
- e === "/" ? this.root = n : this.root = await this.getDirectoryHandle(e, !0, n), r(!0);
490
- } catch (n) {
491
- console.error(n), a(new c("Failed to initialize OPFS", "INIT_FAILED"));
504
+ const i = await navigator.storage.getDirectory();
505
+ t === "/" ? this.root = i : this.root = await this.getDirectoryHandle(t, !0, i), r(!0);
506
+ } catch (i) {
507
+ console.error(i), n(new c("Failed to initialize OPFS", "INIT_FAILED"));
492
508
  } finally {
493
509
  this.mountingPromise = null;
494
510
  }
@@ -498,10 +514,19 @@ class we {
498
514
  * Set the watch callback for file change events
499
515
  *
500
516
  * @param callback - The callback function to invoke when files change
501
- * @param options - Optional configuration for watching
502
517
  */
503
- setWatchCallback(e, r) {
504
- this.watchCallback = e, r?.watchInterval && (this.watchInterval = r.watchInterval);
518
+ setWatchCallback(t) {
519
+ this.watchCallback = t;
520
+ }
521
+ /**
522
+ * Set configuration options for the file system
523
+ *
524
+ * @param options - Configuration options to update
525
+ * @param options.watchInterval - Polling interval in milliseconds for file watching
526
+ * @param options.hashAlgorithm - Hash algorithm for file hashing
527
+ */
528
+ setOptions(t) {
529
+ t.watchInterval !== void 0 && (this.watchInterval = t.watchInterval), t.hashAlgorithm !== void 0 && (this.hashAlgorithm = t.hashAlgorithm);
505
530
  }
506
531
  /**
507
532
  * Automatically mount the OPFS root if not already mounted
@@ -539,14 +564,14 @@ class we {
539
564
  * const docsDir2 = await fs.getDirectoryHandle(['users', 'john', 'documents'], true);
540
565
  * \`\`\`
541
566
  */
542
- async getDirectoryHandle(e, r = !1, a = this.root) {
543
- if (!a)
567
+ async getDirectoryHandle(t, r = !1, n = this.root) {
568
+ if (!n)
544
569
  throw new T();
545
- const n = Array.isArray(e) ? e : m(e);
546
- let i = a;
547
- for (const o of n)
548
- i = await i.getDirectoryHandle(o, { create: r });
549
- return i;
570
+ const i = Array.isArray(t) ? t : m(t);
571
+ let a = n;
572
+ for (const s of i)
573
+ a = await a.getDirectoryHandle(s, { create: r });
574
+ return a;
550
575
  }
551
576
  /**
552
577
  * Get a file handle from a path
@@ -567,45 +592,27 @@ class we {
567
592
  * const fileHandle2 = await fs.getFileHandle(['config', 'settings.json'], true);
568
593
  * \`\`\`
569
594
  */
570
- async getFileHandle(e, r = !1, a = this.root) {
571
- if (!a)
595
+ async getFileHandle(t, r = !1, n = this.root) {
596
+ if (!n)
572
597
  throw new T();
573
- const n = m(e);
574
- if (n.length === 0)
575
- throw new v("Path must not be empty", Array.isArray(e) ? e.join("/") : e);
576
- const i = n.pop();
577
- return (await this.getDirectoryHandle(n, r, a)).getFileHandle(i, { create: r });
598
+ const i = m(t);
599
+ if (i.length === 0)
600
+ throw new k("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 });
578
603
  }
579
604
  /**
580
- * Recursively list all files and directories with their stats
605
+ * Get a complete index of all files and directories in the file system
581
606
  *
582
- * Traverses the entire file system starting from the root and returns
583
- * a Map containing all paths and their corresponding file statistics.
607
+ * This method recursively traverses the entire file system and returns
608
+ * a Map containing FileStat objects for every file and directory.
584
609
  *
585
- * @param options - Options for indexing
586
- * @param options.includeHash - Whether to calculate file hash (default: false)
587
- * @param options.hashAlgorithm - Hash algorithm to use (default: 'SHA-1', fastest)
588
- * @returns Promise that resolves to a Map of path => FileStat
589
- * @throws {OPFSError} If the indexing operation fails
610
+ * @returns Promise that resolves to a Map of paths to FileStat objects
611
+ * @throws {OPFSError} If the file system is not mounted
590
612
  *
591
613
  * @example
592
614
  * \`\`\`typescript
593
- * // Basic index without hash
594
615
  * const index = await fs.index();
595
- *
596
- * // Index with file hash
597
- * const indexWithHash = await fs.index({
598
- * includeHash: true,
599
- * hashAlgorithm: 'SHA-1'
600
- * });
601
- *
602
- * // Iterate through all files and directories
603
- * for (const [path, stat] of index) {
604
- * console.log(\`\${path}: \${stat.isFile ? 'file' : 'directory'} (\${stat.size} bytes)\`);
605
- * if (stat.hash) console.log(\` Hash: \${stat.hash}\`);
606
- * }
607
- *
608
- * // Get specific file stats
609
616
  * const fileStats = index.get('/data/config.json');
610
617
  * if (fileStats) {
611
618
  * console.log(\`File size: \${fileStats.size} bytes\`);
@@ -613,35 +620,35 @@ class we {
613
620
  * }
614
621
  * \`\`\`
615
622
  */
616
- async index(e) {
617
- const r = /* @__PURE__ */ new Map(), a = async (n) => {
623
+ async index() {
624
+ const t = /* @__PURE__ */ new Map(), r = async (n) => {
618
625
  const i = await this.readdir(n, { withFileTypes: !0 });
619
- for (const o of i) {
620
- const s = \`\${n === "/" ? "" : n}/\${o.name}\`;
626
+ for (const a of i) {
627
+ const s = \`\${n === "/" ? "" : n}/\${a.name}\`;
621
628
  try {
622
- const l = await this.stat(s, e);
623
- r.set(s, l), l.isDirectory && await a(s);
624
- } catch (l) {
625
- console.warn(\`Skipping broken entry: \${s}\`, l);
629
+ const o = await this.stat(s);
630
+ t.set(s, o), o.isDirectory && await r(s);
631
+ } catch (o) {
632
+ console.warn(\`Skipping broken entry: \${s}\`, o);
626
633
  }
627
634
  }
628
635
  };
629
- return r.set("/", {
636
+ return t.set("/", {
630
637
  kind: "directory",
631
638
  size: 0,
632
639
  mtime: (/* @__PURE__ */ new Date(0)).toISOString(),
633
640
  ctime: (/* @__PURE__ */ new Date(0)).toISOString(),
634
641
  isFile: !1,
635
642
  isDirectory: !0
636
- }), await a("/"), r;
643
+ }), await r("/"), t;
637
644
  }
638
- async readFile(e, r = "utf-8") {
645
+ async readFile(t, r = "utf-8") {
639
646
  await this.ensureMounted();
640
647
  try {
641
- const a = await this.getFileHandle(e, !1), n = await ue(a);
642
- return r === "binary" ? n : re(n, r);
643
- } catch (a) {
644
- throw console.error(a), new D(e);
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 O(t);
645
652
  }
646
653
  }
647
654
  /**
@@ -669,10 +676,10 @@ class we {
669
676
  * await fs.writeFile('/data/utf16.txt', 'Hello World', 'utf-16le');
670
677
  * \`\`\`
671
678
  */
672
- async writeFile(e, r, a) {
679
+ async writeFile(t, r, n) {
673
680
  await this.ensureMounted();
674
- const n = await this.getFileHandle(e, !0);
675
- await N(n, r, a, { truncate: !0 }), this.notifyInternalChange(e, "change");
681
+ const i = await this.getFileHandle(t, !0);
682
+ await v(i, r, n, { truncate: !0 }), await this.notifyChange({ path: t, type: "changed", isDirectory: !1 });
676
683
  }
677
684
  /**
678
685
  * Append data to a file
@@ -696,10 +703,10 @@ class we {
696
703
  * await fs.appendFile('/data/binary.dat', additionalData);
697
704
  * \`\`\`
698
705
  */
699
- async appendFile(e, r, a) {
706
+ async appendFile(t, r, n) {
700
707
  await this.ensureMounted();
701
- const n = await this.getFileHandle(e, !0);
702
- await N(n, r, a, { append: !0 }), this.notifyInternalChange(e, "change");
708
+ const i = await this.getFileHandle(t, !0);
709
+ await v(i, r, n, { append: !0 }), await this.notifyChange({ path: t, type: "changed", isDirectory: !1 });
703
710
  }
704
711
  /**
705
712
  * Create a directory
@@ -722,55 +729,48 @@ class we {
722
729
  * await fs.mkdir('/users/john/documents/projects', { recursive: true });
723
730
  * \`\`\`
724
731
  */
725
- async mkdir(e, r) {
732
+ async mkdir(t, r) {
726
733
  if (await this.ensureMounted(), !this.root)
727
734
  throw new T();
728
- const a = r?.recursive ?? !1, n = m(e);
729
- let i = this.root;
730
- for (let o = 0; o < n.length; o++) {
731
- const s = n[o];
735
+ const n = r?.recursive ?? !1, i = m(t);
736
+ let a = this.root;
737
+ for (let s = 0; s < i.length; s++) {
738
+ const o = i[s];
732
739
  try {
733
- i = await i.getDirectoryHandle(s, { create: a || o === n.length - 1 });
740
+ a = await a.getDirectoryHandle(o, { create: n || s === i.length - 1 });
734
741
  } catch (l) {
735
742
  throw l.name === "NotFoundError" ? new c(
736
- \`Parent directory does not exist: \${x(n.slice(0, o + 1))}\`,
743
+ \`Parent directory does not exist: \${x(i.slice(0, s + 1))}\`,
737
744
  "ENOENT"
738
- ) : l.name === "TypeMismatchError" ? new c(\`Path segment is not a directory: \${s}\`, "ENOTDIR") : new c("Failed to create directory", "MKDIR_FAILED");
745
+ ) : l.name === "TypeMismatchError" ? new c(\`Path segment is not a directory: \${o}\`, "ENOTDIR") : new c("Failed to create directory", "MKDIR_FAILED");
739
746
  }
740
747
  }
741
- this.notifyInternalChange(e, "create");
748
+ await this.notifyChange({ path: t, type: "added", isDirectory: !0 });
742
749
  }
743
750
  /**
744
- * Get file or directory stats
751
+ * Get file or directory statistics
745
752
  *
746
- * Retrieves metadata about a file or directory, including size, modification time,
747
- * type information, and optionally file hashes.
753
+ * Returns detailed information about a file or directory, including
754
+ * size, modification time, and optionally a hash of the file content.
748
755
  *
749
756
  * @param path - The path to the file or directory
750
- * @param options - Options for stat operation
751
- * @param options.includeHash - Whether to calculate file hash (default: false, only for files)
752
- * @param options.hashAlgorithm - Hash algorithm to use (default: 'SHA-1', fastest)
753
- * @returns Promise that resolves to file/directory statistics
754
- * @throws {OPFSError} If the file or directory does not exist or cannot be accessed
757
+ * @returns Promise that resolves to FileStat object
758
+ * @throws {OPFSError} If the path does not exist or cannot be accessed
755
759
  *
756
760
  * @example
757
761
  * \`\`\`typescript
758
- * // Basic stats
759
- * const stats = await fs.stat('/config/settings.json');
762
+ * const stats = await fs.stat('/data/config.json');
760
763
  * console.log(\`File size: \${stats.size} bytes\`);
761
- * console.log(\`Is file: \${stats.isFile}\`);
762
- * console.log(\`Modified: \${stats.mtime}\`);
763
- *
764
- * // Stats with hash (SHA-1 is fastest)
765
- * const statsWithHash = await fs.stat('/config/settings.json', {
766
- * includeHash: true,
767
- * hashAlgorithm: 'SHA-1'
768
- * });
769
- * console.log(\`Hash: \${statsWithHash.hash}\`);
764
+ * console.log(\`Last modified: \${stats.mtime}\`);
765
+ *
766
+ * // If hashing is enabled, hash will be included
767
+ * if (stats.hash) {
768
+ * console.log(\`Hash: \${stats.hash}\`);
769
+ * }
770
770
  * \`\`\`
771
771
  */
772
- async stat(e, r) {
773
- if (await this.ensureMounted(), e === "/")
772
+ async stat(t) {
773
+ if (await this.ensureMounted(), t === "/")
774
774
  return {
775
775
  kind: "directory",
776
776
  size: 0,
@@ -779,30 +779,30 @@ class we {
779
779
  isFile: !1,
780
780
  isDirectory: !0
781
781
  };
782
- const a = b(e), n = await this.getDirectoryHandle(M(e), !1), i = r?.includeHash ?? !1, o = r?.hashAlgorithm ?? "SHA-1";
782
+ const r = C(t), n = await this.getDirectoryHandle(M(t), !1), i = this.hashAlgorithm !== null;
783
783
  try {
784
- const l = await (await n.getFileHandle(a, { create: !1 })).getFile(), u = {
784
+ const s = await (await n.getFileHandle(r, { create: !1 })).getFile(), o = {
785
785
  kind: "file",
786
- size: l.size,
787
- mtime: new Date(l.lastModified).toISOString(),
788
- ctime: new Date(l.lastModified).toISOString(),
786
+ size: s.size,
787
+ mtime: new Date(s.lastModified).toISOString(),
788
+ ctime: new Date(s.lastModified).toISOString(),
789
789
  isFile: !0,
790
790
  isDirectory: !1
791
791
  };
792
- if (i)
792
+ if (i && this.hashAlgorithm)
793
793
  try {
794
- const f = new Uint8Array(await l.arrayBuffer()), w = await fe(f, o);
795
- u.hash = w;
796
- } catch (f) {
797
- console.warn(\`Failed to calculate hash for \${e}:\`, f);
794
+ const l = await ut(s, this.hashAlgorithm);
795
+ o.hash = l;
796
+ } catch (l) {
797
+ console.warn(\`Failed to calculate hash for \${t}:\`, l);
798
798
  }
799
- return u;
800
- } catch (s) {
801
- if (s.name !== "TypeMismatchError" && s.name !== "NotFoundError")
799
+ return o;
800
+ } catch (a) {
801
+ if (a.name !== "TypeMismatchError" && a.name !== "NotFoundError")
802
802
  throw new c("Failed to stat (file)", "STAT_FAILED");
803
803
  }
804
804
  try {
805
- return await n.getDirectoryHandle(a, { create: !1 }), {
805
+ return await n.getDirectoryHandle(r, { create: !1 }), {
806
806
  kind: "directory",
807
807
  size: 0,
808
808
  mtime: (/* @__PURE__ */ new Date(0)).toISOString(),
@@ -810,30 +810,30 @@ class we {
810
810
  isFile: !1,
811
811
  isDirectory: !0
812
812
  };
813
- } catch (s) {
814
- throw s.name === "NotFoundError" ? new c(\`No such file or directory: \${e}\`, "ENOENT") : new c("Failed to stat (directory)", "STAT_FAILED");
813
+ } catch (a) {
814
+ throw a.name === "NotFoundError" ? new c(\`No such file or directory: \${t}\`, "ENOENT") : new c("Failed to stat (directory)", "STAT_FAILED");
815
815
  }
816
816
  }
817
- async readdir(e, r) {
817
+ async readdir(t, r) {
818
818
  await this.ensureMounted();
819
- const a = r?.withFileTypes ?? !1, n = await this.getDirectoryHandle(e, !1);
820
- if (a) {
821
- const i = [];
822
- for await (const [o, s] of n.entries()) {
823
- const l = s.kind === "file";
824
- i.push({
825
- name: o,
826
- kind: s.kind,
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
827
  isFile: l,
828
828
  isDirectory: !l
829
829
  });
830
830
  }
831
- return i;
831
+ return a;
832
832
  } else {
833
- const i = [];
834
- for await (const [o] of n.entries())
835
- i.push(o);
836
- return i;
833
+ const a = [];
834
+ for await (const [s] of i.entries())
835
+ a.push(s);
836
+ return a;
837
837
  }
838
838
  }
839
839
  /**
@@ -850,29 +850,29 @@ class we {
850
850
  * console.log(\`File exists: \${exists}\`);
851
851
  * \`\`\`
852
852
  */
853
- async exists(e) {
854
- if (await this.ensureMounted(), e === "/")
853
+ async exists(t) {
854
+ if (await this.ensureMounted(), t === "/")
855
855
  return !0;
856
- const r = b(e);
857
- let a = null;
856
+ const r = C(t);
857
+ let n = null;
858
858
  try {
859
- a = await this.getDirectoryHandle(M(e), !1);
860
- } catch (n) {
861
- throw (n.name === "NotFoundError" || n.name === "TypeMismatchError") && (a = null), n;
859
+ n = await this.getDirectoryHandle(M(t), !1);
860
+ } catch (i) {
861
+ throw (i.name === "NotFoundError" || i.name === "TypeMismatchError") && (n = null), i;
862
862
  }
863
- if (!a || !r)
863
+ if (!n || !r)
864
864
  return !1;
865
865
  try {
866
- return await a.getFileHandle(r, { create: !1 }), !0;
867
- } catch (n) {
868
- if (n.name !== "NotFoundError" && n.name !== "TypeMismatchError")
869
- throw n;
866
+ return await n.getFileHandle(r, { create: !1 }), !0;
867
+ } catch (i) {
868
+ if (i.name !== "NotFoundError" && i.name !== "TypeMismatchError")
869
+ throw i;
870
870
  }
871
871
  try {
872
- return await a.getDirectoryHandle(r, { create: !1 }), !0;
873
- } catch (n) {
874
- if (n.name !== "NotFoundError" && n.name !== "TypeMismatchError")
875
- throw n;
872
+ return await n.getDirectoryHandle(r, { create: !1 }), !0;
873
+ } catch (i) {
874
+ if (i.name !== "NotFoundError" && i.name !== "TypeMismatchError")
875
+ throw i;
876
876
  }
877
877
  return !1;
878
878
  }
@@ -895,17 +895,17 @@ class we {
895
895
  * await fs.clear('/data');
896
896
  * \`\`\`
897
897
  */
898
- async clear(e = "/") {
898
+ async clear(t = "/") {
899
899
  await this.ensureMounted();
900
900
  try {
901
- const r = await this.readdir(e, { withFileTypes: !0 });
902
- for (const a of r) {
903
- const n = \`\${e === "/" ? "" : e}/\${a.name}\`;
904
- await this.remove(n, { recursive: !0 });
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 });
905
905
  }
906
- this.notifyInternalChange(e, "change");
906
+ await this.notifyChange({ path: t, type: "changed", isDirectory: !0 });
907
907
  } catch (r) {
908
- throw r instanceof c ? r : new c(\`Failed to clear directory: \${e}\`, "CLEAR_FAILED");
908
+ throw r instanceof c ? r : new c(\`Failed to clear directory: \${t}\`, "CLEAR_FAILED");
909
909
  }
910
910
  }
911
911
  /**
@@ -932,24 +932,24 @@ class we {
932
932
  * await fs.rm('/maybe/exists', { force: true });
933
933
  * \`\`\`
934
934
  */
935
- async remove(e, r) {
935
+ async remove(t, r) {
936
936
  await this.ensureMounted();
937
- const a = r?.recursive ?? !1, n = r?.force ?? !1;
938
- if (e === "/")
937
+ const n = r?.recursive ?? !1, i = r?.force ?? !1;
938
+ if (t === "/")
939
939
  throw new c("Cannot remove root directory", "EROOT");
940
- const i = b(e);
941
- if (!i)
942
- throw new v("Invalid path", e);
943
- const o = await this.getDirectoryHandle(M(e), !1);
940
+ const a = C(t);
941
+ if (!a)
942
+ throw new k("Invalid path", t);
943
+ const s = await this.getDirectoryHandle(M(t), !1);
944
944
  try {
945
- await o.removeEntry(i, { recursive: a });
946
- } catch (s) {
947
- if (s.name === "NotFoundError") {
948
- if (!n)
949
- throw new c(\`No such file or directory: \${e}\`, "ENOENT");
950
- } else throw s.name === "InvalidModificationError" ? new c(\`Directory not empty: \${e}. Use recursive option to force removal.\`, "ENOTEMPTY") : s.name === "TypeMismatchError" && !a ? new c(\`Cannot remove directory without recursive option: \${e}\`, "EISDIR") : new c(\`Failed to remove path: \${e}\`, "RM_FAILED");
945
+ await s.removeEntry(a, { recursive: n });
946
+ } catch (o) {
947
+ if (o.name === "NotFoundError") {
948
+ if (!i)
949
+ 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");
951
951
  }
952
- this.notifyInternalChange(e, "delete");
952
+ await this.notifyChange({ path: t, type: "removed", isDirectory: !1 });
953
953
  }
954
954
  /**
955
955
  * Resolve a path to an absolute path
@@ -969,15 +969,15 @@ class we {
969
969
  * console.log(absolute); // '/data/file.txt'
970
970
  * \`\`\`
971
971
  */
972
- async realpath(e) {
972
+ async realpath(t) {
973
973
  await this.ensureMounted();
974
974
  try {
975
- const r = ce(e);
975
+ const r = ct(t);
976
976
  if (!await this.exists(r))
977
- throw new D(r);
977
+ throw new O(r);
978
978
  return r;
979
979
  } catch (r) {
980
- throw r instanceof c ? r : new c(\`Failed to resolve path: \${e}\`, "REALPATH_FAILED");
980
+ throw r instanceof c ? r : new c(\`Failed to resolve path: \${t}\`, "REALPATH_FAILED");
981
981
  }
982
982
  }
983
983
  /**
@@ -996,14 +996,14 @@ class we {
996
996
  * await fs.rename('/old/path/file.txt', '/new/path/renamed.txt');
997
997
  * \`\`\`
998
998
  */
999
- async rename(e, r) {
999
+ async rename(t, r) {
1000
1000
  await this.ensureMounted();
1001
1001
  try {
1002
- if (!await this.exists(e))
1003
- throw new D(e);
1004
- await this.copy(e, r, { recursive: !0 }), await this.remove(e, { recursive: !0 }), this.notifyInternalChange(e, "delete"), this.notifyInternalChange(r, "create");
1005
- } catch (a) {
1006
- throw a instanceof c ? a : new c(\`Failed to rename from \${e} to \${r}\`, "RENAME_FAILED");
1002
+ if (!await this.exists(t))
1003
+ throw new O(t);
1004
+ 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");
1007
1007
  }
1008
1008
  }
1009
1009
  /**
@@ -1031,85 +1031,81 @@ class we {
1031
1031
  * await fs.copy('/source', '/dest', { recursive: true, force: false });
1032
1032
  * \`\`\`
1033
1033
  */
1034
- async copy(e, r, a) {
1034
+ async copy(t, r, n) {
1035
1035
  await this.ensureMounted();
1036
1036
  try {
1037
- const n = a?.recursive ?? !1, i = a?.force ?? !0;
1038
- if (!await this.exists(e))
1039
- throw new c(\`Source does not exist: \${e}\`, "ENOENT");
1040
- if (await this.exists(r) && !i)
1037
+ const i = n?.recursive ?? !1, a = n?.force ?? !0;
1038
+ if (!await this.exists(t))
1039
+ throw new c(\`Source does not exist: \${t}\`, "ENOENT");
1040
+ if (await this.exists(r) && !a)
1041
1041
  throw new c(\`Destination already exists: \${r}\`, "EEXIST");
1042
- if ((await this.stat(e)).isFile) {
1043
- const u = await this.readFile(e, "binary");
1044
- await this.writeFile(r, u);
1042
+ if ((await this.stat(t)).isFile) {
1043
+ const h = await this.readFile(t, "binary");
1044
+ await this.writeFile(r, h);
1045
1045
  } else {
1046
- if (!n)
1047
- throw new c(\`Cannot copy directory without recursive option: \${e}\`, "EISDIR");
1046
+ if (!i)
1047
+ throw new c(\`Cannot copy directory without recursive option: \${t}\`, "EISDIR");
1048
1048
  await this.mkdir(r, { recursive: !0 });
1049
- const u = await this.readdir(e, { withFileTypes: !0 });
1050
- for (const f of u) {
1051
- const w = \`\${e}/\${f.name}\`, h = \`\${r}/\${f.name}\`;
1052
- await this.copy(w, h, { recursive: !0, force: i });
1049
+ const h = await this.readdir(t, { withFileTypes: !0 });
1050
+ for (const u of h) {
1051
+ const w = \`\${t}/\${u.name}\`, f = \`\${r}/\${u.name}\`;
1052
+ await this.copy(w, f, { recursive: !0, force: a });
1053
1053
  }
1054
1054
  }
1055
- this.notifyInternalChange(r, "create");
1056
- } catch (n) {
1057
- throw n instanceof c ? n : new c(\`Failed to copy from \${e} to \${r}\`, "CP_FAILED");
1055
+ 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");
1058
1058
  }
1059
1059
  }
1060
1060
  /**
1061
1061
  * Start watching a file or directory for changes
1062
1062
  */
1063
- async watch(e) {
1063
+ async watch(t) {
1064
1064
  await this.ensureMounted();
1065
- const r = C(e), a = await this.buildSnapshot(r);
1066
- this.watchers.set(r, a), this.watchTimer || (this.watchTimer = setInterval(() => {
1065
+ const r = b(t), n = await this.buildSnapshot(r);
1066
+ this.watchers.set(r, n), this.watchTimer || (this.watchTimer = setInterval(() => {
1067
1067
  this.scanWatches();
1068
1068
  }, this.watchInterval));
1069
1069
  }
1070
1070
  /**
1071
1071
  * Stop watching a previously watched path
1072
1072
  */
1073
- unwatch(e) {
1074
- const r = C(e);
1073
+ unwatch(t) {
1074
+ const r = b(t);
1075
1075
  this.watchers.delete(r), this.watchers.size === 0 && this.watchTimer && (clearInterval(this.watchTimer), this.watchTimer = null);
1076
1076
  }
1077
- async buildSnapshot(e) {
1078
- const r = /* @__PURE__ */ new Map(), a = async (n) => {
1079
- const i = await this.stat(n);
1080
- if (r.set(n, i), i.isDirectory) {
1081
- const o = await this.readdir(n, { withFileTypes: !0 });
1082
- for (const s of o) {
1083
- const l = \`\${n === "/" ? "" : n}/\${s.name}\`;
1084
- await a(l);
1077
+ async buildSnapshot(t) {
1078
+ const r = /* @__PURE__ */ new Map(), n = async (i) => {
1079
+ const a = await this.stat(i);
1080
+ if (r.set(i, a), a.isDirectory) {
1081
+ const s = await this.readdir(i, { withFileTypes: !0 });
1082
+ for (const o of s) {
1083
+ const l = \`\${i === "/" ? "" : i}/\${o.name}\`;
1084
+ await n(l);
1085
1085
  }
1086
1086
  }
1087
1087
  };
1088
- return await a(e), r;
1088
+ return await n(t), r;
1089
1089
  }
1090
1090
  async scanWatches() {
1091
1091
  if (!this.scanning) {
1092
1092
  this.scanning = !0;
1093
1093
  try {
1094
1094
  await Promise.all(
1095
- [...this.watchers.entries()].map(async ([e, r]) => {
1096
- let a;
1095
+ [...this.watchers.entries()].map(async ([t, r]) => {
1096
+ let n;
1097
1097
  try {
1098
- a = await this.buildSnapshot(e);
1098
+ n = await this.buildSnapshot(t);
1099
1099
  } catch {
1100
- a = /* @__PURE__ */ new Map();
1100
+ n = /* @__PURE__ */ new Map();
1101
1101
  }
1102
- const n = [];
1103
- for (const [i, o] of a) {
1102
+ for (const [i, a] of n) {
1104
1103
  const s = r.get(i);
1105
- s ? (s.mtime !== o.mtime || s.size !== o.size) && n.push({ path: i, type: "change" }) : n.push({ path: i, type: "create" });
1104
+ s ? (s.mtime !== a.mtime || s.size !== a.size) && await this.notifyChange({ path: i, type: "changed", isDirectory: !1 }) : await this.notifyChange({ path: i, type: "added", isDirectory: !1 });
1106
1105
  }
1107
1106
  for (const i of r.keys())
1108
- a.has(i) || n.push({ path: i, type: "delete" });
1109
- if (n.length && this.watchCallback)
1110
- for (const i of n)
1111
- this.watchCallback(i);
1112
- this.watchers.set(e, a);
1107
+ n.has(i) || await this.notifyChange({ path: i, type: "removed", isDirectory: !1 });
1108
+ this.watchers.set(t, n);
1113
1109
  })
1114
1110
  );
1115
1111
  } finally {
@@ -1145,72 +1141,75 @@ class we {
1145
1141
  * await fs.sync(entries, { cleanBefore: true });
1146
1142
  * \`\`\`
1147
1143
  */
1148
- async sync(e, r) {
1144
+ async sync(t, r) {
1149
1145
  await this.ensureMounted();
1150
1146
  try {
1151
1147
  (r?.cleanBefore ?? !1) && await this.clear("/");
1152
- for (const [n, i] of e) {
1153
- const o = C(n);
1154
- let s;
1155
- i instanceof Blob ? s = await he(i) : s = i, await this.writeFile(o, s);
1148
+ for (const [i, a] of t) {
1149
+ const s = b(i);
1150
+ let o;
1151
+ a instanceof Blob ? o = await ft(a) : o = a, await this.writeFile(s, o);
1156
1152
  }
1157
- this.notifyInternalChange("/", "change");
1158
- } catch (a) {
1159
- throw a instanceof c ? a : new c("Failed to sync file system", "SYNC_FAILED");
1153
+ await this.notifyChange({ path: "/", type: "changed", isDirectory: !0 });
1154
+ } catch (n) {
1155
+ throw n instanceof c ? n : new c("Failed to sync file system", "SYNC_FAILED");
1160
1156
  }
1161
1157
  }
1162
1158
  }
1163
- typeof self < "u" && self.constructor.name === "DedicatedWorkerGlobalScope" && O(new we());
1164
- //# sourceMappingURL=worker-DCm4BiWM.js.map
1159
+ typeof self < "u" && self.constructor.name === "DedicatedWorkerGlobalScope" && P(new wt());
1160
+ //# sourceMappingURL=worker-BL3varjN.js.map
1165
1161
  `, r = typeof self < "u" && self.Blob && new Blob(["URL.revokeObjectURL(import.meta.url);", a], { type: "text/javascript;charset=utf-8" });
1166
- function s(t) {
1162
+ function s(e) {
1167
1163
  let n;
1168
1164
  try {
1169
1165
  if (n = r && (self.URL || self.webkitURL).createObjectURL(r), !n) throw "";
1170
- const e = new Worker(n, {
1166
+ const t = new Worker(n, {
1171
1167
  type: "module",
1172
- name: t?.name
1168
+ name: e?.name
1173
1169
  });
1174
- return e.addEventListener("error", () => {
1170
+ return t.addEventListener("error", () => {
1175
1171
  (self.URL || self.webkitURL).revokeObjectURL(n);
1176
- }), e;
1172
+ }), t;
1177
1173
  } catch {
1178
1174
  return new Worker(
1179
1175
  "data:text/javascript;charset=utf-8," + encodeURIComponent(a),
1180
1176
  {
1181
1177
  type: "module",
1182
- name: t?.name
1178
+ name: e?.name
1183
1179
  }
1184
1180
  );
1185
1181
  }
1186
1182
  }
1187
- function l(t, n) {
1188
- const e = i(new s());
1189
- return t && e.setWatchCallback(o(t), n), e;
1183
+ function h(e, n) {
1184
+ const t = i(new s());
1185
+ return e && t.setWatchCallback(
1186
+ e ? o(e) : () => {
1187
+ }
1188
+ ), n && t.setOptions(n), t;
1190
1189
  }
1191
1190
  export {
1192
1191
  d as DirectoryNotFoundError,
1193
- u as FileNotFoundError,
1194
- p as OPFSError,
1195
- w as OPFSNotMountedError,
1196
- y as OPFSNotSupportedError,
1192
+ p as FileNotFoundError,
1193
+ u as OPFSError,
1194
+ y as OPFSNotMountedError,
1195
+ w as OPFSNotSupportedError,
1197
1196
  m as PathError,
1198
1197
  g as PermissionError,
1199
1198
  v as StorageError,
1200
1199
  E as TimeoutError,
1201
- S as basename,
1202
- F as calculateFileHash,
1200
+ F as basename,
1201
+ S as calculateFileHash,
1203
1202
  b as checkOPFSSupport,
1204
1203
  P as convertBlobToUint8Array,
1205
- x as createBuffer,
1206
- l as createWorker,
1207
- T as decodeBuffer,
1208
- I as dirname,
1209
- A as encodeString,
1210
- O as extname,
1204
+ T as createBuffer,
1205
+ h as createWorker,
1206
+ x as decodeBuffer,
1207
+ A as dirname,
1208
+ O as encodeString,
1209
+ D as extname,
1211
1210
  _ as joinPath,
1212
- C as normalizePath,
1213
- D as readFileData,
1211
+ I as normalizePath,
1212
+ C as readFileData,
1214
1213
  k as resolvePath,
1215
1214
  U as splitPath,
1216
1215
  R as writeFileData