Skip to content

Commit 81cd260

Browse files
Embed metadata directly into the RPC call (#979)
* Embed metadata directly into the RPC call * [AUTOMATED]: Prettier Code Styling * Use ...args * [AUTOMATED]: Prettier Code Styling * Minimize diff * Add the OAuth assertion back in
1 parent 284a746 commit 81cd260

File tree

1 file changed

+25
-33
lines changed

1 file changed

+25
-33
lines changed

packages/firestore/src/platform_node/grpc_connection.ts

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -48,45 +48,32 @@ type UnaryRpc<Req, Resp> = (
4848
callback: (err?: grpc.ServiceError, resp?: Resp) => void
4949
) => grpc.ClientUnaryCall;
5050

51-
function createHeaders(databaseInfo: DatabaseInfo, token: Token | null): {} {
51+
function createMetadata(
52+
databaseInfo: DatabaseInfo,
53+
token: Token | null
54+
): grpc.Metadata {
5255
assert(
5356
token === null || token.type === 'OAuth',
5457
'If provided, token must be OAuth'
5558
);
5659

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]);
7365
}
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);
8366
}
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}`
8475
);
85-
86-
return grpc.credentials.combineChannelCredentials(
87-
channelCredentials,
88-
callCredentials
89-
);
76+
return metadata;
9077
}
9178

9279
// The type of these stubs is dynamically generated by the GRPC runtime
@@ -122,7 +109,9 @@ export class GrpcConnection implements Connection {
122109
private ensureActiveStub(token: Token | null): GeneratedGrpcStub {
123110
if (!this.cachedStub || !this.sameToken(this.cachedStub.token, token)) {
124111
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();
126115
this.cachedStub = {
127116
stub: new this.firestore.Firestore(this.databaseInfo.host, credentials),
128117
token
@@ -142,7 +131,10 @@ export class GrpcConnection implements Connection {
142131
const stub = this.ensureActiveStub(token);
143132
const rpc = stub[rpcMethod];
144133
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);
146138
}
147139

148140
invokeRPC<Req, Resp>(

0 commit comments

Comments
 (0)