opfs-worker 0.2.1 → 0.2.2
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/README.md +260 -27
- package/dist/assets/worker-CMvl9yOu.js.map +1 -0
- package/dist/index.cjs +189 -135
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +6 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1184 -11
- package/dist/index.js.map +1 -1
- package/dist/raw.cjs +1 -1
- package/dist/raw.cjs.map +1 -1
- package/dist/raw.js +154 -100
- package/dist/raw.js.map +1 -1
- package/dist/worker.d.ts +36 -8
- package/dist/worker.d.ts.map +1 -1
- package/package.json +1 -1
- package/dist/assets/worker-ChfzBM-o.js.map +0 -1
- package/dist/types.js +0 -1
- package/dist/utils/encoder.js +0 -83
- package/dist/utils/errors.js +0 -77
- package/dist/utils/helpers.js +0 -246
- package/dist/worker.js +0 -818
package/dist/index.js
CHANGED
|
@@ -1,13 +1,1186 @@
|
|
|
1
|
-
import { wrap } from
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Creates a new file system instance with inline worker
|
|
9
|
-
* @returns Promise resolving to the file system interface
|
|
1
|
+
import { wrap as i, proxy as o } from "comlink";
|
|
2
|
+
import { D as u, F as d, O as p, b as w, a as y, P as m, c as g, S as v, T as E, e as S, k as F, d as b, l as P, h as x, o as A, f as T, m as O, g as I, j as _, n as D, i as k, r as C, s as U, w as R } from "./helpers-Co-qCBmA.js";
|
|
3
|
+
const a = `/**
|
|
4
|
+
* @license
|
|
5
|
+
* Copyright 2019 Google LLC
|
|
6
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
10
7
|
*/
|
|
11
|
-
|
|
12
|
-
|
|
8
|
+
const H = 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 = {
|
|
9
|
+
canHandle: (e) => $(e) && e[H],
|
|
10
|
+
serialize(e) {
|
|
11
|
+
const { port1: t, port2: r } = new MessageChannel();
|
|
12
|
+
return O(e, t), [r, [r]];
|
|
13
|
+
},
|
|
14
|
+
deserialize(e) {
|
|
15
|
+
return e.start(), Y(e);
|
|
16
|
+
}
|
|
17
|
+
}, j = {
|
|
18
|
+
canHandle: (e) => $(e) && p in e,
|
|
19
|
+
serialize({ value: e }) {
|
|
20
|
+
let t;
|
|
21
|
+
return e instanceof Error ? t = {
|
|
22
|
+
isError: !0,
|
|
23
|
+
value: {
|
|
24
|
+
message: e.message,
|
|
25
|
+
name: e.name,
|
|
26
|
+
stack: e.stack
|
|
27
|
+
}
|
|
28
|
+
} : t = { isError: !1, value: e }, [t, []];
|
|
29
|
+
},
|
|
30
|
+
deserialize(e) {
|
|
31
|
+
throw e.isError ? Object.assign(new Error(e.value.message), e.value) : e.value;
|
|
32
|
+
}
|
|
33
|
+
}, U = /* @__PURE__ */ new Map([
|
|
34
|
+
["proxy", W],
|
|
35
|
+
["throw", j]
|
|
36
|
+
]);
|
|
37
|
+
function V(e, t) {
|
|
38
|
+
for (const r of e)
|
|
39
|
+
if (t === r || r === "*" || r instanceof RegExp && r.test(t))
|
|
40
|
+
return !0;
|
|
41
|
+
return !1;
|
|
13
42
|
}
|
|
43
|
+
function O(e, t = globalThis, r = ["*"]) {
|
|
44
|
+
t.addEventListener("message", function a(n) {
|
|
45
|
+
if (!n || !n.data)
|
|
46
|
+
return;
|
|
47
|
+
if (!V(r, n.origin)) {
|
|
48
|
+
console.warn(\`Invalid origin '\${n.origin}' for comlink proxy\`);
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
const { id: s, type: o, path: i } = Object.assign({ path: [] }, n.data), l = (n.data.argumentList || []).map(d);
|
|
52
|
+
let u;
|
|
53
|
+
try {
|
|
54
|
+
const f = i.slice(0, -1).reduce((h, g) => h[g], e), w = i.reduce((h, g) => h[g], e);
|
|
55
|
+
switch (o) {
|
|
56
|
+
case "GET":
|
|
57
|
+
u = w;
|
|
58
|
+
break;
|
|
59
|
+
case "SET":
|
|
60
|
+
f[i.slice(-1)[0]] = d(n.data.value), u = !0;
|
|
61
|
+
break;
|
|
62
|
+
case "APPLY":
|
|
63
|
+
u = w.apply(f, l);
|
|
64
|
+
break;
|
|
65
|
+
case "CONSTRUCT":
|
|
66
|
+
{
|
|
67
|
+
const h = new w(...l);
|
|
68
|
+
u = Q(h);
|
|
69
|
+
}
|
|
70
|
+
break;
|
|
71
|
+
case "ENDPOINT":
|
|
72
|
+
{
|
|
73
|
+
const { port1: h, port2: g } = new MessageChannel();
|
|
74
|
+
O(e, g), u = J(h, [h]);
|
|
75
|
+
}
|
|
76
|
+
break;
|
|
77
|
+
case "RELEASE":
|
|
78
|
+
u = void 0;
|
|
79
|
+
break;
|
|
80
|
+
default:
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
} catch (f) {
|
|
84
|
+
u = { value: f, [p]: 0 };
|
|
85
|
+
}
|
|
86
|
+
Promise.resolve(u).catch((f) => ({ value: f, [p]: 0 })).then((f) => {
|
|
87
|
+
const [w, h] = A(f);
|
|
88
|
+
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]());
|
|
89
|
+
}).catch((f) => {
|
|
90
|
+
const [w, h] = A({
|
|
91
|
+
value: new TypeError("Unserializable return value"),
|
|
92
|
+
[p]: 0
|
|
93
|
+
});
|
|
94
|
+
t.postMessage(Object.assign(Object.assign({}, w), { id: s }), h);
|
|
95
|
+
});
|
|
96
|
+
}), t.start && t.start();
|
|
97
|
+
}
|
|
98
|
+
function G(e) {
|
|
99
|
+
return e.constructor.name === "MessagePort";
|
|
100
|
+
}
|
|
101
|
+
function L(e) {
|
|
102
|
+
G(e) && e.close();
|
|
103
|
+
}
|
|
104
|
+
function Y(e, t) {
|
|
105
|
+
const r = /* @__PURE__ */ new Map();
|
|
106
|
+
return e.addEventListener("message", function(n) {
|
|
107
|
+
const { data: s } = n;
|
|
108
|
+
if (!s || !s.id)
|
|
109
|
+
return;
|
|
110
|
+
const o = r.get(s.id);
|
|
111
|
+
if (o)
|
|
112
|
+
try {
|
|
113
|
+
o(s);
|
|
114
|
+
} finally {
|
|
115
|
+
r.delete(s.id);
|
|
116
|
+
}
|
|
117
|
+
}), x(e, r, [], t);
|
|
118
|
+
}
|
|
119
|
+
function E(e) {
|
|
120
|
+
if (e)
|
|
121
|
+
throw new Error("Proxy has been released and is not useable");
|
|
122
|
+
}
|
|
123
|
+
function z(e) {
|
|
124
|
+
return y(e, /* @__PURE__ */ new Map(), {
|
|
125
|
+
type: "RELEASE"
|
|
126
|
+
}).then(() => {
|
|
127
|
+
L(e);
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
const F = /* @__PURE__ */ new WeakMap(), S = "FinalizationRegistry" in globalThis && new FinalizationRegistry((e) => {
|
|
131
|
+
const t = (F.get(e) || 0) - 1;
|
|
132
|
+
F.set(e, t), t === 0 && z(e);
|
|
133
|
+
});
|
|
134
|
+
function X(e, t) {
|
|
135
|
+
const r = (F.get(t) || 0) + 1;
|
|
136
|
+
F.set(t, r), S && S.register(e, t, e);
|
|
137
|
+
}
|
|
138
|
+
function q(e) {
|
|
139
|
+
S && S.unregister(e);
|
|
140
|
+
}
|
|
141
|
+
function x(e, t, r = [], a = function() {
|
|
142
|
+
}) {
|
|
143
|
+
let n = !1;
|
|
144
|
+
const s = new Proxy(a, {
|
|
145
|
+
get(o, i) {
|
|
146
|
+
if (E(n), i === B)
|
|
147
|
+
return () => {
|
|
148
|
+
q(s), z(e), t.clear(), n = !0;
|
|
149
|
+
};
|
|
150
|
+
if (i === "then") {
|
|
151
|
+
if (r.length === 0)
|
|
152
|
+
return { then: () => s };
|
|
153
|
+
const l = y(e, t, {
|
|
154
|
+
type: "GET",
|
|
155
|
+
path: r.map((u) => u.toString())
|
|
156
|
+
}).then(d);
|
|
157
|
+
return l.then.bind(l);
|
|
158
|
+
}
|
|
159
|
+
return x(e, t, [...r, i]);
|
|
160
|
+
},
|
|
161
|
+
set(o, i, l) {
|
|
162
|
+
E(n);
|
|
163
|
+
const [u, f] = A(l);
|
|
164
|
+
return y(e, t, {
|
|
165
|
+
type: "SET",
|
|
166
|
+
path: [...r, i].map((w) => w.toString()),
|
|
167
|
+
value: u
|
|
168
|
+
}, f).then(d);
|
|
169
|
+
},
|
|
170
|
+
apply(o, i, l) {
|
|
171
|
+
E(n);
|
|
172
|
+
const u = r[r.length - 1];
|
|
173
|
+
if (u === R)
|
|
174
|
+
return y(e, t, {
|
|
175
|
+
type: "ENDPOINT"
|
|
176
|
+
}).then(d);
|
|
177
|
+
if (u === "bind")
|
|
178
|
+
return x(e, t, r.slice(0, -1));
|
|
179
|
+
const [f, w] = v(l);
|
|
180
|
+
return y(e, t, {
|
|
181
|
+
type: "APPLY",
|
|
182
|
+
path: r.map((h) => h.toString()),
|
|
183
|
+
argumentList: f
|
|
184
|
+
}, w).then(d);
|
|
185
|
+
},
|
|
186
|
+
construct(o, i) {
|
|
187
|
+
E(n);
|
|
188
|
+
const [l, u] = v(i);
|
|
189
|
+
return y(e, t, {
|
|
190
|
+
type: "CONSTRUCT",
|
|
191
|
+
path: r.map((f) => f.toString()),
|
|
192
|
+
argumentList: l
|
|
193
|
+
}, u).then(d);
|
|
194
|
+
}
|
|
195
|
+
});
|
|
196
|
+
return X(s, e), s;
|
|
197
|
+
}
|
|
198
|
+
function K(e) {
|
|
199
|
+
return Array.prototype.concat.apply([], e);
|
|
200
|
+
}
|
|
201
|
+
function v(e) {
|
|
202
|
+
const t = e.map(A);
|
|
203
|
+
return [t.map((r) => r[0]), K(t.map((r) => r[1]))];
|
|
204
|
+
}
|
|
205
|
+
const _ = /* @__PURE__ */ new WeakMap();
|
|
206
|
+
function J(e, t) {
|
|
207
|
+
return _.set(e, t), e;
|
|
208
|
+
}
|
|
209
|
+
function Q(e) {
|
|
210
|
+
return Object.assign(e, { [H]: !0 });
|
|
211
|
+
}
|
|
212
|
+
function A(e) {
|
|
213
|
+
for (const [t, r] of U)
|
|
214
|
+
if (r.canHandle(e)) {
|
|
215
|
+
const [a, n] = r.serialize(e);
|
|
216
|
+
return [
|
|
217
|
+
{
|
|
218
|
+
type: "HANDLER",
|
|
219
|
+
name: t,
|
|
220
|
+
value: a
|
|
221
|
+
},
|
|
222
|
+
n
|
|
223
|
+
];
|
|
224
|
+
}
|
|
225
|
+
return [
|
|
226
|
+
{
|
|
227
|
+
type: "RAW",
|
|
228
|
+
value: e
|
|
229
|
+
},
|
|
230
|
+
_.get(e) || []
|
|
231
|
+
];
|
|
232
|
+
}
|
|
233
|
+
function d(e) {
|
|
234
|
+
switch (e.type) {
|
|
235
|
+
case "HANDLER":
|
|
236
|
+
return U.get(e.name).deserialize(e.value);
|
|
237
|
+
case "RAW":
|
|
238
|
+
return e.value;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
function y(e, t, r, a) {
|
|
242
|
+
return new Promise((n) => {
|
|
243
|
+
const s = Z();
|
|
244
|
+
t.set(s, n), e.start && e.start(), e.postMessage(Object.assign({ id: s }, r), a);
|
|
245
|
+
});
|
|
246
|
+
}
|
|
247
|
+
function Z() {
|
|
248
|
+
return new Array(4).fill(0).map(() => Math.floor(Math.random() * Number.MAX_SAFE_INTEGER).toString(16)).join("-");
|
|
249
|
+
}
|
|
250
|
+
class c extends Error {
|
|
251
|
+
constructor(t, r, a) {
|
|
252
|
+
super(t), this.code = r, this.path = a, this.name = "OPFSError";
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
class ee extends c {
|
|
256
|
+
constructor() {
|
|
257
|
+
super("OPFS is not supported in this browser", "OPFS_NOT_SUPPORTED");
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
class D extends c {
|
|
261
|
+
constructor() {
|
|
262
|
+
super("OPFS is not mounted", "OPFS_NOT_MOUNTED");
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
class k extends c {
|
|
266
|
+
constructor(t, r) {
|
|
267
|
+
super(t, "INVALID_PATH", r);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
class b extends c {
|
|
271
|
+
constructor(t) {
|
|
272
|
+
super(\`File not found: \${t}\`, "FILE_NOT_FOUND", t);
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
function te(e, t = "utf-8") {
|
|
276
|
+
switch (t) {
|
|
277
|
+
case "utf8":
|
|
278
|
+
case "utf-8":
|
|
279
|
+
return new TextEncoder().encode(e);
|
|
280
|
+
case "utf16le":
|
|
281
|
+
case "ucs2":
|
|
282
|
+
case "ucs-2":
|
|
283
|
+
return ne(e);
|
|
284
|
+
case "ascii":
|
|
285
|
+
return ie(e);
|
|
286
|
+
case "latin1":
|
|
287
|
+
return se(e);
|
|
288
|
+
case "binary":
|
|
289
|
+
return Uint8Array.from(e, (r) => r.charCodeAt(0));
|
|
290
|
+
case "base64":
|
|
291
|
+
return Uint8Array.from(atob(e), (r) => r.charCodeAt(0));
|
|
292
|
+
case "hex":
|
|
293
|
+
if (!/^[\\da-f]+$/i.test(e) || e.length % 2 !== 0)
|
|
294
|
+
throw new c("Invalid hex string", "INVALID_HEX_FORMAT");
|
|
295
|
+
return Uint8Array.from(e.match(/.{1,2}/g).map((r) => parseInt(r, 16)));
|
|
296
|
+
default:
|
|
297
|
+
return console.warn("Encoding not supported, falling back to UTF-8"), new TextEncoder().encode(e);
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
function re(e, t = "utf-8") {
|
|
301
|
+
switch (t) {
|
|
302
|
+
case "utf8":
|
|
303
|
+
case "utf-8":
|
|
304
|
+
return new TextDecoder().decode(e);
|
|
305
|
+
case "utf16le":
|
|
306
|
+
case "ucs2":
|
|
307
|
+
case "ucs-2":
|
|
308
|
+
return ae(e);
|
|
309
|
+
case "latin1":
|
|
310
|
+
return String.fromCharCode(...e);
|
|
311
|
+
case "binary":
|
|
312
|
+
return String.fromCharCode(...e);
|
|
313
|
+
case "ascii":
|
|
314
|
+
return String.fromCharCode(...e.map((r) => r & 127));
|
|
315
|
+
case "base64":
|
|
316
|
+
return btoa(String.fromCharCode(...e));
|
|
317
|
+
case "hex":
|
|
318
|
+
return Array.from(e).map((r) => r.toString(16).padStart(2, "0")).join("");
|
|
319
|
+
default:
|
|
320
|
+
return console.warn("Unsupported encoding, falling back to UTF-8"), new TextDecoder().decode(e);
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
function ne(e) {
|
|
324
|
+
const t = new Uint8Array(e.length * 2);
|
|
325
|
+
for (let r = 0; r < e.length; r++) {
|
|
326
|
+
const a = e.charCodeAt(r);
|
|
327
|
+
t[r * 2] = a & 255, t[r * 2 + 1] = a >> 8;
|
|
328
|
+
}
|
|
329
|
+
return t;
|
|
330
|
+
}
|
|
331
|
+
function ae(e) {
|
|
332
|
+
e.length % 2 !== 0 && (console.warn("Invalid UTF-16LE buffer length, truncating last byte"), e = e.slice(0, e.length - 1));
|
|
333
|
+
const t = new Uint16Array(e.buffer, e.byteOffset, e.byteLength / 2);
|
|
334
|
+
return String.fromCharCode(...t);
|
|
335
|
+
}
|
|
336
|
+
function se(e) {
|
|
337
|
+
const t = new Uint8Array(e.length);
|
|
338
|
+
for (let r = 0; r < e.length; r++)
|
|
339
|
+
t[r] = e.charCodeAt(r) & 255;
|
|
340
|
+
return t;
|
|
341
|
+
}
|
|
342
|
+
function ie(e) {
|
|
343
|
+
const t = new Uint8Array(e.length);
|
|
344
|
+
for (let r = 0; r < e.length; r++)
|
|
345
|
+
t[r] = e.charCodeAt(r) & 127;
|
|
346
|
+
return t;
|
|
347
|
+
}
|
|
348
|
+
function oe() {
|
|
349
|
+
if (!("storage" in navigator) || !("getDirectory" in navigator.storage))
|
|
350
|
+
throw new ee();
|
|
351
|
+
}
|
|
352
|
+
function m(e) {
|
|
353
|
+
return Array.isArray(e) ? e : e.split("/").filter(Boolean);
|
|
354
|
+
}
|
|
355
|
+
function N(e) {
|
|
356
|
+
return typeof e == "string" ? e ?? "/" : \`/\${e.join("/")}\`;
|
|
357
|
+
}
|
|
358
|
+
function P(e) {
|
|
359
|
+
const t = m(e);
|
|
360
|
+
return t[t.length - 1] || "";
|
|
361
|
+
}
|
|
362
|
+
function M(e) {
|
|
363
|
+
const t = m(e);
|
|
364
|
+
return t.pop(), N(t);
|
|
365
|
+
}
|
|
366
|
+
function I(e) {
|
|
367
|
+
return !e || e === "/" ? "/" : e.startsWith("/") ? e : \`/\${e}\`;
|
|
368
|
+
}
|
|
369
|
+
function ce(e) {
|
|
370
|
+
const t = m(e), r = [];
|
|
371
|
+
for (const a of t)
|
|
372
|
+
if (!(a === "." || a === ""))
|
|
373
|
+
if (a === "..") {
|
|
374
|
+
if (r.length === 0)
|
|
375
|
+
continue;
|
|
376
|
+
r.pop();
|
|
377
|
+
} else
|
|
378
|
+
r.push(a);
|
|
379
|
+
return N(r);
|
|
380
|
+
}
|
|
381
|
+
function le(e, t = "utf-8") {
|
|
382
|
+
return typeof e == "string" ? te(e, t) : e instanceof Uint8Array ? e : new Uint8Array(e);
|
|
383
|
+
}
|
|
384
|
+
async function ue(e) {
|
|
385
|
+
const t = await e.createSyncAccessHandle();
|
|
386
|
+
try {
|
|
387
|
+
const r = t.getSize(), a = new Uint8Array(r);
|
|
388
|
+
return t.read(a, { at: 0 }), a;
|
|
389
|
+
} finally {
|
|
390
|
+
t.close();
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
async function C(e, t, r, a = {}) {
|
|
394
|
+
let n = null;
|
|
395
|
+
try {
|
|
396
|
+
n = await e.createSyncAccessHandle();
|
|
397
|
+
const s = le(t, r), o = a.append ? n.getSize() : 0;
|
|
398
|
+
n.write(s, { at: o }), a.truncate && !a.append && n.truncate(s.byteLength), n.flush();
|
|
399
|
+
} catch (s) {
|
|
400
|
+
console.error(s);
|
|
401
|
+
const o = a.append ? "append" : "write";
|
|
402
|
+
throw new c(\`Failed to \${o} file\`, \`\${o.toUpperCase()}_FAILED\`);
|
|
403
|
+
} finally {
|
|
404
|
+
if (n)
|
|
405
|
+
try {
|
|
406
|
+
n.close();
|
|
407
|
+
} catch {
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
async function fe(e, t = "SHA-1") {
|
|
412
|
+
try {
|
|
413
|
+
const r = new Uint8Array(e), a = await crypto.subtle.digest(t, r);
|
|
414
|
+
return Array.from(new Uint8Array(a)).map((s) => s.toString(16).padStart(2, "0")).join("");
|
|
415
|
+
} catch (r) {
|
|
416
|
+
throw console.warn(\`Failed to calculate \${t} hash:\`, r), r;
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
async function he(e) {
|
|
420
|
+
const t = await e.arrayBuffer();
|
|
421
|
+
return new Uint8Array(t);
|
|
422
|
+
}
|
|
423
|
+
class we {
|
|
424
|
+
/** Root directory handle for the file system */
|
|
425
|
+
root = null;
|
|
426
|
+
/** Watch event callback */
|
|
427
|
+
watchCallback = null;
|
|
428
|
+
/** Map of watched paths to their last known state */
|
|
429
|
+
watchers = /* @__PURE__ */ new Map();
|
|
430
|
+
/** Interval handle for polling watched paths */
|
|
431
|
+
watchTimer = null;
|
|
432
|
+
/** Polling interval in milliseconds */
|
|
433
|
+
watchInterval = 1e3;
|
|
434
|
+
/** Flag to avoid concurrent scans */
|
|
435
|
+
scanning = !1;
|
|
436
|
+
/** Promise to prevent concurrent mount operations */
|
|
437
|
+
mountingPromise = null;
|
|
438
|
+
/**
|
|
439
|
+
* Creates a new OPFSFileSystem instance
|
|
440
|
+
*
|
|
441
|
+
* @param watchCallback - Optional callback for file change events
|
|
442
|
+
* @param watchOptions - Optional configuration for watching
|
|
443
|
+
* @throws {OPFSError} If OPFS is not supported in the current browser
|
|
444
|
+
*/
|
|
445
|
+
constructor(t, r) {
|
|
446
|
+
oe(), t && (this.watchCallback = t, r?.watchInterval && (this.watchInterval = r.watchInterval)), this.mount("/");
|
|
447
|
+
}
|
|
448
|
+
/**
|
|
449
|
+
* Initialize the file system within a given directory
|
|
450
|
+
*
|
|
451
|
+
* This method sets up the root directory for all subsequent operations.
|
|
452
|
+
* If no root is specified, it will use the OPFS root directory.
|
|
453
|
+
*
|
|
454
|
+
* @param root - The root path for the file system (default: '/')
|
|
455
|
+
* @returns Promise that resolves to true if initialization was successful
|
|
456
|
+
* @throws {OPFSError} If initialization fails
|
|
457
|
+
*
|
|
458
|
+
* @example
|
|
459
|
+
* \`\`\`typescript
|
|
460
|
+
* const fs = new OPFSFileSystem();
|
|
461
|
+
*
|
|
462
|
+
* // Use OPFS root (default)
|
|
463
|
+
* await fs.mount();
|
|
464
|
+
*
|
|
465
|
+
* // Use custom directory
|
|
466
|
+
* await fs.mount('/my-app');
|
|
467
|
+
* \`\`\`
|
|
468
|
+
*/
|
|
469
|
+
async mount(t = "/") {
|
|
470
|
+
return this.mountingPromise && await this.mountingPromise, this.mountingPromise = new Promise(async (r, a) => {
|
|
471
|
+
this.root = null;
|
|
472
|
+
try {
|
|
473
|
+
const n = await navigator.storage.getDirectory();
|
|
474
|
+
t === "/" ? this.root = n : this.root = await this.getDirectoryHandle(t, !0, n), r(!0);
|
|
475
|
+
} catch (n) {
|
|
476
|
+
console.error(n), a(new c("Failed to initialize OPFS", "INIT_FAILED"));
|
|
477
|
+
} finally {
|
|
478
|
+
this.mountingPromise = null;
|
|
479
|
+
}
|
|
480
|
+
}), this.mountingPromise;
|
|
481
|
+
}
|
|
482
|
+
/**
|
|
483
|
+
* Set the watch callback for file change events
|
|
484
|
+
*
|
|
485
|
+
* @param callback - The callback function to invoke when files change
|
|
486
|
+
* @param options - Optional configuration for watching
|
|
487
|
+
*/
|
|
488
|
+
setWatchCallback(t, r) {
|
|
489
|
+
this.watchCallback = t, r?.watchInterval && (this.watchInterval = r.watchInterval);
|
|
490
|
+
}
|
|
491
|
+
/**
|
|
492
|
+
* Automatically mount the OPFS root if not already mounted
|
|
493
|
+
*
|
|
494
|
+
* This method is called internally when file operations are performed
|
|
495
|
+
* without explicitly mounting first.
|
|
496
|
+
*
|
|
497
|
+
* @returns Promise that resolves when auto-mount is complete
|
|
498
|
+
* @throws {OPFSError} If auto-mount fails
|
|
499
|
+
*/
|
|
500
|
+
async ensureMounted() {
|
|
501
|
+
if (!this.root) {
|
|
502
|
+
if (this.mountingPromise) {
|
|
503
|
+
await this.mountingPromise;
|
|
504
|
+
return;
|
|
505
|
+
}
|
|
506
|
+
throw new c("OPFS not mounted", "NOT_MOUNTED");
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
/**
|
|
510
|
+
* Get a directory handle from a path
|
|
511
|
+
*
|
|
512
|
+
* Navigates through the directory structure to find or create a directory
|
|
513
|
+
* at the specified path.
|
|
514
|
+
*
|
|
515
|
+
* @param path - The path to the directory (string or array of segments)
|
|
516
|
+
* @param create - Whether to create the directory if it doesn't exist (default: false)
|
|
517
|
+
* @param from - The directory to start from (default: root directory)
|
|
518
|
+
* @returns Promise that resolves to the directory handle
|
|
519
|
+
* @throws {OPFSError} If the directory cannot be accessed or created
|
|
520
|
+
*
|
|
521
|
+
* @example
|
|
522
|
+
* \`\`\`typescript
|
|
523
|
+
* const docsDir = await fs.getDirectoryHandle('/users/john/documents', true);
|
|
524
|
+
* const docsDir2 = await fs.getDirectoryHandle(['users', 'john', 'documents'], true);
|
|
525
|
+
* \`\`\`
|
|
526
|
+
*/
|
|
527
|
+
async getDirectoryHandle(t, r = !1, a = this.root) {
|
|
528
|
+
if (!a)
|
|
529
|
+
throw new D();
|
|
530
|
+
const n = Array.isArray(t) ? t : m(t);
|
|
531
|
+
let s = a;
|
|
532
|
+
for (const o of n)
|
|
533
|
+
s = await s.getDirectoryHandle(o, { create: r });
|
|
534
|
+
return s;
|
|
535
|
+
}
|
|
536
|
+
/**
|
|
537
|
+
* Get a file handle from a path
|
|
538
|
+
*
|
|
539
|
+
* Navigates to the parent directory and retrieves or creates a file handle
|
|
540
|
+
* for the specified file path.
|
|
541
|
+
*
|
|
542
|
+
* @param path - The path to the file (string or array of segments)
|
|
543
|
+
* @param create - Whether to create the file if it doesn't exist (default: false)
|
|
544
|
+
* @param from - The directory to start from (default: root directory)
|
|
545
|
+
* @returns Promise that resolves to the file handle
|
|
546
|
+
* @throws {PathError} If the path is empty
|
|
547
|
+
* @throws {OPFSError} If the file cannot be accessed or created
|
|
548
|
+
*
|
|
549
|
+
* @example
|
|
550
|
+
* \`\`\`typescript
|
|
551
|
+
* const fileHandle = await fs.getFileHandle('/config/settings.json', true);
|
|
552
|
+
* const fileHandle2 = await fs.getFileHandle(['config', 'settings.json'], true);
|
|
553
|
+
* \`\`\`
|
|
554
|
+
*/
|
|
555
|
+
async getFileHandle(t, r = !1, a = this.root) {
|
|
556
|
+
if (!a)
|
|
557
|
+
throw new D();
|
|
558
|
+
const n = m(t);
|
|
559
|
+
if (n.length === 0)
|
|
560
|
+
throw new k("Path must not be empty", Array.isArray(t) ? t.join("/") : t);
|
|
561
|
+
const s = n.pop();
|
|
562
|
+
return (await this.getDirectoryHandle(n, r, a)).getFileHandle(s, { create: r });
|
|
563
|
+
}
|
|
564
|
+
/**
|
|
565
|
+
* Recursively list all files and directories with their stats
|
|
566
|
+
*
|
|
567
|
+
* Traverses the entire file system starting from the root and returns
|
|
568
|
+
* a Map containing all paths and their corresponding file statistics.
|
|
569
|
+
*
|
|
570
|
+
* @param options - Options for indexing
|
|
571
|
+
* @param options.includeHash - Whether to calculate file hash (default: false)
|
|
572
|
+
* @param options.hashAlgorithm - Hash algorithm to use (default: 'SHA-1', fastest)
|
|
573
|
+
* @returns Promise that resolves to a Map of path => FileStat
|
|
574
|
+
* @throws {OPFSError} If the indexing operation fails
|
|
575
|
+
*
|
|
576
|
+
* @example
|
|
577
|
+
* \`\`\`typescript
|
|
578
|
+
* // Basic index without hash
|
|
579
|
+
* const index = await fs.index();
|
|
580
|
+
*
|
|
581
|
+
* // Index with file hash
|
|
582
|
+
* const indexWithHash = await fs.index({
|
|
583
|
+
* includeHash: true,
|
|
584
|
+
* hashAlgorithm: 'SHA-1'
|
|
585
|
+
* });
|
|
586
|
+
*
|
|
587
|
+
* // Iterate through all files and directories
|
|
588
|
+
* for (const [path, stat] of index) {
|
|
589
|
+
* console.log(\`\${path}: \${stat.isFile ? 'file' : 'directory'} (\${stat.size} bytes)\`);
|
|
590
|
+
* if (stat.hash) console.log(\` Hash: \${stat.hash}\`);
|
|
591
|
+
* }
|
|
592
|
+
*
|
|
593
|
+
* // Get specific file stats
|
|
594
|
+
* const fileStats = index.get('/data/config.json');
|
|
595
|
+
* if (fileStats) {
|
|
596
|
+
* console.log(\`File size: \${fileStats.size} bytes\`);
|
|
597
|
+
* if (fileStats.hash) console.log(\`Hash: \${fileStats.hash}\`);
|
|
598
|
+
* }
|
|
599
|
+
* \`\`\`
|
|
600
|
+
*/
|
|
601
|
+
async index(t) {
|
|
602
|
+
const r = /* @__PURE__ */ new Map(), a = async (n) => {
|
|
603
|
+
const s = await this.readdir(n, { withFileTypes: !0 });
|
|
604
|
+
for (const o of s) {
|
|
605
|
+
const i = \`\${n === "/" ? "" : n}/\${o.name}\`;
|
|
606
|
+
try {
|
|
607
|
+
const l = await this.stat(i, t);
|
|
608
|
+
r.set(i, l), l.isDirectory && await a(i);
|
|
609
|
+
} catch (l) {
|
|
610
|
+
console.warn(\`Skipping broken entry: \${i}\`, l);
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
};
|
|
614
|
+
return r.set("/", {
|
|
615
|
+
kind: "directory",
|
|
616
|
+
size: 0,
|
|
617
|
+
mtime: (/* @__PURE__ */ new Date(0)).toISOString(),
|
|
618
|
+
ctime: (/* @__PURE__ */ new Date(0)).toISOString(),
|
|
619
|
+
isFile: !1,
|
|
620
|
+
isDirectory: !0
|
|
621
|
+
}), await a("/"), r;
|
|
622
|
+
}
|
|
623
|
+
async readFile(t, r = "utf-8") {
|
|
624
|
+
await this.ensureMounted();
|
|
625
|
+
try {
|
|
626
|
+
const a = await this.getFileHandle(t, !1), n = await ue(a);
|
|
627
|
+
return r === "binary" ? n : re(n, r);
|
|
628
|
+
} catch (a) {
|
|
629
|
+
throw console.error(a), new b(t);
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
/**
|
|
633
|
+
* Write data to a file
|
|
634
|
+
*
|
|
635
|
+
* Creates or overwrites a file with the specified data. If the file already
|
|
636
|
+
* exists, it will be truncated before writing.
|
|
637
|
+
*
|
|
638
|
+
* @param path - The path to the file to write
|
|
639
|
+
* @param data - The data to write to the file (string, Uint8Array, or ArrayBuffer)
|
|
640
|
+
* @param encoding - The encoding to use when writing string data (default: 'utf-8')
|
|
641
|
+
* @returns Promise that resolves when the write operation is complete
|
|
642
|
+
* @throws {OPFSError} If writing the file fails
|
|
643
|
+
*
|
|
644
|
+
* @example
|
|
645
|
+
* \`\`\`typescript
|
|
646
|
+
* // Write text data
|
|
647
|
+
* await fs.writeFile('/config/settings.json', JSON.stringify({ theme: 'dark' }));
|
|
648
|
+
*
|
|
649
|
+
* // Write binary data
|
|
650
|
+
* const binaryData = new Uint8Array([1, 2, 3, 4, 5]);
|
|
651
|
+
* await fs.writeFile('/data/binary.dat', binaryData);
|
|
652
|
+
*
|
|
653
|
+
* // Write with specific encoding
|
|
654
|
+
* await fs.writeFile('/data/utf16.txt', 'Hello World', 'utf-16le');
|
|
655
|
+
* \`\`\`
|
|
656
|
+
*/
|
|
657
|
+
async writeFile(t, r, a) {
|
|
658
|
+
await this.ensureMounted();
|
|
659
|
+
const n = await this.getFileHandle(t, !0);
|
|
660
|
+
await C(n, r, a, { truncate: !0 });
|
|
661
|
+
}
|
|
662
|
+
/**
|
|
663
|
+
* Append data to a file
|
|
664
|
+
*
|
|
665
|
+
* Adds data to the end of an existing file. If the file doesn't exist,
|
|
666
|
+
* it will be created.
|
|
667
|
+
*
|
|
668
|
+
* @param path - The path to the file to append to
|
|
669
|
+
* @param data - The data to append to the file (string, Uint8Array, or ArrayBuffer)
|
|
670
|
+
* @param encoding - The encoding to use when appending string data (default: 'utf-8')
|
|
671
|
+
* @returns Promise that resolves when the append operation is complete
|
|
672
|
+
* @throws {OPFSError} If appending to the file fails
|
|
673
|
+
*
|
|
674
|
+
* @example
|
|
675
|
+
* \`\`\`typescript
|
|
676
|
+
* // Append text to a log file
|
|
677
|
+
* await fs.appendFile('/logs/app.log', \`[\${new Date().toISOString()}] User logged in\\n\`);
|
|
678
|
+
*
|
|
679
|
+
* // Append binary data
|
|
680
|
+
* const additionalData = new Uint8Array([6, 7, 8]);
|
|
681
|
+
* await fs.appendFile('/data/binary.dat', additionalData);
|
|
682
|
+
* \`\`\`
|
|
683
|
+
*/
|
|
684
|
+
async appendFile(t, r, a) {
|
|
685
|
+
await this.ensureMounted();
|
|
686
|
+
const n = await this.getFileHandle(t, !0);
|
|
687
|
+
await C(n, r, a, { append: !0 });
|
|
688
|
+
}
|
|
689
|
+
/**
|
|
690
|
+
* Create a directory
|
|
691
|
+
*
|
|
692
|
+
* Creates a new directory at the specified path. If the recursive option
|
|
693
|
+
* is enabled, parent directories will be created as needed.
|
|
694
|
+
*
|
|
695
|
+
* @param path - The path where the directory should be created
|
|
696
|
+
* @param options - Options for directory creation
|
|
697
|
+
* @param options.recursive - Whether to create parent directories if they don't exist (default: false)
|
|
698
|
+
* @returns Promise that resolves when the directory is created
|
|
699
|
+
* @throws {OPFSError} If the directory cannot be created
|
|
700
|
+
*
|
|
701
|
+
* @example
|
|
702
|
+
* \`\`\`typescript
|
|
703
|
+
* // Create a single directory
|
|
704
|
+
* await fs.mkdir('/users/john');
|
|
705
|
+
*
|
|
706
|
+
* // Create nested directories
|
|
707
|
+
* await fs.mkdir('/users/john/documents/projects', { recursive: true });
|
|
708
|
+
* \`\`\`
|
|
709
|
+
*/
|
|
710
|
+
async mkdir(t, r) {
|
|
711
|
+
if (await this.ensureMounted(), !this.root)
|
|
712
|
+
throw new D();
|
|
713
|
+
const a = r?.recursive ?? !1, n = m(t);
|
|
714
|
+
let s = this.root;
|
|
715
|
+
for (let o = 0; o < n.length; o++) {
|
|
716
|
+
const i = n[o];
|
|
717
|
+
try {
|
|
718
|
+
s = await s.getDirectoryHandle(i, { create: a || o === n.length - 1 });
|
|
719
|
+
} catch (l) {
|
|
720
|
+
throw l.name === "NotFoundError" ? new c(
|
|
721
|
+
\`Parent directory does not exist: \${N(n.slice(0, o + 1))}\`,
|
|
722
|
+
"ENOENT"
|
|
723
|
+
) : l.name === "TypeMismatchError" ? new c(\`Path segment is not a directory: \${i}\`, "ENOTDIR") : new c("Failed to create directory", "MKDIR_FAILED");
|
|
724
|
+
}
|
|
725
|
+
}
|
|
726
|
+
}
|
|
727
|
+
/**
|
|
728
|
+
* Get file or directory stats
|
|
729
|
+
*
|
|
730
|
+
* Retrieves metadata about a file or directory, including size, modification time,
|
|
731
|
+
* type information, and optionally file hashes.
|
|
732
|
+
*
|
|
733
|
+
* @param path - The path to the file or directory
|
|
734
|
+
* @param options - Options for stat operation
|
|
735
|
+
* @param options.includeHash - Whether to calculate file hash (default: false, only for files)
|
|
736
|
+
* @param options.hashAlgorithm - Hash algorithm to use (default: 'SHA-1', fastest)
|
|
737
|
+
* @returns Promise that resolves to file/directory statistics
|
|
738
|
+
* @throws {OPFSError} If the file or directory does not exist or cannot be accessed
|
|
739
|
+
*
|
|
740
|
+
* @example
|
|
741
|
+
* \`\`\`typescript
|
|
742
|
+
* // Basic stats
|
|
743
|
+
* const stats = await fs.stat('/config/settings.json');
|
|
744
|
+
* console.log(\`File size: \${stats.size} bytes\`);
|
|
745
|
+
* console.log(\`Is file: \${stats.isFile}\`);
|
|
746
|
+
* console.log(\`Modified: \${stats.mtime}\`);
|
|
747
|
+
*
|
|
748
|
+
* // Stats with hash (SHA-1 is fastest)
|
|
749
|
+
* const statsWithHash = await fs.stat('/config/settings.json', {
|
|
750
|
+
* includeHash: true,
|
|
751
|
+
* hashAlgorithm: 'SHA-1'
|
|
752
|
+
* });
|
|
753
|
+
* console.log(\`Hash: \${statsWithHash.hash}\`);
|
|
754
|
+
* \`\`\`
|
|
755
|
+
*/
|
|
756
|
+
async stat(t, r) {
|
|
757
|
+
await this.ensureMounted();
|
|
758
|
+
const a = P(t), n = await this.getDirectoryHandle(M(t), !1), s = r?.includeHash ?? !1, o = r?.hashAlgorithm ?? "SHA-1";
|
|
759
|
+
try {
|
|
760
|
+
const l = await (await n.getFileHandle(a, { create: !1 })).getFile(), u = {
|
|
761
|
+
kind: "file",
|
|
762
|
+
size: l.size,
|
|
763
|
+
mtime: new Date(l.lastModified).toISOString(),
|
|
764
|
+
ctime: new Date(l.lastModified).toISOString(),
|
|
765
|
+
isFile: !0,
|
|
766
|
+
isDirectory: !1
|
|
767
|
+
};
|
|
768
|
+
if (s)
|
|
769
|
+
try {
|
|
770
|
+
const f = new Uint8Array(await l.arrayBuffer()), w = await fe(f, o);
|
|
771
|
+
u.hash = w;
|
|
772
|
+
} catch (f) {
|
|
773
|
+
console.warn(\`Failed to calculate hash for \${t}:\`, f);
|
|
774
|
+
}
|
|
775
|
+
return u;
|
|
776
|
+
} catch (i) {
|
|
777
|
+
if (i.name !== "TypeMismatchError" && i.name !== "NotFoundError")
|
|
778
|
+
throw new c("Failed to stat (file)", "STAT_FAILED");
|
|
779
|
+
}
|
|
780
|
+
try {
|
|
781
|
+
return await n.getDirectoryHandle(a, { create: !1 }), {
|
|
782
|
+
kind: "directory",
|
|
783
|
+
size: 0,
|
|
784
|
+
mtime: (/* @__PURE__ */ new Date(0)).toISOString(),
|
|
785
|
+
ctime: (/* @__PURE__ */ new Date(0)).toISOString(),
|
|
786
|
+
isFile: !1,
|
|
787
|
+
isDirectory: !0
|
|
788
|
+
};
|
|
789
|
+
} catch (i) {
|
|
790
|
+
throw i.name === "NotFoundError" ? new c(\`No such file or directory: \${t}\`, "ENOENT") : new c("Failed to stat (directory)", "STAT_FAILED");
|
|
791
|
+
}
|
|
792
|
+
}
|
|
793
|
+
async readdir(t, r) {
|
|
794
|
+
await this.ensureMounted();
|
|
795
|
+
const a = r?.withFileTypes ?? !1, n = await this.getDirectoryHandle(t, !1);
|
|
796
|
+
if (a) {
|
|
797
|
+
const s = [];
|
|
798
|
+
for await (const [o, i] of n.entries()) {
|
|
799
|
+
const l = i.kind === "file";
|
|
800
|
+
s.push({
|
|
801
|
+
name: o,
|
|
802
|
+
kind: i.kind,
|
|
803
|
+
isFile: l,
|
|
804
|
+
isDirectory: !l
|
|
805
|
+
});
|
|
806
|
+
}
|
|
807
|
+
return s;
|
|
808
|
+
} else {
|
|
809
|
+
const s = [];
|
|
810
|
+
for await (const [o] of n.entries())
|
|
811
|
+
s.push(o);
|
|
812
|
+
return s;
|
|
813
|
+
}
|
|
814
|
+
}
|
|
815
|
+
/**
|
|
816
|
+
* Check if a file or directory exists
|
|
817
|
+
*
|
|
818
|
+
* Verifies if a file or directory exists at the specified path.
|
|
819
|
+
*
|
|
820
|
+
* @param path - The path to check
|
|
821
|
+
* @returns Promise that resolves to true if the file or directory exists, false otherwise
|
|
822
|
+
*
|
|
823
|
+
* @example
|
|
824
|
+
* \`\`\`typescript
|
|
825
|
+
* const exists = await fs.exists('/config/settings.json');
|
|
826
|
+
* console.log(\`File exists: \${exists}\`);
|
|
827
|
+
* \`\`\`
|
|
828
|
+
*/
|
|
829
|
+
async exists(t) {
|
|
830
|
+
await this.ensureMounted();
|
|
831
|
+
const r = P(t);
|
|
832
|
+
let a = null;
|
|
833
|
+
try {
|
|
834
|
+
a = await this.getDirectoryHandle(M(t), !1);
|
|
835
|
+
} catch (n) {
|
|
836
|
+
throw (n.name === "NotFoundError" || n.name === "TypeMismatchError") && (a = null), n;
|
|
837
|
+
}
|
|
838
|
+
if (!a || !r)
|
|
839
|
+
return !1;
|
|
840
|
+
try {
|
|
841
|
+
return await a.getFileHandle(r, { create: !1 }), !0;
|
|
842
|
+
} catch (n) {
|
|
843
|
+
if (n.name !== "NotFoundError" && n.name !== "TypeMismatchError")
|
|
844
|
+
throw n;
|
|
845
|
+
}
|
|
846
|
+
try {
|
|
847
|
+
return await a.getDirectoryHandle(r, { create: !1 }), !0;
|
|
848
|
+
} catch (n) {
|
|
849
|
+
if (n.name !== "NotFoundError" && n.name !== "TypeMismatchError")
|
|
850
|
+
throw n;
|
|
851
|
+
}
|
|
852
|
+
return !1;
|
|
853
|
+
}
|
|
854
|
+
/**
|
|
855
|
+
* Clear all contents of a directory without removing the directory itself
|
|
856
|
+
*
|
|
857
|
+
* Removes all files and subdirectories within the specified directory,
|
|
858
|
+
* but keeps the directory itself.
|
|
859
|
+
*
|
|
860
|
+
* @param path - The path to the directory to clear (default: '/')
|
|
861
|
+
* @returns Promise that resolves when all contents are removed
|
|
862
|
+
* @throws {OPFSError} If the operation fails
|
|
863
|
+
*
|
|
864
|
+
* @example
|
|
865
|
+
* \`\`\`typescript
|
|
866
|
+
* // Clear root directory contents
|
|
867
|
+
* await fs.clear('/');
|
|
868
|
+
*
|
|
869
|
+
* // Clear specific directory contents
|
|
870
|
+
* await fs.clear('/data');
|
|
871
|
+
* \`\`\`
|
|
872
|
+
*/
|
|
873
|
+
async clear(t = "/") {
|
|
874
|
+
await this.ensureMounted();
|
|
875
|
+
try {
|
|
876
|
+
const r = await this.readdir(t, { withFileTypes: !0 });
|
|
877
|
+
for (const a of r) {
|
|
878
|
+
const n = \`\${t === "/" ? "" : t}/\${a.name}\`;
|
|
879
|
+
await this.remove(n, { recursive: !0 });
|
|
880
|
+
}
|
|
881
|
+
} catch (r) {
|
|
882
|
+
throw r instanceof c ? r : new c(\`Failed to clear directory: \${t}\`, "CLEAR_FAILED");
|
|
883
|
+
}
|
|
884
|
+
}
|
|
885
|
+
/**
|
|
886
|
+
* Remove files and directories
|
|
887
|
+
*
|
|
888
|
+
* Removes files and directories. Similar to Node.js fs.rm().
|
|
889
|
+
*
|
|
890
|
+
* @param path - The path to remove
|
|
891
|
+
* @param options - Options for removal
|
|
892
|
+
* @param options.recursive - Whether to remove directories and their contents recursively (default: false)
|
|
893
|
+
* @param options.force - Whether to ignore errors if the path doesn't exist (default: false)
|
|
894
|
+
* @returns Promise that resolves when the removal is complete
|
|
895
|
+
* @throws {OPFSError} If the removal fails
|
|
896
|
+
*
|
|
897
|
+
* @example
|
|
898
|
+
* \`\`\`typescript
|
|
899
|
+
* // Remove a file
|
|
900
|
+
* await fs.rm('/path/to/file.txt');
|
|
901
|
+
*
|
|
902
|
+
* // Remove a directory and all its contents
|
|
903
|
+
* await fs.rm('/path/to/directory', { recursive: true });
|
|
904
|
+
*
|
|
905
|
+
* // Remove with force (ignore if doesn't exist)
|
|
906
|
+
* await fs.rm('/maybe/exists', { force: true });
|
|
907
|
+
* \`\`\`
|
|
908
|
+
*/
|
|
909
|
+
async remove(t, r) {
|
|
910
|
+
await this.ensureMounted();
|
|
911
|
+
const a = r?.recursive ?? !1, n = r?.force ?? !1, s = P(t);
|
|
912
|
+
if (!s)
|
|
913
|
+
throw new k("Invalid path", t);
|
|
914
|
+
const o = await this.getDirectoryHandle(M(t), !1);
|
|
915
|
+
try {
|
|
916
|
+
await o.removeEntry(s, { recursive: a });
|
|
917
|
+
} catch (i) {
|
|
918
|
+
if (i.name === "NotFoundError") {
|
|
919
|
+
if (!n)
|
|
920
|
+
throw new c(\`No such file or directory: \${t}\`, "ENOENT");
|
|
921
|
+
} else throw i.name === "InvalidModificationError" ? new c(\`Directory not empty: \${t}. Use recursive option to force removal.\`, "ENOTEMPTY") : i.name === "TypeMismatchError" && !a ? new c(\`Cannot remove directory without recursive option: \${t}\`, "EISDIR") : new c(\`Failed to remove path: \${t}\`, "RM_FAILED");
|
|
922
|
+
}
|
|
923
|
+
}
|
|
924
|
+
/**
|
|
925
|
+
* Resolve a path to an absolute path
|
|
926
|
+
*
|
|
927
|
+
* Resolves relative paths and normalizes path segments (like '..' and '.').
|
|
928
|
+
* Similar to Node.js fs.realpath() but without symlink resolution since OPFS doesn't support symlinks.
|
|
929
|
+
*
|
|
930
|
+
* @param path - The path to resolve
|
|
931
|
+
* @returns Promise that resolves to the absolute normalized path
|
|
932
|
+
* @throws {FileNotFoundError} If the path does not exist
|
|
933
|
+
* @throws {OPFSError} If path resolution fails
|
|
934
|
+
*
|
|
935
|
+
* @example
|
|
936
|
+
* \`\`\`typescript
|
|
937
|
+
* // Resolve relative path
|
|
938
|
+
* const absolute = await fs.realpath('./config/../data/file.txt');
|
|
939
|
+
* console.log(absolute); // '/data/file.txt'
|
|
940
|
+
* \`\`\`
|
|
941
|
+
*/
|
|
942
|
+
async realpath(t) {
|
|
943
|
+
await this.ensureMounted();
|
|
944
|
+
try {
|
|
945
|
+
const r = ce(t);
|
|
946
|
+
if (!await this.exists(r))
|
|
947
|
+
throw new b(r);
|
|
948
|
+
return r;
|
|
949
|
+
} catch (r) {
|
|
950
|
+
throw r instanceof c ? r : new c(\`Failed to resolve path: \${t}\`, "REALPATH_FAILED");
|
|
951
|
+
}
|
|
952
|
+
}
|
|
953
|
+
/**
|
|
954
|
+
* Rename a file or directory
|
|
955
|
+
*
|
|
956
|
+
* Changes the name of a file or directory. If the target path already exists,
|
|
957
|
+
* it will be replaced.
|
|
958
|
+
*
|
|
959
|
+
* @param oldPath - The current path of the file or directory
|
|
960
|
+
* @param newPath - The new path for the file or directory
|
|
961
|
+
* @returns Promise that resolves when the rename operation is complete
|
|
962
|
+
* @throws {OPFSError} If the rename operation fails
|
|
963
|
+
*
|
|
964
|
+
* @example
|
|
965
|
+
* \`\`\`typescript
|
|
966
|
+
* await fs.rename('/old/path/file.txt', '/new/path/renamed.txt');
|
|
967
|
+
* \`\`\`
|
|
968
|
+
*/
|
|
969
|
+
async rename(t, r) {
|
|
970
|
+
await this.ensureMounted();
|
|
971
|
+
try {
|
|
972
|
+
if (!await this.exists(t))
|
|
973
|
+
throw new b(t);
|
|
974
|
+
await this.copy(t, r, { recursive: !0 }), await this.remove(t, { recursive: !0 });
|
|
975
|
+
} catch (a) {
|
|
976
|
+
throw a instanceof c ? a : new c(\`Failed to rename from \${t} to \${r}\`, "RENAME_FAILED");
|
|
977
|
+
}
|
|
978
|
+
}
|
|
979
|
+
/**
|
|
980
|
+
* Copy files and directories
|
|
981
|
+
*
|
|
982
|
+
* Copies files and directories. Similar to Node.js fs.cp().
|
|
983
|
+
*
|
|
984
|
+
* @param source - The source path to copy from
|
|
985
|
+
* @param destination - The destination path to copy to
|
|
986
|
+
* @param options - Options for copying
|
|
987
|
+
* @param options.recursive - Whether to copy directories recursively (default: false)
|
|
988
|
+
* @param options.force - Whether to overwrite existing files (default: true)
|
|
989
|
+
* @returns Promise that resolves when the copy operation is complete
|
|
990
|
+
* @throws {OPFSError} If the copy operation fails
|
|
991
|
+
*
|
|
992
|
+
* @example
|
|
993
|
+
* \`\`\`typescript
|
|
994
|
+
* // Copy a file
|
|
995
|
+
* await fs.copy('/source/file.txt', '/dest/file.txt');
|
|
996
|
+
*
|
|
997
|
+
* // Copy a directory and all its contents
|
|
998
|
+
* await fs.copy('/source/dir', '/dest/dir', { recursive: true });
|
|
999
|
+
*
|
|
1000
|
+
* // Copy without overwriting existing files
|
|
1001
|
+
* await fs.copy('/source', '/dest', { recursive: true, force: false });
|
|
1002
|
+
* \`\`\`
|
|
1003
|
+
*/
|
|
1004
|
+
async copy(t, r, a) {
|
|
1005
|
+
await this.ensureMounted();
|
|
1006
|
+
try {
|
|
1007
|
+
const n = a?.recursive ?? !1, s = a?.force ?? !0;
|
|
1008
|
+
if (!await this.exists(t))
|
|
1009
|
+
throw new c(\`Source does not exist: \${t}\`, "ENOENT");
|
|
1010
|
+
if (await this.exists(r) && !s)
|
|
1011
|
+
throw new c(\`Destination already exists: \${r}\`, "EEXIST");
|
|
1012
|
+
if ((await this.stat(t)).isFile) {
|
|
1013
|
+
const u = await this.readFile(t, "binary");
|
|
1014
|
+
await this.writeFile(r, u);
|
|
1015
|
+
} else {
|
|
1016
|
+
if (!n)
|
|
1017
|
+
throw new c(\`Cannot copy directory without recursive option: \${t}\`, "EISDIR");
|
|
1018
|
+
await this.mkdir(r, { recursive: !0 });
|
|
1019
|
+
const u = await this.readdir(t, { withFileTypes: !0 });
|
|
1020
|
+
for (const f of u) {
|
|
1021
|
+
const w = \`\${t}/\${f.name}\`, h = \`\${r}/\${f.name}\`;
|
|
1022
|
+
await this.copy(w, h, { recursive: !0, force: s });
|
|
1023
|
+
}
|
|
1024
|
+
}
|
|
1025
|
+
} catch (n) {
|
|
1026
|
+
throw n instanceof c ? n : new c(\`Failed to copy from \${t} to \${r}\`, "CP_FAILED");
|
|
1027
|
+
}
|
|
1028
|
+
}
|
|
1029
|
+
/**
|
|
1030
|
+
* Start watching a file or directory for changes
|
|
1031
|
+
*/
|
|
1032
|
+
async watch(t) {
|
|
1033
|
+
await this.ensureMounted();
|
|
1034
|
+
const r = I(t), a = await this.buildSnapshot(r);
|
|
1035
|
+
this.watchers.set(r, a), this.watchTimer || (this.watchTimer = setInterval(() => {
|
|
1036
|
+
this.scanWatches();
|
|
1037
|
+
}, this.watchInterval));
|
|
1038
|
+
}
|
|
1039
|
+
/**
|
|
1040
|
+
* Stop watching a previously watched path
|
|
1041
|
+
*/
|
|
1042
|
+
unwatch(t) {
|
|
1043
|
+
const r = I(t);
|
|
1044
|
+
this.watchers.delete(r), this.watchers.size === 0 && this.watchTimer && (clearInterval(this.watchTimer), this.watchTimer = null);
|
|
1045
|
+
}
|
|
1046
|
+
async buildSnapshot(t) {
|
|
1047
|
+
const r = /* @__PURE__ */ new Map(), a = async (n) => {
|
|
1048
|
+
const s = await this.stat(n);
|
|
1049
|
+
if (r.set(n, s), s.isDirectory) {
|
|
1050
|
+
const o = await this.readdir(n, { withFileTypes: !0 });
|
|
1051
|
+
for (const i of o) {
|
|
1052
|
+
const l = \`\${n === "/" ? "" : n}/\${i.name}\`;
|
|
1053
|
+
await a(l);
|
|
1054
|
+
}
|
|
1055
|
+
}
|
|
1056
|
+
};
|
|
1057
|
+
return await a(t), r;
|
|
1058
|
+
}
|
|
1059
|
+
async scanWatches() {
|
|
1060
|
+
if (!this.scanning) {
|
|
1061
|
+
this.scanning = !0;
|
|
1062
|
+
try {
|
|
1063
|
+
await Promise.all(
|
|
1064
|
+
[...this.watchers.entries()].map(async ([t, r]) => {
|
|
1065
|
+
let a;
|
|
1066
|
+
try {
|
|
1067
|
+
a = await this.buildSnapshot(t);
|
|
1068
|
+
} catch {
|
|
1069
|
+
a = /* @__PURE__ */ new Map();
|
|
1070
|
+
}
|
|
1071
|
+
const n = [];
|
|
1072
|
+
for (const [s, o] of a) {
|
|
1073
|
+
const i = r.get(s);
|
|
1074
|
+
i ? (i.mtime !== o.mtime || i.size !== o.size) && n.push({ path: s, type: "change" }) : n.push({ path: s, type: "create" });
|
|
1075
|
+
}
|
|
1076
|
+
for (const s of r.keys())
|
|
1077
|
+
a.has(s) || n.push({ path: s, type: "delete" });
|
|
1078
|
+
if (n.length && this.watchCallback)
|
|
1079
|
+
for (const s of n)
|
|
1080
|
+
this.watchCallback(s);
|
|
1081
|
+
this.watchers.set(t, a);
|
|
1082
|
+
})
|
|
1083
|
+
);
|
|
1084
|
+
} finally {
|
|
1085
|
+
this.scanning = !1;
|
|
1086
|
+
}
|
|
1087
|
+
}
|
|
1088
|
+
}
|
|
1089
|
+
/**
|
|
1090
|
+
* Synchronize the file system with external data
|
|
1091
|
+
*
|
|
1092
|
+
* Syncs the file system with an array of entries containing paths and data.
|
|
1093
|
+
* This is useful for importing data from external sources or syncing with remote data.
|
|
1094
|
+
*
|
|
1095
|
+
* @param entries - Array of [path, data] tuples to sync
|
|
1096
|
+
* @param options - Options for synchronization
|
|
1097
|
+
* @param options.cleanBefore - Whether to clear the file system before syncing (default: false)
|
|
1098
|
+
* @returns Promise that resolves when synchronization is complete
|
|
1099
|
+
* @throws {OPFSError} If the synchronization fails
|
|
1100
|
+
*
|
|
1101
|
+
* @example
|
|
1102
|
+
* \`\`\`typescript
|
|
1103
|
+
* // Sync with external data
|
|
1104
|
+
* const entries: [string, string | Uint8Array | Blob][] = [
|
|
1105
|
+
* ['/config.json', JSON.stringify({ theme: 'dark' })],
|
|
1106
|
+
* ['/data/binary.dat', new Uint8Array([1, 2, 3, 4])],
|
|
1107
|
+
* ['/upload.txt', new Blob(['file content'], { type: 'text/plain' })]
|
|
1108
|
+
* ];
|
|
1109
|
+
*
|
|
1110
|
+
* // Sync without clearing existing files
|
|
1111
|
+
* await fs.sync(entries);
|
|
1112
|
+
*
|
|
1113
|
+
* // Clean file system and then sync
|
|
1114
|
+
* await fs.sync(entries, { cleanBefore: true });
|
|
1115
|
+
* \`\`\`
|
|
1116
|
+
*/
|
|
1117
|
+
async sync(t, r) {
|
|
1118
|
+
await this.ensureMounted();
|
|
1119
|
+
try {
|
|
1120
|
+
(r?.cleanBefore ?? !1) && await this.clear("/");
|
|
1121
|
+
for (const [n, s] of t) {
|
|
1122
|
+
const o = I(n);
|
|
1123
|
+
let i;
|
|
1124
|
+
s instanceof Blob ? i = await he(s) : i = s, await this.writeFile(o, i);
|
|
1125
|
+
}
|
|
1126
|
+
} catch (a) {
|
|
1127
|
+
throw a instanceof c ? a : new c("Failed to sync file system", "SYNC_FAILED");
|
|
1128
|
+
}
|
|
1129
|
+
}
|
|
1130
|
+
}
|
|
1131
|
+
typeof self < "u" && self.constructor.name === "DedicatedWorkerGlobalScope" && O(new we());
|
|
1132
|
+
//# sourceMappingURL=worker-CMvl9yOu.js.map
|
|
1133
|
+
`, r = typeof self < "u" && self.Blob && new Blob(["URL.revokeObjectURL(import.meta.url);", a], { type: "text/javascript;charset=utf-8" });
|
|
1134
|
+
function s(e) {
|
|
1135
|
+
let n;
|
|
1136
|
+
try {
|
|
1137
|
+
if (n = r && (self.URL || self.webkitURL).createObjectURL(r), !n) throw "";
|
|
1138
|
+
const t = new Worker(n, {
|
|
1139
|
+
type: "module",
|
|
1140
|
+
name: e?.name
|
|
1141
|
+
});
|
|
1142
|
+
return t.addEventListener("error", () => {
|
|
1143
|
+
(self.URL || self.webkitURL).revokeObjectURL(n);
|
|
1144
|
+
}), t;
|
|
1145
|
+
} catch {
|
|
1146
|
+
return new Worker(
|
|
1147
|
+
"data:text/javascript;charset=utf-8," + encodeURIComponent(a),
|
|
1148
|
+
{
|
|
1149
|
+
type: "module",
|
|
1150
|
+
name: e?.name
|
|
1151
|
+
}
|
|
1152
|
+
);
|
|
1153
|
+
}
|
|
1154
|
+
}
|
|
1155
|
+
function l(e, n) {
|
|
1156
|
+
const t = i(new s());
|
|
1157
|
+
return e && t.setWatchCallback(o(e), n), t;
|
|
1158
|
+
}
|
|
1159
|
+
export {
|
|
1160
|
+
u as DirectoryNotFoundError,
|
|
1161
|
+
d as FileNotFoundError,
|
|
1162
|
+
p as OPFSError,
|
|
1163
|
+
w as OPFSNotMountedError,
|
|
1164
|
+
y as OPFSNotSupportedError,
|
|
1165
|
+
m as PathError,
|
|
1166
|
+
g as PermissionError,
|
|
1167
|
+
v as StorageError,
|
|
1168
|
+
E as TimeoutError,
|
|
1169
|
+
S as basename,
|
|
1170
|
+
F as calculateFileHash,
|
|
1171
|
+
b as checkOPFSSupport,
|
|
1172
|
+
P as convertBlobToUint8Array,
|
|
1173
|
+
x as createBuffer,
|
|
1174
|
+
l as createWorker,
|
|
1175
|
+
A as decodeBuffer,
|
|
1176
|
+
T as dirname,
|
|
1177
|
+
O as encodeString,
|
|
1178
|
+
I as extname,
|
|
1179
|
+
_ as joinPath,
|
|
1180
|
+
D as normalizePath,
|
|
1181
|
+
k as readFileData,
|
|
1182
|
+
C as resolvePath,
|
|
1183
|
+
U as splitPath,
|
|
1184
|
+
R as writeFileData
|
|
1185
|
+
};
|
|
1186
|
+
//# sourceMappingURL=index.js.map
|