Skip to content

Commit 874cdbb

Browse files
authored
Added GMPID to websocket connection (#6232)
* Added applicationid to websocket connection
1 parent 1ac3c9d commit 874cdbb

File tree

3 files changed

+63
-12
lines changed

3 files changed

+63
-12
lines changed

.changeset/beige-turkeys-occur.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@firebase/database": patch
3+
---
4+
5+
Added GMPID to websocket connection.

packages/database/src/realtime/WebSocketConnection.ts

+10-12
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import { logWrapper, splitStringBySize } from '../core/util/util';
2525
import { SDK_VERSION } from '../core/version';
2626

2727
import {
28+
APPLICATION_ID_PARAM,
2829
APP_CHECK_TOKEN_PARAM,
2930
FORGE_DOMAIN_RE,
3031
FORGE_REF,
@@ -99,7 +100,8 @@ export class WebSocketConnection implements Transport {
99100
repoInfo,
100101
transportSessionId,
101102
lastSessionId,
102-
appCheckToken
103+
appCheckToken,
104+
applicationId
103105
);
104106
this.nodeAdmin = repoInfo.nodeAdmin;
105107
}
@@ -115,7 +117,8 @@ export class WebSocketConnection implements Transport {
115117
repoInfo: RepoInfo,
116118
transportSessionId?: string,
117119
lastSessionId?: string,
118-
appCheckToken?: string
120+
appCheckToken?: string,
121+
applicationId?: string
119122
): string {
120123
const urlParams: { [k: string]: string } = {};
121124
urlParams[VERSION_PARAM] = PROTOCOL_VERSION;
@@ -137,6 +140,9 @@ export class WebSocketConnection implements Transport {
137140
if (appCheckToken) {
138141
urlParams[APP_CHECK_TOKEN_PARAM] = appCheckToken;
139142
}
143+
if (applicationId) {
144+
urlParams[APPLICATION_ID_PARAM] = applicationId;
145+
}
140146

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

158164
try {
165+
let options: { [k: string]: object };
159166
if (isNodeSdk()) {
160167
const device = this.nodeAdmin ? 'AdminNode' : 'Node';
161168
// UA Format: Firebase/<wire_protocol>/<sdk_version>/<platform>/<device>
@@ -188,17 +195,8 @@ export class WebSocketConnection implements Transport {
188195
if (proxy) {
189196
options['proxy'] = { origin: proxy };
190197
}
191-
192-
this.mySock = new WebSocketImpl(this.connURL, [], options);
193-
} else {
194-
const options: { [k: string]: object } = {
195-
headers: {
196-
'X-Firebase-GMPID': this.applicationId || '',
197-
'X-Firebase-AppCheck': this.appCheckToken || ''
198-
}
199-
};
200-
this.mySock = new WebSocketImpl(this.connURL, [], options);
201198
}
199+
this.mySock = new WebSocketImpl(this.connURL, [], options);
202200
} catch (e) {
203201
this.log_('Error instantiating WebSocket.');
204202
const error = e.message || e.data;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* @license
3+
* Copyright 2022 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
import { expect } from 'chai';
19+
20+
import { APPLICATION_ID_PARAM } from '../src/realtime/Constants';
21+
import { WebSocketConnection } from '../src/realtime/WebSocketConnection';
22+
23+
import { testRepoInfo } from './helpers/util';
24+
25+
describe('WebSocketConnection', () => {
26+
it('should add an applicationId to the query parameter', () => {
27+
const repoInfo = testRepoInfo('https://test-ns.firebaseio.com');
28+
const applicationId = 'myID';
29+
const websocketConnection = new WebSocketConnection(
30+
'connId',
31+
repoInfo,
32+
applicationId
33+
);
34+
const searchParams = new URL(websocketConnection.connURL).searchParams;
35+
expect(searchParams.get(APPLICATION_ID_PARAM)).to.equal(applicationId);
36+
});
37+
it('should not add an applicationId to the query parameter if applicationId is empty', () => {
38+
const repoInfo = testRepoInfo('https://test-ns.firebaseio.com');
39+
const applicationId = '';
40+
const websocketConnection = new WebSocketConnection(
41+
'connId',
42+
repoInfo,
43+
applicationId
44+
);
45+
const searchParams = new URL(websocketConnection.connURL).searchParams;
46+
expect(searchParams.get(APPLICATION_ID_PARAM)).to.be.null;
47+
});
48+
});

0 commit comments

Comments
 (0)