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.5
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.3.0
Choose a head ref
  • 8 commits
  • 7 files changed
  • 2 contributors

Commits on Apr 25, 2020

  1. [doc] Fix badge URL

    lpinca committed Apr 25, 2020
    Copy the full SHA
    e694979 View commit details

Commits on May 2, 2020

  1. Copy the full SHA
    78e1c01 View commit details

Commits on May 7, 2020

  1. Revert "[ci] Update Coveralls GitHub Action to version 1.1.1"

    This reverts commit 78e1c01.
    lpinca committed May 7, 2020
    Copy the full SHA
    b167d1c View commit details
  2. Revert "[ci] Test on node 14"

    This reverts commit 73f8bb6.
    lpinca committed May 7, 2020
    Copy the full SHA
    82f0537 View commit details
  3. Revert "[ci] Use GitHub Actions (#1644)"

    This reverts commit 49ed889.
    lpinca committed May 7, 2020
    Copy the full SHA
    2b49c48 View commit details
  4. [ci] Test on node 14

    lpinca committed May 7, 2020
    Copy the full SHA
    97ddfce View commit details
  5. Verified

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

Commits on May 10, 2020

  1. [dist] 7.3.0

    lpinca committed May 10, 2020
    Copy the full SHA
    41f5e4f View commit details
Showing with 82 additions and 62 deletions.
  1. +0 −41 .github/workflows/ci.yml
  2. +13 −0 .travis.yml
  3. +1 −1 README.md
  4. +7 −3 doc/ws.md
  5. +22 −15 lib/event-target.js
  6. +3 −2 package.json
  7. +36 −0 test/websocket.test.js
41 changes: 0 additions & 41 deletions .github/workflows/ci.yml

This file was deleted.

13 changes: 13 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
language: node_js
node_js:
- '14'
- '13'
- '12'
- '10'
- '8'
os:
- linux
- osx
- windows
after_success:
- nyc report --reporter=text-lcov | coveralls
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# ws: a Node.js WebSocket library

