@tgwf/co2 0.16.2 → 0.16.3
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 +1 -0
- package/README.md +10 -4
- package/dist/cjs/helpers/index.js +27 -82
- package/dist/cjs/helpers/index.js.map +2 -2
- package/dist/esm/helpers/index.js +27 -82
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
17
17
|
- Add ability to create adaptors which provide access to external data sources #134
|
|
18
18
|
- Add access to the Electricity Maps API free tier #134
|
|
19
19
|
- Allow access to individual models and their internal functions #195
|
|
20
|
+
- Update the Github actions testing matrix to Node 18,20, and 22 #242
|
|
20
21
|
<!-- - _(Add a summary of your feature, and if relevant the issue, in your PR for merging into `main`)_ -->
|
|
21
22
|
|
|
22
23
|
## Released
|
package/README.md
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
# CO2.js
|
|
2
2
|
|
|
3
3
|
<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
|
|
4
|
-
|
|
5
|
-
[](#contributors-)
|
|
6
|
-
|
|
4
|
+
[](#contributors-)
|
|
7
5
|
<!-- ALL-CONTRIBUTORS-BADGE:END -->
|
|
8
6
|
|
|
9
7
|
<img src="https://github.com/thegreenwebfoundation/co2.js/actions/workflows/unittests.yml/badge.svg" />
|
|
@@ -24,7 +22,7 @@ Internally, you may want to use this library to create a _carbon budget_ for you
|
|
|
24
22
|
|
|
25
23
|
For user facing applications, CO2.js could be used to check & block the uploading of carbon intensive files. Or, to present users with information about the carbon impact of their online activities (such as browsing a website).
|
|
26
24
|
|
|
27
|
-
The above
|
|
25
|
+
The above are just a few examples of the many and varied ways CO2.js can be applied to provide carbon estimates for data transfer. If you’re using CO2.js in production we’d love to hear how! [Contact us](https://www.thegreenwebfoundation.org/support-form/) via our website.
|
|
28
26
|
|
|
29
27
|
## Installation
|
|
30
28
|
|
|
@@ -160,6 +158,14 @@ To contribute changes back to this project, please follow the steps outlined in
|
|
|
160
158
|
<td align="center" valign="top" width="14.28%"><a href="https://github.com/p-gerard"><img src="https://avatars.githubusercontent.com/u/97036756?v=4?s=100" width="100px;" alt="p-gerard"/><br /><sub><b>p-gerard</b></sub></a><br /><a href="https://github.com/thegreenwebfoundation/co2.js/issues?q=author%3Ap-gerard" title="Bug reports">🐛</a> <a href="https://github.com/thegreenwebfoundation/co2.js/commits?author=p-gerard" title="Code">💻</a></td>
|
|
161
159
|
<td align="center" valign="top" width="14.28%"><a href="https://github.com/sfishel18"><img src="https://avatars.githubusercontent.com/u/294695?v=4?s=100" width="100px;" alt="Simon Fishel"/><br /><sub><b>Simon Fishel</b></sub></a><br /><a href="https://github.com/thegreenwebfoundation/co2.js/commits?author=sfishel18" title="Code">💻</a></td>
|
|
162
160
|
</tr>
|
|
161
|
+
<tr>
|
|
162
|
+
<td align="center" valign="top" width="14.28%"><a href="https://github.com/dsubject"><img src="https://avatars.githubusercontent.com/u/33845418?s=64&v=4?s=100" width="100px;" alt="Dani Subject"/><br /><sub><b>Dani Subject</b></sub></a><br /><a href="https://github.com/thegreenwebfoundation/co2.js/commits?author=dsubject" title="Code">💻</a></td>
|
|
163
|
+
<td align="center" valign="top" width="14.28%"><a href="https://github.com/Malay-dev"><img src="https://avatars.githubusercontent.com/u/91375797?s=64&v=4?s=100" width="100px;" alt="Malay Kumar"/><br /><sub><b>Malay Kumar</b></sub></a><br /><a href="https://github.com/thegreenwebfoundation/co2.js/commits?author=Malay-dev" title="Documentation">📖</a></td>
|
|
164
|
+
<td align="center" valign="top" width="14.28%"><a href="https://github.com/samuelIkoli"><img src="https://avatars.githubusercontent.com/u/98848723?v=4?s=100" width="100px;" alt="Samuel Ikoli"/><br /><sub><b>Samuel Ikoli</b></sub></a><br /><a href="https://github.com/thegreenwebfoundation/co2.js/commits?author=samuelIkoli" title="Documentation">📖</a></td>
|
|
165
|
+
<td align="center" valign="top" width="14.28%"><a href="https://github.com/APJohns"><img src="https://avatars.githubusercontent.com/u/18357173?v=4?s=100" width="100px;" alt="Ash Johns"/><br /><sub><b>Ash Johns</b></sub></a><br /><a href="https://github.com/thegreenwebfoundation/co2.js/issues?q=author%3AAPJohns" title="Bug reports">🐛</a> <a href="https://github.com/thegreenwebfoundation/co2.js/commits?author=APJohns" title="Code">💻</a></td>
|
|
166
|
+
<td align="center" valign="top" width="14.28%"><a href="https://github.com/mgriffin-scottlogic"><img src="https://avatars.githubusercontent.com/u/117279304?v=4?s=100" width="100px;" alt="Matthew Griffin"/><br /><sub><b>Matthew Griffin</b></sub></a><br /><a href="https://github.com/thegreenwebfoundation/co2.js/commits?author=mgriffin-scottlogic" title="Documentation">📖</a> <a href="https://github.com/thegreenwebfoundation/co2.js/commits?author=mgriffin-scottlogic" title="Code">💻</a></td>
|
|
167
|
+
<td align="center" valign="top" width="14.28%"><a href="https://github.com/hanopcan"><img src="https://avatars.githubusercontent.com/u/11687898?v=4?s=100" width="100px;" alt="Hannah"/><br /><sub><b>Hannah</b></sub></a><br /><a href="https://github.com/thegreenwebfoundation/co2.js/commits?author=hanopcan" title="Documentation">📖</a></td>
|
|
168
|
+
</tr>
|
|
163
169
|
</tbody>
|
|
164
170
|
</table>
|
|
165
171
|
|
|
@@ -30,111 +30,56 @@ const SWDM4_GLOBAL_GRID_INTENSITY = import_constants.SWDV4.GLOBAL_GRID_INTENSITY
|
|
|
30
30
|
const formatNumber = (num) => parseFloat(num.toFixed(2));
|
|
31
31
|
const lessThanEqualTo = (num, limit) => num <= limit;
|
|
32
32
|
function parseOptions(options = {}, version = 3, green = false) {
|
|
33
|
-
var _a, _b, _c, _d, _e, _f;
|
|
34
33
|
const globalGridIntensity = version === 4 ? SWDM4_GLOBAL_GRID_INTENSITY : import_constants.GLOBAL_GRID_INTENSITY;
|
|
35
34
|
if (typeof options !== "object") {
|
|
36
35
|
throw new Error("Options must be an object");
|
|
37
36
|
}
|
|
38
37
|
const adjustments = {};
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
if (!import__.averageIntensity.data[(_a = device.country) == null ? void 0 : _a.toUpperCase()]) {
|
|
38
|
+
function setIntensity(segment, segmentIntensity) {
|
|
39
|
+
var _a, _b;
|
|
40
|
+
if (segmentIntensity || segmentIntensity === 0) {
|
|
41
|
+
if (typeof segmentIntensity === "object") {
|
|
42
|
+
if (!import__.averageIntensity.data[(_a = segmentIntensity.country) == null ? void 0 : _a.toUpperCase()]) {
|
|
45
43
|
console.warn(
|
|
46
|
-
`"${
|
|
44
|
+
`"${segmentIntensity.country}" is not a valid country. Please use a valid 3 digit ISO 3166 country code.
|
|
47
45
|
See https://developers.thegreenwebfoundation.org/co2js/data/ for more information.
|
|
48
46
|
Falling back to global average grid intensity.`
|
|
49
47
|
);
|
|
50
|
-
adjustments.gridIntensity[
|
|
48
|
+
adjustments.gridIntensity[segment] = {
|
|
51
49
|
value: globalGridIntensity
|
|
52
50
|
};
|
|
53
51
|
}
|
|
54
|
-
adjustments.gridIntensity[
|
|
55
|
-
country:
|
|
56
|
-
value: parseFloat(
|
|
57
|
-
import__.averageIntensity.data[(_b = device.country) == null ? void 0 : _b.toUpperCase()]
|
|
58
|
-
)
|
|
59
|
-
};
|
|
60
|
-
} else if (typeof device === "number") {
|
|
61
|
-
adjustments.gridIntensity["device"] = {
|
|
62
|
-
value: device
|
|
63
|
-
};
|
|
64
|
-
} else {
|
|
65
|
-
adjustments.gridIntensity["device"] = {
|
|
66
|
-
value: globalGridIntensity
|
|
67
|
-
};
|
|
68
|
-
console.warn(
|
|
69
|
-
`The device grid intensity must be a number or an object. You passed in a ${typeof device}.
|
|
70
|
-
Falling back to global average grid intensity.`
|
|
71
|
-
);
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
if (dataCenter || dataCenter === 0) {
|
|
75
|
-
if (typeof dataCenter === "object") {
|
|
76
|
-
if (!import__.averageIntensity.data[(_c = dataCenter.country) == null ? void 0 : _c.toUpperCase()]) {
|
|
77
|
-
console.warn(
|
|
78
|
-
`"${dataCenter.country}" is not a valid country. Please use a valid 3 digit ISO 3166 country code.
|
|
79
|
-
See https://developers.thegreenwebfoundation.org/co2js/data/ for more information.
|
|
80
|
-
Falling back to global average grid intensity.`
|
|
81
|
-
);
|
|
82
|
-
adjustments.gridIntensity["dataCenter"] = {
|
|
83
|
-
value: import_constants.GLOBAL_GRID_INTENSITY
|
|
84
|
-
};
|
|
85
|
-
}
|
|
86
|
-
adjustments.gridIntensity["dataCenter"] = {
|
|
87
|
-
country: dataCenter.country,
|
|
52
|
+
adjustments.gridIntensity[segment] = {
|
|
53
|
+
country: segmentIntensity.country,
|
|
88
54
|
value: parseFloat(
|
|
89
|
-
import__.averageIntensity.data[(
|
|
55
|
+
import__.averageIntensity.data[(_b = segmentIntensity.country) == null ? void 0 : _b.toUpperCase()]
|
|
90
56
|
)
|
|
91
57
|
};
|
|
92
|
-
} else if (typeof
|
|
93
|
-
adjustments.gridIntensity[
|
|
94
|
-
value:
|
|
58
|
+
} else if (typeof segmentIntensity === "number") {
|
|
59
|
+
adjustments.gridIntensity[segment] = {
|
|
60
|
+
value: segmentIntensity
|
|
95
61
|
};
|
|
96
62
|
} else {
|
|
97
|
-
adjustments.gridIntensity[
|
|
63
|
+
adjustments.gridIntensity[segment] = {
|
|
98
64
|
value: globalGridIntensity
|
|
99
65
|
};
|
|
100
66
|
console.warn(
|
|
101
|
-
`The
|
|
102
|
-
Falling back to global average grid intensity.`
|
|
103
|
-
);
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
if (network || network === 0) {
|
|
107
|
-
if (typeof network === "object") {
|
|
108
|
-
if (!import__.averageIntensity.data[(_e = network.country) == null ? void 0 : _e.toUpperCase()]) {
|
|
109
|
-
console.warn(
|
|
110
|
-
`"${network.country}" is not a valid country. Please use a valid 3 digit ISO 3166 country code.
|
|
111
|
-
See https://developers.thegreenwebfoundation.org/co2js/data/ for more information. Falling back to global average grid intensity.
|
|
112
|
-
Falling back to global average grid intensity.`
|
|
113
|
-
);
|
|
114
|
-
adjustments.gridIntensity["network"] = {
|
|
115
|
-
value: globalGridIntensity
|
|
116
|
-
};
|
|
117
|
-
}
|
|
118
|
-
adjustments.gridIntensity["network"] = {
|
|
119
|
-
country: network.country,
|
|
120
|
-
value: parseFloat(
|
|
121
|
-
import__.averageIntensity.data[(_f = network.country) == null ? void 0 : _f.toUpperCase()]
|
|
122
|
-
)
|
|
123
|
-
};
|
|
124
|
-
} else if (typeof network === "number") {
|
|
125
|
-
adjustments.gridIntensity["network"] = {
|
|
126
|
-
value: network
|
|
127
|
-
};
|
|
128
|
-
} else {
|
|
129
|
-
adjustments.gridIntensity["network"] = {
|
|
130
|
-
value: globalGridIntensity
|
|
131
|
-
};
|
|
132
|
-
console.warn(
|
|
133
|
-
`The network grid intensity must be a number or an object. You passed in a ${typeof network}.
|
|
67
|
+
`The ${segment} grid intensity must be a number or an object. You passed in a ${typeof segmentIntensity}.
|
|
134
68
|
Falling back to global average grid intensity.`
|
|
135
69
|
);
|
|
136
70
|
}
|
|
71
|
+
} else {
|
|
72
|
+
adjustments.gridIntensity[segment] = {
|
|
73
|
+
value: globalGridIntensity
|
|
74
|
+
};
|
|
137
75
|
}
|
|
76
|
+
}
|
|
77
|
+
if (options == null ? void 0 : options.gridIntensity) {
|
|
78
|
+
adjustments.gridIntensity = {};
|
|
79
|
+
const { device, dataCenter, network } = options.gridIntensity;
|
|
80
|
+
setIntensity("device", device);
|
|
81
|
+
setIntensity("dataCenter", dataCenter);
|
|
82
|
+
setIntensity("network", network);
|
|
138
83
|
} else {
|
|
139
84
|
adjustments.gridIntensity = {
|
|
140
85
|
device: { value: globalGridIntensity },
|
|
@@ -244,7 +189,7 @@ Falling back to default value.`
|
|
|
244
189
|
return adjustments;
|
|
245
190
|
}
|
|
246
191
|
function getApiRequestHeaders(comment = "") {
|
|
247
|
-
return { "User-Agent": `co2js/${"0.16.
|
|
192
|
+
return { "User-Agent": `co2js/${"0.16.3"} ${comment}` };
|
|
248
193
|
}
|
|
249
194
|
function outputRating(co2e, swdmVersion) {
|
|
250
195
|
let {
|
|
@@ -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 as SWDM3_GLOBAL_GRID_INTENSITY,\n SWDV4,\n PERCENTAGE_OF_DATA_LOADED_ON_SUBSEQUENT_LOAD,\n FIRST_TIME_VIEWING_PERCENTAGE,\n RETURNING_VISITOR_PERCENTAGE,\n SWDMV3_RATINGS,\n SWDMV4_RATINGS,\n} from \"../constants/index.js\";\n\nconst SWDM4_GLOBAL_GRID_INTENSITY = SWDV4.GLOBAL_GRID_INTENSITY;\n// Shared type definitions to be used across different files\n\n/**\n * @typedef {Object} DomainCheckOptions options to control the behavior when checking a domain\n * @property {string} userAgentIdentifier - Optional. The app, site, or organisation that is making the request.\n * @property {boolean} verbose - Optional. Whether to return a verbose response.\n * @property {string[]} db - Optional. A database list to use for lookups.\n */\n\nconst formatNumber = (num) => parseFloat(num.toFixed(2));\n\nconst lessThanEqualTo = (num, limit) => num <= limit;\n\nfunction parseOptions(options = {}, version = 3, green = false) {\n const globalGridIntensity =\n version === 4 ? SWDM4_GLOBAL_GRID_INTENSITY : SWDM3_GLOBAL_GRID_INTENSITY;\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: globalGridIntensity,\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: globalGridIntensity,\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: SWDM3_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: globalGridIntensity,\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: globalGridIntensity,\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: globalGridIntensity,\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 } else {\n adjustments.gridIntensity = {\n device: { value: globalGridIntensity },\n dataCenter: { value: globalGridIntensity },\n network: { value: globalGridIntensity },\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 version === 3 ? PERCENTAGE_OF_DATA_LOADED_ON_SUBSEQUENT_LOAD : 0;\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 version === 3 ? PERCENTAGE_OF_DATA_LOADED_ON_SUBSEQUENT_LOAD : 0;\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 } else {\n adjustments.dataReloadRatio =\n version === 3 ? PERCENTAGE_OF_DATA_LOADED_ON_SUBSEQUENT_LOAD : 0;\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 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 =\n version === 3 ? FIRST_TIME_VIEWING_PERCENTAGE : 1;\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 =\n version === 3 ? FIRST_TIME_VIEWING_PERCENTAGE : 1;\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 } else {\n adjustments.firstVisitPercentage =\n version === 3 ? FIRST_TIME_VIEWING_PERCENTAGE : 1;\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 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 =\n version === 3 ? RETURNING_VISITOR_PERCENTAGE : 0;\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 =\n version === 3 ? RETURNING_VISITOR_PERCENTAGE : 0;\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 } else {\n adjustments.returnVisitPercentage =\n version === 3 ? RETURNING_VISITOR_PERCENTAGE : 0;\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 if (\n options?.greenHostingFactor ||\n (options.greenHostingFactor === 0 && version === 4)\n ) {\n if (typeof options.greenHostingFactor === \"number\") {\n if (options.greenHostingFactor >= 0 && options.greenHostingFactor <= 1) {\n adjustments.greenHostingFactor = options.greenHostingFactor;\n } else {\n adjustments.greenHostingFactor = 0;\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.greenHostingFactor = 0;\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 } else if (version === 4) {\n adjustments.greenHostingFactor = 0;\n }\n\n if (green) {\n adjustments.greenHostingFactor = 1;\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\n/**\n * Returns the SWDM rating for a given CO2e value and version of the SWDM.\n * @param {number} co2e - The CO2e value to rate.\n * @param {number} swdmVersion - The version of the SWDM to use. Defaults to version 3.\n * @returns {string} The SWDM rating.\n */\nfunction outputRating(co2e, swdmVersion) {\n let {\n FIFTH_PERCENTILE,\n TENTH_PERCENTILE,\n TWENTIETH_PERCENTILE,\n THIRTIETH_PERCENTILE,\n FORTIETH_PERCENTILE,\n FIFTIETH_PERCENTILE,\n } = SWDMV3_RATINGS;\n\n if (swdmVersion === 4) {\n FIFTH_PERCENTILE = SWDMV4_RATINGS.FIFTH_PERCENTILE;\n TENTH_PERCENTILE = SWDMV4_RATINGS.TENTH_PERCENTILE;\n TWENTIETH_PERCENTILE = SWDMV4_RATINGS.TWENTIETH_PERCENTILE;\n THIRTIETH_PERCENTILE = SWDMV4_RATINGS.THIRTIETH_PERCENTILE;\n FORTIETH_PERCENTILE = SWDMV4_RATINGS.FORTIETH_PERCENTILE;\n FIFTIETH_PERCENTILE = SWDMV4_RATINGS.FIFTIETH_PERCENTILE;\n }\n\n if (lessThanEqualTo(co2e, FIFTH_PERCENTILE)) {\n return \"A+\";\n } else if (lessThanEqualTo(co2e, TENTH_PERCENTILE)) {\n return \"A\";\n } else if (lessThanEqualTo(co2e, TWENTIETH_PERCENTILE)) {\n return \"B\";\n } else if (lessThanEqualTo(co2e, THIRTIETH_PERCENTILE)) {\n return \"C\";\n } else if (lessThanEqualTo(co2e, FORTIETH_PERCENTILE)) {\n return \"D\";\n } else if (lessThanEqualTo(co2e, FIFTIETH_PERCENTILE)) {\n return \"E\";\n } else {\n return \"F\";\n }\n}\n\nexport {\n formatNumber,\n parseOptions,\n getApiRequestHeaders,\n lessThanEqualTo,\n outputRating,\n};\n"],
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,eAAiC;AACjC,uBAQO;AAEP,MAAM,8BAA8B,uBAAM;AAU1C,MAAM,eAAe,CAAC,QAAQ,WAAW,IAAI,QAAQ,CAAC,CAAC;AAEvD,MAAM,kBAAkB,CAAC,KAAK,UAAU,OAAO;AAE/C,SAAS,aAAa,UAAU,CAAC,GAAG,UAAU,GAAG,QAAQ,OAAO;
|
|
4
|
+
"sourcesContent": ["import { averageIntensity } from \"../index.js\";\nimport {\n GLOBAL_GRID_INTENSITY as SWDM3_GLOBAL_GRID_INTENSITY,\n SWDV4,\n PERCENTAGE_OF_DATA_LOADED_ON_SUBSEQUENT_LOAD,\n FIRST_TIME_VIEWING_PERCENTAGE,\n RETURNING_VISITOR_PERCENTAGE,\n SWDMV3_RATINGS,\n SWDMV4_RATINGS,\n} from \"../constants/index.js\";\n\nconst SWDM4_GLOBAL_GRID_INTENSITY = SWDV4.GLOBAL_GRID_INTENSITY;\n// Shared type definitions to be used across different files\n\n/**\n * @typedef {Object} DomainCheckOptions options to control the behavior when checking a domain\n * @property {string} userAgentIdentifier - Optional. The app, site, or organisation that is making the request.\n * @property {boolean} verbose - Optional. Whether to return a verbose response.\n * @property {string[]} db - Optional. A database list to use for lookups.\n */\n\nconst formatNumber = (num) => parseFloat(num.toFixed(2));\n\nconst lessThanEqualTo = (num, limit) => num <= limit;\n\nfunction parseOptions(options = {}, version = 3, green = false) {\n const globalGridIntensity =\n version === 4 ? SWDM4_GLOBAL_GRID_INTENSITY : SWDM3_GLOBAL_GRID_INTENSITY;\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 /**\n *\n * @param {string} segment The name of the segment (\"device\"|\"dataCenter\"|\"network\")\n * @param {number|object} segmentIntensity The segment intensity\n */\n function setIntensity(segment, segmentIntensity) {\n if (segmentIntensity || segmentIntensity === 0) {\n if (typeof segmentIntensity === \"object\") {\n if (!averageIntensity.data[segmentIntensity.country?.toUpperCase()]) {\n console.warn(\n `\"${segmentIntensity.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[segment] = {\n value: globalGridIntensity,\n };\n }\n adjustments.gridIntensity[segment] = {\n country: segmentIntensity.country,\n value: parseFloat(\n averageIntensity.data[segmentIntensity.country?.toUpperCase()]\n ),\n };\n } else if (typeof segmentIntensity === \"number\") {\n adjustments.gridIntensity[segment] = {\n value: segmentIntensity,\n };\n } else {\n adjustments.gridIntensity[segment] = {\n value: globalGridIntensity,\n };\n console.warn(\n `The ${segment} grid intensity must be a number or an object. You passed in a ${typeof segmentIntensity}. \\nFalling back to global average grid intensity.`\n );\n }\n } else {\n adjustments.gridIntensity[segment] = {\n value: globalGridIntensity,\n };\n }\n }\n\n if (options?.gridIntensity) {\n adjustments.gridIntensity = {};\n const { device, dataCenter, network } = options.gridIntensity;\n setIntensity(\"device\", device);\n setIntensity(\"dataCenter\", dataCenter);\n setIntensity(\"network\", network);\n } else {\n adjustments.gridIntensity = {\n device: { value: globalGridIntensity },\n dataCenter: { value: globalGridIntensity },\n network: { value: globalGridIntensity },\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 version === 3 ? PERCENTAGE_OF_DATA_LOADED_ON_SUBSEQUENT_LOAD : 0;\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 version === 3 ? PERCENTAGE_OF_DATA_LOADED_ON_SUBSEQUENT_LOAD : 0;\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 } else {\n adjustments.dataReloadRatio =\n version === 3 ? PERCENTAGE_OF_DATA_LOADED_ON_SUBSEQUENT_LOAD : 0;\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 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 =\n version === 3 ? FIRST_TIME_VIEWING_PERCENTAGE : 1;\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 =\n version === 3 ? FIRST_TIME_VIEWING_PERCENTAGE : 1;\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 } else {\n adjustments.firstVisitPercentage =\n version === 3 ? FIRST_TIME_VIEWING_PERCENTAGE : 1;\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 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 =\n version === 3 ? RETURNING_VISITOR_PERCENTAGE : 0;\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 =\n version === 3 ? RETURNING_VISITOR_PERCENTAGE : 0;\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 } else {\n adjustments.returnVisitPercentage =\n version === 3 ? RETURNING_VISITOR_PERCENTAGE : 0;\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 if (\n options?.greenHostingFactor ||\n (options.greenHostingFactor === 0 && version === 4)\n ) {\n if (typeof options.greenHostingFactor === \"number\") {\n if (options.greenHostingFactor >= 0 && options.greenHostingFactor <= 1) {\n adjustments.greenHostingFactor = options.greenHostingFactor;\n } else {\n adjustments.greenHostingFactor = 0;\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.greenHostingFactor = 0;\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 } else if (version === 4) {\n adjustments.greenHostingFactor = 0;\n }\n\n if (green) {\n adjustments.greenHostingFactor = 1;\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\n/**\n * Returns the SWDM rating for a given CO2e value and version of the SWDM.\n * @param {number} co2e - The CO2e value to rate.\n * @param {number} swdmVersion - The version of the SWDM to use. Defaults to version 3.\n * @returns {string} The SWDM rating.\n */\nfunction outputRating(co2e, swdmVersion) {\n let {\n FIFTH_PERCENTILE,\n TENTH_PERCENTILE,\n TWENTIETH_PERCENTILE,\n THIRTIETH_PERCENTILE,\n FORTIETH_PERCENTILE,\n FIFTIETH_PERCENTILE,\n } = SWDMV3_RATINGS;\n\n if (swdmVersion === 4) {\n FIFTH_PERCENTILE = SWDMV4_RATINGS.FIFTH_PERCENTILE;\n TENTH_PERCENTILE = SWDMV4_RATINGS.TENTH_PERCENTILE;\n TWENTIETH_PERCENTILE = SWDMV4_RATINGS.TWENTIETH_PERCENTILE;\n THIRTIETH_PERCENTILE = SWDMV4_RATINGS.THIRTIETH_PERCENTILE;\n FORTIETH_PERCENTILE = SWDMV4_RATINGS.FORTIETH_PERCENTILE;\n FIFTIETH_PERCENTILE = SWDMV4_RATINGS.FIFTIETH_PERCENTILE;\n }\n\n if (lessThanEqualTo(co2e, FIFTH_PERCENTILE)) {\n return \"A+\";\n } else if (lessThanEqualTo(co2e, TENTH_PERCENTILE)) {\n return \"A\";\n } else if (lessThanEqualTo(co2e, TWENTIETH_PERCENTILE)) {\n return \"B\";\n } else if (lessThanEqualTo(co2e, THIRTIETH_PERCENTILE)) {\n return \"C\";\n } else if (lessThanEqualTo(co2e, FORTIETH_PERCENTILE)) {\n return \"D\";\n } else if (lessThanEqualTo(co2e, FIFTIETH_PERCENTILE)) {\n return \"E\";\n } else {\n return \"F\";\n }\n}\n\nexport {\n formatNumber,\n parseOptions,\n getApiRequestHeaders,\n lessThanEqualTo,\n outputRating,\n};\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,eAAiC;AACjC,uBAQO;AAEP,MAAM,8BAA8B,uBAAM;AAU1C,MAAM,eAAe,CAAC,Q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AE,cAAc,SAAS,YAA6B,UAAU;AACzE;AAQA,SAAS,aAAa,MAAM,aAAa;AACvC,MAAI;AAAA,IACF;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF,IAAI;AAEJ,MAAI,gBAAgB,GAAG;AACrB,uBAAmB,gCAAe;AAClC,uBAAmB,gCAAe;AAClC,2BAAuB,gCAAe;AACtC,2BAAuB,gCAAe;AACtC,0BAAsB,gCAAe;AACrC,0BAAsB,gCAAe;AAAA,EACvC;AAEA,MAAI,gBAAgB,MAAM,gBAAgB,GAAG;AAC3C,WAAO;AAAA,EACT,WAAW,gBAAgB,MAAM,gBAAgB,GAAG;AAClD,WAAO;AAAA,EACT,WAAW,gBAAgB,MAAM,oBAAoB,GAAG;AACtD,WAAO;AAAA,EACT,WAAW,gBAAgB,MAAM,oBAAoB,GAAG;AACtD,WAAO;AAAA,EACT,WAAW,gBAAgB,MAAM,mBAAmB,GAAG;AACrD,WAAO;AAAA,EACT,WAAW,gBAAgB,MAAM,mBAAmB,GAAG;AACrD,WAAO;AAAA,EACT,OAAO;AACL,WAAO;AAAA,EACT;AACF;",
|
|
6
6
|
"names": ["SWDM3_GLOBAL_GRID_INTENSITY"]
|
|
7
7
|
}
|
|
@@ -12,111 +12,56 @@ const SWDM4_GLOBAL_GRID_INTENSITY = SWDV4.GLOBAL_GRID_INTENSITY;
|
|
|
12
12
|
const formatNumber = (num) => parseFloat(num.toFixed(2));
|
|
13
13
|
const lessThanEqualTo = (num, limit) => num <= limit;
|
|
14
14
|
function parseOptions(options = {}, version = 3, green = false) {
|
|
15
|
-
var _a, _b, _c, _d, _e, _f;
|
|
16
15
|
const globalGridIntensity = version === 4 ? SWDM4_GLOBAL_GRID_INTENSITY : SWDM3_GLOBAL_GRID_INTENSITY;
|
|
17
16
|
if (typeof options !== "object") {
|
|
18
17
|
throw new Error("Options must be an object");
|
|
19
18
|
}
|
|
20
19
|
const adjustments = {};
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
if (!averageIntensity.data[(_a = device.country) == null ? void 0 : _a.toUpperCase()]) {
|
|
20
|
+
function setIntensity(segment, segmentIntensity) {
|
|
21
|
+
var _a, _b;
|
|
22
|
+
if (segmentIntensity || segmentIntensity === 0) {
|
|
23
|
+
if (typeof segmentIntensity === "object") {
|
|
24
|
+
if (!averageIntensity.data[(_a = segmentIntensity.country) == null ? void 0 : _a.toUpperCase()]) {
|
|
27
25
|
console.warn(
|
|
28
|
-
`"${
|
|
26
|
+
`"${segmentIntensity.country}" is not a valid country. Please use a valid 3 digit ISO 3166 country code.
|
|
29
27
|
See https://developers.thegreenwebfoundation.org/co2js/data/ for more information.
|
|
30
28
|
Falling back to global average grid intensity.`
|
|
31
29
|
);
|
|
32
|
-
adjustments.gridIntensity[
|
|
30
|
+
adjustments.gridIntensity[segment] = {
|
|
33
31
|
value: globalGridIntensity
|
|
34
32
|
};
|
|
35
33
|
}
|
|
36
|
-
adjustments.gridIntensity[
|
|
37
|
-
country:
|
|
38
|
-
value: parseFloat(
|
|
39
|
-
averageIntensity.data[(_b = device.country) == null ? void 0 : _b.toUpperCase()]
|
|
40
|
-
)
|
|
41
|
-
};
|
|
42
|
-
} else if (typeof device === "number") {
|
|
43
|
-
adjustments.gridIntensity["device"] = {
|
|
44
|
-
value: device
|
|
45
|
-
};
|
|
46
|
-
} else {
|
|
47
|
-
adjustments.gridIntensity["device"] = {
|
|
48
|
-
value: globalGridIntensity
|
|
49
|
-
};
|
|
50
|
-
console.warn(
|
|
51
|
-
`The device grid intensity must be a number or an object. You passed in a ${typeof device}.
|
|
52
|
-
Falling back to global average grid intensity.`
|
|
53
|
-
);
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
if (dataCenter || dataCenter === 0) {
|
|
57
|
-
if (typeof dataCenter === "object") {
|
|
58
|
-
if (!averageIntensity.data[(_c = dataCenter.country) == null ? void 0 : _c.toUpperCase()]) {
|
|
59
|
-
console.warn(
|
|
60
|
-
`"${dataCenter.country}" is not a valid country. Please use a valid 3 digit ISO 3166 country code.
|
|
61
|
-
See https://developers.thegreenwebfoundation.org/co2js/data/ for more information.
|
|
62
|
-
Falling back to global average grid intensity.`
|
|
63
|
-
);
|
|
64
|
-
adjustments.gridIntensity["dataCenter"] = {
|
|
65
|
-
value: SWDM3_GLOBAL_GRID_INTENSITY
|
|
66
|
-
};
|
|
67
|
-
}
|
|
68
|
-
adjustments.gridIntensity["dataCenter"] = {
|
|
69
|
-
country: dataCenter.country,
|
|
34
|
+
adjustments.gridIntensity[segment] = {
|
|
35
|
+
country: segmentIntensity.country,
|
|
70
36
|
value: parseFloat(
|
|
71
|
-
averageIntensity.data[(
|
|
37
|
+
averageIntensity.data[(_b = segmentIntensity.country) == null ? void 0 : _b.toUpperCase()]
|
|
72
38
|
)
|
|
73
39
|
};
|
|
74
|
-
} else if (typeof
|
|
75
|
-
adjustments.gridIntensity[
|
|
76
|
-
value:
|
|
40
|
+
} else if (typeof segmentIntensity === "number") {
|
|
41
|
+
adjustments.gridIntensity[segment] = {
|
|
42
|
+
value: segmentIntensity
|
|
77
43
|
};
|
|
78
44
|
} else {
|
|
79
|
-
adjustments.gridIntensity[
|
|
45
|
+
adjustments.gridIntensity[segment] = {
|
|
80
46
|
value: globalGridIntensity
|
|
81
47
|
};
|
|
82
48
|
console.warn(
|
|
83
|
-
`The
|
|
84
|
-
Falling back to global average grid intensity.`
|
|
85
|
-
);
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
if (network || network === 0) {
|
|
89
|
-
if (typeof network === "object") {
|
|
90
|
-
if (!averageIntensity.data[(_e = network.country) == null ? void 0 : _e.toUpperCase()]) {
|
|
91
|
-
console.warn(
|
|
92
|
-
`"${network.country}" is not a valid country. Please use a valid 3 digit ISO 3166 country code.
|
|
93
|
-
See https://developers.thegreenwebfoundation.org/co2js/data/ for more information. Falling back to global average grid intensity.
|
|
94
|
-
Falling back to global average grid intensity.`
|
|
95
|
-
);
|
|
96
|
-
adjustments.gridIntensity["network"] = {
|
|
97
|
-
value: globalGridIntensity
|
|
98
|
-
};
|
|
99
|
-
}
|
|
100
|
-
adjustments.gridIntensity["network"] = {
|
|
101
|
-
country: network.country,
|
|
102
|
-
value: parseFloat(
|
|
103
|
-
averageIntensity.data[(_f = network.country) == null ? void 0 : _f.toUpperCase()]
|
|
104
|
-
)
|
|
105
|
-
};
|
|
106
|
-
} else if (typeof network === "number") {
|
|
107
|
-
adjustments.gridIntensity["network"] = {
|
|
108
|
-
value: network
|
|
109
|
-
};
|
|
110
|
-
} else {
|
|
111
|
-
adjustments.gridIntensity["network"] = {
|
|
112
|
-
value: globalGridIntensity
|
|
113
|
-
};
|
|
114
|
-
console.warn(
|
|
115
|
-
`The network grid intensity must be a number or an object. You passed in a ${typeof network}.
|
|
49
|
+
`The ${segment} grid intensity must be a number or an object. You passed in a ${typeof segmentIntensity}.
|
|
116
50
|
Falling back to global average grid intensity.`
|
|
117
51
|
);
|
|
118
52
|
}
|
|
53
|
+
} else {
|
|
54
|
+
adjustments.gridIntensity[segment] = {
|
|
55
|
+
value: globalGridIntensity
|
|
56
|
+
};
|
|
119
57
|
}
|
|
58
|
+
}
|
|
59
|
+
if (options == null ? void 0 : options.gridIntensity) {
|
|
60
|
+
adjustments.gridIntensity = {};
|
|
61
|
+
const { device, dataCenter, network } = options.gridIntensity;
|
|
62
|
+
setIntensity("device", device);
|
|
63
|
+
setIntensity("dataCenter", dataCenter);
|
|
64
|
+
setIntensity("network", network);
|
|
120
65
|
} else {
|
|
121
66
|
adjustments.gridIntensity = {
|
|
122
67
|
device: { value: globalGridIntensity },
|
|
@@ -226,7 +171,7 @@ Falling back to default value.`
|
|
|
226
171
|
return adjustments;
|
|
227
172
|
}
|
|
228
173
|
function getApiRequestHeaders(comment = "") {
|
|
229
|
-
return { "User-Agent": `co2js/${"0.16.
|
|
174
|
+
return { "User-Agent": `co2js/${"0.16.3"} ${comment}` };
|
|
230
175
|
}
|
|
231
176
|
function outputRating(co2e, swdmVersion) {
|
|
232
177
|
let {
|