redzip 4.6.3 → 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 CHANGED
@@ -1,3 +1,13 @@
1
+ 2026.12.01, v4.7.1
2
+
3
+ feature:
4
+ - 4e05578 redzip: fat: split to directories
5
+
6
+ 2026.12.01, v4.7.0
7
+
8
+ feature:
9
+ - 3198b4e redzip: add fat images support
10
+
1
11
  2026.12.01, v4.6.3
2
12
 
3
13
  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,36 @@
1
+ import {readFile, writeFile} from 'node:fs/promises';
2
+ import {FatFsDisk, FatFsResult} from 'fatfs-wasm';
3
+ import {tryToCatch} from 'try-to-catch';
4
+ import {assertFatImage} from './detect.js';
5
+
6
+ export const loadDisk = async (outerPath) => {
7
+ const buffer = await readFile(outerPath);
8
+ const data = new Uint8Array(buffer);
9
+ const disk = await FatFsDisk.create(data);
10
+
11
+ disk.mount();
12
+ disk
13
+ .openDir('/')
14
+ .close();
15
+
16
+ return {
17
+ disk,
18
+ data,
19
+ };
20
+ };
21
+
22
+ export const saveDisk = async (outerPath, data) => {
23
+ await writeFile(outerPath, data);
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
+ };
@@ -0,0 +1,13 @@
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
+ };
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.1",
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"