-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjidParser.js
64 lines (56 loc) · 1.58 KB
/
jidParser.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
define(function () {
var cache = {};
// TODO: Implement Stringprep
var nodeprep = function (node) {
return node.toLowerCase();
};
var domainprep = function (domain) {
return domain.replace(/(.*)\.$/, "$1").toLowerCase();
};
var resourceprep = function (resource) {
return resource;
};
var stringToBytesArray = function (string) {
var character,
stack,
result = []
;
for (var i = 0; i < string.length; i++) {
character = string.charCodeAt(i);
stack = [];
do {
stack.unshift(character & 0xFF);
character = character >> 8;
} while (character);
result = result.concat(stack);
}
return result;
};
var parse = function (jid) {
var nodeEnd = jid.indexOf("@");
var domainEnd = jid.indexOf("/", nodeEnd);
var node = nodeprep(jid.substring(0, nodeEnd));
var domain = domainprep(jid.substring(nodeEnd + 1, domainEnd === -1 ? jid.length : domainEnd));
var resource = resourceprep(domainEnd === -1 ? "" : jid.substring(domainEnd + 1));
var bare = node.length ? (node + "@" + domain) : domain;
var full = resource.length ? (bare + "/" + resource) : bare;
var valid = domain.length &&
(nodeEnd === -1 || (node.length > 0 && stringToBytesArray(node).length < 1024)) &&
(domainEnd === -1 || (resource.length > 0 && stringToBytesArray(resource).length < 1024));
return valid ? {
// Portions
node: node,
domain: domain,
resource: resource,
// Composites
bare: bare,
full: full
} : null;
};
return function (jid) {
if (!Object.hasOwnProperty.call(cache, jid)) {
cache[jid] = parse(jid);
}
return cache[jid];
};
});