opfs-worker 0.2.0 → 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/raw.js CHANGED
@@ -1,151 +1,6 @@
1
- import { expose as E } from "comlink";
2
- class o extends Error {
3
- constructor(e, t, r) {
4
- super(e), this.code = t, this.path = r, this.name = "OPFSError";
5
- }
6
- }
7
- class S extends o {
8
- constructor() {
9
- super("OPFS is not supported in this browser", "OPFS_NOT_SUPPORTED");
10
- }
11
- }
12
- class y extends o {
13
- constructor() {
14
- super("OPFS is not mounted", "OPFS_NOT_MOUNTED");
15
- }
16
- }
17
- class g extends o {
18
- constructor(e, t) {
19
- super(e, "INVALID_PATH", t);
20
- }
21
- }
22
- class m extends o {
23
- constructor(e) {
24
- super(`File not found: ${e}`, "FILE_NOT_FOUND", e);
25
- }
26
- }
27
- function A(a, e = "utf-8") {
28
- switch (e) {
29
- case "utf8":
30
- case "utf-8":
31
- return new TextEncoder().encode(a);
32
- case "utf16le":
33
- case "ucs2":
34
- case "ucs-2":
35
- return I(a);
36
- case "ascii":
37
- return $(a);
38
- case "latin1":
39
- return v(a);
40
- case "binary":
41
- return Uint8Array.from(a, (t) => t.charCodeAt(0));
42
- case "base64":
43
- return Uint8Array.from(atob(a), (t) => t.charCodeAt(0));
44
- case "hex":
45
- if (!/^[\da-f]+$/i.test(a) || a.length % 2 !== 0)
46
- throw new o("Invalid hex string", "INVALID_HEX_FORMAT");
47
- return Uint8Array.from(a.match(/.{1,2}/g).map((t) => parseInt(t, 16)));
48
- default:
49
- return console.warn("Encoding not supported, falling back to UTF-8"), new TextEncoder().encode(a);
50
- }
51
- }
52
- function D(a, e = "utf-8") {
53
- switch (e) {
54
- case "utf8":
55
- case "utf-8":
56
- return new TextDecoder().decode(a);
57
- case "utf16le":
58
- case "ucs2":
59
- case "ucs-2":
60
- return T(a);
61
- case "latin1":
62
- return String.fromCharCode(...a);
63
- case "binary":
64
- return String.fromCharCode(...a);
65
- case "ascii":
66
- return String.fromCharCode(...a.map((t) => t & 127));
67
- case "base64":
68
- return btoa(String.fromCharCode(...a));
69
- case "hex":
70
- return Array.from(a).map((t) => t.toString(16).padStart(2, "0")).join("");
71
- default:
72
- return console.warn("Unsupported encoding, falling back to UTF-8"), new TextDecoder().decode(a);
73
- }
74
- }
75
- function I(a) {
76
- const e = new Uint8Array(a.length * 2);
77
- for (let t = 0; t < a.length; t++) {
78
- const r = a.charCodeAt(t);
79
- e[t * 2] = r & 255, e[t * 2 + 1] = r >> 8;
80
- }
81
- return e;
82
- }
83
- function T(a) {
84
- a.length % 2 !== 0 && (console.warn("Invalid UTF-16LE buffer length, truncating last byte"), a = a.slice(0, a.length - 1));
85
- const e = new Uint16Array(a.buffer, a.byteOffset, a.byteLength / 2);
86
- return String.fromCharCode(...e);
87
- }
88
- function v(a) {
89
- const e = new Uint8Array(a.length);
90
- for (let t = 0; t < a.length; t++)
91
- e[t] = a.charCodeAt(t) & 255;
92
- return e;
93
- }
94
- function $(a) {
95
- const e = new Uint8Array(a.length);
96
- for (let t = 0; t < a.length; t++)
97
- e[t] = a.charCodeAt(t) & 127;
98
- return e;
99
- }
100
- function N() {
101
- if (!("storage" in navigator) || !("getDirectory" in navigator.storage))
102
- throw new S();
103
- }
104
- function f(a) {
105
- return Array.isArray(a) ? a : a.split("/").filter(Boolean);
106
- }
107
- function p(a) {
108
- return typeof a == "string" ? a ?? "/" : `/${a.join("/")}`;
109
- }
110
- function H(a, e = "utf-8") {
111
- return typeof a == "string" ? A(a, e) : a instanceof Uint8Array ? a : new Uint8Array(a);
112
- }
113
- async function x(a) {
114
- const e = await a.createSyncAccessHandle();
115
- try {
116
- const t = e.getSize(), r = new Uint8Array(t);
117
- return e.read(r, { at: 0 }), r;
118
- } finally {
119
- e.close();
120
- }
121
- }
122
- async function F(a, e, t, r = {}) {
123
- let n = null;
124
- try {
125
- n = await a.createSyncAccessHandle();
126
- const i = H(e, t), s = r.append ? n.getSize() : 0;
127
- n.write(i, { at: s }), r.truncate && !r.append && n.truncate(i.byteLength), n.flush();
128
- } catch (i) {
129
- console.error(i);
130
- const s = r.append ? "append" : "write";
131
- throw new o(`Failed to ${s} file`, `${s.toUpperCase()}_FAILED`);
132
- } finally {
133
- if (n)
134
- try {
135
- n.close();
136
- } catch {
137
- }
138
- }
139
- }
140
- async function O(a, e = "SHA-1") {
141
- try {
142
- const t = new Uint8Array(a), r = await crypto.subtle.digest(e, t);
143
- return Array.from(new Uint8Array(r)).map((i) => i.toString(16).padStart(2, "0")).join("");
144
- } catch (t) {
145
- throw console.warn(`Failed to calculate ${e} hash:`, t), t;
146
- }
147
- }
148
- class U {
1
+ import { expose as p } from "comlink";
2
+ import { d as v, O as o, b as f, s as y, P as E, i as I, o as S, F as d, w as g, j as T, e as u, f as m, k as H, r as $, n as F, l as A } from "./helpers-Co-qCBmA.js";
3
+ class N {
149
4
  /** Root directory handle for the file system */
150
5
  root = null;
151
6
  /** Watch event callback */
@@ -164,7 +19,7 @@ class U {
164
19
  * @throws {OPFSError} If OPFS is not supported in the current browser
165
20
  */
166
21
  constructor() {
167
- N();
22
+ v();
168
23
  }
169
24
  /**
170
25
  * Initialize the file system within a given directory
@@ -182,12 +37,12 @@ class U {
182
37
  * const success = await fs.init('/my-app');
183
38
  * ```
184
39
  */
185
- async mount(e = "/", t, r) {
40
+ async mount(t = "/", e, a) {
186
41
  try {
187
- const n = await navigator.storage.getDirectory();
188
- return this.root = await this.getDirectoryHandle(e, !0, n), t && (this.watchCallback = t, r?.watchInterval && (this.watchInterval = r.watchInterval)), !0;
189
- } catch (n) {
190
- throw console.error(n), new o("Failed to initialize OPFS", "INIT_FAILED");
42
+ const r = await navigator.storage.getDirectory();
43
+ return this.root = await this.getDirectoryHandle(t, !0, r), e && (this.watchCallback = e, a?.watchInterval && (this.watchInterval = a.watchInterval)), !0;
44
+ } catch (r) {
45
+ throw console.error(r), new o("Failed to initialize OPFS", "INIT_FAILED");
191
46
  }
192
47
  }
193
48
  /**
@@ -208,13 +63,13 @@ class U {
208
63
  * const docsDir2 = await fs.getDirectoryHandle(['users', 'john', 'documents'], true);
209
64
  * ```
210
65
  */
211
- async getDirectoryHandle(e, t = !1, r = this.root) {
212
- if (!r)
213
- throw new y();
214
- const n = Array.isArray(e) ? e : f(e);
215
- let i = r;
216
- for (const s of n)
217
- i = await i.getDirectoryHandle(s, { create: t });
66
+ async getDirectoryHandle(t, e = !1, a = this.root) {
67
+ if (!a)
68
+ throw new f();
69
+ const r = Array.isArray(t) ? t : y(t);
70
+ let i = a;
71
+ for (const n of r)
72
+ i = await i.getDirectoryHandle(n, { create: e });
218
73
  return i;
219
74
  }
220
75
  /**
@@ -236,14 +91,14 @@ class U {
236
91
  * const fileHandle2 = await fs.getFileHandle(['config', 'settings.json'], true);
237
92
  * ```
238
93
  */
239
- async getFileHandle(e, t = !1, r = this.root) {
240
- if (!r)
241
- throw new y();
242
- const n = f(e);
243
- if (n.length === 0)
244
- throw new g("Path must not be empty", Array.isArray(e) ? e.join("/") : e);
245
- const i = n.pop();
246
- return (await this.getDirectoryHandle(n, t, r)).getFileHandle(i, { create: t });
94
+ async getFileHandle(t, e = !1, a = this.root) {
95
+ if (!a)
96
+ throw new f();
97
+ const r = y(t);
98
+ if (r.length === 0)
99
+ throw new E("Path must not be empty", Array.isArray(t) ? t.join("/") : t);
100
+ const i = r.pop();
101
+ return (await this.getDirectoryHandle(r, e, a)).getFileHandle(i, { create: e });
247
102
  }
248
103
  /**
249
104
  * Recursively list all files and directories with their stats
@@ -282,34 +137,34 @@ class U {
282
137
  * }
283
138
  * ```
284
139
  */
285
- async index(e) {
286
- const t = /* @__PURE__ */ new Map(), r = async (n) => {
287
- const i = await this.readdir(n, { withFileTypes: !0 });
288
- for (const s of i) {
289
- const c = `${n === "/" ? "" : n}/${s.name}`;
140
+ async index(t) {
141
+ const e = /* @__PURE__ */ new Map(), a = async (r) => {
142
+ const i = await this.readdir(r, { withFileTypes: !0 });
143
+ for (const n of i) {
144
+ const s = `${r === "/" ? "" : r}/${n.name}`;
290
145
  try {
291
- const l = await this.stat(c, e);
292
- t.set(c, l), l.isDirectory && await r(c);
293
- } catch (l) {
294
- console.warn(`Skipping broken entry: ${c}`, l);
146
+ const c = await this.stat(s, t);
147
+ e.set(s, c), c.isDirectory && await a(s);
148
+ } catch (c) {
149
+ console.warn(`Skipping broken entry: ${s}`, c);
295
150
  }
296
151
  }
297
152
  };
298
- return t.set("/", {
153
+ return e.set("/", {
299
154
  kind: "directory",
300
155
  size: 0,
301
156
  mtime: (/* @__PURE__ */ new Date(0)).toISOString(),
302
157
  ctime: (/* @__PURE__ */ new Date(0)).toISOString(),
303
158
  isFile: !1,
304
159
  isDirectory: !0
305
- }), await r("/"), t;
160
+ }), await a("/"), e;
306
161
  }
307
- async readFile(e, t = "utf-8") {
162
+ async readFile(t, e = "utf-8") {
308
163
  try {
309
- const r = await this.getFileHandle(e, !1), n = await x(r);
310
- return t === "binary" ? n : D(n, t);
311
- } catch (r) {
312
- throw console.error(r), new m(e);
164
+ const a = await this.getFileHandle(t, !1), r = await I(a);
165
+ return e === "binary" ? r : S(r, e);
166
+ } catch (a) {
167
+ throw console.error(a), new d(t);
313
168
  }
314
169
  }
315
170
  /**
@@ -337,9 +192,9 @@ class U {
337
192
  * await fs.writeFile('/data/utf16.txt', 'Hello World', 'utf-16le');
338
193
  * ```
339
194
  */
340
- async writeFile(e, t, r) {
341
- const n = await this.getFileHandle(e, !0);
342
- await F(n, t, r, { truncate: !0 });
195
+ async writeFile(t, e, a) {
196
+ const r = await this.getFileHandle(t, !0);
197
+ await g(r, e, a, { truncate: !0 });
343
198
  }
344
199
  /**
345
200
  * Append data to a file
@@ -363,9 +218,9 @@ class U {
363
218
  * await fs.appendFile('/data/binary.dat', additionalData);
364
219
  * ```
365
220
  */
366
- async appendFile(e, t, r) {
367
- const n = await this.getFileHandle(e, !0);
368
- await F(n, t, r, { append: !0 });
221
+ async appendFile(t, e, a) {
222
+ const r = await this.getFileHandle(t, !0);
223
+ await g(r, e, a, { append: !0 });
369
224
  }
370
225
  /**
371
226
  * Create a directory
@@ -388,20 +243,20 @@ class U {
388
243
  * await fs.mkdir('/users/john/documents/projects', { recursive: true });
389
244
  * ```
390
245
  */
391
- async mkdir(e, t) {
246
+ async mkdir(t, e) {
392
247
  if (!this.root)
393
- throw new y();
394
- const r = t?.recursive ?? !1, n = f(e);
248
+ throw new f();
249
+ const a = e?.recursive ?? !1, r = y(t);
395
250
  let i = this.root;
396
- for (let s = 0; s < n.length; s++) {
397
- const c = n[s];
251
+ for (let n = 0; n < r.length; n++) {
252
+ const s = r[n];
398
253
  try {
399
- i = await i.getDirectoryHandle(c, { create: r || s === n.length - 1 });
400
- } catch (l) {
401
- throw l.name === "NotFoundError" ? new o(
402
- `Parent directory does not exist: ${p(n.slice(0, s + 1))}`,
254
+ i = await i.getDirectoryHandle(s, { create: a || n === r.length - 1 });
255
+ } catch (c) {
256
+ throw c.name === "NotFoundError" ? new o(
257
+ `Parent directory does not exist: ${T(r.slice(0, n + 1))}`,
403
258
  "ENOENT"
404
- ) : l.name === "TypeMismatchError" ? new o(`Path segment is not a directory: ${c}`, "ENOTDIR") : new o("Failed to create directory", "MKDIR_FAILED");
259
+ ) : c.name === "TypeMismatchError" ? new o(`Path segment is not a directory: ${s}`, "ENOTDIR") : new o("Failed to create directory", "MKDIR_FAILED");
405
260
  }
406
261
  }
407
262
  }
@@ -434,61 +289,60 @@ class U {
434
289
  * console.log(`Hash: ${statsWithHash.hash}`);
435
290
  * ```
436
291
  */
437
- async stat(e, t) {
438
- const r = f(e), n = r.pop(), i = await this.getDirectoryHandle(r, !1), s = t?.includeHash ?? !1, c = t?.hashAlgorithm ?? "SHA-1";
292
+ async stat(t, e) {
293
+ const a = u(t), r = await this.getDirectoryHandle(m(t), !1), i = e?.includeHash ?? !1, n = e?.hashAlgorithm ?? "SHA-1";
439
294
  try {
440
- const h = await (await i.getFileHandle(n, { create: !1 })).getFile(), w = {
295
+ const c = await (await r.getFileHandle(a, { create: !1 })).getFile(), l = {
441
296
  kind: "file",
442
- size: h.size,
443
- mtime: new Date(h.lastModified).toISOString(),
444
- ctime: new Date(h.lastModified).toISOString(),
297
+ size: c.size,
298
+ mtime: new Date(c.lastModified).toISOString(),
299
+ ctime: new Date(c.lastModified).toISOString(),
445
300
  isFile: !0,
446
301
  isDirectory: !1
447
302
  };
448
- if (s)
303
+ if (i)
449
304
  try {
450
- const u = new Uint8Array(await h.arrayBuffer()), d = await O(u, c);
451
- w.hash = d;
452
- } catch (u) {
453
- console.warn(`Failed to calculate hash for ${e}:`, u);
305
+ const h = new Uint8Array(await c.arrayBuffer()), w = await H(h, n);
306
+ l.hash = w;
307
+ } catch (h) {
308
+ console.warn(`Failed to calculate hash for ${t}:`, h);
454
309
  }
455
- return w;
456
- } catch (l) {
457
- if (l.name !== "TypeMismatchError" && l.name !== "NotFoundError")
310
+ return l;
311
+ } catch (s) {
312
+ if (s.name !== "TypeMismatchError" && s.name !== "NotFoundError")
458
313
  throw new o("Failed to stat (file)", "STAT_FAILED");
459
314
  }
460
315
  try {
461
- return await i.getDirectoryHandle(n, { create: !1 }), {
316
+ return await r.getDirectoryHandle(a, { create: !1 }), {
462
317
  kind: "directory",
463
318
  size: 0,
464
319
  mtime: (/* @__PURE__ */ new Date(0)).toISOString(),
465
320
  ctime: (/* @__PURE__ */ new Date(0)).toISOString(),
466
321
  isFile: !1,
467
322
  isDirectory: !0
468
- // Directories don't have hashes
469
323
  };
470
- } catch (l) {
471
- throw l.name === "NotFoundError" ? new o(`No such file or directory: ${e}`, "ENOENT") : new o("Failed to stat (directory)", "STAT_FAILED");
324
+ } catch (s) {
325
+ throw s.name === "NotFoundError" ? new o(`No such file or directory: ${t}`, "ENOENT") : new o("Failed to stat (directory)", "STAT_FAILED");
472
326
  }
473
327
  }
474
- async readdir(e, t) {
475
- const r = t?.withFileTypes ?? !1, n = await this.getDirectoryHandle(e, !1);
476
- if (r) {
328
+ async readdir(t, e) {
329
+ const a = e?.withFileTypes ?? !1, r = await this.getDirectoryHandle(t, !1);
330
+ if (a) {
477
331
  const i = [];
478
- for await (const [s, c] of n.entries()) {
479
- const l = c.kind === "file";
332
+ for await (const [n, s] of r.entries()) {
333
+ const c = s.kind === "file";
480
334
  i.push({
481
- name: s,
482
- kind: c.kind,
483
- isFile: l,
484
- isDirectory: !l
335
+ name: n,
336
+ kind: s.kind,
337
+ isFile: c,
338
+ isDirectory: !c
485
339
  });
486
340
  }
487
341
  return i;
488
342
  } else {
489
343
  const i = [];
490
- for await (const [s] of n.entries())
491
- i.push(s);
344
+ for await (const [n] of r.entries())
345
+ i.push(n);
492
346
  return i;
493
347
  }
494
348
  }
@@ -506,27 +360,27 @@ class U {
506
360
  * console.log(`File exists: ${exists}`);
507
361
  * ```
508
362
  */
509
- async exists(e) {
510
- const t = f(e), r = t.pop();
511
- let n = null;
363
+ async exists(t) {
364
+ const e = u(t);
365
+ let a = null;
512
366
  try {
513
- n = await this.getDirectoryHandle(t, !1);
514
- } catch (i) {
515
- throw (i.name === "NotFoundError" || i.name === "TypeMismatchError") && (n = null), i;
367
+ a = await this.getDirectoryHandle(m(t), !1);
368
+ } catch (r) {
369
+ throw (r.name === "NotFoundError" || r.name === "TypeMismatchError") && (a = null), r;
516
370
  }
517
- if (!n || !r)
371
+ if (!a || !e)
518
372
  return !1;
519
373
  try {
520
- return await n.getFileHandle(r, { create: !1 }), !0;
521
- } catch (i) {
522
- if (i.name !== "NotFoundError" && i.name !== "TypeMismatchError")
523
- throw i;
374
+ return await a.getFileHandle(e, { create: !1 }), !0;
375
+ } catch (r) {
376
+ if (r.name !== "NotFoundError" && r.name !== "TypeMismatchError")
377
+ throw r;
524
378
  }
525
379
  try {
526
- return await n.getDirectoryHandle(r, { create: !1 }), !0;
527
- } catch (i) {
528
- if (i.name !== "NotFoundError" && i.name !== "TypeMismatchError")
529
- throw i;
380
+ return await a.getDirectoryHandle(e, { create: !1 }), !0;
381
+ } catch (r) {
382
+ if (r.name !== "NotFoundError" && r.name !== "TypeMismatchError")
383
+ throw r;
530
384
  }
531
385
  return !1;
532
386
  }
@@ -549,15 +403,15 @@ class U {
549
403
  * await fs.clear('/data');
550
404
  * ```
551
405
  */
552
- async clear(e = "/") {
406
+ async clear(t = "/") {
553
407
  try {
554
- const t = await this.readdir(e, { withFileTypes: !0 });
555
- for (const r of t) {
556
- const n = `${e === "/" ? "" : e}/${r.name}`;
557
- await this.remove(n, { recursive: !0 });
408
+ const e = await this.readdir(t, { withFileTypes: !0 });
409
+ for (const a of e) {
410
+ const r = `${t === "/" ? "" : t}/${a.name}`;
411
+ await this.remove(r, { recursive: !0 });
558
412
  }
559
- } catch (t) {
560
- throw t instanceof o ? t : new o(`Failed to clear directory: ${e}`, "CLEAR_FAILED");
413
+ } catch (e) {
414
+ throw e instanceof o ? e : new o(`Failed to clear directory: ${t}`, "CLEAR_FAILED");
561
415
  }
562
416
  }
563
417
  /**
@@ -584,18 +438,18 @@ class U {
584
438
  * await fs.rm('/maybe/exists', { force: true });
585
439
  * ```
586
440
  */
587
- async remove(e, t) {
588
- const r = t?.recursive ?? !1, n = t?.force ?? !1, i = f(e), s = i.pop();
589
- if (!s)
590
- throw new g("Invalid path", e);
591
- const c = await this.getDirectoryHandle(i, !1);
441
+ async remove(t, e) {
442
+ const a = e?.recursive ?? !1, r = e?.force ?? !1, i = u(t);
443
+ if (!i)
444
+ throw new E("Invalid path", t);
445
+ const n = await this.getDirectoryHandle(m(t), !1);
592
446
  try {
593
- await c.removeEntry(s, { recursive: r });
594
- } catch (l) {
595
- if (l.name === "NotFoundError") {
596
- if (!n)
597
- throw new o(`No such file or directory: ${e}`, "ENOENT");
598
- } else throw l.name === "InvalidModificationError" ? new o(`Directory not empty: ${e}. Use recursive option to force removal.`, "ENOTEMPTY") : l.name === "TypeMismatchError" && !r ? new o(`Cannot remove directory without recursive option: ${e}`, "EISDIR") : new o(`Failed to remove path: ${e}`, "RM_FAILED");
447
+ await n.removeEntry(i, { recursive: a });
448
+ } catch (s) {
449
+ if (s.name === "NotFoundError") {
450
+ if (!r)
451
+ throw new o(`No such file or directory: ${t}`, "ENOENT");
452
+ } else throw s.name === "InvalidModificationError" ? new o(`Directory not empty: ${t}. Use recursive option to force removal.`, "ENOTEMPTY") : s.name === "TypeMismatchError" && !a ? new o(`Cannot remove directory without recursive option: ${t}`, "EISDIR") : new o(`Failed to remove path: ${t}`, "RM_FAILED");
599
453
  }
600
454
  }
601
455
  /**
@@ -616,23 +470,14 @@ class U {
616
470
  * console.log(absolute); // '/data/file.txt'
617
471
  * ```
618
472
  */
619
- async realpath(e) {
473
+ async realpath(t) {
620
474
  try {
621
- const t = f(e), r = [];
622
- for (const s of t)
623
- if (!(s === "." || s === ""))
624
- if (s === "..") {
625
- if (r.length === 0)
626
- throw new o("Path escapes root", "EINVAL");
627
- r.length > 0 && r.pop();
628
- } else
629
- r.push(s);
630
- const n = p(r);
631
- if (!await this.exists(n))
632
- throw new m(n);
633
- return n;
634
- } catch (t) {
635
- throw t instanceof o ? t : new o(`Failed to resolve path: ${e}`, "REALPATH_FAILED");
475
+ const e = $(t);
476
+ if (!await this.exists(e))
477
+ throw new d(e);
478
+ return e;
479
+ } catch (e) {
480
+ throw e instanceof o ? e : new o(`Failed to resolve path: ${t}`, "REALPATH_FAILED");
636
481
  }
637
482
  }
638
483
  /**
@@ -651,13 +496,13 @@ class U {
651
496
  * await fs.rename('/old/path/file.txt', '/new/path/renamed.txt');
652
497
  * ```
653
498
  */
654
- async rename(e, t) {
499
+ async rename(t, e) {
655
500
  try {
656
- if (!await this.exists(e))
657
- throw new m(e);
658
- await this.copy(e, t, { recursive: !0 }), await this.remove(e, { recursive: !0 });
659
- } catch (r) {
660
- throw r instanceof o ? r : new o(`Failed to rename from ${e} to ${t}`, "RENAME_FAILED");
501
+ if (!await this.exists(t))
502
+ throw new d(t);
503
+ await this.copy(t, e, { recursive: !0 }), await this.remove(t, { recursive: !0 });
504
+ } catch (a) {
505
+ throw a instanceof o ? a : new o(`Failed to rename from ${t} to ${e}`, "RENAME_FAILED");
661
506
  }
662
507
  }
663
508
  /**
@@ -685,82 +530,82 @@ class U {
685
530
  * await fs.cp('/source', '/dest', { recursive: true, force: false });
686
531
  * ```
687
532
  */
688
- async copy(e, t, r) {
533
+ async copy(t, e, a) {
689
534
  try {
690
- const n = r?.recursive ?? !1, i = r?.force ?? !0;
691
- if (!await this.exists(e))
692
- throw new o(`Source does not exist: ${e}`, "ENOENT");
693
- if (await this.exists(t) && !i)
694
- throw new o(`Destination already exists: ${t}`, "EEXIST");
695
- if ((await this.stat(e)).isFile) {
696
- const h = await this.readFile(e, "binary");
697
- await this.writeFile(t, h);
535
+ const r = a?.recursive ?? !1, i = a?.force ?? !0;
536
+ if (!await this.exists(t))
537
+ throw new o(`Source does not exist: ${t}`, "ENOENT");
538
+ if (await this.exists(e) && !i)
539
+ throw new o(`Destination already exists: ${e}`, "EEXIST");
540
+ if ((await this.stat(t)).isFile) {
541
+ const l = await this.readFile(t, "binary");
542
+ await this.writeFile(e, l);
698
543
  } else {
699
- if (!n)
700
- throw new o(`Cannot copy directory without recursive option: ${e}`, "EISDIR");
701
- await this.mkdir(t, { recursive: !0 });
702
- const h = await this.readdir(e, { withFileTypes: !0 });
703
- for (const w of h) {
704
- const u = `${e}/${w.name}`, d = `${t}/${w.name}`;
705
- await this.copy(u, d, { recursive: !0, force: i });
544
+ if (!r)
545
+ throw new o(`Cannot copy directory without recursive option: ${t}`, "EISDIR");
546
+ await this.mkdir(e, { recursive: !0 });
547
+ const l = await this.readdir(t, { withFileTypes: !0 });
548
+ for (const h of l) {
549
+ const w = `${t}/${h.name}`, D = `${e}/${h.name}`;
550
+ await this.copy(w, D, { recursive: !0, force: i });
706
551
  }
707
552
  }
708
- } catch (n) {
709
- throw n instanceof o ? n : new o(`Failed to copy from ${e} to ${t}`, "CP_FAILED");
553
+ } catch (r) {
554
+ throw r instanceof o ? r : new o(`Failed to copy from ${t} to ${e}`, "CP_FAILED");
710
555
  }
711
556
  }
712
557
  /**
713
558
  * Start watching a file or directory for changes
714
559
  */
715
- async watch(e) {
716
- const t = e.startsWith("/") ? e : `/${e}`, r = await this.buildSnapshot(t);
717
- this.watchers.set(t, r), this.watchTimer || (this.watchTimer = setInterval(() => {
560
+ async watch(t) {
561
+ const e = F(t), a = await this.buildSnapshot(e);
562
+ this.watchers.set(e, a), this.watchTimer || (this.watchTimer = setInterval(() => {
718
563
  this.scanWatches();
719
564
  }, this.watchInterval));
720
565
  }
721
566
  /**
722
567
  * Stop watching a previously watched path
723
568
  */
724
- unwatch(e) {
725
- const t = e.startsWith("/") ? e : `/${e}`;
726
- this.watchers.delete(t), this.watchers.size === 0 && this.watchTimer && (clearInterval(this.watchTimer), this.watchTimer = null);
727
- }
728
- async buildSnapshot(e) {
729
- const t = /* @__PURE__ */ new Map(), r = async (n) => {
730
- const i = await this.stat(n);
731
- if (t.set(n, i), i.isDirectory) {
732
- const s = await this.readdir(n, { withFileTypes: !0 });
733
- for (const c of s) {
734
- const l = `${n === "/" ? "" : n}/${c.name}`;
735
- await r(l);
569
+ unwatch(t) {
570
+ const e = F(t);
571
+ this.watchers.delete(e), this.watchers.size === 0 && this.watchTimer && (clearInterval(this.watchTimer), this.watchTimer = null);
572
+ }
573
+ async buildSnapshot(t) {
574
+ const e = /* @__PURE__ */ new Map(), a = async (r) => {
575
+ const i = await this.stat(r);
576
+ if (e.set(r, i), i.isDirectory) {
577
+ const n = await this.readdir(r, { withFileTypes: !0 });
578
+ for (const s of n) {
579
+ const c = `${r === "/" ? "" : r}/${s.name}`;
580
+ await a(c);
736
581
  }
737
582
  }
738
583
  };
739
- return await r(e), t;
584
+ return await a(t), e;
740
585
  }
741
586
  async scanWatches() {
742
587
  if (!this.scanning) {
743
588
  this.scanning = !0;
744
589
  try {
745
590
  await Promise.all(
746
- [...this.watchers.entries()].map(async ([e, t]) => {
747
- let r;
591
+ [...this.watchers.entries()].map(async ([t, e]) => {
592
+ let a;
748
593
  try {
749
- r = await this.buildSnapshot(e);
594
+ a = await this.buildSnapshot(t);
750
595
  } catch {
751
- r = /* @__PURE__ */ new Map();
596
+ a = /* @__PURE__ */ new Map();
752
597
  }
753
- const n = [];
754
- for (const [i, s] of r) {
755
- const c = t.get(i);
756
- c ? (c.mtime !== s.mtime || c.size !== s.size) && n.push({ path: i, type: "change" }) : n.push({ path: i, type: "create" });
598
+ const r = [];
599
+ for (const [i, n] of a) {
600
+ const s = e.get(i);
601
+ s ? (s.mtime !== n.mtime || s.size !== n.size) && r.push({ path: i, type: "change" }) : r.push({ path: i, type: "create" });
757
602
  }
758
- for (const i of t.keys())
759
- r.has(i) || n.push({ path: i, type: "delete" });
760
- if (n.length && this.watchCallback)
761
- for (const i of n)
603
+ for (const i of e.keys())
604
+ a.has(i) || r.push({ path: i, type: "delete" });
605
+ if (r.length && this.watchCallback)
606
+ for (const i of r)
762
607
  this.watchCallback(i);
763
- this.watchers.set(e, r);
608
+ this.watchers.set(t, a);
764
609
  })
765
610
  );
766
611
  } finally {
@@ -796,26 +641,21 @@ class U {
796
641
  * await fs.sync(entries, { cleanBefore: true });
797
642
  * ```
798
643
  */
799
- async sync(e, t) {
644
+ async sync(t, e) {
800
645
  try {
801
- (t?.cleanBefore ?? !1) && await this.clear("/");
802
- for (const [n, i] of e) {
803
- const s = n.startsWith("/") ? n : `/${n}`;
804
- let c;
805
- if (i instanceof Blob) {
806
- const l = await i.arrayBuffer();
807
- c = new Uint8Array(l);
808
- } else
809
- c = i;
810
- await this.writeFile(s, c);
646
+ (e?.cleanBefore ?? !1) && await this.clear("/");
647
+ for (const [r, i] of t) {
648
+ const n = F(r);
649
+ let s;
650
+ i instanceof Blob ? s = await A(i) : s = i, await this.writeFile(n, s);
811
651
  }
812
- } catch (r) {
813
- throw r instanceof o ? r : new o("Failed to sync file system", "SYNC_FAILED");
652
+ } catch (a) {
653
+ throw a instanceof o ? a : new o("Failed to sync file system", "SYNC_FAILED");
814
654
  }
815
655
  }
816
656
  }
817
- typeof self < "u" && self.constructor.name === "DedicatedWorkerGlobalScope" && E(new U());
657
+ typeof self < "u" && self.constructor.name === "DedicatedWorkerGlobalScope" && p(new N());
818
658
  export {
819
- U as OPFSWorker
659
+ N as OPFSWorker
820
660
  };
821
661
  //# sourceMappingURL=raw.js.map