Skip to content

Commit 9cbcb35

Browse files
committed
fix: spell out the delegated port and serve well-known as json
1 parent c3293c7 commit 9cbcb35

2 files changed

Lines changed: 19 additions & 4 deletions

File tree

‎src/index.ts‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@ export default {
2929
const url = new URL(request.url);
3030
// The address it is reached on, unless a deployment pins another one:
3131
// a placeholder default ships on every deploy and delegates the media
32-
// away from this worker.
32+
// away from this worker. The port is always spelled out: a port-less
33+
// delegation sends the requesting server to an SRV lookup and then to
34+
// 8448, which no worker answers on.
3335
const serverName = env.SERVERNAME || url.hostname;
34-
const delegate = env.HOSTNAME ? `${env.HOSTNAME}:${env.PORT || 443}` : url.host;
36+
const delegate = `${env.HOSTNAME || url.hostname}:${env.PORT || url.port || 443}`;
3537

3638
if (url.pathname === '/_matrix/federation/v1/version') {
3739
return new Response(JSON.stringify(returnMatrixServerVers()), {
@@ -60,7 +62,9 @@ export default {
6062
mxcId: matrixId
6163
} satisfies SoliditasAddressConvertResponse))
6264
} else if (url.pathname === '/.well-known/matrix/server') {
63-
return new Response(JSON.stringify({ 'm.server': delegate } satisfies MatrixWellKnownServer));
65+
return new Response(JSON.stringify({ 'm.server': delegate } satisfies MatrixWellKnownServer), {
66+
headers: { 'Content-Type': 'application/json' },
67+
});
6468
} else {
6569
return new Response(JSON.stringify(matrixEndpointNotImplemented('only implements media endpoints')), {
6670
headers: { 'Content-Type': 'application/json' },

‎test/index.spec.ts‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,18 @@ describe('well-known delegation', () => {
1212
{}
1313
);
1414

15-
expect(await response.json()).toEqual({ 'm.server': 'gifs.example' });
15+
expect(await response.json()).toEqual({ 'm.server': 'gifs.example:443' });
16+
expect(response.headers.get('Content-Type')).toBe('application/json');
17+
});
18+
19+
it('keeps the port it was reached on', async () => {
20+
const response = await worker.fetch(
21+
{ url: 'https://gifs.example:8443/.well-known/matrix/server' },
22+
env,
23+
{}
24+
);
25+
26+
expect(await response.json()).toEqual({ 'm.server': 'gifs.example:8443' });
1627
});
1728

1829
it('delegates to a pinned hostname and port when one is configured', async () => {

0 commit comments

Comments
 (0)