Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add auto-update of tlds list #7

Merged
merged 2 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions .github/scripts/update-list.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { execFileSync } from 'node:child_process';
import { writeFile } from 'node:fs/promises';
import { createInterface } from 'node:readline';
import * as Wreck from '@hapi/wreck';

const ianaUri = 'https://data.iana.org/TLD/tlds-alpha-by-domain.txt';

async function fetchTLDs() {
const request = await Wreck.get(ianaUri);
const rl = createInterface({
input: Wreck.toReadableStream(request.payload as string),
crlfDelay: Infinity,
});

let lineNumber = 0;

const data = {
version: '',
domains: [] as string[],
};

for await (const line of rl) {
if (lineNumber === 0) {
if (/^# Version \d{8}, Last Updated .+ UTC$/.test(line)) {
throw new Error('Unexpected version line');
}
data.version = line;
} else if (line.length > 0) {
data.domains.push(line);
}

lineNumber++;
}

const formattedFile = `// ${ianaUri}
// ${data.version}

export const TLDS: string[] = [
${data.domains.map((d) => ` '${d}',`).join('\n')}
];
`;

try {
execFileSync(
'git',
[
'diff',
'--exit-code', // Fail if there are changes
'--no-index',
'-I',
'^// #', // Ignore version line, it changes every day
'--',
'src/tlds.ts',
'-', // Take stdin
],
{
input: formattedFile,
},
);
} catch {
// An error means the file is probably different
await writeFile('./src/tlds.ts', formattedFile);
}
}

fetchTLDs();
33 changes: 33 additions & 0 deletions .github/workflows/update-check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Update check

on:
schedule:
- cron: '0 0 * * *'
workflow_dispatch:

jobs:
update-check:
runs-on: ubuntu-latest

permissions:
contents: write
pull-requests: write

steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4

- name: Install dependencies
run: npm install

- name: Run update list
run: npm run update-list

- name: Create Pull Request
uses: peter-evans/create-pull-request@v6
with:
title: Update TLDs
commit-message: 'fix: 🤖 Update TLDs'
branch: fix/auto-update-tlds
body: Automated update of TLDs
labels: bug
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,23 @@
"devDependencies": {
"@hapi/code": "^9.0.3",
"@hapi/lab": "^25.1.3",
"@types/node": "^16.18.50",
"@hapi/wreck": "^18.0.1",
"@types/node": "^20.11.30",
"@typescript-eslint/eslint-plugin": "^6.7.0",
"@typescript-eslint/parser": "^6.7.0",
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-prettier": "^5.0.0",
"prettier": "^3.0.3",
"ts-node": "^10.9.2",
"typescript": "^5.2.2"
},
"scripts": {
"test": "lab -t 100 -L",
"test-cov-html": "lab -t 100 -L -r html -o coverage.html",
"dist": "rm -rf dist esm && tsc --module commonjs --outdir dist && tsc --module es6 --outdir esm",
"prepublishOnly": "npm run dist",
"format": "prettier --write '**/*.ts'"
"format": "prettier --write '**/*.ts'",
"update-list": "ts-node .github/scripts/update-list.ts"
},
"license": "BSD-3-Clause"
}
Loading