2019-04-10 00:07:21 -06:00
|
|
|
import config from '../config';
|
|
|
|
import * as request from 'request-promise-native';
|
|
|
|
import { query as urlQuery } from '../prelude/url';
|
2018-03-31 04:55:00 -06:00
|
|
|
|
|
|
|
type ILink = {
|
2018-04-08 13:08:56 -06:00
|
|
|
href: string;
|
2019-04-12 10:43:22 -06:00
|
|
|
rel?: string;
|
2018-04-01 06:24:25 -06:00
|
|
|
};
|
2018-03-31 04:55:00 -06:00
|
|
|
|
|
|
|
type IWebFinger = {
|
2018-04-08 13:08:56 -06:00
|
|
|
links: ILink[];
|
|
|
|
subject: string;
|
2018-04-01 06:24:25 -06:00
|
|
|
};
|
2018-03-31 04:55:00 -06:00
|
|
|
|
2019-04-10 00:07:21 -06:00
|
|
|
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}` });
|
|
|
|
}
|
2018-04-02 03:36:47 -06:00
|
|
|
|
2019-04-10 00:07:21 -06:00
|
|
|
throw new Error(`Invalied query (${query})`);
|
2018-04-02 03:36:47 -06:00
|
|
|
}
|