@thzero/library_server 0.17.3 → 0.17.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.
Files changed (53) hide show
  1. package/README.md +93 -93
  2. package/boot/index.js +431 -431
  3. package/boot/plugins/admin/index.js +6 -6
  4. package/boot/plugins/admin/news.js +33 -33
  5. package/boot/plugins/admin/users.js +33 -33
  6. package/boot/plugins/api.js +57 -57
  7. package/boot/plugins/apiFront.js +31 -31
  8. package/boot/plugins/index.js +70 -70
  9. package/boot/plugins/news.js +44 -44
  10. package/boot/plugins/users.js +46 -46
  11. package/boot/plugins/usersExtended.js +32 -32
  12. package/constants.js +45 -45
  13. package/data/baseNews.js +42 -42
  14. package/data/baseSettingsUser.js +9 -9
  15. package/data/baseUser.js +28 -28
  16. package/data/index.js +24 -24
  17. package/data/named.js +20 -20
  18. package/errors/tokenExpired.js +7 -7
  19. package/license.md +8 -8
  20. package/openSource.js +66 -66
  21. package/package.json +33 -36
  22. package/repository/index.js +174 -174
  23. package/repository/usageMetrics/devnull.js +11 -11
  24. package/routes/index.js +76 -76
  25. package/service/admin/baseNews.js +45 -45
  26. package/service/admin/index.js +130 -130
  27. package/service/admin/news.js +6 -6
  28. package/service/admin/users.js +107 -107
  29. package/service/baseSecurity.js +44 -44
  30. package/service/baseUser.js +122 -122
  31. package/service/communication.js +6 -6
  32. package/service/config.js +32 -32
  33. package/service/crypto.js +16 -16
  34. package/service/discovery/index.js +6 -6
  35. package/service/discovery/resources/index.js +101 -101
  36. package/service/external.js +19 -19
  37. package/service/externalRest.js +19 -19
  38. package/service/index.js +20 -20
  39. package/service/monitoring.js +12 -12
  40. package/service/news/base.js +49 -49
  41. package/service/news/index.js +6 -6
  42. package/service/news/validation/index.js +6 -6
  43. package/service/plans.js +27 -27
  44. package/service/restCommunication.js +21 -21
  45. package/service/usageMetrics.js +57 -57
  46. package/service/utility.js +177 -177
  47. package/service/version.js +32 -32
  48. package/utility/injector.js +59 -59
  49. package/utility/internalIp/index.js +48 -48
  50. package/utility/list/doubleLinked.js +88 -88
  51. package/utility/list/priorityQueue.js +109 -109
  52. package/utility/list/queue.js +72 -72
  53. package/utility/os.js +20 -20
