Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

Commit

Permalink
✨ Add DNS module
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Nov 2, 2020
1 parent bcb3c88 commit 59a4a7c
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
23 changes: 23 additions & 0 deletions src/modules/dns/dns.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { MxRecord, NaptrRecord, SoaRecord, SrvRecord, AnyRecord } from 'dns';

export type RecordType =
| 'A'
| 'AAAA'
| 'ANY'
| 'CNAME'
| 'MX'
| 'NAPTR'
| 'NS'
| 'PTR'
| 'SOA'
| 'SRV'
| 'TXT';

export type RecordResult =
| Array<string>
| Array<MxRecord>
| Array<NaptrRecord>
| SoaRecord
| Array<SrvRecord>
| Array<Array<string>>
| Array<AnyRecord>;
27 changes: 26 additions & 1 deletion src/modules/dns/dns.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,29 @@
import { Injectable } from '@nestjs/common';
import dns from 'dns';
import { RecordType, RecordResult } from './dns.interface';

@Injectable()
export class DnsService {}
export class DnsService {
async lookup(
hostname: string,
recordType: RecordType,
): Promise<RecordResult> {
try {
return await this.unsafeLookup(hostname, recordType);
} catch (error) {
return [];
}
}

private unsafeLookup(
hostname: string,
recordType: RecordType,
): Promise<RecordResult> {
return new Promise((resolve, reject) => {
dns.resolve(hostname, recordType, (error, records) => {
if (error) return reject(error);
resolve(records);
});
});
}
}

0 comments on commit 59a4a7c

Please sign in to comment.