@tgwf/co2
Version:
Work out the co2 of your digital services
109 lines (108 loc) • 3.84 kB
JavaScript
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var hosting_node_exports = {};
__export(hosting_node_exports, {
default: () => hosting_node_default
});
module.exports = __toCommonJS(hosting_node_exports);
var import_https = __toESM(require("https"));
var import_hosting_json = __toESM(require("./hosting-json.js"));
var import_hosting_json_node = __toESM(require("./hosting-json.node.js"));
var import_helpers = require("./helpers/index.js");
async function getBody(url, userAgentIdentifier) {
return new Promise(function(resolve, reject) {
const req = import_https.default.get(
url,
{ headers: (0, import_helpers.getApiRequestHeaders)(userAgentIdentifier) },
function(res) {
if (res.statusCode < 200 || res.statusCode >= 300) {
return reject(
new Error(
`Could not get info from: ${url}. Status Code: ${res.statusCode}`
)
);
}
const data = [];
res.on("data", (chunk) => {
data.push(chunk);
});
res.on("end", () => resolve(Buffer.concat(data).toString()));
}
);
req.end();
});
}
function check(domain, optionsOrDb, userAgentIdentifier) {
let db, options = {};
if (!db && Array.isArray(optionsOrDb)) {
db = optionsOrDb;
} else {
options = optionsOrDb;
if (userAgentIdentifier) {
options = { ...options, userAgentIdentifier };
}
db = optionsOrDb == null ? void 0 : optionsOrDb.db;
}
if (db && (options == null ? void 0 : options.verbose)) {
throw new Error("verbose mode cannot be used with a local lookup database");
}
if (db) {
return import_hosting_json.default.check(domain, db);
}
if (typeof domain === "string") {
return checkAgainstAPI(domain, options);
} else {
return checkDomainsAgainstAPI(domain, options);
}
}
async function checkAgainstAPI(domain, options = {}) {
const res = JSON.parse(
await getBody(
`https://api.thegreenwebfoundation.org/greencheck/${domain}`,
options.userAgentIdentifier
)
);
return options.verbose ? res : res.green;
}
async function checkDomainsAgainstAPI(domains, options = {}) {
try {
const allGreenCheckResults = JSON.parse(
await getBody(
`https://api.thegreenwebfoundation.org/v2/greencheckmulti/${JSON.stringify(
domains
)}`,
options.userAgentIdentifier
)
);
return options.verbose ? allGreenCheckResults : import_hosting_json.default.greenDomainsFromResults(allGreenCheckResults);
} catch (e) {
return options.verbose ? {} : [];
}
}
var hosting_node_default = {
check,
greendomains: import_hosting_json.default.greenDomainsFromResults,
loadJSON: import_hosting_json_node.default.loadJSON
};
//# sourceMappingURL=hosting-node.js.map