redzip 4.7.0 → 4.7.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/ChangeLog +5 -0
- package/lib/fat/disk-io.js +15 -1
- package/lib/fat/index.js +13 -273
- package/lib/fat/list/index.js +28 -0
- package/lib/fat/read/index.js +86 -0
- package/lib/fat/read-size/index.js +33 -0
- package/lib/fat/read-stat/index.js +57 -0
- package/lib/fat/remove/index.js +55 -0
- package/lib/fat/write/index.js +67 -0
- package/package.json +1 -1
package/ChangeLog
CHANGED
package/lib/fat/disk-io.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import {readFile, writeFile} from 'node:fs/promises';
|
|
2
|
-
import {FatFsDisk} from 'fatfs-wasm';
|
|
2
|
+
import {FatFsDisk, FatFsResult} from 'fatfs-wasm';
|
|
3
|
+
import {tryToCatch} from 'try-to-catch';
|
|
4
|
+
import {assertFatImage} from './detect.js';
|
|
3
5
|
|
|
4
6
|
export const loadDisk = async (outerPath) => {
|
|
5
7
|
const buffer = await readFile(outerPath);
|
|
@@ -20,3 +22,15 @@ export const loadDisk = async (outerPath) => {
|
|
|
20
22
|
export const saveDisk = async (outerPath, data) => {
|
|
21
23
|
await writeFile(outerPath, data);
|
|
22
24
|
};
|
|
25
|
+
|
|
26
|
+
export const load = async (outerPath) => {
|
|
27
|
+
const [error, loaded] = await tryToCatch(loadDisk, outerPath);
|
|
28
|
+
|
|
29
|
+
if (!error)
|
|
30
|
+
return loaded;
|
|
31
|
+
|
|
32
|
+
if (error.result === FatFsResult.NO_FILESYSTEM)
|
|
33
|
+
await assertFatImage(outerPath, new Uint8Array(await readFile(outerPath)));
|
|
34
|
+
|
|
35
|
+
throw error;
|
|
36
|
+
};
|
package/lib/fat/index.js
CHANGED
|
@@ -1,273 +1,13 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
import
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
const
|
|
12
|
-
const
|
|
13
|
-
|
|
14
|
-
const isNoFile = (e) => e && (e.result === FatFsResult.NO_FILE || e.result === FatFsResult.NO_PATH);
|
|
15
|
-
const isNoPath = (e) => e && e.result === FatFsResult.NO_PATH;
|
|
16
|
-
const isExist = (e) => e && e.result === FatFsResult.EXIST;
|
|
17
|
-
const isNotEmpty = (e) => e && e.result === FatFsResult.DENIED;
|
|
18
|
-
|
|
19
|
-
const createEnoent = (innerPath, verb) => {
|
|
20
|
-
const error = Error(`ENOENT: no such file or directory, ${verb} '${innerPath}'`);
|
|
21
|
-
|
|
22
|
-
assign(error, {
|
|
23
|
-
code: 'ENOENT',
|
|
24
|
-
path: innerPath,
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
return error;
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
const load = async (outerPath) => {
|
|
31
|
-
const [error, loaded] = await tryToCatch(loadDisk, outerPath);
|
|
32
|
-
|
|
33
|
-
if (!error)
|
|
34
|
-
return loaded;
|
|
35
|
-
|
|
36
|
-
if (error.result === FatFsResult.NO_FILESYSTEM)
|
|
37
|
-
await assertFatImage(outerPath, new Uint8Array(await readFile(outerPath)));
|
|
38
|
-
|
|
39
|
-
throw error;
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
const ROOT = {
|
|
43
|
-
name: '',
|
|
44
|
-
size: 0,
|
|
45
|
-
date: new Date(0),
|
|
46
|
-
isDirectory: true,
|
|
47
|
-
isReadOnly: false,
|
|
48
|
-
};
|
|
49
|
-
|
|
50
|
-
const statPath = (disk, path) => {
|
|
51
|
-
if (path === '/')
|
|
52
|
-
return ROOT;
|
|
53
|
-
|
|
54
|
-
return disk.stat(path);
|
|
55
|
-
};
|
|
56
|
-
|
|
57
|
-
const toStatType = (info) => info.isDirectory ? 'dir' : 'file';
|
|
58
|
-
const toMode = (info) => info.isDirectory ? 0o755 : info.isReadOnly ? 0o444 : 0o644;
|
|
59
|
-
|
|
60
|
-
export const readStat = async (outerPath, innerPath) => {
|
|
61
|
-
const {disk} = await load(outerPath);
|
|
62
|
-
const [statError, info] = tryCatch(statPath, disk, innerPath);
|
|
63
|
-
|
|
64
|
-
if (statError) {
|
|
65
|
-
if (isNoFile(statError))
|
|
66
|
-
throw createEnoent(innerPath, 'stat');
|
|
67
|
-
|
|
68
|
-
throw statError;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
return {
|
|
72
|
-
type: toStatType(info),
|
|
73
|
-
name: info.name,
|
|
74
|
-
size: info.size,
|
|
75
|
-
date: info.date,
|
|
76
|
-
owner: 0,
|
|
77
|
-
mode: toMode(info),
|
|
78
|
-
};
|
|
79
|
-
};
|
|
80
|
-
|
|
81
|
-
export const list = async (outerPath, innerPath) => {
|
|
82
|
-
const loaded = await load(outerPath);
|
|
83
|
-
|
|
84
|
-
return walkList(loaded.disk, outerPath, innerPath || '/');
|
|
85
|
-
};
|
|
86
|
-
|
|
87
|
-
function walkList(disk, outerPath, path) {
|
|
88
|
-
const paths = [];
|
|
89
|
-
const dir = disk.openDir(path);
|
|
90
|
-
|
|
91
|
-
for (const info of dir) {
|
|
92
|
-
const entryPath = join(path, info.name);
|
|
93
|
-
|
|
94
|
-
paths.push(join(outerPath, entryPath));
|
|
95
|
-
|
|
96
|
-
if (info.isDirectory)
|
|
97
|
-
paths.push(...walkList(disk, outerPath, entryPath));
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
dir.close();
|
|
101
|
-
|
|
102
|
-
return paths;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
export const read = async (outerPath, innerPath, options = {}) => {
|
|
106
|
-
const {sort, order} = options;
|
|
107
|
-
|
|
108
|
-
const {disk} = await load(outerPath);
|
|
109
|
-
const [statError, info] = tryCatch(statPath, disk, innerPath);
|
|
110
|
-
|
|
111
|
-
if (statError) {
|
|
112
|
-
if (isNoFile(statError))
|
|
113
|
-
throw createEnoent(innerPath, 'open');
|
|
114
|
-
|
|
115
|
-
throw statError;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
if (!info.isDirectory) {
|
|
119
|
-
const content = disk.readFile(innerPath);
|
|
120
|
-
|
|
121
|
-
assign(content, {
|
|
122
|
-
type: 'file',
|
|
123
|
-
contentLength: content.length,
|
|
124
|
-
});
|
|
125
|
-
|
|
126
|
-
return content;
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
const dir = disk.openDir(innerPath);
|
|
130
|
-
const names = [];
|
|
131
|
-
|
|
132
|
-
for (const entry of dir) {
|
|
133
|
-
names.push({
|
|
134
|
-
name: entry.name,
|
|
135
|
-
size: entry.size,
|
|
136
|
-
date: entry.date,
|
|
137
|
-
type: toStatType(entry),
|
|
138
|
-
owner: 0,
|
|
139
|
-
});
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
dir.close();
|
|
143
|
-
|
|
144
|
-
return sortNames(names, {
|
|
145
|
-
sort,
|
|
146
|
-
order,
|
|
147
|
-
});
|
|
148
|
-
};
|
|
149
|
-
|
|
150
|
-
export const readSize = async (outerPath, innerPath, options = {}) => {
|
|
151
|
-
const {type} = options;
|
|
152
|
-
const loaded = await load(outerPath);
|
|
153
|
-
|
|
154
|
-
const sum = sumSize(loaded.disk, innerPath || '/');
|
|
155
|
-
|
|
156
|
-
if (type === 'raw')
|
|
157
|
-
return sum;
|
|
158
|
-
|
|
159
|
-
return format.size(sum);
|
|
160
|
-
};
|
|
161
|
-
|
|
162
|
-
function sumSize(disk, path) {
|
|
163
|
-
let sum = 0;
|
|
164
|
-
const dir = disk.openDir(path);
|
|
165
|
-
|
|
166
|
-
for (const info of dir) {
|
|
167
|
-
if (info.isDirectory)
|
|
168
|
-
sum += sumSize(disk, join(path, info.name));
|
|
169
|
-
else
|
|
170
|
-
sum += info.size;
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
dir.close();
|
|
174
|
-
|
|
175
|
-
return sum;
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
export const write = async (outerPath, innerPath, stream) => {
|
|
179
|
-
const {disk, data} = await load(outerPath);
|
|
180
|
-
|
|
181
|
-
if (!stream) {
|
|
182
|
-
mkdirp(disk, innerPath);
|
|
183
|
-
await saveDisk(outerPath, data);
|
|
184
|
-
|
|
185
|
-
return;
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
const chunks = [];
|
|
189
|
-
|
|
190
|
-
for await (const chunk of stream)
|
|
191
|
-
chunks.push(Buffer.from(chunk));
|
|
192
|
-
|
|
193
|
-
const content = Buffer.concat(chunks);
|
|
194
|
-
const writeFile = disk.writeFile.bind(disk);
|
|
195
|
-
const unlink = disk.unlink.bind(disk);
|
|
196
|
-
|
|
197
|
-
tryCatch(unlink, innerPath);
|
|
198
|
-
|
|
199
|
-
const [writeError] = tryCatch(writeFile, innerPath, content);
|
|
200
|
-
|
|
201
|
-
if (writeError) {
|
|
202
|
-
if (!isNoPath(writeError))
|
|
203
|
-
throw writeError;
|
|
204
|
-
|
|
205
|
-
mkdirp(disk, dirname(innerPath));
|
|
206
|
-
disk.writeFile(innerPath, content);
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
await saveDisk(outerPath, data);
|
|
210
|
-
};
|
|
211
|
-
|
|
212
|
-
function mkdirp(disk, path) {
|
|
213
|
-
if (!path || path === '/')
|
|
214
|
-
return;
|
|
215
|
-
|
|
216
|
-
const mkdir = disk.mkdir.bind(disk);
|
|
217
|
-
const [error] = tryCatch(mkdir, path);
|
|
218
|
-
|
|
219
|
-
if (!error)
|
|
220
|
-
return;
|
|
221
|
-
|
|
222
|
-
if (isExist(error))
|
|
223
|
-
return;
|
|
224
|
-
|
|
225
|
-
if (isNoPath(error)) {
|
|
226
|
-
mkdirp(disk, dirname(path));
|
|
227
|
-
disk.mkdir(path);
|
|
228
|
-
|
|
229
|
-
return;
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
throw error;
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
export const remove = async (outerPath, innerPath) => {
|
|
236
|
-
const {disk, data} = await load(outerPath);
|
|
237
|
-
|
|
238
|
-
removeRecursive(disk, innerPath);
|
|
239
|
-
await saveDisk(outerPath, data);
|
|
240
|
-
};
|
|
241
|
-
|
|
242
|
-
function removeRecursive(disk, innerPath) {
|
|
243
|
-
const unlink = disk.unlink.bind(disk);
|
|
244
|
-
const [unlinkError] = tryCatch(unlink, innerPath);
|
|
245
|
-
|
|
246
|
-
if (!unlinkError)
|
|
247
|
-
return;
|
|
248
|
-
|
|
249
|
-
if (isNoFile(unlinkError))
|
|
250
|
-
throw createEnoent(innerPath, 'open');
|
|
251
|
-
|
|
252
|
-
if (!isNotEmpty(unlinkError))
|
|
253
|
-
throw unlinkError;
|
|
254
|
-
|
|
255
|
-
const dir = disk.openDir(innerPath);
|
|
256
|
-
const names = [];
|
|
257
|
-
|
|
258
|
-
for (const info of dir)
|
|
259
|
-
names.push(info.name);
|
|
260
|
-
|
|
261
|
-
dir.close();
|
|
262
|
-
|
|
263
|
-
for (const name of names)
|
|
264
|
-
removeRecursive(disk, join(innerPath, name));
|
|
265
|
-
|
|
266
|
-
disk.unlink(innerPath);
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
function sortNames(names, {sort = 'name', order = 'asc'}) {
|
|
270
|
-
const dir = order === 'desc' ? -1 : 1;
|
|
271
|
-
|
|
272
|
-
return names.toSorted((a, b) => a[sort] > b[sort] ? dir : a[sort] < b[sort] ? -dir : 0);
|
|
273
|
-
}
|
|
1
|
+
import readFat from './read/index.js';
|
|
2
|
+
import readSizeFat from './read-size/index.js';
|
|
3
|
+
import readStatFat from './read-stat/index.js';
|
|
4
|
+
import writeFat from './write/index.js';
|
|
5
|
+
import removeFat from './remove/index.js';
|
|
6
|
+
|
|
7
|
+
export {list} from './list/index.js';
|
|
8
|
+
|
|
9
|
+
export const read = readFat;
|
|
10
|
+
export const readSize = readSizeFat;
|
|
11
|
+
export const readStat = readStatFat;
|
|
12
|
+
export const write = writeFat;
|
|
13
|
+
export const remove = removeFat;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import {posix} from 'node:path';
|
|
2
|
+
import {load} from '../disk-io.js';
|
|
3
|
+
|
|
4
|
+
const {join} = posix;
|
|
5
|
+
|
|
6
|
+
function walkList(disk, outerPath, path) {
|
|
7
|
+
const paths = [];
|
|
8
|
+
const dir = disk.openDir(path);
|
|
9
|
+
|
|
10
|
+
for (const info of dir) {
|
|
11
|
+
const entryPath = join(path, info.name);
|
|
12
|
+
|
|
13
|
+
paths.push(join(outerPath, entryPath));
|
|
14
|
+
|
|
15
|
+
if (info.isDirectory)
|
|
16
|
+
paths.push(...walkList(disk, outerPath, entryPath));
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
dir.close();
|
|
20
|
+
|
|
21
|
+
return paths;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export const list = async (outerPath, innerPath) => {
|
|
25
|
+
const loaded = await load(outerPath);
|
|
26
|
+
|
|
27
|
+
return walkList(loaded.disk, outerPath, innerPath || '/');
|
|
28
|
+
};
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import {tryCatch} from 'try-catch';
|
|
2
|
+
import {FatFsResult} from 'fatfs-wasm';
|
|
3
|
+
import {load} from '../disk-io.js';
|
|
4
|
+
|
|
5
|
+
const {assign} = Object;
|
|
6
|
+
|
|
7
|
+
const isNoFile = (e) => e && (e.result === FatFsResult.NO_FILE || e.result === FatFsResult.NO_PATH);
|
|
8
|
+
|
|
9
|
+
const createEnoent = (innerPath, verb) => {
|
|
10
|
+
const error = Error(`ENOENT: no such file or directory, ${verb} '${innerPath}'`);
|
|
11
|
+
|
|
12
|
+
assign(error, {
|
|
13
|
+
code: 'ENOENT',
|
|
14
|
+
path: innerPath,
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
return error;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
const ROOT = {
|
|
21
|
+
name: '',
|
|
22
|
+
size: 0,
|
|
23
|
+
date: new Date(0),
|
|
24
|
+
isDirectory: true,
|
|
25
|
+
isReadOnly: false,
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
const statPath = (disk, path) => {
|
|
29
|
+
if (path === '/')
|
|
30
|
+
return ROOT;
|
|
31
|
+
|
|
32
|
+
return disk.stat(path);
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
const toStatType = (info) => info.isDirectory ? 'dir' : 'file';
|
|
36
|
+
|
|
37
|
+
const sortNames = (names, {sort = 'name', order = 'asc'}) => {
|
|
38
|
+
const dir = order === 'desc' ? -1 : 1;
|
|
39
|
+
|
|
40
|
+
return names.toSorted((a, b) => a[sort] > b[sort] ? dir : a[sort] < b[sort] ? -dir : 0);
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
export default async (outerPath, innerPath, options = {}) => {
|
|
44
|
+
const {sort, order} = options;
|
|
45
|
+
|
|
46
|
+
const {disk} = await load(outerPath);
|
|
47
|
+
const [statError, info] = tryCatch(statPath, disk, innerPath);
|
|
48
|
+
|
|
49
|
+
if (statError) {
|
|
50
|
+
if (isNoFile(statError))
|
|
51
|
+
throw createEnoent(innerPath, 'open');
|
|
52
|
+
|
|
53
|
+
throw statError;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
if (!info.isDirectory) {
|
|
57
|
+
const content = disk.readFile(innerPath);
|
|
58
|
+
|
|
59
|
+
assign(content, {
|
|
60
|
+
type: 'file',
|
|
61
|
+
contentLength: content.length,
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
return content;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const dir = disk.openDir(innerPath);
|
|
68
|
+
const names = [];
|
|
69
|
+
|
|
70
|
+
for (const entry of dir) {
|
|
71
|
+
names.push({
|
|
72
|
+
name: entry.name,
|
|
73
|
+
size: entry.size,
|
|
74
|
+
date: entry.date,
|
|
75
|
+
type: toStatType(entry),
|
|
76
|
+
owner: 0,
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
dir.close();
|
|
81
|
+
|
|
82
|
+
return sortNames(names, {
|
|
83
|
+
sort,
|
|
84
|
+
order,
|
|
85
|
+
});
|
|
86
|
+
};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import {posix} from 'node:path';
|
|
2
|
+
import format from 'format-io';
|
|
3
|
+
import {load} from '../disk-io.js';
|
|
4
|
+
|
|
5
|
+
const {join} = posix;
|
|
6
|
+
|
|
7
|
+
function sumSize(disk, path) {
|
|
8
|
+
let sum = 0;
|
|
9
|
+
const dir = disk.openDir(path);
|
|
10
|
+
|
|
11
|
+
for (const info of dir) {
|
|
12
|
+
if (info.isDirectory)
|
|
13
|
+
sum += sumSize(disk, join(path, info.name));
|
|
14
|
+
else
|
|
15
|
+
sum += info.size;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
dir.close();
|
|
19
|
+
|
|
20
|
+
return sum;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export default async (outerPath, innerPath, options = {}) => {
|
|
24
|
+
const {type} = options;
|
|
25
|
+
const loaded = await load(outerPath);
|
|
26
|
+
|
|
27
|
+
const sum = sumSize(loaded.disk, innerPath || '/');
|
|
28
|
+
|
|
29
|
+
if (type === 'raw')
|
|
30
|
+
return sum;
|
|
31
|
+
|
|
32
|
+
return format.size(sum);
|
|
33
|
+
};
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import {tryCatch} from 'try-catch';
|
|
2
|
+
import {FatFsResult} from 'fatfs-wasm';
|
|
3
|
+
import {load} from '../disk-io.js';
|
|
4
|
+
|
|
5
|
+
const {assign} = Object;
|
|
6
|
+
|
|
7
|
+
const isNoFile = (e) => e && (e.result === FatFsResult.NO_FILE || e.result === FatFsResult.NO_PATH);
|
|
8
|
+
|
|
9
|
+
const createEnoent = (innerPath, verb) => {
|
|
10
|
+
const error = Error(`ENOENT: no such file or directory, ${verb} '${innerPath}'`);
|
|
11
|
+
|
|
12
|
+
assign(error, {
|
|
13
|
+
code: 'ENOENT',
|
|
14
|
+
path: innerPath,
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
return error;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
const ROOT = {
|
|
21
|
+
name: '',
|
|
22
|
+
size: 0,
|
|
23
|
+
date: new Date(0),
|
|
24
|
+
isDirectory: true,
|
|
25
|
+
isReadOnly: false,
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
const statPath = (disk, path) => {
|
|
29
|
+
if (path === '/')
|
|
30
|
+
return ROOT;
|
|
31
|
+
|
|
32
|
+
return disk.stat(path);
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
const toStatType = (info) => info.isDirectory ? 'dir' : 'file';
|
|
36
|
+
const toMode = (info) => info.isDirectory ? 0o755 : info.isReadOnly ? 0o444 : 0o644;
|
|
37
|
+
|
|
38
|
+
export default async (outerPath, innerPath) => {
|
|
39
|
+
const {disk} = await load(outerPath);
|
|
40
|
+
const [statError, info] = tryCatch(statPath, disk, innerPath);
|
|
41
|
+
|
|
42
|
+
if (statError) {
|
|
43
|
+
if (isNoFile(statError))
|
|
44
|
+
throw createEnoent(innerPath, 'stat');
|
|
45
|
+
|
|
46
|
+
throw statError;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return {
|
|
50
|
+
type: toStatType(info),
|
|
51
|
+
name: info.name,
|
|
52
|
+
size: info.size,
|
|
53
|
+
date: info.date,
|
|
54
|
+
owner: 0,
|
|
55
|
+
mode: toMode(info),
|
|
56
|
+
};
|
|
57
|
+
};
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import {posix} from 'node:path';
|
|
2
|
+
import {tryCatch} from 'try-catch';
|
|
3
|
+
import {FatFsResult} from 'fatfs-wasm';
|
|
4
|
+
import {load, saveDisk} from '../disk-io.js';
|
|
5
|
+
|
|
6
|
+
const {join} = posix;
|
|
7
|
+
const {assign} = Object;
|
|
8
|
+
|
|
9
|
+
const isNoFile = (e) => e && (e.result === FatFsResult.NO_FILE || e.result === FatFsResult.NO_PATH);
|
|
10
|
+
const isNotEmpty = (e) => e && e.result === FatFsResult.DENIED;
|
|
11
|
+
|
|
12
|
+
const createEnoent = (innerPath, verb) => {
|
|
13
|
+
const error = Error(`ENOENT: no such file or directory, ${verb} '${innerPath}'`);
|
|
14
|
+
|
|
15
|
+
assign(error, {
|
|
16
|
+
code: 'ENOENT',
|
|
17
|
+
path: innerPath,
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
return error;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
function removeRecursive(disk, innerPath) {
|
|
24
|
+
const unlink = disk.unlink.bind(disk);
|
|
25
|
+
const [unlinkError] = tryCatch(unlink, innerPath);
|
|
26
|
+
|
|
27
|
+
if (!unlinkError)
|
|
28
|
+
return;
|
|
29
|
+
|
|
30
|
+
if (isNoFile(unlinkError))
|
|
31
|
+
throw createEnoent(innerPath, 'open');
|
|
32
|
+
|
|
33
|
+
if (!isNotEmpty(unlinkError))
|
|
34
|
+
throw unlinkError;
|
|
35
|
+
|
|
36
|
+
const dir = disk.openDir(innerPath);
|
|
37
|
+
const names = [];
|
|
38
|
+
|
|
39
|
+
for (const info of dir)
|
|
40
|
+
names.push(info.name);
|
|
41
|
+
|
|
42
|
+
dir.close();
|
|
43
|
+
|
|
44
|
+
for (const name of names)
|
|
45
|
+
removeRecursive(disk, join(innerPath, name));
|
|
46
|
+
|
|
47
|
+
disk.unlink(innerPath);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export default async (outerPath, innerPath) => {
|
|
51
|
+
const {disk, data} = await load(outerPath);
|
|
52
|
+
|
|
53
|
+
removeRecursive(disk, innerPath);
|
|
54
|
+
await saveDisk(outerPath, data);
|
|
55
|
+
};
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import {posix} from 'node:path';
|
|
2
|
+
import {Buffer} from 'node:buffer';
|
|
3
|
+
import {tryCatch} from 'try-catch';
|
|
4
|
+
import {FatFsResult} from 'fatfs-wasm';
|
|
5
|
+
import {load, saveDisk} from '../disk-io.js';
|
|
6
|
+
|
|
7
|
+
const {dirname} = posix;
|
|
8
|
+
|
|
9
|
+
const isNoPath = (e) => e && e.result === FatFsResult.NO_PATH;
|
|
10
|
+
const isExist = (e) => e && e.result === FatFsResult.EXIST;
|
|
11
|
+
|
|
12
|
+
function mkdirp(disk, path) {
|
|
13
|
+
if (!path || path === '/')
|
|
14
|
+
return;
|
|
15
|
+
|
|
16
|
+
const mkdir = disk.mkdir.bind(disk);
|
|
17
|
+
const [error] = tryCatch(mkdir, path);
|
|
18
|
+
|
|
19
|
+
if (!error)
|
|
20
|
+
return;
|
|
21
|
+
|
|
22
|
+
if (isExist(error))
|
|
23
|
+
return;
|
|
24
|
+
|
|
25
|
+
if (isNoPath(error)) {
|
|
26
|
+
mkdirp(disk, dirname(path));
|
|
27
|
+
disk.mkdir(path);
|
|
28
|
+
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
throw error;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export default async (outerPath, innerPath, stream) => {
|
|
36
|
+
const {disk, data} = await load(outerPath);
|
|
37
|
+
|
|
38
|
+
if (!stream) {
|
|
39
|
+
mkdirp(disk, innerPath);
|
|
40
|
+
await saveDisk(outerPath, data);
|
|
41
|
+
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const chunks = [];
|
|
46
|
+
|
|
47
|
+
for await (const chunk of stream)
|
|
48
|
+
chunks.push(Buffer.from(chunk));
|
|
49
|
+
|
|
50
|
+
const content = Buffer.concat(chunks);
|
|
51
|
+
const writeFile = disk.writeFile.bind(disk);
|
|
52
|
+
const unlink = disk.unlink.bind(disk);
|
|
53
|
+
|
|
54
|
+
tryCatch(unlink, innerPath);
|
|
55
|
+
|
|
56
|
+
const [writeError] = tryCatch(writeFile, innerPath, content);
|
|
57
|
+
|
|
58
|
+
if (writeError) {
|
|
59
|
+
if (!isNoPath(writeError))
|
|
60
|
+
throw writeError;
|
|
61
|
+
|
|
62
|
+
mkdirp(disk, dirname(innerPath));
|
|
63
|
+
disk.writeFile(innerPath, content);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
await saveDisk(outerPath, data);
|
|
67
|
+
};
|