@tgwf/co2 0.12.2 → 0.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -4
- package/data/output/average-intensities.js +226 -0
- package/data/output/average-intensities.json +1328 -0
- package/data/output/marginal-intensities-2021.json +466 -0
- package/dist/cjs/1byte.js.map +1 -1
- package/dist/cjs/co2.js +13 -44
- package/dist/cjs/co2.js.map +2 -2
- package/dist/cjs/constants/index.js +1 -4
- package/dist/cjs/constants/index.js.map +2 -2
- package/dist/cjs/data/average-intensities.min.js +28 -0
- package/dist/cjs/data/average-intensities.min.js.map +7 -0
- package/dist/cjs/helpers/index.js +27 -57
- package/dist/cjs/helpers/index.js.map +1 -1
- package/dist/cjs/hosting-api.js +2 -7
- package/dist/cjs/hosting-api.js.map +2 -2
- package/dist/cjs/hosting-json.node.js +1 -4
- package/dist/cjs/hosting-json.node.js.map +2 -2
- package/dist/cjs/hosting-node.js +4 -19
- package/dist/cjs/hosting-node.js.map +2 -2
- package/dist/cjs/hosting.js +1 -4
- package/dist/cjs/hosting.js.map +2 -2
- package/dist/cjs/index-node.js +1 -4
- package/dist/cjs/index-node.js.map +2 -2
- package/dist/cjs/index.js +4 -7
- package/dist/cjs/index.js.map +3 -3
- package/dist/cjs/sustainable-web-design.js +9 -32
- package/dist/cjs/sustainable-web-design.js.map +2 -2
- package/dist/esm/co2.js +13 -40
- package/dist/esm/data/average-intensities.min.js +8 -0
- package/dist/esm/helpers/index.js +27 -57
- package/dist/esm/hosting-api.js +2 -3
- package/dist/esm/hosting.js +1 -0
- package/dist/esm/index.js +1 -1
- package/dist/esm/sustainable-web-design.js +9 -28
- package/dist/iife/index.js +7 -7
- package/dist/iife/index.js.map +4 -4
- package/package.json +2 -2
- package/src/data/average-intensities.min.js +1 -0
- package/src/index.js +1 -1
- package/data/output/average-intensities-2021.js +0 -5
- package/data/output/average-intensities-2021.json +0 -1
- package/dist/cjs/data/average-intensities-2021.min.js +0 -30
- package/dist/cjs/data/average-intensities-2021.min.js.map +0 -7
- package/dist/esm/data/average-intensities-2021.min.js +0 -10
- package/src/data/average-intensities-2021.min.js +0 -1
|
@@ -2,6 +2,6 @@
|
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/hosting-node.js"],
|
|
4
4
|
"sourcesContent": ["/*\n\nWe have a separate node-specific hosting.js file for node.\nThis uses the node-specific APIs for making http requests,\nand doing lookups against local JSON and sqlite databases.\nThis is used in the CommonJS build of co2.js\n\nThis lets us keep the total library small, and dependencies minimal.\n*/\n\nimport https from \"https\";\n\nimport debugFactory from \"debug\";\nconst log = debugFactory(\"tgwf:hosting-node\");\n\nimport hostingJSON from \"./hosting-json.node.js\";\n\n/**\n * Accept a url and perform an http request, returning the body\n * for parsing as JSON.\n *\n * @param {string} url\n * @return {string}\n */\nasync function getBody(url) {\n return new Promise(function (resolve, reject) {\n // Do async job\n const req = https.get(url, function (res) {\n if (res.statusCode < 200 || res.statusCode >= 300) {\n log(\n \"Could not get info from the Green Web Foundation API, %s for %s\",\n res.statusCode,\n url\n );\n return reject(new Error(`Status Code: ${res.statusCode}`));\n }\n const data = [];\n\n res.on(\"data\", (chunk) => {\n data.push(chunk);\n });\n\n res.on(\"end\", () => resolve(Buffer.concat(data).toString()));\n });\n req.end();\n });\n}\n\nfunction check(domain, db) {\n if (db) {\n return hostingJSON.check(domain, db);\n }\n\n // is it a single domain or an array of them?\n if (typeof domain === \"string\") {\n return checkAgainstAPI(domain);\n } else {\n return checkDomainsAgainstAPI(domain);\n }\n}\n\nasync function checkAgainstAPI(domain) {\n const res = JSON.parse(\n await getBody(`https://api.thegreenwebfoundation.org/greencheck/${domain}`)\n );\n return res.green;\n}\n\nasync function checkDomainsAgainstAPI(domains) {\n try {\n const allGreenCheckResults = JSON.parse(\n await getBody(\n `https://api.thegreenwebfoundation.org/v2/greencheckmulti/${JSON.stringify(\n domains\n )}`\n )\n );\n return hostingJSON.greenDomainsFromResults(allGreenCheckResults);\n } catch (e) {\n return [];\n }\n}\n\nasync function checkPage(pageXray, db) {\n const domains = Object.keys(pageXray.domains);\n return check(domains, db);\n}\n\nexport default {\n check,\n checkPage,\n greendomains: hostingJSON.greenDomainsFromResults,\n loadJSON: hostingJSON.loadJSON,\n};\n"],
|
|
5
|
-
"mappings": "
|
|
6
|
-
"names": [
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAUA,mBAAkB;AAElB,mBAAyB;AAGzB,+BAAwB;AAFxB,MAAM,MAAM,0BAAa,mBAAmB;AAW5C,uBAAuB,KAAK;AAC1B,SAAO,IAAI,QAAQ,SAAU,SAAS,QAAQ;AAE5C,UAAM,MAAM,qBAAM,IAAI,KAAK,SAAU,KAAK;AACxC,UAAI,IAAI,aAAa,OAAO,IAAI,cAAc,KAAK;AACjD,YACE,mEACA,IAAI,YACJ,GACF;AACA,eAAO,OAAO,IAAI,MAAM,gBAAgB,IAAI,YAAY,CAAC;AAAA,MAC3D;AACA,YAAM,OAAO,CAAC;AAEd,UAAI,GAAG,QAAQ,CAAC,UAAU;AACxB,aAAK,KAAK,KAAK;AAAA,MACjB,CAAC;AAED,UAAI,GAAG,OAAO,MAAM,QAAQ,OAAO,OAAO,IAAI,EAAE,SAAS,CAAC,CAAC;AAAA,IAC7D,CAAC;AACD,QAAI,IAAI;AAAA,EACV,CAAC;AACH;AAEA,eAAe,QAAQ,IAAI;AACzB,MAAI,IAAI;AACN,WAAO,iCAAY,MAAM,QAAQ,EAAE;AAAA,EACrC;AAGA,MAAI,OAAO,WAAW,UAAU;AAC9B,WAAO,gBAAgB,MAAM;AAAA,EAC/B,OAAO;AACL,WAAO,uBAAuB,MAAM;AAAA,EACtC;AACF;AAEA,+BAA+B,QAAQ;AACrC,QAAM,MAAM,KAAK,MACf,MAAM,QAAQ,oDAAoD,QAAQ,CAC5E;AACA,SAAO,IAAI;AACb;AAEA,sCAAsC,SAAS;AAC7C,MAAI;AACF,UAAM,uBAAuB,KAAK,MAChC,MAAM,QACJ,4DAA4D,KAAK,UAC/D,OACF,GACF,CACF;AACA,WAAO,iCAAY,wBAAwB,oBAAoB;AAAA,EACjE,SAAS,GAAP;AACA,WAAO,CAAC;AAAA,EACV;AACF;AAEA,yBAAyB,UAAU,IAAI;AACrC,QAAM,UAAU,OAAO,KAAK,SAAS,OAAO;AAC5C,SAAO,MAAM,SAAS,EAAE;AAC1B;AAEA,IAAO,uBAAQ;AAAA,EACb;AAAA,EACA;AAAA,EACA,cAAc,iCAAY;AAAA,EAC1B,UAAU,iCAAY;AACxB;",
|
|
6
|
+
"names": []
|
|
7
7
|
}
|
package/dist/cjs/hosting.js
CHANGED
|
@@ -17,10 +17,7 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
17
17
|
}
|
|
18
18
|
return to;
|
|
19
19
|
};
|
|
20
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
22
|
-
mod
|
|
23
|
-
));
|
|
20
|
+
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));
|
|
24
21
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
25
22
|
var hosting_exports = {};
|
|
26
23
|
__export(hosting_exports, {
|
package/dist/cjs/hosting.js.map
CHANGED
|
@@ -2,6 +2,6 @@
|
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/hosting.js"],
|
|
4
4
|
"sourcesContent": ["\"use strict\";\n\nimport debugFactory from \"debug\";\nconst log = debugFactory(\"tgwf:hosting\");\n\nimport hostingAPI from \"./hosting-api.js\";\n\nfunction check(domain, db) {\n return hostingAPI.check(domain);\n}\n\nexport default {\n check,\n};\n"],
|
|
5
|
-
"mappings": "
|
|
6
|
-
"names": [
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAEA,mBAAyB;AAGzB,yBAAuB;AAFvB,MAAM,MAAM,0BAAa,cAAc;AAIvC,eAAe,QAAQ,IAAI;AACzB,SAAO,2BAAW,MAAM,MAAM;AAChC;AAEA,IAAO,kBAAQ;AAAA,EACb;AACF;",
|
|
6
|
+
"names": []
|
|
7
7
|
}
|
package/dist/cjs/index-node.js
CHANGED
|
@@ -16,10 +16,7 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
16
16
|
}
|
|
17
17
|
return to;
|
|
18
18
|
};
|
|
19
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
21
|
-
mod
|
|
22
|
-
));
|
|
19
|
+
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));
|
|
23
20
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
24
21
|
var index_node_exports = {};
|
|
25
22
|
__export(index_node_exports, {
|
|
@@ -2,6 +2,6 @@
|
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/index-node.js"],
|
|
4
4
|
"sourcesContent": ["import co2 from \"./co2.js\";\nimport hosting from \"./hosting-node.js\";\nimport averageIntensity from \"./data/average-intensities-2021.min.js\";\nimport marginalIntensity from \"./data/marginal-intensities-2021.min.js\";\nexport { co2, hosting, averageIntensity, marginalIntensity };\n"],
|
|
5
|
-
"mappings": "
|
|
6
|
-
"names": [
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,iBAAgB;AAChB,0BAAoB;AACpB,0CAA6B;AAC7B,2CAA8B;",
|
|
6
|
+
"names": []
|
|
7
7
|
}
|
package/dist/cjs/index.js
CHANGED
|
@@ -16,14 +16,11 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
16
16
|
}
|
|
17
17
|
return to;
|
|
18
18
|
};
|
|
19
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
21
|
-
mod
|
|
22
|
-
));
|
|
19
|
+
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));
|
|
23
20
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
24
21
|
var src_exports = {};
|
|
25
22
|
__export(src_exports, {
|
|
26
|
-
averageIntensity: () =>
|
|
23
|
+
averageIntensity: () => import_average_intensities_min.default,
|
|
27
24
|
co2: () => import_co2.default,
|
|
28
25
|
default: () => src_default,
|
|
29
26
|
hosting: () => import_hosting.default,
|
|
@@ -32,7 +29,7 @@ __export(src_exports, {
|
|
|
32
29
|
module.exports = __toCommonJS(src_exports);
|
|
33
30
|
var import_co2 = __toESM(require("./co2.js"));
|
|
34
31
|
var import_hosting = __toESM(require("./hosting.js"));
|
|
35
|
-
var
|
|
32
|
+
var import_average_intensities_min = __toESM(require("./data/average-intensities.min.js"));
|
|
36
33
|
var import_marginal_intensities_2021_min = __toESM(require("./data/marginal-intensities-2021.min.js"));
|
|
37
|
-
var src_default = { co2: import_co2.default, hosting: import_hosting.default, averageIntensity:
|
|
34
|
+
var src_default = { co2: import_co2.default, hosting: import_hosting.default, averageIntensity: import_average_intensities_min.default, marginalIntensity: import_marginal_intensities_2021_min.default };
|
|
38
35
|
//# sourceMappingURL=index.js.map
|
package/dist/cjs/index.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/index.js"],
|
|
4
|
-
"sourcesContent": ["import co2 from \"./co2.js\";\nimport hosting from \"./hosting.js\";\nimport averageIntensity from \"./data/average-intensities
|
|
5
|
-
"mappings": "
|
|
6
|
-
"names": [
|
|
4
|
+
"sourcesContent": ["import co2 from \"./co2.js\";\nimport hosting from \"./hosting.js\";\nimport averageIntensity from \"./data/average-intensities.min.js\";\nimport marginalIntensity from \"./data/marginal-intensities-2021.min.js\";\n\nexport { co2, hosting, averageIntensity, marginalIntensity };\nexport default { co2, hosting, averageIntensity, marginalIntensity };\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,iBAAgB;AAChB,qBAAoB;AACpB,qCAA6B;AAC7B,2CAA8B;AAG9B,IAAO,cAAQ,EAAE,yBAAK,iCAAS,0DAAkB,gEAAkB;",
|
|
6
|
+
"names": []
|
|
7
7
|
}
|
|
@@ -17,10 +17,7 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
17
17
|
}
|
|
18
18
|
return to;
|
|
19
19
|
};
|
|
20
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
22
|
-
mod
|
|
23
|
-
));
|
|
20
|
+
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));
|
|
24
21
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
25
22
|
var sustainable_web_design_exports = {};
|
|
26
23
|
__export(sustainable_web_design_exports, {
|
|
@@ -83,19 +80,11 @@ class SustainableWebDesign {
|
|
|
83
80
|
perByte(bytes, carbonIntensity = false, segmentResults = false, options = {}) {
|
|
84
81
|
const energyBycomponent = this.energyPerByteByComponent(bytes, options);
|
|
85
82
|
if (typeof carbonIntensity !== "boolean") {
|
|
86
|
-
throw new Error(
|
|
87
|
-
`perByte expects a boolean for the carbon intensity value. Received: ${carbonIntensity}`
|
|
88
|
-
);
|
|
83
|
+
throw new Error(`perByte expects a boolean for the carbon intensity value. Received: ${carbonIntensity}`);
|
|
89
84
|
}
|
|
90
|
-
const co2ValuesbyComponent = this.co2byComponent(
|
|
91
|
-
energyBycomponent,
|
|
92
|
-
carbonIntensity,
|
|
93
|
-
options
|
|
94
|
-
);
|
|
85
|
+
const co2ValuesbyComponent = this.co2byComponent(energyBycomponent, carbonIntensity, options);
|
|
95
86
|
const co2Values = Object.values(co2ValuesbyComponent);
|
|
96
|
-
const co2ValuesSum = co2Values.reduce(
|
|
97
|
-
(prevValue, currentValue) => prevValue + currentValue
|
|
98
|
-
);
|
|
87
|
+
const co2ValuesSum = co2Values.reduce((prevValue, currentValue) => prevValue + currentValue);
|
|
99
88
|
if (segmentResults) {
|
|
100
89
|
return { ...co2ValuesbyComponent, total: co2ValuesSum };
|
|
101
90
|
}
|
|
@@ -104,19 +93,11 @@ class SustainableWebDesign {
|
|
|
104
93
|
perVisit(bytes, carbonIntensity = false, segmentResults = false, options = {}) {
|
|
105
94
|
const energyBycomponent = this.energyPerVisitByComponent(bytes, options);
|
|
106
95
|
if (typeof carbonIntensity !== "boolean") {
|
|
107
|
-
throw new Error(
|
|
108
|
-
`perVisit expects a boolean for the carbon intensity value. Received: ${carbonIntensity}`
|
|
109
|
-
);
|
|
96
|
+
throw new Error(`perVisit expects a boolean for the carbon intensity value. Received: ${carbonIntensity}`);
|
|
110
97
|
}
|
|
111
|
-
const co2ValuesbyComponent = this.co2byComponent(
|
|
112
|
-
energyBycomponent,
|
|
113
|
-
carbonIntensity,
|
|
114
|
-
options
|
|
115
|
-
);
|
|
98
|
+
const co2ValuesbyComponent = this.co2byComponent(energyBycomponent, carbonIntensity, options);
|
|
116
99
|
const co2Values = Object.values(co2ValuesbyComponent);
|
|
117
|
-
const co2ValuesSum = co2Values.reduce(
|
|
118
|
-
(prevValue, currentValue) => prevValue + currentValue
|
|
119
|
-
);
|
|
100
|
+
const co2ValuesSum = co2Values.reduce((prevValue, currentValue) => prevValue + currentValue);
|
|
120
101
|
if (segmentResults) {
|
|
121
102
|
return { ...co2ValuesbyComponent, total: co2ValuesSum };
|
|
122
103
|
}
|
|
@@ -125,9 +106,7 @@ class SustainableWebDesign {
|
|
|
125
106
|
energyPerByte(bytes) {
|
|
126
107
|
const energyByComponent = this.energyPerByteByComponent(bytes);
|
|
127
108
|
const energyValues = Object.values(energyByComponent);
|
|
128
|
-
return energyValues.reduce(
|
|
129
|
-
(prevValue, currentValue) => prevValue + currentValue
|
|
130
|
-
);
|
|
109
|
+
return energyValues.reduce((prevValue, currentValue) => prevValue + currentValue);
|
|
131
110
|
}
|
|
132
111
|
energyPerVisitByComponent(bytes, options = {}, firstView = import_constants.FIRST_TIME_VIEWING_PERCENTAGE, returnView = import_constants.RETURNING_VISITOR_PERCENTAGE, dataReloadRatio = import_constants.PERCENTAGE_OF_DATA_LOADED_ON_SUBSEQUENT_LOAD) {
|
|
133
112
|
if (options.dataReloadRatio) {
|
|
@@ -153,9 +132,7 @@ class SustainableWebDesign {
|
|
|
153
132
|
energyPerVisit(bytes) {
|
|
154
133
|
let firstVisits = 0;
|
|
155
134
|
let subsequentVisits = 0;
|
|
156
|
-
const energyBycomponent = Object.entries(
|
|
157
|
-
this.energyPerVisitByComponent(bytes)
|
|
158
|
-
);
|
|
135
|
+
const energyBycomponent = Object.entries(this.energyPerVisitByComponent(bytes));
|
|
159
136
|
for (const [key, val] of energyBycomponent) {
|
|
160
137
|
if (key.indexOf("first") > 0) {
|
|
161
138
|
firstVisits += val;
|
|
@@ -2,6 +2,6 @@
|
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/sustainable-web-design.js"],
|
|
4
4
|
"sourcesContent": ["\"use strict\";\n\n/**\n * Sustainable Web Design\n *\n * Updated calculations and figures from\n * https://sustainablewebdesign.org/calculating-digital-emissions/\n *\n */\nimport debugFactory from \"debug\";\nconst log = debugFactory(\"tgwf:sustainable-web-design\");\n\nimport {\n fileSize,\n KWH_PER_GB,\n END_USER_DEVICE_ENERGY,\n NETWORK_ENERGY,\n DATACENTER_ENERGY,\n PRODUCTION_ENERGY,\n GLOBAL_GRID_INTENSITY,\n RENEWABLES_GRID_INTENSITY,\n FIRST_TIME_VIEWING_PERCENTAGE,\n RETURNING_VISITOR_PERCENTAGE,\n PERCENTAGE_OF_DATA_LOADED_ON_SUBSEQUENT_LOAD,\n} from \"./constants/index.js\";\nimport { formatNumber } from \"./helpers/index.js\";\n\nclass SustainableWebDesign {\n constructor(options) {\n this.options = options;\n }\n\n /**\n * Accept a figure for bytes transferred and return an object representing\n * the share of the total enrgy use of the entire system, broken down\n * by each corresponding system component\n *\n * @param {number} bytes - the data transferred in bytes\n * @return {object} Object containing the energy in kilowatt hours, keyed by system component\n */\n energyPerByteByComponent(bytes) {\n const transferedBytesToGb = bytes / fileSize.GIGABYTE;\n const energyUsage = transferedBytesToGb * KWH_PER_GB;\n\n // return the total energy, with breakdown by component\n return {\n consumerDeviceEnergy: energyUsage * END_USER_DEVICE_ENERGY,\n networkEnergy: energyUsage * NETWORK_ENERGY,\n productionEnergy: energyUsage * PRODUCTION_ENERGY,\n dataCenterEnergy: energyUsage * DATACENTER_ENERGY,\n };\n }\n /**\n * Accept an object keys by the different system components, and\n * return an object with the co2 figures key by the each component\n *\n * @param {object} energyByComponent - energy grouped by the four system components\n * @param {number} [carbonIntensity] - carbon intensity to apply to the datacentre values\n * @return {number} the total number in grams of CO2 equivalent emissions\n */\n co2byComponent(\n energyByComponent,\n carbonIntensity = GLOBAL_GRID_INTENSITY,\n options = {}\n ) {\n let deviceCarbonIntensity = GLOBAL_GRID_INTENSITY;\n let networkCarbonIntensity = GLOBAL_GRID_INTENSITY;\n let dataCenterCarbonIntensity = GLOBAL_GRID_INTENSITY;\n\n let globalEmissions = GLOBAL_GRID_INTENSITY;\n\n if (options?.gridIntensity) {\n const { device, network, dataCenter } = options.gridIntensity;\n\n if (device?.value) {\n deviceCarbonIntensity = device.value;\n }\n if (network?.value) {\n networkCarbonIntensity = network.value;\n }\n // If the user has set a carbon intensity value for the datacentre, then that overrides everything and is used\n if (dataCenter?.value) {\n dataCenterCarbonIntensity = dataCenter.value;\n }\n }\n\n // If the user passes in a TRUE value (green web host), then use the renewables intensity value\n if (carbonIntensity === true) {\n dataCenterCarbonIntensity = RENEWABLES_GRID_INTENSITY;\n }\n\n const returnCO2ByComponent = {};\n for (const [key, value] of Object.entries(energyByComponent)) {\n // we update the datacentre, as that's what we have information\n // about.\n if (key.startsWith(\"dataCenterEnergy\")) {\n returnCO2ByComponent[key.replace(\"Energy\", \"CO2\")] =\n value * dataCenterCarbonIntensity;\n } else if (key.startsWith(\"consumerDeviceEnergy\")) {\n returnCO2ByComponent[key.replace(\"Energy\", \"CO2\")] =\n value * deviceCarbonIntensity;\n } else if (key.startsWith(\"networkEnergy\")) {\n returnCO2ByComponent[key.replace(\"Energy\", \"CO2\")] =\n value * networkCarbonIntensity;\n } else {\n // Use the global intensity for the remaining segments\n returnCO2ByComponent[key.replace(\"Energy\", \"CO2\")] =\n value * globalEmissions;\n }\n }\n\n return returnCO2ByComponent;\n }\n\n /**\n * Accept a figure for bytes transferred and return a single figure for CO2\n * emissions. Where information exists about the origin data is being\n * fetched from, a different carbon intensity figure\n * is applied for the datacentre share of the carbon intensity.\n *\n * @param {number} bytes - the data transferred in bytes\n * @param {number} `carbonIntensity` the carbon intensity for datacentre (average figures, not marginal ones)\n * @return {number} the total number in grams of CO2 equivalent emissions\n */\n perByte(\n bytes,\n carbonIntensity = false,\n segmentResults = false,\n options = {}\n ) {\n const energyBycomponent = this.energyPerByteByComponent(bytes, options);\n\n // otherwise when faced with non numeric values throw an error\n if (typeof carbonIntensity !== \"boolean\") {\n throw new Error(\n `perByte expects a boolean for the carbon intensity value. Received: ${carbonIntensity}`\n );\n }\n\n const co2ValuesbyComponent = this.co2byComponent(\n energyBycomponent,\n carbonIntensity,\n options\n );\n\n // pull out our values\u2026\n const co2Values = Object.values(co2ValuesbyComponent);\n const co2ValuesSum = co2Values.reduce(\n (prevValue, currentValue) => prevValue + currentValue\n );\n\n if (segmentResults) {\n return { ...co2ValuesbyComponent, total: co2ValuesSum };\n }\n\n return co2ValuesSum;\n }\n\n /**\n * Accept a figure for bytes transferred and return a single figure for CO2\n * emissions. This method applies caching assumptions from the original Sustainable Web Design model.\n *\n * @param {number} bytes - the data transferred in bytes\n * @param {number} `carbonIntensity` the carbon intensity for datacentre (average figures, not marginal ones)\n * @return {number} the total number in grams of CO2 equivalent emissions\n */\n perVisit(\n bytes,\n carbonIntensity = false,\n segmentResults = false,\n options = {}\n ) {\n const energyBycomponent = this.energyPerVisitByComponent(bytes, options);\n\n if (typeof carbonIntensity !== \"boolean\") {\n // otherwise when faced with non numeric values throw an error\n throw new Error(\n `perVisit expects a boolean for the carbon intensity value. Received: ${carbonIntensity}`\n );\n }\n\n const co2ValuesbyComponent = this.co2byComponent(\n energyBycomponent,\n carbonIntensity,\n options\n );\n\n // pull out our values\u2026\n const co2Values = Object.values(co2ValuesbyComponent);\n const co2ValuesSum = co2Values.reduce(\n (prevValue, currentValue) => prevValue + currentValue\n );\n\n if (segmentResults) {\n return { ...co2ValuesbyComponent, total: co2ValuesSum };\n }\n\n // so we can return their sum\n return co2ValuesSum;\n }\n\n /**\n * Accept a figure for bytes transferred and return the number of kilowatt hours used\n * by the total system for this data transfer\n *\n * @param {number} bytes\n * @return {number} the number of kilowatt hours used\n */\n energyPerByte(bytes) {\n const energyByComponent = this.energyPerByteByComponent(bytes);\n\n // pull out our values\u2026\n const energyValues = Object.values(energyByComponent);\n\n // so we can return their sum\n return energyValues.reduce(\n (prevValue, currentValue) => prevValue + currentValue\n );\n }\n\n /**\n * Accept a figure for bytes transferred, and return an object containing figures\n * per system component, with the caching assumptions applied. This tries to account\n * for webpages being loaded from a cache by browsers, so if you had a thousand page views,\n * and tried to work out the energy per visit, the numbers would reflect the reduced amounts\n * of transfer.\n *\n * @param {number} bytes - the data transferred in bytes for loading a webpage\n * @param {number} firstView - what percentage of visits are loading this page for the first time\n * @param {number} returnView - what percentage of visits are loading this page for subsequent times\n * @param {number} dataReloadRatio - what percentage of a page is reloaded on each subsequent page view\n *\n * @return {object} Object containing the energy in kilowatt hours, keyed by system component\n */\n energyPerVisitByComponent(\n bytes,\n options = {},\n firstView = FIRST_TIME_VIEWING_PERCENTAGE,\n returnView = RETURNING_VISITOR_PERCENTAGE,\n dataReloadRatio = PERCENTAGE_OF_DATA_LOADED_ON_SUBSEQUENT_LOAD\n ) {\n if (options.dataReloadRatio) {\n dataReloadRatio = options.dataReloadRatio;\n }\n\n if (options.firstVisitPercentage) {\n firstView = options.firstVisitPercentage;\n }\n\n if (options.returnVisitPercentage) {\n returnView = options.returnVisitPercentage;\n }\n\n const energyBycomponent = this.energyPerByteByComponent(bytes);\n const cacheAdjustedSegmentEnergy = {};\n\n log({ energyBycomponent });\n const energyValues = Object.values(energyBycomponent);\n\n // for this, we want\n for (const [key, value] of Object.entries(energyBycomponent)) {\n // represent the first load\n cacheAdjustedSegmentEnergy[`${key} - first`] = value * firstView;\n\n // then represent the subsequent load\n cacheAdjustedSegmentEnergy[`${key} - subsequent`] =\n value * returnView * dataReloadRatio;\n }\n log({ cacheAdjustedSegmentEnergy });\n\n return cacheAdjustedSegmentEnergy;\n }\n\n /**\n * Accept a figure for bytes, and return the total figure for energy per visit\n * using the default caching assumptions for loading a single website\n *\n * @param {number} bytes\n * @return {number} the total energy use for the visit, after applying the caching assumptions\n */\n energyPerVisit(bytes) {\n // fetch the values using the default caching assumptions\n // const energyValues = Object.values(this.energyPerVisitByComponent(bytes));\n\n let firstVisits = 0;\n let subsequentVisits = 0;\n\n const energyBycomponent = Object.entries(\n this.energyPerVisitByComponent(bytes)\n );\n\n for (const [key, val] of energyBycomponent) {\n if (key.indexOf(\"first\") > 0) {\n firstVisits += val;\n }\n }\n\n for (const [key, val] of energyBycomponent) {\n if (key.indexOf(\"subsequent\") > 0) {\n subsequentVisits += val;\n }\n }\n\n return firstVisits + subsequentVisits;\n }\n\n emissionsPerVisitInGrams(\n energyPerVisit,\n carbonintensity = GLOBAL_GRID_INTENSITY\n ) {\n return formatNumber(energyPerVisit * carbonintensity);\n }\n\n annualEnergyInKwh(energyPerVisit, monthlyVisitors = 1000) {\n return energyPerVisit * monthlyVisitors * 12;\n }\n\n annualEmissionsInGrams(co2grams, monthlyVisitors = 1000) {\n return co2grams * monthlyVisitors * 12;\n }\n\n annualSegmentEnergy(annualEnergy) {\n return {\n consumerDeviceEnergy: formatNumber(annualEnergy * END_USER_DEVICE_ENERGY),\n networkEnergy: formatNumber(annualEnergy * NETWORK_ENERGY),\n dataCenterEnergy: formatNumber(annualEnergy * DATACENTER_ENERGY),\n productionEnergy: formatNumber(annualEnergy * PRODUCTION_ENERGY),\n };\n }\n}\n\nexport { SustainableWebDesign };\nexport default SustainableWebDesign;\n"],
|
|
5
|
-
"mappings": "
|
|
6
|
-
"names": [
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AASA,mBAAyB;AAGzB,uBAYO;AACP,qBAA6B;AAf7B,MAAM,MAAM,0BAAa,6BAA6B;AAiBtD,MAAM,qBAAqB;AAAA,EACzB,YAAY,SAAS;AACnB,SAAK,UAAU;AAAA,EACjB;AAAA,EAUA,yBAAyB,OAAO;AAC9B,UAAM,sBAAsB,QAAQ,0BAAS;AAC7C,UAAM,cAAc,sBAAsB;AAG1C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
package/dist/esm/co2.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
"use strict";
|
|
1
2
|
import OneByte from "./1byte.js";
|
|
2
3
|
import SustainableWebDesign from "./sustainable-web-design.js";
|
|
3
4
|
import {
|
|
@@ -13,10 +14,8 @@ class CO2 {
|
|
|
13
14
|
} else if ((options == null ? void 0 : options.model) === "swd") {
|
|
14
15
|
this.model = new SustainableWebDesign();
|
|
15
16
|
} else if (options == null ? void 0 : options.model) {
|
|
16
|
-
throw new Error(
|
|
17
|
-
|
|
18
|
-
See https://developers.thegreenwebfoundation.org/co2js/models/ to learn more about the models available in CO2.js.`
|
|
19
|
-
);
|
|
17
|
+
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.
|
|
18
|
+
See https://developers.thegreenwebfoundation.org/co2js/models/ to learn more about the models available in CO2.js.`);
|
|
20
19
|
}
|
|
21
20
|
if ((options == null ? void 0 : options.results) === "segment") {
|
|
22
21
|
this.model.results = {
|
|
@@ -36,10 +35,8 @@ See https://developers.thegreenwebfoundation.org/co2js/models/ to learn more abo
|
|
|
36
35
|
if ((_a = this.model) == null ? void 0 : _a.perVisit) {
|
|
37
36
|
return this.model.perVisit(bytes, green, this.model.results.segment);
|
|
38
37
|
} else {
|
|
39
|
-
throw new Error(
|
|
40
|
-
|
|
41
|
-
See https://developers.thegreenwebfoundation.org/co2js/methods/ to learn more about the methods available in CO2.js.`
|
|
42
|
-
);
|
|
38
|
+
throw new Error(`The perVisit() method is not supported in the model you are using. Try using perByte() instead.
|
|
39
|
+
See https://developers.thegreenwebfoundation.org/co2js/methods/ to learn more about the methods available in CO2.js.`);
|
|
43
40
|
}
|
|
44
41
|
}
|
|
45
42
|
perByteTrace(bytes, green = false, options = {}) {
|
|
@@ -49,12 +46,7 @@ See https://developers.thegreenwebfoundation.org/co2js/methods/ to learn more ab
|
|
|
49
46
|
adjustments = parseOptions(options);
|
|
50
47
|
}
|
|
51
48
|
return {
|
|
52
|
-
co2: this.model.perByte(
|
|
53
|
-
bytes,
|
|
54
|
-
green,
|
|
55
|
-
this.model.results.segment,
|
|
56
|
-
adjustments
|
|
57
|
-
),
|
|
49
|
+
co2: this.model.perByte(bytes, green, this.model.results.segment, adjustments),
|
|
58
50
|
green,
|
|
59
51
|
variables: {
|
|
60
52
|
description: "Below are the variables used to calculate this CO2 estimate.",
|
|
@@ -77,12 +69,7 @@ See https://developers.thegreenwebfoundation.org/co2js/methods/ to learn more ab
|
|
|
77
69
|
adjustments = parseOptions(options);
|
|
78
70
|
}
|
|
79
71
|
return {
|
|
80
|
-
co2: this.model.perVisit(
|
|
81
|
-
bytes,
|
|
82
|
-
green,
|
|
83
|
-
this.model.results.segment,
|
|
84
|
-
adjustments
|
|
85
|
-
),
|
|
72
|
+
co2: this.model.perVisit(bytes, green, this.model.results.segment, adjustments),
|
|
86
73
|
green,
|
|
87
74
|
variables: {
|
|
88
75
|
description: "Below are the variables used to calculate this CO2 estimate.",
|
|
@@ -100,10 +87,8 @@ See https://developers.thegreenwebfoundation.org/co2js/methods/ to learn more ab
|
|
|
100
87
|
}
|
|
101
88
|
};
|
|
102
89
|
} else {
|
|
103
|
-
throw new Error(
|
|
104
|
-
|
|
105
|
-
See https://developers.thegreenwebfoundation.org/co2js/methods/ to learn more about the methods available in CO2.js.`
|
|
106
|
-
);
|
|
90
|
+
throw new Error(`The perVisitDetailed() method is not supported in the model you are using. Try using perByte() instead.
|
|
91
|
+
See https://developers.thegreenwebfoundation.org/co2js/methods/ to learn more about the methods available in CO2.js.`);
|
|
107
92
|
}
|
|
108
93
|
}
|
|
109
94
|
perDomain(pageXray, greenDomains) {
|
|
@@ -137,10 +122,7 @@ See https://developers.thegreenwebfoundation.org/co2js/methods/ to learn more ab
|
|
|
137
122
|
for (let asset of pageXray.assets) {
|
|
138
123
|
const domain = new URL(asset.url).domain;
|
|
139
124
|
const transferSize = asset.transferSize;
|
|
140
|
-
const co2ForTransfer = this.perByte(
|
|
141
|
-
transferSize,
|
|
142
|
-
greenDomains && greenDomains.indexOf(domain) > -1
|
|
143
|
-
);
|
|
125
|
+
const co2ForTransfer = this.perByte(transferSize, greenDomains && greenDomains.indexOf(domain) > -1);
|
|
144
126
|
const contentType = asset.type;
|
|
145
127
|
if (!co2PerContentType[contentType]) {
|
|
146
128
|
co2PerContentType[contentType] = { co2: 0, transferSize: 0 };
|
|
@@ -164,10 +146,7 @@ See https://developers.thegreenwebfoundation.org/co2js/methods/ to learn more ab
|
|
|
164
146
|
for (let asset of pageXray.assets) {
|
|
165
147
|
const domain = new URL(asset.url).domain;
|
|
166
148
|
const transferSize = asset.transferSize;
|
|
167
|
-
const co2ForTransfer = this.perByte(
|
|
168
|
-
transferSize,
|
|
169
|
-
greenDomains && greenDomains.indexOf(domain) > -1
|
|
170
|
-
);
|
|
149
|
+
const co2ForTransfer = this.perByte(transferSize, greenDomains && greenDomains.indexOf(domain) > -1);
|
|
171
150
|
allAssets.push({ url: asset.url, co2: co2ForTransfer, transferSize });
|
|
172
151
|
}
|
|
173
152
|
allAssets.sort((a, b) => b.co2 - a.co2);
|
|
@@ -179,15 +158,9 @@ See https://developers.thegreenwebfoundation.org/co2js/methods/ to learn more ab
|
|
|
179
158
|
const firstPartyRegEx = pageXray.firstPartyRegEx;
|
|
180
159
|
for (let d of Object.keys(pageXray.domains)) {
|
|
181
160
|
if (!d.match(firstPartyRegEx)) {
|
|
182
|
-
thirdParty += this.perByte(
|
|
183
|
-
pageXray.domains[d].transferSize,
|
|
184
|
-
greenDomains && greenDomains.indexOf(d) > -1
|
|
185
|
-
);
|
|
161
|
+
thirdParty += this.perByte(pageXray.domains[d].transferSize, greenDomains && greenDomains.indexOf(d) > -1);
|
|
186
162
|
} else {
|
|
187
|
-
firstParty += this.perByte(
|
|
188
|
-
pageXray.domains[d].transferSize,
|
|
189
|
-
greenDomains && greenDomains.indexOf(d) > -1
|
|
190
|
-
);
|
|
163
|
+
firstParty += this.perByte(pageXray.domains[d].transferSize, greenDomains && greenDomains.indexOf(d) > -1);
|
|
191
164
|
}
|
|
192
165
|
}
|
|
193
166
|
return { firstParty, thirdParty };
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
const data = { "AFG": 120.482, "Africa": 483.78, "ALB": 23.438, "DZA": 488.393, "ASM": 687.5, "AGO": 168.594, "ATG": 657.143, "ARG": 337.533, "ARM": 222.678, "ABW": 591.398, "Asia": 531.78, "AUS": 503.179, "AUT": 157.52, "AZE": 532.902, "BHS": 698.113, "BHR": 489.924, "BGD": 581.485, "BRB": 644.86, "BLR": 415.774, "BEL": 165.363, "BLZ": 484.375, "BEN": 666.667, "BTN": 24.444, "BOL": 319.432, "BIH": 516.626, "BWA": 794.521, "BRA": 102.411, "BRN": 658.849, "BGR": 399.565, "BFA": 611.429, "BDI": 250, "CPV": 600, "KHM": 399.31, "CMR": 278.261, "CAN": 125.55, "CYM": 684.932, "CAF": 0, "TCD": 677.419, "CHL": 333.173, "CHN": 530.214, "COL": 169.309, "COM": 714.286, "COG": 398.01, "COD": 25.362, "COK": 400, "CRI": 37.213, "CIV": 390.71, "HRV": 246.459, "CUB": 574.684, "CYP": 589.354, "CZE": 415.345, "DNK": 180.709, "DJI": 666.667, "DMA": 529.412, "DOM": 540.387, "ECU": 169.384, "EGY": 485.367, "SLV": 194.226, "GNQ": 492.958, "ERI": 688.889, "EST": 464.029, "SWZ": 189.189, "ETH": 25.187, "EU": 277.35, "Europe": 298.01, "FLK": 500, "FRO": 428.571, "FJI": 292.035, "FIN": 130.977, "FRA": 84.817, "GUF": 254.717, "PYF": 471.429, "G20": 440.39, "G7": 341.23, "GAB": 397.38, "GMB": 700, "GEO": 134.831, "DEU": 385.467, "GHA": 362.942, "GRC": 343.822, "GRL": 133.333, "GRD": 714.286, "GLP": 611.765, "GUM": 670.33, "GTM": 313.019, "GIN": 208.633, "GNB": 750, "GUY": 634.146, "HTI": 606.061, "HND": 374.269, "HKG": 684.823, "HUN": 222.973, "ISL": 28.557, "IND": 632.406, "IDN": 623.281, "IRN": 493.595, "IRQ": 500.206, "IRL": 345.347, "ISR": 550.493, "ITA": 371.692, "JAM": 524.138, "JPN": 483.039, "JOR": 399.909, "KAZ": 635.574, "KEN": 99.919, "KIR": 666.667, "XKX": 766.605, "KWT": 489.61, "KGZ": 104.354, "LAO": 242.182, "Latin America and Caribbean": 238.27, "LVA": 181.818, "LBN": 545.941, "LSO": 20, "LBR": 304.348, "LBY": 496.565, "LTU": 194.245, "LUX": 168.142, "MAC": 491.525, "MDG": 483.254, "MWI": 133.803, "MYS": 543.737, "MDV": 651.515, "MLI": 466.077, "MLT": 442.478, "MTQ": 698.63, "MRT": 526.596, "MUS": 611.111, "MEX": 421.402, "Middle East": 514.89, "MDA": 685.874, "MNG": 749.656, "MNE": 399.381, "MSR": 1e3, "MAR": 610.412, "MOZ": 125.063, "MMR": 348.837, "NAM": 63.694, "NRU": 750, "NPL": 24.51, "NLD": 354.988, "NCL": 610.119, "NZL": 98.639, "NIC": 354.212, "NER": 622.222, "NGA": 368.421, "North America": 336.4, "PRK": 157.785, "MKD": 529.328, "NOR": 28.818, "Oceania": 446.65, "OECD": 338.75, "OMN": 488.272, "PAK": 343.366, "PSE": 465.116, "PAN": 193.923, "PNG": 526.749, "PRY": 25.487, "PER": 254.521, "POL": 634.579, "PRT": 234.029, "PRI": 681.469, "QAT": 489.867, "REU": 519.031, "ROU": 264.121, "RUS": 367.189, "RWA": 294.118, "KNA": 681.818, "LCA": 685.714, "SPM": 800, "VCT": 500, "WSM": 470.588, "STP": 600, "SAU": 571.336, "SEN": 502.674, "SRB": 569.375, "SYC": 615.385, "SLE": 47.619, "SGP": 489.234, "SVK": 140.666, "SVN": 237.378, "SLB": 727.273, "SOM": 634.146, "ZAF": 709.002, "KOR": 435.689, "SSD": 684.211, "ESP": 217.373, "LKA": 479.829, "SDN": 242.917, "SUR": 356.436, "SWE": 45.084, "CHE": 45.622, "SYR": 540.573, "TWN": 561.431, "TJK": 84.136, "TZA": 361.077, "THA": 503.07, "PHL": 582.365, "TGO": 507.937, "TON": 625, "TTO": 520.046, "TUN": 469.428, "TUR": 413.628, "TKM": 544.393, "TCA": 703.704, "UGA": 52.273, "UKR": 206.539, "ARE": 461.845, "GBR": 257.379, "USA": 367.768, "URY": 116.168, "UZB": 506.164, "VUT": 571.429, "VEN": 189.446, "VNM": 376.828, "VGB": 714.286, "VIR": 685.714, "World": 435.99, "YEM": 528.409, "ZMB": 84.698, "ZWE": 393.035 };
|
|
2
|
+
const type = "average";
|
|
3
|
+
var average_intensities_min_default = { data, type };
|
|
4
|
+
export {
|
|
5
|
+
data,
|
|
6
|
+
average_intensities_min_default as default,
|
|
7
|
+
type
|
|
8
|
+
};
|
|
@@ -18,20 +18,16 @@ function parseOptions(options) {
|
|
|
18
18
|
if (device) {
|
|
19
19
|
if (typeof device === "object") {
|
|
20
20
|
if (!averageIntensity.data[(_a = device.country) == null ? void 0 : _a.toUpperCase()]) {
|
|
21
|
-
console.warn(
|
|
22
|
-
`"${device.country}" is not a valid country. Please use a valid 3 digit ISO 3166 country code.
|
|
21
|
+
console.warn(`"${device.country}" is not a valid country. Please use a valid 3 digit ISO 3166 country code.
|
|
23
22
|
See https://developers.thegreenwebfoundation.org/co2js/data/ for more information.
|
|
24
|
-
Falling back to global average grid intensity.`
|
|
25
|
-
);
|
|
23
|
+
Falling back to global average grid intensity.`);
|
|
26
24
|
adjustments.gridIntensity["device"] = {
|
|
27
25
|
value: GLOBAL_GRID_INTENSITY
|
|
28
26
|
};
|
|
29
27
|
}
|
|
30
28
|
adjustments.gridIntensity["device"] = {
|
|
31
29
|
country: device.country,
|
|
32
|
-
value: parseFloat(
|
|
33
|
-
averageIntensity.data[(_b = device.country) == null ? void 0 : _b.toUpperCase()]
|
|
34
|
-
)
|
|
30
|
+
value: parseFloat(averageIntensity.data[(_b = device.country) == null ? void 0 : _b.toUpperCase()])
|
|
35
31
|
};
|
|
36
32
|
} else if (typeof device === "number") {
|
|
37
33
|
adjustments.gridIntensity["device"] = {
|
|
@@ -41,29 +37,23 @@ Falling back to global average grid intensity.`
|
|
|
41
37
|
adjustments.gridIntensity["device"] = {
|
|
42
38
|
value: GLOBAL_GRID_INTENSITY
|
|
43
39
|
};
|
|
44
|
-
console.warn(
|
|
45
|
-
|
|
46
|
-
Falling back to global average grid intensity.`
|
|
47
|
-
);
|
|
40
|
+
console.warn(`The device grid intensity must be a number or an object. You passed in a ${typeof device}.
|
|
41
|
+
Falling back to global average grid intensity.`);
|
|
48
42
|
}
|
|
49
43
|
}
|
|
50
44
|
if (dataCenter) {
|
|
51
45
|
if (typeof dataCenter === "object") {
|
|
52
46
|
if (!averageIntensity.data[(_c = dataCenter.country) == null ? void 0 : _c.toUpperCase()]) {
|
|
53
|
-
console.warn(
|
|
54
|
-
`"${dataCenter.country}" is not a valid country. Please use a valid 3 digit ISO 3166 country code.
|
|
47
|
+
console.warn(`"${dataCenter.country}" is not a valid country. Please use a valid 3 digit ISO 3166 country code.
|
|
55
48
|
See https://developers.thegreenwebfoundation.org/co2js/data/ for more information.
|
|
56
|
-
Falling back to global average grid intensity.`
|
|
57
|
-
);
|
|
49
|
+
Falling back to global average grid intensity.`);
|
|
58
50
|
adjustments.gridIntensity["dataCenter"] = {
|
|
59
51
|
value: GLOBAL_GRID_INTENSITY
|
|
60
52
|
};
|
|
61
53
|
}
|
|
62
54
|
adjustments.gridIntensity["dataCenter"] = {
|
|
63
55
|
country: dataCenter.country,
|
|
64
|
-
value: parseFloat(
|
|
65
|
-
averageIntensity.data[(_d = dataCenter.country) == null ? void 0 : _d.toUpperCase()]
|
|
66
|
-
)
|
|
56
|
+
value: parseFloat(averageIntensity.data[(_d = dataCenter.country) == null ? void 0 : _d.toUpperCase()])
|
|
67
57
|
};
|
|
68
58
|
} else if (typeof dataCenter === "number") {
|
|
69
59
|
adjustments.gridIntensity["dataCenter"] = {
|
|
@@ -73,29 +63,23 @@ Falling back to global average grid intensity.`
|
|
|
73
63
|
adjustments.gridIntensity["dataCenter"] = {
|
|
74
64
|
value: GLOBAL_GRID_INTENSITY
|
|
75
65
|
};
|
|
76
|
-
console.warn(
|
|
77
|
-
|
|
78
|
-
Falling back to global average grid intensity.`
|
|
79
|
-
);
|
|
66
|
+
console.warn(`The data center grid intensity must be a number or an object. You passed in a ${typeof dataCenter}.
|
|
67
|
+
Falling back to global average grid intensity.`);
|
|
80
68
|
}
|
|
81
69
|
}
|
|
82
70
|
if (network) {
|
|
83
71
|
if (typeof network === "object") {
|
|
84
72
|
if (!averageIntensity.data[(_e = network.country) == null ? void 0 : _e.toUpperCase()]) {
|
|
85
|
-
console.warn(
|
|
86
|
-
`"${network.country}" is not a valid country. Please use a valid 3 digit ISO 3166 country code.
|
|
73
|
+
console.warn(`"${network.country}" is not a valid country. Please use a valid 3 digit ISO 3166 country code.
|
|
87
74
|
See https://developers.thegreenwebfoundation.org/co2js/data/ for more information. Falling back to global average grid intensity.
|
|
88
|
-
Falling back to global average grid intensity.`
|
|
89
|
-
);
|
|
75
|
+
Falling back to global average grid intensity.`);
|
|
90
76
|
adjustments.gridIntensity["network"] = {
|
|
91
77
|
value: GLOBAL_GRID_INTENSITY
|
|
92
78
|
};
|
|
93
79
|
}
|
|
94
80
|
adjustments.gridIntensity["network"] = {
|
|
95
81
|
country: network.country,
|
|
96
|
-
value: parseFloat(
|
|
97
|
-
averageIntensity.data[(_f = network.country) == null ? void 0 : _f.toUpperCase()]
|
|
98
|
-
)
|
|
82
|
+
value: parseFloat(averageIntensity.data[(_f = network.country) == null ? void 0 : _f.toUpperCase()])
|
|
99
83
|
};
|
|
100
84
|
} else if (typeof network === "number") {
|
|
101
85
|
adjustments.gridIntensity["network"] = {
|
|
@@ -105,10 +89,8 @@ Falling back to global average grid intensity.`
|
|
|
105
89
|
adjustments.gridIntensity["network"] = {
|
|
106
90
|
value: GLOBAL_GRID_INTENSITY
|
|
107
91
|
};
|
|
108
|
-
console.warn(
|
|
109
|
-
|
|
110
|
-
Falling back to global average grid intensity.`
|
|
111
|
-
);
|
|
92
|
+
console.warn(`The network grid intensity must be a number or an object. You passed in a ${typeof network}.
|
|
93
|
+
Falling back to global average grid intensity.`);
|
|
112
94
|
}
|
|
113
95
|
}
|
|
114
96
|
}
|
|
@@ -118,17 +100,13 @@ Falling back to global average grid intensity.`
|
|
|
118
100
|
adjustments.dataReloadRatio = options.dataReloadRatio;
|
|
119
101
|
} else {
|
|
120
102
|
adjustments.dataReloadRatio = PERCENTAGE_OF_DATA_LOADED_ON_SUBSEQUENT_LOAD;
|
|
121
|
-
console.warn(
|
|
122
|
-
|
|
123
|
-
Falling back to default value.`
|
|
124
|
-
);
|
|
103
|
+
console.warn(`The dataReloadRatio option must be a number between 0 and 1. You passed in ${options.dataReloadRatio}.
|
|
104
|
+
Falling back to default value.`);
|
|
125
105
|
}
|
|
126
106
|
} else {
|
|
127
107
|
adjustments.dataReloadRatio = PERCENTAGE_OF_DATA_LOADED_ON_SUBSEQUENT_LOAD;
|
|
128
|
-
console.warn(
|
|
129
|
-
|
|
130
|
-
Falling back to default value.`
|
|
131
|
-
);
|
|
108
|
+
console.warn(`The dataReloadRatio option must be a number. You passed in a ${typeof options.dataReloadRatio}.
|
|
109
|
+
Falling back to default value.`);
|
|
132
110
|
}
|
|
133
111
|
}
|
|
134
112
|
if (options == null ? void 0 : options.firstVisitPercentage) {
|
|
@@ -137,17 +115,13 @@ Falling back to default value.`
|
|
|
137
115
|
adjustments.firstVisitPercentage = options.firstVisitPercentage;
|
|
138
116
|
} else {
|
|
139
117
|
adjustments.firstVisitPercentage = FIRST_TIME_VIEWING_PERCENTAGE;
|
|
140
|
-
console.warn(
|
|
141
|
-
|
|
142
|
-
Falling back to default value.`
|
|
143
|
-
);
|
|
118
|
+
console.warn(`The firstVisitPercentage option must be a number between 0 and 1. You passed in ${options.firstVisitPercentage}.
|
|
119
|
+
Falling back to default value.`);
|
|
144
120
|
}
|
|
145
121
|
} else {
|
|
146
122
|
adjustments.firstVisitPercentage = FIRST_TIME_VIEWING_PERCENTAGE;
|
|
147
|
-
console.warn(
|
|
148
|
-
|
|
149
|
-
Falling back to default value.`
|
|
150
|
-
);
|
|
123
|
+
console.warn(`The firstVisitPercentage option must be a number. You passed in a ${typeof options.firstVisitPercentage}.
|
|
124
|
+
Falling back to default value.`);
|
|
151
125
|
}
|
|
152
126
|
}
|
|
153
127
|
if (options == null ? void 0 : options.returnVisitPercentage) {
|
|
@@ -156,17 +130,13 @@ Falling back to default value.`
|
|
|
156
130
|
adjustments.returnVisitPercentage = options.returnVisitPercentage;
|
|
157
131
|
} else {
|
|
158
132
|
adjustments.returnVisitPercentage = RETURNING_VISITOR_PERCENTAGE;
|
|
159
|
-
console.warn(
|
|
160
|
-
|
|
161
|
-
Falling back to default value.`
|
|
162
|
-
);
|
|
133
|
+
console.warn(`The returnVisitPercentage option must be a number between 0 and 1. You passed in ${options.returnVisitPercentage}.
|
|
134
|
+
Falling back to default value.`);
|
|
163
135
|
}
|
|
164
136
|
} else {
|
|
165
137
|
adjustments.returnVisitPercentage = RETURNING_VISITOR_PERCENTAGE;
|
|
166
|
-
console.warn(
|
|
167
|
-
|
|
168
|
-
Falling back to default value.`
|
|
169
|
-
);
|
|
138
|
+
console.warn(`The returnVisitPercentage option must be a number. You passed in a ${typeof options.returnVisitPercentage}.
|
|
139
|
+
Falling back to default value.`);
|
|
170
140
|
}
|
|
171
141
|
}
|
|
172
142
|
return adjustments;
|
package/dist/esm/hosting-api.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
"use strict";
|
|
1
2
|
import debugFactory from "debug";
|
|
2
3
|
const log = debugFactory("tgwf:hostingAPI");
|
|
3
4
|
function check(domain) {
|
|
@@ -8,9 +9,7 @@ function check(domain) {
|
|
|
8
9
|
}
|
|
9
10
|
}
|
|
10
11
|
async function checkAgainstAPI(domain) {
|
|
11
|
-
const req = await fetch(
|
|
12
|
-
`https://api.thegreenwebfoundation.org/greencheck/${domain}`
|
|
13
|
-
);
|
|
12
|
+
const req = await fetch(`https://api.thegreenwebfoundation.org/greencheck/${domain}`);
|
|
14
13
|
const res = await req.json();
|
|
15
14
|
return res.green;
|
|
16
15
|
}
|
package/dist/esm/hosting.js
CHANGED
package/dist/esm/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import co2 from "./co2.js";
|
|
2
2
|
import hosting from "./hosting.js";
|
|
3
|
-
import averageIntensity from "./data/average-intensities
|
|
3
|
+
import averageIntensity from "./data/average-intensities.min.js";
|
|
4
4
|
import marginalIntensity from "./data/marginal-intensities-2021.min.js";
|
|
5
5
|
var src_default = { co2, hosting, averageIntensity, marginalIntensity };
|
|
6
6
|
export {
|