Skip to content

Commit

Permalink
fixup! [feature] Add utility to wrap a WebSocket in a Duplex stream
Browse files Browse the repository at this point in the history
  • Loading branch information
lpinca committed Jun 17, 2019
1 parent 711f246 commit d499d8f
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 27 deletions.
3 changes: 1 addition & 2 deletions README.md
Expand Up @@ -308,13 +308,12 @@ ws.on('message', function incoming(data) {

```js
const WebSocket = require('ws');
const createWebSocketStream = require('ws/stream');

const ws = new WebSocket('wss://echo.websocket.org/', {
origin: 'https://websocket.org'
});

const duplex = createWebSocketStream(ws, { encoding: 'utf8' });
const duplex = WebSocket.createWebSocketStream(ws, { encoding: 'utf8' });

duplex.pipe(process.stdout);
process.stdin.pipe(duplex);
Expand Down
11 changes: 2 additions & 9 deletions doc/ws.md
Expand Up @@ -43,7 +43,7 @@
- [websocket.send(data[, options][, callback])](#websocketsenddata-options-callback)
- [websocket.terminate()](#websocketterminate)
- [websocket.url](#websocketurl)
- [createWebSocketStream(websocket[, options])](#createwebsocketstreamwebsocket-options)
- [WebSocket.createWebSocketStream(websocket[, options])](#websocketcreatewebsocketstreamwebsocket-options)

## Class: WebSocket.Server

Expand Down Expand Up @@ -464,7 +464,7 @@ Forcibly close the connection.

The URL of the WebSocket server. Server clients don't have this attribute.

## createWebSocketStream(websocket[, options])
## WebSocket.createWebSocketStream(websocket[, options])

- `websocket` {WebSocket} A `WebSocket` object.
- `options` {Object} [Options][duplex-options] to pass to the `Duplex`
Expand All @@ -473,13 +473,6 @@ The URL of the WebSocket server. Server clients don't have this attribute.
Returns a `Duplex` stream that allows to use the Node.js streams API on top of a
given `WebSocket`.

This function lives in its own module in the package root and can be required as
follows:

```js
const createWebSocketStream = require('ws/stream');
```

[concurrency-limit]: https://github.com/websockets/ws/issues/1202
[duplex-options]:
https://nodejs.org/api/stream.html#stream_new_stream_duplex_options
Expand Down
1 change: 1 addition & 0 deletions index.js
Expand Up @@ -2,6 +2,7 @@

const WebSocket = require('./lib/websocket');

WebSocket.createWebSocketStream = require('./lib/stream');
WebSocket.Server = require('./lib/websocket-server');
WebSocket.Receiver = require('./lib/receiver');
WebSocket.Sender = require('./lib/sender');
Expand Down
File renamed without changes.
31 changes: 15 additions & 16 deletions test/create-websocket-stream.test.js
Expand Up @@ -5,20 +5,19 @@ const EventEmitter = require('events');
const { Duplex } = require('stream');
const { randomBytes } = require('crypto');

const createWebSocketStream = require('../stream');
const WebSocket = require('..');

describe('createWebSocketStream', () => {
it('returns a `Duplex` stream', () => {
const duplex = createWebSocketStream(new EventEmitter());
const duplex = WebSocket.createWebSocketStream(new EventEmitter());

assert.ok(duplex instanceof Duplex);
});

it('passes the options object to the `Duplex` constructor', (done) => {
const wss = new WebSocket.Server({ port: 0 }, () => {
const ws = new WebSocket(`ws://localhost:${wss.address().port}`);
const duplex = createWebSocketStream(ws, {
const duplex = WebSocket.createWebSocketStream(ws, {
allowHalfOpen: false,
encoding: 'utf8'
});
Expand Down Expand Up @@ -46,7 +45,7 @@ describe('createWebSocketStream', () => {

assert.strictEqual(ws.readyState, 0);

const duplex = createWebSocketStream(ws);
const duplex = WebSocket.createWebSocketStream(ws);

duplex.write(chunk);
});
Expand All @@ -68,7 +67,7 @@ describe('createWebSocketStream', () => {
it('errors if a write occurs when `readyState` is `CLOSING`', (done) => {
const wss = new WebSocket.Server({ port: 0 }, () => {
const ws = new WebSocket(`ws://localhost:${wss.address().port}`);
const duplex = createWebSocketStream(ws);
const duplex = WebSocket.createWebSocketStream(ws);

duplex.on('error', (err) => {
assert.ok(duplex.destroyed);
Expand Down Expand Up @@ -98,7 +97,7 @@ describe('createWebSocketStream', () => {
it('errors if a write occurs when `readyState` is `CLOSED`', (done) => {
const wss = new WebSocket.Server({ port: 0 }, () => {
const ws = new WebSocket(`ws://localhost:${wss.address().port}`);
const duplex = createWebSocketStream(ws);
const duplex = WebSocket.createWebSocketStream(ws);

duplex.on('error', (err) => {
assert.ok(duplex.destroyed);
Expand Down Expand Up @@ -129,7 +128,7 @@ describe('createWebSocketStream', () => {

assert.strictEqual(ws.readyState, 0);

const duplex = createWebSocketStream(ws);
const duplex = WebSocket.createWebSocketStream(ws);

duplex.on('close', () => {
wss.close(done);
Expand All @@ -143,7 +142,7 @@ describe('createWebSocketStream', () => {
it('reemits errors', (done) => {
const wss = new WebSocket.Server({ port: 0 }, () => {
const ws = new WebSocket(`ws://localhost:${wss.address().port}`);
const duplex = createWebSocketStream(ws);
const duplex = WebSocket.createWebSocketStream(ws);

duplex.on('error', (err) => {
assert.ok(err instanceof RangeError);
Expand All @@ -166,7 +165,7 @@ describe('createWebSocketStream', () => {
it("does not suppress the throwing behavior of 'error' events", (done) => {
const wss = new WebSocket.Server({ port: 0 }, () => {
const ws = new WebSocket(`ws://localhost:${wss.address().port}`);
createWebSocketStream(ws);
WebSocket.createWebSocketStream(ws);
});

wss.on('connection', (ws) => {
Expand Down Expand Up @@ -194,7 +193,7 @@ describe('createWebSocketStream', () => {
const wss = new WebSocket.Server({ port: 0 }, () => {
const events = [];
const ws = new WebSocket(`ws://localhost:${wss.address().port}`);
const duplex = createWebSocketStream(ws);
const duplex = WebSocket.createWebSocketStream(ws);

duplex.on('end', () => {
events.push('end');
Expand Down Expand Up @@ -229,7 +228,7 @@ describe('createWebSocketStream', () => {
const wss = new WebSocket.Server({ port: 0 }, () => {
const events = [];
const ws = new WebSocket(`ws://localhost:${wss.address().port}`);
const duplex = createWebSocketStream(ws);
const duplex = WebSocket.createWebSocketStream(ws);

duplex.on('end', () => {
events.push('end');
Expand Down Expand Up @@ -264,7 +263,7 @@ describe('createWebSocketStream', () => {
});

wss.on('connection', (ws) => {
const duplex = createWebSocketStream(ws);
const duplex = WebSocket.createWebSocketStream(ws);

duplex.resume();

Expand All @@ -289,7 +288,7 @@ describe('createWebSocketStream', () => {
const wss = new WebSocket.Server({ port: 0 }, () => {
const called = [];
const ws = new WebSocket(`ws://localhost:${wss.address().port}`);
const duplex = createWebSocketStream(ws);
const duplex = WebSocket.createWebSocketStream(ws);
const read = duplex._read;

duplex._read = () => {
Expand Down Expand Up @@ -326,7 +325,7 @@ describe('createWebSocketStream', () => {
const wss = new WebSocket.Server({ port: 0 }, () => {
const called = [];
const ws = new WebSocket(`ws://localhost:${wss.address().port}`);
const duplex = createWebSocketStream(ws);
const duplex = WebSocket.createWebSocketStream(ws);

const read = duplex._read;

Expand Down Expand Up @@ -361,7 +360,7 @@ describe('createWebSocketStream', () => {
const wss = new WebSocket.Server({ port: 0 }, () => {
const error = new Error('Oops');
const ws = new WebSocket(`ws://localhost:${wss.address().port}`);
const duplex = createWebSocketStream(ws);
const duplex = WebSocket.createWebSocketStream(ws);

duplex.on('error', (err) => {
assert.strictEqual(err, error);
Expand All @@ -380,7 +379,7 @@ describe('createWebSocketStream', () => {
it('can be destroyed (2/2)', (done) => {
const wss = new WebSocket.Server({ port: 0 }, () => {
const ws = new WebSocket(`ws://localhost:${wss.address().port}`);
const duplex = createWebSocketStream(ws);
const duplex = WebSocket.createWebSocketStream(ws);

duplex.on('close', () => {
wss.close(done);
Expand Down

0 comments on commit d499d8f

Please sign in to comment.