UNPKG

@fedify/fedify

Version:

An ActivityPub server framework

61 lines (60 loc) 2.5 kB
import { format } from "../deps/jsr.io/@std/semver/1.0.4/mod.js"; /** * Converts a {@link NodeInfo} object to a JSON value. * @param nodeInfo The {@link NodeInfo} object to convert. * @returns The JSON value that complies with the NodeInfo schema. * @throws {TypeError} If the {@link NodeInfo} object is invalid. */ export function nodeInfoToJson(nodeInfo) { if (!nodeInfo.software.name.match(/^[a-z0-9-]+$/)) { throw new TypeError("Invalid software name."); } if (nodeInfo.protocols.length < 1) { throw new TypeError("At least one protocol must be supported."); } if (nodeInfo.usage.users.total != null && (nodeInfo.usage.users.total < 0 || !Number.isInteger(nodeInfo.usage.users.total))) { throw new TypeError("Invalid total users."); } if (nodeInfo.usage.users.activeHalfyear != null && (nodeInfo.usage.users.activeHalfyear < 0 || !Number.isInteger(nodeInfo.usage.users.activeHalfyear))) { throw new TypeError("Invalid active halfyear users."); } if (nodeInfo.usage.users.activeMonth != null && (nodeInfo.usage.users.activeMonth < 0 || !Number.isInteger(nodeInfo.usage.users.activeMonth))) { throw new TypeError("Invalid active month users."); } if (nodeInfo.usage.localPosts < 0 || !Number.isInteger(nodeInfo.usage.localPosts)) { throw new TypeError("Invalid local posts."); } if (nodeInfo.usage.localComments < 0 || !Number.isInteger(nodeInfo.usage.localComments)) { throw new TypeError("Invalid local comments."); } return { "$schema": "http://nodeinfo.diaspora.software/ns/schema/2.1#", version: "2.1", software: { name: nodeInfo.software.name, version: format(nodeInfo.software.version), repository: nodeInfo.software.repository?.href, homepage: nodeInfo.software.homepage?.href, }, protocols: nodeInfo.protocols, services: nodeInfo.services == null ? { inbound: [], outbound: [] } : { inbound: nodeInfo.services.inbound ?? [], outbound: nodeInfo.services.outbound ?? [], }, openRegistrations: nodeInfo.openRegistrations ?? false, usage: { users: nodeInfo.usage.users, localPosts: nodeInfo.usage.localPosts, localComments: nodeInfo.usage.localComments, }, metadata: nodeInfo.metadata ?? {}, }; }