Files
ccn/Packages/Infrastructure/Sources/Public/Middlewares/CompressionMiddleware.swift
T
javierandClaude Opus 5.5 911424de24 Project updates from Template
This commit contains the latest updates from the generic Website template, which move the toolchain to Swift 6.4, gate analytics and structured data behind traits, and rework page routing:

- Updated the Swift toolchain for all the package dependencies and the Website service to version 6.4. (345b305)
- Adapted all the existing middlewares in the Infrastructure package to Swift 6.4. (26008df)
- Moved test resource teardown in the Persistence and the Website library into defer blocks. (27984a9)
- Gated analytics behind a trait and reworked structured data in the Infrastructure package. (c7d4769)
- Enabled the Analytics trait in the Website service. (f6a6bbd)
- Reworked page routing in the Website service. (d1cb2c1)
- Gated the structured data features in the Infrastructure package behind a trait. (f0a8791)
- Enabled the StructuredData trait of the Infrastructure package in the Website service. (bbfdeb3)
- Fixed the asset pin and a build warning in the Dockerfile of the Website service. (6ed0f9b)

The root README change in 345b305 was left out, as this project no longer carries that file. From d1cb2c1, the RootController rework was left out, as the prototype serves a session-aware landing page and routes its other pages through their own controllers; the view-event reporter was appended to the prototype's shared script instead.

The prototype code was adapted to the stricter Swift 6.4 settings: `String(format:)` was swapped for safe formatting, the NoIndex middleware takes a `@concurrent` next handler, and the tests flagged by warnings-as-errors were tightened.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-25 11:55:43 +01:00

71 lines
2.4 KiB
Swift

import HTTPTypes
import Hummingbird
import HummingbirdCompression
import Logging
/// Compresses the responses that are not already encoded, and passes the ones that are through untouched.
///
/// Hummingbird's `ResponseCompressionMiddleware` appends to `Content-Encoding` without checking for one, so a pre-compressed page (see
/// ``CachedHTMLResponse``) would go out as `gzip, gzip` — a client decodes once and renders the inner gzip stream. This stands in for that
/// middleware and delegates to it, so the threshold, the negotiation and the compressor stay its behaviour.
///
/// - Note: `Context` is the request context the middleware is resolved against.
public struct CompressionMiddleware<Context: RequestContext>: Sendable {
// MARK: Properties
/// The middleware the unencoded responses are handed to.
private let compression: ResponseCompressionMiddleware<Context>
// MARK: Initializers
/// Creates a compression middleware.
/// - Parameter minimumResponseSizeToCompress: the smallest response body, in bytes, that is compressed at all.
public init(
minimumResponseSizeToCompress: Int
) {
self.compression = .init(
minimumResponseSizeToCompress: minimumResponseSizeToCompress
)
}
}
// MARK: - RouterMiddleware
extension CompressionMiddleware: RouterMiddleware {
// MARK: Functions
/// Passes the request down the chain and compresses the response on the way back up, unless it already names an encoding.
/// - Parameters:
/// - request: the incoming request.
/// - context: the context the request is resolved against.
/// - next: the next responder in the middleware chain.
/// - Returns: the downstream response, compressed when it was not already.
/// - Throws: any error thrown downstream.
public func handle(
_ request: Request,
context: Context,
next: @concurrent (Request, Context) async throws -> Response
) async throws -> Response {
let response = try await next(
request,
context
)
guard response.headers[.contentEncoding] == nil else {
return response
}
// The response is already in hand, so the delegate gets it rather than the chain — `next` runs once.
return try await compression.handle(
request,
context: context
) { _, _ in
response
}
}
}