opfs-worker 0.2.5 → 0.2.6

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,21 +1,21 @@
1
1
  import { wrap as i, proxy as o } from "comlink";
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";
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 F, e as S, k as E, d as b, l as P, h as x, o as T, 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-C0nyU6hv.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"), D = Symbol("Comlink.finalizer"), p = Symbol("Comlink.thrown"), $ = (e) => typeof e == "object" && e !== null || typeof e == "function", W = {
8
+ const H = Symbol("Comlink.proxy"), R = Symbol("Comlink.endpoint"), B = Symbol("Comlink.releaseProxy"), D = Symbol("Comlink.finalizer"), E = Symbol("Comlink.thrown"), $ = (e) => typeof e == "object" && e !== null || typeof e == "function", W = {
9
9
  canHandle: (e) => $(e) && e[H],
10
10
  serialize(e) {
11
11
  const { port1: t, port2: r } = new MessageChannel();
12
- return P(e, t), [r, [r]];
12
+ return I(e, t), [r, [r]];
13
13
  },
14
14
  deserialize(e) {
15
15
  return e.start(), Y(e);
16
16
  }
17
17
  }, j = {
18
- canHandle: (e) => $(e) && p in e,
18
+ canHandle: (e) => $(e) && E in e,
19
19
  serialize({ value: e }) {
20
20
  let t;
21
21
  return e instanceof Error ? t = {
@@ -30,7 +30,7 @@ const H = Symbol("Comlink.proxy"), R = Symbol("Comlink.endpoint"), B = Symbol("C
30
30
  deserialize(e) {
31
31
  throw e.isError ? Object.assign(new Error(e.value.message), e.value) : e.value;
32
32
  }
33
- }, U = /* @__PURE__ */ new Map([
33
+ }, z = /* @__PURE__ */ new Map([
34
34
  ["proxy", W],
35
35
  ["throw", j]
36
36
  ]);
@@ -40,7 +40,7 @@ function V(e, t) {
40
40
  return !0;
41
41
  return !1;
42
42
  }
43
- function P(e, t = globalThis, r = ["*"]) {
43
+ function I(e, t = globalThis, r = ["*"]) {
44
44
  t.addEventListener("message", function n(i) {
45
45
  if (!i || !i.data)
46
46
  return;
@@ -71,7 +71,7 @@ function P(e, t = globalThis, r = ["*"]) {
71
71
  case "ENDPOINT":
72
72
  {
73
73
  const { port1: f, port2: g } = new MessageChannel();
74
- P(e, g), h = J(f, [f]);
74
+ I(e, g), h = J(f, [f]);
75
75
  }
76
76
  break;
77
77
  case "RELEASE":
@@ -81,15 +81,15 @@ function P(e, t = globalThis, r = ["*"]) {
81
81
  return;
82
82
  }
83
83
  } catch (u) {
84
- h = { value: u, [p]: 0 };
84
+ h = { value: u, [E]: 0 };
85
85
  }
86
- Promise.resolve(h).catch((u) => ({ value: u, [p]: 0 })).then((u) => {
86
+ Promise.resolve(h).catch((u) => ({ value: u, [E]: 0 })).then((u) => {
87
87
  const [w, f] = A(u);
88
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
89
  }).catch((u) => {
90
90
  const [w, f] = A({
91
91
  value: new TypeError("Unserializable return value"),
92
- [p]: 0
92
+ [E]: 0
93
93
  });
94
94
  t.postMessage(Object.assign(Object.assign({}, w), { id: a }), f);
95
95
  });
@@ -114,13 +114,13 @@ function Y(e, t) {
114
114
  } finally {
115
115
  r.delete(a.id);
116
116
  }
117
- }), I(e, r, [], t);
117
+ }), M(e, r, [], t);
118
118
  }
119
- function E(e) {
119
+ function p(e) {
120
120
  if (e)
121
121
  throw new Error("Proxy has been released and is not useable");
122
122
  }
123
- function z(e) {
123
+ function U(e) {
124
124
  return d(e, /* @__PURE__ */ new Map(), {
125
125
  type: "RELEASE"
126
126
  }).then(() => {
@@ -129,7 +129,7 @@ function z(e) {
129
129
  }
130
130
  const F = /* @__PURE__ */ new WeakMap(), S = "FinalizationRegistry" in globalThis && new FinalizationRegistry((e) => {
131
131
  const t = (F.get(e) || 0) - 1;
132
- F.set(e, t), t === 0 && z(e);
132
+ F.set(e, t), t === 0 && U(e);
133
133
  });
134
134
  function X(e, t) {
135
135
  const r = (F.get(t) || 0) + 1;
@@ -138,14 +138,14 @@ function X(e, t) {
138
138
  function q(e) {
139
139
  S && S.unregister(e);
140
140
  }
141
- function I(e, t, r = [], n = function() {
141
+ function M(e, t, r = [], n = function() {
142
142
  }) {
143
143
  let i = !1;
144
144
  const a = new Proxy(n, {
145
145
  get(s, o) {
146
- if (E(i), o === B)
146
+ if (p(i), o === B)
147
147
  return () => {
148
- q(a), z(e), t.clear(), i = !0;
148
+ q(a), U(e), t.clear(), i = !0;
149
149
  };
150
150
  if (o === "then") {
151
151
  if (r.length === 0)
@@ -156,10 +156,10 @@ function I(e, t, r = [], n = function() {
156
156
  }).then(y);
157
157
  return l.then.bind(l);
158
158
  }
159
- return I(e, t, [...r, o]);
159
+ return M(e, t, [...r, o]);
160
160
  },
161
161
  set(s, o, l) {
162
- E(i);
162
+ p(i);
163
163
  const [h, u] = A(l);
164
164
  return d(e, t, {
165
165
  type: "SET",
@@ -168,14 +168,14 @@ function I(e, t, r = [], n = function() {
168
168
  }, u).then(y);
169
169
  },
170
170
  apply(s, o, l) {
171
- E(i);
171
+ p(i);
172
172
  const h = r[r.length - 1];
173
173
  if (h === R)
174
174
  return d(e, t, {
175
175
  type: "ENDPOINT"
176
176
  }).then(y);
177
177
  if (h === "bind")
178
- return I(e, t, r.slice(0, -1));
178
+ return M(e, t, r.slice(0, -1));
179
179
  const [u, w] = N(l);
180
180
  return d(e, t, {
181
181
  type: "APPLY",
@@ -184,7 +184,7 @@ function I(e, t, r = [], n = function() {
184
184
  }, w).then(y);
185
185
  },
186
186
  construct(s, o) {
187
- E(i);
187
+ p(i);
188
188
  const [l, h] = N(o);
189
189
  return d(e, t, {
190
190
  type: "CONSTRUCT",
@@ -210,7 +210,7 @@ function Q(e) {
210
210
  return Object.assign(e, { [H]: !0 });
211
211
  }
212
212
  function A(e) {
213
- for (const [t, r] of U)
213
+ for (const [t, r] of z)
214
214
  if (r.canHandle(e)) {
215
215
  const [n, i] = r.serialize(e);
216
216
  return [
@@ -233,7 +233,7 @@ function A(e) {
233
233
  function y(e) {
234
234
  switch (e.type) {
235
235
  case "HANDLER":
236
- return U.get(e.name).deserialize(e.value);
236
+ return z.get(e.name).deserialize(e.value);
237
237
  case "RAW":
238
238
  return e.value;
239
239
  }
@@ -352,18 +352,18 @@ function ot() {
352
352
  function m(e) {
353
353
  return Array.isArray(e) ? e : e.split("/").filter(Boolean);
354
354
  }
355
- function x(e) {
355
+ function P(e) {
356
356
  return typeof e == "string" ? e ?? "/" : \`/\${e.join("/")}\`;
357
357
  }
358
- function C(e) {
358
+ function b(e) {
359
359
  const t = m(e);
360
360
  return t[t.length - 1] || "";
361
361
  }
362
- function M(e) {
362
+ function x(e) {
363
363
  const t = m(e);
364
- return t.pop(), x(t);
364
+ return t.pop(), P(t);
365
365
  }
366
- function b(e) {
366
+ function C(e) {
367
367
  return !e || e === "/" ? "/" : e.startsWith("/") ? e : \`/\${e}\`;
368
368
  }
369
369
  function ct(e) {
@@ -376,7 +376,7 @@ function ct(e) {
376
376
  r.pop();
377
377
  } else
378
378
  r.push(n);
379
- return x(r);
379
+ return P(r);
380
380
  }
381
381
  function lt(e, t = "utf-8") {
382
382
  return typeof e == "string" ? et(e, t) : e instanceof Uint8Array ? e : new Uint8Array(e);
@@ -408,14 +408,11 @@ async function v(e, t, r, n = {}) {
408
408
  }
409
409
  }
410
410
  }
411
- async function ut(e, t = "SHA-1") {
412
- e instanceof File && (e = await e.arrayBuffer());
413
- try {
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("");
416
- } catch (r) {
417
- throw console.warn(\`Failed to calculate \${t} hash:\`, r), r;
418
- }
411
+ async function ut(e, t = "SHA-1", r = 50 * 1024 * 1024) {
412
+ if (e instanceof File && (e = await e.arrayBuffer()), e.byteLength > r)
413
+ throw new Error(\`File size \${e.byteLength} bytes exceeds maximum allowed size \${r} bytes\`);
414
+ const n = new Uint8Array(e), i = await crypto.subtle.digest(t, n);
415
+ return Array.from(new Uint8Array(i)).map((s) => s.toString(16).padStart(2, "0")).join("");
419
416
  }
420
417
  async function ft(e) {
421
418
  const t = await e.arrayBuffer();
@@ -430,14 +427,16 @@ class wt {
430
427
  watchers = /* @__PURE__ */ new Map();
431
428
  /** Interval handle for polling watched paths */
432
429
  watchTimer = null;
433
- /** Polling interval in milliseconds */
434
- watchInterval = 1e3;
435
430
  /** Flag to avoid concurrent scans */
436
431
  scanning = !1;
437
432
  /** Promise to prevent concurrent mount operations */
438
433
  mountingPromise = null;
439
- /** Hash algorithm for file hashing (null means no hashing) */
440
- hashAlgorithm = null;
434
+ /** Configuration options */
435
+ options = {
436
+ watchInterval: 1e3,
437
+ maxFileSize: 50 * 1024 * 1024,
438
+ hashAlgorithm: null
439
+ };
441
440
  /**
442
441
  * Notify about internal changes to the file system
443
442
  *
@@ -451,7 +450,7 @@ class wt {
451
450
  if (!this.watchCallback)
452
451
  return;
453
452
  let r;
454
- if (this.hashAlgorithm && !t.isDirectory && t.type !== "removed")
453
+ if (this.options.hashAlgorithm && !t.isDirectory && t.type !== "removed")
455
454
  try {
456
455
  const n = await this.stat(t.path);
457
456
  n.isFile && n.hash && (r = n.hash);
@@ -471,6 +470,7 @@ class wt {
471
470
  * @param options - Optional configuration options
472
471
  * @param options.watchInterval - Polling interval in milliseconds for file watching
473
472
  * @param options.hashAlgorithm - Hash algorithm for file hashing
473
+ * @param options.maxFileSize - Maximum file size for hashing in bytes (default: 50MB)
474
474
  * @throws {OPFSError} If OPFS is not supported in the current browser
475
475
  */
476
476
  constructor(t, r) {
@@ -519,14 +519,15 @@ class wt {
519
519
  this.watchCallback = t;
520
520
  }
521
521
  /**
522
- * Set configuration options for the file system
522
+ * Update configuration options
523
523
  *
524
524
  * @param options - Configuration options to update
525
525
  * @param options.watchInterval - Polling interval in milliseconds for file watching
526
526
  * @param options.hashAlgorithm - Hash algorithm for file hashing
527
+ * @param options.maxFileSize - Maximum file size for hashing in bytes
527
528
  */
528
529
  setOptions(t) {
529
- t.watchInterval !== void 0 && (this.watchInterval = t.watchInterval), t.hashAlgorithm !== void 0 && (this.hashAlgorithm = t.hashAlgorithm);
530
+ t.watchInterval !== void 0 && (this.options.watchInterval = t.watchInterval), t.hashAlgorithm !== void 0 && (this.options.hashAlgorithm = t.hashAlgorithm), t.maxFileSize !== void 0 && (this.options.maxFileSize = t.maxFileSize);
530
531
  }
531
532
  /**
532
533
  * Automatically mount the OPFS root if not already mounted
@@ -740,7 +741,7 @@ class wt {
740
741
  a = await a.getDirectoryHandle(o, { create: n || s === i.length - 1 });
741
742
  } catch (l) {
742
743
  throw l.name === "NotFoundError" ? new c(
743
- \`Parent directory does not exist: \${x(i.slice(0, s + 1))}\`,
744
+ \`Parent directory does not exist: \${P(i.slice(0, s + 1))}\`,
744
745
  "ENOENT"
745
746
  ) : l.name === "TypeMismatchError" ? new c(\`Path segment is not a directory: \${o}\`, "ENOTDIR") : new c("Failed to create directory", "MKDIR_FAILED");
746
747
  }
@@ -779,7 +780,7 @@ class wt {
779
780
  isFile: !1,
780
781
  isDirectory: !0
781
782
  };
782
- const r = C(t), n = await this.getDirectoryHandle(M(t), !1), i = this.hashAlgorithm !== null;
783
+ const r = b(t), n = await this.getDirectoryHandle(x(t), !1), i = this.options.hashAlgorithm !== null;
783
784
  try {
784
785
  const s = await (await n.getFileHandle(r, { create: !1 })).getFile(), o = {
785
786
  kind: "file",
@@ -789,9 +790,9 @@ class wt {
789
790
  isFile: !0,
790
791
  isDirectory: !1
791
792
  };
792
- if (i && this.hashAlgorithm)
793
+ if (i && this.options.hashAlgorithm)
793
794
  try {
794
- const l = await ut(s, this.hashAlgorithm);
795
+ const l = await ut(s, this.options.hashAlgorithm, this.options.maxFileSize);
795
796
  o.hash = l;
796
797
  } catch (l) {
797
798
  console.warn(\`Failed to calculate hash for \${t}:\`, l);
@@ -853,10 +854,10 @@ class wt {
853
854
  async exists(t) {
854
855
  if (await this.ensureMounted(), t === "/")
855
856
  return !0;
856
- const r = C(t);
857
+ const r = b(t);
857
858
  let n = null;
858
859
  try {
859
- n = await this.getDirectoryHandle(M(t), !1);
860
+ n = await this.getDirectoryHandle(x(t), !1);
860
861
  } catch (i) {
861
862
  throw (i.name === "NotFoundError" || i.name === "TypeMismatchError") && (n = null), i;
862
863
  }
@@ -937,10 +938,10 @@ class wt {
937
938
  const n = r?.recursive ?? !1, i = r?.force ?? !1;
938
939
  if (t === "/")
939
940
  throw new c("Cannot remove root directory", "EROOT");
940
- const a = C(t);
941
+ const a = b(t);
941
942
  if (!a)
942
943
  throw new k("Invalid path", t);
943
- const s = await this.getDirectoryHandle(M(t), !1);
944
+ const s = await this.getDirectoryHandle(x(t), !1);
944
945
  try {
945
946
  await s.removeEntry(a, { recursive: n });
946
947
  } catch (o) {
@@ -1062,16 +1063,16 @@ class wt {
1062
1063
  */
1063
1064
  async watch(t) {
1064
1065
  await this.ensureMounted();
1065
- const r = b(t), n = await this.buildSnapshot(r);
1066
+ const r = C(t), n = await this.buildSnapshot(r);
1066
1067
  this.watchers.set(r, n), this.watchTimer || (this.watchTimer = setInterval(() => {
1067
1068
  this.scanWatches();
1068
- }, this.watchInterval));
1069
+ }, this.options.watchInterval));
1069
1070
  }
1070
1071
  /**
1071
1072
  * Stop watching a previously watched path
1072
1073
  */
1073
1074
  unwatch(t) {
1074
- const r = b(t);
1075
+ const r = C(t);
1075
1076
  this.watchers.delete(r), this.watchers.size === 0 && this.watchTimer && (clearInterval(this.watchTimer), this.watchTimer = null);
1076
1077
  }
1077
1078
  async buildSnapshot(t) {
@@ -1146,7 +1147,7 @@ class wt {
1146
1147
  try {
1147
1148
  (r?.cleanBefore ?? !1) && await this.clear("/");
1148
1149
  for (const [i, a] of t) {
1149
- const s = b(i);
1150
+ const s = C(i);
1150
1151
  let o;
1151
1152
  a instanceof Blob ? o = await ft(a) : o = a, await this.writeFile(s, o);
1152
1153
  }
@@ -1156,8 +1157,8 @@ class wt {
1156
1157
  }
1157
1158
  }
1158
1159
  }
1159
- typeof self < "u" && self.constructor.name === "DedicatedWorkerGlobalScope" && P(new wt());
1160
- //# sourceMappingURL=worker-BL3varjN.js.map
1160
+ typeof self < "u" && self.constructor.name === "DedicatedWorkerGlobalScope" && I(new wt());
1161
+ //# sourceMappingURL=worker-CHy3JxL1.js.map
1161
1162
  `, r = typeof self < "u" && self.Blob && new Blob(["URL.revokeObjectURL(import.meta.url);", a], { type: "text/javascript;charset=utf-8" });
1162
1163
  function s(e) {
1163
1164
  let n;
@@ -1196,14 +1197,14 @@ export {
1196
1197
  m as PathError,
1197
1198
  g as PermissionError,
1198
1199
  v as StorageError,
1199
- E as TimeoutError,
1200
- F as basename,
1201
- S as calculateFileHash,
1200
+ F as TimeoutError,
1201
+ S as basename,
1202
+ E as calculateFileHash,
1202
1203
  b as checkOPFSSupport,
1203
1204
  P as convertBlobToUint8Array,
1204
- T as createBuffer,
1205
+ x as createBuffer,
1205
1206
  h as createWorker,
1206
- x as decodeBuffer,
1207
+ T as decodeBuffer,
1207
1208
  A as dirname,
1208
1209
  O as encodeString,
1209
1210
  D as extname,
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":["../src/index.ts"],"sourcesContent":["import { wrap, proxy } from 'comlink';\n\nimport WorkerCtor from './worker?worker&inline';\n\nimport type { OPFSWorker, RemoteOPFSWorker, WatchEvent } from './types';\n\nexport * from './types';\nexport * from './utils/errors';\nexport * from './utils/helpers';\nexport * from './utils/encoder';\n\n/**\n * Creates a new file system instance with inline worker\n * @param watchCallback - Optional callback for file change events\n * @param options - Optional configuration options\n * @param options.watchInterval - Polling interval in milliseconds for file watching\n * @param options.hashAlgorithm - Hash algorithm for file hashing\n * @returns Promise resolving to the file system interface\n */\nexport function createWorker(\n watchCallback?: (event: WatchEvent) => void,\n options?: { \n watchInterval?: number;\n hashAlgorithm?: 'SHA-1' | 'SHA-256' | 'SHA-384' | 'SHA-512';\n }\n): RemoteOPFSWorker {\n const wrapped = wrap<OPFSWorker>(new WorkerCtor());\n \n // Set up watch callback and options if provided\n if (watchCallback) {\n wrapped.setWatchCallback(\n watchCallback ? proxy(watchCallback) : () => {}, \n );\n }\n \n if (options) {\n wrapped.setOptions(options);\n }\n \n return wrapped;\n}\n"],"names":["createWorker","watchCallback","options","wrapped","wrap","WorkerCtor","proxy"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmBO,SAASA,EACZC,GACAC,GAIgB;AAChB,QAAMC,IAAUC,EAAiB,IAAIC,GAAY;AAGjD,SAAIJ,KACAE,EAAQ;AAAA,IACJF,IAAgBK,EAAML,CAAa,IAAI,MAAM;AAAA,IAAC;AAAA,EAAA,GAIlDC,KACAC,EAAQ,WAAWD,CAAO,GAGvBC;AACX;"}
1
+ {"version":3,"file":"index.js","sources":["../src/index.ts"],"sourcesContent":["import { wrap, proxy } from 'comlink';\n\nimport WorkerCtor from './worker?worker&inline';\n\nimport type { OPFSWorker, RemoteOPFSWorker, WatchEvent, OPFSOptions } from './types';\n\nexport * from './types';\nexport * from './utils/errors';\nexport * from './utils/helpers';\nexport * from './utils/encoder';\n\n/**\n * Creates a new file system instance with inline worker\n * @param watchCallback - Optional callback for file change events\n * @param options - Optional configuration options\n * @returns Promise resolving to the file system interface\n */\nexport function createWorker(\n watchCallback?: (event: WatchEvent) => void,\n options?: OPFSOptions\n): RemoteOPFSWorker {\n const wrapped = wrap<OPFSWorker>(new WorkerCtor());\n \n // Set up watch callback and options if provided\n if (watchCallback) {\n wrapped.setWatchCallback(\n watchCallback ? proxy(watchCallback) : () => {}, \n );\n }\n \n if (options) {\n wrapped.setOptions(options);\n }\n \n return wrapped;\n}\n"],"names":["createWorker","watchCallback","options","wrapped","wrap","WorkerCtor","proxy"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAiBO,SAASA,EACZC,GACAC,GACgB;AAChB,QAAMC,IAAUC,EAAiB,IAAIC,GAAY;AAGjD,SAAIJ,KACAE,EAAQ;AAAA,IACJF,IAAgBK,EAAML,CAAa,IAAI,MAAM;AAAA,IAAC;AAAA,EAAA,GAIlDC,KACAC,EAAQ,WAAWD,CAAO,GAGvBC;AACX;"}
package/dist/raw.cjs CHANGED
@@ -1,2 +1,2 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const y=require("comlink"),a=require("./helpers-DjY2OR7f.cjs");class w{root=null;watchCallback=null;watchers=new Map;watchTimer=null;watchInterval=1e3;scanning=!1;mountingPromise=null;hashAlgorithm=null;async notifyChange(t){if(!this.watchCallback)return;let e;if(this.hashAlgorithm&&!t.isDirectory&&t.type!=="removed")try{const r=await this.stat(t.path);r.isFile&&r.hash&&(e=r.hash)}catch(r){console.warn(`Failed to calculate hash for ${t.path}:`,r)}this.watchCallback({timestamp:new Date().toISOString(),...t,...e&&{hash:e}})}constructor(t,e){a.checkOPFSSupport(),t&&(this.watchCallback=t),e&&this.setOptions(e),this.mount("/")}async mount(t="/"){return this.mountingPromise&&await this.mountingPromise,this.mountingPromise=new Promise(async(e,r)=>{this.root=null;try{const i=await navigator.storage.getDirectory();t==="/"?this.root=i:this.root=await this.getDirectoryHandle(t,!0,i),e(!0)}catch(i){console.error(i),r(new a.OPFSError("Failed to initialize OPFS","INIT_FAILED"))}finally{this.mountingPromise=null}}),this.mountingPromise}setWatchCallback(t){this.watchCallback=t}setOptions(t){t.watchInterval!==void 0&&(this.watchInterval=t.watchInterval),t.hashAlgorithm!==void 0&&(this.hashAlgorithm=t.hashAlgorithm)}async ensureMounted(){if(!this.root){if(this.mountingPromise){await this.mountingPromise;return}throw new a.OPFSError("OPFS not mounted","NOT_MOUNTED")}}async getDirectoryHandle(t,e=!1,r=this.root){if(!r)throw new a.OPFSNotMountedError;const i=Array.isArray(t)?t:a.splitPath(t);let s=r;for(const o of i)s=await s.getDirectoryHandle(o,{create:e});return s}async getFileHandle(t,e=!1,r=this.root){if(!r)throw new a.OPFSNotMountedError;const i=a.splitPath(t);if(i.length===0)throw new a.PathError("Path must not be empty",Array.isArray(t)?t.join("/"):t);const s=i.pop();return(await this.getDirectoryHandle(i,e,r)).getFileHandle(s,{create:e})}async index(){const t=new Map,e=async r=>{const i=await this.readdir(r,{withFileTypes:!0});for(const s of i){const o=`${r==="/"?"":r}/${s.name}`;try{const n=await this.stat(o);t.set(o,n),n.isDirectory&&await e(o)}catch(n){console.warn(`Skipping broken entry: ${o}`,n)}}};return t.set("/",{kind:"directory",size:0,mtime:new Date(0).toISOString(),ctime:new Date(0).toISOString(),isFile:!1,isDirectory:!0}),await e("/"),t}async readFile(t,e="utf-8"){await this.ensureMounted();try{const r=await this.getFileHandle(t,!1),i=await a.readFileData(r);return e==="binary"?i:a.decodeBuffer(i,e)}catch(r){throw console.error(r),new a.FileNotFoundError(t)}}async writeFile(t,e,r){await this.ensureMounted();const i=await this.getFileHandle(t,!0);await a.writeFileData(i,e,r,{truncate:!0}),await this.notifyChange({path:t,type:"changed",isDirectory:!1})}async appendFile(t,e,r){await this.ensureMounted();const i=await this.getFileHandle(t,!0);await a.writeFileData(i,e,r,{append:!0}),await this.notifyChange({path:t,type:"changed",isDirectory:!1})}async mkdir(t,e){if(await this.ensureMounted(),!this.root)throw new a.OPFSNotMountedError;const r=e?.recursive??!1,i=a.splitPath(t);let s=this.root;for(let o=0;o<i.length;o++){const n=i[o];try{s=await s.getDirectoryHandle(n,{create:r||o===i.length-1})}catch(c){throw c.name==="NotFoundError"?new a.OPFSError(`Parent directory does not exist: ${a.joinPath(i.slice(0,o+1))}`,"ENOENT"):c.name==="TypeMismatchError"?new a.OPFSError(`Path segment is not a directory: ${n}`,"ENOTDIR"):new a.OPFSError("Failed to create directory","MKDIR_FAILED")}}await this.notifyChange({path:t,type:"added",isDirectory:!0})}async stat(t){if(await this.ensureMounted(),t==="/")return{kind:"directory",size:0,mtime:new Date(0).toISOString(),ctime:new Date(0).toISOString(),isFile:!1,isDirectory:!0};const e=a.basename(t),r=await this.getDirectoryHandle(a.dirname(t),!1),i=this.hashAlgorithm!==null;try{const o=await(await r.getFileHandle(e,{create:!1})).getFile(),n={kind:"file",size:o.size,mtime:new Date(o.lastModified).toISOString(),ctime:new Date(o.lastModified).toISOString(),isFile:!0,isDirectory:!1};if(i&&this.hashAlgorithm)try{const c=await a.calculateFileHash(o,this.hashAlgorithm);n.hash=c}catch(c){console.warn(`Failed to calculate hash for ${t}:`,c)}return n}catch(s){if(s.name!=="TypeMismatchError"&&s.name!=="NotFoundError")throw new a.OPFSError("Failed to stat (file)","STAT_FAILED")}try{return await r.getDirectoryHandle(e,{create:!1}),{kind:"directory",size:0,mtime:new Date(0).toISOString(),ctime:new Date(0).toISOString(),isFile:!1,isDirectory:!0}}catch(s){throw s.name==="NotFoundError"?new a.OPFSError(`No such file or directory: ${t}`,"ENOENT"):new a.OPFSError("Failed to stat (directory)","STAT_FAILED")}}async readdir(t,e){await this.ensureMounted();const r=e?.withFileTypes??!1,i=await this.getDirectoryHandle(t,!1);if(r){const s=[];for await(const[o,n]of i.entries()){const c=n.kind==="file";s.push({name:o,kind:n.kind,isFile:c,isDirectory:!c})}return s}else{const s=[];for await(const[o]of i.entries())s.push(o);return s}}async exists(t){if(await this.ensureMounted(),t==="/")return!0;const e=a.basename(t);let r=null;try{r=await this.getDirectoryHandle(a.dirname(t),!1)}catch(i){throw(i.name==="NotFoundError"||i.name==="TypeMismatchError")&&(r=null),i}if(!r||!e)return!1;try{return await r.getFileHandle(e,{create:!1}),!0}catch(i){if(i.name!=="NotFoundError"&&i.name!=="TypeMismatchError")throw i}try{return await r.getDirectoryHandle(e,{create:!1}),!0}catch(i){if(i.name!=="NotFoundError"&&i.name!=="TypeMismatchError")throw i}return!1}async clear(t="/"){await this.ensureMounted();try{const e=await this.readdir(t,{withFileTypes:!0});for(const r of e){const i=`${t==="/"?"":t}/${r.name}`;await this.remove(i,{recursive:!0})}await this.notifyChange({path:t,type:"changed",isDirectory:!0})}catch(e){throw e instanceof a.OPFSError?e:new a.OPFSError(`Failed to clear directory: ${t}`,"CLEAR_FAILED")}}async remove(t,e){await this.ensureMounted();const r=e?.recursive??!1,i=e?.force??!1;if(t==="/")throw new a.OPFSError("Cannot remove root directory","EROOT");const s=a.basename(t);if(!s)throw new a.PathError("Invalid path",t);const o=await this.getDirectoryHandle(a.dirname(t),!1);try{await o.removeEntry(s,{recursive:r})}catch(n){if(n.name==="NotFoundError"){if(!i)throw new a.OPFSError(`No such file or directory: ${t}`,"ENOENT")}else throw n.name==="InvalidModificationError"?new a.OPFSError(`Directory not empty: ${t}. Use recursive option to force removal.`,"ENOTEMPTY"):n.name==="TypeMismatchError"&&!r?new a.OPFSError(`Cannot remove directory without recursive option: ${t}`,"EISDIR"):new a.OPFSError(`Failed to remove path: ${t}`,"RM_FAILED")}await this.notifyChange({path:t,type:"removed",isDirectory:!1})}async realpath(t){await this.ensureMounted();try{const e=a.resolvePath(t);if(!await this.exists(e))throw new a.FileNotFoundError(e);return e}catch(e){throw e instanceof a.OPFSError?e:new a.OPFSError(`Failed to resolve path: ${t}`,"REALPATH_FAILED")}}async rename(t,e){await this.ensureMounted();try{if(!await this.exists(t))throw new a.FileNotFoundError(t);await this.copy(t,e,{recursive:!0}),await this.remove(t,{recursive:!0}),await this.notifyChange({path:t,type:"removed",isDirectory:!1}),await this.notifyChange({path:e,type:"added",isDirectory:!1})}catch(r){throw r instanceof a.OPFSError?r:new a.OPFSError(`Failed to rename from ${t} to ${e}`,"RENAME_FAILED")}}async copy(t,e,r){await this.ensureMounted();try{const i=r?.recursive??!1,s=r?.force??!0;if(!await this.exists(t))throw new a.OPFSError(`Source does not exist: ${t}`,"ENOENT");if(await this.exists(e)&&!s)throw new a.OPFSError(`Destination already exists: ${e}`,"EEXIST");if((await this.stat(t)).isFile){const h=await this.readFile(t,"binary");await this.writeFile(e,h)}else{if(!i)throw new a.OPFSError(`Cannot copy directory without recursive option: ${t}`,"EISDIR");await this.mkdir(e,{recursive:!0});const h=await this.readdir(t,{withFileTypes:!0});for(const l of h){const f=`${t}/${l.name}`,u=`${e}/${l.name}`;await this.copy(f,u,{recursive:!0,force:s})}}await this.notifyChange({path:e,type:"added",isDirectory:!1})}catch(i){throw i instanceof a.OPFSError?i:new a.OPFSError(`Failed to copy from ${t} to ${e}`,"CP_FAILED")}}async watch(t){await this.ensureMounted();const e=a.normalizePath(t),r=await this.buildSnapshot(e);this.watchers.set(e,r),this.watchTimer||(this.watchTimer=setInterval(()=>{this.scanWatches()},this.watchInterval))}unwatch(t){const e=a.normalizePath(t);this.watchers.delete(e),this.watchers.size===0&&this.watchTimer&&(clearInterval(this.watchTimer),this.watchTimer=null)}async buildSnapshot(t){const e=new Map,r=async i=>{const s=await this.stat(i);if(e.set(i,s),s.isDirectory){const o=await this.readdir(i,{withFileTypes:!0});for(const n of o){const c=`${i==="/"?"":i}/${n.name}`;await r(c)}}};return await r(t),e}async scanWatches(){if(!this.scanning){this.scanning=!0;try{await Promise.all([...this.watchers.entries()].map(async([t,e])=>{let r;try{r=await this.buildSnapshot(t)}catch{r=new Map}for(const[i,s]of r){const o=e.get(i);o?(o.mtime!==s.mtime||o.size!==s.size)&&await this.notifyChange({path:i,type:"changed",isDirectory:!1}):await this.notifyChange({path:i,type:"added",isDirectory:!1})}for(const i of e.keys())r.has(i)||await this.notifyChange({path:i,type:"removed",isDirectory:!1});this.watchers.set(t,r)}))}finally{this.scanning=!1}}}async sync(t,e){await this.ensureMounted();try{(e?.cleanBefore??!1)&&await this.clear("/");for(const[i,s]of t){const o=a.normalizePath(i);let n;s instanceof Blob?n=await a.convertBlobToUint8Array(s):n=s,await this.writeFile(o,n)}await this.notifyChange({path:"/",type:"changed",isDirectory:!0})}catch(r){throw r instanceof a.OPFSError?r:new a.OPFSError("Failed to sync file system","SYNC_FAILED")}}}typeof self<"u"&&self.constructor.name==="DedicatedWorkerGlobalScope"&&y.expose(new w);exports.OPFSWorker=w;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const y=require("comlink"),a=require("./helpers-FvdHLObV.cjs");class w{root=null;watchCallback=null;watchers=new Map;watchTimer=null;scanning=!1;mountingPromise=null;options={watchInterval:1e3,maxFileSize:50*1024*1024,hashAlgorithm:null};async notifyChange(t){if(!this.watchCallback)return;let e;if(this.options.hashAlgorithm&&!t.isDirectory&&t.type!=="removed")try{const r=await this.stat(t.path);r.isFile&&r.hash&&(e=r.hash)}catch(r){console.warn(`Failed to calculate hash for ${t.path}:`,r)}this.watchCallback({timestamp:new Date().toISOString(),...t,...e&&{hash:e}})}constructor(t,e){a.checkOPFSSupport(),t&&(this.watchCallback=t),e&&this.setOptions(e),this.mount("/")}async mount(t="/"){return this.mountingPromise&&await this.mountingPromise,this.mountingPromise=new Promise(async(e,r)=>{this.root=null;try{const i=await navigator.storage.getDirectory();t==="/"?this.root=i:this.root=await this.getDirectoryHandle(t,!0,i),e(!0)}catch(i){console.error(i),r(new a.OPFSError("Failed to initialize OPFS","INIT_FAILED"))}finally{this.mountingPromise=null}}),this.mountingPromise}setWatchCallback(t){this.watchCallback=t}setOptions(t){t.watchInterval!==void 0&&(this.options.watchInterval=t.watchInterval),t.hashAlgorithm!==void 0&&(this.options.hashAlgorithm=t.hashAlgorithm),t.maxFileSize!==void 0&&(this.options.maxFileSize=t.maxFileSize)}async ensureMounted(){if(!this.root){if(this.mountingPromise){await this.mountingPromise;return}throw new a.OPFSError("OPFS not mounted","NOT_MOUNTED")}}async getDirectoryHandle(t,e=!1,r=this.root){if(!r)throw new a.OPFSNotMountedError;const i=Array.isArray(t)?t:a.splitPath(t);let s=r;for(const o of i)s=await s.getDirectoryHandle(o,{create:e});return s}async getFileHandle(t,e=!1,r=this.root){if(!r)throw new a.OPFSNotMountedError;const i=a.splitPath(t);if(i.length===0)throw new a.PathError("Path must not be empty",Array.isArray(t)?t.join("/"):t);const s=i.pop();return(await this.getDirectoryHandle(i,e,r)).getFileHandle(s,{create:e})}async index(){const t=new Map,e=async r=>{const i=await this.readdir(r,{withFileTypes:!0});for(const s of i){const o=`${r==="/"?"":r}/${s.name}`;try{const n=await this.stat(o);t.set(o,n),n.isDirectory&&await e(o)}catch(n){console.warn(`Skipping broken entry: ${o}`,n)}}};return t.set("/",{kind:"directory",size:0,mtime:new Date(0).toISOString(),ctime:new Date(0).toISOString(),isFile:!1,isDirectory:!0}),await e("/"),t}async readFile(t,e="utf-8"){await this.ensureMounted();try{const r=await this.getFileHandle(t,!1),i=await a.readFileData(r);return e==="binary"?i:a.decodeBuffer(i,e)}catch(r){throw console.error(r),new a.FileNotFoundError(t)}}async writeFile(t,e,r){await this.ensureMounted();const i=await this.getFileHandle(t,!0);await a.writeFileData(i,e,r,{truncate:!0}),await this.notifyChange({path:t,type:"changed",isDirectory:!1})}async appendFile(t,e,r){await this.ensureMounted();const i=await this.getFileHandle(t,!0);await a.writeFileData(i,e,r,{append:!0}),await this.notifyChange({path:t,type:"changed",isDirectory:!1})}async mkdir(t,e){if(await this.ensureMounted(),!this.root)throw new a.OPFSNotMountedError;const r=e?.recursive??!1,i=a.splitPath(t);let s=this.root;for(let o=0;o<i.length;o++){const n=i[o];try{s=await s.getDirectoryHandle(n,{create:r||o===i.length-1})}catch(c){throw c.name==="NotFoundError"?new a.OPFSError(`Parent directory does not exist: ${a.joinPath(i.slice(0,o+1))}`,"ENOENT"):c.name==="TypeMismatchError"?new a.OPFSError(`Path segment is not a directory: ${n}`,"ENOTDIR"):new a.OPFSError("Failed to create directory","MKDIR_FAILED")}}await this.notifyChange({path:t,type:"added",isDirectory:!0})}async stat(t){if(await this.ensureMounted(),t==="/")return{kind:"directory",size:0,mtime:new Date(0).toISOString(),ctime:new Date(0).toISOString(),isFile:!1,isDirectory:!0};const e=a.basename(t),r=await this.getDirectoryHandle(a.dirname(t),!1),i=this.options.hashAlgorithm!==null;try{const o=await(await r.getFileHandle(e,{create:!1})).getFile(),n={kind:"file",size:o.size,mtime:new Date(o.lastModified).toISOString(),ctime:new Date(o.lastModified).toISOString(),isFile:!0,isDirectory:!1};if(i&&this.options.hashAlgorithm)try{const c=await a.calculateFileHash(o,this.options.hashAlgorithm,this.options.maxFileSize);n.hash=c}catch(c){console.warn(`Failed to calculate hash for ${t}:`,c)}return n}catch(s){if(s.name!=="TypeMismatchError"&&s.name!=="NotFoundError")throw new a.OPFSError("Failed to stat (file)","STAT_FAILED")}try{return await r.getDirectoryHandle(e,{create:!1}),{kind:"directory",size:0,mtime:new Date(0).toISOString(),ctime:new Date(0).toISOString(),isFile:!1,isDirectory:!0}}catch(s){throw s.name==="NotFoundError"?new a.OPFSError(`No such file or directory: ${t}`,"ENOENT"):new a.OPFSError("Failed to stat (directory)","STAT_FAILED")}}async readdir(t,e){await this.ensureMounted();const r=e?.withFileTypes??!1,i=await this.getDirectoryHandle(t,!1);if(r){const s=[];for await(const[o,n]of i.entries()){const c=n.kind==="file";s.push({name:o,kind:n.kind,isFile:c,isDirectory:!c})}return s}else{const s=[];for await(const[o]of i.entries())s.push(o);return s}}async exists(t){if(await this.ensureMounted(),t==="/")return!0;const e=a.basename(t);let r=null;try{r=await this.getDirectoryHandle(a.dirname(t),!1)}catch(i){throw(i.name==="NotFoundError"||i.name==="TypeMismatchError")&&(r=null),i}if(!r||!e)return!1;try{return await r.getFileHandle(e,{create:!1}),!0}catch(i){if(i.name!=="NotFoundError"&&i.name!=="TypeMismatchError")throw i}try{return await r.getDirectoryHandle(e,{create:!1}),!0}catch(i){if(i.name!=="NotFoundError"&&i.name!=="TypeMismatchError")throw i}return!1}async clear(t="/"){await this.ensureMounted();try{const e=await this.readdir(t,{withFileTypes:!0});for(const r of e){const i=`${t==="/"?"":t}/${r.name}`;await this.remove(i,{recursive:!0})}await this.notifyChange({path:t,type:"changed",isDirectory:!0})}catch(e){throw e instanceof a.OPFSError?e:new a.OPFSError(`Failed to clear directory: ${t}`,"CLEAR_FAILED")}}async remove(t,e){await this.ensureMounted();const r=e?.recursive??!1,i=e?.force??!1;if(t==="/")throw new a.OPFSError("Cannot remove root directory","EROOT");const s=a.basename(t);if(!s)throw new a.PathError("Invalid path",t);const o=await this.getDirectoryHandle(a.dirname(t),!1);try{await o.removeEntry(s,{recursive:r})}catch(n){if(n.name==="NotFoundError"){if(!i)throw new a.OPFSError(`No such file or directory: ${t}`,"ENOENT")}else throw n.name==="InvalidModificationError"?new a.OPFSError(`Directory not empty: ${t}. Use recursive option to force removal.`,"ENOTEMPTY"):n.name==="TypeMismatchError"&&!r?new a.OPFSError(`Cannot remove directory without recursive option: ${t}`,"EISDIR"):new a.OPFSError(`Failed to remove path: ${t}`,"RM_FAILED")}await this.notifyChange({path:t,type:"removed",isDirectory:!1})}async realpath(t){await this.ensureMounted();try{const e=a.resolvePath(t);if(!await this.exists(e))throw new a.FileNotFoundError(e);return e}catch(e){throw e instanceof a.OPFSError?e:new a.OPFSError(`Failed to resolve path: ${t}`,"REALPATH_FAILED")}}async rename(t,e){await this.ensureMounted();try{if(!await this.exists(t))throw new a.FileNotFoundError(t);await this.copy(t,e,{recursive:!0}),await this.remove(t,{recursive:!0}),await this.notifyChange({path:t,type:"removed",isDirectory:!1}),await this.notifyChange({path:e,type:"added",isDirectory:!1})}catch(r){throw r instanceof a.OPFSError?r:new a.OPFSError(`Failed to rename from ${t} to ${e}`,"RENAME_FAILED")}}async copy(t,e,r){await this.ensureMounted();try{const i=r?.recursive??!1,s=r?.force??!0;if(!await this.exists(t))throw new a.OPFSError(`Source does not exist: ${t}`,"ENOENT");if(await this.exists(e)&&!s)throw new a.OPFSError(`Destination already exists: ${e}`,"EEXIST");if((await this.stat(t)).isFile){const h=await this.readFile(t,"binary");await this.writeFile(e,h)}else{if(!i)throw new a.OPFSError(`Cannot copy directory without recursive option: ${t}`,"EISDIR");await this.mkdir(e,{recursive:!0});const h=await this.readdir(t,{withFileTypes:!0});for(const l of h){const f=`${t}/${l.name}`,u=`${e}/${l.name}`;await this.copy(f,u,{recursive:!0,force:s})}}await this.notifyChange({path:e,type:"added",isDirectory:!1})}catch(i){throw i instanceof a.OPFSError?i:new a.OPFSError(`Failed to copy from ${t} to ${e}`,"CP_FAILED")}}async watch(t){await this.ensureMounted();const e=a.normalizePath(t),r=await this.buildSnapshot(e);this.watchers.set(e,r),this.watchTimer||(this.watchTimer=setInterval(()=>{this.scanWatches()},this.options.watchInterval))}unwatch(t){const e=a.normalizePath(t);this.watchers.delete(e),this.watchers.size===0&&this.watchTimer&&(clearInterval(this.watchTimer),this.watchTimer=null)}async buildSnapshot(t){const e=new Map,r=async i=>{const s=await this.stat(i);if(e.set(i,s),s.isDirectory){const o=await this.readdir(i,{withFileTypes:!0});for(const n of o){const c=`${i==="/"?"":i}/${n.name}`;await r(c)}}};return await r(t),e}async scanWatches(){if(!this.scanning){this.scanning=!0;try{await Promise.all([...this.watchers.entries()].map(async([t,e])=>{let r;try{r=await this.buildSnapshot(t)}catch{r=new Map}for(const[i,s]of r){const o=e.get(i);o?(o.mtime!==s.mtime||o.size!==s.size)&&await this.notifyChange({path:i,type:"changed",isDirectory:!1}):await this.notifyChange({path:i,type:"added",isDirectory:!1})}for(const i of e.keys())r.has(i)||await this.notifyChange({path:i,type:"removed",isDirectory:!1});this.watchers.set(t,r)}))}finally{this.scanning=!1}}}async sync(t,e){await this.ensureMounted();try{(e?.cleanBefore??!1)&&await this.clear("/");for(const[i,s]of t){const o=a.normalizePath(i);let n;s instanceof Blob?n=await a.convertBlobToUint8Array(s):n=s,await this.writeFile(o,n)}await this.notifyChange({path:"/",type:"changed",isDirectory:!0})}catch(r){throw r instanceof a.OPFSError?r:new a.OPFSError("Failed to sync file system","SYNC_FAILED")}}}typeof self<"u"&&self.constructor.name==="DedicatedWorkerGlobalScope"&&y.expose(new w);exports.OPFSWorker=w;
2
2
  //# sourceMappingURL=raw.cjs.map