@tgwf/co2 0.7.0 → 0.9.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
@@ -8,7 +8,37 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
8
8
 
9
9
  ## Unreleased
10
10
 
11
- - Include a new alternative, "Green Byte Model" with the figures after speaking to folks at the IEA and other places.
11
+
12
+
13
+ # [0.9.0] - 2022-03-28
14
+
15
+ ### Added
16
+
17
+ - Added newly implemented Sustainable Web Design model [thanks @dryden!]
18
+ - Added new readme page for using both emissions models
19
+ - Added new source of data to the Sustainable Web Design model from Ember Climate.
20
+ ### Changed
21
+
22
+ - Changed the CO2 class to accept either the One Byte model or the Sustainable Web Design model
23
+ ### Fixed
24
+
25
+ - Fixed various typos.
26
+
27
+ # [0.8.0] - 2021-11-28
28
+
29
+ ### Fixed
30
+
31
+ - Update further dependencies
32
+ - Fix embarassing order of magnitude typo in 1byte model (thanks @mstaschik!)
33
+
34
+ ## Added
35
+
36
+ - Read JSON blob also as gzipped #44 (thanks @soulgalore)
37
+
38
+ ### Changed
39
+
40
+ - The 1byte model will give different numbers now. It's mentioned in `#fixed` but it's worth repeating.
41
+
12
42
  ## [0.7.0] - 2021-11-28
13
43
 
14
44
  ### Fixed
@@ -45,9 +75,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
45
75
 
46
76
  ### Changed
47
77
 
48
- - Update README
49
- - Update the emissions figured for green energy after further research on methodology with @@JamieBeevor
50
- - Incorproate class based CO2 models from @soulgalore
78
+ - Updated README
79
+ - Updated the emissions figured for green energy after further research on methodology with @@JamieBeevor
80
+ - Incorporated class based CO2 models from @soulgalore
51
81
  - Credit contributors
52
82
 
53
83
 
@@ -67,7 +97,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
67
97
 
68
98
  ### Added
69
99
 
70
- Add the (currently unused) green byte model.
100
+ Added the (currently unused) green byte model.
71
101
 
72
102
  ### Changed
73
103
 
package/README.md CHANGED
@@ -3,24 +3,37 @@
3
3
  <img src="https://github.com/thegreenwebfoundation/co2.js/actions/workflows/unittests.yml/badge.svg" />
4
4
 
5
5
 
6
- We know computers use electricity, and because most of the electricity we use comes from burning fossil fuels to generate, there is an environmental cost to every upload and download we make over the internet.
6
+ We know computers use electricity, and because most of the electricity we use comes from burning fossil fuels, there is an environmental cost to every upload and download we make over the internet.
7
7
 
8
8
  We can do something about this though. The same way we use performance budgets to make apps and websites faster and cheaper to run, we can use carbon budgets to make them faster, cheaper and _greener_.
9
9
 
10
10
  The CO2 package from [The Green Web Foundation][tgwf] lets you quickly estimate these emissions, to make measurable improvements as part of your workflow.
11
11
 
12
- It does this by implementing the 1byte model as used by the Shift Project, as introduced in their report on CO2 emissions from digital infrastructure, [Lean ICT: for a sober digital][soberDigital], to return a number for the estimated CO2 emissions for the corresponding number of bytes sent over the wire.
12
+ ### How it works
13
13
 
14
- It is currently used in the web performance tool [sitespeed.io][], to help developers build greener, more planet friendly digital services.
14
+ It does this by implementing various models for converting the measurable usage of digital services into a figure for the estimated CO2 emissions.
15
+
16
+ This defaults to the 1byte model as used by the Shift Project, as introduced in their report on CO2 emissions from digital infrastructure, [Lean ICT: for a sober digital][soberDigital], with the [Sustainable Web Design model][swd] as a popular alternative..
17
+
18
+ For more information, see the documentation [for when to use the different models, with code samples to start you off](./src/readme.md).
19
+
20
+ ### Who uses it
21
+
22
+ It is currently used in the web performance tool [sitespeed.io][], [ecoping][], [Websitecarbon.com](websitecarbon), and [ecograder][] to help developers build greener, more planet friendly digital services.
15
23
 
