@tgwf/co2 0.14.0 → 0.14.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.
Files changed (51) hide show
  1. package/CHANGELOG.md +183 -44
  2. package/README.md +70 -16
  3. package/dist/cjs/co2.js +14 -22
  4. package/dist/cjs/co2.js.map +2 -2
  5. package/dist/cjs/data/average-intensities.min.js +1 -1
  6. package/dist/cjs/data/average-intensities.min.js.map +2 -2
  7. package/dist/cjs/helpers/index.js +10 -6
  8. package/dist/cjs/helpers/index.js.map +2 -2
  9. package/dist/cjs/hosting-api.js +12 -7
  10. package/dist/cjs/hosting-api.js.map +2 -2
  11. package/dist/cjs/hosting-node.js +10 -9
  12. package/dist/cjs/hosting-node.js.map +2 -2
  13. package/dist/cjs/hosting.js +2 -2
  14. package/dist/cjs/hosting.js.map +2 -2
  15. package/dist/cjs/sustainable-web-design.js +9 -6
  16. package/dist/cjs/sustainable-web-design.js.map +2 -2
  17. package/dist/esm/co2.js +16 -24
  18. package/dist/esm/data/average-intensities.min.js +1 -1
  19. package/dist/esm/helpers/index.js +10 -6
  20. package/dist/esm/hosting-api.js +12 -7
  21. package/dist/esm/hosting.js +2 -2
  22. package/dist/esm/sustainable-web-design.js +9 -6
  23. package/package.json +11 -7
  24. package/.all-contributorsrc +0 -39
  25. package/.esbuild.browser.js +0 -11
  26. package/.esbuild.esm.js +0 -25
  27. package/.esbuild.node.js +0 -16
  28. package/.eslintignore +0 -1
  29. package/.gitpod.yml +0 -8
  30. package/data/output/average-intensities.js +0 -226
  31. package/data/output/average-intensities.json +0 -1328
  32. package/data/output/marginal-intensities-2021.js +0 -238
  33. package/data/output/marginal-intensities-2021.json +0 -2801
  34. package/dist/iife/index.js +0 -20
  35. package/dist/iife/index.js.map +0 -7
  36. package/fixup +0 -19
  37. package/index.d.ts +0 -2
  38. package/src/1byte.js +0 -77
  39. package/src/co2.js +0 -311
  40. package/src/constants/file-size.js +0 -5
  41. package/src/constants/index.js +0 -39
  42. package/src/data/average-intensities.min.js +0 -1
  43. package/src/data/marginal-intensities-2021.min.js +0 -1
  44. package/src/helpers/index.js +0 -175
  45. package/src/hosting-api.js +0 -64
  46. package/src/hosting-json.node.js +0 -107
  47. package/src/hosting-node.js +0 -113
  48. package/src/hosting.js +0 -16
  49. package/src/index-node.js +0 -5
  50. package/src/index.js +0 -7
  51. package/src/sustainable-web-design.js +0 -329
package/dist/esm/co2.js CHANGED
@@ -17,73 +17,65 @@ class CO2 {
17
17
  throw new Error(`"${options.model}" is not a valid model. Please use "1byte" for the OneByte model, and "swd" for the Sustainable Web Design model.
18
18
  See https://developers.thegreenwebfoundation.org/co2js/models/ to learn more about the models available in CO2.js.`);
19
19
  }
20
- if ((options == null ? void 0 : options.results) === "segment") {
21
- this.model.results = {
22
- segment: true
23
- };
24
- } else {
25
- this.model.results = {
26
- segment: false
27
- };
28
- }
20
+ this._segment = (options == null ? void 0 : options.results) === "segment";
29
21
  }
30
22
  perByte(bytes, green = false) {
31
- return this.model.perByte(bytes, green, this.model.results.segment);
23
+ return this.model.perByte(bytes, green, this._segment);
32
24
  }
33
25
  perVisit(bytes, green = false) {
34
26
  var _a;
35
27
  if ((_a = this.model) == null ? void 0 : _a.perVisit) {
36
- return this.model.perVisit(bytes, green, this.model.results.segment);
28
+ return this.model.perVisit(bytes, green, this._segment);
37
29
  } else {
38
30
  throw new Error(`The perVisit() method is not supported in the model you are using. Try using perByte() instead.
39
31
  See https://developers.thegreenwebfoundation.org/co2js/methods/ to learn more about the methods available in CO2.js.`);
40
32
  }
41
33
  }
