feat: add webfinger ttl cache

This commit is contained in:
Julian Lam
2023-06-16 11:26:25 -04:00
parent 57895b7246
commit 4f5f025d57

View File

@@ -2,6 +2,10 @@
const request = require('request-promise-native'); const request = require('request-promise-native');
const ttl = require('../cache/ttl');
const webfingerCache = ttl({ ttl: 1000 * 60 * 60 * 24 }); // 24 hours
const Helpers = module.exports; const Helpers = module.exports;
Helpers.query = async (id) => { Helpers.query = async (id) => {
@@ -10,6 +14,10 @@ Helpers.query = async (id) => {
return false; return false;
} }
if (webfingerCache.has(id)) {
return webfingerCache.get(id);
}
// Make a webfinger query to retrieve routing information // Make a webfinger query to retrieve routing information
const response = await request(`https://${hostname}/.well-known/webfinger?resource=acct:${id}`, { const response = await request(`https://${hostname}/.well-known/webfinger?resource=acct:${id}`, {
simple: false, simple: false,
@@ -28,5 +36,6 @@ Helpers.query = async (id) => {
({ href: actorUri } = actorUri); ({ href: actorUri } = actorUri);
} }
webfingerCache.set(id, { username, hostname, actorUri });
return { username, hostname, actorUri }; return { username, hostname, actorUri };
}; };