44 lines
1.2 KiB
TypeScript
44 lines
1.2 KiB
TypeScript
|
async function askForAccessToken(client: Client): Promise<string | undefined> {
|
||
|
return new Promise((resolve) => {
|
||
|
const responseKey = Math.random().toString(36);
|
||
|
const listener = (event: ExtendableMessageEvent) => {
|
||
|
if (event.data.responseKey !== responseKey) return;
|
||
|
resolve(event.data.token);
|
||
|
self.removeEventListener('message', listener);
|
||
|
};
|
||
|
self.addEventListener('message', listener);
|
||
|
client.postMessage({ responseKey, type: 'token' });
|
||
|
});
|
||
|
}
|
||
|
|
||
|
function fetchConfig(token?: string): RequestInit | undefined {
|
||
|
if (!token) return undefined;
|
||
|
|
||
|
return {
|
||
|
headers: {
|
||
|
Authorization: `Bearer ${token}`,
|
||
|
},
|
||
|
};
|
||
|
}
|
||
|
|
||
|
self.addEventListener('fetch', (event: FetchEvent) => {
|
||
|
const { url, method } = event.request;
|
||
|
if (method !== 'GET') return;
|
||
|
if (
|
||
|
!url.includes('/_matrix/client/v1/media/download') &&
|
||
|
!url.includes('/_matrix/client/v1/media/thumbnail')
|
||
|
) {
|
||
|
return;
|
||
|
}
|
||
|
event.respondWith(
|
||
|
(async (): Promise<Response> => {
|
||
|
const client = await clients.get(event.clientId);
|
||
|
let token: string | undefined;
|
||
|
if (client) token = await askForAccessToken(client);
|
||
|
|
||
|
// eslint-disable-next-line consistent-return
|
||
|
return fetch(url, fetchConfig(token));
|
||
|
})()
|
||
|
);
|
||
|
});
|