mirror of
https://gitlab.com/soapbox-pub/ditto.git
synced 2025-12-06 11:29:46 +00:00
feat: create and implement nodeInfoCache function
This commit is contained in:
parent
e789e08c0f
commit
b3ef9b5e01
1 changed files with 41 additions and 0 deletions
41
src/utils/domains.ts
Normal file
41
src/utils/domains.ts
Normal file
|
|
@ -0,0 +1,41 @@
|
||||||
|
import { fetchWorker } from '@/workers/fetch.ts';
|
||||||
|
import { SimpleLRU } from '@/utils/SimpleLRU.ts';
|
||||||
|
import { Time } from '@/utils/time.ts';
|
||||||
|
|
||||||
|
type NodeInfoV1 = {
|
||||||
|
usage: {
|
||||||
|
users: {
|
||||||
|
total: number;
|
||||||
|
activeMonth: number;
|
||||||
|
activeHalfyear: number;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
type Domain = string;
|
||||||
|
|
||||||
|
export const nodeInfoCache = new SimpleLRU<Domain, NodeInfoV1>(
|
||||||
|
async (key, { signal }) => {
|
||||||
|
try {
|
||||||
|
const url = new URL('/.well-known/nodeinfo', `https://${key}/`);
|
||||||
|
const res = await fetchWorker(url, { signal });
|
||||||
|
|
||||||
|
const { links } = await res.json();
|
||||||
|
if (links.length < 1) throw new Error(`${key} does not have NodeInfo schemas`);
|
||||||
|
|
||||||
|
// NodeInfo is backwards compatible, and we only care about the 'usage' field,
|
||||||
|
// which is present across all versions, so we can make a request to any version
|
||||||
|
const urlNodeInfo = links[0].href;
|
||||||
|
|
||||||
|
const resNodeIndo = await fetchWorker(urlNodeInfo, { signal });
|
||||||
|
const { usage } = await resNodeIndo.json();
|
||||||
|
|
||||||
|
return {
|
||||||
|
usage,
|
||||||
|
};
|
||||||
|
} catch (e) {
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{ max: 500, ttl: Time.hours(1) },
|
||||||
|
);
|
||||||
Loading…
Add table
Reference in a new issue