diff --git a/lib/transport.ts b/lib/transport.ts index 21200757..14ab9297 100644 --- a/lib/transport.ts +++ b/lib/transport.ts @@ -172,7 +172,7 @@ export abstract class Transport extends Emitter< */ public pause(onPause: () => void) {} - protected uri(schema: string, query: Record = {}) { + protected createUri(schema: string, query: Record = {}) { return ( schema + "://" + @@ -191,8 +191,8 @@ export abstract class Transport extends Emitter< private _port() { if ( this.opts.port && - ((this.opts.secure && this.opts.port !== "443") || - (!this.opts.secure && this.opts.port !== "80")) + ((this.opts.secure && Number(this.opts.port !== 443)) || + (!this.opts.secure && Number(this.opts.port) !== 80)) ) { return ":" + this.opts.port; } else { diff --git a/lib/transports/polling.ts b/lib/transports/polling.ts index 444178bc..525499d2 100644 --- a/lib/transports/polling.ts +++ b/lib/transports/polling.ts @@ -215,10 +215,9 @@ export class Polling extends Transport { * * @private */ - uri() { - let query: { b64?: number; sid?: string } = this.query || {}; + private uri() { const schema = this.opts.secure ? "https" : "http"; - let port = ""; + const query: { b64?: number; sid?: string } = this.query || {}; // cache busting is forced if (false !== this.opts.timestampRequests) { @@ -229,26 +228,7 @@ export class Polling extends Transport { query.b64 = 1; } - // avoid port if default for schema - if ( - this.opts.port && - (("https" === schema && Number(this.opts.port) !== 443) || - ("http" === schema && Number(this.opts.port) !== 80)) - ) { - port = ":" + this.opts.port; - } - - const encodedQuery = encode(query); - const ipv6 = this.opts.hostname.indexOf(":") !== -1; - - return ( - schema + - "://" + - (ipv6 ? "[" + this.opts.hostname + "]" : this.opts.hostname) + - port + - this.opts.path + - (encodedQuery.length ? "?" + encodedQuery : "") - ); + return this.createUri(schema, query); } /** diff --git a/lib/transports/websocket.ts b/lib/transports/websocket.ts index 3ee8d538..33d78724 100644 --- a/lib/transports/websocket.ts +++ b/lib/transports/websocket.ts @@ -175,19 +175,9 @@ export class WS extends Transport { * * @private */ - uri() { - let query: { b64?: number } = this.query || {}; + private uri() { const schema = this.opts.secure ? "wss" : "ws"; - let port = ""; - - // avoid port if default for schema - if ( - this.opts.port && - (("wss" === schema && Number(this.opts.port) !== 443) || - ("ws" === schema && Number(this.opts.port) !== 80)) - ) { - port = ":" + this.opts.port; - } + const query: { b64?: number } = this.query || {}; // append timestamp to URI if (this.opts.timestampRequests) { @@ -199,17 +189,7 @@ export class WS extends Transport { query.b64 = 1; } - const encodedQuery = encode(query); - const ipv6 = this.opts.hostname.indexOf(":") !== -1; - - return ( - schema + - "://" + - (ipv6 ? "[" + this.opts.hostname + "]" : this.opts.hostname) + - port + - this.opts.path + - (encodedQuery.length ? "?" + encodedQuery : "") - ); + return this.createUri(schema, query); } /** diff --git a/lib/transports/webtransport.ts b/lib/transports/webtransport.ts index d46241aa..ccf44a28 100644 --- a/lib/transports/webtransport.ts +++ b/lib/transports/webtransport.ts @@ -35,7 +35,7 @@ export class WT extends Transport { } // @ts-ignore this.transport = new WebTransport( - this.uri("https"), + this.createUri("https"), this.opts.transportOptions[this.name] );