2023-01-13 05:40:33 +01:00
|
|
|
import { URL } from "node:url";
|
|
|
|
import { getJson } from "@/misc/fetch.js";
|
|
|
|
import { query as urlQuery } from "@/prelude/url.js";
|
2018-03-31 12:55:00 +02:00
|
|
|
|
|
|
|
type ILink = {
|
2018-04-08 21:08:56 +02:00
|
|
|
href: string;
|
2019-04-12 18:43:22 +02:00
|
|
|
rel?: string;
|
2018-04-01 14:24:25 +02:00
|
|
|
};
|
2018-03-31 12:55:00 +02:00
|
|
|
|
|
|
|
type IWebFinger = {
|
2018-04-08 21:08:56 +02:00
|
|
|
links: ILink[];
|
|
|
|
subject: string;
|
2018-04-01 14:24:25 +02:00
|
|
|
};
|
2018-03-31 12:55:00 +02:00
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
export default async function (query: string): Promise<IWebFinger> {
|
2019-04-10 08:07:21 +02:00
|
|
|
const url = genUrl(query);
|
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
return (await getJson(
|
|
|
|
url,
|
|
|
|
"application/jrd+json, application/json",
|
|
|
|
)) as IWebFinger;
|
2019-04-10 08:07:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function genUrl(query: string) {
|
|
|
|
if (query.match(/^https?:\/\//)) {
|
|
|
|
const u = new URL(query);
|
2023-01-13 05:40:33 +01:00
|
|
|
return (
|
|
|
|
`${u.protocol}//${u.hostname}/.well-known/webfinger?${urlQuery({ resource: query })}`
|
|
|
|
);
|
2019-04-10 08:07:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const m = query.match(/^([^@]+)@(.*)/);
|
|
|
|
if (m) {
|
|
|
|
const hostname = m[2];
|
2023-01-13 05:40:33 +01:00
|
|
|
return (
|
|
|
|
`https://${hostname}/.well-known/webfinger?${urlQuery({ resource: `acct:${query}` })}`
|
|
|
|
);
|
2019-04-10 08:07:21 +02:00
|
|
|
}
|
2018-04-02 11:36:47 +02:00
|
|
|
|
2019-09-15 16:27:33 +02:00
|
|
|
throw new Error(`Invalid query (${query})`);
|
2018-04-02 11:36:47 +02:00
|
|
|
}
|