websocket-stream
Advanced tools
Comparing version 2.2.0 to 2.3.0
{ | ||
"name": "websocket-stream", | ||
"version": "2.2.0", | ||
"version": "2.3.0", | ||
"license": "BSD-2-Clause", | ||
@@ -5,0 +5,0 @@ "description": "Use websockets with the node streams API. Works in browser and node", |
@@ -21,2 +21,22 @@ # websocket-stream | ||
#### 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) | ||
##### 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 | ||
@@ -23,0 +43,0 @@ |
@@ -7,3 +7,3 @@ var through = require('through2') | ||
function WebSocketStream(target, protocols) { | ||
function WebSocketStream(target, protocols, options) { | ||
var stream, socket | ||
@@ -13,2 +13,15 @@ var socketWrite = process.title === 'browser' ? socketWriteBrowser : socketWriteNode | ||
if (protocols && !Array.isArray(protocols) && 'object' === typeof protocols) { | ||
// accept the "options" Object as the 2nd argument | ||
options = protocols | ||
protocols = null | ||
} | ||
if (!options) options = {} | ||
// 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 | ||
@@ -19,3 +32,3 @@ if (typeof target === 'object') { | ||
} else { | ||
socket = new WS(target, protocols) | ||
socket = new WS(target, protocols, options) | ||
socket.binaryType = 'arraybuffer' | ||
@@ -45,2 +58,7 @@ } | ||
function socketWriteBrowser(chunk, enc, next) { | ||
if (socket.bufferedAmount > bufferSize) { | ||
setTimeout(socketWriteBrowser, bufferTimeout, chunk, enc, next) | ||
return | ||
} | ||
try { | ||
@@ -47,0 +65,0 @@ socket.send(chunk) |
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
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
14599
339
71