Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support customizing the transport protocol used for Via headers #1026

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/platform/web/transport/transport-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
12 changes: 10 additions & 2 deletions src/platform/web/transport/transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ export class Transport implements TransportDefinition {
connectionTimeout: 5,
keepAliveInterval: 0,
keepAliveDebounce: 10,
traceSip: true
traceSip: true,
headerProtocol: ""
};

public onConnect: (() => void) | undefined;
Expand Down Expand Up @@ -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 (typeof this.configuration.headerProtocol === "string" && this.configuration.headerProtocol !== "") {
this._protocol = this.configuration.headerProtocol.toUpperCase();
} else {
this._protocol = parsed.scheme.toUpperCase();
}
}

public dispose(): Promise<void> {
Expand Down
15 changes: 15 additions & 0 deletions test/spec/platform/web/transport.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down