Skip to content

Commit

Permalink
feat(backend): Bearcaps URLに対応
Browse files Browse the repository at this point in the history
  • Loading branch information
u1-liquid authored and noellabo committed Aug 8, 2024
1 parent 59e2e43 commit cc53bc3
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion packages/backend/src/core/HttpRequestService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,14 @@ export class HttpRequestService {
controller.abort();
}, 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',
headers: {
'User-Agent': this.config.userAgent,
...(args.headers ?? {}),
...(bearcaps?.token ? { Authorization: `Bearer ${bearcaps.token}` } : {}),
},
body: args.body,
size: args.size ?? 10 * 1024 * 1024,
Expand All @@ -201,4 +204,17 @@ export class HttpRequestService {

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,
};
}
}

0 comments on commit cc53bc3

Please sign in to comment.