-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
102 lines (87 loc) · 3.16 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/**
* Welcome to Cloudflare Workers! This is your first worker.
*
* - Run `npm run dev` in your terminal to start a development server
* - Open a browser tab at http://localhost:8787/ to see your worker in action
* - Run `npm run deploy` to publish your worker
*
* Learn more at https://developers.cloudflare.com/workers/
*/
import { gridAwareCO2e, gridAwarePower} from '@greenweb/grid-aware-websites';
import { getLocation } from '@greenweb/gaw-plugin-cloudflare-workers';
import {gridAwareRewriter, regularRewriter} from '../../utils';
export default {
async fetch(request, env, ctx) {
// First fetch the request
const response = await fetch(request.url);
// Then check if the request content type is HTML. If not, return the request and headers as is.
const contentType = response.headers.get('content-type');
const { pathname } = new URL(request.url);
if ((!contentType || !contentType.includes('text/html'))) {
return new Response(response.body, {
...response,
});
}
// If the content type is HTML, we can then do the grid aware checks
let cfData = getLocation(request);
let { country } = cfData;
if (!country) {
return new Response(response.body, {
...response,
headers: {
...response.headers,
'grid-aware': 'Error - Country not found',
},
});
}
const gridData = await gridAwareCO2e(country, env.EMAPS_API_KEY);
// console.log(gridData);
if (gridData.status === 'error') {
return new Response(response.body, {
...response,
headers: {
...response.headers,
'grid-aware': 'Error - Unable to fetch grid data',
},
});
}
// If the gridAware value is set to true, then we need to edit the "data-theme" attribute of the HTML tag to "dark" using the HTmlRewriter
if (gridData.gridAware) {
// Create a new HTMLRewriter instance
// Also add a banner to the top of the page to show that this is a modified page
const rewriter = gridAwareRewriter(gridData, "gridAwareCO2e");
// Return the response with the rewriter applied
return new Response(rewriter.transform(response).body, {
...response,
contentType: 'text/html',
headers: {
...response.headers,
'grid-aware': 'true',
zone: gridData.region,
carbonIntensity: gridData.data.carbonIntensity,
averageCarbonIntensity: gridData.data.averageIntensity,
mode: gridData.data.mode,
minimumPercentage: gridData.data.minimumPercentage,
"low-carbon-percentage": gridData.data["low-carbon percentage"],
"renewable-percentage": gridData.data["renewable percentage"],
},
});
}
const rewriter = regularRewriter(gridData, "gridAwareCO2e");
return new Response(rewriter.transform(response).body, {
...response,
contentType: 'text/html',
headers: {
...response.headers,
'grid-aware': 'false',
zone: gridData.region,
carbonIntensity: gridData.data.carbonIntensity,
averageCarbonIntensity: gridData.data.averageIntensity,
mode: gridData.data.mode,
minimumPercentage: gridData.data.minimumPercentage,
"low-carbon-percentage": gridData.data["low-carbon percentage"],
"renewable-percentage": gridData.data["renewable percentage"],
},
});
}
}