@@ -20,6 +20,8 @@ import { Logger } from "../../../logger/Logger";
20
20
import GRPCClientSidecar from "./sidecar" ;
21
21
import DaprClient from "../DaprClient" ;
22
22
import { SDK_VERSION } from "../../../version" ;
23
+ import communicationProtocolEnum from "../../../enum/CommunicationProtocol.enum" ;
24
+ import { GrpcEndpoint } from "../../../network/GrpcEndpoint" ;
23
25
24
26
export default class GRPCClient implements IClient {
25
27
readonly options : DaprClientOptions ;
@@ -29,17 +31,34 @@ export default class GRPCClient implements IClient {
29
31
private readonly clientCredentials : grpc . ChannelCredentials ;
30
32
private readonly logger : Logger ;
31
33
private readonly grpcClientOptions : Partial < grpc . ClientOptions > ;
34
+ private daprEndpoint : GrpcEndpoint ;
35
+
36
+ constructor ( options : Partial < DaprClientOptions > ) {
37
+ this . daprEndpoint = this . generateEndpoint ( options ) ;
38
+
39
+ this . options = {
40
+ daprHost : this . daprEndpoint . hostname ,
41
+ daprPort : this . daprEndpoint . port ,
42
+ communicationProtocol : communicationProtocolEnum . GRPC ,
43
+ isKeepAlive : options ?. isKeepAlive ,
44
+ logger : options ?. logger ,
45
+ actor : options ?. actor ,
46
+ daprApiToken : options ?. daprApiToken ,
47
+ maxBodySizeMb : options ?. maxBodySizeMb ,
48
+ } ;
32
49
33
- constructor ( options : DaprClientOptions ) {
34
- this . options = options ;
35
50
this . clientCredentials = this . generateCredentials ( ) ;
36
51
this . grpcClientOptions = this . generateChannelOptions ( ) ;
37
52
38
53
this . logger = new Logger ( "GRPCClient" , "GRPCClient" , options . logger ) ;
39
54
this . isInitialized = false ;
40
55
41
56
this . logger . info ( `Opening connection to ${ this . options . daprHost } :${ this . options . daprPort } ` ) ;
42
- this . client = this . generateClient ( this . options . daprHost , this . options . daprPort ) ;
57
+ this . client = new GrpcDaprClient (
58
+ this . daprEndpoint . endpoint ,
59
+ this . getClientCredentials ( ) ,
60
+ this . getGrpcClientOptions ( ) ,
61
+ ) ;
43
62
}
44
63
45
64
async getClient ( requiresInitialization = true ) : Promise < GrpcDaprClient > {
@@ -59,8 +78,24 @@ export default class GRPCClient implements IClient {
59
78
return this . grpcClientOptions ;
60
79
}
61
80
81
+ private generateEndpoint ( options : Partial < DaprClientOptions > ) : GrpcEndpoint {
82
+ const host = options ?. daprHost ?? Settings . getDefaultHost ( ) ;
83
+ const port = options ?. daprPort ?? Settings . getDefaultGrpcPort ( ) ;
84
+ let uri = `${ host } :${ port } ` ;
85
+
86
+ if ( ! ( options ?. daprHost || options ?. daprPort ) ) {
87
+ // If neither host nor port are specified, check the endpoint environment variable.
88
+ const endpoint = Settings . getDefaultGrpcEndpoint ( ) ;
89
+ if ( endpoint != "" ) {
90
+ uri = endpoint ;
91
+ }
92
+ }
93
+
94
+ return new GrpcEndpoint ( uri ) ;
95
+ }
96
+
62
97
private generateCredentials ( ) : grpc . ChannelCredentials {
63
- if ( this . options . daprHost . startsWith ( "https" ) ) {
98
+ if ( this . daprEndpoint ?. tls ) {
64
99
return grpc . ChannelCredentials . createSsl ( ) ;
65
100
}
66
101
return grpc . ChannelCredentials . createInsecure ( ) ;
@@ -93,26 +128,6 @@ export default class GRPCClient implements IClient {
93
128
return options ;
94
129
}
95
130
96
- private generateClient ( host : string , port : string ) : GrpcDaprClient {
97
- return new GrpcDaprClient (
98
- GRPCClient . getEndpoint ( host , port ) ,
99
- this . getClientCredentials ( ) ,
100
- this . getGrpcClientOptions ( ) ,
101
- ) ;
102
- }
103
-
104
- // The grpc client doesn't allow http:// or https:// for grpc connections,
105
- // so we need to remove it, if it exists
106
- static getEndpoint ( host : string , port : string ) : string {
107
- let endpoint = `${ host } :${ port } ` ;
108
- const parts = endpoint . split ( "://" ) ;
109
- if ( parts . length > 1 && parts [ 0 ] . startsWith ( "http" ) ) {
110
- endpoint = parts [ 1 ] ;
111
- }
112
-
113
- return endpoint ;
114
- }
115
-
116
131
private generateInterceptors ( ) : ( options : any , nextCall : any ) => grpc . InterceptingCall {
117
132
return ( options : any , nextCall : any ) => {
118
133
return new grpc . InterceptingCall ( nextCall ( options ) , {
0 commit comments