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