mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
22 lines
676 B
TypeScript
22 lines
676 B
TypeScript
import { addHeader } from '../../util/HeaderUtil';
|
|
import { HttpHandler } from '../HttpHandler';
|
|
import type { HttpResponse } from '../HttpResponse';
|
|
|
|
/**
|
|
* Handler that advertises a WebSocket through the Updates-Via header.
|
|
*/
|
|
export class WebSocketAdvertiser extends HttpHandler {
|
|
private readonly socketUrl: string;
|
|
|
|
public constructor(baseUrl: string) {
|
|
super();
|
|
const socketUrl = new URL(baseUrl);
|
|
socketUrl.protocol = /^(?:http|ws):/u.test(baseUrl) ? 'ws:' : 'wss:';
|
|
this.socketUrl = socketUrl.href;
|
|
}
|
|
|
|
public async handle({ response }: { response: HttpResponse }): Promise<void> {
|
|
addHeader(response, 'Updates-Via', this.socketUrl);
|
|
}
|
|
}
|