@tgwf/co2 0.16.6 → 0.16.7

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.
Files changed (39) hide show
  1. package/dist/cjs/1byte.js +6 -0
  2. package/dist/cjs/1byte.js.map +1 -1
  3. package/dist/cjs/co2.js +42 -0
  4. package/dist/cjs/co2.js.map +1 -1
  5. package/dist/cjs/constants/index.js +4 -0
  6. package/dist/cjs/constants/index.js.map +1 -1
  7. package/dist/cjs/data/average-intensities.min.js +1 -1
  8. package/dist/cjs/data/average-intensities.min.js.map +2 -2
  9. package/dist/cjs/data/emaps-yearly-2024.min.js +26 -0
  10. package/dist/cjs/data/emaps-yearly-2024.min.js.map +7 -0
  11. package/dist/cjs/helpers/index.js +1 -1
  12. package/dist/cjs/helpers/index.js.map +1 -1
  13. package/dist/cjs/hosting-api.js +4 -0
  14. package/dist/cjs/hosting-api.js.map +1 -1
  15. package/dist/cjs/hosting-json.js.map +1 -1
  16. package/dist/cjs/hosting-json.node.js +4 -0
  17. package/dist/cjs/hosting-json.node.js.map +1 -1
  18. package/dist/cjs/hosting-node.js +4 -0
  19. package/dist/cjs/hosting-node.js.map +1 -1
  20. package/dist/cjs/hosting.js +4 -0
  21. package/dist/cjs/hosting.js.map +1 -1
  22. package/dist/cjs/index-node.js +4 -0
  23. package/dist/cjs/index-node.js.map +1 -1
  24. package/dist/cjs/index.js +9 -5
  25. package/dist/cjs/index.js.map +1 -1
  26. package/dist/cjs/sustainable-web-design-v3.js +74 -0
  27. package/dist/cjs/sustainable-web-design-v3.js.map +1 -1
  28. package/dist/cjs/sustainable-web-design-v4.js +32 -0
  29. package/dist/cjs/sustainable-web-design-v4.js.map +1 -1
  30. package/dist/esm/1byte.js +6 -0
  31. package/dist/esm/co2.js +43 -10
  32. package/dist/esm/data/average-intensities.min.js +1 -1
  33. package/dist/esm/data/emaps-yearly-2024.min.js +6 -0
  34. package/dist/esm/helpers/index.js +14 -28
  35. package/dist/esm/hosting-api.js +2 -2
  36. package/dist/esm/index.js +2 -2
  37. package/dist/esm/sustainable-web-design-v3.js +78 -4
  38. package/dist/esm/sustainable-web-design-v4.js +32 -0
  39. package/package.json +2 -2
@@ -2,6 +2,6 @@
2
2
  "version": 3,
3
3
  "sources": ["../../src/hosting-json.js"],
4
4
  "sourcesContent": ["\"use strict\";\n\n/**\n * Check if a string or array of domains has been provided\n * @param {string|array} domain - The domain to check, or an array of domains to be checked.\n */\nasync function check(domain, db) {\n // is it a single domain or an array of them?\n if (typeof domain === \"string\") {\n return checkInJSON(domain, db);\n } else {\n return checkDomainsInJSON(domain, db);\n }\n}\n\n/**\n * Check if a domain is hosted by a green web host by querying the database.\n * @param {string} domain - The domain to check.\n * @param {object} db - The database to check against.\n * @returns {boolean} - A boolean indicating whether the domain is hosted by a green web host.\n */\nfunction checkInJSON(domain, db) {\n if (db.indexOf(domain) > -1) {\n return true;\n }\n return false;\n}\n\n/**\n * Extract the green domains from the results of a green check.\n * @param {object} greenResults - The results of a green check.\n * @returns {array} - An array of domains that are hosted by a green web host.\n */\nfunction greenDomainsFromResults(greenResults) {\n const entries = Object.entries(greenResults);\n const greenEntries = entries.filter(([key, val]) => val.green);\n\n return greenEntries.map(([key, val]) => val.url);\n}\n\n/**\n * Check if an array of domains is hosted by a green web host by querying the database.\n * @param {array} domains - An array of domains to check.\n * @param {object} db - The database to check against.\n * @returns {array} - An array of domains that are hosted by a green web host.\n */\nfunction checkDomainsInJSON(domains, db) {\n let greenDomains = [];\n\n for (let domain of domains) {\n if (db.indexOf(domain) > -1) {\n greenDomains.push(domain);\n }\n }\n return greenDomains;\n}\n\n/**\n * Find the provided information a string or array of domains\n * @param {string|array} domain - The domain to check, or an array of domains to be checked.\n */\nfunction find(domain, db) {\n // is it a single domain or an array of them?\n if (typeof domain === \"string\") {\n return findInJSON(domain, db);\n } else {\n return findDomainsInJSON(domain, db);\n }\n}\n\n/**\n * Check if a domain is hosted by a green web host by querying the database.\n * @param {string} domain - The domain to check.\n * @param {object} db - The database to check against.\n * @returns {object} - An object representing the domain provided host information.\n */\nfunction findInJSON(domain, db) {\n if (db.indexOf(domain) > -1) {\n return domain;\n }\n return {\n url: domain,\n green: false,\n };\n}\n\n/**\n * Check if an array of domains is hosted by a green web host by querying the database.\n * @param {array} domains - An array of domains to check.\n * @param {object} db - The database to check against.\n * @returns {array} - A dictionary of domain to provided host information.\n */\nfunction findDomainsInJSON(domains, db) {\n const result = {};\n for (let domain of domains) {\n result[domain] = findInJSON(domain, db);\n }\n return result;\n}\n\nmodule.exports = {\n check,\n greenDomainsFromResults,\n find,\n};\n"],
5
- "mappings": ";AAMA,eAAe,MAAM,QAAQ,IAAI;AAE/B,MAAI,OAAO,WAAW,UAAU;AAC9B,WAAO,YAAY,QAAQ,EAAE;AAAA,EAC/B,OAAO;AACL,WAAO,mBAAmB,QAAQ,EAAE;AAAA,EACtC;AACF;AAQA,SAAS,YAAY,QAAQ,IAAI;AAC/B,MAAI,GAAG,QAAQ,MAAM,IAAI,IAAI;AAC3B,WAAO;AAAA,EACT;AACA,SAAO;AACT;AAOA,SAAS,wBAAwB,cAAc;AAC7C,QAAM,UAAU,OAAO,QAAQ,YAAY;AAC3C,QAAM,eAAe,QAAQ,OAAO,CAAC,CAAC,KAAK,GAAG,MAAM,IAAI,KAAK;AAE7D,SAAO,aAAa,IAAI,CAAC,CAAC,KAAK,GAAG,MAAM,IAAI,GAAG;AACjD;AAQA,SAAS,mBAAmB,SAAS,IAAI;AACvC,MAAI,eAAe,CAAC;AAEpB,WAAS,UAAU,SAAS;AAC1B,QAAI,GAAG,QAAQ,MAAM,IAAI,IAAI;AAC3B,mBAAa,KAAK,MAAM;AAAA,IAC1B;AAAA,EACF;AACA,SAAO;AACT;AAMA,SAAS,KAAK,QAAQ,IAAI;AAExB,MAAI,OAAO,WAAW,UAAU;AAC9B,WAAO,WAAW,QAAQ,EAAE;AAAA,EAC9B,OAAO;AACL,WAAO,kBAAkB,QAAQ,EAAE;AAAA,EACr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AAQA,SAAS,WAAW,QAAQ,IAAI;AAC9B,MAAI,GAAG,QAAQ,MAAM,IAAI,IAAI;AAC3B,WAAO;AAAA,EACT;AACA,SAAO;AAAA,IACL,KAAK;AAAA,IACL,OAAO;AAAA,EACT;AACF;AAQA,SAAS,kBAAkB,SAAS,IAAI;AACtC,QAAM,SAAS,CAAC;AAChB,WAAS,UAAU,SAAS;AAC1B,WAAO,MAAM,IAAI,WAAW,QAAQ,EAAE;AAAA,EACxC;AACA,SAAO;AACT;AAEA,OAAO,UAAU;AAAA,EACf;AAAA,EACA;AAAA,EACA;AACF;",
6
6
  "names": []
