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

Do not swallow errors emitted while destroying #1672

Merged
merged 1 commit into from Feb 29, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 10 additions & 1 deletion lib/stream.js
Expand Up @@ -78,6 +78,8 @@ function createWebSocketStream(ws, options) {
});

ws.once('error', function error(err) {
if (duplex.destroyed) return;

duplex.destroy(err);
});

Expand All @@ -94,8 +96,15 @@ function createWebSocketStream(ws, options) {
return;
}

ws.once('close', function close() {
let called = false;

ws.once('error', function error(err) {
called = true;
callback(err);
});

ws.once('close', function close() {
if (!called) callback(err);
process.nextTick(emitClose, duplex);
});
ws.terminate();
Expand Down
46 changes: 46 additions & 0 deletions test/create-websocket-stream.test.js
Expand Up @@ -168,6 +168,52 @@ describe('createWebSocketStream', () => {
});
});

it('does not swallow errors that may occur while destroying', (done) => {
const frame = Buffer.concat(
Sender.frame(Buffer.from([0x22, 0xfa, 0xec, 0x78]), {
fin: true,
rsv1: true,
opcode: 0x02,
mask: false,
readOnly: false
})
);

const wss = new WebSocket.Server(
{
perMessageDeflate: true,
port: 0
},
() => {
const ws = new WebSocket(`ws://localhost:${wss.address().port}`);
const duplex = createWebSocketStream(ws);

duplex.on('error', (err) => {
assert.ok(err instanceof Error);
assert.strictEqual(err.code, 'Z_DATA_ERROR');
assert.strictEqual(err.errno, -3);

duplex.on('close', () => {
wss.close(done);
});
});

let bytesRead = 0;

ws.on('open', () => {
ws._socket.on('data', (chunk) => {
bytesRead += chunk.length;
if (bytesRead === frame.length) duplex.destroy();
});
});
}
);

wss.on('connection', (ws) => {
ws._socket.write(frame);
});
});

it("does not suppress the throwing behavior of 'error' events", (done) => {
const wss = new WebSocket.Server({ port: 0 }, () => {
const ws = new WebSocket(`ws://localhost:${wss.address().port}`);
Expand Down
1 change: 1 addition & 0 deletions test/permessage-deflate.test.js
Expand Up @@ -589,6 +589,7 @@ describe('PerMessageDeflate', () => {
perMessageDeflate.accept([{}]);
perMessageDeflate.decompress(data, true, (err) => {
assert.ok(err instanceof Error);
assert.strictEqual(err.code, 'Z_DATA_ERROR');
assert.strictEqual(err.errno, -3);
done();
});
Expand Down