Dough Git

clove/isATracker

src/lib/server/rules/recordTargets.ts

back to src/lib/server/rules

/* src/lib/server/rules/recordTargets.ts
 * MIT — (c) Doughmination System®
 */

import { isPlainObject } from './types.ts';

export const httpUrlPrefix = /^https?:\/\//i;

export function readRecordTarget(json: unknown, recordType: 'CNAME' | 'URL'): string | null {
	if (!isPlainObject(json)) {
		return null;
	}

	const records = json.records ?? json.record;

	if (!isPlainObject(records)) {
		return null;
	}

	const key = Object.keys(records).find((name) => name.trim().toUpperCase() === recordType);
	const value = key === undefined ? undefined : records[key];

	return typeof value === 'string' ? value.trim() : null;
}

export function readHttpHostname(target: string): string | null {
	if (!httpUrlPrefix.test(target)) {
		return null;
	}

	try {
		return new URL(target).hostname;
	} catch {
		return null;
	}
}