-
-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds support for verbose response when checking domains
- Loading branch information
Showing
11 changed files
with
334 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
"use strict"; | ||
|
||
/** | ||
* Check if a string or array of domains has been provided | ||
* @param {string|array} domain - The domain to check, or an array of domains to be checked. | ||
*/ | ||
async function check(domain, db) { | ||
// is it a single domain or an array of them? | ||
if (typeof domain === "string") { | ||
return checkInJSON(domain, db); | ||
} else { | ||
return checkDomainsInJSON(domain, db); | ||
} | ||
} | ||
|
||
/** | ||
* Check if a domain is hosted by a green web host by querying the database. | ||
* @param {string} domain - The domain to check. | ||
* @param {object} db - The database to check against. | ||
* @returns {boolean} - A boolean indicating whether the domain is hosted by a green web host. | ||
*/ | ||
function checkInJSON(domain, db) { | ||
if (db.indexOf(domain) > -1) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
/** | ||
* Extract the green domains from the results of a green check. | ||
* @param {object} greenResults - The results of a green check. | ||
* @returns {array} - An array of domains that are hosted by a green web host. | ||
*/ | ||
function greenDomainsFromResults(greenResults) { | ||
const entries = Object.entries(greenResults); | ||
const greenEntries = entries.filter(([key, val]) => val.green); | ||
|
||
return greenEntries.map(([key, val]) => val.url); | ||
} | ||
|
||
/** | ||
* Check if an array of domains is hosted by a green web host by querying the database. | ||
* @param {array} domains - An array of domains to check. | ||
* @param {object} db - The database to check against. | ||
* @returns {array} - An array of domains that are hosted by a green web host. | ||
*/ | ||
function checkDomainsInJSON(domains, db) { | ||
let greenDomains = []; | ||
|
||
for (let domain of domains) { | ||
if (db.indexOf(domain) > -1) { | ||
greenDomains.push(domain); | ||
} | ||
} | ||
return greenDomains; | ||
} | ||
|
||
/** | ||
* Find the provided information a string or array of domains | ||
* @param {string|array} domain - The domain to check, or an array of domains to be checked. | ||
*/ | ||
function find(domain, db) { | ||
// is it a single domain or an array of them? | ||
if (typeof domain === "string") { | ||
return findInJSON(domain, db); | ||
} else { | ||
return findDomainsInJSON(domain, db); | ||
} | ||
} | ||
|
||
/** | ||
* Check if a domain is hosted by a green web host by querying the database. | ||
* @param {string} domain - The domain to check. | ||
* @param {object} db - The database to check against. | ||
* @returns {object} - An object representing the domain provided host information. | ||
*/ | ||
function findInJSON(domain, db) { | ||
if (db.indexOf(domain) > -1) { | ||
return domain; | ||
} | ||
return { | ||
url: domain, | ||
green: false, | ||
}; | ||
} | ||
|
||
/** | ||
* Check if an array of domains is hosted by a green web host by querying the database. | ||
* @param {array} domains - An array of domains to check. | ||
* @param {object} db - The database to check against. | ||
* @returns {array} - A dictionary of domain to provided host information. | ||
*/ | ||
function findDomainsInJSON(domains, db) { | ||
const result = {}; | ||
for (let domain of domains) { | ||
result[domain] = findInJSON(domain, db); | ||
} | ||
return result; | ||
} | ||
|
||
module.exports = { | ||
check, | ||
greenDomainsFromResults, | ||
find, | ||
}; |
Oops, something went wrong.