@@ -48,45 +48,32 @@ type UnaryRpc<Req, Resp> = (
48
48
callback : ( err ?: grpc . ServiceError , resp ?: Resp ) => void
49
49
) => grpc . ClientUnaryCall ;
50
50
51
- function createHeaders ( databaseInfo : DatabaseInfo , token : Token | null ) : { } {
51
+ function createMetadata (
52
+ databaseInfo : DatabaseInfo ,
53
+ token : Token | null
54
+ ) : grpc . Metadata {
52
55
assert (
53
56
token === null || token . type === 'OAuth' ,
54
57
'If provided, token must be OAuth'
55
58
) ;
56
59
57
- const channelCredentials = databaseInfo . ssl
58
- ? grpc . credentials . createSsl ( )
59
- : grpc . credentials . createInsecure ( ) ;
60
-
61
- const callCredentials = grpc . credentials . createFromMetadataGenerator (
62
- (
63
- context : { service_url : string } ,
64
- cb : ( error : Error | null , metadata ?: grpc . Metadata ) => void
65
- ) => {
66
- const metadata = new grpc . Metadata ( ) ;
67
- if ( token ) {
68
- for ( const header in token . authHeaders ) {
69
- if ( token . authHeaders . hasOwnProperty ( header ) ) {
70
- metadata . set ( header , token . authHeaders [ header ] ) ;
71
- }
72
- }
60
+ const metadata = new grpc . Metadata ( ) ;
61
+ if ( token ) {
62
+ for ( const header in token . authHeaders ) {
63
+ if ( token . authHeaders . hasOwnProperty ( header ) ) {
64
+ metadata . set ( header , token . authHeaders [ header ] ) ;
73
65
}
74
- metadata . set ( 'x-goog-api-client' , X_GOOG_API_CLIENT_VALUE ) ;
75
- // This header is used to improve routing and project isolation by the
76
- // backend.
77
- metadata . set (
78
- 'google-cloud-resource-prefix' ,
79
- `projects/${ databaseInfo . databaseId . projectId } /` +
80
- `databases/${ databaseInfo . databaseId . database } `
81
- ) ;
82
- cb ( null , metadata ) ;
83
66
}
67
+ }
68
+ metadata . set ( 'x-goog-api-client' , X_GOOG_API_CLIENT_VALUE ) ;
69
+ // This header is used to improve routing and project isolation by the
70
+ // backend.
71
+ metadata . set (
72
+ 'google-cloud-resource-prefix' ,
73
+ `projects/${ databaseInfo . databaseId . projectId } /` +
74
+ `databases/${ databaseInfo . databaseId . database } `
84
75
) ;
85
-
86
- return grpc . credentials . combineChannelCredentials (
87
- channelCredentials ,
88
- callCredentials
89
- ) ;
76
+ return metadata ;
90
77
}
91
78
92
79
// The type of these stubs is dynamically generated by the GRPC runtime
@@ -122,7 +109,9 @@ export class GrpcConnection implements Connection {
122
109
private ensureActiveStub ( token : Token | null ) : GeneratedGrpcStub {
123
110
if ( ! this . cachedStub || ! this . sameToken ( this . cachedStub . token , token ) ) {
124
111
log . debug ( LOG_TAG , 'Creating Firestore stub.' ) ;
125
- const credentials = createHeaders ( this . databaseInfo , token ) ;
112
+ const credentials = this . databaseInfo . ssl
113
+ ? grpc . credentials . createSsl ( )
114
+ : grpc . credentials . createInsecure ( ) ;
126
115
this . cachedStub = {
127
116
stub : new this . firestore . Firestore ( this . databaseInfo . host , credentials ) ,
128
117
token
@@ -142,7 +131,10 @@ export class GrpcConnection implements Connection {
142
131
const stub = this . ensureActiveStub ( token ) ;
143
132
const rpc = stub [ rpcMethod ] ;
144
133
assert ( rpc != null , 'Unknown RPC: ' + rpcName ) ;
145
- return rpc . bind ( stub ) ;
134
+
135
+ const metadata = createMetadata ( this . databaseInfo , token ) ;
136
+ const f = rpc . bind ( stub ) ;
137
+ return ( ...args ) => f ( ...args , metadata ) ;
146
138
}
147
139
148
140
invokeRPC < Req , Resp > (
0 commit comments