15
15
* limitations under the License.
16
16
*/
17
17
18
- import * as grpc from 'grpc' ;
18
+ import {
19
+ Metadata ,
20
+ GrpcObject ,
21
+ credentials as GrpcCredentials ,
22
+ ServiceError
23
+ } from '@grpc/grpc-js' ;
19
24
20
25
import firebase from '@firebase/app' ;
21
26
const SDK_VERSION = firebase . SDK_VERSION ;
22
27
23
28
// eslint-disable-next-line @typescript-eslint/no-require-imports
24
- const grpcVersion = require ( 'grpc/package.json' ) . version ;
29
+ const grpcVersion = require ( '@ grpc/grpc-js /package.json' ) . version ;
25
30
26
31
import { Token } from '../api/credentials' ;
27
32
import { DatabaseInfo } from '../core/database_info' ;
@@ -44,13 +49,13 @@ const X_GOOG_API_CLIENT_VALUE = `gl-node/${process.versions.node} fire/${SDK_VER
44
49
function createMetadata (
45
50
databaseInfo : DatabaseInfo ,
46
51
token : Token | null
47
- ) : grpc . Metadata {
52
+ ) : Metadata {
48
53
assert (
49
54
token === null || token . type === 'OAuth' ,
50
55
'If provided, token must be OAuth'
51
56
) ;
52
57
53
- const metadata = new grpc . Metadata ( ) ;
58
+ const metadata = new Metadata ( ) ;
54
59
if ( token ) {
55
60
for ( const header in token . authHeaders ) {
56
61
if ( token . authHeaders . hasOwnProperty ( header ) ) {
@@ -84,7 +89,7 @@ export class GrpcConnection implements Connection {
84
89
// We cache stubs for the most-recently-used token.
85
90
private cachedStub : GeneratedGrpcStub | null = null ;
86
91
87
- constructor ( protos : grpc . GrpcObject , private databaseInfo : DatabaseInfo ) {
92
+ constructor ( protos : GrpcObject , private databaseInfo : DatabaseInfo ) {
88
93
// eslint-disable-next-line @typescript-eslint/no-explicit-any
89
94
this . firestore = ( protos as any ) [ 'google' ] [ 'firestore' ] [ 'v1' ] ;
90
95
}
@@ -93,8 +98,8 @@ export class GrpcConnection implements Connection {
93
98
if ( ! this . cachedStub ) {
94
99
logDebug ( LOG_TAG , 'Creating Firestore stub.' ) ;
95
100
const credentials = this . databaseInfo . ssl
96
- ? grpc . credentials . createSsl ( )
97
- : grpc . credentials . createInsecure ( ) ;
101
+ ? GrpcCredentials . createSsl ( )
102
+ : GrpcCredentials . createInsecure ( ) ;
98
103
this . cachedStub = new this . firestore . Firestore (
99
104
this . databaseInfo . host ,
100
105
credentials
@@ -116,7 +121,7 @@ export class GrpcConnection implements Connection {
116
121
return stub [ rpcName ] (
117
122
request ,
118
123
metadata ,
119
- ( grpcError ?: grpc . ServiceError , value ?: Resp ) => {
124
+ ( grpcError ?: ServiceError , value ?: Resp ) => {
120
125
if ( grpcError ) {
121
126
logDebug ( LOG_TAG , `RPC '${ rpcName } ' failed with error:` , grpcError ) ;
122
127
callback (
@@ -162,7 +167,7 @@ export class GrpcConnection implements Connection {
162
167
logDebug ( LOG_TAG , `RPC '${ rpcName } ' completed.` ) ;
163
168
responseDeferred . resolve ( results ) ;
164
169
} ) ;
165
- stream . on ( 'error' , ( grpcError : grpc . ServiceError ) => {
170
+ stream . on ( 'error' , ( grpcError : ServiceError ) => {
166
171
logDebug ( LOG_TAG , `RPC '${ rpcName } ' failed with error:` , grpcError ) ;
167
172
const code = mapCodeFromRpcCode ( grpcError . code ) ;
168
173
responseDeferred . reject ( new FirestoreError ( code , grpcError . message ) ) ;
@@ -224,7 +229,7 @@ export class GrpcConnection implements Connection {
224
229
close ( ) ;
225
230
} ) ;
226
231
227
- grpcStream . on ( 'error' , ( grpcError : grpc . ServiceError ) => {
232
+ grpcStream . on ( 'error' , ( grpcError : ServiceError ) => {
228
233
logDebug (
229
234
LOG_TAG ,
230
235
'GRPC stream error. Code:' ,
0 commit comments