@rsdk/grpc.clients 2.6.0-next.0 → 2.6.0-next.1
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/CHANGELOG.md +6 -0
- package/dist/middleware/exception-mapper.middleware.d.ts +5 -0
- package/dist/middleware/exception-mapper.middleware.js +64 -0
- package/dist/middleware/exception-mapper.middleware.js.map +1 -0
- package/dist/repository/grpc-client.repository.d.ts +5 -7
- package/dist/repository/grpc-client.repository.js +8 -12
- package/dist/repository/grpc-client.repository.js.map +1 -1
- package/package.json +4 -4
- package/src/middleware/exception-mapper.middleware.ts +73 -0
- package/src/repository/grpc-client.repository.ts +17 -26
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,12 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
# [2.6.0-next.1](https://github.com/R-Vision/rsdk/compare/v2.6.0-next.0...v2.6.0-next.1) (2023-07-04)
|
|
7
|
+
|
|
8
|
+
### Bug Fixes
|
|
9
|
+
|
|
10
|
+
* gRPC error mapping into rsdk exceptions ([8c6a2b8](https://github.com/R-Vision/rsdk/commit/8c6a2b8276a7a309d26bc21e4c9ae36d7ee4210c))
|
|
11
|
+
|
|
6
12
|
# [2.6.0-next.0](https://github.com/R-Vision/rsdk/compare/v2.5.0...v2.6.0-next.0) (2023-07-03)
|
|
7
13
|
|
|
8
14
|
### Bug Fixes
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.exceptionMapperMiddleware = void 0;
|
|
4
|
+
const core_1 = require("@rsdk/core");
|
|
5
|
+
const nice_grpc_1 = require("nice-grpc");
|
|
6
|
+
/**
|
|
7
|
+
* Client middleware that performs gRPC code mapping into RSDK exception
|
|
8
|
+
*/
|
|
9
|
+
const exceptionMapperMiddleware = () =>
|
|
10
|
+
// eslint-disable-next-line unicorn/consistent-function-scoping
|
|
11
|
+
async function* (call, options) {
|
|
12
|
+
try {
|
|
13
|
+
return yield* call.next(call.request, options);
|
|
14
|
+
}
|
|
15
|
+
catch (error) {
|
|
16
|
+
if (!(error instanceof nice_grpc_1.ClientError)) {
|
|
17
|
+
throw new core_1.InternalException('GRPC call failed with unknown error', {
|
|
18
|
+
cause: error,
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
const message = 'GRPC Call failed!';
|
|
22
|
+
const props = {
|
|
23
|
+
details: {
|
|
24
|
+
/*
|
|
25
|
+
* Details - will be original message from server,
|
|
26
|
+
* while message field will be a composite string with
|
|
27
|
+
* status path and original message in one line
|
|
28
|
+
*/
|
|
29
|
+
message: error.details,
|
|
30
|
+
/**
|
|
31
|
+
* GRPC-package and method
|
|
32
|
+
*/
|
|
33
|
+
path: error.path,
|
|
34
|
+
/**
|
|
35
|
+
* TBD: Other context from original error is not yet passed
|
|
36
|
+
* via GRPC. Will be added later.
|
|
37
|
+
*/
|
|
38
|
+
},
|
|
39
|
+
};
|
|
40
|
+
/**
|
|
41
|
+
* According to /docs/images/exceptions.mapping.png
|
|
42
|
+
*/
|
|
43
|
+
switch (error.code) {
|
|
44
|
+
case nice_grpc_1.Status.DEADLINE_EXCEEDED:
|
|
45
|
+
throw new core_1.TimeoutException(message, props);
|
|
46
|
+
case nice_grpc_1.Status.INVALID_ARGUMENT:
|
|
47
|
+
throw new core_1.InputException(message, props);
|
|
48
|
+
case nice_grpc_1.Status.ALREADY_EXISTS:
|
|
49
|
+
throw new core_1.DuplicateEntityException(message, props);
|
|
50
|
+
case nice_grpc_1.Status.FAILED_PRECONDITION:
|
|
51
|
+
throw new core_1.ConflictException(message, props);
|
|
52
|
+
case nice_grpc_1.Status.UNAUTHENTICATED:
|
|
53
|
+
throw new core_1.AuthException(message, props);
|
|
54
|
+
case nice_grpc_1.Status.PERMISSION_DENIED:
|
|
55
|
+
throw new core_1.NotAllowedException(message, props);
|
|
56
|
+
case nice_grpc_1.Status.NOT_FOUND:
|
|
57
|
+
throw new core_1.NotFoundException(message, props);
|
|
58
|
+
default:
|
|
59
|
+
throw new core_1.InternalException(message, props);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
exports.exceptionMapperMiddleware = exceptionMapperMiddleware;
|
|
64
|
+
//# sourceMappingURL=exception-mapper.middleware.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"exception-mapper.middleware.js","sourceRoot":"","sources":["../../src/middleware/exception-mapper.middleware.ts"],"names":[],"mappings":";;;AAAA,qCASoB;AACpB,yCAAgD;AAGhD;;GAEG;AACI,MAAM,yBAAyB,GAAG,GAAqB,EAAE;AAC9D,+DAA+D;AAC/D,KAAK,SAAS,CAAC,EAAE,IAAI,EAAE,OAAO;IAC5B,IAAI;QACF,OAAO,KAAK,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;KAChD;IAAC,OAAO,KAAc,EAAE;QACvB,IAAI,CAAC,CAAC,KAAK,YAAY,uBAAW,CAAC,EAAE;YACnC,MAAM,IAAI,wBAAiB,CAAC,qCAAqC,EAAE;gBACjE,KAAK,EAAE,KAAK;aACb,CAAC,CAAC;SACJ;QAED,MAAM,OAAO,GAAG,mBAAmB,CAAC;QACpC,MAAM,KAAK,GAAG;YACZ,OAAO,EAAE;gBACP;;;;mBAIG;gBACH,OAAO,EAAE,KAAK,CAAC,OAAO;gBAEtB;;mBAEG;gBACH,IAAI,EAAE,KAAK,CAAC,IAAI;gBAEhB;;;mBAGG;aACJ;SACF,CAAC;QAEF;;WAEG;QACH,QAAQ,KAAK,CAAC,IAAI,EAAE;YAClB,KAAK,kBAAM,CAAC,iBAAiB;gBAC3B,MAAM,IAAI,uBAAgB,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;YAC7C,KAAK,kBAAM,CAAC,gBAAgB;gBAC1B,MAAM,IAAI,qBAAc,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;YAC3C,KAAK,kBAAM,CAAC,cAAc;gBACxB,MAAM,IAAI,+BAAwB,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;YACrD,KAAK,kBAAM,CAAC,mBAAmB;gBAC7B,MAAM,IAAI,wBAAiB,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;YAC9C,KAAK,kBAAM,CAAC,eAAe;gBACzB,MAAM,IAAI,oBAAa,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;YAC1C,KAAK,kBAAM,CAAC,iBAAiB;gBAC3B,MAAM,IAAI,0BAAmB,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;YAChD,KAAK,kBAAM,CAAC,SAAS;gBACnB,MAAM,IAAI,wBAAiB,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;YAC9C;gBACE,MAAM,IAAI,wBAAiB,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;SAC/C;KACF;AACH,CAAC,CAAC;AAxDS,QAAA,yBAAyB,6BAwDlC"}
|
|
@@ -6,13 +6,11 @@ import type { CompatServiceDefinition } from 'nice-grpc/lib/service-definitions'
|
|
|
6
6
|
*/
|
|
7
7
|
export declare class GrpcClientRepository {
|
|
8
8
|
private static factory;
|
|
9
|
-
logger: import("@rsdk/logging").ILogger;
|
|
10
|
-
servers: Set<string>;
|
|
11
|
-
definitions: Set<CompatServiceDefinition>;
|
|
12
|
-
channels: Map<string, ChannelImplementation>;
|
|
13
|
-
clients: Map<string, Client<CompatServiceDefinition>>;
|
|
14
|
-
private static createChannel;
|
|
15
|
-
private static createClient;
|
|
9
|
+
readonly logger: import("@rsdk/logging").ILogger;
|
|
10
|
+
readonly servers: Set<string>;
|
|
11
|
+
readonly definitions: Set<CompatServiceDefinition>;
|
|
12
|
+
readonly channels: Map<string, ChannelImplementation>;
|
|
13
|
+
readonly clients: Map<string, Client<CompatServiceDefinition>>;
|
|
16
14
|
resolveChannel(address: string, channelToken: string): ChannelImplementation;
|
|
17
15
|
resolveClient(definition: CompatServiceDefinition, channel: ChannelImplementation, clientToken: string): Client<CompatServiceDefinition>;
|
|
18
16
|
}
|
|
@@ -4,11 +4,14 @@ exports.GrpcClientRepository = void 0;
|
|
|
4
4
|
const logging_1 = require("@rsdk/logging");
|
|
5
5
|
const nice_grpc_1 = require("nice-grpc");
|
|
6
6
|
const nice_grpc_opentelemetry_1 = require("nice-grpc-opentelemetry");
|
|
7
|
+
const exception_mapper_middleware_1 = require("../middleware/exception-mapper.middleware");
|
|
7
8
|
/**
|
|
8
9
|
* Простое кеш хранилище внутри DI для кеширования на замену статике
|
|
9
10
|
*/
|
|
10
11
|
class GrpcClientRepository {
|
|
11
|
-
static factory = (0, nice_grpc_1.createClientFactory)()
|
|
12
|
+
static factory = (0, nice_grpc_1.createClientFactory)()
|
|
13
|
+
.use((0, nice_grpc_opentelemetry_1.openTelemetryClientMiddleware)())
|
|
14
|
+
.use((0, exception_mapper_middleware_1.exceptionMapperMiddleware)());
|
|
12
15
|
logger = logging_1.LoggerFactory.create(GrpcClientRepository);
|
|
13
16
|
// Registration
|
|
14
17
|
servers = new Set();
|
|
@@ -16,21 +19,14 @@ class GrpcClientRepository {
|
|
|
16
19
|
// Caches
|
|
17
20
|
channels = new Map();
|
|
18
21
|
clients = new Map();
|
|
19
|
-
static createChannel(address) {
|
|
20
|
-
return (0, nice_grpc_1.createChannel)(address);
|
|
21
|
-
}
|
|
22
|
-
static createClient(definition, channel) {
|
|
23
|
-
return GrpcClientRepository.factory.create(definition, channel);
|
|
24
|
-
}
|
|
25
22
|
resolveChannel(address, channelToken) {
|
|
26
|
-
const
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
return existed;
|
|
23
|
+
const channel = this.channels.get(channelToken) ?? (0, nice_grpc_1.createChannel)(address);
|
|
24
|
+
this.channels.set(channelToken, channel);
|
|
25
|
+
return channel;
|
|
30
26
|
}
|
|
31
27
|
resolveClient(definition, channel, clientToken) {
|
|
32
28
|
const existed = this.clients.get(clientToken) ??
|
|
33
|
-
GrpcClientRepository.
|
|
29
|
+
GrpcClientRepository.factory.create(definition, channel);
|
|
34
30
|
this.clients.set(clientToken, existed);
|
|
35
31
|
return existed;
|
|
36
32
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"grpc-client.repository.js","sourceRoot":"","sources":["../../src/repository/grpc-client.repository.ts"],"names":[],"mappings":";;;AACA,2CAA8C;AAE9C,yCAA+D;AAE/D,qEAAwE;AAExE;;GAEG;AACH,MAAa,oBAAoB;IACvB,MAAM,CAAC,OAAO,
|
|
1
|
+
{"version":3,"file":"grpc-client.repository.js","sourceRoot":"","sources":["../../src/repository/grpc-client.repository.ts"],"names":[],"mappings":";;;AACA,2CAA8C;AAE9C,yCAA+D;AAE/D,qEAAwE;AAExE,2FAAsF;AAEtF;;GAEG;AACH,MAAa,oBAAoB;IACvB,MAAM,CAAC,OAAO,GAAG,IAAA,+BAAmB,GAAE;SAC3C,GAAG,CAAC,IAAA,uDAA6B,GAAE,CAAC;SACpC,GAAG,CAAC,IAAA,uDAAyB,GAAE,CAAC,CAAC;IAE3B,MAAM,GAAG,uBAAa,CAAC,MAAM,CAAC,oBAAoB,CAAC,CAAC;IAE7D,eAAe;IACN,OAAO,GAAG,IAAI,GAAG,EAAU,CAAC;IAC5B,WAAW,GAAG,IAAI,GAAG,EAA2B,CAAC;IAE1D,SAAS;IACA,QAAQ,GAAG,IAAI,GAAG,EAAmB,CAAC;IACtC,OAAO,GAAG,IAAI,GAAG,EAA2C,CAAC;IAEtE,cAAc,CAAC,OAAe,EAAE,YAAoB;QAClD,MAAM,OAAO,GAAG,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,YAAY,CAAC,IAAI,IAAA,yBAAa,EAAC,OAAO,CAAC,CAAC;QAE1E,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,YAAY,EAAE,OAAO,CAAC,CAAC;QAEzC,OAAO,OAAO,CAAC;IACjB,CAAC;IAED,aAAa,CACX,UAAmC,EACnC,OAA8B,EAC9B,WAAmB;QAEnB,MAAM,OAAO,GACX,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,WAAW,CAAC;YAC7B,oBAAoB,CAAC,OAAO,CAAC,MAAM,CAAC,UAAU,EAAE,OAAO,CAAC,CAAC;QAE3D,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,WAAW,EAAE,OAAO,CAAC,CAAC;QAEvC,OAAO,OAAO,CAAC;IACjB,CAAC;;AAnCH,oDAoCC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rsdk/grpc.clients",
|
|
3
|
-
"version": "2.6.0-next.
|
|
3
|
+
"version": "2.6.0-next.1",
|
|
4
4
|
"description": "Grpc clients infrastructure",
|
|
5
5
|
"license": "Apache License 2.0",
|
|
6
6
|
"publishConfig": {
|
|
@@ -19,13 +19,13 @@
|
|
|
19
19
|
"@nestjs/microservices": "^9.0.0",
|
|
20
20
|
"@rsdk/autodoc.protocol": "^2.4.2",
|
|
21
21
|
"@rsdk/common": "^2.4.2",
|
|
22
|
-
"@rsdk/core": "^2.
|
|
23
|
-
"@rsdk/grpc.common": "^2.
|
|
22
|
+
"@rsdk/core": "^2.6.0-next.1",
|
|
23
|
+
"@rsdk/grpc.common": "^2.6.0-next.1",
|
|
24
24
|
"@rsdk/logging": "^2.4.2",
|
|
25
25
|
"@rsdk/metadata": "^2.4.2",
|
|
26
26
|
"nice-grpc": "^2.1.4",
|
|
27
27
|
"nice-grpc-opentelemetry": "^0.1.2",
|
|
28
28
|
"rxjs": "^7.0.0"
|
|
29
29
|
},
|
|
30
|
-
"gitHead": "
|
|
30
|
+
"gitHead": "aa325d2181ddb9438fd4596bc2c793121ad3ef6f"
|
|
31
31
|
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AuthException,
|
|
3
|
+
ConflictException,
|
|
4
|
+
DuplicateEntityException,
|
|
5
|
+
InputException,
|
|
6
|
+
InternalException,
|
|
7
|
+
NotAllowedException,
|
|
8
|
+
NotFoundException,
|
|
9
|
+
TimeoutException,
|
|
10
|
+
} from '@rsdk/core';
|
|
11
|
+
import { ClientError, Status } from 'nice-grpc';
|
|
12
|
+
import type { ClientMiddleware } from 'nice-grpc-common';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Client middleware that performs gRPC code mapping into RSDK exception
|
|
16
|
+
*/
|
|
17
|
+
export const exceptionMapperMiddleware = (): ClientMiddleware =>
|
|
18
|
+
// eslint-disable-next-line unicorn/consistent-function-scoping
|
|
19
|
+
async function* (call, options) {
|
|
20
|
+
try {
|
|
21
|
+
return yield* call.next(call.request, options);
|
|
22
|
+
} catch (error: unknown) {
|
|
23
|
+
if (!(error instanceof ClientError)) {
|
|
24
|
+
throw new InternalException('GRPC call failed with unknown error', {
|
|
25
|
+
cause: error,
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const message = 'GRPC Call failed!';
|
|
30
|
+
const props = {
|
|
31
|
+
details: {
|
|
32
|
+
/*
|
|
33
|
+
* Details - will be original message from server,
|
|
34
|
+
* while message field will be a composite string with
|
|
35
|
+
* status path and original message in one line
|
|
36
|
+
*/
|
|
37
|
+
message: error.details,
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* GRPC-package and method
|
|
41
|
+
*/
|
|
42
|
+
path: error.path,
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* TBD: Other context from original error is not yet passed
|
|
46
|
+
* via GRPC. Will be added later.
|
|
47
|
+
*/
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* According to /docs/images/exceptions.mapping.png
|
|
53
|
+
*/
|
|
54
|
+
switch (error.code) {
|
|
55
|
+
case Status.DEADLINE_EXCEEDED:
|
|
56
|
+
throw new TimeoutException(message, props);
|
|
57
|
+
case Status.INVALID_ARGUMENT:
|
|
58
|
+
throw new InputException(message, props);
|
|
59
|
+
case Status.ALREADY_EXISTS:
|
|
60
|
+
throw new DuplicateEntityException(message, props);
|
|
61
|
+
case Status.FAILED_PRECONDITION:
|
|
62
|
+
throw new ConflictException(message, props);
|
|
63
|
+
case Status.UNAUTHENTICATED:
|
|
64
|
+
throw new AuthException(message, props);
|
|
65
|
+
case Status.PERMISSION_DENIED:
|
|
66
|
+
throw new NotAllowedException(message, props);
|
|
67
|
+
case Status.NOT_FOUND:
|
|
68
|
+
throw new NotFoundException(message, props);
|
|
69
|
+
default:
|
|
70
|
+
throw new InternalException(message, props);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
};
|
|
@@ -1,45 +1,36 @@
|
|
|
1
1
|
import type { ChannelImplementation } from '@grpc/grpc-js/build/src/channel';
|
|
2
2
|
import { LoggerFactory } from '@rsdk/logging';
|
|
3
|
-
import type { Channel, Client
|
|
3
|
+
import type { Channel, Client } from 'nice-grpc';
|
|
4
4
|
import { createChannel, createClientFactory } from 'nice-grpc';
|
|
5
5
|
import type { CompatServiceDefinition } from 'nice-grpc/lib/service-definitions';
|
|
6
6
|
import { openTelemetryClientMiddleware } from 'nice-grpc-opentelemetry';
|
|
7
7
|
|
|
8
|
+
import { exceptionMapperMiddleware } from '../middleware/exception-mapper.middleware';
|
|
9
|
+
|
|
8
10
|
/**
|
|
9
11
|
* Простое кеш хранилище внутри DI для кеширования на замену статике
|
|
10
12
|
*/
|
|
11
13
|
export class GrpcClientRepository {
|
|
12
|
-
private static factory
|
|
13
|
-
openTelemetryClientMiddleware()
|
|
14
|
-
|
|
14
|
+
private static factory = createClientFactory()
|
|
15
|
+
.use(openTelemetryClientMiddleware())
|
|
16
|
+
.use(exceptionMapperMiddleware());
|
|
15
17
|
|
|
16
|
-
logger = LoggerFactory.create(GrpcClientRepository);
|
|
17
|
-
// Registration
|
|
18
|
-
servers = new Set<string>();
|
|
19
|
-
definitions = new Set<CompatServiceDefinition>();
|
|
20
|
-
// Caches
|
|
21
|
-
channels = new Map<string, Channel>();
|
|
22
|
-
clients = new Map<string, Client<CompatServiceDefinition>>();
|
|
18
|
+
readonly logger = LoggerFactory.create(GrpcClientRepository);
|
|
23
19
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
20
|
+
// Registration
|
|
21
|
+
readonly servers = new Set<string>();
|
|
22
|
+
readonly definitions = new Set<CompatServiceDefinition>();
|
|
27
23
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
): Client<CompatServiceDefinition> {
|
|
32
|
-
return GrpcClientRepository.factory.create(definition, channel);
|
|
33
|
-
}
|
|
24
|
+
// Caches
|
|
25
|
+
readonly channels = new Map<string, Channel>();
|
|
26
|
+
readonly clients = new Map<string, Client<CompatServiceDefinition>>();
|
|
34
27
|
|
|
35
28
|
resolveChannel(address: string, channelToken: string): ChannelImplementation {
|
|
36
|
-
const
|
|
37
|
-
this.channels.get(channelToken) ??
|
|
38
|
-
GrpcClientRepository.createChannel(address);
|
|
29
|
+
const channel = this.channels.get(channelToken) ?? createChannel(address);
|
|
39
30
|
|
|
40
|
-
this.channels.set(channelToken,
|
|
31
|
+
this.channels.set(channelToken, channel);
|
|
41
32
|
|
|
42
|
-
return
|
|
33
|
+
return channel;
|
|
43
34
|
}
|
|
44
35
|
|
|
45
36
|
resolveClient(
|
|
@@ -49,7 +40,7 @@ export class GrpcClientRepository {
|
|
|
49
40
|
): Client<CompatServiceDefinition> {
|
|
50
41
|
const existed: Client<CompatServiceDefinition> =
|
|
51
42
|
this.clients.get(clientToken) ??
|
|
52
|
-
GrpcClientRepository.
|
|
43
|
+
GrpcClientRepository.factory.create(definition, channel);
|
|
53
44
|
|
|
54
45
|
this.clients.set(clientToken, existed);
|
|
55
46
|
|