Skip to content

Commit

Permalink
Merge pull request #514 from xmppjs/connection-issues
Browse files Browse the repository at this point in the history
improve connection handling
  • Loading branch information
sonnyp committed Dec 20, 2017
2 parents 68af7fd + 252c386 commit 9527592
Show file tree
Hide file tree
Showing 67 changed files with 800 additions and 465 deletions.
20 changes: 13 additions & 7 deletions .babelrc
@@ -1,12 +1,18 @@
{
"presets": ["es2015"],
"plugins": [
["transform-react-jsx", {
"pragma": "xml"
}],
["babel-plugin-jsx-pragmatic", {
"module": "@xmpp/xml",
"import": "xml"
}]
[
"transform-react-jsx",
{
"pragma": "xml"
}
],
[
"babel-plugin-jsx-pragmatic",
{
"module": "@xmpp/xml",
"import": "xml"
}
]
]
}
2 changes: 1 addition & 1 deletion .eslintrc
Expand Up @@ -33,7 +33,7 @@ rules:
prefer-spread: [error]
prefer-destructuring: [error]
# node https://github.com/mysticatea/eslint-plugin-node
node/no-unsupported-features: [error, {version: 6}]
node/no-unsupported-features: [error, {version: 7.6}]
node/no-unpublished-require: 0 # doesn't play nice with monorepo
node/no-extraneous-require: [error, allowModules: [ava, sinon, "@xmpp/test"]]
# standard https://github.com/xjamundx/eslint-plugin-standard
Expand Down
1 change: 0 additions & 1 deletion .travis.yml
Expand Up @@ -5,7 +5,6 @@ dist: trusty
language: node_js

node_js:
- '6'
- '7'
- '8'
- '9'
Expand Down
14 changes: 7 additions & 7 deletions package.json
Expand Up @@ -12,19 +12,19 @@
"browserify": "^14.5.0",
"bundlesize": "^0.15.3",
"common-shakeify": "^0.4.5",
"eslint": "^4.12.0",
"eslint": "^4.13.1",
"eslint-config-prettier": "^2.9.0",
"eslint-config-xo": "^0.19.0",
"eslint-plugin-node": "^5.2.1",
"eslint-plugin-prettier": "^2.2.0",
"eslint-plugin-prettier": "^2.4.0",
"eslint-plugin-promise": "^3.6.0",
"eslint-plugin-standard": "^3.0.1",
"eslint-plugin-unicorn": "^3.0.0",
"eslint-plugin-unicorn": "^3.0.1",
"exorcist": "^1.0.0",
"lerna": "^2.5.1",
"prettier": "^1.8.2",
"sinon": "^4.1.2",
"uglify-es": "^3.2.0",
"prettier": "^1.9.2",
"sinon": "^4.1.3",
"uglify-es": "^3.2.2",
"util.promisify": "^1.0.0"
},
"ava": {
Expand All @@ -42,7 +42,7 @@
"lint": "eslint ."
},
"engines": {
"node": ">= 6",
"node": ">= 7.6.0",
"yarn": ">= 0.28"
},
"workspaces": [
Expand Down
2 changes: 1 addition & 1 deletion packages/bind/package.json
Expand Up @@ -11,7 +11,7 @@
"@xmpp/xml": "^0.3.0"
},
"engines": {
"node": ">= 6",
"node": ">= 7.6.0",
"npm": ">= 2"
}
}
8 changes: 6 additions & 2 deletions packages/client-core/lib/Client.js
Expand Up @@ -18,14 +18,18 @@ class Client extends Connection {
return super.send(element, ...args)
}

