@tgwf/co2 0.16.9 → 0.17.1-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/CHANGELOG.md CHANGED
@@ -21,6 +21,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
21
21
 
22
22
  ## Released
23
23
 
24
+ ### [0.17.0] - 2025-12-02
25
+
26
+ #### Changes
27
+ * Add `data` and `hosting` subpath exports
28
+ * Add warning about switching default model to SWDMv4
29
+
30
+ #### Security
31
+ * Bump tar-fs from 2.1.3 to 2.1.4 by @dependabot[bot] in https://github.com/thegreenwebfoundation/co2.js/pull/265
32
+ * Bump js-yaml by @dependabot[bot] in https://github.com/thegreenwebfoundation/co2.js/pull/268
33
+
24
34
  ### [0.16.8] - 2025-05-12
25
35
 
26
36
  #### Fixed
package/CONTRIBUTING.md CHANGED
@@ -27,6 +27,7 @@ Thank you for considering contributing to CO2.js. Open source is at the heart of
27
27
 
28
28
  - Push the changes made in your branch to your fork of this repository.
29
29
  - Submit a [pull request](https://github.com/thegreenwebfoundation/co2.js/pulls) to the CO2.js repository in the `thegreenwebfoundation` organization.
30
+ - Please ensure that you *only* merge any pull requests into the `next` branch of this repo.
30
31
  - When opening a new pull request, you'll see a template. Please follow it. It asks you to state:
31
32
  - the type of change (choose from a list)
32
33
  - link to the related issue
package/README.md CHANGED
@@ -10,7 +10,7 @@
10
10
 
11
11
  One day, the internet will be powered by renewable energy. Until that day comes, there’ll be a CO2 cost that comes with every byte of data that’s uploaded or downloaded. By being able to calculate these emissions, developers can be empowered to create more efficient, lower carbon apps, websites, and software.
12
12
 
13
- ## [Documentation](https://developers.thegreenwebfoundation.org/co2js/overview/) | [Changelog](/CHANGELOG.md) | [Roadmap](https://github.com/orgs/thegreenwebfoundation/projects/3/views/1)
13
+ ## [Documentation](https://developers.thegreenwebfoundation.org/co2js/overview/) | [Changelog](/CHANGELOG.md) | [Roadmap](https://developers.thegreenwebfoundation.org/co2js/roadmap/)
14
14
 
15
15
  ## What is CO2.js?
16
16
 
@@ -122,6 +122,7 @@ CO2.js releases will be communicated through the following channels:
122
122
  | W3C Slack Sustainability Channel | ✅ | ❌ |
123
123
  | ClimateAction.Tech Slack | ✅ | ❌ |
124
124
  | [Green Web Foundation LinkedIn Account](https://www.linkedin.com/company/green-web-foundation/) | ✅ | ❌ |
125
+ | [CO2.js Mailing List](https://www.thegreenwebfoundation.org/co2-js/#stay-updated) | ✅ | ❌ |
125
126
 
126
127
  ## Licenses
127
128
 
package/dist/cjs/co2.js CHANGED
@@ -38,12 +38,14 @@ var import_sustainable_web_design_v4 = __toESM(require("./sustainable-web-design
38
38
  var import_helpers = require("./helpers/index.js");
39
39
  class CO2 {
40
40
  constructor(options) {
41
- this.model = new import_sustainable_web_design_v3.default();
41
+ this.model = new import_sustainable_web_design_v4.default();
42
42
  if ((options == null ? void 0 : options.model) === "1byte") {
43
43
  this.model = new import_byte.default();
44
44
  } else if ((options == null ? void 0 : options.model) === "swd") {
45
- this.model = new import_sustainable_web_design_v3.default();
46
- if ((options == null ? void 0 : options.version) === 4) {
45
+ this.model = new import_sustainable_web_design_v4.default();
46
+ if ((options == null ? void 0 : options.version) === 3) {
47
+ this.model = new import_sustainable_web_design_v3.default();
48
+ } else if ((options == null ? void 0 : options.version) === 4) {
47
49
  this.model = new import_sustainable_web_design_v4.default();
48
50
  }
49
51
  } else if (options == null ? void 0 : options.model) {
@@ -51,6 +53,10 @@ class CO2 {
51
53
  `"${options.model}" is not a valid model. Please use "1byte" for the OneByte model, and "swd" for the Sustainable Web Design model.
52
54
  See https://developers.thegreenwebfoundation.org/co2js/models/ to learn more about the models available in CO2.js.`
53
55
  );
56
+ } else if (!(options == null ? void 0 : options.model) && (options == null ? void 0 : options.version)) {
57
+ throw new Error(
58
+ `"Specified version ${options == null ? void 0 : options.version} but an estimation model is missing. Please specify a model to use for the version you have set.`
59
+ );
54
60
  }
55
61
  if ((options == null ? void 0 : options.rating) && typeof options.rating !== "boolean") {
56
62
  throw new Error(
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../src/co2.js"],
4
- "sourcesContent": ["\"use strict\";\n\n/**\n * @typedef {Object} CO2EstimateTraceResultPerByte\n * @property {number|CO2EstimateComponentsPerByte} co2 - The CO2 estimate in grams or its separate components\n * @property {boolean} green - Whether the domain is green or not\n * @property {TraceResultVariablesPerByte} variables - The variables used to calculate the CO2 estimate\n */\n\n/**\n * @typedef {Object} CO2EstimateTraceResultPerVisit\n * @property {number|CO2EstimateComponentsPerVisit} co2 - The CO2 estimate in grams or its separate components\n * @property {boolean} green - Whether the domain is green or not\n * @property {TraceResultVariablesPerVisit} variables - The variables used to calculate the CO2 estimate\n */\n\n/**\n * @typedef {Object} TraceResultVariablesPerByte\n * @property {GridIntensityVariables} gridIntensity - The grid intensity related variables\n */\n/**\n * @typedef {Object} TraceResultVariablesPerVisit\n * @property {GridIntensityVariables} gridIntensity - The grid intensity related variables\n * @property {number} dataReloadRatio - What percentage of a page is reloaded on each subsequent page view\n * @property {number} firstVisitPercentage - What percentage of visits are loading this page for subsequent times\n * @property {number} returnVisitPercentage - What percentage of visits are loading this page for the second or more time\n */\n\n/**\n * @typedef {Object} GridIntensityVariables\n * @property {string} description - The description of the variables\n * @property {number} network - The network grid intensity set by the user or the default\n * @property {number} dataCenter - The data center grid intensity set by the user or the default\n * @property {number} device - The device grid intensity set by the user or the default\n * @property {number} production - The production grid intensity set by the user or the default\n */\n\n/**\n * @typedef {Object} CO2EstimateComponentsPerByte\n * @property {number} networkCO2 - The CO2 estimate for networking in grams\n * @property {number} dataCenterCO2 - The CO2 estimate for data centers in grams\n * @property {number} consumerDeviceCO2 - The CO2 estimate for consumer devices in grams\n * @property {number} productionCO2 - The CO2 estimate for device production in grams\n * @property {string} rating - The rating of the CO2 estimate based on the Sustainable Web Design Model\n * @property {number} total - The total CO2 estimate in grams\n */\n\n/**\n * @typedef {Object} CO2EstimateComponentsPerVisit\n * @property {number} 'networkCO2 - first' - The CO2 estimate for networking in grams on first visit\n * @property {number} 'networkCO2 - subsequent' - The CO2 estimate for networking in grams on subsequent visits\n * @property {number} 'dataCenterCO2 - first' - The CO2 estimate for data centers in grams on first visit\n * @property {number} 'dataCenterCO2 - subsequent' - The CO2 estimate for data centers in grams on subsequent visits\n * @property {number} 'consumerDeviceCO2 - first' - The CO2 estimate for consumer devices in grams on first visit\n * @property {number} 'consumerDeviceCO2 - subsequent' - The CO2 estimate for consumer devices in grams on subsequent visits\n * @property {number} 'productionCO2 - first' - The CO2 estimate for device production in grams on first visit\n * @property {number} 'productionCO2 - subsequent' - The CO2 estimate for device production in grams on subsequent visits\n * @property {string} rating - The rating of the CO2 estimate based on the Sustainable Web Design Model\n * @property {number} total - The total CO2 estimate in grams\n */\n\nimport OneByte from \"./1byte.js\";\nimport SustainableWebDesignV3 from \"./sustainable-web-design-v3.js\";\nimport SustainableWebDesignV4 from \"./sustainable-web-design-v4.js\";\n\nimport {\n parseByteTraceOptions,\n parseVisitTraceOptions,\n} from \"./helpers/index.js\";\n\nclass CO2 {\n constructor(options) {\n this.model = new SustainableWebDesignV3();\n // Using optional chaining allows an empty object to be passed\n // in without breaking the code.\n if (options?.model === \"1byte\") {\n this.model = new OneByte();\n } else if (options?.model === \"swd\") {\n this.model = new SustainableWebDesignV3();\n if (options?.version === 4) {\n this.model = new SustainableWebDesignV4();\n }\n } else if (options?.model) {\n throw new Error(\n `\"${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.`\n );\n }\n\n if (options?.rating && typeof options.rating !== \"boolean\") {\n throw new Error(\n `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.`\n );\n }\n\n // This flag checks to see if the model itself has a rating system.\n const allowRatings = !!this.model.allowRatings;\n\n /** @private */\n this._segment = options?.results === \"segment\";\n // This flag is set by the user to enable the rating system.\n this._rating = options?.rating === true;\n\n // The rating system is only supported in the Sustainable Web Design Model.\n if (!allowRatings && this._rating) {\n throw new Error(\n `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.`\n );\n }\n }\n\n /**\n * Accept a figure in bytes for data transfer, and a boolean for whether\n * the domain shows as 'green', and return a CO2 figure for energy used to shift the corresponding\n * the data transfer.\n *\n * @param {number} bytes\n * @param {boolean} green\n * @return {number|CO2EstimateComponentsPerByte} the amount of CO2 in grammes or its separate components\n */\n perByte(bytes, green = false) {\n return this.model.perByte(bytes, green, this._segment, this._rating);\n }\n\n /**\n * Accept a figure in bytes for data transfer, and a boolean for whether\n * the domain shows as 'green', and return a CO2 figure for energy used to shift the corresponding\n * the data transfer.\n *\n * @param {number} bytes\n * @param {boolean} green\n * @return {number|CO2EstimateComponentsPerVisit} the amount of CO2 in grammes or its separate components\n */\n perVisit(bytes, green = false) {\n if (this.model?.perVisit) {\n return this.model.perVisit(bytes, green, this._segment, this._rating);\n } else {\n throw new Error(\n `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.`\n );\n }\n }\n\n /**\n * Accept a figure in bytes for data transfer, a boolean for whether\n * the domain shows as 'green', and an options object.\n * Returns an object containing CO2 figure, green boolean, and object of the variables used in calculating the CO2 figure.\n *\n * @param {number} bytes\n * @param {boolean} green\n * @param {Object} options\n * @return {CO2EstimateTraceResultPerByte} the amount of CO2 in grammes\n */\n perByteTrace(bytes, green = false, options = {}) {\n const adjustments = parseByteTraceOptions(\n options,\n this.model.version,\n green\n );\n\n // Filter out the trace items that aren't relevant to this function.\n const { gridIntensity, ...traceVariables } = adjustments;\n const {\n dataReloadRatio,\n firstVisitPercentage,\n returnVisitPercentage,\n ...otherVariables\n } = traceVariables;\n return {\n co2: this.model.perByte(\n bytes,\n green,\n this._segment,\n this._rating,\n adjustments\n ),\n green,\n variables: {\n description:\n \"Below are the variables used to calculate this CO2 estimate.\",\n bytes,\n gridIntensity: {\n description:\n \"The grid intensity (grams per kilowatt-hour) used to calculate this CO2 estimate.\",\n ...adjustments.gridIntensity,\n },\n ...otherVariables,\n },\n };\n }\n\n /**\n * Accept a figure in bytes for data transfer, a boolean for whether\n * the domain shows as 'green', and an options object.\n * Returns an object containing CO2 figure, green boolean, and object of the variables used in calculating the CO2 figure.\n *\n * @param {number} bytes\n * @param {boolean} green\n * @param {Object} options\n * @return {CO2EstimateTraceResultPerVisit} the amount of CO2 in grammes\n */\n perVisitTrace(bytes, green = false, options = {}) {\n if (this.model?.perVisit) {\n const adjustments = parseVisitTraceOptions(\n options,\n this.model.version,\n green\n );\n const { gridIntensity, ...variables } = adjustments;\n\n return {\n co2: this.model.perVisit(\n bytes,\n green,\n this._segment,\n this._rating,\n adjustments\n ),\n green,\n variables: {\n description:\n \"Below are the variables used to calculate this CO2 estimate.\",\n bytes,\n gridIntensity: {\n description:\n \"The grid intensity (grams per kilowatt-hour) used to calculate this CO2 estimate.\",\n ...adjustments.gridIntensity,\n },\n ...variables,\n },\n };\n } else {\n throw new Error(\n `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.`\n );\n }\n }\n\n SustainableWebDesignV3() {\n return new SustainableWebDesignV3();\n }\n\n SustainableWebDesignV4() {\n return new SustainableWebDesignV4();\n }\n\n OneByte() {\n return new OneByte();\n }\n}\n\nexport { CO2 };\nexport default CO2;\n"],
5
- "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AA6DA,kBAAoB;AACpB,uCAAmC;AACnC,uCAAmC;AAEnC,qBAGO;AAEP,MAAM,IAAI;AAAA,EACR,YAAY,SAAS;AACnB,SAAK,QAAQ,IAAI,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,yBAAyB;AACvB,WAAO,IAAI,iCAAAE,QAAuB;AAAA,EACpC;AAAA,EAEA,UAAU;AACR,WAAO,IAAI,YAAAD,QAAQ;AAAA,EACrB;AACF;AAGA,IAAO,cAAQ;",
6
- "names": ["SustainableWebDesignV3", "OneByte", "SustainableWebDesignV4"]
4
+ "sourcesContent": ["\"use strict\";\n\n/**\n * @typedef {Object} CO2EstimateTraceResultPerByte\n * @property {number|CO2EstimateComponentsPerByte} co2 - The CO2 estimate in grams or its separate components\n * @property {boolean} green - Whether the domain is green or not\n * @property {TraceResultVariablesPerByte} variables - The variables used to calculate the CO2 estimate\n */\n\n/**\n * @typedef {Object} CO2EstimateTraceResultPerVisit\n * @property {number|CO2EstimateComponentsPerVisit} co2 - The CO2 estimate in grams or its separate components\n * @property {boolean} green - Whether the domain is green or not\n * @property {TraceResultVariablesPerVisit} variables - The variables used to calculate the CO2 estimate\n */\n\n/**\n * @typedef {Object} TraceResultVariablesPerByte\n * @property {GridIntensityVariables} gridIntensity - The grid intensity related variables\n */\n/**\n * @typedef {Object} TraceResultVariablesPerVisit\n * @property {GridIntensityVariables} gridIntensity - The grid intensity related variables\n * @property {number} dataReloadRatio - What percentage of a page is reloaded on each subsequent page view\n * @property {number} firstVisitPercentage - What percentage of visits are loading this page for subsequent times\n * @property {number} returnVisitPercentage - What percentage of visits are loading this page for the second or more time\n */\n\n/**\n * @typedef {Object} GridIntensityVariables\n * @property {string} description - The description of the variables\n * @property {number} network - The network grid intensity set by the user or the default\n * @property {number} dataCenter - The data center grid intensity set by the user or the default\n * @property {number} device - The device grid intensity set by the user or the default\n * @property {number} production - The production grid intensity set by the user or the default\n */\n\n/**\n * @typedef {Object} CO2EstimateComponentsPerByte\n * @property {number} networkCO2 - The CO2 estimate for networking in grams\n * @property {number} dataCenterCO2 - The CO2 estimate for data centers in grams\n * @property {number} consumerDeviceCO2 - The CO2 estimate for consumer devices in grams\n * @property {number} productionCO2 - The CO2 estimate for device production in grams\n * @property {string} rating - The rating of the CO2 estimate based on the Sustainable Web Design Model\n * @property {number} total - The total CO2 estimate in grams\n */\n\n/**\n * @typedef {Object} CO2EstimateComponentsPerVisit\n * @property {number} 'networkCO2 - first' - The CO2 estimate for networking in grams on first visit\n * @property {number} 'networkCO2 - subsequent' - The CO2 estimate for networking in grams on subsequent visits\n * @property {number} 'dataCenterCO2 - first' - The CO2 estimate for data centers in grams on first visit\n * @property {number} 'dataCenterCO2 - subsequent' - The CO2 estimate for data centers in grams on subsequent visits\n * @property {number} 'consumerDeviceCO2 - first' - The CO2 estimate for consumer devices in grams on first visit\n * @property {number} 'consumerDeviceCO2 - subsequent' - The CO2 estimate for consumer devices in grams on subsequent visits\n * @property {number} 'productionCO2 - first' - The CO2 estimate for device production in grams on first visit\n * @property {number} 'productionCO2 - subsequent' - The CO2 estimate for device production in grams on subsequent visits\n * @property {string} rating - The rating of the CO2 estimate based on the Sustainable Web Design Model\n * @property {number} total - The total CO2 estimate in grams\n */\n\nimport OneByte from \"./1byte.js\";\nimport SustainableWebDesignV3 from \"./sustainable-web-design-v3.js\";\nimport SustainableWebDesignV4 from \"./sustainable-web-design-v4.js\";\n\nimport {\n parseByteTraceOptions,\n parseVisitTraceOptions,\n} from \"./helpers/index.js\";\n\nclass CO2 {\n constructor(options) {\n this.model = new SustainableWebDesignV4();\n // Using optional chaining allows an empty object to be passed\n // in without breaking the code.\n if (options?.model === \"1byte\") {\n this.model = new OneByte();\n } else if (options?.model === \"swd\") {\n this.model = new SustainableWebDesignV4();\n if (options?.version === 3) {\n this.model = new SustainableWebDesignV3();\n } else if (options?.version === 4) {\n this.model = new SustainableWebDesignV4();\n }\n } else if (options?.model) {\n throw new Error(\n `\"${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.`\n );\n } else if (!options?.model && options?.version) {\n throw new Error(\n `\"Specified version ${options?.version} but an estimation model is missing. Please specify a model to use for the version you have set.`\n );\n }\n\n if (options?.rating && typeof options.rating !== \"boolean\") {\n throw new Error(\n `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.`\n );\n }\n\n // This flag checks to see if the model itself has a rating system.\n const allowRatings = !!this.model.allowRatings;\n\n /** @private */\n this._segment = options?.results === \"segment\";\n // This flag is set by the user to enable the rating system.\n this._rating = options?.rating === true;\n\n // The rating system is only supported in the Sustainable Web Design Model.\n if (!allowRatings && this._rating) {\n throw new Error(\n `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.`\n );\n }\n }\n\n /**\n * Accept a figure in bytes for data transfer, and a boolean for whether\n * the domain shows as 'green', and return a CO2 figure for energy used to shift the corresponding\n * the data transfer.\n *\n * @param {number} bytes\n * @param {boolean} green\n * @return {number|CO2EstimateComponentsPerByte} the amount of CO2 in grammes or its separate components\n */\n perByte(bytes, green = false) {\n return this.model.perByte(bytes, green, this._segment, this._rating);\n }\n\n /**\n * Accept a figure in bytes for data transfer, and a boolean for whether\n * the domain shows as 'green', and return a CO2 figure for energy used to shift the corresponding\n * the data transfer.\n *\n * @param {number} bytes\n * @param {boolean} green\n * @return {number|CO2EstimateComponentsPerVisit} the amount of CO2 in grammes or its separate components\n */\n perVisit(bytes, green = false) {\n if (this.model?.perVisit) {\n return this.model.perVisit(bytes, green, this._segment, this._rating);\n } else {\n throw new Error(\n `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.`\n );\n }\n }\n\n /**\n * Accept a figure in bytes for data transfer, a boolean for whether\n * the domain shows as 'green', and an options object.\n * Returns an object containing CO2 figure, green boolean, and object of the variables used in calculating the CO2 figure.\n *\n * @param {number} bytes\n * @param {boolean} green\n * @param {Object} options\n * @return {CO2EstimateTraceResultPerByte} the amount of CO2 in grammes\n */\n perByteTrace(bytes, green = false, options = {}) {\n const adjustments = parseByteTraceOptions(\n options,\n this.model.version,\n green\n );\n\n // Filter out the trace items that aren't relevant to this function.\n const { gridIntensity, ...traceVariables } = adjustments;\n const {\n dataReloadRatio,\n firstVisitPercentage,\n returnVisitPercentage,\n ...otherVariables\n } = traceVariables;\n return {\n co2: this.model.perByte(\n bytes,\n green,\n this._segment,\n this._rating,\n adjustments\n ),\n green,\n variables: {\n description:\n \"Below are the variables used to calculate this CO2 estimate.\",\n bytes,\n gridIntensity: {\n description:\n \"The grid intensity (grams per kilowatt-hour) used to calculate this CO2 estimate.\",\n ...adjustments.gridIntensity,\n },\n ...otherVariables,\n },\n };\n }\n\n /**\n * Accept a figure in bytes for data transfer, a boolean for whether\n * the domain shows as 'green', and an options object.\n * Returns an object containing CO2 figure, green boolean, and object of the variables used in calculating the CO2 figure.\n *\n * @param {number} bytes\n * @param {boolean} green\n * @param {Object} options\n * @return {CO2EstimateTraceResultPerVisit} the amount of CO2 in grammes\n */\n perVisitTrace(bytes, green = false, options = {}) {\n if (this.model?.perVisit) {\n const adjustments = parseVisitTraceOptions(\n options,\n this.model.version,\n green\n );\n const { gridIntensity, ...variables } = adjustments;\n\n return {\n co2: this.model.perVisit(\n bytes,\n green,\n this._segment,\n this._rating,\n adjustments\n ),\n green,\n variables: {\n description:\n \"Below are the variables used to calculate this CO2 estimate.\",\n bytes,\n gridIntensity: {\n description:\n \"The grid intensity (grams per kilowatt-hour) used to calculate this CO2 estimate.\",\n ...adjustments.gridIntensity,\n },\n ...variables,\n },\n };\n } else {\n throw new Error(\n `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.`\n );\n }\n }\n\n SustainableWebDesignV3() {\n return new SustainableWebDesignV3();\n }\n\n SustainableWebDesignV4() {\n return new SustainableWebDesignV4();\n }\n\n OneByte() {\n return new OneByte();\n }\n}\n\nexport { CO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,QACL,KAAK,KAAK,MAAM;AAAA,UACd;AAAA,UACA;AAAA,UACA,KAAK;AAAA,UACL,KAAK;AAAA,UACL;AAAA,QACF;AAAA,QACA;AAAA,QACA,WAAW;AAAA,UACT,aACE;AAAA,UACF;AAAA,UACA,eAAe;AAAA,YACb,aACE;AAAA,YACF,GAAG,YAAY;AAAA,UACjB;AAAA,UACA,GAAG;AAAA,QACL;AAAA,MACF;AAAA,IACF,OAAO;AACL,YAAM,IAAI;AAAA,QACR;AAAA;AAAA,MACF;AAAA,IACF;AAAA,EACF;AAAA,EAEA,yBAAyB;AACvB,WAAO,IAAI,iCAAAE,QAAuB;AAAA,EACpC;AAAA,EAEA,yBAAyB;AACvB,WAAO,IAAI,iCAAAF,QAAuB;AAAA,EACpC;AAAA,EAEA,UAAU;AACR,WAAO,IAAI,YAAAC,QAAQ;AAAA,EACrB;AACF;AAGA,IAAO,cAAQ;",
6
+ "names": ["SustainableWebDesignV4", "OneByte", "SustainableWebDesignV3"]
7
7
  }
@@ -0,0 +1,38 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
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.
24
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
25
+ mod
26
+ ));
27
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
28
+ var data_exports = {};
29
+ __export(data_exports, {
30
+ averageIntensity: () => import_average_intensities_min.default,
31
+ default: () => data_default,
32
+ marginalIntensity: () => import_marginal_intensities_2021_min.default
33
+ });
34
+ module.exports = __toCommonJS(data_exports);
35
+ var import_average_intensities_min = __toESM(require("./data/average-intensities.min.js"));
36
+ var import_marginal_intensities_2021_min = __toESM(require("./data/marginal-intensities-2021.min.js"));
37
+ var data_default = { averageIntensity: import_average_intensities_min.default, marginalIntensity: import_marginal_intensities_2021_min.default };
38
+ //# sourceMappingURL=data.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/data.js"],
4
+ "sourcesContent": ["import averageIntensity from \"./data/average-intensities.min.js\";\nimport marginalIntensity from \"./data/marginal-intensities-2021.min.js\";\n\nexport { averageIntensity, marginalIntensity };\nexport default { averageIntensity, marginalIntensity };\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA,yDAAAA;AAAA,EAAA;AAAA,gEAAAC;AAAA;AAAA;AAAA,qCAA6B;AAC7B,2CAA8B;AAG9B,IAAO,eAAQ,EAAE,iDAAAD,SAAkB,wDAAAC,QAAkB;",
6
+ "names": ["averageIntensity", "marginalIntensity"]
7
+ }
@@ -197,7 +197,7 @@ Falling back to default value.`
197
197
  return adjustments;
198
198
  }
199
199
  function getApiRequestHeaders(comment = "") {
200
- return { "User-Agent": `co2js/${"0.16.9"} ${comment}` };
200
+ return { "User-Agent": `co2js/${"0.17.1-0"} ${comment}` };
201
201
  }
202
202
  function outputRating(co2e, swdmVersion) {
203
203
  let {
@@ -2,6 +2,6 @@
2
2
  "version": 3,
3
3
  "sources": ["../../../src/helpers/index.js"],
4
4
  "sourcesContent": ["import { averageIntensity } from \"../index.js\";\nimport {\n GLOBAL_GRID_INTENSITY as SWDM3_GLOBAL_GRID_INTENSITY,\n SWDV4,\n PERCENTAGE_OF_DATA_LOADED_ON_SUBSEQUENT_LOAD,\n FIRST_TIME_VIEWING_PERCENTAGE,\n RETURNING_VISITOR_PERCENTAGE,\n SWDMV3_RATINGS,\n SWDMV4_RATINGS,\n} from \"../constants/index.js\";\n\nconst SWDM4_GLOBAL_GRID_INTENSITY = SWDV4.GLOBAL_GRID_INTENSITY;\n// Shared type definitions to be used across different files\n\n/**\n * @typedef {Object} DomainCheckOptions options to control the behavior when checking a domain\n * @property {string} userAgentIdentifier - Optional. The app, site, or organisation that is making the request.\n * @property {boolean} verbose - Optional. Whether to return a verbose response.\n * @property {string[]} db - Optional. A database list to use for lookups.\n */\n\nconst formatNumber = (num) => parseFloat(num.toFixed(2));\n\nconst lessThanEqualTo = (num, limit) => num <= limit;\n\nfunction parseByteTraceOptions(options = {}, version = 3, green = false) {\n const globalGridIntensity =\n version === 4 ? SWDM4_GLOBAL_GRID_INTENSITY : SWDM3_GLOBAL_GRID_INTENSITY;\n // CHeck that it is an object\n if (typeof options !== \"object\") {\n throw new Error(\"Options must be an object\");\n }\n\n const adjustments = {};\n\n /**\n *\n * @param {string} segment The name of the segment (\"device\"|\"dataCenter\"|\"network\")\n * @param {number|object} segmentIntensity The segment intensity\n */\n function setIntensity(segment, segmentIntensity) {\n if (segmentIntensity || segmentIntensity === 0) {\n if (typeof segmentIntensity === \"object\") {\n if (!averageIntensity.data[segmentIntensity.country?.toUpperCase()]) {\n console.warn(\n `\"${segmentIntensity.country}\" is not a valid country. Please use a valid 3 digit ISO 3166 country code. \\nSee https://developers.thegreenwebfoundation.org/co2js/data/ for more information. \\nFalling back to global average grid intensity.`\n );\n adjustments.gridIntensity[segment] = {\n value: globalGridIntensity,\n };\n }\n adjustments.gridIntensity[segment] = {\n country: segmentIntensity.country,\n value: parseFloat(\n averageIntensity.data[segmentIntensity.country?.toUpperCase()]\n ),\n };\n } else if (typeof segmentIntensity === \"number\") {\n adjustments.gridIntensity[segment] = {\n value: segmentIntensity,\n };\n } else {\n adjustments.gridIntensity[segment] = {\n value: globalGridIntensity,\n };\n console.warn(\n `The ${segment} grid intensity must be a number or an object. You passed in a ${typeof segmentIntensity}. \\nFalling back to global average grid intensity.`\n );\n }\n } else {\n adjustments.gridIntensity[segment] = {\n value: globalGridIntensity,\n };\n }\n }\n\n if (options?.gridIntensity) {\n adjustments.gridIntensity = {};\n const { device, dataCenter, network } = options.gridIntensity;\n setIntensity(\"device\", device);\n setIntensity(\"dataCenter\", dataCenter);\n setIntensity(\"network\", network);\n } else {\n adjustments.gridIntensity = {\n device: { value: globalGridIntensity },\n dataCenter: { value: globalGridIntensity },\n network: { value: globalGridIntensity },\n };\n }\n\n if (\n options?.greenHostingFactor ||\n (options.greenHostingFactor === 0 && version === 4)\n ) {\n if (typeof options.greenHostingFactor === \"number\") {\n if (options.greenHostingFactor >= 0 && options.greenHostingFactor <= 1) {\n adjustments.greenHostingFactor = options.greenHostingFactor;\n } else {\n adjustments.greenHostingFactor = 0;\n console.warn(\n `The returnVisitPercentage option must be a number between 0 and 1. You passed in ${options.returnVisitPercentage}. \\nFalling back to default value.`\n );\n }\n } else {\n adjustments.greenHostingFactor = 0;\n console.warn(\n `The returnVisitPercentage option must be a number. You passed in a ${typeof options.returnVisitPercentage}. \\nFalling back to default value.`\n );\n }\n } else if (version === 4) {\n adjustments.greenHostingFactor = 0;\n }\n\n if (green) {\n adjustments.greenHostingFactor = 1;\n }\n\n return adjustments;\n}\n\nfunction parseVisitTraceOptions(options = {}, version = 3, green = false) {\n // CHeck that it is an object\n if (typeof options !== \"object\") {\n throw new Error(\"Options must be an object\");\n }\n\n const adjustments = parseByteTraceOptions(options, version, green);\n\n if (options?.dataReloadRatio || options.dataReloadRatio === 0) {\n if (typeof options.dataReloadRatio === \"number\") {\n if (options.dataReloadRatio >= 0 && options.dataReloadRatio <= 1) {\n adjustments.dataReloadRatio = options.dataReloadRatio;\n } else {\n adjustments.dataReloadRatio =\n version === 3 ? PERCENTAGE_OF_DATA_LOADED_ON_SUBSEQUENT_LOAD : 0;\n console.warn(\n `The dataReloadRatio option must be a number between 0 and 1. You passed in ${options.dataReloadRatio}. \\nFalling back to default value.`\n );\n }\n } else {\n adjustments.dataReloadRatio =\n version === 3 ? PERCENTAGE_OF_DATA_LOADED_ON_SUBSEQUENT_LOAD : 0;\n console.warn(\n `The dataReloadRatio option must be a number. You passed in a ${typeof options.dataReloadRatio}. \\nFalling back to default value.`\n );\n }\n } else {\n adjustments.dataReloadRatio =\n version === 3 ? PERCENTAGE_OF_DATA_LOADED_ON_SUBSEQUENT_LOAD : 0;\n console.warn(\n `The dataReloadRatio option must be a number. You passed in a ${typeof options.dataReloadRatio}. \\nFalling back to default value.`\n );\n }\n\n if (options?.firstVisitPercentage || options.firstVisitPercentage === 0) {\n if (typeof options.firstVisitPercentage === \"number\") {\n if (\n options.firstVisitPercentage >= 0 &&\n options.firstVisitPercentage <= 1\n ) {\n adjustments.firstVisitPercentage = options.firstVisitPercentage;\n } else {\n adjustments.firstVisitPercentage =\n version === 3 ? FIRST_TIME_VIEWING_PERCENTAGE : 1;\n console.warn(\n `The firstVisitPercentage option must be a number between 0 and 1. You passed in ${options.firstVisitPercentage}. \\nFalling back to default value.`\n );\n }\n } else {\n adjustments.firstVisitPercentage =\n version === 3 ? FIRST_TIME_VIEWING_PERCENTAGE : 1;\n console.warn(\n `The firstVisitPercentage option must be a number. You passed in a ${typeof options.firstVisitPercentage}. \\nFalling back to default value.`\n );\n }\n } else {\n adjustments.firstVisitPercentage =\n version === 3 ? FIRST_TIME_VIEWING_PERCENTAGE : 1;\n console.warn(\n `The firstVisitPercentage option must be a number. You passed in a ${typeof options.firstVisitPercentage}. \\nFalling back to default value.`\n );\n }\n\n if (options?.returnVisitPercentage || options.returnVisitPercentage === 0) {\n if (typeof options.returnVisitPercentage === \"number\") {\n if (\n options.returnVisitPercentage >= 0 &&\n options.returnVisitPercentage <= 1\n ) {\n adjustments.returnVisitPercentage = options.returnVisitPercentage;\n } else {\n adjustments.returnVisitPercentage =\n version === 3 ? RETURNING_VISITOR_PERCENTAGE : 0;\n console.warn(\n `The returnVisitPercentage option must be a number between 0 and 1. You passed in ${options.returnVisitPercentage}. \\nFalling back to default value.`\n );\n }\n } else {\n adjustments.returnVisitPercentage =\n version === 3 ? RETURNING_VISITOR_PERCENTAGE : 0;\n console.warn(\n `The returnVisitPercentage option must be a number. You passed in a ${typeof options.returnVisitPercentage}. \\nFalling back to default value.`\n );\n }\n } else {\n adjustments.returnVisitPercentage =\n version === 3 ? RETURNING_VISITOR_PERCENTAGE : 0;\n console.warn(\n `The returnVisitPercentage option must be a number. You passed in a ${typeof options.returnVisitPercentage}. \\nFalling back to default value.`\n );\n }\n\n return adjustments;\n}\n\n/**\n * Returns an object containing all the HTTP headers to use when making a request to the Green Web Foundation API.\n * @param {string} comment - Optional. The app, site, or organisation that is making the request.\n *\n * @returns {import('http').OutgoingHttpHeaders}\n */\nfunction getApiRequestHeaders(comment = \"\") {\n return { \"User-Agent\": `co2js/${process.env.CO2JS_VERSION} ${comment}` };\n}\n\n/**\n * Returns the SWDM rating for a given CO2e value and version of the SWDM.\n * @param {number} co2e - The CO2e value to rate.\n * @param {number} swdmVersion - The version of the SWDM to use. Defaults to version 3.\n * @returns {string} The SWDM rating.\n */\nfunction outputRating(co2e, swdmVersion) {\n let {\n FIFTH_PERCENTILE,\n TENTH_PERCENTILE,\n TWENTIETH_PERCENTILE,\n THIRTIETH_PERCENTILE,\n FORTIETH_PERCENTILE,\n FIFTIETH_PERCENTILE,\n } = SWDMV3_RATINGS;\n\n if (swdmVersion === 4) {\n FIFTH_PERCENTILE = SWDMV4_RATINGS.FIFTH_PERCENTILE;\n TENTH_PERCENTILE = SWDMV4_RATINGS.TENTH_PERCENTILE;\n TWENTIETH_PERCENTILE = SWDMV4_RATINGS.TWENTIETH_PERCENTILE;\n THIRTIETH_PERCENTILE = SWDMV4_RATINGS.THIRTIETH_PERCENTILE;\n FORTIETH_PERCENTILE = SWDMV4_RATINGS.FORTIETH_PERCENTILE;\n FIFTIETH_PERCENTILE = SWDMV4_RATINGS.FIFTIETH_PERCENTILE;\n }\n\n if (lessThanEqualTo(co2e, FIFTH_PERCENTILE)) {\n return \"A+\";\n } else if (lessThanEqualTo(co2e, TENTH_PERCENTILE)) {\n return \"A\";\n } else if (lessThanEqualTo(co2e, TWENTIETH_PERCENTILE)) {\n return \"B\";\n } else if (lessThanEqualTo(co2e, THIRTIETH_PERCENTILE)) {\n return \"C\";\n } else if (lessThanEqualTo(co2e, FORTIETH_PERCENTILE)) {\n return \"D\";\n } else if (lessThanEqualTo(co2e, FIFTIETH_PERCENTILE)) {\n return \"E\";\n } else {\n return \"F\";\n }\n}\n\nexport {\n formatNumber,\n parseByteTraceOptions,\n parseVisitTraceOptions,\n getApiRequestHeaders,\n lessThanEqualTo,\n outputRating,\n};\n"],
5
- "mappings": ";;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,eAAiC;AACjC,uBAQO;AAEP,MAAM,8BAA8B,uBAAM;AAU1C,MAAM,eAAe,CAAC,QAAQ,WAAW,IAAI,QAAQ,CAAC,CAAC;AAEvD,MAAM,kBAAkB,CAAC,KAAK,UAAU,OAAO;AAE/C,SAAS,sBAAsB,UAAU,CAAC,GAAG,UAAU,GAAG,QAAQ,OAAO;AACvE,QAAM,sBACJ,YAAY,IAAI,8BAA8B,iBAAAA;AAEhD,MAAI,OAAO,YAAY,UAAU;AAC/B,UAAM,IAAI,MAAM,2BAA2B;AAAA,EAC7C;AAEA,QAAM,cAAc,CAAC;AAOrB,WAAS,aAAa,SAAS,kBAAkB;AAxCnD;AAyCI,QAAI,oBAAoB,qBA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AC1C,SAAO,EAAE,cAAc,SAAS,UAAyB,IAAI,OAAO,GAAG;AACzE;AAQA,SAAS,aAAa,MAAM,aAAa;AACvC,MAAI;AAAA,IACF;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF,IAAI;AAEJ,MAAI,gBAAgB,GAAG;AACrB,uBAAmB,gCAAe;AAClC,uBAAmB,gCAAe;AAClC,2BAAuB,gCAAe;AACtC,2BAAuB,gCAAe;AACtC,0BAAsB,gCAAe;AACrC,0BAAsB,gCAAe;AAAA,EACvC;AAEA,MAAI,gBAAgB,MAAM,gBAAgB,GAAG;AAC3C,WAAO;AAAA,EACT,WAAW,gBAAgB,MAAM,gBAAgB,GAAG;AAClD,WAAO;AAAA,EACT,WAAW,gBAAgB,MAAM,oBAAoB,GAAG;AACtD,WAAO;AAAA,EACT,WAAW,gBAAgB,MAAM,oBAAoB,GAAG;AACtD,WAAO;AAAA,EACT,WAAW,gBAAgB,MAAM,mBAAmB,GAAG;AACrD,WAAO;AAAA,EACT,WAAW,gBAAgB,MAAM,mBAAmB,GAAG;AACrD,WAAO;AAAA,EACT,OAAO;AACL,WAAO;AAAA,EACT;AACF;",
6
6
  "names": ["SWDM3_GLOBAL_GRID_INTENSITY"]
7
7
  }
@@ -27,6 +27,7 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
27
27
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
28
28
  var hosting_node_exports = {};
29
29
  __export(hosting_node_exports, {
30
+ check: () => check,
30
31
  default: () => hosting_node_default
31
32
  });
32
33
  module.exports = __toCommonJS(hosting_node_exports);
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../src/hosting-node.js"],
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,QAAQ;AAE5C,UAAM,MAAM,aAAAA,QAAM;AAAA,MAChB;AAAA,MACA,EAAE,aAAS,qCAAqB,mBAAmB,EAAE;AAAA,MACrD,SAAU,KAAK;AACb,YAAI,IAAI,aAAa,OAAO,I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AK;AAAA,UAC/D;AAAA,QACF,CAAC;AAAA,QACD,QAAQ;AAAA,MACV;AAAA,IACF;AACA,WAAO,QAAQ,UACX,uBACA,oBAAAA,QAAY,wBAAwB,oBAAoB;AAAA,EAC9D,SAAS,GAAG;AACV,WAAO,QAAQ,UAAU,CAAC,IAAI,CAAC;AAAA,EACjC;AACF;AAEA,IAAO,uBAAQ;AAAA,EACb;AAAA,EACA,cAAc,oBAAAA,QAAY;AAAA,EAC1B,UAAU,yBAAAC,QAAgB;AAC5B;",
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\nexport function 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 ? {}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EACrC;AAEA,MAAI,OAAO,WAAW,UAAU;AAC9B,WAAO,gBAAgB,QAAQ,OAAO;AAAA,EACxC,OAAO;AACL,WAAO,uBAAuB,QAAQ,OAAO;AAAA,EAC/C;AACF;AASA,eAAe,gBAAgB,QAAQ,UAAU,CAAC,GAAG;AACnD,QAAM,MAAM,KAAK;AAAA,IACf,MAAM;AAAA,MACJ,oDAAoD,MAAM;AAAA,MAC1D,QAAQ;AAAA,IACV;AAAA,EACF;AACA,SAAO,QAAQ,UAAU,MAAM,IAAI;AACrC;AASA,eAAe,uBAAuB,SAAS,UAAU,CAAC,GAAG;AAC3D,MAAI;AACF,UAAM,uBAAuB,KAAK;AAAA,MAChC,MAAM;AAAA,QACJ,4DAA4D,KAAK;AAAA,UAC/D;AAAA,QACF,CAAC;AAAA,QACD,QAAQ;AAAA,MACV;AAAA,IACF;AACA,WAAO,QAAQ,UACX,uBACA,oBAAAA,QAAY,wBAAwB,oBAAoB;AAAA,EAC9D,SAAS,GAAG;AACV,WAAO,QAAQ,UAAU,CAAC,IAAI,CAAC;AAAA,EACjC;AACF;AAEA,IAAO,uBAAQ;AAAA,EACb;AAAA,EACA,cAAc,oBAAAA,QAAY;AAAA,EAC1B,UAAU,yBAAAC,QAAgB;AAC5B;",
6
6
  "names": ["https", "hostingJSON", "hostingJSONNode"]
7
7
  }
@@ -28,6 +28,7 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
28
28
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
29
  var hosting_exports = {};
30
30
  __export(hosting_exports, {
31
+ check: () => check,
31
32
  default: () => hosting_default
32
33
  });
33
34
  module.exports = __toCommonJS(hosting_exports);
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../src/hosting.js"],
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;",
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 */\n\nexport function check(domain, optionsOrAgentId) {\n return hostingAPI.check(domain, optionsOrAgentId);\n}\n\nexport default check;\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAEA,yBAAuB;AAkBhB,SAAS,MAAM,QAAQ,kBAAkB;AAC9C,SAAO,mBAAAA,QAAW,MAAM,QAAQ,gBAAgB;AAClD;AAEA,IAAO,kBAAQ;",
6
6
  "names": ["hostingAPI"]
7
7
  }
@@ -0,0 +1,249 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var sustainable_web_design_exports = {};
20
+ __export(sustainable_web_design_exports, {
21
+ SustainableWebDesign: () => SustainableWebDesign,
22
+ default: () => sustainable_web_design_default
23
+ });
24
+ module.exports = __toCommonJS(sustainable_web_design_exports);
25
+ var import_constants = require("./constants/index.js");
26
+ var import_helpers = require("./helpers/index.js");
27
+ class SustainableWebDesign {
28
+ constructor(options) {
29
+ this.options = options;
30
+ }
31
+ /**
32
+ * Accept a figure for bytes transferred and return an object representing
33
+ * the share of the total enrgy use of the entire system, broken down
34
+ * by each corresponding system component
35
+ *
36
+ * @param {number} bytes - the data transferred in bytes
37
+ * @return {object} Object containing the energy in kilowatt hours, keyed by system component
38
+ */
39
+ energyPerByteByComponent(bytes) {
40
+ const transferedBytesToGb = bytes / import_constants.fileSize.GIGABYTE;
41
+ const energyUsage = transferedBytesToGb * import_constants.KWH_PER_GB;
42
+ return {
43
+ consumerDeviceEnergy: energyUsage * import_constants.END_USER_DEVICE_ENERGY,
44
+ networkEnergy: energyUsage * import_constants.NETWORK_ENERGY,
45
+ productionEnergy: energyUsage * import_constants.PRODUCTION_ENERGY,
46
+ dataCenterEnergy: energyUsage * import_constants.DATACENTER_ENERGY
47
+ };
48
+ }
49
+ /**
50
+ * Accept an object keys by the different system components, and
51
+ * return an object with the co2 figures key by the each component
52
+ *
53
+ * @param {object} energyByComponent - energy grouped by the four system components
54
+ * @param {number} [carbonIntensity] - carbon intensity to apply to the datacentre values
55
+ * @return {number} the total number in grams of CO2 equivalent emissions
56
+ */
57
+ co2byComponent(energyByComponent, carbonIntensity = import_constants.GLOBAL_GRID_INTENSITY, options = {}) {
58
+ let deviceCarbonIntensity = import_constants.GLOBAL_GRID_INTENSITY;
59
+ let networkCarbonIntensity = import_constants.GLOBAL_GRID_INTENSITY;
60
+ let dataCenterCarbonIntensity = import_constants.GLOBAL_GRID_INTENSITY;
61
+ let globalEmissions = import_constants.GLOBAL_GRID_INTENSITY;
62
+ if (options == null ? void 0 : options.gridIntensity) {
63
+ const { device, network, dataCenter } = options.gridIntensity;
64
+ if ((device == null ? void 0 : device.value) || (device == null ? void 0 : device.value) === 0) {
65
+ deviceCarbonIntensity = device.value;
66
+ }
67
+ if ((network == null ? void 0 : network.value) || (network == null ? void 0 : network.value) === 0) {
68
+ networkCarbonIntensity = network.value;
69
+ }
70
+ if ((dataCenter == null ? void 0 : dataCenter.value) || (dataCenter == null ? void 0 : dataCenter.value) === 0) {
71
+ dataCenterCarbonIntensity = dataCenter.value;
72
+ }
73
+ }
74
+ if (carbonIntensity === true) {
75
+ dataCenterCarbonIntensity = import_constants.RENEWABLES_GRID_INTENSITY;
76
+ }
77
+ const returnCO2ByComponent = {};
78
+ for (const [key, value] of Object.entries(energyByComponent)) {
79
+ if (key.startsWith("dataCenterEnergy")) {
80
+ returnCO2ByComponent[key.replace("Energy", "CO2")] = value * dataCenterCarbonIntensity;
81
+ } else if (key.startsWith("consumerDeviceEnergy")) {
82
+ returnCO2ByComponent[key.replace("Energy", "CO2")] = value * deviceCarbonIntensity;
83
+ } else if (key.startsWith("networkEnergy")) {
84
+ returnCO2ByComponent[key.replace("Energy", "CO2")] = value * networkCarbonIntensity;
85
+ } else {
86
+ returnCO2ByComponent[key.replace("Energy", "CO2")] = value * globalEmissions;
87
+ }
88
+ }
89
+ return returnCO2ByComponent;
90
+ }
91
+ /**
92
+ * Accept a figure for bytes transferred and return a single figure for CO2
93
+ * emissions. Where information exists about the origin data is being
94
+ * fetched from, a different carbon intensity figure
95
+ * is applied for the data centre share of the carbon intensity.
96
+ *
97
+ * @param {number} bytes - the data transferred in bytes
98
+ * @param {boolean} carbonIntensity - a boolean indicating whether the data center is green or not
99
+ * @param {boolean} segmentResults - a boolean indicating whether to return the results broken down by component
100
+ * @param {object} options - an object containing the grid intensity and first/return visitor values
101
+ * @return {number|object} the total number in grams of CO2 equivalent emissions, or an object containing the breakdown by component
102
+ */
103
+ perByte(bytes, carbonIntensity = false, segmentResults = false, options = {}) {
104
+ if (bytes < 1) {
105
+ bytes = 0;
106
+ }
107
+ const energyBycomponent = this.energyPerByteByComponent(bytes, options);
108
+ if (typeof carbonIntensity !== "boolean") {
109
+ throw new Error(
110
+ `perByte expects a boolean for the carbon intensity value. Received: ${carbonIntensity}`
111
+ );
112
+ }
113
+ const co2ValuesbyComponent = this.co2byComponent(
114
+ energyBycomponent,
115
+ carbonIntensity,
116
+ options
117
+ );
118
+ const co2Values = Object.values(co2ValuesbyComponent);
119
+ const co2ValuesSum = co2Values.reduce(
120
+ (prevValue, currentValue) => prevValue + currentValue
121
+ );
122
+ if (segmentResults) {
123
+ return { ...co2ValuesbyComponent, total: co2ValuesSum };
124
+ }
125
+ return co2ValuesSum;
126
+ }
127
+ /**
128
+ * Accept a figure for bytes transferred and return a single figure for CO2
129
+ * emissions. This method applies caching assumptions from the original Sustainable Web Design model.
130
+ *
131
+ * @param {number} bytes - the data transferred in bytes
132
+ * @param {boolean} carbonIntensity - a boolean indicating whether the data center is green or not
133
+ * @param {boolean} segmentResults - a boolean indicating whether to return the results broken down by component
134
+ * @param {object} options - an object containing the grid intensity and first/return visitor values
135
+ * @return {number|object} the total number in grams of CO2 equivalent emissions, or an object containing the breakdown by component
136
+ */
137
+ perVisit(bytes, carbonIntensity = false, segmentResults = false, options = {}) {
138
+ const energyBycomponent = this.energyPerVisitByComponent(bytes, options);
139
+ if (typeof carbonIntensity !== "boolean") {
140
+ throw new Error(
141
+ `perVisit expects a boolean for the carbon intensity value. Received: ${carbonIntensity}`
142
+ );
143
+ }
144
+ const co2ValuesbyComponent = this.co2byComponent(
145
+ energyBycomponent,
146
+ carbonIntensity,
147
+ options
148
+ );
149
+ const co2Values = Object.values(co2ValuesbyComponent);
150
+ const co2ValuesSum = co2Values.reduce(
151
+ (prevValue, currentValue) => prevValue + currentValue
152
+ );
153
+ if (segmentResults) {
154
+ return { ...co2ValuesbyComponent, total: co2ValuesSum };
155
+ }
156
+ return co2ValuesSum;
157
+ }
158
+ /**
159
+ * Accept a figure for bytes transferred and return the number of kilowatt hours used
160
+ * by the total system for this data transfer
161
+ *
162
+ * @param {number} bytes
163
+ * @return {number} the number of kilowatt hours used
164
+ */
165
+ energyPerByte(bytes) {
166
+ const energyByComponent = this.energyPerByteByComponent(bytes);
167
+ const energyValues = Object.values(energyByComponent);
168
+ return energyValues.reduce(
169
+ (prevValue, currentValue) => prevValue + currentValue
170
+ );
171
+ }
172
+ /**
173
+ * Accept a figure for bytes transferred, and return an object containing figures
174
+ * per system component, with the caching assumptions applied. This tries to account
175
+ * for webpages being loaded from a cache by browsers, so if you had a thousand page views,
176
+ * and tried to work out the energy per visit, the numbers would reflect the reduced amounts
177
+ * of transfer.
178
+ *
179
+ * @param {number} bytes - the data transferred in bytes for loading a webpage
180
+ * @param {number} firstView - what percentage of visits are loading this page for the first time
181
+ * @param {number} returnView - what percentage of visits are loading this page for subsequent times
182
+ * @param {number} dataReloadRatio - what percentage of a page is reloaded on each subsequent page view
183
+ *
184
+ * @return {object} Object containing the energy in kilowatt hours, keyed by system component
185
+ */
186
+ 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) {
187
+ if (options.dataReloadRatio || options.dataReloadRatio === 0) {
188
+ dataReloadRatio = options.dataReloadRatio;
189
+ }
190
+ if (options.firstVisitPercentage || options.firstVisitPercentage === 0) {
191
+ firstView = options.firstVisitPercentage;
192
+ }
193
+ if (options.returnVisitPercentage || options.returnVisitPercentage === 0) {
194
+ returnView = options.returnVisitPercentage;
195
+ }
196
+ const energyBycomponent = this.energyPerByteByComponent(bytes);
197
+ const cacheAdjustedSegmentEnergy = {};
198
+ const energyValues = Object.values(energyBycomponent);
199
+ for (const [key, value] of Object.entries(energyBycomponent)) {
200
+ cacheAdjustedSegmentEnergy[`${key} - first`] = value * firstView;
201
+ cacheAdjustedSegmentEnergy[`${key} - subsequent`] = value * returnView * dataReloadRatio;
202
+ }
203
+ return cacheAdjustedSegmentEnergy;
204
+ }
205
+ /**
206
+ * Accept a figure for bytes, and return the total figure for energy per visit
207
+ * using the default caching assumptions for loading a single website
208
+ *
209
+ * @param {number} bytes
210
+ * @return {number} the total energy use for the visit, after applying the caching assumptions
211
+ */
212
+ energyPerVisit(bytes) {
213
+ let firstVisits = 0;
214
+ let subsequentVisits = 0;
215
+ const energyBycomponent = Object.entries(
216
+ this.energyPerVisitByComponent(bytes)
217
+ );
218
+ for (const [key, val] of energyBycomponent) {
219
+ if (key.indexOf("first") > 0) {
220
+ firstVisits += val;
221
+ }
222
+ }
223
+ for (const [key, val] of energyBycomponent) {
224
+ if (key.indexOf("subsequent") > 0) {
225
+ subsequentVisits += val;
226
+ }
227
+ }
228
+ return firstVisits + subsequentVisits;
229
+ }
230
+ emissionsPerVisitInGrams(energyPerVisit, carbonintensity = import_constants.GLOBAL_GRID_INTENSITY) {
231
+ return (0, import_helpers.formatNumber)(energyPerVisit * carbonintensity);
232
+ }
233
+ annualEnergyInKwh(energyPerVisit, monthlyVisitors = 1e3) {
234
+ return energyPerVisit * monthlyVisitors * 12;
235
+ }
236
+ annualEmissionsInGrams(co2grams, monthlyVisitors = 1e3) {
237
+ return co2grams * monthlyVisitors * 12;
238
+ }
239
+ annualSegmentEnergy(annualEnergy) {
240
+ return {
241
+ consumerDeviceEnergy: (0, import_helpers.formatNumber)(annualEnergy * import_constants.END_USER_DEVICE_ENERGY),
242
+ networkEnergy: (0, import_helpers.formatNumber)(annualEnergy * import_constants.NETWORK_ENERGY),
243
+ dataCenterEnergy: (0, import_helpers.formatNumber)(annualEnergy * import_constants.DATACENTER_ENERGY),
244
+ productionEnergy: (0, import_helpers.formatNumber)(annualEnergy * import_constants.PRODUCTION_ENERGY)
245
+ };
246
+ }
247
+ }
248
+ var sustainable_web_design_default = SustainableWebDesign;
249
+ //# sourceMappingURL=sustainable-web-design.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/sustainable-web-design.js"],
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 } 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 || 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 {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 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 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 {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 {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 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 || 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\nexport { SustainableWebDesign };\nexport default SustainableWebDesign;\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAUA,uBAYO;AACP,qBAA6B;AAE7B,MAAM,qBAAqB;AAAA,EACzB,YAAY,SAAS;AACnB,SAAK,UAAU;AAAA,EACjB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAUA,yBAAyB,OAAO;AAC9B,UAAM,sBAAsB,QAAQ,0BAAS;AAC7C,UAAM,cAAc,sBAAsB;AAG1C,WAAO;AAAA,MACL,sBAAsB,cAAc;AAAA,MACpC,eAAe,cAAc;AAAA,MAC7B,kBAAkB,cAAc;AAAA,MAChC,kBAAkB,cAAc;AAAA,IAClC;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASA,eACE,mBACA,kBAAkB,wCAClB,UAAU,CAAC,GACX;AACA,QAAI,wBAAwB;AAC5B,QAAI,yBAAyB;AAC7B,QAAI,4BAA4B;AAEhC,QAAI,kBAAkB;AAEtB,QAAI,mCAAS,eAAe;AAC1B,YAAM,EAAE,QAAQ,SAAS,WAAW,IAAI,QAAQ;AAEhD,WAAI,iCAAQ,WAAS,iCAAQ,WAAU,GAAG;AACxC,gCAAwB,OAAO;AAAA,MACjC;AACA,WAAI,mCAAS,WAAS,mCAAS,WAAU,GAAG;AAC1C,iCAAyB,QAAQ;AAAA,MACnC;AAEA,WAAI,yCAAY,WAAS,yCAAY,WAAU,GAAG;AAChD,oCAA4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AAQ;",
6
+ "names": []
7
+ }
package/dist/esm/co2.js CHANGED
@@ -35,18 +35,24 @@ import {
35
35
  } from "./helpers/index.js";
36
36
  class CO2 {
37
37
  constructor(options) {
38
- this.model = new SustainableWebDesignV3();
38
+ this.model = new SustainableWebDesignV4();
39
39
  if ((options == null ? void 0 : options.model) === "1byte") {
40
40
  this.model = new OneByte();
41
41
  } else if ((options == null ? void 0 : options.model) === "swd") {
42
- this.model = new SustainableWebDesignV3();
43
- if ((options == null ? void 0 : options.version) === 4) {
42
+ this.model = new SustainableWebDesignV4();
43
+ if ((options == null ? void 0 : options.version) === 3) {
44
+ this.model = new SustainableWebDesignV3();
45
+ } else if ((options == null ? void 0 : options.version) === 4) {
44
46
  this.model = new SustainableWebDesignV4();
45
47
  }
46
48
  } else if (options == null ? void 0 : options.model) {
47
49
  throw new Error(
48
50
  '"'.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.')
49
51
  );
52
+ } else if (!(options == null ? void 0 : options.model) && (options == null ? void 0 : options.version)) {
53
+ throw new Error(
54
+ '"Specified version '.concat(options == null ? void 0 : options.version, " but an estimation model is missing. Please specify a model to use for the version you have set.")
55
+ );
50
56
  }
51
57
  if ((options == null ? void 0 : options.rating) && typeof options.rating !== "boolean") {
52
58
  throw new Error(
@@ -0,0 +1,8 @@
1
+ import averageIntensity from "./data/average-intensities.min.js";
2
+ import marginalIntensity from "./data/marginal-intensities-2021.min.js";
3
+ var data_default = { averageIntensity, marginalIntensity };
4
+ export {
5
+ averageIntensity,
6
+ data_default as default,
7
+ marginalIntensity
8
+ };
@@ -164,7 +164,7 @@ function parseVisitTraceOptions(options = {}, version = 3, green = false) {
164
164
  return adjustments;
165
165
  }
166
166
  function getApiRequestHeaders(comment = "") {
167
- return { "User-Agent": "co2js/".concat("0.16.9", " ").concat(comment) };
167
+ return { "User-Agent": "co2js/".concat("0.17.1-0", " ").concat(comment) };
168
168
  }
169
169
  function outputRating(co2e, swdmVersion) {
170
170
  let {
@@ -4,5 +4,6 @@ function check(domain, optionsOrAgentId) {
4
4
  }
5
5
  var hosting_default = check;
6
6
  export {
7
+ check,
7
8
  hosting_default as default
8
9
  };
@@ -0,0 +1,259 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __defProps = Object.defineProperties;
3
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
4
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
7
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
8
+ var __spreadValues = (a, b) => {
9
+ for (var prop in b || (b = {}))
10
+ if (__hasOwnProp.call(b, prop))
11
+ __defNormalProp(a, prop, b[prop]);
12
+ if (__getOwnPropSymbols)
13
+ for (var prop of __getOwnPropSymbols(b)) {
14
+ if (__propIsEnum.call(b, prop))
15
+ __defNormalProp(a, prop, b[prop]);
16
+ }
17
+ return a;
18
+ };
19
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
20
+ import {
21
+ fileSize,
22
+ KWH_PER_GB,
23
+ END_USER_DEVICE_ENERGY,
24
+ NETWORK_ENERGY,
25
+ DATACENTER_ENERGY,
26
+ PRODUCTION_ENERGY,
27
+ GLOBAL_GRID_INTENSITY,
28
+ RENEWABLES_GRID_INTENSITY,
29
+ FIRST_TIME_VIEWING_PERCENTAGE,
30
+ RETURNING_VISITOR_PERCENTAGE,
31
+ PERCENTAGE_OF_DATA_LOADED_ON_SUBSEQUENT_LOAD
32
+ } from "./constants/index.js";
33
+ import { formatNumber } from "./helpers/index.js";
34
+ class SustainableWebDesign {
35
+ constructor(options) {
36
+ this.options = options;
37
+ }
38
+ /**
39
+ * Accept a figure for bytes transferred and return an object representing
40
+ * the share of the total enrgy use of the entire system, broken down
41
+ * by each corresponding system component
42
+ *
43
+ * @param {number} bytes - the data transferred in bytes
44
+ * @return {object} Object containing the energy in kilowatt hours, keyed by system component
45
+ */
46
+ energyPerByteByComponent(bytes) {
47
+ const transferedBytesToGb = bytes / fileSize.GIGABYTE;
48
+ const energyUsage = transferedBytesToGb * KWH_PER_GB;
49
+ return {
50
+ consumerDeviceEnergy: energyUsage * END_USER_DEVICE_ENERGY,
51
+ networkEnergy: energyUsage * NETWORK_ENERGY,
52
+ productionEnergy: energyUsage * PRODUCTION_ENERGY,
53
+ dataCenterEnergy: energyUsage * DATACENTER_ENERGY
54
+ };
55
+ }
56
+ /**
57
+ * Accept an object keys by the different system components, and
58
+ * return an object with the co2 figures key by the each component
59
+ *
60
+ * @param {object} energyByComponent - energy grouped by the four system components
61
+ * @param {number} [carbonIntensity] - carbon intensity to apply to the datacentre values
62
+ * @return {number} the total number in grams of CO2 equivalent emissions
63
+ */
64
+ co2byComponent(energyByComponent, carbonIntensity = GLOBAL_GRID_INTENSITY, options = {}) {
65
+ let deviceCarbonIntensity = GLOBAL_GRID_INTENSITY;
66
+ let networkCarbonIntensity = GLOBAL_GRID_INTENSITY;
67
+ let dataCenterCarbonIntensity = GLOBAL_GRID_INTENSITY;
68
+ let globalEmissions = GLOBAL_GRID_INTENSITY;
69
+ if (options == null ? void 0 : options.gridIntensity) {
70
+ const { device, network, dataCenter } = options.gridIntensity;
71
+ if ((device == null ? void 0 : device.value) || (device == null ? void 0 : device.value) === 0) {
72
+ deviceCarbonIntensity = device.value;
73
+ }
74
+ if ((network == null ? void 0 : network.value) || (network == null ? void 0 : network.value) === 0) {
75
+ networkCarbonIntensity = network.value;
76
+ }
77
+ if ((dataCenter == null ? void 0 : dataCenter.value) || (dataCenter == null ? void 0 : dataCenter.value) === 0) {
78
+ dataCenterCarbonIntensity = dataCenter.value;
79
+ }
80
+ }
81
+ if (carbonIntensity === true) {
82
+ dataCenterCarbonIntensity = RENEWABLES_GRID_INTENSITY;
83
+ }
84
+ const returnCO2ByComponent = {};
85
+ for (const [key, value] of Object.entries(energyByComponent)) {
86
+ if (key.startsWith("dataCenterEnergy")) {
87
+ returnCO2ByComponent[key.replace("Energy", "CO2")] = value * dataCenterCarbonIntensity;
88
+ } else if (key.startsWith("consumerDeviceEnergy")) {
89
+ returnCO2ByComponent[key.replace("Energy", "CO2")] = value * deviceCarbonIntensity;
90
+ } else if (key.startsWith("networkEnergy")) {
91
+ returnCO2ByComponent[key.replace("Energy", "CO2")] = value * networkCarbonIntensity;
92
+ } else {
93
+ returnCO2ByComponent[key.replace("Energy", "CO2")] = value * globalEmissions;
94
+ }
95
+ }
96
+ return returnCO2ByComponent;
97
+ }
98
+ /**
99
+ * Accept a figure for bytes transferred and return a single figure for CO2
100
+ * emissions. Where information exists about the origin data is being
101
+ * fetched from, a different carbon intensity figure
102
+ * is applied for the data centre share of the carbon intensity.
103
+ *
104
+ * @param {number} bytes - the data transferred in bytes
105
+ * @param {boolean} carbonIntensity - a boolean indicating whether the data center is green or not
106
+ * @param {boolean} segmentResults - a boolean indicating whether to return the results broken down by component
107
+ * @param {object} options - an object containing the grid intensity and first/return visitor values
108
+ * @return {number|object} the total number in grams of CO2 equivalent emissions, or an object containing the breakdown by component
109
+ */
110
+ perByte(bytes, carbonIntensity = false, segmentResults = false, options = {}) {
111
+ if (bytes < 1) {
112
+ bytes = 0;
113
+ }
114
+ const energyBycomponent = this.energyPerByteByComponent(bytes, options);
115
+ if (typeof carbonIntensity !== "boolean") {
116
+ throw new Error(
117
+ "perByte expects a boolean for the carbon intensity value. Received: ".concat(carbonIntensity)
118
+ );
119
+ }
120
+ const co2ValuesbyComponent = this.co2byComponent(
121
+ energyBycomponent,
122
+ carbonIntensity,
123
+ options
124
+ );
125
+ const co2Values = Object.values(co2ValuesbyComponent);
126
+ const co2ValuesSum = co2Values.reduce(
127
+ (prevValue, currentValue) => prevValue + currentValue
128
+ );
129
+ if (segmentResults) {
130
+ return __spreadProps(__spreadValues({}, co2ValuesbyComponent), { total: co2ValuesSum });
131
+ }
132
+ return co2ValuesSum;
133
+ }
134
+ /**
135
+ * Accept a figure for bytes transferred and return a single figure for CO2
136
+ * emissions. This method applies caching assumptions from the original Sustainable Web Design model.
137
+ *
138
+ * @param {number} bytes - the data transferred in bytes
139
+ * @param {boolean} carbonIntensity - a boolean indicating whether the data center is green or not
140
+ * @param {boolean} segmentResults - a boolean indicating whether to return the results broken down by component
141
+ * @param {object} options - an object containing the grid intensity and first/return visitor values
142
+ * @return {number|object} the total number in grams of CO2 equivalent emissions, or an object containing the breakdown by component
143
+ */
144
+ perVisit(bytes, carbonIntensity = false, segmentResults = false, options = {}) {
145
+ const energyBycomponent = this.energyPerVisitByComponent(bytes, options);
146
+ if (typeof carbonIntensity !== "boolean") {
147
+ throw new Error(
148
+ "perVisit expects a boolean for the carbon intensity value. Received: ".concat(carbonIntensity)
149
+ );
150
+ }
151
+ const co2ValuesbyComponent = this.co2byComponent(
152
+ energyBycomponent,
153
+ carbonIntensity,
154
+ options
155
+ );
156
+ const co2Values = Object.values(co2ValuesbyComponent);
157
+ const co2ValuesSum = co2Values.reduce(
158
+ (prevValue, currentValue) => prevValue + currentValue
159
+ );
160
+ if (segmentResults) {
161
+ return __spreadProps(__spreadValues({}, co2ValuesbyComponent), { total: co2ValuesSum });
162
+ }
163
+ return co2ValuesSum;
164
+ }
165
+ /**
166
+ * Accept a figure for bytes transferred and return the number of kilowatt hours used
167
+ * by the total system for this data transfer
168
+ *
169
+ * @param {number} bytes
170
+ * @return {number} the number of kilowatt hours used
171
+ */
172
+ energyPerByte(bytes) {
173
+ const energyByComponent = this.energyPerByteByComponent(bytes);
174
+ const energyValues = Object.values(energyByComponent);
175
+ return energyValues.reduce(
176
+ (prevValue, currentValue) => prevValue + currentValue
177
+ );
178
+ }
179
+ /**
180
+ * Accept a figure for bytes transferred, and return an object containing figures
181
+ * per system component, with the caching assumptions applied. This tries to account
182
+ * for webpages being loaded from a cache by browsers, so if you had a thousand page views,
183
+ * and tried to work out the energy per visit, the numbers would reflect the reduced amounts
184
+ * of transfer.
185
+ *
186
+ * @param {number} bytes - the data transferred in bytes for loading a webpage
187
+ * @param {number} firstView - what percentage of visits are loading this page for the first time
188
+ * @param {number} returnView - what percentage of visits are loading this page for subsequent times
189
+ * @param {number} dataReloadRatio - what percentage of a page is reloaded on each subsequent page view
190
+ *
191
+ * @return {object} Object containing the energy in kilowatt hours, keyed by system component
192
+ */
193
+ energyPerVisitByComponent(bytes, options = {}, firstView = FIRST_TIME_VIEWING_PERCENTAGE, returnView = RETURNING_VISITOR_PERCENTAGE, dataReloadRatio = PERCENTAGE_OF_DATA_LOADED_ON_SUBSEQUENT_LOAD) {
194
+ if (options.dataReloadRatio || options.dataReloadRatio === 0) {
195
+ dataReloadRatio = options.dataReloadRatio;
196
+ }
197
+ if (options.firstVisitPercentage || options.firstVisitPercentage === 0) {
198
+ firstView = options.firstVisitPercentage;
199
+ }
200
+ if (options.returnVisitPercentage || options.returnVisitPercentage === 0) {
201
+ returnView = options.returnVisitPercentage;
202
+ }
203
+ const energyBycomponent = this.energyPerByteByComponent(bytes);
204
+ const cacheAdjustedSegmentEnergy = {};
205
+ const energyValues = Object.values(energyBycomponent);
206
+ for (const [key, value] of Object.entries(energyBycomponent)) {
207
+ cacheAdjustedSegmentEnergy["".concat(key, " - first")] = value * firstView;
208
+ cacheAdjustedSegmentEnergy["".concat(key, " - subsequent")] = value * returnView * dataReloadRatio;
209
+ }
210
+ return cacheAdjustedSegmentEnergy;
211
+ }
212
+ /**
213
+ * Accept a figure for bytes, and return the total figure for energy per visit
214
+ * using the default caching assumptions for loading a single website
215
+ *
216
+ * @param {number} bytes
217
+ * @return {number} the total energy use for the visit, after applying the caching assumptions
218
+ */
219
+ energyPerVisit(bytes) {
220
+ let firstVisits = 0;
221
+ let subsequentVisits = 0;
222
+ const energyBycomponent = Object.entries(
223
+ this.energyPerVisitByComponent(bytes)
224
+ );
225
+ for (const [key, val] of energyBycomponent) {
226
+ if (key.indexOf("first") > 0) {
227
+ firstVisits += val;
228
+ }
229
+ }
230
+ for (const [key, val] of energyBycomponent) {
231
+ if (key.indexOf("subsequent") > 0) {
232
+ subsequentVisits += val;
233
+ }
234
+ }
235
+ return firstVisits + subsequentVisits;
236
+ }
237
+ emissionsPerVisitInGrams(energyPerVisit, carbonintensity = GLOBAL_GRID_INTENSITY) {
238
+ return formatNumber(energyPerVisit * carbonintensity);
239
+ }
240
+ annualEnergyInKwh(energyPerVisit, monthlyVisitors = 1e3) {
241
+ return energyPerVisit * monthlyVisitors * 12;
242
+ }
243
+ annualEmissionsInGrams(co2grams, monthlyVisitors = 1e3) {
244
+ return co2grams * monthlyVisitors * 12;
245
+ }
246
+ annualSegmentEnergy(annualEnergy) {
247
+ return {
248
+ consumerDeviceEnergy: formatNumber(annualEnergy * END_USER_DEVICE_ENERGY),
249
+ networkEnergy: formatNumber(annualEnergy * NETWORK_ENERGY),
250
+ dataCenterEnergy: formatNumber(annualEnergy * DATACENTER_ENERGY),
251
+ productionEnergy: formatNumber(annualEnergy * PRODUCTION_ENERGY)
252
+ };
253
+ }
254
+ }
255
+ var sustainable_web_design_default = SustainableWebDesign;
256
+ export {
257
+ SustainableWebDesign,
258
+ sustainable_web_design_default as default
259
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tgwf/co2",
3
- "version": "0.16.9",
3
+ "version": "0.17.1-0",
4
4
  "description": "Work out the co2 of your digital services",
5
5
  "main": "dist/cjs/index-node.js",
6
6
  "module": "dist/esm/index.js",
@@ -11,6 +11,14 @@
11
11
  ".": {
12
12
  "import": "./dist/esm/index.js",
13
13
  "require": "./dist/cjs/index-node.js"
14
+ },
15
+ "./data": {
16
+ "import": "./dist/esm/data.js",
17
+ "require": "./dist/cjs/data.js"
18
+ },
19
+ "./hosting": {
20
+ "import": "./dist/esm/hosting.js",
21
+ "require": "./dist/cjs/hosting-node.js"
14
22
  }
15
23
  },
16
24
  "scripts": {