@thzero/library_server_service_grpc 0.19.7 → 0.19.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.
package/eslint.config.mjs CHANGED
@@ -1,61 +1,61 @@
1
- // Rules earned their place: each one catches a defect found in this codebase.
2
- // See AUDIT_SERVER.md in the parent workspace for the catalogue.
3
- export default [
4
- {
5
- files: ['**/*.js'],
6
- ignores: ['node_modules/**', 'dist/**'],
7
- languageOptions: {
8
- ecmaVersion: 2022,
9
- sourceType: 'module',
10
- globals: {
11
- // library_common installs these onto the global String
12
- String: 'writable',
13
- console: 'readonly',
14
- process: 'readonly',
15
- performance: 'readonly',
16
- navigator: 'readonly',
17
- setTimeout: 'readonly',
18
- clearTimeout: 'readonly'
19
- }
20
- },
21
- rules: {
22
- // undeclared identifiers - caught MomentUtility in utility/checksum.js
23
- // and cmd in library_cli_build/boot/cli.js
24
- 'no-undef': 'error',
25
- // === NaN is always false - caught the --major validation in cli.js
26
- 'use-isnan': 'error',
27
- // Unused locals. This is what caught the loop variable in
28
- // `for (const value of values) valid &= values`.
29
- // args is 'none' deliberately: the codebase is built on abstract base
30
- // classes whose methods declare a signature for subclasses and use none
31
- // of it - 58 such parameters in library_common_service alone. Dead
32
- // trailing parameters (idName, index) need a targeted read, not this rule.
33
- 'no-unused-vars': ['error', { args: 'none', ignoreRestSiblings: true }],
34
- // the &= boolean accumulator in the _enforce* block
35
- 'no-bitwise': 'error',
36
- // C17
37
- 'eqeqeq': 'error',
38
- 'no-dupe-class-members': 'error',
39
- 'no-dupe-keys': 'error',
40
- 'no-unreachable': 'error',
41
- 'no-constant-condition': ['error', { checkLoops: false }],
42
- 'no-self-compare': 'error',
43
- // the comma operator - caught `this._logger.exception(a, b, err), correlationId;`
44
- // in library_server_messaging_slack, which silently dropped the correlationId
45
- 'no-sequences': 'error',
46
- // caught `catch (err) { throw err; }` in library_server_repository_redis
47
- 'no-useless-catch': 'error',
48
- // caught `resource.authentication = resource.authentication;` in
49
- // library_server_service_rest_axios
50
- 'no-self-assign': 'error',
51
- // an expression statement that does nothing is almost always a dropped call
52
- 'no-unused-expressions': 'error'
53
- }
54
- },
55
- {
56
- files: ['test/**/*.js'],
57
- languageOptions: {
58
- globals: { process: 'readonly', console: 'readonly', setTimeout: 'readonly' }
59
- }
60
- }
61
- ];
1
+ // Rules earned their place: each one catches a defect found in this codebase.
2
+ // See AUDIT_SERVER.md in the parent workspace for the catalogue.
3
+ export default [
4
+ {
5
+ files: ['**/*.js'],
6
+ ignores: ['node_modules/**', 'dist/**'],
7
+ languageOptions: {
8
+ ecmaVersion: 2022,
9
+ sourceType: 'module',
10
+ globals: {
11
+ // library_common installs these onto the global String
12
+ String: 'writable',
13
+ console: 'readonly',
14
+ process: 'readonly',
15
+ performance: 'readonly',
16
+ navigator: 'readonly',
17
+ setTimeout: 'readonly',
18
+ clearTimeout: 'readonly'
19
+ }
20
+ },
21
+ rules: {
22
+ // undeclared identifiers - caught MomentUtility in utility/checksum.js
23
+ // and cmd in library_cli_build/boot/cli.js
24
+ 'no-undef': 'error',
25
+ // === NaN is always false - caught the --major validation in cli.js
26
+ 'use-isnan': 'error',
27
+ // Unused locals. This is what caught the loop variable in
28
+ // `for (const value of values) valid &= values`.
29
+ // args is 'none' deliberately: the codebase is built on abstract base
30
+ // classes whose methods declare a signature for subclasses and use none
31
+ // of it - 58 such parameters in library_common_service alone. Dead
32
+ // trailing parameters (idName, index) need a targeted read, not this rule.
33
+ 'no-unused-vars': ['error', { args: 'none', ignoreRestSiblings: true }],
34
+ // the &= boolean accumulator in the _enforce* block
35
+ 'no-bitwise': 'error',
36
+ // C17
37
+ 'eqeqeq': 'error',
38
+ 'no-dupe-class-members': 'error',
39
+ 'no-dupe-keys': 'error',
40
+ 'no-unreachable': 'error',
41
+ 'no-constant-condition': ['error', { checkLoops: false }],
42
+ 'no-self-compare': 'error',
43
+ // the comma operator - caught `this._logger.exception(a, b, err), correlationId;`
44
+ // in library_server_messaging_slack, which silently dropped the correlationId
45
+ 'no-sequences': 'error',
46
+ // caught `catch (err) { throw err; }` in library_server_repository_redis
47
+ 'no-useless-catch': 'error',
48
+ // caught `resource.authentication = resource.authentication;` in
49
+ // library_server_service_rest_axios
50
+ 'no-self-assign': 'error',
51
+ // an expression statement that does nothing is almost always a dropped call
52
+ 'no-unused-expressions': 'error'
53
+ }
54
+ },
55
+ {
56
+ files: ['test/**/*.js'],
57
+ languageOptions: {
58
+ globals: { process: 'readonly', console: 'readonly', setTimeout: 'readonly' }
59
+ }
60
+ }
61
+ ];
package/license.md CHANGED
@@ -1,9 +1,9 @@
1
- MIT License
2
-
3
- Copyright (c) 2020-2026 thZero.com
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
-
7
- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
-
1
+ MIT License
2
+
3
+ Copyright (c) 2020-2026 thZero.com
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
9
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/package.json CHANGED
@@ -1,42 +1,42 @@
1
- {
2
- "name": "@thzero/library_server_service_grpc",
3
- "type": "module",
4
- "version": "0.19.7",
5
- "version_major": 0,
6
- "version_minor": 19,
7
- "version_patch": 7,
8
- "version_date": "11/29/2026",
9
- "description": "",
10
- "author": "thZero",
11
- "author_url": "https://www.thzero.com",
12
- "copyright": "2020-2026",
13
- "license": "MIT",
14
- "repository": {
15
- "type": "git",
16
- "url": "git+https://github.com/thzero/library_server_service_grpc.git"
17
- },
18
- "bugs": {
19
- "url": "https://github.com/thzero/library_server_service_grpc/issues"
20
- },
21
- "engines": {
22
- "node": ">=22"
23
- },
24
- "main": "index.js",
25
- "scripts": {
26
- "cli-update": "library-cli --updateversion --pi",
27
- "test": "node --test \"test/*.test.js\"",
28
- "lint": "eslint .",
29
- "lint:fix": "eslint . --fix"
30
- },
31
- "dependencies": {
32
- "@grpc/grpc-js": "^1.14.7"
33
- },
34
- "peerDependencies": {
35
- "@thzero/library_common": "^0.19",
36
- "@thzero/library_common_service": "^0.19",
37
- "@thzero/library_server": "^0.19"
38
- },
39
- "devDependencies": {
40
- "eslint": "^10.16.0"
41
- }
1
+ {
2
+ "name": "@thzero/library_server_service_grpc",
3
+ "type": "module",
4
+ "version": "0.19.9",
5
+ "version_major": 0,
6
+ "version_minor": 19,
7
+ "version_patch": 9,
8
+ "version_date": "11/30/2026",
9
+ "description": "",
10
+ "author": "thZero",
11
+ "author_url": "https://www.thzero.com",
12
+ "copyright": "2020-2026",
13
+ "license": "MIT",
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "git+https://github.com/thzero/library_server_service_grpc.git"
17
+ },
18
+ "bugs": {
19
+ "url": "https://github.com/thzero/library_server_service_grpc/issues"
20
+ },
21
+ "engines": {
22
+ "node": ">=22"
23
+ },
24
+ "main": "index.js",
25
+ "scripts": {
26
+ "cli-update": "library-cli --updateversion --pi",
27
+ "test": "node --test \"test/*.test.js\"",
28
+ "lint": "eslint .",
29
+ "lint:fix": "eslint . --fix"
30
+ },
31
+ "dependencies": {
32
+ "@grpc/grpc-js": "^1.14.7"
33
+ },
34
+ "peerDependencies": {
35
+ "@thzero/library_common": "^0.19",
36
+ "@thzero/library_common_service": "^0.19",
37
+ "@thzero/library_server": "^0.19"
38
+ },
39
+ "devDependencies": {
40
+ "eslint": "^10.16.0"
41
+ }
42
42
  }
