-
Notifications
You must be signed in to change notification settings - Fork 376
/
Copy pathindex.js
38 lines (31 loc) · 1009 Bytes
/
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
import { Component, xml, jid } from "@xmpp/component-core";
import _reconnect from "@xmpp/reconnect";
import _middleware from "@xmpp/middleware";
import _iqCaller from "@xmpp/iq/caller.js";
import _iqCallee from "@xmpp/iq/callee.js";
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,
});
}
export { xml, jid, component };