@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/src/co2.test.js
CHANGED
|
@@ -1,141 +1,140 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
const fs = require(
|
|
4
|
-
const path = require(
|
|
5
|
-
|
|
6
|
-
const
|
|
7
|
-
const pagexray = require(
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const fs = require("fs");
|
|
4
|
+
const path = require("path");
|
|
5
|
+
|
|
6
|
+
const CO2 = require("./co2");
|
|
7
|
+
const pagexray = require("pagexray");
|
|
8
|
+
|
|
9
|
+
describe("co2", function () {
|
|
10
|
+
let har, co2;
|
|
11
|
+
const TGWF_GREY_VALUE = 2.0484539712;
|
|
12
|
+
const TGWF_GREEN_VALUE = 0.54704300112;
|
|
13
|
+
const TGWF_MIXED_VALUE = 1.6706517455999996;
|
|
14
|
+
|
|
15
|
+
const MILLION = 1000000;
|
|
16
|
+
const MILLION_GREY = 2.9064;
|
|
17
|
+
const MILLION_GREEN = 2.318;
|
|
18
|
+
|
|
19
|
+
beforeEach(function () {
|
|
20
|
+
co2 = new CO2();
|
|
21
|
+
har = JSON.parse(
|
|
22
|
+
fs.readFileSync(
|
|
23
|
+
path.resolve(__dirname, "../data/fixtures/tgwf.har"),
|
|
24
|
+
"utf8"
|
|
25
|
+
)
|
|
26
|
+
);
|
|
27
|
+
});
|
|
20
28
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
29
|
+
describe("perByte", function () {
|
|
30
|
+
it("returns a CO2 number for data transfer using 'grey' power", function () {
|
|
31
|
+
expect(co2.perByte(MILLION)).toBe(MILLION_GREY);
|
|
24
32
|
});
|
|
25
33
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
it("returns a CO2 number for data transfer using 'grey' power", function () {
|
|
30
|
-
expect(co2.perByte(MILLION)).toBe(MILLION_GREY);
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
it("returns a lower CO2 number for data transfer from domains using entirely 'green' power", function () {
|
|
34
|
-
expect(co2.perByte(MILLION, false)).toBe(MILLION_GREY);
|
|
35
|
-
expect(co2.perByte(MILLION, true)).toBe(MILLION_GREEN);
|
|
36
|
-
});
|
|
34
|
+
it("returns a lower CO2 number for data transfer from domains using entirely 'green' power", function () {
|
|
35
|
+
expect(co2.perByte(MILLION, false)).toBe(MILLION_GREY);
|
|
36
|
+
expect(co2.perByte(MILLION, true)).toBe(MILLION_GREEN);
|
|
37
37
|
});
|
|
38
|
+
});
|
|
38
39
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
40
|
+
describe("perPage", function () {
|
|
41
|
+
it("returns CO2 for total transfer for page", function () {
|
|
42
|
+
const pages = pagexray.convert(har);
|
|
43
|
+
const pageXrayRun = pages[0];
|
|
43
44
|
|
|
44
|
-
|
|
45
|
-
});
|
|
46
|
-
it('returns lower CO2 for page served from green site', function () {
|
|
47
|
-
const pages = pagexray.convert(har);
|
|
48
|
-
const pageXrayRun = pages[0];
|
|
49
|
-
let green = [
|
|
50
|
-
'www.thegreenwebfoundation.org',
|
|
51
|
-
'fonts.googleapis.com',
|
|
52
|
-
'ajax.googleapis.com',
|
|
53
|
-
'assets.digitalclimatestrike.net',
|
|
54
|
-
'cdnjs.cloudflare.com',
|
|
55
|
-
'graphite.thegreenwebfoundation.org',
|
|
56
|
-
'analytics.thegreenwebfoundation.org',
|
|
57
|
-
'fonts.gstatic.com',
|
|
58
|
-
'api.thegreenwebfoundation.org'
|
|
59
|
-
];
|
|
60
|
-
expect(co2.perPage(pageXrayRun, green)).toBeLessThan(TGWF_GREY_VALUE);
|
|
61
|
-
});
|
|
62
|
-
it('returns a lower CO2 number where *some* domains use green power', function () {
|
|
63
|
-
const pages = pagexray.convert(har);
|
|
64
|
-
const pageXrayRun = pages[0];
|
|
65
|
-
// green can be true, or a array containing entries
|
|
66
|
-
let green = [
|
|
67
|
-
'www.thegreenwebfoundation.org',
|
|
68
|
-
'fonts.googleapis.com',
|
|
69
|
-
'ajax.googleapis.com',
|
|
70
|
-
'assets.digitalclimatestrike.net',
|
|
71
|
-
'cdnjs.cloudflare.com',
|
|
72
|
-
'graphite.thegreenwebfoundation.org',
|
|
73
|
-
'analytics.thegreenwebfoundation.org',
|
|
74
|
-
'fonts.gstatic.com',
|
|
75
|
-
'api.thegreenwebfoundation.org'
|
|
76
|
-
];
|
|
77
|
-
expect(co2.perPage(pageXrayRun, green)).toBe(TGWF_MIXED_VALUE);
|
|
78
|
-
});
|
|
45
|
+
expect(co2.perPage(pageXrayRun)).toBe(TGWF_GREY_VALUE);
|
|
79
46
|
});
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
'ajax.googleapis.com',
|
|
113
|
-
'assets.digitalclimatestrike.net',
|
|
114
|
-
'cdnjs.cloudflare.com',
|
|
115
|
-
'graphite.thegreenwebfoundation.org',
|
|
116
|
-
'analytics.thegreenwebfoundation.org',
|
|
117
|
-
'fonts.gstatic.com',
|
|
118
|
-
'api.thegreenwebfoundation.org'
|
|
119
|
-
];
|
|
120
|
-
const res = co2.perDomain(pageXrayRun);
|
|
121
|
-
const resWithGreen = co2.perDomain(pageXrayRun, greenDomains);
|
|
122
|
-
|
|
123
|
-
for (let obj of res) {
|
|
124
|
-
expect(typeof obj.co2).toBe('number');
|
|
125
|
-
};
|
|
126
|
-
for (let obj of greenDomains) {
|
|
127
|
-
let index = 0
|
|
128
|
-
expect(resWithGreen[index].co2).toBeLessThan(res[index].co2);
|
|
129
|
-
index++
|
|
130
|
-
}
|
|
131
|
-
});
|
|
47
|
+
it("returns lower CO2 for page served from green site", function () {
|
|
48
|
+
const pages = pagexray.convert(har);
|
|
49
|
+
const pageXrayRun = pages[0];
|
|
50
|
+
let green = [
|
|
51
|
+
"www.thegreenwebfoundation.org",
|
|
52
|
+
"fonts.googleapis.com",
|
|
53
|
+
"ajax.googleapis.com",
|
|
54
|
+
"assets.digitalclimatestrike.net",
|
|
55
|
+
"cdnjs.cloudflare.com",
|
|
56
|
+
"graphite.thegreenwebfoundation.org",
|
|
57
|
+
"analytics.thegreenwebfoundation.org",
|
|
58
|
+
"fonts.gstatic.com",
|
|
59
|
+
"api.thegreenwebfoundation.org",
|
|
60
|
+
];
|
|
61
|
+
expect(co2.perPage(pageXrayRun, green)).toBeLessThan(TGWF_GREY_VALUE);
|
|
62
|
+
});
|
|
63
|
+
it("returns a lower CO2 number where *some* domains use green power", function () {
|
|
64
|
+
const pages = pagexray.convert(har);
|
|
65
|
+
const pageXrayRun = pages[0];
|
|
66
|
+
// green can be true, or a array containing entries
|
|
67
|
+
let green = [
|
|
68
|
+
"www.thegreenwebfoundation.org",
|
|
69
|
+
"fonts.googleapis.com",
|
|
70
|
+
"ajax.googleapis.com",
|
|
71
|
+
"assets.digitalclimatestrike.net",
|
|
72
|
+
"cdnjs.cloudflare.com",
|
|
73
|
+
"graphite.thegreenwebfoundation.org",
|
|
74
|
+
"analytics.thegreenwebfoundation.org",
|
|
75
|
+
"fonts.gstatic.com",
|
|
76
|
+
"api.thegreenwebfoundation.org",
|
|
77
|
+
];
|
|
78
|
+
expect(co2.perPage(pageXrayRun, green)).toBe(TGWF_MIXED_VALUE);
|
|
132
79
|
});
|
|
133
|
-
// describe('perContentType', function () {
|
|
134
|
-
// test.skip('shows a breakdown of emissions by content type');
|
|
135
|
-
// });
|
|
136
|
-
// describe('dirtiestResources', function () {
|
|
137
|
-
// it.skip('shows the top 10 resources by CO2 emissions');
|
|
138
|
-
// });
|
|
139
80
|
});
|
|
81
|
+
describe("perDomain", function () {
|
|
82
|
+
it("shows object listing Co2 for each domain", function () {
|
|
83
|
+
const pages = pagexray.convert(har);
|
|
84
|
+
const pageXrayRun = pages[0];
|
|
85
|
+
const res = co2.perDomain(pageXrayRun);
|
|
86
|
+
|
|
87
|
+
const domains = [
|
|
88
|
+
"thegreenwebfoundation.org",
|
|
89
|
+
"www.thegreenwebfoundation.org",
|
|
90
|
+
"maxcdn.bootstrapcdn.com",
|
|
91
|
+
"fonts.googleapis.com",
|
|
92
|
+
"ajax.googleapis.com",
|
|
93
|
+
"assets.digitalclimatestrike.net",
|
|
94
|
+
"cdnjs.cloudflare.com",
|
|
95
|
+
"graphite.thegreenwebfoundation.org",
|
|
96
|
+
"analytics.thegreenwebfoundation.org",
|
|
97
|
+
"fonts.gstatic.com",
|
|
98
|
+
"api.thegreenwebfoundation.org",
|
|
99
|
+
];
|
|
100
|
+
|
|
101
|
+
for (let obj of res) {
|
|
102
|
+
expect(domains.indexOf(obj.domain)).toBeGreaterThan(-1);
|
|
103
|
+
expect(typeof obj.co2).toBe("number");
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
it("shows lower Co2 for green domains", function () {
|
|
107
|
+
const pages = pagexray.convert(har);
|
|
108
|
+
const pageXrayRun = pages[0];
|
|
109
|
+
|
|
110
|
+
const greenDomains = [
|
|
111
|
+
"www.thegreenwebfoundation.org",
|
|
112
|
+
"fonts.googleapis.com",
|
|
113
|
+
"ajax.googleapis.com",
|
|
114
|
+
"assets.digitalclimatestrike.net",
|
|
115
|
+
"cdnjs.cloudflare.com",
|
|
116
|
+
"graphite.thegreenwebfoundation.org",
|
|
117
|
+
"analytics.thegreenwebfoundation.org",
|
|
118
|
+
"fonts.gstatic.com",
|
|
119
|
+
"api.thegreenwebfoundation.org",
|
|
120
|
+
];
|
|
121
|
+
const res = co2.perDomain(pageXrayRun);
|
|
122
|
+
const resWithGreen = co2.perDomain(pageXrayRun, greenDomains);
|
|
123
|
+
|
|
124
|
+
for (let obj of res) {
|
|
125
|
+
expect(typeof obj.co2).toBe("number");
|
|
126
|
+
}
|
|
127
|
+
for (let obj of greenDomains) {
|
|
128
|
+
let index = 0;
|
|
129
|
+
expect(resWithGreen[index].co2).toBeLessThan(res[index].co2);
|
|
130
|
+
index++;
|
|
131
|
+
}
|
|
132
|
+
});
|
|
133
|
+
});
|
|
134
|
+
// describe('perContentType', function () {
|
|
135
|
+
// test.skip('shows a breakdown of emissions by content type');
|
|
136
|
+
// });
|
|
137
|
+
// describe('dirtiestResources', function () {
|
|
138
|
+
// it.skip('shows the top 10 resources by CO2 emissions');
|
|
139
|
+
// });
|
|
140
140
|
});
|
|
141
|
-
|
package/src/green-byte.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
|
|
2
1
|
// PLEASE DO NOT USE THIS MODEL YET FOR CALCS
|
|
3
2
|
|
|
4
3
|
const CO2_PER_KWH_IN_DC_GREY = 519;
|
|
@@ -19,10 +18,9 @@ const KWH_PER_BYTE_FOR_DEVICES = 0.00000000055;
|
|
|
19
18
|
// 1. the usage for devices (which is small proportion of the energy use)
|
|
20
19
|
// 2. the *making* the device, which is comparitively high.
|
|
21
20
|
|
|
22
|
-
|
|
23
21
|
module.exports = {
|
|
24
22
|
KWH_PER_BYTE_IN_DC,
|
|
25
23
|
KWH_PER_BYTE_FOR_NETWORK,
|
|
26
24
|
KWH_PER_BYTE_FOR_DEVICES,
|
|
27
|
-
CO2_PER_KWH_IN_DC_GREY
|
|
28
|
-
}
|
|
25
|
+
CO2_PER_KWH_IN_DC_GREY,
|
|
26
|
+
};
|
|
@@ -1,25 +1,22 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
2
|
|
|
3
|
-
const log = require(
|
|
4
|
-
const https = require(
|
|
3
|
+
const log = require("debug")("tgwf:hostingAPI");
|
|
4
|
+
const https = require("https");
|
|
5
5
|
|
|
6
6
|
function check(domain) {
|
|
7
7
|
// is it a single domain or an array of them?
|
|
8
|
-
if (typeof domain ===
|
|
9
|
-
return checkAgainstAPI(domain)
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
return checkDomainsAgainstAPI(domain)
|
|
8
|
+
if (typeof domain === "string") {
|
|
9
|
+
return checkAgainstAPI(domain);
|
|
10
|
+
} else {
|
|
11
|
+
return checkDomainsAgainstAPI(domain);
|
|
13
12
|
}
|
|
14
13
|
}
|
|
15
14
|
|
|
16
15
|
async function checkAgainstAPI(domain) {
|
|
17
16
|
const res = JSON.parse(
|
|
18
|
-
await getBody(
|
|
19
|
-
`https://api.thegreenwebfoundation.org/greencheck/${domain}`
|
|
20
|
-
)
|
|
17
|
+
await getBody(`https://api.thegreenwebfoundation.org/greencheck/${domain}`)
|
|
21
18
|
);
|
|
22
|
-
return res.green
|
|
19
|
+
return res.green;
|
|
23
20
|
}
|
|
24
21
|
|
|
25
22
|
async function checkDomainsAgainstAPI(domains) {
|
|
@@ -31,9 +28,7 @@ async function checkDomainsAgainstAPI(domains) {
|
|
|
31
28
|
)}`
|
|
32
29
|
)
|
|
33
30
|
);
|
|
34
|
-
|
|
35
|
-
return greenDomainsFromResults(allGreenCheckResults)
|
|
36
|
-
|
|
31
|
+
return greenDomainsFromResults(allGreenCheckResults);
|
|
37
32
|
} catch (e) {
|
|
38
33
|
return [];
|
|
39
34
|
}
|
|
@@ -44,13 +39,11 @@ function greenDomainsFromResults(greenResults) {
|
|
|
44
39
|
let greenEntries = entries.filter(function ([key, val]) {
|
|
45
40
|
return val.green;
|
|
46
41
|
});
|
|
47
|
-
|
|
48
42
|
return greenEntries.map(function ([key, val]) {
|
|
49
43
|
return val.url;
|
|
50
44
|
});
|
|
51
45
|
}
|
|
52
46
|
|
|
53
|
-
|
|
54
47
|
async function getBody(url) {
|
|
55
48
|
// Return new promise
|
|
56
49
|
return new Promise(function (resolve, reject) {
|
|
@@ -58,7 +51,7 @@ async function getBody(url) {
|
|
|
58
51
|
const req = https.get(url, function (res) {
|
|
59
52
|
if (res.statusCode < 200 || res.statusCode >= 300) {
|
|
60
53
|
log(
|
|
61
|
-
|
|
54
|
+
"Could not get info from the Green Web Foundation API, %s for %s",
|
|
62
55
|
res.statusCode,
|
|
63
56
|
url
|
|
64
57
|
);
|
|
@@ -66,16 +59,16 @@ async function getBody(url) {
|
|
|
66
59
|
}
|
|
67
60
|
const data = [];
|
|
68
61
|
|
|
69
|
-
res.on(
|
|
62
|
+
res.on("data", (chunk) => {
|
|
70
63
|
data.push(chunk);
|
|
71
64
|
});
|
|
72
65
|
|
|
73
|
-
res.on(
|
|
66
|
+
res.on("end", () => resolve(Buffer.concat(data).toString()));
|
|
74
67
|
});
|
|
75
68
|
req.end();
|
|
76
69
|
});
|
|
77
70
|
}
|
|
78
71
|
|
|
79
72
|
module.exports = {
|
|
80
|
-
check
|
|
73
|
+
check,
|
|
81
74
|
};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const hosting = require("./hosting-api");
|
|
4
|
+
const nock = require("nock");
|
|
5
|
+
|
|
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
|
+
});
|
|
15
|
+
const res = await hosting.check("google.com");
|
|
16
|
+
expect(res).toEqual(true);
|
|
17
|
+
});
|
|
18
|
+
});
|
|
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
|
+
});
|
|
33
|
+
const res = await hosting.check(["google.com", "kochindustries.com"]);
|
|
34
|
+
expect(res).toContain("google.com");
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
});
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const log = require("debug")("tgwf:url2green:test");
|
|
4
|
+
const { hosting } = require("@tgwf/url2green");
|
|
5
|
+
const path = require("path");
|
|
6
|
+
|
|
7
|
+
const dbPath = path.resolve(
|
|
8
|
+
__dirname,
|
|
9
|
+
"..",
|
|
10
|
+
"data",
|
|
11
|
+
"fixtures",
|
|
12
|
+
"url2green.test.db"
|
|
13
|
+
);
|
|
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 () {
|
|
18
|
+
const res = await hosting.check("google.com", dbPath);
|
|
19
|
+
expect(res).toEqual(true);
|
|
20
|
+
});
|
|
21
|
+
});
|
|
22
|
+
describe("implicitly checking multiple domains with #check", function () {
|
|
23
|
+
test("tries to use a local database if available", async function () {
|
|
24
|
+
const res = await hosting.check(
|
|
25
|
+
["google.com", "kochindustries.com"],
|
|
26
|
+
dbPath
|
|
27
|
+
);
|
|
28
|
+
expect(res).toContain("google.com");
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
});
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const log = require("debug")("tgwf:hostingCache");
|
|
4
|
+
const path = require("path");
|
|
5
|
+
const fs = require("fs");
|
|
6
|
+
const { promisify } = require("util");
|
|
7
|
+
const readFile = promisify(fs.readFile);
|
|
8
|
+
|
|
9
|
+
async function loadJSON(jsonPath) {
|
|
10
|
+
const jsonBuffer = await readFile(jsonPath);
|
|
11
|
+
return JSON.parse(jsonBuffer);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
async function check(domain, db) {
|
|
15
|
+
// is it a single domain or an array of them?
|
|
16
|
+
if (typeof domain === "string") {
|
|
17
|
+
return checkInJSON(domain, db);
|
|
18
|
+
} else {
|
|
19
|
+
return checkDomainsInJSON(domain, db);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function checkInJSON(domain, db) {
|
|
24
|
+
if (db.indexOf(domain) > -1) {
|
|
25
|
+
return true;
|
|
26
|
+
}
|
|
27
|
+
return false;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function greenDomainsFromResults(greenResults) {
|
|
31
|
+
const entries = Object.entries(greenResults);
|
|
32
|
+
let greenEntries = entries.filter(function ([key, val]) {
|
|
33
|
+
return val.green;
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
return greenEntries.map(function ([key, val]) {
|
|
37
|
+
return val.url;
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function checkDomainsInJSON(domains, db) {
|
|
42
|
+
let greenDomains = [];
|
|
43
|
+
|
|
44
|
+
for (let domain of domains) {
|
|
45
|
+
if (db.indexOf(domain) > -1) {
|
|
46
|
+
greenDomains.push(domain);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return greenDomains;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
module.exports = {
|
|
53
|
+
check,
|
|
54
|
+
loadJSON,
|
|
55
|
+
};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const hosting = require("./hosting-json");
|
|
4
|
+
const path = require("path");
|
|
5
|
+
|
|
6
|
+
describe("hostingJSON", function () {
|
|
7
|
+
const jsonPath = path.resolve(
|
|
8
|
+
__dirname,
|
|
9
|
+
"..",
|
|
10
|
+
"data",
|
|
11
|
+
"fixtures",
|
|
12
|
+
"url2green.test.json"
|
|
13
|
+
);
|
|
14
|
+
|
|
15
|
+
describe("checking a single domain with #check", function () {
|
|
16
|
+
test("against the list of domains as JSON", async function () {
|
|
17
|
+
const db = await hosting.loadJSON(jsonPath);
|
|
18
|
+
const res = await hosting.check("google.com", db);
|
|
19
|
+
expect(res).toEqual(true);
|
|
20
|
+
});
|
|
21
|
+
});
|
|
22
|
+
describe("implicitly checking multiple domains with #check", function () {
|
|
23
|
+
test("against the list of domains as JSON", async function () {
|
|
24
|
+
const db = await hosting.loadJSON(jsonPath);
|
|
25
|
+
const domains = ["google.com", "kochindustries.com"];
|
|
26
|
+
|
|
27
|
+
const res = await hosting.check(domains, db);
|
|
28
|
+
expect(res).toContain("google.com");
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
});
|
package/src/hosting.js
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
2
|
|
|
3
|
-
const log = require(
|
|
4
|
-
const hostingAPI = require(
|
|
5
|
-
const
|
|
3
|
+
const log = require("debug")("tgwf:hosting");
|
|
4
|
+
const hostingAPI = require("./hosting-api");
|
|
5
|
+
const hostingJSON = require("./hosting-json");
|
|
6
6
|
|
|
7
|
-
function check(domain,
|
|
8
|
-
if (
|
|
9
|
-
return
|
|
7
|
+
function check(domain, db) {
|
|
8
|
+
if (db) {
|
|
9
|
+
return hostingJSON.check(domain, db);
|
|
10
10
|
} else {
|
|
11
11
|
return hostingAPI.check(domain);
|
|
12
12
|
}
|
|
13
13
|
}
|
|
14
14
|
|
|
15
15
|
function greenDomainsFromResults(greenResults) {
|
|
16
|
-
const entries = Object.entries(greenResults)
|
|
16
|
+
const entries = Object.entries(greenResults);
|
|
17
17
|
let greenEntries = entries.filter(function ([key, val]) {
|
|
18
|
-
return val.green
|
|
18
|
+
return val.green;
|
|
19
19
|
});
|
|
20
20
|
|
|
21
21
|
return greenEntries.map(function ([key, val]) {
|
|
@@ -23,13 +23,14 @@ function greenDomainsFromResults(greenResults) {
|
|
|
23
23
|
});
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
-
async function checkPage(pageXray) {
|
|
27
|
-
const domains = Object.keys(pageXray.domains)
|
|
28
|
-
return check(domains)
|
|
26
|
+
async function checkPage(pageXray, db) {
|
|
27
|
+
const domains = Object.keys(pageXray.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
36
|
};
|