@thzero/library_server 0.16.8 → 0.16.10
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/package.json +3 -3
- package/repository/index.js +61 -38
- package/service/utility.js +37 -101
package/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@thzero/library_server",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.16.
|
|
4
|
+
"version": "0.16.10",
|
|
5
5
|
"version_major": 0,
|
|
6
6
|
"version_minor": 16,
|
|
7
|
-
"version_patch":
|
|
8
|
-
"version_date": "
|
|
7
|
+
"version_patch": 10,
|
|
8
|
+
"version_date": "01/02/2023",
|
|
9
9
|
"description": "An opinionated library of common functionality to bootstrap an API application using MongoDb and Firebase. Currently either Fastify or Koa can be used as the web server.",
|
|
10
10
|
"author": "thZero",
|
|
11
11
|
"license": "MIT",
|
package/repository/index.js
CHANGED
|
@@ -12,10 +12,13 @@ class Repository {
|
|
|
12
12
|
return this._injector.getService(LibraryCommonServiceConstants.InjectorKeys.SERVICE_CONFIG)
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
-
_enforce(clazz, method, value, name, correlationId) {
|
|
15
|
+
_enforce(clazz, method, value, name, correlationId, message) {
|
|
16
16
|
if (!value) {
|
|
17
|
-
|
|
18
|
-
|
|
17
|
+
if (!String.isNullOrEmpty(message))
|
|
18
|
+
message = `${name} is invalid.`;
|
|
19
|
+
|
|
20
|
+
this._logger.error(clazz, method, message, null, correlationId);
|
|
21
|
+
const error = Error(message, true);
|
|
19
22
|
error.correlationId = correlationId;
|
|
20
23
|
throw error;
|
|
21
24
|
}
|
|
@@ -23,71 +26,91 @@ class Repository {
|
|
|
23
26
|
|
|
24
27
|
_enforceNotEmpty(clazz, method, value, name, correlationId) {
|
|
25
28
|
if (String.isNullOrEmpty(value)) {
|
|
26
|
-
this._logger.error(clazz, method,
|
|
27
|
-
const error = Error(
|
|
29
|
+
this._logger.error(clazz, method, `${name} is empty.`, null, correlationId);
|
|
30
|
+
const error = Error(`${name} is empty.`, true);
|
|
28
31
|
error.correlationId = correlationId;
|
|
29
32
|
throw error;
|
|
30
33
|
}
|
|
31
34
|
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
if (
|
|
35
|
-
this._logger.error(clazz, method, `
|
|
36
|
-
const error = Error(`
|
|
35
|
+
|
|
36
|
+
_enforceNotEmptyEither(clazz, method, value1, value2, name1, name2, correlationId) {
|
|
37
|
+
if (String.isNullOrEmpty(value1) && String.isNullOrEmpty(value2)) {
|
|
38
|
+
this._logger.error(clazz, method, `Either ${name1} or ${name2} is empty.`, null, correlationId);
|
|
39
|
+
const error = Error(`Either ${name1} or ${name2} is empty.`, true);
|
|
37
40
|
error.correlationId = correlationId;
|
|
38
41
|
throw error;
|
|
39
42
|
}
|
|
40
43
|
}
|
|
41
44
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
45
|
+
_enforceNotEmptyMultiple(clazz, method, values, names, correlationId) {
|
|
46
|
+
const valid = true;
|
|
47
|
+
for (const value of values)
|
|
48
|
+
valid &= String.isNullOrEmpty(value);
|
|
49
|
+
if (!valid) {
|
|
50
|
+
names = names.join(', ');
|
|
51
|
+
this._logger.error(clazz, method, `None of the fields are not null: ${names}`, null, correlationId);
|
|
52
|
+
const error = Error(`None of the fields are not null: ${names}`, true);
|
|
53
|
+
error.correlationId = correlationId;
|
|
54
|
+
throw error;
|
|
46
55
|
}
|
|
47
|
-
|
|
48
|
-
return this._success(correlationId);
|
|
49
56
|
}
|
|
50
57
|
|
|
51
58
|
_enforceNotEmptyResponse(clazz, method, value, name, correlationId) {
|
|
52
59
|
if (String.isNullOrEmpty(value)) {
|
|
53
|
-
this._logger.error(clazz, method,
|
|
54
|
-
return Response.error(clazz, method,
|
|
60
|
+
this._logger.error(clazz, method, `${name} is empty.`, null, correlationId);
|
|
61
|
+
return Response.error(clazz, method, `${name} is empty.`, null, null, null, correlationId);
|
|
55
62
|
}
|
|
56
63
|
|
|
57
|
-
return this.
|
|
64
|
+
return this._successResponse(value, correlationId);
|
|
58
65
|
}
|
|
59
66
|
|
|
60
|
-
|
|
61
|
-
if (!value) {
|
|
62
|
-
this._logger.error(clazz, method,
|
|
63
|
-
|
|
67
|
+
_enforceNotNull(clazz, method, value, name, correlationId) {
|
|
68
|
+
if (!value || value === undefined) {
|
|
69
|
+
this._logger.error(clazz, method, `${name} is null.`, null, correlationId);
|
|
70
|
+
const error = Error(`${name} is null.`, true);
|
|
71
|
+
error.correlationId = correlationId;
|
|
72
|
+
throw error;
|
|
64
73
|
}
|
|
65
|
-
|
|
66
|
-
return this._success(correlationId);
|
|
67
74
|
}
|
|
68
75
|
|
|
69
|
-
|
|
70
|
-
if (
|
|
71
|
-
this._logger.error(clazz, method, `
|
|
72
|
-
|
|
76
|
+
_enforceNotNullEither(clazz, method, value1, value2, name1, name2, correlationId) {
|
|
77
|
+
if ((!value1 || value1 === undefined) && (!value2 || value2 == undefined)) {
|
|
78
|
+
this._logger.error(clazz, method, `Either ${name1} or ${name2} is null.`, null, correlationId);
|
|
79
|
+
const error = Error(`Either ${name1} or ${name2} is null.`, true);
|
|
80
|
+
error.correlationId = correlationId;
|
|
81
|
+
throw error;
|
|
73
82
|
}
|
|
83
|
+
}
|
|
74
84
|
|
|
75
|
-
|
|
85
|
+
_enforceNotNullMultiple(clazz, method, values, names, correlationId) {
|
|
86
|
+
const valid = true;
|
|
87
|
+
for (const value of values)
|
|
88
|
+
valid &= values;
|
|
89
|
+
if (!valid) {
|
|
90
|
+
names = names.join(', ');
|
|
91
|
+
this._logger.error(clazz, method, `None of the fields are not null: ${names}`, null, correlationId);
|
|
92
|
+
const error = Error(`None of the fields are not null: ${names}`, true);
|
|
93
|
+
error.correlationId = correlationId;
|
|
94
|
+
throw error;
|
|
95
|
+
}
|
|
76
96
|
}
|
|
77
97
|
|
|
78
|
-
|
|
79
|
-
if (!value) {
|
|
80
|
-
this._logger.error(clazz, method,
|
|
81
|
-
return Response.error(clazz, method,
|
|
98
|
+
_enforceNotNullResponse(clazz, method, value, name, correlationId) {
|
|
99
|
+
if (!value || value === undefined) {
|
|
100
|
+
this._logger.error(clazz, method, `${name} is null.`, null, correlationId);
|
|
101
|
+
return Response.error(clazz, method, `${name} is null.`, null, null, null, correlationId);
|
|
82
102
|
}
|
|
83
103
|
|
|
84
|
-
return this._successResponse(
|
|
104
|
+
return this._successResponse(value, correlationId);
|
|
85
105
|
}
|
|
86
106
|
|
|
87
|
-
_enforceResponse(clazz, method, response, name, correlationId) {
|
|
107
|
+
_enforceResponse(clazz, method, response, name, correlationId, message) {
|
|
88
108
|
if (!response || (response && !response.success)) {
|
|
89
|
-
|
|
90
|
-
|
|
109
|
+
if (!String.isNullOrEmpty(message))
|
|
110
|
+
message = `Unsuccessful response for ${name}.`;
|
|
111
|
+
|
|
112
|
+
this._logger.error(clazz, method, message, null, correlationId);
|
|
113
|
+
const error = Error(message, true);
|
|
91
114
|
error.correlationId = correlationId;
|
|
92
115
|
throw error;
|
|
93
116
|
}
|
package/service/utility.js
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
import fs from 'fs';
|
|
2
2
|
import path from 'path';
|
|
3
|
+
import { Mutex as asyncMutex } from 'async-mutex';
|
|
3
4
|
|
|
4
5
|
import LibraryConstants from '@thzero/library_server/constants.js';
|
|
5
6
|
|
|
7
|
+
import Utility from '@thzero/library_common/utility/index.js';
|
|
8
|
+
|
|
6
9
|
import Service from './index.js';
|
|
7
10
|
|
|
8
11
|
class UtilityService extends Service {
|
|
@@ -13,6 +16,11 @@ class UtilityService extends Service {
|
|
|
13
16
|
this._serviceVersion = null;
|
|
14
17
|
|
|
15
18
|
this._openSourceResponse = null;
|
|
19
|
+
|
|
20
|
+
this._initializeResponse = null;
|
|
21
|
+
this._mutexInitialize = new asyncMutex();
|
|
22
|
+
this._ttlInitialize = null;
|
|
23
|
+
this._ttlInitializeDiff = 1000 * 30;
|
|
16
24
|
}
|
|
17
25
|
|
|
18
26
|
async init(injector) {
|
|
@@ -25,24 +33,41 @@ class UtilityService extends Service {
|
|
|
25
33
|
}
|
|
26
34
|
|
|
27
35
|
async initialize(correlationId) {
|
|
28
|
-
const
|
|
29
|
-
|
|
36
|
+
const now = Utility.getTimestamp();
|
|
37
|
+
const ttlInitialize = this._ttlInitialize ? this._ttlInitialize : 0;
|
|
38
|
+
const delta = now - ttlInitialize;
|
|
39
|
+
if (this._initializeResponse && (delta <= this._ttlInitializeDiff))
|
|
40
|
+
return this._initializeResponse;
|
|
41
|
+
|
|
42
|
+
const release = await this._mutexInitialize.acquire();
|
|
43
|
+
try {
|
|
44
|
+
if (this._initializeResponse)
|
|
45
|
+
return this._initializeResponse;
|
|
46
|
+
|
|
47
|
+
const response = this._initResponse(correlationId);
|
|
48
|
+
response.results = {};
|
|
30
49
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
50
|
+
const responsePlans = await this._servicePlans.listing(correlationId);
|
|
51
|
+
if (this._hasFailed(responsePlans))
|
|
52
|
+
return responsePlans;
|
|
34
53
|
|
|
35
|
-
|
|
54
|
+
response.results.plans = responsePlans.results;
|
|
36
55
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
56
|
+
const responseVersion = await this._serviceVersion.version(correlationId);
|
|
57
|
+
if (this._hasFailed(responseVersion))
|
|
58
|
+
return responseVersion;
|
|
40
59
|
|
|
41
|
-
|
|
60
|
+
response.results.version = responseVersion.results;
|
|
42
61
|
|
|
43
|
-
|
|
62
|
+
await this._intialize(correlationId, response);
|
|
44
63
|
|
|
45
|
-
|
|
64
|
+
this._ttlInitialize = Utility.getTimestamp();
|
|
65
|
+
this._initializeResponse = response;
|
|
66
|
+
return response;
|
|
67
|
+
}
|
|
68
|
+
finally {
|
|
69
|
+
release();
|
|
70
|
+
}
|
|
46
71
|
}
|
|
47
72
|
|
|
48
73
|
async logger(content, correlationId) {
|
|
@@ -147,95 +172,6 @@ class UtilityService extends Service {
|
|
|
147
172
|
|
|
148
173
|
_openSource(correlationId, openSource) {
|
|
149
174
|
}
|
|
150
|
-
|
|
151
|
-
// async _openSourceServer(correlationId) {
|
|
152
|
-
// return [
|
|
153
|
-
// {
|
|
154
|
-
// category: 'server',
|
|
155
|
-
// name: '@thzero/library_common',
|
|
156
|
-
// url: 'https://github.com/thzero/library_common',
|
|
157
|
-
// licenseName: 'MIT',
|
|
158
|
-
// licenseUrl: 'https://github.com/thzero/library_common/blob/master/license.md'
|
|
159
|
-
// },
|
|
160
|
-
// {
|
|
161
|
-
// category: 'server',
|
|
162
|
-
// name: '@thzero/library_common_service',
|
|
163
|
-
// url: 'https://github.com/thzero/library_common_service',
|
|
164
|
-
// licenseName: 'MIT',
|
|
165
|
-
// licenseUrl: 'https://github.com/thzero/library_common_service/blob/master/license.md'
|
|
166
|
-
// },
|
|
167
|
-
// {
|
|
168
|
-
// category: 'server',
|
|
169
|
-
// name: '@thzero/library_server',
|
|
170
|
-
// url: 'https://github.com/thzero/library_server',
|
|
171
|
-
// licenseName: 'MIT',
|
|
172
|
-
// licenseUrl: 'https://github.com/thzero/library_server/blob/master/license.md'
|
|
173
|
-
// },
|
|
174
|
-
// {
|
|
175
|
-
// category: 'server',
|
|
176
|
-
// name: 'async-mutex',
|
|
177
|
-
// url: 'https://github.com/DirtyHairy/async-mutex',
|
|
178
|
-
// licenseName: 'MIT',
|
|
179
|
-
// licenseUrl: 'https://github.com/DirtyHairy/async-mutex/blob/master/LICENSE'
|
|
180
|
-
// },
|
|
181
|
-
// {
|
|
182
|
-
// category: 'server',
|
|
183
|
-
// name: 'config',
|
|
184
|
-
// url: 'https://github.com/lorenwest/node-config',
|
|
185
|
-
// licenseName: 'MIT',
|
|
186
|
-
// licenseUrl: 'https://github.com/lorenwest/node-config/blob/master/LICENSE'
|
|
187
|
-
// },
|
|
188
|
-
// {
|
|
189
|
-
// category: 'server',
|
|
190
|
-
// name: 'dayjs',
|
|
191
|
-
// url: 'https://github.com/iamkun/dayjs',
|
|
192
|
-
// licenseName: 'MIT',
|
|
193
|
-
// licenseUrl: 'https://github.com/iamkun/dayjs/blob/dev/LICENSE'
|
|
194
|
-
// },
|
|
195
|
-
// {
|
|
196
|
-
// category: 'server',
|
|
197
|
-
// name: 'dayjs-plugin-utc',
|
|
198
|
-
// url: 'https://github.com/guisturdy/dayjs-plugin-utc',
|
|
199
|
-
// licenseName: '??',
|
|
200
|
-
// licenseUrl: ''
|
|
201
|
-
// },
|
|
202
|
-
// {
|
|
203
|
-
// category: 'server',
|
|
204
|
-
// name: 'easy-rbac',
|
|
205
|
-
// url: 'https://github.com/DeadAlready/easy-rbac',
|
|
206
|
-
// licenseName: 'MIT',
|
|
207
|
-
// licenseUrl: 'https://github.com/DeadAlready/easy-rbac/blob/master/LICENSE'
|
|
208
|
-
// },
|
|
209
|
-
// {
|
|
210
|
-
// category: 'server',
|
|
211
|
-
// name: 'ipaddr.js',
|
|
212
|
-
// url: 'https://github.com/whitequark/ipaddr.js',
|
|
213
|
-
// licenseName: 'MIT',
|
|
214
|
-
// licenseUrl: 'https://github.com/whitequark/ipaddr.js/blob/master/LICENSE'
|
|
215
|
-
// },
|
|
216
|
-
// {
|
|
217
|
-
// category: 'client',
|
|
218
|
-
// name: 'lodash',
|
|
219
|
-
// url: 'https://github.com/lodash/lodash',
|
|
220
|
-
// licenseName: 'MIT',
|
|
221
|
-
// licenseUrl: 'https://github.com/lodash/lodash/blob/master/LICENSE'
|
|
222
|
-
// },
|
|
223
|
-
// {
|
|
224
|
-
// category: 'server',
|
|
225
|
-
// name: 'terminus',
|
|
226
|
-
// url: 'https://github.com/godaddy/terminus',
|
|
227
|
-
// licenseName: 'MIT',
|
|
228
|
-
// licenseUrl: 'https://github.com/godaddy/terminus/blob/master/LICENSE'
|
|
229
|
-
// },
|
|
230
|
-
// {
|
|
231
|
-
// category: 'server',
|
|
232
|
-
// name: 'uuid',
|
|
233
|
-
// url: 'https://github.com/kelektiv/node-uuid',
|
|
234
|
-
// licenseName: 'MIT',
|
|
235
|
-
// licenseUrl: 'https://github.com/kelektiv/node-uuid/blob/master/LICENSE.md'
|
|
236
|
-
// }
|
|
237
|
-
// ];
|
|
238
|
-
// }
|
|
239
175
|
}
|
|
240
176
|
|
|
241
177
|
export default UtilityService;
|