27be8e06cb
ref: https://github.com/misskey-dev/misskey/issues/13540 Co-authored-by: mei23 <m@m544.net> Co-authored-by: atsu1125 <atsuchan@atsuchan.page>
46 lines
1.1 KiB
TypeScript
46 lines
1.1 KiB
TypeScript
import { URL } from "node:url";
|
|
import config from "@/config/index.js";
|
|
import { toASCII } from "punycode";
|
|
import Logger from "@/services/logger.js";
|
|
import { inspect } from "node:util";
|
|
|
|
const logger = new Logger("convert-host");
|
|
|
|
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 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;
|
|
}
|
|
}
|
|
|
|
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());
|
|
}
|