Something went wrong. Try again.
Large file storage for AT Protocol: an IPFS server & gateway with XRPC upload & admin. atfs.dev
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172// Builds the dev.atfs.server record the instance will read at boot, and// refuses the shapes the daemon would reject once it is too late to fix// them cheaply. Kept apart from the network code so it can be tested// without one.
export const SERVER_NSID = "dev.atfs.server";
const DID_PATTERN = /^did:[a-z]+:[a-zA-Z0-9._:%-]*[a-zA-Z0-9._-]$/;const DOMAIN_PATTERN = /^[a-z0-9]([a-z0-9-]*[a-z0-9])?(\.[a-z0-9]([a-z0-9-]*[a-z0-9])?)+$/;
export function isDid(value) { return typeof value === "string" && DID_PATTERN.test(value);}
/** * Reduces whatever the operator pasted for their public address to the bare * domain a serviceDid can be built from. A URL, a trailing slash or stray * capitals are all things people type; a port or a path are not survivable, * because the lexicon only treats a *bare-domain* did:web as this * instance's address. * * @param {string} input * @returns {string} the bare domain. */export function normaliseHostname(input) { let host = String(input).trim().toLowerCase(); host = host.replace(/^[a-z][a-z0-9+.-]*:\/\//, ""); host = host.replace(/\/+$/, "");
if (host === "") throw new Error("give the address people will use to reach this server"); if (host.includes("/")) throw new Error("just the address, with no path after it"); if (host.includes(":")) throw new Error("just the address, with no port number"); if (!DOMAIN_PATTERN.test(host)) throw new Error(`"${host}" isn't a domain name`);
return host;}
export function serviceDidForHostname(hostname) { return `did:web:${normaliseHostname(hostname)}`;}
/** * @param {object} input * @param {string[]} input.accounts DIDs allowed to upload. * @param {string} [input.serviceDid] * @param {string[]} [input.follows] * @returns {{$type: string, accounts: string[], serviceDid?: string, follows?: string[]}} * the record value, ready to put. */export function buildServerRecord({ accounts, serviceDid, follows = [] }) { const unique = [...new Set(accounts.map((a) => a.trim()).filter(Boolean))]; if (unique.length === 0) { throw new Error("list at least one account, or nobody can upload to this server"); } const notADid = unique.find((account) => !isDid(account)); if (notADid) { throw new Error(`"${notADid}" isn't a DID — accounts are listed by DID, not by handle`); } if (serviceDid !== undefined && !isDid(serviceDid)) { throw new Error(`"${serviceDid}" isn't a DID`); }
const record = { $type: SERVER_NSID, accounts: unique }; if (serviceDid) record.serviceDid = serviceDid; if (follows.length > 0) record.follows = [...new Set(follows)]; return record;}
export function recordUri(ownerDid, peerId) { return `at://${ownerDid}/${SERVER_NSID}/${peerId}`;}