2023-01-13 05:40:33 +01:00
|
|
|
import { URL } from "node:url";
|
2023-11-26 21:33:46 +01:00
|
|
|
import config from "@/config/index.js";
|
2023-12-05 08:12:10 +01:00
|
|
|
import { toASCII } from "punycode";
|
2024-03-16 17:00:56 +01:00
|
|
|
import Logger from "@/services/logger.js";
|
|
|
|
import { inspect } from "node:util";
|
|
|
|
|
|
|
|
const logger = new Logger("convert-host");
|
2019-03-12 15:38:11 +01:00
|
|
|
|
2019-04-12 18:43:22 +02:00
|
|
|
export function getFullApAccount(username: string, host: string | null) {
|
2023-01-13 05:40:33 +01:00
|
|
|
return host
|
|
|
|
? `${username}@${toPuny(host)}`
|
|
|
|
: `${username}@${toPuny(config.host)}`;
|
2019-03-12 15:38:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export function isSelfHost(host: string) {
|
|
|
|
if (host == null) return true;
|
2019-04-09 16:59:32 +02:00
|
|
|
return toPuny(config.host) === toPuny(host);
|
2019-03-12 15:38:11 +01:00
|
|
|
}
|
|
|
|
|
2024-03-16 17:00:56 +01:00
|
|
|
export function isSameOrigin(src: unknown): boolean | null {
|
|
|
|
if (typeof src !== "string") {
|
|
|
|
logger.debug(`unknown origin: ${inspect(src)}`);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
const u = new URL(src);
|
|
|
|
return u.origin === config.url;
|
|
|
|
} catch (e) {
|
|
|
|
logger.debug(inspect(e));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-13 03:21:16 +01:00
|
|
|
export function extractDbHost(uri: string) {
|
|
|
|
const url = new URL(uri);
|
2019-04-09 16:59:32 +02:00
|
|
|
return toPuny(url.hostname);
|
2019-03-13 03:21:16 +01:00
|
|
|
}
|
|
|
|
|
2019-04-09 16:59:32 +02:00
|
|
|
export function toPuny(host: string) {
|
2019-03-12 15:38:11 +01:00
|
|
|
return toASCII(host.toLowerCase());
|
|
|
|
}
|
2019-04-13 09:54:21 +02:00
|
|
|
|
|
|
|
export function toPunyNullable(host: string | null | undefined): string | null {
|
|
|
|
if (host == null) return null;
|
|
|
|
return toASCII(host.toLowerCase());
|
|
|
|
}
|