@tgwf/co2 0.8.0 → 0.10.0
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/.tool-versions +1 -0
- package/CHANGELOG.md +33 -8
- package/README.md +28 -33
- 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 +116 -0
- package/dist/cjs/co2.js.map +7 -0
- package/dist/cjs/co2.test.js +244 -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 +151 -0
- package/dist/cjs/sustainable-web-design.js.map +7 -0
- package/dist/cjs/sustainable-web-design.test.js +85 -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 +93 -0
- package/dist/esm/co2.test.js +228 -0
- 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/dist/esm/hosting-database.node.test.js +20 -0
- package/dist/esm/hosting-json.node.test.js +27 -0
- package/dist/esm/hosting.js +13 -0
- package/{src → dist/esm}/hosting.test.js +18 -38
- package/dist/esm/index.js +8 -0
- package/dist/esm/package.json +3 -0
- package/dist/esm/sustainable-web-design.js +128 -0
- package/dist/esm/sustainable-web-design.test.js +69 -0
- package/fixup +19 -0
- package/package.json +37 -16
- package/public/index.html +57 -0
- package/public/index.js +2 -0
- package/public/index.js.map +7 -0
- package/src/1byte.js +40 -6
- package/src/co2.js +24 -47
- package/src/constants/file-size.js +5 -0
- package/src/constants/index.js +3 -0
- package/src/helpers/index.js +3 -0
- 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/readme.md +66 -0
- package/src/sustainable-web-design.js +292 -0
- 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/src/1byte.test.js +0 -16
- package/src/co2.test.js +0 -150
- package/src/green-byte.js +0 -26
- package/src/hosting-api.test.js +0 -37
- package/src/hosting-database.test.js +0 -31
- package/src/hosting-json.test.js +0 -44
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/.tool-versions
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
nodejs 18.4.0
|
package/CHANGELOG.md
CHANGED
|
@@ -4,11 +4,36 @@ 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
|
-
-
|
|
8
7
|
|
|
9
|
-
|
|
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.
|
|
10
14
|
|
|
11
|
-
|
|
15
|
+
## 0.10.0 2022-06-27
|
|
16
|
+
|
|
17
|
+
- Added ES import syntax as the main way for handling imports and exports of code within the module.
|
|
18
|
+
- Changed eslint settings to use later version of ecmascript (2020)
|
|
19
|
+
- Change the bulid tools to use esbulid with jest instead of babel
|
|
20
|
+
- Added more consistent use of the debug logging library in files using the updated import syntax
|
|
21
|
+
- 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.
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
# [0.9.0] - 2022-03-28
|
|
25
|
+
|
|
26
|
+
### Added
|
|
27
|
+
|
|
28
|
+
- Added newly implemented Sustainable Web Design model [thanks @dryden!]
|
|
29
|
+
- Added new readme page for using both emissions models
|
|
30
|
+
- Added new source of data to the Sustainable Web Design model from Ember Climate.
|
|
31
|
+
### Changed
|
|
32
|
+
|
|
33
|
+
- Changed the CO2 class to accept either the One Byte model or the Sustainable Web Design model
|
|
34
|
+
### Fixed
|
|
35
|
+
|
|
36
|
+
- Fixed various typos.
|
|
12
37
|
|
|
13
38
|
# [0.8.0] - 2021-11-28
|
|
14
39
|
|
|
@@ -23,7 +48,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
23
48
|
|
|
24
49
|
### Changed
|
|
25
50
|
|
|
26
|
-
- The
|
|
51
|
+
- The 1byte model will give different numbers now. It's mentioned in `#fixed` but it's worth repeating.
|
|
27
52
|
|
|
28
53
|
## [0.7.0] - 2021-11-28
|
|
29
54
|
|
|
@@ -61,9 +86,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
61
86
|
|
|
62
87
|
### Changed
|
|
63
88
|
|
|
64
|
-
-
|
|
65
|
-
-
|
|
66
|
-
-
|
|
89
|
+
- Updated README
|
|
90
|
+
- Updated the emissions figured for green energy after further research on methodology with @@JamieBeevor
|
|
91
|
+
- Incorporated class based CO2 models from @soulgalore
|
|
67
92
|
- Credit contributors
|
|
68
93
|
|
|
69
94
|
|
|
@@ -83,7 +108,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
83
108
|
|
|
84
109
|
### Added
|
|
85
110
|
|
|
86
|
-
|
|
111
|
+
Added the (currently unused) green byte model.
|
|
87
112
|
|
|
88
113
|
### Changed
|
|
89
114
|
|
package/README.md
CHANGED
|
@@ -3,24 +3,37 @@
|
|
|
3
3
|
<img src="https://github.com/thegreenwebfoundation/co2.js/actions/workflows/unittests.yml/badge.svg" />
|
|
4
4
|
|
|
5
5
|
|
|
6
|
-
We know computers use electricity, and because most of the electricity we use comes from burning fossil fuels
|
|
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
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
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
11
|
|
|
12
|
-
|
|
12
|
+
### How it works
|
|
13
13
|
|
|
14
|
-
It
|
|
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
|
+
|
|
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
|
+
|
|
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
|
+
|
|
20
|
+
### Who uses it
|
|
21
|
+
|
|
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.
|
|
15
23
|
|
|
16
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.
|
|
17
25
|
|
|
18
|
-
This is open source software, with all the guarantees associated
|
|
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].
|
|
19
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
|
|
20
36
|
[soberDigital]: https://theshiftproject.org/en/lean-ict-2/
|
|
21
|
-
[sitespeed.io]: https://sitespeed.io/
|
|
22
|
-
[tgwf]: https://www.thegreenwebfoundation.org/
|
|
23
|
-
[tgwf-services]: https://www.thegreenwebfoundation.org/services/
|
|
24
37
|
|
|
25
38
|
|
|
26
39
|
## Usage
|
|
@@ -29,7 +42,7 @@ This is open source software, with all the guarantees associated, so if you want
|
|
|
29
42
|
|
|
30
43
|
#### Server-side
|
|
31
44
|
|
|
32
|
-
This approach relies on the `fs` module and so can only be used on platforms
|
|
45
|
+
This approach relies on the `fs` module and so can only be used on platforms like Node.js, that support this.
|
|
33
46
|
|
|
34
47
|
```js
|
|
35
48
|
|
|
@@ -44,17 +57,17 @@ console.log(`Sending a gigabyte, had a carbon footprint of ${estimatedCO2.toFixe
|
|
|
44
57
|
|
|
45
58
|
#### Browser-side
|
|
46
59
|
|
|
47
|
-
For
|
|
60
|
+
For example, like this:
|
|
48
61
|
|
|
49
62
|
```js
|
|
50
63
|
|
|
51
|
-
|
|
64
|
+
import { co2 } from '@tgwf/co2'
|
|
52
65
|
const bytesSent = (1024 * 1024 * 1024)
|
|
53
|
-
const co2Emission = new
|
|
66
|
+
const co2Emission = new co2();
|
|
54
67
|
estimatedCO2 = co2Emission.perByte(bytesSent)
|
|
55
68
|
|
|
56
69
|
console.log(`Sending a gigabyte, had a carbon footprint of ${estimatedCO2.toFixed(3)} grams of CO2`)
|
|
57
|
-
|
|
70
|
+
****
|
|
58
71
|
```
|
|
59
72
|
|
|
60
73
|
### Checking for green power
|
|
@@ -63,35 +76,17 @@ Because different digital services and websites use different forms of power, th
|
|
|
63
76
|
|
|
64
77
|
```js
|
|
65
78
|
|
|
66
|
-
|
|
79
|
+
import { hosting } from '@tgwf/co2'
|
|
67
80
|
|
|
68
81
|
// returns true if green, otherwise false
|
|
69
|
-
|
|
82
|
+
hosting.check("google.com")
|
|
70
83
|
|
|
71
84
|
// returns an array of the green domains, in this case ["google.com"].
|
|
72
|
-
|
|
85
|
+
hosting.check(["google.com", "kochindustries.com"])]
|
|
73
86
|
|
|
74
|
-
// returns an array of green domains, again in this case, ["google.com"]
|
|
75
|
-
greencheck.checkPage(["google.com"])
|
|
76
87
|
|
|
77
88
|
```
|
|
78
89
|
|
|
79
|
-
### Notes
|
|
80
|
-
|
|
81
|
-
Please note, we currently look at just the carbon cost of _generating_ the electricity, similar to how the [International Energy Agency (IEA)] does, not the full life cycle cost of the energy.
|
|
82
|
-
|
|
83
|
-
Doing this would include things like:
|
|
84
|
-
|
|
85
|
-
- the carbon emitted when carrying out activity associated with digging up the fuel
|
|
86
|
-
- the carbon associated with mining the materials to _build_ the power stations, datacentres, and so on
|
|
87
|
-
- the end of life costs
|
|
88
|
-
- the maintenance costs over the life of the datacentres, power generation and end user devices, and the rest of the internet
|
|
89
|
-
|
|
90
|
-
Life cycle figures do exist, but they are very difficult to do well. If you're interested in contributing to this, we'd love to hear from you.
|
|
91
|
-
|
|
92
|
-
|
|
93
90
|
# Licenses
|
|
94
91
|
|
|
95
92
|
Apache 2.0
|
|
96
|
-
|
|
97
|
-
[International Energy Agency (IEA)]: https://www.iea.org/
|
|
@@ -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,116 @@
|
|
|
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
|
+
class CO2 {
|
|
30
|
+
constructor(options) {
|
|
31
|
+
this.options = options;
|
|
32
|
+
this.model = new import_byte.default();
|
|
33
|
+
if (options) {
|
|
34
|
+
this.model = new options.model();
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
perByte(bytes, green) {
|
|
38
|
+
return this.model.perByte(bytes, green);
|
|
39
|
+
}
|
|
40
|
+
perDomain(pageXray, greenDomains) {
|
|
41
|
+
const co2PerDomain = [];
|
|
42
|
+
for (let domain of Object.keys(pageXray.domains)) {
|
|
43
|
+
let co2;
|
|
44
|
+
if (greenDomains && greenDomains.indexOf(domain) > -1) {
|
|
45
|
+
co2 = this.perByte(pageXray.domains[domain].transferSize, true);
|
|
46
|
+
} else {
|
|
47
|
+
co2 = this.perByte(pageXray.domains[domain].transferSize);
|
|
48
|
+
}
|
|
49
|
+
co2PerDomain.push({
|
|
50
|
+
domain,
|
|
51
|
+
co2,
|
|
52
|
+
transferSize: pageXray.domains[domain].transferSize
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
co2PerDomain.sort((a, b) => b.co2 - a.co2);
|
|
56
|
+
return co2PerDomain;
|
|
57
|
+
}
|
|
58
|
+
perPage(pageXray, green) {
|
|
59
|
+
const domainCO2 = this.perDomain(pageXray, green);
|
|
60
|
+
let totalCO2 = 0;
|
|
61
|
+
for (let domain of domainCO2) {
|
|
62
|
+
totalCO2 += domain.co2;
|
|
63
|
+
}
|
|
64
|
+
return totalCO2;
|
|
65
|
+
}
|
|
66
|
+
perContentType(pageXray, greenDomains) {
|
|
67
|
+
const co2PerContentType = {};
|
|
68
|
+
for (let asset of pageXray.assets) {
|
|
69
|
+
const domain = new URL(asset.url).domain;
|
|
70
|
+
const transferSize = asset.transferSize;
|
|
71
|
+
const co2ForTransfer = this.perByte(transferSize, greenDomains && greenDomains.indexOf(domain) > -1);
|
|
72
|
+
const contentType = asset.type;
|
|
73
|
+
if (!co2PerContentType[contentType]) {
|
|
74
|
+
co2PerContentType[contentType] = { co2: 0, transferSize: 0 };
|
|
75
|
+
}
|
|
76
|
+
co2PerContentType[contentType].co2 += co2ForTransfer;
|
|
77
|
+
co2PerContentType[contentType].transferSize += transferSize;
|
|
78
|
+
}
|
|
79
|
+
const all = [];
|
|
80
|
+
for (let type of Object.keys(co2PerContentType)) {
|
|
81
|
+
all.push({
|
|
82
|
+
type,
|
|
83
|
+
co2: co2PerContentType[type].co2,
|
|
84
|
+
transferSize: co2PerContentType[type].transferSize
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
all.sort((a, b) => b.co2 - a.co2);
|
|
88
|
+
return all;
|
|
89
|
+
}
|
|
90
|
+
dirtiestResources(pageXray, greenDomains) {
|
|
91
|
+
const allAssets = [];
|
|
92
|
+
for (let asset of pageXray.assets) {
|
|
93
|
+
const domain = new URL(asset.url).domain;
|
|
94
|
+
const transferSize = asset.transferSize;
|
|
95
|
+
const co2ForTransfer = this.perByte(transferSize, greenDomains && greenDomains.indexOf(domain) > -1);
|
|
96
|
+
allAssets.push({ url: asset.url, co2: co2ForTransfer, transferSize });
|
|
97
|
+
}
|
|
98
|
+
allAssets.sort((a, b) => b.co2 - a.co2);
|
|
99
|
+
return allAssets.slice(0, allAssets.length > 10 ? 10 : allAssets.length);
|
|
100
|
+
}
|
|
101
|
+
perParty(pageXray, greenDomains) {
|
|
102
|
+
let firstParty = 0;
|
|
103
|
+
let thirdParty = 0;
|
|
104
|
+
const firstPartyRegEx = pageXray.firstPartyRegEx;
|
|
105
|
+
for (let d of Object.keys(pageXray.domains)) {
|
|
106
|
+
if (!d.match(firstPartyRegEx)) {
|
|
107
|
+
thirdParty += this.perByte(pageXray.domains[d].transferSize, greenDomains && greenDomains.indexOf(d) > -1);
|
|
108
|
+
} else {
|
|
109
|
+
firstParty += this.perByte(pageXray.domains[d].transferSize, greenDomains && greenDomains.indexOf(d) > -1);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
return { firstParty, thirdParty };
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
var co2_default = CO2;
|
|
116
|
+
//# 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\";\n\nclass CO2 {\n constructor(options) {\n this.options = options;\n\n // default model\n this.model = new OneByte();\n\n if (options) {\n this.model = new options.model();\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": ";;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAEA,kBAAoB;AAEpB,MAAM,IAAI;AAAA,EACR,YAAY,SAAS;AACnB,SAAK,UAAU;AAGf,SAAK,QAAQ,IAAI,oBAAQ;AAEzB,QAAI,SAAS;AACX,WAAK,QA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