From 23bee83c34aaea472a3329d3ed09b4e14c0d5dd8 Mon Sep 17 00:00:00 2001 From: Luke Edwards Date: Wed, 26 May 2021 21:25:42 -0700 Subject: [PATCH 1/4] fix(polkompress): always call `writeHead` step --- packages/wmr/src/lib/polkompress.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/wmr/src/lib/polkompress.js b/packages/wmr/src/lib/polkompress.js index 907a7983c..16880082e 100644 --- a/packages/wmr/src/lib/polkompress.js +++ b/packages/wmr/src/lib/polkompress.js @@ -67,7 +67,7 @@ export default function compression({ threshold = 1024, level = -1, brotli = fal listeners.forEach(p => on.apply(res, p)); } - if (pendingStatus) writeHead.call(res, pendingStatus); + writeHead.call(res, pendingStatus || res.statusCode); } const { end, write, on, writeHead } = res; From 8037e223df7732e714eb2aa42cbee22646c483d8 Mon Sep 17 00:00:00 2001 From: Luke Edwards Date: Wed, 26 May 2021 21:25:49 -0700 Subject: [PATCH 2/4] chore: add `polkompress` tests --- packages/wmr/test/lib/polkompress.test.js | 127 ++++++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 packages/wmr/test/lib/polkompress.test.js diff --git a/packages/wmr/test/lib/polkompress.test.js b/packages/wmr/test/lib/polkompress.test.js new file mode 100644 index 000000000..336b603ba --- /dev/null +++ b/packages/wmr/test/lib/polkompress.test.js @@ -0,0 +1,127 @@ +import { createServer } from 'http'; +import compression from '../../src/lib/polkompress.js'; +import { get } from '../test-helpers.js'; + +/** + * @param {string} address + * @param {string} [pathname] + */ +function send(address, pathname = '/') { + let ctx = /** @type {*} */ ({ address }); + return get(ctx, pathname); +} + +function setup(handler) { + let mware = compression({ level: 1, threshold: 4 }); + let server = createServer((req, res) => { + req.headers['accept-encoding'] = 'gzip'; + res.setHeader('content-type', 'text/plain'); + mware(req, res, () => handler(req, res)); + }); + return { + listen() { + return new Promise(res => { + server.listen(() => { + let info = server.address(); + let port = /** @type {import('net').AddressInfo} */(info).port; + return res(`http://localhost:${port}`); + }); + }); + }, + close() { + server.close(); + } + }; +} + +describe.only('polkompress', () => { + it('should be a function', () => { + expect(typeof compression).toBe('function'); + }); + + it('should return a function', () => { + expect(typeof compression()).toBe('function'); + }); + + it('should allow server to work if not compressing', async () => { + const server = setup((r, res) => { + res.end('OK'); + }); + + try { + const address = await server.listen(); + const output = await send(address); + expect(output.status).toBe(200); + expect(output.body).toBe('OK'); + + const headers = output.res.headers; + expect(headers['content-type']).toBe('text/plain'); + expect(headers['content-encoding']).toBe(undefined); + expect(headers['transfer-encoding']).toBe('chunked'); + expect(headers['content-length']).toBe(undefined); + } finally { + server.close(); + } + }); + + it('should compress body when over threshold', async () => { + const server = setup((r, res) => { + res.end('HELLO WORLD'); + }); + + try { + const address = await server.listen(); + const output = await send(address); + expect(output.status).toBe(200); + expect(output.body).not.toBe('HELLO WORLD'); + + const headers = output.res.headers; + expect(headers['content-type']).toBe('text/plain'); + expect(headers['content-encoding']).toBe('gzip'); + expect(headers['transfer-encoding']).toBe('chunked'); + expect(headers['content-length']).toBe(undefined); + } finally { + server.close(); + } + }); + + it('should respect custom `statusCode` when set :: enabled', async () => { + const server = setup((r, res) => { + res.statusCode = 201; + res.end('HELLO WORLD'); + }); + + try { + const address = await server.listen(); + const output = await send(address); + expect(output.status).toBe(201); + expect(output.body).not.toBe('HELLO WORLD'); + + const headers = output.res.headers; + expect(headers['content-encoding']).toBe('gzip'); + expect(headers['transfer-encoding']).toBe('chunked'); + } finally { + server.close(); + } + }); + + it('should respect custom `statusCode` when set :: disabled', async () => { + const server = setup((r, res) => { + res.statusCode = 201; + res.end('OK'); + }); + + try { + const address = await server.listen(); + const output = await send(address); + expect(output.status).toBe(201); + expect(output.body).toBe('OK'); + + const headers = output.res.headers; + expect(headers['content-encoding']).toBe(undefined); + expect(headers['transfer-encoding']).toBe('chunked'); + } finally { + server.close(); + } + }); +}); From 9a72c8cb4f5fa7b303d72cf7e83dbfb7d7185b9c Mon Sep 17 00:00:00 2001 From: Luke Edwards Date: Wed, 26 May 2021 21:47:10 -0700 Subject: [PATCH 3/4] chore: linter fixes --- packages/wmr/test/lib/polkompress.test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/wmr/test/lib/polkompress.test.js b/packages/wmr/test/lib/polkompress.test.js index 336b603ba..8de2ffaf0 100644 --- a/packages/wmr/test/lib/polkompress.test.js +++ b/packages/wmr/test/lib/polkompress.test.js @@ -23,7 +23,7 @@ function setup(handler) { return new Promise(res => { server.listen(() => { let info = server.address(); - let port = /** @type {import('net').AddressInfo} */(info).port; + let port = /** @type {import('net').AddressInfo} */ (info).port; return res(`http://localhost:${port}`); }); }); @@ -34,7 +34,7 @@ function setup(handler) { }; } -describe.only('polkompress', () => { +describe('polkompress', () => { it('should be a function', () => { expect(typeof compression).toBe('function'); }); From 7345e893e21c318738549e7d1c5fe92b30321afe Mon Sep 17 00:00:00 2001 From: Luke Edwards Date: Wed, 26 May 2021 21:48:57 -0700 Subject: [PATCH 4/4] chore: add changeset --- .changeset/hungry-students-pump.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/hungry-students-pump.md diff --git a/.changeset/hungry-students-pump.md b/.changeset/hungry-students-pump.md new file mode 100644 index 000000000..a963e1b7a --- /dev/null +++ b/.changeset/hungry-students-pump.md @@ -0,0 +1,5 @@ +--- +'wmr': patch +--- + +fix(compression): ensure all responses write headers