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