< 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@2d87023b37eb91155071c91dd3d6a2eeb3004705
Line coverage
0%
Covered lines: 0
Uncovered lines: 48
Coverable lines: 48
Total lines: 207
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 09/13/2025 - 17:19:56 Line coverage: 0% (0/29) Total lines: 125 Tag: Kestrun/Kestrun@ea635f1ee1937c260a89d1a43a3c203cd8767c7b09/14/2025 - 21:23:16 Line coverage: 0% (0/50) Total lines: 207 Tag: Kestrun/Kestrun@c9d2f0b3dd164d7dc0dc2407a9f006293d92422310/13/2025 - 16:52:37 Line coverage: 0% (0/48) Total lines: 207 Tag: Kestrun/Kestrun@10d476bee71c71ad215bb8ab59f219887b5b4a5e

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 Server
 8    The Kestrun server instance to which the static file service will be added.
 9.PARAMETER Options
 10    The StaticFileOptions to configure the static file service.
 11.PARAMETER RootPath
 12    The root path from which to serve static files.
 13.PARAMETER RequestPath
 14    The path at which the static file service will be registered.
 15.PARAMETER HttpsCompression
 16    The HTTPS compression mode to use for the static files.
 17.PARAMETER ServeUnknownFileTypes
 18    If specified, allows serving files with unknown MIME types.
 19.PARAMETER DefaultContentType
 20    The default content type to use for files served by the static file service.
 21.PARAMETER RedirectToAppendTrailingSlash
 22    If specified, redirects requests to append a trailing slash to the URL.
 23.PARAMETER ContentTypeMap
 24    A hashtable mapping file extensions to MIME types.
 25.PARAMETER NoCache
 26    If specified, adds a 'no-cache' directive to the Cache-Control header.
 27.PARAMETER NoStore
 28    If specified, adds a 'no-store' directive to the Cache-Control header.
 29.PARAMETER MaxAge
 30    If specified, sets the 'max-age' directive in seconds for the Cache-Control header.
 31.PARAMETER SharedMaxAge
 32    If specified, sets the 's-maxage' directive in seconds for the Cache-Control header
 33    (used by shared caches).
 34.PARAMETER MaxStale
 35    If specified, adds a 'max-stale' directive to the Cache-Control header.
 36.PARAMETER MaxStaleLimit
 37    If specified, sets the limit in seconds for the 'max-stale' directive in the Cache-Control header.
 38.PARAMETER MinFresh
 39    If specified, sets the 'min-fresh' directive in seconds for the Cache-Control header.
 40.PARAMETER NoTransform
 41    If specified, adds a 'no-transform' directive to the Cache-Control header.
 42.PARAMETER OnlyIfCached
 43    If specified, adds an 'only-if-cached' directive to the Cache-Control header.
 44.PARAMETER Public
 45    If specified, adds a 'public' directive to the Cache-Control header.
 46.PARAMETER Private
 47    If specified, adds a 'private' directive to the Cache-Control header.
 48.PARAMETER MustRevalidate
 49    If specified, adds a 'must-revalidate' directive to the Cache-Control header.
 50.PARAMETER ProxyRevalidate
 51    If specified, adds a 'proxy-revalidate' directive to the Cache-Control header.
 52.PARAMETER PassThru
 53    If specified, the cmdlet will return the modified server instance after adding the static file service.
 54.EXAMPLE
 55    $server | Add-KrStaticFilesMiddleware -RequestPath '/static' -HttpsCompression -ServeUnknownFileTypes -DefaultConten
 56    This example adds a static file service to the server for the path '/static', enabling HTTPS compression, allowing s
 57    setting the default content type to 'application/octet-stream', and redirecting requests to append a trailing slash.
 58.EXAMPLE
 59    $server | Add-KrStaticFilesMiddleware -Options $options
 60    This example adds a static file service to the server using the specified StaticFileOptions.
 61.EXAMPLE
 62    $server | Add-KrStaticFilesMiddleware -RequestPath '/static' -MaxAge 600 -Public -MustRevalidate
 63    This example adds a static file service to the server for the path '/static', setting caching headers with a max-age
 64    marking the response as public, and adding the must-revalidate directive.
 65.LINK
 66    https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.builder.staticfileoptions?view=aspnetcore-8.0
 67.NOTES
 68    ContentTypeProvider and ContentTypeProviderOptions are not supported yet.
 69#>
 70function Add-KrStaticFilesMiddleware {
 71    [KestrunRuntimeApi('Definition')]
 72    [CmdletBinding(defaultParameterSetName = 'Items')]
 73    [OutputType([Kestrun.Hosting.KestrunHost])]
 74    param(
 75        [Parameter(Mandatory = $false, ValueFromPipeline = $true)]
 76        [Kestrun.Hosting.KestrunHost]$Server,
 77
 78        [Parameter(Mandatory = $true, ParameterSetName = 'Options')]
 79        [Microsoft.AspNetCore.Builder.StaticFileOptions]$Options,
 80
 81        [Parameter(ParameterSetName = 'Items')]
 82        [string]$RootPath,
 83
 84        [Parameter(ParameterSetName = 'Items')]
 85        [string]$RequestPath,
 86
 87        [Parameter(ParameterSetName = 'Items')]
 88        [Microsoft.AspNetCore.Http.Features.HttpsCompressionMode]$HttpsCompression,
 89
 90        [Parameter(ParameterSetName = 'Items')]
 91        [switch]$ServeUnknownFileTypes,
 92
 93        [Parameter(ParameterSetName = 'Items')]
 94        [string]$DefaultContentType,
 95
 96        [Parameter(ParameterSetName = 'Items')]
 97        [switch]$RedirectToAppendTrailingSlash,
 98
 99        [Parameter(ParameterSetName = 'Items')]
 100        [hashtable]$ContentTypeMap,
 101
 102        [Parameter()]
 103        [switch]$NoCache,
 104        [Parameter()]
 105        [switch]$NoStore,
 106        [Parameter()]
 107        [ValidateRange(1, [int]::MaxValue)]
 108        [int]$MaxAge,
 109        [Parameter()]
 110        [ValidateRange(1, [int]::MaxValue)]
 111        [int]$SharedMaxAge,
 112        [Parameter()]
 113        [switch]$MaxStale,
 114        [Parameter()]
 115        [ValidateRange(1, [int]::MaxValue)]
 116        [int]$MaxStaleLimit,
 117        [Parameter()]
 118        [ValidateRange(1, [int]::MaxValue)]
 119        [int]$MinFresh,
 120        [Parameter()]
 121        [switch]$NoTransform,
 122        [Parameter()]
 123        [switch]$OnlyIfCached,
 124        [Parameter()]
 125        [switch]$Public,
 126        [Parameter()]
 127        [switch]$Private,
 128        [Parameter()]
 129        [switch]$MustRevalidate,
 130        [Parameter()]
 131        [switch]$ProxyRevalidate,
 132
 133        [Parameter()]
 134        [switch]$PassThru
 135    )
 136    begin {
 137        # Ensure the server instance is resolved
 0138        $Server = Resolve-KestrunServer -Server $Server
 139    }
 140    process {
 0141        if ($PSCmdlet.ParameterSetName -eq 'Items') {
 0142            $Options = [Microsoft.AspNetCore.Builder.StaticFileOptions]::new()
 0143            if (-not [string]::IsNullOrEmpty($RootPath)) {
 0144                $resolvedPath = Resolve-KrPath $RootPath -KestrunRoot
 0145                $Options.FileProvider = [Microsoft.Extensions.FileProviders.PhysicalFileProvider]::new($resolvedPath)
 146            }
 0147            if (-not [string]::IsNullOrEmpty($RequestPath)) {
 0148                $Options.RequestPath = [Microsoft.AspNetCore.Http.PathString]::new($RequestPath.TrimEnd('/'))
 149            }
 0150            if ($ServeUnknownFileTypes.IsPresent) {
 0151                $Options.ServeUnknownFileTypes = $true
 152            }
 0153            if ($PSBoundParameters.ContainsKey('HttpsCompression')) {
 0154                $Options.HttpsCompression = $HttpsCompression
 155            }
 0156            if (-not [string]::IsNullOrEmpty($DefaultContentType)) {
 0157                $Options.DefaultContentType = $DefaultContentType
 158            }
 0159            if ($RedirectToAppendTrailingSlash.IsPresent) {
 0160                $Options.RedirectToAppendTrailingSlash = $true
 161            }
 0162            if ($ContentTypeMap) {
 0163                $provider = [Microsoft.AspNetCore.StaticFiles.FileExtensionContentTypeProvider]::new()
 0164                foreach ($k in $ContentTypeMap.Keys) {
 0165                    $ext = if ($k -like '.*') { $k } else { ".$k" }
 0166                    $mime = [string]$ContentTypeMap[$k]
 0167                    if ([string]::IsNullOrWhiteSpace($mime)) { continue }
 0168                    $provider.Mappings[$ext] = $mime
 169                }
 0170                $Options.StaticFileOptions.ContentTypeProvider = $provider
 171            }
 172        }
 0173        if ($PSBoundParameters.ContainsKey('NoCache') -or ($PSBoundParameters.ContainsKey('NoStore')) -or ($PSBoundParam
 0174            ($PSBoundParameters.ContainsKey('SharedMaxAge')) -or ($PSBoundParameters.ContainsKey('MaxStale')) -or
 0175            ($PSBoundParameters.ContainsKey('MaxStaleLimit')) -or ($PSBoundParameters.ContainsKey('MinFresh')) -or
 0176            ($PSBoundParameters.ContainsKey('NoTransform')) -or ($PSBoundParameters.ContainsKey('OnlyIfCached')) -or
 0177            ($PSBoundParameters.ContainsKey('Public')) -or ($PSBoundParameters.ContainsKey('Private')) -or
 0178            ($PSBoundParameters.ContainsKey('MustRevalidate')) -or ($PSBoundParameters.ContainsKey('ProxyRevalidate'))
 179        ) {
 0180            $cacheControl = [Microsoft.Net.Http.Headers.CacheControlHeaderValue]::new();
 0181            if ($PSBoundParameters.ContainsKey('NoCache')) { $cacheControl.NoCache = $NoCache.IsPresent }
 0182            if ($PSBoundParameters.ContainsKey('NoStore')) { $cacheControl.NoStore = $NoStore.IsPresent }
 0183            if ($PSBoundParameters.ContainsKey('MaxAge')) { $cacheControl.MaxAge = [TimeSpan]::FromSeconds($MaxAge) }
 0184            if ($PSBoundParameters.ContainsKey('SharedMaxAge')) { $cacheControl.SharedMaxAge = [TimeSpan]::FromSeconds($
 0185            if ($PSBoundParameters.ContainsKey('MaxStale')) { $cacheControl.MaxStale = $MaxStale.IsPresent }
 0186            if ($PSBoundParameters.ContainsKey('MaxStaleLimit')) { $cacheControl.MaxStaleLimit = [TimeSpan]::FromSeconds
 0187            if ($PSBoundParameters.ContainsKey('MinFresh')) { $cacheControl.MinFresh = [TimeSpan]::FromSeconds($MinFresh
 0188            if ($PSBoundParameters.ContainsKey('NoTransform')) { $cacheControl.NoTransform = $NoTransform.IsPresent }
 0189            if ($PSBoundParameters.ContainsKey('OnlyIfCached')) { $cacheControl.OnlyIfCached = $OnlyIfCached.IsPresent }
 0190            if ($PSBoundParameters.ContainsKey('Public')) { $cacheControl.Public = $Public.IsPresent }
 0191            if ($PSBoundParameters.ContainsKey('Private')) { $cacheControl.Private = $Private.IsPresent }
 0192            if ($PSBoundParameters.ContainsKey('MustRevalidate')) { $cacheControl.MustRevalidate = $MustRevalidate.IsPre
 0193            if ($PSBoundParameters.ContainsKey('ProxyRevalidate')) { $cacheControl.ProxyRevalidate = $ProxyRevalidate.Is
 194            # Add the static file service to the server with caching options
 0195            [Kestrun.Hosting.KestrunHostStaticFilesExtensions]::AddStaticFiles($Server, $Options, $cacheControl) | Out-N
 196        } else {
 197            # Add the static file service to the server without caching options
 0198            [Kestrun.Hosting.KestrunHostStaticFilesExtensions]::AddStaticFiles($Server, $Options) | Out-Null
 199        }
 200
 0201        if ($PassThru.IsPresent) {
 202            # if the PassThru switch is specified, return the modified server instance
 0203            return $Server
 204        }
 205    }
 206}
 207

Methods/Properties

Add-KrStaticFilesMiddleware()