1 import * as WebFinger from 'webfinger.js'
2 import { WebFingerData } from '../../shared'
3 import { fetchRemoteAccount } from '../lib/activitypub'
4 import { isTestInstance } from './core-utils'
5 import { isActivityPubUrlValid } from './custom-validators/activitypub'
7 const webfinger = new WebFinger({
8 webfist_fallback: false,
9 tls_only: isTestInstance(),
14 async function getAccountFromWebfinger (nameWithHost: string) {
15 const webfingerData: WebFingerData = await webfingerLookup(nameWithHost)
17 if (Array.isArray(webfingerData.links) === false) throw new Error('WebFinger links is not an array.')
19 const selfLink = webfingerData.links.find(l => l.rel === 'self')
20 if (selfLink === undefined || isActivityPubUrlValid(selfLink.href) === false) {
21 throw new Error('Cannot find self link or href is not a valid URL.')
24 const account = await fetchRemoteAccount(selfLink.href)
25 if (account === undefined) throw new Error('Cannot fetch remote account ' + selfLink.href)
30 // ---------------------------------------------------------------------------
33 getAccountFromWebfinger
36 // ---------------------------------------------------------------------------
38 function webfingerLookup (nameWithHost: string) {
39 return new Promise<WebFingerData>((res, rej) => {
40 webfinger.lookup(nameWithHost, (err, p) => {
41 if (err) return rej(err)