diff --git a/src/env.d.ts b/src/env.d.ts index 9b7ca01..1a4b698 100644 --- a/src/env.d.ts +++ b/src/env.d.ts @@ -1,5 +1,5 @@ export interface Env { - HOSTNAME: string; - SERVERNAME: string; - PORT: number; + HOSTNAME?: string; + SERVERNAME?: string; + PORT?: number; } \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index 7374feb..e4e1723 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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()), { @@ -47,7 +49,7 @@ 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, @@ -55,7 +57,9 @@ export default { 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' }, diff --git a/test/index.spec.ts b/test/index.spec.ts new file mode 100644 index 0000000..96d9e9e --- /dev/null +++ b/test/index.spec.ts @@ -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' }); + }); +}); diff --git a/wrangler.jsonc b/wrangler.jsonc index ea9b6a8..7a0b535 100644 --- a/wrangler.jsonc +++ b/wrangler.jsonc @@ -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 @@ -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/