diff --git a/lib/plugins/sustainable/index.js b/lib/plugins/sustainable/index.js index 9cbb0ab977..0911befdda 100644 --- a/lib/plugins/sustainable/index.js +++ b/lib/plugins/sustainable/index.js @@ -1,5 +1,4 @@ import { resolve, join } from 'node:path'; -import { readFileSync } from 'node:fs'; import { fileURLToPath } from 'node:url'; import fs from 'node:fs'; import zlib from 'node:zlib'; @@ -10,9 +9,15 @@ import { co2, hosting } from '@tgwf/co2'; import { SitespeedioPlugin } from '@sitespeed.io/plugin'; import { Aggregator } from './aggregator.js'; -const readFile = promisify(fs.readFile); -const gunzip = promisify(zlib.gunzip); +const fsp = fs.promises; const __dirname = fileURLToPath(new URL('.', import.meta.url)); + +const packageJson = JSON.parse( + await fsp.readFile(resolve(join(__dirname, '..', '..', '..', 'package.json'))) +); +const co2Version = packageJson.dependencies['@tgwf/co2']; + +const gunzip = promisify(zlib.gunzip); const log = intel.getLogger('sitespeedio.plugin.sustainable'); const DEFAULT_METRICS_PAGE_SUMMARY = [ @@ -41,7 +46,7 @@ async function getGzippedFileAsJson(jsonPath) { async function loadJSON(jsonPath) { const jsonBuffer = jsonPath.endsWith('.gz') ? await getGzippedFileAsJson(jsonPath) - : await readFile(jsonPath); + : await fsp.readFile(jsonPath); return JSON.parse(jsonBuffer); } @@ -50,12 +55,15 @@ export default class SustainablePlugin extends SitespeedioPlugin { super({ name: 'sustainable', options, context, queue }); } - open(context, options) { + async open(context, options) { this.storageManager = context.storageManager; this.options = options; this.sustainableOptions = options.sustainable || {}; this.make = context.messageMaker('sustainable').make; - this.pug = readFileSync(resolve(__dirname, 'pug', 'index.pug'), 'utf8'); + this.pug = await fsp.readFile( + resolve(__dirname, 'pug', 'index.pug'), + 'utf8' + ); this.aggregator = new Aggregator(options); this.firstRunsData = {}; context.filterRegistry.registerFilterForType( @@ -204,7 +212,8 @@ export default class SustainablePlugin extends SitespeedioPlugin { co2ThirdParty: co2PerParty.thirdParty, hostingGreenCheck, dirtiestResources, - co2PerContentType + co2PerContentType, + co2Version }, { url: message.url, @@ -226,6 +235,8 @@ export default class SustainablePlugin extends SitespeedioPlugin { const extras = this.firstRunsData[url]; // Attach first run so we can show that extra data that we don't collect stats for summaries.urls[url].firstRun = extras; + + summaries.urls[url].co2Version = co2Version; queue.postMessage( make('sustainable.pageSummary', summaries.urls[url], { url: url, diff --git a/lib/plugins/sustainable/pug/index.pug b/lib/plugins/sustainable/pug/index.pug index b93f4ee0e1..d45af49557 100644 --- a/lib/plugins/sustainable/pug/index.pug +++ b/lib/plugins/sustainable/pug/index.pug @@ -11,6 +11,10 @@ h1 Sustainable Web - let sustainable = pageInfo.data.sustainable.run ? pageInfo.data.sustainable.run : pageInfo.data.sustainable.pageSummary +p The sustainable metrics is calculated using + a(href='https://github.com/thegreenwebfoundation/co2.js') @tgwf/co2 + |  version #{sustainable.co2Version}. + p We know that using the internet means using electricity, and because most of that electricity comes from fossil fuels, it means that more data we send, the more fossil fuels we end up burning. p This is terrible news for the climate, and @@ -27,7 +31,8 @@ if !options.sustainable.pageViews if (sustainable.hostingInfo) p #{sustainable.hostingInfo.url} #{sustainable.hostingInfo.green === true ? 'is hosted green! This means the servers running the site use green energy' : 'does not appear to be running on green energy.'} - p This information comes from The Green Web Foundation's datasets, which are published as open data. If you have questions, corrections or want to use this data yourself, please visit (href='https://www.thegreenwebfoundation.org') The Green Web Foundation website. + p This information comes from The Green Web Foundation's datasets, which are published as open data. If you have questions, corrections or want to use this data yourself, please visit + a(href='https://www.thegreenwebfoundation.org') The Green Web Foundation website. - const co2FirstParty = sustainable.co2FirstParty.median || sustainable.co2FirstParty - const co2ThirdParty = sustainable.co2ThirdParty.median === 0 ? 0 : sustainable.co2ThirdParty.median || sustainable.co2ThirdParty