WebFingerリクエストで Proxy, Keep-Alive などをサポート #4658

Co-Authored-By: MeiMei <mei23@users.noreply.github.com>
This commit is contained in:
syuilo
2019-04-10 15:07:21 +09:00
parent 8468a9d4c7
commit 30172b92e6
4 changed files with 34 additions and 78 deletions

View File

@@ -1,6 +1,7 @@
import { WebFinger } from 'webfinger.js';
const webFinger = new WebFinger({ });
import config from '../config';
import * as request from 'request-promise-native';
import { URL } from 'url';
import { query as urlQuery } from '../prelude/url';
type ILink = {
href: string;
@@ -12,12 +13,33 @@ type IWebFinger = {
subject: string;
};
export default async function resolve(query: any): Promise<IWebFinger> {
return await new Promise((res, rej) => webFinger.lookup(query, (error: Error | string, result: any) => {
if (error) {
return rej(error);
}
export default async function(query: string): Promise<IWebFinger> {
const url = genUrl(query);
res(result.object);
})) as IWebFinger;
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})`);
}