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