@tgwf/co2 0.6.1 → 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/.github/workflows/unittests.yml +26 -0
- package/CHANGELOG.md +12 -2
- package/README.md +39 -10
- package/data/fixtures/url2green.test.db +0 -0
- package/data/fixtures/url2green.test.json +1 -1
- package/package.json +12 -10
- package/src/1byte.js +1 -1
- package/src/co2.js +5 -5
- package/src/co2.test.js +16 -16
- package/src/green-byte.js +1 -1
- package/src/hosting-api.js +6 -8
- package/src/hosting-api.test.js +24 -5
- package/src/hosting-database.test.js +5 -5
- package/src/hosting-json.js +3 -3
- package/src/hosting-json.test.js +5 -5
- package/src/hosting.js +5 -5
- package/src/hosting.test.js +31 -20
- package/src/index.js +1 -1
- package/.travis.yml +0 -12
|
@@ -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
|
@@ -5,12 +5,22 @@ All notable changes to this project will be documented in this file.
|
|
|
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
7
|
-
|
|
8
|
+
|
|
8
9
|
## Unreleased
|
|
9
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
|
|
10
20
|
|
|
11
|
-
-
|
|
21
|
+
- Switch to github actions instead of travis for CI.
|
|
12
22
|
|
|
13
|
-
##
|
|
23
|
+
## [0.6.1] - 2020-03-15
|
|
14
24
|
|
|
15
25
|
### Fixed
|
|
16
26
|
|
package/README.md
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
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
|
-
We
|
|
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_.
|
|
8
9
|
|
|
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.
|
|
10
11
|
|
|
@@ -24,17 +25,40 @@ This is open source software, with all the guarantees associated, so if you want
|
|
|
24
25
|
|
|
25
26
|
## Usage
|
|
26
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
|
+
|
|
27
34
|
```js
|
|
28
35
|
|
|
29
36
|
const CO2 = require('@tgwf/co2')
|
|
30
|
-
const bytesSent =
|
|
37
|
+
const bytesSent = (1024 * 1024 * 1024)
|
|
31
38
|
const co2Emission = new CO2();
|
|
32
39
|
estimatedCO2 = co2Emission.perByte(bytesSent)
|
|
33
40
|
|
|
34
|
-
console.log(`Sending a
|
|
41
|
+
console.log(`Sending a gigabyte, had a carbon footprint of ${estimatedCO2.toFixed(3)} grams of CO2`)
|
|
35
42
|
|
|
36
43
|
```
|
|
37
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:
|
|
48
|
+
|
|
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)
|
|
55
|
+
|
|
56
|
+
console.log(`Sending a gigabyte, had a carbon footprint of ${estimatedCO2.toFixed(3)} grams of CO2`)
|
|
57
|
+
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### Checking for green power
|
|
61
|
+
|
|
38
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.
|
|
39
63
|
|
|
40
64
|
```js
|
|
@@ -44,25 +68,30 @@ const greencheck = require('@tgwf/hosting')
|
|
|
44
68
|
// returns true if green, otherwise false
|
|
45
69
|
greencheck.check("google.com")
|
|
46
70
|
|
|
47
|
-
// returns an array of the green domains, in this case ["google.
|
|
48
|
-
greencheck.check(["google.com", "kochindustries.com"])
|
|
71
|
+
// returns an array of the green domains, in this case ["google.com"].
|
|
72
|
+
greencheck.check(["google.com", "kochindustries.com"])]
|
|
49
73
|
|
|
50
74
|
// returns an array of green domains, again in this case, ["google.com"]
|
|
51
75
|
greencheck.checkPage(["google.com"])
|
|
52
76
|
|
|
53
77
|
```
|
|
54
78
|
|
|
55
|
-
|
|
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:
|
|
56
84
|
|
|
57
|
-
- the carbon associated with digging up the fuel
|
|
58
|
-
- the carbon associated with mining the materials to
|
|
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
|
|
59
87
|
- the end of life costs
|
|
60
88
|
- the maintenance costs over the life of the datacentres, power generation and end user devices, and the rest of the internet
|
|
61
89
|
|
|
62
|
-
|
|
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.
|
|
63
91
|
|
|
64
92
|
|
|
65
93
|
# Licenses
|
|
66
94
|
|
|
67
95
|
Apache 2.0
|
|
68
96
|
|
|
97
|
+
[International Energy Agency (IEA)]: https://www.iea.org/
|
|
Binary file
|
|
@@ -1 +1 @@
|
|
|
1
|
-
["google.com"]
|
|
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,6 +1,6 @@
|
|
|
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": {
|
|
@@ -21,15 +21,17 @@
|
|
|
21
21
|
"author": "Chris Adams",
|
|
22
22
|
"license": "Apache-2.0",
|
|
23
23
|
"devDependencies": {
|
|
24
|
-
"@tgwf/url2green": "^0.
|
|
25
|
-
"eslint": "
|
|
26
|
-
"eslint-config-prettier": "
|
|
27
|
-
"eslint-plugin-jest": "
|
|
28
|
-
"eslint-plugin-prettier": "
|
|
29
|
-
"jest": "
|
|
30
|
-
"
|
|
31
|
-
"
|
|
32
|
-
"
|
|
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"
|
|
33
35
|
},
|
|
34
36
|
"np": {
|
|
35
37
|
"yarn": false
|
package/src/1byte.js
CHANGED
package/src/co2.js
CHANGED
|
@@ -57,10 +57,10 @@ class CO2 {
|
|
|
57
57
|
co2PerDomain.push({
|
|
58
58
|
domain,
|
|
59
59
|
co2,
|
|
60
|
-
transferSize: pageXray.domains[domain].transferSize
|
|
60
|
+
transferSize: pageXray.domains[domain].transferSize,
|
|
61
61
|
});
|
|
62
62
|
}
|
|
63
|
-
co2PerDomain.sort(function(a, b) {
|
|
63
|
+
co2PerDomain.sort(function (a, b) {
|
|
64
64
|
return b.co2 - a.co2;
|
|
65
65
|
});
|
|
66
66
|
|
|
@@ -105,10 +105,10 @@ class CO2 {
|
|
|
105
105
|
all.push({
|
|
106
106
|
type,
|
|
107
107
|
co2: co2PerContentType[type].co2,
|
|
108
|
-
transferSize: co2PerContentType[type].transferSize
|
|
108
|
+
transferSize: co2PerContentType[type].transferSize,
|
|
109
109
|
});
|
|
110
110
|
}
|
|
111
|
-
all.sort(function(a, b) {
|
|
111
|
+
all.sort(function (a, b) {
|
|
112
112
|
return b.co2 - a.co2;
|
|
113
113
|
});
|
|
114
114
|
return all;
|
|
@@ -125,7 +125,7 @@ class CO2 {
|
|
|
125
125
|
);
|
|
126
126
|
allAssets.push({ url: asset.url, co2: co2ForTransfer, transferSize });
|
|
127
127
|
}
|
|
128
|
-
allAssets.sort(function(a, b) {
|
|
128
|
+
allAssets.sort(function (a, b) {
|
|
129
129
|
return b.co2 - a.co2;
|
|
130
130
|
});
|
|
131
131
|
|
package/src/co2.test.js
CHANGED
|
@@ -6,7 +6,7 @@ const path = require("path");
|
|
|
6
6
|
const CO2 = require("./co2");
|
|
7
7
|
const pagexray = require("pagexray");
|
|
8
8
|
|
|
9
|
-
describe("co2", function() {
|
|
9
|
+
describe("co2", function () {
|
|
10
10
|
let har, co2;
|
|
11
11
|
const TGWF_GREY_VALUE = 2.0484539712;
|
|
12
12
|
const TGWF_GREEN_VALUE = 0.54704300112;
|
|
@@ -16,7 +16,7 @@ describe("co2", function() {
|
|
|
16
16
|
const MILLION_GREY = 2.9064;
|
|
17
17
|
const MILLION_GREEN = 2.318;
|
|
18
18
|
|
|
19
|
-
beforeEach(function() {
|
|
19
|
+
beforeEach(function () {
|
|
20
20
|
co2 = new CO2();
|
|
21
21
|
har = JSON.parse(
|
|
22
22
|
fs.readFileSync(
|
|
@@ -26,25 +26,25 @@ describe("co2", function() {
|
|
|
26
26
|
);
|
|
27
27
|
});
|
|
28
28
|
|
|
29
|
-
describe("perByte", function() {
|
|
30
|
-
it("returns a CO2 number for data transfer using 'grey' power", function() {
|
|
29
|
+
describe("perByte", function () {
|
|
30
|
+
it("returns a CO2 number for data transfer using 'grey' power", function () {
|
|
31
31
|
expect(co2.perByte(MILLION)).toBe(MILLION_GREY);
|
|
32
32
|
});
|
|
33
33
|
|
|
34
|
-
it("returns a lower CO2 number for data transfer from domains using entirely 'green' power", function() {
|
|
34
|
+
it("returns a lower CO2 number for data transfer from domains using entirely 'green' power", function () {
|
|
35
35
|
expect(co2.perByte(MILLION, false)).toBe(MILLION_GREY);
|
|
36
36
|
expect(co2.perByte(MILLION, true)).toBe(MILLION_GREEN);
|
|
37
37
|
});
|
|
38
38
|
});
|
|
39
39
|
|
|
40
|
-
describe("perPage", function() {
|
|
41
|
-
it("returns CO2 for total transfer for page", function() {
|
|
40
|
+
describe("perPage", function () {
|
|
41
|
+
it("returns CO2 for total transfer for page", function () {
|
|
42
42
|
const pages = pagexray.convert(har);
|
|
43
43
|
const pageXrayRun = pages[0];
|
|
44
44
|
|
|
45
45
|
expect(co2.perPage(pageXrayRun)).toBe(TGWF_GREY_VALUE);
|
|
46
46
|
});
|
|
47
|
-
it("returns lower CO2 for page served from green site", function() {
|
|
47
|
+
it("returns lower CO2 for page served from green site", function () {
|
|
48
48
|
const pages = pagexray.convert(har);
|
|
49
49
|
const pageXrayRun = pages[0];
|
|
50
50
|
let green = [
|
|
@@ -56,11 +56,11 @@ describe("co2", function() {
|
|
|
56
56
|
"graphite.thegreenwebfoundation.org",
|
|
57
57
|
"analytics.thegreenwebfoundation.org",
|
|
58
58
|
"fonts.gstatic.com",
|
|
59
|
-
"api.thegreenwebfoundation.org"
|
|
59
|
+
"api.thegreenwebfoundation.org",
|
|
60
60
|
];
|
|
61
61
|
expect(co2.perPage(pageXrayRun, green)).toBeLessThan(TGWF_GREY_VALUE);
|
|
62
62
|
});
|
|
63
|
-
it("returns a lower CO2 number where *some* domains use green power", function() {
|
|
63
|
+
it("returns a lower CO2 number where *some* domains use green power", function () {
|
|
64
64
|
const pages = pagexray.convert(har);
|
|
65
65
|
const pageXrayRun = pages[0];
|
|
66
66
|
// green can be true, or a array containing entries
|
|
@@ -73,13 +73,13 @@ describe("co2", function() {
|
|
|
73
73
|
"graphite.thegreenwebfoundation.org",
|
|
74
74
|
"analytics.thegreenwebfoundation.org",
|
|
75
75
|
"fonts.gstatic.com",
|
|
76
|
-
"api.thegreenwebfoundation.org"
|
|
76
|
+
"api.thegreenwebfoundation.org",
|
|
77
77
|
];
|
|
78
78
|
expect(co2.perPage(pageXrayRun, green)).toBe(TGWF_MIXED_VALUE);
|
|
79
79
|
});
|
|
80
80
|
});
|
|
81
|
-
describe("perDomain", function() {
|
|
82
|
-
it("shows object listing Co2 for each domain", function() {
|
|
81
|
+
describe("perDomain", function () {
|
|
82
|
+
it("shows object listing Co2 for each domain", function () {
|
|
83
83
|
const pages = pagexray.convert(har);
|
|
84
84
|
const pageXrayRun = pages[0];
|
|
85
85
|
const res = co2.perDomain(pageXrayRun);
|
|
@@ -95,7 +95,7 @@ describe("co2", function() {
|
|
|
95
95
|
"graphite.thegreenwebfoundation.org",
|
|
96
96
|
"analytics.thegreenwebfoundation.org",
|
|
97
97
|
"fonts.gstatic.com",
|
|
98
|
-
"api.thegreenwebfoundation.org"
|
|
98
|
+
"api.thegreenwebfoundation.org",
|
|
99
99
|
];
|
|
100
100
|
|
|
101
101
|
for (let obj of res) {
|
|
@@ -103,7 +103,7 @@ describe("co2", function() {
|
|
|
103
103
|
expect(typeof obj.co2).toBe("number");
|
|
104
104
|
}
|
|
105
105
|
});
|
|
106
|
-
it("shows lower Co2 for green domains", function() {
|
|
106
|
+
it("shows lower Co2 for green domains", function () {
|
|
107
107
|
const pages = pagexray.convert(har);
|
|
108
108
|
const pageXrayRun = pages[0];
|
|
109
109
|
|
|
@@ -116,7 +116,7 @@ describe("co2", function() {
|
|
|
116
116
|
"graphite.thegreenwebfoundation.org",
|
|
117
117
|
"analytics.thegreenwebfoundation.org",
|
|
118
118
|
"fonts.gstatic.com",
|
|
119
|
-
"api.thegreenwebfoundation.org"
|
|
119
|
+
"api.thegreenwebfoundation.org",
|
|
120
120
|
];
|
|
121
121
|
const res = co2.perDomain(pageXrayRun);
|
|
122
122
|
const resWithGreen = co2.perDomain(pageXrayRun, greenDomains);
|
package/src/green-byte.js
CHANGED
package/src/hosting-api.js
CHANGED
|
@@ -28,7 +28,6 @@ async function checkDomainsAgainstAPI(domains) {
|
|
|
28
28
|
)}`
|
|
29
29
|
)
|
|
30
30
|
);
|
|
31
|
-
|
|
32
31
|
return greenDomainsFromResults(allGreenCheckResults);
|
|
33
32
|
} catch (e) {
|
|
34
33
|
return [];
|
|
@@ -37,20 +36,19 @@ async function checkDomainsAgainstAPI(domains) {
|
|
|
37
36
|
|
|
38
37
|
function greenDomainsFromResults(greenResults) {
|
|
39
38
|
const entries = Object.entries(greenResults);
|
|
40
|
-
let greenEntries = entries.filter(function([key, val]) {
|
|
39
|
+
let greenEntries = entries.filter(function ([key, val]) {
|
|
41
40
|
return val.green;
|
|
42
41
|
});
|
|
43
|
-
|
|
44
|
-
return greenEntries.map(function([key, val]) {
|
|
42
|
+
return greenEntries.map(function ([key, val]) {
|
|
45
43
|
return val.url;
|
|
46
44
|
});
|
|
47
45
|
}
|
|
48
46
|
|
|
49
47
|
async function getBody(url) {
|
|
50
48
|
// Return new promise
|
|
51
|
-
return new Promise(function(resolve, reject) {
|
|
49
|
+
return new Promise(function (resolve, reject) {
|
|
52
50
|
// Do async job
|
|
53
|
-
const req = https.get(url, function(res) {
|
|
51
|
+
const req = https.get(url, function (res) {
|
|
54
52
|
if (res.statusCode < 200 || res.statusCode >= 300) {
|
|
55
53
|
log(
|
|
56
54
|
"Could not get info from the Green Web Foundation API, %s for %s",
|
|
@@ -61,7 +59,7 @@ async function getBody(url) {
|
|
|
61
59
|
}
|
|
62
60
|
const data = [];
|
|
63
61
|
|
|
64
|
-
res.on("data", chunk => {
|
|
62
|
+
res.on("data", (chunk) => {
|
|
65
63
|
data.push(chunk);
|
|
66
64
|
});
|
|
67
65
|
|
|
@@ -72,5 +70,5 @@ async function getBody(url) {
|
|
|
72
70
|
}
|
|
73
71
|
|
|
74
72
|
module.exports = {
|
|
75
|
-
check
|
|
73
|
+
check,
|
|
76
74
|
};
|
package/src/hosting-api.test.js
CHANGED
|
@@ -1,16 +1,35 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
const hosting = require("./hosting-api");
|
|
4
|
+
const nock = require("nock");
|
|
4
5
|
|
|
5
|
-
describe("hostingAPI", function() {
|
|
6
|
-
describe("checking a single domain with #check", function() {
|
|
7
|
-
it("using the API", async function() {
|
|
6
|
+
describe("hostingAPI", function () {
|
|
7
|
+
describe("checking a single domain with #check", function () {
|
|
8
|
+
it("using the API", async function () {
|
|
9
|
+
const scope = nock("https://api.thegreenwebfoundation.org/")
|
|
10
|
+
.get("/greencheck/google.com")
|
|
11
|
+
.reply(200, {
|
|
12
|
+
url: "google.com",
|
|
13
|
+
green: true,
|
|
14
|
+
});
|
|
8
15
|
const res = await hosting.check("google.com");
|
|
9
16
|
expect(res).toEqual(true);
|
|
10
17
|
});
|
|
11
18
|
});
|
|
12
|
-
describe("implicitly checking multiple domains with #check", function() {
|
|
13
|
-
it("using the API", async function() {
|
|
19
|
+
describe("implicitly checking multiple domains with #check", function () {
|
|
20
|
+
it("using the API", async function () {
|
|
21
|
+
const scope = nock("https://api.thegreenwebfoundation.org/")
|
|
22
|
+
.get("/v2/greencheckmulti/[%22google.com%22,%22kochindustries.com%22]")
|
|
23
|
+
.reply(200, {
|
|
24
|
+
"google.com": {
|
|
25
|
+
url: "google.com",
|
|
26
|
+
green: true,
|
|
27
|
+
},
|
|
28
|
+
"kochindustries.com": {
|
|
29
|
+
url: "kochindustries.com",
|
|
30
|
+
green: null,
|
|
31
|
+
},
|
|
32
|
+
});
|
|
14
33
|
const res = await hosting.check(["google.com", "kochindustries.com"]);
|
|
15
34
|
expect(res).toContain("google.com");
|
|
16
35
|
});
|
|
@@ -12,15 +12,15 @@ const dbPath = path.resolve(
|
|
|
12
12
|
"url2green.test.db"
|
|
13
13
|
);
|
|
14
14
|
|
|
15
|
-
describe("hostingDatabase", function() {
|
|
16
|
-
describe("checking a single domain with #check", function() {
|
|
17
|
-
test("tries to use a local database if available ", async function() {
|
|
15
|
+
describe("hostingDatabase", function () {
|
|
16
|
+
describe("checking a single domain with #check", function () {
|
|
17
|
+
test("tries to use a local database if available ", async function () {
|
|
18
18
|
const res = await hosting.check("google.com", dbPath);
|
|
19
19
|
expect(res).toEqual(true);
|
|
20
20
|
});
|
|
21
21
|
});
|
|
22
|
-
describe("implicitly checking multiple domains with #check", function() {
|
|
23
|
-
test("tries to use a local database if available", async function() {
|
|
22
|
+
describe("implicitly checking multiple domains with #check", function () {
|
|
23
|
+
test("tries to use a local database if available", async function () {
|
|
24
24
|
const res = await hosting.check(
|
|
25
25
|
["google.com", "kochindustries.com"],
|
|
26
26
|
dbPath
|
package/src/hosting-json.js
CHANGED
|
@@ -29,11 +29,11 @@ function checkInJSON(domain, db) {
|
|
|
29
29
|
|
|
30
30
|
function greenDomainsFromResults(greenResults) {
|
|
31
31
|
const entries = Object.entries(greenResults);
|
|
32
|
-
let greenEntries = entries.filter(function([key, val]) {
|
|
32
|
+
let greenEntries = entries.filter(function ([key, val]) {
|
|
33
33
|
return val.green;
|
|
34
34
|
});
|
|
35
35
|
|
|
36
|
-
return greenEntries.map(function([key, val]) {
|
|
36
|
+
return greenEntries.map(function ([key, val]) {
|
|
37
37
|
return val.url;
|
|
38
38
|
});
|
|
39
39
|
}
|
|
@@ -51,5 +51,5 @@ function checkDomainsInJSON(domains, db) {
|
|
|
51
51
|
|
|
52
52
|
module.exports = {
|
|
53
53
|
check,
|
|
54
|
-
loadJSON
|
|
54
|
+
loadJSON,
|
|
55
55
|
};
|
package/src/hosting-json.test.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
const hosting = require("./hosting-json");
|
|
4
4
|
const path = require("path");
|
|
5
5
|
|
|
6
|
-
describe("hostingJSON", function() {
|
|
6
|
+
describe("hostingJSON", function () {
|
|
7
7
|
const jsonPath = path.resolve(
|
|
8
8
|
__dirname,
|
|
9
9
|
"..",
|
|
@@ -12,15 +12,15 @@ describe("hostingJSON", function() {
|
|
|
12
12
|
"url2green.test.json"
|
|
13
13
|
);
|
|
14
14
|
|
|
15
|
-
describe("checking a single domain with #check", function() {
|
|
16
|
-
test("against the list of domains as JSON", async function() {
|
|
15
|
+
describe("checking a single domain with #check", function () {
|
|
16
|
+
test("against the list of domains as JSON", async function () {
|
|
17
17
|
const db = await hosting.loadJSON(jsonPath);
|
|
18
18
|
const res = await hosting.check("google.com", db);
|
|
19
19
|
expect(res).toEqual(true);
|
|
20
20
|
});
|
|
21
21
|
});
|
|
22
|
-
describe("implicitly checking multiple domains with #check", function() {
|
|
23
|
-
test("against the list of domains as JSON", async function() {
|
|
22
|
+
describe("implicitly checking multiple domains with #check", function () {
|
|
23
|
+
test("against the list of domains as JSON", async function () {
|
|
24
24
|
const db = await hosting.loadJSON(jsonPath);
|
|
25
25
|
const domains = ["google.com", "kochindustries.com"];
|
|
26
26
|
|
package/src/hosting.js
CHANGED
|
@@ -14,23 +14,23 @@ function check(domain, db) {
|
|
|
14
14
|
|
|
15
15
|
function greenDomainsFromResults(greenResults) {
|
|
16
16
|
const entries = Object.entries(greenResults);
|
|
17
|
-
let greenEntries = entries.filter(function([key, val]) {
|
|
17
|
+
let greenEntries = entries.filter(function ([key, val]) {
|
|
18
18
|
return val.green;
|
|
19
19
|
});
|
|
20
20
|
|
|
21
|
-
return greenEntries.map(function([key, val]) {
|
|
21
|
+
return greenEntries.map(function ([key, val]) {
|
|
22
22
|
return val.url;
|
|
23
23
|
});
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
-
async function checkPage(pageXray) {
|
|
26
|
+
async function checkPage(pageXray, db) {
|
|
27
27
|
const domains = Object.keys(pageXray.domains);
|
|
28
|
-
return check(domains);
|
|
28
|
+
return check(domains, db);
|
|
29
29
|
}
|
|
30
30
|
|
|
31
31
|
module.exports = {
|
|
32
32
|
check,
|
|
33
33
|
checkPage,
|
|
34
34
|
greenDomains: greenDomainsFromResults,
|
|
35
|
-
loadJSON: hostingJSON.loadJSON
|
|
35
|
+
loadJSON: hostingJSON.loadJSON,
|
|
36
36
|
};
|
package/src/hosting.test.js
CHANGED
|
@@ -6,9 +6,17 @@ const path = require("path");
|
|
|
6
6
|
const hosting = require("./hosting");
|
|
7
7
|
const pagexray = require("pagexray");
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
const jsonPath = path.resolve(
|
|
10
|
+
__dirname,
|
|
11
|
+
"..",
|
|
12
|
+
"data",
|
|
13
|
+
"fixtures",
|
|
14
|
+
"url2green.test.json"
|
|
15
|
+
);
|
|
16
|
+
|
|
17
|
+
describe("hosting", function () {
|
|
10
18
|
let har;
|
|
11
|
-
beforeEach(function() {
|
|
19
|
+
beforeEach(function () {
|
|
12
20
|
har = JSON.parse(
|
|
13
21
|
fs.readFileSync(
|
|
14
22
|
path.resolve(__dirname, "../data/fixtures/tgwf.har"),
|
|
@@ -16,17 +24,16 @@ describe("hosting", function() {
|
|
|
16
24
|
)
|
|
17
25
|
);
|
|
18
26
|
});
|
|
19
|
-
describe("checking all domains on a page object with #checkPage ", function() {
|
|
20
|
-
it("it returns a list of green domains, when passed a page object", async function() {
|
|
27
|
+
describe("checking all domains on a page object with #checkPage ", function () {
|
|
28
|
+
it("it returns a list of green domains, when passed a page object", async function () {
|
|
21
29
|
const pages = pagexray.convert(har);
|
|
22
30
|
const pageXrayRun = pages[0];
|
|
31
|
+
const db = await hosting.loadJSON(jsonPath);
|
|
32
|
+
const greenDomains = await hosting.checkPage(pageXrayRun, db);
|
|
23
33
|
|
|
24
|
-
|
|
25
|
-
const greenDomains = await hosting.checkPage(pageXrayRun);
|
|
26
|
-
|
|
27
|
-
expect(greenDomains).toHaveLength(10);
|
|
28
|
-
|
|
34
|
+
expect(greenDomains).toHaveLength(11);
|
|
29
35
|
const expectedGreendomains = [
|
|
36
|
+
"maxcdn.bootstrapcdn.com",
|
|
30
37
|
"thegreenwebfoundation.org",
|
|
31
38
|
"www.thegreenwebfoundation.org",
|
|
32
39
|
"fonts.googleapis.com",
|
|
@@ -36,9 +43,9 @@ describe("hosting", function() {
|
|
|
36
43
|
"graphite.thegreenwebfoundation.org",
|
|
37
44
|
"analytics.thegreenwebfoundation.org",
|
|
38
45
|
"fonts.gstatic.com",
|
|
39
|
-
"api.thegreenwebfoundation.org"
|
|
46
|
+
"api.thegreenwebfoundation.org",
|
|
40
47
|
];
|
|
41
|
-
greenDomains.forEach(function(dom) {
|
|
48
|
+
greenDomains.forEach(function (dom) {
|
|
42
49
|
expect(expectedGreendomains).toContain(dom);
|
|
43
50
|
});
|
|
44
51
|
});
|
|
@@ -46,21 +53,25 @@ describe("hosting", function() {
|
|
|
46
53
|
// 'it returns an empty list, when passed a page object with no green domains'
|
|
47
54
|
// );
|
|
48
55
|
});
|
|
49
|
-
describe("checking a single domain with #check", function() {
|
|
50
|
-
it("use the API instead", async function() {
|
|
51
|
-
const
|
|
56
|
+
describe("checking a single domain with #check", function () {
|
|
57
|
+
it("use the API instead", async function () {
|
|
58
|
+
const db = await hosting.loadJSON(jsonPath);
|
|
59
|
+
const res = await hosting.check("google.com", db);
|
|
52
60
|
expect(res).toEqual(true);
|
|
53
61
|
});
|
|
54
62
|
});
|
|
55
|
-
describe("implicitly checking multiple domains with #check", function() {
|
|
56
|
-
it("Use the API", async function() {
|
|
57
|
-
const
|
|
63
|
+
describe("implicitly checking multiple domains with #check", function () {
|
|
64
|
+
it("Use the API", async function () {
|
|
65
|
+
const db = await hosting.loadJSON(jsonPath);
|
|
66
|
+
|
|
67
|
+
const res = await hosting.check(["google.com", "kochindustries.com"], db);
|
|
58
68
|
expect(res).toContain("google.com");
|
|
59
69
|
});
|
|
60
70
|
});
|
|
61
|
-
describe("explicitly checking multiple domains with #checkMulti", function() {
|
|
62
|
-
it("use the API", async function() {
|
|
63
|
-
const
|
|
71
|
+
describe("explicitly checking multiple domains with #checkMulti", function () {
|
|
72
|
+
it("use the API", async function () {
|
|
73
|
+
const db = await hosting.loadJSON(jsonPath);
|
|
74
|
+
const res = await hosting.check(["google.com", "kochindustries.com"], db);
|
|
64
75
|
expect(res).toContain("google.com");
|
|
65
76
|
});
|
|
66
77
|
});
|
package/src/index.js
CHANGED