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> {
+19
View File
@@ -508,6 +508,7 @@ __metadata:
"@polkadot/x-fetch": "npm:^13.5.1"
"@types/js-yaml": "npm:^4.0.9"
js-yaml: "npm:^4.1.0"
tldts: "npm:^7.0.8"
tslib: "npm:^2.8.1"
languageName: unknown
linkType: soft
@@ -8985,6 +8986,24 @@ __metadata:
languageName: node
linkType: hard
"tldts-core@npm:^7.0.8":
version: 7.0.8
resolution: "tldts-core@npm:7.0.8"
checksum: 10/a43d1a485ef460fd897bcfc618c4bc57e33f59a97526a08cffa8d8e3447abee82c9ec3bc652c2aebd8faf4a4f10cabd63211f9e955af4fca20258b2dba42bae9
languageName: node
linkType: hard
"tldts@npm:^7.0.8":
version: 7.0.8
resolution: "tldts@npm:7.0.8"
dependencies:
tldts-core: "npm:^7.0.8"
bin:
tldts: bin/cli.js
checksum: 10/b3cf1c0d78c982e99618eccacd628e5af1b4ef3700d6fb182eba78eda5dbb944fb4a154be220cc60342ab2c52de52876ef632df00e8cb2d1ceef01c833ef9e7c
languageName: node
linkType: hard
"tmp@npm:^0.0.33":
version: 0.0.33
resolution: "tmp@npm:0.0.33"