@tgwf/co2 0.4.7 → 0.7.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/.all-contributorsrc +27 -2
- package/.eslintrc.json +21 -0
- package/.github/workflows/unittests.yml +26 -0
- package/CHANGELOG.md +46 -0
- package/README.md +63 -11
- package/data/fixtures/url2green.test.db +0 -0
- package/data/fixtures/url2green.test.json +1 -0
- package/package.json +20 -6
- package/src/1byte.js +3 -4
- package/src/co2.js +124 -133
- package/src/co2.test.js +130 -131
- package/src/green-byte.js +2 -4
- package/src/{hostingAPI.js → hosting-api.js} +14 -21
- package/src/hosting-api.test.js +37 -0
- package/src/hosting-database.test.js +31 -0
- package/src/hosting-json.js +55 -0
- package/src/hosting-json.test.js +31 -0
- package/src/hosting.js +13 -12
- package/src/hosting.test.js +56 -55
- package/src/index.js +4 -4
- package/.travis.yml +0 -11
- package/src/hostingAPI.test.js +0 -19
- package/src/hostingDatabase.js +0 -80
- package/src/hostingDatabase.test.js +0 -20
- package/url2green.test.db +0 -0
package/.all-contributorsrc
CHANGED
|
@@ -5,10 +5,35 @@
|
|
|
5
5
|
"imageSize": 100,
|
|
6
6
|
"commit": false,
|
|
7
7
|
"contributors": [
|
|
8
|
+
{
|
|
9
|
+
"login": "JamieBeevor",
|
|
10
|
+
"name": "JamieB",
|
|
11
|
+
"avatar_url": "https://avatars1.githubusercontent.com/u/26165947?v=4",
|
|
12
|
+
"profile": "https://greengumption.co.uk",
|
|
13
|
+
"contributions": [
|
|
14
|
+
"ideas",
|
|
15
|
+
"question",
|
|
16
|
+
"review"
|
|
17
|
+
]
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
"login": "soulgalore",
|
|
21
|
+
"name": "Peter Hedenskog",
|
|
22
|
+
"avatar_url": "https://avatars2.githubusercontent.com/u/540757?v=4",
|
|
23
|
+
"profile": "https://www.peterhedenskog.com",
|
|
24
|
+
"contributions": [
|
|
25
|
+
"code",
|
|
26
|
+
"ideas",
|
|
27
|
+
"review",
|
|
28
|
+
"infra",
|
|
29
|
+
"docs"
|
|
30
|
+
]
|
|
31
|
+
}
|
|
8
32
|
],
|
|
9
33
|
"contributorsPerLine": 7,
|
|
10
|
-
"projectName": "co2",
|
|
34
|
+
"projectName": "co2.js",
|
|
11
35
|
"projectOwner": "thegreenwebfoundation",
|
|
12
36
|
"repoType": "github",
|
|
13
|
-
"repoHost": "https://github.com"
|
|
37
|
+
"repoHost": "https://github.com",
|
|
38
|
+
"skipCi": true
|
|
14
39
|
}
|
package/.eslintrc.json
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"root": true,
|
|
3
|
+
"env": {
|
|
4
|
+
"node": true,
|
|
5
|
+
"es6": true,
|
|
6
|
+
"jest/globals": true
|
|
7
|
+
},
|
|
8
|
+
"parserOptions": {
|
|
9
|
+
"ecmaVersion": 8
|
|
10
|
+
},
|
|
11
|
+
"plugins": ["prettier", "jest"],
|
|
12
|
+
"extends": "eslint:recommended",
|
|
13
|
+
"rules": {
|
|
14
|
+
"prettier/prettier": [
|
|
15
|
+
"error",
|
|
16
|
+
{
|
|
17
|
+
}
|
|
18
|
+
],
|
|
19
|
+
"no-unused-vars": "off"
|
|
20
|
+
}
|
|
21
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
name: Unit tests
|
|
2
|
+
on:
|
|
3
|
+
push:
|
|
4
|
+
branches:
|
|
5
|
+
- main
|
|
6
|
+
pull_request:
|
|
7
|
+
branches:
|
|
8
|
+
- main
|
|
9
|
+
jobs:
|
|
10
|
+
build:
|
|
11
|
+
runs-on: ubuntu-latest
|
|
12
|
+
strategy:
|
|
13
|
+
matrix:
|
|
14
|
+
node-version: [12.x, 14.x, 16.x]
|
|
15
|
+
steps:
|
|
16
|
+
- uses: actions/checkout@v2
|
|
17
|
+
- name: Use Node.js ${{ matrix.node-version }}
|
|
18
|
+
uses: actions/setup-node@v1
|
|
19
|
+
with:
|
|
20
|
+
node-version: ${{ matrix.node-version }}
|
|
21
|
+
- name: Install
|
|
22
|
+
run: npm ci
|
|
23
|
+
- name: Verify lint
|
|
24
|
+
run: npm run lint
|
|
25
|
+
- name: Run unit tests
|
|
26
|
+
run: npm test
|
package/CHANGELOG.md
CHANGED
|
@@ -4,6 +4,52 @@ All notable changes to this project will be documented in this file.
|
|
|
4
4
|
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
|
+
-
|
|
8
|
+
|
|
9
|
+
## Unreleased
|
|
10
|
+
|
|
11
|
+
- Include a new alternative, "Green Byte Model" with the figures after speaking to folks at the IEA and other places.
|
|
12
|
+
## [0.7.0] - 2021-11-28
|
|
13
|
+
|
|
14
|
+
### Fixed
|
|
15
|
+
|
|
16
|
+
- Update tests to avoid network requests #50
|
|
17
|
+
- Update dependencies across the board
|
|
18
|
+
|
|
19
|
+
### Changed
|
|
20
|
+
|
|
21
|
+
- Switch to github actions instead of travis for CI.
|
|
22
|
+
|
|
23
|
+
## [0.6.1] - 2020-03-15
|
|
24
|
+
|
|
25
|
+
### Fixed
|
|
26
|
+
|
|
27
|
+
- Added the function to load JSON, on the tgwg.hosting object, for use in the sustaiable web sitespeed plugin.
|
|
28
|
+
|
|
29
|
+
## [0.6.0] - 2020-03-15
|
|
30
|
+
|
|
31
|
+
### Added
|
|
32
|
+
|
|
33
|
+
- Added the hosting-JSON for running local checks against an array instead of SQLite.
|
|
34
|
+
|
|
35
|
+
### Changed
|
|
36
|
+
|
|
37
|
+
- Swapped out checking against a sqlite database `hosting-json`in favour of simple array in,
|
|
38
|
+
- Updated conventions for style - using kebab-cases over CamelCase for naming files
|
|
39
|
+
|
|
40
|
+
### Removed
|
|
41
|
+
|
|
42
|
+
- Extracted sqlite usage and dependencies into a separate module, `url2green`. This means you no longer need to compile SQLite on install.
|
|
43
|
+
|
|
44
|
+
## [0.5.0] - 2020-03-03
|
|
45
|
+
|
|
46
|
+
### Changed
|
|
47
|
+
|
|
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
|
|
51
|
+
- Credit contributors
|
|
52
|
+
|
|
7
53
|
|
|
8
54
|
## [0.4.7] - 2020-03-02
|
|
9
55
|
|
package/README.md
CHANGED
|
@@ -1,45 +1,97 @@
|
|
|
1
1
|
# CO2
|
|
2
2
|
|
|
3
|
-
<img src="https://
|
|
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
7
|
|
|
7
|
-
|
|
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
|
+
|
|
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.
|
|
8
11
|
|
|
9
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.
|
|
10
13
|
|
|
11
|
-
It is currently used in the web performance tool [sitespeed.io], to help developers build greener, more planet friendly digital services.
|
|
14
|
+
It is currently used in the web performance tool [sitespeed.io][], to help developers build greener, more planet friendly digital services.
|
|
15
|
+
|
|
16
|
+
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
|
+
|
|
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].
|
|
12
19
|
|
|
13
20
|
[soberDigital]: https://theshiftproject.org/en/lean-ict-2/
|
|
14
|
-
[
|
|
21
|
+
[sitespeed.io]: https://sitespeed.io/
|
|
15
22
|
[tgwf]: https://www.thegreenwebfoundation.org/
|
|
23
|
+
[tgwf-services]: https://www.thegreenwebfoundation.org/services/
|
|
16
24
|
|
|
17
25
|
|
|
18
26
|
## Usage
|
|
19
27
|
|
|
28
|
+
### Calculating emissions per byte
|
|
29
|
+
|
|
30
|
+
#### Server-side
|
|
31
|
+
|
|
32
|
+
This approach relies on the `fs` module and so can only be used on platforms, like Node.js, that support this.
|
|
33
|
+
|
|
20
34
|
```js
|
|
21
35
|
|
|
22
|
-
const
|
|
23
|
-
const bytesSent =
|
|
36
|
+
const CO2 = require('@tgwf/co2')
|
|
37
|
+
const bytesSent = (1024 * 1024 * 1024)
|
|
38
|
+
const co2Emission = new CO2();
|
|
39
|
+
estimatedCO2 = co2Emission.perByte(bytesSent)
|
|
40
|
+
|
|
41
|
+
console.log(`Sending a gigabyte, had a carbon footprint of ${estimatedCO2.toFixed(3)} grams of CO2`)
|
|
42
|
+
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
#### Browser-side
|
|
46
|
+
|
|
47
|
+
For browser-based solutions, you must import the `co2.js` submodule directly from `node_modules`. For example, like this:
|
|
24
48
|
|
|
25
|
-
|
|
49
|
+
```js
|
|
50
|
+
|
|
51
|
+
const CO2 = require('node_modules/@tgwf/co2/src/co2.js')
|
|
52
|
+
const bytesSent = (1024 * 1024 * 1024)
|
|
53
|
+
const co2Emission = new CO2();
|
|
54
|
+
estimatedCO2 = co2Emission.perByte(bytesSent)
|
|
26
55
|
|
|
27
|
-
console.log(`Sending a
|
|
56
|
+
console.log(`Sending a gigabyte, had a carbon footprint of ${estimatedCO2.toFixed(3)} grams of CO2`)
|
|
28
57
|
|
|
29
58
|
```
|
|
30
59
|
|
|
60
|
+
### Checking for green power
|
|
61
|
+
|
|
31
62
|
Because different digital services and websites use different forms of power, there is also a module for checking if a domain uses green power or not, and whether the domains linked to on a page use green power as well.
|
|
32
63
|
|
|
33
64
|
```js
|
|
34
65
|
|
|
35
66
|
const greencheck = require('@tgwf/hosting')
|
|
36
67
|
|
|
37
|
-
|
|
38
|
-
greencheck.check(
|
|
39
|
-
|
|
68
|
+
// returns true if green, otherwise false
|
|
69
|
+
greencheck.check("google.com")
|
|
70
|
+
|
|
71
|
+
// returns an array of the green domains, in this case ["google.com"].
|
|
72
|
+
greencheck.check(["google.com", "kochindustries.com"])]
|
|
73
|
+
|
|
74
|
+
// returns an array of green domains, again in this case, ["google.com"]
|
|
75
|
+
greencheck.checkPage(["google.com"])
|
|
40
76
|
|
|
41
77
|
```
|
|
42
78
|
|
|
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
|
+
|
|
43
93
|
# Licenses
|
|
44
94
|
|
|
45
95
|
Apache 2.0
|
|
96
|
+
|
|
97
|
+
[International Energy Agency (IEA)]: https://www.iea.org/
|
|
Binary file
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
["google.com","maxcdn.bootstrapcdn.com","thegreenwebfoundation.org","www.thegreenwebfoundation.org","fonts.googleapis.com","ajax.googleapis.com","assets.digitalclimatestrike.net","cdnjs.cloudflare.com","graphite.thegreenwebfoundation.org","analytics.thegreenwebfoundation.org","fonts.gstatic.com","api.thegreenwebfoundation.org"]
|
package/package.json
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tgwf/co2",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.0",
|
|
4
4
|
"description": "Work out the co2 of your digital services",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"scripts": {
|
|
7
|
-
"test": "jest"
|
|
7
|
+
"test": "jest",
|
|
8
|
+
"lint": "eslint .",
|
|
9
|
+
"lint:fix": "eslint . --fix",
|
|
10
|
+
"travis": "npm run lint && jest"
|
|
8
11
|
},
|
|
9
12
|
"keywords": [
|
|
10
13
|
"sustainability",
|
|
@@ -18,9 +21,17 @@
|
|
|
18
21
|
"author": "Chris Adams",
|
|
19
22
|
"license": "Apache-2.0",
|
|
20
23
|
"devDependencies": {
|
|
21
|
-
"
|
|
22
|
-
"
|
|
23
|
-
"
|
|
24
|
+
"@tgwf/url2green": "^0.4.0",
|
|
25
|
+
"eslint": "^8.3.0",
|
|
26
|
+
"eslint-config-prettier": "^8.3.0",
|
|
27
|
+
"eslint-plugin-jest": "^25.2.4",
|
|
28
|
+
"eslint-plugin-prettier": "^4.0.0",
|
|
29
|
+
"jest": "^27.3.1",
|
|
30
|
+
"minimist": "^1.2.5",
|
|
31
|
+
"nock": "^13.2.1",
|
|
32
|
+
"np": "^7.6.0",
|
|
33
|
+
"pagexray": "^4.3.1",
|
|
34
|
+
"prettier": "^2.4.1"
|
|
24
35
|
},
|
|
25
36
|
"np": {
|
|
26
37
|
"yarn": false
|
|
@@ -29,7 +40,10 @@
|
|
|
29
40
|
"access": "public"
|
|
30
41
|
},
|
|
31
42
|
"dependencies": {
|
|
32
|
-
"better-sqlite3": "^5.4.3",
|
|
33
43
|
"debug": "^4.1.1"
|
|
44
|
+
},
|
|
45
|
+
"repository": {
|
|
46
|
+
"type": "git",
|
|
47
|
+
"url": "https://github.com/thegreenwebfoundation/co2.js.git"
|
|
34
48
|
}
|
|
35
49
|
}
|
package/src/1byte.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
|
|
2
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
|
|
3
2
|
const CO2_PER_KWH_IN_DC_GREY = 519;
|
|
4
3
|
|
|
@@ -19,10 +18,10 @@ const KWH_PER_BYTE_IN_DC = 0.00000000072;
|
|
|
19
18
|
// Pull requests gratefully accepted
|
|
20
19
|
const KWH_PER_BYTE_FOR_NETWORK = 0.00000000488;
|
|
21
20
|
|
|
22
|
-
const KWH_PER_BYTE_FOR_DEVICES = 0.00000000013
|
|
21
|
+
const KWH_PER_BYTE_FOR_DEVICES = 0.00000000013;
|
|
23
22
|
module.exports = {
|
|
24
23
|
KWH_PER_BYTE_IN_DC,
|
|
25
24
|
KWH_PER_BYTE_FOR_NETWORK,
|
|
26
25
|
KWH_PER_BYTE_FOR_DEVICES,
|
|
27
|
-
CO2_PER_KWH_IN_DC_GREY
|
|
28
|
-
}
|
|
26
|
+
CO2_PER_KWH_IN_DC_GREY,
|
|
27
|
+
};
|
package/src/co2.js
CHANGED
|
@@ -1,166 +1,157 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
2
|
|
|
3
|
-
const url = require(
|
|
4
|
-
const oneByte = require(
|
|
3
|
+
const url = require("url");
|
|
4
|
+
const oneByte = require("./1byte.js");
|
|
5
5
|
|
|
6
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
|
|
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
9
|
|
|
10
10
|
// this figure is from the IEA's 2018 report for a global average:
|
|
11
|
-
const CO2_PER_KWH_NETWORK_GREY =
|
|
11
|
+
const CO2_PER_KWH_NETWORK_GREY = 475;
|
|
12
12
|
|
|
13
|
-
//
|
|
14
|
-
//
|
|
15
|
-
//
|
|
16
|
-
//
|
|
17
|
-
|
|
18
|
-
// for RE.
|
|
19
|
-
// More here:
|
|
20
|
-
// https://www.ecotricity.co.uk/layout/set/popup/layout/set/print/for-your-home/britain-s-greenest-energy-company/lifecycle-carbon-emissions
|
|
21
|
-
// https://twitter.com/mrchrisadams/status/1227972969756086284
|
|
22
|
-
// https://en.wikipedia.org/wiki/Life-cycle_greenhouse-gas_emissions_of_energy_sources
|
|
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;
|
|
23
18
|
|
|
24
|
-
|
|
19
|
+
class CO2 {
|
|
20
|
+
constructor(options) {
|
|
21
|
+
this.options = options;
|
|
22
|
+
}
|
|
25
23
|
|
|
24
|
+
perByte(bytes, green) {
|
|
25
|
+
// return a CO2 figure for energy used to shift the corresponding
|
|
26
|
+
// the data transfer.
|
|
26
27
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
28
|
+
if (bytes < 1) {
|
|
29
|
+
return 0;
|
|
30
|
+
}
|
|
30
31
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
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;
|
|
34
35
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
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;
|
|
38
40
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
const Co2forNetwork =
|
|
42
|
-
bytes * KWH_PER_BYTE_FOR_NETWORK * CO2_PER_KWH_NETWORK_GREY;
|
|
41
|
+
return Co2ForDC + Co2forNetwork;
|
|
42
|
+
}
|
|
43
43
|
|
|
44
|
-
|
|
44
|
+
const KwHPerByte = KWH_PER_BYTE_IN_DC + KWH_PER_BYTE_FOR_NETWORK;
|
|
45
|
+
return bytes * KwHPerByte * CO2_PER_KWH_IN_DC_GREY;
|
|
45
46
|
}
|
|
46
47
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
48
|
+
perDomain(pageXray, greenDomains) {
|
|
49
|
+
const co2PerDomain = [];
|
|
50
|
+
for (let domain of Object.keys(pageXray.domains)) {
|
|
51
|
+
let co2;
|
|
52
|
+
if (greenDomains && greenDomains.indexOf(domain) > -1) {
|
|
53
|
+
co2 = this.perByte(pageXray.domains[domain].transferSize, true);
|
|
54
|
+
} else {
|
|
55
|
+
co2 = this.perByte(pageXray.domains[domain].transferSize);
|
|
56
|
+
}
|
|
57
|
+
co2PerDomain.push({
|
|
58
|
+
domain,
|
|
59
|
+
co2,
|
|
60
|
+
transferSize: pageXray.domains[domain].transferSize,
|
|
61
|
+
});
|
|
59
62
|
}
|
|
60
|
-
co2PerDomain.
|
|
61
|
-
|
|
62
|
-
co2,
|
|
63
|
-
transferSize: pageXray.domains[domain].transferSize
|
|
63
|
+
co2PerDomain.sort(function (a, b) {
|
|
64
|
+
return b.co2 - a.co2;
|
|
64
65
|
});
|
|
65
|
-
}
|
|
66
|
-
co2PerDomain.sort(function (a, b) {
|
|
67
|
-
return b.co2 - a.co2;
|
|
68
|
-
});
|
|
69
66
|
|
|
70
|
-
|
|
71
|
-
}
|
|
67
|
+
return co2PerDomain;
|
|
68
|
+
}
|
|
72
69
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
70
|
+
perPage(pageXray, green) {
|
|
71
|
+
// Accept an xray object, and if we receive a boolean as the second
|
|
72
|
+
// argument, we assume every request we make is sent to a server
|
|
73
|
+
// running on renwewable power.
|
|
77
74
|
|
|
78
|
-
|
|
79
|
-
|
|
75
|
+
// if we receive an array of domains, return a number accounting the
|
|
76
|
+
// reduced CO2 from green hosted domains
|
|
80
77
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
78
|
+
const domainCO2 = this.perDomain(pageXray, green);
|
|
79
|
+
let totalCO2 = 0;
|
|
80
|
+
for (let domain of domainCO2) {
|
|
81
|
+
totalCO2 += domain.co2;
|
|
82
|
+
}
|
|
83
|
+
return totalCO2;
|
|
85
84
|
}
|
|
86
|
-
return totalCO2;
|
|
87
|
-
}
|
|
88
85
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
86
|
+
perContentType(pageXray, greenDomains) {
|
|
87
|
+
const co2PerContentType = {};
|
|
88
|
+
for (let asset of pageXray.assets) {
|
|
89
|
+
const domain = url.parse(asset.url).domain;
|
|
90
|
+
const transferSize = asset.transferSize;
|
|
91
|
+
const co2ForTransfer = this.perByte(
|
|
92
|
+
transferSize,
|
|
93
|
+
greenDomains && greenDomains.indexOf(domain) > -1
|
|
94
|
+
);
|
|
95
|
+
const contentType = asset.type;
|
|
96
|
+
if (!co2PerContentType[contentType]) {
|
|
97
|
+
co2PerContentType[contentType] = { co2: 0, transferSize: 0 };
|
|
98
|
+
}
|
|
99
|
+
co2PerContentType[contentType].co2 += co2ForTransfer;
|
|
100
|
+
co2PerContentType[contentType].transferSize += transferSize;
|
|
101
101
|
}
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
102
|
+
// restructure and sort
|
|
103
|
+
const all = [];
|
|
104
|
+
for (let type of Object.keys(co2PerContentType)) {
|
|
105
|
+
all.push({
|
|
106
|
+
type,
|
|
107
|
+
co2: co2PerContentType[type].co2,
|
|
108
|
+
transferSize: co2PerContentType[type].transferSize,
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
all.sort(function (a, b) {
|
|
112
|
+
return b.co2 - a.co2;
|
|
112
113
|
});
|
|
114
|
+
return all;
|
|
113
115
|
}
|
|
114
|
-
all.sort(function (a, b) {
|
|
115
|
-
return b.co2 - a.co2;
|
|
116
|
-
});
|
|
117
|
-
return all;
|
|
118
|
-
}
|
|
119
116
|
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
117
|
+
dirtiestResources(pageXray, greenDomains) {
|
|
118
|
+
const allAssets = [];
|
|
119
|
+
for (let asset of pageXray.assets) {
|
|
120
|
+
const domain = url.parse(asset.url).domain;
|
|
121
|
+
const transferSize = asset.transferSize;
|
|
122
|
+
const co2ForTransfer = this.perByte(
|
|
123
|
+
transferSize,
|
|
124
|
+
greenDomains && greenDomains.indexOf(domain) > -1
|
|
125
|
+
);
|
|
126
|
+
allAssets.push({ url: asset.url, co2: co2ForTransfer, transferSize });
|
|
127
|
+
}
|
|
128
|
+
allAssets.sort(function (a, b) {
|
|
129
|
+
return b.co2 - a.co2;
|
|
130
|
+
});
|
|
134
131
|
|
|
135
|
-
|
|
136
|
-
}
|
|
132
|
+
return allAssets.slice(0, allAssets.length > 10 ? 10 : allAssets.length);
|
|
133
|
+
}
|
|
137
134
|
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
135
|
+
perParty(pageXray, greenDomains) {
|
|
136
|
+
let firstParty = 0;
|
|
137
|
+
let thirdParty = 0;
|
|
138
|
+
// calculate co2 per first/third party
|
|
139
|
+
const firstPartyRegEx = pageXray.firstPartyRegEx;
|
|
140
|
+
for (let d of Object.keys(pageXray.domains)) {
|
|
141
|
+
if (!d.match(firstPartyRegEx)) {
|
|
142
|
+
thirdParty += this.perByte(
|
|
143
|
+
pageXray.domains[d].transferSize,
|
|
144
|
+
greenDomains && greenDomains.indexOf(d) > -1
|
|
145
|
+
);
|
|
146
|
+
} else {
|
|
147
|
+
firstParty += this.perByte(
|
|
148
|
+
pageXray.domains[d].transferSize,
|
|
149
|
+
greenDomains && greenDomains.indexOf(d) > -1
|
|
150
|
+
);
|
|
151
|
+
}
|
|
154
152
|
}
|
|
153
|
+
return { firstParty, thirdParty };
|
|
155
154
|
}
|
|
156
|
-
return { firstParty, thirdParty };
|
|
157
155
|
}
|
|
158
156
|
|
|
159
|
-
module.exports =
|
|
160
|
-
perByte: getCO2PerByte,
|
|
161
|
-
perDomain: getCO2PerDomain,
|
|
162
|
-
perPage: getCO2perPage,
|
|
163
|
-
perParty: getCO2PerParty,
|
|
164
|
-
perContentType: getCO2PerContentType,
|
|
165
|
-
dirtiestResources
|
|
166
|
-
};
|
|
157
|
+
module.exports = CO2;
|