feat: add tldts dependency for improved hostname parsing (#5128)

This commit is contained in:
Arjun Porwal
2025-06-16 14:15:24 +05:30
committed by GitHub
parent b3eeda420d
commit 35515a4ac7
3 changed files with 37 additions and 10 deletions
+1
View File
@@ -24,6 +24,7 @@
"@polkadot/util": "^13.5.1",
"@polkadot/util-crypto": "^13.5.1",
"@polkadot/x-fetch": "^13.5.1",
"tldts": "^7.0.8",
"tslib": "^2.8.1"
},
"devDependencies": {
+17 -10
View File
@@ -3,6 +3,8 @@
import type { AddressList, HostList } from './types.js';
import { parse } from 'tldts';
import { u8aEq } from '@polkadot/util';
import { decodeAddress } from '@polkadot/util-crypto';
@@ -37,21 +39,26 @@ const cacheAddr: CacheAddrList = {
const cacheHost: Record<string, CacheHostList> = {};
function splitHostParts (host: string): string[] {
return host
// split domain
const parsed = parse(host, { allowPrivateDomains: true });
if (!parsed.hostname) {
return [];
}
return parsed.hostname
.replace(/\.$/, '') // remove trailing dot
.split('.')
// reverse order
.reverse();
}
function extractHostParts (host: string): string[] {
return splitHostParts(
host
// remove protocol
.replace(/https:\/\/|http:\/\/|wss:\/\/|ws:\/\//, '')
// get the domain-only part
.split('/')[0]
);
const parsed = parse(host, { allowPrivateDomains: true });
if (!parsed.hostname) {
return [];
}
return splitHostParts(parsed.hostname);
}
async function retrieveAddrCache (allowCached = true): Promise<CacheAddrList> {