UNPKG

website-carbon-meter

Version:

Tracks the carbon emissions of a website as live metrics. Based on CO2.js and the actual grid intensity.

22 lines (21 loc) 25.5 kB
(function(f,_){typeof exports=="object"&&typeof module<"u"?module.exports=_():typeof define=="function"&&define.amd?define(_):(f=typeof globalThis<"u"?globalThis:f||self,f["website-carbon-meter"]=_())})(this,function(){"use strict";var ve=f=>{throw TypeError(f)};var re=(f,_,T)=>_.has(f)||ve("Cannot "+T);var R=(f,_,T)=>(re(f,_,"read from private field"),T?T.call(f):_.get(f)),I=(f,_,T)=>_.has(f)?ve("Cannot add the same private member more than once"):_ instanceof WeakSet?_.add(f):_.set(f,T),N=(f,_,T,x)=>(re(f,_,"write to private field"),x?x.call(f,T):_.set(f,T),T),C=(f,_,T)=>(re(f,_,"access private method"),T);var w,L,M,F,S,p,g,me,be,Ae,ne,we,J,Se,ae,pe,Be,B,G,H,y;const X=4883333333333333e-25;class Ge{constructor(e){this.allowRatings=!1,this.options=e,this.KWH_PER_BYTE_FOR_NETWORK=X}perByte(e,t){if(e<1)return 0;if(t){const a=e*72e-12*0,i=e*X*475;return a+i}const n=72e-12+X;return e*n*519}}var ie=Ge,q={GIGABYTE:1e3*1e3*1e3},W={data:{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},type:"average"};const De=.81,se=.52,oe=.14,le=.15,ce=.19,P=W.data.WORLD,Ve=50,K=.75,U=.25,k=.02,ue={OPERATIONAL_KWH_PER_GB_DATACENTER:.055,OPERATIONAL_KWH_PER_GB_NETWORK:.059,OPERATIONAL_KWH_PER_GB_DEVICE:.08,EMBODIED_KWH_PER_GB_DATACENTER:.012,EMBODIED_KWH_PER_GB_NETWORK:.013,EMBODIED_KWH_PER_GB_DEVICE:.081,GLOBAL_GRID_INTENSITY:494},Le={FIFTH_PERCENTILE:.095,TENTH_PERCENTILE:.186,TWENTIETH_PERCENTILE:.341,THIRTIETH_PERCENTILE:.493,FORTIETH_PERCENTILE:.656,FIFTIETH_PERCENTILE:.846},v={FIFTH_PERCENTILE:.04,TENTH_PERCENTILE:.079,TWENTIETH_PERCENTILE:.145,THIRTIETH_PERCENTILE:.209,FORTIETH_PERCENTILE:.278,FIFTIETH_PERCENTILE:.359},Me=ue.GLOBAL_GRID_INTENSITY,D=r=>parseFloat(r.toFixed(2)),m=(r,e)=>r<=e;function de(r={},e=3,t=!1){const n=e===4?Me:P;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"?(W.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(W.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 Fe(r={},e=3,t=!1){if(typeof r!="object")throw new Error("Options must be an object");const n=de(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?k: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?k: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?k: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?K: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?K: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?K: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?U: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?U: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?U:0,console.warn(`The returnVisitPercentage option must be a number. You passed in a ${typeof r.returnVisitPercentage}. Falling back to default value.`)),n}function Ee(r=""){return{"User-Agent":`co2js/0.16.4 ${r}`}}function fe(r,e){let{FIFTH_PERCENTILE:t,TENTH_PERCENTILE:n,TWENTIETH_PERCENTILE:a,THIRTIETH_PERCENTILE:i,FORTIETH_PERCENTILE:c,FIFTIETH_PERCENTILE:o}=Le;return e===4&&(t=v.FIFTH_PERCENTILE,n=v.TENTH_PERCENTILE,a=v.TWENTIETH_PERCENTILE,i=v.THIRTIETH_PERCENTILE,c=v.FORTIETH_PERCENTILE,o=v.FIFTIETH_PERCENTILE),m(r,t)?"A+":m(r,n)?"A":m(r,a)?"B":m(r,i)?"C":m(r,c)?"D":m(r,o)?"E":"F"}var He=Object.defineProperty,We=Object.defineProperties,Ke=Object.getOwnPropertyDescriptors,ge=Object.getOwnPropertySymbols,Ue=Object.prototype.hasOwnProperty,ke=Object.prototype.propertyIsEnumerable,Re=(r,e,t)=>e in r?He(r,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):r[e]=t,$=(r,e)=>{for(var t in e||(e={}))Ue.call(e,t)&&Re(r,t,e[t]);if(ge)for(var t of ge(e))ke.call(e,t)&&Re(r,t,e[t]);return r},Y=(r,e)=>We(r,Ke(e)),Q=class{constructor(e){this.allowRatings=!0,this.options=e,this.version=3}energyPerByteByComponent(e){const n=e/q.GIGABYTE*De;return{consumerDeviceEnergy:n*se,networkEnergy:n*oe,productionEnergy:n*ce,dataCenterEnergy:n*le}}co2byComponent(e,t=P,n={}){let a=P,i=P,c=P,o=P;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=Ve);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?Y($({},o),{total:s,rating:l}):Y($({},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?Y($({},o),{total:s,rating:l}):Y($({},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=K,a=U,i=k){(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=P){return D(e*t)}annualEnergyInKwh(e,t=1e3){return e*t*12}annualEmissionsInGrams(e,t=1e3){return e*t*12}annualSegmentEnergy(e){return{consumerDeviceEnergy:D(e*se),networkEnergy:D(e*oe),dataCenterEnergy:D(e*le),productionEnergy:D(e*ce)}}ratingScale(e){return fe(e,this.version)}},$e=Object.defineProperty,Ye=Object.defineProperties,je=Object.getOwnPropertyDescriptors,_e=Object.getOwnPropertySymbols,Ze=Object.prototype.hasOwnProperty,Je=Object.prototype.propertyIsEnumerable,Te=(r,e,t)=>e in r?$e(r,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):r[e]=t,b=(r,e)=>{for(var t in e||(e={}))Ze.call(e,t)&&Te(r,t,e[t]);if(_e)for(var t of _e(e))Je.call(e,t)&&Te(r,t,e[t]);return r},V=(r,e)=>Ye(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:A}=ue;function Ie(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 he(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=A,o=A,u=A;if(t!=null&&t.gridIntensity){const{device:E,network:h,dataCenter:O}=t.gridIntensity;(E!=null&&E.value||(E==null?void 0:E.value)===0)&&(u=E.value),(h!=null&&h.value||(h==null?void 0:h.value)===0)&&(o=h.value),(O!=null&&O.value||(O==null?void 0:O.value)===0)&&(c=O.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=A,c=A,o=A,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=he(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=b({},Ie(c,o));return a?V(b({},E),{total:l,rating:d}):V(b({},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=he(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,h=(l.dataCenter*(1-s)+d.dataCenter+l.network+d.network+l.device+d.device)*(1-u),O=E*c+h*o;let te=null;if(a&&(te=this.ratingScale(O)),n){const Pe=V(b({},Ie(l,d)),{firstVisitCO2e:E,returnVisitCO2e:h});return a?V(b({},Pe),{total:O,rating:te}):V(b({},Pe),{total:O})}return a?{total:O,rating:te}:O}ratingScale(e){return fe(e,this.version)}}var Ce=tt,rt=Object.defineProperty,j=Object.getOwnPropertySymbols,Oe=Object.prototype.hasOwnProperty,Ne=Object.prototype.propertyIsEnumerable,ye=(r,e,t)=>e in r?rt(r,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):r[e]=t,Z=(r,e)=>{for(var t in e||(e={}))Oe.call(e,t)&&ye(r,t,e[t]);if(j)for(var t of j(e))Ne.call(e,t)&&ye(r,t,e[t]);return r},z=(r,e)=>{var t={};for(var n in r)Oe.call(r,n)&&e.indexOf(n)<0&&(t[n]=r[n]);if(r!=null&&j)for(var n of j(r))e.indexOf(n)<0&&Ne.call(r,n)&&(t[n]=r[n]);return t};class nt{constructor(e){if(this.model=new Q,(e==null?void 0:e.model)==="1byte")this.model=new ie;else if((e==null?void 0:e.model)==="swd")this.model=new Q,(e==null?void 0:e.version)===4&&(this.model=new Ce);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=de(n,this.model.version,t),o=z(a,["gridIntensity"]),u=z(o,["dataReloadRatio","firstVisitPercentage","returnVisitPercentage"]);return{co2:this.model.perByte(e,t,this._segment,this._rating,a),green:t,variables:Z({description:"Below are the variables used to calculate this CO2 estimate.",bytes:e,gridIntensity:Z({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=Fe(n,this.model.version,t),c=i,o=z(c,["gridIntensity"]);return{co2:this.model.perVisit(e,t,this._segment,this._rating,i),green:t,variables:Z({description:"Below are the variables used to calculate this CO2 estimate.",bytes:e,gridIntensity:Z({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 Q}SustainableWebDesignV4(){return new Ce}OneByte(){return new ie}}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,h])=>h.green).map(([E,h])=>h.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:Ee(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:Ee(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,_t={data:{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"},type:"marginal",year:"2021"},Tt={co2:at,hosting:Rt,averageIntensity:W,marginalIntensity:_t};class It{constructor(e){I(this,g);I(this,w,(e,t)=>{});I(this,L,"de");I(this,M);I(this,F,new ee(window.sessionStorage,"carbonMeter.totalEmission",Number.MAX_SAFE_INTEGER));I(this,S);I(this,p);I(this,J,()=>{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&&(N(this,L,e),console.info(`CarbonMeter: Choose '${e}' for gathering grid carbon intensity`)),N(this,M,new Tt.co2({model:"swd",version:4}));let t=6e5,n=144e5;N(this,S,new ee(window.sessionStorage,`carbonMeter.${e}.carbonIntensity`,t)),N(this,p,new ee(window.localStorage,`carbonMeter.${e}.forecastData`,n))}start(){setTimeout(()=>{C(this,g,me).call(this)},1)}onMetering(e){N(this,w,e)}}w=new WeakMap,L=new WeakMap,M=new WeakMap,F=new WeakMap,S=new WeakMap,p=new WeakMap,g=new WeakSet,me=async function(){await C(this,g,Ae).call(this),await C(this,g,be).call(this)},be=async function(){let e=await C(this,g,ae).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;C(this,g,ne).call(this,"From Background",i,e),console.debug(`${a.initiatorType}: Count ${i} bytes in background from ${a.name}`)}},1)}).observe({entryTypes:["resource"]})},Ae=async function(){let e=await C(this,g,ae).call(this),t=R(this,J).call(this);C(this,g,ne).call(this,"From Browser",t,e)},ne=function(e,t,n){let a=R(this,M).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}`),C(this,g,we).call(this,i)},we=function(e){let t=parseFloat(R(this,F).getItem());t?t+=e:t=e,R(this,F).setItem(t),R(this,w)&&R(this,w).call(this,t,e)},J=new WeakMap,Se=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},ae=async function(){let e=parseFloat(R(this,S).getItem());if(e===void 0||Number.isNaN(e)){let t=R(this,p).getItem();if(t===void 0){let n=await C(this,g,pe).call(this);t=JSON.stringify(n),R(this,p).setItem(t)}e=C(this,g,Se).call(this,t),e&&R(this,S).setItem(e)}return e},pe=async function(){return await C(this,g,Be).call(this,R(this,L))},Be=async function(e){return await(await fetch(`https://carbonawarecomputing.blob.core.windows.net/forecasts/${e}.min.json`)).json()};class ee{constructor(e,t,n){I(this,B);I(this,G);I(this,H);I(this,y);N(this,H,n),N(this,B,t),N(this,G,t+".Updated"),N(this,y,e)}getItem(){let e=R(this,y).getItem(R(this,B));if(e&&parseInt(R(this,y).getItem(R(this,G)))+R(this,H)>Date.now())return e}setItem(e){R(this,y).setItem(R(this,B),e),R(this,y).setItem(R(this,G),Date.now())}}return B=new WeakMap,G=new WeakMap,H=new WeakMap,y=new WeakMap,It}); //# sourceMappingURL=website-carbon-meter.umd.cjs.map