@@ -1,177 +1,177 @@
1
- import fs from 'fs';
2
- import path from 'path';
3
- import { Mutex as asyncMutex } from 'async-mutex';
4
-
5
- import LibraryServerConstants from '@thzero/library_server/constants.js';
6
-
7
- import LibraryCommonUtility from '@thzero/library_common/utility/index.js';
8
-
9
- import Service from './index.js';
10
-
11
- class UtilityService extends Service {
12
- constructor() {
13
- super();
14
-
15
- this._servicePlans = null;
16
- this._serviceVersion = null;
17
-
18
- this._openSourceResponse = null;
19
-
20
- this._initializeResponse = null;
21
- this._mutexInitialize = new asyncMutex();
22
- this._ttlInitialize = null;
23
- this._ttlInitializeDiff = 1000 * 30;
24
- }
25
-
26
- async init(injector) {
27
- await super.init(injector);
28
-
29
- this._servicePlans = this._injector.getService(LibraryServerConstants.InjectorKeys.SERVICE_PLANS);
30
- this._serviceVersion = this._injector.getService(LibraryServerConstants.InjectorKeys.SERVICE_VERSION);
31
-
32
- await this._initializeOopenSource();
33
- }
34
-
35
- async initialize(correlationId) {
36
- const now = LibraryCommonUtility.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 = {};
49
-
50
- const responsePlans = await this._servicePlans.listing(correlationId);
51
- if (this._hasFailed(responsePlans))
52
- return responsePlans;
53
-
54
- response.results.plans = responsePlans.results;
55
-
56
- const responseVersion = await this._serviceVersion.version(correlationId);
57
- if (this._hasFailed(responseVersion))
58
- return responseVersion;
59
-
60
- response.results.version = responseVersion.results;
61
-
62
- await this._intialize(correlationId, response);
63
-
64
- this._ttlInitialize = LibraryCommonUtility.getTimestamp();
65
- this._initializeResponse = response;
66
- return response;
67
- }
68
- finally {
69
- release();
70
- }
71
- }
72
-
73
- async logger(content, correlationId) {
74
- if (!content)
75
- return this._error('UtilityService', 'logger');
76
-
77
- const type = content.type;
78
- switch(type) {
79
- case 'DEBUG':
80
- this._logger.debug('UtilityService', 'logger', content.message, content.data, correlationId, true);
81
- break;
82
- case 'ERROR':
83
- this._logger.error('UtilityService', 'logger', content.message, content.data, correlationId, true);
84
- break;
85
- case 'EXCEPTION':
86
- this._logger.exception('UtilityService', 'logger', content.ex, correlationId, true);
87
- break;
88
- case 'FATAL':
89
- this._logger.fatal('UtilityService', 'logger', content.message, content.data, correlationId, true);
90
- break;
91
- case 'INFO':
92
- this._logger.info('UtilityService', 'logger', content.message, content.data, correlationId, true);
93
- break;
94
- case 'TRACE':
95
- this._logger.trace('UtilityService', 'logger', content.message, content.data, correlationId, true);
96
- break;
97
- case 'WARN':
98
- this._logger.warn('UtilityService', 'logger', content.message, content.data, correlationId, true);
99
- break;
100
- }
101
-
102
- return this._success(correlationId);
103
- }
104
-
105
- async openSource(correlationId) {
106
- return this._openSourceResponse ? this._openSourceResponse : this._error();
107
- }
108
-
109
- _intialize(correlationId, response) {
110
- }
111
-
112
- async _initializeOopenSource(correlationId) {
113
- this._openSourceResponse = this._initResponse(correlationId);
114
- this._openSourceResponse.results = [];
115
-
116
- try {
117
- const __dirname = path.resolve();
118
- const dir = path.join(path.resolve(__dirname), 'node_modules', '@thzero');
119
- const dirs = await fs.promises.readdir(dir);
120
-
121
- console.log(`\tOpenSource...`);
122
-
123
- let file;
124
- let importPath;
125
- let fileI;
126
- let items;
127
- for (const item of dirs) {
128
- try {
129
- file = path.join(dir, item, 'openSource.js');
130
- console.log(`\t${file}...`);
131
- if (!fs.existsSync(file)){
132
- console.log(`\t...not found.`);
133
- continue;
134
- }
135
-
136
- importPath = ['@thzero', item, 'openSource.js'].join('/');
137
- console.log(`\t${importPath}...`);
138
- fileI = await import(importPath);
139
- if (!fileI.default) {
140
- console.log(`\t...failed to load.`);
141
- continue;
142
- }
143
-
144
- items = fileI.default();
145
- items.forEach(element => {
146
- if (element.category !== 'server')
147
- return;
148
- if (this._openSourceResponse.results.filter(l => l.name === element.name).length > 0)
149
- return;
150
- this._openSourceResponse.results.push(element);
151
- });
152
- console.log(`\t...processed.`);
153
- }
154
- catch(err) {
155
- console.log(`\t...failed.`, err);
156
- this._logger.warn('UtilityService', '_initializeOopenSource', null, err, correlationId);
157
- }
158
- }
159
- }
160
- catch(err) {
161
- this._logger.warn('UtilityService', '_initializeOopenSource', null, err, correlationId);
162
- }
163
- finally {
164
- console.log(`\t...OpenSource`);
165
- }
166
-
167
- // response.results = await this._openSourceServer(correlationId);
168
- this._openSource(correlationId, this._openSourceResponse.results);
169
-
170
- this._openSourceResponse.results = this._openSourceResponse.results.sort((a, b) => a.name.localeCompare(b.name));
171
- }
172
-
173
- _openSource(correlationId, openSource) {
174
- }
175
- }
176
-
177
- export default UtilityService;
1
+ import fs from 'fs';
2
+ import path from 'path';
3
+ import { Mutex as asyncMutex } from 'async-mutex';
4
+
5
+ import LibraryServerConstants from '@thzero/library_server/constants.js';
6
+
7
+ import LibraryCommonUtility from '@thzero/library_common/utility/index.js';
8
+
9
+ import Service from './index.js';
10
+
11
+ class UtilityService extends Service {
12
+ constructor() {
13
+ super();
14
+
15
+ this._servicePlans = null;
16
+ this._serviceVersion = null;
17
+
18
+ this._openSourceResponse = null;
19
+
20
+ this._initializeResponse = null;
21
+ this._mutexInitialize = new asyncMutex();
22
+ this._ttlInitialize = null;
23
+ this._ttlInitializeDiff = 1000 * 30;
24
+ }
25
+
26
+ async init(injector) {
27
+ await super.init(injector);
28
+
29
+ this._servicePlans = this._injector.getService(LibraryServerConstants.InjectorKeys.SERVICE_PLANS);
30
+ this._serviceVersion = this._injector.getService(LibraryServerConstants.InjectorKeys.SERVICE_VERSION);
31
+
32
+ await this._initializeOopenSource();
33
+ }
34
+
35
+ async initialize(correlationId) {
36
+ const now = LibraryCommonUtility.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 = {};
49
+
50
+ const responsePlans = await this._servicePlans.listing(correlationId);
51
+ if (this._hasFailed(responsePlans))
52
+ return responsePlans;
53
+
54
+ response.results.plans = responsePlans.results;
55
+
56
+ const responseVersion = await this._serviceVersion.version(correlationId);
57
+ if (this._hasFailed(responseVersion))
58
+ return responseVersion;
59
+
60
+ response.results.version = responseVersion.results;
61
+
62
+ await this._intialize(correlationId, response);
63
+
64
+ this._ttlInitialize = LibraryCommonUtility.getTimestamp();
65
+ this._initializeResponse = response;
66
+ return response;
67
+ }
68
+ finally {
69
+ release();
70
+ }
71
+ }
72
+
73
+ async logger(content, correlationId) {
74
+ if (!content)
75
+ return this._error('UtilityService', 'logger');
76
+
77
+ const type = content.type;
78
+ switch(type) {
79
+ case 'DEBUG':
80
+ this._logger.debug('UtilityService', 'logger', content.message, content.data, correlationId, true);
81
+ break;
82
+ case 'ERROR':
83
+ this._logger.error('UtilityService', 'logger', content.message, content.data, correlationId, true);
84
+ break;
85
+ case 'EXCEPTION':
86
+ this._logger.exception('UtilityService', 'logger', content.ex, correlationId, true);
87
+ break;
88
+ case 'FATAL':
89
+ this._logger.fatal('UtilityService', 'logger', content.message, content.data, correlationId, true);
90
+ break;
91
+ case 'INFO':
92
+ this._logger.info('UtilityService', 'logger', content.message, content.data, correlationId, true);
93
+ break;
94
+ case 'TRACE':
95
+ this._logger.trace('UtilityService', 'logger', content.message, content.data, correlationId, true);
96
+ break;
97
+ case 'WARN':
98
+ this._logger.warn('UtilityService', 'logger', content.message, content.data, correlationId, true);
99
+ break;
100
+ }
101
+
102
+ return this._success(correlationId);
103
+ }
104
+
105
+ async openSource(correlationId) {
106
+ return this._openSourceResponse ? this._openSourceResponse : this._error();
107
+ }
108
+
109
+ _intialize(correlationId, response) {
110
+ }
111
+
112
+ async _initializeOopenSource(correlationId) {
113
+ this._openSourceResponse = this._initResponse(correlationId);
114
+ this._openSourceResponse.results = [];
115
+
116
+ try {
117
+ const __dirname = path.resolve();
118
+ const dir = path.join(path.resolve(__dirname), 'node_modules', '@thzero');
119
+ const dirs = await fs.promises.readdir(dir);
120
+
121
+ console.log(`\tOpenSource...`);
122
+
123
+ let file;
124
+ let importPath;
125
+ let fileI;
126
+ let items;
127
+ for (const item of dirs) {
128
+ try {
129
+ file = path.join(dir, item, 'openSource.js');
130
+ console.log(`\t${file}...`);
131
+ if (!fs.existsSync(file)){
132
+ console.log(`\t...not found.`);
133
+ continue;
134
+ }
135
+
136
+ importPath = ['@thzero', item, 'openSource.js'].join('/');
137
+ console.log(`\t${importPath}...`);
138
+ fileI = await import(importPath);
139
+ if (!fileI.default) {
140
+ console.log(`\t...failed to load.`);
141
+ continue;
142
+ }
143
+
144
+ items = fileI.default();
145
+ items.forEach(element => {
146
+ if (element.category !== 'server')
147
+ return;
148
+ if (this._openSourceResponse.results.filter(l => l.name === element.name).length > 0)
149
+ return;
150
+ this._openSourceResponse.results.push(element);
151
+ });
152
+ console.log(`\t...processed.`);
153
+ }
154
+ catch(err) {
155
+ console.log(`\t...failed.`, err);
156
+ this._logger.warn('UtilityService', '_initializeOopenSource', null, err, correlationId);
157
+ }
158
+ }
159
+ }
160
+ catch(err) {
161
+ this._logger.warn('UtilityService', '_initializeOopenSource', null, err, correlationId);
162
+ }
163
+ finally {
164
+ console.log(`\t...OpenSource`);
165
+ }
166
+
167
+ // response.results = await this._openSourceServer(correlationId);
168
+ this._openSource(correlationId, this._openSourceResponse.results);
169
+
170
+ this._openSourceResponse.results = this._openSourceResponse.results.sort((a, b) => a.name.localeCompare(b.name));
171
+ }
172
+
173
+ _openSource(correlationId, openSource) {
174
+ }
175
+ }
176
+
177
+ export default UtilityService;
@@ -1,32 +1,32 @@
1
- import fs from 'fs';
2
- import path from 'path';
3
-
4
- import Service from './index.js';
5
-
6
- class VersionService extends Service {
7
- async version(correlationId) {
8
- const filePath = path.join(process.cwd(), 'package.json');
9
- const file = fs.readFileSync(filePath, 'utf8');
10
- if (String.isNullOrEmpty(file))
11
- throw Error('Invalid package.json file for versioning; expected in the <app root> folder.');
12
-
13
- const packageObj = JSON.parse(file);
14
- if (!packageObj)
15
- throw Error('Invalid package.json file for versioning.');
16
-
17
- return this._generate(correlationId, packageObj.version_major, packageObj.version_minor, packageObj.version_patch, packageObj.version_date);
18
- }
19
-
20
- _generate(correlationId, version_major, version_minor, version_patch, version_date) {
21
- const response = this._success(correlationId);
22
- response.results = {
23
- major: version_major,
24
- minor: version_minor,
25
- patch: version_patch,
26
- date: version_date
27
- };
28
- return response;
29
- }
30
- }
31
-
32
- export default VersionService;
1
+ import fs from 'fs';
2
+ import path from 'path';
3
+
4
+ import Service from './index.js';
5
+
6
+ class VersionService extends Service {
7
+ async version(correlationId) {
8
+ const filePath = path.join(process.cwd(), 'package.json');
9
+ const file = fs.readFileSync(filePath, 'utf8');
10
+ if (String.isNullOrEmpty(file))
11
+ throw Error('Invalid package.json file for versioning; expected in the <app root> folder.');
12
+
13
+ const packageObj = JSON.parse(file);
14
+ if (!packageObj)
15
+ throw Error('Invalid package.json file for versioning.');
16
+
17
+ return this._generate(correlationId, packageObj.version_major, packageObj.version_minor, packageObj.version_patch, packageObj.version_date);
18
+ }
19
+
20
+ _generate(correlationId, version_major, version_minor, version_patch, version_date) {
21
+ const response = this._success(correlationId);
22
+ response.results = {
23
+ major: version_major,
24
+ minor: version_minor,
25
+ patch: version_patch,
26
+ date: version_date
27
+ };
28
+ return response;
29
+ }
30
+ }
31
+
32
+ export default VersionService;
@@ -1,59 +1,59 @@
1
- class Injector {
2
- constructor() {
3
- this._di = {};
4
- }
5
-
6
- addService(key, dependency) {
7
- if (String.isNullOrEmpty(key))
8
- throw Error(`Invalid injector key '${key}'.`);
9
- if (!dependency)
10
- throw Error('Invalid injector dependency.');
11
-
12
- if (this._di[key])
13
- return;
14
-
15
- this._di[key] = { dependency: dependency };
16
- }
17
-
18
- addSingleton(key, dependency) {
19
- if (String.isNullOrEmpty(key))
20
- throw Error(`Invalid injector key '${key}'.`);
21
- if (!dependency)
22
- throw Error('Invalid injector dependency.');
23
-
24
- if (this._di[key])
25
- return;
26
-
27
- this._di[key] = { key: key, singleton: true, dependency: dependency };
28
- }
29
-
30
- getInjector() {
31
- return this._di;
32
- }
33
-
34
- getService(key, args) {
35
- if (String.isNullOrEmpty(key))
36
- throw Error(`Invalid injector key '${key}'.`);
37
-
38
- const result = this._di[key];
39
- if (!result)
40
- return null;
41
-
42
- if (result.singleton)
43
- return result.dependency;
44
-
45
- return new result.dependency.prototype.constructor(args);
46
- }
47
-
48
- getServices() {
49
- return Object.values(this._di);
50
- }
51
-
52
- getSingletons() {
53
- return Object.values(this._di).filter(l => l.singleton).map(l => l.dependency);
54
- }
55
- }
56
-
57
- const singletonInstance = new Injector();
58
- Object.freeze(singletonInstance);
59
- export default singletonInstance;
1
+ class Injector {
2
+ constructor() {
3
+ this._di = {};
4
+ }
5
+
6
+ addService(key, dependency) {
7
+ if (String.isNullOrEmpty(key))
8
+ throw Error(`Invalid injector key '${key}'.`);
9
+ if (!dependency)
10
+ throw Error('Invalid injector dependency.');
11
+
12
+ if (this._di[key])
13
+ return;
14
+
15
+ this._di[key] = { dependency: dependency };
16
+ }
17
+
18
+ addSingleton(key, dependency) {
19
+ if (String.isNullOrEmpty(key))
20
+ throw Error(`Invalid injector key '${key}'.`);
21
+ if (!dependency)
22
+ throw Error('Invalid injector dependency.');
23
+
24
+ if (this._di[key])
25
+ return;
26
+
27
+ this._di[key] = { key: key, singleton: true, dependency: dependency };
28
+ }
29
+
30
+ getInjector() {
31
+ return this._di;
32
+ }
33
+
34
+ getService(key, args) {
35
+ if (String.isNullOrEmpty(key))
36
+ throw Error(`Invalid injector key '${key}'.`);
37
+
38
+ const result = this._di[key];
39
+ if (!result)
40
+ return null;
41
+
42
+ if (result.singleton)
43
+ return result.dependency;
44
+
45
+ return new result.dependency.prototype.constructor(args);
46
+ }
47
+
48
+ getServices() {
49
+ return Object.values(this._di);
50
+ }
51
+
52
+ getSingletons() {
53
+ return Object.values(this._di).filter(l => l.singleton).map(l => l.dependency);
54
+ }
55
+ }
56
+
57
+ const singletonInstance = new Injector();
58
+ Object.freeze(singletonInstance);
59
+ export default singletonInstance;
@@ -1,49 +1,49 @@
1
- import {networkInterfaces} from 'os';
2
- import defaultGateway from 'default-gateway';
3
- import ip from 'ipaddr.js';
4
-
5
- function findIp(gateway) {
6
- const gatewayIp = ip.parse(gateway);
7
-
8
- // Look for the matching interface in all local interfaces.
9
- for (const addresses of Object.values(networkInterfaces())) {
10
- for (const {cidr} of addresses) {
11
- const net = ip.parseCIDR(cidr);
12
-
13
- // eslint-disable-next-line unicorn/prefer-regexp-test
14
- if (net[0] && net[0].kind() === gatewayIp.kind() && gatewayIp.match(net)) {
15
- return net[0].toString();
16
- }
17
- }
18
- }
19
- }
20
-
21
- async function async(family) {
22
- try {
23
- const {gateway} = await defaultGateway[family]();
24
- return findIp(gateway);
25
- } catch {}
26
- }
27
-
28
- function sync(family) {
29
- try {
30
- const {gateway} = defaultGateway[family].sync();
31
- return findIp(gateway);
32
- } catch {}
33
- }
34
-
35
- export async function internalIpV6() {
36
- return async('v6');
37
- }
38
-
39
- export async function internalIpV4() {
40
- return async('v4');
41
- }
42
-
43
- export function internalIpV6Sync() {
44
- return sync('v6');
45
- }
46
-
47
- export function internalIpV4Sync() {
48
- return sync('v4');
1
+ import {networkInterfaces} from 'os';
2
+ import defaultGateway from 'default-gateway';
3
+ import ip from 'ipaddr.js';
4
+
5
+ function findIp(gateway) {
6
+ const gatewayIp = ip.parse(gateway);
7
+
8
+ // Look for the matching interface in all local interfaces.
9
+ for (const addresses of Object.values(networkInterfaces())) {
10
+ for (const {cidr} of addresses) {
11
+ const net = ip.parseCIDR(cidr);
12
+
13
+ // eslint-disable-next-line unicorn/prefer-regexp-test
14
+ if (net[0] && net[0].kind() === gatewayIp.kind() && gatewayIp.match(net)) {
15
+ return net[0].toString();
16
+ }
17
+ }
18
+ }
19
+ }
20
+
21
+ async function async(family) {
22
+ try {
23
+ const {gateway} = await defaultGateway[family]();
24
+ return findIp(gateway);
25
+ } catch {}
26
+ }
27
+
28
+ function sync(family) {
29
+ try {
30
+ const {gateway} = defaultGateway[family].sync();
31
+ return findIp(gateway);
32
+ } catch {}
33
+ }
34
+
35
+ export async function internalIpV6() {
36
+ return async('v6');
37
+ }
38
+
39
+ export async function internalIpV4() {
40
+ return async('v4');
41
+ }
42
+
43
+ export function internalIpV6Sync() {
44
+ return sync('v6');
45
+ }
46
+
47
+ export function internalIpV4Sync() {
48
+ return sync('v4');
49
49
  }