-
Notifications
You must be signed in to change notification settings - Fork 376
/
Copy pathbind2.js
55 lines (44 loc) · 1.23 KB
/
bind2.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
import xml from "@xmpp/xml";
const NS = "urn:xmpp:bind:0";
export default function bind2({ sasl2, entity }, tag) {
const features = new Map();
sasl2.use(
NS,
async (element) => {
if (!element.is("bind", NS)) return;
tag = typeof tag === "function" ? await tag() : tag;
const sessionFeatures = await getSessionFeatures({ element, features });
return xml(
"bind",
{ xmlns: "urn:xmpp:bind:0" },
tag && xml("tag", null, tag),
...sessionFeatures,
);
},
(element) => {
if (!element.is("bound")) return;
entity._ready(false);
for (const child of element.getChildElements()) {
const feature = features.get(child.getNS());
feature?.[1]?.(child);
}
},
);
return {
use(ns, req, res) {
features.set(ns, [req, res]);
},
};
}
function getSessionFeatures({ element, features }) {
const promises = [];
const inline = element.getChild("inline");
if (!inline) return promises;
for (const element of inline.getChildElements()) {
const xmlns = element.attrs.var;
const feature = features.get(xmlns);
if (!feature) continue;
promises.push(feature[0](element));
}
return Promise.all(promises);
}