mirror of
https://activitypub.software/TransFem-org/Sharkey.git
synced 2024-11-16 16:57:14 +01:00
1331f0b953
> New application code should use the WHATWG API.
44 lines
983 B
TypeScript
44 lines
983 B
TypeScript
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(`Invalied query (${query})`);
|
|
}
|