UNPKG

@fedify/fedify

Version:

An ActivityPub server framework

96 lines (95 loc) 3.71 kB
import { Temporal } from "@js-temporal/polyfill"; import "urlpattern-polyfill"; globalThis.addEventListener = () => {}; import { c as getNormalizationContextLoader } from "./ld-tusP_XxG.mjs"; import jsonld from "@fedify/vocab-runtime/jsonld"; //#region src/federation/temporal.ts function isPlainObject(value) { return typeof value === "object" && value != null && !Array.isArray(value); } function normalizeDateTimeLiteral(value) { return value.substring(19).match(/[Z+-]/) ? value : value + "Z"; } function isMalformedDateTimeLiteral(value) { if (typeof value !== "string") return false; try { Temporal.Instant.from(normalizeDateTimeLiteral(value)); return false; } catch { return true; } } function isMalformedDurationLiteral(value) { if (typeof value !== "string") return false; try { Temporal.Duration.from(value); return false; } catch { return true; } } const TEMPORAL_DATE_TIME_IRIS = new Set([ "https://www.w3.org/ns/activitystreams#deleted", "https://www.w3.org/ns/activitystreams#endTime", "https://www.w3.org/ns/activitystreams#published", "https://www.w3.org/ns/activitystreams#startTime", "https://www.w3.org/ns/activitystreams#updated", "http://purl.org/dc/terms/created", "https://w3id.org/security#created" ]); const TEMPORAL_DURATION_IRIS = new Set(["https://www.w3.org/ns/activitystreams#duration"]); const QUESTION_CLOSED_IRI = "https://www.w3.org/ns/activitystreams#closed"; const XSD_DATE_TIME_IRI = "http://www.w3.org/2001/XMLSchema#dateTime"; function hasMalformedExpandedDateTimeLiteral(value) { if (Array.isArray(value)) return value.some(hasMalformedExpandedDateTimeLiteral); return isPlainObject(value) && "@value" in value && isMalformedDateTimeLiteral(value["@value"]); } function hasMalformedExpandedQuestionClosedLiteral(value) { if (Array.isArray(value)) return value.some(hasMalformedExpandedQuestionClosedLiteral); if (!isPlainObject(value) || !("@value" in value)) return false; const literal = value["@value"]; if (typeof literal === "boolean") return false; if (typeof literal !== "string") return false; if (value["@type"] !== XSD_DATE_TIME_IRI) return false; if (new Date(literal).toString() === "Invalid Date") return false; return isMalformedDateTimeLiteral(literal); } function hasMalformedExpandedDurationLiteral(value) { if (Array.isArray(value)) return value.some(hasMalformedExpandedDurationLiteral); return isPlainObject(value) && "@value" in value && isMalformedDurationLiteral(value["@value"]); } function hasMalformedKnownTemporalLiteralInternal(value, visited) { if (Array.isArray(value)) return value.some((item) => hasMalformedKnownTemporalLiteralInternal(item, visited)); if (!isPlainObject(value)) return false; if (visited.has(value)) return false; visited.add(value); if ("@value" in value) return false; for (const [key, child] of Object.entries(value)) { if (TEMPORAL_DATE_TIME_IRIS.has(key)) { if (hasMalformedExpandedDateTimeLiteral(child)) return true; continue; } if (key === QUESTION_CLOSED_IRI) { if (hasMalformedExpandedQuestionClosedLiteral(child)) return true; continue; } if (TEMPORAL_DURATION_IRIS.has(key)) { if (hasMalformedExpandedDurationLiteral(child)) return true; continue; } if (hasMalformedKnownTemporalLiteralInternal(child, visited)) return true; } return false; } async function hasMalformedKnownTemporalLiteral(value, contextLoader) { try { return hasMalformedKnownTemporalLiteralInternal(await jsonld.expand(value, { documentLoader: getNormalizationContextLoader(contextLoader), keepFreeFloatingNodes: true }), /* @__PURE__ */ new Set()); } catch { return false; } } //#endregion export { hasMalformedKnownTemporalLiteral as t };