pax_global_header00006660000000000000000000000064134521046200014507gustar00rootroot0000000000000052 comment=8d75e55b8b0bb0f10ca539aacc3b5dadae1271ea websocket-stream-5.4.0/000077500000000000000000000000001345210462000147745ustar00rootroot00000000000000websocket-stream-5.4.0/.gitignore000066400000000000000000000000411345210462000167570ustar00rootroot00000000000000node_modules .DS_Store yarn.lock websocket-stream-5.4.0/LICENSE000066400000000000000000000023771345210462000160120ustar00rootroot00000000000000Copyright (c) 2013, Max Ogden All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.websocket-stream-5.4.0/collaborators.md000066400000000000000000000014121345210462000201620ustar00rootroot00000000000000## Collaborators websocket-stream is only possible due to the excellent work of the following collaborators:
gstGitHub/gst
maxogdenGitHub/maxogden
deanlandoltGitHub/deanlandolt
mcollinaGitHub/mcollina
jnordbergGitHub/jnordberg
mafintoshGitHub/mafintosh
websocket-stream-5.4.0/echo-server.js000066400000000000000000000015711345210462000175600ustar00rootroot00000000000000'use strict' var http = require('http') var websocket = require('./') var server = null var port = module.exports.port = 8343 var url = module.exports.url = 'ws://localhost:' + module.exports.port module.exports.start = function(opts, cb) { if (server) { cb(new Error('already started')); return; } if (typeof opts == 'function') { cb = opts; opts = {}; } server = http.createServer() opts.server = server websocket.createServer(opts, echo) server.listen(port, cb) function echo(stream) { stream.pipe(stream) } } module.exports.stop = function(cb) { if (!server) { cb(new Error('not started')) return } server.close(cb) server = null } if (!module.parent) { module.exports.start(function(err) { if (err) { console.error(err); return; } console.log('Echo server started on port ' + port); }); } websocket-stream-5.4.0/index.js000066400000000000000000000002461345210462000164430ustar00rootroot00000000000000 var server = require('./server.js') module.exports = require('./stream.js') module.exports.Server = server.Server module.exports.createServer = server.createServer websocket-stream-5.4.0/package.json000066400000000000000000000022621345210462000172640ustar00rootroot00000000000000{ "name": "websocket-stream", "version": "5.4.0", "license": "BSD-2-Clause", "description": "Use websockets with the node streams API. Works in browser and node", "scripts": { "test": "node test.js", "start": "beefy test-client.js" }, "repository": { "type": "git", "url": "git+ssh://git@github.com/maxogden/websocket-stream.git" }, "keywords": [ "websocket", "websockets", "stream", "streams", "realtime" ], "_npmUser": { "name": "maxogden", "email": "max@maxogden.com" }, "dependencies": { "duplexify": "^3.5.1", "inherits": "^2.0.1", "readable-stream": "^2.3.3", "safe-buffer": "^5.1.2", "ws": "^3.2.0", "xtend": "^4.0.0" }, "devDependencies": { "beefy": "^2.1.8", "browserify": "^16.2.3", "concat-stream": "^1.6.2", "tape": "^4.9.1" }, "optionalDependencies": {}, "browser": { "./echo-server.js": "./fake-server.js", "./index.js": "./stream.js", "ws": "./ws-fallback.js" }, "bugs": { "url": "https://github.com/maxogden/websocket-stream/issues" }, "homepage": "https://github.com/maxogden/websocket-stream#readme", "main": "index.js", "author": "" } websocket-stream-5.4.0/readme.md000066400000000000000000000101311345210462000165470ustar00rootroot00000000000000# websocket-stream [![NPM](https://nodei.co/npm/websocket-stream.png?global=true)](https://nodei.co/npm/websocket-stream/) Use HTML5 [websockets](https://developer.mozilla.org/en-US/docs/WebSockets) using the Node Streams API. ### Usage This module works in Node or in Browsers that support WebSockets. You can use [browserify](http://github.com/substack/node-browserify) to package this module for browser use. ```javascript var websocket = require('websocket-stream') var ws = websocket('ws://echo.websocket.org') process.stdin.pipe(ws) ws.pipe(process.stdout) ``` In the example above `ws` is a duplex stream. That means you can pipe output to anything that accepts streams. You can also pipe data into streams (such as a webcam feed or audio data). The underlying `WebSocket` instance is available as `ws.socket`. #### Options The available options differs depending on if you use this module in the browser or with node.js. Options can be passed in as the third or second argument - `WebSocket(address, [protocols], [options])`. ##### `options.browserBufferSize` How much to allow the [socket.bufferedAmount](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket#Attributes) to grow before starting to throttle writes. This option has no effect in node.js. Default: `1024 * 512` (512KiB) ##### `options.browserBufferTimeout` How long to wait before checking if the socket buffer has drained sufficently for another write. This option has no effect in node.js. Default: `1000` (1 second) ##### `options.objectMode` Send each chunk on its own, and do not try to pack them in a single websocket frame. Default: `false` ##### `options.binary` Always convert to `Buffer` in Node.js before sending. Forces `options.objectMode` to `false`. Default: `true` ##### `options.perMessageDeflate` We recommend disabling the [per message deflate extension](https://tools.ietf.org/html/rfc7692) to achieve the best throughput. Default: `true` on the client, `false` on the server. Example: ```js var websocket = require('websocket-stream') var ws = websocket('ws://realtimecats.com', { perMessageDeflate: false }) ``` Beware that this option is ignored by browser clients. To make sure that permessage-deflate is never used, disable it on the server. ##### Other options When used in node.js see the [ws.WebSocket documentation](https://github.com/websockets/ws/blob/master/doc/ws.md#class-wswebsocket) ### On the server Using the [`ws`](http://npmjs.org/ws) module you can make a websocket server and use this module to get websocket streams on the server: ```javascript var websocket = require('websocket-stream') var wss = websocket.createServer({server: someHTTPServer}, handle) function handle(stream, request) { // `request` is the upgrade request sent by the client. fs.createReadStream('bigdata.json').pipe(stream) } ``` We recommend disabling the [per message deflate extension](https://tools.ietf.org/html/rfc7692) to achieve the best throughput: ```javascript var websocket = require('websocket-stream') var wss = websocket.createServer({ perMessageDeflate: false, server: someHTTPServer }, handle) function handle(stream) { fs.createReadStream('bigdata.json').pipe(stream) } ``` You can even use it on express.js with the [express-ws](https://www.npmjs.com/package/express-ws) library: ```js const express = require('express'); const expressWebSocket = require('express-ws'); const websocketStream = require('websocket-stream/stream'); const app = express(); // extend express app with app.ws() expressWebSocket(app, null, { // ws options here perMessageDeflate: false, }); app.ws('/bigdata.json', function(ws, req) { // convert ws instance to stream const stream = websocketStream(ws, { // websocket-stream options here binary: true, }); fs.createReadStream('bigdata.json').pipe(stream); }); app.listen(3000); ``` ## Run the tests ### Server-side tests ``` npm test ``` ### Client-side tests First start the echo server by running `node test-server.js` Then run `npm start` and open `localhost:9966` in your browser and open the Dev Tools console to see test output. ## license BSD LICENSE websocket-stream-5.4.0/server.js000066400000000000000000000011431345210462000166370ustar00rootroot00000000000000'use strict' var WebSocketServer = require('ws').Server var stream = require('./stream') class Server extends WebSocketServer{ constructor(opts, cb) { super(opts) var proxied = false this.on('newListener', function(event) { if (!proxied && event === 'stream') { proxied = true this.on('connection', function(conn, req) { this.emit('stream', stream(conn, opts), req) }) } }) if (cb) { this.on('stream', cb) } } } module.exports.Server = Server module.exports.createServer = function(opts, cb) { return new Server(opts, cb) } websocket-stream-5.4.0/stream.js000066400000000000000000000077661345210462000166450ustar00rootroot00000000000000'use strict' var Transform = require('readable-stream').Transform var duplexify = require('duplexify') var WS = require('ws') var Buffer = require('safe-buffer').Buffer module.exports = WebSocketStream function buildProxy (options, socketWrite, socketEnd) { var proxy = new Transform({ objectMode: options.objectMode }) proxy._write = socketWrite proxy._flush = socketEnd return proxy } function WebSocketStream(target, protocols, options) { var stream, socket var isBrowser = process.title === 'browser' var isNative = !!global.WebSocket var socketWrite = isBrowser ? socketWriteBrowser : socketWriteNode if (protocols && !Array.isArray(protocols) && 'object' === typeof protocols) { // accept the "options" Object as the 2nd argument options = protocols protocols = null if (typeof options.protocol === 'string' || Array.isArray(options.protocol)) { protocols = options.protocol; } } if (!options) options = {} if (options.objectMode === undefined) { options.objectMode = !(options.binary === true || options.binary === undefined) } var proxy = buildProxy(options, socketWrite, socketEnd) if (!options.objectMode) { proxy._writev = writev } // browser only: sets the maximum socket buffer size before throttling var bufferSize = options.browserBufferSize || 1024 * 512 // browser only: how long to wait when throttling var bufferTimeout = options.browserBufferTimeout || 1000 // use existing WebSocket object that was passed in if (typeof target === 'object') { socket = target // otherwise make a new one } else { // special constructor treatment for native websockets in browsers, see // https://github.com/maxogden/websocket-stream/issues/82 if (isNative && isBrowser) { socket = new WS(target, protocols) } else { socket = new WS(target, protocols, options) } socket.binaryType = 'arraybuffer' } // was already open when passed in if (socket.readyState === socket.OPEN) { stream = proxy } else { stream = stream = duplexify(undefined, undefined, options) if (!options.objectMode) { stream._writev = writev } socket.onopen = onopen } stream.socket = socket socket.onclose = onclose socket.onerror = onerror socket.onmessage = onmessage proxy.on('close', destroy) var coerceToBuffer = !options.objectMode function socketWriteNode(chunk, enc, next) { // avoid errors, this never happens unless // destroy() is called if (socket.readyState !== socket.OPEN) { next() return } if (coerceToBuffer && typeof chunk === 'string') { chunk = Buffer.from(chunk, 'utf8') } socket.send(chunk, next) } function socketWriteBrowser(chunk, enc, next) { if (socket.bufferedAmount > bufferSize) { setTimeout(socketWriteBrowser, bufferTimeout, chunk, enc, next) return } if (coerceToBuffer && typeof chunk === 'string') { chunk = Buffer.from(chunk, 'utf8') } try { socket.send(chunk) } catch(err) { return next(err) } next() } function socketEnd(done) { socket.close() done() } function onopen() { stream.setReadable(proxy) stream.setWritable(proxy) stream.emit('connect') } function onclose() { stream.end() stream.destroy() } function onerror(err) { stream.destroy(err) } function onmessage(event) { var data = event.data if (data instanceof ArrayBuffer) data = Buffer.from(data) else data = Buffer.from(data, 'utf8') proxy.push(data) } function destroy() { socket.close() } // this is to be enabled only if objectMode is false function writev (chunks, cb) { var buffers = new Array(chunks.length) for (var i = 0; i < chunks.length; i++) { if (typeof chunks[i].chunk === 'string') { buffers[i] = Buffer.from(chunks[i], 'utf8') } else { buffers[i] = chunks[i].chunk } } this._write(Buffer.concat(buffers), 'binary', cb) } return stream } websocket-stream-5.4.0/test-client.js000066400000000000000000000036311345210462000175700ustar00rootroot00000000000000var ws = require('./') var test = require('tape') var Buffer = require('safe-buffer').Buffer test('echo works', function(t) { var stream = ws('ws://localhost:8343') stream.on('data', function(o) { t.ok(Buffer.isBuffer(o), 'is buffer') t.equal(o.toString(), 'hello', 'got hello back') stream.destroy() t.end() }) stream.write(Buffer.from('hello')) }) test('echo works two times', function(t) { var stream = ws('ws://localhost:8343') stream.once('data', function(o) { t.equal(o.toString(), 'hello', 'got first hello back') stream.write(Buffer.from('hello')) stream.once('data', function(o) { t.equal(o.toString(), 'hello', 'got second hello back') stream.destroy() t.end() }) }) stream.write(Buffer.from('hello')) }) test('with bare WebSocket, strings as strings', function (t) { var socket = new WebSocket('ws://localhost:8344') socket.onmessage = function (e) { var data = e.data t.ok(typeof data === 'string', 'data must be a string') socket.close() t.end() } }) test('with bare WebSocket, binary only', function (t) { var socket = new WebSocket('ws://localhost:8345') socket.onmessage = function (e) { var data = e.data t.notOk(typeof data === 'string', 'data must not be a string') socket.close() t.end() } }) test('coerce client data as binary', function(t) { var stream = ws('ws://localhost:8346', { binary: true }) stream.on('data', function(o) { t.ok(Buffer.isBuffer(o), 'is buffer') t.equal(o.toString(), 'success', 'success!') stream.destroy() t.end() }) stream.write('hello') }) test('cork logic test', function (t) { var stream = ws('ws://localhost:8343', { binary: true }) stream.on('data', function(o) { t.equal(o.toString(), 'hello', 'success!') stream.destroy() t.end() }) stream.cork() stream.write('he') stream.write('l') stream.write('lo') stream.uncork() })websocket-stream-5.4.0/test-server.js000066400000000000000000000017411345210462000176200ustar00rootroot00000000000000var http = require('http') var websocket = require('./') var echo = require('./echo-server.js') var WebSocketServer = require('ws').Server var Buffer = require('safe-buffer').Buffer echo.start(function(){ console.log('echo server is running') }) function forBare (opts) { var server = http.createServer() websocket.createServer({ server: server, binary: opts.binary }, sendString) server.listen(opts.port) function sendString (stream) { stream.write('hello world') } } forBare({ port: 8344, binary: false }) forBare({ port: 8345 }) function checkIfDataIsBinary () { var server = http.createServer() var wss = new WebSocketServer({ server: server }) server.listen(8346) wss.on('connection', waitFor) function waitFor (ws) { ws.on('message', function (data) { if (!Buffer.isBuffer(data)) { ws.send(Buffer.from('fail')) } else { ws.send(Buffer.from('success')) } }) } } checkIfDataIsBinary() websocket-stream-5.4.0/test.js000066400000000000000000000147371345210462000163250ustar00rootroot00000000000000var test = require('tape') var websocket = require('./') var echo = require("./echo-server") var WebSocketServer = require('ws').Server var http = require('http') var concat = require('concat-stream') var Buffer = require('safe-buffer').Buffer test('echo server', function(t) { echo.start(function() { var client = websocket(echo.url) client.on('error', console.error) client.on('data', function(data) { t.ok(Buffer.isBuffer(data), 'is a buffer') t.equal(data.toString(), 'hello world') client.end() echo.stop(function() { t.end() }) }) client.write('hello world') }) }) test('emitting not connected errors', function(t) { echo.start(function() { var client = websocket(echo.url) client.on('error', function() { echo.stop(function() { t.true(true, 'should emit error') t.end() }) }) client.once('data', function(data) { client.end() client.write('abcde') }) client.write('hello world') }) }) test('passes options to websocket constructor', function(t) { t.plan(3) opts = { verifyClient: function verifyClient(info) { t.equal(info.req.headers['x-custom-header'], 'Custom Value') return true } } echo.start(opts, function() { var options = {headers: {'x-custom-header': 'Custom Value'}} var client = websocket(echo.url, options) client.on('error', console.error) client.on('data', function(data) { t.ok(Buffer.isBuffer(data), 'is a buffer') t.equal(data.toString(), 'hello world') client.end() echo.stop(function() {}) }) client.write('hello world') }) }) test('destroy', function(t) { t.plan(1) echo.start(function() { var client = websocket(echo.url, echo.options) client.on('close', function() { echo.stop(function() { t.pass('destroyed') }) }) setTimeout(function() { client.destroy() }, 200) }) }) test('drain', function(t) { t.plan(1) echo.start(function() { var client = websocket(echo.url, echo.options) client.on('drain', function() { client.destroy() echo.stop(function() { t.pass('drained') }) }) // write until buffer is full while (client.write('foobar')) {} }) }) test('emit sending errors if the socket is closed by the other party', function(t) { var server = http.createServer() var wss = new WebSocketServer({ server: server }) server.listen(8344, function() { var client = websocket('ws://localhost:8344') wss.on('connection', function(ws) { var stream = websocket(ws) client.destroy() setTimeout(function() { stream.write('hello world') }, 50) stream.on('error', function(err) { t.ok(err, 'client errors') server.close(t.end.bind(t)) }) }) }) }) test('destroy client pipe should close server pipe', function(t) { t.plan(1) var clientDestroy = function() { var client = websocket(echo.url, echo.options) client.on('data', function(o) { client.destroy() }) client.write(Buffer.from('hello')) } var opts = {} var server = http.createServer() opts.server = server var wss = new WebSocketServer(opts) wss.on('connection', function(ws) { var stream = websocket(ws) stream.on('close', function() { server.close(function() { t.pass('close is called') }) }) stream.pipe(stream) }) server.listen(echo.port, clientDestroy) }) test('error on socket should forward it to pipe', function(t) { t.plan(1) var clientConnect = function() { websocket(echo.url, echo.options) } var opts = {} var server = http.createServer() opts.server = server var wss = new WebSocketServer(opts) wss.on('connection', function(ws) { var stream = websocket(ws) stream.on('error', function() { server.close(function() { t.pass('error is called') }) }) stream.socket.emit('error', new Error('Fake error')) }) server.listen(echo.port, clientConnect) }) test('stream end', function(t) { t.plan(1) var server = http.createServer() websocket.createServer({ server: server }, handle) function handle (stream) { stream.pipe(concat(function (body) { t.equal(body.toString(), 'pizza cats\n') server.close() })) } server.listen(0, function () { var w = websocket('ws://localhost:' + server.address().port) w.end('pizza cats\n') }) }) test('stream handlers should fire once per connection', function(t) { t.plan(2) var server = http.createServer() var wss = websocket.createServer({ server: server }, function() { server.close(function() { t.equal(m, 1) }) }) var m = 0 wss.on('stream', function(stream, request) { t.ok(request instanceof http.IncomingMessage) m++ }) server.listen(0, function() { var w = websocket('ws://localhost:' + server.address().port) w.end('pizza cats\n') }) }) test('client with writev', function(t) { var server = http.createServer() var str = '' var wss = websocket.createServer({ server: server }, function (stream) { stream.once('data', function(data) { t.ok(Buffer.isBuffer(data), 'is a buffer') t.equal(data.toString(), 'hello world') stream.once('data', function(data) { t.ok(Buffer.isBuffer(data), 'is a buffer') t.equal(data.toString(), str) stream.end() server.close() t.end() }) }) }) server.listen(8352, function () { var client = websocket('ws://localhost:8352', { objectMode: false }) client.on('error', console.error) client.once('connect', function () { client.cork() do { str += 'foobar' } while (client.write('foobar')) client.uncork() }) client.write('hello world') }) }) test('server with writev', function(t) { var server = http.createServer() var str = '' var wss = websocket.createServer({ server: server, objectMode: false }, function (stream) { stream.cork() do { str += 'foobar' } while (stream.write('foobar')) stream.uncork() }) server.listen(8352, function () { var client = websocket('ws://localhost:8352') client.on('error', console.error) client.once('data', function(data) { t.ok(Buffer.isBuffer(data), 'is a buffer') t.equal(data.toString(), str) client.end() server.close() t.end() }) }) }) test('stop echo', function(t) { echo.stop(function() { t.end() }) }) websocket-stream-5.4.0/ws-fallback.js000066400000000000000000000004011345210462000175130ustar00rootroot00000000000000 var ws = null if (typeof WebSocket !== 'undefined') { ws = WebSocket } else if (typeof MozWebSocket !== 'undefined') { ws = MozWebSocket } else if (typeof window !== 'undefined') { ws = window.WebSocket || window.MozWebSocket } module.exports = ws