Return plain JSON, to be handled by middleware instead

This commit is contained in:
Alicia Sykes 2023-09-03 16:58:46 +01:00
parent c169a3762d
commit 8a7b024e99
6 changed files with 13 additions and 51 deletions

View File

@ -97,10 +97,7 @@ const checkDomainAgainstDnsServers = async (domain) => {
const handler = middleware(async (url) => { const handler = middleware(async (url) => {
const domain = new URL(url).hostname; const domain = new URL(url).hostname;
const results = await checkDomainAgainstDnsServers(domain); const results = await checkDomainAgainstDnsServers(domain);
return { return { blocklists: results };
statusCode: 200,
body: JSON.stringify({ blocklists: results })
};
}); });
module.exports = middleware(handler); module.exports = middleware(handler);

View File

@ -8,11 +8,8 @@ const handler = async (url, event, context) => {
body: JSON.stringify({ error: message }), body: JSON.stringify({ error: message }),
}; };
}; };
const hstsIncompatible = (message, statusCode = 200) => { const hstsIncompatible = (message, compatible = false, hstsHeader = null ) => {
return { return { message, compatible, hstsHeader };
statusCode: statusCode,
body: JSON.stringify({ message, compatible: false }),
};
}; };
@ -35,14 +32,7 @@ const handler = async (url, event, context) => {
} else if (!preload) { } else if (!preload) {
resolve(hstsIncompatible(`HSTS header does not contain the preload directive.`)); resolve(hstsIncompatible(`HSTS header does not contain the preload directive.`));
} else { } else {
resolve({ resolve(hstsIncompatible(`Site is compatible with the HSTS preload list!`, true, hstsHeader));
statusCode: 200,
body: JSON.stringify({
message: "Site is compatible with the HSTS preload list!",
compatible: true,
hstsHeader: hstsHeader,
}),
});
} }
} }
}); });

View File

@ -73,17 +73,11 @@ const handler = async (url, event, context) => {
return errorResponse('The function timed out before completing.'); return errorResponse('The function timed out before completing.');
} }
return { return { openPorts, failedPorts };
statusCode: 200,
body: JSON.stringify({ openPorts, failedPorts }),
};
}; };
const errorResponse = (message, statusCode = 444) => { const errorResponse = (message, statusCode = 444) => {
return { return { error: message };
statusCode: statusCode,
body: JSON.stringify({ error: message }),
};
}; };
module.exports = middleware(handler); module.exports = middleware(handler);

View File

@ -10,7 +10,7 @@ const handler = async (url) => {
{ auth: { username: process.env.TRANCO_USERNAME, password: process.env.TRANCO_API_KEY } } { auth: { username: process.env.TRANCO_USERNAME, password: process.env.TRANCO_API_KEY } }
: {}; : {};
const response = await axios.get( const response = await axios.get(
`https://tranco-list.eu/api/ranks/domain/${domain}`, { timeout: 2000 }, auth, `https://tranco-list.eu/api/ranks/domain/${domain}`, { timeout: 5000 }, auth,
); );
if (!response.data || !response.data.ranks || response.data.ranks.length === 0) { if (!response.data || !response.data.ranks || response.data.ranks.length === 0) {
return { skipped: `Skipping, as ${domain} isn't ranked in the top 100 million sites yet.`}; return { skipped: `Skipping, as ${domain} isn't ranked in the top 100 million sites yet.`};

View File

@ -25,10 +25,7 @@ const handler = async (url) => {
} }
if (!sitemapUrl) { if (!sitemapUrl) {
return { return { skipped: 'No sitemap found' };
statusCode: 404,
body: JSON.stringify({ skipped: 'No sitemap found' }),
};
} }
sitemapRes = await axios.get(sitemapUrl, { timeout: 5000 }); sitemapRes = await axios.get(sitemapUrl, { timeout: 5000 });
@ -40,23 +37,14 @@ const handler = async (url) => {
const parser = new xml2js.Parser(); const parser = new xml2js.Parser();
const sitemap = await parser.parseStringPromise(sitemapRes.data); const sitemap = await parser.parseStringPromise(sitemapRes.data);
return { return sitemap;
statusCode: 200,
body: JSON.stringify(sitemap),
};
} catch (error) { } catch (error) {
// If error occurs // If error occurs
console.log(error.message); console.log(error.message);
if (error.code === 'ECONNABORTED') { if (error.code === 'ECONNABORTED') {
return { return { error: 'Request timed out' };
statusCode: 500,
body: JSON.stringify({ error: 'Request timed out' }),
};
} else { } else {
return { return { error: error.message };
statusCode: 500,
body: JSON.stringify({ error: error.message }),
};
} }
} }
}; };

View File

@ -50,16 +50,9 @@ const handler = async (url) => {
}; };
if (Object.keys(metadata).length === 0) { if (Object.keys(metadata).length === 0) {
return { return { skipped: 'No metadata found' };
statusCode: 200,
body: JSON.stringify({ skipped: 'No metadata found' }),
};
} }
return metadata;
return {
statusCode: 200,
body: JSON.stringify(metadata),
};
} catch (error) { } catch (error) {
return { return {
statusCode: 500, statusCode: 500,