[![Version npm](https://img.shields.io/npm/v/ws.svg?logo=npm)](https://www.npmjs.com/package/ws)
[![Build](https://github.com/websockets/ws/workflows/.github/workflows/build/badge.svg)](https://github.com/websockets/ws/actions?workflow=build)
[![Build](https://img.shields.io/travis/websockets/ws/master.svg?logo=travis)](https://travis-ci.com/websockets/ws)
[![Windows x86 Build](https://img.shields.io/appveyor/ci/lpinca/ws/master.svg?logo=appveyor)](https://ci.appveyor.com/project/lpinca/ws)
[![Coverage Status](https://img.shields.io/coveralls/websockets/ws/master.svg)](https://coveralls.io/github/websockets/ws)

10 changes: 7 additions & 3 deletions doc/ws.md
Original file line number Diff line number Diff line change
@@ -26,7 +26,7 @@
- [Event: 'pong'](#event-pong)
- [Event: 'unexpected-response'](#event-unexpected-response)
- [Event: 'upgrade'](#event-upgrade)
- [websocket.addEventListener(type, listener)](#websocketaddeventlistenertype-listener)
- [websocket.addEventListener(type, listener[, options])](#websocketaddeventlistenertype-listener-options)
- [websocket.binaryType](#websocketbinarytype)
- [websocket.bufferedAmount](#websocketbufferedamount)
- [websocket.close([code[, reason]])](#websocketclosecode-reason)
@@ -89,7 +89,7 @@ is provided with a single argument then that is:
- `secure` {Boolean} `true` if `req.connection.authorized` or
`req.connection.encrypted` is set.

The return value (Boolean) of the function determines whether or not to accept
The return value (`Boolean`) of the function determines whether or not to accept
the handshake.

if `verifyClient` is provided with two arguments then those are:
@@ -336,10 +336,14 @@ Emitted when response headers are received from the server as part of the
handshake. This allows you to read headers from the server, for example
'set-cookie' headers.

### websocket.addEventListener(type, listener)
### websocket.addEventListener(type, listener[, options])

- `type` {String} A string representing the event type to listen for.
- `listener` {Function} The listener to add.
- `options` {Object}
- `once` {Boolean} A `Boolean` indicating that the listener should be invoked
at most once after being added. If `true`, the listener would be
automatically removed when invoked.

Register an event listener emulating the `EventTarget` interface.

37 changes: 22 additions & 15 deletions lib/event-target.js
Original file line number Diff line number Diff line change
@@ -109,11 +109,16 @@ const EventTarget = {
/**
* Register an event listener.
*
* @param {String} method A string representing the event type to listen for
* @param {String} type A string representing the event type to listen for
* @param {Function} listener The listener to add
* @param {Object} options An options object specifies characteristics about
* the event listener
* @param {Boolean} options.once A `Boolean`` indicating that the listener
* should be invoked at most once after being added. If `true`, the
* listener would be automatically removed when invoked.
* @public
*/
addEventListener(method, listener) {
addEventListener(type, listener, options) {
if (typeof listener !== 'function') return;

function onMessage(data) {
@@ -132,36 +137,38 @@ const EventTarget = {
listener.call(this, new OpenEvent(this));
}

if (method === 'message') {
const method = options && options.once ? 'once' : 'on';

if (type === 'message') {
onMessage._listener = listener;
this.on(method, onMessage);
} else if (method === 'close') {
this[method](type, onMessage);
} else if (type === 'close') {
onClose._listener = listener;
this.on(method, onClose);
} else if (method === 'error') {
this[method](type, onClose);
} else if (type === 'error') {
onError._listener = listener;
this.on(method, onError);
} else if (method === 'open') {
this[method](type, onError);
} else if (type === 'open') {
onOpen._listener = listener;
this.on(method, onOpen);
this[method](type, onOpen);
} else {
this.on(method, listener);
this[method](type, listener);
}
},

/**
* Remove an event listener.
*
* @param {String} method A string representing the event type to remove
* @param {String} type A string representing the event type to remove
* @param {Function} listener The listener to remove
* @public
*/
removeEventListener(method, listener) {
const listeners = this.listeners(method);
removeEventListener(type, listener) {
const listeners = this.listeners(type);

for (let i = 0; i < listeners.length; i++) {
if (listeners[i] === listener || listeners[i]._listener === listener) {
this.removeListener(method, listeners[i]);
this.removeListener(type, listeners[i]);
}
}
}
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ws",
"version": "7.2.5",
"version": "7.3.0",
"description": "Simple to use, blazing fast and thoroughly tested websocket client and server for Node.js",
"keywords": [
"HyBi",
@@ -26,7 +26,7 @@
"lib/*.js"
],
"scripts": {
"test": "npm run lint && nyc --reporter=lcov --reporter=text mocha --throw-deprecation test/*.test.js",
"test": "npm run lint && nyc --reporter=html --reporter=text mocha --throw-deprecation test/*.test.js",
"integration": "npm run lint && mocha --throw-deprecation test/*.integration.js",
"lint": "eslint --ignore-path .gitignore . && prettier --check --ignore-path .gitignore \"**/*.{json,md,yaml,yml}\""
},
@@ -45,6 +45,7 @@
"devDependencies": {
"benchmark": "^2.1.4",
"bufferutil": "^4.0.1",
"coveralls": "^3.0.3",
"eslint": "^6.0.0",
"eslint-config-prettier": "^6.0.0",
"eslint-plugin-prettier": "^3.0.1",
36 changes: 36 additions & 0 deletions test/websocket.test.js
Original file line number Diff line number Diff line change
@@ -1809,6 +1809,22 @@ describe('WebSocket', () => {
assert.strictEqual(ws.listeners('bar').length, 0);
});

it('allows to add one time listeners with `addEventListener`', (done) => {
const ws = new WebSocket('ws://localhost', { agent: new CustomAgent() });

ws.addEventListener(
'foo',
() => {
assert.strictEqual(ws.listenerCount('foo'), 0);
done();
},
{ once: true }
);

assert.strictEqual(ws.listenerCount('foo'), 1);
ws.emit('foo');
});

it('supports the `removeEventListener` method', () => {
const ws = new WebSocket('ws://localhost', { agent: new CustomAgent() });

@@ -1831,6 +1847,26 @@ describe('WebSocket', () => {
assert.strictEqual(ws.listenerCount('message'), 0);
assert.strictEqual(ws.listenerCount('open'), 0);
assert.strictEqual(ws.listenerCount('foo'), 0);

ws.addEventListener('message', NOOP, { once: true });
ws.addEventListener('open', NOOP, { once: true });
ws.addEventListener('foo', NOOP, { once: true });

assert.strictEqual(ws.listeners('message')[0]._listener, NOOP);
assert.strictEqual(ws.listeners('open')[0]._listener, NOOP);
assert.strictEqual(ws.listeners('foo')[0], NOOP);

ws.removeEventListener('message', () => {});

assert.strictEqual(ws.listeners('message')[0]._listener, NOOP);

ws.removeEventListener('message', NOOP);
ws.removeEventListener('open', NOOP);
ws.removeEventListener('foo', NOOP);

assert.strictEqual(ws.listenerCount('message'), 0);
assert.strictEqual(ws.listenerCount('open'), 0);
assert.strictEqual(ws.listenerCount('foo'), 0);
});

it('wraps text data in a `MessageEvent`', (done) => {