@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 +35 -5
- package/README.md +22 -25
- package/data/Lean-ICT-Materials-1byte-Model-2018.xlsx +0 -0
- package/data/Lean-ICT-Materials-Forecast-Model-2018.xlsx +0 -0
- package/data/fixtures/url2green.test.json.gz +0 -0
- package/images/swd-energy-usage.png +0 -0
- package/package.json +3 -2
- package/src/1byte.js +52 -8
- package/src/1byte.test.js +17 -0
- package/src/co2.js +19 -34
- package/src/co2.test.js +267 -118
- package/src/constants/file-size.js +5 -0
- package/src/constants/index.js +3 -0
- package/src/helpers/index.js +5 -0
- package/src/hosting-api.test.js +5 -5
- package/src/hosting-database.test.js +5 -5
- package/src/hosting-json.js +21 -1
- package/src/hosting-json.test.js +19 -6
- package/src/hosting.test.js +10 -10
- package/src/readme.md +66 -0
- package/src/sustainable-web-design.js +224 -0
- package/src/sustainable-web-design.test.js +81 -0
package/src/co2.test.js
CHANGED
|
@@ -4,137 +4,286 @@ const fs = require("fs");
|
|
|
4
4
|
const path = require("path");
|
|
5
5
|
|
|
6
6
|
const CO2 = require("./co2");
|
|
7
|
+
const swd = require("./sustainable-web-design");
|
|
7
8
|
const pagexray = require("pagexray");
|
|
8
9
|
|
|
9
|
-
describe("co2",
|
|
10
|
+
describe("co2", () => {
|
|
10
11
|
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
|
-
});
|
|
28
12
|
|
|
29
|
-
describe("
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
13
|
+
describe("1 byte model", () => {
|
|
14
|
+
const TGWF_GREY_VALUE = 0.20497;
|
|
15
|
+
const TGWF_GREEN_VALUE = 0.54704;
|
|
16
|
+
const TGWF_MIXED_VALUE = 0.16718;
|
|
17
|
+
|
|
18
|
+
const MILLION = 1000000;
|
|
19
|
+
const MILLION_GREY = 0.29081;
|
|
20
|
+
const MILLION_GREEN = 0.23196;
|
|
33
21
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
22
|
+
beforeEach(() => {
|
|
23
|
+
co2 = new CO2();
|
|
24
|
+
har = JSON.parse(
|
|
25
|
+
fs.readFileSync(
|
|
26
|
+
path.resolve(__dirname, "../data/fixtures/tgwf.har"),
|
|
27
|
+
"utf8"
|
|
28
|
+
)
|
|
29
|
+
);
|
|
37
30
|
});
|
|
38
|
-
});
|
|
39
31
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
32
|
+
describe("perByte", () => {
|
|
33
|
+
it("returns a CO2 number for data transfer using 'grey' power", () => {
|
|
34
|
+
expect(co2.perByte(MILLION).toPrecision(5)).toBe(
|
|
35
|
+
MILLION_GREY.toPrecision(5)
|
|
36
|
+
);
|
|
37
|
+
});
|
|
44
38
|
|
|
45
|
-
|
|
39
|
+
it("returns a lower CO2 number for data transfer from domains using entirely 'green' power", () => {
|
|
40
|
+
expect(co2.perByte(MILLION).toPrecision(5)).toBe(
|
|
41
|
+
MILLION_GREY.toPrecision(5)
|
|
42
|
+
);
|
|
43
|
+
expect(co2.perByte(MILLION, true).toPrecision(5)).toBe(
|
|
44
|
+
MILLION_GREEN.toPrecision(5)
|
|
45
|
+
);
|
|
46
|
+
});
|
|
46
47
|
});
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
48
|
+
|
|
49
|
+
describe("perPage", () => {
|
|
50
|
+
it("returns CO2 for total transfer for page", () => {
|
|
51
|
+
const pages = pagexray.convert(har);
|
|
52
|
+
const pageXrayRun = pages[0];
|
|
53
|
+
|
|
54
|
+
expect(co2.perPage(pageXrayRun).toPrecision(5)).toBe(
|
|
55
|
+
TGWF_GREY_VALUE.toPrecision(5)
|
|
56
|
+
);
|
|
57
|
+
});
|
|
58
|
+
it("returns lower CO2 for page served from green site", () => {
|
|
59
|
+
const pages = pagexray.convert(har);
|
|
60
|
+
const pageXrayRun = pages[0];
|
|
61
|
+
let green = [
|
|
62
|
+
"www.thegreenwebfoundation.org",
|
|
63
|
+
"fonts.googleapis.com",
|
|
64
|
+
"ajax.googleapis.com",
|
|
65
|
+
"assets.digitalclimatestrike.net",
|
|
66
|
+
"cdnjs.cloudflare.com",
|
|
67
|
+
"graphite.thegreenwebfoundation.org",
|
|
68
|
+
"analytics.thegreenwebfoundation.org",
|
|
69
|
+
"fonts.gstatic.com",
|
|
70
|
+
"api.thegreenwebfoundation.org",
|
|
71
|
+
];
|
|
72
|
+
expect(co2.perPage(pageXrayRun, green)).toBeLessThan(TGWF_GREY_VALUE);
|
|
73
|
+
});
|
|
74
|
+
it("returns a lower CO2 number where *some* domains use green power", () => {
|
|
75
|
+
const pages = pagexray.convert(har);
|
|
76
|
+
const pageXrayRun = pages[0];
|
|
77
|
+
// green can be true, or a array containing entries
|
|
78
|
+
let green = [
|
|
79
|
+
"www.thegreenwebfoundation.org",
|
|
80
|
+
"fonts.googleapis.com",
|
|
81
|
+
"ajax.googleapis.com",
|
|
82
|
+
"assets.digitalclimatestrike.net",
|
|
83
|
+
"cdnjs.cloudflare.com",
|
|
84
|
+
"graphite.thegreenwebfoundation.org",
|
|
85
|
+
"analytics.thegreenwebfoundation.org",
|
|
86
|
+
"fonts.gstatic.com",
|
|
87
|
+
"api.thegreenwebfoundation.org",
|
|
88
|
+
];
|
|
89
|
+
expect(co2.perPage(pageXrayRun, green).toPrecision(5)).toBe(
|
|
90
|
+
TGWF_MIXED_VALUE.toPrecision(5)
|
|
91
|
+
);
|
|
92
|
+
});
|
|
62
93
|
});
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
94
|
+
describe("perDomain", () => {
|
|
95
|
+
it("shows object listing Co2 for each domain", () => {
|
|
96
|
+
const pages = pagexray.convert(har);
|
|
97
|
+
const pageXrayRun = pages[0];
|
|
98
|
+
const res = co2.perDomain(pageXrayRun);
|
|
99
|
+
|
|
100
|
+
const domains = [
|
|
101
|
+
"thegreenwebfoundation.org",
|
|
102
|
+
"www.thegreenwebfoundation.org",
|
|
103
|
+
"maxcdn.bootstrapcdn.com",
|
|
104
|
+
"fonts.googleapis.com",
|
|
105
|
+
"ajax.googleapis.com",
|
|
106
|
+
"assets.digitalclimatestrike.net",
|
|
107
|
+
"cdnjs.cloudflare.com",
|
|
108
|
+
"graphite.thegreenwebfoundation.org",
|
|
109
|
+
"analytics.thegreenwebfoundation.org",
|
|
110
|
+
"fonts.gstatic.com",
|
|
111
|
+
"api.thegreenwebfoundation.org",
|
|
112
|
+
];
|
|
113
|
+
|
|
114
|
+
for (let obj of res) {
|
|
115
|
+
expect(domains.indexOf(obj.domain)).toBeGreaterThan(-1);
|
|
116
|
+
expect(typeof obj.co2).toBe("number");
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
it("shows lower Co2 for green domains", () => {
|
|
120
|
+
const pages = pagexray.convert(har);
|
|
121
|
+
const pageXrayRun = pages[0];
|
|
122
|
+
|
|
123
|
+
const greenDomains = [
|
|
124
|
+
"www.thegreenwebfoundation.org",
|
|
125
|
+
"fonts.googleapis.com",
|
|
126
|
+
"ajax.googleapis.com",
|
|
127
|
+
"assets.digitalclimatestrike.net",
|
|
128
|
+
"cdnjs.cloudflare.com",
|
|
129
|
+
"graphite.thegreenwebfoundation.org",
|
|
130
|
+
"analytics.thegreenwebfoundation.org",
|
|
131
|
+
"fonts.gstatic.com",
|
|
132
|
+
"api.thegreenwebfoundation.org",
|
|
133
|
+
];
|
|
134
|
+
const res = co2.perDomain(pageXrayRun);
|
|
135
|
+
const resWithGreen = co2.perDomain(pageXrayRun, greenDomains);
|
|
136
|
+
|
|
137
|
+
for (let obj of res) {
|
|
138
|
+
expect(typeof obj.co2).toBe("number");
|
|
139
|
+
}
|
|
140
|
+
for (let obj of greenDomains) {
|
|
141
|
+
let index = 0;
|
|
142
|
+
expect(resWithGreen[index].co2).toBeLessThan(res[index].co2);
|
|
143
|
+
index++;
|
|
144
|
+
}
|
|
145
|
+
});
|
|
79
146
|
});
|
|
80
147
|
});
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
148
|
+
|
|
149
|
+
describe("Sustainable Web Design model", () => {
|
|
150
|
+
// the SWD model should have slightly higher values as
|
|
151
|
+
// we include more of the system in calculations for the
|
|
152
|
+
// same levels of data transfer
|
|
153
|
+
const MILLION = 1000000;
|
|
154
|
+
const MILLION_GREY = 0.33343;
|
|
155
|
+
const MILLION_GREEN = 0.28908;
|
|
156
|
+
|
|
157
|
+
const TGWF_GREY_VALUE = 0.23501;
|
|
158
|
+
const TGWF_GREEN_VALUE = 0.54704;
|
|
159
|
+
const TGWF_MIXED_VALUE = 0.20652;
|
|
160
|
+
|
|
161
|
+
beforeEach(() => {
|
|
162
|
+
co2 = new CO2({ model: swd });
|
|
163
|
+
har = JSON.parse(
|
|
164
|
+
fs.readFileSync(
|
|
165
|
+
path.resolve(__dirname, "../data/fixtures/tgwf.har"),
|
|
166
|
+
"utf8"
|
|
167
|
+
)
|
|
168
|
+
);
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
describe("perByte", () => {
|
|
172
|
+
it("returns a CO2 number for data transfer", () => {
|
|
173
|
+
co2.perByte(MILLION);
|
|
174
|
+
expect(co2.perByte(MILLION).toPrecision(5)).toBe(
|
|
175
|
+
MILLION_GREY.toPrecision(5)
|
|
176
|
+
);
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
it("returns a lower CO2 number for data transfer from domains using entirely 'green' power", () => {
|
|
180
|
+
expect(co2.perByte(MILLION, false).toPrecision(5)).toBe(
|
|
181
|
+
MILLION_GREY.toPrecision(5)
|
|
182
|
+
);
|
|
183
|
+
|
|
184
|
+
expect(co2.perByte(MILLION, true).toPrecision(5)).toBe(
|
|
185
|
+
MILLION_GREEN.toPrecision(5)
|
|
186
|
+
);
|
|
187
|
+
});
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
describe("perPage", () => {
|
|
191
|
+
it("returns CO2 for total transfer for page", () => {
|
|
192
|
+
const pages = pagexray.convert(har);
|
|
193
|
+
const pageXrayRun = pages[0];
|
|
194
|
+
|
|
195
|
+
expect(co2.perPage(pageXrayRun).toPrecision(5)).toBe(
|
|
196
|
+
TGWF_GREY_VALUE.toPrecision(5)
|
|
197
|
+
);
|
|
198
|
+
});
|
|
199
|
+
it("returns lower CO2 for page served from green site", () => {
|
|
200
|
+
const pages = pagexray.convert(har);
|
|
201
|
+
const pageXrayRun = pages[0];
|
|
202
|
+
let green = [
|
|
203
|
+
"www.thegreenwebfoundation.org",
|
|
204
|
+
"fonts.googleapis.com",
|
|
205
|
+
"ajax.googleapis.com",
|
|
206
|
+
"assets.digitalclimatestrike.net",
|
|
207
|
+
"cdnjs.cloudflare.com",
|
|
208
|
+
"graphite.thegreenwebfoundation.org",
|
|
209
|
+
"analytics.thegreenwebfoundation.org",
|
|
210
|
+
"fonts.gstatic.com",
|
|
211
|
+
"api.thegreenwebfoundation.org",
|
|
212
|
+
];
|
|
213
|
+
expect(co2.perPage(pageXrayRun, green)).toBeLessThan(TGWF_GREY_VALUE);
|
|
214
|
+
});
|
|
215
|
+
it("returns a lower CO2 number where *some* domains use green power", () => {
|
|
216
|
+
const pages = pagexray.convert(har);
|
|
217
|
+
const pageXrayRun = pages[0];
|
|
218
|
+
// green can be true, or a array containing entries
|
|
219
|
+
let green = [
|
|
220
|
+
"www.thegreenwebfoundation.org",
|
|
221
|
+
"fonts.googleapis.com",
|
|
222
|
+
"ajax.googleapis.com",
|
|
223
|
+
"assets.digitalclimatestrike.net",
|
|
224
|
+
"cdnjs.cloudflare.com",
|
|
225
|
+
"graphite.thegreenwebfoundation.org",
|
|
226
|
+
"analytics.thegreenwebfoundation.org",
|
|
227
|
+
"fonts.gstatic.com",
|
|
228
|
+
"api.thegreenwebfoundation.org",
|
|
229
|
+
];
|
|
230
|
+
expect(co2.perPage(pageXrayRun, green).toPrecision(5)).toBe(
|
|
231
|
+
TGWF_MIXED_VALUE.toPrecision(5)
|
|
232
|
+
);
|
|
233
|
+
});
|
|
105
234
|
});
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
235
|
+
describe("perDomain", () => {
|
|
236
|
+
it("shows object listing Co2 for each domain", () => {
|
|
237
|
+
const pages = pagexray.convert(har);
|
|
238
|
+
const pageXrayRun = pages[0];
|
|
239
|
+
const res = co2.perDomain(pageXrayRun);
|
|
240
|
+
|
|
241
|
+
const domains = [
|
|
242
|
+
"thegreenwebfoundation.org",
|
|
243
|
+
"www.thegreenwebfoundation.org",
|
|
244
|
+
"maxcdn.bootstrapcdn.com",
|
|
245
|
+
"fonts.googleapis.com",
|
|
246
|
+
"ajax.googleapis.com",
|
|
247
|
+
"assets.digitalclimatestrike.net",
|
|
248
|
+
"cdnjs.cloudflare.com",
|
|
249
|
+
"graphite.thegreenwebfoundation.org",
|
|
250
|
+
"analytics.thegreenwebfoundation.org",
|
|
251
|
+
"fonts.gstatic.com",
|
|
252
|
+
"api.thegreenwebfoundation.org",
|
|
253
|
+
];
|
|
254
|
+
|
|
255
|
+
for (let obj of res) {
|
|
256
|
+
expect(domains.indexOf(obj.domain)).toBeGreaterThan(-1);
|
|
257
|
+
expect(typeof obj.co2).toBe("number");
|
|
258
|
+
}
|
|
259
|
+
});
|
|
260
|
+
it("shows lower Co2 for green domains", () => {
|
|
261
|
+
const pages = pagexray.convert(har);
|
|
262
|
+
const pageXrayRun = pages[0];
|
|
263
|
+
|
|
264
|
+
const greenDomains = [
|
|
265
|
+
"www.thegreenwebfoundation.org",
|
|
266
|
+
"fonts.googleapis.com",
|
|
267
|
+
"ajax.googleapis.com",
|
|
268
|
+
"assets.digitalclimatestrike.net",
|
|
269
|
+
"cdnjs.cloudflare.com",
|
|
270
|
+
"graphite.thegreenwebfoundation.org",
|
|
271
|
+
"analytics.thegreenwebfoundation.org",
|
|
272
|
+
"fonts.gstatic.com",
|
|
273
|
+
"api.thegreenwebfoundation.org",
|
|
274
|
+
];
|
|
275
|
+
const res = co2.perDomain(pageXrayRun);
|
|
276
|
+
const resWithGreen = co2.perDomain(pageXrayRun, greenDomains);
|
|
277
|
+
|
|
278
|
+
for (let obj of res) {
|
|
279
|
+
expect(typeof obj.co2).toBe("number");
|
|
280
|
+
}
|
|
281
|
+
for (let obj of greenDomains) {
|
|
282
|
+
let index = 0;
|
|
283
|
+
expect(resWithGreen[index].co2).toBeLessThan(res[index].co2);
|
|
284
|
+
index++;
|
|
285
|
+
}
|
|
286
|
+
});
|
|
132
287
|
});
|
|
133
288
|
});
|
|
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
289
|
});
|
package/src/hosting-api.test.js
CHANGED
|
@@ -3,9 +3,9 @@
|
|
|
3
3
|
const hosting = require("./hosting-api");
|
|
4
4
|
const nock = require("nock");
|
|
5
5
|
|
|
6
|
-
describe("hostingAPI",
|
|
7
|
-
describe("checking a single domain with #check",
|
|
8
|
-
it("using the API", async
|
|
6
|
+
describe("hostingAPI", () => {
|
|
7
|
+
describe("checking a single domain with #check", () => {
|
|
8
|
+
it("using the API", async () => {
|
|
9
9
|
const scope = nock("https://api.thegreenwebfoundation.org/")
|
|
10
10
|
.get("/greencheck/google.com")
|
|
11
11
|
.reply(200, {
|
|
@@ -16,8 +16,8 @@ describe("hostingAPI", function () {
|
|
|
16
16
|
expect(res).toEqual(true);
|
|
17
17
|
});
|
|
18
18
|
});
|
|
19
|
-
describe("implicitly checking multiple domains with #check",
|
|
20
|
-
it("using the API", async
|
|
19
|
+
describe("implicitly checking multiple domains with #check", () => {
|
|
20
|
+
it("using the API", async () => {
|
|
21
21
|
const scope = nock("https://api.thegreenwebfoundation.org/")
|
|
22
22
|
.get("/v2/greencheckmulti/[%22google.com%22,%22kochindustries.com%22]")
|
|
23
23
|
.reply(200, {
|
|
@@ -12,15 +12,15 @@ const dbPath = path.resolve(
|
|
|
12
12
|
"url2green.test.db"
|
|
13
13
|
);
|
|
14
14
|
|
|
15
|
-
describe("hostingDatabase",
|
|
16
|
-
describe("checking a single domain with #check",
|
|
17
|
-
test("tries to use a local database if available ", async
|
|
15
|
+
describe("hostingDatabase", () => {
|
|
16
|
+
describe("checking a single domain with #check", () => {
|
|
17
|
+
test("tries to use a local database if available ", async () => {
|
|
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",
|
|
23
|
-
test("tries to use a local database if available", async
|
|
22
|
+
describe("implicitly checking multiple domains with #check", () => {
|
|
23
|
+
test("tries to use a local database if available", async () => {
|
|
24
24
|
const res = await hosting.check(
|
|
25
25
|
["google.com", "kochindustries.com"],
|
|
26
26
|
dbPath
|
package/src/hosting-json.js
CHANGED
|
@@ -3,11 +3,31 @@
|
|
|
3
3
|
const log = require("debug")("tgwf:hostingCache");
|
|
4
4
|
const path = require("path");
|
|
5
5
|
const fs = require("fs");
|
|
6
|
+
const zlib = require("zlib");
|
|
6
7
|
const { promisify } = require("util");
|
|
7
8
|
const readFile = promisify(fs.readFile);
|
|
9
|
+
const gunzip = promisify(zlib.gunzip);
|
|
10
|
+
|
|
11
|
+
async function streamToString(stream) {
|
|
12
|
+
return new Promise((resolve, reject) => {
|
|
13
|
+
const chunks = [];
|
|
14
|
+
stream.on("error", reject);
|
|
15
|
+
stream.on("data", (chunk) => chunks.push(chunk));
|
|
16
|
+
stream.on("end", () => resolve(Buffer.concat(chunks)));
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
async function getGzippedFileAsJson(jsonPath) {
|
|
21
|
+
const readStream = fs.createReadStream(jsonPath);
|
|
22
|
+
const text = await streamToString(readStream);
|
|
23
|
+
const unzipped = await gunzip(text);
|
|
24
|
+
return unzipped.toString();
|
|
25
|
+
}
|
|
8
26
|
|
|
9
27
|
async function loadJSON(jsonPath) {
|
|
10
|
-
const jsonBuffer =
|
|
28
|
+
const jsonBuffer = jsonPath.endsWith(".gz")
|
|
29
|
+
? await getGzippedFileAsJson(jsonPath)
|
|
30
|
+
: await readFile(jsonPath);
|
|
11
31
|
return JSON.parse(jsonBuffer);
|
|
12
32
|
}
|
|
13
33
|
|
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",
|
|
6
|
+
describe("hostingJSON", () => {
|
|
7
7
|
const jsonPath = path.resolve(
|
|
8
8
|
__dirname,
|
|
9
9
|
"..",
|
|
@@ -11,16 +11,29 @@ describe("hostingJSON", function () {
|
|
|
11
11
|
"fixtures",
|
|
12
12
|
"url2green.test.json"
|
|
13
13
|
);
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
14
|
+
const jsonPathGz = path.resolve(
|
|
15
|
+
__dirname,
|
|
16
|
+
"..",
|
|
17
|
+
"data",
|
|
18
|
+
"fixtures",
|
|
19
|
+
"url2green.test.json.gz"
|
|
20
|
+
);
|
|
21
|
+
describe("checking a single domain with #check", () => {
|
|
22
|
+
test("against the list of domains as JSON", async () => {
|
|
17
23
|
const db = await hosting.loadJSON(jsonPath);
|
|
18
24
|
const res = await hosting.check("google.com", db);
|
|
19
25
|
expect(res).toEqual(true);
|
|
20
26
|
});
|
|
21
27
|
});
|
|
22
|
-
describe("
|
|
23
|
-
test("against the list of domains as JSON", async
|
|
28
|
+
describe("checking a single domain with #check", () => {
|
|
29
|
+
test("against the list of domains as JSON loaded from a gzipped JSON", async () => {
|
|
30
|
+
const db = await hosting.loadJSON(jsonPathGz);
|
|
31
|
+
const res = await hosting.check("google.com", db);
|
|
32
|
+
expect(res).toEqual(true);
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
describe("implicitly checking multiple domains with #check", () => {
|
|
36
|
+
test("against the list of domains as JSON", async () => {
|
|
24
37
|
const db = await hosting.loadJSON(jsonPath);
|
|
25
38
|
const domains = ["google.com", "kochindustries.com"];
|
|
26
39
|
|
package/src/hosting.test.js
CHANGED
|
@@ -14,9 +14,9 @@ const jsonPath = path.resolve(
|
|
|
14
14
|
"url2green.test.json"
|
|
15
15
|
);
|
|
16
16
|
|
|
17
|
-
describe("hosting",
|
|
17
|
+
describe("hosting", () => {
|
|
18
18
|
let har;
|
|
19
|
-
beforeEach(
|
|
19
|
+
beforeEach(() => {
|
|
20
20
|
har = JSON.parse(
|
|
21
21
|
fs.readFileSync(
|
|
22
22
|
path.resolve(__dirname, "../data/fixtures/tgwf.har"),
|
|
@@ -24,8 +24,8 @@ describe("hosting", function () {
|
|
|
24
24
|
)
|
|
25
25
|
);
|
|
26
26
|
});
|
|
27
|
-
describe("checking all domains on a page object with #checkPage ",
|
|
28
|
-
it("it returns a list of green domains, when passed a page object", async
|
|
27
|
+
describe("checking all domains on a page object with #checkPage ", () => {
|
|
28
|
+
it("it returns a list of green domains, when passed a page object", async () => {
|
|
29
29
|
const pages = pagexray.convert(har);
|
|
30
30
|
const pageXrayRun = pages[0];
|
|
31
31
|
const db = await hosting.loadJSON(jsonPath);
|
|
@@ -53,23 +53,23 @@ describe("hosting", function () {
|
|
|
53
53
|
// 'it returns an empty list, when passed a page object with no green domains'
|
|
54
54
|
// );
|
|
55
55
|
});
|
|
56
|
-
describe("checking a single domain with #check",
|
|
57
|
-
it("use the API instead", async
|
|
56
|
+
describe("checking a single domain with #check", () => {
|
|
57
|
+
it("use the API instead", async () => {
|
|
58
58
|
const db = await hosting.loadJSON(jsonPath);
|
|
59
59
|
const res = await hosting.check("google.com", db);
|
|
60
60
|
expect(res).toEqual(true);
|
|
61
61
|
});
|
|
62
62
|
});
|
|
63
|
-
describe("implicitly checking multiple domains with #check",
|
|
64
|
-
it("Use the API", async
|
|
63
|
+
describe("implicitly checking multiple domains with #check", () => {
|
|
64
|
+
it("Use the API", async () => {
|
|
65
65
|
const db = await hosting.loadJSON(jsonPath);
|
|
66
66
|
|
|
67
67
|
const res = await hosting.check(["google.com", "kochindustries.com"], db);
|
|
68
68
|
expect(res).toContain("google.com");
|
|
69
69
|
});
|
|
70
70
|
});
|
|
71
|
-
describe("explicitly checking multiple domains with #checkMulti",
|
|
72
|
-
it("use the API", async
|
|
71
|
+
describe("explicitly checking multiple domains with #checkMulti", () => {
|
|
72
|
+
it("use the API", async () => {
|
|
73
73
|
const db = await hosting.loadJSON(jsonPath);
|
|
74
74
|
const res = await hosting.check(["google.com", "kochindustries.com"], db);
|
|
75
75
|
expect(res).toContain("google.com");
|