@tgwf/co2 0.9.0 → 0.10.2

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.
Files changed (92) hide show
  1. package/.esbuild.browser.js +11 -0
  2. package/.esbuild.esm.js +25 -0
  3. package/.esbuild.node.js +16 -0
  4. package/.gitpod.yml +8 -0
  5. package/CHANGELOG.md +22 -1
  6. package/README.md +29 -66
  7. package/dist/cjs/1byte.js +52 -0
  8. package/dist/cjs/1byte.js.map +7 -0
  9. package/dist/cjs/1byte.test.js +27 -0
  10. package/dist/cjs/1byte.test.js.map +7 -0
  11. package/dist/cjs/co2.js +121 -0
  12. package/dist/cjs/co2.js.map +7 -0
  13. package/dist/cjs/co2.test.js +258 -0
  14. package/dist/cjs/co2.test.js.map +7 -0
  15. package/dist/cjs/constants/file-size.js +27 -0
  16. package/dist/cjs/constants/file-size.js.map +7 -0
  17. package/dist/cjs/constants/index.js +27 -0
  18. package/dist/cjs/constants/index.js.map +7 -0
  19. package/dist/cjs/helpers/index.js +24 -0
  20. package/dist/cjs/helpers/index.js.map +7 -0
  21. package/dist/cjs/hosting-api.js +64 -0
  22. package/dist/cjs/hosting-api.js.map +7 -0
  23. package/dist/cjs/hosting-api.test.js +47 -0
  24. package/dist/cjs/hosting-api.test.js.map +7 -0
  25. package/dist/cjs/hosting-database.node.test.js +36 -0
  26. package/dist/cjs/hosting-database.node.test.js.map +7 -0
  27. package/dist/cjs/hosting-json.node.js +73 -0
  28. package/dist/cjs/hosting-json.node.js.map +7 -0
  29. package/dist/cjs/hosting-json.node.test.js +43 -0
  30. package/dist/cjs/hosting-json.node.test.js.map +7 -0
  31. package/dist/cjs/hosting-node.js +78 -0
  32. package/dist/cjs/hosting-node.js.map +7 -0
  33. package/dist/cjs/hosting.js +36 -0
  34. package/dist/cjs/hosting.js.map +7 -0
  35. package/dist/cjs/hosting.test.js +74 -0
  36. package/dist/cjs/hosting.test.js.map +7 -0
  37. package/dist/cjs/index-node.js +29 -0
  38. package/dist/cjs/index-node.js.map +7 -0
  39. package/dist/cjs/index.js +31 -0
  40. package/dist/cjs/index.js.map +7 -0
  41. package/dist/cjs/package.json +3 -0
  42. package/dist/cjs/sustainable-web-design.js +134 -0
  43. package/dist/cjs/sustainable-web-design.js.map +7 -0
  44. package/dist/cjs/sustainable-web-design.test.js +79 -0
  45. package/dist/cjs/sustainable-web-design.test.js.map +7 -0
  46. package/dist/esm/1byte.js +32 -0
  47. package/dist/esm/1byte.test.js +11 -0
  48. package/dist/esm/co2.js +98 -0
  49. package/{src → dist/esm}/co2.test.js +47 -94
  50. package/dist/esm/constants/file-size.js +7 -0
  51. package/dist/esm/constants/index.js +4 -0
  52. package/dist/esm/helpers/index.js +4 -0
  53. package/dist/esm/hosting-api.js +41 -0
  54. package/dist/esm/hosting-api.test.js +31 -0
  55. package/{src/hosting-database.test.js → dist/esm/hosting-database.node.test.js} +7 -18
  56. package/{src/hosting-json.test.js → dist/esm/hosting-json.node.test.js} +4 -21
  57. package/dist/esm/hosting.js +13 -0
  58. package/{src → dist/esm}/hosting.test.js +10 -30
  59. package/dist/esm/index.js +8 -0
  60. package/dist/esm/package.json +3 -0
  61. package/dist/esm/sustainable-web-design.js +111 -0
  62. package/{src → dist/esm}/sustainable-web-design.test.js +19 -37
  63. package/dist/iife/index.js +2 -0
  64. package/dist/iife/index.js.map +7 -0
  65. package/fixup +19 -0
  66. package/package.json +38 -17
  67. package/public/index.html +58 -0
  68. package/public/index.js +2 -0
  69. package/src/1byte.js +2 -3
  70. package/src/co2.js +17 -18
  71. package/src/constants/file-size.js +2 -2
  72. package/src/constants/index.js +2 -2
  73. package/src/helpers/index.js +1 -3
  74. package/src/hosting-api.js +23 -43
  75. package/src/{hosting-json.js → hosting-json.node.js} +9 -11
  76. package/src/hosting-node.js +94 -0
  77. package/src/hosting.js +6 -28
  78. package/src/index-node.js +4 -0
  79. package/src/index.js +4 -6
  80. package/src/sustainable-web-design.js +37 -11
  81. package/.eslintrc.json +0 -21
  82. package/.github/workflows/unittests.yml +0 -26
  83. package/data/Lean-ICT-Materials-1byte-Model-2018.xlsx +0 -0
  84. package/data/Lean-ICT-Materials-Forecast-Model-2018.xlsx +0 -0
  85. package/data/fixtures/tgwf.har +0 -6107
  86. package/data/fixtures/url2green.test.db +0 -0
  87. package/data/fixtures/url2green.test.json +0 -1
  88. package/data/fixtures/url2green.test.json.gz +0 -0
  89. package/images/swd-energy-usage.png +0 -0
  90. package/src/1byte.test.js +0 -17
  91. package/src/green-byte.js +0 -26
  92. package/src/hosting-api.test.js +0 -37
