Skip to content

feat: HTTP trailers support infrastructure #429

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

mcollina
Copy link
Member

@mcollina mcollina commented Jul 24, 2025

Summary

Implements comprehensive HTTP trailers support for @fastify/reply-from, enabling automatic forwarding of HTTP trailers from upstream servers to clients across all supported HTTP protocols.

Key Features

🏗️ Core Infrastructure

  • HTTP/1.1, HTTP/2, and Undici client trailer collection
  • RFC 7230/9110 compliant trailer filtering
  • Built-in security protections and size limits

⚙️ Configuration Options

  • forwardTrailers - Enable/disable trailer forwarding (default: true)
  • stripForbiddenTrailers - Remove forbidden trailer fields (default: true)
  • requireTrailerSupport - Only forward when client advertises support (default: false)
  • trailersTimeout - Timeout for trailer collection (default: 5s)
  • maxTrailerSize - Size limit for trailer data (default: 8KB)

🎯 Request/Response Hooks

  • rewriteTrailers(trailers, request) - Transform upstream trailers before forwarding
  • onTrailers(request, reply, trailers) - Custom trailer handling logic
  • addTrailers - Add custom trailers (static values or async functions)

Implementation Details

Automatic Trailer Forwarding: Trailers from upstream servers are automatically collected and forwarded to clients while respecting RFC specifications and security requirements.

Security & Performance:

  • Filters forbidden trailer fields per RFC 7230/9110
  • Implements size limits to prevent memory exhaustion
  • Timeout protection for trailer collection
  • Comprehensive error handling and logging

Multi-Protocol Support: Works seamlessly across HTTP/1.1, HTTP/2, and Undici clients with protocol-specific optimizations.

Usage Examples

// Basic trailer forwarding
proxy.register(require('@fastify/reply-from'), {
  base: 'http://upstream-server',
  forwardTrailers: true
})

// Advanced trailer manipulation
proxy.get('/', (request, reply) => {
  reply.from('/', {
    rewriteTrailers: (trailers, request) => ({
      ...trailers,
      'x-proxy-timing': Date.now() - request.startTime
    }),
    addTrailers: {
      'x-proxy-id': 'fastify-reply-from',
      'x-response-time': async () => calculateResponseTime()
    }
  })
})

Test Coverage

  • ✅ Core trailer collection across all HTTP clients
  • ✅ Plugin configuration options validation
  • ✅ Request/response hook functionality
  • ✅ Security filtering and size limits
  • ✅ Timeout protection and error handling
  • ✅ Backward compatibility verification

Backward Compatibility

Fully backward compatible - all trailer functionality is opt-in and defaults to enabled for seamless adoption.

Related

Implements the comprehensive plan outlined in TRAILERS.md

mcollina and others added 3 commits July 25, 2025 23:21
This document outlines the comprehensive plan for implementing HTTP trailers
support in @fastify/reply-from, including:

- Architecture design for trailer collection across HTTP/1.1, HTTP/2, and undici clients
- Plugin integration with new configuration options and hooks
- Security considerations with forbidden trailer filtering
- Comprehensive testing strategy and timeline
- Backward-compatible implementation approach

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
Adds foundation for trailer collection across all HTTP clients:

- HTTP/1.1: Collect trailers on response 'end' event
- HTTP/2: Listen for 'trailers' event on stream
- Undici: Use built-in trailers property from response
- New utility functions for trailer filtering and client capability detection
- Comprehensive unit tests for trailer utilities

All HTTP clients now return getTrailers() accessor function in response object.
Trailer functionality is backward compatible and follows RFC 7230/9110 specifications.

Establishes the foundation for trailer forwarding without breaking existing functionality.
Adds complete trailer forwarding capability with plugin integration:

**Configuration Options:**
- `forwardTrailers` - Enable/disable trailer forwarding (default: true)
- `stripForbiddenTrailers` - Remove RFC-forbidden trailer fields (default: true)
- `requireTrailerSupport` - Only forward if client advertises support (default: false)
- `trailersTimeout` - Timeout for trailer collection (default: 5s)
- `maxTrailerSize` - Size limit for trailer data (default: 8KB)

**Request/Response Hooks:**
- `rewriteTrailers(trailers, request)` - Transform upstream trailers
- `onTrailers(request, reply, trailers)` - Custom trailer handling
- `addTrailers` - Add custom trailers (static values or async functions)

**Features:**
- Automatic trailer forwarding from upstream servers to clients
- RFC 7230/9110 compliant security filtering
- Timeout protection for trailer collection
- Size limits to prevent memory exhaustion
- Comprehensive error handling and logging

**Tests:**
- Integration tests for all trailer forwarding scenarios
- Configuration option validation
- Hook functionality verification
- Size limit and timeout protection tests

Maintains backward compatibility with existing functionality.
@mcollina mcollina force-pushed the feature/http-trailers-support branch from 03dcc3f to 002859e Compare July 25, 2025 21:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant