Skip to content

Commit

Permalink
move more plugins to packages
Browse files Browse the repository at this point in the history
  • Loading branch information
Sonny Piers committed Nov 29, 2017
1 parent 7b29c82 commit 9278794
Show file tree
Hide file tree
Showing 54 changed files with 712 additions and 383 deletions.
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"private": true,
"devDependencies": {
"ava": "^0.23.0",
"ava": "^0.24.0",
"babel-cli": "^6.26.0",
"babel-plugin-jsx-pragmatic": "^1.0.2",
"babel-plugin-transform-react-jsx": "^6.24.1",
Expand All @@ -12,8 +12,8 @@
"browserify": "^14.5.0",
"bundlesize": "^0.15.3",
"common-shakeify": "^0.4.5",
"eslint": "^4.11.0",
"eslint-config-prettier": "^2.8.0",
"eslint": "^4.12.0",
"eslint-config-prettier": "^2.9.0",
"eslint-config-xo": "^0.19.0",
"eslint-plugin-node": "^5.2.1",
"eslint-plugin-prettier": "^2.2.0",
Expand Down Expand Up @@ -45,7 +45,9 @@
"node": ">= 6",
"yarn": ">= 0.28"
},
"workspaces": ["packages/*"],
"workspaces": [
"packages/*"
],
"bundlesize": [
{
"path": "./packages/client/dist/xmpp.min.js.gz",
Expand Down
File renamed without changes.
50 changes: 50 additions & 0 deletions packages/bind/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
'use strict'

const xml = require('@xmpp/xml')

/*
* References
* https://xmpp.org/rfcs/rfc6120.html#bind
*/

const NS = 'urn:ietf:params:xml:ns:xmpp-bind'

function makeBindElement(resource) {
return xml('bind', {xmlns: NS}, resource && xml('resource', {}, resource))
}

function match(features) {
return features.getChild('bind', NS)
}

module.exports = function bind(streamFeatures) {
const {entity} = streamFeatures

function bind(resource) {
entity._status('binding')
return entity.plugins['iq-caller']
.set(makeBindElement(resource))
.then(result => {
const jid = result.getChildText('jid')
entity._jid(jid)
entity._status('bound')
return jid
})
}

function handleFeature() {
entity._status('bind')
return entity.isHandled('bind')
? entity.delegate('bind', resource => bind(resource))
: bind()
}

streamFeatures.use({
name: 'bind',
priority: 2500,
match,
run: () => handleFeature(),
})

return {bind}
}
17 changes: 17 additions & 0 deletions packages/bind/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "@xmpp/bind",
"description": "XMPP bind for JavaScript",
"repository": "github:xmppjs/xmpp.js",
"homepage": "https://github.com/xmppjs/xmpp.js/tree/master/packages/bind",
"bugs": "http://github.com/xmppjs/xmpp.js/issues",
"version": "0.3.0",
"license": "ISC",
"keywords": ["XMPP", "bind"],
"dependencies": {
"@xmpp/xml": "^0.3.0"
},
"engines": {
"node": ">= 6",
"npm": ">= 2"
}
}
22 changes: 17 additions & 5 deletions packages/plugins/bind/test.js → packages/bind/test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
'use strict'

/* eslint node/no-extraneous-require: 0 */

const test = require('ava')
const plugin = require('.')
const testPlugin = require('@xmpp/test/testPlugin')
const bind = require('.')
const _streamFeatures = require('@xmpp/stream-features')
const _middleware = require('@xmpp/middleware')
const _router = require('@xmpp/router')
const {context} = require('@xmpp/test')
const iqCaller = require('@xmpp/plugins/iq-caller')

test.beforeEach(t => {
t.context = testPlugin(plugin)
const ctx = context()
ctx.entity.plugin(iqCaller)
const middleware = _middleware(ctx.entity)
const router = _router(middleware)
const streamFeatures = _streamFeatures(router)
t.context = ctx
t.context.bind = bind(streamFeatures)
})

test('without resource', t => {
Expand All @@ -19,7 +31,7 @@ test('without resource', t => {
t.context.catchOutgoingSet().then(child => {
t.deepEqual(child, <bind xmlns="urn:ietf:params:xml:ns:xmpp-bind" />)
}),
t.context.plugin.bind().then(jid => {
t.context.bind.bind().then(jid => {
t.is(jid, 'foo@bar/foobar')
}),
])
Expand All @@ -41,7 +53,7 @@ test('with resource', t => {
</bind>
)
}),
t.context.plugin.bind('resource').then(jid => {
t.context.bind.bind('resource').then(jid => {
t.is(jid, 'foo@bar/foobar')
}),
])
Expand Down
3 changes: 2 additions & 1 deletion packages/client/example/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

const {xmpp, xml} =
typeof require === 'undefined' ? global.xmpp : require('..') // For you; require('@xmpp/client')

const {client} = xmpp()

// Log errors
Expand Down Expand Up @@ -55,7 +56,7 @@

// "start" opens the socket and the XML stream
client
.start('jabberfr.org') // Auto
.start('xmpp://jabberfr.org:5222') // Auto
// .start('xmpp://localhost:5222') // TCP
// .start('xmpps://localhost:5223') // TLS
// .start('ws://localhost:5280/xmpp-websocket') // Websocket
Expand Down
37 changes: 36 additions & 1 deletion packages/client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,45 @@ const websocket = require('@xmpp/websocket')
const tls = require('@xmpp/tls')
const packages = {reconnect, tcp, websocket, tls}

const _middleware = require('@xmpp/middleware')
const _router = require('@xmpp/router')
const _streamFeatures = require('@xmpp/stream-features')
const _bind = require('@xmpp/bind')
const _sasl = require('@xmpp/sasl')

const _saslPlain = require('@xmpp/sasl-plain')
const _saslScramSha1 = require('@xmpp/sasl-scram-sha-1')
const _saslAnonymous = require('@xmpp/sasl-anonymous')

const _sessionEstablishment = require('@xmpp/session-establishment')
const _starttls = require('@xmpp/starttls')

function xmpp() {
const client = new Client()
const middleware = _middleware(client)
const router = _router(middleware)
const streamFeatures = _streamFeatures(router)
const bind = _bind(streamFeatures)
const sasl = _sasl(streamFeatures)
const saslPlain = _saslPlain(sasl)
const saslScramSha1 = _saslScramSha1(sasl)
const saslAnonymous = _saslAnonymous(sasl)
const sessionEstablishment = _sessionEstablishment(streamFeatures)
const starttls = _starttls(streamFeatures)
return Object.assign(
{client},
{
client,
middleware,
router,
streamFeatures,
bind,
sasl,
saslPlain,
saslScramSha1,
saslAnonymous,
sessionEstablishment,
starttls,
},
...entries(packages)
// Ignore browserify stubs
.filter(([, v]) => typeof v === 'function')
Expand Down
11 changes: 1 addition & 10 deletions packages/client/lib/plugins.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
'use strict'

exports['stream-features'] = require('@xmpp/plugins/stream-features')
exports.bind = require('@xmpp/plugins/bind')
exports.sasl = require('@xmpp/plugins/sasl')
exports['sasl-plain'] = require('@xmpp/plugins/sasl-plain')
exports['sasl-scram-sha-1'] = require('@xmpp/plugins/sasl-scram-sha-1')
exports['sasl-anonymous'] = require('@xmpp/plugins/sasl-anonymous')
exports.starttls = require('@xmpp/plugins/starttls')
exports.resolve = require('@xmpp/plugins/resolve')
exports[
'session-establishment'
] = require('@xmpp/plugins/session-establishment')
exports['iq-caller'] = require('@xmpp/plugins/iq-caller')
14 changes: 12 additions & 2 deletions packages/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,19 @@
"version": "0.3.0",
"license": "ISC",
"dependencies": {
"@xmpp/bind": "^0.3.0",
"@xmpp/client-core": "^0.3.0",
"@xmpp/middleware": "^0.3.0",
"@xmpp/plugins": "^0.3.0",
"@xmpp/reconnect": "^0.3.0",
"@xmpp/router": "^0.3.0",
"@xmpp/sasl": "^0.3.0",
"@xmpp/sasl-anonymous": "^0.3.0",
"@xmpp/sasl-plain": "^0.3.0",
"@xmpp/sasl-scram-sha-1": "^0.3.0",
"@xmpp/session-establishment": "^0.3.0",
"@xmpp/starttls": "^0.3.0",
"@xmpp/stream-features": "^0.3.0",
"@xmpp/tcp": "^0.3.0",
"@xmpp/tls": "^0.3.0",
"@xmpp/websocket": "^0.3.0",
Expand All @@ -17,8 +27,8 @@
"browser": {
"@xmpp/tcp": false,
"@xmpp/tls": false,
"@xmpp/plugins/starttls": false,
"@xmpp/plugins/sasl-scram-sha-1": false,
"@xmpp/starttls": false,
"@xmpp/sasl-scram-sha-1": false,
"object.entries": false
},
"scripts": {
Expand Down
4 changes: 3 additions & 1 deletion packages/component/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ const entries = Object.entries || require('object.entries') // eslint-disable-li
const {Component, xml, jid} = require('@xmpp/component-core')

const reconnect = require('@xmpp/reconnect')
const packages = {reconnect}
const middleware = require('@xmpp/middleware')
const router = require('@xmpp/router')
const packages = {reconnect, middleware, router}

function xmpp() {
const component = new Component()
Expand Down
2 changes: 2 additions & 0 deletions packages/component/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
"keywords": ["XMPP", "component"],
"dependencies": {
"@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"
},
"engines": {
Expand Down
4 changes: 2 additions & 2 deletions packages/console/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@
"chalk": "^2.3.0",
"express": "^4.15.4",
"ltx": "^2.7.1",
"meow": "^3.7.0",
"meow": "^4.0.0",
"opn": "^5.1.0",
"p-map-series": "^1.0.0"
},
"devDependencies": {
"bootstrap": "^4.0.0-alpha.6",
"bootstrap": "^3.3.7",
"clipboard": "^1.7.1",
"codemirror": "^5.32.0",
"jquery": "^3.1.1",
Expand Down
5 changes: 1 addition & 4 deletions packages/middleware/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ npm install @xmpp/middleware
## Usage

```js
const {Client} = new require('@xmpp/client')
const {Client} = require('@xmpp/client')
const middleware = require('@xmpp/middlware')

const client = new Client()
Expand All @@ -30,13 +30,10 @@ app.use(ctx (ctx, next) => {
})
```


### filter


The `filter` method registers a middleware for outgoing stanzas.


```js
app.use(ctx (ctx, next) => {

Expand Down
1 change: 1 addition & 0 deletions packages/middleware/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ module.exports = function(entity) {
entity.hookOutgoing = outgoingListener

return {
entity,
incoming,
outgoing,
incomingListener,
Expand Down
54 changes: 0 additions & 54 deletions packages/plugins/bind/index.js

This file was deleted.

12 changes: 8 additions & 4 deletions packages/plugins/iq-caller/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,14 @@ module.exports = plugin('iq-caller', {
stanza.attrs.id = xid()
}

return this.entity.send(stanza).then(() => {
return new Promise((resolve, reject) =>
return Promise.all([
new Promise((resolve, reject) => {
this.handlers.set(stanza.attrs.id, [resolve, reject])
)
})
}),
this.entity.send(stanza).catch(err => {
this.handlers.remove(stanza.attrs.id)
throw err
}),
]).then(([res]) => res)
},
})
Loading

0 comments on commit 9278794

Please sign in to comment.