Skip to content

Commit 7491f46

Browse files
author
v1rtl
committed
support brotli
1 parent 80635d4 commit 7491f46

File tree

3 files changed

+22
-9
lines changed

3 files changed

+22
-9
lines changed

README.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ Deno HTTP compression middleware.
66

77
## Features
88

9+
- `gzip`, `deflate` and `brotli` support
910
- Detects supported encodings with `Accept-Encoding` header
10-
- Supports chaining algorithms (e.g. `gzip` -> `deflate`)
11+
- Respects encodings order (depending on `Accept-Encoding` value)
1112
- Creates a `Content-Encoding` header with applied compression
1213
- Send `409 Not Acceptable` if encoding is not supported
1314

@@ -21,14 +22,22 @@ const s = serve({ port: 3000 })
2122

2223
for await (const req of s) {
2324
await compression({
24-
// Path to file
25+
// Path to a file
2526
path: 'README.md',
26-
// Apply all algos in a queue
2727
compression: ['gzip', 'deflate']
2828
})(req)
2929
}
3030
```
3131

32+
Now try to send a `HEAD` request with `curl`:
33+
34+
```sh
35+
$ curl localhost:3000 --head -H "Accept-Encoding: br, gzip, deflate" --compressed
36+
HTTP/1.1 200 OK
37+
content-length: 550
38+
content-encoding: br, gzip, deflate
39+
```
40+
3241
[releases]: https://img.shields.io/github/v/release/deno-libs/compression?style=flat-square
3342
[docs-badge]: https://img.shields.io/github/v/release/deno-libs/compression?color=yellow&label=Documentation&logo=deno&style=flat-square
3443
[docs]: https://doc.deno.land/https/deno.land/x/compression/mod.ts

example.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ const s = serve({ port: 3000 })
66
for await (const req of s) {
77
await compression({
88
path: 'README.md',
9-
compression: ['gzip', 'deflate']
9+
compression: ['br', 'gzip', 'deflate']
1010
})(req)
1111
}

mod.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
1-
// import { compress as brotli } from 'https://deno.land/x/[email protected]/mod.ts'
1+
import { compress as brotli } from 'https://deno.land/x/[email protected]/mod.ts'
22
import { gzip, deflate } from 'https://deno.land/x/[email protected]/mod.ts'
33
import { ServerRequest } from 'https://deno.land/[email protected]/http/server.ts'
44
import { Accepts } from 'https://deno.land/x/[email protected]/mod.ts'
5+
import { readAll } from 'https://deno.land/[email protected]/io/util.ts'
56

67
const funcs = {
7-
// br: brotli,
8+
br: brotli,
89
gzip: (body: Uint8Array) => gzip(body, undefined),
910
deflate: (body: Uint8Array) => deflate(body, undefined)
1011
}
1112

1213
/**
1314
* Supported compression algorithms
1415
*/
15-
type Compression = 'gzip' | /* 'br' | */ 'deflate'
16+
type Compression = 'gzip' | 'br' | 'deflate'
1617

1718
export type CompressionOptions = {
1819
/**
@@ -51,7 +52,7 @@ export const compression = (opts: CompressionOptions) => async (req: ServerReque
5152

5253
const encodings = accepts.encodings()
5354

54-
const buf = await Deno.readAll(await Deno.open(opts.path))
55+
const buf = await readAll(await Deno.open(opts.path))
5556

5657
if (!acceptHeader || acceptHeader === 'identity' || (Array.isArray(encodings) && encodings[0] === 'identity')) {
5758
return await req.respond({
@@ -77,7 +78,10 @@ export const compression = (opts: CompressionOptions) => async (req: ServerReque
7778
if (Array.isArray(encodings)) {
7879
let compressed: Uint8Array = buf
7980
let encs: string[] = []
80-
for (const enc of encodings.filter((x) => x !== 'identity')) {
81+
82+
for (let enc of encodings.filter((x) => x !== 'identity')) {
83+
if (enc === 'brotli') enc = 'br'
84+
8185
if (Object.keys(funcs).includes(enc as string)) {
8286
compressed = funcs[enc as Compression](compressed)
8387
encs.push(enc)

0 commit comments

Comments
 (0)