mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
feat: Implement --baseUrl flag.
Closes https://github.com/solid/community-server/issues/372
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user