summaryrefslogtreecommitdiff
path: root/src/remote/webfinger.ts
blob: e19ef96a2fc30cf732d612cc830321ba5ee02124 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import config from '../config';
import * as request from 'request-promise-native';
import { query as urlQuery } from '../prelude/url';

type ILink = {
	href: string;
	rel?: string;
};

type IWebFinger = {
	links: ILink[];
	subject: string;
};

export default async function(query: string): Promise<IWebFinger> {
	const url = genUrl(query);

	return await request({
		url,
		proxy: config.proxy,
		timeout: 10 * 1000,
		forever: true,
		headers: {
			'User-Agent': config.userAgent,
			Accept: 'application/jrd+json, application/json'
		},
		json: true
	});
}

function genUrl(query: string) {
	if (query.match(/^https?:\/\//)) {
		const u = new URL(query);
		return `${u.protocol}//${u.hostname}/.well-known/webfinger?` + urlQuery({ resource: query });
	}

	const m = query.match(/^([^@]+)@(.*)/);
	if (m) {
		const hostname = m[2];
		return `https://${hostname}/.well-known/webfinger?` + urlQuery({ resource: `acct:${query}` });
	}

	throw new Error(`Invalid query (${query})`);
}