Skip to content

Commit 65c05e2

Browse files
Add GPMID header to Firebase Database (#3190)
1 parent 02da0f0 commit 65c05e2

File tree

10 files changed

+47
-27
lines changed

10 files changed

+47
-27
lines changed

packages/database/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Unreleased
2+
- [changed] Added internal HTTP header to the WebChannel connection.
23
- [feature] Added ServerValue.increment() to support atomic field value increments
34
without transactions.
45
- [fixed] Fixed Realtime Database URL parsing bug to support domains with more than 3 components.

packages/database/src/core/PersistentConnection.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,12 @@ export class PersistentConnection extends ServerActions {
132132
/**
133133
* @implements {ServerActions}
134134
* @param repoInfo_ Data about the namespace we are connecting to
135+
* @param applicationId_ The Firebase App ID for this project
135136
* @param onDataUpdate_ A callback for new data from the server
136137
*/
137138
constructor(
138139
private repoInfo_: RepoInfo,
140+
private applicationId_: string,
139141
private onDataUpdate_: (
140142
a: string,
141143
b: unknown,
@@ -782,6 +784,7 @@ export class PersistentConnection extends ServerActions {
782784
connection = new Connection(
783785
connId,
784786
self.repoInfo_,
787+
self.applicationId_,
785788
onDataMessage,
786789
onReady,
787790
onDisconnect,

packages/database/src/core/Repo.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ export class Repo {
116116

117117
this.persistentConnection_ = new PersistentConnection(
118118
this.repoInfo_,
119+
app.options.appId,
119120
this.onDataUpdate_.bind(this),
120121
this.onConnectStatus_.bind(this),
121122
this.onServerInfoUpdate_.bind(this),

packages/database/src/realtime/BrowserPollConnection.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
import { StatsManager } from '../core/stats/StatsManager';
2828
import { PacketReceiver } from './polling/PacketReceiver';
2929
import {
30+
APPLICATION_ID_PARAM,
3031
FORGE_DOMAIN,
3132
FORGE_REF,
3233
LAST_SESSION_PARAM,
@@ -104,16 +105,18 @@ export class BrowserPollConnection implements Transport {
104105
private onDisconnect_: ((a?: boolean) => void) | null;
105106

106107
/**
107-
* @param {string} connId An identifier for this connection, used for logging
108-
* @param {RepoInfo} repoInfo The info for the endpoint to send data to.
109-
* @param {string=} transportSessionId Optional transportSessionid if we are reconnecting for an existing
108+
* @param connId An identifier for this connection, used for logging
109+
* @param repoInfo The info for the endpoint to send data to.
110+
* @param applicationId The Firebase App ID for this project.
111+
* @param transportSessionId Optional transportSessionid if we are reconnecting for an existing
110112
* transport session
111-
* @param {string=} lastSessionId Optional lastSessionId if the PersistentConnection has already created a
113+
* @param lastSessionId Optional lastSessionId if the PersistentConnection has already created a
112114
* connection previously
113115
*/
114116
constructor(
115117
public connId: string,
116118
public repoInfo: RepoInfo,
119+
private applicationId?: string,
117120
public transportSessionId?: string,
118121
public lastSessionId?: string
119122
) {
@@ -214,6 +217,9 @@ export class BrowserPollConnection implements Transport {
214217
if (this.lastSessionId) {
215218
urlParams[LAST_SESSION_PARAM] = this.lastSessionId;
216219
}
220+
if (this.applicationId) {
221+
urlParams[APPLICATION_ID_PARAM] = this.applicationId;
222+
}
217223
if (
218224
typeof location !== 'undefined' &&
219225
location.href &&

packages/database/src/realtime/Connection.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,17 +84,19 @@ export class Connection {
8484
private tx_: Transport;
8585

8686
/**
87-
* @param {!string} id - an id for this connection
88-
* @param {!RepoInfo} repoInfo_ - the info for the endpoint to connect to
89-
* @param {function(Object)} onMessage_ - the callback to be triggered when a server-push message arrives
90-
* @param {function(number, string)} onReady_ - the callback to be triggered when this connection is ready to send messages.
91-
* @param {function()} onDisconnect_ - the callback to be triggered when a connection was lost
92-
* @param {function(string)} onKill_ - the callback to be triggered when this connection has permanently shut down.
93-
* @param {string=} lastSessionId - last session id in persistent connection. is used to clean up old session in real-time server
87+
* @param id - an id for this connection
88+
* @param repoInfo_ - the info for the endpoint to connect to
89+
* @param applicationId_ - the Firebase App ID for this project
90+
* @param onMessage_ - the callback to be triggered when a server-push message arrives
91+
* @param onReady_ - the callback to be triggered when this connection is ready to send messages.
92+
* @param onDisconnect_ - the callback to be triggered when a connection was lost
93+
* @param onKill_ - the callback to be triggered when this connection has permanently shut down.
94+
* @param lastSessionId - last session id in persistent connection. is used to clean up old session in real-time server
9495
*/
9596
constructor(
9697
public id: string,
9798
private repoInfo_: RepoInfo,
99+
private applicationId_: string | undefined,
98100
private onMessage_: (a: {}) => void,
99101
private onReady_: (a: number, b: string) => void,
100102
private onDisconnect_: () => void,
@@ -116,6 +118,7 @@ export class Connection {
116118
this.conn_ = new conn(
117119
this.nextTransportId_(),
118120
this.repoInfo_,
121+
this.applicationId_,
119122
undefined,
120123
this.lastSessionId
121124
);
@@ -409,6 +412,7 @@ export class Connection {
409412
this.secondaryConn_ = new conn(
410413
this.nextTransportId_(),
411414
this.repoInfo_,
415+
this.applicationId_,
412416
this.sessionId
413417
);
414418
// For certain transports (WebSockets), we need to send and receive several messages back and forth before we

packages/database/src/realtime/Constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ export const FORGE_DOMAIN = 'firebaseio.com';
2929

3030
export const LAST_SESSION_PARAM = 'ls';
3131

32+
export const APPLICATION_ID_PARAM = 'p';
33+
3234
export const WEBSOCKET = 'websocket';
3335

3436
export const LONG_POLLING = 'long_polling';

packages/database/src/realtime/Transport.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export interface TransportConstructor {
2121
new (
2222
connId: string,
2323
repoInfo: RepoInfo,
24+
applicationId?: string,
2425
transportSessionId?: string,
2526
lastSessionId?: string
2627
): Transport;
@@ -86,12 +87,3 @@ export abstract class Transport {
8687

8788
abstract markConnectionHealthy(): void;
8889
}
89-
90-
export interface TransportConstructor {
91-
new (
92-
connId: string,
93-
RepoInfo,
94-
transportSessionId?: string,
95-
lastSessionId?: string
96-
);
97-
}

packages/database/src/realtime/TransportManager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
import { BrowserPollConnection } from './BrowserPollConnection';
1919
import { WebSocketConnection } from './WebSocketConnection';
20-
import { warn, each } from '../core/util/util';
20+
import { warn } from '../core/util/util';
2121
import { TransportConstructor } from './Transport';
2222
import { RepoInfo } from '../core/RepoInfo';
2323

packages/database/src/realtime/WebSocketConnection.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,17 @@ export class WebSocketConnection implements Transport {
7878
private isClosed_: boolean;
7979

8080
/**
81-
* @param {string} connId identifier for this transport
82-
* @param {RepoInfo} repoInfo The info for the websocket endpoint.
83-
* @param {string=} transportSessionId Optional transportSessionId if this is connecting to an existing transport
81+
* @param connId identifier for this transport
82+
* @param repoInfo The info for the websocket endpoint.
83+
* @param applicationId The Firebase App ID for this project.
84+
* @param transportSessionId Optional transportSessionId if this is connecting to an existing transport
8485
* session
85-
* @param {string=} lastSessionId Optional lastSessionId if there was a previous connection
86+
* @param lastSessionId Optional lastSessionId if there was a previous connection
8687
*/
8788
constructor(
8889
public connId: string,
8990
repoInfo: RepoInfo,
91+
private applicationId?: string,
9092
transportSessionId?: string,
9193
lastSessionId?: string
9294
) {
@@ -153,7 +155,8 @@ export class WebSocketConnection implements Transport {
153155
// UA Format: Firebase/<wire_protocol>/<sdk_version>/<platform>/<device>
154156
const options: { [k: string]: object } = {
155157
headers: {
156-
'User-Agent': `Firebase/${PROTOCOL_VERSION}/${SDK_VERSION}/${process.platform}/${device}`
158+
'User-Agent': `Firebase/${PROTOCOL_VERSION}/${SDK_VERSION}/${process.platform}/${device}`,
159+
'X-Firebase-GMPID': this.applicationId || ''
157160
}
158161
};
159162

@@ -170,7 +173,12 @@ export class WebSocketConnection implements Transport {
170173

171174
this.mySock = new WebSocketImpl(this.connURL, [], options);
172175
} else {
173-
this.mySock = new WebSocketImpl(this.connURL);
176+
const options: { [k: string]: object } = {
177+
headers: {
178+
'X-Firebase-GMPID': this.applicationId || ''
179+
}
180+
};
181+
this.mySock = new WebSocketImpl(this.connURL, [], options);
174182
}
175183
} catch (e) {
176184
this.log_('Error instantiating WebSocket.');

packages/database/test/connection.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ describe('Connection', () => {
2424
new Connection(
2525
'1',
2626
repoInfoForConnectionTest(),
27+
'fake-app-id',
2728
message => {},
2829
(timestamp, sessionId) => {
2930
expect(sessionId).not.to.be.null;
@@ -43,11 +44,13 @@ describe('Connection', () => {
4344
new Connection(
4445
'1',
4546
info,
47+
'fake-app-id',
4648
message => {},
4749
(timestamp, sessionId) => {
4850
new Connection(
4951
'2',
5052
info,
53+
'fake-app-id',
5154
message => {},
5255
(timestamp, sessionId) => {},
5356
() => {},

0 commit comments

Comments
 (0)