7
7
  }
@@ -14,6 +14,10 @@ var __copyProps = (to, from, except, desc) => {
14
14
  return to;
15
15
  };
16
16
  var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
17
+ // If the importer is in node compatibility mode or this is not an ESM
18
+ // file that has been converted to a CommonJS file using a Babel-
19
+ // compatible transform (i.e. "__esModule" has not been set), then set
20
+ // "default" to the CommonJS "module.exports" for node compatibility.
17
21
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
18
22
  mod
19
23
  ));
@@ -2,6 +2,6 @@
2
2
  "version": 3,
3
3
  "sources": ["../../src/hosting-json.node.js"],
4
4
  "sourcesContent": ["\"use strict\";\n\nimport fs from \"fs\";\nimport zlib from \"zlib\";\nimport { promisify } from \"util\";\n\nconst readFile = promisify(fs.readFile);\nconst gunzip = promisify(zlib.gunzip);\n\n/**\n * Converts a readable stream to a string.\n * @param {ReadableStream} stream - The readable stream to convert.\n * @returns {Promise<string>} A promise that resolves to the string representation of the stream.\n */\nasync function streamToString(stream) {\n return new Promise((resolve, reject) => {\n const chunks = [];\n stream.on(\"error\", reject);\n stream.on(\"data\", (chunk) => chunks.push(chunk));\n stream.on(\"end\", () => resolve(Buffer.concat(chunks)));\n });\n}\n\n/**\n * Get the contents of a gzipped file as a JSON string.\n * @param {string} jsonPath - The path to the gzipped JSON file.\n * @returns {Promise<string>} A promise that resolves to the JSON string.\n */\nasync function getGzippedFileAsJson(jsonPath) {\n const readStream = fs.createReadStream(jsonPath);\n const text = await streamToString(readStream);\n const unzipped = await gunzip(text);\n return unzipped.toString();\n}\n\n/**\n * Loads JSON data from a file path.\n * @param {string} jsonPath - The path to the JSON file.\n * @returns {Promise<object>} A promise that resolves to the parsed JSON object.\n */\nasync function loadJSON(jsonPath) {\n const jsonBuffer = jsonPath.endsWith(\".gz\")\n ? await getGzippedFileAsJson(jsonPath)\n : await readFile(jsonPath);\n return JSON.parse(jsonBuffer);\n}\n\nmodule.exports = {\n loadJSON,\n};\n"],
5
- "mappings": ";;;;;;;;;;;;;;;;;;;AAEA,gBAAe;AACf,kBAAiB;AACjB,kBAA0B;AAE1B,MAAM,eAAW,uBAAU,UAAAA,QAAG,QAAQ;AACtC,MAAM,aAAS,uBAAU,YAAAC,QAAK,MAAM;AAOpC,eAAe,eAAe,QAAQ;AACpC,SAAO,IAAI,QAAQ,CAAC,SAAS,WAAW;AACtC,UAAM,SAAS,CAAC;AAChB,WAAO,GAAG,SAAS,MAAM;AACzB,WAAO,GAAG,QAAQ,CAAC,UAAU,OAAO,KAAK,KAAK,CAAC;AAC/C,WAAO,GAAG,OAAO,MAAM,QAAQ,OAAO,OAAO,MAAM,CAAC,CAAC;AAAA,EACvD,CAAC;AACH;AAOA,eAAe,qBAAqB,UAAU;AAC5C,QAAM,aAAa,UAAAD,QAAG,iBAAiB,QAAQ;AAC/C,QAAM,OAAO,MAAM,eAAe,UAAU;AAC5C,QAAM,WAAW,MAAM,OAAO,IAAI;AAClC,SAAO,SAAS,SAAS;AAC3B;AAOA,eAAe,SAAS,UAAU;AAChC,QAAM,aAAa,SAAS,SAAS,KAAK,IACtC,MAAM,qBAAqB,QAAQ,IACnC,MAAM,SAAS,QAAQ;AAC3B,SAAO,KAAK,MAAM,UAAU;AAC9B;AAEA,OAAO,UAAU;AAAA,EACf;AACF;",
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;;;;AAEA,gBAAe;AACf,kBAAiB;AACjB,kBAA0B;AAE1B,MAAM,eAAW,uBAAU,UAAAA,QAAG,QAAQ;AACtC,MAAM,aAAS,uBAAU,YAAAC,QAAK,MAAM;AAOpC,eAAe,eAAe,QAAQ;AACpC,SAAO,IAAI,QAAQ,CAAC,SAAS,WAAW;AACtC,UAAM,SAAS,CAAC;AAChB,WAAO,GAAG,SAAS,MAAM;AACzB,WAAO,GAAG,QAAQ,CAAC,UAAU,OAAO,KAAK,KAAK,CAAC;AAC/C,WAAO,GAAG,OAAO,MAAM,QAAQ,OAAO,OAAO,MAAM,CAAC,CAAC;AAAA,EACvD,CAAC;AACH;AAOA,eAAe,qBAAqB,UAAU;AAC5C,QAAM,aAAa,UAAAD,QAAG,iBAAiB,QAAQ;AAC/C,QAAM,OAAO,MAAM,eAAe,UAAU;AAC5C,QAAM,WAAW,MAAM,OAAO,IAAI;AAClC,SAAO,SAAS,SAAS;AAC3B;AAOA,eAAe,SAAS,UAAU;AAChC,QAAM,aAAa,SAAS,SAAS,KAAK,IACtC,MAAM,qBAAqB,QAAQ,IACnC,MAAM,SAAS,QAAQ;AAC3B,SAAO,KAAK,MAAM,UAAU;AAC9B;AAEA,OAAO,UAAU;AAAA,EACf;AACF;",
6
6
  "names": ["fs", "zlib"]
7
7
  }
@@ -17,6 +17,10 @@ var __copyProps = (to, from, except, desc) => {
17
17
  return to;
18
18
  };
19
19
  var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ // If the importer is in node compatibility mode or this is not an ESM
21
+ // file that has been converted to a CommonJS file using a Babel-
22
+ // compatible transform (i.e. "__esModule" has not been set), then set
23
+ // "default" to the CommonJS "module.exports" for node compatibility.
20
24
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
21
25
  mod
22
26
  ));
