-
Notifications
You must be signed in to change notification settings - Fork 375
/
index.js
93 lines (82 loc) · 2.7 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
"use strict";
const { xml, jid, Client } = require("@xmpp/client-core");
const getDomain = require("./lib/getDomain");
const _reconnect = require("@xmpp/reconnect");
const _websocket = require("@xmpp/websocket");
const _tcp = require("@xmpp/tcp");
const _tls = require("@xmpp/tls");
const _middleware = require("@xmpp/middleware");
const _streamFeatures = require("@xmpp/stream-features");
const _iqCaller = require("@xmpp/iq/caller");
const _iqCallee = require("@xmpp/iq/callee");
const _resolve = require("@xmpp/resolve");
// Stream features - order matters and define priority
const _starttls = require("@xmpp/starttls/client");
const _sasl = require("@xmpp/sasl");
const _resourceBinding = require("@xmpp/resource-binding");
const _sessionEstablishment = require("@xmpp/session-establishment");
const _streamManagement = require("@xmpp/stream-management");
// SASL mechanisms - order matters and define priority
const scramsha1 = require("@xmpp/sasl-scram-sha-1");
const plain = require("@xmpp/sasl-plain");
const anonymous = require("@xmpp/sasl-anonymous");
function client(options = {}) {
const { resource, credentials, username, password, ...params } = options;
const { domain, service } = params;
if (!domain && service) {
params.domain = getDomain(service);
}
const entity = new Client(params);
const reconnect = _reconnect({ entity });
const websocket = _websocket({ entity });
const tcp = _tcp({ entity });
const tls = _tls({ entity });
const middleware = _middleware({ entity });
const streamFeatures = _streamFeatures({ middleware });
const iqCaller = _iqCaller({ middleware, entity });
const iqCallee = _iqCallee({ middleware, entity });
const resolve = _resolve({ entity });
// Stream features - order matters and define priority
const starttls = _starttls({ streamFeatures });
const sasl = _sasl({ streamFeatures }, credentials || { username, password });
const streamManagement = _streamManagement({
streamFeatures,
entity,
middleware,
});
const resourceBinding = _resourceBinding(
{ iqCaller, streamFeatures },
resource,
);
const sessionEstablishment = _sessionEstablishment({
iqCaller,
streamFeatures,
});
// SASL mechanisms - order matters and define priority
const mechanisms = Object.entries({
scramsha1,
plain,
anonymous,
}).map(([k, v]) => ({ [k]: v(sasl) }));
return Object.assign(entity, {
entity,
reconnect,
tcp,
websocket,
tls,
middleware,
streamFeatures,
iqCaller,
iqCallee,
resolve,
starttls,
sasl,
resourceBinding,
sessionEstablishment,
streamManagement,
mechanisms,
});
}
module.exports.xml = xml;
module.exports.jid = jid;
module.exports.client = client;