-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
54 lines (41 loc) · 1.6 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
var Opener = require('./build/Release/nnb');
var url = require('url');
var _ = require('lodash');
var NNB = function (params) {
var options = _.cloneDeep(params) || {};
var urlObj;
var self = this;
if (!options.concurrency) {
throw new Error("Concurrency is a mandatory parameter");
}
if (options.url) {
urlObj = url.parse(options.url);
if (urlObj.protocol === 'http:') {
options.port = options.port || 80;
} else {
throw new Error('Unknown protocol');
}
options.host = urlObj.host;
options.path = urlObj.path + (urlObj.query || '');
}
options.method = (options.method || 'GET').toUpperCase();
options.headers = options.headers || '';
var headers = options.headers || {};
headers['Host'] = options.host;
if (options.data) {
headers['Content-Length'] = options.data.length * 2;
}
options.headers = Object.keys(headers).reduce(function (prev, headerName) {
return prev + headerName + ': ' + headers[headerName] + '\r\n';
}, '') + '\r\n';
//options.headers = 'Host:' + options.host + '\r\n' + (options.headers ? options.headers + '\r\n' : '') + '\r\n';
options.headersOnly = options.headersOnly ? 1 : 0;
options.data = options.data || '';
Object.keys(options).forEach(function (paramName) {
self[paramName] = options[paramName];
});
};
NNB.prototype.go = function (callback) {
return Opener.open(this.concurrency, this.method, this.host, this.path, this.port, this.headers, this.data, this.headersOnly, callback);
};
module.exports = NNB;