Skip to content

Commit 3ccddaa

Browse files
committed
Merge pull request http-party#463 from thornjad/brotli-en...
...coding Add support for brotli encoding
2 parents e703c5b + e1f5d23 commit 3ccddaa

File tree

7 files changed

+65
-1
lines changed

7 files changed

+65
-1
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ Using `npx` you can run the script without installing it first:
4343

4444
`-i` Display autoIndex (defaults to `true`)
4545

46-
`-g` or `--gzip` When enabled (defaults to `false`) it will serve `./public/some-file.js.gz` in place of `./public/some-file.js` when a gzipped version of the file exists and the request accepts gzip encoding.
46+
`-g` or `--gzip` When enabled (defaults to `false`) it will serve `./public/some-file.js.gz` in place of `./public/some-file.js` when a gzipped version of the file exists and the request accepts gzip encoding. If brotli is also enabled, it will try to serve brotli first.
47+
48+
`-b` or `--brotli` When enabled (defaults to `false`) it will serve `./public/some-file.js.br` in place of `./public/some-file.js` when a brotli compressed version of the file exists and the request accepts `br` encoding. If gzip is also enabled, it will try to serve brotli first.
4749

4850
`-e` or `--ext` Default file extension if none supplied (defaults to `html`)
4951

bin/http-server

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ if (argv.h || argv.help) {
2424
' -d Show directory listings [true]',
2525
' -i Display autoIndex [true]',
2626
' -g --gzip Serve gzip files when possible [false]',
27+
' -b --brotli Serve brotli files when possible [false]',
28+
' If both brotli and gzip are enabled, brotli takes precedence',
2729
' -e --ext Default file extension if none supplied [none]',
2830
' -s --silent Suppress log messages from output',
2931
' --cors[=headers] Enable CORS via the "Access-Control-Allow-Origin" header',
@@ -109,6 +111,7 @@ function listen(port) {
109111
showDir: argv.d,
110112
autoIndex: argv.i,
111113
gzip: argv.g || argv.gzip,
114+
brotli: argv.b || argv.brotli,
112115
robots: argv.r || argv.robots,
113116
ext: argv.e || argv.ext,
114117
logFn: logger.request,

lib/http-server.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ function HttpServer(options) {
5656
this.autoIndex = options.autoIndex !== 'false';
5757
this.showDotfiles = options.showDotfiles;
5858
this.gzip = options.gzip === true;
59+
this.brotli = options.brotli === true;
5960
this.contentType = options.contentType || 'application/octet-stream';
6061

6162
if (options.ext) {
@@ -130,6 +131,7 @@ function HttpServer(options) {
130131
autoIndex: this.autoIndex,
131132
defaultExt: this.ext,
132133
gzip: this.gzip,
134+
brotli: this.brotli,
133135
contentType: this.contentType,
134136
handleError: typeof options.proxy !== 'string'
135137
}));
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
I'm not compressed!
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
�im brotli compressed!!
3+

52 Bytes
Binary file not shown.

test/http-server-test.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,59 @@ vows.describe('http-server').addBatch({
158158
}
159159
}
160160
},
161+
'When gzip and brotli compression is enabled and a compressed file is available': {
162+
topic: function () {
163+
var server = httpServer.createServer({
164+
root: root,
165+
brotli: true,
166+
gzip: true
167+
});
168+
server.listen(8084);
169+
this.callback(null, server);
170+
},
171+
'and a request accepting only gzip is made': {
172+
topic: function () {
173+
request({
174+
uri: 'http://127.0.0.1:8084/compression/',
175+
headers: {
176+
'accept-encoding': 'gzip'
177+
}
178+
}, this.callback);
179+
},
180+
'response should be gzip compressed': function (err, res, body) {
181+
assert.equal(res.statusCode, 200);
182+
assert.equal(res.headers['content-encoding'], 'gzip');
183+
}
184+
},
185+
'and a request accepting only brotli is made': {
186+
topic: function () {
187+
request({
188+
uri: 'http://127.0.0.1:8084/compression/',
189+
headers: {
190+
'accept-encoding': 'br'
191+
}
192+
}, this.callback);
193+
},
194+
'response should be brotli compressed': function (err, res, body) {
195+
assert.equal(res.statusCode, 200);
196+
assert.equal(res.headers['content-encoding'], 'br');
197+
}
198+
},
199+
'and a request accepting both brotli and gzip is made': {
200+
topic: function () {
201+
request({
202+
uri: 'http://127.0.0.1:8084/compression/',
203+
headers: {
204+
'accept-encoding': 'gzip, br'
205+
}
206+
}, this.callback);
207+
},
208+
'response should be brotli compressed': function (err, res, body) {
209+
assert.equal(res.statusCode, 200);
210+
assert.equal(res.headers['content-encoding'], 'br');
211+
}
212+
}
213+
},
161214
'When http-server is listening on 8083 with username "good_username" and password "good_password"': {
162215
topic: function () {
163216
var server = httpServer.createServer({

0 commit comments

Comments
 (0)