@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.
@@ -1,6 +1,6 @@
1
1
  require('esbuild').buildSync({
2
2
  entryPoints: ['src/index.js'],
3
- outdir: 'public',
3
+ outdir: 'dist/iife',
4
4
  globalName: 'co2',
5
5
  format: 'iife',
6
6
  platform: 'browser',
package/.gitpod.yml ADDED
@@ -0,0 +1,8 @@
1
+ # Commands to start on workspace startup
2
+ tasks:
3
+ - init: npm install
4
+ command: npm run gitpod
5
+ # Ports to expose on workspace startup
6
+ ports:
7
+ - port: 8000
8
+ onOpen: open-preview
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
- We know computers use electricity, and because most of the electricity we use comes from burning fossil fuels, there is an environmental cost to every upload and download we make over the internet.
7
+ ## [Documentation](https://developers.thegreenwebfoundation.org/co2js/overview/)
7
8
 
8
- We can do something about this though. The same way we use performance budgets to make apps and websites faster and cheaper to run, we can use carbon budgets to make them faster, cheaper and _greener_.
9
+ ## What is CO2.js?
9
10
 
10
- The CO2 package from [The Green Web Foundation][tgwf] lets you quickly estimate these emissions, to make measurable improvements as part of your workflow.
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
- ### How it works
13
+ ## Why use it?
13
14
 
14
- It does this by implementing various models for converting the measurable usage of digital services into a figure for the estimated CO2 emissions.
15
+ Being able to estimate the CO2 emissions associated with digital activities can be of benefit to both developers and users.
15
16
 
16
- This defaults to the 1byte model as used by the Shift Project, as introduced in their report on CO2 emissions from digital infrastructure, [Lean ICT: for a sober digital][soberDigital], with the [Sustainable Web Design model][swd] as a popular alternative..
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 more information, see the documentation [for when to use the different models, with code samples to start you off](./src/readme.md).
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
- ### Who uses it
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
- It is currently used in the web performance tool [sitespeed.io][], [ecoping][], [Websitecarbon.com](websitecarbon), and [ecograder][] to help developers build greener, more planet friendly digital services.
23
+ ## Installation
23
24
 
24
- If you want to build this kind of environmental information into your own software, and want some advice, we'd be happy to hear from you - please open an issue, remembering to link to your project.
25
+ ### Using NPM
25
26
 
26
- This is open source software, with all the guarantees associated. So if you want professional advice, to a deadline, and you have a budget please see the services offered by the [Green Web Foundation][tgwf-services].
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
- #### Browser-side
33
+ ### Using Skypack
59
34
 
60
- For example, like this:
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
- Because different digital services and websites use different forms of power, there is also a module for checking if a domain uses green power or not, and whether the domains linked to on a page use green power as well.
76
-
77
- ```js
42
+ ### Build it yourself
78
43
 
79
- import { hosting } from '@tgwf/co2'
44
+ You can also build the CO2.js library from the source code. To do this:
80
45
 
81
- // returns true if green, otherwise false
82
- hosting.check("google.com")
83
-
84
- // returns an array of the green domains, in this case ["google.com"].
85
- hosting.check(["google.com", "kochindustries.com"])]
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
- # Licenses
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.0",
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</h1>
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({result})
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>
@@ -1,4 +1,4 @@
1
- const GIGABYTE = 1024 * 1024 * 1024;
1
+ const GIGABYTE = 1000 * 1000 * 1000;
2
2
 
3
3
  export default {
4
4
  GIGABYTE,
@@ -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 v9firstVisits = 0;
213
- let v9subsequentVisits = 0;
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
- v9firstVisits += val;
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
- v9subsequentVisits += val;
218
+ subsequentVisits += val;
228
219
  }
229
220
  }
230
221
 
231
- log({ v9firstVisits });
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