Skip to content

Commit 00bf18a

Browse files
authored
Merge pull request #89 from underprovisioned/cc-tlds
chore: use psl for base domain resolution
2 parents e5fa0e6 + a235a78 commit 00bf18a

File tree

7 files changed

+39
-9
lines changed

7 files changed

+39
-9
lines changed

global.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,9 @@
11
// global.d.ts
22
/// <reference types="vite-plugin-md-to-html/types" />
3+
4+
// Type definitions for the 'psl' module
5+
declare module 'psl' {
6+
export function parse(domain: string): { input: string; tld: string; sld: string; domain: string; subdomain: string }
7+
export function isValid(domain: string): boolean
8+
export function get(domain: string): string | null
9+
}

package-lock.json

Lines changed: 13 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"linkedom": "^0.18.11",
3737
"primeicons": "^7.0.0",
3838
"primevue": "^4.2.5",
39+
"psl": "^1.15.0",
3940
"tailwindcss-primeui": "^0.4.0",
4041
"vue": "^3.5.12"
4142
},

src/entrypoints/background/interception/declarativeNetRequestHandler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import notifications from '@/services/notifications'
99
import {
1010
DeserializedResponse,
1111
deserializeResponse,
12-
getBaseDomainFromULR,
12+
getBaseDomainFromURL,
1313
serializeRequest,
1414
} from '@/utils/fetchUtilities'
1515

@@ -115,7 +115,7 @@ function onErrorOccurredListener(details: Browser.webRequest.WebRequestDetails):
115115
if (!cachedRequest) throw new Error(`no cached request found for requestId ${details.requestId}`)
116116
requestCache.delete(details.requestId) // Clear the cache after processing
117117
const { url, source } = cachedRequest
118-
const domain = getBaseDomainFromULR(url)
118+
const domain = getBaseDomainFromURL(url)
119119
const setting = (await interception.getSettings()).domains.find((d) => d.domain === domain)
120120
if (!setting) throw new Error(`no domain setting found for ${domain}`)
121121
try {

src/entrypoints/background/interception/interceptedRequestsHandler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import notifications from '@/services/notifications'
1111
import { NZBFileObject } from '@/services/nzbfile'
1212
import {
1313
DeserializedResponse,
14-
getBaseDomainFromULR,
14+
getBaseDomainFromURL,
1515
getFilenameFromResponse,
1616
getHttpStatusText,
1717
} from '@/utils/fetchUtilities'
@@ -46,7 +46,7 @@ export async function processInterceptedRequestResponse({
4646
}): Promise<void> {
4747
let nzbFiles: NZBFileObject[] = [] // Initialize nzbFiles here
4848
const url = response.url
49-
const domain = getBaseDomainFromULR(url)
49+
const domain = getBaseDomainFromURL(url)
5050
const filename = getFilenameFromResponse(response as Response)
5151
const setting = (await getInterceptionSettings()).domains.find((d) => d.domain === domain)
5252
try {

src/services/resolvers.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { FormFieldResolverOptions } from '@primevue/forms'
2+
import psl from 'psl'
23

34
import { i18n } from '#i18n'
45
import { browser } from '#imports'
@@ -52,8 +53,7 @@ export const requiredBaseDomainResolver = ({ value, name = '' }: FormFieldResolv
5253
message: i18n.t('validation.isRequired', [name]),
5354
})
5455
}
55-
const regex = /^[a-zA-Z0-9-]{1,63}(?:\.(?:(?:a[cd]|com?|edu|gov|net|org?)\.[a-zA-Z0-9-]{2}|[a-zA-Z]{2,63}))?$/i
56-
if (!regex.test(value)) {
56+
if (psl.get(value) !== value) {
5757
errors.push({ message: i18n.t('validation.noBaseDomain') })
5858
}
5959
return { errors }

src/utils/fetchUtilities.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import psl from 'psl'
2+
13
import { b64EncodeUnicode, getFileNameFromPath } from '@/utils/stringUtilities'
24

35
const DEFAULT_HEADER = { 'X-NZBDonkey': 'true' }
@@ -214,8 +216,16 @@ export const getFilenameFromResponse = (response: Response): string => {
214216
* @return {string} The base domain.
215217
* @throws {Error} Throws an error if the URL is invalid.
216218
*/
217-
export const getBaseDomainFromULR = (url: string): string => {
218-
return new URL(url).hostname.split('.').slice(-2).join('.')
219+
export const getBaseDomainFromURL = (url: string): string => {
220+
const hostname = new URL(url).hostname
221+
const domain = psl.get(hostname)
222+
223+
// Fall back to simple extraction for edge cases
224+
if (domain === null) {
225+
return hostname.split('.').slice(-2).join('.')
226+
}
227+
228+
return domain
219229
}
220230

221231
/**

0 commit comments

Comments
 (0)