2023-06-29 01:27:09 +02:00
|
|
|
const dns = require('dns').promises;
|
2023-08-09 23:33:36 +02:00
|
|
|
const middleware = require('./_common/middleware');
|
2023-06-29 01:27:09 +02:00
|
|
|
|
2023-08-09 23:33:36 +02:00
|
|
|
const handler = async (url, event, context) => {
|
2023-07-28 22:46:20 +02:00
|
|
|
try {
|
2023-08-09 23:33:36 +02:00
|
|
|
const parsedUrl = new URL(url);
|
|
|
|
|
|
|
|
const txtRecords = await dns.resolveTxt(parsedUrl.hostname);
|
2023-06-29 01:27:09 +02:00
|
|
|
|
|
|
|
// Parsing and formatting TXT records into a single object
|
|
|
|
const readableTxtRecords = txtRecords.reduce((acc, recordArray) => {
|
|
|
|
const recordObject = recordArray.reduce((recordAcc, recordString) => {
|
|
|
|
const splitRecord = recordString.split('=');
|
|
|
|
const key = splitRecord[0];
|
|
|
|
const value = splitRecord.slice(1).join('=');
|
|
|
|
return { ...recordAcc, [key]: value };
|
|
|
|
}, {});
|
|
|
|
return { ...acc, ...recordObject };
|
|
|
|
}, {});
|
|
|
|
|
2023-08-09 23:33:36 +02:00
|
|
|
return readableTxtRecords;
|
|
|
|
|
2023-06-29 01:27:09 +02:00
|
|
|
} catch (error) {
|
2023-08-09 23:33:36 +02:00
|
|
|
if (error.code === 'ERR_INVALID_URL') {
|
|
|
|
throw new Error(`Invalid URL ${error}`);
|
|
|
|
} else {
|
|
|
|
throw error;
|
|
|
|
}
|
2023-06-29 01:27:09 +02:00
|
|
|
}
|
|
|
|
};
|
2023-08-09 23:33:36 +02:00
|
|
|
|
2023-09-03 13:27:04 +02:00
|
|
|
module.exports = middleware(handler);
|
|
|
|
module.exports.handler = middleware(handler);
|