@tgwf/co2 0.10.0 → 0.10.1
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/.esbuild.browser.js +1 -1
- package/.gitpod.yml +8 -0
- package/README.md +29 -64
- package/package.json +3 -2
- package/public/index.html +5 -4
- package/src/constants/file-size.js +1 -1
- package/src/sustainable-web-design.js +5 -47
- package/.tool-versions +0 -1
package/.esbuild.browser.js
CHANGED
package/.gitpod.yml
ADDED
package/README.md
CHANGED
|
@@ -1,92 +1,57 @@
|
|
|
1
|
-
# CO2
|
|
1
|
+
# CO2.js
|
|
2
2
|
|
|
3
3
|
<img src="https://github.com/thegreenwebfoundation/co2.js/actions/workflows/unittests.yml/badge.svg" />
|
|
4
4
|
|
|
5
|
+
One day, there internet will be powered by renewable energy. Until that day comes, there’ll be a CO2 cost that comes with every byte of data that’s uploaded or downloaded. By being able to calculate these emissions, developers can be empowered to create more efficient, lower carbon apps, websites, and software.
|
|
5
6
|
|
|
6
|
-
|
|
7
|
+
## [Documentation](https://developers.thegreenwebfoundation.org/co2js/overview/)
|
|
7
8
|
|
|
8
|
-
|
|
9
|
+
## What is CO2.js?
|
|
9
10
|
|
|
10
|
-
|
|
11
|
+
CO2.js is a JavaScript library that enables developers a way to estimate the emissions related to use of their apps, websites, and software.
|
|
11
12
|
|
|
12
|
-
|
|
13
|
+
## Why use it?
|
|
13
14
|
|
|
14
|
-
|
|
15
|
+
Being able to estimate the CO2 emissions associated with digital activities can be of benefit to both developers and users.
|
|
15
16
|
|
|
16
|
-
|
|
17
|
+
Internally, you may want to use this library to create a *carbon budget* for your site or app. It is also useful for inclusion in dashboards and monitoring tools.
|
|
17
18
|
|
|
18
|
-
For
|
|
19
|
+
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).
|
|
19
20
|
|
|
20
|
-
|
|
21
|
+
The above a 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.
|
|
21
22
|
|
|
22
|
-
|
|
23
|
+
## Installation
|
|
23
24
|
|
|
24
|
-
|
|
25
|
+
### Using NPM
|
|
25
26
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
[sitespeed.io]: https://sitespeed.io
|
|
30
|
-
[ecoping]: https://ecoping.earth
|
|
31
|
-
[ecograder]: https://ecograder.com
|
|
32
|
-
[websitecarbon]: https://www.websitecarbon.com
|
|
33
|
-
[tgwf]: https://www.thegreenwebfoundation.org
|
|
34
|
-
[tgwf-services]: https://www.thegreenwebfoundation.org/services
|
|
35
|
-
[swd]: https://sustainablewebdesign.org/calculating-digital-emissions
|
|
36
|
-
[soberDigital]: https://theshiftproject.org/en/lean-ict-2/
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
## Usage
|
|
40
|
-
|
|
41
|
-
### Calculating emissions per byte
|
|
42
|
-
|
|
43
|
-
#### Server-side
|
|
44
|
-
|
|
45
|
-
This approach relies on the `fs` module and so can only be used on platforms like Node.js, that support this.
|
|
46
|
-
|
|
47
|
-
```js
|
|
48
|
-
|
|
49
|
-
const CO2 = require('@tgwf/co2')
|
|
50
|
-
const bytesSent = (1024 * 1024 * 1024)
|
|
51
|
-
const co2Emission = new CO2();
|
|
52
|
-
estimatedCO2 = co2Emission.perByte(bytesSent)
|
|
53
|
-
|
|
54
|
-
console.log(`Sending a gigabyte, had a carbon footprint of ${estimatedCO2.toFixed(3)} grams of CO2`)
|
|
27
|
+
You can install CO2.js as a dependency for your projects using NPM.
|
|
55
28
|
|
|
29
|
+
```bash
|
|
30
|
+
npm install @tgwf/co2
|
|
56
31
|
```
|
|
57
32
|
|
|
58
|
-
|
|
33
|
+
### Using Skypack
|
|
59
34
|
|
|
60
|
-
|
|
35
|
+
You can import the CO2.js library into projects using Skypack.
|
|
61
36
|
|
|
62
37
|
```js
|
|
63
|
-
|
|
64
|
-
import { co2 } from '@tgwf/co2'
|
|
65
|
-
const bytesSent = (1024 * 1024 * 1024)
|
|
66
|
-
const co2Emission = new co2();
|
|
67
|
-
estimatedCO2 = co2Emission.perByte(bytesSent)
|
|
68
|
-
|
|
69
|
-
console.log(`Sending a gigabyte, had a carbon footprint of ${estimatedCO2.toFixed(3)} grams of CO2`)
|
|
70
|
-
****
|
|
38
|
+
import tgwf from 'https://cdn.skypack.dev/@tgwf/co2';
|
|
71
39
|
```
|
|
72
40
|
|
|
73
|
-
### Checking for green power
|
|
74
41
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
```js
|
|
42
|
+
### Build it yourself
|
|
78
43
|
|
|
79
|
-
|
|
44
|
+
You can also build the CO2.js library from the source code. To do this:
|
|
80
45
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
46
|
+
1. Go to the [CO2.js repository](https://github.com/thegreenwebfoundation/co2.js) on GitHub.
|
|
47
|
+
1. Clone or fork the repository.
|
|
48
|
+
1. Navigate to the folder on your machine and run `npm run build` in your terminal.
|
|
49
|
+
1. Once the build has finished running, you will find a `/dist` folder has been created. Inside you can find:
|
|
50
|
+
|
|
51
|
+
- `dist/cjs` - A CommonJS compatible build.
|
|
52
|
+
- `dist/esm` - An ES Modules compatible build.
|
|
53
|
+
- `dist/iife` - An Immediately Invoked Function Expression (IIFE) version of the library.
|
|
89
54
|
|
|
90
|
-
|
|
55
|
+
## Licenses
|
|
91
56
|
|
|
92
57
|
Apache 2.0
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tgwf/co2",
|
|
3
|
-
"version": "0.10.
|
|
3
|
+
"version": "0.10.1",
|
|
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",
|
|
@@ -19,7 +19,8 @@
|
|
|
19
19
|
"build": "npm run build:esm && npm run build:browser && npm run build:node",
|
|
20
20
|
"build:esm": "node .esbuild.esm.js && ./fixup",
|
|
21
21
|
"build:browser": "node .esbuild.browser.js",
|
|
22
|
-
"build:node": "node .esbuild.node.js && ./fixup"
|
|
22
|
+
"build:node": "node .esbuild.node.js && ./fixup",
|
|
23
|
+
"gitpod": "npm run build && cp ./dist/iife/index.js ./public && npm run serve"
|
|
23
24
|
},
|
|
24
25
|
"keywords": [
|
|
25
26
|
"sustainability",
|
package/public/index.html
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
|
|
8
8
|
<body class="">
|
|
9
9
|
|
|
10
|
-
<h1>This is a minimal demo
|
|
10
|
+
<h1>This is a minimal demo using the IIFE version of CO2.js.</h1>
|
|
11
11
|
|
|
12
12
|
<p>Add a number in bytes, and we convert it to carbon, using the SWD model</p>
|
|
13
13
|
|
|
@@ -26,7 +26,6 @@
|
|
|
26
26
|
|
|
27
27
|
<script src="./index.js"></script>
|
|
28
28
|
<script>
|
|
29
|
-
|
|
30
29
|
let emissions = new co2.co2();
|
|
31
30
|
|
|
32
31
|
async function main() {
|
|
@@ -36,7 +35,9 @@
|
|
|
36
35
|
// is the result green?
|
|
37
36
|
const result = await hosting.check("google.com")
|
|
38
37
|
// should return true or false
|
|
39
|
-
console.log({
|
|
38
|
+
console.log({
|
|
39
|
+
result
|
|
40
|
+
})
|
|
40
41
|
|
|
41
42
|
document.querySelector('form').addEventListener('submit', function (event) {
|
|
42
43
|
event.preventDefault();
|
|
@@ -54,4 +55,4 @@
|
|
|
54
55
|
</script>
|
|
55
56
|
</body>
|
|
56
57
|
|
|
57
|
-
</html>
|
|
58
|
+
</html>
|
|
@@ -175,15 +175,6 @@ class SustainableWebDesign {
|
|
|
175
175
|
log({ energyBycomponent });
|
|
176
176
|
const energyValues = Object.values(energyBycomponent);
|
|
177
177
|
|
|
178
|
-
// sanity check that these numbers add back up
|
|
179
|
-
const v9recombinedNoCaching = energyValues.reduce(
|
|
180
|
-
(prevValue, currentValue) => prevValue + currentValue
|
|
181
|
-
);
|
|
182
|
-
|
|
183
|
-
// energyBycomponent doesn't apply any caching logic, to should be the
|
|
184
|
-
// same number as the total in the v8
|
|
185
|
-
log({ v9recombinedNoCaching });
|
|
186
|
-
|
|
187
178
|
// for this, we want
|
|
188
179
|
for (const [key, value] of Object.entries(energyBycomponent)) {
|
|
189
180
|
// represent the first load
|
|
@@ -209,8 +200,8 @@ class SustainableWebDesign {
|
|
|
209
200
|
// fetch the values using the default caching assumptions
|
|
210
201
|
// const energyValues = Object.values(this.energyPerVisitByComponent(bytes));
|
|
211
202
|
|
|
212
|
-
let
|
|
213
|
-
let
|
|
203
|
+
let firstVisits = 0;
|
|
204
|
+
let subsequentVisits = 0;
|
|
214
205
|
|
|
215
206
|
const energyBycomponent = Object.entries(
|
|
216
207
|
this.energyPerVisitByComponent(bytes)
|
|
@@ -218,50 +209,17 @@ class SustainableWebDesign {
|
|
|
218
209
|
|
|
219
210
|
for (const [key, val] of energyBycomponent) {
|
|
220
211
|
if (key.indexOf("first") > 0) {
|
|
221
|
-
|
|
212
|
+
firstVisits += val;
|
|
222
213
|
}
|
|
223
214
|
}
|
|
224
215
|
|
|
225
216
|
for (const [key, val] of energyBycomponent) {
|
|
226
217
|
if (key.indexOf("subsequent") > 0) {
|
|
227
|
-
|
|
218
|
+
subsequentVisits += val;
|
|
228
219
|
}
|
|
229
220
|
}
|
|
230
221
|
|
|
231
|
-
|
|
232
|
-
log({ v9subsequentVisits });
|
|
233
|
-
return v9firstVisits + v9subsequentVisits;
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
/*
|
|
237
|
-
* JUST FOR TEST PURPOSES
|
|
238
|
-
* Testing v0.8.0 => v0.9.0 versions
|
|
239
|
-
*
|
|
240
|
-
*
|
|
241
|
-
*/
|
|
242
|
-
energyPerVisitV8(bytes) {
|
|
243
|
-
log({ bytes });
|
|
244
|
-
const transferedBytesToGb = bytes / fileSize.GIGABYTE;
|
|
245
|
-
|
|
246
|
-
const v8visitWithNoCaching = transferedBytesToGb * KWH_PER_GB;
|
|
247
|
-
|
|
248
|
-
const v8firstVisit =
|
|
249
|
-
transferedBytesToGb * KWH_PER_GB * RETURNING_VISITOR_PERCENTAGE;
|
|
250
|
-
|
|
251
|
-
const v8subsequentVisits =
|
|
252
|
-
transferedBytesToGb *
|
|
253
|
-
KWH_PER_GB *
|
|
254
|
-
FIRST_TIME_VIEWING_PERCENTAGE *
|
|
255
|
-
PERCENTAGE_OF_DATA_LOADED_ON_SUBSEQUENT_LOAD;
|
|
256
|
-
|
|
257
|
-
const v8firstAndSubsequentVisits = v8firstVisit + v8subsequentVisits;
|
|
258
|
-
|
|
259
|
-
log({ v8visitWithNoCaching });
|
|
260
|
-
log({ v8firstVisit });
|
|
261
|
-
log({ v8subsequentVisits });
|
|
262
|
-
log({ v8firstAndSubsequentVisits });
|
|
263
|
-
|
|
264
|
-
return v8firstVisit + v8subsequentVisits;
|
|
222
|
+
return firstVisits + subsequentVisits;
|
|
265
223
|
}
|
|
266
224
|
|
|
267
225
|
// TODO: this method looks like it applies the carbon intensity
|
package/.tool-versions
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
nodejs 18.4.0
|