Renamed the Web package as Infrastructure (#25)

This PR contains the work done to rename the _Web_ package as _Infrastructure_, to provide a clear naming and purpose to this particular package within the project.

To provide further details about the work:

* Infrastructure
  * Asset fingerprinting: an FNV-1a token derived from the static files directory, appended as ?v= to asset URLs so deploys bust caches; pre-rendered pages also revalidate via weak ETags.
  * New middlewares: fixed-window RateLimitMiddleware (per-client budgets keyed by trusted X-Forwarded-For or remote address) and VaryMiddleware (Accept-Encoding on every response); SecurityHeadersMiddleware now also stamps error responses.
  * Auto-generated HEAD endpoints, cache max-age configuration, and Docker build/Compose refinements.
  * Protocols and scaffolding: Asset/AssetExtension, the Page protocol (viewport, stylesheets, scripts, versioned URLs), and LocalizedRequestContext.
  * Rate limiter's counter store swapped from an actor to a Mutex (no executor hop per request) with amortized batch eviction instead of O(n²) scans under client floods.
  * FingerprintAssets reports unreadable files to a logger instead of silently producing a token that never busts their cache.

Reviewed-on: rock-n-code/loud-amsterdam#25
Co-authored-by: Javier Cicchelli <javier@rock-n-code.com>
Co-committed-by: Javier Cicchelli <javier@rock-n-code.com>
This commit is contained in:
2026-07-23 01:04:37 +00:00
committed by javier
parent a868275347
commit cdded06ba3
58 changed files with 2844 additions and 519 deletions
@@ -0,0 +1,79 @@
import Testing
@testable import Infrastructure
@Suite("Asset protocol")
struct AssetTests {
// MARK: Properties
private let image = StubAsset(
fileExtensions: [.png],
fileName: "icon"
)
private let shared = StubAsset(
fileExtensions: [.css, .js],
fileName: "shared"
)
// MARK: Method tests
@Test
func `relative path nests the file inside its extension's sub-directory`() {
#expect(shared.relativePath(for: .css) == "css/shared.css")
#expect(shared.relativePath(for: .js) == "js/shared.js")
}
@Test
func `relative path keeps the file at the root without a sub-directory`() {
#expect(image.relativePath(for: .png) == "icon.png")
}
@Test
func `url path prefixes the relative path with a slash`() {
#expect(shared.urlPath(for: .css) == "/css/shared.css")
#expect(image.urlPath(for: .png) == "/icon.png")
}
@Test
func `url path appends a version token as a query parameter`() {
#expect(shared.urlPath(
for: .css,
version: "0123456789abcdef"
) == "/css/shared.css?v=0123456789abcdef")
}
@Test(arguments: [nil, ""] as [String?])
func `url path without a version`(
version: String?
) {
#expect(shared.urlPath(
for: .css,
version: version
) == "/css/shared.css")
}
@Test(arguments: [
"",
".",
"Resources/Static"
])
func `path relative to`(
_ basePath: String
) {
for fileExtension in shared.fileExtensions {
let pathRelativeToBasePath = shared.path(
relativeTo: basePath,
for: fileExtension
)
let relativePath = shared.relativePath(for: fileExtension)
if basePath.isEmpty {
#expect(pathRelativeToBasePath == relativePath)
} else {
#expect(pathRelativeToBasePath == "\(basePath)/\(relativePath)")
}
}
}
}
@@ -0,0 +1,62 @@
import Elementary
import Foundation
import Testing
@testable import Infrastructure
@Suite("Page protocol", .tags(.page))
struct PageTests {
// MARK: Functional tests
@Test
func `assembles the document around the page's parts`() {
let html = StubPage().render()
#expect(html.contains("<title>Stub Page</title>"))
#expect(html.contains(#"lang="en""#))
#expect(html.contains(#"name="viewport""#))
#expect(html.contains(#"<meta name="stub" content="marker">"#))
#expect(html.contains(#"<link rel="stylesheet" href="/css/stub.css">"#))
#expect(html.contains(#"<script src="/js/stub.js"></script>"#))
#expect(html.contains("Stub content"))
}
@Test
func `places the metadata between the viewport and the stylesheets`() throws {
let html = StubPage().render()
let viewport = try #require(html.range(of: #"name="viewport""#))
let metadata = try #require(html.range(of: #"name="stub""#))
let stylesheet = try #require(html.range(of: "/css/stub.css"))
#expect(viewport.lowerBound < metadata.lowerBound)
#expect(metadata.lowerBound < stylesheet.lowerBound)
}
@Test
func `renders the scripts after the content`() throws {
let html = StubPage().render()
let content = try #require(html.range(of: "Stub content"))
let script = try #require(html.range(of: "/js/stub.js"))
#expect(content.lowerBound < script.lowerBound)
}
@Test
func `appends the version token to the asset URLs`() {
let html = StubPage(assetVersion: "0123456789abcdef").render()
#expect(html.contains("/css/stub.css?v=0123456789abcdef"))
#expect(html.contains("/js/stub.js?v=0123456789abcdef"))
}
@Test
func `derives the document language from the locale`() {
let html = StubPage(locale: .init(identifier: "de-DE")).render()
#expect(html.contains(#"lang="de""#))
}
}