@@ -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 hostingJSON from \"./hosting-json.js\";\nimport hostingJSONNode from \"./hosting-json.node.js\";\nimport { getApiRequestHeaders } from \"./helpers/index.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 * @param {string} userAgentIdentifier - Optional. The app, site, or organisation that is making the request.\n * @return {string}\n */\nasync function getBody(url, userAgentIdentifier) {\n return new Promise(function (resolve, reject) {\n // Do async job\n const req = https.get(\n url,\n { headers: getApiRequestHeaders(userAgentIdentifier) },\n function (res) {\n if (res.statusCode < 200 || res.statusCode >= 300) {\n return reject(\n new Error(\n `Could not get info from: ${url}. Status Code: ${res.statusCode}`\n )\n );\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 );\n req.end();\n });\n}\n\n/**\n * Check if a domain is hosted by a green web host.\n * @param {string|array} domain - The domain to check, or an array of domains to be checked.\n * @param {string[] | DomainCheckOptions} optionsOrDb - Optional. An object of domain check options, or a database list to use for lookups.\n * @param {string } userAgentIdentifier - Optional. The app, site, or organisation that is making the request.\n * @returns - A boolean if a string was provided, or an array of booleans if an array of domains was provided.\n * if a string was provided for `domain`: a boolean indicating whether the domain is hosted by a green web host if `options.verbose` is false,\n * otherwise an object representing the domain host information.\n * if an array was provided for `domain`: an array of domains that are hosted by a green web host if `options.verbose` is false,\n * otherwise a dictionary of domain to host information.\n */\n\nfunction check(domain, optionsOrDb, userAgentIdentifier) {\n let db,\n options = {};\n if (!db && Array.isArray(optionsOrDb)) {\n db = optionsOrDb;\n } else {\n options = optionsOrDb;\n if (userAgentIdentifier) {\n options = { ...options, userAgentIdentifier };\n }\n db = optionsOrDb?.db;\n }\n\n if (db && options?.verbose) {\n throw new Error(\"verbose mode cannot be used with a local lookup database\");\n }\n if (db) {\n return hostingJSON.check(domain, db);\n }\n // is it a single domain or an array of them?\n if (typeof domain === \"string\") {\n return checkAgainstAPI(domain, options);\n } else {\n return checkDomainsAgainstAPI(domain, options);\n }\n}\n\n/**\n * Check if a domain is hosted by a green web host by querying the Green Web Foundation API.\n * @param {string} domain - The domain to check.\n * @param {DomainCheckOptions} options\n * @returns {boolean} - A boolean indicating whether the domain is hosted by a green web host if `options.verbose` is false,\n * otherwise an object representing the domain host information.\n */\nasync function checkAgainstAPI(domain, options = {}) {\n const res = JSON.parse(\n await getBody(\n `https://api.thegreenwebfoundation.org/greencheck/${domain}`,\n options.userAgentIdentifier\n )\n );\n return options.verbose ? res : res.green;\n}\n\n/**\n * Check if an array of domains is hosted by a green web host by querying the Green Web Foundation API.\n * @param {array} domains - An array of domains to check.\n * @param {DomainCheckOptions} options\n * @returns {array} - An array of domains that are hosted by a green web host if `options.verbose` is false,\n * otherwise a dictionary of domain to host information.\n */\nasync function checkDomainsAgainstAPI(domains, options = {}) {\n try {\n const allGreenCheckResults = JSON.parse(\n await getBody(\n `https://api.thegreenwebfoundation.org/v2/greencheckmulti/${JSON.stringify(\n domains\n )}`,\n options.userAgentIdentifier\n )\n );\n return options.verbose\n ? allGreenCheckResults\n : hostingJSON.greenDomainsFromResults(allGreenCheckResults);\n } catch (e) {\n return options.verbose ? {} : [];\n }\n}\n\nexport default {\n check,\n greendomains: hostingJSON.greenDomainsFromResults,\n loadJSON: hostingJSONNode.loadJSON,\n};\n"],
5
- "mappings": ";;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAUA,mBAAkB;AAElB,0BAAwB;AACxB,+BAA4B;AAC5B,qBAAqC;AAUrC,eAAe,QAAQ,KAAK,qBAAqB;AAC/C,SAAO,IAAI,QAAQ,SAAU,SAAS,Q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;
19
19
  };
20
20
  var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
21
25
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
22
26
  mod
23
27
  ));
@@ -2,6 +2,6 @@
2
2
  "version": 3,
3
3
  "sources": ["../../src/hosting.js"],
4
4
  "sourcesContent": ["\"use strict\";\n\nimport hostingAPI from \"./hosting-api.js\";\n\n/**\n * @module hosting\n */\n\n/**\n * Check if a domain is hosted by a green web host.\n * @param {string|array} domain - The domain to check, or an array of domains to be checked.\n * @param {string} optionsOrAgentId - Optional. An object of domain check options, or a string\n * representing the app, site, or organisation that is making the request.\n * @returns - A boolean if a string was provided, or an array of booleans if an array of domains was provided.\n * if a string was provided for `domain`: a boolean indicating whether the domain is hosted by a green web host if `options.verbose` is false,\n * otherwise an object representing the domain host information.\n * if an array was provided for `domain`: an array of domains that are hosted by a green web host if `options.verbose` is false,\n * otherwise a dictionary of domain to host information.\n */\nfunction check(domain, optionsOrAgentId) {\n return hostingAPI.check(domain, optionsOrAgentId);\n}\n\nexport default check;\n"],
5
- "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAEA,yBAAuB;AAiBvB,SAAS,MAAM,QAAQ,kBAAkB;AACvC,SAAO,mBAAAA,QAAW,MAAM,QAAQ,gBAAgB;AAClD;AAEA,IAAO,kBAAQ;",
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAEA,yBAAuB;AAiBvB,SAAS,MAAM,QAAQ,kBAAkB;AACvC,SAAO,mBAAAA,QAAW,MAAM,QAAQ,gBAAgB;AAClD;AAEA,IAAO,kBAAQ;",
6
6
  "names": ["hostingAPI"]
7
7
  }
@@ -17,6 +17,10 @@ var __copyProps = (to, from, except, desc) => {
17
17
  return to;
18
18
  };
19
19
  var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ // If the importer is in node compatibility mode or this is not an ESM
21
+ // file that has been converted to a CommonJS file using a Babel-
22
+ // compatible transform (i.e. "__esModule" has not been set), then set
23
+ // "default" to the CommonJS "module.exports" for node compatibility.
20
24
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
21
25
  mod
22
26
  ));
@@ -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.min.js\";\nimport marginalIntensity from \"./data/marginal-intensities-2021.min.js\";\nexport { co2, hosting, averageIntensity, marginalIntensity };\n"],
5
- "mappings": ";;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA,yDAAAA;AAAA,EAAA,sBAAAC;AAAA,EAAA,mCAAAC;AAAA,EAAA,8DAAAC;AAAA;AAAA;AAAA,iBAAgB;AAChB,0BAAoB;AACpB,qCAA6B;AAC7B,2CAA8B;",
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA,yDAAAA;AAAA,EAAA,sBAAAC;AAAA,EAAA,mCAAAC;AAAA,EAAA,8DAAAC;AAAA;AAAA;AAAA,iBAAgB;AAChB,0BAAoB;AACpB,qCAA6B;AAC7B,2CAA8B;",
6
6
  "names": ["averageIntensity", "co2", "hosting", "marginalIntensity"]
7
7
  }
package/dist/cjs/index.js CHANGED
@@ -17,22 +17,26 @@ var __copyProps = (to, from, except, desc) => {
17
17
  return to;
18
18
  };
19
19
  var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ // If the importer is in node compatibility mode or this is not an ESM
21
+ // file that has been converted to a CommonJS file using a Babel-
22
+ // compatible transform (i.e. "__esModule" has not been set), then set
23
+ // "default" to the CommonJS "module.exports" for node compatibility.
20
24
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
21
25
  mod
22
26
  ));
