CommunitySolidServer/src/server/middleware/WebSocketAdvertiser.ts
2021-01-26 10:52:38 +01:00

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);
}
}