From 98f7c0f775c88ba09e412b18d6a537e844be6c4b Mon Sep 17 00:00:00 2001 From: "Ryan P. Brewster" Date: Tue, 3 Jul 2018 15:38:35 -0700 Subject: [PATCH 1/6] Embed metadata directly into the RPC call --- .../src/platform_node/grpc_connection.ts | 67 +++++++------------ 1 file changed, 24 insertions(+), 43 deletions(-) diff --git a/packages/firestore/src/platform_node/grpc_connection.ts b/packages/firestore/src/platform_node/grpc_connection.ts index 639c8c3b831..96c8326d80f 100644 --- a/packages/firestore/src/platform_node/grpc_connection.ts +++ b/packages/firestore/src/platform_node/grpc_connection.ts @@ -48,47 +48,6 @@ type UnaryRpc = ( callback: (err?: grpc.ServiceError, resp?: Resp) => void ) => grpc.ClientUnaryCall; -function createHeaders(databaseInfo: DatabaseInfo, token: Token | null): {} { - assert( - token === null || token.type === 'OAuth', - 'If provided, token must be OAuth' - ); - - const channelCredentials = databaseInfo.ssl - ? grpc.credentials.createSsl() - : grpc.credentials.createInsecure(); - - const callCredentials = grpc.credentials.createFromMetadataGenerator( - ( - context: { service_url: string }, - cb: (error: Error | null, metadata?: grpc.Metadata) => void - ) => { - const metadata = new grpc.Metadata(); - if (token) { - for (const header in token.authHeaders) { - if (token.authHeaders.hasOwnProperty(header)) { - metadata.set(header, token.authHeaders[header]); - } - } - } - metadata.set('x-goog-api-client', X_GOOG_API_CLIENT_VALUE); - // This header is used to improve routing and project isolation by the - // backend. - metadata.set( - 'google-cloud-resource-prefix', - `projects/${databaseInfo.databaseId.projectId}/` + - `databases/${databaseInfo.databaseId.database}` - ); - cb(null, metadata); - } - ); - - return grpc.credentials.combineChannelCredentials( - channelCredentials, - callCredentials - ); -} - // The type of these stubs is dynamically generated by the GRPC runtime // from the protocol buffer. // tslint:disable-next-line:no-any @@ -122,7 +81,9 @@ export class GrpcConnection implements Connection { private ensureActiveStub(token: Token | null): GeneratedGrpcStub { if (!this.cachedStub || !this.sameToken(this.cachedStub.token, token)) { log.debug(LOG_TAG, 'Creating Firestore stub.'); - const credentials = createHeaders(this.databaseInfo, token); + const credentials = this.databaseInfo.ssl + ? grpc.credentials.createSsl() + : grpc.credentials.createInsecure(); this.cachedStub = { stub: new this.firestore.Firestore(this.databaseInfo.host, credentials), token @@ -142,7 +103,27 @@ export class GrpcConnection implements Connection { const stub = this.ensureActiveStub(token); const rpc = stub[rpcMethod]; assert(rpc != null, 'Unknown RPC: ' + rpcName); - return rpc.bind(stub); + + const metadata = new grpc.Metadata(); + if (token) { + for (const header in token.authHeaders) { + if (token.authHeaders.hasOwnProperty(header)) { + metadata.set(header, token.authHeaders[header]); + } + } + } + metadata.set('x-goog-api-client', X_GOOG_API_CLIENT_VALUE); + // This header is used to improve routing and project isolation by the + // backend. + metadata.set( + 'google-cloud-resource-prefix', + `projects/${this.databaseInfo.databaseId.projectId}/` + + `databases/${this.databaseInfo.databaseId.database}` + ); + const f = rpc.bind(stub); + return function() { + return f(arguments, metadata); + } } invokeRPC( From c1fe2a1521cd23705c32afe7fc29179cf018ebe8 Mon Sep 17 00:00:00 2001 From: "Ryan P. Brewster" Date: Tue, 3 Jul 2018 15:38:57 -0700 Subject: [PATCH 2/6] [AUTOMATED]: Prettier Code Styling --- packages/firestore/src/platform_node/grpc_connection.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/firestore/src/platform_node/grpc_connection.ts b/packages/firestore/src/platform_node/grpc_connection.ts index 96c8326d80f..fdb31409118 100644 --- a/packages/firestore/src/platform_node/grpc_connection.ts +++ b/packages/firestore/src/platform_node/grpc_connection.ts @@ -123,7 +123,7 @@ export class GrpcConnection implements Connection { const f = rpc.bind(stub); return function() { return f(arguments, metadata); - } + }; } invokeRPC( From 6d7bf23d12182d3fdedc51c1d400664a6322feca Mon Sep 17 00:00:00 2001 From: "Ryan P. Brewster" Date: Tue, 3 Jul 2018 16:06:57 -0700 Subject: [PATCH 3/6] Use ...args --- .../src/platform_node/grpc_connection.ts | 41 ++++++++++--------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/packages/firestore/src/platform_node/grpc_connection.ts b/packages/firestore/src/platform_node/grpc_connection.ts index fdb31409118..13cdacf6ada 100644 --- a/packages/firestore/src/platform_node/grpc_connection.ts +++ b/packages/firestore/src/platform_node/grpc_connection.ts @@ -58,6 +58,26 @@ interface CachedStub { token: Token | null; } +function createMetadata(databaseInfo: DatabaseInfo, token: Token | null): grpc.Metadata { + const metadata = new grpc.Metadata(); + if (token) { + for (const header in token.authHeaders) { + if (token.authHeaders.hasOwnProperty(header)) { + metadata.set(header, token.authHeaders[header]); + } + } + } + metadata.set('x-goog-api-client', X_GOOG_API_CLIENT_VALUE); + // This header is used to improve routing and project isolation by the + // backend. + metadata.set( + 'google-cloud-resource-prefix', + `projects/${databaseInfo.databaseId.projectId}/` + + `databases/${databaseInfo.databaseId.database}` + ); + return metadata; +} + /** * A Connection implemented by GRPC-Node. */ @@ -104,26 +124,9 @@ export class GrpcConnection implements Connection { const rpc = stub[rpcMethod]; assert(rpc != null, 'Unknown RPC: ' + rpcName); - const metadata = new grpc.Metadata(); - if (token) { - for (const header in token.authHeaders) { - if (token.authHeaders.hasOwnProperty(header)) { - metadata.set(header, token.authHeaders[header]); - } - } - } - metadata.set('x-goog-api-client', X_GOOG_API_CLIENT_VALUE); - // This header is used to improve routing and project isolation by the - // backend. - metadata.set( - 'google-cloud-resource-prefix', - `projects/${this.databaseInfo.databaseId.projectId}/` + - `databases/${this.databaseInfo.databaseId.database}` - ); + const metadata = createMetadata(this.databaseInfo, token); const f = rpc.bind(stub); - return function() { - return f(arguments, metadata); - }; + return (...args) => f(...args, metadata); } invokeRPC( From 014696ea62394c4c4dff99e6cd1eba0b974b364e Mon Sep 17 00:00:00 2001 From: "Ryan P. Brewster" Date: Tue, 3 Jul 2018 16:07:17 -0700 Subject: [PATCH 4/6] [AUTOMATED]: Prettier Code Styling --- packages/firestore/src/platform_node/grpc_connection.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/firestore/src/platform_node/grpc_connection.ts b/packages/firestore/src/platform_node/grpc_connection.ts index 13cdacf6ada..fd9377a6220 100644 --- a/packages/firestore/src/platform_node/grpc_connection.ts +++ b/packages/firestore/src/platform_node/grpc_connection.ts @@ -58,7 +58,10 @@ interface CachedStub { token: Token | null; } -function createMetadata(databaseInfo: DatabaseInfo, token: Token | null): grpc.Metadata { +function createMetadata( + databaseInfo: DatabaseInfo, + token: Token | null +): grpc.Metadata { const metadata = new grpc.Metadata(); if (token) { for (const header in token.authHeaders) { From 63957d2a7f7891e96028ba4322c5ede214b1632a Mon Sep 17 00:00:00 2001 From: "Ryan P. Brewster" Date: Tue, 3 Jul 2018 16:08:27 -0700 Subject: [PATCH 5/6] Minimize diff --- .../src/platform_node/grpc_connection.ts | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/packages/firestore/src/platform_node/grpc_connection.ts b/packages/firestore/src/platform_node/grpc_connection.ts index fd9377a6220..02affd29df4 100644 --- a/packages/firestore/src/platform_node/grpc_connection.ts +++ b/packages/firestore/src/platform_node/grpc_connection.ts @@ -48,16 +48,6 @@ type UnaryRpc = ( callback: (err?: grpc.ServiceError, resp?: Resp) => void ) => grpc.ClientUnaryCall; -// The type of these stubs is dynamically generated by the GRPC runtime -// from the protocol buffer. -// tslint:disable-next-line:no-any -type GeneratedGrpcStub = any; - -interface CachedStub { - stub: GeneratedGrpcStub; - token: Token | null; -} - function createMetadata( databaseInfo: DatabaseInfo, token: Token | null @@ -81,6 +71,16 @@ function createMetadata( return metadata; } +// The type of these stubs is dynamically generated by the GRPC runtime +// from the protocol buffer. +// tslint:disable-next-line:no-any +type GeneratedGrpcStub = any; + +interface CachedStub { + stub: GeneratedGrpcStub; + token: Token | null; +} + /** * A Connection implemented by GRPC-Node. */ From c6cddf54005f28b79e580a403f884c08b0498b37 Mon Sep 17 00:00:00 2001 From: "Ryan P. Brewster" Date: Tue, 3 Jul 2018 16:13:20 -0700 Subject: [PATCH 6/6] Add the OAuth assertion back in --- packages/firestore/src/platform_node/grpc_connection.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/firestore/src/platform_node/grpc_connection.ts b/packages/firestore/src/platform_node/grpc_connection.ts index 02affd29df4..2fac9fd2007 100644 --- a/packages/firestore/src/platform_node/grpc_connection.ts +++ b/packages/firestore/src/platform_node/grpc_connection.ts @@ -52,6 +52,11 @@ function createMetadata( databaseInfo: DatabaseInfo, token: Token | null ): grpc.Metadata { + assert( + token === null || token.type === 'OAuth', + 'If provided, token must be OAuth' + ); + const metadata = new grpc.Metadata(); if (token) { for (const header in token.authHeaders) {