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:
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);
|
||||||
|
}
|
||||||
|
}
|
37
test/unit/ldp/http/metadata/WebSocketMetadataWriter.test.ts
Normal file
37
test/unit/ldp/http/metadata/WebSocketMetadataWriter.test.ts
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
import { createResponse } from 'node-mocks-http';
|
||||||
|
import { WebSocketMetadataWriter } from '../../../../../src/ldp/http/metadata/WebSocketMetadataWriter';
|
||||||
|
|
||||||
|
describe('A WebSocketMetadataWriter', (): void => {
|
||||||
|
it('writes a default HTTP WebSocket.', async(): Promise<void> => {
|
||||||
|
const writer = new WebSocketMetadataWriter({});
|
||||||
|
const response = createResponse();
|
||||||
|
await writer.handle({ response } as any);
|
||||||
|
expect(response.getHeaders()).toEqual({ 'updates-via': 'ws://localhost' });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('writes an HTTP WebSocket with port 80.', async(): Promise<void> => {
|
||||||
|
const writer = new WebSocketMetadataWriter({ hostname: 'test.example', port: 80, protocol: 'http' });
|
||||||
|
const response = createResponse();
|
||||||
|
await writer.handle({ response } as any);
|
||||||
|
expect(response.getHeaders()).toEqual({ 'updates-via': 'ws://test.example' });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('writes an HTTP WebSocket with port 3000.', async(): Promise<void> => {
|
||||||
|
const writer = new WebSocketMetadataWriter({ hostname: 'test.example', port: 3000, protocol: 'http' });
|
||||||
|
const response = createResponse();
|
||||||
|
await writer.handle({ response } as any);
|
||||||
|
expect(response.getHeaders()).toEqual({ 'updates-via': 'ws://test.example:3000' });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('writes an HTTPS WebSocket with port 443.', async(): Promise<void> => {
|
||||||
|
const writer = new WebSocketMetadataWriter({ hostname: 'test.example', port: 443, protocol: 'https' });
|
||||||
|
const response = createResponse();
|
||||||
|
await writer.handle({ response } as any);
|
||||||
|
expect(response.getHeaders()).toEqual({ 'updates-via': 'wss://test.example' });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('rejects an invalid hostname.', (): void => {
|
||||||
|
expect((): any => new WebSocketMetadataWriter({ hostname: 'test.example/invalid' }))
|
||||||
|
.toThrow('Invalid hostname: test.example/invalid');
|
||||||
|
});
|
||||||
|
});
|
Loading…
x
Reference in New Issue
Block a user