UNPKG

@tgwf/co2

Version:
112 lines (111 loc) 6.08 kB
"use strict"; 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 co2_exports = {}; __export(co2_exports, { CO2: () => CO2, default: () => co2_default }); module.exports = __toCommonJS(co2_exports); var import_byte = __toESM(require("./1byte.js")); var import_sustainable_web_design = __toESM(require("./sustainable-web-design.js")); var import_constants = require("./constants/index.js"); var import_helpers = require("./helpers/index.js"); class CO2 { constructor(options) { this.model = new import_sustainable_web_design.default(); if ((options == null ? void 0 : options.model) === "1byte") { this.model = new import_byte.default(); } else if ((options == null ? void 0 : options.model) === "swd") { this.model = new import_sustainable_web_design.default(); } else if (options == null ? void 0 : options.model) { throw new Error(`"${options.model}" is not a valid model. Please use "1byte" for the OneByte model, and "swd" for the Sustainable Web Design model. See https://developers.thegreenwebfoundation.org/co2js/models/ to learn more about the models available in CO2.js.`); } this._segment = (options == null ? void 0 : options.results) === "segment"; } perByte(bytes, green = false) { return this.model.perByte(bytes, green, this._segment); } perVisit(bytes, green = false) { var _a; if ((_a = this.model) == null ? void 0 : _a.perVisit) { return this.model.perVisit(bytes, green, this._segment); } else { throw new Error(`The perVisit() method is not supported in the model you are using. Try using perByte() instead. See https://developers.thegreenwebfoundation.org/co2js/methods/ to learn more about the methods available in CO2.js.`); } } perByteTrace(bytes, green = false, options = {}) { var _a, _b, _c, _d, _e, _f; let adjustments = {}; if (options) { adjustments = (0, import_helpers.parseOptions)(options); } return { co2: this.model.perByte(bytes, green, this._segment, adjustments), green, variables: { description: "Below are the variables used to calculate this CO2 estimate.", bytes, gridIntensity: { description: "The grid intensity (grams per kilowatt-hour) used to calculate this CO2 estimate.", network: ((_b = (_a = adjustments == null ? void 0 : adjustments.gridIntensity) == null ? void 0 : _a.network) == null ? void 0 : _b.value) ?? import_constants.GLOBAL_GRID_INTENSITY, dataCenter: green ? import_constants.RENEWABLES_GRID_INTENSITY : ((_d = (_c = adjustments == null ? void 0 : adjustments.gridIntensity) == null ? void 0 : _c.dataCenter) == null ? void 0 : _d.value) ?? import_constants.GLOBAL_GRID_INTENSITY, production: import_constants.GLOBAL_GRID_INTENSITY, device: ((_f = (_e = adjustments == null ? void 0 : adjustments.gridIntensity) == null ? void 0 : _e.device) == null ? void 0 : _f.value) ?? import_constants.GLOBAL_GRID_INTENSITY } } }; } perVisitTrace(bytes, green = false, options = {}) { var _a, _b, _c, _d, _e, _f, _g; if ((_a = this.model) == null ? void 0 : _a.perVisit) { let adjustments = {}; if (options) { adjustments = (0, import_helpers.parseOptions)(options); } return { co2: this.model.perVisit(bytes, green, this._segment, adjustments), green, variables: { description: "Below are the variables used to calculate this CO2 estimate.", bytes, gridIntensity: { description: "The grid intensity (grams per kilowatt-hour) used to calculate this CO2 estimate.", network: ((_c = (_b = adjustments == null ? void 0 : adjustments.gridIntensity) == null ? void 0 : _b.network) == null ? void 0 : _c.value) ?? import_constants.GLOBAL_GRID_INTENSITY, dataCenter: green ? import_constants.RENEWABLES_GRID_INTENSITY : ((_e = (_d = adjustments == null ? void 0 : adjustments.gridIntensity) == null ? void 0 : _d.dataCenter) == null ? void 0 : _e.value) ?? import_constants.GLOBAL_GRID_INTENSITY, production: import_constants.GLOBAL_GRID_INTENSITY, device: ((_g = (_f = adjustments == null ? void 0 : adjustments.gridIntensity) == null ? void 0 : _f.device) == null ? void 0 : _g.value) ?? import_constants.GLOBAL_GRID_INTENSITY }, dataReloadRatio: (adjustments == null ? void 0 : adjustments.dataReloadRatio) ?? 0.02, firstVisitPercentage: (adjustments == null ? void 0 : adjustments.firstVisitPercentage) ?? 0.75, returnVisitPercentage: (adjustments == null ? void 0 : adjustments.returnVisitPercentage) ?? 0.25 } }; } else { throw new Error(`The perVisitDetailed() method is not supported in the model you are using. Try using perByte() instead. See https://developers.thegreenwebfoundation.org/co2js/methods/ to learn more about the methods available in CO2.js.`); } } } var co2_default = CO2; //# sourceMappingURL=co2.js.map