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