1 | module.exports = compact2string = function (buf) {
|
2 | switch(buf.length) {
|
3 | case 6:
|
4 | return buf[0] + "." + buf[1] + "." + buf[2] + "." + buf[3] + ":" + buf.readUInt16BE(4);
|
5 | break;
|
6 | case 18:
|
7 | var hexGroups = [];
|
8 | for(var i = 0; i < 8; i++) {
|
9 | hexGroups.push(buf.readUInt16BE(i * 2).toString(16));
|
10 | }
|
11 | return "[" + hexGroups.join(":") + "]:" + buf.readUInt16BE(16);
|
12 | default:
|
13 | throw new Error("Invalid Compact IP/PORT, It should contain 6 or 18 bytes");
|
14 | }
|
15 | };
|
16 |
|
17 | compact2string.multi = function (buf) {
|
18 | if(buf.length % 6 !== 0)
|
19 | throw new Error("buf length isn't multiple of compact IP/PORTs (6 bytes)");
|
20 |
|
21 | var output = [];
|
22 | for (var i = 0; i <= buf.length - 1; i = i + 6) {
|
23 | output.push(compact2string(buf.slice(i, i + 6)));
|
24 | }
|
25 |
|
26 | return output;
|
27 | };
|
28 |
|
29 | compact2string.multi6 = function (buf) {
|
30 | if(buf.length % 18 !== 0)
|
31 | throw new Error("buf length isn't multiple of compact IP6/PORTs (18 bytes)");
|
32 |
|
33 | var output = [];
|
34 | for (var i = 0; i <= buf.length - 1; i = i + 18) {
|
35 | output.push(compact2string(buf.slice(i, i + 18)));
|
36 | }
|
37 |
|
38 | return output;
|
39 | };
|