soajs.controller 4.2.30 → 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/.dockerignore ADDED
@@ -0,0 +1,74 @@
1
+ # Logs
2
+ logs
3
+ *.log
4
+ npm-debug.log*
5
+ yarn-debug.log*
6
+ yarn-error.log*
7
+
8
+ # Runtime data
9
+ pids
10
+ *.pid
11
+ *.seed
12
+ *.pid.lock
13
+ dist
14
+
15
+ # Directory for instrumented libs generated by jscoverage/JSCover
16
+ lib-cov
17
+
18
+ # Coverage directory used by tools like istanbul
19
+ coverage
20
+
21
+ # nyc test coverage
22
+ .nyc_output
23
+
24
+ # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
25
+ .grunt
26
+
27
+ # Bower dependency directory (https://bower.io/)
28
+ bower_components
29
+
30
+ # node-waf configuration
31
+ .lock-wscript
32
+
33
+ # Compiled binary addons (https://nodejs.org/api/addons.html)
34
+ build/Release
35
+
36
+ # Dependency directories
37
+ node_modules/
38
+ jspm_packages/
39
+
40
+ # TypeScript v1 declaration files
41
+ typings/
42
+
43
+ # Optional npm cache directory
44
+ .npm
45
+
46
+ # Optional eslint cache
47
+ .eslintcache
48
+
49
+ # Optional REPL history
50
+ .node_repl_history
51
+
52
+ # Output of 'npm pack'
53
+ *.tgz
54
+
55
+ # Yarn Integrity file
56
+ .yarn-integrity
57
+
58
+ # dotenv environment variables file
59
+ .env
60
+
61
+ # next.js build output
62
+ .next
63
+
64
+ # DS_Store files automatically generated by Mac OS X
65
+ .DS_Store
66
+
67
+ # webstorm project formatting
68
+ .idea
69
+
70
+ # package-lock
71
+ package-lock.json
72
+
73
+ # GIT
74
+ .git
package/.travis.yml CHANGED
@@ -48,7 +48,7 @@ jobs:
48
48
 
49
49
  - echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin
50
50
 
51
- - docker build -t soajsorg/gateway .
51
+ - docker build -t soajsorg/gateway . --platform linux/amd64
52
52
 
53
53
  - docker images
54
54
 
package/Dockerfile CHANGED
@@ -1,4 +1,4 @@
1
- FROM soajsorg/node
1
+ FROM soajsorg/node-slim
2
2
 
3
3
  RUN mkdir -p /opt/soajs/soajs.controller/node_modules/
4
4
  WORKDIR /opt/soajs/soajs.controller/
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
  }
