-
-
Notifications
You must be signed in to change notification settings - Fork 175
Expand file tree
/
Copy pathindex.js
More file actions
137 lines (99 loc) · 3.99 KB
/
index.js
File metadata and controls
137 lines (99 loc) · 3.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import { suite } from 'uvu';
import * as assert from 'uvu/assert';
import fs from 'fs';
import { dirname, join } from 'path';
import { fileURLToPath } from 'url';
import * as zlib from 'zlib';
import { prepare, toAscii } from './util/index.js';
import compression from '../index.js';
const __dirname = dirname(fileURLToPath(import.meta.url));
const GZIP = 'gzip, deflate';
const BR = 'br, gzip, deflate';
const instantiation = suite('instantiation');
instantiation('should export a function', () => {
assert.type(compression, 'function');
});
instantiation('installs as middleware', () => {
const { req, res } = prepare('GET', GZIP);
const middleware = compression();
let calledNext = false;
middleware(req, res, () => {
calledNext = true;
});
assert.ok(calledNext);
});
instantiation('works without next callback', () => {
const { req, res } = prepare('GET', GZIP);
const middleware = compression();
assert.not.throws(() => middleware(req, res));
});
instantiation.run();
// ---
const basics = suite('basics');
basics('compresses content over threshold', () => {
const { req, res } = prepare('GET', GZIP);
compression()(req, res);
res.writeHead(200, { 'content-type': 'text/plain' })
res.write('hello world'.repeat(1000));
res.end();
assert.is(res.getHeader('content-encoding'), 'gzip', 'above threshold triggers gzip');
});
basics('compresses content with no content-type', () => {
const { req, res } = prepare('GET', GZIP);
compression({ threshold: 0 })(req, res);
res.end('hello world');
assert.is(res.getHeader('content-encoding'), 'gzip', 'above threshold triggers gzip');
});
basics('ignores content with unmatched content-type', async () => {
const { req, res } = prepare('GET', GZIP);
compression({ threshold: 0 })(req, res);
res.writeHead(200, { 'content-type': 'image/jpeg' });
const content = 'hello world';
res.end(content);
assert.is(res.hasHeader('content-encoding'), false, 'no content-encoding header should be set');
assert.is(await res.getResponseText(), content, 'content should be unmodified');
});
basics('preserves plaintext below threshold', async () => {
const { req, res } = prepare('GET', GZIP);
compression()(req, res);
res.writeHead(200, { 'content-type': 'text/plain' });
const content = 'hello world'.repeat(20);
res.end(content);
assert.is(res.hasHeader('content-encoding'), false, 'below threshold triggers gzip');
assert.is(await res.getResponseText(), content, 'content should be unmodified');
});
basics.run();
// ---
const brotli = suite('brotli');
const brotliIfSupported = zlib.createBrotliCompress != null ? brotli : brotli.skip;
brotliIfSupported('compresses content with brotli when supported', async () => {
const { req, res } = prepare('GET', 'br');
compression({ brotli: true, threshold: 0 })(req, res);
res.writeHead(200, { 'content-type': 'text/plain' })
res.end('hello world');
const body = await res.getResponseData();
assert.is(res.getHeader('content-encoding'), 'br', 'uses brotli encoding');
assert.snapshot(toAscii(body), toAscii('\u000b\u0005\u0000hello world\u0003'), 'compressed content');
});
brotliIfSupported('gives brotli precedence over gzip', () => {
const { req, res } = prepare('GET', BR);
compression({ brotli: true, threshold: 0 })(req, res);
res.writeHead(200, { 'content-type': 'text/plain' })
res.end('hello world'.repeat(20));
assert.is(res.getHeader('content-encoding'), 'br', 'uses brotli encoding');
});
brotli.run();
// ---
const streaming = suite('streaming');
streaming('allows piping streams', async () => {
const pkg = join(__dirname, '../package.json');
const gzipped = zlib.gzipSync(fs.readFileSync(pkg));
const { req, res } = prepare('GET', GZIP);
compression({ threshold: 0 })(req, res);
res.writeHead(200, { 'content-type': 'text/plain' });
fs.createReadStream(pkg).pipe(res, { end: true });
const body = await res.getResponseData();
assert.is(res.getHeader('content-encoding'), 'gzip', 'compresses with gzip');
assert.equal(toAscii(body), toAscii(gzipped), 'content is compressed');
});
streaming.run();