23
27
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
24
- var src_exports = {};
25
- __export(src_exports, {
28
+ var index_exports = {};
29
+ __export(index_exports, {
26
30
  averageIntensity: () => import_average_intensities_min.default,
27
31
  co2: () => import_co2.default,
28
- default: () => src_default,
32
+ default: () => index_default,
29
33
  hosting: () => import_hosting.default,
30
34
  marginalIntensity: () => import_marginal_intensities_2021_min.default
31
35
  });
32
- module.exports = __toCommonJS(src_exports);
36
+ module.exports = __toCommonJS(index_exports);
33
37
  var import_co2 = __toESM(require("./co2.js"));
34
38
  var import_hosting = __toESM(require("./hosting.js"));
35
39
  var import_average_intensities_min = __toESM(require("./data/average-intensities.min.js"));
36
40
  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: import_average_intensities_min.default, marginalIntensity: import_marginal_intensities_2021_min.default };
41
+ var index_default = { co2: import_co2.default, hosting: import_hosting.default, averageIntensity: import_average_intensities_min.default, marginalIntensity: import_marginal_intensities_2021_min.default };
38
42
  //# sourceMappingURL=index.js.map
@@ -2,6 +2,6 @@
2
2
  "version": 3,
3
3
  "sources": ["../../src/index.js"],
4
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,yDAAAA;AAAA,EAAA,sBAAAC;AAAA,EAAA;AAAA,gCAAAC;AAAA,EAAA,8DAAAC;AAAA;AAAA;AAAA,iBAAgB;AAChB,qBAAoB;AACpB,qCAA6B;AAC7B,2CAA8B;AAG9B,IAAO,cAAQ,EAAE,gBAAAF,SAAK,wBAAAC,SAAS,iDAAAF,SAAkB,wDAAAG,QAAkB;",
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA,yDAAAA;AAAA,EAAA,sBAAAC;AAAA,EAAA;AAAA,gCAAAC;AAAA,EAAA,8DAAAC;AAAA;AAAA;AAAA,iBAAgB;AAChB,qBAAoB;AACpB,qCAA6B;AAC7B,2CAA8B;AAG9B,IAAO,gBAAQ,EAAE,gBAAAF,SAAK,wBAAAC,SAAS,iDAAAF,SAAkB,wDAAAG,QAAkB;",
6
6
  "names": ["averageIntensity", "co2", "hosting", "marginalIntensity"]
7
7
  }
@@ -30,6 +30,14 @@ class SustainableWebDesign {
30
30
  this.options = options;
31
31
  this.version = 3;
32
32
  }
33
+ /**
34
+ * Accept a figure for bytes transferred and return an object representing
35
+ * the share of the total enrgy use of the entire system, broken down
36
+ * by each corresponding system component
37
+ *
38
+ * @param {number} bytes - the data transferred in bytes
39
+ * @return {object} Object containing the energy in kilowatt hours, keyed by system component
40
+ */
33
41
  energyPerByteByComponent(bytes) {
34
42
  const transferedBytesToGb = bytes / import_constants.fileSize.GIGABYTE;
35
43
  const energyUsage = transferedBytesToGb * import_constants.KWH_PER_GB;
@@ -40,6 +48,14 @@ class SustainableWebDesign {
40
48
  dataCenterEnergy: energyUsage * import_constants.DATACENTER_ENERGY
41
49
  };
42
50
  }
51
+ /**
52
+ * Accept an object keys by the different system components, and
53
+ * return an object with the co2 figures key by the each component
54
+ *
55
+ * @param {object} energyByComponent - energy grouped by the four system components
56
+ * @param {number} [carbonIntensity] - carbon intensity to apply to the datacentre values
57
+ * @return {number} the total number in grams of CO2 equivalent emissions
58
+ */
43
59
  co2byComponent(energyByComponent, carbonIntensity = import_constants.GLOBAL_GRID_INTENSITY, options = {}) {
44
60
  let deviceCarbonIntensity = import_constants.GLOBAL_GRID_INTENSITY;
45
61
  let networkCarbonIntensity = import_constants.GLOBAL_GRID_INTENSITY;
@@ -74,6 +90,19 @@ class SustainableWebDesign {
74
90
  }
75
91
  return returnCO2ByComponent;
76
92
  }
