Skip to content

Add GPMID header to Firebase Database #3190

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 6 commits into from
Jun 10, 2020
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
1 change: 1 addition & 0 deletions packages/database/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Unreleased
- [changed] Added internal HTTP header to the WebChannel connection.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think database uses WebChannel? or are you using it as a general term for realtime connection?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WebSockets. Will fix.

- [feature] Added ServerValue.increment() to support atomic field value increments
without transactions.
- [fixed] Fixed Realtime Database URL parsing bug to support domains with more than 3 components.
Expand Down
3 changes: 3 additions & 0 deletions packages/database/src/core/PersistentConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,12 @@ export class PersistentConnection extends ServerActions {
/**
* @implements {ServerActions}
* @param repoInfo_ Data about the namespace we are connecting to
* @param applicationId_ The Firebase App ID for this project
* @param onDataUpdate_ A callback for new data from the server
*/
constructor(
private repoInfo_: RepoInfo,
private applicationId_: string,
private onDataUpdate_: (
a: string,
b: unknown,
Expand Down Expand Up @@ -782,6 +784,7 @@ export class PersistentConnection extends ServerActions {
connection = new Connection(
connId,
self.repoInfo_,
self.applicationId_,
onDataMessage,
onReady,
onDisconnect,
Expand Down
1 change: 1 addition & 0 deletions packages/database/src/core/Repo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ export class Repo {

this.persistentConnection_ = new PersistentConnection(
this.repoInfo_,
app.options.appId,
this.onDataUpdate_.bind(this),
this.onConnectStatus_.bind(this),
this.onServerInfoUpdate_.bind(this),
Expand Down
14 changes: 10 additions & 4 deletions packages/database/src/realtime/BrowserPollConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
import { StatsManager } from '../core/stats/StatsManager';
import { PacketReceiver } from './polling/PacketReceiver';
import {
APPLICATION_ID_PARAM,
FORGE_DOMAIN,
FORGE_REF,
LAST_SESSION_PARAM,
Expand Down Expand Up @@ -104,16 +105,18 @@ export class BrowserPollConnection implements Transport {
private onDisconnect_: ((a?: boolean) => void) | null;

/**
* @param {string} connId An identifier for this connection, used for logging
* @param {RepoInfo} repoInfo The info for the endpoint to send data to.
* @param {string=} transportSessionId Optional transportSessionid if we are reconnecting for an existing
* @param connId An identifier for this connection, used for logging
* @param repoInfo The info for the endpoint to send data to.
* @param applicationId The Firebase App ID for this project.
* @param transportSessionId Optional transportSessionid if we are reconnecting for an existing
* transport session
* @param {string=} lastSessionId Optional lastSessionId if the PersistentConnection has already created a
* @param lastSessionId Optional lastSessionId if the PersistentConnection has already created a
* connection previously
*/
constructor(
public connId: string,
public repoInfo: RepoInfo,
private applicationId?: string,
public transportSessionId?: string,
public lastSessionId?: string
) {
Expand Down Expand Up @@ -214,6 +217,9 @@ export class BrowserPollConnection implements Transport {
if (this.lastSessionId) {
urlParams[LAST_SESSION_PARAM] = this.lastSessionId;
}
if (this.applicationId) {
urlParams[APPLICATION_ID_PARAM] = this.applicationId;
}
if (
typeof location !== 'undefined' &&
location.href &&
Expand Down
18 changes: 11 additions & 7 deletions packages/database/src/realtime/Connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,19 @@ export class Connection {
private tx_: Transport;

/**
* @param {!string} id - an id for this connection
* @param {!RepoInfo} repoInfo_ - the info for the endpoint to connect to
* @param {function(Object)} onMessage_ - the callback to be triggered when a server-push message arrives
* @param {function(number, string)} onReady_ - the callback to be triggered when this connection is ready to send messages.
* @param {function()} onDisconnect_ - the callback to be triggered when a connection was lost
* @param {function(string)} onKill_ - the callback to be triggered when this connection has permanently shut down.
* @param {string=} lastSessionId - last session id in persistent connection. is used to clean up old session in real-time server
* @param id - an id for this connection
* @param repoInfo_ - the info for the endpoint to connect to
* @param applicationId_ - the Firebase App ID for this project
* @param onMessage_ - the callback to be triggered when a server-push message arrives
* @param onReady_ - the callback to be triggered when this connection is ready to send messages.
* @param onDisconnect_ - the callback to be triggered when a connection was lost
* @param onKill_ - the callback to be triggered when this connection has permanently shut down.
* @param lastSessionId - last session id in persistent connection. is used to clean up old session in real-time server
*/
constructor(
public id: string,
private repoInfo_: RepoInfo,
private applicationId_: string | undefined,
private onMessage_: (a: {}) => void,
private onReady_: (a: number, b: string) => void,
private onDisconnect_: () => void,
Expand All @@ -116,6 +118,7 @@ export class Connection {
this.conn_ = new conn(
this.nextTransportId_(),
this.repoInfo_,
this.applicationId_,
undefined,
this.lastSessionId
);
Expand Down Expand Up @@ -409,6 +412,7 @@ export class Connection {
this.secondaryConn_ = new conn(
this.nextTransportId_(),
this.repoInfo_,
this.applicationId_,
this.sessionId
);
// For certain transports (WebSockets), we need to send and receive several messages back and forth before we
Expand Down
2 changes: 2 additions & 0 deletions packages/database/src/realtime/Constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ export const FORGE_DOMAIN = 'firebaseio.com';

export const LAST_SESSION_PARAM = 'ls';

export const APPLICATION_ID_PARAM = 'p';

export const WEBSOCKET = 'websocket';

export const LONG_POLLING = 'long_polling';
10 changes: 1 addition & 9 deletions packages/database/src/realtime/Transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export interface TransportConstructor {
new (
connId: string,
repoInfo: RepoInfo,
applicationId?: string,
transportSessionId?: string,
lastSessionId?: string
): Transport;
Expand Down Expand Up @@ -86,12 +87,3 @@ export abstract class Transport {

abstract markConnectionHealthy(): void;
}

export interface TransportConstructor {
new (
connId: string,
RepoInfo,
transportSessionId?: string,
lastSessionId?: string
);
}
2 changes: 1 addition & 1 deletion packages/database/src/realtime/TransportManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import { BrowserPollConnection } from './BrowserPollConnection';
import { WebSocketConnection } from './WebSocketConnection';
import { warn, each } from '../core/util/util';
import { warn } from '../core/util/util';
import { TransportConstructor } from './Transport';
import { RepoInfo } from '../core/RepoInfo';

Expand Down
20 changes: 14 additions & 6 deletions packages/database/src/realtime/WebSocketConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,17 @@ export class WebSocketConnection implements Transport {
private isClosed_: boolean;

/**
* @param {string} connId identifier for this transport
* @param {RepoInfo} repoInfo The info for the websocket endpoint.
* @param {string=} transportSessionId Optional transportSessionId if this is connecting to an existing transport
* @param connId identifier for this transport
* @param repoInfo The info for the websocket endpoint.
* @param applicationId The Firebase App ID for this project.
* @param transportSessionId Optional transportSessionId if this is connecting to an existing transport
* session
* @param {string=} lastSessionId Optional lastSessionId if there was a previous connection
* @param lastSessionId Optional lastSessionId if there was a previous connection
*/
constructor(
public connId: string,
repoInfo: RepoInfo,
private applicationId?: string,
transportSessionId?: string,
lastSessionId?: string
) {
Expand Down Expand Up @@ -153,7 +155,8 @@ export class WebSocketConnection implements Transport {
// UA Format: Firebase/<wire_protocol>/<sdk_version>/<platform>/<device>
const options: { [k: string]: object } = {
headers: {
'User-Agent': `Firebase/${PROTOCOL_VERSION}/${SDK_VERSION}/${process.platform}/${device}`
'User-Agent': `Firebase/${PROTOCOL_VERSION}/${SDK_VERSION}/${process.platform}/${device}`,
'X-Firebase-GMPID': this.applicationId || ''
}
};

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

this.mySock = new WebSocketImpl(this.connURL, [], options);
} else {
this.mySock = new WebSocketImpl(this.connURL);
const options: { [k: string]: object } = {
headers: {
'X-Firebase-GMPID': this.applicationId || ''
}
};
this.mySock = new WebSocketImpl(this.connURL, [], options);
}
} catch (e) {
this.log_('Error instantiating WebSocket.');
Expand Down
3 changes: 3 additions & 0 deletions packages/database/test/connection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ describe('Connection', () => {
new Connection(
'1',
repoInfoForConnectionTest(),
'fake-app-id',
message => {},
(timestamp, sessionId) => {
expect(sessionId).not.to.be.null;
Expand All @@ -43,11 +44,13 @@ describe('Connection', () => {
new Connection(
'1',
info,
'fake-app-id',
message => {},
(timestamp, sessionId) => {
new Connection(
'2',
info,
'fake-app-id',
message => {},
(timestamp, sessionId) => {},
() => {},
Expand Down