@thzero/library_server 0.16.7 → 0.16.9

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.
Files changed (2) hide show
  1. package/package.json +3 -3
  2. package/service/utility.js +39 -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.7",
4
+ "version": "0.16.9",
5
5
  "version_major": 0,
6
6
  "version_minor": 16,
7
- "version_patch": 7,
8
- "version_date": "12/20/2022",
7
+ "version_patch": 9,
8
+ "version_date": "12/21/2022",
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",
@@ -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 response = this._initResponse(correlationId);
29
- response.results = {};
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
- const responsePlans = await this._servicePlans.listing(correlationId);
32
- if (this._hasFailed(responsePlans))
33
- return responsePlans;
50
+ const responsePlans = await this._servicePlans.listing(correlationId);
51
+ if (this._hasFailed(responsePlans))
52
+ return responsePlans;
34
53
 
35
- response.results.plans = responsePlans.results;
54
+ response.results.plans = responsePlans.results;
36
55
 
37
- const responseVersion = await this._serviceVersion.version(correlationId);
38
- if (this._hasFailed(responseVersion))
39
- return responseVersion;
56
+ const responseVersion = await this._serviceVersion.version(correlationId);
57
+ if (this._hasFailed(responseVersion))
58
+ return responseVersion;
40
59
 
41
- response.results.version = responseVersion.results;
60
+ response.results.version = responseVersion.results;
42
61
 
43
- this._intialize(correlationId, response);
62
+ await this._intialize(correlationId, response);
44
63
 
45
- return response;
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) {
@@ -118,6 +143,8 @@ class UtilityService extends Service {
118
143
 
119
144
  items = fileI.default();
120
145
  items.forEach(element => {
146
+ if (element.category !== 'server')
147
+ return;
121
148
  if (this._openSourceResponse.results.filter(l => l.name === element.name).length > 0)
122
149
  return;
123
150
  this._openSourceResponse.results.push(element);
@@ -145,95 +172,6 @@ class UtilityService extends Service {
145
172
 
146
173
  _openSource(correlationId, openSource) {
147
174
  }
148
-
149
- // async _openSourceServer(correlationId) {
150
- // return [
151
- // {
152
- // category: 'server',
153
- // name: '@thzero/library_common',
154
- // url: 'https://github.com/thzero/library_common',
155
- // licenseName: 'MIT',
156
- // licenseUrl: 'https://github.com/thzero/library_common/blob/master/license.md'
157
- // },
158
- // {
159
- // category: 'server',
160
- // name: '@thzero/library_common_service',
161
- // url: 'https://github.com/thzero/library_common_service',
162
- // licenseName: 'MIT',
163
- // licenseUrl: 'https://github.com/thzero/library_common_service/blob/master/license.md'
164
- // },
165
- // {
166
- // category: 'server',
167
- // name: '@thzero/library_server',
168
- // url: 'https://github.com/thzero/library_server',
169
- // licenseName: 'MIT',
170
- // licenseUrl: 'https://github.com/thzero/library_server/blob/master/license.md'
171
- // },
172
- // {
173
- // category: 'server',
174
- // name: 'async-mutex',
175
- // url: 'https://github.com/DirtyHairy/async-mutex',
176
- // licenseName: 'MIT',
177
- // licenseUrl: 'https://github.com/DirtyHairy/async-mutex/blob/master/LICENSE'
178
- // },
179
- // {
180
- // category: 'server',
181
- // name: 'config',
182
- // url: 'https://github.com/lorenwest/node-config',
183
- // licenseName: 'MIT',
184
- // licenseUrl: 'https://github.com/lorenwest/node-config/blob/master/LICENSE'
185
- // },
186
- // {
187
- // category: 'server',
188
- // name: 'dayjs',
189
- // url: 'https://github.com/iamkun/dayjs',
190
- // licenseName: 'MIT',
191
- // licenseUrl: 'https://github.com/iamkun/dayjs/blob/dev/LICENSE'
192
- // },
193
- // {
194
- // category: 'server',
195
- // name: 'dayjs-plugin-utc',
196
- // url: 'https://github.com/guisturdy/dayjs-plugin-utc',
197
- // licenseName: '??',
198
- // licenseUrl: ''
199
- // },
200
- // {
201
- // category: 'server',
202
- // name: 'easy-rbac',
203
- // url: 'https://github.com/DeadAlready/easy-rbac',
204
- // licenseName: 'MIT',
205
- // licenseUrl: 'https://github.com/DeadAlready/easy-rbac/blob/master/LICENSE'
206
- // },
207
- // {
208
- // category: 'server',
209
- // name: 'ipaddr.js',
210
- // url: 'https://github.com/whitequark/ipaddr.js',
211
- // licenseName: 'MIT',
212
- // licenseUrl: 'https://github.com/whitequark/ipaddr.js/blob/master/LICENSE'
213
- // },
214
- // {
215
- // category: 'client',
216
- // name: 'lodash',
217
- // url: 'https://github.com/lodash/lodash',
218
- // licenseName: 'MIT',
219
- // licenseUrl: 'https://github.com/lodash/lodash/blob/master/LICENSE'
220
- // },
221
- // {
222
- // category: 'server',
223
- // name: 'terminus',
224
- // url: 'https://github.com/godaddy/terminus',
225
- // licenseName: 'MIT',
226
- // licenseUrl: 'https://github.com/godaddy/terminus/blob/master/LICENSE'
227
- // },
228
- // {
229
- // category: 'server',
230
- // name: 'uuid',
231
- // url: 'https://github.com/kelektiv/node-uuid',
232
- // licenseName: 'MIT',
233
- // licenseUrl: 'https://github.com/kelektiv/node-uuid/blob/master/LICENSE.md'
234
- // }
235
- // ];
236
- // }
237
175
  }
238
176
 
239
177
  export default UtilityService;