93
+ /**
94
+ * Accept a figure for bytes transferred and return a single figure for CO2
95
+ * emissions. Where information exists about the origin data is being
96
+ * fetched from, a different carbon intensity figure
97
+ * is applied for the data centre share of the carbon intensity.
98
+ *
99
+ * @param {number} bytes - the data transferred in bytes
100
+ * @param {boolean} carbonIntensity - a boolean indicating whether the data center is green or not
101
+ * @param {boolean} segmentResults - a boolean indicating whether to return the results broken down by component
102
+ * @param {boolean} ratingResults - a boolean indicating whether to return the rating based on the Sustainable Web Design Model
103
+ * @param {object} options - an object containing the grid intensity and first/return visitor values
104
+ * @return {number|object} the total number in grams of CO2 equivalent emissions, or an object containing the breakdown by component
105
+ */
77
106
  perByte(bytes, carbonIntensity = false, segmentResults = false, ratingResults = false, options = {}) {
78
107
  if (bytes < 1) {
79
108
  bytes = 0;
@@ -112,6 +141,17 @@ class SustainableWebDesign {
112
141
  }
113
142
  return co2ValuesSum;
114
143
  }
144
+ /**
145
+ * Accept a figure for bytes transferred and return a single figure for CO2
146
+ * emissions. This method applies caching assumptions from the original Sustainable Web Design model.
147
+ *
148
+ * @param {number} bytes - the data transferred in bytes
149
+ * @param {boolean} carbonIntensity - a boolean indicating whether the data center is green or not
150
+ * @param {boolean} segmentResults - a boolean indicating whether to return the results broken down by component
151
+ * @param {boolean} ratingResults - a boolean indicating whether to return the rating based on the Sustainable Web Design Model
152
+ * @param {object} options - an object containing the grid intensity and first/return visitor values
153
+ * @return {number|object} the total number in grams of CO2 equivalent emissions, or an object containing the breakdown by component
154
+ */
115
155
  perVisit(bytes, carbonIntensity = false, segmentResults = false, ratingResults = false, options = {}) {
116
156
  const energyBycomponent = this.energyPerVisitByComponent(bytes, options);
117
157
  if (typeof carbonIntensity !== "boolean") {
@@ -147,6 +187,13 @@ class SustainableWebDesign {
147
187
  }
148
188
  return co2ValuesSum;
149
189
  }
190
+ /**
191
+ * Accept a figure for bytes transferred and return the number of kilowatt hours used
192
+ * by the total system for this data transfer
193
+ *
194
+ * @param {number} bytes
195
+ * @return {number} the number of kilowatt hours used
196
+ */
150
197
  energyPerByte(bytes) {
151
198
  const energyByComponent = this.energyPerByteByComponent(bytes);
152
199
  const energyValues = Object.values(energyByComponent);
@@ -154,6 +201,20 @@ class SustainableWebDesign {
154
201
  (prevValue, currentValue) => prevValue + currentValue
155
202
  );
156
203
  }
204
+ /**
205
+ * Accept a figure for bytes transferred, and return an object containing figures
206
+ * per system component, with the caching assumptions applied. This tries to account
207
+ * for webpages being loaded from a cache by browsers, so if you had a thousand page views,
208
+ * and tried to work out the energy per visit, the numbers would reflect the reduced amounts
209
+ * of transfer.
210
+ *
211
+ * @param {number} bytes - the data transferred in bytes for loading a webpage
212
+ * @param {number} firstView - what percentage of visits are loading this page for the first time
213
+ * @param {number} returnView - what percentage of visits are loading this page for subsequent times
214
+ * @param {number} dataReloadRatio - what percentage of a page is reloaded on each subsequent page view
215
+ *
216
+ * @return {object} Object containing the energy in kilowatt hours, keyed by system component
217
+ */
157
218
  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) {
158
219
  if (options.dataReloadRatio || options.dataReloadRatio === 0) {
159
220
  dataReloadRatio = options.dataReloadRatio;
@@ -173,6 +234,13 @@ class SustainableWebDesign {
173
234
  }
174
235
  return cacheAdjustedSegmentEnergy;
175
236
  }
237
+ /**
238
+ * Accept a figure for bytes, and return the total figure for energy per visit
239
+ * using the default caching assumptions for loading a single website
240
+ *
241
+ * @param {number} bytes
242
+ * @return {number} the total energy use for the visit, after applying the caching assumptions
243
+ */
176
244
  energyPerVisit(bytes) {
177
245
  let firstVisits = 0;
178
246
  let subsequentVisits = 0;
@@ -208,6 +276,12 @@ class SustainableWebDesign {
208
276
  productionEnergy: (0, import_helpers.formatNumber)(annualEnergy * import_constants.PRODUCTION_ENERGY)
209
277
  };
210
278
  }
279
+ /**
280
+ * Determines the rating of a website's sustainability based on its CO2 emissions.
281
+ *
282
+ * @param {number} co2e - The CO2 emissions of the website in grams.
283
+ * @returns {string} The sustainability rating, ranging from "A+" (best) to "F" (worst).
284
+ */
211
285
  ratingScale(co2e) {
212
286
  return (0, import_helpers.outputRating)(co2e, this.version);
213
287
  }
@@ -2,6 +2,6 @@
2
2
  "version": 3,
3
3
  "sources": ["../../src/sustainable-web-design-v3.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 */\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, outputRating } from \"./helpers/index.js\";\n\nclass SustainableWebDesign {\n constructor(options) {\n this.allowRatings = true;\n this.options = options;\n this.version = 3;\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 || device?.value === 0) {\n deviceCarbonIntensity = device.value;\n }\n if (network?.value || network?.value === 0) {\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 || dataCenter?.value === 0) {\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 data centre share of the carbon intensity.\n *\n * @param {number} bytes - the data transferred in bytes\n * @param {boolean} carbonIntensity - a boolean indicating whether the data center is green or not\n * @param {boolean} segmentResults - a boolean indicating whether to return the results broken down by component\n * @param {boolean} ratingResults - a boolean indicating whether to return the rating based on the Sustainable Web Design Model\n * @param {object} options - an object containing the grid intensity and first/return visitor values\n * @return {number|object} the total number in grams of CO2 equivalent emissions, or an object containing the breakdown by component\n */\n perByte(\n bytes,\n carbonIntensity = false,\n segmentResults = false,\n ratingResults = false,\n options = {}\n ) {\n if (bytes < 1) {\n bytes = 0;\n }\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 let rating = null;\n if (ratingResults) {\n rating = this.ratingScale(co2ValuesSum);\n }\n\n if (segmentResults) {\n if (ratingResults) {\n return {\n ...co2ValuesbyComponent,\n total: co2ValuesSum,\n rating: rating,\n };\n }\n\n return { ...co2ValuesbyComponent, total: co2ValuesSum };\n }\n\n if (ratingResults) {\n return { total: co2ValuesSum, rating: rating };\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 {boolean} carbonIntensity - a boolean indicating whether the data center is green or not\n * @param {boolean} segmentResults - a boolean indicating whether to return the results broken down by component\n * @param {boolean} ratingResults - a boolean indicating whether to return the rating based on the Sustainable Web Design Model\n * @param {object} options - an object containing the grid intensity and first/return visitor values\n * @return {number|object} the total number in grams of CO2 equivalent emissions, or an object containing the breakdown by component\n */\n perVisit(\n bytes,\n carbonIntensity = false,\n segmentResults = false,\n ratingResults = 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 let rating = null;\n if (ratingResults) {\n rating = this.ratingScale(co2ValuesSum);\n }\n\n if (segmentResults) {\n if (ratingResults) {\n return {\n ...co2ValuesbyComponent,\n total: co2ValuesSum,\n rating: rating,\n };\n }\n return { ...co2ValuesbyComponent, total: co2ValuesSum };\n }\n\n if (ratingResults) {\n return { total: co2ValuesSum, rating: rating };\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 || options.dataReloadRatio === 0) {\n dataReloadRatio = options.dataReloadRatio;\n }\n\n if (options.firstVisitPercentage || options.firstVisitPercentage === 0) {\n firstView = options.firstVisitPercentage;\n }\n\n if (options.returnVisitPercentage || options.returnVisitPercentage === 0) {\n returnView = options.returnVisitPercentage;\n }\n\n const energyBycomponent = this.energyPerByteByComponent(bytes);\n const cacheAdjustedSegmentEnergy = {};\n\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\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 /**\n * Determines the rating of a website's sustainability based on its CO2 emissions.\n *\n * @param {number} co2e - The CO2 emissions of the website in grams.\n * @returns {string} The sustainability rating, ranging from \"A+\" (best) to \"F\" (worst).\n */\n ratingScale(co2e) {\n return outputRating(co2e, this.version);\n }\n}\n\nexport { SustainableWebDesign };\nexport default SustainableWebDesign;\n"],
5
- "mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAUA,uBAYO;AACP,qBAA2C;AAE3C,MAAM,qBAAqB;AAAA,EACzB,YAAY,SAAS;AACnB,SAAK,eAAe;AACpB,SAAK,UAAU;AACf,SAAK,UAAU;AAAA,EACjB;AAAA,EAUA,yBAAyB,OAAO;AAC9B,UAAM,sBAAsB,QAAQ,0BAAS;AAC7C,UAAM,cAAc,sBAAsB;AAG1C,WAAO;AAAA,MACL,sBAAsB,cAAc;AAAA,MACpC,eAAe,cAAc;A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AAa;AAAA,IACzB;AAEA,WAAO;AAAA,EACT;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASA,eAAe,OAAO;AAIpB,QAAI,cAAc;AAClB,QAAI,mBAAmB;AAEvB,UAAM,oBAAoB,OAAO;AAAA,MAC/B,KAAK,0BAA0B,KAAK;AAAA,IACtC;AAEA,eAAW,CAAC,KAAK,GAAG,KAAK,mBAAmB;AAC1C,UAAI,IAAI,QAAQ,OAAO,IAAI,GAAG;AAC5B,uBAAe;AAAA,MACjB;AAAA,IACF;AAEA,eAAW,CAAC,KAAK,GAAG,KAAK,mBAAmB;AAC1C,UAAI,IAAI,QAAQ,YAAY,IAAI,GAAG;AACjC,4BAAoB;AAAA,MACtB;AAAA,IACF;AAEA,WAAO,cAAc;AAAA,EACvB;AAAA,EAEA,yBACE,gBACA,kBAAkB,wCAClB;AACA,eAAO,6BAAa,iBAAiB,eAAe;AAAA,EACtD;AAAA,EAEA,kBAAkB,gBAAgB,kBAAkB,KAAM;AACxD,WAAO,iBAAiB,kBAAkB;AAAA,EAC5C;AAAA,EAEA,uBAAuB,UAAU,kBAAkB,KAAM;AACvD,WAAO,WAAW,kBAAkB;AAAA,EACtC;AAAA,EAEA,oBAAoB,cAAc;AAChC,WAAO;AAAA,MACL,0BAAsB,6BAAa,eAAe,uCAAsB;AAAA,MACxE,mBAAe,6BAAa,eAAe,+BAAc;AAAA,MACzD,sBAAkB,6BAAa,eAAe,kCAAiB;AAAA,MAC/D,sBAAkB,6BAAa,eAAe,kCAAiB;AAAA,IACjE;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,YAAY,MAAM;AAChB,eAAO,6BAAa,MAAM,KAAK,OAAO;AAAA,EACxC;AACF;AAGA,IAAO,oCAAQ;",
6
6
  "names": []
7
7
  }
@@ -67,6 +67,12 @@ class SustainableWebDesign {
67
67
  this.options = options;
68
68
  this.version = 4;
69
69
  }
70
+ /**
71
+ * Calculate the operational energy of data transfer for each system segment
72
+ *
73
+ * @param {number} bytes
74
+ * @returns {object}
75
+ */
70
76
  operationalEnergyPerSegment(bytes) {
71
77
  const transferedBytesToGb = bytes / import_constants.fileSize.GIGABYTE;
72
78
  const dataCenter = transferedBytesToGb * OPERATIONAL_KWH_PER_GB_DATACENTER;
@@ -78,6 +84,13 @@ class SustainableWebDesign {
78
84
  device
79
85
  };
80
86
  }
87
+ /**
88
+ * Calculate the operational emissions of data transfer for each system segment
89
+ *
90
+ * @param {number} bytes
91
+ * @param {object} options
92
+ * @returns {object}
93
+ */
81
94
  operationalEmissions(bytes, options = {}) {
82
95
  const { dataCenter, network, device } = this.operationalEnergyPerSegment(bytes);
83
96
  let dataCenterGridIntensity = GLOBAL_GRID_INTENSITY;
@@ -104,6 +117,12 @@ class SustainableWebDesign {
104
117
  device: deviceEmissions
105
118
  };
106
119
  }
120
+ /**
121
+ * Calculate the embodied energy of data transfer for each system segment
122
+ *
123
+ * @param {number} bytes
124
+ * @returns {object}
125
+ */
107
126
  embodiedEnergyPerSegment(bytes) {
108
127
  const transferedBytesToGb = bytes / import_constants.fileSize.GIGABYTE;
109
128
  const dataCenter = transferedBytesToGb * EMBODIED_KWH_PER_GB_DATACENTER;
@@ -115,6 +134,12 @@ class SustainableWebDesign {
115
134
  device
116
135
  };
117
136
  }
137
+ /**
138
+ * Calculate the embodied emissions of data transfer for each system segment
139
+ *
140
+ * @param {number} bytes
141
+ * @returns {object}
142
+ */
118
143
  embodiedEmissions(bytes) {
119
144
  const { dataCenter, network, device } = this.embodiedEnergyPerSegment(bytes);
120
145
  const dataCenterGridIntensity = GLOBAL_GRID_INTENSITY;
@@ -129,6 +154,7 @@ class SustainableWebDesign {
129
154
  device: deviceEmissions
130
155
  };
131
156
  }
157
+ // NOTE: Setting green: true should result in a greenHostingFactor of 1.0
132
158
  perByte(bytes, green = false, segmented = false, ratingResults = false, options = {}) {
133
159
  if (bytes < 1) {
134
160
  return 0;
@@ -210,6 +236,12 @@ class SustainableWebDesign {
210
236
  }
211
237
  return total;
212
238
  }
239
+ /**
240
+ * Determines the rating of a website's sustainability based on its CO2 emissions.
241
+ *
242
+ * @param {number} co2e - The CO2 emissions of the website in grams.
243
+ * @returns {string} The sustainability rating, ranging from "A+" (best) to "F" (worst).
244
+ */
213
245
  ratingScale(co2e) {
214
246
  return (0, import_helpers.outputRating)(co2e, this.version);
215
247
  }
@@ -2,6 +2,6 @@
2
2
  "version": 3,
3
3
  "sources": ["../../src/sustainable-web-design-v4.js"],
4
4
  "sourcesContent": ["\"use strict\";\n\n/**\n * Sustainable Web Design version 4\n *\n * Updated calculations and figures from\n * https://sustainablewebdesign.org/estimating-digital-emissions/\n *\n */\n\nimport { fileSize, SWDV4 } from \"./constants/index.js\";\nimport { outputRating } from \"./helpers/index.js\";\n\nconst {\n OPERATIONAL_KWH_PER_GB_DATACENTER,\n OPERATIONAL_KWH_PER_GB_NETWORK,\n OPERATIONAL_KWH_PER_GB_DEVICE,\n EMBODIED_KWH_PER_GB_DATACENTER,\n EMBODIED_KWH_PER_GB_NETWORK,\n EMBODIED_KWH_PER_GB_DEVICE,\n GLOBAL_GRID_INTENSITY,\n} = SWDV4;\n\n/**\n * Output the CO2e emissions for each system segment\n * @param {object} operationalEmissions\n * @param {object} embodiedEmissions\n * @returns {object}\n */\nfunction outputSegments(operationalEmissions, embodiedEmissions) {\n const totalOperationalCO2e =\n operationalEmissions.dataCenter +\n operationalEmissions.network +\n operationalEmissions.device;\n const totalEmbodiedCO2e =\n embodiedEmissions.dataCenter +\n embodiedEmissions.network +\n embodiedEmissions.device;\n\n const dataCenterCO2e =\n operationalEmissions.dataCenter + embodiedEmissions.dataCenter;\n const networkCO2e = operationalEmissions.network + embodiedEmissions.network;\n const consumerDeviceCO2e =\n operationalEmissions.device + embodiedEmissions.device;\n\n return {\n dataCenterOperationalCO2e: operationalEmissions.dataCenter,\n networkOperationalCO2e: operationalEmissions.network,\n consumerDeviceOperationalCO2e: operationalEmissions.device,\n dataCenterEmbodiedCO2e: embodiedEmissions.dataCenter,\n networkEmbodiedCO2e: embodiedEmissions.network,\n consumerDeviceEmbodiedCO2e: embodiedEmissions.device,\n totalEmbodiedCO2e,\n totalOperationalCO2e,\n dataCenterCO2e,\n networkCO2e,\n consumerDeviceCO2e,\n };\n}\n\n/**\n * Determine the green hosting factor\n * @param {boolean} green\n * @param {object} options\n * @returns {number}\n */\nfunction getGreenHostingFactor(green, options) {\n if (green) {\n return 1.0;\n } else if (options?.greenHostingFactor || options?.greenHostingFactor === 0) {\n return options.greenHostingFactor;\n }\n return 0;\n}\nclass SustainableWebDesign {\n constructor(options) {\n this.allowRatings = true;\n this.options = options;\n this.version = 4;\n }\n\n /**\n * Calculate the operational energy of data transfer for each system segment\n *\n * @param {number} bytes\n * @returns {object}\n */\n operationalEnergyPerSegment(bytes) {\n const transferedBytesToGb = bytes / fileSize.GIGABYTE;\n const dataCenter = transferedBytesToGb * OPERATIONAL_KWH_PER_GB_DATACENTER;\n const network = transferedBytesToGb * OPERATIONAL_KWH_PER_GB_NETWORK;\n const device = transferedBytesToGb * OPERATIONAL_KWH_PER_GB_DEVICE;\n\n return {\n dataCenter,\n network,\n device,\n };\n }\n\n /**\n * Calculate the operational emissions of data transfer for each system segment\n *\n * @param {number} bytes\n * @param {object} options\n * @returns {object}\n */\n operationalEmissions(bytes, options = {}) {\n const { dataCenter, network, device } =\n this.operationalEnergyPerSegment(bytes);\n\n let dataCenterGridIntensity = GLOBAL_GRID_INTENSITY;\n let networkGridIntensity = GLOBAL_GRID_INTENSITY;\n let deviceGridIntensity = GLOBAL_GRID_INTENSITY;\n\n if (options?.gridIntensity) {\n const { device, network, dataCenter } = options.gridIntensity;\n\n if (device?.value || device?.value === 0) {\n deviceGridIntensity = device.value;\n }\n\n if (network?.value || network?.value === 0) {\n networkGridIntensity = network.value;\n }\n\n if (dataCenter?.value || dataCenter?.value === 0) {\n dataCenterGridIntensity = dataCenter.value;\n }\n }\n\n const dataCenterEmissions = dataCenter * dataCenterGridIntensity;\n const networkEmissions = network * networkGridIntensity;\n const deviceEmissions = device * deviceGridIntensity;\n\n return {\n dataCenter: dataCenterEmissions,\n network: networkEmissions,\n device: deviceEmissions,\n };\n }\n\n /**\n * Calculate the embodied energy of data transfer for each system segment\n *\n * @param {number} bytes\n * @returns {object}\n */\n embodiedEnergyPerSegment(bytes) {\n const transferedBytesToGb = bytes / fileSize.GIGABYTE;\n const dataCenter = transferedBytesToGb * EMBODIED_KWH_PER_GB_DATACENTER;\n const network = transferedBytesToGb * EMBODIED_KWH_PER_GB_NETWORK;\n const device = transferedBytesToGb * EMBODIED_KWH_PER_GB_DEVICE;\n\n return {\n dataCenter,\n network,\n device,\n };\n }\n\n /**\n * Calculate the embodied emissions of data transfer for each system segment\n *\n * @param {number} bytes\n * @returns {object}\n */\n embodiedEmissions(bytes) {\n const { dataCenter, network, device } =\n this.embodiedEnergyPerSegment(bytes);\n\n const dataCenterGridIntensity = GLOBAL_GRID_INTENSITY;\n const networkGridIntensity = GLOBAL_GRID_INTENSITY;\n const deviceGridIntensity = GLOBAL_GRID_INTENSITY;\n\n // NOTE: Per the guidance in the SWDM v4, the grid intensity values for embodied emissions are fixed to the global grid intensity.\n\n const dataCenterEmissions = dataCenter * dataCenterGridIntensity;\n const networkEmissions = network * networkGridIntensity;\n const deviceEmissions = device * deviceGridIntensity;\n\n return {\n dataCenter: dataCenterEmissions,\n network: networkEmissions,\n device: deviceEmissions,\n };\n }\n\n // NOTE: Setting green: true should result in a greenHostingFactor of 1.0\n perByte(\n bytes,\n green = false,\n segmented = false,\n ratingResults = false,\n options = {}\n ) {\n if (bytes < 1) {\n return 0;\n }\n\n const operationalEmissions = this.operationalEmissions(bytes, options);\n const embodiedEmissions = this.embodiedEmissions(bytes);\n const greenHostingFactor = getGreenHostingFactor(green, options);\n\n const totalEmissions = {\n dataCenter:\n operationalEmissions.dataCenter * (1 - greenHostingFactor) +\n embodiedEmissions.dataCenter,\n network: operationalEmissions.network + embodiedEmissions.network,\n device: operationalEmissions.device + embodiedEmissions.device,\n };\n\n const total =\n totalEmissions.dataCenter +\n totalEmissions.network +\n totalEmissions.device;\n\n let rating = null;\n if (ratingResults) {\n rating = this.ratingScale(total);\n }\n\n if (segmented) {\n const segments = {\n ...outputSegments(operationalEmissions, embodiedEmissions),\n };\n\n if (ratingResults) {\n return {\n ...segments,\n total,\n rating,\n };\n }\n return { ...segments, total };\n }\n\n if (ratingResults) {\n return { total, rating };\n }\n\n return total;\n }\n\n perVisit(\n bytes,\n green = false,\n segmented = false,\n ratingResults = false,\n options = {}\n ) {\n let firstViewRatio = 1;\n let returnViewRatio = 0;\n let dataReloadRatio = 0;\n const greenHostingFactor = getGreenHostingFactor(green, options);\n const operationalEmissions = this.operationalEmissions(bytes, options);\n const embodiedEmissions = this.embodiedEmissions(bytes);\n\n if (bytes < 1) {\n return 0;\n }\n\n if (options.firstVisitPercentage || options.firstVisitPercentage === 0) {\n firstViewRatio = options.firstVisitPercentage;\n }\n\n if (options.returnVisitPercentage || options.returnVisitPercentage === 0) {\n returnViewRatio = options.returnVisitPercentage;\n }\n\n if (options.dataReloadRatio || options.dataReloadRatio === 0) {\n dataReloadRatio = options.dataReloadRatio;\n }\n\n // NOTE: First visit emissions are calculated as the sum of all three segments without any caching.\n\n const firstVisitEmissions =\n operationalEmissions.dataCenter * (1 - greenHostingFactor) +\n embodiedEmissions.dataCenter +\n operationalEmissions.network +\n embodiedEmissions.network +\n operationalEmissions.device +\n embodiedEmissions.device;\n\n // NOTE: First visit emissions are calculated as the sum of all three segments with caching applied.\n\n const returnVisitEmissions =\n (operationalEmissions.dataCenter * (1 - greenHostingFactor) +\n embodiedEmissions.dataCenter +\n operationalEmissions.network +\n embodiedEmissions.network +\n operationalEmissions.device +\n embodiedEmissions.device) *\n (1 - dataReloadRatio);\n\n // NOTE: The total emissions account for the percentage of first and return visits.\n const total =\n firstVisitEmissions * firstViewRatio +\n returnVisitEmissions * returnViewRatio;\n\n let rating = null;\n if (ratingResults) {\n rating = this.ratingScale(total);\n }\n\n if (segmented) {\n const segments = {\n ...outputSegments(operationalEmissions, embodiedEmissions),\n firstVisitCO2e: firstVisitEmissions,\n returnVisitCO2e: returnVisitEmissions,\n };\n\n if (ratingResults) {\n return {\n ...segments,\n total,\n rating,\n };\n }\n\n return { ...segments, total };\n }\n\n if (ratingResults) {\n return { total, rating };\n }\n\n return total;\n }\n\n /**\n * Determines the rating of a website's sustainability based on its CO2 emissions.\n *\n * @param {number} co2e - The CO2 emissions of the website in grams.\n * @returns {string} The sustainability rating, ranging from \"A+\" (best) to \"F\" (worst).\n */\n ratingScale(co2e) {\n return outputRating(co2e, this.version);\n }\n}\n\nexport { SustainableWebDesign };\nexport default SustainableWebDesign;\n"],
5
- "mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAUA,uBAAgC;AAChC,qBAA6B;AAE7B,MAAM;AAAA,EACJ;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,IAAI;AAQJ,SAAS,eAAe,sBAAsB,mBAAmB;AAC/D,QAAM,uBACJ,qBAAqB,aACrB,qBAAqB,UACrB,qBAAqB;AACvB,QAAM,oBACJ,kBAAkB,aAClB,kBAAkB,UAClB,kBAAkB;AAEpB,QAAM,iBACJ,qBAAqB,aAAa,kBAAkB;AACtD,QAAM,cAAc,qBAAqB,UAAU,kBAAkB;AACrE,QAAM,qBACJ,qBAAqB,SAAS,kBAAkB;AAElD,SAAO;AAAA,IACL,2BAA2B,qBAAqB;AAAA,IAChD,wBAAwB,qBAAqB;AAAA,IAC7C,+BAA+B,qBAAqB;AAAA,IACpD,wBAAwB,kBAAkB;AAAA,IAC1C,qBAAqB,kBAAkB;AAAA,IACvC,4BAA4B,kBAAkB;AAAA,IAC9C;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;AAQA,SAAS,sBAAsB,OAAO,SAAS;AAC7C,MAAI,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ACA;AAAA,QACF;AAAA,MACF;AAEA,aAAO,EAAE,GAAG,UAAU,MAAM;AAAA,IAC9B;AAEA,QAAI,eAAe;AACjB,aAAO,EAAE,OAAO,OAAO;AAAA,IACzB;AAEA,WAAO;AAAA,EACT;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,YAAY,MAAM;AAChB,eAAO,6BAAa,MAAM,KAAK,OAAO;AAAA,EACxC;AACF;AAGA,IAAO,oCAAQ;",
6
6
  "names": ["device", "network", "dataCenter"]
7
7
  }
package/dist/esm/1byte.js CHANGED
@@ -13,6 +13,12 @@ class OneByte {
13
13
  this.options = options;
14
14
  this.KWH_PER_BYTE_FOR_NETWORK = KWH_PER_BYTE_FOR_NETWORK;
15
15
  }
16
+ /**
17
+ * Calculates the carbon footprint of a website using the OneByte model
18
+ * @param {number} bytes - The number of bytes to calculate the carbon footprint for
19
+ * @param {boolean} green - Whether the energy is green or not
20
+ * @returns {number} The carbon footprint in grams of CO2
21
+ */
16
22
  perByte(bytes, green) {
17
23
  if (bytes < 1) {
18
24
  return 0;
package/dist/esm/co2.js CHANGED
@@ -45,14 +45,12 @@ class CO2 {
45
45
  }
46
46
  } else if (options == null ? void 0 : options.model) {
47
47
  throw new Error(
48
- `"${options.model}" is not a valid model. Please use "1byte" for the OneByte model, and "swd" for the Sustainable Web Design model.
49
- See https://developers.thegreenwebfoundation.org/co2js/models/ to learn more about the models available in CO2.js.`
48
+ '"'.concat(options.model, '" is not a valid model. Please use "1byte" for the OneByte model, and "swd" for the Sustainable Web Design model.\nSee https://developers.thegreenwebfoundation.org/co2js/models/ to learn more about the models available in CO2.js.')
50
49
  );
51
50
  }
52
51
  if ((options == null ? void 0 : options.rating) && typeof options.rating !== "boolean") {
53
52
  throw new Error(
54
- `The rating option must be a boolean. Please use true or false.
55
- See https://developers.thegreenwebfoundation.org/co2js/options/ to learn more about the options available in CO2.js.`
53
+ "The rating option must be a boolean. Please use true or false.\nSee https://developers.thegreenwebfoundation.org/co2js/options/ to learn more about the options available in CO2.js."
56
54
  );
57
55
  }
58
56
  const allowRatings = !!this.model.allowRatings;
@@ -60,25 +58,51 @@ See https://developers.thegreenwebfoundation.org/co2js/options/ to learn more ab
60
58
  this._rating = (options == null ? void 0 : options.rating) === true;
61
59
  if (!allowRatings && this._rating) {
62
60
  throw new Error(
63
- `The rating system is not supported in the model you are using. Try using the Sustainable Web Design model instead.
64
- See https://developers.thegreenwebfoundation.org/co2js/models/ to learn more about the models available in CO2.js.`
61
+ "The rating system is not supported in the model you are using. Try using the Sustainable Web Design model instead.\nSee https://developers.thegreenwebfoundation.org/co2js/models/ to learn more about the models available in CO2.js."
65
62
  );
66
63
  }
67
64
  }
65
+ /**
66
+ * Accept a figure in bytes for data transfer, and a boolean for whether
67
+ * the domain shows as 'green', and return a CO2 figure for energy used to shift the corresponding
68
+ * the data transfer.
69
+ *
70
+ * @param {number} bytes
71
+ * @param {boolean} green
72
+ * @return {number|CO2EstimateComponentsPerByte} the amount of CO2 in grammes or its separate components
73
+ */
68
74
  perByte(bytes, green = false) {
69
75
  return this.model.perByte(bytes, green, this._segment, this._rating);
70
76
  }
77
+ /**
78
+ * Accept a figure in bytes for data transfer, and a boolean for whether
79
+ * the domain shows as 'green', and return a CO2 figure for energy used to shift the corresponding
80
+ * the data transfer.
81
+ *
82
+ * @param {number} bytes
83
+ * @param {boolean} green
84
+ * @return {number|CO2EstimateComponentsPerVisit} the amount of CO2 in grammes or its separate components
85
+ */
71
86
  perVisit(bytes, green = false) {
72
87
  var _a;
73
88
  if ((_a = this.model) == null ? void 0 : _a.perVisit) {
74
89
  return this.model.perVisit(bytes, green, this._segment, this._rating);
75
90
  } else {
76
91
  throw new Error(
77
- `The perVisit() method is not supported in the model you are using. Try using perByte() instead.
78
- See https://developers.thegreenwebfoundation.org/co2js/methods/ to learn more about the methods available in CO2.js.`
92
+ "The perVisit() method is not supported in the model you are using. Try using perByte() instead.\nSee https://developers.thegreenwebfoundation.org/co2js/methods/ to learn more about the methods available in CO2.js."
79
93
  );
80
94
  }
81
95
  }
96
+ /**
97
+ * Accept a figure in bytes for data transfer, a boolean for whether
98
+ * the domain shows as 'green', and an options object.
99
+ * Returns an object containing CO2 figure, green boolean, and object of the variables used in calculating the CO2 figure.
100
+ *
101
+ * @param {number} bytes
102
+ * @param {boolean} green
103
+ * @param {Object} options
104
+ * @return {CO2EstimateTraceResultPerByte} the amount of CO2 in grammes
105
+ */
82
106
  perByteTrace(bytes, green = false, options = {}) {
83
107
  const adjustments = parseByteTraceOptions(
84
108
  options,
@@ -113,6 +137,16 @@ See https://developers.thegreenwebfoundation.org/co2js/methods/ to learn more ab
113
137
  }, otherVariables)
114
138
  };
