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: 7.2.0
Choose a base ref
...
head repository: websockets/ws
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 7.2.1
Choose a head ref
  • 7 commits
  • 7 files changed
  • 2 contributors

Commits on Oct 23, 2019

  1. [pkg] Add bufferutil and utf-8-validate as peer dependencies (#1626)

    Add bufferutil and utf-8-validate as optional peer dependencies.
    eps1lon authored and lpinca committed Oct 23, 2019
    Copy the full SHA
    289724f View commit details

Commits on Nov 14, 2019

  1. [pkg] Add engines field

    lpinca authored Nov 14, 2019

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    f06a738 View commit details

Commits on Nov 23, 2019

  1. [minor] Remove unnecessary optimization

    It does not make much sense to optimize for empty messages.
    lpinca committed Nov 23, 2019
    Copy the full SHA
    3293284 View commit details

Commits on Nov 29, 2019

  1. 6
    Copy the full SHA
    950e41a View commit details

Commits on Nov 30, 2019

  1. Copy the full SHA
    6df06d9 View commit details

Commits on Dec 11, 2019

  1. [ci] Test on node 13

    lpinca committed Dec 11, 2019
    Copy the full SHA
    a4b9e0b View commit details

Commits on Dec 14, 2019

  1. [dist] 7.2.1

    lpinca committed Dec 14, 2019
    Copy the full SHA
    9531cd0 View commit details
Showing with 119 additions and 17 deletions.
  1. +1 −0 .travis.yml
  2. +1 −0 appveyor.yml
  3. +54 −0 lib/limiter.js
  4. +6 −13 lib/permessage-deflate.js
  5. +15 −3 package.json
  6. +41 −0 test/limiter.test.js
  7. +1 −1 test/sender.test.js
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
language: node_js
node_js:
- '13'
- '12'
- '10'
- '8'
1 change: 1 addition & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
environment:
matrix:
- nodejs_version: '13'
- nodejs_version: '12'
- nodejs_version: '10'
- nodejs_version: '8'
54 changes: 54 additions & 0 deletions lib/limiter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
'use strict';

const kDone = Symbol('kDone');
const kRun = Symbol('kRun');

/**
* A very simple job queue with adjustable concurrency. Adapted from
* https://github.com/STRML/async-limiter
*/
class Limiter {
/**
* Creates a new `Limiter`.
*
* @param {Number} concurrency The maximum number of jobs allowed to run
* concurrently
*/
constructor(concurrency) {
this[kDone] = () => {
this.pending--;
this[kRun]();
};
this.concurrency = concurrency || Infinity;
this.jobs = [];
this.pending = 0;
}

/**
* Adds a job to the queue.
*
* @public
*/
add(job) {
this.jobs.push(job);
this[kRun]();
}

/**
* Removes a job from the queue and runs it if possible.
*
* @private
*/
[kRun]() {
if (this.pending === this.concurrency) return;

if (this.jobs.length) {
const job = this.jobs.shift();

this.pending++;
job(this[kDone]);
}
}
}

module.exports = Limiter;
19 changes: 6 additions & 13 deletions lib/permessage-deflate.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
'use strict';

const Limiter = require('async-limiter');
const zlib = require('zlib');

const bufferUtil = require('./buffer-util');
const Limiter = require('./limiter');
const { kStatusCode, NOOP } = require('./constants');

const TRAILER = Buffer.from([0x00, 0x00, 0xff, 0xff]);
const EMPTY_BLOCK = Buffer.from([0x00]);

const kPerMessageDeflate = Symbol('permessage-deflate');
const kTotalLength = Symbol('total-length');
const kCallback = Symbol('callback');
@@ -66,7 +64,7 @@ class PerMessageDeflate {
this._options.concurrencyLimit !== undefined
? this._options.concurrencyLimit
: 10;
zlibLimiter = new Limiter({ concurrency });
zlibLimiter = new Limiter(concurrency);
}
}

