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

Added options.maxPayload for websocket clients #1402

Merged
merged 6 commits into from
Jul 7, 2018
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
4 changes: 3 additions & 1 deletion bench/speed.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ if (cluster.isMaster) {

const runConfig = (useBinary, roundtrips, size, cb) => {
const data = randomBytes.slice(0, size);
const ws = new WebSocket(`ws://localhost:${port}`);
const ws = new WebSocket(`ws://localhost:${port}`, {
maxPayload: 600 * 1024 * 1024
});
var roundtrip = 0;
var time;

Expand Down
1 change: 1 addition & 0 deletions doc/ws.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ This class represents a WebSocket. It extends the `EventEmitter`.
- `protocolVersion` {Number} Value of the `Sec-WebSocket-Version` header.
- `origin` {String} Value of the `Origin` or `Sec-WebSocket-Origin` header
depending on the `protocolVersion`.
- `maxPayload` {Number} The maximum allowed message size in bytes.
- Any other option allowed in [http.request()][] or [https.request()][].

`perMessageDeflate` default value is `true`. When using an object, parameters
Expand Down
9 changes: 6 additions & 3 deletions lib/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -411,12 +411,14 @@ module.exports = WebSocket;
* @param {Number} options.handshakeTimeout Timeout in milliseconds for the handshake request
* @param {Number} options.protocolVersion Value of the `Sec-WebSocket-Version` header
* @param {String} options.origin Value of the `Origin` or `Sec-WebSocket-Origin` header
* @param {Number} options.maxPayload The maximum allowed message size
* @private
*/
function initAsClient (address, protocols, options) {
options = Object.assign({
protocolVersion: protocolVersions[1],
perMessageDeflate: true
perMessageDeflate: true,
maxPayload: 100 * 1024 * 1024
}, options, {
createConnection: undefined,
socketPath: undefined,
Expand Down Expand Up @@ -479,7 +481,8 @@ function initAsClient (address, protocols, options) {
if (options.perMessageDeflate) {
perMessageDeflate = new PerMessageDeflate(
options.perMessageDeflate !== true ? options.perMessageDeflate : {},
false
false,
options.maxPayload
);
options.headers['Sec-WebSocket-Extensions'] = extension.format({
[PerMessageDeflate.extensionName]: perMessageDeflate.offer()
Expand Down Expand Up @@ -599,7 +602,7 @@ function initAsClient (address, protocols, options) {
}
}

this.setSocket(socket, head, 0);
this.setSocket(socket, head, options.maxPayload);
});
}

Expand Down
22 changes: 22 additions & 0 deletions test/websocket.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,28 @@ describe('WebSocket', function () {
assert.strictEqual(count, 3);
});

it('accepts the `maxPayload` option', function (done) {
const maxPayload = 20480;
const wss = new WebSocket.Server({
perMessageDeflate: true,
port: 0
}, () => {
const ws = new WebSocket(`ws://localhost:${wss.address().port}`, {
perMessageDeflate: true,
maxPayload
});

ws.on('open', () => {
assert.strictEqual(ws._receiver._maxPayload, maxPayload);
assert.strictEqual(
ws._receiver._extensions['permessage-deflate']._maxPayload,
maxPayload
);
wss.close(done);
});
});
});

it('throws an error when using an invalid `protocolVersion`', function () {
const options = { agent: new CustomAgent(), protocolVersion: 1000 };

Expand Down