feat(backend): Bearcaps URLに対応 (MisskeyIO#330)

This commit is contained in:
まっちゃとーにゅ 2024-01-06 02:56:42 +09:00 committed by GitHub
parent a2cc3b6a12
commit fa8784c038
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -150,11 +150,14 @@ export class HttpRequestService {
controller.abort(); controller.abort();
}, timeout); }, timeout);
const res = await fetch(url, { const bearcaps = url.startsWith('bear:?') ? this.parseBearcaps(url) : undefined;
const res = await fetch(bearcaps?.url ?? url, {
method: args.method ?? 'GET', method: args.method ?? 'GET',
headers: { headers: {
'User-Agent': this.config.userAgent, 'User-Agent': this.config.userAgent,
...(args.headers ?? {}), ...(args.headers ?? {}),
...(bearcaps?.token ? { Authorization: `Bearer ${bearcaps.token}` } : {}),
}, },
body: args.body, body: args.body,
size: args.size ?? 10 * 1024 * 1024, size: args.size ?? 10 * 1024 * 1024,
@ -168,4 +171,17 @@ export class HttpRequestService {
return res; return res;
} }
// Bearcaps https://docs.joinmastodon.org/spec/bearcaps/
// bear:?t=<token>&u=https://example.com/foo'
// -> GET https://example.com/foo Authorization: Bearer <token>
private parseBearcaps(url: string): { url: string, token: string | undefined } | undefined {
const params = new URLSearchParams(url.split('?')[1]);
if (!params.has('u')) return undefined;
return {
url: params.get('u')!,
token: params.get('t') ?? undefined,
};
}
} }