Skip to content

Commit e327c77

Browse files
author
zhangfuxing
committed
feat: add Brotli support(#21)
1 parent fb749ad commit e327c77

File tree

8 files changed

+119
-4
lines changed

8 files changed

+119
-4
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Utilities to compress and uncompress for Deno!
1111
- [x] `gzip`
1212
- [x] `tgz`
1313
- [x] `zip`
14+
- [x] `brotli`
1415

1516
## Changelog
1617

@@ -253,6 +254,19 @@ await zip.compress("./test.txt", "./test.tar.gz");
253254
await zip.uncompress("./test.tar.gz", "./dest");
254255
```
255256

257+
## `brotli`
258+
259+
### Example
260+
261+
```ts
262+
import { brotli } from "jsr:@deno-library/compress";
263+
const compressedBuffer = await brotli.compress(inputString);
264+
265+
// deno version >= v2.1.8 / 2025.01.30
266+
// https://github.com/denoland/deno/pull/27815
267+
const uncompressedBuffer = await brotli.uncompress(compressedBuffer);
268+
```
269+
256270
# test
257271

258272
```ts

brotli/mod.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import zlib from "node:zlib";
2+
import { promisify } from "node:util";
3+
import type { Buffer } from "node:buffer";
4+
5+
const compress: (
6+
buffer: zlib.InputType,
7+
options?: zlib.BrotliOptions,
8+
) => Promise<Buffer> = promisify(zlib.brotliCompress);
9+
const compressSync: (
10+
buf: zlib.InputType,
11+
options?: zlib.BrotliOptions,
12+
) => Buffer = zlib.brotliCompressSync;
13+
const uncompress: (
14+
buffer: zlib.InputType,
15+
options?: zlib.BrotliOptions,
16+
) => Promise<Buffer> = promisify(zlib.brotliDecompress);
17+
const uncompressSync: (
18+
buf: zlib.InputType,
19+
options?: zlib.BrotliOptions,
20+
) => Buffer = zlib.brotliDecompressSync;
21+
export { compress, compressSync, uncompress, uncompressSync };

changelog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
## Changelog
22

3+
### v0.5.6 - 2025.03.26
4+
5+
- feat: add Brotli support(#21)
6+
37
### v0.5.5 - 2024.11.20
48

59
- fix: fix exists usage error

deno.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"name": "@deno-library/compress",
3-
"version": "0.5.5",
3+
"version": "0.5.6",
44
"exports": "./mod.ts"
55
}

deno.lock

Lines changed: 38 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mod.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export * as tar from "./tar/mod.ts";
22
export * as tgz from "./tgz/mod.ts";
33
export * as zip from "./zip/mod.ts";
4+
export * as brotli from "./brotli/mod.ts";
45
export {
56
gunzipFile,
67
gzipFile,

test/brotli.test.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { assert } from "jsr:@std/assert";
2+
import { brotli } from "../mod.ts";
3+
import { Buffer } from "node:buffer";
4+
5+
const inputString = 'ΩΩLorem ipsum dolor sit amet, consectetur adipiscing eli' +
6+
't. Morbi faucibus, purus at gravida dictum, libero arcu ' +
7+
'convallis lacus, in commodo libero metus eu nisi. Nullam' +
8+
' commodo, neque nec porta placerat, nisi est fermentum a' +
9+
'ugue, vitae gravida tellus sapien sit amet tellus. Aenea' +
10+
'n non diam orci. Proin quis elit turpis. Suspendisse non' +
11+
' diam ipsum. Suspendisse nec ullamcorper odio. Vestibulu' +
12+
'm arcu mi, sodales non suscipit id, ultrices ut massa. S' +
13+
'ed ac sem sit amet arcu malesuada fermentum. Nunc sed. ';
14+
const compressedString = 'G/gBQBwHdky2aHV5KK9Snf05//1pPdmNw/7232fnIm1IB' +
15+
'K1AA8RsN8OB8Nb7Lpgk3UWWUlzQXZyHQeBBbXMTQXC1j7' +
16+
'wg3LJs9LqOGHRH2bj/a2iCTLLx8hBOyTqgoVuD1e+Qqdn' +
17+
'f1rkUNyrWq6LtOhWgxP3QUwdhKGdZm3rJWaDDBV7+pDk1' +
18+
'MIkrmjp4ma2xVi5MsgJScA3tP1I7mXeby6MELozrwoBQD' +
19+
'mVTnEAicZNj4lkGqntJe2qSnGyeMmcFgraK94vCg/4iLu' +
20+
'Tw5RhKhnVY++dZ6niUBmRqIutsjf5TzwF5iAg8a9UkjF5' +
21+
'2eZ0tB2vo6v8SqVfNMkBmmhxr0NT9LkYF69aEjlYzj7IE' +
22+
'KmEUQf1HBogRYhFIt4ymRNEgHAIzOyNEsQM=';
23+
24+
Deno.test("brotli", async () => {
25+
// async
26+
const compressedBuffer = await brotli.compress(inputString);
27+
assert(compressedBuffer.toString('base64') === compressedString);
28+
29+
// deno version >= v2.1.8 / 2025.01.30
30+
// https://github.com/denoland/deno/pull/27815
31+
const uncompressedBuffer = await brotli.uncompress(compressedBuffer);
32+
assert(uncompressedBuffer.toString() === inputString);
33+
34+
// sync
35+
const buffer = Buffer.from(compressedString, 'base64');
36+
const d = brotli.uncompressSync(buffer);
37+
assert(d.toString() === inputString);
38+
});

test/deflate.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { assert, assertEquals } from "https://deno.land/std/testing/asserts.ts";
1+
import { assert } from "jsr:@std/assert";
22
import { deflate, inflate } from "../mod.ts";
33

44
Deno.test("deflate", () => {
@@ -7,4 +7,4 @@ Deno.test("deflate", () => {
77
const compressed = deflate(bytes);
88
const decompressed = inflate(compressed);
99
assert(str === new TextDecoder().decode(decompressed));
10-
});
10+
});

0 commit comments

Comments
 (0)