|
| 1 | +#!/usr/bin/env -S npx --yes tsx |
| 2 | +/** |
| 3 | + * domainfree — bulk domain availability, straight from the registry. |
| 4 | + * |
| 5 | + * Prints only the names that can actually be registered, one per line, so the |
| 6 | + * output pipes into anything. Companion to `domainjson`, which looks one name |
| 7 | + * up in depth; this answers one question across thousands. |
| 8 | + */ |
| 9 | + |
| 10 | +import { readFile } from 'node:fs/promises'; |
| 11 | +import { UsageError, integer, parseArgs } from '../src/args.ts'; |
| 12 | +import { isMain } from '../src/is-main.ts'; |
| 13 | +import { |
| 14 | + DEFAULT_JOBS, |
| 15 | + DEFAULT_TIMEOUT_MS, |
| 16 | + checkMany, |
| 17 | + normalizeNames, |
| 18 | + summarize, |
| 19 | +} from '../src/domain-free.ts'; |
| 20 | + |
| 21 | +const USAGE = `Usage: |
| 22 | + domainfree <name>... |
| 23 | + domainfree --file candidates.txt |
| 24 | + generate-names | domainfree --jobs 24 |
| 25 | +
|
| 26 | +Availability is read from RDAP, never inferred from DNS: a parked domain |
| 27 | +resolves but is taken, and a domain registered with no nameservers returns |
| 28 | +NXDOMAIN exactly like a free one. |
| 29 | +
|
| 30 | +Options: |
| 31 | + -f, --file FILE read names from FILE, one per line ("-" for stdin) |
| 32 | + -j, --jobs N parallel lookups (default: ${DEFAULT_JOBS}) |
| 33 | + -t, --timeout MS per-lookup timeout (default: ${DEFAULT_TIMEOUT_MS}) |
| 34 | + -a, --all print every name as "STATUS domain", not just the free ones |
| 35 | + -q, --quiet suppress the summary, which goes to stderr |
| 36 | + -h, --help show this help |
| 37 | +
|
| 38 | +Only available names go to stdout, so \`domainfree -f in.txt | wc -l\` counts |
| 39 | +what you can buy. Exit status is 2 when any lookup stayed indeterminate — an |
| 40 | +unknown is never reported as available. |
| 41 | +`; |
| 42 | + |
| 43 | +async function readStdin(): Promise<string> { |
| 44 | + const chunks: Buffer[] = []; |
| 45 | + for await (const chunk of process.stdin) chunks.push(chunk as Buffer); |
| 46 | + return Buffer.concat(chunks).toString('utf8'); |
| 47 | +} |
| 48 | + |
| 49 | +if (isMain(import.meta.url)) { |
| 50 | + try { |
| 51 | + const { flags, values, positional } = parseArgs(process.argv.slice(2), { |
| 52 | + boolean: ['-a', '--all', '-q', '--quiet', '-h', '--help'], |
| 53 | + string: ['-f', '--file', '-j', '--jobs', '-t', '--timeout'], |
| 54 | + }); |
| 55 | + |
| 56 | + if (flags.has('-h') || flags.has('--help')) { |
| 57 | + process.stdout.write(USAGE); |
| 58 | + process.exit(0); |
| 59 | + } |
| 60 | + |
| 61 | + const showAll = flags.has('-a') || flags.has('--all'); |
| 62 | + const quiet = flags.has('-q') || flags.has('--quiet'); |
| 63 | + const jobs = integer(values, values.has('-j') ? '-j' : '--jobs', DEFAULT_JOBS, { |
| 64 | + min: 1, |
| 65 | + max: 128, |
| 66 | + }); |
| 67 | + const timeout = integer( |
| 68 | + values, |
| 69 | + values.has('-t') ? '-t' : '--timeout', |
| 70 | + DEFAULT_TIMEOUT_MS, |
| 71 | + { min: 100, max: 120_000 }, |
| 72 | + ); |
| 73 | + |
| 74 | + const file = values.get('-f') ?? values.get('--file'); |
| 75 | + let raw: string; |
| 76 | + if (file) { |
| 77 | + raw = file === '-' ? await readStdin() : await readFile(file, 'utf8'); |
| 78 | + } else if (positional.length > 0) { |
| 79 | + raw = positional.join('\n'); |
| 80 | + } else if (!process.stdin.isTTY) { |
| 81 | + raw = await readStdin(); |
| 82 | + } else { |
| 83 | + process.stderr.write(USAGE); |
| 84 | + process.exit(1); |
| 85 | + } |
| 86 | + |
| 87 | + const names = normalizeNames(raw); |
| 88 | + if (names.length === 0) { |
| 89 | + process.stderr.write('domainfree: no valid domain names given\n'); |
| 90 | + process.exit(1); |
| 91 | + } |
| 92 | + |
| 93 | + const results = await checkMany(names, { jobs, timeout }); |
| 94 | + |
| 95 | + for (const result of results.slice().sort((a, b) => a.domain.localeCompare(b.domain))) { |
| 96 | + if (showAll) { |
| 97 | + const label = |
| 98 | + result.status === 'available' |
| 99 | + ? 'AVAILABLE' |
| 100 | + : result.status === 'taken' |
| 101 | + ? 'TAKEN' |
| 102 | + : `ERR:${result.code ?? 'timeout'}`; |
| 103 | + process.stdout.write(`${label} ${result.domain}\n`); |
| 104 | + } else if (result.status === 'available') { |
| 105 | + process.stdout.write(`${result.domain}\n`); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + const { available, taken, unknown } = summarize(results); |
| 110 | + if (!quiet) { |
| 111 | + const parts = [`${names.length} checked`, `${available} available`, `${taken} taken`]; |
| 112 | + if (unknown > 0) parts.push(`${unknown} unknown`); |
| 113 | + process.stderr.write(`${parts.join(' · ')}\n`); |
| 114 | + } |
| 115 | + |
| 116 | + process.exit(unknown > 0 ? 2 : 0); |
| 117 | + } catch (error) { |
| 118 | + if (error instanceof UsageError) { |
| 119 | + process.stderr.write(`domainfree: ${error.message}\n`); |
| 120 | + process.exit(1); |
| 121 | + } |
| 122 | + throw error; |
| 123 | + } |
| 124 | +} |
0 commit comments