feat: Implement --baseUrl flag.

Closes https://github.com/solid/community-server/issues/372
This commit is contained in:
Ruben Verborgh
2020-12-01 15:52:44 +01:00
parent 528688bc4c
commit eabe6bc4ed
13 changed files with 60 additions and 66 deletions

View File

@@ -2,26 +2,16 @@ import { addHeader } from '../../util/HeaderUtil';
import { HttpHandler } from '../HttpHandler';
import type { HttpResponse } from '../HttpResponse';
interface WebSocketSettings {
hostname?: string;
port?: number;
protocol?: string;
}
/**
* Handler that advertises a WebSocket through the Updates-Via header.
*/
export class WebSocketAdvertiser extends HttpHandler {
private readonly socketUrl: string;
public constructor(settings: WebSocketSettings = {}) {
public constructor(baseUrl: string) {
super();
const { hostname = 'localhost', port = 80, protocol = 'ws:' } = settings;
const secure = /^(?:https|wss)/u.test(protocol);
const socketUrl = new URL(`${secure ? 'wss' : 'ws'}://${hostname}:${port}/`);
if (socketUrl.hostname !== hostname) {
throw new Error(`Invalid hostname: ${hostname}`);
}
const socketUrl = new URL(baseUrl);
socketUrl.protocol = /^(?:http|ws):/u.test(baseUrl) ? 'ws:' : 'wss:';
this.socketUrl = socketUrl.href;
}