connect(uri) {
const Transport = this.transports.find(Transport => {
_findTransport(uri) {
return this.transports.find(Transport => {
try {
return Transport.prototype.socketParameters(uri) !== undefined
} catch (err) {
return false
}
})
}

connect(uri) {
const Transport = this._findTransport(uri)

if (!Transport) {
throw new Error('No compatible connection method found.')
Expand Down
2 changes: 1 addition & 1 deletion packages/client-core/package.json
Expand Up @@ -11,7 +11,7 @@
"@xmpp/xml": "^0.3.0"
},
"engines": {
"node": ">= 6",
"node": ">= 7.6.0",
"npm": ">= 2"
}
}
24 changes: 24 additions & 0 deletions packages/client-core/test/Client.js
@@ -0,0 +1,24 @@
'use strict'

const test = require('ava')
const Client = require('../lib/Client')

test('_findTransport', t => {
class Transport {
socketParameters(uri) {
if (uri === 'a') {
return uri
}
if (uri === 'b') {
return undefined
}
throw new Error('foobar')
}
}

const entity = new Client()
entity.transports.push(Transport)
t.is(entity._findTransport('a'), Transport)
t.is(entity._findTransport('b'), undefined)
t.is(entity._findTransport('c'), undefined)
})
6 changes: 2 additions & 4 deletions packages/client/index.js
@@ -1,7 +1,5 @@
'use strict'

const entries = Object.entries || require('object.entries') // eslint-disable-line node/no-unsupported-features

const Client = require('./lib/Client')
const {xml, jid} = require('@xmpp/client-core')

Expand Down Expand Up @@ -40,7 +38,7 @@ function xmpp() {
streamFeatures.use(...bind.streamFeature())
router.use('stream:features', sessionEstablishment())

const mechanisms = entries(_mechanisms)
const mechanisms = Object.entries(_mechanisms)
// Ignore browserify stubs
.filter(([, v]) => typeof v === 'function')
.map(([k, v]) => ({[k]: v(_sasl)}))
Expand All @@ -54,7 +52,7 @@ function xmpp() {
},
// ...features,
...mechanisms,
...entries(packages)
...Object.entries(packages)
// Ignore browserify stubs
.filter(([, v]) => typeof v === 'function')
.map(([k, v]) => ({[k]: v(client)}))
Expand Down
8 changes: 3 additions & 5 deletions packages/client/package.json
Expand Up @@ -21,15 +21,13 @@
"@xmpp/stream-features": "^0.3.0",
"@xmpp/tcp": "^0.3.0",
"@xmpp/tls": "^0.3.0",
"@xmpp/websocket": "^0.3.0",
"object.entries": "^1.0.4"
"@xmpp/websocket": "^0.3.0"
},
"browser": {
"@xmpp/tcp": false,
"@xmpp/tls": false,
"@xmpp/starttls": false,
"@xmpp/sasl-scram-sha-1": false,
"object.entries": false
"@xmpp/sasl-scram-sha-1": false
},
"scripts": {
"prepublish": "npm run bundle && npm run minify",
Expand All @@ -39,7 +37,7 @@
"../../node_modules/uglify-es/bin/uglifyjs dist/xmpp.js -c -m --source-map url=xmpp.min.js.map,content=dist/xmpp.js.map -o dist/xmpp.min.js"
},
"engines": {
"node": ">= 6",
"node": ">= 7.6.0",
"npm": ">= 2"
}
}
11 changes: 4 additions & 7 deletions packages/component-core/package.json
Expand Up @@ -2,22 +2,19 @@
"name": "@xmpp/component-core",
"description": "XMPP component core for JavaScript",
"repository": "github:xmppjs/xmpp.js",
"homepage": "https://github.com/xmppjs/xmpp.js/tree/master/packages/component-core",
"homepage":
"https://github.com/xmppjs/xmpp.js/tree/master/packages/component-core",
"bugs": "http://github.com/xmppjs/xmpp.js/issues",
"version": "0.3.0",
"license": "ISC",
"keywords": [
"XMPP",
"component",
"core"
],
"keywords": ["XMPP", "component", "core"],
"dependencies": {
"@xmpp/connection-tcp": "^0.3.0",
"@xmpp/jid": "^0.3.0",
"@xmpp/xml": "^0.3.0"
},
"engines": {
"node": ">= 6",
"node": ">= 7.6.0",
"npm": ">= 2"
}
}
3 changes: 1 addition & 2 deletions packages/component/index.js
@@ -1,6 +1,5 @@
'use strict'

const entries = Object.entries || require('object.entries') // eslint-disable-line node/no-unsupported-features
const {Component, xml, jid} = require('@xmpp/component-core')

const reconnect = require('@xmpp/reconnect')
Expand All @@ -12,7 +11,7 @@ function xmpp() {
const component = new Component()
return Object.assign(
{component},
...entries(packages)
...Object.entries(packages)
// Ignore browserify stubs
.filter(([, v]) => typeof v === 'function')
.map(([k, v]) => ({[k]: v(component)}))
Expand Down
5 changes: 2 additions & 3 deletions packages/component/package.json
Expand Up @@ -12,11 +12,10 @@
"@xmpp/component-core": "^0.3.0",
"@xmpp/middleware": "^0.3.0",
"@xmpp/reconnect": "^0.3.0",
"@xmpp/router": "^0.3.0",
"object.entries": "^1.0.4"
"@xmpp/router": "^0.3.0"
},
"engines": {
"node": ">= 6",
"node": ">= 7.6.0",
"npm": ">= 2"
}
}
17 changes: 10 additions & 7 deletions packages/connection-tcp/index.js
Expand Up @@ -3,17 +3,20 @@
const {Socket} = require('net')
const Connection = require('@xmpp/connection')
const {Parser} = require('@xmpp/xml')
const url = require('url')

const NS_STREAM = 'http://etherx.jabber.org/streams'

/* References
* Extensible Messaging and Presence Protocol (XMPP): Core http://xmpp.org/rfcs/rfc6120.html
*/

class TCP extends Connection {
class ConnectionTCP extends Connection {
socketParameters(uri) {
const {port, host, protocol} = super.socketParameters(uri)
return protocol === 'xmpp:' ? {port, host} : undefined
const {port, hostname, protocol} = url.parse(uri)
return protocol === 'xmpp:'
? {port: port ? Number(port) : null, host: hostname}
: undefined
}

// https://xmpp.org/rfcs/rfc6120.html#streams-open
Expand All @@ -36,8 +39,8 @@ class TCP extends Connection {
}
}

TCP.prototype.NS = NS_STREAM
TCP.prototype.Socket = Socket
TCP.prototype.Parser = Parser
ConnectionTCP.prototype.NS = NS_STREAM
ConnectionTCP.prototype.Socket = Socket
ConnectionTCP.prototype.Parser = Parser

module.exports = TCP
module.exports = ConnectionTCP
11 changes: 4 additions & 7 deletions packages/connection-tcp/package.json
Expand Up @@ -2,22 +2,19 @@
"name": "@xmpp/connection-tcp",
"description": "XMPP TCP connection for JavaScript",
"repository": "github:xmppjs/xmpp.js",
"homepage": "https://github.com/xmppjs/xmpp.js/tree/master/packages/connection-tcp",
"homepage":
"https://github.com/xmppjs/xmpp.js/tree/master/packages/connection-tcp",
"bugs": "http://github.com/xmppjs/xmpp.js/issues",
"version": "0.3.0",
"license": "ISC",
"keywords": [
"XMPP",
"connection",
"TCP"
],
"keywords": ["XMPP", "connection", "TCP"],
"dependencies": {
"@xmpp/connection": "^0.3.0",
"@xmpp/streamparser": "^0.0.6",
"@xmpp/xml": "^0.3.0"
},
"engines": {
"node": ">= 6",
"node": ">= 7.6.0",
"npm": ">= 2"
}
}
17 changes: 17 additions & 0 deletions packages/connection-tcp/test/Connection.js
Expand Up @@ -35,3 +35,20 @@ test('footer()', t => {
const conn = new Connection()
t.is(conn.footer(), '</stream:stream>')
})

test('socketParameters()', t => {
t.deepEqual(Connection.prototype.socketParameters('xmpp://foo'), {
port: null,
host: 'foo',
})

t.deepEqual(Connection.prototype.socketParameters('xmpp://foo:1234'), {
port: 1234,
host: 'foo',
})

t.deepEqual(
Connection.prototype.socketParameters('xmpps://foo:1234'),
undefined
)
})

0 comments on commit 9527592

Please sign in to comment.