@tgwf/co2 0.14.1 → 0.14.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.
- package/CHANGELOG.md +107 -73
- package/dist/cjs/helpers/index.js +4 -0
- package/dist/cjs/helpers/index.js.map +2 -2
- package/dist/cjs/hosting-api.js +12 -7
- package/dist/cjs/hosting-api.js.map +2 -2
- package/dist/cjs/hosting-node.js +10 -9
- package/dist/cjs/hosting-node.js.map +2 -2
- package/dist/cjs/hosting.js +2 -2
- package/dist/cjs/hosting.js.map +2 -2
- package/dist/esm/helpers/index.js +4 -0
- package/dist/esm/hosting-api.js +12 -7
- package/dist/esm/hosting.js +2 -2
- package/package.json +3 -2
package/CHANGELOG.md
CHANGED
|
@@ -12,58 +12,92 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
12
12
|
> - **Fixed** for any bug fixes.
|
|
13
13
|
> - **Security** in case of vulnerabilities.
|
|
14
14
|
|
|
15
|
-
|
|
15
|
+
## Unreleased
|
|
16
16
|
|
|
17
|
-
-
|
|
17
|
+
- Adds user agent header for requests to Green Web Foundation APIs
|
|
18
|
+
<!-- - _(Add a summary of your feature, and if relevant the issue, in your PR for merging into `main`)_ -->
|
|
18
19
|
|
|
19
|
-
|
|
20
|
+
## Released
|
|
20
21
|
|
|
21
|
-
|
|
22
|
+
### [0.14.1] - 2024-01-09
|
|
23
|
+
|
|
24
|
+
#### Changed
|
|
25
|
+
|
|
26
|
+
- Removed `index.d.ts` in favour of importing type definitions from `@types/tgwf__co2`.
|
|
27
|
+
- Reduce package size by excluding files from publish NPM package.
|
|
28
|
+
- Automated monthly update of annual average grid intensity data.
|
|
29
|
+
|
|
30
|
+
### [0.13.10] - 2023-12-16
|
|
31
|
+
|
|
32
|
+
#### Changed
|
|
22
33
|
|
|
23
|
-
- Fix: Properly use value of 0 for system segments and variables in the perByteTrace and perVisitTrace functions.
|
|
24
34
|
- Automated monthly update of annual average grid intensity data.
|
|
25
35
|
|
|
26
|
-
|
|
36
|
+
### [0.13.9] - 2023-11-07
|
|
37
|
+
|
|
38
|
+
#### Fixed
|
|
39
|
+
|
|
40
|
+
- Fix to return expected results when variables with 0 value are passed into function
|
|
41
|
+
|
|
42
|
+
#### Changed
|
|
27
43
|
|
|
28
44
|
- Automated monthly update of annual average grid intensity data.
|
|
29
45
|
|
|
30
|
-
|
|
46
|
+
### [0.13.8] - 2023-10-09
|
|
47
|
+
|
|
48
|
+
#### Fixed
|
|
49
|
+
|
|
50
|
+
- Properly use value of 0 for system segments and variables in the perByteTrace and perVisitTrace functions.
|
|
51
|
+
|
|
52
|
+
#### Changed
|
|
53
|
+
|
|
54
|
+
- Automated monthly update of annual average grid intensity data.
|
|
55
|
+
|
|
56
|
+
### [0.13.7] - 2023-09-13
|
|
57
|
+
|
|
58
|
+
#### Changed
|
|
59
|
+
|
|
60
|
+
- Automated monthly update of annual average grid intensity data.
|
|
61
|
+
|
|
62
|
+
### [0.13.6] - 2023-08-08
|
|
63
|
+
|
|
64
|
+
#### Changed
|
|
31
65
|
|
|
32
66
|
- Automated monthly update of annual average grid intensity data.
|
|
33
67
|
- Create FUNDING.yml to allow sponsor contribution to this project.
|
|
34
68
|
- Store segment flag on CO2 instance, not models.
|
|
35
69
|
|
|
36
|
-
|
|
70
|
+
### [0.13.5] - 2023-07-5
|
|
37
71
|
|
|
38
|
-
|
|
72
|
+
#### Changed
|
|
39
73
|
|
|
40
74
|
- Automated monthly update of annual average grid intensity data.
|
|
41
75
|
|
|
42
|
-
|
|
76
|
+
### [0.13.4] - 2023-05-24
|
|
43
77
|
|
|
44
|
-
|
|
78
|
+
#### Fixed
|
|
45
79
|
|
|
46
80
|
- Fixed an error when try to use global grid intensities in IIFE. (PR #147)
|
|
47
81
|
|
|
48
|
-
|
|
82
|
+
### [0.13.3] - 2023-05-18
|
|
49
83
|
|
|
50
|
-
|
|
84
|
+
#### Changed
|
|
51
85
|
|
|
52
86
|
- Updated the global grid intensity constant to use the latest WORLD grid intensity value from Ember. (PR #142)
|
|
53
87
|
|
|
54
|
-
|
|
88
|
+
### [0.13.2] - 2023-04-21
|
|
55
89
|
|
|
56
90
|
- Fix to ensure that region names that are keys in the average annual grid intensity export are capitalised.
|
|
57
91
|
|
|
58
|
-
|
|
92
|
+
### [0.13.1] - 2023-04-20
|
|
59
93
|
|
|
60
|
-
|
|
94
|
+
#### Fixed
|
|
61
95
|
|
|
62
96
|
- Fixed the import of average grid intensities in node. (PR #137)
|
|
63
97
|
|
|
64
|
-
|
|
98
|
+
### [0.13.0] - 2023-04-13
|
|
65
99
|
|
|
66
|
-
|
|
100
|
+
#### Changed
|
|
67
101
|
|
|
68
102
|
In PR #135 there were significant changes made to how average annual grid intensities are fetched and generated.
|
|
69
103
|
|
|
@@ -73,59 +107,59 @@ In PR #135 there were significant changes made to how average annual grid intens
|
|
|
73
107
|
- Get the _latest_ annual average values for each country/region.
|
|
74
108
|
- Renamed the average grid intensities export file.
|
|
75
109
|
|
|
76
|
-
|
|
110
|
+
### [0.12.2] - 2023-03-01
|
|
77
111
|
|
|
78
|
-
|
|
112
|
+
#### Added
|
|
79
113
|
|
|
80
114
|
- Add a module declaration for use from typescript (PR #131)
|
|
81
115
|
|
|
82
|
-
|
|
116
|
+
#### Changed
|
|
83
117
|
|
|
84
118
|
- Updated 2021 average annual grid intensity values (PR #133)
|
|
85
119
|
|
|
86
|
-
|
|
120
|
+
### [0.12.1] - 2023-02-02
|
|
87
121
|
|
|
88
|
-
|
|
122
|
+
#### Fixed
|
|
89
123
|
|
|
90
124
|
- Removed incorrectly imported constants in tests.
|
|
91
125
|
|
|
92
|
-
|
|
126
|
+
### [0.12.0] - 2023-02-02
|
|
93
127
|
|
|
94
|
-
|
|
128
|
+
#### Added
|
|
95
129
|
|
|
96
130
|
- Introduced two new functions `perByteTrace` and `perVisitTrace` which allow developers to pass an options object containing customised values for the constants used in the Sustainable Web Design model. (PR #126)
|
|
97
131
|
|
|
98
|
-
|
|
132
|
+
#### Changed
|
|
99
133
|
|
|
100
134
|
- Allowed developers now have the option to return a breakdown of carbon emissions estimates by system segment when using the Sustainable Web Design model. (PR #113)
|
|
101
135
|
|
|
102
|
-
|
|
136
|
+
### [0.11.4] - 2022-12-02
|
|
103
137
|
|
|
104
|
-
|
|
138
|
+
#### Fixed
|
|
105
139
|
|
|
106
140
|
- Updated the `greenCheckMulti` function to work properly in ESM. (PR #123)
|
|
107
141
|
|
|
108
|
-
|
|
142
|
+
### [0.11.3] - 2022-10-13
|
|
109
143
|
|
|
110
|
-
|
|
144
|
+
#### Fixed
|
|
111
145
|
|
|
112
146
|
- Corrected the Node export for the hosting raised in issue #110. (PR #118)
|
|
113
147
|
|
|
114
|
-
|
|
148
|
+
### [0.11.2] - 2022-10-11
|
|
115
149
|
|
|
116
|
-
|
|
150
|
+
#### Fixed
|
|
117
151
|
|
|
118
152
|
- v0.11.x updates increased library size to 17MB + when published to NPM. This has been raised in [#108](https://github.com/thegreenwebfoundation/co2.js/issues/108) and it was found data files were being included in the published package. (PR #117)
|
|
119
153
|
|
|
120
|
-
|
|
154
|
+
### [0.11.1] - 2022-10-07
|
|
121
155
|
|
|
122
|
-
|
|
156
|
+
#### Changed
|
|
123
157
|
|
|
124
158
|
- We have used generic filenames for data files, to avoid any confusion around the data being provided in this library. (PR #112)
|
|
125
159
|
|
|
126
|
-
|
|
160
|
+
### [0.11.0] - 2022-10-03
|
|
127
161
|
|
|
128
|
-
|
|
162
|
+
#### Added
|
|
129
163
|
|
|
130
164
|
- Introduced average and marginal carbon intensity data into the library. This comes from [Ember Climate](https://ember-climate.org/data/data-explorer/) (for average carbon intensity data), and [The Green Web Foundation](https://developers.thegreenwebfoundation.org/co2js/data) (marginal intensity data, originally sourced from the UNFCCC - the United Nations Framework Convention on Climate Change). For more, [see our release guide for v0.11](https://www.thegreenwebfoundation.org/news/release-guide-co2-js-v0-11/) about the differences between the kinds of data. See [#64](https://github.com/thegreenwebfoundation/co2.js/issues/64), and [#97](https://github.com/thegreenwebfoundation/co2.js/issues/97) for more.
|
|
131
165
|
- Added new paths to `import` and `require` the annual, country-level average and marginal carbon intensity data mentioned above like, as javascript objects, or as JSON. See [#104 for more](https://github.com/thegreenwebfoundation/co2.js/issues/104).
|
|
@@ -133,32 +167,32 @@ In PR #135 there were significant changes made to how average annual grid intens
|
|
|
133
167
|
- Introduced scripts to automate the generation of grid intensity data based of Ember & UNFCCC source files.
|
|
134
168
|
- Introduced a `release:minor` command, to automate the publishing process, to complement `release:patch`.
|
|
135
169
|
|
|
136
|
-
|
|
170
|
+
#### Changed
|
|
137
171
|
|
|
138
172
|
- Changed the default model for transfer based CO2 calculations from the _1byte_ model to the _Sustainable Web Design_ model instead. See <https://developers.thegreenwebfoundation.org/co2js/models/> for guidance on the differences and how to migrate between them. See [#94 for more](https://github.com/thegreenwebfoundation/co2.js/issues/94).
|
|
139
173
|
- Updated our release commands to generate and format the carbon intensity data as part of the release process.
|
|
140
174
|
|
|
141
|
-
|
|
175
|
+
### [0.10.4] - 2022-08-12
|
|
142
176
|
|
|
143
|
-
|
|
177
|
+
#### Added
|
|
144
178
|
|
|
145
179
|
- Introduced a `release:patch` command, to automate the publishing process. This is designed to make sure we always publish the most recent compiled code, by adding a rebuild step that can be easy to forget.
|
|
146
180
|
|
|
147
|
-
|
|
181
|
+
### [0.10.3] - 2022-08-12
|
|
148
182
|
|
|
149
|
-
|
|
183
|
+
#### Added
|
|
150
184
|
|
|
151
185
|
- Introduced a new `perVisit()` function for the Sustainable Web Design model, which applies [caching and return visits assumptions](https://sustainablewebdesign.org/calculating-digital-emissions/).
|
|
152
186
|
|
|
153
|
-
|
|
187
|
+
### [0.10.2] - 2022-08-12
|
|
154
188
|
|
|
155
189
|
- Added the ability to set the model used by CO2.js to the Sustainable Web Design model, using a simple 'swd' string, instead of needing to pass in a class.
|
|
156
190
|
|
|
157
|
-
|
|
191
|
+
### [0.10.1] - 2022-08-01
|
|
158
192
|
|
|
159
193
|
This release used a version bump as previously we had released v0.10.0 under a pre-release tag.
|
|
160
194
|
|
|
161
|
-
|
|
195
|
+
### [0.10.0] - 2022-06-27
|
|
162
196
|
|
|
163
197
|
- Added ES import syntax as the main way for handling imports and exports of code within the module.
|
|
164
198
|
- Changed eslint settings to use later version of ecmascript (2020)
|
|
@@ -166,104 +200,104 @@ This release used a version bump as previously we had released v0.10.0 under a p
|
|
|
166
200
|
- Added more consistent use of the debug logging library in files using the updated import syntax
|
|
167
201
|
- Fixed the incorrect order of FIRST_TIME_VIEWING_PERCENTAGE and RETURNING_VISITOR_PERCENTAGE constants in the SWD model. This will result in **larger** values for calculations using the sustainable web design, and the default caching assumptions.
|
|
168
202
|
|
|
169
|
-
|
|
203
|
+
### [0.9.0] - 2022-03-28
|
|
170
204
|
|
|
171
|
-
|
|
205
|
+
#### Added
|
|
172
206
|
|
|
173
207
|
- Added newly implemented Sustainable Web Design model [thanks @dryden!]
|
|
174
208
|
- Added new readme page for using both emissions models
|
|
175
209
|
- Added new source of data to the Sustainable Web Design model from Ember Climate.
|
|
176
210
|
|
|
177
|
-
|
|
211
|
+
#### Changed
|
|
178
212
|
|
|
179
213
|
- Changed the CO2 class to accept either the One Byte model or the Sustainable Web Design model
|
|
180
214
|
|
|
181
|
-
|
|
215
|
+
#### Fixed
|
|
182
216
|
|
|
183
217
|
- Fixed various typos.
|
|
184
218
|
|
|
185
|
-
|
|
219
|
+
### [0.8.0] - 2021-11-28
|
|
186
220
|
|
|
187
|
-
|
|
221
|
+
#### Fixed
|
|
188
222
|
|
|
189
223
|
- Update further dependencies
|
|
190
224
|
- Fix embarassing order of magnitude typo in 1byte model (thanks @mstaschik!)
|
|
191
225
|
|
|
192
|
-
|
|
226
|
+
#### Added
|
|
193
227
|
|
|
194
228
|
- Read JSON blob also as gzipped #44 (thanks @soulgalore)
|
|
195
229
|
|
|
196
|
-
|
|
230
|
+
#### Changed
|
|
197
231
|
|
|
198
232
|
- The 1byte model will give different numbers now. It's mentioned in `#fixed` but it's worth repeating.
|
|
199
233
|
|
|
200
|
-
|
|
234
|
+
### [0.7.0] - 2021-11-28
|
|
201
235
|
|
|
202
|
-
|
|
236
|
+
#### Fixed
|
|
203
237
|
|
|
204
238
|
- Update tests to avoid network requests #50
|
|
205
239
|
- Update dependencies across the board
|
|
206
240
|
|
|
207
|
-
|
|
241
|
+
#### Changed
|
|
208
242
|
|
|
209
243
|
- Switch to github actions instead of travis for CI.
|
|
210
244
|
|
|
211
|
-
|
|
245
|
+
### [0.6.1] - 2020-03-15
|
|
212
246
|
|
|
213
|
-
|
|
247
|
+
#### Fixed
|
|
214
248
|
|
|
215
249
|
- Added the function to load JSON, on the tgwg.hosting object, for use in the sustaiable web sitespeed plugin.
|
|
216
250
|
|
|
217
|
-
|
|
251
|
+
### [0.6.0] - 2020-03-15
|
|
218
252
|
|
|
219
|
-
|
|
253
|
+
#### Added
|
|
220
254
|
|
|
221
255
|
- Added the hosting-JSON for running local checks against an array instead of SQLite.
|
|
222
256
|
|
|
223
|
-
|
|
257
|
+
#### Changed
|
|
224
258
|
|
|
225
259
|
- Swapped out checking against a sqlite database `hosting-json`in favour of simple array in,
|
|
226
260
|
- Updated conventions for style - using kebab-cases over CamelCase for naming files
|
|
227
261
|
|
|
228
|
-
|
|
262
|
+
#### Removed
|
|
229
263
|
|
|
230
264
|
- Extracted sqlite usage and dependencies into a separate module, `url2green`. This means you no longer need to compile SQLite on install.
|
|
231
265
|
|
|
232
|
-
|
|
266
|
+
### [0.5.0] - 2020-03-03
|
|
233
267
|
|
|
234
|
-
|
|
268
|
+
#### Changed
|
|
235
269
|
|
|
236
270
|
- Updated README
|
|
237
271
|
- Updated the emissions figured for green energy after further research on methodology with @@JamieBeevor
|
|
238
272
|
- Incorporated class based CO2 models from @soulgalore
|
|
239
273
|
- Credit contributors
|
|
240
274
|
|
|
241
|
-
|
|
275
|
+
### [0.4.7] - 2020-03-02
|
|
242
276
|
|
|
243
|
-
|
|
277
|
+
#### Added
|
|
244
278
|
|
|
245
279
|
- Added a changelog at last!
|
|
246
280
|
|
|
247
|
-
|
|
281
|
+
### [0.4.6] - 2020-03-01
|
|
248
282
|
|
|
249
|
-
|
|
283
|
+
#### Added
|
|
250
284
|
|
|
251
285
|
- Changelog inconsistency section in Bad Practices
|
|
252
286
|
|
|
253
|
-
|
|
287
|
+
### [0.4.4] - 2020-03-01
|
|
254
288
|
|
|
255
|
-
|
|
289
|
+
#### Added
|
|
256
290
|
|
|
257
291
|
Added the (currently unused) green byte model.
|
|
258
292
|
|
|
259
|
-
|
|
293
|
+
#### Changed
|
|
260
294
|
|
|
261
295
|
Update the 1byte model to use an average of devices, rather than just wifi
|
|
262
296
|
|
|
263
|
-
|
|
297
|
+
### [0.4.3] - 2020-03-01
|
|
264
298
|
|
|
265
|
-
|
|
299
|
+
#### Added
|
|
266
300
|
|
|
267
|
-
|
|
301
|
+
#### Changed
|
|
268
302
|
|
|
269
303
|
Split hosting API into two separate files (one for sqlite, and one relying on the greencheck API)
|
|
@@ -18,6 +18,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
18
18
|
var helpers_exports = {};
|
|
19
19
|
__export(helpers_exports, {
|
|
20
20
|
formatNumber: () => formatNumber,
|
|
21
|
+
getApiRequestHeaders: () => getApiRequestHeaders,
|
|
21
22
|
parseOptions: () => parseOptions
|
|
22
23
|
});
|
|
23
24
|
module.exports = __toCommonJS(helpers_exports);
|
|
@@ -159,4 +160,7 @@ Falling back to default value.`);
|
|
|
159
160
|
}
|
|
160
161
|
return adjustments;
|
|
161
162
|
}
|
|
163
|
+
function getApiRequestHeaders(comment = "") {
|
|
164
|
+
return { "User-Agent": `co2js/${"0.14.2"} ${comment}` };
|
|
165
|
+
}
|
|
162
166
|
//# sourceMappingURL=index.js.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/helpers/index.js"],
|
|
4
|
-
"sourcesContent": ["import { averageIntensity } from \"../index.js\";\nimport {\n GLOBAL_GRID_INTENSITY,\n PERCENTAGE_OF_DATA_LOADED_ON_SUBSEQUENT_LOAD,\n FIRST_TIME_VIEWING_PERCENTAGE,\n RETURNING_VISITOR_PERCENTAGE,\n} from \"../constants/index.js\";\nconst formatNumber = (num) => parseFloat(num.toFixed(2));\n\nfunction parseOptions(options) {\n // CHeck that it is an object\n if (typeof options !== \"object\") {\n throw new Error(\"Options must be an object\");\n }\n\n const adjustments = {};\n\n if (options?.gridIntensity) {\n adjustments.gridIntensity = {};\n const { device, dataCenter, network } = options.gridIntensity;\n if (device || device === 0) {\n if (typeof device === \"object\") {\n if (!averageIntensity.data[device.country?.toUpperCase()]) {\n console.warn(\n `\"${device.country}\" is not a valid country. Please use a valid 3 digit ISO 3166 country code. \\nSee https://developers.thegreenwebfoundation.org/co2js/data/ for more information. \\nFalling back to global average grid intensity.`\n );\n adjustments.gridIntensity[\"device\"] = {\n value: GLOBAL_GRID_INTENSITY,\n };\n }\n adjustments.gridIntensity[\"device\"] = {\n country: device.country,\n value: parseFloat(\n averageIntensity.data[device.country?.toUpperCase()]\n ),\n };\n } else if (typeof device === \"number\") {\n adjustments.gridIntensity[\"device\"] = {\n value: device,\n };\n } else {\n adjustments.gridIntensity[\"device\"] = {\n value: GLOBAL_GRID_INTENSITY,\n };\n console.warn(\n `The device grid intensity must be a number or an object. You passed in a ${typeof device}. \\nFalling back to global average grid intensity.`\n );\n }\n }\n if (dataCenter || dataCenter === 0) {\n if (typeof dataCenter === \"object\") {\n if (!averageIntensity.data[dataCenter.country?.toUpperCase()]) {\n console.warn(\n `\"${dataCenter.country}\" is not a valid country. Please use a valid 3 digit ISO 3166 country code. \\nSee https://developers.thegreenwebfoundation.org/co2js/data/ for more information. \\nFalling back to global average grid intensity.`\n );\n adjustments.gridIntensity[\"dataCenter\"] = {\n value: GLOBAL_GRID_INTENSITY,\n };\n }\n adjustments.gridIntensity[\"dataCenter\"] = {\n country: dataCenter.country,\n value: parseFloat(\n averageIntensity.data[dataCenter.country?.toUpperCase()]\n ),\n };\n } else if (typeof dataCenter === \"number\") {\n adjustments.gridIntensity[\"dataCenter\"] = {\n value: dataCenter,\n };\n } else {\n adjustments.gridIntensity[\"dataCenter\"] = {\n value: GLOBAL_GRID_INTENSITY,\n };\n console.warn(\n `The data center grid intensity must be a number or an object. You passed in a ${typeof dataCenter}. \\nFalling back to global average grid intensity.`\n );\n }\n }\n if (network || network === 0) {\n if (typeof network === \"object\") {\n if (!averageIntensity.data[network.country?.toUpperCase()]) {\n console.warn(\n `\"${network.country}\" is not a valid country. Please use a valid 3 digit ISO 3166 country code. \\nSee https://developers.thegreenwebfoundation.org/co2js/data/ for more information. Falling back to global average grid intensity. \\nFalling back to global average grid intensity.`\n );\n adjustments.gridIntensity[\"network\"] = {\n value: GLOBAL_GRID_INTENSITY,\n };\n }\n adjustments.gridIntensity[\"network\"] = {\n country: network.country,\n value: parseFloat(\n averageIntensity.data[network.country?.toUpperCase()]\n ),\n };\n } else if (typeof network === \"number\") {\n adjustments.gridIntensity[\"network\"] = {\n value: network,\n };\n } else {\n adjustments.gridIntensity[\"network\"] = {\n value: GLOBAL_GRID_INTENSITY,\n };\n console.warn(\n `The network grid intensity must be a number or an object. You passed in a ${typeof network}. \\nFalling back to global average grid intensity.`\n );\n }\n }\n }\n\n if (options?.dataReloadRatio || options.dataReloadRatio === 0) {\n if (typeof options.dataReloadRatio === \"number\") {\n if (options.dataReloadRatio >= 0 && options.dataReloadRatio <= 1) {\n adjustments.dataReloadRatio = options.dataReloadRatio;\n } else {\n adjustments.dataReloadRatio =\n PERCENTAGE_OF_DATA_LOADED_ON_SUBSEQUENT_LOAD;\n console.warn(\n `The dataReloadRatio option must be a number between 0 and 1. You passed in ${options.dataReloadRatio}. \\nFalling back to default value.`\n );\n }\n } else {\n adjustments.dataReloadRatio =\n PERCENTAGE_OF_DATA_LOADED_ON_SUBSEQUENT_LOAD;\n console.warn(\n `The dataReloadRatio option must be a number. You passed in a ${typeof options.dataReloadRatio}. \\nFalling back to default value.`\n );\n }\n }\n\n if (options?.firstVisitPercentage || options.firstVisitPercentage === 0) {\n if (typeof options.firstVisitPercentage === \"number\") {\n if (\n options.firstVisitPercentage >= 0 &&\n options.firstVisitPercentage <= 1\n ) {\n adjustments.firstVisitPercentage = options.firstVisitPercentage;\n } else {\n adjustments.firstVisitPercentage = FIRST_TIME_VIEWING_PERCENTAGE;\n console.warn(\n `The firstVisitPercentage option must be a number between 0 and 1. You passed in ${options.firstVisitPercentage}. \\nFalling back to default value.`\n );\n }\n } else {\n adjustments.firstVisitPercentage = FIRST_TIME_VIEWING_PERCENTAGE;\n console.warn(\n `The firstVisitPercentage option must be a number. You passed in a ${typeof options.firstVisitPercentage}. \\nFalling back to default value.`\n );\n }\n }\n\n if (options?.returnVisitPercentage || options.returnVisitPercentage === 0) {\n if (typeof options.returnVisitPercentage === \"number\") {\n if (\n options.returnVisitPercentage >= 0 &&\n options.returnVisitPercentage <= 1\n ) {\n adjustments.returnVisitPercentage = options.returnVisitPercentage;\n } else {\n adjustments.returnVisitPercentage = RETURNING_VISITOR_PERCENTAGE;\n console.warn(\n `The returnVisitPercentage option must be a number between 0 and 1. You passed in ${options.returnVisitPercentage}. \\nFalling back to default value.`\n );\n }\n } else {\n adjustments.returnVisitPercentage = RETURNING_VISITOR_PERCENTAGE;\n console.warn(\n `The returnVisitPercentage option must be a number. You passed in a ${typeof options.returnVisitPercentage}. \\nFalling back to default value.`\n );\n }\n }\n\n return adjustments;\n}\n\nexport { formatNumber, parseOptions };\n"],
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,eAAiC;AACjC,uBAKO;AACP,MAAM,eAAe,CAAC,QAAQ,WAAW,IAAI,QAAQ,CAAC,CAAC;AAEvD,sBAAsB,SAAS;AAT/B;AAWE,MAAI,OAAO,YAAY,UAAU;AAC/B,UAAM,IAAI,MAAM,2BAA2B;AAAA,EAC7C;AAEA,QAA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AAO;AACL,oBAAY,uBAAuB;AACnC,gBAAQ,KACN,mFAAmF,QAAQ;AAAA,+BAC7F;AAAA,MACF;AAAA,IACF,OAAO;AACL,kBAAY,uBAAuB;AACnC,cAAQ,KACN,qEAAqE,OAAO,QAAQ;AAAA,+BACtF;AAAA,IACF;AAAA,EACF;AAEA,MAAI,oCAAS,0BAAyB,QAAQ,0BAA0B,GAAG;AACzE,QAAI,OAAO,QAAQ,0BAA0B,UAAU;AACrD,UACE,QAAQ,yBAAyB,KACjC,QAAQ,yBAAyB,GACjC;AACA,oBAAY,wBAAwB,QAAQ;AAAA,MAC9C,OAAO;AACL,oBAAY,wBAAwB;AACpC,gBAAQ,KACN,oFAAoF,QAAQ;AAAA,+BAC9F;AAAA,MACF;AAAA,IACF,OAAO;AACL,kBAAY,wBAAwB;AACpC,cAAQ,KACN,sEAAsE,OAAO,QAAQ;AAAA,+BACvF;AAAA,IACF;AAAA,EACF;AAEA,SAAO;AACT;",
|
|
4
|
+
"sourcesContent": ["import { averageIntensity } from \"../index.js\";\nimport {\n GLOBAL_GRID_INTENSITY,\n PERCENTAGE_OF_DATA_LOADED_ON_SUBSEQUENT_LOAD,\n FIRST_TIME_VIEWING_PERCENTAGE,\n RETURNING_VISITOR_PERCENTAGE,\n} from \"../constants/index.js\";\nconst formatNumber = (num) => parseFloat(num.toFixed(2));\n\nfunction parseOptions(options) {\n // CHeck that it is an object\n if (typeof options !== \"object\") {\n throw new Error(\"Options must be an object\");\n }\n\n const adjustments = {};\n\n if (options?.gridIntensity) {\n adjustments.gridIntensity = {};\n const { device, dataCenter, network } = options.gridIntensity;\n if (device || device === 0) {\n if (typeof device === \"object\") {\n if (!averageIntensity.data[device.country?.toUpperCase()]) {\n console.warn(\n `\"${device.country}\" is not a valid country. Please use a valid 3 digit ISO 3166 country code. \\nSee https://developers.thegreenwebfoundation.org/co2js/data/ for more information. \\nFalling back to global average grid intensity.`\n );\n adjustments.gridIntensity[\"device\"] = {\n value: GLOBAL_GRID_INTENSITY,\n };\n }\n adjustments.gridIntensity[\"device\"] = {\n country: device.country,\n value: parseFloat(\n averageIntensity.data[device.country?.toUpperCase()]\n ),\n };\n } else if (typeof device === \"number\") {\n adjustments.gridIntensity[\"device\"] = {\n value: device,\n };\n } else {\n adjustments.gridIntensity[\"device\"] = {\n value: GLOBAL_GRID_INTENSITY,\n };\n console.warn(\n `The device grid intensity must be a number or an object. You passed in a ${typeof device}. \\nFalling back to global average grid intensity.`\n );\n }\n }\n if (dataCenter || dataCenter === 0) {\n if (typeof dataCenter === \"object\") {\n if (!averageIntensity.data[dataCenter.country?.toUpperCase()]) {\n console.warn(\n `\"${dataCenter.country}\" is not a valid country. Please use a valid 3 digit ISO 3166 country code. \\nSee https://developers.thegreenwebfoundation.org/co2js/data/ for more information. \\nFalling back to global average grid intensity.`\n );\n adjustments.gridIntensity[\"dataCenter\"] = {\n value: GLOBAL_GRID_INTENSITY,\n };\n }\n adjustments.gridIntensity[\"dataCenter\"] = {\n country: dataCenter.country,\n value: parseFloat(\n averageIntensity.data[dataCenter.country?.toUpperCase()]\n ),\n };\n } else if (typeof dataCenter === \"number\") {\n adjustments.gridIntensity[\"dataCenter\"] = {\n value: dataCenter,\n };\n } else {\n adjustments.gridIntensity[\"dataCenter\"] = {\n value: GLOBAL_GRID_INTENSITY,\n };\n console.warn(\n `The data center grid intensity must be a number or an object. You passed in a ${typeof dataCenter}. \\nFalling back to global average grid intensity.`\n );\n }\n }\n if (network || network === 0) {\n if (typeof network === \"object\") {\n if (!averageIntensity.data[network.country?.toUpperCase()]) {\n console.warn(\n `\"${network.country}\" is not a valid country. Please use a valid 3 digit ISO 3166 country code. \\nSee https://developers.thegreenwebfoundation.org/co2js/data/ for more information. Falling back to global average grid intensity. \\nFalling back to global average grid intensity.`\n );\n adjustments.gridIntensity[\"network\"] = {\n value: GLOBAL_GRID_INTENSITY,\n };\n }\n adjustments.gridIntensity[\"network\"] = {\n country: network.country,\n value: parseFloat(\n averageIntensity.data[network.country?.toUpperCase()]\n ),\n };\n } else if (typeof network === \"number\") {\n adjustments.gridIntensity[\"network\"] = {\n value: network,\n };\n } else {\n adjustments.gridIntensity[\"network\"] = {\n value: GLOBAL_GRID_INTENSITY,\n };\n console.warn(\n `The network grid intensity must be a number or an object. You passed in a ${typeof network}. \\nFalling back to global average grid intensity.`\n );\n }\n }\n }\n\n if (options?.dataReloadRatio || options.dataReloadRatio === 0) {\n if (typeof options.dataReloadRatio === \"number\") {\n if (options.dataReloadRatio >= 0 && options.dataReloadRatio <= 1) {\n adjustments.dataReloadRatio = options.dataReloadRatio;\n } else {\n adjustments.dataReloadRatio =\n PERCENTAGE_OF_DATA_LOADED_ON_SUBSEQUENT_LOAD;\n console.warn(\n `The dataReloadRatio option must be a number between 0 and 1. You passed in ${options.dataReloadRatio}. \\nFalling back to default value.`\n );\n }\n } else {\n adjustments.dataReloadRatio =\n PERCENTAGE_OF_DATA_LOADED_ON_SUBSEQUENT_LOAD;\n console.warn(\n `The dataReloadRatio option must be a number. You passed in a ${typeof options.dataReloadRatio}. \\nFalling back to default value.`\n );\n }\n }\n\n if (options?.firstVisitPercentage || options.firstVisitPercentage === 0) {\n if (typeof options.firstVisitPercentage === \"number\") {\n if (\n options.firstVisitPercentage >= 0 &&\n options.firstVisitPercentage <= 1\n ) {\n adjustments.firstVisitPercentage = options.firstVisitPercentage;\n } else {\n adjustments.firstVisitPercentage = FIRST_TIME_VIEWING_PERCENTAGE;\n console.warn(\n `The firstVisitPercentage option must be a number between 0 and 1. You passed in ${options.firstVisitPercentage}. \\nFalling back to default value.`\n );\n }\n } else {\n adjustments.firstVisitPercentage = FIRST_TIME_VIEWING_PERCENTAGE;\n console.warn(\n `The firstVisitPercentage option must be a number. You passed in a ${typeof options.firstVisitPercentage}. \\nFalling back to default value.`\n );\n }\n }\n\n if (options?.returnVisitPercentage || options.returnVisitPercentage === 0) {\n if (typeof options.returnVisitPercentage === \"number\") {\n if (\n options.returnVisitPercentage >= 0 &&\n options.returnVisitPercentage <= 1\n ) {\n adjustments.returnVisitPercentage = options.returnVisitPercentage;\n } else {\n adjustments.returnVisitPercentage = RETURNING_VISITOR_PERCENTAGE;\n console.warn(\n `The returnVisitPercentage option must be a number between 0 and 1. You passed in ${options.returnVisitPercentage}. \\nFalling back to default value.`\n );\n }\n } else {\n adjustments.returnVisitPercentage = RETURNING_VISITOR_PERCENTAGE;\n console.warn(\n `The returnVisitPercentage option must be a number. You passed in a ${typeof options.returnVisitPercentage}. \\nFalling back to default value.`\n );\n }\n }\n\n return adjustments;\n}\n\n/**\n * Returns an object containing all the HTTP headers to use when making a request to the Green Web Foundation API.\n * @param {string} comment - Optional. The app, site, or organisation that is making the request.\n *\n * @returns {import('http').OutgoingHttpHeaders}\n */\nfunction getApiRequestHeaders(comment = \"\") {\n return { \"User-Agent\": `co2js/${process.env.CO2JS_VERSION} ${comment}` };\n}\n\nexport { formatNumber, parseOptions, getApiRequestHeaders };\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,eAAiC;AACjC,uBAKO;AACP,MAAM,eAAe,CAAC,QAAQ,WAAW,IAAI,QAAQ,CAAC,CAAC;AAEvD,sBAAsB,SAAS;AAT/B;AAWE,MAAI,OAAO,YAAY,UAAU;AAC/B,UAAM,IAAI,MAAM,2BAA2B;AAAA,EAC7C;AAEA,QAAM,cAAc,CAAC;AAErB,MAAI,mCAAS,eAAe;AAC1B,gBAAY,gBAAgB,CAAC;AAC7B,UAAM,EAAE,QAAQ,YAAY,YAAY,QAAQ;AAChD,QAAI,UAAU,WAAW,GAAG;AAC1B,UAAI,OAAO,WAAW,UAAU;AAC9B,YAAI,CAAC,0BAAiB,KAAK,aAAO,YAAP,mBAAgB,gBAAgB;AACzD,kBAAQ,KACN,IAAI,OAAO;AAAA;AAAA,+CACb;AACA,sBAAY,cAAc,YAAY;AAAA,YACpC,OAAO;AAAA,UACT;AAAA,QACF;AACA,oBAAY,cAA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;AAAA,MAC9C,OAAO;AACL,oBAAY,wBAAwB;AACpC,gBAAQ,KACN,oFAAoF,QAAQ;AAAA,+BAC9F;AAAA,MACF;AAAA,IACF,OAAO;AACL,kBAAY,wBAAwB;AACpC,cAAQ,KACN,sEAAsE,OAAO,QAAQ;AAAA,+BACvF;AAAA,IACF;AAAA,EACF;AAEA,SAAO;AACT;AAQA,8BAA8B,UAAU,IAAI;AAC1C,SAAO,EAAE,cAAc,SAAS,YAA6B,UAAU;AACzE;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/dist/cjs/hosting-api.js
CHANGED
|
@@ -21,23 +21,28 @@ __export(hosting_api_exports, {
|
|
|
21
21
|
default: () => hosting_api_default
|
|
22
22
|
});
|
|
23
23
|
module.exports = __toCommonJS(hosting_api_exports);
|
|
24
|
-
|
|
24
|
+
var import_helpers = require("./helpers/index.js");
|
|
25
|
+
function check(domain, userAgentIdentifier) {
|
|
25
26
|
if (typeof domain === "string") {
|
|
26
|
-
return checkAgainstAPI(domain);
|
|
27
|
+
return checkAgainstAPI(domain, userAgentIdentifier);
|
|
27
28
|
} else {
|
|
28
|
-
return checkDomainsAgainstAPI(domain);
|
|
29
|
+
return checkDomainsAgainstAPI(domain, userAgentIdentifier);
|
|
29
30
|
}
|
|
30
31
|
}
|
|
31
|
-
async function checkAgainstAPI(domain) {
|
|
32
|
-
const req = await fetch(`https://api.thegreenwebfoundation.org/greencheck/${domain}
|
|
32
|
+
async function checkAgainstAPI(domain, userAgentIdentifier) {
|
|
33
|
+
const req = await fetch(`https://api.thegreenwebfoundation.org/greencheck/${domain}`, {
|
|
34
|
+
headers: (0, import_helpers.getApiRequestHeaders)(userAgentIdentifier)
|
|
35
|
+
});
|
|
33
36
|
const res = await req.json();
|
|
34
37
|
return res.green;
|
|
35
38
|
}
|
|
36
|
-
async function checkDomainsAgainstAPI(domains) {
|
|
39
|
+
async function checkDomainsAgainstAPI(domains, userAgentIdentifier) {
|
|
37
40
|
try {
|
|
38
41
|
const apiPath = "https://api.thegreenwebfoundation.org/v2/greencheckmulti";
|
|
39
42
|
const domainsString = JSON.stringify(domains);
|
|
40
|
-
const req = await fetch(`${apiPath}/${domainsString}
|
|
43
|
+
const req = await fetch(`${apiPath}/${domainsString}`, {
|
|
44
|
+
headers: (0, import_helpers.getApiRequestHeaders)(userAgentIdentifier)
|
|
45
|
+
});
|
|
41
46
|
const allGreenCheckResults = await req.json();
|
|
42
47
|
return greenDomainsFromResults(allGreenCheckResults);
|
|
43
48
|
} catch (e) {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/hosting-api.js"],
|
|
4
|
-
"sourcesContent": ["\"use strict\";\n\n/**\n * Check if a string or array of domains has been provided\n * @param {string|array} domain - The domain to check, or an array of domains to be checked.\n */\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\n/**\n * Check if a domain is hosted by a green web host by querying the Green Web Foundation API.\n * @param {string} domain - The domain to check.\n * @returns {boolean} - A boolean indicating whether the domain is hosted by a green web host.\n */\nasync function checkAgainstAPI(domain) {\n const req = await fetch(\n `https://api.thegreenwebfoundation.org/greencheck/${domain}
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;
|
|
4
|
+
"sourcesContent": ["\"use strict\";\n\nimport { getApiRequestHeaders } from \"./helpers/index.js\";\n\n/**\n * Check if a string or array of domains has been provided\n * @param {string|array} domain - The domain to check, or an array of domains to be checked.\n * @param {string} userAgentIdentifier - Optional. The app, site, or organisation that is making the request.\n */\n\nfunction check(domain, userAgentIdentifier) {\n // is it a single domain or an array of them?\n if (typeof domain === \"string\") {\n return checkAgainstAPI(domain, userAgentIdentifier);\n } else {\n return checkDomainsAgainstAPI(domain, userAgentIdentifier);\n }\n}\n\n/**\n * Check if a domain is hosted by a green web host by querying the Green Web Foundation API.\n * @param {string} domain - The domain to check.\n * @param {string} userAgentIdentifier - Optional. The app, site, or organisation that is making the request.\n * @returns {boolean} - A boolean indicating whether the domain is hosted by a green web host.\n */\nasync function checkAgainstAPI(domain, userAgentIdentifier) {\n const req = await fetch(\n `https://api.thegreenwebfoundation.org/greencheck/${domain}`,\n {\n headers: getApiRequestHeaders(userAgentIdentifier),\n }\n );\n const res = await req.json();\n return res.green;\n}\n\n/**\n * Check if an array of domains is hosted by a green web host by querying the Green Web Foundation API.\n * @param {array} domains - An array of domains to check.\n * @param {string} userAgentIdentifier - Optional. The app, site, or organisation that is making the request.\n * @returns {array} - An array of domains that are hosted by a green web host.\n */\n\nasync function checkDomainsAgainstAPI(domains, userAgentIdentifier) {\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 headers: getApiRequestHeaders(userAgentIdentifier),\n });\n\n const allGreenCheckResults = await req.json();\n\n return greenDomainsFromResults(allGreenCheckResults);\n } catch (e) {\n return [];\n }\n}\n\n/**\n * Extract the green domains from the results of a green check.\n * @param {object} greenResults - The results of a green check.\n * @returns {array} - An array of domains that are hosted by a green web host.\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,qBAAqC;AAQrC,eAAe,QAAQ,qBAAqB;AAE1C,MAAI,OAAO,WAAW,UAAU;AAC9B,WAAO,gBAAgB,QAAQ,mBAAmB;AAAA,EACpD,OAAO;AACL,WAAO,uBAAuB,QAAQ,mBAAmB;AAAA,EAC3D;AACF;AAQA,+BAA+B,QAAQ,qBAAqB;AAC1D,QAAM,MAAM,MAAM,MAChB,oDAAoD,UACpD;AAAA,IACE,SAAS,yCAAqB,mBAAmB;AAAA,EACnD,CACF;AACA,QAAM,MAAM,MAAM,IAAI,KAAK;AAC3B,SAAO,IAAI;AACb;AASA,sCAAsC,SAAS,qBAAqB;AAClE,MAAI;AACF,UAAM,UAAU;AAChB,UAAM,gBAAgB,KAAK,UAAU,OAAO;AAE5C,UAAM,MAAM,MAAM,MAAM,GAAG,WAAW,iBAAiB;AAAA,MACrD,SAAS,yCAAqB,mBAAmB;AAAA,IACnD,CAAC;AAED,UAAM,uBAAuB,MAAM,IAAI,KAAK;AAE5C,WAAO,wBAAwB,oBAAoB;AAAA,EACrD,SAAS,GAAP;AACA,WAAO,CAAC;AAAA,EACV;AACF;AAOA,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
6
|
"names": []
|
|
7
7
|
}
|
package/dist/cjs/hosting-node.js
CHANGED
|
@@ -25,9 +25,10 @@ __export(hosting_node_exports, {
|
|
|
25
25
|
module.exports = __toCommonJS(hosting_node_exports);
|
|
26
26
|
var import_https = __toESM(require("https"));
|
|
27
27
|
var import_hosting_json_node = __toESM(require("./hosting-json.node.js"));
|
|
28
|
-
|
|
28
|
+
var import_helpers = require("./helpers/index.js");
|
|
29
|
+
async function getBody(url, userAgentIdentifier) {
|
|
29
30
|
return new Promise(function(resolve, reject) {
|
|
30
|
-
const req = import_https.default.get(url, function(res) {
|
|
31
|
+
const req = import_https.default.get(url, { headers: (0, import_helpers.getApiRequestHeaders)(userAgentIdentifier) }, function(res) {
|
|
31
32
|
if (res.statusCode < 200 || res.statusCode >= 300) {
|
|
32
33
|
return reject(new Error(`Could not get info from: ${url}. Status Code: ${res.statusCode}`));
|
|
33
34
|
}
|
|
@@ -40,23 +41,23 @@ async function getBody(url) {
|
|
|
40
41
|
req.end();
|
|
41
42
|
});
|
|
42
43
|
}
|
|
43
|
-
function check(domain, db) {
|
|
44
|
+
function check(domain, db, userAgentIdentifier) {
|
|
44
45
|
if (db) {
|
|
45
46
|
return import_hosting_json_node.default.check(domain, db);
|
|
46
47
|
}
|
|
47
48
|
if (typeof domain === "string") {
|
|
48
|
-
return checkAgainstAPI(domain);
|
|
49
|
+
return checkAgainstAPI(domain, userAgentIdentifier);
|
|
49
50
|
} else {
|
|
50
|
-
return checkDomainsAgainstAPI(domain);
|
|
51
|
+
return checkDomainsAgainstAPI(domain, userAgentIdentifier);
|
|
51
52
|
}
|
|
52
53
|
}
|
|
53
|
-
async function checkAgainstAPI(domain) {
|
|
54
|
-
const res = JSON.parse(await getBody(`https://api.thegreenwebfoundation.org/greencheck/${domain}
|
|
54
|
+
async function checkAgainstAPI(domain, userAgentIdentifier) {
|
|
55
|
+
const res = JSON.parse(await getBody(`https://api.thegreenwebfoundation.org/greencheck/${domain}`, userAgentIdentifier));
|
|
55
56
|
return res.green;
|
|
56
57
|
}
|
|
57
|
-
async function checkDomainsAgainstAPI(domains) {
|
|
58
|
+
async function checkDomainsAgainstAPI(domains, userAgentIdentifier) {
|
|
58
59
|
try {
|
|
59
|
-
const allGreenCheckResults = JSON.parse(await getBody(`https://api.thegreenwebfoundation.org/v2/greencheckmulti/${JSON.stringify(domains)}
|
|
60
|
+
const allGreenCheckResults = JSON.parse(await getBody(`https://api.thegreenwebfoundation.org/v2/greencheckmulti/${JSON.stringify(domains)}`, userAgentIdentifier));
|
|
60
61
|
return import_hosting_json_node.default.greenDomainsFromResults(allGreenCheckResults);
|
|
61
62
|
} catch (e) {
|
|
62
63
|
return [];
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/hosting-node.js"],
|
|
4
|
-
"sourcesContent": ["/*\n\nWe have a separate node-specific hosting.js file for node.\nThis uses the node-specific APIs for making http requests,\nand doing lookups against local JSON and sqlite databases.\nThis is used in the CommonJS build of co2.js\n\nThis lets us keep the total library small, and dependencies minimal.\n*/\n\nimport https from \"https\";\n\nimport hostingJSON from \"./hosting-json.node.js\";\n\n/**\n * Accept a url and perform an http request, returning the body\n * for parsing as JSON.\n *\n * @param {string} url\n * @return {string}\n */\nasync function getBody(url) {\n return new Promise(function (resolve, reject) {\n // Do async job\n const req = https.get(url
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAUA,mBAAkB;AAElB,+BAAwB;
|
|
4
|
+
"sourcesContent": ["/*\n\nWe have a separate node-specific hosting.js file for node.\nThis uses the node-specific APIs for making http requests,\nand doing lookups against local JSON and sqlite databases.\nThis is used in the CommonJS build of co2.js\n\nThis lets us keep the total library small, and dependencies minimal.\n*/\n\nimport https from \"https\";\n\nimport hostingJSON from \"./hosting-json.node.js\";\nimport { getApiRequestHeaders } from \"./helpers/index.js\";\n\n/**\n * Accept a url and perform an http request, returning the body\n * for parsing as JSON.\n *\n * @param {string} url\n * @param {string} userAgentIdentifier - Optional. The app, site, or organisation that is making the request.\n * @return {string}\n */\nasync function getBody(url, userAgentIdentifier) {\n return new Promise(function (resolve, reject) {\n // Do async job\n const req = https.get(\n url,\n { headers: getApiRequestHeaders(userAgentIdentifier) },\n function (res) {\n if (res.statusCode < 200 || res.statusCode >= 300) {\n return reject(\n new Error(\n `Could not get info from: ${url}. Status Code: ${res.statusCode}`\n )\n );\n }\n const data = [];\n\n res.on(\"data\", (chunk) => {\n data.push(chunk);\n });\n\n res.on(\"end\", () => resolve(Buffer.concat(data).toString()));\n }\n );\n req.end();\n });\n}\n\n/**\n * Check if a domain is hosted by a green web host.\n * @param {string|array} domain - The domain to check, or an array of domains to be checked.\n * @param {object} db - Optional. A database object to use for lookups.\n * @param {string} userAgentIdentifier - Optional. The app, site, or organisation that is making the request.\n * @returns {boolean|array} - A boolean if a string was provided, or an array of booleans if an array of domains was provided.\n */\n\nfunction check(domain, db, userAgentIdentifier) {\n if (db) {\n return hostingJSON.check(domain, db);\n }\n\n // is it a single domain or an array of them?\n if (typeof domain === \"string\") {\n return checkAgainstAPI(domain, userAgentIdentifier);\n } else {\n return checkDomainsAgainstAPI(domain, userAgentIdentifier);\n }\n}\n\n/**\n * Check if a domain is hosted by a green web host by querying the Green Web Foundation API.\n * @param {string} domain - The domain to check.\n * @param {string} userAgentIdentifier - Optional. The app, site, or organisation that is making the request.\n * @returns {boolean} - A boolean indicating whether the domain is hosted by a green web host.\n */\nasync function checkAgainstAPI(domain, userAgentIdentifier) {\n const res = JSON.parse(\n await getBody(\n `https://api.thegreenwebfoundation.org/greencheck/${domain}`,\n userAgentIdentifier\n )\n );\n return res.green;\n}\n\n/**\n * Check if an array of domains is hosted by a green web host by querying the Green Web Foundation API.\n * @param {array} domains - An array of domains to check.\n * @param {string} userAgentIdentifier - Optional. The app, site, or organisation that is making the request.\n * @returns {array} - An array of domains that are hosted by a green web host.\n */\nasync function checkDomainsAgainstAPI(domains, userAgentIdentifier) {\n try {\n const allGreenCheckResults = JSON.parse(\n await getBody(\n `https://api.thegreenwebfoundation.org/v2/greencheckmulti/${JSON.stringify(\n domains\n )}`,\n userAgentIdentifier\n )\n );\n return hostingJSON.greenDomainsFromResults(allGreenCheckResults);\n } catch (e) {\n return [];\n }\n}\n\n/**\n * Take the result of a pageXray and check the domains in it against the database.\n * @param {object} pageXray - The result of a pageXray.\n * @param {object} db - A database object to use for lookups.\n * @returns {array} - An array indicating whether the domain is hosted by a green web host.\n */\nasync function checkPage(pageXray, db) {\n const domains = Object.keys(pageXray.domains);\n return check(domains, db);\n}\n\nexport default {\n check,\n checkPage,\n greendomains: hostingJSON.greenDomainsFromResults,\n loadJSON: hostingJSON.loadJSON,\n};\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;A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AO,MAAM,SAAS,EAAE;AAC1B;AAEA,IAAO,uBAAQ;AAAA,EACb;AAAA,EACA;AAAA,EACA,cAAc,iCAAY;AAAA,EAC1B,UAAU,iCAAY;AACxB;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/dist/cjs/hosting.js
CHANGED
|
@@ -25,8 +25,8 @@ __export(hosting_exports, {
|
|
|
25
25
|
});
|
|
26
26
|
module.exports = __toCommonJS(hosting_exports);
|
|
27
27
|
var import_hosting_api = __toESM(require("./hosting-api.js"));
|
|
28
|
-
function check(domain) {
|
|
29
|
-
return import_hosting_api.default.check(domain);
|
|
28
|
+
function check(domain, userAgentIdentifier) {
|
|
29
|
+
return import_hosting_api.default.check(domain, userAgentIdentifier);
|
|
30
30
|
}
|
|
31
31
|
var hosting_default = {
|
|
32
32
|
check
|
package/dist/cjs/hosting.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/hosting.js"],
|
|
4
|
-
"sourcesContent": ["\"use strict\";\n\nimport hostingAPI from \"./hosting-api.js\";\n\n/**\n * Check if a domain is hosted by a green web host.\n * @param {string|array} domain - The domain to check, or an array of domains to be checked.\n * @returns {boolean|array} - A boolean if a string was provided, or an array of booleans if an array of domains was provided.\n */\nfunction check(domain) {\n return hostingAPI.check(domain);\n}\n\nexport default {\n check,\n};\n"],
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAEA,yBAAuB;
|
|
4
|
+
"sourcesContent": ["\"use strict\";\n\nimport hostingAPI from \"./hosting-api.js\";\n\n/**\n * Check if a domain is hosted by a green web host.\n * @param {string|array} domain - The domain to check, or an array of domains to be checked.\n * @param {string} userAgentIdentifier - Optional. The app, site, or organisation that is making the request.\n * @returns {boolean|array} - A boolean if a string was provided, or an array of booleans if an array of domains was provided.\n */\nfunction check(domain, userAgentIdentifier) {\n return hostingAPI.check(domain, userAgentIdentifier);\n}\n\nexport default {\n check,\n};\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAEA,yBAAuB;AAQvB,eAAe,QAAQ,qBAAqB;AAC1C,SAAO,2BAAW,MAAM,QAAQ,mBAAmB;AACrD;AAEA,IAAO,kBAAQ;AAAA,EACb;AACF;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -141,7 +141,11 @@ Falling back to default value.`);
|
|
|
141
141
|
}
|
|
142
142
|
return adjustments;
|
|
143
143
|
}
|
|
144
|
+
function getApiRequestHeaders(comment = "") {
|
|
145
|
+
return { "User-Agent": `co2js/${"0.14.2"} ${comment}` };
|
|
146
|
+
}
|
|
144
147
|
export {
|
|
145
148
|
formatNumber,
|
|
149
|
+
getApiRequestHeaders,
|
|
146
150
|
parseOptions
|
|
147
151
|
};
|
package/dist/esm/hosting-api.js
CHANGED
|
@@ -1,21 +1,26 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
2
|
+
import { getApiRequestHeaders } from "./helpers/index.js";
|
|
3
|
+
function check(domain, userAgentIdentifier) {
|
|
3
4
|
if (typeof domain === "string") {
|
|
4
|
-
return checkAgainstAPI(domain);
|
|
5
|
+
return checkAgainstAPI(domain, userAgentIdentifier);
|
|
5
6
|
} else {
|
|
6
|
-
return checkDomainsAgainstAPI(domain);
|
|
7
|
+
return checkDomainsAgainstAPI(domain, userAgentIdentifier);
|
|
7
8
|
}
|
|
8
9
|
}
|
|
9
|
-
async function checkAgainstAPI(domain) {
|
|
10
|
-
const req = await fetch(`https://api.thegreenwebfoundation.org/greencheck/${domain}
|
|
10
|
+
async function checkAgainstAPI(domain, userAgentIdentifier) {
|
|
11
|
+
const req = await fetch(`https://api.thegreenwebfoundation.org/greencheck/${domain}`, {
|
|
12
|
+
headers: getApiRequestHeaders(userAgentIdentifier)
|
|
13
|
+
});
|
|
11
14
|
const res = await req.json();
|
|
12
15
|
return res.green;
|
|
13
16
|
}
|
|
14
|
-
async function checkDomainsAgainstAPI(domains) {
|
|
17
|
+
async function checkDomainsAgainstAPI(domains, userAgentIdentifier) {
|
|
15
18
|
try {
|
|
16
19
|
const apiPath = "https://api.thegreenwebfoundation.org/v2/greencheckmulti";
|
|
17
20
|
const domainsString = JSON.stringify(domains);
|
|
18
|
-
const req = await fetch(`${apiPath}/${domainsString}
|
|
21
|
+
const req = await fetch(`${apiPath}/${domainsString}`, {
|
|
22
|
+
headers: getApiRequestHeaders(userAgentIdentifier)
|
|
23
|
+
});
|
|
19
24
|
const allGreenCheckResults = await req.json();
|
|
20
25
|
return greenDomainsFromResults(allGreenCheckResults);
|
|
21
26
|
} catch (e) {
|
package/dist/esm/hosting.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tgwf/co2",
|
|
3
|
-
"version": "0.14.
|
|
3
|
+
"version": "0.14.2",
|
|
4
4
|
"description": "Work out the co2 of your digital services",
|
|
5
5
|
"main": "dist/cjs/index-node.js",
|
|
6
6
|
"module": "dist/esm/index.js",
|
|
@@ -29,7 +29,8 @@
|
|
|
29
29
|
"intensity-data:average": "node data/functions/generate_average_co2.js",
|
|
30
30
|
"intensity-data:marginal": "node data/functions/generate_marginal_co2.js",
|
|
31
31
|
"intensity-data": "npm run intensity-data:average && npm run intensity-data:marginal && npm run format-data",
|
|
32
|
-
"format-data": "cd data && prettier --write '**/*.{js,json}'"
|
|
32
|
+
"format-data": "cd data && prettier --write '**/*.{js,json}'",
|
|
33
|
+
"version": "npm run build"
|
|
33
34
|
},
|
|
34
35
|
"keywords": [
|
|
35
36
|
"sustainability",
|