summaryrefslogtreecommitdiff
path: root/src/misc/convert-host.ts
blob: f8957a64a8c5ba369152f39a895c8b1f97b0e7e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import { URL } from 'url';
import config from '@/config';
import { toASCII } from 'punycode';

export function getFullApAccount(username: string, host: string | null) {
	return host ? `${username}@${toPuny(host)}` : `${username}@${toPuny(config.host)}`;
}

export function isSelfHost(host: string) {
	if (host == null) return true;
	return toPuny(config.host) === toPuny(host);
}

export function extractDbHost(uri: string) {
	const url = new URL(uri);
	return toPuny(url.hostname);
}

export function toPuny(host: string) {
	return toASCII(host.toLowerCase());
}

export function toPunyNullable(host: string | null | undefined): string | null {
	if (host == null) return null;
	return toASCII(host.toLowerCase());
}