update: better TLD detection
This commit is contained in:
parent
874fd082f8
commit
756055a079
File diff suppressed because one or more lines are too long
1447
lib/url/tlds.txt
Normal file
1447
lib/url/tlds.txt
Normal file
File diff suppressed because it is too large
Load Diff
3
lib/url/updateTLD.ts
Normal file
3
lib/url/updateTLD.ts
Normal 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);
|
@ -1,5 +1,5 @@
|
|||||||
import punycode from "punycode";
|
import { toASCII } from "tr46";
|
||||||
import { tldList } from "./tldList";
|
import { getTLD } from "./tldList";
|
||||||
|
|
||||||
export default function validLink(link: string) {
|
export default function validLink(link: string) {
|
||||||
let finalURL;
|
let finalURL;
|
||||||
@ -28,8 +28,10 @@ export default function validLink(link: string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function validTLD(domain: string): boolean {
|
export function validTLD(domain: string): boolean {
|
||||||
const tld = punycode.toUnicode(domain.split(".").reverse()[0]);
|
if (!domain.includes(".")) return false;
|
||||||
if (tldList.includes(tld)) {
|
const tld = toASCII(domain.split(".").reverse()[0]);
|
||||||
|
const tldList = getTLD();
|
||||||
|
if (tldList.includes(tld.toUpperCase())) {
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
return false;
|
return false;
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"name": "sparkhome",
|
"name": "sparkhome",
|
||||||
"private": false,
|
"private": false,
|
||||||
"version": "5.5.1",
|
"version": "5.5.2",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "bun server.ts",
|
"dev": "bun server.ts",
|
||||||
@ -14,6 +14,7 @@
|
|||||||
"@nextui-org/react": "^2.4.2",
|
"@nextui-org/react": "^2.4.2",
|
||||||
"@types/bun": "^1.1.6",
|
"@types/bun": "^1.1.6",
|
||||||
"@types/express": "^4.17.21",
|
"@types/express": "^4.17.21",
|
||||||
|
"@types/tr46": "^5.0.0",
|
||||||
"cac": "^6.7.14",
|
"cac": "^6.7.14",
|
||||||
"chalk": "^5.3.0",
|
"chalk": "^5.3.0",
|
||||||
"express": "^4.19.2",
|
"express": "^4.19.2",
|
||||||
@ -31,6 +32,7 @@
|
|||||||
"react-router": "^6.23.1",
|
"react-router": "^6.23.1",
|
||||||
"react-router-dom": "^6.23.1",
|
"react-router-dom": "^6.23.1",
|
||||||
"search-engine-autocomplete": "^0.4.3",
|
"search-engine-autocomplete": "^0.4.3",
|
||||||
|
"tr46": "^5.0.0",
|
||||||
"valid-url": "^1.0.9",
|
"valid-url": "^1.0.9",
|
||||||
"validate-color": "^2.2.4",
|
"validate-color": "^2.2.4",
|
||||||
"vite-express": "^0.17.0"
|
"vite-express": "^0.17.0"
|
||||||
|
@ -28,43 +28,50 @@ describe("Check if a string is an accessible domain/URL/IP", () => {
|
|||||||
test("IPv6 without protocol", () => {
|
test("IPv6 without protocol", () => {
|
||||||
expect(validLink("[::]")).toBe(true);
|
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
|
// Reference: https://www.iana.org/domains/root/db
|
||||||
describe("Check if the given TLD exist and assigned.", () => {
|
describe("Check if the given TLD exist and assigned.", () => {
|
||||||
test("Valid normal TLD", () => {
|
test("Valid normal TLD", () => {
|
||||||
expect(validTLD("com")).toBe(true);
|
expect(validTLD("example.com")).toBe(true);
|
||||||
expect(validTLD("top")).toBe(true);
|
expect(validTLD("example.top")).toBe(true);
|
||||||
expect(validTLD("net")).toBe(true);
|
expect(validTLD("example.net")).toBe(true);
|
||||||
expect(validTLD("org")).toBe(true);
|
expect(validTLD("example.org")).toBe(true);
|
||||||
});
|
});
|
||||||
test("Valid new TLDs", () => {
|
test("Valid new TLDs", () => {
|
||||||
// they really exist!
|
// they really exist!
|
||||||
expect(validTLD("foo")).toBe(true);
|
expect(validTLD("example.foo")).toBe(true);
|
||||||
expect(validTLD("bar")).toBe(true);
|
expect(validTLD("example.bar")).toBe(true);
|
||||||
|
expect(validTLD('example.zip')).toBe(true);
|
||||||
});
|
});
|
||||||
test("Exist but not assigned TLD", () => {
|
test("Exist but not assigned TLD", () => {
|
||||||
expect(validTLD("active")).toBe(false);
|
expect(validTLD("example.active")).toBe(false);
|
||||||
expect(validTLD("off")).toBe(false);
|
expect(validTLD("example.off")).toBe(false);
|
||||||
});
|
});
|
||||||
test("with dot", () => {
|
test("with dot", () => {
|
||||||
expect(validTLD(".com")).toBe(true);
|
expect(validTLD("example.com")).toBe(true);
|
||||||
expect(validTLD(".us")).toBe(true);
|
expect(validTLD("example.us")).toBe(true);
|
||||||
expect(validTLD(".cn")).toBe(true);
|
expect(validTLD("example.cn")).toBe(true);
|
||||||
expect(validTLD(".io")).toBe(true);
|
expect(validTLD("example.io")).toBe(true);
|
||||||
});
|
});
|
||||||
test("Punycode TLDs", () => {
|
test("Punycode TLDs", () => {
|
||||||
expect(validTLD(".中国")).toBe(true);
|
expect(validTLD("example.中国")).toBe(true);
|
||||||
expect(validTLD(".РФ")).toBe(true);
|
expect(validTLD("example.РФ")).toBe(true);
|
||||||
expect(validTLD(".कॉम")).toBe(true);
|
expect(validTLD("example.कॉम")).toBe(true);
|
||||||
expect(validTLD("ایران")).toBe(true);
|
expect(validTLD("example.ایران")).toBe(true);
|
||||||
expect(validTLD("இலங்கை")).toBe(true);
|
expect(validTLD("example.இலங்கை")).toBe(true);
|
||||||
expect(validTLD("გე")).toBe(true);
|
expect(validTLD("example.გე")).toBe(true);
|
||||||
expect(validTLD("ポイント")).toBe(true);
|
expect(validTLD("example.ポイント")).toBe(true);
|
||||||
});
|
});
|
||||||
test("Punycode TLDs but not assigned", () => {
|
test("Punycode TLDs but not assigned", () => {
|
||||||
expect(validTLD("テスト")).toBe(false);
|
expect(validTLD("example.テスト")).toBe(false);
|
||||||
expect(validTLD("परीक्षा")).toBe(false);
|
expect(validTLD("example.परीक्षा")).toBe(false);
|
||||||
expect(validTLD("测试")).toBe(false);
|
expect(validTLD("example.测试")).toBe(false);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user