From d499d8fed6ee7859e3ce6231fdd79fb1f13eeafa Mon Sep 17 00:00:00 2001 From: Luigi Pinca Date: Mon, 17 Jun 2019 16:26:02 +0200 Subject: [PATCH] fixup! [feature] Add utility to wrap a `WebSocket` in a `Duplex` stream --- README.md | 3 +-- doc/ws.md | 11 ++-------- index.js | 1 + stream.js => lib/stream.js | 0 test/create-websocket-stream.test.js | 31 ++++++++++++++-------------- 5 files changed, 19 insertions(+), 27 deletions(-) rename stream.js => lib/stream.js (100%) diff --git a/README.md b/README.md index a5002972d..3d63abc0c 100644 --- a/README.md +++ b/README.md @@ -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); diff --git a/doc/ws.md b/doc/ws.md index 81c75e3a8..77b3e42e0 100644 --- a/doc/ws.md +++ b/doc/ws.md @@ -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 @@ -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` @@ -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 diff --git a/index.js b/index.js index b8d6be1c9..722c78676 100644 --- a/index.js +++ b/index.js @@ -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'); diff --git a/stream.js b/lib/stream.js similarity index 100% rename from stream.js rename to lib/stream.js diff --git a/test/create-websocket-stream.test.js b/test/create-websocket-stream.test.js index 835ba98d9..e294862f5 100644 --- a/test/create-websocket-stream.test.js +++ b/test/create-websocket-stream.test.js @@ -5,12 +5,11 @@ 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); }); @@ -18,7 +17,7 @@ describe('createWebSocketStream', () => { 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' }); @@ -46,7 +45,7 @@ describe('createWebSocketStream', () => { assert.strictEqual(ws.readyState, 0); - const duplex = createWebSocketStream(ws); + const duplex = WebSocket.createWebSocketStream(ws); duplex.write(chunk); }); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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) => { @@ -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'); @@ -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'); @@ -264,7 +263,7 @@ describe('createWebSocketStream', () => { }); wss.on('connection', (ws) => { - const duplex = createWebSocketStream(ws); + const duplex = WebSocket.createWebSocketStream(ws); duplex.resume(); @@ -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 = () => { @@ -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; @@ -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); @@ -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);