@tgwf/co2 0.9.0 → 0.10.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.esbuild.browser.js +11 -0
- package/.esbuild.esm.js +25 -0
- package/.esbuild.node.js +16 -0
- package/.gitpod.yml +8 -0
- package/CHANGELOG.md +22 -1
- package/README.md +29 -66
- package/dist/cjs/1byte.js +52 -0
- package/dist/cjs/1byte.js.map +7 -0
- package/dist/cjs/1byte.test.js +27 -0
- package/dist/cjs/1byte.test.js.map +7 -0
- package/dist/cjs/co2.js +121 -0
- package/dist/cjs/co2.js.map +7 -0
- package/dist/cjs/co2.test.js +258 -0
- package/dist/cjs/co2.test.js.map +7 -0
- package/dist/cjs/constants/file-size.js +27 -0
- package/dist/cjs/constants/file-size.js.map +7 -0
- package/dist/cjs/constants/index.js +27 -0
- package/dist/cjs/constants/index.js.map +7 -0
- package/dist/cjs/helpers/index.js +24 -0
- package/dist/cjs/helpers/index.js.map +7 -0
- package/dist/cjs/hosting-api.js +64 -0
- package/dist/cjs/hosting-api.js.map +7 -0
- package/dist/cjs/hosting-api.test.js +47 -0
- package/dist/cjs/hosting-api.test.js.map +7 -0
- package/dist/cjs/hosting-database.node.test.js +36 -0
- package/dist/cjs/hosting-database.node.test.js.map +7 -0
- package/dist/cjs/hosting-json.node.js +73 -0
- package/dist/cjs/hosting-json.node.js.map +7 -0
- package/dist/cjs/hosting-json.node.test.js +43 -0
- package/dist/cjs/hosting-json.node.test.js.map +7 -0
- package/dist/cjs/hosting-node.js +78 -0
- package/dist/cjs/hosting-node.js.map +7 -0
- package/dist/cjs/hosting.js +36 -0
- package/dist/cjs/hosting.js.map +7 -0
- package/dist/cjs/hosting.test.js +74 -0
- package/dist/cjs/hosting.test.js.map +7 -0
- package/dist/cjs/index-node.js +29 -0
- package/dist/cjs/index-node.js.map +7 -0
- package/dist/cjs/index.js +31 -0
- package/dist/cjs/index.js.map +7 -0
- package/dist/cjs/package.json +3 -0
- package/dist/cjs/sustainable-web-design.js +134 -0
- package/dist/cjs/sustainable-web-design.js.map +7 -0
- package/dist/cjs/sustainable-web-design.test.js +79 -0
- package/dist/cjs/sustainable-web-design.test.js.map +7 -0
- package/dist/esm/1byte.js +32 -0
- package/dist/esm/1byte.test.js +11 -0
- package/dist/esm/co2.js +98 -0
- package/{src → dist/esm}/co2.test.js +47 -94
- package/dist/esm/constants/file-size.js +7 -0
- package/dist/esm/constants/index.js +4 -0
- package/dist/esm/helpers/index.js +4 -0
- package/dist/esm/hosting-api.js +41 -0
- package/dist/esm/hosting-api.test.js +31 -0
- package/{src/hosting-database.test.js → dist/esm/hosting-database.node.test.js} +7 -18
- package/{src/hosting-json.test.js → dist/esm/hosting-json.node.test.js} +4 -21
- package/dist/esm/hosting.js +13 -0
- package/{src → dist/esm}/hosting.test.js +10 -30
- package/dist/esm/index.js +8 -0
- package/dist/esm/package.json +3 -0
- package/dist/esm/sustainable-web-design.js +111 -0
- package/{src → dist/esm}/sustainable-web-design.test.js +19 -37
- package/dist/iife/index.js +2 -0
- package/dist/iife/index.js.map +7 -0
- package/fixup +19 -0
- package/package.json +38 -17
- package/public/index.html +58 -0
- package/public/index.js +2 -0
- package/src/1byte.js +2 -3
- package/src/co2.js +17 -18
- package/src/constants/file-size.js +2 -2
- package/src/constants/index.js +2 -2
- package/src/helpers/index.js +1 -3
- package/src/hosting-api.js +23 -43
- package/src/{hosting-json.js → hosting-json.node.js} +9 -11
- package/src/hosting-node.js +94 -0
- package/src/hosting.js +6 -28
- package/src/index-node.js +4 -0
- package/src/index.js +4 -6
- package/src/sustainable-web-design.js +37 -11
- package/.eslintrc.json +0 -21
- package/.github/workflows/unittests.yml +0 -26
- package/data/Lean-ICT-Materials-1byte-Model-2018.xlsx +0 -0
- package/data/Lean-ICT-Materials-Forecast-Model-2018.xlsx +0 -0
- package/data/fixtures/tgwf.har +0 -6107
- package/data/fixtures/url2green.test.db +0 -0
- package/data/fixtures/url2green.test.json +0 -1
- package/data/fixtures/url2green.test.json.gz +0 -0
- package/images/swd-energy-usage.png +0 -0
- package/src/1byte.test.js +0 -17
- package/src/green-byte.js +0 -26
- package/src/hosting-api.test.js +0 -37
package/.esbuild.esm.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
const esbuild = require('esbuild')
|
|
2
|
+
// tiny glob is a dependency of `esbuild-plugin-glob`.
|
|
3
|
+
// For this build however we need to filter out some extra files
|
|
4
|
+
// that are used for nodejs, but not in browsers, so we use the
|
|
5
|
+
// library directly instead of using `esbuild-plugin-glob` as a plugin
|
|
6
|
+
const glob = require('tiny-glob');
|
|
7
|
+
|
|
8
|
+
async function main() {
|
|
9
|
+
const results = await glob('src/**/**.js')
|
|
10
|
+
// we remove node specific files here, with the assumption that
|
|
11
|
+
// the common use case is bundling into browser based web apps
|
|
12
|
+
const justBrowserCompatibleFiles = results.filter(filepath => !filepath.endsWith('node.js'))
|
|
13
|
+
|
|
14
|
+
esbuild.build({
|
|
15
|
+
entryPoints: justBrowserCompatibleFiles,
|
|
16
|
+
bundle: false,
|
|
17
|
+
minify: false,
|
|
18
|
+
sourcemap: false,
|
|
19
|
+
target: ['chrome58', 'firefox57', 'safari11', 'edge18', 'esnext'],
|
|
20
|
+
outdir: 'dist/esm',
|
|
21
|
+
outExtension: { '.js': '.js' },
|
|
22
|
+
format: 'esm'
|
|
23
|
+
})
|
|
24
|
+
}
|
|
25
|
+
main()
|
package/.esbuild.node.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
const { globPlugin } = require('esbuild-plugin-glob');
|
|
2
|
+
|
|
3
|
+
function main() {
|
|
4
|
+
require('esbuild').build({
|
|
5
|
+
entryPoints: ['src/**/**.js',],
|
|
6
|
+
bundle: false,
|
|
7
|
+
minify: false,
|
|
8
|
+
sourcemap: true,
|
|
9
|
+
target: ['node14'],
|
|
10
|
+
outdir: 'dist/cjs/',
|
|
11
|
+
outExtension: { '.js': '.js' },
|
|
12
|
+
format: 'cjs',
|
|
13
|
+
plugins: [globPlugin()]
|
|
14
|
+
})
|
|
15
|
+
}
|
|
16
|
+
main()
|
package/.gitpod.yml
ADDED
package/CHANGELOG.md
CHANGED
|
@@ -4,10 +4,31 @@ All notable changes to this project will be documented in this file.
|
|
|
4
4
|
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
|
-
|
|
7
|
+
|
|
8
|
+
> **Added** for new features.
|
|
9
|
+
> **Changed** for changes in existing functionality.
|
|
10
|
+
> **Deprecated** for soon-to-be removed features.
|
|
11
|
+
> **Removed** for now removed features.
|
|
12
|
+
> **Fixed** for any bug fixes.
|
|
13
|
+
> **Security** in case of vulnerabilities.
|
|
8
14
|
|
|
9
15
|
## Unreleased
|
|
10
16
|
|
|
17
|
+
### Added
|
|
18
|
+
|
|
19
|
+
- 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.
|
|
20
|
+
|
|
21
|
+
## 0.10.1 2022-08-01
|
|
22
|
+
|
|
23
|
+
This release used a version bump as previously we had released v0.10.0 under a pre-release tag.
|
|
24
|
+
|
|
25
|
+
## 0.10.0 2022-06-27
|
|
26
|
+
|
|
27
|
+
- Added ES import syntax as the main way for handling imports and exports of code within the module.
|
|
28
|
+
- Changed eslint settings to use later version of ecmascript (2020)
|
|
29
|
+
- Change the bulid tools to use esbulid with jest instead of babel
|
|
30
|
+
- Added more consistent use of the debug logging library in files using the updated import syntax
|
|
31
|
+
- 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.
|
|
11
32
|
|
|
12
33
|
|
|
13
34
|
# [0.9.0] - 2022-03-28
|
package/README.md
CHANGED
|
@@ -1,94 +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
|
-
const CO2 = require('node_modules/@tgwf/co2/src/co2.js')
|
|
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
|
-
|
|
89
|
-
|
|
90
|
-
```
|
|
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.
|
|
91
54
|
|
|
92
|
-
|
|
55
|
+
## Licenses
|
|
93
56
|
|
|
94
57
|
Apache 2.0
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
var byte_exports = {};
|
|
19
|
+
__export(byte_exports, {
|
|
20
|
+
OneByte: () => OneByte,
|
|
21
|
+
default: () => byte_default
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(byte_exports);
|
|
24
|
+
const CO2_PER_KWH_IN_DC_GREY = 519;
|
|
25
|
+
const CO2_PER_KWH_NETWORK_GREY = 475;
|
|
26
|
+
const CO2_PER_KWH_IN_DC_GREEN = 0;
|
|
27
|
+
const KWH_PER_BYTE_IN_DC = 72e-12;
|
|
28
|
+
const FIXED_NETWORK_WIRED = 429e-12;
|
|
29
|
+
const FIXED_NETWORK_WIFI = 152e-12;
|
|
30
|
+
const FOUR_G_MOBILE = 884e-12;
|
|
31
|
+
const KWH_PER_BYTE_FOR_NETWORK = (FIXED_NETWORK_WIRED + FIXED_NETWORK_WIFI + FOUR_G_MOBILE) / 3;
|
|
32
|
+
const KWH_PER_BYTE_FOR_DEVICES = 13e-11;
|
|
33
|
+
class OneByte {
|
|
34
|
+
constructor(options) {
|
|
35
|
+
this.options = options;
|
|
36
|
+
this.KWH_PER_BYTE_FOR_NETWORK = KWH_PER_BYTE_FOR_NETWORK;
|
|
37
|
+
}
|
|
38
|
+
perByte(bytes, green) {
|
|
39
|
+
if (bytes < 1) {
|
|
40
|
+
return 0;
|
|
41
|
+
}
|
|
42
|
+
if (green) {
|
|
43
|
+
const Co2ForDC = bytes * KWH_PER_BYTE_IN_DC * CO2_PER_KWH_IN_DC_GREEN;
|
|
44
|
+
const Co2forNetwork = bytes * KWH_PER_BYTE_FOR_NETWORK * CO2_PER_KWH_NETWORK_GREY;
|
|
45
|
+
return Co2ForDC + Co2forNetwork;
|
|
46
|
+
}
|
|
47
|
+
const KwHPerByte = KWH_PER_BYTE_IN_DC + KWH_PER_BYTE_FOR_NETWORK;
|
|
48
|
+
return bytes * KwHPerByte * CO2_PER_KWH_IN_DC_GREY;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
var byte_default = OneByte;
|
|
52
|
+
//# sourceMappingURL=1byte.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/1byte.js"],
|
|
4
|
+
"sourcesContent": ["// Use the 1byte model for now from the Shift Project, and assume a US grid mix figure they use of around 519 g co2 for the time being. It's lower for Europe, and in particular, France, but for v1, we don't include this\nconst CO2_PER_KWH_IN_DC_GREY = 519;\n\n// this figure is from the IEA's 2018 report for a global average:\nconst CO2_PER_KWH_NETWORK_GREY = 475;\n\n// TODO - these figures need to be updated, as the figures for green\n// shouldn't really be zero now we know that carbon intensity figures\n// for renewables still usually include the life cycle emissions\nconst CO2_PER_KWH_IN_DC_GREEN = 0;\n\n// the 1 byte model gives figures for energy usage for:\n\n// datacentres\n// networks\n// the device used to access a site/app\n\n// The device usage figure combines figures for:\n// 1. the usage for devices (which is small proportion of the energy use)\n// 2. the *making* the device, which is comparatively high.\n\nconst KWH_PER_BYTE_IN_DC = 7.2e-11;\n\n// this is probably best left as something users can define, or\n// a weighted average based on total usage.\n// Using a simple mean for now, as while web traffic to end users might trend\n// towards wifi and mobile,\n// Web traffic between servers is likely wired networks\n\nconst FIXED_NETWORK_WIRED = 4.29e-10;\nconst FIXED_NETWORK_WIFI = 1.52e-10;\nconst FOUR_G_MOBILE = 8.84e-10;\n\n// Pull requests gratefully accepted\nconst KWH_PER_BYTE_FOR_NETWORK =\n (FIXED_NETWORK_WIRED + FIXED_NETWORK_WIFI + FOUR_G_MOBILE) / 3;\n\nconst KWH_PER_BYTE_FOR_DEVICES = 1.3e-10;\n\nclass OneByte {\n constructor(options) {\n this.options = options;\n\n this.KWH_PER_BYTE_FOR_NETWORK = KWH_PER_BYTE_FOR_NETWORK;\n }\n\n perByte(bytes, green) {\n if (bytes < 1) {\n return 0;\n }\n\n if (green) {\n // if we have a green datacentre, use the lower figure for renewable energy\n const Co2ForDC = bytes * KWH_PER_BYTE_IN_DC * CO2_PER_KWH_IN_DC_GREEN;\n\n // but for the worest of the internet, we can't easily check, so assume\n // grey for now\n const Co2forNetwork =\n bytes * KWH_PER_BYTE_FOR_NETWORK * CO2_PER_KWH_NETWORK_GREY;\n\n return Co2ForDC + Co2forNetwork;\n }\n\n const KwHPerByte = KWH_PER_BYTE_IN_DC + KWH_PER_BYTE_FOR_NETWORK;\n return bytes * KwHPerByte * CO2_PER_KWH_IN_DC_GREY;\n }\n}\n\nexport { OneByte };\nexport default OneByte;\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AACA,MAAM,yBAAyB;AAG/B,MAAM,2BAA2B;AAKjC,MAAM,0BAA0B;AAYhC,MAAM,qBAAqB;AAQ3B,MAAM,sBAAsB;AAC5B,MAAM,qBAAqB;AAC3B,MAAM,gBAAgB;AAGtB,MAAM,2BACH,uBAAsB,qBAAqB,iBAAiB;AAE/D,MAAM,2BAA2B;AAEjC,MAAM,QAAQ;AAAA,EACZ,YAAY,SAAS;AACnB,SAAK,UAAU;AAEf,SAAK,2BAA2B;AAAA,EAClC;AAAA,EAEA,QAAQ,OAAO,OAAO;AACpB,QAAI,QAAQ,GAAG;AACb,aAAO;AAAA,IACT;AAEA,QAAI,OAAO;AAET,YAAM,WAAW,QAAQ,qBAAqB;AAI9C,YAAM,gBACJ,QAAQ,2BAA2B;AAErC,aAAO,WAAW;AAAA,IACpB;AAEA,UAAM,aAAa,qBAAqB;AACxC,WAAO,QAAQ,aAAa;AAAA,EAC9B;AACF;AAGA,IAAO,eAAQ;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __copyProps = (to, from, except, desc) => {
|
|
9
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
10
|
+
for (let key of __getOwnPropNames(from))
|
|
11
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
12
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
13
|
+
}
|
|
14
|
+
return to;
|
|
15
|
+
};
|
|
16
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod));
|
|
17
|
+
var import_byte = __toESM(require("./1byte.js"));
|
|
18
|
+
describe("onebyte", () => {
|
|
19
|
+
describe("perByte", () => {
|
|
20
|
+
it("returns a simple average of the different networks", () => {
|
|
21
|
+
const expected_val = 488e-12 .toFixed(12);
|
|
22
|
+
const instance = new import_byte.default();
|
|
23
|
+
expect(instance.KWH_PER_BYTE_FOR_NETWORK.toFixed(12)).toBe(expected_val);
|
|
24
|
+
});
|
|
25
|
+
});
|
|
26
|
+
});
|
|
27
|
+
//# sourceMappingURL=1byte.test.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/1byte.test.js"],
|
|
4
|
+
"sourcesContent": ["\"use strict\";\n\nimport OneByte from \"./1byte.js\";\n\ndescribe(\"onebyte\", () => {\n describe(\"perByte\", () => {\n it(\"returns a simple average of the different networks\", () => {\n // we limit this to 12 figures with toFixed(12), because\n // we have a recurring 333333 afterwards\n // 4.88e-10 is the same as 0.000000000488\n const expected_val = (0.000000000488).toFixed(12);\n const instance = new OneByte();\n\n expect(instance.KWH_PER_BYTE_FOR_NETWORK.toFixed(12)).toBe(expected_val);\n });\n });\n});\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;AAEA,kBAAoB;AAEpB,SAAS,WAAW,MAAM;AACxB,WAAS,WAAW,MAAM;AACxB,OAAG,sDAAsD,MAAM;AAI7D,YAAM,eAAgB,SAAgB,QAAQ,EAAE;AAChD,YAAM,WAAW,IAAI,oBAAQ;AAE7B,aAAO,SAAS,yBAAyB,QAAQ,EAAE,CAAC,EAAE,KAAK,YAAY;AAAA,IACzE,CAAC;AAAA,EACH,CAAC;AACH,CAAC;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
package/dist/cjs/co2.js
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod));
|
|
21
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
22
|
+
var co2_exports = {};
|
|
23
|
+
__export(co2_exports, {
|
|
24
|
+
CO2: () => CO2,
|
|
25
|
+
default: () => co2_default
|
|
26
|
+
});
|
|
27
|
+
module.exports = __toCommonJS(co2_exports);
|
|
28
|
+
var import_byte = __toESM(require("./1byte.js"));
|
|
29
|
+
var import_sustainable_web_design = __toESM(require("./sustainable-web-design.js"));
|
|
30
|
+
class CO2 {
|
|
31
|
+
constructor(options) {
|
|
32
|
+
this.options = options;
|
|
33
|
+
this.model = new import_byte.default();
|
|
34
|
+
if (options) {
|
|
35
|
+
if (options.model === "swd") {
|
|
36
|
+
this.model = new import_sustainable_web_design.default();
|
|
37
|
+
} else {
|
|
38
|
+
this.model = new options.model();
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
perByte(bytes, green) {
|
|
43
|
+
return this.model.perByte(bytes, green);
|
|
44
|
+
}
|
|
45
|
+
perDomain(pageXray, greenDomains) {
|
|
46
|
+
const co2PerDomain = [];
|
|
47
|
+
for (let domain of Object.keys(pageXray.domains)) {
|
|
48
|
+
let co2;
|
|
49
|
+
if (greenDomains && greenDomains.indexOf(domain) > -1) {
|
|
50
|
+
co2 = this.perByte(pageXray.domains[domain].transferSize, true);
|
|
51
|
+
} else {
|
|
52
|
+
co2 = this.perByte(pageXray.domains[domain].transferSize);
|
|
53
|
+
}
|
|
54
|
+
co2PerDomain.push({
|
|
55
|
+
domain,
|
|
56
|
+
co2,
|
|
57
|
+
transferSize: pageXray.domains[domain].transferSize
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
co2PerDomain.sort((a, b) => b.co2 - a.co2);
|
|
61
|
+
return co2PerDomain;
|
|
62
|
+
}
|
|
63
|
+
perPage(pageXray, green) {
|
|
64
|
+
const domainCO2 = this.perDomain(pageXray, green);
|
|
65
|
+
let totalCO2 = 0;
|
|
66
|
+
for (let domain of domainCO2) {
|
|
67
|
+
totalCO2 += domain.co2;
|
|
68
|
+
}
|
|
69
|
+
return totalCO2;
|
|
70
|
+
}
|
|
71
|
+
perContentType(pageXray, greenDomains) {
|
|
72
|
+
const co2PerContentType = {};
|
|
73
|
+
for (let asset of pageXray.assets) {
|
|
74
|
+
const domain = new URL(asset.url).domain;
|
|
75
|
+
const transferSize = asset.transferSize;
|
|
76
|
+
const co2ForTransfer = this.perByte(transferSize, greenDomains && greenDomains.indexOf(domain) > -1);
|
|
77
|
+
const contentType = asset.type;
|
|
78
|
+
if (!co2PerContentType[contentType]) {
|
|
79
|
+
co2PerContentType[contentType] = { co2: 0, transferSize: 0 };
|
|
80
|
+
}
|
|
81
|
+
co2PerContentType[contentType].co2 += co2ForTransfer;
|
|
82
|
+
co2PerContentType[contentType].transferSize += transferSize;
|
|
83
|
+
}
|
|
84
|
+
const all = [];
|
|
85
|
+
for (let type of Object.keys(co2PerContentType)) {
|
|
86
|
+
all.push({
|
|
87
|
+
type,
|
|
88
|
+
co2: co2PerContentType[type].co2,
|
|
89
|
+
transferSize: co2PerContentType[type].transferSize
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
all.sort((a, b) => b.co2 - a.co2);
|
|
93
|
+
return all;
|
|
94
|
+
}
|
|
95
|
+
dirtiestResources(pageXray, greenDomains) {
|
|
96
|
+
const allAssets = [];
|
|
97
|
+
for (let asset of pageXray.assets) {
|
|
98
|
+
const domain = new URL(asset.url).domain;
|
|
99
|
+
const transferSize = asset.transferSize;
|
|
100
|
+
const co2ForTransfer = this.perByte(transferSize, greenDomains && greenDomains.indexOf(domain) > -1);
|
|
101
|
+
allAssets.push({ url: asset.url, co2: co2ForTransfer, transferSize });
|
|
102
|
+
}
|
|
103
|
+
allAssets.sort((a, b) => b.co2 - a.co2);
|
|
104
|
+
return allAssets.slice(0, allAssets.length > 10 ? 10 : allAssets.length);
|
|
105
|
+
}
|
|
106
|
+
perParty(pageXray, greenDomains) {
|
|
107
|
+
let firstParty = 0;
|
|
108
|
+
let thirdParty = 0;
|
|
109
|
+
const firstPartyRegEx = pageXray.firstPartyRegEx;
|
|
110
|
+
for (let d of Object.keys(pageXray.domains)) {
|
|
111
|
+
if (!d.match(firstPartyRegEx)) {
|
|
112
|
+
thirdParty += this.perByte(pageXray.domains[d].transferSize, greenDomains && greenDomains.indexOf(d) > -1);
|
|
113
|
+
} else {
|
|
114
|
+
firstParty += this.perByte(pageXray.domains[d].transferSize, greenDomains && greenDomains.indexOf(d) > -1);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
return { firstParty, thirdParty };
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
var co2_default = CO2;
|
|
121
|
+
//# sourceMappingURL=co2.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/co2.js"],
|
|
4
|
+
"sourcesContent": ["\"use strict\";\n\nimport OneByte from \"./1byte.js\";\nimport SustainableWebDesign from \"./sustainable-web-design.js\";\n\nclass CO2 {\n constructor(options) {\n this.options = options;\n\n // default model\n this.model = new OneByte();\n\n if (options) {\n if (options.model === \"swd\") {\n this.model = new SustainableWebDesign();\n } else {\n // retain the fallback for people developing\n // new models that follow the same API\n this.model = new options.model();\n }\n }\n }\n\n /**\n * Accept a figure in bytes for data transfer, and a boolean for whether\n * the domain shows as 'green', and return a CO2 figure for energy used to shift the corresponding\n * the data transfer.\n *\n * @param {number} bytes\n * @param {boolean} green\n * @return {number} the amount of CO2 in grammes\n */\n perByte(bytes, green) {\n return this.model.perByte(bytes, green);\n }\n\n perDomain(pageXray, greenDomains) {\n const co2PerDomain = [];\n for (let domain of Object.keys(pageXray.domains)) {\n let co2;\n if (greenDomains && greenDomains.indexOf(domain) > -1) {\n co2 = this.perByte(pageXray.domains[domain].transferSize, true);\n } else {\n co2 = this.perByte(pageXray.domains[domain].transferSize);\n }\n co2PerDomain.push({\n domain,\n co2,\n transferSize: pageXray.domains[domain].transferSize,\n });\n }\n co2PerDomain.sort((a, b) => b.co2 - a.co2);\n\n return co2PerDomain;\n }\n\n perPage(pageXray, green) {\n // Accept an xray object, and if we receive a boolean as the second\n // argument, we assume every request we make is sent to a server\n // running on renwewable power.\n\n // if we receive an array of domains, return a number accounting the\n // reduced CO2 from green hosted domains\n\n const domainCO2 = this.perDomain(pageXray, green);\n let totalCO2 = 0;\n for (let domain of domainCO2) {\n totalCO2 += domain.co2;\n }\n return totalCO2;\n }\n\n perContentType(pageXray, greenDomains) {\n const co2PerContentType = {};\n for (let asset of pageXray.assets) {\n const domain = new URL(asset.url).domain;\n const transferSize = asset.transferSize;\n const co2ForTransfer = this.perByte(\n transferSize,\n greenDomains && greenDomains.indexOf(domain) > -1\n );\n const contentType = asset.type;\n if (!co2PerContentType[contentType]) {\n co2PerContentType[contentType] = { co2: 0, transferSize: 0 };\n }\n co2PerContentType[contentType].co2 += co2ForTransfer;\n co2PerContentType[contentType].transferSize += transferSize;\n }\n // restructure and sort\n const all = [];\n for (let type of Object.keys(co2PerContentType)) {\n all.push({\n type,\n co2: co2PerContentType[type].co2,\n transferSize: co2PerContentType[type].transferSize,\n });\n }\n all.sort((a, b) => b.co2 - a.co2);\n return all;\n }\n\n dirtiestResources(pageXray, greenDomains) {\n const allAssets = [];\n for (let asset of pageXray.assets) {\n const domain = new URL(asset.url).domain;\n const transferSize = asset.transferSize;\n const co2ForTransfer = this.perByte(\n transferSize,\n greenDomains && greenDomains.indexOf(domain) > -1\n );\n allAssets.push({ url: asset.url, co2: co2ForTransfer, transferSize });\n }\n allAssets.sort((a, b) => b.co2 - a.co2);\n\n return allAssets.slice(0, allAssets.length > 10 ? 10 : allAssets.length);\n }\n\n perParty(pageXray, greenDomains) {\n let firstParty = 0;\n let thirdParty = 0;\n // calculate co2 per first/third party\n const firstPartyRegEx = pageXray.firstPartyRegEx;\n for (let d of Object.keys(pageXray.domains)) {\n if (!d.match(firstPartyRegEx)) {\n thirdParty += this.perByte(\n pageXray.domains[d].transferSize,\n greenDomains && greenDomains.indexOf(d) > -1\n );\n } else {\n firstParty += this.perByte(\n pageXray.domains[d].transferSize,\n greenDomains && greenDomains.indexOf(d) > -1\n );\n }\n }\n return { firstParty, thirdParty };\n }\n}\n\nexport { CO2 };\nexport default CO2;\n"],
|
|
5
|
+
"mappings": "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",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|