< 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@2d87023b37eb91155071c91dd3d6a2eeb3004705
Line coverage
0%
Covered lines: 0
Uncovered lines: 50
Coverable lines: 50
Total lines: 211
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/31) Total lines: 133 Tag: Kestrun/Kestrun@ea635f1ee1937c260a89d1a43a3c203cd8767c7b09/14/2025 - 21:23:16 Line coverage: 0% (0/52) Total lines: 211 Tag: Kestrun/Kestrun@c9d2f0b3dd164d7dc0dc2407a9f006293d92422310/13/2025 - 16:52:37 Line coverage: 0% (0/50) Total lines: 211 Tag: Kestrun/Kestrun@10d476bee71c71ad215bb8ab59f219887b5b4a5e

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 Server
 8    The Kestrun server instance to which the file server will be added.
 9.PARAMETER Options
 10    The FileServerOptions to configure the file server.
 11.PARAMETER RootPath
 12    The root path from which to serve files.
 13.PARAMETER RequestPath
 14    The path at which the file server 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 EnableDirectoryBrowsing
 22    If specified, enables directory browsing for the file server.
 23.PARAMETER RedirectToAppendTrailingSlash
 24    If specified, requests to the path will be redirected to append a trailing slash.
 25.PARAMETER ContentTypeMap
 26    A hashtable mapping file extensions to MIME types (e.g., @{ ".yaml"="application/x-yaml"; ".yml"="application/x-yaml
 27    This allows for serving files with the correct `Content-Type` header.
 28.PARAMETER NoCache
 29    If specified, adds a 'no-cache' directive to the Cache-Control header.
 30.PARAMETER NoStore
 31    If specified, adds a 'no-store' directive to the Cache-Control header.
 32.PARAMETER MaxAge
 33    If specified, sets the 'max-age' directive in seconds for the Cache-Control header.
 34.PARAMETER SharedMaxAge
 35    If specified, sets the 's-maxage' directive in seconds for the Cache-Control header
 36    (used by shared caches).
 37.PARAMETER MaxStale
 38    If specified, adds a 'max-stale' directive to the Cache-Control header.
 39.PARAMETER MaxStaleLimit
 40    If specified, sets the limit in seconds for the 'max-stale' directive in the Cache-Control header.
 41.PARAMETER MinFresh
 42    If specified, sets the 'min-fresh' directive in seconds for the Cache-Control header.
 43.PARAMETER NoTransform
 44    If specified, adds a 'no-transform' directive to the Cache-Control header.
 45.PARAMETER OnlyIfCached
 46    If specified, adds an 'only-if-cached' directive to the Cache-Control header.
 47.PARAMETER Public
 48    If specified, adds a 'public' directive to the Cache-Control header.
 49.PARAMETER Private
 50    If specified, adds a 'private' directive to the Cache-Control header.
 51.PARAMETER MustRevalidate
 52    If specified, adds a 'must-revalidate' directive to the Cache-Control header.
 53.PARAMETER ProxyRevalidate
 54    If specified, adds a 'proxy-revalidate' directive to the Cache-Control header.
 55.PARAMETER PassThru
 56    If specified, the cmdlet will return the modified server instance.
 57.EXAMPLE
 58    $server | Add-KrFileServerMiddleware -RequestPath '/files' -EnableDirectoryBrowsing
 59    This example adds a file server to the server for the path '/files', enabling directory browsing.
 60    The file server will use the default options for serving static files.
 61.EXAMPLE
 62    $server | Add-KrFileServerMiddleware -Options $options
 63    This example adds a file server to the server using the specified FileServerOptions.
 64.LINK
 65    https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.builder.fileserveroptions?view=aspnetcore-8.0
 66#>
 67function Add-KrFileServerMiddleware {
 68    [KestrunRuntimeApi('Definition')]
 69    [CmdletBinding(defaultParameterSetName = 'Items')]
 70    [OutputType([Kestrun.Hosting.KestrunHost])]
 71    param(
 72        [Parameter(Mandatory = $false, ValueFromPipeline = $true)]
 73        [Kestrun.Hosting.KestrunHost]$Server,
 74
 75        [Parameter(Mandatory = $true, ParameterSetName = 'Options')]
 76        [Microsoft.AspNetCore.Builder.FileServerOptions]$Options,
 77
 78        [Parameter(ParameterSetName = 'Items')]
 79        [string]$RootPath,
 80
 81        [Parameter(ParameterSetName = 'Items')]
 82        [string]$RequestPath,
 83
 84        [Parameter(ParameterSetName = 'Items')]
 85        [Microsoft.AspNetCore.Http.Features.HttpsCompressionMode]$HttpsCompression,
 86
 87        [Parameter(ParameterSetName = 'Items')]
 88        [switch]$ServeUnknownFileTypes,
 89
 90        [Parameter(ParameterSetName = 'Items')]
 91        [string]$DefaultContentType,
 92
 93        [Parameter(ParameterSetName = 'Items')]
 94        [switch]$EnableDirectoryBrowsing,
 95
 96        [Parameter(ParameterSetName = 'Items')]
 97        [switch]$RedirectToAppendTrailingSlash,
 98
 99        [Parameter(ParameterSetName = 'Items')]
 100        [hashtable]$ContentTypeMap,
 101        [Parameter()]
 102        [switch]$NoCache,
 103        [Parameter()]
 104        [switch]$NoStore,
 105        [Parameter()]
 106        [ValidateRange(1, [int]::MaxValue)]
 107        [int]$MaxAge,
 108        [Parameter()]
 109        [ValidateRange(1, [int]::MaxValue)]
 110        [int]$SharedMaxAge,
 111        [Parameter()]
 112        [switch]$MaxStale,
 113        [Parameter()]
 114        [ValidateRange(1, [int]::MaxValue)]
 115        [int]$MaxStaleLimit,
 116        [Parameter()]
 117        [ValidateRange(1, [int]::MaxValue)]
 118        [int]$MinFresh,
 119        [Parameter()]
 120        [switch]$NoTransform,
 121        [Parameter()]
 122        [switch]$OnlyIfCached,
 123        [Parameter()]
 124        [switch]$Public,
 125        [Parameter()]
 126        [switch]$Private,
 127        [Parameter()]
 128        [switch]$MustRevalidate,
 129        [Parameter()]
 130        [switch]$ProxyRevalidate,
 131
 132        [Parameter()]
 133        [switch]$PassThru
 134    )
 135    begin {
 136        # Ensure the server instance is resolved
 0137        $Server = Resolve-KestrunServer -Server $Server
 138    }
 139    process {
 0140        if ($PSCmdlet.ParameterSetName -eq 'Items') {
 0141            $Options = [Microsoft.AspNetCore.Builder.FileServerOptions]::new()
 142
 0143            if (-not [string]::IsNullOrEmpty($RequestPath)) {
 0144                $Options.RequestPath = [Microsoft.AspNetCore.Http.PathString]::new($RequestPath.TrimEnd('/'))
 145            }
 0146            if (-not [string]::IsNullOrEmpty($RootPath)) {
 0147                $resolvedPath = Resolve-KrPath $RootPath -KestrunRoot
 0148                $Options.FileProvider = [Microsoft.Extensions.FileProviders.PhysicalFileProvider]::new($resolvedPath)
 149            }
 0150            if ($EnableDirectoryBrowsing.IsPresent) {
 0151                $Options.EnableDirectoryBrowsing = $true
 152            }
 0153            if ($ServeUnknownFileTypes.IsPresent) {
 0154                $Options.StaticFileOptions.ServeUnknownFileTypes = $true
 155            }
 0156            if ($PSBoundParameters.ContainsKey('HttpsCompression')) {
 0157                $Options.StaticFileOptions.HttpsCompression = $HttpsCompression
 158            }
 0159            if (-not [string]::IsNullOrEmpty($DefaultContentType)) {
 0160                $Options.StaticFileOptions.DefaultContentType = $DefaultContentType
 161            }
 0162            if ($RedirectToAppendTrailingSlash.IsPresent) {
 0163                $Options.RedirectToAppendTrailingSlash = $true
 164            }
 0165            if ($ContentTypeMap) {
 0166                $provider = [Microsoft.AspNetCore.StaticFiles.FileExtensionContentTypeProvider]::new()
 0167                foreach ($k in $ContentTypeMap.Keys) {
 0168                    $ext = if ($k -like ".*") { $k } else { ".$k" }
 0169                    $mime = [string]$ContentTypeMap[$k]
 0170                    if ([string]::IsNullOrWhiteSpace($mime)) { continue }
 0171                    $provider.Mappings[$ext] = $mime
 172                }
 0173                $Options.StaticFileOptions.ContentTypeProvider = $provider
 174            }
 175        }
 176
 0177        if ($PSBoundParameters.ContainsKey('NoCache') -or ($PSBoundParameters.ContainsKey('NoStore')) -or ($PSBoundParam
 0178            ($PSBoundParameters.ContainsKey('SharedMaxAge')) -or ($PSBoundParameters.ContainsKey('MaxStale')) -or
 0179            ($PSBoundParameters.ContainsKey('MaxStaleLimit')) -or ($PSBoundParameters.ContainsKey('MinFresh')) -or
 0180            ($PSBoundParameters.ContainsKey('NoTransform')) -or ($PSBoundParameters.ContainsKey('OnlyIfCached')) -or
 0181            ($PSBoundParameters.ContainsKey('Public')) -or ($PSBoundParameters.ContainsKey('Private')) -or
 0182            ($PSBoundParameters.ContainsKey('MustRevalidate')) -or ($PSBoundParameters.ContainsKey('ProxyRevalidate'))
 183        ) {
 0184            $cacheControl = [Microsoft.Net.Http.Headers.CacheControlHeaderValue]::new();
 0185            if ($PSBoundParameters.ContainsKey('NoCache')) { $cacheControl.NoCache = $NoCache.IsPresent }
 0186            if ($PSBoundParameters.ContainsKey('NoStore')) { $cacheControl.NoStore = $NoStore.IsPresent }
 0187            if ($PSBoundParameters.ContainsKey('MaxAge')) { $cacheControl.MaxAge = [TimeSpan]::FromSeconds($MaxAge) }
 0188            if ($PSBoundParameters.ContainsKey('SharedMaxAge')) { $cacheControl.SharedMaxAge = [TimeSpan]::FromSeconds($
 0189            if ($PSBoundParameters.ContainsKey('MaxStale')) { $cacheControl.MaxStale = $MaxStale.IsPresent }
 0190            if ($PSBoundParameters.ContainsKey('MaxStaleLimit')) { $cacheControl.MaxStaleLimit = [TimeSpan]::FromSeconds
 0191            if ($PSBoundParameters.ContainsKey('MinFresh')) { $cacheControl.MinFresh = [TimeSpan]::FromSeconds($MinFresh
 0192            if ($PSBoundParameters.ContainsKey('NoTransform')) { $cacheControl.NoTransform = $NoTransform.IsPresent }
 0193            if ($PSBoundParameters.ContainsKey('OnlyIfCached')) { $cacheControl.OnlyIfCached = $OnlyIfCached.IsPresent }
 0194            if ($PSBoundParameters.ContainsKey('Public')) { $cacheControl.Public = $Public.IsPresent }
 0195            if ($PSBoundParameters.ContainsKey('Private')) { $cacheControl.Private = $Private.IsPresent }
 0196            if ($PSBoundParameters.ContainsKey('MustRevalidate')) { $cacheControl.MustRevalidate = $MustRevalidate.IsPre
 0197            if ($PSBoundParameters.ContainsKey('ProxyRevalidate')) { $cacheControl.ProxyRevalidate = $ProxyRevalidate.Is
 198            # Add the file server to the server with caching options
 0199            [Kestrun.Hosting.KestrunHostStaticFilesExtensions]::AddFileServer($Server, $Options, $cacheControl) | Out-Nu
 200        } else {
 201            # Add the file server to the server without caching options
 0202            [Kestrun.Hosting.KestrunHostStaticFilesExtensions]::AddFileServer($Server, $Options) | Out-Null
 203        }
 204
 0205        if ($PassThru.IsPresent) {
 206            # if the PassThru switch is specified, return the modified server instance
 0207            return $Server
 208        }
 209    }
 210}
 211

Methods/Properties

Add-KrFileServerMiddleware()