Skip to content

Commit 36724da

Browse files
refactor: cleanup URI creation
Following 7195c0f
1 parent e05367f commit 36724da

File tree

4 files changed

+10
-50
lines changed

4 files changed

+10
-50
lines changed

lib/transport.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ export abstract class Transport extends Emitter<
172172
*/
173173
public pause(onPause: () => void) {}
174174

175-
protected uri(schema: string, query: Record<string, unknown> = {}) {
175+
protected createUri(schema: string, query: Record<string, unknown> = {}) {
176176
return (
177177
schema +
178178
"://" +
@@ -191,8 +191,8 @@ export abstract class Transport extends Emitter<
191191
private _port() {
192192
if (
193193
this.opts.port &&
194-
((this.opts.secure && this.opts.port !== "443") ||
195-
(!this.opts.secure && this.opts.port !== "80"))
194+
((this.opts.secure && Number(this.opts.port !== 443)) ||
195+
(!this.opts.secure && Number(this.opts.port) !== 80))
196196
) {
197197
return ":" + this.opts.port;
198198
} else {

lib/transports/polling.ts

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -215,10 +215,9 @@ export class Polling extends Transport {
215215
*
216216
* @private
217217
*/
218-
uri() {
219-
let query: { b64?: number; sid?: string } = this.query || {};
218+
private uri() {
220219
const schema = this.opts.secure ? "https" : "http";
221-
let port = "";
220+
const query: { b64?: number; sid?: string } = this.query || {};
222221

223222
// cache busting is forced
224223
if (false !== this.opts.timestampRequests) {
@@ -229,26 +228,7 @@ export class Polling extends Transport {
229228
query.b64 = 1;
230229
}
231230

232-
// avoid port if default for schema
233-
if (
234-
this.opts.port &&
235-
(("https" === schema && Number(this.opts.port) !== 443) ||
236-
("http" === schema && Number(this.opts.port) !== 80))
237-
) {
238-
port = ":" + this.opts.port;
239-
}
240-
241-
const encodedQuery = encode(query);
242-
const ipv6 = this.opts.hostname.indexOf(":") !== -1;
243-
244-
return (
245-
schema +
246-
"://" +
247-
(ipv6 ? "[" + this.opts.hostname + "]" : this.opts.hostname) +
248-
port +
249-
this.opts.path +
250-
(encodedQuery.length ? "?" + encodedQuery : "")
251-
);
231+
return this.createUri(schema, query);
252232
}
253233

254234
/**

lib/transports/websocket.ts

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -175,19 +175,9 @@ export class WS extends Transport {
175175
*
176176
* @private
177177
*/
178-
uri() {
179-
let query: { b64?: number } = this.query || {};
178+
private uri() {
180179
const schema = this.opts.secure ? "wss" : "ws";
181-
let port = "";
182-
183-
// avoid port if default for schema
184-
if (
185-
this.opts.port &&
186-
(("wss" === schema && Number(this.opts.port) !== 443) ||
187-
("ws" === schema && Number(this.opts.port) !== 80))
188-
) {
189-
port = ":" + this.opts.port;
190-
}
180+
const query: { b64?: number } = this.query || {};
191181

192182
// append timestamp to URI
193183
if (this.opts.timestampRequests) {
@@ -199,17 +189,7 @@ export class WS extends Transport {
199189
query.b64 = 1;
200190
}
201191

202-
const encodedQuery = encode(query);
203-
const ipv6 = this.opts.hostname.indexOf(":") !== -1;
204-
205-
return (
206-
schema +
207-
"://" +
208-
(ipv6 ? "[" + this.opts.hostname + "]" : this.opts.hostname) +
209-
port +
210-
this.opts.path +
211-
(encodedQuery.length ? "?" + encodedQuery : "")
212-
);
192+
return this.createUri(schema, query);
213193
}
214194

215195
/**

lib/transports/webtransport.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export class WT extends Transport {
3535
}
3636
// @ts-ignore
3737
this.transport = new WebTransport(
38-
this.uri("https"),
38+
this.createUri("https"),
3939
this.opts.transportOptions[this.name]
4040
);
4141

0 commit comments

Comments
 (0)