diff options
| author | Raphael Kabo <raphaelkabo@hey.com> | 2024-02-02 12:14:38 +0000 | 
|---|---|---|
| committer | Raphael Kabo <raphaelkabo@hey.com> | 2024-02-02 12:14:38 +0000 | 
| commit | 7bd4eb728d27636321e9ac6dadd764ef5fa95af1 (patch) | |
| tree | 4db3ca79cc85f3022918096cd5d443d4531f0759 /src/lib | |
| parent | fbd2dd29739b76e76855a3d4c87d9d43da2953c2 (diff) | |
refactor: send and accept only spec-compliant AP headers
Diffstat (limited to 'src/lib')
| -rw-r--r-- | src/lib/activitypub.ts | 23 | 
1 files changed, 18 insertions, 5 deletions
diff --git a/src/lib/activitypub.ts b/src/lib/activitypub.ts index 0a3db7b..11f0770 100644 --- a/src/lib/activitypub.ts +++ b/src/lib/activitypub.ts @@ -1,9 +1,22 @@ -import { Request } from "express"; +import { Request, Response } from "express"; +// From https://www.w3.org/TR/activitypub/#client-to-server-interactions: +// "Servers MAY interpret a Content-Type or Accept header of application/activity+json +// as equivalent to application/ld+json; profile="https://www.w3.org/ns/activitystreams" +// for client-to-server interactions. +// For best compatibility, we always send application/ld+json; profile="https://www.w3.org/ns/activitystreams" +// and accept both application/ld+json; profile="https://www.w3.org/ns/activitystreams" and application/activity+json. +export const activityPubContentType = +    'application/ld+json; profile="https://www.w3.org/ns/activitystreams"'; +export const alternateActivityPubContentType = "application/activity+json"; + +// Cf. https://www.w3.org/TR/activitypub/#retrieving-objects  export const acceptsActivityPub = (req: Request) => { -    return ( -        req.headers.accept && -        (req.headers.accept.includes("application/activity+json") || -            req.headers.accept.includes("application/ld+json")) +    const validAcceptHeaders = [ +        activityPubContentType, +        alternateActivityPubContentType, +    ]; +    return validAcceptHeaders.some( +        (header) => req.headers.accept?.includes(header),      );  };  | 
