mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
feat: Advertise WebSocket via Updates-Via header.
This commit is contained in:
committed by
Joachim Van Herwegen
parent
d8799368fd
commit
f08617b1c9
25
src/ldp/http/metadata/WebSocketMetadataWriter.ts
Normal file
25
src/ldp/http/metadata/WebSocketMetadataWriter.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import type { HttpResponse } from '../../../server/HttpResponse';
|
||||
import { addHeader } from '../../../util/HeaderUtil';
|
||||
import { MetadataWriter } from './MetadataWriter';
|
||||
|
||||
/**
|
||||
* A {@link MetadataWriter} that advertises a WebSocket through the Updates-Via header.
|
||||
*/
|
||||
export class WebSocketMetadataWriter extends MetadataWriter {
|
||||
private readonly socketUrl: string;
|
||||
|
||||
public constructor({ hostname = 'localhost', port = 80, protocol = 'ws:' }:
|
||||
{ hostname?: string; port?: number; protocol?: string }) {
|
||||
super();
|
||||
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}`);
|
||||
}
|
||||
this.socketUrl = socketUrl.href.slice(0, -1);
|
||||
}
|
||||
|
||||
public async handle({ response }: { response: HttpResponse }): Promise<void> {
|
||||
addHeader(response, 'updates-via', this.socketUrl);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user