Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/env.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export interface Env {
HOSTNAME: string;
SERVERNAME: string;
PORT: number;
HOSTNAME?: string;
SERVERNAME?: string;
PORT?: number;
}
8 changes: 6 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ export default {
SERVERNAME: any; HOSTNAME: any; PORT: any;
}, context: any) {
const url = new URL(request.url);
const serverName = env.SERVERNAME || url.hostname;
const delegate = `${env.HOSTNAME || url.hostname}:${env.PORT || url.port || 443}`;

if (url.pathname === '/_matrix/federation/v1/version') {
return new Response(JSON.stringify(returnMatrixServerVers()), {
Expand All @@ -47,15 +49,17 @@ export default {
})
}
const matrixId = toMatrixID(remoteId, `${remoteType}_`)
const mxcUrl = `mxc://${env.SERVERNAME}/${matrixId}`
const mxcUrl = `mxc://${serverName}/${matrixId}`
return new Response(JSON.stringify({
remoteType,
remoteId,
mxcUrl,
mxcId: matrixId
} satisfies SoliditasAddressConvertResponse))
} else if (url.pathname === '/.well-known/matrix/server') {
return new Response(JSON.stringify({ 'm.server': `${env.HOSTNAME}:${env.PORT}` } satisfies MatrixWellKnownServer));
return new Response(JSON.stringify({ 'm.server': delegate } satisfies MatrixWellKnownServer), {
headers: { 'Content-Type': 'application/json' },
});
} else {
return new Response(JSON.stringify(matrixEndpointNotImplemented('only implements media endpoints')), {
headers: { 'Content-Type': 'application/json' },
Expand Down
50 changes: 50 additions & 0 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { describe, it, expect } from 'vitest';

import worker from '../src/index';

const env = {} as { SERVERNAME: any; HOSTNAME: any; PORT: any };

describe('well-known delegation', () => {
it('delegates to the address the worker was reached on', async () => {
const response = await worker.fetch(
{ url: 'https://gifs.example/.well-known/matrix/server' },
env,
{}
);

expect(await response.json()).toEqual({ 'm.server': 'gifs.example:443' });
expect(response.headers.get('Content-Type')).toBe('application/json');
});

it('keeps the port it was reached on', async () => {
const response = await worker.fetch(
{ url: 'https://gifs.example:8443/.well-known/matrix/server' },
env,
{}
);

expect(await response.json()).toEqual({ 'm.server': 'gifs.example:8443' });
});

it('delegates to a pinned hostname and port when one is configured', async () => {
const response = await worker.fetch({ url: 'https://gifs.example/.well-known/matrix/server' }, {
...env,
HOSTNAME: 'proxy.example',
PORT: 8448,
}, {});

expect(await response.json()).toEqual({ 'm.server': 'proxy.example:8448' });
});
});

describe('address convert', () => {
it('builds the mxc url on the address the worker was reached on', async () => {
const response = await worker.fetch(
{ url: 'https://gifs.example/_soliditas/adressconvert?remoteType=tenor&remoteId=abc' },
env,
{}
);

expect(await response.json()).toMatchObject({ mxcUrl: 'mxc://gifs.example/tenor_YWJj' });
});
});
3 changes: 1 addition & 2 deletions wrangler.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"enabled": true
},
"upload_source_maps": true,
"compatibility_flags": [],
"compatibility_flags": []
/**
* Smart Placement
* https://developers.cloudflare.com/workers/configuration/smart-placement/#smart-placement
Expand All @@ -29,7 +29,6 @@
* Note: Use secrets to store sensitive data.
* https://developers.cloudflare.com/workers/configuration/secrets/
*/
"vars": { "HOSTNAME": "example.org", "SERVERNAME": "example.org", "PORT": 443 }
/**
* Static Assets
* https://developers.cloudflare.com/workers/static-assets/binding/
Expand Down
Loading