Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: websockets/ws
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 8.14.1
Choose a base ref
...
head repository: websockets/ws
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 8.14.2
Choose a head ref
  • 2 commits
  • 3 files changed
  • 1 contributor

Commits on Sep 19, 2023

  1. [fix] Add missing rejection handler

    Use `queueMicrotask()` when available and add a rejection handler to the
    shim for it.
    lpinca committed Sep 19, 2023
    Copy the full SHA
    7f4e1a7 View commit details
  2. [dist] 8.14.2

    lpinca committed Sep 19, 2023
    Copy the full SHA
    d8dd485 View commit details
Showing with 67 additions and 6 deletions.
  1. +36 −5 lib/receiver.js
  2. +1 −1 package.json
  3. +30 −0 test/receiver.test.js
41 changes: 36 additions & 5 deletions lib/receiver.js
Original file line number Diff line number Diff line change
@@ -15,6 +15,12 @@ const { isValidStatusCode, isValidUTF8 } = require('./validation');
const FastBuffer = Buffer[Symbol.species];
const promise = Promise.resolve();

//
// `queueMicrotask()` is not available in Node.js < 11.
//
const queueTask =
typeof queueMicrotask === 'function' ? queueMicrotask : queueMicrotaskShim;

const GET_INFO = 0;
const GET_PAYLOAD_LENGTH_16 = 1;
const GET_PAYLOAD_LENGTH_64 = 2;
@@ -169,11 +175,7 @@ class Receiver extends Writable {
//
this._loop = false;

//
// `queueMicrotask()` is not available in Node.js < 11 and is no
// better anyway.
//
promise.then(() => {
queueTask(() => {
this._state = GET_INFO;
this.startLoop(cb);
});
@@ -646,3 +648,32 @@ function error(ErrorCtor, message, prefix, statusCode, errorCode) {
err[kStatusCode] = statusCode;
return err;
}

/**
* A shim for `queueMicrotask()`.
*
* @param {Function} cb Callback
*/
function queueMicrotaskShim(cb) {
promise.then(cb).catch(throwErrorNextTick);
}

/**
* Throws an error.
*
* @param {Error} err The error to throw
* @private
*/
function throwError(err) {
throw err;
}

/**
* Throws an error in the next tick.
*
* @param {Error} err The error to throw
* @private
*/
function throwErrorNextTick(err) {
process.nextTick(throwError, err);
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ws",
"version": "8.14.1",
"version": "8.14.2",
"description": "Simple to use, blazing fast and thoroughly tested websocket client and server for Node.js",
"keywords": [
"HyBi",
30 changes: 30 additions & 0 deletions test/receiver.test.js
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@

const assert = require('assert');
const crypto = require('crypto');
const EventEmitter = require('events');

const PerMessageDeflate = require('../lib/permessage-deflate');
const Receiver = require('../lib/receiver');
@@ -1175,4 +1176,33 @@ describe('Receiver', () => {

receiver.write(Buffer.from('8A01318A01328A0133', 'hex'));
});

it('does not swallow errors thrown from event handlers', (done) => {
const receiver = new Receiver();
let count = 0;

receiver.on('message', function () {
if (++count === 2) {
throw new Error('Oops');
}
});

assert.strictEqual(
process.listenerCount('uncaughtException'),
EventEmitter.usingDomains ? 2 : 1
);

const listener = process.listeners('uncaughtException').pop();

process.removeListener('uncaughtException', listener);
process.once('uncaughtException', (err) => {
assert.ok(err instanceof Error);
assert.strictEqual(err.message, 'Oops');

process.on('uncaughtException', listener);
done();
});

receiver.write(Buffer.from('82008200', 'hex'));
});
});