Skip to content

Commit 91c1123

Browse files
authored
Remove pagexray functions
2 parents 083e1e7 + 1c61b98 commit 91c1123

File tree

5 files changed

+9
-369
lines changed

5 files changed

+9
-369
lines changed

package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@
5656
"jest": "^28.1.0",
5757
"nock": "^13.2.4",
5858
"np": "^8.0.4",
59-
"pagexray": "^4.4.2",
6059
"prettier": "^2.6.2"
6160
},
6261
"jest": {
@@ -76,4 +75,4 @@
7675
"type": "git",
7776
"url": "https://github.com/thegreenwebfoundation/co2.js.git"
7877
}
79-
}
78+
}

src/co2.js

-102
Original file line numberDiff line numberDiff line change
@@ -208,108 +208,6 @@ class CO2 {
208208
);
209209
}
210210
}
211-
212-
perDomain(pageXray, greenDomains) {
213-
const co2PerDomain = [];
214-
for (let domain of Object.keys(pageXray.domains)) {
215-
let co2;
216-
if (greenDomains && greenDomains.indexOf(domain) > -1) {
217-
co2 = this.perByte(pageXray.domains[domain].transferSize, true);
218-
} else {
219-
co2 = this.perByte(pageXray.domains[domain].transferSize);
220-
}
221-
co2PerDomain.push({
222-
domain,
223-
co2,
224-
transferSize: pageXray.domains[domain].transferSize,
225-
});
226-
}
227-
co2PerDomain.sort((a, b) => b.co2 - a.co2);
228-
229-
return co2PerDomain;
230-
}
231-
232-
perPage(pageXray, green) {
233-
// Accept an xray object, and if we receive a boolean as the second
234-
// argument, we assume every request we make is sent to a server
235-
// running on renwewable power.
236-
237-
// if we receive an array of domains, return a number accounting the
238-
// reduced CO2 from green hosted domains
239-
240-
const domainCO2 = this.perDomain(pageXray, green);
241-
let totalCO2 = 0;
242-
for (let domain of domainCO2) {
243-
totalCO2 += domain.co2;
244-
}
245-
return totalCO2;
246-
}
247-
248-
perContentType(pageXray, greenDomains) {
249-
const co2PerContentType = {};
250-
for (let asset of pageXray.assets) {
251-
const domain = new URL(asset.url).domain;
252-
const transferSize = asset.transferSize;
253-
const co2ForTransfer = this.perByte(
254-
transferSize,
255-
greenDomains && greenDomains.indexOf(domain) > -1
256-
);
257-
const contentType = asset.type;
258-
if (!co2PerContentType[contentType]) {
259-
co2PerContentType[contentType] = { co2: 0, transferSize: 0 };
260-
}
261-
co2PerContentType[contentType].co2 += co2ForTransfer;
262-
co2PerContentType[contentType].transferSize += transferSize;
263-
}
264-
// restructure and sort
265-
const all = [];
266-
for (let type of Object.keys(co2PerContentType)) {
267-
all.push({
268-
type,
269-
co2: co2PerContentType[type].co2,
270-
transferSize: co2PerContentType[type].transferSize,
271-
});
272-
}
273-
all.sort((a, b) => b.co2 - a.co2);
274-
return all;
275-
}
276-
277-
dirtiestResources(pageXray, greenDomains) {
278-
const allAssets = [];
279-
for (let asset of pageXray.assets) {
280-
const domain = new URL(asset.url).domain;
281-
const transferSize = asset.transferSize;
282-
const co2ForTransfer = this.perByte(
283-
transferSize,
284-
greenDomains && greenDomains.indexOf(domain) > -1
285-
);
286-
allAssets.push({ url: asset.url, co2: co2ForTransfer, transferSize });
287-
}
288-
allAssets.sort((a, b) => b.co2 - a.co2);
289-
290-
return allAssets.slice(0, allAssets.length > 10 ? 10 : allAssets.length);
291-
}
292-
293-
perParty(pageXray, greenDomains) {
294-
let firstParty = 0;
295-
let thirdParty = 0;
296-
// calculate co2 per first/third party
297-
const firstPartyRegEx = pageXray.firstPartyRegEx;
298-
for (let d of Object.keys(pageXray.domains)) {
299-
if (!d.match(firstPartyRegEx)) {
300-
thirdParty += this.perByte(
301-
pageXray.domains[d].transferSize,
302-
greenDomains && greenDomains.indexOf(d) > -1
303-
);
304-
} else {
305-
firstParty += this.perByte(
306-
pageXray.domains[d].transferSize,
307-
greenDomains && greenDomains.indexOf(d) > -1
308-
);
309-
}
310-
}
311-
return { firstParty, thirdParty };
312-
}
313211
}
314212