16
24
  If you want to build this kind of environmental information into your own software, and want some advice, we'd be happy to hear from you - please open an issue, remembering to link to your project.
17
25
 
18
- This is open source software, with all the guarantees associated, so if you want professional advice, to a deadline, and you have a budget please see the services offered by the [Green Web Foundation][tgwf-services].
26
+ This is open source software, with all the guarantees associated. So if you want professional advice, to a deadline, and you have a budget please see the services offered by the [Green Web Foundation][tgwf-services].
19
27
 
28
+
29
+ [sitespeed.io]: https://sitespeed.io
30
+ [ecoping]: https://ecoping.earth
31
+ [ecograder]: https://ecograder.com
32
+ [websitecarbon]: https://www.websitecarbon.com
33
+ [tgwf]: https://www.thegreenwebfoundation.org
34
+ [tgwf-services]: https://www.thegreenwebfoundation.org/services
35
+ [swd]: https://sustainablewebdesign.org/calculating-digital-emissions
20
36
  [soberDigital]: https://theshiftproject.org/en/lean-ict-2/
21
- [sitespeed.io]: https://sitespeed.io/
22
- [tgwf]: https://www.thegreenwebfoundation.org/
23
- [tgwf-services]: https://www.thegreenwebfoundation.org/services/
24
37
 
25
38
 
26
39
  ## Usage
@@ -29,7 +42,7 @@ This is open source software, with all the guarantees associated, so if you want
29
42
 
30
43
  #### Server-side
31
44
 
32
- This approach relies on the `fs` module and so can only be used on platforms, like Node.js, that support this.
45
+ This approach relies on the `fs` module and so can only be used on platforms like Node.js, that support this.
33
46
 
34
47
  ```js
35
48
 
@@ -54,7 +67,7 @@ const co2Emission = new CO2();
54
67
  estimatedCO2 = co2Emission.perByte(bytesSent)
55
68
 
56
69
  console.log(`Sending a gigabyte, had a carbon footprint of ${estimatedCO2.toFixed(3)} grams of CO2`)
57
-
70
+ ****
58
71
  ```
59
72
 
60
73
  ### Checking for green power
@@ -76,22 +89,6 @@ greencheck.checkPage(["google.com"])
76
89
 
