website-carbon-meter
Version:
Tracks the carbon emissions of a website as live metrics. Based on CO2.js and the actual grid intensity.
704 lines (703 loc) • 32.5 kB
JavaScript
var re = (r) => {
throw TypeError(r);
};
var Z = (r, e, t) => e.has(r) || re("Cannot " + t);
var g = (r, e, t) => (Z(r, e, "read from private field"), t ? t.call(r) : e.get(r)), R = (r, e, t) => e.has(r) ? re("Cannot add the same private member more than once") : e instanceof WeakSet ? e.add(r) : e.set(r, t), C = (r, e, t, n) => (Z(r, e, "write to private field"), n ? n.call(r, t) : e.set(r, t), t), T = (r, e, t) => (Z(r, e, "access private method"), t);
const J = 4883333333333333e-25;
class we {
constructor(e) {
this.allowRatings = !1, this.options = e, this.KWH_PER_BYTE_FOR_NETWORK = J;
}
perByte(e, t) {
if (e < 1)
return 0;
if (t) {
const a = e * 72e-12 * 0, i = e * J * 475;
return a + i;
}
const n = 72e-12 + J;
return e * n * 519;
}
}
var ne = we;
const Se = 1e3 * 1e3 * 1e3;
var Q = {
GIGABYTE: Se
};
const Be = { AFG: 132.53, AFRICA: 547.83, ALB: 24.29, DZA: 634.61, ASM: 611.11, AGO: 174.73, ATG: 611.11, ARG: 353.96, ARM: 264.54, ABW: 561.22, ASEAN: 570.43, ASIA: 591.02, AUS: 556.3, AUT: 110.14, AZE: 671.39, BHS: 660.1, BHR: 904.62, BGD: 691.41, BRB: 605.51, BLR: 441.74, BEL: 138.11, BLZ: 225.81, BEN: 584.07, BTN: 23.33, BOL: 531.69, BIH: 600.94, BWA: 847.91, BRA: 96.4, BRN: 893.91, BGR: 335.33, BFA: 467.53, BDI: 250, CPV: 558.14, KHM: 417.71, CMR: 305.42, CAN: 165.15, CYM: 642.86, CAF: 0, TCD: 628.57, CHL: 291.11, CHN: 583.61, COL: 259.51, COM: 642.86, COG: 700, COD: 24.46, COK: 250, CRI: 53.38, CIV: 393.89, HRV: 202.68, CUB: 637.61, CYP: 526.02, CZE: 449.72, DNK: 151.65, DJI: 692.31, DMA: 529.41, DOM: 578.41, ECU: 166.91, EGY: 574.04, SLV: 224.76, GNQ: 591.84, ERI: 631.58, EST: 416.67, SWZ: 172.41, ETH: 24.64, EU: 243.55, EUROPE: 302.09, FLK: 500, FRO: 404.76, FJI: 288.46, FIN: 79.39, FRA: 56.02, GUF: 217.82, PYF: 442.86, G20: 477.79, G7: 341.49, GAB: 491.6, GMB: 666.67, GEO: 167.59, DEU: 381.41, GHA: 484, GRC: 336.57, GRL: 178.57, GRD: 640, GLP: 500, GUM: 622.86, GTM: 328.27, GIN: 236.84, GNB: 625, GUY: 640.35, HTI: 567.31, HND: 282.27, HKG: 699.5, HUN: 204.01, ISL: 27.68, IND: 713.01, IDN: 682.43, IRN: 641.73, IRQ: 688.81, IRL: 283.71, ISR: 582.93, ITA: 330.72, JAM: 555.56, JPN: 493.59, JOR: 540.92, KAZ: 821.9, KEN: 71.2, KIR: 666.67, XKX: 894.65, KWT: 649.16, KGZ: 147.29, LAO: 265.51, "LATIN AMERICA AND CARIBBEAN": 259.77, LVA: 123.99, LBN: 599.01, LSO: 20, LBR: 227.85, LBY: 818.69, LTU: 160.07, LUX: 105.26, MAC: 448.98, MDG: 436.44, MWI: 66.67, MYS: 607.88, MDV: 611.77, MLI: 408, MLT: 444.03, MTQ: 523.18, MRT: 464.71, MUS: 632.48, MEX: 492.34, "MIDDLE EAST": 643.04, MDA: 643.46, MNG: 775.31, MNE: 418.09, MSR: 1e3, MAR: 624.45, MOZ: 135.65, MMR: 440.37, NAM: 59.26, NRU: 750, NPL: 24.44, NLD: 268.48, NCL: 660.58, NZL: 112.76, NIC: 265.12, NER: 670.89, NGA: 523.25, "NORTH AMERICA": 343.03, PRK: 389.59, MKD: 539.55, NOR: 30.05, OCEANIA: 495.74, OECD: 341.25, OMN: 564.55, PAK: 440.61, PSE: 516.13, PAN: 161.68, PNG: 507.25, PRY: 24.31, PER: 266.48, POL: 661.93, PRT: 164.86, PRI: 677.96, QAT: 602.5, REU: 572.82, ROU: 238.65, RUS: 445.02, RWA: 316.33, KNA: 636.36, LCA: 666.67, SPM: 600, VCT: 529.41, WSM: 473.68, STP: 642.86, SAU: 696.31, SEN: 511.6, SRB: 648.2, SYC: 564.52, SLE: 50, SGP: 470.87, SVK: 116.77, SVN: 230.87, SLB: 700, SOM: 578.95, ZAF: 709.69, KOR: 432.11, SSD: 629.03, ESP: 172.2, LKA: 509.78, SDN: 263.16, SUR: 349.28, SWE: 40.77, CHE: 29.56, SYR: 701.66, TWN: 644.36, TJK: 116.86, TZA: 339.25, THA: 549.85, PHL: 610.74, TGO: 443.18, TON: 625, TTO: 681.53, TUN: 563.96, TUR: 464.59, TKM: 1306.03, TCA: 653.85, UGA: 44.53, UKR: 256.21, ARE: 492.7, GBR: 228.25, USA: 369.53, URY: 128.79, UZB: 1167.6, VUT: 571.43, VEN: 185.8, VNM: 472.47, VGB: 647.06, VIR: 632.35, WORLD: 481.6, YEM: 566.1, ZMB: 111.97, ZWE: 297.87 }, pe = "average";
var k = { data: Be, type: pe };
const Ge = 0.81, ae = 0.52, ie = 0.14, se = 0.15, oe = 0.19, O = k.data.WORLD, De = 50, W = 0.75, K = 0.25, U = 0.02, _e = {
OPERATIONAL_KWH_PER_GB_DATACENTER: 0.055,
OPERATIONAL_KWH_PER_GB_NETWORK: 0.059,
OPERATIONAL_KWH_PER_GB_DEVICE: 0.08,
EMBODIED_KWH_PER_GB_DATACENTER: 0.012,
EMBODIED_KWH_PER_GB_NETWORK: 0.013,
EMBODIED_KWH_PER_GB_DEVICE: 0.081,
GLOBAL_GRID_INTENSITY: 494
}, Ve = {
FIFTH_PERCENTILE: 0.095,
TENTH_PERCENTILE: 0.186,
TWENTIETH_PERCENTILE: 0.341,
THIRTIETH_PERCENTILE: 0.493,
FORTIETH_PERCENTILE: 0.656,
FIFTIETH_PERCENTILE: 0.846
}, N = {
FIFTH_PERCENTILE: 0.04,
TENTH_PERCENTILE: 0.079,
TWENTIETH_PERCENTILE: 0.145,
THIRTIETH_PERCENTILE: 0.209,
FORTIETH_PERCENTILE: 0.278,
FIFTIETH_PERCENTILE: 0.359
}, Le = _e.GLOBAL_GRID_INTENSITY, B = (r) => parseFloat(r.toFixed(2)), P = (r, e) => r <= e;
function Te(r = {}, e = 3, t = !1) {
const n = e === 4 ? Le : O;
if (typeof r != "object")
throw new Error("Options must be an object");
const a = {};
function i(c, o) {
var u, s;
o || o === 0 ? typeof o == "object" ? (k.data[(u = o.country) == null ? void 0 : u.toUpperCase()] || (console.warn(
`"${o.country}" is not a valid country. Please use a valid 3 digit ISO 3166 country code.
See https://developers.thegreenwebfoundation.org/co2js/data/ for more information.
Falling back to global average grid intensity.`
), a.gridIntensity[c] = {
value: n
}), a.gridIntensity[c] = {
country: o.country,
value: parseFloat(
k.data[(s = o.country) == null ? void 0 : s.toUpperCase()]
)
}) : typeof o == "number" ? a.gridIntensity[c] = {
value: o
} : (a.gridIntensity[c] = {
value: n
}, console.warn(
`The ${c} grid intensity must be a number or an object. You passed in a ${typeof o}.
Falling back to global average grid intensity.`
)) : a.gridIntensity[c] = {
value: n
};
}
if (r != null && r.gridIntensity) {
a.gridIntensity = {};
const { device: c, dataCenter: o, network: u } = r.gridIntensity;
i("device", c), i("dataCenter", o), i("network", u);
} else
a.gridIntensity = {
device: { value: n },
dataCenter: { value: n },
network: { value: n }
};
return r != null && r.greenHostingFactor || r.greenHostingFactor === 0 && e === 4 ? typeof r.greenHostingFactor == "number" ? r.greenHostingFactor >= 0 && r.greenHostingFactor <= 1 ? a.greenHostingFactor = r.greenHostingFactor : (a.greenHostingFactor = 0, console.warn(
`The returnVisitPercentage option must be a number between 0 and 1. You passed in ${r.returnVisitPercentage}.
Falling back to default value.`
)) : (a.greenHostingFactor = 0, console.warn(
`The returnVisitPercentage option must be a number. You passed in a ${typeof r.returnVisitPercentage}.
Falling back to default value.`
)) : e === 4 && (a.greenHostingFactor = 0), t && (a.greenHostingFactor = 1), a;
}
function Me(r = {}, e = 3, t = !1) {
if (typeof r != "object")
throw new Error("Options must be an object");
const n = Te(r, e, t);
return r != null && r.dataReloadRatio || r.dataReloadRatio === 0 ? typeof r.dataReloadRatio == "number" ? r.dataReloadRatio >= 0 && r.dataReloadRatio <= 1 ? n.dataReloadRatio = r.dataReloadRatio : (n.dataReloadRatio = e === 3 ? U : 0, console.warn(
`The dataReloadRatio option must be a number between 0 and 1. You passed in ${r.dataReloadRatio}.
Falling back to default value.`
)) : (n.dataReloadRatio = e === 3 ? U : 0, console.warn(
`The dataReloadRatio option must be a number. You passed in a ${typeof r.dataReloadRatio}.
Falling back to default value.`
)) : (n.dataReloadRatio = e === 3 ? U : 0, console.warn(
`The dataReloadRatio option must be a number. You passed in a ${typeof r.dataReloadRatio}.
Falling back to default value.`
)), r != null && r.firstVisitPercentage || r.firstVisitPercentage === 0 ? typeof r.firstVisitPercentage == "number" ? r.firstVisitPercentage >= 0 && r.firstVisitPercentage <= 1 ? n.firstVisitPercentage = r.firstVisitPercentage : (n.firstVisitPercentage = e === 3 ? W : 1, console.warn(
`The firstVisitPercentage option must be a number between 0 and 1. You passed in ${r.firstVisitPercentage}.
Falling back to default value.`
)) : (n.firstVisitPercentage = e === 3 ? W : 1, console.warn(
`The firstVisitPercentage option must be a number. You passed in a ${typeof r.firstVisitPercentage}.
Falling back to default value.`
)) : (n.firstVisitPercentage = e === 3 ? W : 1, console.warn(
`The firstVisitPercentage option must be a number. You passed in a ${typeof r.firstVisitPercentage}.
Falling back to default value.`
)), r != null && r.returnVisitPercentage || r.returnVisitPercentage === 0 ? typeof r.returnVisitPercentage == "number" ? r.returnVisitPercentage >= 0 && r.returnVisitPercentage <= 1 ? n.returnVisitPercentage = r.returnVisitPercentage : (n.returnVisitPercentage = e === 3 ? K : 0, console.warn(
`The returnVisitPercentage option must be a number between 0 and 1. You passed in ${r.returnVisitPercentage}.
Falling back to default value.`
)) : (n.returnVisitPercentage = e === 3 ? K : 0, console.warn(
`The returnVisitPercentage option must be a number. You passed in a ${typeof r.returnVisitPercentage}.
Falling back to default value.`
)) : (n.returnVisitPercentage = e === 3 ? K : 0, console.warn(
`The returnVisitPercentage option must be a number. You passed in a ${typeof r.returnVisitPercentage}.
Falling back to default value.`
)), n;
}
function Ie(r = "") {
return { "User-Agent": `co2js/0.16.4 ${r}` };
}
function Ce(r, e) {
let {
FIFTH_PERCENTILE: t,
TENTH_PERCENTILE: n,
TWENTIETH_PERCENTILE: a,
THIRTIETH_PERCENTILE: i,
FORTIETH_PERCENTILE: c,
FIFTIETH_PERCENTILE: o
} = Ve;
return e === 4 && (t = N.FIFTH_PERCENTILE, n = N.TENTH_PERCENTILE, a = N.TWENTIETH_PERCENTILE, i = N.THIRTIETH_PERCENTILE, c = N.FORTIETH_PERCENTILE, o = N.FIFTIETH_PERCENTILE), P(r, t) ? "A+" : P(r, n) ? "A" : P(r, a) ? "B" : P(r, i) ? "C" : P(r, c) ? "D" : P(r, o) ? "E" : "F";
}
var Fe = Object.defineProperty, He = Object.defineProperties, We = Object.getOwnPropertyDescriptors, le = Object.getOwnPropertySymbols, Ke = Object.prototype.hasOwnProperty, Ue = Object.prototype.propertyIsEnumerable, ce = (r, e, t) => e in r ? Fe(r, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : r[e] = t, M = (r, e) => {
for (var t in e || (e = {}))
Ke.call(e, t) && ce(r, t, e[t]);
if (le)
for (var t of le(e))
Ue.call(e, t) && ce(r, t, e[t]);
return r;
}, F = (r, e) => He(r, We(e));
let ke = class {
constructor(e) {
this.allowRatings = !0, this.options = e, this.version = 3;
}
energyPerByteByComponent(e) {
const n = e / Q.GIGABYTE * Ge;
return {
consumerDeviceEnergy: n * ae,
networkEnergy: n * ie,
productionEnergy: n * oe,
dataCenterEnergy: n * se
};
}
co2byComponent(e, t = O, n = {}) {
let a = O, i = O, c = O, o = O;
if (n != null && n.gridIntensity) {
const { device: s, network: l, dataCenter: d } = n.gridIntensity;
(s != null && s.value || (s == null ? void 0 : s.value) === 0) && (a = s.value), (l != null && l.value || (l == null ? void 0 : l.value) === 0) && (i = l.value), (d != null && d.value || (d == null ? void 0 : d.value) === 0) && (c = d.value);
}
t === !0 && (c = De);
const u = {};
for (const [s, l] of Object.entries(e))
s.startsWith("dataCenterEnergy") ? u[s.replace("Energy", "CO2")] = l * c : s.startsWith("consumerDeviceEnergy") ? u[s.replace("Energy", "CO2")] = l * a : s.startsWith("networkEnergy") ? u[s.replace("Energy", "CO2")] = l * i : u[s.replace("Energy", "CO2")] = l * o;
return u;
}
perByte(e, t = !1, n = !1, a = !1, i = {}) {
e < 1 && (e = 0);
const c = this.energyPerByteByComponent(e, i);
if (typeof t != "boolean")
throw new Error(
`perByte expects a boolean for the carbon intensity value. Received: ${t}`
);
const o = this.co2byComponent(
c,
t,
i
), s = Object.values(o).reduce(
(d, E) => d + E
);
let l = null;
return a && (l = this.ratingScale(s)), n ? a ? F(M({}, o), {
total: s,
rating: l
}) : F(M({}, o), { total: s }) : a ? { total: s, rating: l } : s;
}
perVisit(e, t = !1, n = !1, a = !1, i = {}) {
const c = this.energyPerVisitByComponent(e, i);
if (typeof t != "boolean")
throw new Error(
`perVisit expects a boolean for the carbon intensity value. Received: ${t}`
);
const o = this.co2byComponent(
c,
t,
i
), s = Object.values(o).reduce(
(d, E) => d + E
);
let l = null;
return a && (l = this.ratingScale(s)), n ? a ? F(M({}, o), {
total: s,
rating: l
}) : F(M({}, o), { total: s }) : a ? { total: s, rating: l } : s;
}
energyPerByte(e) {
const t = this.energyPerByteByComponent(e);
return Object.values(t).reduce(
(a, i) => a + i
);
}
energyPerVisitByComponent(e, t = {}, n = W, a = K, i = U) {
(t.dataReloadRatio || t.dataReloadRatio === 0) && (i = t.dataReloadRatio), (t.firstVisitPercentage || t.firstVisitPercentage === 0) && (n = t.firstVisitPercentage), (t.returnVisitPercentage || t.returnVisitPercentage === 0) && (a = t.returnVisitPercentage);
const c = this.energyPerByteByComponent(e), o = {};
Object.values(c);
for (const [u, s] of Object.entries(c))
o[`${u} - first`] = s * n, o[`${u} - subsequent`] = s * a * i;
return o;
}
energyPerVisit(e) {
let t = 0, n = 0;
const a = Object.entries(
this.energyPerVisitByComponent(e)
);
for (const [i, c] of a)
i.indexOf("first") > 0 && (t += c);
for (const [i, c] of a)
i.indexOf("subsequent") > 0 && (n += c);
return t + n;
}
emissionsPerVisitInGrams(e, t = O) {
return B(e * t);
}
annualEnergyInKwh(e, t = 1e3) {
return e * t * 12;
}
annualEmissionsInGrams(e, t = 1e3) {
return e * t * 12;
}
annualSegmentEnergy(e) {
return {
consumerDeviceEnergy: B(e * ae),
networkEnergy: B(e * ie),
dataCenterEnergy: B(e * se),
productionEnergy: B(e * oe)
};
}
ratingScale(e) {
return Ce(e, this.version);
}
};
var x = ke, Ye = Object.defineProperty, $e = Object.defineProperties, je = Object.getOwnPropertyDescriptors, ue = Object.getOwnPropertySymbols, Ze = Object.prototype.hasOwnProperty, Je = Object.prototype.propertyIsEnumerable, de = (r, e, t) => e in r ? Ye(r, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : r[e] = t, y = (r, e) => {
for (var t in e || (e = {}))
Ze.call(e, t) && de(r, t, e[t]);
if (ue)
for (var t of ue(e))
Je.call(e, t) && de(r, t, e[t]);
return r;
}, p = (r, e) => $e(r, je(e));
const {
OPERATIONAL_KWH_PER_GB_DATACENTER: xe,
OPERATIONAL_KWH_PER_GB_NETWORK: Xe,
OPERATIONAL_KWH_PER_GB_DEVICE: qe,
EMBODIED_KWH_PER_GB_DATACENTER: Qe,
EMBODIED_KWH_PER_GB_NETWORK: ze,
EMBODIED_KWH_PER_GB_DEVICE: et,
GLOBAL_GRID_INTENSITY: v
} = _e;
function Ee(r, e) {
const t = r.dataCenter + r.network + r.device, n = e.dataCenter + e.network + e.device, a = r.dataCenter + e.dataCenter, i = r.network + e.network, c = r.device + e.device;
return {
dataCenterOperationalCO2e: r.dataCenter,
networkOperationalCO2e: r.network,
consumerDeviceOperationalCO2e: r.device,
dataCenterEmbodiedCO2e: e.dataCenter,
networkEmbodiedCO2e: e.network,
consumerDeviceEmbodiedCO2e: e.device,
totalEmbodiedCO2e: n,
totalOperationalCO2e: t,
dataCenterCO2e: a,
networkCO2e: i,
consumerDeviceCO2e: c
};
}
function fe(r, e) {
return r ? 1 : e != null && e.greenHostingFactor || (e == null ? void 0 : e.greenHostingFactor) === 0 ? e.greenHostingFactor : 0;
}
class tt {
constructor(e) {
this.allowRatings = !0, this.options = e, this.version = 4;
}
operationalEnergyPerSegment(e) {
const t = e / Q.GIGABYTE, n = t * xe, a = t * Xe, i = t * qe;
return {
dataCenter: n,
network: a,
device: i
};
}
operationalEmissions(e, t = {}) {
const { dataCenter: n, network: a, device: i } = this.operationalEnergyPerSegment(e);
let c = v, o = v, u = v;
if (t != null && t.gridIntensity) {
const { device: E, network: _, dataCenter: I } = t.gridIntensity;
(E != null && E.value || (E == null ? void 0 : E.value) === 0) && (u = E.value), (_ != null && _.value || (_ == null ? void 0 : _.value) === 0) && (o = _.value), (I != null && I.value || (I == null ? void 0 : I.value) === 0) && (c = I.value);
}
const s = n * c, l = a * o, d = i * u;
return {
dataCenter: s,
network: l,
device: d
};
}
embodiedEnergyPerSegment(e) {
const t = e / Q.GIGABYTE, n = t * Qe, a = t * ze, i = t * et;
return {
dataCenter: n,
network: a,
device: i
};
}
embodiedEmissions(e) {
const { dataCenter: t, network: n, device: a } = this.embodiedEnergyPerSegment(e), i = v, c = v, o = v, u = t * i, s = n * c, l = a * o;
return {
dataCenter: u,
network: s,
device: l
};
}
perByte(e, t = !1, n = !1, a = !1, i = {}) {
if (e < 1)
return 0;
const c = this.operationalEmissions(e, i), o = this.embodiedEmissions(e), u = fe(t, i), s = {
dataCenter: c.dataCenter * (1 - u) + o.dataCenter,
network: c.network + o.network,
device: c.device + o.device
}, l = s.dataCenter + s.network + s.device;
let d = null;
if (a && (d = this.ratingScale(l)), n) {
const E = y({}, Ee(c, o));
return a ? p(y({}, E), {
total: l,
rating: d
}) : p(y({}, E), { total: l });
}
return a ? { total: l, rating: d } : l;
}
perVisit(e, t = !1, n = !1, a = !1, i = {}) {
let c = 1, o = 0, u = 0;
const s = fe(t, i), l = this.operationalEmissions(e, i), d = this.embodiedEmissions(e);
if (e < 1)
return 0;
(i.firstVisitPercentage || i.firstVisitPercentage === 0) && (c = i.firstVisitPercentage), (i.returnVisitPercentage || i.returnVisitPercentage === 0) && (o = i.returnVisitPercentage), (i.dataReloadRatio || i.dataReloadRatio === 0) && (u = i.dataReloadRatio);
const E = l.dataCenter * (1 - s) + d.dataCenter + l.network + d.network + l.device + d.device, _ = (l.dataCenter * (1 - s) + d.dataCenter + l.network + d.network + l.device + d.device) * (1 - u), I = E * c + _ * o;
let j = null;
if (a && (j = this.ratingScale(I)), n) {
const te = p(y({}, Ee(l, d)), {
firstVisitCO2e: E,
returnVisitCO2e: _
});
return a ? p(y({}, te), {
total: I,
rating: j
}) : p(y({}, te), { total: I });
}
return a ? { total: I, rating: j } : I;
}
ratingScale(e) {
return Ce(e, this.version);
}
}
var ge = tt, rt = Object.defineProperty, Y = Object.getOwnPropertySymbols, he = Object.prototype.hasOwnProperty, Oe = Object.prototype.propertyIsEnumerable, Re = (r, e, t) => e in r ? rt(r, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : r[e] = t, H = (r, e) => {
for (var t in e || (e = {}))
he.call(e, t) && Re(r, t, e[t]);
if (Y)
for (var t of Y(e))
Oe.call(e, t) && Re(r, t, e[t]);
return r;
}, X = (r, e) => {
var t = {};
for (var n in r)
he.call(r, n) && e.indexOf(n) < 0 && (t[n] = r[n]);
if (r != null && Y)
for (var n of Y(r))
e.indexOf(n) < 0 && Oe.call(r, n) && (t[n] = r[n]);
return t;
};
class nt {
constructor(e) {
if (this.model = new x(), (e == null ? void 0 : e.model) === "1byte")
this.model = new ne();
else if ((e == null ? void 0 : e.model) === "swd")
this.model = new x(), (e == null ? void 0 : e.version) === 4 && (this.model = new ge());
else if (e != null && e.model)
throw new Error(
`"${e.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.`
);
if (e != null && e.rating && typeof e.rating != "boolean")
throw new Error(
`The rating option must be a boolean. Please use true or false.
See https://developers.thegreenwebfoundation.org/co2js/options/ to learn more about the options available in CO2.js.`
);
const t = !!this.model.allowRatings;
if (this._segment = (e == null ? void 0 : e.results) === "segment", this._rating = (e == null ? void 0 : e.rating) === !0, !t && this._rating)
throw new Error(
`The rating system is not supported in the model you are using. Try using the Sustainable Web Design model instead.
See https://developers.thegreenwebfoundation.org/co2js/models/ to learn more about the models available in CO2.js.`
);
}
perByte(e, t = !1) {
return this.model.perByte(e, t, this._segment, this._rating);
}
perVisit(e, t = !1) {
var n;
if ((n = this.model) != null && n.perVisit)
return this.model.perVisit(e, t, this._segment, this._rating);
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(e, t = !1, n = {}) {
const a = Te(
n,
this.model.version,
t
), o = X(a, ["gridIntensity"]), u = X(o, [
"dataReloadRatio",
"firstVisitPercentage",
"returnVisitPercentage"
]);
return {
co2: this.model.perByte(
e,
t,
this._segment,
this._rating,
a
),
green: t,
variables: H({
description: "Below are the variables used to calculate this CO2 estimate.",
bytes: e,
gridIntensity: H({
description: "The grid intensity (grams per kilowatt-hour) used to calculate this CO2 estimate."
}, a.gridIntensity)
}, u)
};
}
perVisitTrace(e, t = !1, n = {}) {
var a;
if ((a = this.model) != null && a.perVisit) {
const i = Me(
n,
this.model.version,
t
), c = i, o = X(c, ["gridIntensity"]);
return {
co2: this.model.perVisit(
e,
t,
this._segment,
this._rating,
i
),
green: t,
variables: H({
description: "Below are the variables used to calculate this CO2 estimate.",
bytes: e,
gridIntensity: H({
description: "The grid intensity (grams per kilowatt-hour) used to calculate this CO2 estimate."
}, i.gridIntensity)
}, o)
};
} else
throw new Error(
`The perVisitTrace() 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.`
);
}
SustainableWebDesignV3() {
return new x();
}
SustainableWebDesignV4() {
return new ge();
}
OneByte() {
return new ne();
}
}
var at = nt, it = Object.getOwnPropertyNames, st = (r, e) => function() {
return e || (0, r[it(r)[0]])((e = { exports: {} }).exports, e), e.exports;
}, ot = st({
"src/hosting-json.js"(r, e) {
async function t(s, l) {
return typeof s == "string" ? n(s, l) : i(s, l);
}
function n(s, l) {
return l.indexOf(s) > -1;
}
function a(s) {
return Object.entries(s).filter(([E, _]) => _.green).map(([E, _]) => _.url);
}
function i(s, l) {
let d = [];
for (let E of s)
l.indexOf(E) > -1 && d.push(E);
return d;
}
function c(s, l) {
return typeof s == "string" ? o(s, l) : u(s, l);
}
function o(s, l) {
return l.indexOf(s) > -1 ? s : {
url: s,
green: !1
};
}
function u(s, l) {
const d = {};
for (let E of s)
d[E] = o(E, l);
return d;
}
e.exports = {
check: t,
greenDomainsFromResults: a,
find: c
};
}
});
const lt = ot();
function ct(r, e) {
const t = typeof e == "string" ? { userAgentIdentifier: e } : e;
if (t != null && t.db && t.verbose)
throw new Error("verbose mode cannot be used with a local lookup database");
return typeof r == "string" ? ut(r, t) : dt(r, t);
}
async function ut(r, e = {}) {
const t = await fetch(
`https://api.thegreenwebfoundation.org/greencheck/${r}`,
{
headers: Ie(e.userAgentIdentifier)
}
);
if (e != null && e.db)
return lt.check(r, e.db);
const n = await t.json();
return e.verbose ? n : n.green;
}
async function dt(r, e = {}) {
try {
const t = "https://api.thegreenwebfoundation.org/v2/greencheckmulti", n = JSON.stringify(r), i = await (await fetch(`${t}/${n}`, {
headers: Ie(e.userAgentIdentifier)
})).json();
return e.verbose ? i : Et(i);
} catch {
return e.verbose ? {} : [];
}
}
function Et(r) {
return Object.entries(r).filter(([n, a]) => a.green).map(([n, a]) => a.url);
}
var ft = {
check: ct
};
function gt(r, e) {
return ft.check(r, e);
}
var Rt = gt;
const _t = { AFG: "414", ALB: "0", DZA: "528", ASM: "753", AND: "188", AGO: "1476", AIA: "753", ATG: "753", ARG: "478", ARM: "390", ABW: "753", AUS: "808", AUT: "242", AZE: "534", "AZORES (PORTUGAL)": "753", BHS: "753", BHR: "726", BGD: "528", BRB: "749", BLR: "400", BEL: "252", BLZ: "403", BEN: "745", BMU: "753", BTN: "0", BOL: "604", BES: "753", BIH: "1197", BWA: "1486", BRA: "284", VGB: "753", BRN: "681", BGR: "911", BFA: "753", BDI: "414", KHM: "1046", CMR: "659", CAN: "372", CYM: "753", CPV: "753", CAF: "188", TCD: "753", "CHANNEL ISLANDS (U.K)": "753", CHL: "657", CHN: "899", COL: "410", COM: "753", COD: "0", COG: "659", COK: "753", CRI: "108", CIV: "466", HRV: "294", CUB: "559", CUW: "876", CYP: "751", CZE: "902", DNK: "362", DJI: "753", DMA: "753", DOM: "601", ECU: "560", EGY: "554", SLV: "547", GNQ: "632", ERI: "915", EST: "1057", SWZ: "0", ETH: "0", FLK: "753", FRO: "753", FJI: "640", FIN: "267", FRA: "158", GUF: "423", PYF: "753", GAB: "946", GMB: "753", GEO: "289", DEU: "650", GHA: "495", GIB: "779", GRC: "507", GRL: "264", GRD: "753", GLP: "753", GUM: "753", GTM: "798", GIN: "753", GNB: "753", GUY: "847", HTI: "1048", HND: "662", HUN: "296", ISL: "0", IND: "951", IDN: "783", IRN: "592", IRQ: "1080", IRL: "380", IMN: "436", ISR: "394", ITA: "414", JAM: "711", JPN: "471", JOR: "529", KAZ: "797", KEN: "574", KIR: "753", PRK: "754", KOR: "555", XKX: "1145", KWT: "675", KGZ: "217", LAO: "1069", LVA: "240", LBN: "794", LSO: "0", LBR: "677", LBY: "668", LIE: "151", LTU: "211", LUX: "220", MDG: "876", "MADEIRA (PORTUGAL)": "663", MWI: "489", MYS: "551", MDV: "753", MLI: "1076", MLT: "520", MHL: "753", MTQ: "753", MRT: "753", MUS: "700", MYT: "753", MEX: "531", FSM: "753", MDA: "541", MCO: "158", MNG: "1366", MNE: "899", MSR: "753", MAR: "729", MOZ: "234", MMR: "719", NAM: "355", NRU: "753", NPL: "0", NLD: "326", NCL: "779", NZL: "246", NIC: "675", NER: "772", NGA: "526", NIU: "753", MKD: "851", MNP: "753", NOR: "47", OMN: "479", PAK: "592", PLW: "753", PSE: "719", PAN: "477", PNG: "597", PRY: "0", PER: "473", PHL: "672", POL: "828", PRT: "389", PRI: "596", QAT: "503", REU: "772", ROU: "489", RUS: "476", RWA: "712", SHN: "753", KNA: "753", LCA: "753", MAF: "753", SPM: "753", VCT: "753", WSM: "753", SMR: "414", STP: "753", SAU: "592", SEN: "870", SRB: "1086", SYC: "753", SLE: "489", SGP: "379", SXM: "753", SVK: "332", SVN: "620", SLB: "753", SOM: "753", ZAF: "1070", SSD: "890", ESP: "402", LKA: "731", SDN: "736", SUR: "1029", SWE: "68", CHE: "48", SYR: "713", TWN: "484", TJK: "255", TZA: "531", THA: "450", TLS: "753", TGO: "859", TON: "753", TTO: "559", TUN: "468", TUR: "376", TKM: "927", TCA: "753", TUV: "753", UGA: "279", UKR: "768", ARE: "556", GBR: "380", USA: "416", URY: "174", UZB: "612", VUT: "753", VEN: "711", VNM: "560", VIR: "650", YEM: "807", ZMB: "416", ZWE: "1575", "MEMO: EU 27": "409" }, Tt = "marginal", It = "2021";
var Ct = { data: _t, type: Tt, year: It }, ht = { co2: at, hosting: Rt, averageIntensity: k, marginalIntensity: Ct }, m, G, D, V, b, A, f, Ne, Pe, ye, z, ve, $, me, ee, be, Ae;
class Pt {
constructor(e) {
R(this, f);
R(this, m, (e, t) => {
});
R(this, G, "de");
R(this, D);
R(this, V, new q(window.sessionStorage, "carbonMeter.totalEmission", Number.MAX_SAFE_INTEGER));
R(this, b);
R(this, A);
R(this, $, () => {
let e = 0;
return performance.getEntriesByType("resource").map((t) => {
const n = t.toJSON();
e += n.transferSize, console.debug(`Count ${n.transferSize} bytes in browser from ${n.name}`);
}), performance.getEntriesByType("navigation").map((t) => {
const n = t.toJSON();
e += n.transferSize, console.debug(`Count ${n.transferSize} bytes in browser from ${n.name}`);
}), e;
});
e && (C(this, G, e), console.info(`CarbonMeter: Choose '${e}' for gathering grid carbon intensity`)), C(this, D, new ht.co2({ model: "swd", version: 4 }));
let t = 6e5, n = 144e5;
C(this, b, new q(window.sessionStorage, `carbonMeter.${e}.carbonIntensity`, t)), C(this, A, new q(window.localStorage, `carbonMeter.${e}.forecastData`, n));
}
start() {
setTimeout(() => {
T(this, f, Ne).call(this);
}, 1);
}
/**
* Registers a listener function to handle metering data.
*
* @param {function(number, number): void} listnerFunc - The listener function that will be called with the total CO2 emissions and the estimated CO2 emissions for the current metering event.
*/
onMetering(e) {
C(this, m, e);
}
}
m = new WeakMap(), G = new WeakMap(), D = new WeakMap(), V = new WeakMap(), b = new WeakMap(), A = new WeakMap(), f = new WeakSet(), Ne = async function() {
await T(this, f, ye).call(this), await T(this, f, Pe).call(this);
}, Pe = async function() {
let e = await T(this, f, ee).call(this);
new PerformanceObserver((n) => {
setTimeout(() => {
for (const a of n.getEntries())
if (a.initiatorType === "fetch" || a.initiatorType === "xmlhttprequest" || a.initiatorType === "img" || a.initiatorType === "script") {
let i = a.transferSize;
T(this, f, z).call(this, "From Background", i, e), console.debug(`${a.initiatorType}: Count ${i} bytes in background from ${a.name}`);
}
}, 1);
}).observe({
entryTypes: ["resource"]
});
}, ye = async function() {
let e = await T(this, f, ee).call(this), t = g(this, $).call(this);
T(this, f, z).call(this, "From Browser", t, e);
}, z = function(e, t, n) {
let a = g(this, D).perByteTrace(
t,
!1,
{
gridIntensity: {
device: n,
dataCenter: n,
networks: n
}
}
), i = parseFloat(a.co2.toFixed(2));
console.debug(`Report: ${e}, Bytes transfered: ${t}, Grid intensity: ${n}, Carbon: ${i}`), T(this, f, ve).call(this, i);
}, ve = function(e) {
let t = parseFloat(g(this, V).getItem());
t ? t += e : t = e, g(this, V).setItem(t), g(this, m) && g(this, m).call(this, t, e);
}, $ = new WeakMap(), me = function(e) {
let t = JSON.parse(e), n = t.Start, a = t.Interval, i = t.Ratings, c = Date.now(), o = Math.round((c - n) / a);
if (o >= 0 && o < i.length) {
let u = i[o];
return console.debug(`Current Grid CO2 Intensity: ${u}`), u;
}
return null;
}, ee = async function() {
let e = parseFloat(g(this, b).getItem());
if (e === void 0 || Number.isNaN(e)) {
let t = g(this, A).getItem();
if (t === void 0) {
let n = await T(this, f, be).call(this);
t = JSON.stringify(n), g(this, A).setItem(t);
}
e = T(this, f, me).call(this, t), e && g(this, b).setItem(e);
}
return e;
}, be = async function() {
return await T(this, f, Ae).call(this, g(this, G));
}, Ae = async function(e) {
return await (await fetch(`https://carbonawarecomputing.blob.core.windows.net/forecasts/${e}.min.json`)).json();
};
var w, S, L, h;
class q {
constructor(e, t, n) {
R(this, w);
R(this, S);
R(this, L);
R(this, h);
C(this, L, n), C(this, w, t), C(this, S, t + ".Updated"), C(this, h, e);
}
getItem() {
let e = g(this, h).getItem(g(this, w));
if (e && parseInt(g(this, h).getItem(g(this, S))) + g(this, L) > Date.now())
return e;
}
setItem(e) {
g(this, h).setItem(g(this, w), e), g(this, h).setItem(g(this, S), Date.now());
}
}
w = new WeakMap(), S = new WeakMap(), L = new WeakMap(), h = new WeakMap();
export {
Pt as default
};
//# sourceMappingURL=website-carbon-meter.js.map