315213
export { CO2 };

src/co2.test.js

+3-231
Original file line numberDiff line numberDiff line change
@@ -1,154 +1,25 @@
11
"use strict";
22

3-
import fs from "fs";
4-
import path from "path";
5-
import { MILLION, ONEBYTE, SWD } from "./constants/test-constants.js";
6-
7-
import pagexray from "pagexray";
3+
import { MILLION, SWD } from "./constants/test-constants.js";
84

95
import CO2 from "./co2.js";
106
import { averageIntensity, marginalIntensity } from "./index.js";
117

128
const TwnGridIntensityValue = averageIntensity.data["TWN"];
139

1410
describe("co2", () => {
15-
let har, co2;
16-
describe("1 byte model", () => {
17-
const { TGWF_GREY_VALUE, TGWF_GREEN_VALUE, TGWF_MIXED_VALUE } = ONEBYTE;
18-
19-
beforeEach(() => {
20-
co2 = new CO2({ model: "1byte" });
21-
har = JSON.parse(
22-
fs.readFileSync(
23-
path.resolve(__dirname, "../data/fixtures/tgwf.har"),
24-
"utf8"
25-
)
26-
);
27-
});
28-
29-
describe("perPage", () => {
30-
it("returns CO2 for total transfer for page", () => {
31-
const pages = pagexray.convert(har);
32-
const pageXrayRun = pages[0];
33-
34-
expect(co2.perPage(pageXrayRun).toFixed(5)).toBe(
35-
TGWF_GREY_VALUE.toFixed(5)
36-
);
37-
});
38-
it("returns lower CO2 for page served from green site", () => {
39-
const pages = pagexray.convert(har);
40-
const pageXrayRun = pages[0];
41-
let green = [
42-
"www.thegreenwebfoundation.org",
43-
"fonts.googleapis.com",
44-
"ajax.googleapis.com",
45-
"assets.digitalclimatestrike.net",
46-
"cdnjs.cloudflare.com",
47-
"graphite.thegreenwebfoundation.org",
48-
"analytics.thegreenwebfoundation.org",
49-
"fonts.gstatic.com",
50-
"api.thegreenwebfoundation.org",
51-
];
52-
expect(co2.perPage(pageXrayRun, green)).toBeLessThan(TGWF_GREY_VALUE);
53-
});
54-
it("returns a lower CO2 number where *some* domains use green power", () => {
55-
const pages = pagexray.convert(har);
56-
const pageXrayRun = pages[0];
57-
// green can be true, or a array containing entries
58-
let green = [
59-
"www.thegreenwebfoundation.org",
60-
"fonts.googleapis.com",
61-
"ajax.googleapis.com",
62-
"assets.digitalclimatestrike.net",
63-
"cdnjs.cloudflare.com",
64-
"graphite.thegreenwebfoundation.org",
65-
"analytics.thegreenwebfoundation.org",
66-
"fonts.gstatic.com",
67-
"api.thegreenwebfoundation.org",
68-
];
69-
expect(co2.perPage(pageXrayRun, green).toFixed(5)).toBe(
70-
TGWF_MIXED_VALUE.toFixed(5)
71-
);
72-
});
73-
});
74-
describe("perDomain", () => {
75-
it("shows object listing Co2 for each domain", () => {
76-
const pages = pagexray.convert(har);
77-
const pageXrayRun = pages[0];
78-
const res = co2.perDomain(pageXrayRun);
79-
80-
const domains = [
81-
"thegreenwebfoundation.org",
82-
"www.thegreenwebfoundation.org",
83-
"maxcdn.bootstrapcdn.com",
84-
"fonts.googleapis.com",
85-
"ajax.googleapis.com",
86-
"assets.digitalclimatestrike.net",
87-
"cdnjs.cloudflare.com",
88-
"graphite.thegreenwebfoundation.org",
89-
"analytics.thegreenwebfoundation.org",
90-
"fonts.gstatic.com",
91-
"api.thegreenwebfoundation.org",
92-
];
93-
94-
for (let obj of res) {
95-
expect(domains.indexOf(obj.domain)).toBeGreaterThan(-1);
96-
expect(typeof obj.co2).toBe("number");
97-
}
98-
});
99-
it("shows lower Co2 for green domains", () => {
100-
const pages = pagexray.convert(har);
101-
const pageXrayRun = pages[0];
102-
103-
const greenDomains = [
104-
"www.thegreenwebfoundation.org",
105-
"fonts.googleapis.com",
106-
"ajax.googleapis.com",
107-
"assets.digitalclimatestrike.net",
108-
"cdnjs.cloudflare.com",
109-
"graphite.thegreenwebfoundation.org",
110-
"analytics.thegreenwebfoundation.org",
111-
"fonts.gstatic.com",
112-
"api.thegreenwebfoundation.org",
113-
];
114-
const res = co2.perDomain(pageXrayRun);
115-
const resWithGreen = co2.perDomain(pageXrayRun, greenDomains);
116-
117-
for (let obj of res) {
118-
expect(typeof obj.co2).toBe("number");
119-
}
120-
for (let obj of greenDomains) {
121-
let index = 0;
122-
expect(resWithGreen[index].co2).toBeLessThan(res[index].co2);
123-
index++;
124-
}
125-
});
126-
});
127-
});
11+
let co2;
12812

12913
describe("Sustainable Web Design model as simple option", () => {
13014
// the SWD model should have slightly higher values as
13115
// we include more of the system in calculations for the
13216
// same levels of data transfer
13317

134-
const {
135-
MILLION_GREEN,
136-
MILLION_GREY,
137-
TGWF_GREY_VALUE,
138-
TGWF_MIXED_VALUE,
139-
MILLION_PERVISIT_GREY,
140-
MILLION_PERVISIT_GREEN,
141-
} = SWD;
18+
const { MILLION_PERVISIT_GREY, MILLION_PERVISIT_GREEN } = SWD;
14219

14320
// We're not passing in a model parameter here to check that SWD is used by default
14421
beforeEach(() => {
14522
co2 = new CO2();
146-
har = JSON.parse(
147-
fs.readFileSync(
148-
path.resolve(__dirname, "../data/fixtures/tgwf.har"),
149-
"utf8"
150-
)
151-
);
15223
});
15324

15425
describe("perVisit", () => {
@@ -173,105 +44,6 @@ describe("co2", () => {
17344
});
17445
});
17546

176-
describe("perPage", () => {
177-
it("returns CO2 for total transfer for page", () => {
178-
const pages = pagexray.convert(har);
179-
const pageXrayRun = pages[0];
180-
181-
expect(parseFloat(co2.perPage(pageXrayRun).toFixed(5))).toBeCloseTo(
182-
parseFloat(TGWF_GREY_VALUE.toFixed(5)),
183-
3
184-
);
185-
});
186-
it("returns lower CO2 for page served from green site", () => {
187-
const pages = pagexray.convert(har);
188-
const pageXrayRun = pages[0];
189-
let green = [
190-
"www.thegreenwebfoundation.org",
191-
"fonts.googleapis.com",
192-
"ajax.googleapis.com",
193-
"assets.digitalclimatestrike.net",
194-
"cdnjs.cloudflare.com",
195-
"graphite.thegreenwebfoundation.org",
196-
"analytics.thegreenwebfoundation.org",
197-
"fonts.gstatic.com",
198-
"api.thegreenwebfoundation.org",
199-
];
200-
expect(co2.perPage(pageXrayRun, green)).toBeLessThan(TGWF_GREY_VALUE);
201-
});
202-
it("returns a lower CO2 number where *some* domains use green power", () => {
203-
const pages = pagexray.convert(har);
204-
const pageXrayRun = pages[0];
205-
// green can be true, or a array containing entries
206-
let green = [
207-
"www.thegreenwebfoundation.org",
208-
"fonts.googleapis.com",
209-
"ajax.googleapis.com",
210-
"assets.digitalclimatestrike.net",
211-
"cdnjs.cloudflare.com",
212-
"graphite.thegreenwebfoundation.org",
213-
"analytics.thegreenwebfoundation.org",
214-
"fonts.gstatic.com",
215-
"api.thegreenwebfoundation.org",
216-
];
217-
expect(
218-
parseFloat(co2.perPage(pageXrayRun, green).toFixed(5))
219-
).toBeCloseTo(parseFloat(TGWF_MIXED_VALUE.toFixed(5)), 3);
220-
});
221-
});
222-
describe("perDomain", () => {
223-
it("shows object listing Co2 for each domain", () => {
224-
const pages = pagexray.convert(har);
225-
const pageXrayRun = pages[0];
226-
const res = co2.perDomain(pageXrayRun);
227-
228-
const domains = [
229-
"thegreenwebfoundation.org",
230-
"www.thegreenwebfoundation.org",
231-
"maxcdn.bootstrapcdn.com",
232-
"fonts.googleapis.com",
233-
"ajax.googleapis.com",
234-
"assets.digitalclimatestrike.net",
235-
"cdnjs.cloudflare.com",
236-
"graphite.thegreenwebfoundation.org",
237-
"analytics.thegreenwebfoundation.org",
238-
"fonts.gstatic.com",
239-
"api.thegreenwebfoundation.org",
240-
];
241-
242-
for (let obj of res) {
243-
expect(domains.indexOf(obj.domain)).toBeGreaterThan(-1);
244-
expect(typeof obj.co2).toBe("number");
245-
}
246-
});
247-
it("shows lower Co2 for green domains", () => {
248-
const pages = pagexray.convert(har);
249-
const pageXrayRun = pages[0];
250-
251-
const greenDomains = [
252-
"www.thegreenwebfoundation.org",
253-
"fonts.googleapis.com",
254-
"ajax.googleapis.com",
255-
"assets.digitalclimatestrike.net",
256-
"cdnjs.cloudflare.com",
257-
"graphite.thegreenwebfoundation.org",
258-
"analytics.thegreenwebfoundation.org",
259-
"fonts.gstatic.com",
260-
"api.thegreenwebfoundation.org",
261-
];
262-
const res = co2.perDomain(pageXrayRun);
263-
const resWithGreen = co2.perDomain(pageXrayRun, greenDomains);
264-
265-
for (let obj of res) {
266-
expect(typeof obj.co2).toBe("number");
267-
}
268-
for (let obj of greenDomains) {
269-
let index = 0;
270-
expect(resWithGreen[index].co2).toBeLessThan(res[index].co2);
271-
index++;
272-
}
273-
});
274-
});
27547
describe("Returning results by segment", () => {
27648
const {
27749
MILLION_GREY,

0 commit comments

Comments
 (0)