opfs-worker 0.2.0 → 0.2.1
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/dist/assets/worker-ChfzBM-o.js.map +1 -0
- package/dist/helpers-Co-qCBmA.js +224 -0
- package/dist/helpers-Co-qCBmA.js.map +1 -0
- package/dist/helpers-hEpet7x7.cjs +2 -0
- package/dist/helpers-hEpet7x7.cjs.map +1 -0
- package/dist/index.cjs +217 -205
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +4 -19
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -16
- package/dist/index.js.map +1 -1
- package/dist/raw.cjs +1 -1
- package/dist/raw.cjs.map +1 -1
- package/dist/raw.js +187 -347
- 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/encoder.d.ts.map +1 -1
- package/dist/utils/encoder.js +0 -3
- package/dist/utils/helpers.d.ts +108 -0
- package/dist/utils/helpers.d.ts.map +1 -1
- package/dist/utils/helpers.js +151 -1
- package/dist/worker.d.ts.map +1 -1
- package/dist/worker.js +12 -59
- package/package.json +9 -9
- package/dist/assets/worker-D5GbafFH.js.map +0 -1
- package/dist/test/setup.d.ts +0 -2
- package/dist/test/setup.d.ts.map +0 -1
- package/dist/test/setup.js +0 -148
package/dist/raw.js
CHANGED
|
@@ -1,151 +1,6 @@
|
|
|
1
|
-
import { expose as
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
super(e), this.code = t, this.path = r, this.name = "OPFSError";
|
|
5
|
-
}
|
|
6
|
-
}
|
|
7
|
-
class S extends o {
|
|
8
|
-
constructor() {
|
|
9
|
-
super("OPFS is not supported in this browser", "OPFS_NOT_SUPPORTED");
|
|
10
|
-
}
|
|
11
|
-
}
|
|
12
|
-
class y extends o {
|
|
13
|
-
constructor() {
|
|
14
|
-
super("OPFS is not mounted", "OPFS_NOT_MOUNTED");
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
class g extends o {
|
|
18
|
-
constructor(e, t) {
|
|
19
|
-
super(e, "INVALID_PATH", t);
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
class m extends o {
|
|
23
|
-
constructor(e) {
|
|
24
|
-
super(`File not found: ${e}`, "FILE_NOT_FOUND", e);
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
function A(a, e = "utf-8") {
|
|
28
|
-
switch (e) {
|
|
29
|
-
case "utf8":
|
|
30
|
-
case "utf-8":
|
|
31
|
-
return new TextEncoder().encode(a);
|
|
32
|
-
case "utf16le":
|
|
33
|
-
case "ucs2":
|
|
34
|
-
case "ucs-2":
|
|
35
|
-
return I(a);
|
|
36
|
-
case "ascii":
|
|
37
|
-
return $(a);
|
|
38
|
-
case "latin1":
|
|
39
|
-
return v(a);
|
|
40
|
-
case "binary":
|
|
41
|
-
return Uint8Array.from(a, (t) => t.charCodeAt(0));
|
|
42
|
-
case "base64":
|
|
43
|
-
return Uint8Array.from(atob(a), (t) => t.charCodeAt(0));
|
|
44
|
-
case "hex":
|
|
45
|
-
if (!/^[\da-f]+$/i.test(a) || a.length % 2 !== 0)
|
|
46
|
-
throw new o("Invalid hex string", "INVALID_HEX_FORMAT");
|
|
47
|
-
return Uint8Array.from(a.match(/.{1,2}/g).map((t) => parseInt(t, 16)));
|
|
48
|
-
default:
|
|
49
|
-
return console.warn("Encoding not supported, falling back to UTF-8"), new TextEncoder().encode(a);
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
function D(a, e = "utf-8") {
|
|
53
|
-
switch (e) {
|
|
54
|
-
case "utf8":
|
|
55
|
-
case "utf-8":
|
|
56
|
-
return new TextDecoder().decode(a);
|
|
57
|
-
case "utf16le":
|
|
58
|
-
case "ucs2":
|
|
59
|
-
case "ucs-2":
|
|
60
|
-
return T(a);
|
|
61
|
-
case "latin1":
|
|
62
|
-
return String.fromCharCode(...a);
|
|
63
|
-
case "binary":
|
|
64
|
-
return String.fromCharCode(...a);
|
|
65
|
-
case "ascii":
|
|
66
|
-
return String.fromCharCode(...a.map((t) => t & 127));
|
|
67
|
-
case "base64":
|
|
68
|
-
return btoa(String.fromCharCode(...a));
|
|
69
|
-
case "hex":
|
|
70
|
-
return Array.from(a).map((t) => t.toString(16).padStart(2, "0")).join("");
|
|
71
|
-
default:
|
|
72
|
-
return console.warn("Unsupported encoding, falling back to UTF-8"), new TextDecoder().decode(a);
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
function I(a) {
|
|
76
|
-
const e = new Uint8Array(a.length * 2);
|
|
77
|
-
for (let t = 0; t < a.length; t++) {
|
|
78
|
-
const r = a.charCodeAt(t);
|
|
79
|
-
e[t * 2] = r & 255, e[t * 2 + 1] = r >> 8;
|
|
80
|
-
}
|
|
81
|
-
return e;
|
|
82
|
-
}
|
|
83
|
-
function T(a) {
|
|
84
|
-
a.length % 2 !== 0 && (console.warn("Invalid UTF-16LE buffer length, truncating last byte"), a = a.slice(0, a.length - 1));
|
|
85
|
-
const e = new Uint16Array(a.buffer, a.byteOffset, a.byteLength / 2);
|
|
86
|
-
return String.fromCharCode(...e);
|
|
87
|
-
}
|
|
88
|
-
function v(a) {
|
|
89
|
-
const e = new Uint8Array(a.length);
|
|
90
|
-
for (let t = 0; t < a.length; t++)
|
|
91
|
-
e[t] = a.charCodeAt(t) & 255;
|
|
92
|
-
return e;
|
|
93
|
-
}
|
|
94
|
-
function $(a) {
|
|
95
|
-
const e = new Uint8Array(a.length);
|
|
96
|
-
for (let t = 0; t < a.length; t++)
|
|
97
|
-
e[t] = a.charCodeAt(t) & 127;
|
|
98
|
-
return e;
|
|
99
|
-
}
|
|
100
|
-
function N() {
|
|
101
|
-
if (!("storage" in navigator) || !("getDirectory" in navigator.storage))
|
|
102
|
-
throw new S();
|
|
103
|
-
}
|
|
104
|
-
function f(a) {
|
|
105
|
-
return Array.isArray(a) ? a : a.split("/").filter(Boolean);
|
|
106
|
-
}
|
|
107
|
-
function p(a) {
|
|
108
|
-
return typeof a == "string" ? a ?? "/" : `/${a.join("/")}`;
|
|
109
|
-
}
|
|
110
|
-
function H(a, e = "utf-8") {
|
|
111
|
-
return typeof a == "string" ? A(a, e) : a instanceof Uint8Array ? a : new Uint8Array(a);
|
|
112
|
-
}
|
|
113
|
-
async function x(a) {
|
|
114
|
-
const e = await a.createSyncAccessHandle();
|
|
115
|
-
try {
|
|
116
|
-
const t = e.getSize(), r = new Uint8Array(t);
|
|
117
|
-
return e.read(r, { at: 0 }), r;
|
|
118
|
-
} finally {
|
|
119
|
-
e.close();
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
async function F(a, e, t, r = {}) {
|
|
123
|
-
let n = null;
|
|
124
|
-
try {
|
|
125
|
-
n = await a.createSyncAccessHandle();
|
|
126
|
-
const i = H(e, t), s = r.append ? n.getSize() : 0;
|
|
127
|
-
n.write(i, { at: s }), r.truncate && !r.append && n.truncate(i.byteLength), n.flush();
|
|
128
|
-
} catch (i) {
|
|
129
|
-
console.error(i);
|
|
130
|
-
const s = r.append ? "append" : "write";
|
|
131
|
-
throw new o(`Failed to ${s} file`, `${s.toUpperCase()}_FAILED`);
|
|
132
|
-
} finally {
|
|
133
|
-
if (n)
|
|
134
|
-
try {
|
|
135
|
-
n.close();
|
|
136
|
-
} catch {
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
async function O(a, e = "SHA-1") {
|
|
141
|
-
try {
|
|
142
|
-
const t = new Uint8Array(a), r = await crypto.subtle.digest(e, t);
|
|
143
|
-
return Array.from(new Uint8Array(r)).map((i) => i.toString(16).padStart(2, "0")).join("");
|
|
144
|
-
} catch (t) {
|
|
145
|
-
throw console.warn(`Failed to calculate ${e} hash:`, t), t;
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
class U {
|
|
1
|
+
import { expose as p } from "comlink";
|
|
2
|
+
import { d as v, O as o, b as f, s as y, P as E, i as I, o as S, F as d, w as g, j as T, e as u, f as m, k as H, r as $, n as F, l as A } from "./helpers-Co-qCBmA.js";
|
|
3
|
+
class N {
|
|
149
4
|
/** Root directory handle for the file system */
|
|
150
5
|
root = null;
|
|
151
6
|
/** Watch event callback */
|
|
@@ -164,7 +19,7 @@ class U {
|
|
|
164
19
|
* @throws {OPFSError} If OPFS is not supported in the current browser
|
|
165
20
|
*/
|
|
166
21
|
constructor() {
|
|
167
|
-
|
|
22
|
+
v();
|
|
168
23
|
}
|
|
169
24
|
/**
|
|
170
25
|
* Initialize the file system within a given directory
|
|
@@ -182,12 +37,12 @@ class U {
|
|
|
182
37
|
* const success = await fs.init('/my-app');
|
|
183
38
|
* ```
|
|
184
39
|
*/
|
|
185
|
-
async mount(
|
|
40
|
+
async mount(t = "/", e, a) {
|
|
186
41
|
try {
|
|
187
|
-
const
|
|
188
|
-
return this.root = await this.getDirectoryHandle(
|
|
189
|
-
} catch (
|
|
190
|
-
throw console.error(
|
|
42
|
+
const r = await navigator.storage.getDirectory();
|
|
43
|
+
return this.root = await this.getDirectoryHandle(t, !0, r), e && (this.watchCallback = e, a?.watchInterval && (this.watchInterval = a.watchInterval)), !0;
|
|
44
|
+
} catch (r) {
|
|
45
|
+
throw console.error(r), new o("Failed to initialize OPFS", "INIT_FAILED");
|
|
191
46
|
}
|
|
192
47
|
}
|
|
193
48
|
/**
|
|
@@ -208,13 +63,13 @@ class U {
|
|
|
208
63
|
* const docsDir2 = await fs.getDirectoryHandle(['users', 'john', 'documents'], true);
|
|
209
64
|
* ```
|
|
210
65
|
*/
|
|
211
|
-
async getDirectoryHandle(
|
|
212
|
-
if (!
|
|
213
|
-
throw new
|
|
214
|
-
const
|
|
215
|
-
let i =
|
|
216
|
-
for (const
|
|
217
|
-
i = await i.getDirectoryHandle(
|
|
66
|
+
async getDirectoryHandle(t, e = !1, a = this.root) {
|
|
67
|
+
if (!a)
|
|
68
|
+
throw new f();
|
|
69
|
+
const r = Array.isArray(t) ? t : y(t);
|
|
70
|
+
let i = a;
|
|
71
|
+
for (const n of r)
|
|
72
|
+
i = await i.getDirectoryHandle(n, { create: e });
|
|
218
73
|
return i;
|
|
219
74
|
}
|
|
220
75
|
/**
|
|
@@ -236,14 +91,14 @@ class U {
|
|
|
236
91
|
* const fileHandle2 = await fs.getFileHandle(['config', 'settings.json'], true);
|
|
237
92
|
* ```
|
|
238
93
|
*/
|
|
239
|
-
async getFileHandle(
|
|
240
|
-
if (!
|
|
241
|
-
throw new
|
|
242
|
-
const
|
|
243
|
-
if (
|
|
244
|
-
throw new
|
|
245
|
-
const i =
|
|
246
|
-
return (await this.getDirectoryHandle(
|
|
94
|
+
async getFileHandle(t, e = !1, a = this.root) {
|
|
95
|
+
if (!a)
|
|
96
|
+
throw new f();
|
|
97
|
+
const r = y(t);
|
|
98
|
+
if (r.length === 0)
|
|
99
|
+
throw new E("Path must not be empty", Array.isArray(t) ? t.join("/") : t);
|
|
100
|
+
const i = r.pop();
|
|
101
|
+
return (await this.getDirectoryHandle(r, e, a)).getFileHandle(i, { create: e });
|
|
247
102
|
}
|
|
248
103
|
/**
|
|
249
104
|
* Recursively list all files and directories with their stats
|
|
@@ -282,34 +137,34 @@ class U {
|
|
|
282
137
|
* }
|
|
283
138
|
* ```
|
|
284
139
|
*/
|
|
285
|
-
async index(
|
|
286
|
-
const
|
|
287
|
-
const i = await this.readdir(
|
|
288
|
-
for (const
|
|
289
|
-
const
|
|
140
|
+
async index(t) {
|
|
141
|
+
const e = /* @__PURE__ */ new Map(), a = async (r) => {
|
|
142
|
+
const i = await this.readdir(r, { withFileTypes: !0 });
|
|
143
|
+
for (const n of i) {
|
|
144
|
+
const s = `${r === "/" ? "" : r}/${n.name}`;
|
|
290
145
|
try {
|
|
291
|
-
const
|
|
292
|
-
|
|
293
|
-
} catch (
|
|
294
|
-
console.warn(`Skipping broken entry: ${
|
|
146
|
+
const c = await this.stat(s, t);
|
|
147
|
+
e.set(s, c), c.isDirectory && await a(s);
|
|
148
|
+
} catch (c) {
|
|
149
|
+
console.warn(`Skipping broken entry: ${s}`, c);
|
|
295
150
|
}
|
|
296
151
|
}
|
|
297
152
|
};
|
|
298
|
-
return
|
|
153
|
+
return e.set("/", {
|
|
299
154
|
kind: "directory",
|
|
300
155
|
size: 0,
|
|
301
156
|
mtime: (/* @__PURE__ */ new Date(0)).toISOString(),
|
|
302
157
|
ctime: (/* @__PURE__ */ new Date(0)).toISOString(),
|
|
303
158
|
isFile: !1,
|
|
304
159
|
isDirectory: !0
|
|
305
|
-
}), await
|
|
160
|
+
}), await a("/"), e;
|
|
306
161
|
}
|
|
307
|
-
async readFile(
|
|
162
|
+
async readFile(t, e = "utf-8") {
|
|
308
163
|
try {
|
|
309
|
-
const
|
|
310
|
-
return
|
|
311
|
-
} catch (
|
|
312
|
-
throw console.error(
|
|
164
|
+
const a = await this.getFileHandle(t, !1), r = await I(a);
|
|
165
|
+
return e === "binary" ? r : S(r, e);
|
|
166
|
+
} catch (a) {
|
|
167
|
+
throw console.error(a), new d(t);
|
|
313
168
|
}
|
|
314
169
|
}
|
|
315
170
|
/**
|
|
@@ -337,9 +192,9 @@ class U {
|
|
|
337
192
|
* await fs.writeFile('/data/utf16.txt', 'Hello World', 'utf-16le');
|
|
338
193
|
* ```
|
|
339
194
|
*/
|
|
340
|
-
async writeFile(
|
|
341
|
-
const
|
|
342
|
-
await
|
|
195
|
+
async writeFile(t, e, a) {
|
|
196
|
+
const r = await this.getFileHandle(t, !0);
|
|
197
|
+
await g(r, e, a, { truncate: !0 });
|
|
343
198
|
}
|
|
344
199
|
/**
|
|
345
200
|
* Append data to a file
|
|
@@ -363,9 +218,9 @@ class U {
|
|
|
363
218
|
* await fs.appendFile('/data/binary.dat', additionalData);
|
|
364
219
|
* ```
|
|
365
220
|
*/
|
|
366
|
-
async appendFile(
|
|
367
|
-
const
|
|
368
|
-
await
|
|
221
|
+
async appendFile(t, e, a) {
|
|
222
|
+
const r = await this.getFileHandle(t, !0);
|
|
223
|
+
await g(r, e, a, { append: !0 });
|
|
369
224
|
}
|
|
370
225
|
/**
|
|
371
226
|
* Create a directory
|
|
@@ -388,20 +243,20 @@ class U {
|
|
|
388
243
|
* await fs.mkdir('/users/john/documents/projects', { recursive: true });
|
|
389
244
|
* ```
|
|
390
245
|
*/
|
|
391
|
-
async mkdir(
|
|
246
|
+
async mkdir(t, e) {
|
|
392
247
|
if (!this.root)
|
|
393
|
-
throw new
|
|
394
|
-
const
|
|
248
|
+
throw new f();
|
|
249
|
+
const a = e?.recursive ?? !1, r = y(t);
|
|
395
250
|
let i = this.root;
|
|
396
|
-
for (let
|
|
397
|
-
const
|
|
251
|
+
for (let n = 0; n < r.length; n++) {
|
|
252
|
+
const s = r[n];
|
|
398
253
|
try {
|
|
399
|
-
i = await i.getDirectoryHandle(
|
|
400
|
-
} catch (
|
|
401
|
-
throw
|
|
402
|
-
`Parent directory does not exist: ${
|
|
254
|
+
i = await i.getDirectoryHandle(s, { create: a || n === r.length - 1 });
|
|
255
|
+
} catch (c) {
|
|
256
|
+
throw c.name === "NotFoundError" ? new o(
|
|
257
|
+
`Parent directory does not exist: ${T(r.slice(0, n + 1))}`,
|
|
403
258
|
"ENOENT"
|
|
404
|
-
) :
|
|
259
|
+
) : c.name === "TypeMismatchError" ? new o(`Path segment is not a directory: ${s}`, "ENOTDIR") : new o("Failed to create directory", "MKDIR_FAILED");
|
|
405
260
|
}
|
|
406
261
|
}
|
|
407
262
|
}
|
|
@@ -434,61 +289,60 @@ class U {
|
|
|
434
289
|
* console.log(`Hash: ${statsWithHash.hash}`);
|
|
435
290
|
* ```
|
|
436
291
|
*/
|
|
437
|
-
async stat(
|
|
438
|
-
const
|
|
292
|
+
async stat(t, e) {
|
|
293
|
+
const a = u(t), r = await this.getDirectoryHandle(m(t), !1), i = e?.includeHash ?? !1, n = e?.hashAlgorithm ?? "SHA-1";
|
|
439
294
|
try {
|
|
440
|
-
const
|
|
295
|
+
const c = await (await r.getFileHandle(a, { create: !1 })).getFile(), l = {
|
|
441
296
|
kind: "file",
|
|
442
|
-
size:
|
|
443
|
-
mtime: new Date(
|
|
444
|
-
ctime: new Date(
|
|
297
|
+
size: c.size,
|
|
298
|
+
mtime: new Date(c.lastModified).toISOString(),
|
|
299
|
+
ctime: new Date(c.lastModified).toISOString(),
|
|
445
300
|
isFile: !0,
|
|
446
301
|
isDirectory: !1
|
|
447
302
|
};
|
|
448
|
-
if (
|
|
303
|
+
if (i)
|
|
449
304
|
try {
|
|
450
|
-
const
|
|
451
|
-
|
|
452
|
-
} catch (
|
|
453
|
-
console.warn(`Failed to calculate hash for ${
|
|
305
|
+
const h = new Uint8Array(await c.arrayBuffer()), w = await H(h, n);
|
|
306
|
+
l.hash = w;
|
|
307
|
+
} catch (h) {
|
|
308
|
+
console.warn(`Failed to calculate hash for ${t}:`, h);
|
|
454
309
|
}
|
|
455
|
-
return
|
|
456
|
-
} catch (
|
|
457
|
-
if (
|
|
310
|
+
return l;
|
|
311
|
+
} catch (s) {
|
|
312
|
+
if (s.name !== "TypeMismatchError" && s.name !== "NotFoundError")
|
|
458
313
|
throw new o("Failed to stat (file)", "STAT_FAILED");
|
|
459
314
|
}
|
|
460
315
|
try {
|
|
461
|
-
return await
|
|
316
|
+
return await r.getDirectoryHandle(a, { create: !1 }), {
|
|
462
317
|
kind: "directory",
|
|
463
318
|
size: 0,
|
|
464
319
|
mtime: (/* @__PURE__ */ new Date(0)).toISOString(),
|
|
465
320
|
ctime: (/* @__PURE__ */ new Date(0)).toISOString(),
|
|
466
321
|
isFile: !1,
|
|
467
322
|
isDirectory: !0
|
|
468
|
-
// Directories don't have hashes
|
|
469
323
|
};
|
|
470
|
-
} catch (
|
|
471
|
-
throw
|
|
324
|
+
} catch (s) {
|
|
325
|
+
throw s.name === "NotFoundError" ? new o(`No such file or directory: ${t}`, "ENOENT") : new o("Failed to stat (directory)", "STAT_FAILED");
|
|
472
326
|
}
|
|
473
327
|
}
|
|
474
|
-
async readdir(
|
|
475
|
-
const
|
|
476
|
-
if (
|
|
328
|
+
async readdir(t, e) {
|
|
329
|
+
const a = e?.withFileTypes ?? !1, r = await this.getDirectoryHandle(t, !1);
|
|
330
|
+
if (a) {
|
|
477
331
|
const i = [];
|
|
478
|
-
for await (const [
|
|
479
|
-
const
|
|
332
|
+
for await (const [n, s] of r.entries()) {
|
|
333
|
+
const c = s.kind === "file";
|
|
480
334
|
i.push({
|
|
481
|
-
name:
|
|
482
|
-
kind:
|
|
483
|
-
isFile:
|
|
484
|
-
isDirectory: !
|
|
335
|
+
name: n,
|
|
336
|
+
kind: s.kind,
|
|
337
|
+
isFile: c,
|
|
338
|
+
isDirectory: !c
|
|
485
339
|
});
|
|
486
340
|
}
|
|
487
341
|
return i;
|
|
488
342
|
} else {
|
|
489
343
|
const i = [];
|
|
490
|
-
for await (const [
|
|
491
|
-
i.push(
|
|
344
|
+
for await (const [n] of r.entries())
|
|
345
|
+
i.push(n);
|
|
492
346
|
return i;
|
|
493
347
|
}
|
|
494
348
|
}
|
|
@@ -506,27 +360,27 @@ class U {
|
|
|
506
360
|
* console.log(`File exists: ${exists}`);
|
|
507
361
|
* ```
|
|
508
362
|
*/
|
|
509
|
-
async exists(
|
|
510
|
-
const
|
|
511
|
-
let
|
|
363
|
+
async exists(t) {
|
|
364
|
+
const e = u(t);
|
|
365
|
+
let a = null;
|
|
512
366
|
try {
|
|
513
|
-
|
|
514
|
-
} catch (
|
|
515
|
-
throw (
|
|
367
|
+
a = await this.getDirectoryHandle(m(t), !1);
|
|
368
|
+
} catch (r) {
|
|
369
|
+
throw (r.name === "NotFoundError" || r.name === "TypeMismatchError") && (a = null), r;
|
|
516
370
|
}
|
|
517
|
-
if (!
|
|
371
|
+
if (!a || !e)
|
|
518
372
|
return !1;
|
|
519
373
|
try {
|
|
520
|
-
return await
|
|
521
|
-
} catch (
|
|
522
|
-
if (
|
|
523
|
-
throw
|
|
374
|
+
return await a.getFileHandle(e, { create: !1 }), !0;
|
|
375
|
+
} catch (r) {
|
|
376
|
+
if (r.name !== "NotFoundError" && r.name !== "TypeMismatchError")
|
|
377
|
+
throw r;
|
|
524
378
|
}
|
|
525
379
|
try {
|
|
526
|
-
return await
|
|
527
|
-
} catch (
|
|
528
|
-
if (
|
|
529
|
-
throw
|
|
380
|
+
return await a.getDirectoryHandle(e, { create: !1 }), !0;
|
|
381
|
+
} catch (r) {
|
|
382
|
+
if (r.name !== "NotFoundError" && r.name !== "TypeMismatchError")
|
|
383
|
+
throw r;
|
|
530
384
|
}
|
|
531
385
|
return !1;
|
|
532
386
|
}
|
|
@@ -549,15 +403,15 @@ class U {
|
|
|
549
403
|
* await fs.clear('/data');
|
|
550
404
|
* ```
|
|
551
405
|
*/
|
|
552
|
-
async clear(
|
|
406
|
+
async clear(t = "/") {
|
|
553
407
|
try {
|
|
554
|
-
const
|
|
555
|
-
for (const
|
|
556
|
-
const
|
|
557
|
-
await this.remove(
|
|
408
|
+
const e = await this.readdir(t, { withFileTypes: !0 });
|
|
409
|
+
for (const a of e) {
|
|
410
|
+
const r = `${t === "/" ? "" : t}/${a.name}`;
|
|
411
|
+
await this.remove(r, { recursive: !0 });
|
|
558
412
|
}
|
|
559
|
-
} catch (
|
|
560
|
-
throw
|
|
413
|
+
} catch (e) {
|
|
414
|
+
throw e instanceof o ? e : new o(`Failed to clear directory: ${t}`, "CLEAR_FAILED");
|
|
561
415
|
}
|
|
562
416
|
}
|
|
563
417
|
/**
|
|
@@ -584,18 +438,18 @@ class U {
|
|
|
584
438
|
* await fs.rm('/maybe/exists', { force: true });
|
|
585
439
|
* ```
|
|
586
440
|
*/
|
|
587
|
-
async remove(
|
|
588
|
-
const
|
|
589
|
-
if (!
|
|
590
|
-
throw new
|
|
591
|
-
const
|
|
441
|
+
async remove(t, e) {
|
|
442
|
+
const a = e?.recursive ?? !1, r = e?.force ?? !1, i = u(t);
|
|
443
|
+
if (!i)
|
|
444
|
+
throw new E("Invalid path", t);
|
|
445
|
+
const n = await this.getDirectoryHandle(m(t), !1);
|
|
592
446
|
try {
|
|
593
|
-
await
|
|
594
|
-
} catch (
|
|
595
|
-
if (
|
|
596
|
-
if (!
|
|
597
|
-
throw new o(`No such file or directory: ${
|
|
598
|
-
} else throw
|
|
447
|
+
await n.removeEntry(i, { recursive: a });
|
|
448
|
+
} catch (s) {
|
|
449
|
+
if (s.name === "NotFoundError") {
|
|
450
|
+
if (!r)
|
|
451
|
+
throw new o(`No such file or directory: ${t}`, "ENOENT");
|
|
452
|
+
} else throw s.name === "InvalidModificationError" ? new o(`Directory not empty: ${t}. Use recursive option to force removal.`, "ENOTEMPTY") : s.name === "TypeMismatchError" && !a ? new o(`Cannot remove directory without recursive option: ${t}`, "EISDIR") : new o(`Failed to remove path: ${t}`, "RM_FAILED");
|
|
599
453
|
}
|
|
600
454
|
}
|
|
601
455
|
/**
|
|
@@ -616,23 +470,14 @@ class U {
|
|
|
616
470
|
* console.log(absolute); // '/data/file.txt'
|
|
617
471
|
* ```
|
|
618
472
|
*/
|
|
619
|
-
async realpath(
|
|
473
|
+
async realpath(t) {
|
|
620
474
|
try {
|
|
621
|
-
const
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
r.length > 0 && r.pop();
|
|
628
|
-
} else
|
|
629
|
-
r.push(s);
|
|
630
|
-
const n = p(r);
|
|
631
|
-
if (!await this.exists(n))
|
|
632
|
-
throw new m(n);
|
|
633
|
-
return n;
|
|
634
|
-
} catch (t) {
|
|
635
|
-
throw t instanceof o ? t : new o(`Failed to resolve path: ${e}`, "REALPATH_FAILED");
|
|
475
|
+
const e = $(t);
|
|
476
|
+
if (!await this.exists(e))
|
|
477
|
+
throw new d(e);
|
|
478
|
+
return e;
|
|
479
|
+
} catch (e) {
|
|
480
|
+
throw e instanceof o ? e : new o(`Failed to resolve path: ${t}`, "REALPATH_FAILED");
|
|
636
481
|
}
|
|
637
482
|
}
|
|
638
483
|
/**
|
|
@@ -651,13 +496,13 @@ class U {
|
|
|
651
496
|
* await fs.rename('/old/path/file.txt', '/new/path/renamed.txt');
|
|
652
497
|
* ```
|
|
653
498
|
*/
|
|
654
|
-
async rename(
|
|
499
|
+
async rename(t, e) {
|
|
655
500
|
try {
|
|
656
|
-
if (!await this.exists(
|
|
657
|
-
throw new
|
|
658
|
-
await this.copy(
|
|
659
|
-
} catch (
|
|
660
|
-
throw
|
|
501
|
+
if (!await this.exists(t))
|
|
502
|
+
throw new d(t);
|
|
503
|
+
await this.copy(t, e, { recursive: !0 }), await this.remove(t, { recursive: !0 });
|
|
504
|
+
} catch (a) {
|
|
505
|
+
throw a instanceof o ? a : new o(`Failed to rename from ${t} to ${e}`, "RENAME_FAILED");
|
|
661
506
|
}
|
|
662
507
|
}
|
|
663
508
|
/**
|
|
@@ -685,82 +530,82 @@ class U {
|
|
|
685
530
|
* await fs.cp('/source', '/dest', { recursive: true, force: false });
|
|
686
531
|
* ```
|
|
687
532
|
*/
|
|
688
|
-
async copy(
|
|
533
|
+
async copy(t, e, a) {
|
|
689
534
|
try {
|
|
690
|
-
const
|
|
691
|
-
if (!await this.exists(
|
|
692
|
-
throw new o(`Source does not exist: ${
|
|
693
|
-
if (await this.exists(
|
|
694
|
-
throw new o(`Destination already exists: ${
|
|
695
|
-
if ((await this.stat(
|
|
696
|
-
const
|
|
697
|
-
await this.writeFile(
|
|
535
|
+
const r = a?.recursive ?? !1, i = a?.force ?? !0;
|
|
536
|
+
if (!await this.exists(t))
|
|
537
|
+
throw new o(`Source does not exist: ${t}`, "ENOENT");
|
|
538
|
+
if (await this.exists(e) && !i)
|
|
539
|
+
throw new o(`Destination already exists: ${e}`, "EEXIST");
|
|
540
|
+
if ((await this.stat(t)).isFile) {
|
|
541
|
+
const l = await this.readFile(t, "binary");
|
|
542
|
+
await this.writeFile(e, l);
|
|
698
543
|
} else {
|
|
699
|
-
if (!
|
|
700
|
-
throw new o(`Cannot copy directory without recursive option: ${
|
|
701
|
-
await this.mkdir(
|
|
702
|
-
const
|
|
703
|
-
for (const
|
|
704
|
-
const
|
|
705
|
-
await this.copy(
|
|
544
|
+
if (!r)
|
|
545
|
+
throw new o(`Cannot copy directory without recursive option: ${t}`, "EISDIR");
|
|
546
|
+
await this.mkdir(e, { recursive: !0 });
|
|
547
|
+
const l = await this.readdir(t, { withFileTypes: !0 });
|
|
548
|
+
for (const h of l) {
|
|
549
|
+
const w = `${t}/${h.name}`, D = `${e}/${h.name}`;
|
|
550
|
+
await this.copy(w, D, { recursive: !0, force: i });
|
|
706
551
|
}
|
|
707
552
|
}
|
|
708
|
-
} catch (
|
|
709
|
-
throw
|
|
553
|
+
} catch (r) {
|
|
554
|
+
throw r instanceof o ? r : new o(`Failed to copy from ${t} to ${e}`, "CP_FAILED");
|
|
710
555
|
}
|
|
711
556
|
}
|
|
712
557
|
/**
|
|
713
558
|
* Start watching a file or directory for changes
|
|
714
559
|
*/
|
|
715
|
-
async watch(
|
|
716
|
-
const
|
|
717
|
-
this.watchers.set(
|
|
560
|
+
async watch(t) {
|
|
561
|
+
const e = F(t), a = await this.buildSnapshot(e);
|
|
562
|
+
this.watchers.set(e, a), this.watchTimer || (this.watchTimer = setInterval(() => {
|
|
718
563
|
this.scanWatches();
|
|
719
564
|
}, this.watchInterval));
|
|
720
565
|
}
|
|
721
566
|
/**
|
|
722
567
|
* Stop watching a previously watched path
|
|
723
568
|
*/
|
|
724
|
-
unwatch(
|
|
725
|
-
const
|
|
726
|
-
this.watchers.delete(
|
|
727
|
-
}
|
|
728
|
-
async buildSnapshot(
|
|
729
|
-
const
|
|
730
|
-
const i = await this.stat(
|
|
731
|
-
if (
|
|
732
|
-
const
|
|
733
|
-
for (const
|
|
734
|
-
const
|
|
735
|
-
await
|
|
569
|
+
unwatch(t) {
|
|
570
|
+
const e = F(t);
|
|
571
|
+
this.watchers.delete(e), this.watchers.size === 0 && this.watchTimer && (clearInterval(this.watchTimer), this.watchTimer = null);
|
|
572
|
+
}
|
|
573
|
+
async buildSnapshot(t) {
|
|
574
|
+
const e = /* @__PURE__ */ new Map(), a = async (r) => {
|
|
575
|
+
const i = await this.stat(r);
|
|
576
|
+
if (e.set(r, i), i.isDirectory) {
|
|
577
|
+
const n = await this.readdir(r, { withFileTypes: !0 });
|
|
578
|
+
for (const s of n) {
|
|
579
|
+
const c = `${r === "/" ? "" : r}/${s.name}`;
|
|
580
|
+
await a(c);
|
|
736
581
|
}
|
|
737
582
|
}
|
|
738
583
|
};
|
|
739
|
-
return await
|
|
584
|
+
return await a(t), e;
|
|
740
585
|
}
|
|
741
586
|
async scanWatches() {
|
|
742
587
|
if (!this.scanning) {
|
|
743
588
|
this.scanning = !0;
|
|
744
589
|
try {
|
|
745
590
|
await Promise.all(
|
|
746
|
-
[...this.watchers.entries()].map(async ([
|
|
747
|
-
let
|
|
591
|
+
[...this.watchers.entries()].map(async ([t, e]) => {
|
|
592
|
+
let a;
|
|
748
593
|
try {
|
|
749
|
-
|
|
594
|
+
a = await this.buildSnapshot(t);
|
|
750
595
|
} catch {
|
|
751
|
-
|
|
596
|
+
a = /* @__PURE__ */ new Map();
|
|
752
597
|
}
|
|
753
|
-
const
|
|
754
|
-
for (const [i,
|
|
755
|
-
const
|
|
756
|
-
|
|
598
|
+
const r = [];
|
|
599
|
+
for (const [i, n] of a) {
|
|
600
|
+
const s = e.get(i);
|
|
601
|
+
s ? (s.mtime !== n.mtime || s.size !== n.size) && r.push({ path: i, type: "change" }) : r.push({ path: i, type: "create" });
|
|
757
602
|
}
|
|
758
|
-
for (const i of
|
|
759
|
-
|
|
760
|
-
if (
|
|
761
|
-
for (const i of
|
|
603
|
+
for (const i of e.keys())
|
|
604
|
+
a.has(i) || r.push({ path: i, type: "delete" });
|
|
605
|
+
if (r.length && this.watchCallback)
|
|
606
|
+
for (const i of r)
|
|
762
607
|
this.watchCallback(i);
|
|
763
|
-
this.watchers.set(
|
|
608
|
+
this.watchers.set(t, a);
|
|
764
609
|
})
|
|
765
610
|
);
|
|
766
611
|
} finally {
|
|
@@ -796,26 +641,21 @@ class U {
|
|
|
796
641
|
* await fs.sync(entries, { cleanBefore: true });
|
|
797
642
|
* ```
|
|
798
643
|
*/
|
|
799
|
-
async sync(
|
|
644
|
+
async sync(t, e) {
|
|
800
645
|
try {
|
|
801
|
-
(
|
|
802
|
-
for (const [
|
|
803
|
-
const
|
|
804
|
-
let
|
|
805
|
-
|
|
806
|
-
const l = await i.arrayBuffer();
|
|
807
|
-
c = new Uint8Array(l);
|
|
808
|
-
} else
|
|
809
|
-
c = i;
|
|
810
|
-
await this.writeFile(s, c);
|
|
646
|
+
(e?.cleanBefore ?? !1) && await this.clear("/");
|
|
647
|
+
for (const [r, i] of t) {
|
|
648
|
+
const n = F(r);
|
|
649
|
+
let s;
|
|
650
|
+
i instanceof Blob ? s = await A(i) : s = i, await this.writeFile(n, s);
|
|
811
651
|
}
|
|
812
|
-
} catch (
|
|
813
|
-
throw
|
|
652
|
+
} catch (a) {
|
|
653
|
+
throw a instanceof o ? a : new o("Failed to sync file system", "SYNC_FAILED");
|
|
814
654
|
}
|
|
815
655
|
}
|
|
816
656
|
}
|
|
817
|
-
typeof self < "u" && self.constructor.name === "DedicatedWorkerGlobalScope" &&
|
|
657
|
+
typeof self < "u" && self.constructor.name === "DedicatedWorkerGlobalScope" && p(new N());
|
|
818
658
|
export {
|
|
819
|
-
|
|
659
|
+
N as OPFSWorker
|
|
820
660
|
};
|
|
821
661
|
//# sourceMappingURL=raw.js.map
|