UNPKG

@tgwf/co2

Version:
92 lines (91 loc) 4.79 kB
"use strict"; import OneByte from "./1byte.js"; import SustainableWebDesign from "./sustainable-web-design.js"; import { GLOBAL_GRID_INTENSITY, RENEWABLES_GRID_INTENSITY } from "./constants/index.js"; import { parseOptions } from "./helpers/index.js"; class CO2 { constructor(options) { this.model = new SustainableWebDesign(); if ((options == null ? void 0 : options.model) === "1byte") { this.model = new OneByte(); } else if ((options == null ? void 0 : options.model) === "swd") { this.model = new SustainableWebDesign(); } 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, _g, _h, _i; let adjustments = {}; if (options) { adjustments = 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: (_c = (_b = (_a = adjustments == null ? void 0 : adjustments.gridIntensity) == null ? void 0 : _a.network) == null ? void 0 : _b.value) != null ? _c : GLOBAL_GRID_INTENSITY, dataCenter: green ? RENEWABLES_GRID_INTENSITY : (_f = (_e = (_d = adjustments == null ? void 0 : adjustments.gridIntensity) == null ? void 0 : _d.dataCenter) == null ? void 0 : _e.value) != null ? _f : GLOBAL_GRID_INTENSITY, production: GLOBAL_GRID_INTENSITY, device: (_i = (_h = (_g = adjustments == null ? void 0 : adjustments.gridIntensity) == null ? void 0 : _g.device) == null ? void 0 : _h.value) != null ? _i : GLOBAL_GRID_INTENSITY } } }; } perVisitTrace(bytes, green = false, options = {}) { var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m; if ((_a = this.model) == null ? void 0 : _a.perVisit) { let adjustments = {}; if (options) { adjustments = 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: (_d = (_c = (_b = adjustments == null ? void 0 : adjustments.gridIntensity) == null ? void 0 : _b.network) == null ? void 0 : _c.value) != null ? _d : GLOBAL_GRID_INTENSITY, dataCenter: green ? RENEWABLES_GRID_INTENSITY : (_g = (_f = (_e = adjustments == null ? void 0 : adjustments.gridIntensity) == null ? void 0 : _e.dataCenter) == null ? void 0 : _f.value) != null ? _g : GLOBAL_GRID_INTENSITY, production: GLOBAL_GRID_INTENSITY, device: (_j = (_i = (_h = adjustments == null ? void 0 : adjustments.gridIntensity) == null ? void 0 : _h.device) == null ? void 0 : _i.value) != null ? _j : GLOBAL_GRID_INTENSITY }, dataReloadRatio: (_k = adjustments == null ? void 0 : adjustments.dataReloadRatio) != null ? _k : 0.02, firstVisitPercentage: (_l = adjustments == null ? void 0 : adjustments.firstVisitPercentage) != null ? _l : 0.75, returnVisitPercentage: (_m = adjustments == null ? void 0 : adjustments.returnVisitPercentage) != null ? _m : 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; export { CO2, co2_default as default };