performances: async loading
Lint / Oxlint (push) Successful in 52s
Lint / TypeScript (push) Successful in 55s
Lint / ESLint (push) Successful in 59s
Deploy / Build (push) Successful in 1m3s
Deploy / Deploy to Stapler (push) Successful in 6m4s

This commit is contained in:
2026-06-26 11:53:44 +02:00
parent c41bdbec06
commit e40a082c2a
6 changed files with 73 additions and 80 deletions
+54 -38
View File
@@ -1,11 +1,9 @@
import rawTldList from "@/data/tld.txt?raw";
import englishWords from "@/data/english.txt?raw";
import frenchWords from "@/data/french.txt?raw";
const rawTldList = () => import("@/data/tld.txt?raw");
const englishWords = () => import("@/data/english.txt?raw");
const frenchWords = () => import("@/data/french.txt?raw");
import { randomElement, shuffled } from "./random";
import type { Domain } from "@/types";
const TLD_LIST: string[] = rawTldList.trim().split("\n");
export class DomainFactory {
error: string | null = null;
words: Record<string, string[]> = {};
@@ -13,10 +11,20 @@ export class DomainFactory {
history: Domain[] = [];
tldList: string[] = [];
init() {
this.words.fr = this.initWords(frenchWords);
this.words.en = this.initWords(englishWords);
this.tldList = TLD_LIST;
async init(lang: string | null = null) {
if (lang && !this.words[lang]) {
switch (lang) {
case "en":
this.words.en = this.initWords((await englishWords()).default);
break;
case "fr":
this.words.fr = this.initWords((await frenchWords()).default);
break;
}
}
if (this.tldList.length == 0) {
this.tldList = (await rawTldList()).default.trim().split("\n");
}
}
escapeWord(word: string): string {
@@ -64,20 +72,24 @@ export class DomainFactory {
next(lang: string): Promise<Domain> {
return new Promise((resolve, reject) => {
let k = 10000;
while (k > 0) {
k--;
const word = this.randomWord(lang);
const domain = this.randomDomain(word);
if (domain) {
const output = this.newDomain(domain);
this.history.push(output);
resolve(output);
return;
}
}
this.error = "Could not generate a valid domain";
reject(new Error(this.error));
this.init(lang)
.then(() => {
let k = 10000;
while (k > 0) {
k--;
const word = this.randomWord(lang);
const domain = this.randomDomain(word);
if (domain) {
const output = this.newDomain(domain);
this.history.push(output);
resolve(output);
return;
}
}
this.error = "Could not generate a valid domain";
reject(new Error(this.error));
})
.catch(reject);
});
}
@@ -97,22 +109,26 @@ export class DomainFactory {
allFromWord(word: string): Promise<Domain[]> {
const escapedWord = this.escapeWord(word.replace(/ /gm, "-"));
return new Promise((resolve, reject) => {
const domains = [];
for (const tld of shuffled(this.tldList.slice())) {
const tldEscaped: string = tld.replace(".", "");
if (escapedWord.endsWith(tldEscaped) && escapedWord.length >= tldEscaped.length + 2) {
const domain = `${escapedWord.substring(0, escapedWord.length - tldEscaped.length)}.${tld}`;
if (!domain.includes("-.")) {
const output = this.newDomain(domain);
this.history.push(output);
domains.push(output);
this.init()
.then(() => {
const domains = [];
for (const tld of shuffled(this.tldList.slice())) {
const tldEscaped: string = tld.replace(".", "");
if (escapedWord.endsWith(tldEscaped) && escapedWord.length >= tldEscaped.length + 2) {
const domain = `${escapedWord.substring(0, escapedWord.length - tldEscaped.length)}.${tld}`;
if (!domain.includes("-.")) {
const output = this.newDomain(domain);
this.history.push(output);
domains.push(output);
}
}
}
}
}
if (domains.length) {
resolve(domains);
}
reject(new Error("Could not generate a valid domain"));
if (domains.length) {
resolve(domains);
}
reject(new Error("Could not generate a valid domain"));
})
.catch(reject);
});
}
}