forked from springernature/boomcatch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
udp.js
108 lines (84 loc) · 2.86 KB
/
udp.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Copyright © 2014, 2015, 2016 Springer Nature
//
// This file is part of boomcatch.
//
// Boomcatch is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Boomcatch is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with boomcatch. If not, see <http://www.gnu.org/licenses/>.
/*globals require, exports, Buffer */
'use strict';
var check = require('check-types'),
udp = require('dgram');
exports.initialise = function (options) {
return send.bind(null, normaliseHost(options.fwdHost), normalisePort(options.fwdPort), normaliseSize(options.fwdSize));
};
function normaliseHost (host) {
return normaliseValue(host, 'unemptyString', '127.0.0.1');
}
function normaliseValue (value, test, defaultValue) {
if (check[test](value)) {
return value;
}
return defaultValue;
}
function normalisePort (port) {
return normaliseValue(port, 'positive', 8125);
}
function normaliseSize (size) {
return normaliseValue(size, 'positive', 512);
}
function send (host, port, size, data, type, separator, callback) {
var count = 0, length = 1, socket, chunks, failed;
socket = udp.createSocket('udp4');
if (data.length <= size) {
return sendToSocket(data);
}
chunks = chunkData(data, size, separator || '', []);
length = chunks.length;
chunks.forEach(sendToSocket);
function sendToSocket (data) {
var buffer = new Buffer(data);
socket.send(buffer, 0, buffer.length, port, host, finish);
}
function finish (error, bytesSent) {
if (error && !failed) {
callback(error);
failed = true;
}
count += 1;
if (count === length) {
socket.close();
if (!failed) {
callback(null, bytesSent);
}
}
}
}
function chunkData (data, size, separator, chunks) {
var i;
if (data.length <= size) {
chunks.push(data);
return chunks;
}
if (separator !== '') {
for (i = size; i > 0; i -= 1) {
if (data.substr(i, separator.length) === separator) {
return chunkDataAtIndex(data, i, separator.length, size, separator, chunks);
}
}
}
return chunkDataAtIndex(data, size, 0, size, separator, chunks);
}
function chunkDataAtIndex(data, index, gap, size, separator, chunks) {
chunks.push(data.substring(0, index));
return chunkData(data.substring(index + gap), size, separator, chunks);
}