soajs.controller 4.2.31 → 4.2.32
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/request.js
CHANGED
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
11
|
const http = require('http');
|
|
12
|
+
const https = require('https');
|
|
12
13
|
|
|
13
14
|
function checkStatus(uri) {
|
|
14
15
|
return new Promise((resolve, reject) => {
|
|
@@ -59,8 +60,9 @@ function httpRequestLight({ uri, data = null, method = 'GET', headers = null, js
|
|
|
59
60
|
let options = {};
|
|
60
61
|
const requestDataString = data ? (json ? JSON.stringify(data) : data.toString()) : '';
|
|
61
62
|
|
|
63
|
+
let urlObj = {};
|
|
62
64
|
try {
|
|
63
|
-
|
|
65
|
+
urlObj = new URL(uri);
|
|
64
66
|
options = {
|
|
65
67
|
hostname: urlObj.hostname,
|
|
66
68
|
port: urlObj.port,
|
|
@@ -84,7 +86,13 @@ function httpRequestLight({ uri, data = null, method = 'GET', headers = null, js
|
|
|
84
86
|
return reject(error); // Reject with error and null data for request errors
|
|
85
87
|
}
|
|
86
88
|
}
|
|
87
|
-
const req = http.request(options);
|
|
89
|
+
// const req = http.request(options);
|
|
90
|
+
let req = null;
|
|
91
|
+
if (urlObj.protocol === "https:") {
|
|
92
|
+
req = https.request(options);
|
|
93
|
+
} else {
|
|
94
|
+
req = http.request(options);
|
|
95
|
+
}
|
|
88
96
|
|
|
89
97
|
req.on('response', (res) => { // Listen for the 'response' event
|
|
90
98
|
if (res.statusCode < 200 || res.statusCode >= 300) {
|
|
@@ -145,14 +153,35 @@ function httpRequestLight({ uri, data = null, method = 'GET', headers = null, js
|
|
|
145
153
|
});
|
|
146
154
|
}
|
|
147
155
|
|
|
148
|
-
function httpRequest({ uri, data = null, method = 'GET', headers = null, json = true }) {
|
|
156
|
+
function httpRequest({ uri, data = null, body = null, qs = null, method = 'GET', headers = null, json = true }) {
|
|
149
157
|
return new Promise((resolve, reject) => {
|
|
158
|
+
data = data || body; // to be compatible with request package
|
|
159
|
+
|
|
150
160
|
let onResponse = false;
|
|
151
161
|
let options = {};
|
|
162
|
+
let urlObj = {};
|
|
152
163
|
|
|
153
164
|
const requestDataString = data ? (json ? JSON.stringify(data) : data.toString()) : '';
|
|
154
165
|
try {
|
|
155
|
-
|
|
166
|
+
urlObj = new URL(uri);
|
|
167
|
+
|
|
168
|
+
if (qs) {
|
|
169
|
+
// Merge query parameters into the path
|
|
170
|
+
const existingParams = new URLSearchParams(urlObj.search);
|
|
171
|
+
const mergedParams = new URLSearchParams();
|
|
172
|
+
|
|
173
|
+
// Add existing params
|
|
174
|
+
existingParams.forEach((value, key) => {
|
|
175
|
+
mergedParams.append(key, value);
|
|
176
|
+
});
|
|
177
|
+
// Add/override queryParams
|
|
178
|
+
for (const key in qs) {
|
|
179
|
+
if (qs.hasOwnProperty(key)) {
|
|
180
|
+
mergedParams.set(key, qs[key]);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
urlObj.search = mergedParams.toString();
|
|
184
|
+
}
|
|
156
185
|
|
|
157
186
|
options = {
|
|
158
187
|
"hostname": urlObj.hostname,
|
|
@@ -177,7 +206,13 @@ function httpRequest({ uri, data = null, method = 'GET', headers = null, json =
|
|
|
177
206
|
return reject({ error: error, body: null }); // Reject with error and null data for request errors
|
|
178
207
|
}
|
|
179
208
|
}
|
|
180
|
-
const req = http.request(options);
|
|
209
|
+
// const req = http.request(options);
|
|
210
|
+
let req = null;
|
|
211
|
+
if (urlObj.protocol === "https:") {
|
|
212
|
+
req = https.request(options);
|
|
213
|
+
} else {
|
|
214
|
+
req = http.request(options);
|
|
215
|
+
}
|
|
181
216
|
|
|
182
217
|
req.on('response', (res) => { // Listen for the 'response' event
|
|
183
218
|
let responseData = '';
|
|
@@ -246,7 +281,14 @@ function proxyRequest(req, res, options, extraOptions = {}) {
|
|
|
246
281
|
return new Promise((resolve, reject) => {
|
|
247
282
|
let onResponse = false;
|
|
248
283
|
|
|
249
|
-
const proxyReq = http.request(options);
|
|
284
|
+
// const proxyReq = http.request(options);
|
|
285
|
+
let proxyReq = null;
|
|
286
|
+
if (options.protocol === "https:") {
|
|
287
|
+
proxyReq = https.request(options);
|
|
288
|
+
} else {
|
|
289
|
+
proxyReq = http.request(options);
|
|
290
|
+
}
|
|
291
|
+
|
|
250
292
|
if (extraOptions && extraOptions.fetchProxReq) {
|
|
251
293
|
extraOptions.fetchProxReq(proxyReq);
|
|
252
294
|
}
|
|
@@ -308,7 +350,14 @@ function proxyRequestMonitor(req, res, options, extraOptions = {}) {
|
|
|
308
350
|
return new Promise((resolve, reject) => {
|
|
309
351
|
let onResponse = false;
|
|
310
352
|
|
|
311
|
-
const proxyReq = http.request(options);
|
|
353
|
+
// const proxyReq = http.request(options);
|
|
354
|
+
let proxyReq = null;
|
|
355
|
+
if (options.protocol === "https:") {
|
|
356
|
+
proxyReq = https.request(options);
|
|
357
|
+
} else {
|
|
358
|
+
proxyReq = http.request(options);
|
|
359
|
+
}
|
|
360
|
+
|
|
312
361
|
if (extraOptions && extraOptions.fetchProxReq) {
|
|
313
362
|
extraOptions.fetchProxReq(proxyReq);
|
|
314
363
|
}
|
|
@@ -44,6 +44,7 @@ module.exports = (configuration) => {
|
|
|
44
44
|
|
|
45
45
|
requestConfig = {
|
|
46
46
|
"hostname": urlObj.hostname,
|
|
47
|
+
"protocol": urlObj.protocol,
|
|
47
48
|
"port": urlObj.port,
|
|
48
49
|
"path": fullPath, // Use the full path with query parameters
|
|
49
50
|
"method": req.method.toUpperCase(),
|
|
@@ -65,7 +66,7 @@ module.exports = (configuration) => {
|
|
|
65
66
|
}
|
|
66
67
|
|
|
67
68
|
delete requestConfig.headers.host;
|
|
68
|
-
|
|
69
|
+
|
|
69
70
|
req.soajs.log.debug(requestConfig);
|
|
70
71
|
|
|
71
72
|
const extraOptions = {
|
|
@@ -159,6 +159,7 @@ module.exports = (configuration) => {
|
|
|
159
159
|
const urlObj = new URL(obj.uri);
|
|
160
160
|
requestOptions = {
|
|
161
161
|
"hostname": urlObj.hostname,
|
|
162
|
+
"protocol": urlObj.protocol,
|
|
162
163
|
"port": urlObj.port,
|
|
163
164
|
"path": urlObj.pathname + urlObj.search,
|
|
164
165
|
"method": req.method.toUpperCase(),
|
package/mw/lastSeen/index.js
CHANGED
|
@@ -21,92 +21,91 @@ module.exports = () => {
|
|
|
21
21
|
return (req, res, next) => {
|
|
22
22
|
next();
|
|
23
23
|
|
|
24
|
-
|
|
25
|
-
|
|
24
|
+
if (req.soajs.uracDriver) {
|
|
25
|
+
let lastSeen = get(["soajs", "registry", "custom", "gateway", "value", "lastSeen"], req);
|
|
26
|
+
if (lastSeen && lastSeen.active) {
|
|
26
27
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
28
|
+
const item = {
|
|
29
|
+
"name": lastSeen.serviceName || "urac",
|
|
30
|
+
"version": lastSeen.serviceVersion || "3",
|
|
31
|
+
"api": lastSeen.api || "/user/last/seen"
|
|
32
|
+
};
|
|
32
33
|
|
|
33
|
-
|
|
34
|
-
if (req.soajs.uracDriver) {
|
|
34
|
+
let userId = null;
|
|
35
35
|
let uracObj = req.soajs.uracDriver.getProfile();
|
|
36
36
|
if (uracObj) {
|
|
37
37
|
userId = uracObj._id;
|
|
38
38
|
}
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
item.host = host;
|
|
45
|
-
item.port = req.soajs.registry.services[item.name].port;
|
|
39
|
+
if (userId) {
|
|
40
|
+
req.soajs.awareness.getHost(item.name, item.version, (host) => {
|
|
41
|
+
if (host) {
|
|
42
|
+
item.host = host;
|
|
43
|
+
item.port = req.soajs.registry.services[item.name].port;
|
|
46
44
|
|
|
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
|
-
|
|
45
|
+
let options = {
|
|
46
|
+
"uri": "http://" + item.host + ":" + item.port + item.api,
|
|
47
|
+
"json": true,
|
|
48
|
+
"method": "POST"
|
|
49
|
+
};
|
|
50
|
+
if (req.headers && req.headers.soajsinjectobj) {
|
|
51
|
+
options.headers = { "soajsinjectobj": req.headers.soajsinjectobj };
|
|
52
|
+
}
|
|
53
|
+
httpRequest(options)
|
|
54
|
+
.then(() => { })
|
|
55
|
+
.catch(({ error, body }) => {
|
|
56
|
+
if (error) {
|
|
57
|
+
req.soajs.log.debug("lastSeen failed for [" + item.name + "@" + item.version + " " + item.api + "]");
|
|
58
|
+
req.soajs.log.debug("lastSeen " + error.message);
|
|
59
|
+
} else if (!body.result) {
|
|
60
|
+
let errorMsg = "";
|
|
61
|
+
if (body.errors && body.errors && body.errors.details && body.errors.details.length > 0) {
|
|
62
|
+
body.errors.details.forEach((detail) => {
|
|
63
|
+
if (errorMsg === "") {
|
|
64
|
+
errorMsg += detail.message;
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
errorMsg += " - " + detail.message;
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
req.soajs.log.debug("lastSeen", errorMsg);
|
|
72
72
|
}
|
|
73
|
-
|
|
74
|
-
}
|
|
75
|
-
});
|
|
73
|
+
});
|
|
76
74
|
|
|
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
|
-
|
|
75
|
+
// let options = {
|
|
76
|
+
// method: "post",
|
|
77
|
+
// uri: "http://" + item.host + ":" + item.port + item.api,
|
|
78
|
+
// json: true
|
|
79
|
+
// };
|
|
80
|
+
// if (req.headers && req.headers.soajsinjectobj) {
|
|
81
|
+
// options.headers = { "soajsinjectobj": req.headers.soajsinjectobj };
|
|
82
|
+
// }
|
|
83
|
+
// request(options, (error, response, body) => {
|
|
84
|
+
// if (error) {
|
|
85
|
+
// req.soajs.log.debug("lastSeen failed for [" + item.name + "@" + item.version + " " + item.api + "]");
|
|
86
|
+
// req.soajs.log.debug("lastSeen " + error.message);
|
|
87
|
+
// } else if (!body.result) {
|
|
88
|
+
// let errorMsg = "";
|
|
89
|
+
// if (body.errors && body.errors && body.errors.details && body.errors.details.length > 0) {
|
|
90
|
+
// body.errors.details.forEach((detail) => {
|
|
91
|
+
// if (errorMsg === "") {
|
|
92
|
+
// errorMsg += detail.message;
|
|
93
|
+
// }
|
|
94
|
+
// else {
|
|
95
|
+
// errorMsg += " - " + detail.message;
|
|
96
|
+
// }
|
|
97
|
+
// });
|
|
98
|
+
// }
|
|
99
|
+
// req.soajs.log.debug("lastSeen", errorMsg);
|
|
100
|
+
// }
|
|
101
|
+
// });
|
|
102
|
+
} else {
|
|
103
|
+
req.soajs.log.debug("lastSeen failed with no host for [" + item.name + "@" + item.version + " " + item.api + "]");
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
} else {
|
|
107
|
+
req.soajs.log.debug("lastSeen failed with no userId for [" + item.name + "@" + item.version + " " + item.api + "]");
|
|
108
|
+
}
|
|
110
109
|
}
|
|
111
110
|
}
|
|
112
111
|
};
|