@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/hosting.test.js
CHANGED
|
@@ -1,39 +1,49 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
2
|
|
|
3
|
-
const fs = require(
|
|
4
|
-
const path = require(
|
|
3
|
+
const fs = require("fs");
|
|
4
|
+
const path = require("path");
|
|
5
5
|
|
|
6
|
-
const hosting = require(
|
|
7
|
-
const pagexray = require(
|
|
6
|
+
const hosting = require("./hosting");
|
|
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
19
|
beforeEach(function () {
|
|
12
|
-
har = JSON.parse(
|
|
13
|
-
.readFileSync(
|
|
20
|
+
har = JSON.parse(
|
|
21
|
+
fs.readFileSync(
|
|
22
|
+
path.resolve(__dirname, "../data/fixtures/tgwf.har"),
|
|
23
|
+
"utf8"
|
|
24
|
+
)
|
|
25
|
+
);
|
|
14
26
|
});
|
|
15
|
-
describe(
|
|
16
|
-
it(
|
|
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 () {
|
|
17
29
|
const pages = pagexray.convert(har);
|
|
18
30
|
const pageXrayRun = pages[0];
|
|
31
|
+
const db = await hosting.loadJSON(jsonPath);
|
|
32
|
+
const greenDomains = await hosting.checkPage(pageXrayRun, db);
|
|
19
33
|
|
|
20
|
-
|
|
21
|
-
const greenDomains = await hosting.checkPage(pageXrayRun);
|
|
22
|
-
|
|
23
|
-
expect(greenDomains)
|
|
24
|
-
.toHaveLength(10);
|
|
25
|
-
|
|
34
|
+
expect(greenDomains).toHaveLength(11);
|
|
26
35
|
const expectedGreendomains = [
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
36
|
+
"maxcdn.bootstrapcdn.com",
|
|
37
|
+
"thegreenwebfoundation.org",
|
|
38
|
+
"www.thegreenwebfoundation.org",
|
|
39
|
+
"fonts.googleapis.com",
|
|
40
|
+
"ajax.googleapis.com",
|
|
41
|
+
"assets.digitalclimatestrike.net",
|
|
42
|
+
"cdnjs.cloudflare.com",
|
|
43
|
+
"graphite.thegreenwebfoundation.org",
|
|
44
|
+
"analytics.thegreenwebfoundation.org",
|
|
45
|
+
"fonts.gstatic.com",
|
|
46
|
+
"api.thegreenwebfoundation.org",
|
|
37
47
|
];
|
|
38
48
|
greenDomains.forEach(function (dom) {
|
|
39
49
|
expect(expectedGreendomains).toContain(dom);
|
|
@@ -43,35 +53,26 @@ describe('hosting', function () {
|
|
|
43
53
|
// 'it returns an empty list, when passed a page object with no green domains'
|
|
44
54
|
// );
|
|
45
55
|
});
|
|
46
|
-
describe(
|
|
47
|
-
it("tries to use a local database", async function () {
|
|
48
|
-
const res = await hosting.check("google.com", path.resolve(__dirname, "..", "url2green.test.db"))
|
|
49
|
-
expect(res).toEqual(true)
|
|
50
|
-
})
|
|
56
|
+
describe("checking a single domain with #check", function () {
|
|
51
57
|
it("use the API instead", async function () {
|
|
52
|
-
const
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
})
|
|
57
|
-
describe(
|
|
58
|
-
it("tries to use a local database if available", async function () {
|
|
59
|
-
const res = await hosting.check(["google.com", "kochindustries.com"], path.resolve(__dirname, "..", "url2green.test.db"))
|
|
60
|
-
expect(res).toContain("google.com")
|
|
61
|
-
})
|
|
58
|
+
const db = await hosting.loadJSON(jsonPath);
|
|
59
|
+
const res = await hosting.check("google.com", db);
|
|
60
|
+
expect(res).toEqual(true);
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
describe("implicitly checking multiple domains with #check", function () {
|
|
62
64
|
it("Use the API", async function () {
|
|
63
|
-
const
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
expect(res).toContain("google.com")
|
|
71
|
-
})
|
|
65
|
+
const db = await hosting.loadJSON(jsonPath);
|
|
66
|
+
|
|
67
|
+
const res = await hosting.check(["google.com", "kochindustries.com"], db);
|
|
68
|
+
expect(res).toContain("google.com");
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
describe("explicitly checking multiple domains with #checkMulti", function () {
|
|
72
72
|
it("use the API", async function () {
|
|
73
|
-
const
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
});
|
|
73
|
+
const db = await hosting.loadJSON(jsonPath);
|
|
74
|
+
const res = await hosting.check(["google.com", "kochindustries.com"], db);
|
|
75
|
+
expect(res).toContain("google.com");
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
});
|
package/src/index.js
CHANGED
package/.travis.yml
DELETED
package/src/hostingAPI.test.js
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const hosting = require('./hostingAPI');
|
|
4
|
-
|
|
5
|
-
describe('hostingAPI', function () {
|
|
6
|
-
describe('checking a single domain with #check', function () {
|
|
7
|
-
it("using the API", async function () {
|
|
8
|
-
const res = await hosting.check("google.com")
|
|
9
|
-
expect(res).toEqual(true)
|
|
10
|
-
})
|
|
11
|
-
|
|
12
|
-
})
|
|
13
|
-
describe('implicitly checking multiple domains with #check', function () {
|
|
14
|
-
it("using the API", async function () {
|
|
15
|
-
const res = await hosting.check(["google.com", "kochindustries.com"])
|
|
16
|
-
expect(res).toContain("google.com")
|
|
17
|
-
})
|
|
18
|
-
})
|
|
19
|
-
});
|
package/src/hostingDatabase.js
DELETED
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const log = require('debug')('tgwf:hostingDatabase');
|
|
4
|
-
const Database = require('better-sqlite3');
|
|
5
|
-
|
|
6
|
-
function getQ(domains) {
|
|
7
|
-
const q = [];
|
|
8
|
-
for (let domain of domains) {
|
|
9
|
-
q.push('?')
|
|
10
|
-
}
|
|
11
|
-
return q.join(',');
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
function getDatabase(databaseFullPathAndName) {
|
|
15
|
-
log(`looking for db at ${databaseFullPathAndName}`)
|
|
16
|
-
return new Database(databaseFullPathAndName, { readonly: true, fileMustExist: true });
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
function check(domain, dbName) {
|
|
20
|
-
let db;
|
|
21
|
-
|
|
22
|
-
try {
|
|
23
|
-
db = getDatabase(dbName)
|
|
24
|
-
}
|
|
25
|
-
catch (SqliteError) {
|
|
26
|
-
log(`couldn't find SQlite database at path: ${dbName}`)
|
|
27
|
-
throw SqliteError
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
// is it a single domain or an array of them?
|
|
31
|
-
if (typeof domain === 'string') {
|
|
32
|
-
return checkInDB(domain, db)
|
|
33
|
-
}
|
|
34
|
-
else {
|
|
35
|
-
return checkDomainsInDB(domain, db)
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
function checkInDB(domain, db) {
|
|
40
|
-
try {
|
|
41
|
-
const stmt = db.prepare('SELECT * FROM green_presenting WHERE url = ?')
|
|
42
|
-
return !!stmt.get(domain).green
|
|
43
|
-
} finally {
|
|
44
|
-
if (db) {
|
|
45
|
-
db.close()
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
function greenDomainsFromResults(greenResults) {
|
|
52
|
-
const entries = Object.entries(greenResults);
|
|
53
|
-
let greenEntries = entries.filter(function ([key, val]) {
|
|
54
|
-
return val.green
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
return greenEntries.map(function ([key, val]) {
|
|
58
|
-
return val.url
|
|
59
|
-
});
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
function checkDomainsInDB(domains, db) {
|
|
63
|
-
|
|
64
|
-
try {
|
|
65
|
-
const stmt = db.prepare(`SELECT * FROM green_presenting WHERE url in (${getQ(domains)})`)
|
|
66
|
-
|
|
67
|
-
const res = stmt.all(domains)
|
|
68
|
-
|
|
69
|
-
return greenDomainsFromResults(res)
|
|
70
|
-
|
|
71
|
-
} finally {
|
|
72
|
-
if (db) {
|
|
73
|
-
db.close()
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
module.exports = {
|
|
79
|
-
check
|
|
80
|
-
};
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const hosting = require('./hostingDatabase');
|
|
4
|
-
const path = require('path');
|
|
5
|
-
|
|
6
|
-
describe('hostingDatabase', function () {
|
|
7
|
-
|
|
8
|
-
describe('checking a single domain with #check', function () {
|
|
9
|
-
it("tries to use a local database if available ", async function () {
|
|
10
|
-
const res = await hosting.check("google.com", path.resolve(__dirname, "..", "url2green.test.db"))
|
|
11
|
-
expect(res).toEqual(true)
|
|
12
|
-
})
|
|
13
|
-
})
|
|
14
|
-
describe('implicitly checking multiple domains with #check', function () {
|
|
15
|
-
it("tries to use a local database if available", async function () {
|
|
16
|
-
const res = await hosting.check(["google.com", "kochindustries.com"], path.resolve(__dirname, "..", "url2green.test.db"))
|
|
17
|
-
expect(res).toContain("google.com")
|
|
18
|
-
})
|
|
19
|
-
})
|
|
20
|
-
});
|
package/url2green.test.db
DELETED
|
Binary file
|