From c4498d812d2fc2a32cb991232064709b10a480f3 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Thu, 18 Aug 2022 10:49:10 -0400 Subject: [PATCH] *Initial version --- LICENSE | 2 +- package.json | 6 ++++++ src/index.ts | 2 ++ src/resolverModule.ts | 34 ++++++++++++++++++++++++++++++++++ src/types.ts | 31 +++++++++++++++++++++++++++++++ src/util.ts | 30 ++++++++++++++++++++++++++++++ tsconfig.json | 23 +++++++++++++++++++++++ 7 files changed, 127 insertions(+), 1 deletion(-) create mode 100644 package.json create mode 100644 src/index.ts create mode 100644 src/resolverModule.ts create mode 100644 src/types.ts create mode 100644 src/util.ts create mode 100644 tsconfig.json diff --git a/LICENSE b/LICENSE index 13a5b6e..f161a44 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2022 Lume Web +Copyright (c) 2022 Hammmer Technologies LLC Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/package.json b/package.json new file mode 100644 index 0000000..6d93821 --- /dev/null +++ b/package.json @@ -0,0 +1,6 @@ +{ + "name": "@lumeweb/resolver-common", + "version": "0.1.0", + "description": "Common code and interfaces for the DNS resolver modules", + "main": "dist/index.js" +} diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..3e7483c --- /dev/null +++ b/src/index.ts @@ -0,0 +1,2 @@ +export * from "./types.js"; +export * from "./resolverModule.js"; diff --git a/src/resolverModule.ts b/src/resolverModule.ts new file mode 100644 index 0000000..1ba130a --- /dev/null +++ b/src/resolverModule.ts @@ -0,0 +1,34 @@ +import type { DNSResult, ResolverOptions } from "./types.js"; +import { getTld } from "./util.js"; + +export default interface ResolverModule { + constructor(resolver: any): void; + + resolve( + domain: string, + options: ResolverOptions, + bypassCache: boolean + ): Promise; +} + +export abstract class AbstractResolverModule { + protected resolver: any; + + constructor(resolver: any) { + this.resolver = resolver; + } + + abstract resolve( + input: string, + options: ResolverOptions, + force: boolean + ): Promise; + + getSupportedTlds(): string[] { + return []; + } + + isTldSupported(domain: string): boolean { + return this.getSupportedTlds().includes(getTld(domain)); + } +} diff --git a/src/types.ts b/src/types.ts new file mode 100644 index 0000000..fe22c90 --- /dev/null +++ b/src/types.ts @@ -0,0 +1,31 @@ +export interface ResolverOptions { + type: any; + customType?: string; + records: any[]; + options: any; +} + +export interface DNSResult { + records: any[]; + error?: Error; +} + +export interface DNSRecord { + type: string; + customType?: string; + value: string; +} + +const DNS_RECORD_TYPE = { + A: "A", + CNAME: "CNAME", + NS: "NS", + CONTENT: "CONTENT", + TEXT: "TEXT", + ALL: "ALL", + CUSTOM: "CUSTOM", +}; + +Object.freeze(DNS_RECORD_TYPE); + +export { DNS_RECORD_TYPE }; diff --git a/src/util.ts b/src/util.ts new file mode 100644 index 0000000..e9e55d9 --- /dev/null +++ b/src/util.ts @@ -0,0 +1,30 @@ +export function getTld(domain: string): string { + if (domain.includes(".")) { + domain = domain.split(".")[domain.split(".").length - 1]; + } + + return domain; +} + +export function getSld(domain: string): string { + if (domain.includes(".")) { + domain = domain + .split(".") + .slice(0, domain.split(".").length - 1) + .join("."); + } + + return domain; +} + +export function isIp(ip: string) { + return /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test( + ip + ); +} + +export function isDomain(domain: string) { + return /(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z0-9][a-z0-9-]{0,61}[a-z0-9]/.test( + domain + ); +} diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..b2f765c --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "declaration": true, + "strict": true, + "module": "esnext", + "target": "esnext", + "esModuleInterop": true, + "sourceMap": false, + "rootDir": "src", + "outDir": "dist", + "typeRoots": [ + "node_modules/@types" + ], + "moduleResolution": "node", + "declarationMap": true, + "declarationDir": "dist", + "emitDeclarationOnly": false, + "allowJs": true + }, + "include": [ + "src" + ] +}