77
90
  ```
78
91
 
79
- ### Notes
80
-
81
- Please note, we currently look at just the carbon cost of _generating_ the electricity, similar to how the [International Energy Agency (IEA)] does, not the full life cycle cost of the energy.
82
-
83
- Doing this would include things like:
84
-
85
- - the carbon emitted when carrying out activity associated with digging up the fuel
86
- - the carbon associated with mining the materials to _build_ the power stations, datacentres, and so on
87
- - the end of life costs
88
- - the maintenance costs over the life of the datacentres, power generation and end user devices, and the rest of the internet
89
-
90
- Life cycle figures do exist, but they are very difficult to do well. If you're interested in contributing to this, we'd love to hear from you.
91
-
92
-
93
92
  # Licenses
94
93
 
95
94
  Apache 2.0
96
-
97
- [International Energy Agency (IEA)]: https://www.iea.org/
Binary file
package/package.json CHANGED
@@ -1,10 +1,11 @@
1
1
  {
2
2
  "name": "@tgwf/co2",
3
- "version": "0.7.0",
3
+ "version": "0.9.0",
4
4
  "description": "Work out the co2 of your digital services",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
7
7
  "test": "jest",
8
+ "test:watch": "jest --watch",
8
9
  "lint": "eslint .",
9
10
  "lint:fix": "eslint . --fix",
10
11
  "travis": "npm run lint && jest"
@@ -26,7 +27,7 @@
26
27
  "eslint-config-prettier": "^8.3.0",
27
28
  "eslint-plugin-jest": "^25.2.4",
28
29
  "eslint-plugin-prettier": "^4.0.0",
29
- "jest": "^27.3.1",
30
+ "jest": "^27.5.1",
30
31
  "minimist": "^1.2.5",
31
32
  "nock": "^13.2.1",
32
33
  "np": "^7.6.0",
package/src/1byte.js CHANGED
@@ -1,6 +1,14 @@
1
1
  // Use the 1byte model for now from the Shift Project, and assume a US grid mix figure they use of around 519 g co2 for the time being. It's lower for Europe, and in particular, France, but for v1, we don't include this
2
2
  const CO2_PER_KWH_IN_DC_GREY = 519;
3
3
 
4
+ // this figure is from the IEA's 2018 report for a global average:
5
+ const CO2_PER_KWH_NETWORK_GREY = 475;
6
+
7
+ // TODO - these figures need to be updated, as the figures for green
8
+ // shouldn't really be zero now we know that carbon intensity figures
9
+ // for renewables still usually include the life cycle emissions
10
+ const CO2_PER_KWH_IN_DC_GREEN = 0;
11
+
4
12
  // the 1 byte model gives figures for energy usage for:
5
13
 
6
14
  // datacentres
@@ -9,19 +17,55 @@ const CO2_PER_KWH_IN_DC_GREY = 519;
9
17
 
10
18
  // The device usage figure combines figures for:
11
19
  // 1. the usage for devices (which is small proportion of the energy use)
12
- // 2. the *making* the device, which is comparitively high.
20
+ // 2. the *making* the device, which is comparatively high.
13
21
 
14
- const KWH_PER_BYTE_IN_DC = 0.00000000072;
22
+ const KWH_PER_BYTE_IN_DC = 7.2e-11;
15
23
 
16
24
  // this is probably best left as something users can define, or
17
25
  // a weighted average based on total usage.
26
+ // Using a simple mean for now, as while web traffic to end users might trend
27
+ // towards wifi and mobile,
28
+ // Web traffic between servers is likely wired networks
29
+
30
+ const FIXED_NETWORK_WIRED = 4.29e-10;
31
+ const FIXED_NETWORK_WIFI = 1.52e-10;
32
+ const FOUR_G_MOBILE = 8.84e-10;
33
+
18
34
  // Pull requests gratefully accepted
19
- const KWH_PER_BYTE_FOR_NETWORK = 0.00000000488;
35
+ const KWH_PER_BYTE_FOR_NETWORK =
36
+ (FIXED_NETWORK_WIRED + FIXED_NETWORK_WIFI + FOUR_G_MOBILE) / 3;
37
+
38
+ const KWH_PER_BYTE_FOR_DEVICES = 1.3e-10;
39
+
40
+ class OneByte {
41
+ constructor(options) {
42
+ this.options = options;
43
+
44
+ this.KWH_PER_BYTE_FOR_NETWORK = KWH_PER_BYTE_FOR_NETWORK;
45
+ }
46
+
47
+ perByte(bytes, green) {
48
+ if (bytes < 1) {
49
+ return 0;
50
+ }
51
+
52
+ if (green) {
53
+ // if we have a green datacentre, use the lower figure for renewable energy
54
+ const Co2ForDC = bytes * KWH_PER_BYTE_IN_DC * CO2_PER_KWH_IN_DC_GREEN;
55
+
56
+ // but for the worest of the internet, we can't easily check, so assume
57
+ // grey for now
58
+ const Co2forNetwork =
59
+ bytes * KWH_PER_BYTE_FOR_NETWORK * CO2_PER_KWH_NETWORK_GREY;
60
+
61
+ return Co2ForDC + Co2forNetwork;
62
+ }
63
+
64
+ const KwHPerByte = KWH_PER_BYTE_IN_DC + KWH_PER_BYTE_FOR_NETWORK;
65
+ return bytes * KwHPerByte * CO2_PER_KWH_IN_DC_GREY;
66
+ }
67
+ }
20
68
 
21
- const KWH_PER_BYTE_FOR_DEVICES = 0.00000000013;
22
69
  module.exports = {
23
- KWH_PER_BYTE_IN_DC,
24
- KWH_PER_BYTE_FOR_NETWORK,
25
- KWH_PER_BYTE_FOR_DEVICES,
26
- CO2_PER_KWH_IN_DC_GREY,
70
+ OneByte,
27
71
  };
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+
3
+ const oneByte = require("./1byte");
4
+
5
+ describe("onebyte", () => {
6
+ describe("perByte", () => {
7
+ it.only("returns a simple average of the different networks", () => {
8
+ // we limit this to 12 figures with toFixed(12), because
9
+ // we have a recurring 333333 afterwards
10
+ // 4.88e-10 is the same as 0.000000000488
11
+ const expected_val = (0.000000000488).toFixed(12);
12
+ const instance = new oneByte.OneByte();
13
+
14
+ expect(instance.KWH_PER_BYTE_FOR_NETWORK.toFixed(12)).toBe(expected_val);
15
+ });
16
+ });
17
+ });
package/src/co2.js CHANGED
@@ -1,48 +1,33 @@
1
1
  "use strict";
2
2
 
3
3
  const url = require("url");
4
- const oneByte = require("./1byte.js");
5
-
6
- const KWH_PER_BYTE_IN_DC = oneByte.KWH_PER_BYTE_IN_DC;
7
- const KWH_PER_BYTE_FOR_NETWORK = oneByte.KWH_PER_BYTE_FOR_NETWORK;
8
- const CO2_PER_KWH_IN_DC_GREY = oneByte.CO2_PER_KWH_IN_DC_GREY;
9
-
10
- // this figure is from the IEA's 2018 report for a global average:
11
- const CO2_PER_KWH_NETWORK_GREY = 475;
12
-
13
- // The IEA figures cover electricity but as far as I can tell, it does not
14
- // cover life cycle emissions, and the 1byte models appears to do the same
15
- // so, we use zero emissions for green infra in the DC
16
- // https://github.com/thegreenwebfoundation/co2.js/issues/2
17
- const CO2_PER_KWH_IN_DC_GREEN = 0;
4
+ const onebyte = require("./1byte.js");
18
5
 
19
6
  class CO2 {
20
7
  constructor(options) {
21
8
  this.options = options;
22
- }
23
9
 
24
- perByte(bytes, green) {
25
- // return a CO2 figure for energy used to shift the corresponding
26
- // the data transfer.
10
+ // default model
11
+ this.model = new onebyte.OneByte();
27
12
 
28
- if (bytes < 1) {
29
- return 0;
30
- }
31
-
32
- if (green) {
33
- // if we have a green datacentre, use the lower figure for renewable energy
34
- const Co2ForDC = bytes * KWH_PER_BYTE_IN_DC * CO2_PER_KWH_IN_DC_GREEN;
35
-
36
- // but for the rest of the internet, we can't easily check, so assume
37
- // grey for now
38
- const Co2forNetwork =
39
- bytes * KWH_PER_BYTE_FOR_NETWORK * CO2_PER_KWH_NETWORK_GREY;
40
-
41
- return Co2ForDC + Co2forNetwork;
13
+ if (options) {
14
+ this.model = new options.model();
42
15
  }
16
+ }
43
17
 
44
- const KwHPerByte = KWH_PER_BYTE_IN_DC + KWH_PER_BYTE_FOR_NETWORK;
45
- return bytes * KwHPerByte * CO2_PER_KWH_IN_DC_GREY;
18
+ //
19
+ //
20
+ /**
21
+ * Accept a figure in bytes for data transfer, and a boolean for whether
22
+ * the domain shows as 'green', and return a CO2 figure for energy used to shift the corresponding
23
+ * the data transfer.
24
+ *
25
+ * @param {number} bytes
26
+ * @param {boolean} green
27
+ * @return {number} the amount of CO2 in grammes
28
+ */
29
+ perByte(bytes, green) {
30
+ return this.model.perByte(bytes, green);
46
31
  }
47
32
 
48
33
  perDomain(pageXray, greenDomains) {