@@ -0,0 +1,258 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __copyProps = (to, from, except, desc) => {
9
+ if (from && typeof from === "object" || typeof from === "function") {
10
+ for (let key of __getOwnPropNames(from))
11
+ if (!__hasOwnProp.call(to, key) && key !== except)
12
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
13
+ }
14
+ return to;
15
+ };
16
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod));
17
+ var import_fs = __toESM(require("fs"));
18
+ var import_path = __toESM(require("path"));
19
+ var import_pagexray = __toESM(require("pagexray"));
20
+ var import_co2 = __toESM(require("./co2.js"));
21
+ var import_sustainable_web_design = __toESM(require("./sustainable-web-design.js"));
22
+ describe("co2", () => {
23
+ let har, co2;
24
+ describe("1 byte model", () => {
25
+ const TGWF_GREY_VALUE = 0.20497;
26
+ const TGWF_GREEN_VALUE = 0.54704;
27
+ const TGWF_MIXED_VALUE = 0.16718;
28
+ const MILLION = 1e6;
29
+ const MILLION_GREY = 0.29081;
30
+ const MILLION_GREEN = 0.23196;
31
+ beforeEach(() => {
32
+ co2 = new import_co2.default();
33
+ har = JSON.parse(import_fs.default.readFileSync(import_path.default.resolve(__dirname, "../data/fixtures/tgwf.har"), "utf8"));
34
+ });
35
+ describe("perByte", () => {
36
+ it("returns a CO2 number for data transfer using 'grey' power", () => {
37
+ expect(co2.perByte(MILLION).toPrecision(5)).toBe(MILLION_GREY.toPrecision(5));
38
+ });
39
+ it("returns a lower CO2 number for data transfer from domains using entirely 'green' power", () => {
40
+ expect(co2.perByte(MILLION).toPrecision(5)).toBe(MILLION_GREY.toPrecision(5));
41
+ expect(co2.perByte(MILLION, true).toPrecision(5)).toBe(MILLION_GREEN.toPrecision(5));
42
+ });
43
+ });
44
+ describe("perPage", () => {
45
+ it("returns CO2 for total transfer for page", () => {
46
+ const pages = import_pagexray.default.convert(har);
47
+ const pageXrayRun = pages[0];
48
+ expect(co2.perPage(pageXrayRun).toPrecision(5)).toBe(TGWF_GREY_VALUE.toPrecision(5));
49
+ });
50
+ it("returns lower CO2 for page served from green site", () => {
51
+ const pages = import_pagexray.default.convert(har);
52
+ const pageXrayRun = pages[0];
53
+ let green = [
54
+ "www.thegreenwebfoundation.org",
55
+ "fonts.googleapis.com",
56
+ "ajax.googleapis.com",
57
+ "assets.digitalclimatestrike.net",
58
+ "cdnjs.cloudflare.com",
59
+ "graphite.thegreenwebfoundation.org",
60
+ "analytics.thegreenwebfoundation.org",
61
+ "fonts.gstatic.com",
62
+ "api.thegreenwebfoundation.org"
63
+ ];
64
+ expect(co2.perPage(pageXrayRun, green)).toBeLessThan(TGWF_GREY_VALUE);
65
+ });
66
+ it("returns a lower CO2 number where *some* domains use green power", () => {
67
+ const pages = import_pagexray.default.convert(har);
68
+ const pageXrayRun = pages[0];
69
+ let green = [
70
+ "www.thegreenwebfoundation.org",
71
+ "fonts.googleapis.com",
72
+ "ajax.googleapis.com",
73
+ "assets.digitalclimatestrike.net",
74
+ "cdnjs.cloudflare.com",
75
+ "graphite.thegreenwebfoundation.org",
76
+ "analytics.thegreenwebfoundation.org",
77
+ "fonts.gstatic.com",
78
+ "api.thegreenwebfoundation.org"
79
+ ];
80
+ expect(co2.perPage(pageXrayRun, green).toPrecision(5)).toBe(TGWF_MIXED_VALUE.toPrecision(5));
81
+ });
82
+ });
83
+ describe("perDomain", () => {
84
+ it("shows object listing Co2 for each domain", () => {
85
+ const pages = import_pagexray.default.convert(har);
86
+ const pageXrayRun = pages[0];
87
+ const res = co2.perDomain(pageXrayRun);
88
+ const domains = [
89
+ "thegreenwebfoundation.org",
90
+ "www.thegreenwebfoundation.org",
91
+ "maxcdn.bootstrapcdn.com",
92
+ "fonts.googleapis.com",
93
+ "ajax.googleapis.com",
94
+ "assets.digitalclimatestrike.net",
95
+ "cdnjs.cloudflare.com",
96
+ "graphite.thegreenwebfoundation.org",
97
+ "analytics.thegreenwebfoundation.org",
98
+ "fonts.gstatic.com",
99
+ "api.thegreenwebfoundation.org"
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", () => {
107
+ const pages = import_pagexray.default.convert(har);
108
+ const pageXrayRun = pages[0];
109
+ const greenDomains = [
110
+ "www.thegreenwebfoundation.org",
111
+ "fonts.googleapis.com",
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
+ for (let obj of res) {
123
+ expect(typeof obj.co2).toBe("number");
124
+ }
125
+ for (let obj of greenDomains) {
126
+ let index = 0;
127
+ expect(resWithGreen[index].co2).toBeLessThan(res[index].co2);
128
+ index++;
129
+ }
130
+ });
131
+ });
132
+ });
133
+ describe("Sustainable Web Design model as simple option", () => {
134
+ const MILLION = 1e6;
135
+ const MILLION_GREY = 0.35802;
136
+ const MILLION_GREEN = 0.31039;
137
+ const TGWF_GREY_VALUE = 0.25234;
138
+ const TGWF_GREEN_VALUE = 0.54704;
139
+ const TGWF_MIXED_VALUE = 0.22175;
140
+ beforeEach(() => {
141
+ co2 = new import_co2.default({ model: "swd" });
142
+ har = JSON.parse(import_fs.default.readFileSync(import_path.default.resolve(__dirname, "../data/fixtures/tgwf.har"), "utf8"));
143
+ });
144
+ describe("perByte", () => {
145
+ it("returns a CO2 number for data transfer", () => {
146
+ co2.perByte(MILLION);
147
+ expect(co2.perByte(MILLION).toPrecision(5)).toBe(MILLION_GREY.toPrecision(5));
148
+ });
149
+ it("returns a lower CO2 number for data transfer from domains using entirely 'green' power", () => {
150
+ expect(co2.perByte(MILLION, false).toPrecision(5)).toBe(MILLION_GREY.toPrecision(5));
151
+ expect(co2.perByte(MILLION, true).toPrecision(5)).toBe(MILLION_GREEN.toPrecision(5));
152
+ });
153
+ });
154
+ describe("perPage", () => {
155
+ it("returns CO2 for total transfer for page", () => {
156
+ const pages = import_pagexray.default.convert(har);
157
+ const pageXrayRun = pages[0];
158
+ expect(co2.perPage(pageXrayRun).toPrecision(5)).toBe(TGWF_GREY_VALUE.toPrecision(5));
159
+ });
160
+ it("returns lower CO2 for page served from green site", () => {
161
+ const pages = import_pagexray.default.convert(har);
162
+ const pageXrayRun = pages[0];
163
+ let green = [
164
+ "www.thegreenwebfoundation.org",
165
+ "fonts.googleapis.com",
166
+ "ajax.googleapis.com",
167
+ "assets.digitalclimatestrike.net",
168
+ "cdnjs.cloudflare.com",
169
+ "graphite.thegreenwebfoundation.org",
170
+ "analytics.thegreenwebfoundation.org",
171
+ "fonts.gstatic.com",
172
+ "api.thegreenwebfoundation.org"
173
+ ];
174
+ expect(co2.perPage(pageXrayRun, green)).toBeLessThan(TGWF_GREY_VALUE);
175
+ });
176
+ it("returns a lower CO2 number where *some* domains use green power", () => {
177
+ const pages = import_pagexray.default.convert(har);
178
+ const pageXrayRun = pages[0];
179
+ let green = [
180
+ "www.thegreenwebfoundation.org",
181
+ "fonts.googleapis.com",
182
+ "ajax.googleapis.com",
183
+ "assets.digitalclimatestrike.net",
184
+ "cdnjs.cloudflare.com",
185
+ "graphite.thegreenwebfoundation.org",
186
+ "analytics.thegreenwebfoundation.org",
187
+ "fonts.gstatic.com",
188
+ "api.thegreenwebfoundation.org"
189
+ ];
190
+ expect(co2.perPage(pageXrayRun, green).toPrecision(5)).toBe(TGWF_MIXED_VALUE.toPrecision(5));
191
+ });
192
+ });
193
+ describe("perDomain", () => {
194
+ it("shows object listing Co2 for each domain", () => {
195
+ const pages = import_pagexray.default.convert(har);
196
+ const pageXrayRun = pages[0];
197
+ const res = co2.perDomain(pageXrayRun);
198
+ const domains = [
199
+ "thegreenwebfoundation.org",
200
+ "www.thegreenwebfoundation.org",
201
+ "maxcdn.bootstrapcdn.com",
202
+ "fonts.googleapis.com",
203
+ "ajax.googleapis.com",
204
+ "assets.digitalclimatestrike.net",
205
+ "cdnjs.cloudflare.com",
206
+ "graphite.thegreenwebfoundation.org",
207
+ "analytics.thegreenwebfoundation.org",
208
+ "fonts.gstatic.com",
209
+ "api.thegreenwebfoundation.org"
210
+ ];
211
+ for (let obj of res) {
212
+ expect(domains.indexOf(obj.domain)).toBeGreaterThan(-1);
213
+ expect(typeof obj.co2).toBe("number");
214
+ }
215
+ });
216
+ it("shows lower Co2 for green domains", () => {
217
+ const pages = import_pagexray.default.convert(har);
218
+ const pageXrayRun = pages[0];
219
+ const greenDomains = [
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
+ const res = co2.perDomain(pageXrayRun);
231
+ const resWithGreen = co2.perDomain(pageXrayRun, greenDomains);
232
+ for (let obj of res) {
233
+ expect(typeof obj.co2).toBe("number");
234
+ }
235
+ for (let obj of greenDomains) {
236
+ let index = 0;
237
+ expect(resWithGreen[index].co2).toBeLessThan(res[index].co2);
238
+ index++;
239
+ }
240
+ });
241
+ });
242
+ });
243
+ describe("New model that implements the same API", () => {
244
+ const MILLION = 1e6;
245
+ const MILLION_GREY = 0.35802;
246
+ beforeEach(() => {
247
+ co2 = new import_co2.default({ model: import_sustainable_web_design.default });
248
+ har = JSON.parse(import_fs.default.readFileSync(import_path.default.resolve(__dirname, "../data/fixtures/tgwf.har"), "utf8"));
249
+ });
250
+ describe("perByte", () => {
251
+ it("returns a CO2 number for data transfer", () => {
252
+ co2.perByte(MILLION);
253
+ expect(co2.perByte(MILLION).toPrecision(5)).toBe(MILLION_GREY.toPrecision(5));
254
+ });
255
+ });
256
+ });
257
+ });
258
+ //# sourceMappingURL=co2.test.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/co2.test.js"],
4
+ "sourcesContent": ["\"use strict\";\n\nimport fs from \"fs\";\nimport path from \"path\";\n\nimport pagexray from \"pagexray\";\n\nimport CO2 from \"./co2.js\";\nimport SustainableWebDesign from \"./sustainable-web-design.js\";\n\ndescribe(\"co2\", () => {\n let har, co2;\n\n describe(\"1 byte model\", () => {\n const TGWF_GREY_VALUE = 0.20497;\n const TGWF_GREEN_VALUE = 0.54704;\n const TGWF_MIXED_VALUE = 0.16718;\n\n const MILLION = 1000000;\n const MILLION_GREY = 0.29081;\n const MILLION_GREEN = 0.23196;\n\n beforeEach(() => {\n co2 = new CO2();\n har = JSON.parse(\n fs.readFileSync(\n path.resolve(__dirname, \"../data/fixtures/tgwf.har\"),\n \"utf8\"\n )\n );\n });\n\n describe(\"perByte\", () => {\n it(\"returns a CO2 number for data transfer using 'grey' power\", () => {\n expect(co2.perByte(MILLION).toPrecision(5)).toBe(\n MILLION_GREY.toPrecision(5)\n );\n });\n\n it(\"returns a lower CO2 number for data transfer from domains using entirely 'green' power\", () => {\n expect(co2.perByte(MILLION).toPrecision(5)).toBe(\n MILLION_GREY.toPrecision(5)\n );\n expect(co2.perByte(MILLION, true).toPrecision(5)).toBe(\n MILLION_GREEN.toPrecision(5)\n );\n });\n });\n\n describe(\"perPage\", () => {\n it(\"returns CO2 for total transfer for page\", () => {\n const pages = pagexray.convert(har);\n const pageXrayRun = pages[0];\n\n expect(co2.perPage(pageXrayRun).toPrecision(5)).toBe(\n TGWF_GREY_VALUE.toPrecision(5)\n );\n });\n it(\"returns lower CO2 for page served from green site\", () => {\n const pages = pagexray.convert(har);\n const pageXrayRun = pages[0];\n let green = [\n \"www.thegreenwebfoundation.org\",\n \"fonts.googleapis.com\",\n \"ajax.googleapis.com\",\n \"assets.digitalclimatestrike.net\",\n \"cdnjs.cloudflare.com\",\n \"graphite.thegreenwebfoundation.org\",\n \"analytics.thegreenwebfoundation.org\",\n \"fonts.gstatic.com\",\n \"api.thegreenwebfoundation.org\",\n ];\n expect(co2.perPage(pageXrayRun, green)).toBeLessThan(TGWF_GREY_VALUE);\n });\n it(\"returns a lower CO2 number where *some* domains use green power\", () => {\n const pages = pagexray.convert(har);\n const pageXrayRun = pages[0];\n // green can be true, or a array containing entries\n let green = [\n \"www.thegreenwebfoundation.org\",\n \"fonts.googleapis.com\",\n \"ajax.googleapis.com\",\n \"assets.digitalclimatestrike.net\",\n \"cdnjs.cloudflare.com\",\n \"graphite.thegreenwebfoundation.org\",\n \"analytics.thegreenwebfoundation.org\",\n \"fonts.gstatic.com\",\n \"api.thegreenwebfoundation.org\",\n ];\n expect(co2.perPage(pageXrayRun, green).toPrecision(5)).toBe(\n TGWF_MIXED_VALUE.toPrecision(5)\n );\n });\n });\n describe(\"perDomain\", () => {\n it(\"shows object listing Co2 for each domain\", () => {\n const pages = pagexray.convert(har);\n const pageXrayRun = pages[0];\n const res = co2.perDomain(pageXrayRun);\n\n const domains = [\n \"thegreenwebfoundation.org\",\n \"www.thegreenwebfoundation.org\",\n \"maxcdn.bootstrapcdn.com\",\n \"fonts.googleapis.com\",\n \"ajax.googleapis.com\",\n \"assets.digitalclimatestrike.net\",\n \"cdnjs.cloudflare.com\",\n \"graphite.thegreenwebfoundation.org\",\n \"analytics.thegreenwebfoundation.org\",\n \"fonts.gstatic.com\",\n \"api.thegreenwebfoundation.org\",\n ];\n\n for (let obj of res) {\n expect(domains.indexOf(obj.domain)).toBeGreaterThan(-1);\n expect(typeof obj.co2).toBe(\"number\");\n }\n });\n it(\"shows lower Co2 for green domains\", () => {\n const pages = pagexray.convert(har);\n const pageXrayRun = pages[0];\n\n const greenDomains = [\n \"www.thegreenwebfoundation.org\",\n \"fonts.googleapis.com\",\n \"ajax.googleapis.com\",\n \"assets.digitalclimatestrike.net\",\n \"cdnjs.cloudflare.com\",\n \"graphite.thegreenwebfoundation.org\",\n \"analytics.thegreenwebfoundation.org\",\n \"fonts.gstatic.com\",\n \"api.thegreenwebfoundation.org\",\n ];\n const res = co2.perDomain(pageXrayRun);\n const resWithGreen = co2.perDomain(pageXrayRun, greenDomains);\n\n for (let obj of res) {\n expect(typeof obj.co2).toBe(\"number\");\n }\n for (let obj of greenDomains) {\n let index = 0;\n expect(resWithGreen[index].co2).toBeLessThan(res[index].co2);\n index++;\n }\n });\n });\n });\n\n describe(\"Sustainable Web Design model as simple option\", () => {\n // the SWD model should have slightly higher values as\n // we include more of the system in calculations for the\n // same levels of data transfer\n const MILLION = 1000000;\n const MILLION_GREY = 0.35802;\n const MILLION_GREEN = 0.31039;\n\n const TGWF_GREY_VALUE = 0.25234;\n const TGWF_GREEN_VALUE = 0.54704;\n const TGWF_MIXED_VALUE = 0.22175;\n\n beforeEach(() => {\n co2 = new CO2({ model: \"swd\" });\n har = JSON.parse(\n fs.readFileSync(\n path.resolve(__dirname, \"../data/fixtures/tgwf.har\"),\n \"utf8\"\n )\n );\n });\n\n describe(\"perByte\", () => {\n it(\"returns a CO2 number for data transfer\", () => {\n co2.perByte(MILLION);\n expect(co2.perByte(MILLION).toPrecision(5)).toBe(\n MILLION_GREY.toPrecision(5)\n );\n });\n\n it(\"returns a lower CO2 number for data transfer from domains using entirely 'green' power\", () => {\n expect(co2.perByte(MILLION, false).toPrecision(5)).toBe(\n MILLION_GREY.toPrecision(5)\n );\n\n expect(co2.perByte(MILLION, true).toPrecision(5)).toBe(\n MILLION_GREEN.toPrecision(5)\n );\n });\n });\n\n describe(\"perPage\", () => {\n it(\"returns CO2 for total transfer for page\", () => {\n const pages = pagexray.convert(har);\n const pageXrayRun = pages[0];\n\n expect(co2.perPage(pageXrayRun).toPrecision(5)).toBe(\n TGWF_GREY_VALUE.toPrecision(5)\n );\n });\n it(\"returns lower CO2 for page served from green site\", () => {\n const pages = pagexray.convert(har);\n const pageXrayRun = pages[0];\n let green = [\n \"www.thegreenwebfoundation.org\",\n \"fonts.googleapis.com\",\n \"ajax.googleapis.com\",\n \"assets.digitalclimatestrike.net\",\n \"cdnjs.cloudflare.com\",\n \"graphite.thegreenwebfoundation.org\",\n \"analytics.thegreenwebfoundation.org\",\n \"fonts.gstatic.com\",\n \"api.thegreenwebfoundation.org\",\n ];\n expect(co2.perPage(pageXrayRun, green)).toBeLessThan(TGWF_GREY_VALUE);\n });\n it(\"returns a lower CO2 number where *some* domains use green power\", () => {\n const pages = pagexray.convert(har);\n const pageXrayRun = pages[0];\n // green can be true, or a array containing entries\n let green = [\n \"www.thegreenwebfoundation.org\",\n \"fonts.googleapis.com\",\n \"ajax.googleapis.com\",\n \"assets.digitalclimatestrike.net\",\n \"cdnjs.cloudflare.com\",\n \"graphite.thegreenwebfoundation.org\",\n \"analytics.thegreenwebfoundation.org\",\n \"fonts.gstatic.com\",\n \"api.thegreenwebfoundation.org\",\n ];\n expect(co2.perPage(pageXrayRun, green).toPrecision(5)).toBe(\n TGWF_MIXED_VALUE.toPrecision(5)\n );\n });\n });\n describe(\"perDomain\", () => {\n it(\"shows object listing Co2 for each domain\", () => {\n const pages = pagexray.convert(har);\n const pageXrayRun = pages[0];\n const res = co2.perDomain(pageXrayRun);\n\n const domains = [\n \"thegreenwebfoundation.org\",\n \"www.thegreenwebfoundation.org\",\n \"maxcdn.bootstrapcdn.com\",\n \"fonts.googleapis.com\",\n \"ajax.googleapis.com\",\n \"assets.digitalclimatestrike.net\",\n \"cdnjs.cloudflare.com\",\n \"graphite.thegreenwebfoundation.org\",\n \"analytics.thegreenwebfoundation.org\",\n \"fonts.gstatic.com\",\n \"api.thegreenwebfoundation.org\",\n ];\n\n for (let obj of res) {\n expect(domains.indexOf(obj.domain)).toBeGreaterThan(-1);\n expect(typeof obj.co2).toBe(\"number\");\n }\n });\n it(\"shows lower Co2 for green domains\", () => {\n const pages = pagexray.convert(har);\n const pageXrayRun = pages[0];\n\n const greenDomains = [\n \"www.thegreenwebfoundation.org\",\n \"fonts.googleapis.com\",\n \"ajax.googleapis.com\",\n \"assets.digitalclimatestrike.net\",\n \"cdnjs.cloudflare.com\",\n \"graphite.thegreenwebfoundation.org\",\n \"analytics.thegreenwebfoundation.org\",\n \"fonts.gstatic.com\",\n \"api.thegreenwebfoundation.org\",\n ];\n const res = co2.perDomain(pageXrayRun);\n const resWithGreen = co2.perDomain(pageXrayRun, greenDomains);\n\n for (let obj of res) {\n expect(typeof obj.co2).toBe(\"number\");\n }\n for (let obj of greenDomains) {\n let index = 0;\n expect(resWithGreen[index].co2).toBeLessThan(res[index].co2);\n index++;\n }\n });\n });\n });\n\n describe(\"New model that implements the same API\", () => {\n const MILLION = 1000000;\n const MILLION_GREY = 0.35802;\n\n beforeEach(() => {\n // we use the SustainableWebDesign to demonstrate passing\n // in a complex object instead of a simple string\n co2 = new CO2({ model: SustainableWebDesign });\n har = JSON.parse(\n fs.readFileSync(\n path.resolve(__dirname, \"../data/fixtures/tgwf.har\"),\n \"utf8\"\n )\n );\n });\n\n describe(\"perByte\", () => {\n it(\"returns a CO2 number for data transfer\", () => {\n co2.perByte(MILLION);\n expect(co2.perByte(MILLION).toPrecision(5)).toBe(\n MILLION_GREY.toPrecision(5)\n );\n });\n });\n });\n});\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;AAEA,gBAAe;AACf,kBAAiB;AAEjB,sBAAqB;AAErB,iBAAgB;AAChB,oCAAiC;AAEjC,SAAS,OAAO,MAAM;AACpB,MAAI,KAAK;AAET,WAAS,gBAAgB,MAAM;AAC7B,UAAM,kBAAkB;AACxB,UAAM,mBAAmB;AACzB,UAAM,mBAAmB;AAEzB,UAAM,UAAU;AAChB,UAAM,eAAe;AACrB,UAAM,gBAAgB;AAEtB,eAAW,MAAM;AACf,YAAM,IAAI,mBAAI;AACd,YAAM,KAAK,MACT,kBAAG,aACD,oBAAK,QAAQ,WAAW,2BAA2B,GACnD,MACF,CACF;AAAA,IACF,CAAC;AAED,aAAS,WAAW,MAAM;AACxB,SAAG,6DAA6D,MAAM;AACpE,eAAO,IAAI,QAAQ,OAAO,EAAE,YAAY,CAAC,CAAC,EAAE,KAC1C,aAAa,YAAY,CAAC,CAC5B;AAAA,MACF,CAAC;AAED,SAAG,0FAA0F,MAAM;AACjG,eAAO,IAAI,QAAQ,OAAO,EAAE,YAAY,CAAC,CAAC,EAAE,KAC1C,aAAa,YAAY,CAAC,CAC5B;AACA,eAAO,IAAI,QAAQ,SAAS,IAAI,EAAE,YAAY,CAAC,CAAC,EAAE,KAChD,cAAc,YAAY,CAAC,CAC7B;AAAA,MACF,CAAC;AAAA,IACH,CAAC;AAED,aAAS,WAAW,MAAM;AACxB,SAAG,2CAA2C,MAAM;AAClD,cAAM,QAAQ,wBAAS,QAAQ,GAAG;AAClC,cAAM,cAAc,MAAM;AAE1B,eAAO,IAAI,QAAQ,WAAW,EAAE,YAAY,CAAC,CAAC,EAAE,KAC9C,gBAAgB,YAAY,CAAC,CAC/B;AAAA,MACF,CAAC;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;AACtD,iBAAO,OAAO,IAAI,GAAG,EAAE,KAAK,QAAQ;AAAA,QACtC;AAAA,MACF,CAAC;AACD,SAAG,qCAAqC,MAAM;AAC5C,cAAM,QAAQ,wBAAS,QAAQ,GAAG;AAClC,cAAM,cAAc,MAAM;AAE1B,cAAM,eAAe;AAAA,UACnB;AAAA,UACA;AAAA,UACA;AAAA,UACA;AAAA,UACA;AAAA,UACA;AAAA,UACA;AAAA,UACA;AAAA,UACA;AAAA,QACF;AACA,cAAM,MAAM,IAAI,UAAU,WAAW;AACrC,cAAM,eAAe,IAAI,UAAU,aAAa,YAAY;AAE5D,iBAAS,OAAO,KAAK;AACnB,iBAAO,OAAO,IAAI,GAAG,EAAE,KAAK,QAAQ;AAAA,QACtC;AACA,iBAAS,OAAO,cAAc;AAC5B,cAAI,QAAQ;AACZ,iBAAO,aAAa,OAAO,GAAG,EAAE,aAAa,IAAI,OAAO,GAAG;AAC3D;AAAA,QACF;AAAA,MACF,CAAC;AAAA,IACH,CAAC;AAAA,EACH,CAAC;AAED,WAAS,0CAA0C,MAAM;AACvD,UAAM,UAAU;AAChB,UAAM,eAAe;AAErB,eAAW,MAAM;AAGf,YAAM,IAAI,mBAAI,EAAE,OAAO,sCAAqB,CAAC;AAC7C,YAAM,KAAK,MACT,kBAAG,aACD,oBAAK,QAAQ,WAAW,2BAA2B,GACnD,MACF,CACF;AAAA,IACF,CAAC;AAED,aAAS,WAAW,MAAM;AACxB,SAAG,0CAA0C,MAAM;AACjD,YAAI,QAAQ,OAAO;AACnB,eAAO,IAAI,QAAQ,OAAO,EAAE,YAAY,CAAC,CAAC,EAAE,KAC1C,aAAa,YAAY,CAAC,CAC5B;AAAA,MACF,CAAC;AAAA,IACH,CAAC;AAAA,EACH,CAAC;AACH,CAAC;",
6
+ "names": []
7
+ }
@@ -0,0 +1,27 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+ var file_size_exports = {};
19
+ __export(file_size_exports, {
20
+ default: () => file_size_default
21
+ });
22
+ module.exports = __toCommonJS(file_size_exports);
23
+ const GIGABYTE = 1e3 * 1e3 * 1e3;
24
+ var file_size_default = {
25
+ GIGABYTE
26
+ };
27
+ //# sourceMappingURL=file-size.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../../src/constants/file-size.js"],
4
+ "sourcesContent": ["const GIGABYTE = 1000 * 1000 * 1000;\n\nexport default {\n GIGABYTE,\n};\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,MAAM,WAAW,MAAO,MAAO;AAE/B,IAAO,oBAAQ;AAAA,EACb;AACF;",
6
+ "names": []
7
+ }
@@ -0,0 +1,27 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod));
20
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
21
+ var constants_exports = {};
22
+ __export(constants_exports, {
23
+ fileSize: () => import_file_size.default
24
+ });
25
+ module.exports = __toCommonJS(constants_exports);
26
+ var import_file_size = __toESM(require("./file-size.js"));
27
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../../src/constants/index.js"],
4
+ "sourcesContent": ["import fileSize from \"./file-size.js\";\n\nexport { fileSize };\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,uBAAqB;",
6
+ "names": []
7
+ }
@@ -0,0 +1,24 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+ var helpers_exports = {};
19
+ __export(helpers_exports, {
20
+ formatNumber: () => formatNumber
21
+ });
22
+ module.exports = __toCommonJS(helpers_exports);
23
+ const formatNumber = (num) => parseFloat(num.toFixed(2));
24
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../../src/helpers/index.js"],
4
+ "sourcesContent": ["const formatNumber = (num) => parseFloat(num.toFixed(2));\n\nexport { formatNumber };\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,MAAM,eAAe,CAAC,QAAQ,WAAW,IAAI,QAAQ,CAAC,CAAC;",
6
+ "names": []
7
+ }
@@ -0,0 +1,64 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod));
21
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
22
+ var hosting_api_exports = {};
23
+ __export(hosting_api_exports, {
24
+ default: () => hosting_api_default
25
+ });
26
+ module.exports = __toCommonJS(hosting_api_exports);
27
+ var import_debug = __toESM(require("debug"));
28
+ const log = (0, import_debug.default)("tgwf:hostingAPI");
29
+ function check(domain) {
30
+ if (typeof domain === "string") {
31
+ return checkAgainstAPI(domain);
32
+ } else {
33
+ return checkDomainsAgainstAPI(domain);
34
+ }
35
+ }
36
+ async function checkAgainstAPI(domain) {
37
+ const req = await fetch(`https://api.thegreenwebfoundation.org/greencheck/${domain}`);
38
+ const res = await req.json();
39
+ return res.green;
40
+ }
41
+ async function checkDomainsAgainstAPI(domains) {
42
+ try {
43
+ const apiPath = "https://api.thegreenwebfoundation.org/v2/greencheckmulti";
44
+ const domainsString = JSON.stringify(domains);
45
+ const req = await fetch(`${apiPath}/${domainsString}`);
46
+ log(`${apiPath}/${domainsString}`);
47
+ log({ req });
48
+ const textResult = await req.text();
49
+ log({ textResult });
50
+ const allGreenCheckResults = await req.json();
51
+ return greenDomainsFromResults(allGreenCheckResults);
52
+ } catch (e) {
53
+ return [];
54
+ }
55
+ }
56
+ function greenDomainsFromResults(greenResults) {
57
+ const entries = Object.entries(greenResults);
58
+ const greenEntries = entries.filter(([key, val]) => val.green);
59
+ return greenEntries.map(([key, val]) => val.url);
60
+ }
61
+ var hosting_api_default = {
62
+ check
63
+ };
64
+ //# sourceMappingURL=hosting-api.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/hosting-api.js"],
4
+ "sourcesContent": ["\"use strict\";\n\nimport debugFactory from \"debug\";\nconst log = debugFactory(\"tgwf:hostingAPI\");\n\nfunction check(domain) {\n // is it a single domain or an array of them?\n if (typeof domain === \"string\") {\n return checkAgainstAPI(domain);\n } else {\n return checkDomainsAgainstAPI(domain);\n }\n}\n\nasync function checkAgainstAPI(domain) {\n const req = await fetch(\n `https://api.thegreenwebfoundation.org/greencheck/${domain}`\n );\n const res = await req.json();\n return res.green;\n}\n\nasync function checkDomainsAgainstAPI(domains) {\n try {\n const apiPath = \"https://api.thegreenwebfoundation.org/v2/greencheckmulti\";\n const domainsString = JSON.stringify(domains);\n\n const req = await fetch(`${apiPath}/${domainsString}`);\n\n // sanity check API result. Is this the library or\n // the actual API request that's the problem?\n // Is nock mocking node-native fetch API calls properly?\n log(`${apiPath}/${domainsString}`);\n log({ req });\n const textResult = await req.text();\n log({ textResult });\n\n const allGreenCheckResults = await req.json();\n\n return greenDomainsFromResults(allGreenCheckResults);\n } catch (e) {\n return [];\n }\n}\n\nfunction greenDomainsFromResults(greenResults) {\n const entries = Object.entries(greenResults);\n const greenEntries = entries.filter(([key, val]) => val.green);\n return greenEntries.map(([key, val]) => val.url);\n}\n\nexport default {\n check,\n};\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAEA,mBAAyB;AACzB,MAAM,MAAM,0BAAa,iBAAiB;AAE1C,eAAe,QAAQ;AAErB,MAAI,OAAO,WAAW,UAAU;AAC9B,WAAO,gBAAgB,MAAM;AAAA,EAC/B,OAAO;AACL,WAAO,uBAAuB,MAAM;AAAA,EACtC;AACF;AAEA,+BAA+B,QAAQ;AACrC,QAAM,MAAM,MAAM,MAChB,oDAAoD,QACtD;AACA,QAAM,MAAM,MAAM,IAAI,KAAK;AAC3B,SAAO,IAAI;AACb;AAEA,sCAAsC,SAAS;AAC7C,MAAI;AACF,UAAM,UAAU;AAChB,UAAM,gBAAgB,KAAK,UAAU,OAAO;AAE5C,UAAM,MAAM,MAAM,MAAM,GAAG,WAAW,eAAe;AAKrD,QAAI,GAAG,WAAW,eAAe;AACjC,QAAI,EAAE,IAAI,CAAC;AACX,UAAM,aAAa,MAAM,IAAI,KAAK;AAClC,QAAI,EAAE,WAAW,CAAC;AAElB,UAAM,uBAAuB,MAAM,IAAI,KAAK;AAE5C,WAAO,wBAAwB,oBAAoB;AAAA,EACrD,SAAS,GAAP;AACA,WAAO,CAAC;AAAA,EACV;AACF;AAEA,iCAAiC,cAAc;AAC7C,QAAM,UAAU,OAAO,QAAQ,YAAY;AAC3C,QAAM,eAAe,QAAQ,OAAO,CAAC,CAAC,KAAK,SAAS,IAAI,KAAK;AAC7D,SAAO,aAAa,IAAI,CAAC,CAAC,KAAK,SAAS,IAAI,GAAG;AACjD;AAEA,IAAO,sBAAQ;AAAA,EACb;AACF;",
6
+ "names": []
7
+ }
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __copyProps = (to, from, except, desc) => {
9
+ if (from && typeof from === "object" || typeof from === "function") {
10
+ for (let key of __getOwnPropNames(from))
11
+ if (!__hasOwnProp.call(to, key) && key !== except)
12
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
13
+ }
14
+ return to;
15
+ };
16
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod));
17
+ var import_hosting_node = __toESM(require("./hosting-node.js"));
18
+ var import_nock = __toESM(require("nock"));
19
+ describe("hostingAPI", () => {
20
+ describe("checking a single domain with #check", () => {
21
+ it.skip("using the API", async () => {
22
+ const scope = (0, import_nock.default)("https://api.thegreenwebfoundation.org/").get("/greencheck/google.com").reply(200, {
23
+ url: "google.com",
24
+ green: true
25
+ });
26
+ const res = await import_hosting_node.default.check("google.com");
27
+ expect(res).toEqual(true);
28
+ });
29
+ });
30
+ describe("implicitly checking multiple domains with #check", () => {
31
+ it.skip("using the API", async () => {
32
+ const scope = (0, import_nock.default)("https://api.thegreenwebfoundation.org/").get("/v2/greencheckmulti/[%22google.com%22,%22kochindustries.com%22]").reply(200, {
33
+ "google.com": {
34
+ url: "google.com",
35
+ green: true
36
+ },
37
+ "kochindustries.com": {
38
+ url: "kochindustries.com",
39
+ green: null
40
+ }
41
+ });
42
+ const res = await import_hosting_node.default.check(["google.com", "kochindustries.com"]);
43
+ expect(res).toContain("google.com");
44
+ });
45
+ });
46
+ });
47
+ //# sourceMappingURL=hosting-api.test.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/hosting-api.test.js"],
4
+ "sourcesContent": ["\"use strict\";\n\nimport hosting from \"./hosting-node.js\";\nimport nock from \"nock\";\n/* eslint-disable jest/no-disabled-tests */\ndescribe(\"hostingAPI\", () => {\n describe(\"checking a single domain with #check\", () => {\n it.skip(\"using the API\", async () => {\n const scope = nock(\"https://api.thegreenwebfoundation.org/\")\n .get(\"/greencheck/google.com\")\n .reply(200, {\n url: \"google.com\",\n green: true,\n });\n const res = await hosting.check(\"google.com\");\n expect(res).toEqual(true);\n });\n });\n describe(\"implicitly checking multiple domains with #check\", () => {\n it.skip(\"using the API\", async () => {\n const scope = nock(\"https://api.thegreenwebfoundation.org/\")\n .get(\"/v2/greencheckmulti/[%22google.com%22,%22kochindustries.com%22]\")\n .reply(200, {\n \"google.com\": {\n url: \"google.com\",\n green: true,\n },\n \"kochindustries.com\": {\n url: \"kochindustries.com\",\n green: null,\n },\n });\n const res = await hosting.check([\"google.com\", \"kochindustries.com\"]);\n expect(res).toContain(\"google.com\");\n });\n });\n});\n/* eslint-enable jest/no-disabled-tests */\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;AAEA,0BAAoB;AACpB,kBAAiB;AAEjB,SAAS,cAAc,MAAM;AAC3B,WAAS,wCAAwC,MAAM;AACrD,OAAG,KAAK,iBAAiB,YAAY;AACnC,YAAM,QAAQ,yBAAK,wCAAwC,EACxD,IAAI,wBAAwB,EAC5B,MAAM,KAAK;AAAA,QACV,KAAK;AAAA,QACL,OAAO;AAAA,MACT,CAAC;AACH,YAAM,MAAM,MAAM,4BAAQ,MAAM,YAAY;AAC5C,aAAO,GAAG,EAAE,QAAQ,IAAI;AAAA,IAC1B,CAAC;AAAA,EACH,CAAC;AACD,WAAS,oDAAoD,MAAM;AACjE,OAAG,KAAK,iBAAiB,YAAY;AACnC,YAAM,QAAQ,yBAAK,wCAAwC,EACxD,IAAI,iEAAiE,EACrE,MAAM,KAAK;AAAA,QACV,cAAc;AAAA,UACZ,KAAK;AAAA,UACL,OAAO;AAAA,QACT;AAAA,QACA,sBAAsB;AAAA,UACpB,KAAK;AAAA,UACL,OAAO;AAAA,QACT;AAAA,MACF,CAAC;AACH,YAAM,MAAM,MAAM,4BAAQ,MAAM,CAAC,cAAc,oBAAoB,CAAC;AACpE,aAAO,GAAG,EAAE,UAAU,YAAY;AAAA,IACpC,CAAC;AAAA,EACH,CAAC;AACH,CAAC;",
6
+ "names": []
7
+ }
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __copyProps = (to, from, except, desc) => {
9
+ if (from && typeof from === "object" || typeof from === "function") {
10
+ for (let key of __getOwnPropNames(from))
11
+ if (!__hasOwnProp.call(to, key) && key !== except)
12
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
13
+ }
14
+ return to;
15
+ };
16
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod));
17
+ var import_path = __toESM(require("path"));
18
+ var import_debug = __toESM(require("debug"));
19
+ var import_url2green = require("@tgwf/url2green");
20
+ const log = (0, import_debug.default)("tgwf:url2green:test");
21
+ const dbPath = import_path.default.resolve(__dirname, "..", "data", "fixtures", "url2green.test.db");
22
+ describe("hostingDatabase", () => {
23
+ describe("checking a single domain with #check", () => {
24
+ test("tries to use a local database if available", async () => {
25
+ const res = await import_url2green.hosting.check("google.com", dbPath);
26
+ expect(res).toEqual(true);
27
+ });
28
+ });
29
+ describe("implicitly checking multiple domains with #check", () => {
30
+ test("tries to use a local database if available", async () => {
31
+ const res = await import_url2green.hosting.check(["google.com", "kochindustries.com"], dbPath);
32
+ expect(res).toContain("google.com");
33
+ });
34
+ });
35
+ });
36
+ //# sourceMappingURL=hosting-database.node.test.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/hosting-database.node.test.js"],
4
+ "sourcesContent": ["\"use strict\";\n\nimport path from \"path\";\n\nimport debugFactory from \"debug\";\nconst log = debugFactory(\"tgwf:url2green:test\");\n\nimport { hosting } from \"@tgwf/url2green\";\n\nconst dbPath = path.resolve(\n __dirname,\n \"..\",\n \"data\",\n \"fixtures\",\n \"url2green.test.db\"\n);\n\ndescribe(\"hostingDatabase\", () => {\n describe(\"checking a single domain with #check\", () => {\n test(\"tries to use a local database if available\", async () => {\n const res = await hosting.check(\"google.com\", dbPath);\n expect(res).toEqual(true);\n });\n });\n describe(\"implicitly checking multiple domains with #check\", () => {\n test(\"tries to use a local database if available\", async () => {\n const res = await hosting.check(\n [\"google.com\", \"kochindustries.com\"],\n dbPath\n );\n expect(res).toContain(\"google.com\");\n });\n });\n});\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;AAEA,kBAAiB;AAEjB,mBAAyB;AAGzB,uBAAwB;AAFxB,MAAM,MAAM,0BAAa,qBAAqB;AAI9C,MAAM,SAAS,oBAAK,QAClB,WACA,MACA,QACA,YACA,mBACF;AAEA,SAAS,mBAAmB,MAAM;AAChC,WAAS,wCAAwC,MAAM;AACrD,SAAK,8CAA8C,YAAY;AAC7D,YAAM,MAAM,MAAM,yBAAQ,MAAM,cAAc,MAAM;AACpD,aAAO,GAAG,EAAE,QAAQ,IAAI;AAAA,IAC1B,CAAC;AAAA,EACH,CAAC;AACD,WAAS,oDAAoD,MAAM;AACjE,SAAK,8CAA8C,YAAY;AAC7D,YAAM,MAAM,MAAM,yBAAQ,MACxB,CAAC,cAAc,oBAAoB,GACnC,MACF;AACA,aAAO,GAAG,EAAE,UAAU,YAAY;AAAA,IACpC,CAAC;AAAA,EACH,CAAC;AACH,CAAC;",
6
+ "names": []
7
+ }