1 import * as WebFinger from 'webfinger.js'
3 import { isTestInstance } from './core-utils'
4 import { isActivityPubUrlValid } from './custom-validators'
5 import { WebFingerData } from '../../shared'
6 import { fetchRemoteAccountAndCreatePod } from './activitypub'
8 const webfinger = new WebFinger({
9 webfist_fallback: false,
10 tls_only: isTestInstance(),
15 async function getAccountFromWebfinger (nameWithHost: string) {
16 const webfingerData: WebFingerData = await webfingerLookup(nameWithHost)
18 if (Array.isArray(webfingerData.links) === false) throw new Error('WebFinger links is not an array.')
20 const selfLink = webfingerData.links.find(l => l.rel === 'self')
21 if (selfLink === undefined || isActivityPubUrlValid(selfLink.href) === false) {
22 throw new Error('Cannot find self link or href is not a valid URL.')
25 const res = await fetchRemoteAccountAndCreatePod(selfLink.href)
26 if (res === undefined) throw new Error('Cannot fetch and create pod of remote account ' + selfLink.href)
31 // ---------------------------------------------------------------------------
34 getAccountFromWebfinger
37 // ---------------------------------------------------------------------------
39 function webfingerLookup (nameWithHost: string) {
40 return new Promise<WebFingerData>((res, rej) => {
41 webfinger.lookup(nameWithHost, (err, p) => {
42 if (err) return rej(err)