@@ -288,15 +286,15 @@ class PerMessageDeflate {
}

/**
* Decompress data. Concurrency limited by async-limiter.
* Decompress data. Concurrency limited.
*
* @param {Buffer} data Compressed data
* @param {Boolean} fin Specifies whether or not this is the last fragment
* @param {Function} callback Callback
* @public
*/
decompress(data, fin, callback) {
zlibLimiter.push((done) => {
zlibLimiter.add((done) => {
this._decompress(data, fin, (err, result) => {
done();
callback(err, result);
@@ -305,15 +303,15 @@ class PerMessageDeflate {
}

/**
* Compress data. Concurrency limited by async-limiter.
* Compress data. Concurrency limited.
*
* @param {Buffer} data Data to compress
* @param {Boolean} fin Specifies whether or not this is the last fragment
* @param {Function} callback Callback
* @public
*/
compress(data, fin, callback) {
zlibLimiter.push((done) => {
zlibLimiter.add((done) => {
this._compress(data, fin, (err, result) => {
done();
if (err || result) {
@@ -393,11 +391,6 @@ class PerMessageDeflate {
* @private
*/
_compress(data, fin, callback) {
if (!data || data.length === 0) {
process.nextTick(callback, null, EMPTY_BLOCK);
return;
}

const endpoint = this._isServer ? 'server' : 'client';

if (!this._deflate) {
18 changes: 15 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ws",
"version": "7.2.0",
"version": "7.2.1",
"description": "Simple to use, blazing fast and thoroughly tested websocket client and server for Node.js",
"keywords": [
"HyBi",
@@ -17,6 +17,9 @@
"license": "MIT",
"main": "index.js",
"browser": "browser.js",
"engines": {
"node": ">=8.3.0"
},
"files": [
"browser.js",
"index.js",
@@ -27,8 +30,17 @@
"integration": "npm run lint && mocha --throw-deprecation test/*.integration.js",
"lint": "eslint --ignore-path .gitignore . && prettier --check --ignore-path .gitignore \"**/*.{json,md,yaml,yml}\""
},
"dependencies": {
"async-limiter": "^1.0.0"
"peerDependencies": {
"bufferutil": "^4.0.1",
"utf-8-validate": "^5.0.2"
},
"peerDependenciesMeta": {
"bufferutil": {
"optional": true
},
"utf-8-validate": {
"optional": true
}
},
"devDependencies": {
"benchmark": "^2.1.4",
41 changes: 41 additions & 0 deletions test/limiter.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use strict';

const assert = require('assert');

const Limiter = require('../lib/limiter');

describe('Limiter', () => {
describe('#ctor', () => {
it('takes a `concurrency` argument', () => {
const limiter = new Limiter(0);

assert.strictEqual(limiter.concurrency, Infinity);
});
});

describe('#kRun', () => {
it('limits the number of jobs allowed to run concurrently', (done) => {
const limiter = new Limiter(1);

limiter.add((callback) => {
setImmediate(() => {
callback();

assert.strictEqual(limiter.jobs.length, 0);
assert.strictEqual(limiter.pending, 1);
});
});

limiter.add((callback) => {
setImmediate(() => {
callback();

assert.strictEqual(limiter.pending, 0);
done();
});
});

assert.strictEqual(limiter.jobs.length, 1);
});
});
});
2 changes: 1 addition & 1 deletion test/sender.test.js
Original file line number Diff line number Diff line change
@@ -154,7 +154,7 @@ describe('Sender', () => {

assert.strictEqual(chunks[0].length, 2);
assert.strictEqual(chunks[0][0] & 0x40, 0x40);
assert.strictEqual(chunks[1].length, 1);
assert.strictEqual(chunks[1].length, 5);

assert.strictEqual(chunks[2].length, 2);
assert.strictEqual(chunks[2][0] & 0x40, 0x00);