soajs.controller 4.2.31 → 4.2.33

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
- const urlObj = new URL(uri);
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
- const urlObj = new URL(uri);
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(),
@@ -21,92 +21,94 @@ module.exports = () => {
21
21
  return (req, res, next) => {
22
22
  next();
23
23
 
24
- let lastSeen = get(["soajs", "registry", "custom", "gateway", "value", "lastSeen"], req);
25
- if (lastSeen && lastSeen.active) {
24
+ if (req.soajs.uracDriver) {
25
+ let lastSeen = get(["soajs", "registry", "custom", "gateway", "value", "lastSeen"], req);
26
+ if (lastSeen && lastSeen.active) {
26
27
 
27
- const item = {
28
- "name": lastSeen.serviceName || "urac",
29
- "version": lastSeen.serviceVersion || "3",
30
- "api": lastSeen.api || "/user/last/seen"
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
- let userId = null;
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
- if (userId) {
42
- req.soajs.awareness.getHost(item.name, item.version, (host) => {
43
- if (host) {
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
- let options = {
48
- "uri": "http://" + item.host + ":" + item.port + item.api,
49
- "json": true,
50
- "method": "POST"
51
- };
52
- if (req.headers && req.headers.soajsinjectobj) {
53
- options.headers = { "soajsinjectobj": req.headers.soajsinjectobj };
54
- }
55
- httpRequest(options)
56
- .then(() => { })
57
- .catch(({ error, body }) => {
58
- if (error) {
59
- req.soajs.log.debug("lastSeen failed for [" + item.name + "@" + item.version + " " + item.api + "]");
60
- req.soajs.log.debug("lastSeen " + error.message);
61
- } else if (!body.result) {
62
- let errorMsg = "";
63
- if (body.errors && body.errors && body.errors.details && body.errors.details.length > 0) {
64
- body.errors.details.forEach((detail) => {
65
- if (errorMsg === "") {
66
- errorMsg += detail.message;
67
- }
68
- else {
69
- errorMsg += " - " + detail.message;
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 {
60
+ req.soajs.log.debug("lastSeen ", body);
72
61
  }
73
- req.soajs.log.debug("lastSeen", errorMsg);
74
- }
75
- });
62
+ // else if (!body.result) {
63
+ // let errorMsg = "";
64
+ // if (body.errors && body.errors && body.errors.details && body.errors.details.length > 0) {
65
+ // body.errors.details.forEach((detail) => {
66
+ // if (errorMsg === "") {
67
+ // errorMsg += detail.message;
68
+ // }
69
+ // else {
70
+ // errorMsg += " - " + detail.message;
71
+ // }
72
+ // });
73
+ // }
74
+ // req.soajs.log.debug("lastSeen", errorMsg);
75
+ // }
76
+ });
76
77
 
77
- // let options = {
78
- // method: "post",
79
- // uri: "http://" + item.host + ":" + item.port + item.api,
80
- // json: true
81
- // };
82
- // if (req.headers && req.headers.soajsinjectobj) {
83
- // options.headers = { "soajsinjectobj": req.headers.soajsinjectobj };
84
- // }
85
- // request(options, (error, response, body) => {
86
- // if (error) {
87
- // req.soajs.log.debug("lastSeen failed for [" + item.name + "@" + item.version + " " + item.api + "]");
88
- // req.soajs.log.debug("lastSeen " + error.message);
89
- // } else if (!body.result) {
90
- // let errorMsg = "";
91
- // if (body.errors && body.errors && body.errors.details && body.errors.details.length > 0) {
92
- // body.errors.details.forEach((detail) => {
93
- // if (errorMsg === "") {
94
- // errorMsg += detail.message;
95
- // }
96
- // else {
97
- // errorMsg += " - " + detail.message;
98
- // }
99
- // });
100
- // }
101
- // req.soajs.log.debug("lastSeen", errorMsg);
102
- // }
103
- // });
104
- } else {
105
- req.soajs.log.debug("lastSeen failed with no host for [" + item.name + "@" + item.version + " " + item.api + "]");
106
- }
107
- });
108
- } else {
109
- req.soajs.log.debug("lastSeen failed with no userId for [" + item.name + "@" + item.version + " " + item.api + "]");
78
+ // let options = {
79
+ // method: "post",
80
+ // uri: "http://" + item.host + ":" + item.port + item.api,
81
+ // json: true
82
+ // };
83
+ // if (req.headers && req.headers.soajsinjectobj) {
84
+ // options.headers = { "soajsinjectobj": req.headers.soajsinjectobj };
85
+ // }
86
+ // request(options, (error, response, body) => {
87
+ // if (error) {
88
+ // req.soajs.log.debug("lastSeen failed for [" + item.name + "@" + item.version + " " + item.api + "]");
89
+ // req.soajs.log.debug("lastSeen " + error.message);
90
+ // } else if (!body.result) {
91
+ // let errorMsg = "";
92
+ // if (body.errors && body.errors && body.errors.details && body.errors.details.length > 0) {
93
+ // body.errors.details.forEach((detail) => {
94
+ // if (errorMsg === "") {
95
+ // errorMsg += detail.message;
96
+ // }
97
+ // else {
98
+ // errorMsg += " - " + detail.message;
99
+ // }
100
+ // });
101
+ // }
102
+ // req.soajs.log.debug("lastSeen", errorMsg);
103
+ // }
104
+ // });
105
+ } else {
106
+ req.soajs.log.debug("lastSeen failed with no host for [" + item.name + "@" + item.version + " " + item.api + "]");
107
+ }
108
+ });
109
+ } else {
110
+ req.soajs.log.debug("lastSeen failed with no userId for [" + item.name + "@" + item.version + " " + item.api + "]");
111
+ }
110
112
  }
111
113
  }
112
114
  };
package/mw/mt/urac.js CHANGED
@@ -16,7 +16,7 @@ const lib = require("./lib.js");
16
16
 
17
17
  function Urac(param) {
18
18
  let _self = this;
19
-
19
+
20
20
  _self.soajs = param.soajs;
21
21
  _self.userRecord = null;
22
22
  _self.user_ACL = null;
@@ -70,7 +70,7 @@ Urac.prototype.init = function (cb) {
70
70
  }
71
71
  }
72
72
  else if (_self.id) {
73
- uracDriver.getRecord(_self.soajs, {id: _self.id.toString(), username: _self.username}, function (err, record) {
73
+ uracDriver.getRecord(_self.soajs, { id: _self.id.toString(), username: _self.username }, function (err, record) {
74
74
  if (record) {
75
75
  _self.userRecord = record;
76
76
  _self.resolveACL(() => {
@@ -78,7 +78,9 @@ Urac.prototype.init = function (cb) {
78
78
  });
79
79
  } else {
80
80
  if (err && err.msg) {
81
+ const code = err.code;
81
82
  err = new Error(err.msg);
83
+ err.code = code;
82
84
  }
83
85
  return cb(err, null);
84
86
  }
@@ -141,19 +143,19 @@ Urac.prototype.getProfile = function (_ALL) {
141
143
  "profile": _self.userRecord.profile,
142
144
  "tenant": _self.userRecord.tenant
143
145
  };
144
-
146
+
145
147
  if (_self.userRecord.socialLogin) {
146
148
  urac.socialLogin = {
147
149
  "strategy": _self.userRecord.socialLogin.strategy,
148
150
  "id": _self.userRecord.socialLogin.id
149
151
  };
150
152
  }
151
-
153
+
152
154
  if (_ALL) {
153
155
  if (_self.userRecord.socialLogin) {
154
156
  urac.socialLogin.accessToken = _self.userRecord.socialLogin.accessToken;
155
157
  }
156
-
158
+
157
159
  urac.config = _self.userRecord.config;
158
160
  urac.groupsConfig = _self.userRecord.groupsConfig;
159
161
  }
@@ -162,7 +164,7 @@ Urac.prototype.getProfile = function (_ALL) {
162
164
  urac = {
163
165
  "_id": _self.userRecord._id,
164
166
  "username": _self.userRecord.userId,
165
- "tenant": {"id": _self.userRecord.tId}
167
+ "tenant": { "id": _self.userRecord.tId }
166
168
  };
167
169
  }
168
170
  else if (_self.userRecord) {
@@ -182,7 +184,7 @@ Urac.prototype.getProfile = function (_ALL) {
182
184
  */
183
185
  Urac.prototype.getAcl = function () {
184
186
  let _self = this;
185
-
187
+
186
188
  if (_self.user_ACL && _self.user_ACL.acl) {
187
189
  return _self.user_ACL.acl;
188
190
  } else {
@@ -195,7 +197,7 @@ Urac.prototype.getAcl = function () {
195
197
  */
196
198
  Urac.prototype.getAclAllEnv = function () {
197
199
  let _self = this;
198
-
200
+
199
201
  if (_self.user_ACL && _self.user_ACL.acl_all_env) {
200
202
  return _self.user_ACL.acl_all_env;
201
203
  } else {
@@ -214,13 +216,13 @@ Urac.prototype.getConfig = function () {
214
216
  return null;
215
217
  }
216
218
  let config = null;
217
-
219
+
218
220
  if (_self.userRecord.config && _self.userRecord.config.keys) {
219
221
  if (_self.userRecord.config.keys[key] && _self.userRecord.config.keys[key].config) {
220
222
  config = _self.userRecord.config.keys[key].config;
221
223
  }
222
224
  }
223
-
225
+
224
226
  return config;
225
227
  };
226
228
 
package/mw/mt/utils.js CHANGED
@@ -590,7 +590,7 @@ let utils = {
590
590
  obj.req.soajs.uracDriver = new UracDriver({ "soajs": obj.req.soajs, "oauth": obj.req.oauth });
591
591
  obj.req.soajs.uracDriver.init((error) => {
592
592
  if (error && !roaming) {
593
- obj.req.soajs.log.error(error.message);
593
+ obj.req.soajs.log.error(error.message, error.code);
594
594
  return cb(146);
595
595
  }
596
596
  let userServiceConf = obj.req.soajs.uracDriver.getConfig();
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "soajs.controller",
3
3
  "description": "soajs multi tenant API gateway",
4
- "version": "4.2.31",
4
+ "version": "4.2.33",
5
5
  "author": {
6
6
  "name": "soajs team",
7
7
  "email": "team@soajs.org"
@@ -34,9 +34,10 @@
34
34
  "useragent": "2.3.0"
35
35
  },
36
36
  "devDependencies": {
37
- "@babel/core": "7.26.0",
38
- "@babel/plugin-transform-runtime": "7.25.9",
39
- "@babel/preset-env": "7.26.0",
37
+ "@babel/core": "7.27.1",
38
+ "@babel/plugin-transform-runtime": "7.27.1",
39
+ "@babel/preset-env": "7.27.2",
40
+ "@babel/runtime": "7.27.1",
40
41
  "express": "4.21.1",
41
42
  "grunt": "1.6.1",
42
43
  "grunt-babel": "8.0.0",