forked from xmppjs/xmpp.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
42 lines (34 loc) · 1.08 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
"use strict";
const { Component, xml, jid } = require("@xmpp/component-core");
const _reconnect = require("@xmpp/reconnect");
const _middleware = require("@xmpp/middleware");
const _iqCaller = require("@xmpp/iq/caller");
const _iqCallee = require("@xmpp/iq/callee");
function component(options) {
const { password, service, domain } = options;
const entity = new Component({ service, domain });
const reconnect = _reconnect({ entity });
const middleware = _middleware({ entity });
const iqCaller = _iqCaller({ entity, middleware });
const iqCallee = _iqCallee({ entity, middleware });
entity.on("open", async (el) => {
try {
const { id } = el.attrs;
await (typeof password === "function"
? password((creds) => entity.authenticate(id, creds))
: entity.authenticate(id, password));
} catch (err) {
entity.emit("error", err);
}
});
return Object.assign(entity, {
entity,
reconnect,
middleware,
iqCaller,
iqCallee,
});
}
module.exports.xml = xml;
module.exports.jid = jid;
module.exports.component = component;