Skip to content

Implement symmetric RFC 6570 expansion and pattern matching #418

@dahlia

Description

@dahlia

Implement our own RFC 6570 URI Template expansion and pattern matching to ensure symmetric behavior and eliminate dependency on third-party libraries with asymmetric implementations.

Background

Currently, Fedify relies on:

This leads to issues like #416 where expansion and parsing don't round-trip correctly, causing:

  • Inconsistent encoding/decoding behavior
  • Double-encoding problems in collections
  • Unpredictable results for users

The asymmetric behavior is documented in uri-template-router issue #11.

Problem with current approach

The fundamental issue is that RFC 6570 only defines expansion behavior, not pattern matching. uri-template-router implements pattern matching as an extension, but this creates asymmetry:

Value → url-template (expand) → URL → uri-template-router (parse) → Different value

Proposed solution

Implement a custom RFC 6570 library that provides:

Core features

  • Expansion: Full RFC 6570 compliance for all expression types
  • Symmetric pattern matching: Custom implementation that guarantees round-trip consistency
  • Type safety: Better TypeScript integration than current libraries

Expression types to support

  • {var}: Simple string expansion
  • {+var}: Reserved string expansion
  • {/var}: Path segment expansion
  • {?var}: Query parameter expansion
  • {&var}: Query continuation expansion
  • {#var}: Fragment expansion

Benefits

  • Guaranteed symmetry: expand(parse(url)) === url and parse(expand(value)) === value
  • Predictable behavior: Eliminates current inconsistencies
  • Reduced dependencies: Remove url-template and uri-template-router
  • Fedify-optimized: Can add ActivityPub-specific optimizations if needed

Implementation plan

Phase 1: RFC 6570 expansion

  • Implement core expansion logic
  • Support all expression types
  • Comprehensive test suite against RFC 6570 examples
  • Performance benchmarking vs url-template

Phase 2: Symmetric pattern matching

  • Design pattern matching algorithm that mirrors expansion
  • Implement parsing for all expression types
  • Ensure round-trip consistency tests pass
  • Handle edge cases and malformed inputs

Phase 3: Integration

  • Replace url-template usage in Fedify
  • Replace uri-template-router usage in Fedify
  • Update tests and ensure no regressions
  • Performance validation

Phase 4: Documentation

Considerations

  • Scope: This is a significant undertaking that essentially creates a new library
  • Timeline: Should be treated as a long-term goal, not urgent
  • Compatibility: Must maintain backward compatibility with existing Fedify APIs
  • Testing: Requires extensive testing against RFC 6570 spec and real-world usage

Alternative approaches

  • Contribute fixes to uri-template-router (but maintainer may not accept due to breaking changes)
  • Find alternative libraries with symmetric behavior (research needed)
  • Live with current limitations and document workarounds

Metadata

Metadata

Assignees

Type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions