Skip to main content
This release is 23 versions behind 1.4.9 — the latest version of @fedify/fedify. Jump to latest

Built and signed on GitHub Actions

An ActivityPub/fediverse server framework

This package works with Node.js, Deno, Bun
This package works with Node.js
This package works with Deno
This package works with Bun
JSR Score
94%
Published
3 months ago (1.3.3)
Package root>federation>collection.ts
import { encodeHex } from "jsr:/@std/encoding@^1.0.5/hex"; /** * A page of items. */ export interface PageItems<TItem> { prevCursor?: string | null; nextCursor?: string | null; items: TItem[]; } /** * Calculates the [partial follower collection digest][1]. * * [1]: https://codeberg.org/fediverse/fep/src/branch/main/fep/8fcf/fep-8fcf.md#partial-follower-collection-digest * @param uris The URIs to calculate the digest. Duplicate URIs are ignored. * @returns The digest. */ export async function digest( uris: Iterable<string | URL>, ): Promise<Uint8Array> { const processed = new Set<string>(); const encoder = new TextEncoder(); const result = new Uint8Array(32); for (const uri of uris) { const u = uri instanceof URL ? uri.href : uri; if (processed.has(u)) continue; processed.add(u); const encoded = encoder.encode(u); const digest = new Uint8Array( await crypto.subtle.digest("SHA-256", encoded), ); for (let i = 0; i < 32; i++) { result[i] ^= digest[i]; } } return result; } /** * Builds [`Collection-Synchronization`][1] header content. * * [1]: https://codeberg.org/fediverse/fep/src/branch/main/fep/8fcf/fep-8fcf.md#the-collection-synchronization-http-header * * @param collectionId The sender's followers collection URI. * @param actorIds The actor URIs to digest. * @returns The header content. */ export async function buildCollectionSynchronizationHeader( collectionId: string | URL, actorIds: Iterable<string | URL>, ): Promise<string> { const [anyActorId] = actorIds; const baseUrl = new URL(anyActorId); const url = new URL(collectionId); url.searchParams.set("base-url", `${baseUrl.origin}/`); const hash = encodeHex(await digest(actorIds)); return `collectionId="${collectionId}", url="${url}", digest="${hash}"`; }