@tgwf/co2 0.17.3-0 → 0.18.1-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/CHANGELOG.md +8 -0
- package/README.md +0 -8
- package/dist/cjs/carbon-txt.js +65 -0
- package/dist/cjs/carbon-txt.js.map +7 -0
- package/dist/cjs/co2.js.map +1 -1
- package/dist/cjs/helpers/index.js +1 -1
- package/dist/esm/carbon-txt.js +44 -0
- package/dist/esm/helpers/index.js +1 -1
- package/package.json +10 -6
package/CHANGELOG.md
CHANGED
|
@@ -19,6 +19,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
19
19
|
|
|
20
20
|
## Released
|
|
21
21
|
|
|
22
|
+
### [0.18.0] - 2026-02-26
|
|
23
|
+
|
|
24
|
+
#### Added
|
|
25
|
+
* Annual grid carbon intensity, renewable percentage, carbon free percentage data from Electricity Maps for the years 2021 - 2025.
|
|
26
|
+
|
|
27
|
+
#### Changed
|
|
28
|
+
* Default carbon estimation model is now the Sustainaable Web Design Model version 4.
|
|
29
|
+
|
|
22
30
|
### [0.18.0-alpha2]
|
|
23
31
|
|
|
24
32
|
#### Added
|
package/README.md
CHANGED
|
@@ -12,14 +12,6 @@ One day, the internet will be powered by renewable energy. Until that day comes,
|
|
|
12
12
|
|
|
13
13
|
## [Documentation](https://developers.thegreenwebfoundation.org/co2js/overview/) | [Changelog](/CHANGELOG.md) | [Roadmap](https://developers.thegreenwebfoundation.org/co2js/roadmap/)
|
|
14
14
|
|
|
15
|
-
> [!IMPORTANT]
|
|
16
|
-
> Important note about upcoming changes in CO2.js v0.18
|
|
17
|
-
In the next release of CO2.js (v0.18 – February 2026) we will be changing the default carbon estimation model to Sustainable Web Design Model version 4. If you are using CO2.js to perform carbon estimation, and are not explicitly declaring the model and version to use then you will be impacted by this change.
|
|
18
|
-
>
|
|
19
|
-
> You can install a PREVIEW release to test your code to see how this change will effect you. To do so, use the `npm install @tgwf/co2@next` command.
|
|
20
|
-
>
|
|
21
|
-
> We have detailed the impacts of this change for users of CO2.js in this GitHub issue – [Impact of changing from v3 to v4](https://github.com/thegreenwebfoundation/co2.js/issues/209#issuecomment-3624614392)
|
|
22
|
-
|
|
23
15
|
## What is CO2.js?
|
|
24
16
|
|
|
25
17
|
CO2.js is a JavaScript library that enables developers a way to estimate the emissions related to use of their apps, websites, and software.
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var carbon_txt_exports = {};
|
|
20
|
+
__export(carbon_txt_exports, {
|
|
21
|
+
check: () => check,
|
|
22
|
+
default: () => carbon_txt_default
|
|
23
|
+
});
|
|
24
|
+
module.exports = __toCommonJS(carbon_txt_exports);
|
|
25
|
+
var import_helpers = require("./helpers/index.js");
|
|
26
|
+
const processResponse = (res, verbose = false) => {
|
|
27
|
+
var _a, _b, _c, _d;
|
|
28
|
+
if (!res.success && !res.errors) {
|
|
29
|
+
return {
|
|
30
|
+
success: false,
|
|
31
|
+
errors: [res.detail[0].msg]
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
if (verbose) {
|
|
35
|
+
return res;
|
|
36
|
+
}
|
|
37
|
+
if (!res.success) {
|
|
38
|
+
return { success: false, errors: res.errors };
|
|
39
|
+
}
|
|
40
|
+
return {
|
|
41
|
+
success: true,
|
|
42
|
+
url: res.url,
|
|
43
|
+
disclosures: (_b = (_a = res == null ? void 0 : res.data) == null ? void 0 : _a.org) == null ? void 0 : _b.disclosures,
|
|
44
|
+
upstream: (_d = (_c = res == null ? void 0 : res.data) == null ? void 0 : _c.upstream) == null ? void 0 : _d.services
|
|
45
|
+
};
|
|
46
|
+
};
|
|
47
|
+
async function check(domain, options) {
|
|
48
|
+
if (typeof domain !== "string") {
|
|
49
|
+
throw new Error("Invalid domain. Domain must be a string.");
|
|
50
|
+
}
|
|
51
|
+
const agentId = options == null ? void 0 : options.userAgentIdentifier;
|
|
52
|
+
const verbose = (options == null ? void 0 : options.verbose) || false;
|
|
53
|
+
const req = await fetch(
|
|
54
|
+
`https://carbon-txt-api.greenweb.org/api/validate/domain/`,
|
|
55
|
+
{
|
|
56
|
+
headers: (0, import_helpers.getApiRequestHeaders)(agentId),
|
|
57
|
+
method: "POST",
|
|
58
|
+
body: JSON.stringify({ domain })
|
|
59
|
+
}
|
|
60
|
+
);
|
|
61
|
+
const res = await req.json();
|
|
62
|
+
return processResponse(res, verbose);
|
|
63
|
+
}
|
|
64
|
+
var carbon_txt_default = check;
|
|
65
|
+
//# sourceMappingURL=carbon-txt.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/carbon-txt.js"],
|
|
4
|
+
"sourcesContent": ["\"use strict\";\n\nimport { getApiRequestHeaders } from \"./helpers/index.js\";\n\n/**\n * @module carbontxt-validator\n */\n\n/**\n * Process the response from the carbon.txt validator API\n * @param {object} res - The response object from the API\n * @param {boolean} verbose - Optional. Whether to return verbose results\n * @returns {object} - The processed response object\n */\nconst processResponse = (res, verbose = false) => {\n // This captures errors when an invalid domain is sent to the API\n if (!res.success && !res.errors) {\n return {\n success: false,\n errors: [res.detail[0].msg],\n };\n }\n\n if (verbose) {\n return res;\n }\n\n if (!res.success) {\n return { success: false, errors: res.errors };\n }\n\n return {\n success: true,\n url: res.url,\n disclosures: res?.data?.org?.disclosures,\n upstream: res?.data?.upstream?.services,\n };\n};\n\n/**\n * Perform a domain lookup using the Green Web Foundation carbon.txt validator endpoint\n * @param {string} domain - The domain to check, or an array of domains to be checked.\n * @param {string} options - Optional. An object of domain check options, or a string\n * @param {string} options.userAgentIdentifier - Optional. A string representing the app, site, or organisation that is making the request.\n * @param {string} options.verbose - Optional. A boolean indicating whether to return verbose results.\n */\n\nexport async function check(domain, options) {\n if (typeof domain !== \"string\") {\n throw new Error(\"Invalid domain. Domain must be a string.\");\n }\n\n const agentId = options?.userAgentIdentifier;\n const verbose = options?.verbose || false;\n\n const req = await fetch(\n `https://carbon-txt-api.greenweb.org/api/validate/domain/`,\n {\n headers: getApiRequestHeaders(agentId),\n method: \"POST\",\n body: JSON.stringify({ domain }),\n }\n );\n\n const res = await req.json();\n return processResponse(res, verbose);\n}\n\nexport default check;\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAEA,qBAAqC;AAYrC,MAAM,kBAAkB,CAAC,KAAK,UAAU,UAAU;AAdlD;AAgBE,MAAI,CAAC,IAAI,WAAW,CAAC,IAAI,QAAQ;AAC/B,WAAO;AAAA,MACL,SAAS;AAAA,MACT,QAAQ,CAAC,IAAI,OAAO,CAAC,EAAE,GAAG;AAAA,IAC5B;AAAA,EACF;AAEA,MAAI,SAAS;AACX,WAAO;AAAA,EACT;AAEA,MAAI,CAAC,IAAI,SAAS;AAChB,WAAO,EAAE,SAAS,OAAO,QAAQ,IAAI,OAAO;AAAA,EAC9C;AAEA,SAAO;AAAA,IACL,SAAS;AAAA,IACT,KAAK,IAAI;AAAA,IACT,cAAa,sCAAK,SAAL,mBAAW,QAAX,mBAAgB;AAAA,IAC7B,WAAU,sCAAK,SAAL,mBAAW,aAAX,mBAAqB;AAAA,EACjC;AACF;AAUA,eAAsB,MAAM,QAAQ,SAAS;AAC3C,MAAI,OAAO,WAAW,UAAU;AAC9B,UAAM,IAAI,MAAM,0CAA0C;AAAA,EAC5D;AAEA,QAAM,UAAU,mCAAS;AACzB,QAAM,WAAU,mCAAS,YAAW;AAEpC,QAAM,MAAM,MAAM;AAAA,IAChB;AAAA,IACA;AAAA,MACE,aAAS,qCAAqB,OAAO;AAAA,MACrC,QAAQ;AAAA,MACR,MAAM,KAAK,UAAU,EAAE,OAAO,CAAC;AAAA,IACjC;AAAA,EACF;AAEA,QAAM,MAAM,MAAM,IAAI,KAAK;AAC3B,SAAO,gBAAgB,KAAK,OAAO;AACrC;AAEA,IAAO,qBAAQ;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
package/dist/cjs/co2.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/co2.js"],
|
|
4
|
-
"sourcesContent": ["\"use strict\";\n\n/**\n * @typedef {Object} CO2EstimateTraceResultPerByte\n * @property {number|CO2EstimateComponentsPerByte} co2 - The CO2 estimate in grams or its separate components\n * @property {boolean} green - Whether the domain is green or not\n * @property {TraceResultVariablesPerByte} variables - The variables used to calculate the CO2 estimate\n */\n\n/**\n * @typedef {Object} CO2EstimateTraceResultPerVisit\n * @property {number|CO2EstimateComponentsPerVisit} co2 - The CO2 estimate in grams or its separate components\n * @property {boolean} green - Whether the domain is green or not\n * @property {TraceResultVariablesPerVisit} variables - The variables used to calculate the CO2 estimate\n */\n\n/**\n * @typedef {Object} TraceResultVariablesPerByte\n * @property {GridIntensityVariables} gridIntensity - The grid intensity related variables\n */\n/**\n * @typedef {Object} TraceResultVariablesPerVisit\n * @property {GridIntensityVariables} gridIntensity - The grid intensity related variables\n * @property {number} dataReloadRatio - What percentage of a page is reloaded on each subsequent page view\n * @property {number} firstVisitPercentage - What percentage of visits are loading this page for subsequent times\n * @property {number} returnVisitPercentage - What percentage of visits are loading this page for the second or more time\n */\n\n/**\n * @typedef {Object} GridIntensityVariables\n * @property {string} description - The description of the variables\n * @property {number} network - The network grid intensity set by the user or the default\n * @property {number} dataCenter - The data center grid intensity set by the user or the default\n * @property {number} device - The device grid intensity set by the user or the default\n * @property {number} production - The production grid intensity set by the user or the default\n */\n\n/**\n * @typedef {Object} CO2EstimateComponentsPerByte\n * @property {number} networkCO2 - The CO2 estimate for networking in grams\n * @property {number} dataCenterCO2 - The CO2 estimate for data centers in grams\n * @property {number} consumerDeviceCO2 - The CO2 estimate for consumer devices in grams\n * @property {number} productionCO2 - The CO2 estimate for device production in grams\n * @property {string} rating - The rating of the CO2 estimate based on the Sustainable Web Design Model\n * @property {number} total - The total CO2 estimate in grams\n */\n\n/**\n * @typedef {Object} CO2EstimateComponentsPerVisit\n * @property {number} 'networkCO2 - first' - The CO2 estimate for networking in grams on first visit\n * @property {number} 'networkCO2 - subsequent' - The CO2 estimate for networking in grams on subsequent visits\n * @property {number} 'dataCenterCO2 - first' - The CO2 estimate for data centers in grams on first visit\n * @property {number} 'dataCenterCO2 - subsequent' - The CO2 estimate for data centers in grams on subsequent visits\n * @property {number} 'consumerDeviceCO2 - first' - The CO2 estimate for consumer devices in grams on first visit\n * @property {number} 'consumerDeviceCO2 - subsequent' - The CO2 estimate for consumer devices in grams on subsequent visits\n * @property {number} 'productionCO2 - first' - The CO2 estimate for device production in grams on first visit\n * @property {number} 'productionCO2 - subsequent' - The CO2 estimate for device production in grams on subsequent visits\n * @property {string} rating - The rating of the CO2 estimate based on the Sustainable Web Design Model\n * @property {number} total - The total CO2 estimate in grams\n */\n\nimport OneByte from \"./1byte.js\";\nimport SustainableWebDesignV3 from \"./sustainable-web-design-v3.js\";\nimport SustainableWebDesignV4 from \"./sustainable-web-design-v4.js\";\n\nimport {\n parseByteTraceOptions,\n parseVisitTraceOptions,\n} from \"./helpers/index.js\";\n\nclass CO2 {\n constructor(options) {\n this.model = new SustainableWebDesignV4();\n // Using optional chaining allows an empty object to be passed\n // in without breaking the code.\n if (options?.model === \"1byte\") {\n this.model = new OneByte();\n } else if (options?.model === \"swd\") {\n this.model = new SustainableWebDesignV4();\n if (options?.version === 3) {\n this.model = new SustainableWebDesignV3();\n } else if (options?.version === 4) {\n this.model = new SustainableWebDesignV4();\n }\n } else if (options?.model) {\n throw new Error(\n `\"${options.model}\" is not a valid model. Please use \"1byte\" for the OneByte model, and \"swd\" for the Sustainable Web Design model.\\nSee https://developers.thegreenwebfoundation.org/co2js/models/ to learn more about the models available in CO2.js.`,\n );\n } else if (!options?.model && options?.version) {\n throw new Error(\n `\"Specified version ${options?.version} but an estimation model is missing. Please specify a model to use for the version you have set.`,\n );\n }\n\n if (options?.rating && typeof options.rating !== \"boolean\") {\n throw new Error(\n `The rating option must be a boolean. Please use true or false.\\nSee https://developers.thegreenwebfoundation.org/co2js/options/ to learn more about the options available in CO2.js.`,\n );\n }\n\n // This flag checks to see if the model itself has a rating system.\n const allowRatings = !!this.model.allowRatings;\n\n /** @private */\n this._segment = options?.results === \"segment\";\n // This flag is set by the user to enable the rating system.\n this._rating = options?.rating === true;\n\n // The rating system is only supported in the Sustainable Web Design Model.\n if (!allowRatings && this._rating) {\n throw new Error(\n `The rating system is not supported in the model you are using. Try using the Sustainable Web Design model instead.\\nSee https://developers.thegreenwebfoundation.org/co2js/models/ to learn more about the models available in CO2.js.`,\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|CO2EstimateComponentsPerByte} the amount of CO2 in grammes or its separate components\n */\n perByte(bytes, green = false) {\n return this.model.perByte(bytes, green, this._segment, this._rating);\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|CO2EstimateComponentsPerVisit} the amount of CO2 in grammes or its separate components\n */\n perVisit(bytes, green = false) {\n if (this.model?.perVisit) {\n return this.model.perVisit(bytes, green, this._segment, this._rating);\n } else {\n throw new Error(\n `The perVisit() method is not supported in the model you are using. Try using perByte() instead.\\nSee https://developers.thegreenwebfoundation.org/co2js/methods/ to learn more about the methods available in CO2.js.`,\n );\n }\n }\n\n /**\n * Accept a figure in bytes for data transfer, a boolean for whether\n * the domain shows as 'green', and an options object.\n * Returns an object containing CO2 figure, green boolean, and object of the variables used in calculating the CO2 figure.\n *\n * @param {number} bytes\n * @param {boolean} green\n * @param {Object} options\n * @return {CO2EstimateTraceResultPerByte} the amount of CO2 in grammes\n */\n perByteTrace(bytes, green = false, options = {}) {\n const adjustments = parseByteTraceOptions(\n options,\n this.model.version,\n green,\n );\n\n // Filter out the trace items that aren't relevant to this function.\n const { gridIntensity, ...traceVariables } = adjustments;\n const {\n dataReloadRatio,\n firstVisitPercentage,\n returnVisitPercentage,\n ...otherVariables\n } = traceVariables;\n return {\n co2: this.model.perByte(\n bytes,\n green,\n this._segment,\n this._rating,\n adjustments,\n ),\n green,\n variables: {\n description:\n \"Below are the variables used to calculate this CO2 estimate.\",\n bytes,\n gridIntensity: {\n description:\n \"The grid intensity (grams per kilowatt-hour) used to calculate this CO2 estimate.\",\n ...adjustments.gridIntensity,\n },\n ...otherVariables,\n },\n };\n }\n\n /**\n * Accept a figure in bytes for data transfer, a boolean for whether\n * the domain shows as 'green', and an options object.\n * Returns an object containing CO2 figure, green boolean, and object of the variables used in calculating the CO2 figure.\n *\n * @param {number} bytes\n * @param {boolean} green\n * @param {Object} options\n * @return {CO2EstimateTraceResultPerVisit} the amount of CO2 in grammes\n */\n perVisitTrace(bytes, green = false, options = {}) {\n if (this.model?.perVisit) {\n const adjustments = parseVisitTraceOptions(\n options,\n this.model.version,\n green,\n );\n const { gridIntensity, ...variables } = adjustments;\n\n return {\n co2: this.model.perVisit(\n bytes,\n green,\n this._segment,\n this._rating,\n adjustments,\n ),\n green,\n variables: {\n description:\n \"Below are the variables used to calculate this CO2 estimate.\",\n bytes,\n gridIntensity: {\n description:\n \"The grid intensity (grams per kilowatt-hour) used to calculate this CO2 estimate.\",\n ...adjustments.gridIntensity,\n },\n ...variables,\n },\n };\n } else {\n throw new Error(\n `The perVisitTrace() method is not supported in the model you are using. Try using perByte() instead.\\nSee https://developers.thegreenwebfoundation.org/co2js/methods/ to learn more about the methods available in CO2.js.`,\n );\n }\n }\n\n SustainableWebDesignV3() {\n return new SustainableWebDesignV3();\n }\n\n SustainableWebDesignV4() {\n return new SustainableWebDesignV4();\n }\n\n OneByte() {\n return new OneByte();\n }\n}\n\nexport { CO2 };\nexport default CO2;\n"],
|
|
4
|
+
"sourcesContent": ["\"use strict\";\n\n/**\n * @typedef {Object} CO2EstimateTraceResultPerByte\n * @property {number|CO2EstimateComponentsPerByte} co2 - The CO2 estimate in grams or its separate components\n * @property {boolean} green - Whether the domain is green or not\n * @property {TraceResultVariablesPerByte} variables - The variables used to calculate the CO2 estimate\n */\n\n/**\n * @typedef {Object} CO2EstimateTraceResultPerVisit\n * @property {number|CO2EstimateComponentsPerVisit} co2 - The CO2 estimate in grams or its separate components\n * @property {boolean} green - Whether the domain is green or not\n * @property {TraceResultVariablesPerVisit} variables - The variables used to calculate the CO2 estimate\n */\n\n/**\n * @typedef {Object} TraceResultVariablesPerByte\n * @property {GridIntensityVariables} gridIntensity - The grid intensity related variables\n */\n/**\n * @typedef {Object} TraceResultVariablesPerVisit\n * @property {GridIntensityVariables} gridIntensity - The grid intensity related variables\n * @property {number} dataReloadRatio - What percentage of a page is reloaded on each subsequent page view\n * @property {number} firstVisitPercentage - What percentage of visits are loading this page for subsequent times\n * @property {number} returnVisitPercentage - What percentage of visits are loading this page for the second or more time\n */\n\n/**\n * @typedef {Object} GridIntensityVariables\n * @property {string} description - The description of the variables\n * @property {number} network - The network grid intensity set by the user or the default\n * @property {number} dataCenter - The data center grid intensity set by the user or the default\n * @property {number} device - The device grid intensity set by the user or the default\n * @property {number} production - The production grid intensity set by the user or the default\n */\n\n/**\n * @typedef {Object} CO2EstimateComponentsPerByte\n * @property {number} networkCO2 - The CO2 estimate for networking in grams\n * @property {number} dataCenterCO2 - The CO2 estimate for data centers in grams\n * @property {number} consumerDeviceCO2 - The CO2 estimate for consumer devices in grams\n * @property {number} productionCO2 - The CO2 estimate for device production in grams\n * @property {string} rating - The rating of the CO2 estimate based on the Sustainable Web Design Model\n * @property {number} total - The total CO2 estimate in grams\n */\n\n/**\n * @typedef {Object} CO2EstimateComponentsPerVisit\n * @property {number} 'networkCO2 - first' - The CO2 estimate for networking in grams on first visit\n * @property {number} 'networkCO2 - subsequent' - The CO2 estimate for networking in grams on subsequent visits\n * @property {number} 'dataCenterCO2 - first' - The CO2 estimate for data centers in grams on first visit\n * @property {number} 'dataCenterCO2 - subsequent' - The CO2 estimate for data centers in grams on subsequent visits\n * @property {number} 'consumerDeviceCO2 - first' - The CO2 estimate for consumer devices in grams on first visit\n * @property {number} 'consumerDeviceCO2 - subsequent' - The CO2 estimate for consumer devices in grams on subsequent visits\n * @property {number} 'productionCO2 - first' - The CO2 estimate for device production in grams on first visit\n * @property {number} 'productionCO2 - subsequent' - The CO2 estimate for device production in grams on subsequent visits\n * @property {string} rating - The rating of the CO2 estimate based on the Sustainable Web Design Model\n * @property {number} total - The total CO2 estimate in grams\n */\n\nimport OneByte from \"./1byte.js\";\nimport SustainableWebDesignV3 from \"./sustainable-web-design-v3.js\";\nimport SustainableWebDesignV4 from \"./sustainable-web-design-v4.js\";\n\nimport {\n parseByteTraceOptions,\n parseVisitTraceOptions,\n} from \"./helpers/index.js\";\n\nclass CO2 {\n constructor(options) {\n this.model = new SustainableWebDesignV4();\n // Using optional chaining allows an empty object to be passed\n // in without breaking the code.\n if (options?.model === \"1byte\") {\n this.model = new OneByte();\n } else if (options?.model === \"swd\") {\n this.model = new SustainableWebDesignV4();\n if (options?.version === 3) {\n this.model = new SustainableWebDesignV3();\n } else if (options?.version === 4) {\n this.model = new SustainableWebDesignV4();\n }\n } else if (options?.model) {\n throw new Error(\n `\"${options.model}\" is not a valid model. Please use \"1byte\" for the OneByte model, and \"swd\" for the Sustainable Web Design model.\\nSee https://developers.thegreenwebfoundation.org/co2js/models/ to learn more about the models available in CO2.js.`\n );\n } else if (!options?.model && options?.version) {\n throw new Error(\n `\"Specified version ${options?.version} but an estimation model is missing. Please specify a model to use for the version you have set.`\n );\n }\n\n if (options?.rating && typeof options.rating !== \"boolean\") {\n throw new Error(\n `The rating option must be a boolean. Please use true or false.\\nSee https://developers.thegreenwebfoundation.org/co2js/options/ to learn more about the options available in CO2.js.`\n );\n }\n\n // This flag checks to see if the model itself has a rating system.\n const allowRatings = !!this.model.allowRatings;\n\n /** @private */\n this._segment = options?.results === \"segment\";\n // This flag is set by the user to enable the rating system.\n this._rating = options?.rating === true;\n\n // The rating system is only supported in the Sustainable Web Design Model.\n if (!allowRatings && this._rating) {\n throw new Error(\n `The rating system is not supported in the model you are using. Try using the Sustainable Web Design model instead.\\nSee https://developers.thegreenwebfoundation.org/co2js/models/ to learn more about the models available in CO2.js.`\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|CO2EstimateComponentsPerByte} the amount of CO2 in grammes or its separate components\n */\n perByte(bytes, green = false) {\n return this.model.perByte(bytes, green, this._segment, this._rating);\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|CO2EstimateComponentsPerVisit} the amount of CO2 in grammes or its separate components\n */\n perVisit(bytes, green = false) {\n if (this.model?.perVisit) {\n return this.model.perVisit(bytes, green, this._segment, this._rating);\n } else {\n throw new Error(\n `The perVisit() method is not supported in the model you are using. Try using perByte() instead.\\nSee https://developers.thegreenwebfoundation.org/co2js/methods/ to learn more about the methods available in CO2.js.`\n );\n }\n }\n\n /**\n * Accept a figure in bytes for data transfer, a boolean for whether\n * the domain shows as 'green', and an options object.\n * Returns an object containing CO2 figure, green boolean, and object of the variables used in calculating the CO2 figure.\n *\n * @param {number} bytes\n * @param {boolean} green\n * @param {Object} options\n * @return {CO2EstimateTraceResultPerByte} the amount of CO2 in grammes\n */\n perByteTrace(bytes, green = false, options = {}) {\n const adjustments = parseByteTraceOptions(\n options,\n this.model.version,\n green\n );\n\n // Filter out the trace items that aren't relevant to this function.\n const { gridIntensity, ...traceVariables } = adjustments;\n const {\n dataReloadRatio,\n firstVisitPercentage,\n returnVisitPercentage,\n ...otherVariables\n } = traceVariables;\n return {\n co2: this.model.perByte(\n bytes,\n green,\n this._segment,\n this._rating,\n adjustments\n ),\n green,\n variables: {\n description:\n \"Below are the variables used to calculate this CO2 estimate.\",\n bytes,\n gridIntensity: {\n description:\n \"The grid intensity (grams per kilowatt-hour) used to calculate this CO2 estimate.\",\n ...adjustments.gridIntensity,\n },\n ...otherVariables,\n },\n };\n }\n\n /**\n * Accept a figure in bytes for data transfer, a boolean for whether\n * the domain shows as 'green', and an options object.\n * Returns an object containing CO2 figure, green boolean, and object of the variables used in calculating the CO2 figure.\n *\n * @param {number} bytes\n * @param {boolean} green\n * @param {Object} options\n * @return {CO2EstimateTraceResultPerVisit} the amount of CO2 in grammes\n */\n perVisitTrace(bytes, green = false, options = {}) {\n if (this.model?.perVisit) {\n const adjustments = parseVisitTraceOptions(\n options,\n this.model.version,\n green\n );\n const { gridIntensity, ...variables } = adjustments;\n\n return {\n co2: this.model.perVisit(\n bytes,\n green,\n this._segment,\n this._rating,\n adjustments\n ),\n green,\n variables: {\n description:\n \"Below are the variables used to calculate this CO2 estimate.\",\n bytes,\n gridIntensity: {\n description:\n \"The grid intensity (grams per kilowatt-hour) used to calculate this CO2 estimate.\",\n ...adjustments.gridIntensity,\n },\n ...variables,\n },\n };\n } else {\n throw new Error(\n `The perVisitTrace() method is not supported in the model you are using. Try using perByte() instead.\\nSee https://developers.thegreenwebfoundation.org/co2js/methods/ to learn more about the methods available in CO2.js.`\n );\n }\n }\n\n SustainableWebDesignV3() {\n return new SustainableWebDesignV3();\n }\n\n SustainableWebDesignV4() {\n return new SustainableWebDesignV4();\n }\n\n OneByte() {\n return new OneByte();\n }\n}\n\nexport { CO2 };\nexport default CO2;\n"],
|
|
5
5
|
"mappings": "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",
|
|
6
6
|
"names": ["SustainableWebDesignV4", "OneByte", "SustainableWebDesignV3"]
|
|
7
7
|
}
|
|
@@ -197,7 +197,7 @@ Falling back to default value.`
|
|
|
197
197
|
return adjustments;
|
|
198
198
|
}
|
|
199
199
|
function getApiRequestHeaders(comment = "") {
|
|
200
|
-
return { "User-Agent": `co2js/${"0.
|
|
200
|
+
return { "User-Agent": `co2js/${"0.18.1-0"} ${comment}` };
|
|
201
201
|
}
|
|
202
202
|
function outputRating(co2e, swdmVersion) {
|
|
203
203
|
let {
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { getApiRequestHeaders } from "./helpers/index.js";
|
|
2
|
+
const processResponse = (res, verbose = false) => {
|
|
3
|
+
var _a, _b, _c, _d;
|
|
4
|
+
if (!res.success && !res.errors) {
|
|
5
|
+
return {
|
|
6
|
+
success: false,
|
|
7
|
+
errors: [res.detail[0].msg]
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
if (verbose) {
|
|
11
|
+
return res;
|
|
12
|
+
}
|
|
13
|
+
if (!res.success) {
|
|
14
|
+
return { success: false, errors: res.errors };
|
|
15
|
+
}
|
|
16
|
+
return {
|
|
17
|
+
success: true,
|
|
18
|
+
url: res.url,
|
|
19
|
+
disclosures: (_b = (_a = res == null ? void 0 : res.data) == null ? void 0 : _a.org) == null ? void 0 : _b.disclosures,
|
|
20
|
+
upstream: (_d = (_c = res == null ? void 0 : res.data) == null ? void 0 : _c.upstream) == null ? void 0 : _d.services
|
|
21
|
+
};
|
|
22
|
+
};
|
|
23
|
+
async function check(domain, options) {
|
|
24
|
+
if (typeof domain !== "string") {
|
|
25
|
+
throw new Error("Invalid domain. Domain must be a string.");
|
|
26
|
+
}
|
|
27
|
+
const agentId = options == null ? void 0 : options.userAgentIdentifier;
|
|
28
|
+
const verbose = (options == null ? void 0 : options.verbose) || false;
|
|
29
|
+
const req = await fetch(
|
|
30
|
+
"https://carbon-txt-api.greenweb.org/api/validate/domain/",
|
|
31
|
+
{
|
|
32
|
+
headers: getApiRequestHeaders(agentId),
|
|
33
|
+
method: "POST",
|
|
34
|
+
body: JSON.stringify({ domain })
|
|
35
|
+
}
|
|
36
|
+
);
|
|
37
|
+
const res = await req.json();
|
|
38
|
+
return processResponse(res, verbose);
|
|
39
|
+
}
|
|
40
|
+
var carbon_txt_default = check;
|
|
41
|
+
export {
|
|
42
|
+
check,
|
|
43
|
+
carbon_txt_default as default
|
|
44
|
+
};
|
|
@@ -164,7 +164,7 @@ function parseVisitTraceOptions(options = {}, version = 3, green = false) {
|
|
|
164
164
|
return adjustments;
|
|
165
165
|
}
|
|
166
166
|
function getApiRequestHeaders(comment = "") {
|
|
167
|
-
return { "User-Agent": "co2js/".concat("0.
|
|
167
|
+
return { "User-Agent": "co2js/".concat("0.18.1-0", " ").concat(comment) };
|
|
168
168
|
}
|
|
169
169
|
function outputRating(co2e, swdmVersion) {
|
|
170
170
|
let {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tgwf/co2",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.18.1-0",
|
|
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",
|
|
@@ -23,6 +23,10 @@
|
|
|
23
23
|
"./hosting": {
|
|
24
24
|
"import": "./dist/esm/hosting.js",
|
|
25
25
|
"require": "./dist/cjs/hosting-node.js"
|
|
26
|
+
},
|
|
27
|
+
"./carbon-txt": {
|
|
28
|
+
"import": "./dist/esm/carbon-txt.js",
|
|
29
|
+
"require": "./dist/cjs/carbon-txt.js"
|
|
26
30
|
}
|
|
27
31
|
},
|
|
28
32
|
"scripts": {
|
|
@@ -62,14 +66,14 @@
|
|
|
62
66
|
"esbuild": "^0.27.3",
|
|
63
67
|
"esbuild-jest": "^0.5.0",
|
|
64
68
|
"esbuild-plugin-glob": "^1.1.2",
|
|
65
|
-
"eslint": "^
|
|
66
|
-
"eslint-config-prettier": "^
|
|
67
|
-
"eslint-plugin-jest": "^
|
|
68
|
-
"eslint-plugin-prettier": "^
|
|
69
|
+
"eslint": "^8.15.0",
|
|
70
|
+
"eslint-config-prettier": "^8.5.0",
|
|
71
|
+
"eslint-plugin-jest": "^26.1.5",
|
|
72
|
+
"eslint-plugin-prettier": "^4.0.0",
|
|
69
73
|
"jest": "^30.2.0",
|
|
70
74
|
"nock": "^14.0.10",
|
|
71
75
|
"np": "^11.0.2",
|
|
72
|
-
"prettier": "^
|
|
76
|
+
"prettier": "^2.6.2"
|
|
73
77
|
},
|
|
74
78
|
"jest": {
|
|
75
79
|
"transform": {
|