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