mirror of
https://github.com/Lissy93/web-check.git
synced 2025-07-22 14:24:00 +02:00
.github
api
_common
archives.js
block-lists.js
carbon.js
cookies.js
dns-server.js
dns.js
dnssec.js
features.js
firewall.js
get-ip.js
headers.js
hsts.js
http-security.js
legacy-rank.js
linked-pages.js
mail-config.js
ports.js
quality.js
rank.js
redirects.js
robots-txt.js
screenshot.js
security-txt.js
sitemap.js
social-tags.js
ssl.js
status.js
tech-stack.js
threats.js
tls.js
trace-route.js
txt-records.js
whois.js
public
src
.env
.gitignore
Dockerfile
LICENSE
astro.config.mjs
docker-compose.yml
fly.toml
netlify.toml
package.json
server.js
svelte.config.js
tsconfig.json
vercel.json
vite.config.js
yarn.lock
53 lines
1.4 KiB
JavaScript
53 lines
1.4 KiB
JavaScript
import https from 'https';
|
|
import middleware from './_common/middleware.js';
|
|
|
|
const carbonHandler = async (url) => {
|
|
|
|
// First, get the size of the website's HTML
|
|
const getHtmlSize = (url) => new Promise((resolve, reject) => {
|
|
https.get(url, res => {
|
|
let data = '';
|
|
res.on('data', chunk => {
|
|
data += chunk;
|
|
});
|
|
res.on('end', () => {
|
|
const sizeInBytes = Buffer.byteLength(data, 'utf8');
|
|
resolve(sizeInBytes);
|
|
});
|
|
}).on('error', reject);
|
|
});
|
|
|
|
try {
|
|
const sizeInBytes = await getHtmlSize(url);
|
|
const apiUrl = `https://api.websitecarbon.com/data?bytes=${sizeInBytes}&green=0`;
|
|
|
|
// Then use that size to get the carbon data
|
|
const carbonData = await new Promise((resolve, reject) => {
|
|
https.get(apiUrl, res => {
|
|
let data = '';
|
|
res.on('data', chunk => {
|
|
data += chunk;
|
|
});
|
|
res.on('end', () => {
|
|
resolve(JSON.parse(data));
|
|
});
|
|
}).on('error', reject);
|
|
});
|
|
|
|
if (!carbonData.statistics || (carbonData.statistics.adjustedBytes === 0 && carbonData.statistics.energy === 0)) {
|
|
return {
|
|
statusCode: 200,
|
|
body: JSON.stringify({ skipped: 'Not enough info to get carbon data' }),
|
|
};
|
|
}
|
|
|
|
carbonData.scanUrl = url;
|
|
return carbonData;
|
|
} catch (error) {
|
|
throw new Error(`Error: ${error.message}`);
|
|
}
|
|
};
|
|
|
|
export const handler = middleware(carbonHandler);
|
|
export default handler;
|