< Summary - Kestrun — Combined Coverage

Information
Class: Public.Middleware.Add-KrFileServerMiddleware
Assembly: Kestrun.PowerShell.Public
File(s): /home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Middleware/Add-KrFileServerMiddleware.ps1
Tag: Kestrun/Kestrun@6135d944f8787fb570e4dfbacac6e80312799a86
Line coverage
0%
Covered lines: 0
Uncovered lines: 48
Coverable lines: 48
Total lines: 204
Line coverage: 0%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100 12/01/2025 - 20:55:19 Line coverage: 0% (0/50) Total lines: 211 Tag: Kestrun/Kestrun@638a27c2dd54103f693f023b6ba5f56a884caafa01/08/2026 - 08:19:25 Line coverage: 0% (0/50) Total lines: 210 Tag: Kestrun/Kestrun@6ab94ca7560634c2ac58b36c2b98e2a9b1bf305d05/09/2026 - 21:51:36 Line coverage: 0% (0/48) Total lines: 204 Tag: Kestrun/Kestrun@6b24c7512a1bad61723a28d32446de0aa658293e

Coverage delta

Coverage delta 1 -1

Metrics

File(s)

/home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Middleware/Add-KrFileServerMiddleware.ps1

#LineLine coverage
 1<#
 2.SYNOPSIS
 3    Registers a file server to serve static files from a specified path.
 4.DESCRIPTION
 5    This cmdlet allows you to serve static files from a specified path using the Kestrun server.
 6    It can be used to serve files like images, stylesheets, and scripts.
 7.PARAMETER Options
 8    The FileServerOptions to configure the file server.
 9.PARAMETER RootPath
 10    The root path from which to serve files.
 11.PARAMETER RequestPath
 12    The path at which the file server will be registered.
 13.PARAMETER HttpsCompression
 14    The HTTPS compression mode to use for the static files.
 15.PARAMETER ServeUnknownFileTypes
 16    If specified, allows serving files with unknown MIME types.
 17.PARAMETER DefaultContentType
 18    The default content type to use for files served by the static file service.
 19.PARAMETER EnableDirectoryBrowsing
 20    If specified, enables directory browsing for the file server.
 21.PARAMETER RedirectToAppendTrailingSlash
 22    If specified, requests to the path will be redirected to append a trailing slash.
 23.PARAMETER ContentTypeMap
 24    A hashtable mapping file extensions to MIME types (e.g., @{ ".yaml"="application/x-yaml"; ".yml"="application/x-yaml
 25    This allows for serving files with the correct `Content-Type` header.
 26.PARAMETER NoCache
 27    If specified, adds a 'no-cache' directive to the Cache-Control header.
 28.PARAMETER NoStore
 29    If specified, adds a 'no-store' directive to the Cache-Control header.
 30.PARAMETER MaxAge
 31    If specified, sets the 'max-age' directive in seconds for the Cache-Control header.
 32.PARAMETER SharedMaxAge
 33    If specified, sets the 's-maxage' directive in seconds for the Cache-Control header
 34    (used by shared caches).
 35.PARAMETER MaxStale
 36    If specified, adds a 'max-stale' directive to the Cache-Control header.
 37.PARAMETER MaxStaleLimit
 38    If specified, sets the limit in seconds for the 'max-stale' directive in the Cache-Control header.
 39.PARAMETER MinFresh
 40    If specified, sets the 'min-fresh' directive in seconds for the Cache-Control header.
 41.PARAMETER NoTransform
 42    If specified, adds a 'no-transform' directive to the Cache-Control header.
 43.PARAMETER OnlyIfCached
 44    If specified, adds an 'only-if-cached' directive to the Cache-Control header.
 45.PARAMETER Public
 46    If specified, adds a 'public' directive to the Cache-Control header.
 47.PARAMETER Private
 48    If specified, adds a 'private' directive to the Cache-Control header.
 49.PARAMETER MustRevalidate
 50    If specified, adds a 'must-revalidate' directive to the Cache-Control header.
 51.PARAMETER ProxyRevalidate
 52    If specified, adds a 'proxy-revalidate' directive to the Cache-Control header.
 53.EXAMPLE
 54    Add-KrFileServerMiddleware -RequestPath '/files' -EnableDirectoryBrowsing
 55    This example adds a file server to the server for the path '/files', enabling directory browsing.
 56    The file server will use the default options for serving static files.
 57.EXAMPLE
 58    Add-KrFileServerMiddleware -Options $options
 59    This example adds a file server to the server using the specified FileServerOptions.
 60.LINK
 61    https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.builder.fileserveroptions?view=aspnetcore-8.0
 62#>
 63function Add-KrFileServerMiddleware {
 64    [KestrunRuntimeApi('Definition')]
 65    [CmdletBinding(defaultParameterSetName = 'Items')]
 66    param(
 67        [Parameter(Mandatory = $true, ParameterSetName = 'Options')]
 68        [Microsoft.AspNetCore.Builder.FileServerOptions]$Options,
 69
 70        [Parameter(ParameterSetName = 'Items')]
 71        [string]$RootPath,
 72
 73        [Parameter(ParameterSetName = 'Items')]
 74        [string]$RequestPath,
 75
 76        [Parameter(ParameterSetName = 'Items')]
 77        [Microsoft.AspNetCore.Http.Features.HttpsCompressionMode]$HttpsCompression,
 78
 79        [Parameter(ParameterSetName = 'Items')]
 80        [switch]$ServeUnknownFileTypes,
 81
 82        [Parameter(ParameterSetName = 'Items')]
 83        [string]$DefaultContentType,
 84
 85        [Parameter(ParameterSetName = 'Items')]
 86        [switch]$EnableDirectoryBrowsing,
 87
 88        [Parameter(ParameterSetName = 'Items')]
 89        [switch]$RedirectToAppendTrailingSlash,
 90
 91        [Parameter(ParameterSetName = 'Items')]
 92        [hashtable]$ContentTypeMap,
 93
 94        [Parameter()]
 95        [switch]$NoCache,
 96
 97        [Parameter()]
 98        [switch]$NoStore,
 99
 100        [Parameter()]
 101        [ValidateRange(1, [int]::MaxValue)]
 102        [int]$MaxAge,
 103
 104        [Parameter()]
 105        [ValidateRange(1, [int]::MaxValue)]
 106        [int]$SharedMaxAge,
 107
 108        [Parameter()]
 109        [switch]$MaxStale,
 110
 111        [Parameter()]
 112        [ValidateRange(1, [int]::MaxValue)]
 113        [int]$MaxStaleLimit,
 114
 115        [Parameter()]
 116        [ValidateRange(1, [int]::MaxValue)]
 117        [int]$MinFresh,
 118
 119        [Parameter()]
 120        [switch]$NoTransform,
 121
 122        [Parameter()]
 123        [switch]$OnlyIfCached,
 124
 125        [Parameter()]
 126        [switch]$Public,
 127
 128        [Parameter()]
 129        [switch]$Private,
 130
 131        [Parameter()]
 132        [switch]$MustRevalidate,
 133
 134        [Parameter()]
 135        [switch]$ProxyRevalidate
 136    )
 137    # Ensure the server instance is resolved
 0138    $Server = Resolve-KestrunServer
 0139    if ($PSCmdlet.ParameterSetName -eq 'Items') {
 0140        $Options = [Microsoft.AspNetCore.Builder.FileServerOptions]::new()
 141
 0142        if (-not [string]::IsNullOrEmpty($RequestPath)) {
 0143            $Options.RequestPath = [Microsoft.AspNetCore.Http.PathString]::new($RequestPath.TrimEnd('/'))
 144        }
 0145        if (-not [string]::IsNullOrEmpty($RootPath)) {
 0146            $resolvedPath = Resolve-KrPath $RootPath -KestrunRoot
 0147            $Options.FileProvider = [Microsoft.Extensions.FileProviders.PhysicalFileProvider]::new($resolvedPath)
 148        }
 0149        if ($EnableDirectoryBrowsing.IsPresent) {
 0150            $Options.EnableDirectoryBrowsing = $true
 151        }
 0152        if ($ServeUnknownFileTypes.IsPresent) {
 0153            $Options.StaticFileOptions.ServeUnknownFileTypes = $true
 154        }
 0155        if ($PSBoundParameters.ContainsKey('HttpsCompression')) {
 0156            $Options.StaticFileOptions.HttpsCompression = $HttpsCompression
 157        }
 0158        if (-not [string]::IsNullOrEmpty($DefaultContentType)) {
 0159            $Options.StaticFileOptions.DefaultContentType = $DefaultContentType
 160        }
 0161        if ($RedirectToAppendTrailingSlash.IsPresent) {
 0162            $Options.RedirectToAppendTrailingSlash = $true
 163        }
 0164        if ($ContentTypeMap) {
 0165            $provider = [Microsoft.AspNetCore.StaticFiles.FileExtensionContentTypeProvider]::new()
 0166            foreach ($k in $ContentTypeMap.Keys) {
 0167                $ext = if ($k -like '.*') { $k } else { ".$k" }
 0168                $mime = [string]$ContentTypeMap[$k]
 0169                if ([string]::IsNullOrWhiteSpace($mime)) { continue }
 0170                $provider.Mappings[$ext] = $mime
 171            }
 0172            $Options.StaticFileOptions.ContentTypeProvider = $provider
 173        }
 174    }
 175
 0176    if ($PSBoundParameters.ContainsKey('NoCache') -or ($PSBoundParameters.ContainsKey('NoStore')) -or ($PSBoundParameter
 0177        ($PSBoundParameters.ContainsKey('SharedMaxAge')) -or ($PSBoundParameters.ContainsKey('MaxStale')) -or
 0178        ($PSBoundParameters.ContainsKey('MaxStaleLimit')) -or ($PSBoundParameters.ContainsKey('MinFresh')) -or
 0179        ($PSBoundParameters.ContainsKey('NoTransform')) -or ($PSBoundParameters.ContainsKey('OnlyIfCached')) -or
 0180        ($PSBoundParameters.ContainsKey('Public')) -or ($PSBoundParameters.ContainsKey('Private')) -or
 0181        ($PSBoundParameters.ContainsKey('MustRevalidate')) -or ($PSBoundParameters.ContainsKey('ProxyRevalidate'))
 182    ) {
 0183        $cacheControl = [Microsoft.Net.Http.Headers.CacheControlHeaderValue]::new();
 0184        if ($PSBoundParameters.ContainsKey('NoCache')) { $cacheControl.NoCache = $NoCache.IsPresent }
 0185        if ($PSBoundParameters.ContainsKey('NoStore')) { $cacheControl.NoStore = $NoStore.IsPresent }
 0186        if ($PSBoundParameters.ContainsKey('MaxAge')) { $cacheControl.MaxAge = [TimeSpan]::FromSeconds($MaxAge) }
 0187        if ($PSBoundParameters.ContainsKey('SharedMaxAge')) { $cacheControl.SharedMaxAge = [TimeSpan]::FromSeconds($Shar
 0188        if ($PSBoundParameters.ContainsKey('MaxStale')) { $cacheControl.MaxStale = $MaxStale.IsPresent }
 0189        if ($PSBoundParameters.ContainsKey('MaxStaleLimit')) { $cacheControl.MaxStaleLimit = [TimeSpan]::FromSeconds($Ma
 0190        if ($PSBoundParameters.ContainsKey('MinFresh')) { $cacheControl.MinFresh = [TimeSpan]::FromSeconds($MinFresh) }
 0191        if ($PSBoundParameters.ContainsKey('NoTransform')) { $cacheControl.NoTransform = $NoTransform.IsPresent }
 0192        if ($PSBoundParameters.ContainsKey('OnlyIfCached')) { $cacheControl.OnlyIfCached = $OnlyIfCached.IsPresent }
 0193        if ($PSBoundParameters.ContainsKey('Public')) { $cacheControl.Public = $Public.IsPresent }
 0194        if ($PSBoundParameters.ContainsKey('Private')) { $cacheControl.Private = $Private.IsPresent }
 0195        if ($PSBoundParameters.ContainsKey('MustRevalidate')) { $cacheControl.MustRevalidate = $MustRevalidate.IsPresent
 0196        if ($PSBoundParameters.ContainsKey('ProxyRevalidate')) { $cacheControl.ProxyRevalidate = $ProxyRevalidate.IsPres
 197        # Add the file server to the server with caching options
 0198        $Server.AddFileServer( $Options, $cacheControl) | Out-Null
 199    } else {
 200        # Add the file server to the server without caching options
 0201        $Server.AddFileServer( $Options) | Out-Null
 202    }
 203}
 204

Methods/Properties

Add-KrFileServerMiddleware()