From 356ddf5a23526f02c891e04d4ad83f0656921b25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Acosta?= Date: Thu, 21 Feb 2019 10:19:02 -0300 Subject: [PATCH 01/10] Added heroku support. --- README.md | 3 +++ app.js | 5 +++++ 2 files changed, 8 insertions(+) create mode 100644 app.js diff --git a/README.md b/README.md index b4f7257..fb2a1d3 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,9 @@ See https://github.com/mafintosh/signalhub now soyuka/signalhubws ``` +## Deploy with Heroku +[![Deploy](https://www.herokucdn.com/deploy/button.png)](https://heroku.com/deploy) + ## Available signalhubs (WSS): ``` diff --git a/app.js b/app.js new file mode 100644 index 0000000..7fe378b --- /dev/null +++ b/app.js @@ -0,0 +1,5 @@ +{ + "name": "signalhubws", + "description": "Signalhub implementation with websockets.", + "repository": "https://github.com/soyuka/signalhubws" +} From bb2130383347f0cb9df7ffbcd25d8d6343ccfe31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Acosta?= Date: Thu, 21 Feb 2019 10:21:28 -0300 Subject: [PATCH 02/10] Fixed heroku support. --- app.js | 5 ----- 1 file changed, 5 deletions(-) delete mode 100644 app.js diff --git a/app.js b/app.js deleted file mode 100644 index 7fe378b..0000000 --- a/app.js +++ /dev/null @@ -1,5 +0,0 @@ -{ - "name": "signalhubws", - "description": "Signalhub implementation with websockets.", - "repository": "https://github.com/soyuka/signalhubws" -} From d1ef851142d9a0e7c26e2a4e770eb9b5d61c4312 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Acosta?= Date: Thu, 21 Feb 2019 10:21:37 -0300 Subject: [PATCH 03/10] Fixed heroku support. --- app.json | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 app.json diff --git a/app.json b/app.json new file mode 100644 index 0000000..7fe378b --- /dev/null +++ b/app.json @@ -0,0 +1,5 @@ +{ + "name": "signalhubws", + "description": "Signalhub implementation with websockets.", + "repository": "https://github.com/soyuka/signalhubws" +} From 077f3bf2d8c7131dbe5c71e4c9fab46247401663 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Acosta?= Date: Thu, 28 Feb 2019 21:00:46 -0300 Subject: [PATCH 04/10] refactory signalhubws client to use sockette --- index.js | 313 ++++++++++++++++++++++++------------------ package-lock.json | 274 ++++++++++++++++-------------------- package.json | 6 +- tests/boilerplate.js | 14 +- tests/browser.test.js | 1 + 5 files changed, 315 insertions(+), 293 deletions(-) diff --git a/index.js b/index.js index 8d2d764..7b6a35a 100644 --- a/index.js +++ b/index.js @@ -1,182 +1,231 @@ -const events = require('events') +const { EventEmitter } = require('events') const through2 = require('through2') -const inherits = require('inherits') -const WebSocket = (typeof window !== 'undefined' && window.WebSocket) ? window.WebSocket : null - -function SignalhubWs (app, urls, WebSocketClass) { - this.opened = false - this.sockets = [] - this.app = app - const channels = this.channels = new Map() - this.subscribers = { - get length () { - return channels.size - } - } +const Sockette = require('sockette') +const WebSocket = window.WebSocket +const noop = () => {} - if (!Array.isArray(urls)) { - urls = [urls] - } +class SignalhubWs extends EventEmitter { + constructor (app, urls, opts = {}) { + super() - urls = urls.map(function (url) { - url = url.replace(/\/$/, '') - return url.indexOf('://') === -1 ? 'ws://' + url : url - }) + this.sockets = [] - let countOpen = 0 + this.app = app - for (let index = 0; index < urls.length; index++) { - const socket = new WebSocketClass(`${urls[index]}/${app}`) + const channels = this.channels = new Map() - this.sockets.push(socket) + this._ready = false - socket.addEventListener('open', () => { - if (++countOpen === urls.length) { - this.opened = true - this.emit('open') - for (let channel of this.channels.values()) { - channel.emit('open') - } + this.subscribers = { + get length () { + return channels.size } - }) - - socket.addEventListener('message', (message) => { - this.onMessage(message) - }) - } -} + } -inherits(SignalhubWs, events.EventEmitter) + if (!Array.isArray(urls)) { + urls = [urls] + } -SignalhubWs.prototype.subscribe = function (channel) { - if (this.closed) { - throw new Error('Cannot subscribe after close') - } + urls = urls.map(function (url) { + url = url.replace(/\/$/, '') + return url.indexOf('://') === -1 ? 'ws://' + url : url + }) - if (this.channels.has(channel)) { - return this.channels.get(channel) - } + for (let url of urls) { + const id = `${url}/${app}` + + const socket = new Sockette(id, Object.assign({}, + opts.sockette || {}, + { + onopen: e => { + this._onOpen(e, socket) + }, + onmessage: e => { + this._onMessage(e, socket) + }, + onclose: e => { + this._onClose(e, socket) + }, + onerror: e => { + this._onError(e, socket) + }, + onreconnect: () => { + console.log(`Reconnecting to: ${id}`) + } + } + )) - // use a stream for channel - this.channels.set(channel, through2.obj()) + socket.id = id - this.channels.get(channel).on('close', () => { - this.channels.delete(channel) - }) + this.sockets.push(socket) + } - if (this.opened) { - process.nextTick(() => { - if (this.channels.has(channel)) { - this.channels.get(channel).emit('open') + let ready = this.sockets.length + this.on('socket:ready', socket => { + ready-- + if (ready === 0) { + this._ready = true + this.emit('ready') } }) } - return this.channels.get(channel) -} - -SignalhubWs.prototype.broadcast = function (channel, message, cb) { - if (this.closed) { - throw new Error('Cannot broadcast after close') - } + get opened () { + if (!this._ready) { + return false + } - const data = { - app: this.app, - channel: channel, - message: message + return !!this.sockets.find(socket => socket.ws.readyState === WebSocket.OPEN) } - this.sockets.forEach((socket) => { - socket.send(JSON.stringify(data)) - }) - - cb && cb() -} + get closed () { + if (!this._ready) { + return false + } -SignalhubWs.prototype.onMessage = function (message) { - message = JSON.parse(message.data) + return this.sockets.filter(socket => socket.ws.readyState === WebSocket.CLOSED).length === this.sockets.length + } - for (let key of this.channels.keys()) { - if (message.channel === key) { - this.channels.get(key).write(message.message) - continue + ready (cb) { + if (this._ready) { + return process.nextTick(cb) } - if (!Array.isArray(key)) { - continue + this.once('ready', cb) + } + + subscribe (channel) { + if (this.channels.has(channel)) { + return this.channels.get(channel) } - for (let i = 0; i < key.length; i++) { - if (key[i] === message.channel) { - this.channels.get(key).write(message.message) + // use a stream for channel + const stream = through2.obj( + (chunk, enc, cb) => cb(null, chunk), + (cb) => { + this.channels.delete(channel) + this.emit('channel:close', channel) + cb() } + ) + + this.channels.set(channel, stream) + + if (this.opened) { + process.nextTick(() => { + if (this.channels.has(channel)) { + this._openChannel(this.channels.get(channel)) + } + }) } - } -} -SignalhubWs.prototype.close = function (cb) { - if (this.closed) { - if (cb) process.nextTick(cb) - return + return this.channels.get(channel) } - this.once('close:socket', () => { - this._closeChannels(cb) - }) + broadcast (channel, message, cb) { + this.ready(() => { + const data = { + app: this.app, + channel: channel, + message: message + } + + this.sockets.forEach(socket => { + socket.send(JSON.stringify(data)) + }) - const len = this.sockets.length - if (len === 0) { - this.emit('close') - return + cb && cb() + }) } - let closed = 0 - this.sockets.forEach((socket) => { - socket.addEventListener('close', () => { - if (++closed === len) { - this.emit('close:socket') + close (cb = noop) { + this.ready(() => { + if (this.closed) { + this._closeChannels(cb) + return } - }) - process.nextTick(function () { - socket.close() + const onSocketClose = () => { + if (this.closed) { + this.removeListener('socket:close', onSocketClose) + this._closeChannels(cb) + } + } + this.on('socket:close', onSocketClose) + + this.sockets.forEach(socket => { + process.nextTick(() => socket.close()) + }) }) - }) -} + } -SignalhubWs.prototype._closeChannels = function (cb) { - if (this.closed) { - if (cb) process.nextTick(cb) - return + _onOpen (event, socket) { + this._checkInitializeWs(event, socket) + this.emit('socket:open', socket) + for (let channel of this.channels.values()) { + this._openChannel(channel) + } } - this.closed = true - if (cb) { - this.on('close', cb) + _onClose (event, socket) { + this._checkInitializeWs(event, socket) + this.emit('socket:close', socket) } - const len = this.channels.size - if (len === 0) { - this.emit('close') - return + _onError (event, socket) { + this._checkInitializeWs(event, socket) + this.emit('socket:error', socket) } - let closed = 0 - for (let channel of this.channels.values()) { - process.nextTick(() => { - channel.end(() => { - if (++closed === len) { - this.channels.clear() - this.emit('close') + _onMessage (message) { + message = JSON.parse(message.data) + + for (let key of this.channels.keys()) { + if (message.channel === key) { + this.channels.get(key).write(message.message) + continue + } + + if (!Array.isArray(key)) { + continue + } + + for (let i = 0; i < key.length; i++) { + if (key[i] === message.channel) { + this.channels.get(key).write(message.message) } - }) - }) + } + } } -} -module.exports = function (app, urls, WebSocketClass = WebSocket) { - if (!WebSocketClass) { - throw TypeError('No WebSocket class given.') + _checkInitializeWs (event, socket) { + if (!socket.ws) { + socket.ws = event.target + this.emit('socket:ready', socket) + } + } + + _openChannel (channel) { + if (!channel.opened) { + channel.opened = true + channel.emit('open') + } + } + + _closeChannels (cb) { + const onChannelClose = () => { + if (this.channels.size === 0) { + this.removeListener('channel:close', onChannelClose) + cb() + } + } + + this.on('channel:close', onChannelClose) + + for (let channel of this.channels.values()) { + process.nextTick(() => channel.end()) + } } - return new SignalhubWs(app, urls, WebSocketClass) } + +module.exports = (...args) => new SignalhubWs(...args) diff --git a/package-lock.json b/package-lock.json index 935cb39..95ed4d6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,18 +1,18 @@ { "name": "signalhubws", - "version": "1.0.7", + "version": "1.0.10", "lockfileVersion": 1, "requires": true, "dependencies": { "@clusterws/cws": { - "version": "0.11.0", - "resolved": "https://registry.npmjs.org/@clusterws/cws/-/cws-0.11.0.tgz", - "integrity": "sha512-vatQ88J2C5D7lNlDUtb7PG7IjZU/k6ZpUCAx/WQQbwC0mb22Ic9Yja3rwXF+M2QUa4uEhKQBNRGjF71y9gPkcA==" + "version": "0.11.1", + "resolved": "https://registry.npmjs.org/@clusterws/cws/-/cws-0.11.1.tgz", + "integrity": "sha512-nogWghc8OOQB+P68alPMkl/tXvRGRQgWtx426OR8T/zwG3ZGZ5JHlyY6TNOfOBuadKBZP7EuNWL1YD37TO/erw==" }, "acorn": { - "version": "6.0.5", - "resolved": "https://registry.npmjs.org/acorn/-/acorn-6.0.5.tgz", - "integrity": "sha512-i33Zgp3XWtmZBMNvCr4azvOFeWVw1Rk6p3hfi3LUDvIFraOMywb1kAtrbi+med14m4Xfpqm3zRZMT+c0FNE7kg==", + "version": "6.1.1", + "resolved": "https://registry.npmjs.org/acorn/-/acorn-6.1.1.tgz", + "integrity": "sha512-jPTiwtOxaHNaAPg/dmrJ/beuzLRnXtB0kQPQ8JpotKJgTB6rX6c8mlf315941pyjBSaPg8NHXS9fhP4u17DpGA==", "dev": true }, "acorn-jsx": { @@ -22,9 +22,9 @@ "dev": true }, "ajv": { - "version": "6.7.0", - "resolved": "https://registry.npmjs.org/ajv/-/ajv-6.7.0.tgz", - "integrity": "sha512-RZXPviBTtfmtka9n9sy1N5M5b82CbxWIR6HIis4s3WQTXDJamc/0gpCWNGz6EWdWp4DOfjzJfhz/AS9zVPjjWg==", + "version": "6.9.2", + "resolved": "https://registry.npmjs.org/ajv/-/ajv-6.9.2.tgz", + "integrity": "sha512-4UFy0/LgDo7Oa/+wOAlj44tp9K78u38E5/359eSrqEp1Z5PdVfimCcs7SluXMP755RUQu6d2b4AvF0R1C9RZjg==", "dev": true, "requires": { "fast-deep-equal": "^2.0.1", @@ -34,15 +34,15 @@ } }, "ajv-keywords": { - "version": "3.2.0", - "resolved": "https://registry.npmjs.org/ajv-keywords/-/ajv-keywords-3.2.0.tgz", - "integrity": "sha1-6GuBnGAs+IIa1jdBNpjx3sAhhHo=", + "version": "3.4.0", + "resolved": "https://registry.npmjs.org/ajv-keywords/-/ajv-keywords-3.4.0.tgz", + "integrity": "sha512-aUjdRFISbuFOl0EIZc+9e4FfZp0bDZgAdOOf30bJmw8VM9v84SHyVyxDfbWxpGYbdZD/9XoKxfHVNmxPkhwyGw==", "dev": true }, "ansi-escapes": { - "version": "3.1.0", - "resolved": "https://registry.npmjs.org/ansi-escapes/-/ansi-escapes-3.1.0.tgz", - "integrity": "sha512-UgAb8H9D41AQnu/PbWlCofQVcnV4Gs2bBJi9eZPxfU/hgglFh3SMDMENRIqdr7H6XFnXdoknctFByVsCOotTVw==", + "version": "3.2.0", + "resolved": "https://registry.npmjs.org/ansi-escapes/-/ansi-escapes-3.2.0.tgz", + "integrity": "sha512-cBhpre4ma+U0T1oM5fXg7Dy1Jw7zzwv7lt/GoCpr+hDQJoYnKVPLL4dCvSEFMmQurOQvSrwT7SL/DAlhBI97RQ==", "dev": true }, "ansi-regex": { @@ -120,23 +120,19 @@ "balanced-match": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz", - "integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=" + "integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=", + "dev": true }, "brace-expansion": { "version": "1.1.11", "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", "integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==", + "dev": true, "requires": { "balanced-match": "^1.0.0", "concat-map": "0.0.1" } }, - "builtin-modules": { - "version": "1.1.1", - "resolved": "https://registry.npmjs.org/builtin-modules/-/builtin-modules-1.1.1.tgz", - "integrity": "sha1-Jw8HbFpywC9bZaR9+Uxf46J4iS8=", - "dev": true - }, "caller-path": { "version": "0.1.0", "resolved": "https://registry.npmjs.org/caller-path/-/caller-path-0.1.0.tgz", @@ -228,7 +224,8 @@ "concat-map": { "version": "0.0.1", "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", - "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=" + "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=", + "dev": true }, "contains-path": { "version": "0.1.0", @@ -266,7 +263,8 @@ "deep-equal": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/deep-equal/-/deep-equal-1.0.1.tgz", - "integrity": "sha1-9dJgKStmDghO/0zbyfCK0yR0SLU=" + "integrity": "sha1-9dJgKStmDghO/0zbyfCK0yR0SLU=", + "dev": true }, "deep-is": { "version": "0.1.3", @@ -278,6 +276,7 @@ "version": "1.1.3", "resolved": "https://registry.npmjs.org/define-properties/-/define-properties-1.1.3.tgz", "integrity": "sha512-3MqfYKj2lLzdMSf8ZIZE/V+Zuy+BgD6f164e8K2w7dgnpKArBDerGYpM46IYYcjnkdPNMjPk9A6VFB8+3SKlXQ==", + "dev": true, "requires": { "object-keys": "^1.0.12" } @@ -285,7 +284,8 @@ "defined": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/defined/-/defined-1.0.0.tgz", - "integrity": "sha1-yY2bzvdWdBiOEQlpFRGZ45sfppM=" + "integrity": "sha1-yY2bzvdWdBiOEQlpFRGZ45sfppM=", + "dev": true }, "deglob": { "version": "2.1.1", @@ -331,6 +331,7 @@ "version": "1.13.0", "resolved": "https://registry.npmjs.org/es-abstract/-/es-abstract-1.13.0.tgz", "integrity": "sha512-vDZfg/ykNxQVwup/8E1BZhVzFfBxs9NqMzGcvIJrqg5k2/5Za2bWo40dK2J1pgLngZ7c+Shh8lwYtLGyrwPutg==", + "dev": true, "requires": { "es-to-primitive": "^1.2.0", "function-bind": "^1.1.1", @@ -344,6 +345,7 @@ "version": "1.2.0", "resolved": "https://registry.npmjs.org/es-to-primitive/-/es-to-primitive-1.2.0.tgz", "integrity": "sha512-qZryBOJjV//LaxLTV6UC//WewneB3LcXOL9NP++ozKVXsIIIpm/2c13UDiD9Jp2eThsecw9m3jPqDwTyobcdbg==", + "dev": true, "requires": { "is-callable": "^1.1.4", "is-date-object": "^1.0.1", @@ -453,13 +455,13 @@ } }, "eslint-module-utils": { - "version": "2.2.0", - "resolved": "https://registry.npmjs.org/eslint-module-utils/-/eslint-module-utils-2.2.0.tgz", - "integrity": "sha1-snA2LNiLGkitMIl2zn+lTphBF0Y=", + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/eslint-module-utils/-/eslint-module-utils-2.3.0.tgz", + "integrity": "sha512-lmDJgeOOjk8hObTysjqH7wyMi+nsHwwvfBykwfhjR1LNdd7C2uFJBvx4OpWYpXOw4df1yE1cDEVd1yLHitk34w==", "dev": true, "requires": { "debug": "^2.6.8", - "pkg-dir": "^1.0.0" + "pkg-dir": "^2.0.0" }, "dependencies": { "debug": { @@ -546,17 +548,6 @@ "minimatch": "^3.0.4", "resolve": "^1.8.1", "semver": "^5.5.0" - }, - "dependencies": { - "resolve": { - "version": "1.9.0", - "resolved": "https://registry.npmjs.org/resolve/-/resolve-1.9.0.tgz", - "integrity": "sha512-TZNye00tI67lwYvzxCxHGjwTNlUV70io54/Ed4j6PscB8xVfuBJpRenI/o6dVk0cY0PYTY27AgCoGGxRnYuItQ==", - "dev": true, - "requires": { - "path-parse": "^1.0.6" - } - } } }, "eslint-plugin-promise": { @@ -713,13 +704,12 @@ "dev": true }, "find-up": { - "version": "1.1.2", - "resolved": "https://registry.npmjs.org/find-up/-/find-up-1.1.2.tgz", - "integrity": "sha1-ay6YIrGizgpgq2TWEOzK1TyyTQ8=", + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/find-up/-/find-up-2.1.0.tgz", + "integrity": "sha1-RdG35QbHF93UgndaK3eSCjwMV6c=", "dev": true, "requires": { - "path-exists": "^2.0.0", - "pinkie-promise": "^2.0.0" + "locate-path": "^2.0.0" } }, "flat-cache": { @@ -738,6 +728,7 @@ "version": "0.3.3", "resolved": "https://registry.npmjs.org/for-each/-/for-each-0.3.3.tgz", "integrity": "sha512-jqYfLp7mo9vIyQf8ykW2v7A+2N4QjeCeI5+Dz9XraiO1ign81wjiH7Fb9vSOWvQfNtmSa4H2RoQTrrXivdUZmw==", + "dev": true, "requires": { "is-callable": "^1.1.3" } @@ -745,12 +736,14 @@ "fs.realpath": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", - "integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=" + "integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=", + "dev": true }, "function-bind": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.1.tgz", - "integrity": "sha512-yIovAzMX49sF8Yl58fSCWJ5svSLuaibPxXQJFLmBObTuCr0Mf1KiPopGM9NiFjiYBCbfaa2Fh6breQ6ANVTI0A==" + "integrity": "sha512-yIovAzMX49sF8Yl58fSCWJ5svSLuaibPxXQJFLmBObTuCr0Mf1KiPopGM9NiFjiYBCbfaa2Fh6breQ6ANVTI0A==", + "dev": true }, "functional-red-black-tree": { "version": "1.0.1", @@ -768,6 +761,7 @@ "version": "7.1.3", "resolved": "https://registry.npmjs.org/glob/-/glob-7.1.3.tgz", "integrity": "sha512-vcfuiIxogLV4DlGBHIUOwI0IbrJ8HWPc4MU7HzviGeNho/UJDfi6B5p3sHeWIQ0KGIU0Jpxi5ZHxemQfLkkAwQ==", + "dev": true, "requires": { "fs.realpath": "^1.0.0", "inflight": "^1.0.4", @@ -778,9 +772,9 @@ } }, "globals": { - "version": "11.10.0", - "resolved": "https://registry.npmjs.org/globals/-/globals-11.10.0.tgz", - "integrity": "sha512-0GZF1RiPKU97IHUO5TORo9w1PwrH/NBPl+fS7oMLdaTRiYmYbwK4NWoZWrAdd0/abG9R2BU+OiwyQpTpE6pdfQ==", + "version": "11.11.0", + "resolved": "https://registry.npmjs.org/globals/-/globals-11.11.0.tgz", + "integrity": "sha512-WHq43gS+6ufNOEqlrDBxVEbb8ntfXrfAUU2ZOpCxrBdGKW3gyv8mCxAfIBD0DroPKGrJ2eSsXsLtY9MPntsyTw==", "dev": true }, "graceful-fs": { @@ -793,6 +787,7 @@ "version": "1.0.3", "resolved": "https://registry.npmjs.org/has/-/has-1.0.3.tgz", "integrity": "sha512-f2dvO0VU6Oej7RkWJGrehjbzMAjFp5/VKPp5tTpWIV4JHHZK1/BxbFRtf/siA2SWTe09caDmVtYYzWEIbBS4zw==", + "dev": true, "requires": { "function-bind": "^1.1.1" } @@ -815,7 +810,8 @@ "has-symbols": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/has-symbols/-/has-symbols-1.0.0.tgz", - "integrity": "sha1-uhqPGvKg/DllD1yFA2dwQSIGO0Q=" + "integrity": "sha1-uhqPGvKg/DllD1yFA2dwQSIGO0Q=", + "dev": true }, "hosted-git-info": { "version": "2.7.1", @@ -848,6 +844,7 @@ "version": "1.0.6", "resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz", "integrity": "sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk=", + "dev": true, "requires": { "once": "^1.3.0", "wrappy": "1" @@ -885,24 +882,17 @@ "integrity": "sha1-d8mYQFJ6qOyxqLppe4BkWnqSap0=", "dev": true }, - "is-builtin-module": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/is-builtin-module/-/is-builtin-module-1.0.0.tgz", - "integrity": "sha1-VAVy0096wxGfj3bDDLwbHgN6/74=", - "dev": true, - "requires": { - "builtin-modules": "^1.0.0" - } - }, "is-callable": { "version": "1.1.4", "resolved": "https://registry.npmjs.org/is-callable/-/is-callable-1.1.4.tgz", - "integrity": "sha512-r5p9sxJjYnArLjObpjA4xu5EKI3CuKHkJXMhT7kwbpUyIFD1n5PMAsoPvWnvtZiNz7LjkYDRZhd7FlI0eMijEA==" + "integrity": "sha512-r5p9sxJjYnArLjObpjA4xu5EKI3CuKHkJXMhT7kwbpUyIFD1n5PMAsoPvWnvtZiNz7LjkYDRZhd7FlI0eMijEA==", + "dev": true }, "is-date-object": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/is-date-object/-/is-date-object-1.0.1.tgz", - "integrity": "sha1-mqIOtq7rv/d/vTPnTKAbM1gdOhY=" + "integrity": "sha1-mqIOtq7rv/d/vTPnTKAbM1gdOhY=", + "dev": true }, "is-fullwidth-code-point": { "version": "2.0.0", @@ -920,6 +910,7 @@ "version": "1.0.4", "resolved": "https://registry.npmjs.org/is-regex/-/is-regex-1.0.4.tgz", "integrity": "sha1-VRdIm1RwkbCTDglWVM7SXul+lJE=", + "dev": true, "requires": { "has": "^1.0.1" } @@ -934,6 +925,7 @@ "version": "1.0.2", "resolved": "https://registry.npmjs.org/is-symbol/-/is-symbol-1.0.2.tgz", "integrity": "sha512-HS8bZ9ox60yCJLH9snBpIwv9pYUAkcuLhSA1oero1UB5y9aiQpRA8y2ex945AOtCZL1lJDeIk3G5LthswI46Lw==", + "dev": true, "requires": { "has-symbols": "^1.0.0" } @@ -962,9 +954,9 @@ "dev": true }, "js-yaml": { - "version": "3.12.1", - "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-3.12.1.tgz", - "integrity": "sha512-um46hB9wNOKlwkHgiuyEVAybXBjwFUV0Z/RaHJblRd9DXltue9FTYvzCr9ErQrK9Adz5MU4gHWVaNUfdmrC8qA==", + "version": "3.12.2", + "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-3.12.2.tgz", + "integrity": "sha512-QHn/Lh/7HhZ/Twc7vJYQTkjuCa0kaCcDcjK5Zlk2rvnUpy7DxMJ23+Jc2dcyvltwQVg1nygAVlB2oRDFHoRS5Q==", "dev": true, "requires": { "argparse": "^1.0.7", @@ -1028,14 +1020,6 @@ "requires": { "p-locate": "^2.0.0", "path-exists": "^3.0.0" - }, - "dependencies": { - "path-exists": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/path-exists/-/path-exists-3.0.0.tgz", - "integrity": "sha1-zg6+ql94yxiSXqfYENe1mwEP1RU=", - "dev": true - } } }, "lodash": { @@ -1063,6 +1047,7 @@ "version": "3.0.4", "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz", "integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==", + "dev": true, "requires": { "brace-expansion": "^1.1.7" } @@ -1118,13 +1103,13 @@ "dev": true }, "normalize-package-data": { - "version": "2.4.0", - "resolved": "https://registry.npmjs.org/normalize-package-data/-/normalize-package-data-2.4.0.tgz", - "integrity": "sha512-9jjUFbTPfEy3R/ad/2oNbKtW9Hgovl5O1FvFWKkKblNXoN/Oou6+9+KKohPK13Yc3/TyunyWhJp6gvRNR/PPAw==", + "version": "2.5.0", + "resolved": "https://registry.npmjs.org/normalize-package-data/-/normalize-package-data-2.5.0.tgz", + "integrity": "sha512-/5CMN3T0R4XTj4DcGaexo+roZSdSFW/0AOOTROrjxzCG1wrWXEsGbRKevjlIL+ZDE4sZlJr5ED4YW0yqmkK+eA==", "dev": true, "requires": { "hosted-git-info": "^2.1.4", - "is-builtin-module": "^1.0.0", + "resolve": "^1.10.0", "semver": "2 || 3 || 4 || 5", "validate-npm-package-license": "^3.0.1" } @@ -1138,17 +1123,20 @@ "object-inspect": { "version": "1.6.0", "resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.6.0.tgz", - "integrity": "sha512-GJzfBZ6DgDAmnuaM3104jR4s1Myxr3Y3zfIyN4z3UdqN69oSRacNK8UhnobDdC+7J2AHCjGwxQubNJfE70SXXQ==" + "integrity": "sha512-GJzfBZ6DgDAmnuaM3104jR4s1Myxr3Y3zfIyN4z3UdqN69oSRacNK8UhnobDdC+7J2AHCjGwxQubNJfE70SXXQ==", + "dev": true }, "object-keys": { - "version": "1.0.12", - "resolved": "https://registry.npmjs.org/object-keys/-/object-keys-1.0.12.tgz", - "integrity": "sha512-FTMyFUm2wBcGHnH2eXmz7tC6IwlqQZ6mVZ+6dm6vZ4IQIHjs6FdNsQBuKGPuUUUY6NfJw2PshC08Tn6LzLDOag==" + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/object-keys/-/object-keys-1.1.0.tgz", + "integrity": "sha512-6OO5X1+2tYkNyNEx6TsCxEqFfRWaqx6EtMiSbGrw8Ob8v9Ne+Hl8rBAgLBZn5wjEz3s/s6U1WXFUFOcxxAwUpg==", + "dev": true }, "once": { "version": "1.4.0", "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", "integrity": "sha1-WDsap3WWHUsROsF9nFC6753Xa9E=", + "dev": true, "requires": { "wrappy": "1" } @@ -1216,18 +1204,16 @@ } }, "path-exists": { - "version": "2.1.0", - "resolved": "https://registry.npmjs.org/path-exists/-/path-exists-2.1.0.tgz", - "integrity": "sha1-D+tsZPD8UY2adU3V77YscCJ2H0s=", - "dev": true, - "requires": { - "pinkie-promise": "^2.0.0" - } + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/path-exists/-/path-exists-3.0.0.tgz", + "integrity": "sha1-zg6+ql94yxiSXqfYENe1mwEP1RU=", + "dev": true }, "path-is-absolute": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz", - "integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18=" + "integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18=", + "dev": true }, "path-is-inside": { "version": "1.0.2", @@ -1244,7 +1230,8 @@ "path-parse": { "version": "1.0.6", "resolved": "https://registry.npmjs.org/path-parse/-/path-parse-1.0.6.tgz", - "integrity": "sha512-GSmOT2EbHrINBf9SR7CDELwlJ8AENk3Qn7OikK4nFYAu3Ote2+JYNVvkpAEQm3/TLNEJFD/xZJjzyxg3KBWOzw==" + "integrity": "sha512-GSmOT2EbHrINBf9SR7CDELwlJ8AENk3Qn7OikK4nFYAu3Ote2+JYNVvkpAEQm3/TLNEJFD/xZJjzyxg3KBWOzw==", + "dev": true }, "path-type": { "version": "2.0.0", @@ -1261,21 +1248,6 @@ "integrity": "sha1-7RQaasBDqEnqWISY59yosVMw6Qw=", "dev": true }, - "pinkie": { - "version": "2.0.4", - "resolved": "https://registry.npmjs.org/pinkie/-/pinkie-2.0.4.tgz", - "integrity": "sha1-clVrgM+g1IqXToDnckjoDtT3+HA=", - "dev": true - }, - "pinkie-promise": { - "version": "2.0.1", - "resolved": "https://registry.npmjs.org/pinkie-promise/-/pinkie-promise-2.0.1.tgz", - "integrity": "sha1-ITXW36ejWMBprJsXh3YogihFD/o=", - "dev": true, - "requires": { - "pinkie": "^2.0.0" - } - }, "pkg-conf": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/pkg-conf/-/pkg-conf-2.1.0.tgz", @@ -1286,15 +1258,6 @@ "load-json-file": "^4.0.0" }, "dependencies": { - "find-up": { - "version": "2.1.0", - "resolved": "https://registry.npmjs.org/find-up/-/find-up-2.1.0.tgz", - "integrity": "sha1-RdG35QbHF93UgndaK3eSCjwMV6c=", - "dev": true, - "requires": { - "locate-path": "^2.0.0" - } - }, "load-json-file": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/load-json-file/-/load-json-file-4.0.0.tgz", @@ -1337,12 +1300,12 @@ } }, "pkg-dir": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/pkg-dir/-/pkg-dir-1.0.0.tgz", - "integrity": "sha1-ektQio1bstYp1EcFb/TpyTFM89Q=", + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/pkg-dir/-/pkg-dir-2.0.0.tgz", + "integrity": "sha1-9tXREJ4Z1j7fQo4L1X4Sd3YVM0s=", "dev": true, "requires": { - "find-up": "^1.0.0" + "find-up": "^2.1.0" } }, "pluralize": { @@ -1364,13 +1327,14 @@ "dev": true }, "prop-types": { - "version": "15.6.2", - "resolved": "https://registry.npmjs.org/prop-types/-/prop-types-15.6.2.tgz", - "integrity": "sha512-3pboPvLiWD7dkI3qf3KbUe6hKFKa52w+AE0VCqECtf+QHAKgOL37tTaNCnuX1nAAQ4ZhyP+kYVKf8rLmJ/feDQ==", + "version": "15.7.2", + "resolved": "https://registry.npmjs.org/prop-types/-/prop-types-15.7.2.tgz", + "integrity": "sha512-8QQikdH7//R2vurIJSutZ1smHYTcLpRWEOlHnzcWHmBYrOGUysKwSsrC89BCiFj3CbrfJ/nXFdJepOVrY1GCHQ==", "dev": true, "requires": { - "loose-envify": "^1.3.1", - "object-assign": "^4.1.1" + "loose-envify": "^1.4.0", + "object-assign": "^4.1.1", + "react-is": "^16.8.1" } }, "punycode": { @@ -1379,6 +1343,12 @@ "integrity": "sha512-XRsRjdf+j5ml+y/6GKHPZbrF/8p2Yga0JPtdqTIY2Xe5ohJPD9saDJJLPvp9+NSBprVvevdXZybnj2cv8OEd0A==", "dev": true }, + "react-is": { + "version": "16.8.3", + "resolved": "https://registry.npmjs.org/react-is/-/react-is-16.8.3.tgz", + "integrity": "sha512-Y4rC1ZJmsxxkkPuMLwvKvlL1Zfpbcu+Bf4ZigkHup3v9EfdYhAlWAaVyA19olXq2o2mGn0w+dFKvk3pVVlYcIA==", + "dev": true + }, "read-pkg": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/read-pkg/-/read-pkg-2.0.0.tgz", @@ -1398,23 +1368,12 @@ "requires": { "find-up": "^2.0.0", "read-pkg": "^2.0.0" - }, - "dependencies": { - "find-up": { - "version": "2.1.0", - "resolved": "https://registry.npmjs.org/find-up/-/find-up-2.1.0.tgz", - "integrity": "sha1-RdG35QbHF93UgndaK3eSCjwMV6c=", - "dev": true, - "requires": { - "locate-path": "^2.0.0" - } - } } }, "readable-stream": { - "version": "3.1.1", - "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-3.1.1.tgz", - "integrity": "sha512-DkN66hPyqDhnIQ6Jcsvx9bFjhw214O4poMBcIMgPVpQvNy9a0e0Uhg5SqySyDKAmUlwt8LonTBz1ezOnM8pUdA==", + "version": "3.2.0", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-3.2.0.tgz", + "integrity": "sha512-RV20kLjdmpZuTF1INEb9IA3L68Nmi+Ri7ppZqo78wj//Pn62fCoJyV9zalccNzDD/OuJpMG4f+pfMl8+L6QdGw==", "requires": { "inherits": "^2.0.3", "string_decoder": "^1.1.1", @@ -1438,11 +1397,12 @@ } }, "resolve": { - "version": "1.7.1", - "resolved": "https://registry.npmjs.org/resolve/-/resolve-1.7.1.tgz", - "integrity": "sha512-c7rwLofp8g1U+h1KNyHL/jicrKg1Ek4q+Lr33AL65uZTinUZHe30D5HlyN5V9NW0JX1D5dXQ4jqW5l7Sy/kGfw==", + "version": "1.10.0", + "resolved": "https://registry.npmjs.org/resolve/-/resolve-1.10.0.tgz", + "integrity": "sha512-3sUr9aq5OfSg2S9pNtPA9hL1FVEAjvfOC4leW0SNf/mpnaakz2a9femSd6LqAww2RaFctwyf1lCqnTHuF1rxDg==", + "dev": true, "requires": { - "path-parse": "^1.0.5" + "path-parse": "^1.0.6" } }, "resolve-from": { @@ -1465,6 +1425,7 @@ "version": "0.0.0", "resolved": "https://registry.npmjs.org/resumer/-/resumer-0.0.0.tgz", "integrity": "sha1-8ej0YeQGS6Oegq883CqMiT0HZ1k=", + "dev": true, "requires": { "through": "~2.3.4" } @@ -1549,6 +1510,11 @@ "is-fullwidth-code-point": "^2.0.0" } }, + "sockette": { + "version": "2.0.5", + "resolved": "https://registry.npmjs.org/sockette/-/sockette-2.0.5.tgz", + "integrity": "sha512-jQbd/n+bHKdOfd9ii95aZ+TTHlaNCbKUpOW4YxiNx/U8OxfhvM9g/SpMSHl/hGSZ5BUYtltXBz02p+ih11M3WQ==" + }, "spdx-correct": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/spdx-correct/-/spdx-correct-3.1.0.tgz", @@ -1630,6 +1596,7 @@ "version": "1.1.2", "resolved": "https://registry.npmjs.org/string.prototype.trim/-/string.prototype.trim-1.1.2.tgz", "integrity": "sha1-0E3iyJ4Tf019IG8Ia17S+ua+jOo=", + "dev": true, "requires": { "define-properties": "^1.1.2", "es-abstract": "^1.5.0", @@ -1700,20 +1667,21 @@ } }, "tape": { - "version": "4.9.2", - "resolved": "https://registry.npmjs.org/tape/-/tape-4.9.2.tgz", - "integrity": "sha512-lPXKRKILZ1kZaUy5ynWKs8ATGSUO7HAFHCFnBam6FaGSqPdOwMWbxXHq4EXFLE8WRTleo/YOMXkaUTRmTB1Fiw==", + "version": "4.10.1", + "resolved": "https://registry.npmjs.org/tape/-/tape-4.10.1.tgz", + "integrity": "sha512-G0DywYV1jQeY3axeYnXUOt6ktnxS9OPJh97FGR3nrua8lhWi1zPflLxcAHavZ7Jf3qUfY7cxcVIVFa4mY2IY1w==", + "dev": true, "requires": { "deep-equal": "~1.0.1", "defined": "~1.0.0", "for-each": "~0.3.3", "function-bind": "~1.1.1", - "glob": "~7.1.2", + "glob": "~7.1.3", "has": "~1.0.3", "inherits": "~2.0.3", "minimist": "~1.2.0", "object-inspect": "~1.6.0", - "resolve": "~1.7.1", + "resolve": "~1.10.0", "resumer": "~0.0.0", "string.prototype.trim": "~1.1.2", "through": "~2.3.8" @@ -1728,7 +1696,8 @@ "through": { "version": "2.3.8", "resolved": "https://registry.npmjs.org/through/-/through-2.3.8.tgz", - "integrity": "sha1-DdTJ/6q8NXlgsbckEV1+Doai4fU=" + "integrity": "sha1-DdTJ/6q8NXlgsbckEV1+Doai4fU=", + "dev": true }, "through2": { "version": "3.0.0", @@ -1839,7 +1808,8 @@ "wrappy": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", - "integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=" + "integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=", + "dev": true }, "write": { "version": "0.2.1", @@ -1851,9 +1821,9 @@ } }, "ws": { - "version": "6.1.2", - "resolved": "https://registry.npmjs.org/ws/-/ws-6.1.2.tgz", - "integrity": "sha512-rfUqzvz0WxmSXtJpPMX2EeASXabOrSMk1ruMOV3JBTBjo4ac2lDjGGsbQSyxj8Odhw5fBib8ZKEjDNvgouNKYw==", + "version": "6.1.4", + "resolved": "https://registry.npmjs.org/ws/-/ws-6.1.4.tgz", + "integrity": "sha512-eqZfL+NE/YQc1/ZynhojeV8q+H050oR8AZ2uIev7RU10svA9ZnJUddHcOUZTJLinZ9yEfdA2kSATS2qZK5fhJA==", "dev": true, "requires": { "async-limiter": "~1.0.0" diff --git a/package.json b/package.json index f60aa19..641d536 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,8 @@ "main": "index.js", "bin": "bin.js", "scripts": { - "test": "standard && tape tests/**/*.test.js", + "test": "tape tests/**/*.test.js", + "posttest": "standard", "start": "node bin.js" }, "keywords": [], @@ -17,12 +18,13 @@ "events": "^3.0.0", "inherits": "^2.0.3", "minimist": "^1.2.0", - "tape": "^4.9.2", + "sockette": "^2.0.5", "through2": "^3.0.0", "websocket": "^1.0.28" }, "devDependencies": { "standard": "^12.0.1", + "tape": "^4.9.2", "ws": "^6.1.2" } } diff --git a/tests/boilerplate.js b/tests/boilerplate.js index 02fc3eb..5370c58 100644 --- a/tests/boilerplate.js +++ b/tests/boilerplate.js @@ -39,11 +39,9 @@ module.exports = function (test, server, client, port) { function done () { if (--missing) return - setTimeout(function () { - c1.close() - c2.close() - t.end() - }, 100) + c1.close() + c2.close() + t.end() } }) @@ -106,10 +104,12 @@ module.exports = function (test, server, client, port) { `localhost:${port}`, `localhost:${port}` ]) + c.subscribe('hello').on('open', function () { t.ok(true, 'got an open event') - c.close() - t.end() + c.close(() => { + t.end() + }) }) }) diff --git a/tests/browser.test.js b/tests/browser.test.js index 502480b..3ea62e0 100644 --- a/tests/browser.test.js +++ b/tests/browser.test.js @@ -1,5 +1,6 @@ global.window = {} window.WebSocket = require('websocket').w3cwebsocket +global.WebSocket = window.WebSocket var test = require('tape') var macro = require('./boilerplate') var server = require('../server')() From 8f1d7f315686929c305f72b881c81be6beec5d81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Acosta?= Date: Thu, 28 Feb 2019 21:11:12 -0300 Subject: [PATCH 05/10] fixed default commonjs --- index.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 7b6a35a..50e8515 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,12 @@ const { EventEmitter } = require('events') const through2 = require('through2') -const Sockette = require('sockette') +let Sockette = require('sockette') const WebSocket = window.WebSocket + +if (Sockette.default) { + Sockette = Sockette.default +} + const noop = () => {} class SignalhubWs extends EventEmitter { From 8f2e31004ffb3cb4cb73ce8f2bc0e5bac4f5eae9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Acosta?= Date: Fri, 1 Mar 2019 14:40:33 -0300 Subject: [PATCH 06/10] check closed state durin ws.send --- index.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 50e8515..a80e267 100644 --- a/index.js +++ b/index.js @@ -136,7 +136,7 @@ class SignalhubWs extends EventEmitter { } this.sockets.forEach(socket => { - socket.send(JSON.stringify(data)) + this._send(socket, JSON.stringify(data)) }) cb && cb() @@ -217,6 +217,14 @@ class SignalhubWs extends EventEmitter { } } + _send (socket, message) { + if (!socket.ws || socket.ws.readyState !== WebSocket.OPEN) { + return + } + + socket.send(message) + } + _closeChannels (cb) { const onChannelClose = () => { if (this.channels.size === 0) { From 2a300792a888ca03361da1e69b706c8b2dd4afa2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Acosta?= Date: Fri, 1 Mar 2019 16:12:53 -0300 Subject: [PATCH 07/10] added nodejs support --- index.js | 19 +++++++++--- package-lock.json | 65 +++++------------------------------------- package.json | 5 ++-- tests/browser.test.js | 3 -- tests/server.test.js | 2 +- tests/serverws.test.js | 2 +- 6 files changed, 26 insertions(+), 70 deletions(-) diff --git a/index.js b/index.js index a80e267..5a16fe8 100644 --- a/index.js +++ b/index.js @@ -1,8 +1,14 @@ const { EventEmitter } = require('events') const through2 = require('through2') -let Sockette = require('sockette') -const WebSocket = window.WebSocket +let WebSocket +if (typeof window !== 'undefined' && window.WebSocket) { + WebSocket = window.WebSocket +} else { + WebSocket = global.WebSocket = require('ws') +} + +let Sockette = require('sockette') if (Sockette.default) { Sockette = Sockette.default } @@ -144,16 +150,21 @@ class SignalhubWs extends EventEmitter { } close (cb = noop) { + const _close = () => { + this.emit('close') + cb() + } + this.ready(() => { if (this.closed) { - this._closeChannels(cb) + this._closeChannels(_close) return } const onSocketClose = () => { if (this.closed) { this.removeListener('socket:close', onSocketClose) - this._closeChannels(cb) + this._closeChannels(_close) } } this.on('socket:close', onSocketClose) diff --git a/package-lock.json b/package-lock.json index 95ed4d6..cd8a220 100644 --- a/package-lock.json +++ b/package-lock.json @@ -79,8 +79,7 @@ "async-limiter": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/async-limiter/-/async-limiter-1.0.0.tgz", - "integrity": "sha512-jp/uFnooOiO+L211eZOoSyzpOITMXx1rBITauYykG3BRYPu8h0UcxsPNB04RR5vo4Tyz3+ay17tR6JVf9qzYWg==", - "dev": true + "integrity": "sha512-jp/uFnooOiO+L211eZOoSyzpOITMXx1rBITauYykG3BRYPu8h0UcxsPNB04RR5vo4Tyz3+ay17tR6JVf9qzYWg==" }, "babel-code-frame": { "version": "6.26.0", @@ -930,11 +929,6 @@ "has-symbols": "^1.0.0" } }, - "is-typedarray": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/is-typedarray/-/is-typedarray-1.0.0.tgz", - "integrity": "sha1-5HnICFjfDBsR3dppQPlgEfzaSpo=" - }, "isarray": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz", @@ -1085,11 +1079,6 @@ "integrity": "sha1-MHXOk7whuPq0PhvE2n6BFe0ee6s=", "dev": true }, - "nan": { - "version": "2.12.1", - "resolved": "https://registry.npmjs.org/nan/-/nan-2.12.1.tgz", - "integrity": "sha512-JY7V6lRkStKcKTvHO5NVSQRv+RV+FIL5pvDoLiAtSL9pKlC5x9PKQcZDsq7m4FO4d57mkhC6Z+QhAh3Jdk5JFw==" - }, "natural-compare": { "version": "1.4.0", "resolved": "https://registry.npmjs.org/natural-compare/-/natural-compare-1.4.0.tgz", @@ -1700,12 +1689,11 @@ "dev": true }, "through2": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/through2/-/through2-3.0.0.tgz", - "integrity": "sha512-8B+sevlqP4OiCjonI1Zw03Sf8PuV1eRsYQgLad5eonILOdyeRsY27A/2Ze8IlvlMvq31OH+3fz/styI7Ya62yQ==", + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/through2/-/through2-3.0.1.tgz", + "integrity": "sha512-M96dvTalPT3YbYLaKaCuwu+j06D/8Jfib0o/PxbVt6Amhv3dUAtW6rTV1jPgJSBG83I/e04Y6xkVdVhSRhi0ww==", "requires": { - "readable-stream": "2 || 3", - "xtend": "~4.0.1" + "readable-stream": "2 || 3" } }, "tmp": { @@ -1726,14 +1714,6 @@ "prelude-ls": "~1.1.2" } }, - "typedarray-to-buffer": { - "version": "3.1.5", - "resolved": "https://registry.npmjs.org/typedarray-to-buffer/-/typedarray-to-buffer-3.1.5.tgz", - "integrity": "sha512-zdu8XMNEDepKKR+XYOXAVPtWui0ly0NtohUscw+UmaHiAWT8hrV1rr//H6V+0DvJ3OQ19S979M0laLfX8rm82Q==", - "requires": { - "is-typedarray": "^1.0.0" - } - }, "uniq": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/uniq/-/uniq-1.0.1.tgz", @@ -1764,32 +1744,6 @@ "spdx-expression-parse": "^3.0.0" } }, - "websocket": { - "version": "1.0.28", - "resolved": "https://registry.npmjs.org/websocket/-/websocket-1.0.28.tgz", - "integrity": "sha512-00y/20/80P7H4bCYkzuuvvfDvh+dgtXi5kzDf3UcZwN6boTYaKvsrtZ5lIYm1Gsg48siMErd9M4zjSYfYFHTrA==", - "requires": { - "debug": "^2.2.0", - "nan": "^2.11.0", - "typedarray-to-buffer": "^3.1.5", - "yaeti": "^0.0.6" - }, - "dependencies": { - "debug": { - "version": "2.6.9", - "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz", - "integrity": "sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==", - "requires": { - "ms": "2.0.0" - } - }, - "ms": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", - "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=" - } - } - }, "which": { "version": "1.3.1", "resolved": "https://registry.npmjs.org/which/-/which-1.3.1.tgz", @@ -1824,7 +1778,6 @@ "version": "6.1.4", "resolved": "https://registry.npmjs.org/ws/-/ws-6.1.4.tgz", "integrity": "sha512-eqZfL+NE/YQc1/ZynhojeV8q+H050oR8AZ2uIev7RU10svA9ZnJUddHcOUZTJLinZ9yEfdA2kSATS2qZK5fhJA==", - "dev": true, "requires": { "async-limiter": "~1.0.0" } @@ -1832,12 +1785,8 @@ "xtend": { "version": "4.0.1", "resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.1.tgz", - "integrity": "sha1-pcbVMr5lbiPbgg77lDofBJmNY68=" - }, - "yaeti": { - "version": "0.0.6", - "resolved": "https://registry.npmjs.org/yaeti/-/yaeti-0.0.6.tgz", - "integrity": "sha1-8m9ITXJoTPQr7ft2lwqhYI+/lXc=" + "integrity": "sha1-pcbVMr5lbiPbgg77lDofBJmNY68=", + "dev": true } } } diff --git a/package.json b/package.json index 641d536..32ca635 100644 --- a/package.json +++ b/package.json @@ -20,11 +20,10 @@ "minimist": "^1.2.0", "sockette": "^2.0.5", "through2": "^3.0.0", - "websocket": "^1.0.28" + "ws": "^6.1.4" }, "devDependencies": { "standard": "^12.0.1", - "tape": "^4.9.2", - "ws": "^6.1.2" + "tape": "^4.9.2" } } diff --git a/tests/browser.test.js b/tests/browser.test.js index 3ea62e0..1eed089 100644 --- a/tests/browser.test.js +++ b/tests/browser.test.js @@ -1,6 +1,3 @@ -global.window = {} -window.WebSocket = require('websocket').w3cwebsocket -global.WebSocket = window.WebSocket var test = require('tape') var macro = require('./boilerplate') var server = require('../server')() diff --git a/tests/server.test.js b/tests/server.test.js index 1003169..2efc940 100644 --- a/tests/server.test.js +++ b/tests/server.test.js @@ -2,7 +2,7 @@ var test = require('tape') var macro = require('./boilerplate') var server = require('../server')() var browserClient = require('../index') -var serverClient = (app, urls) => browserClient(app, urls, require('websocket').w3cwebsocket) +var serverClient = (app, urls) => browserClient(app, urls, require('ws')) var PORT = 9001 server.listen(PORT, () => { diff --git a/tests/serverws.test.js b/tests/serverws.test.js index c43e969..4ad769a 100644 --- a/tests/serverws.test.js +++ b/tests/serverws.test.js @@ -2,7 +2,7 @@ var test = require('tape') var macro = require('./boilerplate') var server = require('../server')(require('ws').WebSocketServer) var browserClient = require('../index') -var serverClient = (app, urls) => browserClient(app, urls, require('websocket').w3cwebsocket) +var serverClient = (app, urls) => browserClient(app, urls, require('ws')) var PORT = 9001 server.listen(PORT, () => { From 5a0bb1c82d8ee660684e96d67ff1d4e7a6db1ad6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Acosta?= Date: Fri, 1 Mar 2019 16:31:53 -0300 Subject: [PATCH 08/10] fixed reconnection --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 5a16fe8..c6fcb89 100644 --- a/index.js +++ b/index.js @@ -215,7 +215,7 @@ class SignalhubWs extends EventEmitter { } _checkInitializeWs (event, socket) { - if (!socket.ws) { + if (socket.ws !== event.target) { socket.ws = event.target this.emit('socket:ready', socket) } From af7baeaf9820fe3590604070874924409c7142ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Acosta?= Date: Sat, 2 Mar 2019 10:08:04 -0300 Subject: [PATCH 09/10] added new events for the socket connection --- index.js | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index c6fcb89..3965990 100644 --- a/index.js +++ b/index.js @@ -60,8 +60,11 @@ class SignalhubWs extends EventEmitter { onerror: e => { this._onError(e, socket) }, - onreconnect: () => { - console.log(`Reconnecting to: ${id}`) + onreconnect: e => { + this.emit('socket:reconnect', e, socket) + }, + onmaximum: e => { + this.emit('socket:maximum', e, socket) } } )) @@ -177,7 +180,7 @@ class SignalhubWs extends EventEmitter { _onOpen (event, socket) { this._checkInitializeWs(event, socket) - this.emit('socket:open', socket) + this.emit('socket:open', event, socket) for (let channel of this.channels.values()) { this._openChannel(channel) } @@ -185,16 +188,16 @@ class SignalhubWs extends EventEmitter { _onClose (event, socket) { this._checkInitializeWs(event, socket) - this.emit('socket:close', socket) + this.emit('socket:close', event, socket) } _onError (event, socket) { this._checkInitializeWs(event, socket) - this.emit('socket:error', socket) + this.emit('socket:error', event, socket) } - _onMessage (message) { - message = JSON.parse(message.data) + _onMessage (event, socket) { + const message = JSON.parse(event.data) for (let key of this.channels.keys()) { if (message.channel === key) { @@ -212,6 +215,8 @@ class SignalhubWs extends EventEmitter { } } } + + this.emit('socket:message', event, socket) } _checkInitializeWs (event, socket) { From 7465b4a7199860a9afc0234b5d2e15c5cb2ed872 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Acosta?= Date: Wed, 6 Mar 2019 10:23:11 -0300 Subject: [PATCH 10/10] fixed tests --- tests/server.test.js | 2 +- tests/serverws.test.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/server.test.js b/tests/server.test.js index 2efc940..cef540d 100644 --- a/tests/server.test.js +++ b/tests/server.test.js @@ -2,7 +2,7 @@ var test = require('tape') var macro = require('./boilerplate') var server = require('../server')() var browserClient = require('../index') -var serverClient = (app, urls) => browserClient(app, urls, require('ws')) +var serverClient = (app, urls) => browserClient(app, urls) var PORT = 9001 server.listen(PORT, () => { diff --git a/tests/serverws.test.js b/tests/serverws.test.js index 4ad769a..81f52f2 100644 --- a/tests/serverws.test.js +++ b/tests/serverws.test.js @@ -2,7 +2,7 @@ var test = require('tape') var macro = require('./boilerplate') var server = require('../server')(require('ws').WebSocketServer) var browserClient = require('../index') -var serverClient = (app, urls) => browserClient(app, urls, require('ws')) +var serverClient = (app, urls) => browserClient(app, urls) var PORT = 9001 server.listen(PORT, () => {