@mimik/local 1.6.2 → 4.4.4

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/common.js CHANGED
@@ -5,6 +5,8 @@ const mIDConfigFile = '../mIDConfig.json';
5
5
  const sumoLogFile = '../sumoLog.json';
6
6
  const s3LogFile = '../s3Log.json';
7
7
  const kinesisLogFile = '../kinesisLog.json';
8
+ const locationFile = '../locationConfig.json';
9
+ const keyFile = '../key.json';
8
10
  const customerConfigFile = '../customerConfig.json';
9
11
  const APIProvider = 'https://api.swaggerhub.com/apis';
10
12
  const swaggerExt = 'swagger.json';
@@ -14,17 +16,28 @@ const AWS_S3 = 'awsS3';
14
16
  const AWS_KINESIS = 'awsKinesis';
15
17
  const SUMOLOGIC = 'sumologic';
16
18
  const ALL = 'all'; // legacy support
17
- const ALL_MODES = [AWS_S3, AWS_KINESIS, SUMOLOGIC, ALL];
19
+ const NONE = 'none';
20
+ const ALL_MODES = [AWS_S3, AWS_KINESIS, SUMOLOGIC, ALL, NONE];
21
+
22
+ const SWAGGER_SEP = '_';
23
+ const TEST = 'test';
24
+
25
+ const SYSTEM_NAME = 'System';
26
+ const TOKEN_SERVICE = 'mST';
27
+ const IT_REGISTRY = 'mIT';
28
+ const IDENTITY_SERVICE = 'mID';
18
29
 
19
30
  const testJsonFile = './server-test.json';
20
31
  const shellFile = './server-start.sh';
21
32
  const startFile = './local/start.json';
22
33
  const testStartFile = './local/testStart.json';
23
34
  const exampleStartFile = './local/start-example.json';
35
+ const exampleTestStartFile = './local/testStart-example.json';
24
36
 
25
37
  const LITERAL = true;
38
+ const IGNORE = '!';
26
39
 
27
- const correlationId = 'test-local';
40
+ const correlationId = `test-local@0/${new Date().toISOString()}`;
28
41
 