package/server.js CHANGED
@@ -1,92 +1,164 @@
1
- import * as grpc from '@grpc/grpc-js';
2
-
3
- import Service from '@thzero/library_server/service/index.js';
4
-
5
- class BaseServerGrpcService extends Service {
6
- constructor() {
7
- super();
8
-
9
- this._configGrpc = null;
10
-
11
- this._grpc = null;
12
- }
13
-
14
- async init(injector) {
15
- await super.init(injector);
16
-
17
- try {
18
- this._configGrpc = this._config.get(`grpc`);
19
-
20
- this._grpc = new grpc.Server();
21
- await this._initServices(this._grpc);
22
- await this._start();
23
- }
24
- catch (err) {
25
- this._logger.error('BaseServerGrpcService', 'init', null, err);
26
- }
27
- }
28
-
29
- _authenticate(call) {
30
- const correlationId = this._correlationId(call);
31
- this._logger.debug('BaseServerGrpcService', '_authenticate', 'correlationId', correlationId, correlationId);
32
-
33
- const meta = call.metadata.get('authorization');
34
- this._logger.debug('BaseServerGrpcService', '_authenticate', 'meta', meta, correlationId);
35
- const authorization = (meta && (meta.length > 0)) ? meta[0] : null;
36
- this._logger.debug('BaseServerGrpcService', '_authenticate', 'authorization', authorization, correlationId);
37
-
38
- const valid = true; // TODO
39
- if (!valid)
40
- throw Error('Unauthorized.');
41
-
42
- return correlationId;
43
- }
44
-
45
- _correlationId(call) {
46
- this._enforceNotNull('BaseServerGrpcService', '_correlationId', call, 'call');
47
-
48
- const meta = call.metadata.get('correlationId');
49
- this._logger.debug('BaseServerGrpcService', '_correlationId', 'meta', meta);
50
- return (meta && (meta.length > 0)) ? meta[0] : null;
51
- }
52
-
53
- _handleError(correlationId, err, callback) {
54
- this._enforceNotNull('BaseServerGrpcService', '_handleError', callback, 'callback', correlationId);
55
-
56
- if (!err)
57
- return;
58
-
59
- let message = '';
60
- if (err.name)
61
- message += err.name + ' ';
62
- if (err.message)
63
- message += err.message;
64
- message = message.trim();
65
-
66
- callback({ message: message }, null);
67
- }
68
-
69
- async _initServices(grpc) {
70
- }
71
-
72
- async _start(correlationId) {
73
- this._enforceNotNull('BaseServerGrpcService', '_start', this._configGrpc, 'config.grpc', correlationId);
74
- this._enforceNotNull('BaseServerGrpcService', '_start', this._configGrpc.port, 'config.grpc.port', correlationId);
75
-
76
- return await new Promise((resolve, reject) => {
77
- this._grpc.bindAsync('0.0.0.0:' + this._configGrpc.port, grpc.ServerCredentials.createInsecure(), (err, port) => {
78
- if (err) {
79
- reject(this._error('BaseServerGrpcService', '_start', null, err, null, null, correlationId));
80
- return;
81
- }
82
-
83
- this._logger.info2(`GRPC: starting server on port '${this._configGrpc.port}'...`, null, correlationId);
84
- this._grpc.start();
85
-
86
- resolve(this._success(correlationId));
87
- });
88
- });
89
- }
90
- }
91
-
92
- export default BaseServerGrpcService;
1
+ import { Buffer } from 'node:buffer';
2
+ import crypto from 'node:crypto';
3
+ import fs from 'node:fs';
4
+
5
+ import * as grpc from '@grpc/grpc-js';
6
+
7
+ import Service from '@thzero/library_server/service/index.js';
8
+
9
+ const bearer = 'bearer ';
10
+
11
+ const hash = (value) => crypto.createHash('sha256').update(value).digest();
12
+
13
+ const pem = (value) => Buffer.from(String(value).trimStart().startsWith('-----BEGIN') ? value : fs.readFileSync(value, 'utf8'));
14
+
15
+ class BaseServerGrpcService extends Service {
16
+ constructor() {
17
+ super();
18
+
19
+ this._configGrpc = null;
20
+ this._authKeys = null;
21
+
22
+ this._grpc = null;
23
+ }
24
+
25
+ async init(injector) {
26
+ await super.init(injector);
27
+
28
+ try {
29
+ this._configGrpc = this._config.get(`grpc`);
30
+
31
+ this._grpc = new grpc.Server();
32
+ await this._initServices(this._grpc);
33
+ await this._start();
34
+ }
35
+ catch (err) {
36
+ this._logger.error('BaseServerGrpcService', 'init', null, err);
37
+ }
38
+ }
39
+
40
+ _authenticate(call) {
41
+ const correlationId = this._correlationId(call);
42
+
43
+ const meta = call.metadata.get('authorization');
44
+ const authorization = (meta && (meta.length > 0)) ? meta[0] : null;
45
+
46
+ if (!this._authenticateValidate(correlationId, authorization, call)) {
47
+ this._logger.warn('BaseServerGrpcService', '_authenticate', 'Unauthenticated.', null, correlationId);
48
+ const err = new Error('Unauthenticated.');
49
+ err.code = grpc.status.UNAUTHENTICATED;
50
+ throw err;
51
+ }
52
+
53
+ return correlationId;
54
+ }
55
+
56
+ _authenticateValidate(correlationId, authorization, call) {
57
+ const auth = this._configGrpc ? this._configGrpc.auth : null;
58
+ if (auth && (auth.required === false))
59
+ return true;
60
+
61
+ const keys = this._authenticateKeys(auth);
62
+ if (keys.length === 0)
63
+ return false;
64
+
65
+ if (!String.isString(authorization))
66
+ return false;
67
+
68
+ let value = authorization.trim();
69
+ if (value.toLowerCase().startsWith(bearer))
70
+ value = value.substring(bearer.length).trim();
71
+ if (String.isNullOrEmpty(value))
72
+ return false;
73
+
74
+ const digest = hash(value);
75
+ let valid = false;
76
+ for (const key of keys)
77
+ valid = crypto.timingSafeEqual(digest, key) || valid;
78
+ return valid;
79
+ }
80
+
81
+ _authenticateKeys(auth) {
82
+ if (this._authKeys)
83
+ return this._authKeys;
84
+
85
+ const configured = auth ? auth.apiKey : null;
86
+ const list = Array.isArray(configured) ? configured : [ configured ];
87
+ this._authKeys = list.filter(key => String.isString(key) && !String.isNullOrEmpty(key.trim())).map(key => hash(key.trim()));
88
+ return this._authKeys;
89
+ }
90
+
91
+ _correlationId(call) {
92
+ this._enforceNotNull('BaseServerGrpcService', '_correlationId', call, 'call');
93
+
94
+ const meta = call.metadata.get('correlationId');
95
+ return (meta && (meta.length > 0)) ? meta[0] : null;
96
+ }
97
+
98
+ _handleError(correlationId, err, callback) {
99
+ this._enforceNotNull('BaseServerGrpcService', '_handleError', callback, 'callback', correlationId);
100
+
101
+ if (!err)
102
+ return;
103
+
104
+ if (!Number.isInteger(err.code)) {
105
+ this._logger.exception('BaseServerGrpcService', '_handleError', err, correlationId);
106
+ callback({ code: grpc.status.INTERNAL, message: 'Internal error.' }, null);
107
+ return;
108
+ }
109
+
110
+ let message = '';
111
+ if (err.name)
112
+ message += err.name + ' ';
113
+ if (err.message)
114
+ message += err.message;
115
+ message = message.trim();
116
+
117
+ callback({ code: err.code, message: message }, null);
118
+ }
119
+
120
+ async _initServices(grpc) {
121
+ }
122
+
123
+ async _start(correlationId) {
124
+ this._enforceNotNull('BaseServerGrpcService', '_start', this._configGrpc, 'config.grpc', correlationId);
125
+ this._enforceNotNull('BaseServerGrpcService', '_start', this._configGrpc.port, 'config.grpc.port', correlationId);
126
+
127
+ return await new Promise((resolve, reject) => {
128
+ const address = (this._configGrpc.host ? this._configGrpc.host : '0.0.0.0') + ':' + this._configGrpc.port;
129
+ this._grpc.bindAsync(address, this._serverCredentials(correlationId), (err, port) => {
130
+ if (err) {
131
+ reject(this._error('BaseServerGrpcService', '_start', null, err, null, null, correlationId));
132
+ return;
133
+ }
134
+
135
+ this._logger.info2(`GRPC: starting server on port '${this._configGrpc.port}'...`, null, correlationId);
136
+ this._grpc.start();
137
+
138
+ resolve(this._success(correlationId));
139
+ });
140
+ });
141
+ }
142
+
143
+ _serverCredentials(correlationId) {
144
+ const tls = this._configGrpc.tls;
145
+ const secure = this._configGrpc.secure !== false;
146
+ if (tls && !secure)
147
+ throw Error('config.grpc.tls is set but config.grpc.secure is false; remove one.');
148
+
149
+ if (tls) {
150
+ this._enforceNotEmpty('BaseServerGrpcService', '_serverCredentials', tls.key, 'config.grpc.tls.key', correlationId);
151
+ this._enforceNotEmpty('BaseServerGrpcService', '_serverCredentials', tls.cert, 'config.grpc.tls.cert', correlationId);
152
+
153
+ const ca = tls.ca ? pem(tls.ca) : null;
154
+ return grpc.ServerCredentials.createSsl(ca, [ { private_key: pem(tls.key), cert_chain: pem(tls.cert) } ], ca ? (tls.clientCertificateRequired === true) : false);
155
+ }
156
+
157
+ if (!secure)
158
+ return grpc.ServerCredentials.createInsecure();
159
+
160
+ throw Error('config.grpc.tls is required; set config.grpc.secure to false to run without TLS.');
161
+ }
162
+ }
163
+
164
+ export default BaseServerGrpcService;