42
34
  perByteTrace(bytes, green = false, options = {}) {
43
- var _a, _b, _c, _d, _e, _f;
35
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i;
44
36
  let adjustments = {};
45
37
  if (options) {
46
38
  adjustments = parseOptions(options);
47
39
  }
48
40
  return {
49
- co2: this.model.perByte(bytes, green, this.model.results.segment, adjustments),
41
+ co2: this.model.perByte(bytes, green, this._segment, adjustments),
50
42
  green,
51
43
  variables: {
52
44
  description: "Below are the variables used to calculate this CO2 estimate.",
53
45
  bytes,
54
46
  gridIntensity: {
55
47
  description: "The grid intensity (grams per kilowatt-hour) used to calculate this CO2 estimate.",
56
- network: ((_b = (_a = adjustments == null ? void 0 : adjustments.gridIntensity) == null ? void 0 : _a.network) == null ? void 0 : _b.value) || GLOBAL_GRID_INTENSITY,
57
- dataCenter: green ? RENEWABLES_GRID_INTENSITY : ((_d = (_c = adjustments == null ? void 0 : adjustments.gridIntensity) == null ? void 0 : _c.dataCenter) == null ? void 0 : _d.value) || GLOBAL_GRID_INTENSITY,
48
+ network: (_c = (_b = (_a = adjustments == null ? void 0 : adjustments.gridIntensity) == null ? void 0 : _a.network) == null ? void 0 : _b.value) != null ? _c : GLOBAL_GRID_INTENSITY,
49
+ dataCenter: green ? RENEWABLES_GRID_INTENSITY : (_f = (_e = (_d = adjustments == null ? void 0 : adjustments.gridIntensity) == null ? void 0 : _d.dataCenter) == null ? void 0 : _e.value) != null ? _f : GLOBAL_GRID_INTENSITY,
58
50
  production: GLOBAL_GRID_INTENSITY,
59
- device: ((_f = (_e = adjustments == null ? void 0 : adjustments.gridIntensity) == null ? void 0 : _e.device) == null ? void 0 : _f.value) || GLOBAL_GRID_INTENSITY
51
+ device: (_i = (_h = (_g = adjustments == null ? void 0 : adjustments.gridIntensity) == null ? void 0 : _g.device) == null ? void 0 : _h.value) != null ? _i : GLOBAL_GRID_INTENSITY
60
52
  }
61
53
  }
62
54
  };
63
55
  }
