Skip to content

Commit

Permalink
fix: multiple problems in WhoisData build output
Browse files Browse the repository at this point in the history
  • Loading branch information
mwguerra committed May 29, 2023
1 parent b066fc7 commit 51bbf75
Show file tree
Hide file tree
Showing 6 changed files with 610 additions and 281 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Import the package and use it!
Javascript example:
```javascript
// Javascript
const whoisClient = require("@mwguerra/whois-client")
const { whoisClient } = require("@mwguerra/whois-client")

async function whoisLookup(hostname) {
return await whoisClient(hostname)
Expand All @@ -34,7 +34,7 @@ whoisLookup("https://www.google.com")
Typescript example:
```typescript
// Typescript
import whoisClient from "@mwguerra/rdap-client"
import { whoisClient } from "@mwguerra/rdap-client"

const whoisLookup = async (hostname) => await whoisClient(hostname)

Expand Down
6 changes: 5 additions & 1 deletion src/helpers/DateHelper.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
class DateHelper {
public getDateFromString(dateString: string): Date | null {
public getDateFromString(dateString: string | null): Date | null {
if (!dateString) {
return null;
}

const normalizedDate = this.normalizeDateString(dateString);
return normalizedDate ? new Date(normalizedDate) : null;
}
Expand Down
2 changes: 1 addition & 1 deletion src/libraries/WhoisClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ class WhoisClient {
}
}

console.log(`:: [${new Date().toISOString()}] Domain ${sanitizedDomainName} retrieved with ${testOrder[i]}Connection.`)
// console.log(`:: [${new Date().toISOString()}] Domain ${sanitizedDomainName} retrieved with ${testOrder[i]}Connection.`)
return response
}

Expand Down
65 changes: 39 additions & 26 deletions src/libraries/WhoisData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,20 @@ interface WhoisResponse {
}

class WhoisData {
protected rawData: string;
protected parsedData: object | undefined;
protected whoisResponse: any;
protected rawData: string | undefined;
protected whoisResponse: { [key: string]: string } | undefined;
protected currentServer: any;

constructor(rawData: string) {
this.rawData = rawData
constructor(data: string | { [key: string]: string }) {
this.rawData = (typeof data === 'string') ? data : undefined
this.whoisResponse = (typeof data === 'object')? data : undefined
}

public parse() {
if (!this.rawData || typeof this.rawData !== 'string') {
throw new Error('rawData is missing or is not a string');
}

const decodedRawData: string = decode(this.rawData)

let result: { [key: string]: string } = {};
Expand Down Expand Up @@ -58,31 +62,49 @@ class WhoisData {
}
});

this.parsedData = result;
this.whoisResponse = result;

return this;
}

public getParsed() {
return this.parsedData;
if (!this.whoisResponse || typeof this.whoisResponse !== 'object') {
throw new Error('whoisResponse is missing or is not an object. Run the parser first.');
}
return this.whoisResponse;
}

protected fetchData(keyIncludes: string[], processor?: (data: any) => any) {
for (const server of this.whoisResponse) {
this.currentServer = server; // Set current server here
const dataKeys = Object.keys(server?.data);
const targetKeys = dataKeys.filter(key => keyIncludes.some(inclusion => key.includes(inclusion)));

for (const key of targetKeys) {
const data = server.data[key];
if (data) {
return processor ? processor(data) : data;
}
if (!this.whoisResponse || typeof this.whoisResponse !== 'object') {
throw new Error('whoisResponse is missing or is not an object');
}

// Check for the exact key match first
for (const key of keyIncludes) {
const exactMatchData = this.whoisResponse[key];
if (exactMatchData) {
return processor ? processor(exactMatchData) : exactMatchData;
}
}

// If no exact match found, perform partial key match
const whoisResponseEntries = Object.entries(this.whoisResponse || {});
for (const [key, data] of whoisResponseEntries) {
if (keyIncludes.some(inclusion => key.includes(inclusion)) && data) {
return processor ? processor(data) : data;
}
}
return null;
}

protected isBrDomain() {
if (!this.whoisResponse || typeof this.whoisResponse !== 'object') {
throw new Error('whoisResponse is missing or is not an object');
}

return this.whoisResponse?.domainName?.endsWith('.br') || this.whoisResponse?.domain?.endsWith('.br') || false;
}

protected getExpirationDate() {
return this.fetchData(['xpir'], dateFromWhois => new DateHelper().getDateFromString(dateFromWhois));
}
Expand Down Expand Up @@ -111,15 +133,6 @@ class WhoisData {
return this.fetchData(['techC']) || (this.isBrDomain() ? this.currentServer.data.techC : null);
}

protected isBrDomain() {
for (const server of this.whoisResponse) {
if (server.data.domain?.endsWith('.br')) {
return true;
}
}
return false;
}

public buildResponse(): WhoisResponse {
if (!this.whoisResponse) {
return {
Expand Down
Loading

0 comments on commit 51bbf75

Please sign in to comment.