redzip 4.6.3 → 4.7.0

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 CHANGED
@@ -1,3 +1,8 @@
1
+ 2026.12.01, v4.7.0
2
+
3
+ feature:
4
+ - 3198b4e redzip: add fat images support
5
+
1
6
  2026.12.01, v4.6.3
2
7
 
3
8
  feature:
package/README.md CHANGED
@@ -10,7 +10,9 @@
10
10
  [CoverageURL]: https://coveralls.io/github/coderaiser/redzip?branch=master
11
11
  [CoverageIMGURL]: https://coveralls.io/repos/coderaiser/redzip/badge.svg?branch=master&service=github
12
12
 
13
- Work with zip archives as it is regular files and directories.
13
+ Work with zip archives and FAT images as it is regular files and directories.
14
+
15
+ Supported path patterns: `/home/user/hello.zip/readme.md` and `/home/user/disk.fat/file.txt`.
14
16
 
15
17
  ## Install
16
18
 
@@ -226,6 +228,40 @@ await list(dirPath);
226
228
  ];
227
229
  ```
228
230
 
231
+ ## FAT images
232
+
233
+ A path containing a `.fat` file works the same way a path containing a `.zip` file does, for
234
+ every function above: `read`, `readSize`, `readStat`, `write`, `remove`, `list`. Detection is
235
+ by the `.fat` extension in the path, the same way `.zip` is detected today.
236
+
237
+ #### Example
238
+
239
+ ```js
240
+ import {Readable} from 'node:stream';
241
+ import {
242
+ read,
243
+ write,
244
+ remove,
245
+ } from 'redzip';
246
+ import pullout from 'pullout';
247
+
248
+ const path = '/home/coderaiser/disk.fat/hello.txt';
249
+
250
+ await write(path, Readable.from('hello'));
251
+ await pullout(await read(path));
252
+ // returns
253
+ 'hello';
254
+
255
+ await remove(path);
256
+ ```
257
+
258
+ A `.fat` image holds a real FAT12/FAT16/FAT32 filesystem (the kind used on floppy disks, SD
259
+ cards, and USB sticks formatted "FAT"), read and written through
260
+ [`fatfs-wasm`](https://github.com/parkertomatoes/fatfs-wasm). Unlike the zip backend, which
261
+ streams entries one at a time, every `write` or `remove` against a `.fat` path reads the whole
262
+ image into memory, changes it, and writes the whole image back — fine for a floppy-sized
263
+ image, slow for a multi-gigabyte one.
264
+
229
265
  ## Related
230
266
 
231
267
  - [readify](https://github.com/coderaiser/readify "readify") - read directory content with file attributes: size, date, owner, mode
@@ -0,0 +1,15 @@
1
+ import * as zip from './zip/index.js';
2
+ import * as local from './local/index.js';
3
+ import * as fat from './fat/index.js';
4
+
5
+ export const backends = [{
6
+ name: 'zip',
7
+ extension: '.zip',
8
+ module: zip,
9
+ }, {
10
+ name: 'fat',
11
+ extension: '.fat',
12
+ module: fat,
13
+ }];
14
+
15
+ export const localBackend = local;
@@ -0,0 +1,24 @@
1
+ import {tryCatch} from 'try-catch';
2
+ import {FatFsDisk, FatFsResult} from 'fatfs-wasm';
3
+
4
+ export const assertFatImage = async (outerPath, data, overrides = {}) => {
5
+ const {create = FatFsDisk.create} = overrides;
6
+ const disk = await create(data);
7
+
8
+ const probe = () => {
9
+ disk.mount();
10
+ disk
11
+ .openDir('/')
12
+ .close();
13
+ };
14
+
15
+ const [error] = tryCatch(probe);
16
+
17
+ if (!error)
18
+ return;
19
+
20
+ if (error.result === FatFsResult.NO_FILESYSTEM)
21
+ throw Error(`'${outerPath}' does not hold a FAT filesystem`);
22
+
23
+ throw error;
24
+ };
@@ -0,0 +1,22 @@
1
+ import {readFile, writeFile} from 'node:fs/promises';
2
+ import {FatFsDisk} from 'fatfs-wasm';
3
+
4
+ export const loadDisk = async (outerPath) => {
5
+ const buffer = await readFile(outerPath);
6
+ const data = new Uint8Array(buffer);
7
+ const disk = await FatFsDisk.create(data);
8
+
9
+ disk.mount();
10
+ disk
11
+ .openDir('/')
12
+ .close();
13
+
14
+ return {
15
+ disk,
16
+ data,
17
+ };
18
+ };
19
+
20
+ export const saveDisk = async (outerPath, data) => {
21
+ await writeFile(outerPath, data);
22
+ };
@@ -0,0 +1,273 @@
1
+ import {posix} from 'node:path';
2
+ import {readFile} from 'node:fs/promises';
3
+ import {Buffer} from 'node:buffer';
4
+ import {tryCatch} from 'try-catch';
5
+ import {tryToCatch} from 'try-to-catch';
6
+ import {FatFsResult} from 'fatfs-wasm';
7
+ import format from 'format-io';
8
+ import {loadDisk, saveDisk} from './disk-io.js';
9
+ import {assertFatImage} from './detect.js';
10
+
11
+ const {join, dirname} = posix;
12
+ const {assign} = Object;
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
+ }
package/lib/redfs.js CHANGED
@@ -1,35 +1,42 @@
1
1
  import {sep as _sep} from 'node:path';
2
- import * as zip from './zip/index.js';
3
- import * as local from './local/index.js';
2
+ import {backends, localBackend} from './backends.js';
4
3
 
5
4
  export default (path, overrides = {}) => {
6
5
  const {sep = _sep} = overrides;
7
- const [outerPath, innerPath] = split(path, {
8
- sep,
9
- });
10
6
 
11
- if (!innerPath)
12
- return [
13
- local,
14
- path,
15
- '',
16
- ];
7
+ for (const {extension, module} of backends) {
8
+ const [outerPath, innerPath] = split(path, extension, {
9
+ sep,
10
+ });
11
+
12
+ if (innerPath)
13
+ return [
14
+ module,
15
+ outerPath,
16
+ innerPath,
17
+ ];
18
+ }
17
19
 
18
20
  return [
19
- zip,
20
- outerPath,
21
- innerPath,
21
+ localBackend,
22
+ path,
23
+ '',
22
24
  ];
23
25
  };
24
26
 
25
- function split(path, {sep}) {
26
- const replaced = path.replace(`.zip${sep}`, `*${sep}`);
27
+ function split(path, extension, {sep}) {
28
+ const marker = `${extension}${sep}`;
29
+
30
+ if (!path.includes(marker))
31
+ return [path, ''];
32
+
33
+ const replaced = path.replace(marker, `*${sep}`);
27
34
  let [outer, inner = ''] = replaced.split('*');
28
35
 
29
36
  inner = inner.replace(/\\/g, '/');
30
37
 
31
38
  return [
32
- `${outer}.zip`,
39
+ `${outer}${extension}`,
33
40
  inner || '',
34
41
  ];
35
42
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "redzip",
3
- "version": "4.6.3",
3
+ "version": "4.7.0",
4
4
  "type": "module",
5
5
  "main": "lib/redzip.js",
6
6
  "imports": {
@@ -29,6 +29,7 @@
29
29
  "dependencies": {
30
30
  "@cloudcmd/formatify": "^3.0.0",
31
31
  "@cloudcmd/sortify": "^3.1.0",
32
+ "fatfs-wasm": "^4.0.0",
32
33
  "format-io": "^2.0.0",
33
34
  "once": "^1.4.0",
34
35
  "pipe-io": "^4.0.1",
@@ -36,6 +37,7 @@
36
37
  "readzip": "^3.0.0",
37
38
  "totalist": "^3.0.1",
38
39
  "trammel": "^6.0.0",
40
+ "try-catch": "^4.1.1",
39
41
  "try-to-catch": "^4.0.4",
40
42
  "wraptile": "^3.0.0",
41
43
  "yauzl": "^3.1.2",
@@ -61,7 +63,8 @@
61
63
  "readify",
62
64
  "file",
63
65
  "folder",
64
- "zip"
66
+ "zip",
67
+ "fat"
65
68
  ],
66
69
  "engines": {
67
70
  "node": ">=22"