simple-websocket
Advanced tools
Comparing version 4.2.0 to 4.3.0
43
index.js
@@ -6,3 +6,5 @@ /* global WebSocket */ | ||
var debug = require('debug')('simple-websocket') | ||
var extend = require('xtend') | ||
var inherits = require('inherits') | ||
var randombytes = require('randombytes') | ||
var stream = require('readable-stream') | ||
@@ -24,7 +26,12 @@ var ws = require('ws') // websockets in node - will be empty object in browser | ||
if (!opts) opts = {} | ||
debug('new websocket: %s %o', url, opts) | ||
opts.allowHalfOpen = false | ||
if (opts.highWaterMark == null) opts.highWaterMark = 1024 * 1024 | ||
self._id = randombytes(4).toString('hex').slice(0, 7) | ||
self._debug('new websocket: %s %o', url, opts) | ||
// TODO: replace with Object.assign() when ready to drop IE11. | ||
opts = extend({}, { | ||
allowHalfOpen: false, | ||
highWaterMark: 1024 * 1024 | ||
}, opts) | ||
stream.Duplex.call(self, opts) | ||
@@ -98,6 +105,3 @@ | ||
var self = this | ||
var len = chunk.length || chunk.byteLength || chunk.size | ||
self._ws.send(chunk) | ||
debug('write: %d bytes', len) | ||
} | ||
@@ -115,5 +119,5 @@ | ||
debug('destroy (error: %s)', err && err.message) | ||
self._debug('destroy (error: %s)', err && err.message) | ||
this.readable = this.writable = false | ||
self.readable = self.writable = false | ||
@@ -170,3 +174,3 @@ if (!self._readableState.ended) self.push(null) | ||
if (typeof ws !== 'function' && self._ws.bufferedAmount > self._maxBufferedAmount) { | ||
debug('start backpressure: bufferedAmount %d', self._ws.bufferedAmount) | ||
self._debug('start backpressure: bufferedAmount %d', self._ws.bufferedAmount) | ||
self._cb = cb | ||
@@ -177,3 +181,3 @@ } else { | ||
} else { | ||
debug('write before connect') | ||
self._debug('write before connect') | ||
self._chunk = chunk | ||
@@ -188,4 +192,2 @@ self._cb = cb | ||
var data = event.data | ||
debug('read: %d bytes', data.byteLength || data.length) | ||
if (data instanceof ArrayBuffer) data = new Buffer(data) | ||
@@ -207,3 +209,3 @@ self.push(data) | ||
self._chunk = null | ||
debug('sent chunk from "write before connect"') | ||
self._debug('sent chunk from "write before connect"') | ||
@@ -222,3 +224,3 @@ var cb = self._cb | ||
} | ||
debug('ending backpressure: bufferedAmount %d', self._ws.bufferedAmount) | ||
self._debug('ending backpressure: bufferedAmount %d', self._ws.bufferedAmount) | ||
var cb = self._cb | ||
@@ -231,3 +233,3 @@ self._cb = null | ||
debug('connect') | ||
self._debug('connect') | ||
self.emit('connect') | ||
@@ -239,3 +241,3 @@ } | ||
if (self.destroyed) return | ||
debug('on close') | ||
self._debug('on close') | ||
self._destroy() | ||
@@ -247,4 +249,11 @@ } | ||
if (self.destroyed) return | ||
debug('error: %s', err.message || err) | ||
self._debug('error: %s', err.message || err) | ||
self._destroy(err) | ||
} | ||
Socket.prototype._debug = function () { | ||
var self = this | ||
var args = [].slice.call(arguments) | ||
args[0] = '[' + self._id + '] ' + args[0] | ||
debug.apply(null, args) | ||
} |
{ | ||
"name": "simple-websocket", | ||
"description": "Simple, EventEmitter API for WebSockets (browser)", | ||
"version": "4.2.0", | ||
"version": "4.3.0", | ||
"author": { | ||
@@ -20,9 +20,13 @@ "name": "Feross Aboukhadijeh", | ||
"readable-stream": "^2.0.5", | ||
"ws": "^1.0.0" | ||
"ws": "^2.0.0", | ||
"xtend": "^4.0.1" | ||
}, | ||
"devDependencies": { | ||
"browserify": "^13.0.0", | ||
"browserify": "^14.0.0", | ||
"prettier-bytes": "^1.0.3", | ||
"speedometer": "^1.0.0", | ||
"standard": "*", | ||
"tape": "^4.0.0", | ||
"uglify-js": "^2.4.15", | ||
"ws": "^1.1.1", | ||
"zuul": "^3.0.0" | ||
@@ -29,0 +33,0 @@ }, |
@@ -6,4 +6,22 @@ var Socket = require('../') | ||
test('detect WebSocket support', function (t) { | ||
t.equal(Socket.WEBSOCKET_SUPPORT, true, 'websocket support') | ||
t.end() | ||
}) | ||
test('create socket without options', function (t) { | ||
t.plan(1) | ||
var socket | ||
t.doesNotThrow(function () { | ||
socket = new Socket() | ||
}) | ||
socket.on('error', function (err) { | ||
t.ok(err instanceof Error, 'got error') | ||
}) | ||
socket.destroy() | ||
}) | ||
test('echo string', function (t) { | ||
t.plan(3) | ||
t.plan(4) | ||
@@ -15,2 +33,3 @@ var socket = new Socket(SOCKET_SERVER) | ||
socket.on('data', function (data) { | ||
t.ok(Buffer.isBuffer(data), 'data is Buffer') | ||
t.equal(data.toString(), 'sup!') | ||
@@ -25,3 +44,3 @@ | ||
test('echo Uint8Array', function (t) { | ||
test('echo Buffer', function (t) { | ||
t.plan(4) | ||
@@ -32,3 +51,3 @@ | ||
t.pass('connect emitted') | ||
socket.send(new Uint8Array([1, 2, 3])) | ||
socket.send(new Buffer([1, 2, 3])) | ||
socket.on('data', function (data) { | ||
@@ -45,3 +64,3 @@ t.ok(Buffer.isBuffer(data), 'data is Buffer') | ||
test('echo Buffer', function (t) { | ||
test('echo Uint8Array', function (t) { | ||
t.plan(4) | ||
@@ -52,4 +71,6 @@ | ||
t.pass('connect emitted') | ||
socket.send(new Buffer([1, 2, 3])) | ||
socket.send(new Uint8Array([1, 2, 3])) | ||
socket.on('data', function (data) { | ||
// binary types always get converted to Buffer | ||
// See: https://github.com/feross/simple-peer/issues/138#issuecomment-278240571 | ||
t.ok(Buffer.isBuffer(data), 'data is Buffer') | ||
@@ -82,6 +103,1 @@ t.deepEqual(data, new Buffer([1, 2, 3]), 'got correct data') | ||
}) | ||
test('Socket.WEBSOCKET_SUPPORT', function (t) { | ||
t.ok(Socket.WEBSOCKET_SUPPORT) | ||
t.end() | ||
}) |
Sorry, the diff of this file is too big to display
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
82527
11
644
5
8
+ Addedxtend@^4.0.1
+ Addedsafe-buffer@5.0.1(transitive)
+ Addedultron@1.1.1(transitive)
+ Addedws@2.3.1(transitive)
+ Addedxtend@4.0.2(transitive)
- Removedoptions@0.0.6(transitive)
- Removedultron@1.0.2(transitive)
- Removedws@1.1.5(transitive)
Updatedws@^2.0.0