2022-02-27 11:07:39 +09:00
|
|
|
import { URL } from 'node:url';
|
|
|
|
import { getJson } from '@/misc/fetch.js';
|
|
|
|
import { query as urlQuery } from '@/prelude/url.js';
|
2018-03-31 19:55:00 +09:00
|
|
|
|
|
|
|
type ILink = {
|
2018-04-09 04:08:56 +09:00
|
|
|
href: string;
|
2019-04-13 01:43:22 +09:00
|
|
|
rel?: string;
|
2018-04-01 21:24:25 +09:00
|
|
|
};
|
2018-03-31 19:55:00 +09:00
|
|
|
|
|
|
|
type IWebFinger = {
|
2018-04-09 04:08:56 +09:00
|
|
|
links: ILink[];
|
|
|
|
subject: string;
|
2018-04-01 21:24:25 +09:00
|
|
|
};
|
2018-03-31 19:55:00 +09:00
|
|
|
|
2019-04-10 15:07:21 +09:00
|
|
|
export default async function(query: string): Promise<IWebFinger> {
|
|
|
|
const url = genUrl(query);
|
|
|
|
|
2022-04-03 16:35:36 +09:00
|
|
|
return await getJson(url, 'application/jrd+json, application/json') as IWebFinger;
|
2019-04-10 15:07:21 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
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 18:36:47 +09:00
|
|
|
|
2019-09-15 23:27:33 +09:00
|
|
|
throw new Error(`Invalid query (${query})`);
|
2018-04-02 18:36:47 +09:00
|
|
|
}
|