@@ -0,0 +1,65 @@
1
+ "use strict";
2
+
3
+ /**
4
+ * @license
5
+ * Copyright SOAJS All Rights Reserved.
6
+ *
7
+ * Use of this source code is governed by an Apache license that can be
8
+ * found in the LICENSE file at the root of this repository
9
+ */
10
+
11
+ const fs = require('fs');
12
+ let regFile = (process.env.SOAJS_PROFILE || __dirname + "/../../profiles/solo.js");
13
+
14
+ module.exports = {
15
+ "init": function () {
16
+ },
17
+ "loadData": function (dbConfiguration, envCode, param, callback) {
18
+ let error = null;
19
+ if (fs.existsSync(regFile)) {
20
+ delete require.cache[require.resolve(regFile)];
21
+ let regFileObj = require(regFile);
22
+ if (regFileObj && typeof regFileObj === 'object' && !(Object.keys(regFileObj).length === 0 && regFileObj.constructor === Object)) {
23
+ let obj = {};
24
+ obj.ENV_schema = regFileObj;
25
+ return callback(null, obj);
26
+ } else {
27
+ error = new Error('Invalid profile file: ' + regFile);
28
+ }
29
+ }
30
+ else {
31
+ error = new Error('Invalid profile path: ' + regFile);
32
+ }
33
+ return callback(error, null);
34
+ },
35
+ "registerNewService": function (dbConfiguration, serviceObj, collection, cb) {
36
+ return cb(null);
37
+ },
38
+ "addUpdateServiceIP": function (dbConfiguration, hostObj, cb) {
39
+ return cb(null, true);
40
+ },
41
+ "loadOtherEnvHosts": function (param, cb) {
42
+ let obj = {};
43
+ return cb(null, obj);
44
+ },
45
+ "loadProfile": function (envFrom, cb) {
46
+ let regFileObj = {};
47
+ let registry = {
48
+ "timeLoaded": new Date().getTime(),
49
+ "name": envFrom,
50
+ "environment": envFrom,
51
+ "profileOnly": true,
52
+ "coreDB": {
53
+ "provision": regFileObj
54
+ }
55
+ };
56
+ return cb(null, registry);
57
+ },
58
+ "getAllEnvironments": function (cb) {
59
+ let obj = {};
60
+ return cb(null, obj);
61
+ },
62
+ "addUpdateEnvControllers": function (param, cb) {
63
+ return cb(null);
64
+ }
65
+ };
@@ -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,91 @@ 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 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
- req.soajs.log.debug("lastSeen", errorMsg);
74
- }
75
- });
73
+ });
76
74
 
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 + "]");
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
  };
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.30",
4
+ "version": "4.2.32",
5
5
  "author": {
6
6
  "name": "soajs team",
7
7
  "email": "team@soajs.org"
@@ -0,0 +1,50 @@
1
+ 'use strict';
2
+
3
+ module.exports = {
4
+ "services": {
5
+ "controller": {
6
+ "maxPoolSize": 100,
7
+ "authorization": true,
8
+ "requestTimeout": 30,
9
+ "requestTimeoutRenewal": 0
10
+ },
11
+ "config": {
12
+ "awareness": {},
13
+ "key": {
14
+ "algorithm": "aes256",
15
+ "password": "soajs key lal massa"
16
+ },
17
+ "logger": {
18
+ "src": true,
19
+ "level": "debug",
20
+ "formatter": {
21
+ "outputMode": "long"
22
+ }
23
+ },
24
+ "cors": {
25
+ "enabled": true,
26
+ "origin": "*",
27
+ "credentials": "true",
28
+ "methods": "GET,HEAD,PUT,PATCH,POST,DELETE",
29
+ "headers": "key,soajsauth,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type",
30
+ "maxage": 1728000
31
+ },
32
+ "oauth": {
33
+ "grants": [
34
+ "password",
35
+ "refresh_token"
36
+ ],
37
+ "accessTokenLifetime": 7200,
38
+ "refreshTokenLifetime": 1209600,
39
+ "debug": false
40
+ },
41
+ "ports": {
42
+ "controller": 4000,
43
+ "maintenanceInc": 1000,
44
+ "randomInc": 100
45
+ },
46
+ "cookie": {},
47
+ "session": {}
48
+ }
49
+ }
50
+ };
package/release ADDED
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env bash
2
+ IMAGE="gateway"
3
+ DFILE="Dockerfile"
4
+
5
+ echo "Release ${IMAGE} from ${DFILE}"
6
+
7
+ PACKAGE_VERSION=$(cat package.json | grep version | head -1 | awk -Fn '{ print $2 }' | sed 's/[n:",]//g' | tr -d '[[:space:]]')
8
+ MAJOR_VERSION=$(echo ${PACKAGE_VERSION} | awk -F. '{ print $1 }')
9
+ echo "Package version ${PACKAGE_VERSION}"
10
+ echo "Major version ${MAJOR_VERSION}"
11
+
12
+ docker build -t soajsorg/${IMAGE} -f ${DFILE} . --platform linux/amd64
13
+ docker tag soajsorg/${IMAGE}:latest soajsorg/${IMAGE}:${PACKAGE_VERSION}
14
+ docker tag soajsorg/${IMAGE}:latest soajsorg/${IMAGE}:${MAJOR_VERSION}.x
15
+
16
+ docker push soajsorg/${IMAGE}
17
+ docker push soajsorg/${IMAGE}:${PACKAGE_VERSION}
18
+ docker push soajsorg/${IMAGE}:${MAJOR_VERSION}.x