2024-05-06 22:51:32 +02:00
|
|
|
import dns from 'dns/promises';
|
|
|
|
import middleware from './_common/middleware.js';
|
2023-06-29 01:27:09 +02:00
|
|
|
|
2024-05-06 22:51:32 +02:00
|
|
|
const txtRecordHandler = 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
|
|
|
|
2024-05-06 22:51:32 +02:00
|
|
|
export const handler = middleware(txtRecordHandler);
|
|
|
|
export default handler;
|