websocket-stream
Advanced tools
Comparing version 0.1.0 to 0.2.0
28
index.js
@@ -11,2 +11,4 @@ var stream = require('stream') | ||
this.writable = true | ||
this._buffer = [] | ||
if (typeof server === "object") { | ||
@@ -18,2 +20,3 @@ this.ws = server | ||
this.ws.on('open', this.onOpen.bind(this)) | ||
if (this.ws.readyState === 1) this._open = true | ||
} else { | ||
@@ -44,2 +47,3 @@ this.ws = new WebSocket(server, this.options.protocol) | ||
WebsocketStream.prototype.onClose = function(err) { | ||
if (this._destroy) return | ||
this.emit('end') | ||
@@ -49,7 +53,22 @@ } | ||
WebsocketStream.prototype.onOpen = function(err) { | ||
if (this._destroy) return | ||
this._open = true | ||
for (var i = 0; i < this._buffer.length; i++) { | ||
this._write(this._buffer[i]) | ||
} | ||
this._buffer = undefined | ||
this.emit('open') | ||
this.emit('connect') | ||
if (this._end) this.ws.close() | ||
} | ||
WebsocketStream.prototype.write = function(data) { | ||
if (!this._open) { | ||
this._buffer.push(data) | ||
} else { | ||
this._write(data) | ||
} | ||
} | ||
WebsocketStream.prototype._write = function(data) { | ||
typeof WebSocket != 'undefined' && this.ws instanceof WebSocket | ||
@@ -60,4 +79,11 @@ ? this.ws.send(data) | ||
WebsocketStream.prototype.end = function() { | ||
WebsocketStream.prototype.end = function(data) { | ||
if (data !== undefined) this.write(data) | ||
if (this._open) this.ws.close() | ||
this._end = true | ||
} | ||
WebsocketStream.prototype.destroy = function() { | ||
this._destroy = true | ||
this.ws.close() | ||
} |
{ | ||
"name": "websocket-stream", | ||
"version": "0.2.0", | ||
"description": "websockets with the node stream api. works in browser and node", | ||
@@ -14,3 +15,2 @@ "scripts": { | ||
], | ||
"version": "0.1.0", | ||
"_npmUser": { | ||
@@ -17,0 +17,0 @@ "name": "maxogden", |
@@ -19,2 +19,10 @@ # websocket-stream | ||
### browserify steps | ||
```javascript | ||
npm install -g browserify // install browserify | ||
cd node_modules/websocket-stream | ||
npm install . // install dev dependencies | ||
browserify index.js -s websocket-stream > websocket-stream.js // require websocket-stream.js in your client-side app | ||
``` | ||
# on the server | ||
@@ -21,0 +29,0 @@ |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
68
3
6784
8
107
3