@thzero/library_server_service_grpc 0.19.7 → 0.19.8
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/README.md +141 -121
- package/SECURITY.md +29 -0
- package/client.js +181 -166
- package/eslint.config.mjs +61 -61
- package/license.md +8 -8
- package/package.json +41 -41
- package/server.js +160 -92
- package/test/client.test.js +286 -236
- package/test/server.test.js +141 -85
package/test/server.test.js
CHANGED
|
@@ -1,85 +1,141 @@
|
|
|
1
|
-
import assert from 'node:assert/strict';
|
|
2
|
-
import { beforeEach, describe, it } from 'node:test';
|
|
3
|
-
|
|
4
|
-
import '@
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
service.
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
service.
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
1
|
+
import assert from 'node:assert/strict';
|
|
2
|
+
import { beforeEach, describe, it } from 'node:test';
|
|
3
|
+
|
|
4
|
+
import * as grpc from '@grpc/grpc-js';
|
|
5
|
+
|
|
6
|
+
import '@thzero/library_common/utility/string.js';
|
|
7
|
+
import BaseServerGrpcService from '../server.js';
|
|
8
|
+
|
|
9
|
+
const inject = (target, name, value) => {
|
|
10
|
+
Object.defineProperty(target, name, { value, writable: true, configurable: true });
|
|
11
|
+
return target;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
const newLogger = () => ({ debug() {}, info() {}, info2() {}, warn() {}, error() {}, exception() {}, fatal() {}, trace() {} });
|
|
15
|
+
|
|
16
|
+
// grpc metadata exposes get(key) -> array of values
|
|
17
|
+
const newCall = (metadata = {}) => ({ metadata: { get: (key) => metadata[key] ?? [] } });
|
|
18
|
+
|
|
19
|
+
let service;
|
|
20
|
+
|
|
21
|
+
beforeEach(() => {
|
|
22
|
+
service = new BaseServerGrpcService();
|
|
23
|
+
inject(service, '_logger', newLogger());
|
|
24
|
+
inject(service, '_config', { get: () => null });
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
describe('_correlationId', () => {
|
|
28
|
+
it('reads the first correlationId from the call metadata', () => {
|
|
29
|
+
assert.equal(service._correlationId(newCall({ correlationId: [ 'cid-123' ] })), 'cid-123');
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it('returns null when the metadata carries none', () => {
|
|
33
|
+
assert.equal(service._correlationId(newCall()), null);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it('throws on a missing call', () => {
|
|
37
|
+
assert.throws(() => service._correlationId(null), /call is null/);
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
describe('_authenticate', () => {
|
|
42
|
+
const configure = (grpcConfig) => { service._configGrpc = grpcConfig; service._authKeys = null; };
|
|
43
|
+
|
|
44
|
+
it('returns the correlationId for a valid key', () => {
|
|
45
|
+
configure({ auth: { apiKey: 'secret' } });
|
|
46
|
+
assert.equal(service._authenticate(newCall({ correlationId: [ 'cid-123' ], authorization: [ 'secret' ] })), 'cid-123');
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it('accepts a bearer prefix', () => {
|
|
50
|
+
configure({ auth: { apiKey: 'secret' } });
|
|
51
|
+
assert.doesNotThrow(() => service._authenticate(newCall({ authorization: [ 'Bearer secret' ] })));
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it('accepts any of several keys', () => {
|
|
55
|
+
configure({ auth: { apiKey: [ 'old', 'new' ] } });
|
|
56
|
+
assert.doesNotThrow(() => service._authenticate(newCall({ authorization: [ 'old' ] })));
|
|
57
|
+
assert.doesNotThrow(() => service._authenticate(newCall({ authorization: [ 'new' ] })));
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it('rejects a wrong or missing key with UNAUTHENTICATED', () => {
|
|
61
|
+
configure({ auth: { apiKey: 'secret' } });
|
|
62
|
+
for (const metadata of [ { authorization: [ 'nonsense' ] }, { authorization: [ '' ] }, {} ]) {
|
|
63
|
+
assert.throws(() => service._authenticate(newCall(metadata)), (err) => err.code === grpc.status.UNAUTHENTICATED);
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it('rejects everything when no key is configured', () => {
|
|
68
|
+
configure({});
|
|
69
|
+
assert.throws(() => service._authenticate(newCall({ authorization: [ 'anything' ] })), /Unauthenticated/);
|
|
70
|
+
configure(null);
|
|
71
|
+
assert.throws(() => service._authenticate(newCall({ authorization: [ 'anything' ] })), /Unauthenticated/);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it('allows everything only when auth is explicitly not required', () => {
|
|
75
|
+
configure({ auth: { required: false } });
|
|
76
|
+
assert.doesNotThrow(() => service._authenticate(newCall()));
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
describe('_handleError', () => {
|
|
81
|
+
it('requires a callback', () => {
|
|
82
|
+
assert.throws(() => service._handleError('cid', new Error('boom'), null), /callback is null/);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it('does nothing when there is no error', () => {
|
|
86
|
+
let called = false;
|
|
87
|
+
service._handleError('cid', null, () => { called = true; });
|
|
88
|
+
assert.equal(called, false);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it('hides an unexpected error behind INTERNAL and logs it', () => {
|
|
92
|
+
const logged = [];
|
|
93
|
+
service._logger.exception = (clazz, method, err) => logged.push(err);
|
|
94
|
+
let payload = null;
|
|
95
|
+
const err = new Error('E11000 duplicate key error collection: app.users');
|
|
96
|
+
service._handleError('cid', err, (e) => { payload = e; });
|
|
97
|
+
assert.deepEqual(payload, { code: grpc.status.INTERNAL, message: 'Internal error.' });
|
|
98
|
+
assert.deepEqual(logged, [ err ]);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it('reports the name and message of an error carrying a grpc status code', () => {
|
|
102
|
+
let payload = null;
|
|
103
|
+
const err = new Error('Unauthenticated.');
|
|
104
|
+
err.code = grpc.status.UNAUTHENTICATED;
|
|
105
|
+
service._handleError('cid', err, (e) => { payload = e; });
|
|
106
|
+
assert.deepEqual(payload, { code: grpc.status.UNAUTHENTICATED, message: 'Error Unauthenticated.' });
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it('trims when only one of name and message is present', () => {
|
|
110
|
+
let payload = null;
|
|
111
|
+
service._handleError('cid', { message: 'boom', code: grpc.status.NOT_FOUND }, (err) => { payload = err; });
|
|
112
|
+
assert.deepEqual(payload, { code: grpc.status.NOT_FOUND, message: 'boom' });
|
|
113
|
+
|
|
114
|
+
service._handleError('cid', { name: 'Bad', code: grpc.status.INVALID_ARGUMENT }, (err) => { payload = err; });
|
|
115
|
+
assert.deepEqual(payload, { code: grpc.status.INVALID_ARGUMENT, message: 'Bad' });
|
|
116
|
+
});
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
describe('_start', () => {
|
|
120
|
+
it('requires a grpc config with a port', async () => {
|
|
121
|
+
await assert.rejects(() => service._start('cid'), /config.grpc is null/);
|
|
122
|
+
service._configGrpc = {};
|
|
123
|
+
await assert.rejects(() => service._start('cid'), /config.grpc.port is null/);
|
|
124
|
+
});
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
describe('_serverCredentials', () => {
|
|
128
|
+
it('refuses to run without TLS unless insecure is explicit', () => {
|
|
129
|
+
service._configGrpc = { port: 1 };
|
|
130
|
+
assert.throws(() => service._serverCredentials('cid'), /config.grpc.tls is required/);
|
|
131
|
+
service._configGrpc = { port: 1, insecure: true };
|
|
132
|
+
assert.equal(service._serverCredentials('cid')._isSecure(), false);
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
it('requires a key and cert when tls is configured', () => {
|
|
136
|
+
service._configGrpc = { port: 1, tls: { cert: 'x' } };
|
|
137
|
+
assert.throws(() => service._serverCredentials('cid'), /config.grpc.tls.key is empty/);
|
|
138
|
+
service._configGrpc = { port: 1, tls: { key: 'x' } };
|
|
139
|
+
assert.throws(() => service._serverCredentials('cid'), /config.grpc.tls.cert is empty/);
|
|
140
|
+
});
|
|
141
|
+
});
|