UNPKG

@fedify/fedify

Version:

An ActivityPub server framework

68 lines (67 loc) 2.12 kB
import "@js-temporal/polyfill"; import "urlpattern-polyfill"; globalThis.addEventListener = () => {}; //#region src/federation/negotiation.ts function compareSpecs(a, b) { return b.q - a.q || (b.s ?? 0) - (a.s ?? 0) || (a.o ?? 0) - (b.o ?? 0) || a.i - b.i || 0; } function isQuality(spec) { return spec.q > 0; } const simpleMediaTypeRegExp = /^\s*([^\s\/;]+)\/([^;\s]+)\s*(?:;(.*))?$/; function splitKeyValuePair(str) { const [key, value] = str.split("="); return [key.toLowerCase(), value]; } function parseMediaType(str, i) { const match = simpleMediaTypeRegExp.exec(str); if (!match) return; const [, type, subtype, parameters] = match; if (!type || !subtype) return; const params = Object.create(null); let q = 1; if (parameters) { const kvps = parameters.split(";").map((p) => p.trim()).map(splitKeyValuePair); for (const [key, val] of kvps) { const value = val && val[0] === `"` && val[val.length - 1] === `"` ? val.slice(1, val.length - 1) : val; if (key === "q" && value) { q = parseFloat(value); break; } params[key] = value; } } return { type, subtype, params, i, o: void 0, q, s: void 0 }; } function parseAccept(accept) { const accepts = accept.split(",").map((p) => p.trim()); const mediaTypes = []; for (const [index, accept] of accepts.entries()) { const mediaType = parseMediaType(accept.trim(), index); if (mediaType) mediaTypes.push(mediaType); } return mediaTypes; } function getFullType(spec) { return `${spec.type}/${spec.subtype}`; } function preferredMediaTypes(accept) { return parseAccept(accept === void 0 ? "*/*" : accept ?? "").filter(isQuality).sort(compareSpecs).map(getFullType); } function acceptsJsonLd(request) { const accept = request.headers.get("Accept"); const types = accept ? preferredMediaTypes(accept) : ["*/*"]; if (types == null) return true; if (types[0] === "text/html" || types[0] === "application/xhtml+xml") return false; return types.includes("application/activity+json") || types.includes("application/ld+json") || types.includes("application/json"); } //#endregion export { acceptsJsonLd as t };