Skip to content

Commit 9413433

Browse files
authored
fix: use URLSearchParams when building WS url (#1498)
## CLA - [ ] I have signed the [Stream CLA](https://docs.google.com/forms/d/e/1FAIpQLScFKsKkAJI7mhCr7K9rEIOpqIDThrWxuvxnwUq2XkHyG154vQ/viewform) (required). - [ ] Code changes are tested ## Description of the changes, What, Why and How? ## Changelog -
1 parent d14d0ff commit 9413433

4 files changed

Lines changed: 20 additions & 11 deletions

File tree

src/client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ export class StreamChat<StreamChatGenerics extends ExtendableGenerics = DefaultG
330330
warmUp: false,
331331
recoverStateOnReconnect: true,
332332
disableCache: false,
333-
wsUrlSuffix: '',
333+
wsUrlParams: new URLSearchParams({}),
334334
...inputOptions,
335335
};
336336

src/connection.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -184,14 +184,20 @@ export class StableWSConnection<StreamChatGenerics extends ExtendableGenerics =
184184
* @returns url string
185185
*/
186186
_buildUrl = () => {
187-
const qs = encodeURIComponent(this.client._buildWSPayload(this.requestID));
187+
const qs = this.client._buildWSPayload(this.requestID);
188188
const token = this.client.tokenManager.getToken();
189-
190-
return `${this.client.wsBaseURL}/connect?json=${qs}&api_key=${
191-
this.client.key
192-
}&authorization=${token}&stream-auth-type=${this.client.getAuthType()}&X-Stream-Client=${encodeURIComponent(
193-
this.client.getUserAgent(),
194-
)}${this.client.options.wsUrlSuffix}`;
189+
const wsUrlParams = this.client.options.wsUrlParams;
190+
191+
const params = new URLSearchParams(wsUrlParams);
192+
params.set('json', qs);
193+
params.set('api_key', this.client.key);
194+
// it is expected that the autorization parameter exists even if
195+
// the token is undefined, so we interpolate it to be safe
196+
params.set('authorization', `${token}`);
197+
params.set('stream-auth-type', this.client.getAuthType());
198+
params.set('X-Stream-Client', this.client.getUserAgent());
199+
200+
return `${this.client.wsBaseURL}/connect?${params.toString()}`;
195201
};
196202

197203
/**

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1245,7 +1245,7 @@ export type StreamChatOptions = AxiosRequestConfig & {
12451245
* Sets a suffix to the wsUrl when it is being built in `wsConnection`. Is meant to be
12461246
* used purely in testing suites and should not be used in production apps.
12471247
*/
1248-
wsUrlSuffix?: string;
1248+
wsUrlParams?: URLSearchParams;
12491249
};
12501250

12511251
export type SyncOptions = {

test/unit/connection.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,10 @@ describe('connection', function () {
8282
client.userAgent = userAgent;
8383
const url = ws._buildUrl();
8484

85-
expect(url).to.contain(encodeURIComponent(userAgent));
85+
const searchParams = new URLSearchParams({
86+
'X-Stream-Client': userAgent,
87+
});
88+
expect(url).to.contain(searchParams.toString());
8689
});
8790

8891
it('should not include device if not there', function () {
@@ -94,7 +97,7 @@ describe('connection', function () {
9497

9598
it('should include extra params when building url if provided', function () {
9699
const { query: prevQuery } = url.parse(ws._buildUrl(), true);
97-
ws.client.options.wsUrlSuffix = '&foo=1&bar=2';
100+
ws.client.options.wsUrlParams = new URLSearchParams({ foo: '1', bar: '2' });
98101
const { query } = url.parse(ws._buildUrl(), true);
99102

100103
// all of the previous query params should remain intact

0 commit comments

Comments
 (0)