Skip to content

Commit 92c093a

Browse files
Merge 9a2033a into b9087b9
2 parents b9087b9 + 9a2033a commit 92c093a

File tree

11 files changed

+40
-6
lines changed

11 files changed

+40
-6
lines changed

.changeset/honest-hounds-sin.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@firebase/firestore": patch
3+
---
4+
5+
Added new internal HTTP header to all network requests.

packages/firestore/exp/src/api/database.ts

+1
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ export class FirebaseFirestore
118118
await this._receivedInitialUser.promise;
119119
const databaseInfo = new DatabaseInfo(
120120
this._databaseId,
121+
this.app.options.appId || '',
121122
this._persistenceKey,
122123
settings.host ?? DEFAULT_HOST,
123124
settings.ssl ?? DEFAULT_SSL,

packages/firestore/lite/src/api/components.ts

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ export function getDatastore(firestore: FirebaseFirestore): Datastore {
5656
const settings = firestore._getSettings();
5757
const databaseInfo = new DatabaseInfo(
5858
firestore._databaseId,
59+
firestore.app.options.appId || '',
5960
firestore._persistenceKey,
6061
settings.host ?? DEFAULT_HOST,
6162
settings.ssl ?? DEFAULT_SSL,

packages/firestore/src/api/database.ts

+1
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,7 @@ export class Firestore implements PublicFirestore, FirebaseService {
549549
private makeDatabaseInfo(): DatabaseInfo {
550550
return new DatabaseInfo(
551551
this._databaseId,
552+
this._firebaseApp?.options.appId || '',
552553
this._persistenceKey,
553554
this._settings.host,
554555
this._settings.ssl,

packages/firestore/src/core/database_info.ts

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export class DatabaseInfo {
2323
* persistenceKey.
2424
*
2525
* @param databaseId The database to use.
26+
* @param appId The Firebase App Id.
2627
* @param persistenceKey A unique identifier for this Firestore's local
2728
* storage (used in conjunction with the databaseId).
2829
* @param host The Firestore backend host to connect to.
@@ -32,6 +33,7 @@ export class DatabaseInfo {
3233
*/
3334
constructor(
3435
readonly databaseId: DatabaseId,
36+
readonly appId: string,
3537
readonly persistenceKey: string,
3638
readonly host: string,
3739
readonly ssl: boolean,

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

+23-6
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,11 @@ import { SDK_VERSION } from '../../core/version';
3737
const LOG_TAG = 'Connection';
3838
const X_GOOG_API_CLIENT_VALUE = `gl-node/${process.versions.node} fire/${SDK_VERSION} grpc/${grpcVersion}`;
3939

40-
function createMetadata(databasePath: string, token: Token | null): Metadata {
40+
function createMetadata(
41+
databasePath: string,
42+
token: Token | null,
43+
appId: string
44+
): Metadata {
4145
hardAssert(
4246
token === null || token.type === 'OAuth',
4347
'If provided, token must be OAuth'
@@ -51,10 +55,11 @@ function createMetadata(databasePath: string, token: Token | null): Metadata {
5155
}
5256
}
5357
}
54-
metadata.set('x-goog-api-client', X_GOOG_API_CLIENT_VALUE);
58+
metadata.set('X-Firebase-GMPID', appId);
59+
metadata.set('X-Goog-Api-Client', X_GOOG_API_CLIENT_VALUE);
5560
// This header is used to improve routing and project isolation by the
5661
// backend.
57-
metadata.set('google-cloud-resource-prefix', databasePath);
62+
metadata.set('Google-Cloud-Resource-Prefix', databasePath);
5863
return metadata;
5964
}
6065

@@ -101,7 +106,11 @@ export class GrpcConnection implements Connection {
101106
token: Token | null
102107
): Promise<Resp> {
103108
const stub = this.ensureActiveStub();
104-
const metadata = createMetadata(this.databasePath, token);
109+
const metadata = createMetadata(
110+
this.databasePath,
111+
token,
112+
this.databaseInfo.appId
113+
);
105114
const jsonRequest = { database: this.databasePath, ...request };
106115

107116
return nodePromise((callback: NodeCallback<Resp>) => {
@@ -146,7 +155,11 @@ export class GrpcConnection implements Connection {
146155
request
147156
);
148157
const stub = this.ensureActiveStub();
149-
const metadata = createMetadata(this.databasePath, token);
158+
const metadata = createMetadata(
159+
this.databasePath,
160+
token,
161+
this.databaseInfo.appId
162+
);
150163
const jsonRequest = { ...request, database: this.databasePath };
151164
const stream = stub[rpcName](jsonRequest, metadata);
152165
stream.on('data', (response: Resp) => {
@@ -172,7 +185,11 @@ export class GrpcConnection implements Connection {
172185
token: Token | null
173186
): Stream<Req, Resp> {
174187
const stub = this.ensureActiveStub();
175-
const metadata = createMetadata(this.databasePath, token);
188+
const metadata = createMetadata(
189+
this.databasePath,
190+
token,
191+
this.databaseInfo.appId
192+
);
176193
const grpcStream = stub[rpcName](metadata);
177194

178195
let closed = false;

packages/firestore/src/remote/rest_connection.ts

+1
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ export abstract class RestConnection implements Connection {
118118
token: Token | null
119119
): void {
120120
headers['X-Goog-Api-Client'] = X_GOOG_API_CLIENT_VALUE;
121+
headers['X-Firebase-GMPID'] = this.databaseInfo.appId;
121122

122123
// Content-Type: text/plain will avoid preflight requests which might
123124
// mess with CORS and redirects by proxies. If we add custom headers

packages/firestore/test/integration/util/internal_helpers.ts

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export function asyncQueue(db: firestore.FirebaseFirestore): AsyncQueue {
4141
export function getDefaultDatabaseInfo(): DatabaseInfo {
4242
return new DatabaseInfo(
4343
new DatabaseId(DEFAULT_PROJECT_ID),
44+
'test-app-id',
4445
'persistenceKey',
4546
DEFAULT_SETTINGS.host!,
4647
!!DEFAULT_SETTINGS.ssl,

packages/firestore/test/unit/local/persistence_test_helpers.ts

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ export const MOCK_SEQUENCE_NUMBER_SYNCER: SequenceNumberSyncer = {
5959
export const TEST_PROJECT = 'test-project';
6060
export const TEST_DATABASE_ID = new DatabaseId(TEST_PROJECT);
6161
export const TEST_PERSISTENCE_KEY = '[PersistenceTestHelpers]';
62+
export const TEST_APP_ID = 'test-app-id';
6263

6364
/** The persistence prefix used for testing in IndexedBD and LocalStorage. */
6465
export const TEST_PERSISTENCE_PREFIX = indexedDbStoragePrefix(

packages/firestore/test/unit/remote/rest_connection.test.ts

+2
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ export class TestRestConnection extends RestConnection {
5757
describe('RestConnection', () => {
5858
const testDatabaseInfo = new DatabaseInfo(
5959
new DatabaseId('testproject'),
60+
'test-app-id',
6061
'persistenceKey',
6162
'example.com',
6263
/*ssl=*/ false,
@@ -90,6 +91,7 @@ describe('RestConnection', () => {
9091
expect(connection.lastHeaders).to.deep.equal({
9192
'Authorization': 'Bearer owner',
9293
'Content-Type': 'text/plain',
94+
'X-Firebase-GMPID': 'test-app-id',
9395
'X-Goog-Api-Client': `gl-js/ fire/${SDK_VERSION}`
9496
});
9597
});

packages/firestore/test/unit/specs/spec_test_runner.ts

+2
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ import { encodeWatchChange } from '../../util/spec_test_helpers';
124124
import {
125125
clearTestPersistence,
126126
INDEXEDDB_TEST_DATABASE_NAME,
127+
TEST_APP_ID,
127128
TEST_DATABASE_ID,
128129
TEST_PERSISTENCE_KEY,
129130
TEST_SERIALIZER
@@ -238,6 +239,7 @@ abstract class TestRunner {
238239
this.clientId = `client${clientIndex}`;
239240
this.databaseInfo = new DatabaseInfo(
240241
TEST_DATABASE_ID,
242+
TEST_APP_ID,
241243
TEST_PERSISTENCE_KEY,
242244
'host',
243245
/*ssl=*/ false,

0 commit comments

Comments
 (0)