Skip to content

Commit

Permalink
Added GMPID to websocket connection (#6232)
Browse files Browse the repository at this point in the history
* Added applicationid to websocket connection
  • Loading branch information
maneesht committed May 6, 2022
1 parent 1ac3c9d commit 874cdbb
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/beige-turkeys-occur.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@firebase/database": patch
---

Added GMPID to websocket connection.
22 changes: 10 additions & 12 deletions packages/database/src/realtime/WebSocketConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { logWrapper, splitStringBySize } from '../core/util/util';
import { SDK_VERSION } from '../core/version';

import {
APPLICATION_ID_PARAM,
APP_CHECK_TOKEN_PARAM,
FORGE_DOMAIN_RE,
FORGE_REF,
Expand Down Expand Up @@ -99,7 +100,8 @@ export class WebSocketConnection implements Transport {
repoInfo,
transportSessionId,
lastSessionId,
appCheckToken
appCheckToken,
applicationId
);
this.nodeAdmin = repoInfo.nodeAdmin;
}
Expand All @@ -115,7 +117,8 @@ export class WebSocketConnection implements Transport {
repoInfo: RepoInfo,
transportSessionId?: string,
lastSessionId?: string,
appCheckToken?: string
appCheckToken?: string,
applicationId?: string
): string {
const urlParams: { [k: string]: string } = {};
urlParams[VERSION_PARAM] = PROTOCOL_VERSION;
Expand All @@ -137,6 +140,9 @@ export class WebSocketConnection implements Transport {
if (appCheckToken) {
urlParams[APP_CHECK_TOKEN_PARAM] = appCheckToken;
}
if (applicationId) {
urlParams[APPLICATION_ID_PARAM] = applicationId;
}

return repoInfoConnectionURL(repoInfo, WEBSOCKET, urlParams);
}
Expand All @@ -156,6 +162,7 @@ export class WebSocketConnection implements Transport {
PersistentStorage.set('previous_websocket_failure', true);

try {
let options: { [k: string]: object };
if (isNodeSdk()) {
const device = this.nodeAdmin ? 'AdminNode' : 'Node';
// UA Format: Firebase/<wire_protocol>/<sdk_version>/<platform>/<device>
Expand Down Expand Up @@ -188,17 +195,8 @@ export class WebSocketConnection implements Transport {
if (proxy) {
options['proxy'] = { origin: proxy };
}

this.mySock = new WebSocketImpl(this.connURL, [], options);
} else {
const options: { [k: string]: object } = {
headers: {
'X-Firebase-GMPID': this.applicationId || '',
'X-Firebase-AppCheck': this.appCheckToken || ''
}
};
this.mySock = new WebSocketImpl(this.connURL, [], options);
}
this.mySock = new WebSocketImpl(this.connURL, [], options);
} catch (e) {
this.log_('Error instantiating WebSocket.');
const error = e.message || e.data;
Expand Down
48 changes: 48 additions & 0 deletions packages/database/test/websocketconnection.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* @license
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { expect } from 'chai';

import { APPLICATION_ID_PARAM } from '../src/realtime/Constants';
import { WebSocketConnection } from '../src/realtime/WebSocketConnection';

import { testRepoInfo } from './helpers/util';

describe('WebSocketConnection', () => {
it('should add an applicationId to the query parameter', () => {
const repoInfo = testRepoInfo('https://test-ns.firebaseio.com');
const applicationId = 'myID';
const websocketConnection = new WebSocketConnection(
'connId',
repoInfo,
applicationId
);
const searchParams = new URL(websocketConnection.connURL).searchParams;
expect(searchParams.get(APPLICATION_ID_PARAM)).to.equal(applicationId);
});
it('should not add an applicationId to the query parameter if applicationId is empty', () => {
const repoInfo = testRepoInfo('https://test-ns.firebaseio.com');
const applicationId = '';
const websocketConnection = new WebSocketConnection(
'connId',
repoInfo,
applicationId
);
const searchParams = new URL(websocketConnection.connURL).searchParams;
expect(searchParams.get(APPLICATION_ID_PARAM)).to.be.null;
});
});

0 comments on commit 874cdbb

Please sign in to comment.