fedify
fedify copied to clipboard
refactor(relay): implement factory pattern with Mastodon and LitePub relay support
Summary
This PR refactors the @fedify/relay package to introduce a factory pattern that supports multiple relay implementations, specifically Mastodon and LitePub relay protocols. The changes improve code organization, maintainability, and extensibility for future relay implementations.
Key Changes
- Factory Pattern: Introduced
createRelay()factory function that instantiates relay implementations based on type ("mastodon"or"litepub") - Separate Relay Implementations:
MastodonRelay: Implements Mastodon's relay protocol with immediate follower acceptanceLitePubRelay: Implements LitePub's relay protocol with reciprocal follow pattern (pending → accepted states)
- Refactored Core Module: Extracted common relay infrastructure into
relay.tswith shared:- Actor dispatcher for relay server actor
- Key pair management (RSA + Ed25519)
- Followers/Following collection dispatchers
- Comprehensive Test Coverage: Added dedicated test suites for both implementations:
mastodon.test.ts: 896 lines of tests covering Mastodon relay behaviorlitepub.test.ts: 896 lines of tests covering LitePub relay behavior with reciprocal follows- Removed old monolithic
relay.test.ts
Protocol Differences
Mastodon Relay:
- Immediately accepts Follow activities and adds followers to the active list
- Simpler subscription flow without reciprocal follow requirement
- Suitable for permissive relay scenarios
LitePub Relay:
- Implements reciprocal follow pattern (relay follows back the follower)
- Two-state follower lifecycle:
pending→accepted - Follower is only activated after accepting the relay's Follow activity
- More secure and controlled subscription process
Architecture Improvements
- Modular Design: Each relay type is self-contained with its own file and tests
- Extensibility: Easy to add new relay implementations (e.g., Akkoma, Pleroma variants)
- Shared Infrastructure: Common functionality centralized in
relay.tsandrelayBuilder - Type Safety: Strong TypeScript typing with proper interfaces (
RelayOptions,LitePubFollower)
Migration Guide
Before:
import { MastodonRelay, LitePubRelay } from "@fedify/relay";
const mastodonRelay = new MastodonRelay(options);
const litepubRelay = new LitePubRelay(options);
After:
import { createRelay } from "@fedify/relay";
// For Mastodon-compatible relay
const mastodonRelay = createRelay("mastodon", options);
// For LitePub-compatible relay
const litepubRelay = createRelay("litepub", options);
Files Changed
packages/relay/src/relay.ts: Refactored to shared infrastructure (771 → 161 lines)packages/relay/src/mastodon.ts: New Mastodon relay implementation (191 lines)packages/relay/src/litepub.ts: New LitePub relay implementation (297 lines)packages/relay/src/mastodon.test.ts: New comprehensive test suite (784 lines)packages/relay/src/litepub.test.ts: New comprehensive test suite (896 lines)packages/relay/src/relay.test.ts: Removed old monolithic tests (1128 lines deleted)packages/relay/src/mod.ts: Updated exports to include new relay classes
Test Plan
- [x] All Mastodon relay tests pass (784 lines of test coverage)
- [x] All LitePub relay tests pass (896 lines of test coverage)
- [x] Factory function correctly instantiates both relay types
- [x] Follower management works correctly for both protocols
- [x] Activity forwarding (Create, Update, Delete, Move, Announce) tested for both
- [x] Subscription approval/rejection flows tested
- [x] Reciprocal follow pattern tested for LitePub
- [x] HTTP signature verification works across both implementations
Breaking Changes
This is a breaking change that requires updates to existing relay usage code:
- The
Relayclass constructor is replaced withcreateRelay()factory function - Users must specify relay type:
"mastodon"or"litepub" - Default behavior changes based on selected relay type
Related Commits
- 36b65745: Add relay factory pattern with federation builder
- 03ea8aab: Add create factory function for mastodon and litepub
- 1e7c16f2: Add mastodon relay and its test
- 0e174116: Add litepub relay and its test
- 7902d022: Delete old relay test
🤖 Generated with Claude Code