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.1.2
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.0
Choose a head ref
  • 7 commits
  • 8 files changed
  • 3 contributors

Commits on Aug 25, 2019

  1. 2

    Verified

    This commit was signed with the committer’s verified signature.
    Copy the full SHA
    5b7315f View commit details

Commits on Sep 15, 2019

  1. [fix] Ensure that concat() never returns uninitialized data (#1600)

    This is just a safeguard that ensures that for cases if `totalLength`
    is by some mistake miscalculated, no uninitialized memory would be
    leaked.
    ChALkeR authored and lpinca committed Sep 15, 2019
    Copy the full SHA
    08c6c8b View commit details

Commits on Oct 6, 2019

  1. Copy the full SHA
    96d8c8f View commit details

Commits on Oct 14, 2019

  1. Copy the full SHA
    2b7d84d View commit details

Commits on Oct 19, 2019

  1. Copy the full SHA
    a6c4238 View commit details
  2. [doc] Fix Travis CI URL

    lpinca committed Oct 19, 2019
    Copy the full SHA
    54e3011 View commit details
  3. [dist] 7.2.0

    lpinca committed Oct 19, 2019
    Copy the full SHA
    65f7800 View commit details
Showing with 40 additions and 12 deletions.
  1. +1 −1 README.md
  2. +15 −4 examples/express-session-parse/index.js
  3. +3 −1 lib/buffer-util.js
  4. +3 −2 lib/receiver.js
  5. +0 −1 lib/stream.js
  6. +1 −1 package.json
  7. +15 −0 test/buffer-util.test.js
  8. +2 −2 test/websocket.test.js
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)
[![Linux Build](https://img.shields.io/travis/websockets/ws/master.svg?logo=travis)](https://travis-ci.org/websockets/ws)
[![Linux Build](https://img.shields.io/travis/websockets/ws/master.svg?logo=travis)](https://travis-ci.com/websockets/ws)
[![Windows 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)

19 changes: 15 additions & 4 deletions examples/express-session-parse/index.js
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@ const uuid = require('uuid');
const WebSocket = require('../..');

const app = express();
const map = new Map();

//
// We need the same instance of the session parser in express and
@@ -37,8 +38,12 @@ app.post('/login', function(req, res) {
});

app.delete('/logout', function(request, response) {
const ws = map.get(request.session.userId);

console.log('Destroying session');
request.session.destroy(function() {
if (ws) ws.close();

response.send({ result: 'OK', message: 'Session destroyed' });
});
});
@@ -47,7 +52,7 @@ app.delete('/logout', function(request, response) {
// Create HTTP server by ourselves.
//
const server = http.createServer(app);
const wss = new WebSocket.Server({ noServer: true });
const wss = new WebSocket.Server({ clientTracking: false, noServer: true });

server.on('upgrade', function(request, socket, head) {
console.log('Parsing session from request...');
@@ -67,13 +72,19 @@ server.on('upgrade', function(request, socket, head) {
});

wss.on('connection', function(ws, request) {
const userId = request.session.userId;

map.set(userId, ws);

ws.on('message', function(message) {
//
// Here we can now use session parameters.
//
console.log(
`Received message ${message} from user ${request.session.userId}`
);
console.log(`Received message ${message} from user ${userId}`);
});

ws.on('close', function() {
map.delete(userId);
});
});

4 changes: 3 additions & 1 deletion lib/buffer-util.js
Original file line number Diff line number Diff line change
@@ -19,10 +19,12 @@ function concat(list, totalLength) {

for (let i = 0; i < list.length; i++) {
const buf = list[i];
buf.copy(target, offset);
target.set(buf, offset);
offset += buf.length;
}

if (offset < totalLength) return target.slice(0, offset);

return target;
}

5 changes: 3 additions & 2 deletions lib/receiver.js
Original file line number Diff line number Diff line change
@@ -96,11 +96,12 @@ class Receiver extends Writable {

do {
const buf = this._buffers[0];
const offset = dst.length - n;

if (n >= buf.length) {
this._buffers.shift().copy(dst, dst.length - n);
dst.set(this._buffers.shift(), offset);
} else {
buf.copy(dst, dst.length - n, 0, n);
dst.set(new Uint8Array(buf.buffer, buf.byteOffset, n), offset);
this._buffers[0] = buf.slice(n);
}

1 change: 0 additions & 1 deletion lib/stream.js
Original file line number Diff line number Diff line change
@@ -67,7 +67,6 @@ function createWebSocketStream(ws, options) {
autoDestroy: false,
emitClose: false,
objectMode: false,
readableObjectMode: false,
writableObjectMode: false
});

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": "7.1.2",
"version": "7.2.0",
"description": "Simple to use, blazing fast and thoroughly tested websocket client and server for Node.js",
"keywords": [
"HyBi",
15 changes: 15 additions & 0 deletions test/buffer-util.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';

const assert = require('assert');

const { concat } = require('../lib/buffer-util');

describe('bufferUtil', () => {
describe('concat', () => {
it('never returns uninitialized data', () => {
const buf = concat([Buffer.from([1, 2]), Buffer.from([3, 4])], 6);

assert.ok(buf.equals(Buffer.from([1, 2, 3, 4])));
});
});
});
4 changes: 2 additions & 2 deletions test/websocket.test.js
Original file line number Diff line number Diff line change
@@ -1381,7 +1381,7 @@ describe('WebSocket', () => {
ws.close();
ws.on('close', () => done());
});
});
}).timeout(4000);

it("can be called from a listener of the 'upgrade' event", (done) => {
const wss = new WebSocket.Server({ port: 0 }, () => {
@@ -1630,7 +1630,7 @@ describe('WebSocket', () => {
ws.terminate();
ws.on('close', () => done());
});
});
}).timeout(4000);

it("can be called from a listener of the 'upgrade' event", (done) => {
const wss = new WebSocket.Server({ port: 0 }, () => {