Skip to content

Commit 9b1839c

Browse files
jonathanongGuillaume Mayer
and
Guillaume Mayer
authored
fix: ensure each fn call has a new state (#109)
* test gzip, deflate, br then gzip * fix lint * fix issue #104 * move new test to the new test file * encodings: move initialization to the middleware fn Co-authored-by: Guillaume Mayer <gmayer@cocha.com>
1 parent 895fbe8 commit 9b1839c

File tree

4 files changed

+72
-24
lines changed

4 files changed

+72
-24
lines changed

__tests__/index.js

Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,17 @@ describe('Compress', () => {
2121
ctx.body = buffer
2222
}
2323

24+
let server
25+
afterEach(() => { if (server) server.close() })
26+
2427
it('should compress strings', (done) => {
2528
const app = new Koa()
2629

2730
app.use(compress())
2831
app.use(sendString)
32+
server = app.listen()
2933

30-
request(app.listen())
34+
request(server)
3135
.get('/')
3236
.expect(200)
3337
.end((err, res) => {
@@ -49,8 +53,9 @@ describe('Compress', () => {
4953
threshold: '1mb'
5054
}))
5155
app.use(sendString)
56+
server = app.listen()
5257

53-
request(app.listen())
58+
request(server)
5459
.get('/')
5560
.expect(200)
5661
.end((err, res) => {
@@ -74,8 +79,9 @@ describe('Compress', () => {
7479
app.use((ctx, next) => {
7580
ctx.body = jsonBody
7681
})
82+
server = app.listen()
7783

78-
request(app.listen())
84+
request(server)
7985
.get('/')
8086
.expect(200)
8187
.end((err, res) => {
@@ -98,8 +104,9 @@ describe('Compress', () => {
98104
app.use((ctx, next) => {
99105
ctx.body = jsonBody
100106
})
107+
server = app.listen()
101108

102-
request(app.listen())
109+
request(server)
103110
.get('/')
104111
.expect(200)
105112
.end((err, res) => {
@@ -119,8 +126,9 @@ describe('Compress', () => {
119126

120127
app.use(compress())
121128
app.use(sendBuffer)
129+
server = app.listen()
122130

123-
request(app.listen())
131+
request(server)
124132
.get('/')
125133
.expect(200)
126134
.end((err, res) => {
@@ -143,8 +151,9 @@ describe('Compress', () => {
143151
ctx.type = 'application/javascript'
144152
ctx.body = fs.createReadStream(path.join(__dirname, 'index.js'))
145153
})
154+
server = app.listen()
146155

147-
request(app.listen())
156+
request(server)
148157
.get('/')
149158
.expect(200)
150159
.end((err, res) => {
@@ -164,8 +173,9 @@ describe('Compress', () => {
164173

165174
app.use(compress())
166175
app.use(sendBuffer)
176+
server = app.listen()
167177

168-
request(app.listen())
178+
request(server)
169179
.get('/')
170180
.expect(200)
171181
.end((err, res) => {
@@ -187,8 +197,9 @@ describe('Compress', () => {
187197
ctx.compress = false
188198
ctx.body = buffer
189199
})
200+
server = app.listen()
190201

191-
request(app.listen())
202+
request(server)
192203
.get('/')
193204
.expect(200)
194205
.end((err, res) => {
@@ -208,8 +219,9 @@ describe('Compress', () => {
208219

209220
app.use(compress())
210221
app.use(sendString)
222+
server = app.listen()
211223

212-
request(app.listen())
224+
request(server)
213225
.head('/')
214226
.expect(200, (err, res) => {
215227
if (err) { return done(err) }
@@ -225,8 +237,9 @@ describe('Compress', () => {
225237

226238
app.use(compress())
227239
app.use(sendBuffer)
240+
server = app.listen()
228241

229-
request(app.listen())
242+
request(server)
230243
.get('/')
231244
.set('Accept-Encoding', 'sdch, gzip, deflate')
232245
.expect(200, done)
@@ -237,8 +250,9 @@ describe('Compress', () => {
237250

238251
app.use(compress())
239252
app.use(sendBuffer)
253+
server = app.listen()
240254

241-
request(app.listen())
255+
request(server)
242256
.get('/')
243257
.expect(200, done)
244258
})
@@ -251,8 +265,9 @@ describe('Compress', () => {
251265
ctx.type = 'image/png'
252266
ctx.body = Buffer.alloc(2048)
253267
})
268+
server = app.listen()
254269

255-
request(app.listen())
270+
request(server)
256271
.get('/')
257272
.expect(200, done)
258273
})
@@ -268,8 +283,9 @@ describe('Compress', () => {
268283
ctx.type = 'text'
269284
ctx.body = 'asdf'
270285
})
286+
server = app.listen()
271287

272-
request(app.listen())
288+
request(server)
273289
.get('/')
274290
.expect('asdf', done)
275291
})
@@ -281,8 +297,9 @@ describe('Compress', () => {
281297
flush: zlib.Z_SYNC_FLUSH
282298
}))
283299
app.use(sendString)
300+
server = app.listen()
284301

285-
request(app.listen())
302+
request(server)
286303
.get('/')
287304
.expect(200)
288305
.end((err, res) => {
@@ -309,8 +326,9 @@ describe('Compress', () => {
309326
next()
310327
})
311328
app.use(sendString)
329+
server = app.listen()
312330

313-
request(app.listen())
331+
request(server)
314332
.get('/')
315333
.expect(200)
316334
.end((err, res) => {
@@ -337,8 +355,9 @@ describe('Compress', () => {
337355
next()
338356
})
339357
app.use(sendString)
358+
server = app.listen()
340359

341-
request(app.listen())
360+
request(server)
342361
.get('/')
343362
.expect(200)
344363
.end((err, res) => {
@@ -360,8 +379,9 @@ describe('Compress', () => {
360379

361380
app.use(compress())
362381
app.use(sendBuffer)
382+
server = app.listen()
363383

364-
request(app.listen())
384+
request(server)
365385
.get('/')
366386
.set('Accept-Encoding', 'deflate')
367387
.expect(200)
@@ -380,8 +400,9 @@ describe('Compress', () => {
380400

381401
app.use(compress())
382402
app.use(sendBuffer)
403+
server = app.listen()
383404

384-
request(app.listen())
405+
request(server)
385406
.get('/')
386407
.set('Accept-Encoding', 'gzip, deflate')
387408
.expect(200)
@@ -402,8 +423,9 @@ describe('Compress', () => {
402423

403424
app.use(compress())
404425
app.use(sendBuffer)
426+
server = app.listen()
405427

406-
request(app.listen())
428+
request(server)
407429
.get('/')
408430
.set('Accept-Encoding', 'br')
409431
.expect(200)
@@ -422,8 +444,9 @@ describe('Compress', () => {
422444

423445
app.use(compress({ br: false }))
424446
app.use(sendBuffer)
447+
server = app.listen()
425448

426-
request(app.listen())
449+
request(server)
427450
.get('/')
428451
.set('Accept-Encoding', 'gzip, deflate, br')
429452
.expect(200)

__tests__/middleware.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,28 @@ describe('Accept-Encodings', () => {
4848
})
4949
})
5050
})
51+
52+
describe('Subsequent requests', () => {
53+
test('accept-encoding: "gzip, deflate, br", then "gzip"', async () => {
54+
const app = new Koa()
55+
56+
app.use(compress())
57+
app.use(async (ctx) => {
58+
ctx.body = await crypto.randomBytes(2048).toString('base64')
59+
})
60+
server = app.listen()
61+
62+
const res1 = await request(server)
63+
.get('/')
64+
.set('Accept-Encoding', 'gzip, deflate, br')
65+
.expect(200)
66+
expect(res1.headers['content-encoding']).toBe('br')
67+
68+
const res2 = await request(server)
69+
.get('/')
70+
.set('Accept-Encoding', 'gzip')
71+
.expect(200)
72+
73+
expect(res2.headers['content-encoding']).toBe('gzip')
74+
})
75+
})

lib/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@ module.exports = (options = {}) => {
3131

3232
// `options.br = false` would remove it as a preferred encoding
3333
const preferredEncodings = Encodings.preferredEncodings.filter((encoding) => options[encoding] !== false && options[encoding] !== null)
34-
const encodings = new Encodings({
35-
preferredEncodings
36-
})
3734

3835
return async (ctx, next) => {
3936
ctx.vary('Accept-Encoding')
@@ -61,6 +58,9 @@ module.exports = (options = {}) => {
6158
if (threshold && ctx.response.length < threshold) return
6259

6360
// get the preferred content encoding
61+
const encodings = new Encodings({
62+
preferredEncodings
63+
})
6464
encodings.parseAcceptEncoding(ctx.request.headers['accept-encoding'] || undefined)
6565
const encoding = encodings.getPreferredContentEncoding()
6666

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
},
3131
"scripts": {
3232
"eslint": "eslint --ignore-path .gitignore .",
33-
"test": "jest --forceExit"
33+
"test": "jest"
3434
},
3535
"files": [
3636
"lib",

0 commit comments

Comments
 (0)