redzip 4.7.1 → 4.8.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.8.0
2
+
3
+ feature:
4
+ - ba7e958 redzip: add tar and tar.gz support
5
+
1
6
  2026.12.01, v4.7.1
2
7
 
3
8
  feature:
package/README.md CHANGED
@@ -10,9 +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 and FAT images as it is regular files and directories.
13
+ Work with zip, tar, tar.gz archives and FAT images as it is regular files and directories.
14
14
 
15
- Supported path patterns: `/home/user/hello.zip/readme.md` and `/home/user/disk.fat/file.txt`.
15
+ Supported path patterns: `/home/user/hello.zip/readme.md` and `/home/user/disk.fat/file.txt`, `/home/user/hello.tar/file.txt` and `/home/user/hello.tar.gz/file.txt`.
16
16
 
17
17
  ## Install
18
18
 
@@ -262,6 +262,39 @@ streams entries one at a time, every `write` or `remove` against a `.fat` path r
262
262
  image into memory, changes it, and writes the whole image back — fine for a floppy-sized
263
263
  image, slow for a multi-gigabyte one.
264
264
 
265
+ ## tar and tar.gz archives
266
+
267
+ A path containing a `.tar` or `.tar.gz` file works the same way a path containing a `.zip` file does, for
268
+ every function above: `read`, `readSize`, `readStat`, `write`, `remove`, `list`. Detection is
269
+ by the `.tar` or `.tar.gz` extension in the path.
270
+
271
+ #### Example
272
+
273
+ ```js
274
+ import {Readable} from 'node:stream';
275
+ import {
276
+ read,
277
+ write,
278
+ remove,
279
+ } from 'redzip';
280
+ import pullout from 'pullout';
281
+
282
+ const tarPath = '/home/coderaiser/hello.tar/hello.txt';
283
+ const tarGzPath = '/home/coderaiser/hello.tar.gz/hello.txt';
284
+
285
+ await write(tarPath, Readable.from('hello'));
286
+ await pullout(await read(tarPath));
287
+ // returns
288
+ 'hello';
289
+
290
+ await write(tarGzPath, Readable.from('hello'));
291
+ await remove(tarGzPath);
292
+ ```
293
+
294
+ The archive has to exist already. Tar has no index and no in-place changes, so reading one
295
+ entry streams the archive from the start, and every `write` or `remove` rebuilds the whole
296
+ archive. Read through [`tar-stream`](https://github.com/mafintosh/tar-stream).
297
+
265
298
  ## Related
266
299
 
267
300
  - [readify](https://github.com/coderaiser/readify "readify") - read directory content with file attributes: size, date, owner, mode
package/lib/backends.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import * as zip from './zip/index.js';
2
2
  import * as local from './local/index.js';
3
3
  import * as fat from './fat/index.js';
4
+ import {tar, tarGz} from './tar/index.js';
4
5
 
5
6
  export const backends = [{
6
7
  name: 'zip',
@@ -10,6 +11,14 @@ export const backends = [{
10
11
  name: 'fat',
11
12
  extension: '.fat',
12
13
  module: fat,
14
+ }, {
15
+ name: 'tar',
16
+ extension: '.tar',
17
+ module: tar,
18
+ }, {
19
+ name: 'tar.gz',
20
+ extension: '.tar.gz',
21
+ module: tarGz,
13
22
  }];
14
23
 
15
24
  export const localBackend = local;
@@ -0,0 +1,12 @@
1
+ const isGzipError = (error) => error.code === 'Z_DATA_ERROR';
2
+ const isTarError = (error) => error.message.includes('Unexpected end of data');
3
+
4
+ export const wrapTarError = (outerPath, error, {gzip}) => {
5
+ if (isGzipError(error))
6
+ return Error(`'${outerPath}' is not a valid gzip file`);
7
+
8
+ if (isTarError(error))
9
+ return Error(`'${outerPath}' does not hold a valid${gzip ? ' tar.gz' : ' tar'} archive`);
10
+
11
+ return error;
12
+ };
@@ -0,0 +1,99 @@
1
+ import {createReadStream} from 'node:fs';
2
+ import {createGunzip} from 'node:zlib';
3
+ import {dirname} from 'node:path';
4
+ import {Buffer} from 'node:buffer';
5
+ import tar from 'tar-stream';
6
+ import pipe from 'pipe-io';
7
+ import {tryToCatch} from 'try-to-catch';
8
+ import {wrapTarError} from './detect.js';
9
+
10
+ const toPosixName = (name) => name.replace(/\/$/, '');
11
+ const toNameEntry = (entry) => [entry.name, entry];
12
+ const isNotRoot = (dir) => dir !== '.' && dir !== '/';
13
+
14
+ const toHeaderEntry = (header) => ({
15
+ name: toPosixName(header.name),
16
+ type: header.type === 'directory' ? 'directory' : 'file',
17
+ size: header.size,
18
+ mode: header.mode,
19
+ date: header.mtime,
20
+ });
21
+
22
+ const toChunks = async (stream) => {
23
+ const chunks = [];
24
+
25
+ for await (const chunk of stream)
26
+ chunks.push(chunk);
27
+
28
+ return chunks;
29
+ };
30
+
31
+ export const listEntries = async (outerPath, {gzip} = {}) => {
32
+ const entries = [];
33
+
34
+ await walk(outerPath, {gzip}, (header) => {
35
+ entries.push(toHeaderEntry(header));
36
+ });
37
+
38
+ return withImplicitDirs(entries);
39
+ };
40
+
41
+ export const readEntryContent = async (outerPath, targetName, {gzip} = {}) => {
42
+ let content;
43
+
44
+ await walk(outerPath, {gzip}, (header, chunks) => {
45
+ if (toPosixName(header.name) === targetName)
46
+ content = Buffer.concat(chunks);
47
+ });
48
+
49
+ return content;
50
+ };
51
+
52
+ async function walk(outerPath, {gzip}, onEntry) {
53
+ const extract = tar.extract();
54
+
55
+ extract.on('entry', async (header, stream, next) => {
56
+ const chunks = await toChunks(stream);
57
+
58
+ onEntry(header, chunks);
59
+ next();
60
+ });
61
+
62
+ const streams = gzip
63
+ ? [
64
+ createReadStream(outerPath),
65
+ createGunzip(),
66
+ extract,
67
+ ]
68
+ : [
69
+ createReadStream(outerPath),
70
+ extract,
71
+ ];
72
+
73
+ const [error] = await tryToCatch(pipe, streams);
74
+
75
+ if (error)
76
+ throw wrapTarError(outerPath, error, {
77
+ gzip,
78
+ });
79
+ }
80
+
81
+ function withImplicitDirs(entries) {
82
+ const byName = new Map(entries.map(toNameEntry));
83
+
84
+ for (const entry of entries) {
85
+ let dir = dirname(entry.name);
86
+
87
+ while (isNotRoot(dir) && !byName.has(dir)) {
88
+ byName.set(dir, {
89
+ name: dir,
90
+ type: 'directory',
91
+ size: 0,
92
+ mode: 0o755,
93
+ });
94
+ dir = dirname(dir);
95
+ }
96
+ }
97
+
98
+ return [...byName.values()];
99
+ }
@@ -0,0 +1,32 @@
1
+ import readStat from './read-stat/index.js';
2
+ import read from './read/index.js';
3
+ import readSize from './read-size/index.js';
4
+ import write from './write/index.js';
5
+ import remove from './remove/index.js';
6
+ import {list} from './list/index.js';
7
+
8
+ const createTarBackend = ({gzip} = {}) => ({
9
+ list: (outerPath, innerPath) => list(outerPath, innerPath, {
10
+ gzip,
11
+ }),
12
+ read: (outerPath, innerPath, options) => read(outerPath, innerPath, options, {
13
+ gzip,
14
+ }),
15
+ readSize: (outerPath, innerPath, options) => readSize(outerPath, innerPath, options, {
16
+ gzip,
17
+ }),
18
+ readStat: (outerPath, innerPath) => readStat(outerPath, innerPath, {}, {
19
+ gzip,
20
+ }),
21
+ write: (outerPath, innerPath, stream, options) => write(outerPath, innerPath, stream, options, {
22
+ gzip,
23
+ }),
24
+ remove: (outerPath, innerPath) => remove(outerPath, innerPath, {}, {
25
+ gzip,
26
+ }),
27
+ });
28
+
29
+ export const tar = createTarBackend();
30
+ export const tarGz = createTarBackend({
31
+ gzip: true,
32
+ });
@@ -0,0 +1,21 @@
1
+ import {join} from 'node:path';
2
+ import {listEntries} from '../entries.js';
3
+
4
+ const stripSlashes = (name) => name.replace(/^\/|\/$/g, '');
5
+ const toName = (entry) => entry.name;
6
+ const isInside = (prefix) => (entry) => !prefix || entry.name.startsWith(`${prefix}/`);
7
+ const isSame = (prefix) => (entry) => entry.type === 'file' && entry.name === prefix;
8
+ const toPath = (outerPath) => (name) => join(outerPath, name);
9
+
10
+ export const list = async (outerPath, innerPath, {gzip} = {}) => {
11
+ const entries = await listEntries(outerPath, {
12
+ gzip,
13
+ });
14
+
15
+ const prefix = stripSlashes(innerPath);
16
+ const file = entries.filter(isSame(prefix));
17
+
18
+ return (file.length ? file : entries.filter(isInside(prefix)))
19
+ .map(toName)
20
+ .map(toPath(outerPath));
21
+ };
@@ -0,0 +1,79 @@
1
+ import {dirname} from 'node:path';
2
+ import {sortify} from '@cloudcmd/sortify';
3
+ import {formatify} from '@cloudcmd/formatify';
4
+ import {
5
+ listEntries,
6
+ readEntryContent,
7
+ } from '../entries.js';
8
+
9
+ const {assign} = Object;
10
+
11
+ const stripSlashes = (name) => name.replace(/^\/|\/$/g, '');
12
+ const ifRaw = (type, fn, a) => type === 'raw' ? a : fn(a);
13
+ const toParent = (name) => dirname(name) === '.' ? '' : dirname(name);
14
+
15
+ const toLastSegment = (name) => name
16
+ .split('/')
17
+ .pop();
18
+
19
+ const isChildOf = (dir) => (entry) => toParent(entry.name) === dir;
20
+
21
+ const toListItem = (entry) => ({
22
+ name: toLastSegment(entry.name),
23
+ size: entry.size,
24
+ date: entry.date,
25
+ mode: entry.mode,
26
+ type: entry.type,
27
+ owner: 'root',
28
+ });
29
+
30
+ const matchesFile = (name) => (entry) => entry.name === name && entry.type === 'file';
31
+ const matchesDir = (name) => (entry) => entry.name === name && entry.type === 'directory';
32
+
33
+ const createEnoent = (path) => {
34
+ const error = Error(`ENOENT: no such file or directory, open '${path}'`);
35
+
36
+ return assign(error, {
37
+ code: 'ENOENT',
38
+ path,
39
+ });
40
+ };
41
+
42
+ export default async (outerPath, innerPath, options = {}, {gzip} = {}) => {
43
+ const {
44
+ type,
45
+ order,
46
+ sort,
47
+ } = options;
48
+
49
+ const name = stripSlashes(innerPath);
50
+
51
+ const entries = await listEntries(outerPath, {
52
+ gzip,
53
+ });
54
+
55
+ if (entries.find(matchesFile(name))) {
56
+ const content = await readEntryContent(outerPath, name, {
57
+ gzip,
58
+ });
59
+
60
+ return assign(content, {
61
+ type: 'file',
62
+ contentLength: content.length,
63
+ });
64
+ }
65
+
66
+ if (name && !entries.find(matchesDir(name)))
67
+ throw createEnoent(innerPath);
68
+
69
+ const names = entries
70
+ .filter(isChildOf(name))
71
+ .map(toListItem);
72
+
73
+ const sorted = sortify({
74
+ sort,
75
+ order,
76
+ }, names);
77
+
78
+ return ifRaw(type, formatify, sorted);
79
+ };
@@ -0,0 +1,26 @@
1
+ import format from 'format-io';
2
+ import {listEntries} from '../entries.js';
3
+
4
+ const stripSlashes = (name) => name.replace(/^\/|\/$/g, '');
5
+ const isFile = (entry) => entry.type === 'file';
6
+ const isUnder = (prefix) => (entry) => !prefix || entry.name === prefix || entry.name.startsWith(`${prefix}/`);
7
+ const toSize = (entry) => entry.size;
8
+ const sum = (a, b) => a + b;
9
+
10
+ export default async (outerPath, innerPath, options = {}, {gzip} = {}) => {
11
+ const {type} = options;
12
+ const entries = await listEntries(outerPath, {
13
+ gzip,
14
+ });
15
+
16
+ const total = entries
17
+ .filter(isFile)
18
+ .filter(isUnder(stripSlashes(innerPath)))
19
+ .map(toSize)
20
+ .reduce(sum, 0);
21
+
22
+ if (type === 'raw')
23
+ return total;
24
+
25
+ return format.size(total);
26
+ };
@@ -0,0 +1,49 @@
1
+ import {listEntries} from '../entries.js';
2
+
3
+ const {assign} = Object;
4
+
5
+ const stripSlashes = (name) => name.replace(/^\/|\/$/g, '');
6
+ const toRootEntry = () => ({
7
+ name: '',
8
+ type: 'directory',
9
+ size: 0,
10
+ mode: 0o755,
11
+ });
12
+
13
+ const matchesName = (name) => (entry) => entry.name === name;
14
+
15
+ const toLastSegment = (name) => name
16
+ .split('/')
17
+ .pop() || '/';
18
+
19
+ const toStatType = (entry) => entry.type === 'directory' ? 'dir' : 'file';
20
+
21
+ const createEnoent = (innerPath) => {
22
+ const error = Error(`ENOENT: no such file or directory, stat '${innerPath}'`);
23
+
24
+ return assign(error, {
25
+ code: 'ENOENT',
26
+ path: innerPath,
27
+ });
28
+ };
29
+
30
+ export default async (outerPath, innerPath, options, {gzip} = {}) => {
31
+ const name = stripSlashes(innerPath);
32
+ const entries = await listEntries(outerPath, {
33
+ gzip,
34
+ });
35
+
36
+ const entry = name === '' ? toRootEntry() : entries.find(matchesName(name));
37
+
38
+ if (!entry)
39
+ throw createEnoent(innerPath);
40
+
41
+ return {
42
+ type: toStatType(entry),
43
+ name: toLastSegment(entry.name),
44
+ size: entry.size,
45
+ date: entry.date,
46
+ owner: 0,
47
+ mode: entry.mode,
48
+ };
49
+ };
@@ -0,0 +1,9 @@
1
+ import write from '../write/index.js';
2
+
3
+ export default async (outerPath, innerPath, options, {gzip} = {}) => {
4
+ await write(outerPath, innerPath, null, {
5
+ remove: true,
6
+ }, {
7
+ gzip,
8
+ });
9
+ };
@@ -0,0 +1,123 @@
1
+ import {
2
+ createReadStream,
3
+ createWriteStream,
4
+ } from 'node:fs';
5
+ import {createGzip, createGunzip} from 'node:zlib';
6
+ import {
7
+ rename as _rename,
8
+ unlink,
9
+ } from 'node:fs/promises';
10
+ import {posix} from 'node:path';
11
+ import {Buffer} from 'node:buffer';
12
+ import tar from 'tar-stream';
13
+ import pipe from 'pipe-io';
14
+ import {tryToCatch} from 'try-to-catch';
15
+ import {wrapTarError} from '../detect.js';
16
+
17
+ const {assign} = Object;
18
+ const {join, dirname} = posix;
19
+
20
+ const toTmpName = (gzip) => `.redzip-${Math
21
+ .random()
22
+ .toString(36)
23
+ .slice(2)}.tar${gzip ? '.gz' : ''}`;
24
+
25
+ const stripSlashes = (name) => name.replace(/^\/|\/$/g, '');
26
+
27
+ const isInside = (name) => (header) => {
28
+ const current = stripSlashes(header.name);
29
+
30
+ return current === name || current.startsWith(`${name}/`);
31
+ };
32
+
33
+ const toBuffer = async (stream) => {
34
+ const chunks = [];
35
+
36
+ for await (const chunk of stream)
37
+ chunks.push(Buffer.from(chunk));
38
+
39
+ return Buffer.concat(chunks);
40
+ };
41
+
42
+ const createEnoent = (path) => {
43
+ const error = Error(`ENOENT: no such file or directory, unlink '${path}'`);
44
+
45
+ return assign(error, {
46
+ code: 'ENOENT',
47
+ path,
48
+ });
49
+ };
50
+
51
+ export default async (outerPath, innerPath, stream, options = {}, {gzip} = {}) => {
52
+ const {rename = _rename, remove} = options;
53
+ const name = stripSlashes(innerPath);
54
+ const isTarget = isInside(name);
55
+ const tmpPath = join(dirname(outerPath), toTmpName(gzip));
56
+ const content = stream ? await toBuffer(stream) : null;
57
+
58
+ const extract = tar.extract();
59
+ const pack = tar.pack();
60
+
61
+ let found = false;
62
+
63
+ extract.on('entry', (header, entryStream, next) => {
64
+ if (isTarget(header)) {
65
+ found = true;
66
+ entryStream.resume();
67
+ entryStream.on('end', next);
68
+
69
+ return;
70
+ }
71
+
72
+ entryStream.pipe(pack.entry(header, next));
73
+ });
74
+
75
+ extract.on('finish', () => {
76
+ if (!remove && content)
77
+ pack.entry({
78
+ name,
79
+ }, content);
80
+
81
+ if (!remove && !content)
82
+ pack.entry({
83
+ name,
84
+ type: 'directory',
85
+ });
86
+
87
+ pack.finalize();
88
+ });
89
+
90
+ const readStreams = gzip
91
+ ? [
92
+ createReadStream(outerPath),
93
+ createGunzip(),
94
+ extract,
95
+ ]
96
+ : [
97
+ createReadStream(outerPath),
98
+ extract,
99
+ ];
100
+
101
+ const writeStreams = gzip
102
+ ? [pack, createGzip(), createWriteStream(tmpPath)]
103
+ : [pack, createWriteStream(tmpPath)];
104
+
105
+ const [error] = await tryToCatch(Promise.all.bind(Promise), [
106
+ pipe(readStreams),
107
+ pipe(writeStreams),
108
+ ]);
109
+
110
+ if (error) {
111
+ await unlink(tmpPath);
112
+ throw wrapTarError(outerPath, error, {
113
+ gzip,
114
+ });
115
+ }
116
+
117
+ if (remove && !found) {
118
+ await unlink(tmpPath);
119
+ throw createEnoent(innerPath);
120
+ }
121
+
122
+ await rename(tmpPath, outerPath);
123
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "redzip",
3
- "version": "4.7.1",
3
+ "version": "4.8.0",
4
4
  "type": "module",
5
5
  "main": "lib/redzip.js",
6
6
  "imports": {
@@ -35,6 +35,7 @@
35
35
  "pipe-io": "^4.0.1",
36
36
  "readify": "^11.0.0",
37
37
  "readzip": "^3.0.0",
38
+ "tar-stream": "^3.3.0",
38
39
  "totalist": "^3.0.1",
39
40
  "trammel": "^6.0.0",
40
41
  "try-catch": "^4.1.1",