64
56
  perVisitTrace(bytes, green = false, options = {}) {
65
- var _a, _b, _c, _d, _e, _f, _g;
57
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m;
66
58
  if ((_a = this.model) == null ? void 0 : _a.perVisit) {
67
59
  let adjustments = {};
68
60
  if (options) {
69
61
  adjustments = parseOptions(options);
70
62
  }
71
63
  return {
72
- co2: this.model.perVisit(bytes, green, this.model.results.segment, adjustments),
64
+ co2: this.model.perVisit(bytes, green, this._segment, adjustments),
73
65
  green,
74
66
  variables: {
75
67
  description: "Below are the variables used to calculate this CO2 estimate.",
76
68
  bytes,
77
69
  gridIntensity: {
78
70
  description: "The grid intensity (grams per kilowatt-hour) used to calculate this CO2 estimate.",
79
- network: ((_c = (_b = adjustments == null ? void 0 : adjustments.gridIntensity) == null ? void 0 : _b.network) == null ? void 0 : _c.value) || GLOBAL_GRID_INTENSITY,
80
- dataCenter: green ? RENEWABLES_GRID_INTENSITY : ((_e = (_d = adjustments == null ? void 0 : adjustments.gridIntensity) == null ? void 0 : _d.dataCenter) == null ? void 0 : _e.value) || GLOBAL_GRID_INTENSITY,
71
+ network: (_d = (_c = (_b = adjustments == null ? void 0 : adjustments.gridIntensity) == null ? void 0 : _b.network) == null ? void 0 : _c.value) != null ? _d : GLOBAL_GRID_INTENSITY,
72
+ dataCenter: green ? RENEWABLES_GRID_INTENSITY : (_g = (_f = (_e = adjustments == null ? void 0 : adjustments.gridIntensity) == null ? void 0 : _e.dataCenter) == null ? void 0 : _f.value) != null ? _g : GLOBAL_GRID_INTENSITY,
81
73
  production: GLOBAL_GRID_INTENSITY,
82
- device: ((_g = (_f = adjustments == null ? void 0 : adjustments.gridIntensity) == null ? void 0 : _f.device) == null ? void 0 : _g.value) || GLOBAL_GRID_INTENSITY
74
+ device: (_j = (_i = (_h = adjustments == null ? void 0 : adjustments.gridIntensity) == null ? void 0 : _h.device) == null ? void 0 : _i.value) != null ? _j : GLOBAL_GRID_INTENSITY
83
75
  },
84
- dataReloadRatio: (adjustments == null ? void 0 : adjustments.dataReloadRatio) || 0.02,
85
- firstVisitPercentage: (adjustments == null ? void 0 : adjustments.firstVisitPercentage) || 0.75,
86
- returnVisitPercentage: (adjustments == null ? void 0 : adjustments.returnVisitPercentage) || 0.25
76
+ dataReloadRatio: (_k = adjustments == null ? void 0 : adjustments.dataReloadRatio) != null ? _k : 0.02,
77
+ firstVisitPercentage: (_l = adjustments == null ? void 0 : adjustments.firstVisitPercentage) != null ? _l : 0.75,
78
+ returnVisitPercentage: (_m = adjustments == null ? void 0 : adjustments.returnVisitPercentage) != null ? _m : 0.25
87
79
  }
88
80
  };
89
81
  } else {
@@ -1,4 +1,4 @@
1
- const data = { "AFG": 120.482, "AFRICA": 491.69, "ALB": 23.438, "DZA": 498.904, "ASM": 687.5, "AGO": 195.983, "ATG": 657.143, "ARG": 338.275, "ARM": 222.678, "ABW": 591.398, "ASIA": 532.12, "AUS": 503.179, "AUT": 157.52, "AZE": 532.902, "BHS": 698.113, "BHR": 489.77, "BGD": 577.027, "BRB": 644.86, "BLR": 415.641, "BEL": 165.363, "BLZ": 484.375, "BEN": 666.667, "BTN": 24.444, "BOL": 323.869, "BIH": 517.241, "BWA": 794.521, "BRA": 101.986, "BRN": 658.849, "BGR": 399.565, "BFA": 611.429, "BDI": 250, "CPV": 600, "KHM": 400.46, "CMR": 278.261, "CAN": 127.816, "CYM": 684.932, "CAF": 0, "TCD": 677.419, "CHL": 332.612, "CHN": 531.151, "COL": 168.954, "COM": 714.286, "COG": 395.522, "COD": 25.362, "COK": 400, "CRI": 37.213, "CIV": 410.747, "HRV": 246.459, "CUB": 602.025, "CYP": 589.354, "CZE": 415.345, "DNK": 180.709, "DJI": 666.667, "DMA": 529.412, "DOM": 550.881, "ECU": 188.708, "EGY": 485.367, "SLV": 194.226, "GNQ": 492.958, "ERI": 688.889, "EST": 464.029, "SWZ": 189.189, "ETH": 25.187, "EU": 277.37, "EUROPE": 298.02, "FLK": 500, "FRO": 428.571, "FJI": 289.474, "FIN": 130.977, "FRA": 84.817, "GUF": 254.717, "PYF": 471.429, "G20": 440.49, "G7": 341.12, "GAB": 397.38, "GMB": 700, "GEO": 134.831, "DEU": 385.467, "GHA": 361.204, "GRC": 343.822, "GRL": 133.333, "GRD": 714.286, "GLP": 611.765, "GUM": 670.33, "GTM": 320.637, "GIN": 208.633, "GNB": 750, "GUY": 642.276, "HTI": 606.061, "HND": 375.104, "HKG": 683.972, "HUN": 222.973, "ISL": 28.557, "IND": 632.406, "IDN": 618.996, "IRN": 493.595, "IRQ": 498.869, "IRL": 345.347, "ISR": 548.03, "ITA": 371.692, "JAM": 519.54, "JPN": 482.87, "JOR": 399.909, "KAZ": 635.574, "KEN": 100.568, "KIR": 666.667, "XKX": 768.733, "KWT": 489.61, "KGZ": 104.426, "LAO": 242.182, "LATIN AMERICA AND CARIBBEAN": 240.38, "LVA": 181.818, "LBN": 594.752, "LSO": 20, "LBR": 304.348, "LBY": 550.421, "LTU": 194.245, "LUX": 168.142, "MAC": 491.525, "MDG": 483.254, "MWI": 133.803, "MYS": 543.737, "MDV": 651.515, "MLI": 466.077, "MLT": 495, "MTQ": 698.63, "MRT": 526.596, "MUS": 611.111, "MEX": 421.402, "MIDDLE EAST": 515.47, "MDA": 678.439, "MNG": 749.656, "MNE": 399.381, "MSR": 1e3, "MAR": 610.412, "MOZ": 126.633, "MMR": 344.365, "NAM": 63.694, "NRU": 750, "NPL": 24.51, "NLD": 354.988, "NCL": 610.119, "NZL": 97.732, "NIC": 354.212, "NER": 622.222, "NGA": 368.224, "NORTH AMERICA": 336.26, "PRK": 157.785, "MKD": 529.328, "NOR": 28.818, "OCEANIA": 446.55, "OECD": 338.65, "OMN": 488.272, "PAK": 344.155, "PSE": 465.116, "PAN": 192.136, "PNG": 526.749, "PRY": 25.487, "PER": 256.512, "POL": 634.579, "PRT": 234.029, "PRI": 681.469, "QAT": 489.76, "REU": 519.031, "ROU": 264.121, "RUS": 367.189, "RWA": 294.118, "KNA": 681.818, "LCA": 685.714, "SPM": 800, "VCT": 500, "WSM": 470.588, "STP": 600, "SAU": 571.336, "SEN": 523.132, "SRB": 569.375, "SYC": 615.385, "SLE": 47.619, "SGP": 488.775, "SVK": 140.666, "SVN": 237.378, "SLB": 727.273, "SOM": 634.146, "ZAF": 708.989, "KOR": 435.689, "SSD": 684.211, "ESP": 217.373, "LKA": 462.997, "SDN": 264.014, "SUR": 356.436, "SWE": 45.084, "CHE": 45.622, "SYR": 541.169, "TWN": 561, "TJK": 83.629, "TZA": 366.748, "THA": 502.305, "PHL": 582.047, "TGO": 460.317, "TON": 625, "TTO": 491.409, "TUN": 469.428, "TUR": 413.628, "TKM": 490.187, "TCA": 703.704, "UGA": 52.273, "UKR": 206.539, "ARE": 461.845, "GBR": 257.379, "USA": 367.259, "URY": 114.81, "UZB": 505.405, "VUT": 571.429, "VEN": 212.476, "VNM": 376.828, "VGB": 714.286, "VIR": 685.714, "WORLD": 436.33, "YEM": 543.909, "ZMB": 84.698, "ZWE": 392.279 };
1
+ const data = { "AFG": 120.48, "AFRICA": 484.7, "ALB": 23.44, "DZA": 485.49, "ASM": 687.5, "AGO": 195.98, "ATG": 657.14, "ARG": 344.31, "ARM": 222.68, "ABW": 591.4, "ASEAN": 508.2, "ASIA": 534.89, "AUS": 501.7, "AUT": 158.22, "AZE": 469.58, "BHS": 698.11, "BHR": 494.02, "BGD": 574.28, "BRB": 644.86, "BLR": 425.9, "BEL": 167.11, "BLZ": 484.38, "BEN": 666.67, "BTN": 24.44, "BOL": 335.4, "BIH": 553.47, "BWA": 794.52, "BRA": 102.04, "BRN": 493.59, "BGR": 399.72, "BFA": 611.43, "BDI": 250, "CPV": 600, "KHM": 400.46, "CMR": 278.26, "CAN": 125.84, "CYM": 684.93, "CAF": 0, "TCD": 677.42, "CHL": 332.61, "CHN": 533.98, "COL": 163.99, "COM": 714.29, "COG": 395.52, "COD": 25.36, "COK": 400, "CRI": 37.21, "CIV": 410.75, "HRV": 246.29, "CUB": 654.68, "CYP": 589.35, "CZE": 414.8, "DNK": 180.42, "DJI": 666.67, "DMA": 529.41, "DOM": 549.8, "ECU": 183.63, "EGY": 469.63, "SLV": 194.23, "GNQ": 492.96, "ERI": 688.89, "EST": 460.26, "SWZ": 189.19, "ETH": 25.19, "EU": 276.63, "EUROPE": 297.05, "FLK": 500, "FRO": 428.57, "FJI": 289.47, "FIN": 131.71, "FRA": 84.88, "GUF": 254.72, "PYF": 471.43, "G20": 442.57, "G7": 344.31, "GAB": 397.38, "GMB": 700, "GEO": 134.83, "DEU": 385.39, "GHA": 361.2, "GRC": 344.41, "GRL": 133.33, "GRD": 714.29, "GLP": 623.53, "GUM": 670.33, "GTM": 304.71, "GIN": 208.63, "GNB": 750, "GUY": 642.28, "HTI": 606.06, "HND": 373.96, "HKG": 609.93, "HUN": 222.1, "ISL": 28.56, "IND": 633.4, "IDN": 619.03, "IRN": 487.86, "IRQ": 531.36, "IRL": 346.43, "ISR": 537.57, "ITA": 372.63, "JAM": 537.93, "JPN": 494.86, "JOR": 391.13, "KAZ": 635.57, "KEN": 101.13, "KIR": 666.67, "XKX": 767, "KWT": 574.56, "KGZ": 104.43, "LAO": 242.18, "LATIN AMERICA AND CARIBBEAN": 237.91, "LVA": 183.43, "LBN": 663.1, "LSO": 20, "LBR": 304.35, "LBY": 558.85, "LTU": 195.7, "LUX": 162.6, "MAC": 491.53, "MDG": 483.25, "MWI": 133.8, "MYS": 543.87, "MDV": 651.52, "MLI": 463.13, "MLT": 433.48, "MTQ": 698.63, "MRT": 526.6, "MUS": 611.11, "MEX": 423.81, "MIDDLE EAST": 519.92, "MDA": 666.67, "MNG": 749.66, "MNE": 392.75, "MSR": 1e3, "MAR": 630.75, "MOZ": 126.63, "MMR": 330.8, "NAM": 63.69, "NRU": 750, "NPL": 24.51, "NLD": 354.31, "NCL": 610.12, "NZL": 105.22, "NIC": 354.21, "NER": 622.22, "NGA": 368.11, "NORTH AMERICA": 336.68, "PRK": 102.42, "MKD": 543.71, "NOR": 28.93, "OCEANIA": 450.73, "OECD": 341.08, "OMN": 488.27, "PAK": 344.16, "PSE": 465.12, "PAN": 152.68, "PNG": 526.75, "PRY": 25.49, "PER": 256.51, "POL": 633.23, "PRT": 234.61, "PRI": 612.39, "QAT": 490.28, "REU": 519.03, "ROU": 264.24, "RUS": 363.68, "RWA": 294.12, "KNA": 681.82, "LCA": 685.71, "SPM": 800, "VCT": 500, "WSM": 470.59, "STP": 600, "SAU": 557.78, "SEN": 523.13, "SRB": 582.13, "SYC": 615.39, "SLE": 47.62, "SGP": 488.78, "SVK": 140.14, "SVN": 237.38, "SLB": 727.27, "SOM": 634.15, "ZAF": 708.21, "KOR": 437.6, "SSD": 684.21, "ESP": 217.42, "LKA": 501.53, "SDN": 288.13, "SUR": 356.44, "SWE": 45.12, "CHE": 41.28, "SYR": 541.17, "TWN": 560.98, "TJK": 83.63, "TZA": 366.75, "THA": 501.57, "PHL": 594.45, "TGO": 460.32, "TON": 625, "TTO": 491.41, "TUN": 469.43, "TUR": 413.6, "TKM": 490.19, "TCA": 703.7, "UGA": 52.27, "UKR": 232.74, "ARE": 407.98, "GBR": 261.16, "USA": 368.1, "URY": 150.13, "UZB": 505.41, "VUT": 571.43, "VEN": 212.48, "VNM": 386.49, "VGB": 714.29, "VIR": 685.71, "WORLD": 437.66, "YEM": 559.66, "ZMB": 84.7, "ZWE": 392.28 };
2
2
  const type = "average";
3
3
  var average_intensities_min_default = { data, type };
4
4
  export {
@@ -15,7 +15,7 @@ function parseOptions(options) {
15
15
  if (options == null ? void 0 : options.gridIntensity) {
16
16
  adjustments.gridIntensity = {};
17
17
  const { device, dataCenter, network } = options.gridIntensity;
18
- if (device) {
18
+ if (device || device === 0) {
19
19
  if (typeof device === "object") {
20
20
  if (!averageIntensity.data[(_a = device.country) == null ? void 0 : _a.toUpperCase()]) {
21
21
  console.warn(`"${device.country}" is not a valid country. Please use a valid 3 digit ISO 3166 country code.
@@ -41,7 +41,7 @@ Falling back to global average grid intensity.`);
41
41
  Falling back to global average grid intensity.`);
42
42
  }
43
43
  }
44
- if (dataCenter) {
44
+ if (dataCenter || dataCenter === 0) {
45
45
  if (typeof dataCenter === "object") {
46
46
  if (!averageIntensity.data[(_c = dataCenter.country) == null ? void 0 : _c.toUpperCase()]) {
47
47
  console.warn(`"${dataCenter.country}" is not a valid country. Please use a valid 3 digit ISO 3166 country code.
@@ -67,7 +67,7 @@ Falling back to global average grid intensity.`);
67
67
  Falling back to global average grid intensity.`);
68
68
  }
69
69
  }
70
- if (network) {
70
+ if (network || network === 0) {
71
71
  if (typeof network === "object") {
72
72
  if (!averageIntensity.data[(_e = network.country) == null ? void 0 : _e.toUpperCase()]) {
73
73
  console.warn(`"${network.country}" is not a valid country. Please use a valid 3 digit ISO 3166 country code.
@@ -94,7 +94,7 @@ Falling back to global average grid intensity.`);
94
94
  }
95
95
  }
96
96
  }
97
- if (options == null ? void 0 : options.dataReloadRatio) {
97
+ if ((options == null ? void 0 : options.dataReloadRatio) || options.dataReloadRatio === 0) {
98
98
  if (typeof options.dataReloadRatio === "number") {
99
99
  if (options.dataReloadRatio >= 0 && options.dataReloadRatio <= 1) {
100
100
  adjustments.dataReloadRatio = options.dataReloadRatio;
@@ -109,7 +109,7 @@ Falling back to default value.`);
109
109
  Falling back to default value.`);
110
110
  }
111
111
  }
112
- if (options == null ? void 0 : options.firstVisitPercentage) {
112
+ if ((options == null ? void 0 : options.firstVisitPercentage) || options.firstVisitPercentage === 0) {
113
113
  if (typeof options.firstVisitPercentage === "number") {
114
114
  if (options.firstVisitPercentage >= 0 && options.firstVisitPercentage <= 1) {
115
115
  adjustments.firstVisitPercentage = options.firstVisitPercentage;
@@ -124,7 +124,7 @@ Falling back to default value.`);
124
124
  Falling back to default value.`);
125
125
  }
126
126
  }
127
- if (options == null ? void 0 : options.returnVisitPercentage) {
127
+ if ((options == null ? void 0 : options.returnVisitPercentage) || options.returnVisitPercentage === 0) {
128
128
  if (typeof options.returnVisitPercentage === "number") {
129
129
  if (options.returnVisitPercentage >= 0 && options.returnVisitPercentage <= 1) {
130
130
  adjustments.returnVisitPercentage = options.returnVisitPercentage;
@@ -141,7 +141,11 @@ Falling back to default value.`);
141
141
  }
142
142
  return adjustments;
143
143
  }
144
+ function getApiRequestHeaders(comment = "") {
145
+ return { "User-Agent": `co2js/${"0.14.2"} ${comment}` };
146
+ }
144
147
  export {
145
148
  formatNumber,
149
+ getApiRequestHeaders,
146
150
  parseOptions
147
151
  };
@@ -1,21 +1,26 @@
1
1
  "use strict";
2
- function check(domain) {
2
+ import { getApiRequestHeaders } from "./helpers/index.js";
3
+ function check(domain, userAgentIdentifier) {
3
4
  if (typeof domain === "string") {
4
- return checkAgainstAPI(domain);
5
+ return checkAgainstAPI(domain, userAgentIdentifier);
5
6
  } else {
6
- return checkDomainsAgainstAPI(domain);
7
+ return checkDomainsAgainstAPI(domain, userAgentIdentifier);
7
8
  }
8
9
  }
9
- async function checkAgainstAPI(domain) {
10
- const req = await fetch(`https://api.thegreenwebfoundation.org/greencheck/${domain}`);
10
+ async function checkAgainstAPI(domain, userAgentIdentifier) {
11
+ const req = await fetch(`https://api.thegreenwebfoundation.org/greencheck/${domain}`, {
12
+ headers: getApiRequestHeaders(userAgentIdentifier)
13
+ });
11
14
  const res = await req.json();
12
15
  return res.green;
13
16
  }
14
- async function checkDomainsAgainstAPI(domains) {
17
+ async function checkDomainsAgainstAPI(domains, userAgentIdentifier) {
15
18
  try {
16
19
  const apiPath = "https://api.thegreenwebfoundation.org/v2/greencheckmulti";
17
20
  const domainsString = JSON.stringify(domains);
18
- const req = await fetch(`${apiPath}/${domainsString}`);
21
+ const req = await fetch(`${apiPath}/${domainsString}`, {
22
+ headers: getApiRequestHeaders(userAgentIdentifier)
23
+ });
19
24
  const allGreenCheckResults = await req.json();
20
25
  return greenDomainsFromResults(allGreenCheckResults);
21
26
  } catch (e) {
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  import hostingAPI from "./hosting-api.js";
3
- function check(domain) {
4
- return hostingAPI.check(domain);
3
+ function check(domain, userAgentIdentifier) {
4
+ return hostingAPI.check(domain, userAgentIdentifier);
5
5
  }
6
6
  var hosting_default = {
7
7
  check
@@ -53,13 +53,13 @@ class SustainableWebDesign {
53
53
  let globalEmissions = GLOBAL_GRID_INTENSITY;
54
54
  if (options == null ? void 0 : options.gridIntensity) {
55
55
  const { device, network, dataCenter } = options.gridIntensity;
56
- if (device == null ? void 0 : device.value) {
56
+ if ((device == null ? void 0 : device.value) || (device == null ? void 0 : device.value) === 0) {
57
57
  deviceCarbonIntensity = device.value;
58
58
  }
59
- if (network == null ? void 0 : network.value) {
59
+ if ((network == null ? void 0 : network.value) || (network == null ? void 0 : network.value) === 0) {
60
60
  networkCarbonIntensity = network.value;
61
61
  }
62
- if (dataCenter == null ? void 0 : dataCenter.value) {
62
+ if ((dataCenter == null ? void 0 : dataCenter.value) || (dataCenter == null ? void 0 : dataCenter.value) === 0) {
63
63
  dataCenterCarbonIntensity = dataCenter.value;
64
64
  }
65
65
  }
@@ -81,6 +81,9 @@ class SustainableWebDesign {
81
81
  return returnCO2ByComponent;
82
82
  }
83
83
  perByte(bytes, carbonIntensity = false, segmentResults = false, options = {}) {
84
+ if (bytes < 1) {
85
+ bytes = 0;
86
+ }
84
87
  const energyBycomponent = this.energyPerByteByComponent(bytes, options);
85
88
  if (typeof carbonIntensity !== "boolean") {
86
89
  throw new Error(`perByte expects a boolean for the carbon intensity value. Received: ${carbonIntensity}`);
@@ -112,13 +115,13 @@ class SustainableWebDesign {
112
115
  return energyValues.reduce((prevValue, currentValue) => prevValue + currentValue);
113
116
  }
114
117
  energyPerVisitByComponent(bytes, options = {}, firstView = FIRST_TIME_VIEWING_PERCENTAGE, returnView = RETURNING_VISITOR_PERCENTAGE, dataReloadRatio = PERCENTAGE_OF_DATA_LOADED_ON_SUBSEQUENT_LOAD) {
115
- if (options.dataReloadRatio) {
118
+ if (options.dataReloadRatio || options.dataReloadRatio === 0) {
116
119
  dataReloadRatio = options.dataReloadRatio;
117
120
  }
118
- if (options.firstVisitPercentage) {
121
+ if (options.firstVisitPercentage || options.firstVisitPercentage === 0) {
119
122
  firstView = options.firstVisitPercentage;
120
123
  }
121
- if (options.returnVisitPercentage) {
124
+ if (options.returnVisitPercentage || options.returnVisitPercentage === 0) {
122
125
  returnView = options.returnVisitPercentage;
123
126
  }
124
127
  const energyBycomponent = this.energyPerByteByComponent(bytes);
package/package.json CHANGED
@@ -1,9 +1,12 @@
1
1
  {
2
2
  "name": "@tgwf/co2",
3
- "version": "0.14.0",
3
+ "version": "0.14.2",
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",
7
+ "engines": {
8
+ "node": ">=14.0.0"
9
+ },
7
10
  "exports": {
8
11
  ".": {
9
12
  "import": "./dist/esm/index.js",
@@ -26,7 +29,8 @@
26
29
  "intensity-data:average": "node data/functions/generate_average_co2.js",
27
30
  "intensity-data:marginal": "node data/functions/generate_marginal_co2.js",
28
31
  "intensity-data": "npm run intensity-data:average && npm run intensity-data:marginal && npm run format-data",
29
- "format-data": "cd data && prettier --write '**/*.{js,json}'"
32
+ "format-data": "cd data && prettier --write '**/*.{js,json}'",
33
+ "version": "npm run build"
30
34
  },
31
35
  "keywords": [
32
36
  "sustainability",
@@ -41,16 +45,17 @@
41
45
  "license": "Apache-2.0",
42
46
  "devDependencies": {
43
47
  "@tgwf/url2green": "^0.4.0",
48
+ "all-contributors-cli": "^6.26.1",
49
+ "esbuild": "^0.14.47",
50
+ "esbuild-jest": "^0.5.0",
51
+ "esbuild-plugin-glob": "^1.1.2",
44
52
  "eslint": "^8.15.0",
45
53
  "eslint-config-prettier": "^8.5.0",
46
54
  "eslint-plugin-jest": "^26.1.5",
47
55
  "eslint-plugin-prettier": "^4.0.0",
48
- "esbuild": "^0.14.47",
49
- "esbuild-jest": "^0.5.0",
50
- "esbuild-plugin-glob": "^1.1.2",
51
56
  "jest": "^28.1.0",
52
57
  "nock": "^13.2.4",
53
- "np": "^7.7.0",
58
+ "np": "^8.0.4",
54
59
  "pagexray": "^4.4.2",
55
60
  "prettier": "^2.6.2"
56
61
  },
@@ -67,7 +72,6 @@
67
72
  "publishConfig": {
68
73
  "access": "public"
69
74
  },
70
- "dependencies": {},
71
75
  "repository": {
72
76
  "type": "git",
73
77
  "url": "https://github.com/thegreenwebfoundation/co2.js.git"
@@ -1,39 +0,0 @@
1
- {
2
- "files": [
3
- "README.md"
4
- ],
5
- "imageSize": 100,
6
- "commit": false,
7
- "contributors": [
8
- {
9
- "login": "JamieBeevor",
10
- "name": "JamieB",
11
- "avatar_url": "https://avatars1.githubusercontent.com/u/26165947?v=4",
12
- "profile": "https://greengumption.co.uk",
13
- "contributions": [
14
- "ideas",
15
- "question",
16
- "review"
17
- ]
18
- },
19
- {
20
- "login": "soulgalore",
21
- "name": "Peter Hedenskog",
22
- "avatar_url": "https://avatars2.githubusercontent.com/u/540757?v=4",
23
- "profile": "https://www.peterhedenskog.com",
24
- "contributions": [
25
- "code",
26
- "ideas",
27
- "review",
28
- "infra",
29
- "docs"
30
- ]
31
- }
32
- ],
33
- "contributorsPerLine": 7,
34
- "projectName": "co2.js",
35
- "projectOwner": "thegreenwebfoundation",
36
- "repoType": "github",
37
- "repoHost": "https://github.com",
38
- "skipCi": true
39
- }
@@ -1,11 +0,0 @@
1
- require('esbuild').buildSync({
2
- entryPoints: ['src/index.js'],
3
- outdir: 'dist/iife',
4
- globalName: 'co2',
5
- format: 'iife',
6
- platform: 'browser',
7
- bundle: true,
8
- sourcemap: true,
9
- minify: true,
10
- // target: ['es5'],
11
- })
package/.esbuild.esm.js DELETED
@@ -1,25 +0,0 @@
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/**/!(*.test.js|test-constants.js|!(*.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 DELETED
@@ -1,16 +0,0 @@
1
- const { globPlugin } = require('esbuild-plugin-glob');
2
-
3
- function main() {
4
- require('esbuild').build({
5
- entryPoints: ['src/**/!(*.test.js|test-constants.js|!(*.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/.eslintignore DELETED
@@ -1 +0,0 @@
1
- *.min.js
package/.gitpod.yml DELETED
@@ -1,8 +0,0 @@
1
- # Commands to start on workspace startup
2
- tasks:
3
- - init: npm install
4
- command: npm run gitpod
5
- # Ports to expose on workspace startup
6
- ports:
7
- - port: 8000
8
- onOpen: open-preview