Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(polkompress): avoid hanging requests and/or runtime errors #648

Merged
merged 4 commits into from
May 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/hungry-students-pump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'wmr': patch
---

fix(compression): ensure all responses write headers
2 changes: 1 addition & 1 deletion packages/wmr/src/lib/polkompress.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
127 changes: 127 additions & 0 deletions packages/wmr/test/lib/polkompress.test.js
Original file line number Diff line number Diff line change
@@ -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('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();
}
});
});