@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.
@@ -1,39 +1,49 @@
1
- 'use strict';
1
+ "use strict";
2
2
 
3
- const fs = require('fs');
4
- const path = require('path');
3
+ const fs = require("fs");
4
+ const path = require("path");
5
5
 
6
- const hosting = require('./hosting');
7
- const pagexray = require('pagexray');
6
+ const hosting = require("./hosting");
7
+ const pagexray = require("pagexray");
8
8
 
9
- describe('hosting', function () {
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(fs
13
- .readFileSync(path.resolve(__dirname, '../data/fixtures/tgwf.har'), 'utf8'))
20
+ har = JSON.parse(
21
+ fs.readFileSync(
22
+ path.resolve(__dirname, "../data/fixtures/tgwf.har"),
23
+ "utf8"
24
+ )
25
+ );
14
26
  });
15
- describe('checking all domains on a page object with #checkPage ', function () {
16
- 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 () {
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
- // TODO find a way to not hit the API each time
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
- 'thegreenwebfoundation.org',
28
- 'www.thegreenwebfoundation.org',
29
- 'fonts.googleapis.com',
30
- 'ajax.googleapis.com',
31
- 'assets.digitalclimatestrike.net',
32
- 'cdnjs.cloudflare.com',
33
- 'graphite.thegreenwebfoundation.org',
34
- 'analytics.thegreenwebfoundation.org',
35
- 'fonts.gstatic.com',
36
- 'api.thegreenwebfoundation.org'
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('checking a single domain with #check', function () {
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 res = await hosting.check("google.com")
53
- expect(res).toEqual(true)
54
- })
55
-
56
- })
57
- describe('implicitly checking multiple domains with #check', function () {
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 res = await hosting.check(["google.com", "kochindustries.com"])
64
- expect(res).toContain("google.com")
65
- })
66
- })
67
- describe('explicitly checking multiple domains with #checkMulti', function () {
68
- it("tries to use a local database if available", async function () {
69
- const res = await hosting.check(["google.com", "kochindustries.com"], path.resolve(__dirname, "..", "url2green.test.db"))
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 res = await hosting.check(["google.com", "kochindustries.com"])
74
- expect(res).toContain("google.com")
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
@@ -1,7 +1,7 @@
1
- const co2 = require("./co2")
2
- const hosting = require("./hosting")
1
+ const co2 = require("./co2");
2
+ const hosting = require("./hosting");
3
3
 
4
4
  module.exports = {
5
5
  co2,
6
- hosting
7
- }
6
+ hosting,
7
+ };
package/.travis.yml DELETED
@@ -1,11 +0,0 @@
1
- language: node_js
2
- node_js: 10
3
- matrix:
4
- include:
5
- - name: "Test on Linux"
6
- dist: bionic
7
- - name: "Test on OS X"
8
- os: osx
9
- osx_image: xcode11.3
10
- - name: "Test on Windows"
11
- os: windows
@@ -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
- });
@@ -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