-
Notifications
You must be signed in to change notification settings - Fork 373
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
137 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
6 changes: 3 additions & 3 deletions
6
packages/reconnect/package.json → packages/plugin-reconnect/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} |