update: better TLD detection

This commit is contained in:
alikia2x 2024-07-14 17:43:40 +08:00
parent 874fd082f8
commit 756055a079
7 changed files with 1493 additions and 28 deletions

BIN
bun.lockb

Binary file not shown.

File diff suppressed because one or more lines are too long

1447
lib/url/tlds.txt Normal file

File diff suppressed because it is too large Load Diff

3
lib/url/updateTLD.ts Normal file
View File

@ -0,0 +1,3 @@
const response = await fetch("https://data.iana.org/TLD/tlds-alpha-by-domain.txt");
const tldText = await response.text();
await Bun.write("./lib/url/tlds.txt", tldText);

View File

@ -1,5 +1,5 @@
import punycode from "punycode";
import { tldList } from "./tldList";
import { toASCII } from "tr46";
import { getTLD } from "./tldList";
export default function validLink(link: string) {
let finalURL;
@ -28,8 +28,10 @@ export default function validLink(link: string) {
}
export function validTLD(domain: string): boolean {
const tld = punycode.toUnicode(domain.split(".").reverse()[0]);
if (tldList.includes(tld)) {
if (!domain.includes(".")) return false;
const tld = toASCII(domain.split(".").reverse()[0]);
const tldList = getTLD();
if (tldList.includes(tld.toUpperCase())) {
return true;
} else {
return false;

View File

@ -1,7 +1,7 @@
{
"name": "sparkhome",
"private": false,
"version": "5.5.1",
"version": "5.5.2",
"type": "module",
"scripts": {
"dev": "bun server.ts",
@ -14,6 +14,7 @@
"@nextui-org/react": "^2.4.2",
"@types/bun": "^1.1.6",
"@types/express": "^4.17.21",
"@types/tr46": "^5.0.0",
"cac": "^6.7.14",
"chalk": "^5.3.0",
"express": "^4.19.2",
@ -31,6 +32,7 @@
"react-router": "^6.23.1",
"react-router-dom": "^6.23.1",
"search-engine-autocomplete": "^0.4.3",
"tr46": "^5.0.0",
"valid-url": "^1.0.9",
"validate-color": "^2.2.4",
"vite-express": "^0.17.0"

View File

@ -28,43 +28,50 @@ describe("Check if a string is an accessible domain/URL/IP", () => {
test("IPv6 without protocol", () => {
expect(validLink("[::]")).toBe(true);
});
test("special test for 铜锣湾.chn.moe", () => {
expect(validLink("铜锣湾.chn.moe")).toBe(true);
});
test("Not a valid host/URL.", () => {
expect(validLink("weather")).toBe(false);
});
});
// Reference: https://www.iana.org/domains/root/db
describe("Check if the given TLD exist and assigned.", () => {
test("Valid normal TLD", () => {
expect(validTLD("com")).toBe(true);
expect(validTLD("top")).toBe(true);
expect(validTLD("net")).toBe(true);
expect(validTLD("org")).toBe(true);
expect(validTLD("example.com")).toBe(true);
expect(validTLD("example.top")).toBe(true);
expect(validTLD("example.net")).toBe(true);
expect(validTLD("example.org")).toBe(true);
});
test("Valid new TLDs", () => {
// they really exist!
expect(validTLD("foo")).toBe(true);
expect(validTLD("bar")).toBe(true);
expect(validTLD("example.foo")).toBe(true);
expect(validTLD("example.bar")).toBe(true);
expect(validTLD('example.zip')).toBe(true);
});
test("Exist but not assigned TLD", () => {
expect(validTLD("active")).toBe(false);
expect(validTLD("off")).toBe(false);
expect(validTLD("example.active")).toBe(false);
expect(validTLD("example.off")).toBe(false);
});
test("with dot", () => {
expect(validTLD(".com")).toBe(true);
expect(validTLD(".us")).toBe(true);
expect(validTLD(".cn")).toBe(true);
expect(validTLD(".io")).toBe(true);
expect(validTLD("example.com")).toBe(true);
expect(validTLD("example.us")).toBe(true);
expect(validTLD("example.cn")).toBe(true);
expect(validTLD("example.io")).toBe(true);
});
test("Punycode TLDs", () => {
expect(validTLD(".中国")).toBe(true);
expect(validTLD(".РФ")).toBe(true);
expect(validTLD(".कॉम")).toBe(true);
expect(validTLD("ایران")).toBe(true);
expect(validTLD("இலங்கை")).toBe(true);
expect(validTLD("გე")).toBe(true);
expect(validTLD("ポイント")).toBe(true);
expect(validTLD("example.中国")).toBe(true);
expect(validTLD("example.РФ")).toBe(true);
expect(validTLD("example.कॉम")).toBe(true);
expect(validTLD("example.ایران")).toBe(true);
expect(validTLD("example.இலங்கை")).toBe(true);
expect(validTLD("example.გე")).toBe(true);
expect(validTLD("example.ポイント")).toBe(true);
});
test("Punycode TLDs but not assigned", () => {
expect(validTLD("テスト")).toBe(false);
expect(validTLD("परीक्षा")).toBe(false);
expect(validTLD("测试")).toBe(false);
expect(validTLD("example.テスト")).toBe(false);
expect(validTLD("example.परीक्षा")).toBe(false);
expect(validTLD("example.测试")).toBe(false);
});
});