Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

websocket-stream

Package Overview
Dependencies
Maintainers
5
Versions
54
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

websocket-stream - npm Package Compare versions

Comparing version 3.3.3 to 4.0.0

2

echo-server.js

@@ -0,1 +1,3 @@

'use strict'
var http = require('http')

@@ -2,0 +4,0 @@ var websocket = require('./')

6

index.js
var Server = require('./server.js')
var server = require('./server.js')
module.exports = require('./stream.js')
module.exports.Server = Server
module.exports.createServer = Server
module.exports.Server = server.Server
module.exports.createServer = server.createServer
{
"name": "websocket-stream",
"version": "3.3.3",
"version": "4.0.0",
"license": "BSD-2-Clause",

@@ -28,4 +28,5 @@ "description": "Use websockets with the node streams API. Works in browser and node",

"inherits": "^2.0.1",
"through2": "^2.0.0",
"ws": "^1.0.1",
"readable-stream": "^2.2.0",
"safe-buffer": "^5.0.1",
"ws": "^2.2.3",
"xtend": "^4.0.0"

@@ -35,5 +36,5 @@ },

"beefy": "^2.1.1",
"browserify": "^5.11.1",
"browserify": "^14.0.0",
"concat-stream": "^1.4.7",
"tape": "^2.14.0"
"tape": "^4.6.0"
},

@@ -40,0 +41,0 @@ "optionalDependencies": {},

@@ -37,8 +37,33 @@ # websocket-stream

##### `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`
Example:
```js
var websocket = require('websocket-stream')
var ws = websocket('ws://realtimecats.com', {
perMessageDeflate: false
})
```
##### Other options

@@ -61,2 +86,18 @@

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)
}
```
## Run the tests

@@ -63,0 +104,0 @@

@@ -0,31 +1,29 @@

'use strict'
var inherits = require('inherits')
var WebSocketServer = require('ws').Server
var stream = require('./stream')
module.exports = Server
class Server extends WebSocketServer{
constructor(opts, cb) {
super(opts)
function Server(opts, cb) {
if (!(this instanceof Server)) {
return new Server(opts, cb)
}
var proxied = false
this.on('newListener', function(event) {
if (!proxied && event === 'stream') {
proxied = true
this.on('connection', function(conn) {
this.emit('stream', stream(conn, opts))
})
}
})
WebSocketServer.call(this, opts)
var proxied = false
this.on('newListener', function(event) {
if (!proxied && event === 'stream') {
proxied = true
this.on('connection', function(conn) {
this.emit('stream', stream(conn, opts))
})
if (cb) {
this.on('stream', cb)
}
})
if (cb) {
this.on('stream', cb)
}
}
inherits(Server, WebSocketServer)
module.exports.Server = Server
module.exports.createServer = function(opts, cb) {
return new Server(opts, cb)
}
'use strict'
var through = require('through2')
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._destroyed = false
proxy._write = socketWrite
proxy._flush = socketEnd
proxy.destroy = function(err) {
if (this._destroyed) return
this._destroyed = true
var self = this
process.nextTick(function() {
if (err)
self.emit('error', err)
self.emit('close')
})
}
return proxy
}
function WebSocketStream(target, protocols, options) {

@@ -15,3 +40,2 @@ var stream, socket

var socketWrite = isBrowser ? socketWriteBrowser : socketWriteNode
var proxy = through.obj(socketWrite, socketEnd)

@@ -30,2 +54,12 @@ if (protocols && !Array.isArray(protocols) && 'object' === typeof protocols) {

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

@@ -69,5 +103,12 @@ var bufferSize = options.browserBufferSize || 1024 * 512

var coerceToBuffer = options.binary || options.binary === undefined
var coerceToBuffer = !options.objectMode
function socketWriteNode(chunk, enc, next) {
// avoid errors, this never happens unless
// destroy() is called
if (socket.readyState !== WS.OPEN) {
next()
return
}
if (coerceToBuffer && typeof chunk === 'string') {

@@ -120,4 +161,4 @@ chunk = new Buffer(chunk, 'utf8')

var data = event.data
if (data instanceof ArrayBuffer) data = new Buffer(new Uint8Array(data))
else data = new Buffer(data)
if (data instanceof ArrayBuffer) data = Buffer.from(new Uint8Array(data))
else data = Buffer.from(data, 'utf8')
proxy.push(data)

@@ -130,3 +171,17 @@ }

// 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
}
var ws = require('./')
var test = require('tape')
var Buffer = require('safe-buffer').Buffer

@@ -12,3 +13,3 @@ test('echo works', function(t) {

})
stream.write(new Buffer('hello'))
stream.write(Buffer.from('hello'))
})

@@ -20,3 +21,3 @@

t.equal(o.toString(), 'hello', 'got first hello back')
stream.write(new Buffer('hello'))
stream.write(Buffer.from('hello'))
stream.once('data', function(o) {

@@ -28,3 +29,3 @@ t.equal(o.toString(), 'hello', 'got second hello back')

})
stream.write(new Buffer('hello'))
stream.write(Buffer.from('hello'))
})

@@ -31,0 +32,0 @@

@@ -7,2 +7,3 @@ var test = require('tape')

var concat = require('concat-stream')
var Buffer = require('safe-buffer').Buffer

@@ -12,3 +13,3 @@ test('echo server', function(t) {

echo.start(function() {
var client = websocket(echo.url, echo.options)
var client = websocket(echo.url)

@@ -34,3 +35,3 @@ client.on('error', console.error)

echo.start(function() {
var client = websocket(echo.url, echo.options)
var client = websocket(echo.url)

@@ -153,3 +154,3 @@ client.on('error', function() {

})
client.write(new Buffer('hello'))
client.write(Buffer.from('hello'))
}

@@ -232,1 +233,77 @@

})
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()
})
})
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc