websocket-stream
Advanced tools
Comparing version 1.2.0 to 1.3.0
@@ -1,2 +0,1 @@ | ||
var WebSocketServer = require('ws').Server | ||
var http = require('http') | ||
@@ -23,11 +22,9 @@ var websocket = require('./') | ||
var wss = new WebSocketServer(opts) | ||
websocket.createServer(opts, echo) | ||
wss.on('connection', function(ws) { | ||
var stream = websocket(ws) | ||
server.listen(port, cb) | ||
stream.pipe(stream) // echo | ||
}) | ||
server.listen(port, cb) | ||
function echo(stream) { | ||
stream.pipe(stream) | ||
} | ||
} | ||
@@ -34,0 +31,0 @@ |
72
index.js
@@ -1,70 +0,6 @@ | ||
var through = require('through2') | ||
var duplexify = require('duplexify') | ||
var WS = require('ws') | ||
module.exports = WebSocketStream | ||
var Server = require('./server.js') | ||
function WebSocketStream(target, protocols) { | ||
var stream, socket | ||
var proxy = through(socketWrite, socketEnd) | ||
// use existing WebSocket object that was passed in | ||
if (typeof target === 'object') { | ||
socket = target | ||
// otherwise make a new one | ||
} else { | ||
socket = new WS(target, protocols) | ||
socket.binaryType = 'arraybuffer' | ||
} | ||
// was already open when passed in | ||
if (socket.readyState === 1) { | ||
stream = proxy | ||
} else { | ||
stream = duplexify() | ||
socket.addEventListener("open", onready) | ||
} | ||
stream.socket = socket | ||
socket.addEventListener("close", onclose) | ||
socket.addEventListener("error", onerror) | ||
socket.addEventListener("message", onmessage) | ||
proxy.on('close', destroy) | ||
function socketWrite(chunk, enc, next) { | ||
socket.send(chunk, next) | ||
} | ||
function socketEnd(done) { | ||
socket.close() | ||
done() | ||
} | ||
function onready() { | ||
stream.setReadable(proxy) | ||
stream.setWritable(proxy) | ||
stream.emit('connect') | ||
} | ||
function onclose() { | ||
stream.destroy() | ||
} | ||
function onerror(err) { | ||
stream.destroy(err) | ||
} | ||
function onmessage(event) { | ||
var data = event.data | ||
if (data instanceof ArrayBuffer) data = new Buffer(new Uint8Array(data)) | ||
proxy.push(data) | ||
} | ||
function destroy() { | ||
socket.close() | ||
} | ||
return stream | ||
} | ||
module.exports = require('./stream.js') | ||
module.exports.Server = Server | ||
module.exports.createServer = Server |
{ | ||
"name": "websocket-stream", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "Use websockets with the node streams API. Works in browser and node", | ||
@@ -26,4 +26,5 @@ "scripts": { | ||
"duplexify": "^3.2.0", | ||
"inherits": "^2.0.1", | ||
"through2": "^0.6.1", | ||
"ws": "^0.4.32", | ||
"ws": "^0.6.5", | ||
"xtend": "^4.0.0" | ||
@@ -38,4 +39,5 @@ }, | ||
"browser": { | ||
"./echo-server.js": "./fake-server.js" | ||
"./echo-server.js": "./fake-server.js", | ||
"./index.js": "stream.js" | ||
} | ||
} |
@@ -17,3 +17,3 @@ # websocket-stream | ||
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). | ||
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). | ||
@@ -27,7 +27,6 @@ The underlying `WebSocket` instance is available as `ws.socket`. | ||
```javascript | ||
var WebSocketServer = require('ws').Server | ||
var websocket = require('websocket-stream') | ||
var wss = new WebSocketServer({server: someHTTPServer}) | ||
wss.on('connection', function(ws) { | ||
var stream = websocket(ws) | ||
var wss = websocket.createServer({server: someHTTPServer}, handle) | ||
function handle(stream) { | ||
fs.createReadStream('bigdata.json').pipe(stream) | ||
@@ -34,0 +33,0 @@ }) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
11184
12
267
5
51
+ Addedinherits@^2.0.1
+ Addednan@1.4.3(transitive)
+ Addedultron@1.0.2(transitive)
+ Addedws@0.6.5(transitive)
- Removedcommander@2.1.0(transitive)
- Removednan@1.0.0(transitive)
- Removedtinycolor@0.0.1(transitive)
- Removedws@0.4.32(transitive)
Updatedws@^0.6.5