Skip to content

Commit 96c9b3d

Browse files
authored
[major] Flip the default value of allowSynchronousEvents (#2221)
Flip the default value of the `allowSynchronousEvents` option to `true`. Refs: #2218
1 parent e5f32c7 commit 96c9b3d

File tree

5 files changed

+19
-51
lines changed

5 files changed

+19
-51
lines changed

doc/ws.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ This class represents a WebSocket server. It extends the `EventEmitter`.
7676
in response to a ping. Defaults to `true`.
7777
- `allowSynchronousEvents` {Boolean} Specifies whether any of the `'message'`,
7878
`'ping'`, and `'pong'` events can be emitted multiple times in the same
79-
tick. To improve compatibility with the WHATWG standard, the default value
80-
is `false`. Setting it to `true` improves performance slightly.
79+
tick. Defaults to `true`. Setting it to `false` improves compatibility with
80+
the WHATWG standardbut may negatively impact performance.
8181
- `backlog` {Number} The maximum length of the queue of pending connections.
8282
- `clientTracking` {Boolean} Specifies whether or not to track clients.
8383
- `handleProtocols` {Function} A function which can be used to handle the
@@ -302,8 +302,8 @@ This class represents a WebSocket. It extends the `EventEmitter`.
302302
in response to a ping. Defaults to `true`.
303303
- `allowSynchronousEvents` {Boolean} Specifies whether any of the `'message'`,
304304
`'ping'`, and `'pong'` events can be emitted multiple times in the same
305-
tick. To improve compatibility with the WHATWG standard, the default value
306-
is `false`. Setting it to `true` improves performance slightly.
305+
tick. Defaults to `true`. Setting it to `false` improves compatibility with
306+
the WHATWG standardbut may negatively impact performance.
307307
- `finishRequest` {Function} A function which can be used to customize the
308308
headers of each HTTP request before it is sent. See description below.
309309
- `followRedirects` {Boolean} Whether or not to follow redirects. Defaults to

lib/receiver.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class Receiver extends Writable {
3232
* Creates a Receiver instance.
3333
*
3434
* @param {Object} [options] Options object
35-
* @param {Boolean} [options.allowSynchronousEvents=false] Specifies whether
35+
* @param {Boolean} [options.allowSynchronousEvents=true] Specifies whether
3636
* any of the `'message'`, `'ping'`, and `'pong'` events can be emitted
3737
* multiple times in the same tick
3838
* @param {String} [options.binaryType=nodebuffer] The type for binary data
@@ -47,7 +47,10 @@ class Receiver extends Writable {
4747
constructor(options = {}) {
4848
super();
4949

50-
this._allowSynchronousEvents = !!options.allowSynchronousEvents;
50+
this._allowSynchronousEvents =
51+
options.allowSynchronousEvents !== undefined
52+
? options.allowSynchronousEvents
53+
: true;
5154
this._binaryType = options.binaryType || BINARY_TYPES[0];
5255
this._extensions = options.extensions || {};
5356
this._isServer = !!options.isServer;

lib/websocket-server.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class WebSocketServer extends EventEmitter {
2929
* Create a `WebSocketServer` instance.
3030
*
3131
* @param {Object} options Configuration options
32-
* @param {Boolean} [options.allowSynchronousEvents=false] Specifies whether
32+
* @param {Boolean} [options.allowSynchronousEvents=true] Specifies whether
3333
* any of the `'message'`, `'ping'`, and `'pong'` events can be emitted
3434
* multiple times in the same tick
3535
* @param {Boolean} [options.autoPong=true] Specifies whether or not to
@@ -60,7 +60,7 @@ class WebSocketServer extends EventEmitter {
6060
super();
6161

6262
options = {
63-
allowSynchronousEvents: false,
63+
allowSynchronousEvents: true,
6464
autoPong: true,
6565
maxPayload: 100 * 1024 * 1024,
6666
skipUTF8Validation: false,

lib/websocket.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,7 @@ module.exports = WebSocket;
623623
* @param {(String|URL)} address The URL to which to connect
624624
* @param {Array} protocols The subprotocols
625625
* @param {Object} [options] Connection options
626-
* @param {Boolean} [options.allowSynchronousEvents=false] Specifies whether any
626+
* @param {Boolean} [options.allowSynchronousEvents=true] Specifies whether any
627627
* of the `'message'`, `'ping'`, and `'pong'` events can be emitted multiple
628628
* times in the same tick
629629
* @param {Boolean} [options.autoPong=true] Specifies whether or not to
@@ -652,7 +652,7 @@ module.exports = WebSocket;
652652
*/
653653
function initAsClient(websocket, address, protocols, options) {
654654
const opts = {
655-
allowSynchronousEvents: false,
655+
allowSynchronousEvents: true,
656656
autoPong: true,
657657
protocolVersion: protocolVersions[1],
658658
maxPayload: 100 * 1024 * 1024,

test/receiver.test.js

+6-41
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ describe('Receiver', () => {
443443
buf[i + 1] = 0x00;
444444
}
445445

446-
const receiver = new Receiver({ allowSynchronousEvents: true });
446+
const receiver = new Receiver();
447447
let counter = 0;
448448

449449
receiver.on('message', (data, isBinary) => {
@@ -1085,7 +1085,7 @@ describe('Receiver', () => {
10851085
receiver.write(Buffer.from([0x88, 0x03, 0x03, 0xe8, 0xf8]));
10861086
});
10871087

1088-
it('emits at most one event per event loop iteration', (done) => {
1088+
it('honors the `allowSynchronousEvents` option', (done) => {
10891089
const actual = [];
10901090
const expected = [
10911091
'1',
@@ -1121,7 +1121,7 @@ describe('Receiver', () => {
11211121
});
11221122
}
11231123

1124-
const receiver = new Receiver();
1124+
const receiver = new Receiver({ allowSynchronousEvents: false });
11251125

11261126
receiver.on('message', listener);
11271127
receiver.on('ping', listener);
@@ -1156,43 +1156,8 @@ describe('Receiver', () => {
11561156
done();
11571157
});
11581158

1159-
receiver.write(Buffer.from('82008200', 'hex'));
1160-
});
1161-
1162-
it('honors the `allowSynchronousEvents` option', (done) => {
1163-
const actual = [];
1164-
const expected = [
1165-
'1',
1166-
'2',
1167-
'3',
1168-
'4',
1169-
'microtask 1',
1170-
'microtask 2',
1171-
'microtask 3',
1172-
'microtask 4'
1173-
];
1174-
1175-
function listener(data) {
1176-
const message = data.toString();
1177-
actual.push(message);
1178-
1179-
// `queueMicrotask()` is not available in Node.js < 11.
1180-
Promise.resolve().then(() => {
1181-
actual.push(`microtask ${message}`);
1182-
1183-
if (actual.length === 8) {
1184-
assert.deepStrictEqual(actual, expected);
1185-
done();
1186-
}
1187-
});
1188-
}
1189-
1190-
const receiver = new Receiver({ allowSynchronousEvents: true });
1191-
1192-
receiver.on('message', listener);
1193-
receiver.on('ping', listener);
1194-
receiver.on('pong', listener);
1195-
1196-
receiver.write(Buffer.from('8101318901328a0133810134', 'hex'));
1159+
setImmediate(() => {
1160+
receiver.write(Buffer.from('82008200', 'hex'));
1161+
});
11971162
});
11981163
});

0 commit comments

Comments
 (0)