forked from Supereg/homebridge-http-base
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.js
147 lines (116 loc) · 4.76 KB
/
http.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
const request = require("request");
const async = require("async");
const delayPattern = /^delay\(\d+\)*$/;
const numberPattern = /\d+/;
const ExecutionStrategy = Object.freeze({
PARALLEL: async.parallel,
SERIES: async.series,
});
let multipleUrlExecutionStrategy = ExecutionStrategy.PARALLEL;
module.exports = {
setMultipleUrlExecutionStrategy: function (strategyString) {
strategyString = strategyString.toUpperCase();
const strategy = ExecutionStrategy[strategyString];
if (strategy) {
multipleUrlExecutionStrategy = strategy;
return true;
}
return false;
},
isHttpSuccessCode: function (statusCode) {
return Math.floor(statusCode / 100) === 2;
},
httpRequest: function (urlObject, callback) {
let url = urlObject.url;
let body = urlObject.body;
let auth = undefined;
if (urlObject.auth && urlObject.auth.username && urlObject.auth.password) {
auth = {};
auth.username = urlObject.auth.username;
auth.password = urlObject.auth.password;
if (typeof urlObject.auth.sendImmediately !== "undefined")
auth.sendImmediately = urlObject.auth.sendImmediately;
}
for (let i = 2; i < arguments.length; i++) {
const element = arguments[i];
if (typeof element !== "object")
continue;
let args = element;
if (element.constructor === Object)
args = [element];
/** @namespace argument.searchValue */
for (let j = 0; j < args.length; j++) {
const argument = args[j];
url = url.replace(argument.searchValue, argument.replacer);
if (body)
body = body.replace(argument.searchValue, argument.replacer);
}
}
request(
{
url: url,
body: body,
method: urlObject.method || "GET",
headers: urlObject.headers,
auth: auth,
strictSSL: urlObject.strictSSL,
timeout: urlObject.requestTimeout || 20000,
},
(error, response, body) => {
callback(error, response, body);
}
)
},
multipleHttpRequests: function (urlObjectArray, callback, argument) {
if (urlObjectArray.length === 0)
throw new Error("Empty urlObject array");
const taskArray = [];
const executionCounter = [];
for (let i = 0; i < urlObjectArray.length; i++) {
const urlObject = urlObjectArray[i];
if (executionCounter[i] === undefined)
executionCounter[i] = urlObject.repeat;
taskArray.push(function (callback, delayed) { // callback gets (error, response, body)
if (urlObject.url.startsWith("delay") && delayPattern.test(urlObject.url)) {
if (multipleUrlExecutionStrategy !== ExecutionStrategy.SERIES) {
console.warn("There was a 'delay' method specified but execution is unaffected because of unsuitable execution strategy!");
callback();
return;
}
const delay = parseInt(urlObject.url.match(numberPattern)[0]);
// execute callback from async framework => finish urlObject
setTimeout(() => callback(), delay);
return;
}
if (!delayed && urlObject.delayBeforeExecution > 0) {
const self = arguments.callee.bind(this);
// execute the current method a second time though delayed=true
setTimeout(() => self(callback, true), urlObject.delayBeforeExecution);
return;
}
this.httpRequest(urlObject, callback, argument);
}.bind(this));
executionCounter[i]--;
if (executionCounter[i] > 0)
i--; // repeat current urlObject
}
multipleUrlExecutionStrategy(async.reflectAll(taskArray), (ignored, results) => {
const callbackArray = [];
for (let i = 0; i < results.length; i++) {
const element = results[i];
if (element.error) {
callbackArray.push({
error: element.error
});
}
else if (element.value) {
callbackArray.push({
response: element.value[0],
body: element.value[1]
});
}
}
callback(callbackArray);
});
}
};