From d48d798038102060b0a7d9da78442abeccecf7ce Mon Sep 17 00:00:00 2001 From: Cate Stock Date: Mon, 6 Feb 2023 16:54:24 -0500 Subject: [PATCH 1/3] Support configurable transport protocol in header --- src/platform/web/transport/transport-options.ts | 6 ++++++ src/platform/web/transport/transport.ts | 12 ++++++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/platform/web/transport/transport-options.ts b/src/platform/web/transport/transport-options.ts index ba0d16cc2..bb3b4e336 100644 --- a/src/platform/web/transport/transport-options.ts +++ b/src/platform/web/transport/transport-options.ts @@ -31,4 +31,10 @@ export interface TransportOptions { * @defaultValue `true` */ traceSip?: boolean; + + /** + * The transport protocol to use in Via header declarations. + * If not specified, will be inferred from the server URL. + */ + headerProtocol?: string; } diff --git a/src/platform/web/transport/transport.ts b/src/platform/web/transport/transport.ts index 13943b753..5a8cb0c28 100644 --- a/src/platform/web/transport/transport.ts +++ b/src/platform/web/transport/transport.ts @@ -16,7 +16,8 @@ export class Transport implements TransportDefinition { connectionTimeout: 5, keepAliveInterval: 0, keepAliveDebounce: 10, - traceSip: true + traceSip: true, + headerProtocol: "" }; public onConnect: (() => void) | undefined; @@ -100,7 +101,14 @@ export class Transport implements TransportDefinition { this.logger.error(`Invalid scheme in WebSocket Server URL "${url}"`); throw new Error("Invalid scheme in WebSocket Server URL"); } - this._protocol = parsed.scheme.toUpperCase(); + + // Use the explicit header protocol if defined, but fall back to the + // server's indicated scheme + if (this.configuration.headerProtocol !== "") { + this._protocol = this.configuration.headerProtocol.toUpperCase(); + } else { + this._protocol = parsed.scheme.toUpperCase(); + } } public dispose(): Promise { From dd5c13d8504717de3ef783e53624bfd53a73b4ee Mon Sep 17 00:00:00 2001 From: Cate Stock Date: Tue, 7 Feb 2023 11:22:14 -0500 Subject: [PATCH 2/3] Add explicit type guard --- src/platform/web/transport/transport.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/platform/web/transport/transport.ts b/src/platform/web/transport/transport.ts index 5a8cb0c28..16799fb1e 100644 --- a/src/platform/web/transport/transport.ts +++ b/src/platform/web/transport/transport.ts @@ -104,7 +104,7 @@ export class Transport implements TransportDefinition { // Use the explicit header protocol if defined, but fall back to the // server's indicated scheme - if (this.configuration.headerProtocol !== "") { + if (typeof this.configuration.headerProtocol === "string" && this.configuration.headerProtocol !== "") { this._protocol = this.configuration.headerProtocol.toUpperCase(); } else { this._protocol = parsed.scheme.toUpperCase(); From e15f5783e3ceb181e2771e4836ad512036ebb6e6 Mon Sep 17 00:00:00 2001 From: Cate Stock Date: Tue, 7 Feb 2023 11:22:30 -0500 Subject: [PATCH 3/3] Add unit test for custom header protocols --- test/spec/platform/web/transport.spec.ts | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/test/spec/platform/web/transport.spec.ts b/test/spec/platform/web/transport.spec.ts index f18911ee6..7bb5148aa 100644 --- a/test/spec/platform/web/transport.spec.ts +++ b/test/spec/platform/web/transport.spec.ts @@ -64,6 +64,21 @@ describe("Web Transport", () => { // The transport should now be in the Disconnected state, so traverse the FSM. traverseTransportStateMachine(); + + describe("supports customized header protocols", () => { + // constructTransport normally validates that the protocol is WSS + // given that the server URL is wss://... + const headerProtocol = "ws"; + const customizedTransport = new Transport(logger, { + connectionTimeout, + server, + headerProtocol + }); + + it("protocol MUST be WS", () => { + expect(customizedTransport.protocol).toBe(headerProtocol.toUpperCase()); + }); + }); }); function initServer(): void {