peasy-compress 0.1.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/LICENSE +21 -0
- package/README.md +242 -0
- package/dist/index.d.ts +180 -0
- package/dist/index.js +102 -0
- package/package.json +47 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Peasy Tools
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
# peasy-compress
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/peasy-compress)
|
|
4
|
+
[](https://www.typescriptlang.org/)
|
|
5
|
+
[](https://opensource.org/licenses/MIT)
|
|
6
|
+
|
|
7
|
+
Archive and compression library for Node.js -- ZIP create/extract/list/add, gzip, brotli, and deflate. TypeScript-first with full type safety, built on Node.js native `zlib` and `adm-zip`.
|
|
8
|
+
|
|
9
|
+
Built from [Peasy Compress](https://peasytools.com), the developer tools platform for file processing, text analysis, and web utilities.
|
|
10
|
+
|
|
11
|
+
> **Try the interactive tools at [peasytools.com](https://peasytools.com)** -- [Compress Tool](https://peasytools.com/tools/compress/), [Archive Tool](https://peasytools.com/tools/archive/)
|
|
12
|
+
|
|
13
|
+
## Table of Contents
|
|
14
|
+
|
|
15
|
+
- [Install](#install)
|
|
16
|
+
- [Quick Start](#quick-start)
|
|
17
|
+
- [What You Can Do](#what-you-can-do)
|
|
18
|
+
- [ZIP Archives](#zip-archives)
|
|
19
|
+
- [Gzip Compression](#gzip-compression)
|
|
20
|
+
- [Brotli Compression](#brotli-compression)
|
|
21
|
+
- [Deflate Compression](#deflate-compression)
|
|
22
|
+
- [TypeScript Types](#typescript-types)
|
|
23
|
+
- [API Reference](#api-reference)
|
|
24
|
+
- [Also Available for Python](#also-available-for-python)
|
|
25
|
+
- [Peasy Developer Tools](#peasy-developer-tools)
|
|
26
|
+
- [License](#license)
|
|
27
|
+
|
|
28
|
+
## Install
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
npm install peasy-compress
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Quick Start
|
|
35
|
+
|
|
36
|
+
```typescript
|
|
37
|
+
import {
|
|
38
|
+
zipCreate,
|
|
39
|
+
zipExtract,
|
|
40
|
+
gzipCompress,
|
|
41
|
+
gzipDecompress,
|
|
42
|
+
brotliCompress,
|
|
43
|
+
} from "peasy-compress";
|
|
44
|
+
|
|
45
|
+
// Create a ZIP archive from files
|
|
46
|
+
const zip = zipCreate({
|
|
47
|
+
"readme.txt": Buffer.from("Hello, world!"),
|
|
48
|
+
"data.json": Buffer.from('{"version": 1}'),
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
// Extract all files from a ZIP
|
|
52
|
+
const files = zipExtract(zip);
|
|
53
|
+
console.log(files["readme.txt"]?.toString()); // "Hello, world!"
|
|
54
|
+
|
|
55
|
+
// Gzip compress and decompress
|
|
56
|
+
const compressed = gzipCompress(Buffer.from("Repetitive data ".repeat(100)));
|
|
57
|
+
const original = gzipDecompress(compressed);
|
|
58
|
+
|
|
59
|
+
// Brotli compression (great for web content)
|
|
60
|
+
const brotlied = brotliCompress(Buffer.from("<html>...</html>"), 9);
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## What You Can Do
|
|
64
|
+
|
|
65
|
+
### ZIP Archives
|
|
66
|
+
|
|
67
|
+
ZIP is the most widely used archive format, combining multiple files into a single container with per-file compression. The ZIP format (PKZIP, 1989) supports file metadata, directory structures, and random access to individual entries without decompressing the entire archive.
|
|
68
|
+
|
|
69
|
+
| Operation | Function | Description |
|
|
70
|
+
|-----------|----------|-------------|
|
|
71
|
+
| Create | `zipCreate()` | Build a ZIP from a name-to-Buffer mapping |
|
|
72
|
+
| Extract | `zipExtract()` | Extract all files from a ZIP to a Record |
|
|
73
|
+
| List | `zipList()` | Inspect archive contents, sizes, and counts |
|
|
74
|
+
| Add | `zipAdd()` | Append files to an existing ZIP archive |
|
|
75
|
+
|
|
76
|
+
```typescript
|
|
77
|
+
import { zipCreate, zipExtract, zipList, zipAdd } from "peasy-compress";
|
|
78
|
+
|
|
79
|
+
// Create a ZIP archive with multiple files
|
|
80
|
+
const archive = zipCreate({
|
|
81
|
+
"src/index.ts": Buffer.from('export const version = "1.0";'),
|
|
82
|
+
"package.json": Buffer.from('{"name": "my-lib"}'),
|
|
83
|
+
"LICENSE": Buffer.from("MIT License..."),
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
// Inspect archive contents without extracting
|
|
87
|
+
const info = zipList(archive);
|
|
88
|
+
console.log(info.fileCount); // 3
|
|
89
|
+
console.log(info.totalSize); // total uncompressed bytes
|
|
90
|
+
console.log(info.entries[0]); // { name, size, compressedSize, isDir }
|
|
91
|
+
|
|
92
|
+
// Add more files to an existing archive
|
|
93
|
+
const updated = zipAdd(archive, {
|
|
94
|
+
"README.md": Buffer.from("# My Library"),
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
// Extract everything
|
|
98
|
+
const files = zipExtract(updated);
|
|
99
|
+
Object.keys(files); // ["src/index.ts", "package.json", "LICENSE", "README.md"]
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
Learn more: [Compress Tool](https://peasytools.com/tools/compress/) -- [Archive Guide](https://peasytools.com/glossary/archive/)
|
|
103
|
+
|
|
104
|
+
### Gzip Compression
|
|
105
|
+
|
|
106
|
+
Gzip (RFC 1952) is the standard compression format for HTTP content encoding and Unix file compression. It wraps DEFLATE with a header containing metadata like timestamps and checksums. Virtually all web servers and browsers support gzip natively.
|
|
107
|
+
|
|
108
|
+
```typescript
|
|
109
|
+
import { gzipCompress, gzipDecompress } from "peasy-compress";
|
|
110
|
+
|
|
111
|
+
// Compress text content for storage or transfer
|
|
112
|
+
const html = Buffer.from("<html>".repeat(1000));
|
|
113
|
+
const compressed = gzipCompress(html);
|
|
114
|
+
console.log(compressed.length); // much smaller than original
|
|
115
|
+
|
|
116
|
+
// Verify gzip magic bytes (0x1f 0x8b)
|
|
117
|
+
console.log(compressed[0] === 0x1f); // true
|
|
118
|
+
|
|
119
|
+
// Decompress back to original
|
|
120
|
+
const original = gzipDecompress(compressed);
|
|
121
|
+
console.log(original.toString().startsWith("<html>")); // true
|
|
122
|
+
|
|
123
|
+
// Control compression level (1=fastest, 9=best compression)
|
|
124
|
+
const fast = gzipCompress(html, 1);
|
|
125
|
+
const best = gzipCompress(html, 9);
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
Learn more: [Gzip Reference](https://peasytools.com/glossary/gzip/) -- [Compression Guide](https://peasytools.com/glossary/compression/)
|
|
129
|
+
|
|
130
|
+
### Brotli Compression
|
|
131
|
+
|
|
132
|
+
Brotli (RFC 7932) is a modern compression algorithm developed by Google, achieving 15-25% better compression ratios than gzip for web content. All modern browsers support Brotli via the `Content-Encoding: br` header. It uses a pre-defined dictionary of common web patterns for superior text compression.
|
|
133
|
+
|
|
134
|
+
```typescript
|
|
135
|
+
import { brotliCompress, brotliDecompress } from "peasy-compress";
|
|
136
|
+
|
|
137
|
+
// Compress web content with Brotli for best ratios
|
|
138
|
+
const css = Buffer.from("body { margin: 0; padding: 0; } ".repeat(500));
|
|
139
|
+
const compressed = brotliCompress(css, 9);
|
|
140
|
+
|
|
141
|
+
// Brotli typically outperforms gzip on text content
|
|
142
|
+
console.log(`${css.length} -> ${compressed.length} bytes`);
|
|
143
|
+
|
|
144
|
+
// Decompress
|
|
145
|
+
const original = brotliDecompress(compressed);
|
|
146
|
+
console.log(original.equals(css)); // true
|
|
147
|
+
|
|
148
|
+
// Fast compression for real-time use cases
|
|
149
|
+
const quick = brotliCompress(css, 1);
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
Learn more: [Brotli Reference](https://peasytools.com/glossary/brotli/) -- [Web Compression Guide](https://peasytools.com/glossary/web-compression/)
|
|
153
|
+
|
|
154
|
+
### Deflate Compression
|
|
155
|
+
|
|
156
|
+
DEFLATE (RFC 1951) is the foundational compression algorithm used inside both gzip and ZIP. The raw deflate functions are useful when you need the core LZ77+Huffman compression without any framing format -- for example, inside custom binary protocols or when implementing your own container format.
|
|
157
|
+
|
|
158
|
+
```typescript
|
|
159
|
+
import { deflateCompress, deflateDecompress } from "peasy-compress";
|
|
160
|
+
|
|
161
|
+
// Raw DEFLATE compression (no gzip/zlib headers)
|
|
162
|
+
const data = Buffer.from("DEFLATE is the foundation of gzip and ZIP");
|
|
163
|
+
const compressed = deflateCompress(data);
|
|
164
|
+
const original = deflateDecompress(compressed);
|
|
165
|
+
console.log(original.toString()); // "DEFLATE is the foundation of gzip and ZIP"
|
|
166
|
+
|
|
167
|
+
// Compression levels work the same way
|
|
168
|
+
const fast = deflateCompress(data, 1);
|
|
169
|
+
const best = deflateCompress(data, 9);
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
Learn more: [Deflate Reference](https://peasytools.com/glossary/deflate/) -- [Compression Algorithms](https://peasytools.com/glossary/compression-algorithms/)
|
|
173
|
+
|
|
174
|
+
## TypeScript Types
|
|
175
|
+
|
|
176
|
+
```typescript
|
|
177
|
+
import type {
|
|
178
|
+
ArchiveEntry,
|
|
179
|
+
ArchiveInfo,
|
|
180
|
+
CompressionLevel,
|
|
181
|
+
} from "peasy-compress";
|
|
182
|
+
|
|
183
|
+
// ArchiveEntry — metadata for a single file in an archive
|
|
184
|
+
const entry: ArchiveEntry = {
|
|
185
|
+
name: "hello.txt",
|
|
186
|
+
size: 13,
|
|
187
|
+
compressedSize: 11,
|
|
188
|
+
isDir: false,
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
// ArchiveInfo — summary of an archive's contents
|
|
192
|
+
const info: ArchiveInfo = {
|
|
193
|
+
format: "zip",
|
|
194
|
+
entries: [entry],
|
|
195
|
+
totalSize: 13,
|
|
196
|
+
totalCompressed: 11,
|
|
197
|
+
fileCount: 1,
|
|
198
|
+
dirCount: 0,
|
|
199
|
+
};
|
|
200
|
+
|
|
201
|
+
// CompressionLevel — 1 (fastest) to 9 (best compression)
|
|
202
|
+
const level: CompressionLevel = 6;
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
## API Reference
|
|
206
|
+
|
|
207
|
+
| Function | Description |
|
|
208
|
+
|----------|-------------|
|
|
209
|
+
| `zipCreate(files)` | Create ZIP archive from `Record<string, Buffer>` |
|
|
210
|
+
| `zipExtract(source)` | Extract all files from ZIP to `Record<string, Buffer>` |
|
|
211
|
+
| `zipList(source)` | List ZIP contents with metadata (`ArchiveInfo`) |
|
|
212
|
+
| `zipAdd(source, files)` | Add files to existing ZIP archive |
|
|
213
|
+
| `gzipCompress(data, level?)` | Gzip compress a Buffer |
|
|
214
|
+
| `gzipDecompress(data)` | Gzip decompress a Buffer |
|
|
215
|
+
| `deflateCompress(data, level?)` | DEFLATE compress a Buffer |
|
|
216
|
+
| `deflateDecompress(data)` | DEFLATE decompress (inflate) a Buffer |
|
|
217
|
+
| `brotliCompress(data, level?)` | Brotli compress a Buffer |
|
|
218
|
+
| `brotliDecompress(data)` | Brotli decompress a Buffer |
|
|
219
|
+
|
|
220
|
+
## Also Available for Python
|
|
221
|
+
|
|
222
|
+
```bash
|
|
223
|
+
pip install peasy-compress
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
The Python package provides ZIP, tar, and gzip operations with CLI, MCP server, and REST API client. See [peasy-compress on PyPI](https://pypi.org/project/peasy-compress/).
|
|
227
|
+
|
|
228
|
+
## Peasy Developer Tools
|
|
229
|
+
|
|
230
|
+
| Package | PyPI | npm | Description |
|
|
231
|
+
|---------|------|-----|-------------|
|
|
232
|
+
| peasytext | [PyPI](https://pypi.org/project/peasytext/) | [npm](https://www.npmjs.com/package/peasytext) | Text analysis -- readability, sentiment, keywords |
|
|
233
|
+
| peasy-pdf | [PyPI](https://pypi.org/project/peasy-pdf/) | -- | PDF processing -- extract, merge, split, metadata |
|
|
234
|
+
| peasy-image | [PyPI](https://pypi.org/project/peasy-image/) | -- | Image ops -- resize, crop, filter, watermark |
|
|
235
|
+
| peasy-css | [PyPI](https://pypi.org/project/peasy-css/) | [npm](https://www.npmjs.com/package/peasy-css) | CSS generation -- gradients, shadows, flexbox, grid |
|
|
236
|
+
| **peasy-compress** | [PyPI](https://pypi.org/project/peasy-compress/) | **[npm](https://www.npmjs.com/package/peasy-compress)** | **Archive & compression -- ZIP, gzip, brotli, deflate** |
|
|
237
|
+
|
|
238
|
+
Part of the [Peasy](https://peasytools.com) developer tools ecosystem.
|
|
239
|
+
|
|
240
|
+
## License
|
|
241
|
+
|
|
242
|
+
MIT
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* peasy-compress — Archive & compression types.
|
|
3
|
+
*
|
|
4
|
+
* @packageDocumentation
|
|
5
|
+
*/
|
|
6
|
+
/** Metadata for a single entry inside an archive. */
|
|
7
|
+
interface ArchiveEntry {
|
|
8
|
+
/** File or directory name (relative path within the archive). */
|
|
9
|
+
name: string;
|
|
10
|
+
/** Uncompressed size in bytes. */
|
|
11
|
+
size: number;
|
|
12
|
+
/** Compressed size in bytes. */
|
|
13
|
+
compressedSize: number;
|
|
14
|
+
/** Whether this entry is a directory. */
|
|
15
|
+
isDir: boolean;
|
|
16
|
+
}
|
|
17
|
+
/** Summary information about an archive. */
|
|
18
|
+
interface ArchiveInfo {
|
|
19
|
+
/** Archive format identifier (e.g. "zip"). */
|
|
20
|
+
format: string;
|
|
21
|
+
/** List of all entries in the archive. */
|
|
22
|
+
entries: ArchiveEntry[];
|
|
23
|
+
/** Total uncompressed size in bytes. */
|
|
24
|
+
totalSize: number;
|
|
25
|
+
/** Total compressed size in bytes. */
|
|
26
|
+
totalCompressed: number;
|
|
27
|
+
/** Number of file entries. */
|
|
28
|
+
fileCount: number;
|
|
29
|
+
/** Number of directory entries. */
|
|
30
|
+
dirCount: number;
|
|
31
|
+
}
|
|
32
|
+
/** Compression level from 1 (fastest) to 9 (best compression). */
|
|
33
|
+
type CompressionLevel = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* peasy-compress — Archive & compression engine.
|
|
37
|
+
*
|
|
38
|
+
* ZIP operations use adm-zip. Gzip, deflate, and brotli use Node.js
|
|
39
|
+
* built-in zlib module for zero-config compression.
|
|
40
|
+
*
|
|
41
|
+
* @packageDocumentation
|
|
42
|
+
*/
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Create a ZIP archive from a name-to-content mapping.
|
|
46
|
+
*
|
|
47
|
+
* @param files - Record where keys are filenames and values are file contents
|
|
48
|
+
* @returns Buffer containing the ZIP archive
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* ```typescript
|
|
52
|
+
* const zip = zipCreate({
|
|
53
|
+
* "hello.txt": Buffer.from("Hello, world!"),
|
|
54
|
+
* "data.json": Buffer.from('{"key": "value"}'),
|
|
55
|
+
* });
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
58
|
+
declare function zipCreate(files: Record<string, Buffer>): Buffer;
|
|
59
|
+
/**
|
|
60
|
+
* Extract all files from a ZIP archive.
|
|
61
|
+
*
|
|
62
|
+
* @param source - Buffer containing a ZIP archive
|
|
63
|
+
* @returns Record where keys are filenames and values are file contents
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
* ```typescript
|
|
67
|
+
* const files = zipExtract(zipBuffer);
|
|
68
|
+
* console.log(files["hello.txt"].toString()); // "Hello, world!"
|
|
69
|
+
* ```
|
|
70
|
+
*/
|
|
71
|
+
declare function zipExtract(source: Buffer): Record<string, Buffer>;
|
|
72
|
+
/**
|
|
73
|
+
* List the contents of a ZIP archive with metadata.
|
|
74
|
+
*
|
|
75
|
+
* @param source - Buffer containing a ZIP archive
|
|
76
|
+
* @returns Archive information including entries, sizes, and counts
|
|
77
|
+
*
|
|
78
|
+
* @example
|
|
79
|
+
* ```typescript
|
|
80
|
+
* const info = zipList(zipBuffer);
|
|
81
|
+
* console.log(info.fileCount); // 2
|
|
82
|
+
* console.log(info.totalSize); // 42
|
|
83
|
+
* ```
|
|
84
|
+
*/
|
|
85
|
+
declare function zipList(source: Buffer): ArchiveInfo;
|
|
86
|
+
/**
|
|
87
|
+
* Add files to an existing ZIP archive.
|
|
88
|
+
*
|
|
89
|
+
* @param source - Buffer containing an existing ZIP archive
|
|
90
|
+
* @param files - Record of files to add (name to content mapping)
|
|
91
|
+
* @returns New Buffer containing the updated ZIP archive
|
|
92
|
+
*
|
|
93
|
+
* @example
|
|
94
|
+
* ```typescript
|
|
95
|
+
* const updated = zipAdd(existingZip, {
|
|
96
|
+
* "new-file.txt": Buffer.from("New content"),
|
|
97
|
+
* });
|
|
98
|
+
* ```
|
|
99
|
+
*/
|
|
100
|
+
declare function zipAdd(source: Buffer, files: Record<string, Buffer>): Buffer;
|
|
101
|
+
/**
|
|
102
|
+
* Compress data using gzip.
|
|
103
|
+
*
|
|
104
|
+
* @param data - Buffer to compress
|
|
105
|
+
* @param level - Compression level 1-9 (default: 6)
|
|
106
|
+
* @returns Gzip-compressed Buffer
|
|
107
|
+
*
|
|
108
|
+
* @example
|
|
109
|
+
* ```typescript
|
|
110
|
+
* const compressed = gzipCompress(Buffer.from("Hello, world!"));
|
|
111
|
+
* ```
|
|
112
|
+
*/
|
|
113
|
+
declare function gzipCompress(data: Buffer, level?: CompressionLevel): Buffer;
|
|
114
|
+
/**
|
|
115
|
+
* Decompress gzip data.
|
|
116
|
+
*
|
|
117
|
+
* @param data - Gzip-compressed Buffer
|
|
118
|
+
* @returns Decompressed Buffer
|
|
119
|
+
*
|
|
120
|
+
* @example
|
|
121
|
+
* ```typescript
|
|
122
|
+
* const original = gzipDecompress(compressed);
|
|
123
|
+
* console.log(original.toString()); // "Hello, world!"
|
|
124
|
+
* ```
|
|
125
|
+
*/
|
|
126
|
+
declare function gzipDecompress(data: Buffer): Buffer;
|
|
127
|
+
/**
|
|
128
|
+
* Compress data using deflate (raw DEFLATE without gzip/zlib headers).
|
|
129
|
+
*
|
|
130
|
+
* @param data - Buffer to compress
|
|
131
|
+
* @param level - Compression level 1-9 (default: 6)
|
|
132
|
+
* @returns Deflated Buffer
|
|
133
|
+
*
|
|
134
|
+
* @example
|
|
135
|
+
* ```typescript
|
|
136
|
+
* const compressed = deflateCompress(Buffer.from("Hello, world!"));
|
|
137
|
+
* ```
|
|
138
|
+
*/
|
|
139
|
+
declare function deflateCompress(data: Buffer, level?: CompressionLevel): Buffer;
|
|
140
|
+
/**
|
|
141
|
+
* Decompress deflate data.
|
|
142
|
+
*
|
|
143
|
+
* @param data - Deflated Buffer
|
|
144
|
+
* @returns Decompressed Buffer
|
|
145
|
+
*
|
|
146
|
+
* @example
|
|
147
|
+
* ```typescript
|
|
148
|
+
* const original = deflateDecompress(compressed);
|
|
149
|
+
* console.log(original.toString()); // "Hello, world!"
|
|
150
|
+
* ```
|
|
151
|
+
*/
|
|
152
|
+
declare function deflateDecompress(data: Buffer): Buffer;
|
|
153
|
+
/**
|
|
154
|
+
* Compress data using Brotli.
|
|
155
|
+
*
|
|
156
|
+
* @param data - Buffer to compress
|
|
157
|
+
* @param level - Compression level 1-9 (maps to Brotli quality 1-11, default: 6)
|
|
158
|
+
* @returns Brotli-compressed Buffer
|
|
159
|
+
*
|
|
160
|
+
* @example
|
|
161
|
+
* ```typescript
|
|
162
|
+
* const compressed = brotliCompress(Buffer.from("Hello, world!"));
|
|
163
|
+
* ```
|
|
164
|
+
*/
|
|
165
|
+
declare function brotliCompress(data: Buffer, level?: CompressionLevel): Buffer;
|
|
166
|
+
/**
|
|
167
|
+
* Decompress Brotli data.
|
|
168
|
+
*
|
|
169
|
+
* @param data - Brotli-compressed Buffer
|
|
170
|
+
* @returns Decompressed Buffer
|
|
171
|
+
*
|
|
172
|
+
* @example
|
|
173
|
+
* ```typescript
|
|
174
|
+
* const original = brotliDecompress(compressed);
|
|
175
|
+
* console.log(original.toString()); // "Hello, world!"
|
|
176
|
+
* ```
|
|
177
|
+
*/
|
|
178
|
+
declare function brotliDecompress(data: Buffer): Buffer;
|
|
179
|
+
|
|
180
|
+
export { type ArchiveEntry, type ArchiveInfo, type CompressionLevel, brotliCompress, brotliDecompress, deflateCompress, deflateDecompress, gzipCompress, gzipDecompress, zipAdd, zipCreate, zipExtract, zipList };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
// src/engine.ts
|
|
2
|
+
import {
|
|
3
|
+
gzipSync,
|
|
4
|
+
gunzipSync,
|
|
5
|
+
deflateSync,
|
|
6
|
+
inflateSync,
|
|
7
|
+
brotliCompressSync,
|
|
8
|
+
brotliDecompressSync,
|
|
9
|
+
constants
|
|
10
|
+
} from "zlib";
|
|
11
|
+
import AdmZip from "adm-zip";
|
|
12
|
+
function zipCreate(files) {
|
|
13
|
+
const zip = new AdmZip();
|
|
14
|
+
for (const [name, content] of Object.entries(files)) {
|
|
15
|
+
zip.addFile(name, content);
|
|
16
|
+
}
|
|
17
|
+
return zip.toBuffer();
|
|
18
|
+
}
|
|
19
|
+
function zipExtract(source) {
|
|
20
|
+
const zip = new AdmZip(source);
|
|
21
|
+
const result = {};
|
|
22
|
+
for (const entry of zip.getEntries()) {
|
|
23
|
+
if (!entry.isDirectory) {
|
|
24
|
+
result[entry.entryName] = entry.getData();
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return result;
|
|
28
|
+
}
|
|
29
|
+
function zipList(source) {
|
|
30
|
+
const zip = new AdmZip(source);
|
|
31
|
+
const entries = [];
|
|
32
|
+
let totalSize = 0;
|
|
33
|
+
let totalCompressed = 0;
|
|
34
|
+
let fileCount = 0;
|
|
35
|
+
let dirCount = 0;
|
|
36
|
+
for (const entry of zip.getEntries()) {
|
|
37
|
+
const archiveEntry = {
|
|
38
|
+
name: entry.entryName,
|
|
39
|
+
size: entry.header.size,
|
|
40
|
+
compressedSize: entry.header.compressedSize,
|
|
41
|
+
isDir: entry.isDirectory
|
|
42
|
+
};
|
|
43
|
+
entries.push(archiveEntry);
|
|
44
|
+
if (entry.isDirectory) {
|
|
45
|
+
dirCount++;
|
|
46
|
+
} else {
|
|
47
|
+
fileCount++;
|
|
48
|
+
totalSize += entry.header.size;
|
|
49
|
+
totalCompressed += entry.header.compressedSize;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return {
|
|
53
|
+
format: "zip",
|
|
54
|
+
entries,
|
|
55
|
+
totalSize,
|
|
56
|
+
totalCompressed,
|
|
57
|
+
fileCount,
|
|
58
|
+
dirCount
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
function zipAdd(source, files) {
|
|
62
|
+
const zip = new AdmZip(source);
|
|
63
|
+
for (const [name, content] of Object.entries(files)) {
|
|
64
|
+
zip.addFile(name, content);
|
|
65
|
+
}
|
|
66
|
+
return zip.toBuffer();
|
|
67
|
+
}
|
|
68
|
+
function gzipCompress(data, level) {
|
|
69
|
+
return gzipSync(data, { level: level ?? constants.Z_DEFAULT_COMPRESSION });
|
|
70
|
+
}
|
|
71
|
+
function gzipDecompress(data) {
|
|
72
|
+
return gunzipSync(data);
|
|
73
|
+
}
|
|
74
|
+
function deflateCompress(data, level) {
|
|
75
|
+
return deflateSync(data, { level: level ?? constants.Z_DEFAULT_COMPRESSION });
|
|
76
|
+
}
|
|
77
|
+
function deflateDecompress(data) {
|
|
78
|
+
return inflateSync(data);
|
|
79
|
+
}
|
|
80
|
+
function brotliCompress(data, level) {
|
|
81
|
+
const params = {};
|
|
82
|
+
if (level !== void 0) {
|
|
83
|
+
const quality = Math.round(1 + (level - 1) / 8 * 10);
|
|
84
|
+
params[constants.BROTLI_PARAM_QUALITY] = quality;
|
|
85
|
+
}
|
|
86
|
+
return brotliCompressSync(data, { params });
|
|
87
|
+
}
|
|
88
|
+
function brotliDecompress(data) {
|
|
89
|
+
return brotliDecompressSync(data);
|
|
90
|
+
}
|
|
91
|
+
export {
|
|
92
|
+
brotliCompress,
|
|
93
|
+
brotliDecompress,
|
|
94
|
+
deflateCompress,
|
|
95
|
+
deflateDecompress,
|
|
96
|
+
gzipCompress,
|
|
97
|
+
gzipDecompress,
|
|
98
|
+
zipAdd,
|
|
99
|
+
zipCreate,
|
|
100
|
+
zipExtract,
|
|
101
|
+
zipList
|
|
102
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "peasy-compress",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Archive & compression library for Node.js — ZIP, gzip, brotli, deflate. Zero-config, TypeScript-first.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"import": "./dist/index.js",
|
|
11
|
+
"types": "./dist/index.d.ts"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"dist"
|
|
16
|
+
],
|
|
17
|
+
"scripts": {
|
|
18
|
+
"build": "tsup src/index.ts --format esm --dts",
|
|
19
|
+
"test": "vitest run",
|
|
20
|
+
"typecheck": "tsc --noEmit"
|
|
21
|
+
},
|
|
22
|
+
"keywords": [
|
|
23
|
+
"compress",
|
|
24
|
+
"zip",
|
|
25
|
+
"gzip",
|
|
26
|
+
"brotli",
|
|
27
|
+
"archive",
|
|
28
|
+
"peasy"
|
|
29
|
+
],
|
|
30
|
+
"author": "Peasy Tools",
|
|
31
|
+
"license": "MIT",
|
|
32
|
+
"repository": {
|
|
33
|
+
"url": "https://github.com/peasytools/peasy-compress-js.git"
|
|
34
|
+
},
|
|
35
|
+
"homepage": "https://peasytools.com",
|
|
36
|
+
"dependencies": {
|
|
37
|
+
"adm-zip": "^0.5",
|
|
38
|
+
"archiver": "^7.0"
|
|
39
|
+
},
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"@types/adm-zip": "^0.5",
|
|
42
|
+
"@types/archiver": "^6.0",
|
|
43
|
+
"tsup": "^8.0",
|
|
44
|
+
"typescript": "^5.7",
|
|
45
|
+
"vitest": "^3.0"
|
|
46
|
+
}
|
|
47
|
+
}
|