Skip to content

Use package import for GRPC #6002

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 11 additions & 14 deletions packages/firestore/src/platform/node/grpc_connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,9 @@
* limitations under the License.
*/

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

import { Token } from '../../api/credentials';
import { DatabaseInfo } from '../../core/database_info';
Expand All @@ -46,12 +43,12 @@ function createMetadata(
authToken: Token | null,
appCheckToken: Token | null,
appId: string
): Metadata {
): grpc.Metadata {
hardAssert(
authToken === null || authToken.type === 'OAuth',
'If provided, token must be OAuth'
);
const metadata = new Metadata();
const metadata = new grpc.Metadata();
if (authToken) {
authToken.headers.forEach((value, key) => metadata.set(key, value));
}
Expand Down Expand Up @@ -84,7 +81,7 @@ export class GrpcConnection implements Connection {
// We cache stubs for the most-recently-used token.
private cachedStub: GeneratedGrpcStub | null = null;

constructor(protos: GrpcObject, private databaseInfo: DatabaseInfo) {
constructor(protos: grpc.GrpcObject, private databaseInfo: DatabaseInfo) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
this.firestore = (protos as any)['google']['firestore']['v1'];
this.databasePath = `projects/${databaseInfo.databaseId.projectId}/databases/${databaseInfo.databaseId.database}`;
Expand All @@ -94,8 +91,8 @@ export class GrpcConnection implements Connection {
if (!this.cachedStub) {
logDebug(LOG_TAG, 'Creating Firestore stub.');
const credentials = this.databaseInfo.ssl
? GrpcCredentials.createSsl()
: GrpcCredentials.createInsecure();
? grpc.credentials.createSsl()
: grpc.credentials.createInsecure();
this.cachedStub = new this.firestore.Firestore(
this.databaseInfo.host,
credentials
Expand Down Expand Up @@ -125,7 +122,7 @@ export class GrpcConnection implements Connection {
return stub[rpcName](
jsonRequest,
metadata,
(grpcError?: ServiceError, value?: Resp) => {
(grpcError?: grpc.ServiceError, value?: Resp) => {
if (grpcError) {
logDebug(LOG_TAG, `RPC '${rpcName}' failed with error:`, grpcError);
callback(
Expand Down Expand Up @@ -179,7 +176,7 @@ export class GrpcConnection implements Connection {
logDebug(LOG_TAG, `RPC '${rpcName}' completed.`);
responseDeferred.resolve(results);
});
stream.on('error', (grpcError: ServiceError) => {
stream.on('error', (grpcError: grpc.ServiceError) => {
logDebug(LOG_TAG, `RPC '${rpcName}' failed with error:`, grpcError);
const code = mapCodeFromRpcCode(grpcError.code);
responseDeferred.reject(new FirestoreError(code, grpcError.message));
Expand Down Expand Up @@ -247,7 +244,7 @@ export class GrpcConnection implements Connection {
close();
});

grpcStream.on('error', (grpcError: ServiceError) => {
grpcStream.on('error', (grpcError: grpc.ServiceError) => {
if (!closed) {
logWarn(
LOG_TAG,
Expand Down
13 changes: 8 additions & 5 deletions packages/firestore/src/platform/node/load_protos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@

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

import { loadPackageDefinition, GrpcObject } from '@grpc/grpc-js';
import { fromJSON } from '@grpc/proto-loader';
// Note: We have to use a package import here to avoid build errors such as
// https://github.com/firebase/firebase-js-sdk/issues/5983

import * as grpc from '@grpc/grpc-js';
import * as protoLoader from '@grpc/proto-loader';
// only used in tests
// eslint-disable-next-line import/no-extraneous-dependencies
import { IConversionOptions, Root } from 'protobufjs';
Expand All @@ -38,9 +41,9 @@ export const protoLoaderOptions: IConversionOptions = {
*
* @returns The GrpcObject representing our protos.
*/
export function loadProtos(): GrpcObject {
const packageDefinition = fromJSON(protos, protoLoaderOptions);
return loadPackageDefinition(packageDefinition);
export function loadProtos(): grpc.GrpcObject {
const packageDefinition = protoLoader.fromJSON(protos, protoLoaderOptions);
return grpc.loadPackageDefinition(packageDefinition);
}

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