< Summary - Kestrun — Combined Coverage

Information
Class: Public.Middleware.Add-KrStaticFilesMiddleware
Assembly: Kestrun.PowerShell.Public
File(s): /home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Middleware/Add-KrStaticFilesMiddleware.ps1
Tag: Kestrun/Kestrun@6135d944f8787fb570e4dfbacac6e80312799a86
Line coverage
0%
Covered lines: 0
Uncovered lines: 46
Coverable lines: 46
Total lines: 200
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/48) Total lines: 207 Tag: Kestrun/Kestrun@638a27c2dd54103f693f023b6ba5f56a884caafa01/08/2026 - 08:19:25 Line coverage: 0% (0/48) Total lines: 206 Tag: Kestrun/Kestrun@6ab94ca7560634c2ac58b36c2b98e2a9b1bf305d05/09/2026 - 21:51:36 Line coverage: 0% (0/46) Total lines: 200 Tag: Kestrun/Kestrun@6b24c7512a1bad61723a28d32446de0aa658293e

Coverage delta

Coverage delta 1 -1

Metrics

File(s)

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

#LineLine coverage
 1<#
 2    .SYNOPSIS
 3        Registers a static file server to serve 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 StaticFileOptions to configure the static file service.
 9    .PARAMETER RootPath
 10        The root path from which to serve static files.
 11    .PARAMETER RequestPath
 12        The path at which the static file service 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 RedirectToAppendTrailingSlash
 20        If specified, redirects requests to append a trailing slash to the URL.
 21    .PARAMETER ContentTypeMap
 22        A hashtable mapping file extensions to MIME types.
 23    .PARAMETER NoCache
 24        If specified, adds a 'no-cache' directive to the Cache-Control header.
 25    .PARAMETER NoStore
 26        If specified, adds a 'no-store' directive to the Cache-Control header.
 27    .PARAMETER MaxAge
 28        If specified, sets the 'max-age' directive in seconds for the Cache-Control header.
 29    .PARAMETER SharedMaxAge
 30        If specified, sets the 's-maxage' directive in seconds for the Cache-Control header
 31        (used by shared caches).
 32    .PARAMETER MaxStale
 33        If specified, adds a 'max-stale' directive to the Cache-Control header.
 34    .PARAMETER MaxStaleLimit
 35        If specified, sets the limit in seconds for the 'max-stale' directive in the Cache-Control header.
 36    .PARAMETER MinFresh
 37        If specified, sets the 'min-fresh' directive in seconds for the Cache-Control header.
 38    .PARAMETER NoTransform
 39        If specified, adds a 'no-transform' directive to the Cache-Control header.
 40    .PARAMETER OnlyIfCached
 41        If specified, adds an 'only-if-cached' directive to the Cache-Control header.
 42    .PARAMETER Public
 43        If specified, adds a 'public' directive to the Cache-Control header.
 44    .PARAMETER Private
 45        If specified, adds a 'private' directive to the Cache-Control header.
 46    .PARAMETER MustRevalidate
 47        If specified, adds a 'must-revalidate' directive to the Cache-Control header.
 48    .PARAMETER ProxyRevalidate
 49        If specified, adds a 'proxy-revalidate' directive to the Cache-Control header.
 50    .EXAMPLE
 51          Add-KrStaticFilesMiddleware -RequestPath '/static' -HttpsCompression -ServeUnknownFileTypes -DefaultContentTyp
 52        This example adds a static file service to the server for the path '/static', enabling HTTPS compression, allowi
 53        setting the default content type to 'application/octet-stream', and redirecting requests to append a trailing sl
 54    .EXAMPLE
 55        Add-KrStaticFilesMiddleware -Options $options
 56        This example adds a static file service to the server using the specified StaticFileOptions.
 57    .EXAMPLE
 58        Add-KrStaticFilesMiddleware -RequestPath '/static' -MaxAge 600 -Public -MustRevalidate
 59        This example adds a static file service to the server for the path '/static', setting caching headers with a max
 60        marking the response as public, and adding the must-revalidate directive.
 61    .LINK
 62        https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.builder.staticfileoptions?view=aspnetcore-8.0
 63    .NOTES
 64        ContentTypeProvider and ContentTypeProviderOptions are not supported yet.
 65#>
 66function Add-KrStaticFilesMiddleware {
 67    [KestrunRuntimeApi('Definition')]
 68    [CmdletBinding(defaultParameterSetName = 'Items')]
 69    param(
 70        [Parameter(Mandatory = $true, ParameterSetName = 'Options')]
 71        [Microsoft.AspNetCore.Builder.StaticFileOptions]$Options,
 72
 73        [Parameter(ParameterSetName = 'Items')]
 74        [string]$RootPath,
 75
 76        [Parameter(ParameterSetName = 'Items')]
 77        [string]$RequestPath,
 78
 79        [Parameter(ParameterSetName = 'Items')]
 80        [Microsoft.AspNetCore.Http.Features.HttpsCompressionMode]$HttpsCompression,
 81
 82        [Parameter(ParameterSetName = 'Items')]
 83        [switch]$ServeUnknownFileTypes,
 84
 85        [Parameter(ParameterSetName = 'Items')]
 86        [string]$DefaultContentType,
 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
 139
 0140    if ($PSCmdlet.ParameterSetName -eq 'Items') {
 0141        $Options = [Microsoft.AspNetCore.Builder.StaticFileOptions]::new()
 0142        if (-not [string]::IsNullOrEmpty($RootPath)) {
 0143            $resolvedPath = Resolve-KrPath $RootPath -KestrunRoot
 0144            $Options.FileProvider = [Microsoft.Extensions.FileProviders.PhysicalFileProvider]::new($resolvedPath)
 145        }
 0146        if (-not [string]::IsNullOrEmpty($RequestPath)) {
 0147            $Options.RequestPath = [Microsoft.AspNetCore.Http.PathString]::new($RequestPath.TrimEnd('/'))
 148        }
 0149        if ($ServeUnknownFileTypes.IsPresent) {
 0150            $Options.ServeUnknownFileTypes = $true
 151        }
 0152        if ($PSBoundParameters.ContainsKey('HttpsCompression')) {
 0153            $Options.HttpsCompression = $HttpsCompression
 154        }
 0155        if (-not [string]::IsNullOrEmpty($DefaultContentType)) {
 0156            $Options.DefaultContentType = $DefaultContentType
 157        }
 0158        if ($RedirectToAppendTrailingSlash.IsPresent) {
 0159            $Options.RedirectToAppendTrailingSlash = $true
 160        }
 0161        if ($ContentTypeMap) {
 0162            $provider = [Microsoft.AspNetCore.StaticFiles.FileExtensionContentTypeProvider]::new()
 0163            foreach ($k in $ContentTypeMap.Keys) {
 0164                $ext = if ($k -like '.*') { $k } else { ".$k" }
 0165                $mime = [string]$ContentTypeMap[$k]
 0166                if ([string]::IsNullOrWhiteSpace($mime)) { continue }
 0167                $provider.Mappings[$ext] = $mime
 168            }
 0169            $Options.StaticFileOptions.ContentTypeProvider = $provider
 170        }
 171    }
 0172    if ($PSBoundParameters.ContainsKey('NoCache') -or ($PSBoundParameters.ContainsKey('NoStore')) -or ($PSBoundParameter
 0173        ($PSBoundParameters.ContainsKey('SharedMaxAge')) -or ($PSBoundParameters.ContainsKey('MaxStale')) -or
 0174        ($PSBoundParameters.ContainsKey('MaxStaleLimit')) -or ($PSBoundParameters.ContainsKey('MinFresh')) -or
 0175        ($PSBoundParameters.ContainsKey('NoTransform')) -or ($PSBoundParameters.ContainsKey('OnlyIfCached')) -or
 0176        ($PSBoundParameters.ContainsKey('Public')) -or ($PSBoundParameters.ContainsKey('Private')) -or
 0177        ($PSBoundParameters.ContainsKey('MustRevalidate')) -or ($PSBoundParameters.ContainsKey('ProxyRevalidate'))
 178    ) {
 0179        $cacheControl = [Microsoft.Net.Http.Headers.CacheControlHeaderValue]::new();
 0180        if ($PSBoundParameters.ContainsKey('NoCache')) { $cacheControl.NoCache = $NoCache.IsPresent }
 0181        if ($PSBoundParameters.ContainsKey('NoStore')) { $cacheControl.NoStore = $NoStore.IsPresent }
 0182        if ($PSBoundParameters.ContainsKey('MaxAge')) { $cacheControl.MaxAge = [TimeSpan]::FromSeconds($MaxAge) }
 0183        if ($PSBoundParameters.ContainsKey('SharedMaxAge')) { $cacheControl.SharedMaxAge = [TimeSpan]::FromSeconds($Shar
 0184        if ($PSBoundParameters.ContainsKey('MaxStale')) { $cacheControl.MaxStale = $MaxStale.IsPresent }
 0185        if ($PSBoundParameters.ContainsKey('MaxStaleLimit')) { $cacheControl.MaxStaleLimit = [TimeSpan]::FromSeconds($Ma
 0186        if ($PSBoundParameters.ContainsKey('MinFresh')) { $cacheControl.MinFresh = [TimeSpan]::FromSeconds($MinFresh) }
 0187        if ($PSBoundParameters.ContainsKey('NoTransform')) { $cacheControl.NoTransform = $NoTransform.IsPresent }
 0188        if ($PSBoundParameters.ContainsKey('OnlyIfCached')) { $cacheControl.OnlyIfCached = $OnlyIfCached.IsPresent }
 0189        if ($PSBoundParameters.ContainsKey('Public')) { $cacheControl.Public = $Public.IsPresent }
 0190        if ($PSBoundParameters.ContainsKey('Private')) { $cacheControl.Private = $Private.IsPresent }
 0191        if ($PSBoundParameters.ContainsKey('MustRevalidate')) { $cacheControl.MustRevalidate = $MustRevalidate.IsPresent
 0192        if ($PSBoundParameters.ContainsKey('ProxyRevalidate')) { $cacheControl.ProxyRevalidate = $ProxyRevalidate.IsPres
 193        # Add the static file service to the server with caching options
 0194        $Server.AddStaticFiles($Options, $cacheControl) | Out-Null
 195    } else {
 196        # Add the static file service to the server without caching options
 0197        $Server.AddStaticFiles($Options) | Out-Null
 198    }
 199}
 200

Methods/Properties

Add-KrStaticFilesMiddleware()