-
Notifications
You must be signed in to change notification settings - Fork 371
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
stream-management: add @xmpp/stream-management package
- Loading branch information
Showing
16 changed files
with
920 additions
and
2 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
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,11 @@ | ||
# stream-management | ||
|
||
[Stream Management](https://xmpp.org/extensions/xep-0198.html) for `@xmpp/client`. | ||
|
||
Included and enabled in `@xmpp/client`. | ||
|
||
Supports Node.js and browsers. | ||
|
||
Responds to ack requests and resumes connection uppon disconnect whenever possible. | ||
|
||
Does not support requesting acks yet. |
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,108 @@ | ||
'use strict' | ||
|
||
const xml = require('@xmpp/xml') | ||
const StanzaError = require('@xmpp/middleware/lib/StanzaError') | ||
|
||
// https://xmpp.org/extensions/xep-0198.html | ||
|
||
const NS = 'urn:xmpp:sm:3' | ||
|
||
async function enable(entity, resume, max) { | ||
const response = await entity.sendReceive( | ||
xml('enable', {xmlns: NS, max, resume: resume ? 'true' : undefined}) | ||
) | ||
|
||
if (!response.is('enabled')) { | ||
throw StanzaError.fromElement(response) | ||
} | ||
|
||
return response | ||
} | ||
|
||
async function resume(entity, h, previd) { | ||
const response = await entity.sendReceive( | ||
xml('resume', {xmlns: NS, h, previd}) | ||
) | ||
|
||
if (!response.is('resumed')) { | ||
throw StanzaError.fromElement(response) | ||
} | ||
|
||
return response | ||
} | ||
|
||
module.exports = function({streamFeatures, entity, middleware}) { | ||
let address = null | ||
|
||
const sm = { | ||
allowResume: true, | ||
preferredMaximum: null, | ||
enabled: false, | ||
id: '', | ||
outbound: 0, | ||
inbound: 0, | ||
max: null, | ||
} | ||
|
||
entity.on('online', jid => { | ||
address = jid | ||
sm.outbound = 0 | ||
sm.inbound = 0 | ||
sm.enabled = false | ||
}) | ||
|
||
entity.on('offline', () => { | ||
address = null | ||
sm.outbound = 0 | ||
sm.inbound = 0 | ||
sm.enabled = false | ||
}) | ||
|
||
middleware.use((context, next) => { | ||
const {stanza} = context | ||
if (['presence', 'message', 'iq'].includes(stanza.name)) { | ||
sm.inbound += 1 | ||
} else if (stanza.is('r', NS)) { | ||
// > When an <r/> element ("request") is received, the recipient MUST acknowledge it by sending an <a/> element to the sender containing a value of 'h' that is equal to the number of stanzas handled by the recipient of the <r/> element. | ||
entity.send(xml('a', {xmlns: NS, h: sm.inbound})).catch(() => {}) | ||
} else if (stanza.is('a', NS)) { | ||
// > When a party receives an <a/> element, it SHOULD keep a record of the 'h' value returned as the sequence number of the last handled outbound stanza for the current stream (and discard the previous value). | ||
sm.outbound = stanza.attrs.h | ||
} | ||
|
||
return next() | ||
}) | ||
|
||
streamFeatures.use('sm', NS, async (context, next) => { | ||
// Resuming | ||
if (sm.id && address) { | ||
try { | ||
await resume(entity, sm.inbound, sm.id) | ||
entity.jid = address | ||
entity.status = 'online' | ||
return true | ||
// eslint-disable-next-line no-unused-vars | ||
} catch (err) { | ||
sm.id = '' | ||
address = null | ||
sm.enabled = false | ||
} | ||
} | ||
|
||
// Enabling | ||
|
||
// Resource binding first | ||
await next() | ||
|
||
const promiseEnable = enable(entity, sm.allowResume, sm.preferredMaximum) | ||
|
||
// > The counter for an entity's own sent stanzas is set to zero and started after sending either <enable/> or <enabled/>. | ||
sm.outbound = 0 | ||
|
||
const response = await promiseEnable | ||
sm.inbound = 0 | ||
sm.enabled = true | ||
sm.id = response.attrs.id | ||
sm.max = response.attrs.max | ||
}) | ||
} |
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,25 @@ | ||
{ | ||
"name": "@xmpp/stream-management", | ||
"description": "XMPP stream management for JavaScript", | ||
"repository": "github:xmppjs/xmpp.js", | ||
"homepage": "https://github.com/xmppjs/xmpp.js/tree/master/packages/stream-management", | ||
"bugs": "http://github.com/xmppjs/xmpp.js/issues", | ||
"version": "0.9.0", | ||
"license": "ISC", | ||
"keywords": [ | ||
"XMPP", | ||
"stream", | ||
"management" | ||
], | ||
"dependencies": { | ||
"@xmpp/middleware": "^0.9.0", | ||
"@xmpp/xml": "^0.9.0" | ||
}, | ||
"engines": { | ||
"node": ">= 10.0.0", | ||
"yarn": ">= 1.0.0" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
} | ||
} |
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,43 @@ | ||
'use strict' | ||
|
||
const test = require('ava') | ||
const {mockClient, promise, timeout} = require('@xmpp/test') | ||
|
||
test('mandatory', async t => { | ||
const {entity} = mockClient() | ||
|
||
entity.mockInput( | ||
<features xmlns="http://etherx.jabber.org/streams"> | ||
<session xmlns="urn:ietf:params:xml:ns:xmpp-session" /> | ||
</features> | ||
) | ||
|
||
entity.scheduleIncomingResult() | ||
|
||
await entity.catchOutgoingSet().then(child => { | ||
t.deepEqual(child, <session xmlns="urn:ietf:params:xml:ns:xmpp-session" />) | ||
return child | ||
}) | ||
|
||
await promise(entity, 'online') | ||
}) | ||
|
||
test('optional', async t => { | ||
const {entity} = mockClient() | ||
|
||
entity.mockInput( | ||
<features xmlns="http://etherx.jabber.org/streams"> | ||
<session xmlns="urn:ietf:params:xml:ns:xmpp-session"> | ||
<optional /> | ||
</session> | ||
</features> | ||
) | ||
|
||
const promiseSend = promise(entity, 'send') | ||
|
||
await promise(entity, 'online') | ||
|
||
await timeout(promiseSend, 0).catch(err => { | ||
t.is(err.name, 'TimeoutError') | ||
}) | ||
}) |
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
Oops, something went wrong.