115
139
  }
140
+ /**
141
+ * Accept a figure in bytes for data transfer, a boolean for whether
142
+ * the domain shows as 'green', and an options object.
143
+ * Returns an object containing CO2 figure, green boolean, and object of the variables used in calculating the CO2 figure.
144
+ *
145
+ * @param {number} bytes
146
+ * @param {boolean} green
147
+ * @param {Object} options
148
+ * @return {CO2EstimateTraceResultPerVisit} the amount of CO2 in grammes
149
+ */
116
150
  perVisitTrace(bytes, green = false, options = {}) {
117
151
  var _a;
118
152
  if ((_a = this.model) == null ? void 0 : _a.perVisit) {
@@ -141,8 +175,7 @@ See https://developers.thegreenwebfoundation.org/co2js/methods/ to learn more ab
141
175
  };
142
176
  } else {
143
177
  throw new Error(
144
- `The perVisitTrace() method is not supported in the model you are using. Try using perByte() instead.
145
- See https://developers.thegreenwebfoundation.org/co2js/methods/ to learn more about the methods available in CO2.js.`
178
+ "The perVisitTrace() method is not supported in the model you are using. Try using perByte() instead.\nSee https://developers.thegreenwebfoundation.org/co2js/methods/ to learn more about the methods available in CO2.js."
146
179
  );
147
180
  }
148
181
  }