Skip to content

Commit 307d019

Browse files
Use package import for GRPC (#6002)
1 parent 3a8d4c1 commit 307d019

File tree

2 files changed

+19
-19
lines changed

2 files changed

+19
-19
lines changed

packages/firestore/src/platform/node/grpc_connection.ts

+11-14
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,9 @@
1515
* limitations under the License.
1616
*/
1717

18-
import {
19-
Metadata,
20-
GrpcObject,
21-
credentials as GrpcCredentials,
22-
ServiceError
23-
} from '@grpc/grpc-js';
18+
// Note: We have to use a package import here to avoid build errors such as
19+
// https://github.com/firebase/firebase-js-sdk/issues/5983
20+
import * as grpc from '@grpc/grpc-js';
2421

2522
import { Token } from '../../api/credentials';
2623
import { DatabaseInfo } from '../../core/database_info';
@@ -46,12 +43,12 @@ function createMetadata(
4643
authToken: Token | null,
4744
appCheckToken: Token | null,
4845
appId: string
49-
): Metadata {
46+
): grpc.Metadata {
5047
hardAssert(
5148
authToken === null || authToken.type === 'OAuth',
5249
'If provided, token must be OAuth'
5350
);
54-
const metadata = new Metadata();
51+
const metadata = new grpc.Metadata();
5552
if (authToken) {
5653
authToken.headers.forEach((value, key) => metadata.set(key, value));
5754
}
@@ -84,7 +81,7 @@ export class GrpcConnection implements Connection {
8481
// We cache stubs for the most-recently-used token.
8582
private cachedStub: GeneratedGrpcStub | null = null;
8683

87-
constructor(protos: GrpcObject, private databaseInfo: DatabaseInfo) {
84+
constructor(protos: grpc.GrpcObject, private databaseInfo: DatabaseInfo) {
8885
// eslint-disable-next-line @typescript-eslint/no-explicit-any
8986
this.firestore = (protos as any)['google']['firestore']['v1'];
9087
this.databasePath = `projects/${databaseInfo.databaseId.projectId}/databases/${databaseInfo.databaseId.database}`;
@@ -94,8 +91,8 @@ export class GrpcConnection implements Connection {
9491
if (!this.cachedStub) {
9592
logDebug(LOG_TAG, 'Creating Firestore stub.');
9693
const credentials = this.databaseInfo.ssl
97-
? GrpcCredentials.createSsl()
98-
: GrpcCredentials.createInsecure();
94+
? grpc.credentials.createSsl()
95+
: grpc.credentials.createInsecure();
9996
this.cachedStub = new this.firestore.Firestore(
10097
this.databaseInfo.host,
10198
credentials
@@ -125,7 +122,7 @@ export class GrpcConnection implements Connection {
125122
return stub[rpcName](
126123
jsonRequest,
127124
metadata,
128-
(grpcError?: ServiceError, value?: Resp) => {
125+
(grpcError?: grpc.ServiceError, value?: Resp) => {
129126
if (grpcError) {
130127
logDebug(LOG_TAG, `RPC '${rpcName}' failed with error:`, grpcError);
131128
callback(
@@ -179,7 +176,7 @@ export class GrpcConnection implements Connection {
179176
logDebug(LOG_TAG, `RPC '${rpcName}' completed.`);
180177
responseDeferred.resolve(results);
181178
});
182-
stream.on('error', (grpcError: ServiceError) => {
179+
stream.on('error', (grpcError: grpc.ServiceError) => {
183180
logDebug(LOG_TAG, `RPC '${rpcName}' failed with error:`, grpcError);
184181
const code = mapCodeFromRpcCode(grpcError.code);
185182
responseDeferred.reject(new FirestoreError(code, grpcError.message));
@@ -247,7 +244,7 @@ export class GrpcConnection implements Connection {
247244
close();
248245
});
249246

250-
grpcStream.on('error', (grpcError: ServiceError) => {
247+
grpcStream.on('error', (grpcError: grpc.ServiceError) => {
251248
if (!closed) {
252249
logWarn(
253250
LOG_TAG,

packages/firestore/src/platform/node/load_protos.ts

+8-5
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@
1717

1818
import { join, resolve, isAbsolute } from 'path';
1919

20-
import { loadPackageDefinition, GrpcObject } from '@grpc/grpc-js';
21-
import { fromJSON } from '@grpc/proto-loader';
20+
// Note: We have to use a package import here to avoid build errors such as
21+
// https://github.com/firebase/firebase-js-sdk/issues/5983
22+
23+
import * as grpc from '@grpc/grpc-js';
24+
import * as protoLoader from '@grpc/proto-loader';
2225
// only used in tests
2326
// eslint-disable-next-line import/no-extraneous-dependencies
2427
import { IConversionOptions, Root } from 'protobufjs';
@@ -38,9 +41,9 @@ export const protoLoaderOptions: IConversionOptions = {
3841
*
3942
* @returns The GrpcObject representing our protos.
4043
*/
41-
export function loadProtos(): GrpcObject {
42-
const packageDefinition = fromJSON(protos, protoLoaderOptions);
43-
return loadPackageDefinition(packageDefinition);
44+
export function loadProtos(): grpc.GrpcObject {
45+
const packageDefinition = protoLoader.fromJSON(protos, protoLoaderOptions);
46+
return grpc.loadPackageDefinition(packageDefinition);
4447
}
4548

4649
/** Used by tests so we can directly create ProtobufJS proto message objects from JSON protos. */

0 commit comments

Comments
 (0)