opfs-worker 0.1.2 → 0.2.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"),a=`/**
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const o=require("comlink"),n=require("./helpers-hEpet7x7.cjs"),i=`/**
2
2
  * @license
3
3
  * Copyright 2019 Google LLC
4
4
  * SPDX-License-Identifier: Apache-2.0
5
5
  */
6
- const C = Symbol("Comlink.proxy"), v = Symbol("Comlink.endpoint"), _ = Symbol("Comlink.releaseProxy"), D = Symbol("Comlink.finalizer"), p = Symbol("Comlink.thrown"), M = (e) => typeof e == "object" && e !== null || typeof e == "function", R = {
7
- canHandle: (e) => M(e) && e[C],
6
+ const M = Symbol("Comlink.proxy"), R = Symbol("Comlink.endpoint"), B = Symbol("Comlink.releaseProxy"), T = Symbol("Comlink.finalizer"), p = Symbol("Comlink.thrown"), $ = (e) => typeof e == "object" && e !== null || typeof e == "function", W = {
7
+ canHandle: (e) => $(e) && e[M],
8
8
  serialize(e) {
9
9
  const { port1: t, port2: r } = new MessageChannel();
10
- return O(e, t), [r, [r]];
10
+ return N(e, t), [r, [r]];
11
11
  },
12
12
  deserialize(e) {
13
- return e.start(), V(e);
13
+ return e.start(), Y(e);
14
14
  }
15
- }, z = {
16
- canHandle: (e) => M(e) && p in e,
15
+ }, j = {
16
+ canHandle: (e) => $(e) && p in e,
17
17
  serialize({ value: e }) {
18
18
  let t;
19
19
  return e instanceof Error ? t = {
@@ -28,187 +28,187 @@ const C = Symbol("Comlink.proxy"), v = Symbol("Comlink.endpoint"), _ = Symbol("C
28
28
  deserialize(e) {
29
29
  throw e.isError ? Object.assign(new Error(e.value.message), e.value) : e.value;
30
30
  }
31
- }, k = /* @__PURE__ */ new Map([
32
- ["proxy", R],
33
- ["throw", z]
31
+ }, U = /* @__PURE__ */ new Map([
32
+ ["proxy", W],
33
+ ["throw", j]
34
34
  ]);
35
- function B(e, t) {
35
+ function V(e, t) {
36
36
  for (const r of e)
37
37
  if (t === r || r === "*" || r instanceof RegExp && r.test(t))
38
38
  return !0;
39
39
  return !1;
40
40
  }
41
- function O(e, t = globalThis, r = ["*"]) {
41
+ function N(e, t = globalThis, r = ["*"]) {
42
42
  t.addEventListener("message", function a(n) {
43
43
  if (!n || !n.data)
44
44
  return;
45
- if (!B(r, n.origin)) {
45
+ if (!V(r, n.origin)) {
46
46
  console.warn(\`Invalid origin '\${n.origin}' for comlink proxy\`);
47
47
  return;
48
48
  }
49
- const { id: o, type: s, path: c } = Object.assign({ path: [] }, n.data), i = (n.data.argumentList || []).map(d);
50
- let u;
49
+ const { id: s, type: o, path: i } = Object.assign({ path: [] }, n.data), c = (n.data.argumentList || []).map(y);
50
+ let f;
51
51
  try {
52
- const f = c.slice(0, -1).reduce((h, g) => h[g], e), w = c.reduce((h, g) => h[g], e);
53
- switch (s) {
52
+ const u = i.slice(0, -1).reduce((h, g) => h[g], e), w = i.reduce((h, g) => h[g], e);
53
+ switch (o) {
54
54
  case "GET":
55
- u = w;
55
+ f = w;
56
56
  break;
57
57
  case "SET":
58
- f[c.slice(-1)[0]] = d(n.data.value), u = !0;
58
+ u[i.slice(-1)[0]] = y(n.data.value), f = !0;
59
59
  break;
60
60
  case "APPLY":
61
- u = w.apply(f, i);
61
+ f = w.apply(u, c);
62
62
  break;
63
63
  case "CONSTRUCT":
64
64
  {
65
- const h = new w(...i);
66
- u = q(h);
65
+ const h = new w(...c);
66
+ f = Q(h);
67
67
  }
68
68
  break;
69
69
  case "ENDPOINT":
70
70
  {
71
71
  const { port1: h, port2: g } = new MessageChannel();
72
- O(e, g), u = X(h, [h]);
72
+ N(e, g), f = J(h, [h]);
73
73
  }
74
74
  break;
75
75
  case "RELEASE":
76
- u = void 0;
76
+ f = void 0;
77
77
  break;
78
78
  default:
79
79
  return;
80
80
  }
81
- } catch (f) {
82
- u = { value: f, [p]: 0 };
81
+ } catch (u) {
82
+ f = { value: u, [p]: 0 };
83
83
  }
84
- Promise.resolve(u).catch((f) => ({ value: f, [p]: 0 })).then((f) => {
85
- const [w, h] = S(f);
86
- t.postMessage(Object.assign(Object.assign({}, w), { id: o }), h), s === "RELEASE" && (t.removeEventListener("message", a), $(t), D in e && typeof e[D] == "function" && e[D]());
87
- }).catch((f) => {
88
- const [w, h] = S({
84
+ Promise.resolve(f).catch((u) => ({ value: u, [p]: 0 })).then((u) => {
85
+ const [w, h] = A(u);
86
+ t.postMessage(Object.assign(Object.assign({}, w), { id: s }), h), o === "RELEASE" && (t.removeEventListener("message", a), L(t), T in e && typeof e[T] == "function" && e[T]());
87
+ }).catch((u) => {
88
+ const [w, h] = A({
89
89
  value: new TypeError("Unserializable return value"),
90
90
  [p]: 0
91
91
  });
92
- t.postMessage(Object.assign(Object.assign({}, w), { id: o }), h);
92
+ t.postMessage(Object.assign(Object.assign({}, w), { id: s }), h);
93
93
  });
94
94
  }), t.start && t.start();
95
95
  }
96
- function j(e) {
96
+ function G(e) {
97
97
  return e.constructor.name === "MessagePort";
98
98
  }
99
- function $(e) {
100
- j(e) && e.close();
99
+ function L(e) {
100
+ G(e) && e.close();
101
101
  }
102
- function V(e, t) {
102
+ function Y(e, t) {
103
103
  const r = /* @__PURE__ */ new Map();
104
104
  return e.addEventListener("message", function(n) {
105
- const { data: o } = n;
106
- if (!o || !o.id)
105
+ const { data: s } = n;
106
+ if (!s || !s.id)
107
107
  return;
108
- const s = r.get(o.id);
109
- if (s)
108
+ const o = r.get(s.id);
109
+ if (o)
110
110
  try {
111
- s(o);
111
+ o(s);
112
112
  } finally {
113
- r.delete(o.id);
113
+ r.delete(s.id);
114
114
  }
115
- }), b(e, r, [], t);
115
+ }), P(e, r, [], t);
116
116
  }
117
117
  function E(e) {
118
118
  if (e)
119
119
  throw new Error("Proxy has been released and is not useable");
120
120
  }
121
- function L(e) {
122
- return m(e, /* @__PURE__ */ new Map(), {
121
+ function z(e) {
122
+ return d(e, /* @__PURE__ */ new Map(), {
123
123
  type: "RELEASE"
124
124
  }).then(() => {
125
- $(e);
125
+ L(e);
126
126
  });
127
127
  }
128
- const F = /* @__PURE__ */ new WeakMap(), A = "FinalizationRegistry" in globalThis && new FinalizationRegistry((e) => {
128
+ const F = /* @__PURE__ */ new WeakMap(), S = "FinalizationRegistry" in globalThis && new FinalizationRegistry((e) => {
129
129
  const t = (F.get(e) || 0) - 1;
130
- F.set(e, t), t === 0 && L(e);
130
+ F.set(e, t), t === 0 && z(e);
131
131
  });
132
- function W(e, t) {
132
+ function X(e, t) {
133
133
  const r = (F.get(t) || 0) + 1;
134
- F.set(t, r), A && A.register(e, t, e);
134
+ F.set(t, r), S && S.register(e, t, e);
135
135
  }
136
- function Y(e) {
137
- A && A.unregister(e);
136
+ function q(e) {
137
+ S && S.unregister(e);
138
138
  }
139
- function b(e, t, r = [], a = function() {
139
+ function P(e, t, r = [], a = function() {
140
140
  }) {
141
141
  let n = !1;
142
- const o = new Proxy(a, {
143
- get(s, c) {
144
- if (E(n), c === _)
142
+ const s = new Proxy(a, {
143
+ get(o, i) {
144
+ if (E(n), i === B)
145
145
  return () => {
146
- Y(o), L(e), t.clear(), n = !0;
146
+ q(s), z(e), t.clear(), n = !0;
147
147
  };
148
- if (c === "then") {
148
+ if (i === "then") {
149
149
  if (r.length === 0)
150
- return { then: () => o };
151
- const i = m(e, t, {
150
+ return { then: () => s };
151
+ const c = d(e, t, {
152
152
  type: "GET",
153
- path: r.map((u) => u.toString())
154
- }).then(d);
155
- return i.then.bind(i);
153
+ path: r.map((f) => f.toString())
154
+ }).then(y);
155
+ return c.then.bind(c);
156
156
  }
157
- return b(e, t, [...r, c]);
157
+ return P(e, t, [...r, i]);
158
158
  },
159
- set(s, c, i) {
159
+ set(o, i, c) {
160
160
  E(n);
161
- const [u, f] = S(i);
162
- return m(e, t, {
161
+ const [f, u] = A(c);
162
+ return d(e, t, {
163
163
  type: "SET",
164
- path: [...r, c].map((w) => w.toString()),
165
- value: u
166
- }, f).then(d);
164
+ path: [...r, i].map((w) => w.toString()),
165
+ value: f
166
+ }, u).then(y);
167
167
  },
168
- apply(s, c, i) {
168
+ apply(o, i, c) {
169
169
  E(n);
170
- const u = r[r.length - 1];
171
- if (u === v)
172
- return m(e, t, {
170
+ const f = r[r.length - 1];
171
+ if (f === R)
172
+ return d(e, t, {
173
173
  type: "ENDPOINT"
174
- }).then(d);
175
- if (u === "bind")
176
- return b(e, t, r.slice(0, -1));
177
- const [f, w] = I(i);
178
- return m(e, t, {
174
+ }).then(y);
175
+ if (f === "bind")
176
+ return P(e, t, r.slice(0, -1));
177
+ const [u, w] = k(c);
178
+ return d(e, t, {
179
179
  type: "APPLY",
180
180
  path: r.map((h) => h.toString()),
181
- argumentList: f
182
- }, w).then(d);
181
+ argumentList: u
182
+ }, w).then(y);
183
183
  },
184
- construct(s, c) {
184
+ construct(o, i) {
185
185
  E(n);
186
- const [i, u] = I(c);
187
- return m(e, t, {
186
+ const [c, f] = k(i);
187
+ return d(e, t, {
188
188
  type: "CONSTRUCT",
189
- path: r.map((f) => f.toString()),
190
- argumentList: i
191
- }, u).then(d);
189
+ path: r.map((u) => u.toString()),
190
+ argumentList: c
191
+ }, f).then(y);
192
192
  }
193
193
  });
194
- return W(o, e), o;
194
+ return X(s, e), s;
195
195
  }
196
- function G(e) {
196
+ function K(e) {
197
197
  return Array.prototype.concat.apply([], e);
198
198
  }
199
- function I(e) {
200
- const t = e.map(S);
201
- return [t.map((r) => r[0]), G(t.map((r) => r[1]))];
199
+ function k(e) {
200
+ const t = e.map(A);
201
+ return [t.map((r) => r[0]), K(t.map((r) => r[1]))];
202
202
  }
203
- const U = /* @__PURE__ */ new WeakMap();
204
- function X(e, t) {
205
- return U.set(e, t), e;
203
+ const _ = /* @__PURE__ */ new WeakMap();
204
+ function J(e, t) {
205
+ return _.set(e, t), e;
206
206
  }
207
- function q(e) {
208
- return Object.assign(e, { [C]: !0 });
207
+ function Q(e) {
208
+ return Object.assign(e, { [M]: !0 });
209
209
  }
210
- function S(e) {
211
- for (const [t, r] of k)
210
+ function A(e) {
211
+ for (const [t, r] of U)
212
212
  if (r.canHandle(e)) {
213
213
  const [a, n] = r.serialize(e);
214
214
  return [
@@ -225,24 +225,24 @@ function S(e) {
225
225
  type: "RAW",
226
226
  value: e
227
227
  },
228
- U.get(e) || []
228
+ _.get(e) || []
229
229
  ];
230
230
  }
231
- function d(e) {
231
+ function y(e) {
232
232
  switch (e.type) {
233
233
  case "HANDLER":
234
- return k.get(e.name).deserialize(e.value);
234
+ return U.get(e.name).deserialize(e.value);
235
235
  case "RAW":
236
236
  return e.value;
237
237
  }
238
238
  }
239
- function m(e, t, r, a) {
239
+ function d(e, t, r, a) {
240
240
  return new Promise((n) => {
241
- const o = K();
242
- t.set(o, n), e.start && e.start(), e.postMessage(Object.assign({ id: o }, r), a);
241
+ const s = Z();
242
+ t.set(s, n), e.start && e.start(), e.postMessage(Object.assign({ id: s }, r), a);
243
243
  });
244
244
  }
245
- function K() {
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 l extends Error {
@@ -250,27 +250,27 @@ class l extends Error {
250
250
  super(t), this.code = r, this.path = a, this.name = "OPFSError";
251
251
  }
252
252
  }
253
- class J extends l {
253
+ class ee extends l {
254
254
  constructor() {
255
255
  super("OPFS is not supported in this browser", "OPFS_NOT_SUPPORTED");
256
256
  }
257
257
  }
258
- class T extends l {
258
+ class D extends l {
259
259
  constructor() {
260
260
  super("OPFS is not mounted", "OPFS_NOT_MOUNTED");
261
261
  }
262
262
  }
263
- class N extends l {
263
+ class v extends l {
264
264
  constructor(t, r) {
265
265
  super(t, "INVALID_PATH", r);
266
266
  }
267
267
  }
268
- class x extends l {
268
+ class b extends l {
269
269
  constructor(t) {
270
270
  super(\`File not found: \${t}\`, "FILE_NOT_FOUND", t);
271
271
  }
272
272
  }
273
- function Q(e, t = "utf-8") {
273
+ function te(e, t = "utf-8") {
274
274
  switch (t) {
275
275
  case "utf8":
276
276
  case "utf-8":
@@ -278,11 +278,11 @@ function Q(e, t = "utf-8") {
278
278
  case "utf16le":
279
279
  case "ucs2":
280
280
  case "ucs-2":
281
- return ee(e);
282
- case "ascii":
283
281
  return ne(e);
282
+ case "ascii":
283
+ return ie(e);
284
284
  case "latin1":
285
- return re(e);
285
+ return se(e);
286
286
  case "binary":
287
287
  return Uint8Array.from(e, (r) => r.charCodeAt(0));
288
288
  case "base64":
@@ -295,7 +295,7 @@ function Q(e, t = "utf-8") {
295
295
  return console.warn("Encoding not supported, falling back to UTF-8"), new TextEncoder().encode(e);
296
296
  }
297
297
  }
298
- function Z(e, t = "utf-8") {
298
+ function re(e, t = "utf-8") {
299
299
  switch (t) {
300
300
  case "utf8":
301
301
  case "utf-8":
@@ -303,7 +303,7 @@ function Z(e, t = "utf-8") {
303
303
  case "utf16le":
304
304
  case "ucs2":
305
305
  case "ucs-2":
306
- return te(e);
306
+ return ae(e);
307
307
  case "latin1":
308
308
  return String.fromCharCode(...e);
309
309
  case "binary":
@@ -318,7 +318,7 @@ function Z(e, t = "utf-8") {
318
318
  return console.warn("Unsupported encoding, falling back to UTF-8"), new TextDecoder().decode(e);
319
319
  }
320
320
  }
321
- function ee(e) {
321
+ function ne(e) {
322
322
  const t = new Uint8Array(e.length * 2);
323
323
  for (let r = 0; r < e.length; r++) {
324
324
  const a = e.charCodeAt(r);
@@ -326,37 +326,60 @@ function ee(e) {
326
326
  }
327
327
  return t;
328
328
  }
329
- function te(e) {
329
+ function ae(e) {
330
330
  e.length % 2 !== 0 && (console.warn("Invalid UTF-16LE buffer length, truncating last byte"), e = e.slice(0, e.length - 1));
331
331
  const t = new Uint16Array(e.buffer, e.byteOffset, e.byteLength / 2);
332
332
  return String.fromCharCode(...t);
333
333
  }
334
- function re(e) {
334
+ function se(e) {
335
335
  const t = new Uint8Array(e.length);
336
336
  for (let r = 0; r < e.length; r++)
337
337
  t[r] = e.charCodeAt(r) & 255;
338
338
  return t;
339
339
  }
340
- function ne(e) {
340
+ function ie(e) {
341
341
  const t = new Uint8Array(e.length);
342
342
  for (let r = 0; r < e.length; r++)
343
343
  t[r] = e.charCodeAt(r) & 127;
344
344
  return t;
345
345
  }
346
- function ae() {
346
+ function oe() {
347
347
  if (!("storage" in navigator) || !("getDirectory" in navigator.storage))
348
- throw new J();
348
+ throw new ee();
349
349
  }
350
- function y(e) {
350
+ function m(e) {
351
351
  return Array.isArray(e) ? e : e.split("/").filter(Boolean);
352
352
  }
353
- function P(e) {
353
+ function H(e) {
354
354
  return typeof e == "string" ? e ?? "/" : \`/\${e.join("/")}\`;
355
355
  }
356
- function oe(e, t = "utf-8") {
357
- return typeof e == "string" ? Q(e, t) : e instanceof Uint8Array ? e : new Uint8Array(e);
356
+ function I(e) {
357
+ const t = m(e);
358
+ return t[t.length - 1] || "";
359
+ }
360
+ function x(e) {
361
+ const t = m(e);
362
+ return t.pop(), H(t);
363
+ }
364
+ function O(e) {
365
+ return !e || e === "/" ? "/" : e.startsWith("/") ? e : \`/\${e}\`;
366
+ }
367
+ function ce(e) {
368
+ const t = m(e), r = [];
369
+ for (const a of t)
370
+ if (!(a === "." || a === ""))
371
+ if (a === "..") {
372
+ if (r.length === 0)
373
+ continue;
374
+ r.pop();
375
+ } else
376
+ r.push(a);
377
+ return H(r);
378
+ }
379
+ function le(e, t = "utf-8") {
380
+ return typeof e == "string" ? te(e, t) : e instanceof Uint8Array ? e : new Uint8Array(e);
358
381
  }
359
- async function se(e) {
382
+ async function fe(e) {
360
383
  const t = await e.createSyncAccessHandle();
361
384
  try {
362
385
  const r = t.getSize(), a = new Uint8Array(r);
@@ -365,16 +388,16 @@ async function se(e) {
365
388
  t.close();
366
389
  }
367
390
  }
368
- async function H(e, t, r, a = {}) {
391
+ async function C(e, t, r, a = {}) {
369
392
  let n = null;
370
393
  try {
371
394
  n = await e.createSyncAccessHandle();
372
- const o = oe(t, r), s = a.append ? n.getSize() : 0;
373
- n.write(o, { at: s }), a.truncate && !a.append && n.truncate(o.byteLength), n.flush();
374
- } catch (o) {
375
- console.error(o);
376
- const s = a.append ? "append" : "write";
377
- throw new l(\`Failed to \${s} file\`, \`\${s.toUpperCase()}_FAILED\`);
395
+ const s = le(t, r), o = a.append ? n.getSize() : 0;
396
+ n.write(s, { at: o }), a.truncate && !a.append && n.truncate(s.byteLength), n.flush();
397
+ } catch (s) {
398
+ console.error(s);
399
+ const o = a.append ? "append" : "write";
400
+ throw new l(\`Failed to \${o} file\`, \`\${o.toUpperCase()}_FAILED\`);
378
401
  } finally {
379
402
  if (n)
380
403
  try {
@@ -383,24 +406,38 @@ async function H(e, t, r, a = {}) {
383
406
  }
384
407
  }
385
408
  }
386
- async function ie(e, t = "SHA-1") {
409
+ async function ue(e, t = "SHA-1") {
387
410
  try {
388
411
  const r = new Uint8Array(e), a = await crypto.subtle.digest(t, r);
389
- return Array.from(new Uint8Array(a)).map((o) => o.toString(16).padStart(2, "0")).join("");
412
+ return Array.from(new Uint8Array(a)).map((s) => s.toString(16).padStart(2, "0")).join("");
390
413
  } catch (r) {
391
414
  throw console.warn(\`Failed to calculate \${t} hash:\`, r), r;
392
415
  }
393
416
  }
394
- class ce {
417
+ async function he(e) {
418
+ const t = await e.arrayBuffer();
419
+ return new Uint8Array(t);
420
+ }
421
+ class we {
395
422
  /** Root directory handle for the file system */
396
423
  root = null;
424
+ /** Watch event callback */
425
+ watchCallback = null;
426
+ /** Map of watched paths to their last known state */
427
+ watchers = /* @__PURE__ */ new Map();
428
+ /** Interval handle for polling watched paths */
429
+ watchTimer = null;
430
+ /** Polling interval in milliseconds */
431
+ watchInterval = 1e3;
432
+ /** Flag to avoid concurrent scans */
433
+ scanning = !1;
397
434
  /**
398
435
  * Creates a new OPFSFileSystem instance
399
436
  *
400
437
  * @throws {OPFSError} If OPFS is not supported in the current browser
401
438
  */
402
439
  constructor() {
403
- ae();
440
+ oe();
404
441
  }
405
442
  /**
406
443
  * Initialize the file system within a given directory
@@ -418,12 +455,12 @@ class ce {
418
455
  * const success = await fs.init('/my-app');
419
456
  * \`\`\`
420
457
  */
421
- async mount(t = "/") {
458
+ async mount(t = "/", r, a) {
422
459
  try {
423
- const r = await navigator.storage.getDirectory();
424
- return this.root = await this.getDirectoryHandle(t, !0, r), !0;
425
- } catch (r) {
426
- throw console.error(r), new l("Failed to initialize OPFS", "INIT_FAILED");
460
+ const n = await navigator.storage.getDirectory();
461
+ return this.root = await this.getDirectoryHandle(t, !0, n), r && (this.watchCallback = r, a?.watchInterval && (this.watchInterval = a.watchInterval)), !0;
462
+ } catch (n) {
463
+ throw console.error(n), new l("Failed to initialize OPFS", "INIT_FAILED");
427
464
  }
428
465
  }
429
466
  /**
@@ -446,12 +483,12 @@ class ce {
446
483
  */
447
484
  async getDirectoryHandle(t, r = !1, a = this.root) {
448
485
  if (!a)
449
- throw new T();
450
- const n = Array.isArray(t) ? t : y(t);
451
- let o = a;
452
- for (const s of n)
453
- o = await o.getDirectoryHandle(s, { create: r });
454
- return o;
486
+ throw new D();
487
+ const n = Array.isArray(t) ? t : m(t);
488
+ let s = a;
489
+ for (const o of n)
490
+ s = await s.getDirectoryHandle(o, { create: r });
491
+ return s;
455
492
  }
456
493
  /**
457
494
  * Get a file handle from a path
@@ -474,12 +511,12 @@ class ce {
474
511
  */
475
512
  async getFileHandle(t, r = !1, a = this.root) {
476
513
  if (!a)
477
- throw new T();
478
- const n = y(t);
514
+ throw new D();
515
+ const n = m(t);
479
516
  if (n.length === 0)
480
- throw new N("Path must not be empty", Array.isArray(t) ? t.join("/") : t);
481
- const o = n.pop();
482
- return (await this.getDirectoryHandle(n, r, a)).getFileHandle(o, { create: r });
517
+ throw new v("Path must not be empty", Array.isArray(t) ? t.join("/") : t);
518
+ const s = n.pop();
519
+ return (await this.getDirectoryHandle(n, r, a)).getFileHandle(s, { create: r });
483
520
  }
484
521
  /**
485
522
  * Recursively list all files and directories with their stats
@@ -520,14 +557,14 @@ class ce {
520
557
  */
521
558
  async index(t) {
522
559
  const r = /* @__PURE__ */ new Map(), a = async (n) => {
523
- const o = await this.readdir(n, { withFileTypes: !0 });
524
- for (const s of o) {
525
- const c = \`\${n === "/" ? "" : n}/\${s.name}\`;
560
+ const s = await this.readdir(n, { withFileTypes: !0 });
561
+ for (const o of s) {
562
+ const i = \`\${n === "/" ? "" : n}/\${o.name}\`;
526
563
  try {
527
- const i = await this.stat(c, t);
528
- r.set(c, i), i.isDirectory && await a(c);
529
- } catch (i) {
530
- console.warn(\`Skipping broken entry: \${c}\`, i);
564
+ const c = await this.stat(i, t);
565
+ r.set(i, c), c.isDirectory && await a(i);
566
+ } catch (c) {
567
+ console.warn(\`Skipping broken entry: \${i}\`, c);
531
568
  }
532
569
  }
533
570
  };
@@ -542,10 +579,10 @@ class ce {
542
579
  }
543
580
  async readFile(t, r = "utf-8") {
544
581
  try {
545
- const a = await this.getFileHandle(t, !1), n = await se(a);
546
- return r === "binary" ? n : Z(n, r);
582
+ const a = await this.getFileHandle(t, !1), n = await fe(a);
583
+ return r === "binary" ? n : re(n, r);
547
584
  } catch (a) {
548
- throw console.error(a), new x(t);
585
+ throw console.error(a), new b(t);
549
586
  }
550
587
  }
551
588
  /**
@@ -575,7 +612,7 @@ class ce {
575
612
  */
576
613
  async writeFile(t, r, a) {
577
614
  const n = await this.getFileHandle(t, !0);
578
- await H(n, r, a, { truncate: !0 });
615
+ await C(n, r, a, { truncate: !0 });
579
616
  }
580
617
  /**
581
618
  * Append data to a file
@@ -601,7 +638,7 @@ class ce {
601
638
  */
602
639
  async appendFile(t, r, a) {
603
640
  const n = await this.getFileHandle(t, !0);
604
- await H(n, r, a, { append: !0 });
641
+ await C(n, r, a, { append: !0 });
605
642
  }
606
643
  /**
607
644
  * Create a directory
@@ -626,18 +663,18 @@ class ce {
626
663
  */
627
664
  async mkdir(t, r) {
628
665
  if (!this.root)
629
- throw new T();
630
- const a = r?.recursive ?? !1, n = y(t);
631
- let o = this.root;
632
- for (let s = 0; s < n.length; s++) {
633
- const c = n[s];
666
+ throw new D();
667
+ const a = r?.recursive ?? !1, n = m(t);
668
+ let s = this.root;
669
+ for (let o = 0; o < n.length; o++) {
670
+ const i = n[o];
634
671
  try {
635
- o = await o.getDirectoryHandle(c, { create: a || s === n.length - 1 });
636
- } catch (i) {
637
- throw i.name === "NotFoundError" ? new l(
638
- \`Parent directory does not exist: \${P(n.slice(0, s + 1))}\`,
672
+ s = await s.getDirectoryHandle(i, { create: a || o === n.length - 1 });
673
+ } catch (c) {
674
+ throw c.name === "NotFoundError" ? new l(
675
+ \`Parent directory does not exist: \${H(n.slice(0, o + 1))}\`,
639
676
  "ENOENT"
640
- ) : i.name === "TypeMismatchError" ? new l(\`Path segment is not a directory: \${c}\`, "ENOTDIR") : new l("Failed to create directory", "MKDIR_FAILED");
677
+ ) : c.name === "TypeMismatchError" ? new l(\`Path segment is not a directory: \${i}\`, "ENOTDIR") : new l("Failed to create directory", "MKDIR_FAILED");
641
678
  }
642
679
  }
643
680
  }
@@ -671,22 +708,22 @@ class ce {
671
708
  * \`\`\`
672
709
  */
673
710
  async stat(t, r) {
674
- const a = y(t), n = a.pop(), o = await this.getDirectoryHandle(a, !1), s = r?.includeHash ?? !1, c = r?.hashAlgorithm ?? "SHA-1";
711
+ const a = I(t), n = await this.getDirectoryHandle(x(t), !1), s = r?.includeHash ?? !1, o = r?.hashAlgorithm ?? "SHA-1";
675
712
  try {
676
- const u = await (await o.getFileHandle(n, { create: !1 })).getFile(), f = {
713
+ const c = await (await n.getFileHandle(a, { create: !1 })).getFile(), f = {
677
714
  kind: "file",
678
- size: u.size,
679
- mtime: new Date(u.lastModified).toISOString(),
680
- ctime: new Date(u.lastModified).toISOString(),
715
+ size: c.size,
716
+ mtime: new Date(c.lastModified).toISOString(),
717
+ ctime: new Date(c.lastModified).toISOString(),
681
718
  isFile: !0,
682
719
  isDirectory: !1
683
720
  };
684
721
  if (s)
685
722
  try {
686
- const w = new Uint8Array(await u.arrayBuffer()), h = await ie(w, c);
687
- f.hash = h;
688
- } catch (w) {
689
- console.warn(\`Failed to calculate hash for \${t}:\`, w);
723
+ const u = new Uint8Array(await c.arrayBuffer()), w = await ue(u, o);
724
+ f.hash = w;
725
+ } catch (u) {
726
+ console.warn(\`Failed to calculate hash for \${t}:\`, u);
690
727
  }
691
728
  return f;
692
729
  } catch (i) {
@@ -694,14 +731,13 @@ class ce {
694
731
  throw new l("Failed to stat (file)", "STAT_FAILED");
695
732
  }
696
733
  try {
697
- return await o.getDirectoryHandle(n, { create: !1 }), {
734
+ return await n.getDirectoryHandle(a, { create: !1 }), {
698
735
  kind: "directory",
699
736
  size: 0,
700
737
  mtime: (/* @__PURE__ */ new Date(0)).toISOString(),
701
738
  ctime: (/* @__PURE__ */ new Date(0)).toISOString(),
702
739
  isFile: !1,
703
740
  isDirectory: !0
704
- // Directories don't have hashes
705
741
  };
706
742
  } catch (i) {
707
743
  throw i.name === "NotFoundError" ? new l(\`No such file or directory: \${t}\`, "ENOENT") : new l("Failed to stat (directory)", "STAT_FAILED");
@@ -710,22 +746,22 @@ class ce {
710
746
  async readdir(t, r) {
711
747
  const a = r?.withFileTypes ?? !1, n = await this.getDirectoryHandle(t, !1);
712
748
  if (a) {
713
- const o = [];
714
- for await (const [s, c] of n.entries()) {
715
- const i = c.kind === "file";
716
- o.push({
717
- name: s,
718
- kind: c.kind,
719
- isFile: i,
720
- isDirectory: !i
749
+ const s = [];
750
+ for await (const [o, i] of n.entries()) {
751
+ const c = i.kind === "file";
752
+ s.push({
753
+ name: o,
754
+ kind: i.kind,
755
+ isFile: c,
756
+ isDirectory: !c
721
757
  });
722
758
  }
723
- return o;
759
+ return s;
724
760
  } else {
725
- const o = [];
726
- for await (const [s] of n.entries())
727
- o.push(s);
728
- return o;
761
+ const s = [];
762
+ for await (const [o] of n.entries())
763
+ s.push(o);
764
+ return s;
729
765
  }
730
766
  }
731
767
  /**
@@ -743,26 +779,26 @@ class ce {
743
779
  * \`\`\`
744
780
  */
745
781
  async exists(t) {
746
- const r = y(t), a = r.pop();
747
- let n = null;
782
+ const r = I(t);
783
+ let a = null;
748
784
  try {
749
- n = await this.getDirectoryHandle(r, !1);
750
- } catch (o) {
751
- throw (o.name === "NotFoundError" || o.name === "TypeMismatchError") && (n = null), o;
785
+ a = await this.getDirectoryHandle(x(t), !1);
786
+ } catch (n) {
787
+ throw (n.name === "NotFoundError" || n.name === "TypeMismatchError") && (a = null), n;
752
788
  }
753
- if (!n || !a)
789
+ if (!a || !r)
754
790
  return !1;
755
791
  try {
756
- return await n.getFileHandle(a, { create: !1 }), !0;
757
- } catch (o) {
758
- if (o.name !== "NotFoundError" && o.name !== "TypeMismatchError")
759
- throw o;
792
+ return await a.getFileHandle(r, { create: !1 }), !0;
793
+ } catch (n) {
794
+ if (n.name !== "NotFoundError" && n.name !== "TypeMismatchError")
795
+ throw n;
760
796
  }
761
797
  try {
762
- return await n.getDirectoryHandle(a, { create: !1 }), !0;
763
- } catch (o) {
764
- if (o.name !== "NotFoundError" && o.name !== "TypeMismatchError")
765
- throw o;
798
+ return await a.getDirectoryHandle(r, { create: !1 }), !0;
799
+ } catch (n) {
800
+ if (n.name !== "NotFoundError" && n.name !== "TypeMismatchError")
801
+ throw n;
766
802
  }
767
803
  return !1;
768
804
  }
@@ -821,12 +857,12 @@ class ce {
821
857
  * \`\`\`
822
858
  */
823
859
  async remove(t, r) {
824
- const a = r?.recursive ?? !1, n = r?.force ?? !1, o = y(t), s = o.pop();
860
+ const a = r?.recursive ?? !1, n = r?.force ?? !1, s = I(t);
825
861
  if (!s)
826
- throw new N("Invalid path", t);
827
- const c = await this.getDirectoryHandle(o, !1);
862
+ throw new v("Invalid path", t);
863
+ const o = await this.getDirectoryHandle(x(t), !1);
828
864
  try {
829
- await c.removeEntry(s, { recursive: a });
865
+ await o.removeEntry(s, { recursive: a });
830
866
  } catch (i) {
831
867
  if (i.name === "NotFoundError") {
832
868
  if (!n)
@@ -854,19 +890,10 @@ class ce {
854
890
  */
855
891
  async realpath(t) {
856
892
  try {
857
- const r = y(t), a = [];
858
- for (const s of r)
859
- if (!(s === "." || s === ""))
860
- if (s === "..") {
861
- if (a.length === 0)
862
- throw new l("Path escapes root", "EINVAL");
863
- a.length > 0 && a.pop();
864
- } else
865
- a.push(s);
866
- const n = P(a);
867
- if (!await this.exists(n))
868
- throw new x(n);
869
- return n;
893
+ const r = ce(t);
894
+ if (!await this.exists(r))
895
+ throw new b(r);
896
+ return r;
870
897
  } catch (r) {
871
898
  throw r instanceof l ? r : new l(\`Failed to resolve path: \${t}\`, "REALPATH_FAILED");
872
899
  }
@@ -890,7 +917,7 @@ class ce {
890
917
  async rename(t, r) {
891
918
  try {
892
919
  if (!await this.exists(t))
893
- throw new x(t);
920
+ throw new b(t);
894
921
  await this.copy(t, r, { recursive: !0 }), await this.remove(t, { recursive: !0 });
895
922
  } catch (a) {
896
923
  throw a instanceof l ? a : new l(\`Failed to rename from \${t} to \${r}\`, "RENAME_FAILED");
@@ -923,28 +950,87 @@ class ce {
923
950
  */
924
951
  async copy(t, r, a) {
925
952
  try {
926
- const n = a?.recursive ?? !1, o = a?.force ?? !0;
953
+ const n = a?.recursive ?? !1, s = a?.force ?? !0;
927
954
  if (!await this.exists(t))
928
955
  throw new l(\`Source does not exist: \${t}\`, "ENOENT");
929
- if (await this.exists(r) && !o)
956
+ if (await this.exists(r) && !s)
930
957
  throw new l(\`Destination already exists: \${r}\`, "EEXIST");
931
958
  if ((await this.stat(t)).isFile) {
932
- const u = await this.readFile(t, "binary");
933
- await this.writeFile(r, u);
959
+ const f = await this.readFile(t, "binary");
960
+ await this.writeFile(r, f);
934
961
  } else {
935
962
  if (!n)
936
963
  throw new l(\`Cannot copy directory without recursive option: \${t}\`, "EISDIR");
937
964
  await this.mkdir(r, { recursive: !0 });
938
- const u = await this.readdir(t, { withFileTypes: !0 });
939
- for (const f of u) {
940
- const w = \`\${t}/\${f.name}\`, h = \`\${r}/\${f.name}\`;
941
- await this.copy(w, h, { recursive: !0, force: o });
965
+ const f = await this.readdir(t, { withFileTypes: !0 });
966
+ for (const u of f) {
967
+ const w = \`\${t}/\${u.name}\`, h = \`\${r}/\${u.name}\`;
968
+ await this.copy(w, h, { recursive: !0, force: s });
942
969
  }
943
970
  }
944
971
  } catch (n) {
945
972
  throw n instanceof l ? n : new l(\`Failed to copy from \${t} to \${r}\`, "CP_FAILED");
946
973
  }
947
974
  }
975
+ /**
976
+ * Start watching a file or directory for changes
977
+ */
978
+ async watch(t) {
979
+ const r = O(t), a = await this.buildSnapshot(r);
980
+ this.watchers.set(r, a), this.watchTimer || (this.watchTimer = setInterval(() => {
981
+ this.scanWatches();
982
+ }, this.watchInterval));
983
+ }
984
+ /**
985
+ * Stop watching a previously watched path
986
+ */
987
+ unwatch(t) {
988
+ const r = O(t);
989
+ this.watchers.delete(r), this.watchers.size === 0 && this.watchTimer && (clearInterval(this.watchTimer), this.watchTimer = null);
990
+ }
991
+ async buildSnapshot(t) {
992
+ const r = /* @__PURE__ */ new Map(), a = async (n) => {
993
+ const s = await this.stat(n);
994
+ if (r.set(n, s), s.isDirectory) {
995
+ const o = await this.readdir(n, { withFileTypes: !0 });
996
+ for (const i of o) {
997
+ const c = \`\${n === "/" ? "" : n}/\${i.name}\`;
998
+ await a(c);
999
+ }
1000
+ }
1001
+ };
1002
+ return await a(t), r;
1003
+ }
1004
+ async scanWatches() {
1005
+ if (!this.scanning) {
1006
+ this.scanning = !0;
1007
+ try {
1008
+ await Promise.all(
1009
+ [...this.watchers.entries()].map(async ([t, r]) => {
1010
+ let a;
1011
+ try {
1012
+ a = await this.buildSnapshot(t);
1013
+ } catch {
1014
+ a = /* @__PURE__ */ new Map();
1015
+ }
1016
+ const n = [];
1017
+ for (const [s, o] of a) {
1018
+ const i = r.get(s);
1019
+ i ? (i.mtime !== o.mtime || i.size !== o.size) && n.push({ path: s, type: "change" }) : n.push({ path: s, type: "create" });
1020
+ }
1021
+ for (const s of r.keys())
1022
+ a.has(s) || n.push({ path: s, type: "delete" });
1023
+ if (n.length && this.watchCallback)
1024
+ for (const s of n)
1025
+ this.watchCallback(s);
1026
+ this.watchers.set(t, a);
1027
+ })
1028
+ );
1029
+ } finally {
1030
+ this.scanning = !1;
1031
+ }
1032
+ }
1033
+ }
948
1034
  /**
949
1035
  * Synchronize the file system with external data
950
1036
  *
@@ -976,22 +1062,17 @@ class ce {
976
1062
  async sync(t, r) {
977
1063
  try {
978
1064
  (r?.cleanBefore ?? !1) && await this.clear("/");
979
- for (const [n, o] of t) {
980
- const s = n.startsWith("/") ? n : \`/\${n}\`;
981
- let c;
982
- if (o instanceof Blob) {
983
- const i = await o.arrayBuffer();
984
- c = new Uint8Array(i);
985
- } else
986
- c = o;
987
- await this.writeFile(s, c);
1065
+ for (const [n, s] of t) {
1066
+ const o = O(n);
1067
+ let i;
1068
+ s instanceof Blob ? i = await he(s) : i = s, await this.writeFile(o, i);
988
1069
  }
989
1070
  } catch (a) {
990
1071
  throw a instanceof l ? a : new l("Failed to sync file system", "SYNC_FAILED");
991
1072
  }
992
1073
  }
993
1074
  }
994
- O(new ce());
995
- //# sourceMappingURL=worker-BiWuxhcz.js.map
996
- `,r=typeof self<"u"&&self.Blob&&new Blob(["URL.revokeObjectURL(import.meta.url);",a],{type:"text/javascript;charset=utf-8"});function i(t){let n;try{if(n=r&&(self.URL||self.webkitURL).createObjectURL(r),!n)throw"";const e=new Worker(n,{type:"module",name:t?.name});return e.addEventListener("error",()=>{(self.URL||self.webkitURL).revokeObjectURL(n)}),e}catch{return new Worker("data:text/javascript;charset=utf-8,"+encodeURIComponent(a),{type:"module",name:t?.name})}}function s(){return o.wrap(new i)}exports.createWorker=s;
1075
+ typeof self < "u" && self.constructor.name === "DedicatedWorkerGlobalScope" && N(new we());
1076
+ //# sourceMappingURL=worker-ChfzBM-o.js.map
1077
+ `,a=typeof self<"u"&&self.Blob&&new Blob(["URL.revokeObjectURL(import.meta.url);",i],{type:"text/javascript;charset=utf-8"});function s(r){let e;try{if(e=a&&(self.URL||self.webkitURL).createObjectURL(a),!e)throw"";const t=new Worker(e,{type:"module",name:r?.name});return t.addEventListener("error",()=>{(self.URL||self.webkitURL).revokeObjectURL(e)}),t}catch{return new Worker("data:text/javascript;charset=utf-8,"+encodeURIComponent(i),{type:"module",name:r?.name})}}function c(){return o.wrap(new s)}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;
997
1078
  //# sourceMappingURL=index.cjs.map