Skip to content

Commit

Permalink
@xmpp/plugin-iq-caller (#354)
Browse files Browse the repository at this point in the history
  • Loading branch information
sonnyp committed Dec 10, 2016
1 parent 6b7720d commit bf2ab99
Show file tree
Hide file tree
Showing 11 changed files with 137 additions and 9 deletions.
2 changes: 1 addition & 1 deletion packages/component/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

XMPP component for JavaScript

Includes `@xmpp/component-core` and `@xmpp/reconnect`.
Includes `@xmpp/component-core` and `@xmpp/plugin-reconnect`.

## Install

Expand Down
2 changes: 1 addition & 1 deletion packages/component/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const ComponentCore = require('@xmpp/component-core')
const reconnect = require('@xmpp/reconnect')
const reconnect = require('@xmpp/plugin-reconnect')

class Component extends ComponentCore {
constructor () {
Expand Down
2 changes: 1 addition & 1 deletion packages/component/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
],
"dependencies": {
"@xmpp/component-core": "^0.0.3",
"@xmpp/reconnect": "^0.0.1"
"@xmpp/plugin-reconnect": "^0.0.0"
}
}
5 changes: 4 additions & 1 deletion packages/connection/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,9 @@ class Connection extends EventEmitter {

send (element) {
return new Promise((resolve, reject) => {
this.write(element.root()).then(resolve, reject)
element = element.root()
this.emit('send', element)
this.write(element).then(resolve, reject)
})
}

Expand Down Expand Up @@ -263,6 +265,7 @@ class Connection extends EventEmitter {
if (!this.plugins[plugin.name]) {
this.plugins[plugin.name] = plugin.plugin(this)
}
if (this.plugins[plugin.name].register) this.plugins[plugin.name].register()
return this.plugins[plugin.name]
}

Expand Down
67 changes: 67 additions & 0 deletions packages/plugin-iq-caller/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
'use strict'

const xml = require('@xmpp/xml')
const stanzaRouter = require('@xmpp/plugin-stanza-router')

const makeId = function () {
return Math.random().toString().substr(2)
}

const match = function (stanza) {
return (
stanza.is('iq') &&
stanza.attrs.id &&
(stanza.attrs.type === 'error' || stanza.attrs.type === 'result')
)
}

function plugin (entity) {
const handlers = new Map()

const router = entity.plugin(stanzaRouter)
router.add(match, (stanza) => {
const {id} = stanza.attrs

const handler = handlers.get(id)
if (!handler) return

if (stanza.attrs.type === 'error') {
handler[1](stanza.getChild('error'))
} else {
handler[0](stanza.children[0])
}

handlers.delete(id)
})

return {
entity,
handlers,
get (to, el, options) {
const iq = xml`<iq type='get' to='${to}'/>`
iq.cnode(el)
return this.request(iq, options)
},
set (to, el, options) {
const iq = xml`<iq type='set' to='${to}'/>`
iq.cnode(el)
return this.request(iq, options)
},
request (stanza, options = {}) {
return new Promise((resolve, reject) => {
stanza = stanza.root()
if (!stanza.attrs.id) stanza.attrs.id = makeId()

this.handlers.set(stanza.attrs.id, [resolve, reject])

entity.send(stanza)
})
}
}
}

module.exports = {
name: 'iq-caller',
plugin,
id: makeId
}
19 changes: 19 additions & 0 deletions packages/plugin-iq-caller/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "@xmpp/plugin-iq-caller",
"description": "XMPP iq caller for JavaScript",
"repository": "github:node-xmpp/node-xmpp",
"homepage": "https://github.com/node-xmpp/node-xmpp/tree/master/packages/plugin-iq-caller",
"bugs": "http://github.com/node-xmpp/node-xmpp/issues",
"version": "0.0.0",
"license": "ISC",
"keywords": [
"XMPP",
"iq",
"caller",
"request"
],
"dependencies": {
"@xmpp/plugin-router": "^0.0.0",
"@xmpp/xml": "^0.1.2"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ Included in `@xmpp/component`.
## Install

```
npm install @xmpp/reconnect
npm install @xmpp/plugin-reconnect
```

## Usage

```javascript
const reconnect = require('@xmpp/reconnect')
const reconnect = require('@xmpp/plugin-reconnect')
const entity = ...

const plugin = entity.plugin(reconnect)
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "@xmpp/reconnect",
"name": "@xmpp/plugin-reconnect",
"description": "XMPP reconnect for JavaScript",
"repository": "github:node-xmpp/node-xmpp",
"homepage": "https://github.com/node-xmpp/node-xmpp/tree/master/packages/reconnect",
"homepage": "https://github.com/node-xmpp/node-xmpp/tree/master/packages/plugin-reconnect",
"bugs": "http://github.com/node-xmpp/node-xmpp/issues",
"version": "0.0.1",
"version": "0.0.0",
"license": "ISC",
"keywords": [
"XMPP",
Expand Down
26 changes: 26 additions & 0 deletions packages/plugin-router/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict'

function plugin (entity) {
const routes = new Map()
return {
entity,
routes,
register () {
this.entity.on('element', this.handler)
},
unregister () {
this.entity.removeListener('element', this.handler)
},
add (match, handle) {
routes.set(match, handle)
},
handler (stanza) {
routes.forEach((handle, match) => {
if (match(stanza)) handle(stanza, entity)
})
}
}
}

module.exports.plugin = plugin
module.exports.name = 'router'
13 changes: 13 additions & 0 deletions packages/plugin-router/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "@xmpp/plugin-router",
"description": "XMPP element router for JavaScript",
"repository": "github:node-xmpp/node-xmpp",
"homepage": "https://github.com/node-xmpp/node-xmpp/tree/master/packages/plugin-router",
"bugs": "http://github.com/node-xmpp/node-xmpp/issues",
"version": "0.0.0",
"license": "ISC",
"keywords": [
"XMPP",
"router"
]
}

0 comments on commit bf2ab99

Please sign in to comment.