| | 1 | | <# |
| | 2 | | .SYNOPSIS |
| | 3 | | Adds response compression to the server. |
| | 4 | | .DESCRIPTION |
| | 5 | | This cmdlet allows you to configure response compression for the Kestrun server. |
| | 6 | | It can be used to compress responses using various algorithms like Gzip, Brotli, etc. |
| | 7 | | .PARAMETER Server |
| | 8 | | The Kestrun server instance to which the response compression will be added. |
| | 9 | | .PARAMETER Options |
| | 10 | | The ResponseCompressionOptions to configure the response compression. |
| | 11 | | .PARAMETER EnableForHttps |
| | 12 | | If specified, enables response compression for HTTPS requests. |
| | 13 | | .PARAMETER MimeTypes |
| | 14 | | An array of MIME types to compress. If not specified, defaults to common text-based MIME types. |
| | 15 | | .PARAMETER ExcludedMimeTypes |
| | 16 | | An array of MIME types to exclude from compression. |
| | 17 | | .PARAMETER PassThru |
| | 18 | | If specified, the cmdlet will return the modified server instance. |
| | 19 | | .EXAMPLE |
| | 20 | | $server | Add-KrResponseCompression -EnableForHttps -MimeTypes 'text/plain', 'application/json' -ExcludedMimeTyp |
| | 21 | | This example adds response compression to the server, enabling it for HTTPS requests, and specifying the MIME ty |
| | 22 | | .EXAMPLE |
| | 23 | | $server | Add-KrResponseCompression -Options $options |
| | 24 | | This example adds response compression to the server using the specified ResponseCompressionOptions. |
| | 25 | | .LINK |
| | 26 | | https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.responsecompression.responsecompressionoptions |
| | 27 | | .NOTES |
| | 28 | | This cmdlet is used to configure response compression for the Kestrun server, allowing you to specify which MIME |
| | 29 | | Providers is not supported yet. |
| | 30 | | #> |
| | 31 | | function Add-KrResponseCompression { |
| | 32 | | [KestrunRuntimeApi('Definition')] |
| | 33 | | [CmdletBinding(defaultParameterSetName = 'Items')] |
| | 34 | | [OutputType([Kestrun.Hosting.KestrunHost])] |
| | 35 | | param( |
| | 36 | | [Parameter(Mandatory = $false, ValueFromPipeline = $true)] |
| | 37 | | [Kestrun.Hosting.KestrunHost]$Server, |
| | 38 | | [Parameter(Mandatory = $true, ParameterSetName = 'Options')] |
| | 39 | | [Microsoft.AspNetCore.ResponseCompression.ResponseCompressionOptions]$Options, |
| | 40 | | [Parameter(ParameterSetName = 'Items')] |
| | 41 | | [switch]$EnableForHttps, |
| | 42 | | [Parameter(ParameterSetName = 'Items')] |
| | 43 | | [string[]]$MimeTypes, |
| | 44 | | [Parameter(ParameterSetName = 'Items')] |
| | 45 | | [string[]]$ExcludedMimeTypes, |
| | 46 | | [Parameter()] |
| | 47 | | [switch]$PassThru |
| | 48 | | ) |
| | 49 | | begin { |
| | 50 | | # Ensure the server instance is resolved |
| 1 | 51 | | $Server = Resolve-KestrunServer -Server $Server |
| 1 | 52 | | if ($null -eq $Server) { |
| 0 | 53 | | throw 'Server is not initialized. Please ensure the server is configured before setting options.' |
| | 54 | | } |
| | 55 | | } |
| | 56 | | process { |
| 1 | 57 | | if ($PSCmdlet.ParameterSetName -eq 'Items') { |
| 1 | 58 | | $Options = [Microsoft.AspNetCore.ResponseCompression.ResponseCompressionOptions]::new() |
| 1 | 59 | | if ($null -ne $MimeTypes -and $MimeTypes.Count -gt 0) { |
| 1 | 60 | | $Options.MimeTypes = $MimeTypes |
| | 61 | | } |
| 1 | 62 | | if ($null -ne $ExcludedMimeTypes -and $ExcludedMimeTypes.Count -gt 0) { |
| 0 | 63 | | $Options.ExcludedMimeTypes = $ExcludedMimeTypes |
| | 64 | | } |
| 1 | 65 | | if ($EnableForHttps.IsPresent) { |
| 1 | 66 | | $Options.EnableForHttps = $true |
| | 67 | | } |
| | 68 | | # Providers are not supported yet |
| | 69 | | <# if ($null -ne $Providers -and $Providers.Count -gt 0) { |
| | 70 | | foreach ($Provider in $Providers) { |
| | 71 | | $Options.Providers.Add($Provider) |
| | 72 | | } |
| | 73 | | }#> |
| | 74 | | } |
| | 75 | |
|
| 2 | 76 | | [Kestrun.Hosting.KestrunHttpMiddlewareExtensions]::AddResponseCompression($Server, $Options) | Out-Null |
| | 77 | |
|
| 1 | 78 | | if ($PassThru.IsPresent) { |
| | 79 | | # if the PassThru switch is specified, return the server instance |
| | 80 | | # Return the modified server instance |
| 0 | 81 | | return $Server |
| | 82 | | } |
| | 83 | | } |
| | 84 | | } |
| | 85 | |
|