29
42
  const DEFAULT_MST = {
30
43
  basePath: '/mST/v1',
@@ -102,6 +115,13 @@ const DEFAULT_KINESISLOG = {
102
115
  streamNameError: '--- default streamName Error ---',
103
116
  streamNameOther: '--- default streamName Other ---',
104
117
  };
118
+ const DEFAULT_KEY = {
119
+ apiKey: null,
120
+ };
121
+ const DEFAULT_LOCATION = {
122
+ url: null,
123
+ key: null,
124
+ };
105
125
 
106
126
  const ERR_CHECK = '✘';
107
127
  const SUCCESS_CHECK = '✔︎';
@@ -114,6 +134,8 @@ module.exports = {
114
134
  sumoLogFile,
115
135
  s3LogFile,
116
136
  kinesisLogFile,
137
+ locationFile,
138
+ keyFile,
117
139
  customerConfigFile,
118
140
  APIProvider,
119
141
  swaggerExt,
@@ -123,14 +145,19 @@ module.exports = {
123
145
  startFile,
124
146
  testStartFile,
125
147
  exampleStartFile,
148
+ exampleTestStartFile,
126
149
  correlationId,
127
150
  LITERAL,
151
+ IGNORE,
152
+ TEST,
128
153
  DEFAULT_MST,
129
154
  DEFAULT_MIT,
130
155
  DEFAULT_MID,
131
156
  DEFAULT_SUMOLOG,
132
157
  DEFAULT_S3LOG,
133
158
  DEFAULT_KINESISLOG,
159
+ DEFAULT_KEY,
160
+ DEFAULT_LOCATION,
134
161
  DUMMY_MST,
135
162
  DUMMY_CUSTOMER_CODE,
136
163
  DUMMY_MIT,
@@ -141,5 +168,11 @@ module.exports = {
141
168
  AWS_KINESIS,
142
169
  SUMOLOGIC,
143
170
  ALL,
171
+ NONE,
144
172
  ALL_MODES,
173
+ SWAGGER_SEP,
174
+ SYSTEM_NAME,
175
+ TOKEN_SERVICE,
176
+ IT_REGISTRY,
177
+ IDENTITY_SERVICE,
145
178
  };
package/lib/commonExt.js CHANGED
@@ -1,38 +1,46 @@
1
1
  const _ = require('lodash');
2
2
 
3
3
  // duplicate of functions in mST lib/common.js
4
- const allType = 'all';
5
- const clusterType = 'cluster';
4
+ const ALL_TYPE = 'all';
5
+ const CLUSTER_TYPE = 'cluster';
6
6
 
7
7
  const inList = (type, list) => _.findIndex(list, (listItem) => listItem.type === type);
8
8
 
9
- const getClientTypes = (client, type) => {
10
- const types = [];
9
+ const setServiceInfo = (sharedWith, type, scopes, customer) => {
10
+ const result = { type };
11
11
 
12
- client.targets.forEach((target) => {
13
- if (!target.except || !type || target.except.indexOf(type) === -1) {
14
- if (target.type !== clusterType) types.push(target.type);
15
- else if (type) types.push(type);
16
- }
17
- });
18
- return types;
12
+ if (customer) result.customer = customer;
13
+ if (sharedWith) result.sharedWith = sharedWith;
14
+ if (scopes) result.scopes = scopes;
15
+ return result;
19
16
  };
20
17
 
21
- const getTargets = (type, custConfig) => {
22
- const index = inList(type, custConfig);
23
- let targets = [];
18
+ const getAllTargets = (serviceType, custConfig, serviceIndex) => {
19
+ const index = serviceIndex || serviceIndex === 0 ? serviceIndex : inList(serviceType, custConfig);
20
+ const targets = [];
21
+ const serviceInfo = (target, st) => {
22
+ if (target.type !== CLUSTER_TYPE) return setServiceInfo(null, target.type, target.scopes, target.customer);
23
+ return setServiceInfo(null, st, target.scopes);
24
+ };
24
25
 
25
26
  if (index !== -1) {
26
- targets = getClientTypes(custConfig[index], type);
27
- }
28
- const allIndex = inList(allType, custConfig);
29
-
30
- if (allIndex !== -1) {
31
- targets = _.union(targets, getClientTypes(custConfig[allIndex], type));
27
+ custConfig[index].targets.forEach((target) => targets.push(serviceInfo(target, serviceType)));
28
+ const allIndex = inList(ALL_TYPE, custConfig);
29
+
30
+ if (allIndex !== -1) {
31
+ custConfig[allIndex].targets.forEach((allTarget) => {
32
+ if (!allTarget.except || !allTarget.except.includes(serviceType)) {
33
+ const allTargetIndex = inList(allTarget.type === CLUSTER_TYPE ? serviceType : allTarget.type, targets);
34
+
35
+ if (allTargetIndex === -1) targets.push(serviceInfo(allTarget, serviceType));
36
+ else targets[allTargetIndex].scopes = _.union(targets[allTargetIndex].scopes, allTarget.scopes);
37
+ }
38
+ });
39
+ }
32
40
  }
33
41
  return targets;
34
42
  };
35
43
 
36
44
  module.exports = {
37
- getTargets,
45
+ getAllTargets,
38
46
  };
package/lib/helpers.js CHANGED
@@ -1,4 +1,4 @@
1
- /* eslint-disable prefer-template, no-console */
1
+ /* eslint-disable prefer-template, no-console, no-process-env, @mimik/document-env/validate-document-env */
2
2
  const colors = require('colors');
3
3
  const fs = require('fs');
4
4
  const ip = require('ip');
@@ -16,14 +16,14 @@ colors.setTheme({
16
16
  const exitError = (regType, error, filename) => {
17
17
  let details = `{ statusCode: ${error.statusCode || 500}, message: ${error.message || error}`;
18
18
 
19
- if (filename) details = `, ${details}, filename: ${filename}`;
19
+ if (filename) details = `${details}, filename: ${filename}`;
20
20
  details = `${details} }`;
21
21
  console.error(`${regType}status: ` + 'error'.error + ', ' + details.info);
22
22
  process.exit(1);
23
23
  };
24
24
 
25
25
  const start2shell = (start) => {
26
- let result = '';
26
+ let result = '#!/bin/sh\n';
27
27
 
28
28
  Object.keys(start).forEach((key) => {
29
29
  if (key[0] !== '/' && key[1] !== '/') {
@@ -32,6 +32,7 @@ const start2shell = (start) => {
32
32
  else result = `${result}export ${key}="${start[key]}"\n`;
33
33
  }
34
34
  });
35
+ result = `${result}node src/index\n`;
35
36
  return result;
36
37
  };
37
38
 
@@ -64,9 +65,12 @@ const start2process = (start) => {
64
65
  };
65
66
  const setDomainName = (serverType, regType, domainName, port, literal) => {
66
67
  if (!domainName) exitError(regType, `domainName must exist in ${serverType}Config.json`);
67
- if ((domainName === 'localhost' || net.isIP(domainName)) && !port) exitError(regType, `port missing in ${serverType}Config.json`);
68
+ const needPort = domainName === 'localhost' || net.isIP(domainName);
69
+
70
+ if (needPort && !port) exitError(regType, `port missing in ${serverType}Config.json`);
68
71
  if (domainName === 'localhost' && !literal) return `${ip.address()}:${port}`;
69
- return `${domainName}:${port}`;
72
+ if (needPort) return `${domainName}:${port}`;
73
+ return domainName;
70
74
  };
71
75
  const baseUrl = (serverType, regType, config, literal) => {
72
76
  if (!config.basePath) exitError(regType, `basePath missing in ${serverType}Config.json`);
@@ -87,11 +91,13 @@ const baseUrl = (serverType, regType, config, literal) => {
87
91
  *``` javascript
88
92
  * {
89
93
  * sourceFilename: {PATH<string>}, // Source filename to use if altFilename does not retrieve any file.
94
+ * sourceFilenameSupp: {PATH<string}, // Source filename supplementatry to by use with source filename.
90
95
  * altFilename: {PATH<string>}, // Alternate filename to use if filename does not retrieve any file.
91
96
  * default: {object}, // default content if no file exist
92
97
  * }
93
98
  *````
94
99
  * Will exit 1 if there is an error.
100
+ * Source filename supp is only used when source filename exist. If environement variables between source filename and source filename supplementatry exist the environement variable in source filename will be commented out.
95
101
  *
96
102
  */
97
103
  const retrieve = (regType, filename, options) => {
@@ -107,22 +113,69 @@ const retrieve = (regType, filename, options) => {
107
113
  }
108
114
  return readFile;
109
115
  };
116
+
117
+ const readMerge = (fname, fnameSupp) => {
118
+ const parse = (rawFile, rawFilename) => {
119
+ try { return json.parse(rawFile); }
120
+ catch (errParse) { return exitError(regType, errParse, rawFilename); }
121
+ };
122
+
123
+ const merge = (origRawFile, parsedFile, origRawFileSupp, parsedFileSupp) => {
124
+ const keysParsedFile = Object.keys(parsedFile);
125
+ let rawFile = origRawFile;
126
+ let rawFileSupp = origRawFileSupp;
127
+
128
+ Object.keys(parsedFileSupp).forEach((key) => {
129
+ if (keysParsedFile.includes(key)) {
130
+ const regex = new RegExp(`\n[ \t]+"${key}"`);
131
+
132
+ rawFile = rawFile.replace(regex, `\n// "${key}"`);
133
+ }
134
+ });
135
+ rawFile = rawFile.substring(0, rawFile.length - 2);
136
+ rawFileSupp = rawFileSupp.substring(1);
137
+ return `${rawFile},\n//-- test\n${rawFileSupp}`;
138
+ };
139
+
140
+ let readFileRaw;
141
+ let readFileSuppRaw;
142
+
143
+ try { readFileRaw = fs.readFileSync(fname).toString(); }
144
+ catch (errFname) {
145
+ if (errFname.code !== 'ENOENT') exitError(regType, errFname, fname);
146
+ throw errFname;
147
+ }
148
+ const readFile = parse(readFileRaw, fname);
149
+
150
+ if (!fnameSupp) return readFile;
151
+ try { readFileSuppRaw = fs.readFileSync(fnameSupp).toString(); }
152
+ catch (errFnameSupp) {
153
+ if (errFnameSupp.code !== 'ENOENT') exitError(regType, errFnameSupp, fnameSupp);
154
+ return readFile;
155
+ }
156
+
157
+ return parse(merge(readFileRaw, readFile, readFileSuppRaw, parse(readFileSuppRaw, fnameSupp)), `${fname} + ${fnameSupp}`);
158
+ };
159
+
110
160
  const write = (fname, content) => {
111
161
  try { fs.writeFileSync(fname, json.stringify(content, null, 2)); }
112
162
  catch (err) { exitError(regType, err, fname); }
113
163
  return content;
114
164
  };
115
- const readSourceDefault = (fsource, content, fname) => {
116
- let readFile;
117
165
 
118
- try { readFile = read(fsource); }
166
+ const readSourceDefault = (fSource, fSourceSupp, content, fname) => {
167
+ try {
168
+ const readFile = readMerge(fSource, fSourceSupp);
169
+
170
+ if (!fSourceSupp) console.log('- using ' + fSource.warn + ' for ' + fname.info);
171
+ else console.log('- using ' + fSource.warn + ' + ' + fSourceSupp.warn + ' for ' + fname.info);
172
+ return readFile;
173
+ }
119
174
  catch (err) {
120
- if (!content) exitError(regType, { statusCode: 404, message: 'no files or default' }, fname);
121
- console.log('- using ' + 'default'.warn + ' for ' + filename.info);
175
+ if (!content) exitError(regType, { statusCode: 404, message: `no files or default (${err.message})` }, fname);
176
+ console.log('- using ' + 'default'.warn + ' for ' + fname.info);
122
177
  return content;
123
178
  }
124
- console.log('- using ' + filename.info);
125
- return readFile;
126
179
  };
127
180
 
128
181
  try {
@@ -131,28 +184,28 @@ const retrieve = (regType, filename, options) => {
131
184
  return result;
132
185
  }
133
186
  catch (errFilename) {
134
- if (!options) exitError(regType, { statusCode: 404, message: 'no options' }, filename);
187
+ if (!options) exitError(regType, { statusCode: 404, message: `no options (${errFilename})` }, filename);
135
188
  if (options.altFilename) {
136
189
  try {
137
190
  result = read(options.altFilename);
138
191
  console.log('- using ' + options.altFilename.info);
139
- return result; // if test cannot create a test start if start is available
192
+ return result;
140
193
  }
141
194
  catch (errAltFilename) {
142
- if (options.sourceFilename) result = readSourceDefault(options.sourceFilename, options.default, filename);
143
- else {
144
- if (!options.default) exitError(regType, { statusCode: 404, message: 'no files or default' }, filename);
195
+ if (!options.sourceFileName) {
196
+ if (!options.default) exitError(regType, { statusCode: 404, message: `no files or default (${errAltFilename})` }, filename);
145
197
  console.log('- using ' + 'default'.warn + ' for ' + filename.info);
146
- result = options.default;
198
+ return write(filename, options.default);
147
199
  }
200
+ return write(filename, readSourceDefault(options.sourceFilename, options.sourceFilenameSupp, options.default, filename));
148
201
  }
149
- return write(filename, result);
150
202
  }
151
203
  if (!options.sourceFilename) {
204
+ if (!options.default) exitError(regType, { statusCode: 404, message: `no files or default (${errFilename.message})` }, filename);
152
205
  console.log('- using ' + 'default'.warn + ' for ' + filename.info);
153
206
  return write(filename, options.default);
154
207
  }
155
- return write(filename, readSourceDefault(options.sourceFilename, options.default, filename));
208
+ return write(filename, readSourceDefault(options.sourceFilename, options.sourceFilenameSupp, options.default, filename));
156
209
  }
157
210
  };
158
211
 
@@ -0,0 +1,36 @@
1
+ const axios = require('axios');
2
+ const http = require('http');
3
+
4
+ const rp = (options) => axios(options)
5
+ .then((res) => {
6
+ if (options.resolveWithFullResponse) return res;
7
+ return res.data;
8
+ })
9
+ .catch((err) => {
10
+ const { response } = err;
11
+
12
+ if (!response) {
13
+ const error = new Error('system Error');
14
+
15
+ error.statusCode = 500;
16
+ error.title = http.STATUS_CODES[error.statusCode];
17
+ error.info = {
18
+ code: err.code,
19
+ address: err.address,
20
+ port: err.port,
21
+ syscall: err.syscall,
22
+ };
23
+ throw error;
24
+ }
25
+ const { data } = response;
26
+ const error = new Error(data ? data.message || response.statusText : response.statusText);
27
+
28
+ error.statusCode = response.status;
29
+ error.title = response.statusText;
30
+ if (data && data.info) error.info = data.info;
31
+ throw error;
32
+ });
33
+
34
+ module.exports = {
35
+ rp,
36
+ };