| | | 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 DisableGzip |
| | | 18 | | If specified, disables Gzip compression. |
| | | 19 | | .PARAMETER DisableBrotli |
| | | 20 | | If specified, disables Brotli compression. |
| | | 21 | | .PARAMETER PassThru |
| | | 22 | | If specified, the cmdlet will return the modified server instance. |
| | | 23 | | .EXAMPLE |
| | | 24 | | $server | Add-KrCompressionMiddleware -EnableForHttps -MimeTypes 'text/plain', 'application/json' -ExcludedMimeT |
| | | 25 | | This example adds response compression to the server, enabling it for HTTPS requests, and specifying the MIME ty |
| | | 26 | | .EXAMPLE |
| | | 27 | | $server | Add-KrCompressionMiddleware -Options $options |
| | | 28 | | This example adds response compression to the server using the specified ResponseCompressionOptions. |
| | | 29 | | .LINK |
| | | 30 | | https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.responsecompression.responsecompressionoptions |
| | | 31 | | .NOTES |
| | | 32 | | This cmdlet is used to configure response compression for the Kestrun server, allowing you to specify which MIME |
| | | 33 | | Providers is not supported yet. |
| | | 34 | | #> |
| | | 35 | | function Add-KrCompressionMiddleware { |
| | | 36 | | [KestrunRuntimeApi('Definition')] |
| | | 37 | | [CmdletBinding(defaultParameterSetName = 'Items')] |
| | | 38 | | [OutputType([Kestrun.Hosting.KestrunHost])] |
| | | 39 | | param( |
| | | 40 | | [Parameter(Mandatory = $false, ValueFromPipeline = $true)] |
| | | 41 | | [Kestrun.Hosting.KestrunHost]$Server, |
| | | 42 | | [Parameter(Mandatory = $true, ParameterSetName = 'Options')] |
| | | 43 | | [Microsoft.AspNetCore.ResponseCompression.ResponseCompressionOptions]$Options, |
| | | 44 | | [Parameter(ParameterSetName = 'Items')] |
| | | 45 | | [switch]$EnableForHttps, |
| | | 46 | | [Parameter(ParameterSetName = 'Items')] |
| | | 47 | | [string[]]$MimeTypes, |
| | | 48 | | [Parameter(ParameterSetName = 'Items')] |
| | | 49 | | [string[]]$ExcludedMimeTypes, |
| | | 50 | | [Parameter(ParameterSetName = 'Items')] |
| | | 51 | | [switch]$DisableGzip, |
| | | 52 | | [Parameter(ParameterSetName = 'Items')] |
| | | 53 | | [switch]$DisableBrotli, |
| | | 54 | | [Parameter()] |
| | | 55 | | [switch]$PassThru |
| | | 56 | | ) |
| | | 57 | | begin { |
| | | 58 | | # Ensure the server instance is resolved |
| | 0 | 59 | | $Server = Resolve-KestrunServer -Server $Server |
| | | 60 | | } |
| | | 61 | | process { |
| | 0 | 62 | | if ($PSCmdlet.ParameterSetName -eq 'Items') { |
| | 0 | 63 | | $Options = [Microsoft.AspNetCore.ResponseCompression.ResponseCompressionOptions]::new() |
| | 0 | 64 | | if ($null -ne $MimeTypes -and $MimeTypes.Count -gt 0) { |
| | 0 | 65 | | $Options.MimeTypes = $MimeTypes |
| | | 66 | | } |
| | 0 | 67 | | if ($null -ne $ExcludedMimeTypes -and $ExcludedMimeTypes.Count -gt 0) { |
| | 0 | 68 | | $Options.ExcludedMimeTypes = $ExcludedMimeTypes |
| | | 69 | | } |
| | 0 | 70 | | if ($EnableForHttps.IsPresent) { |
| | 0 | 71 | | $Options.EnableForHttps = $true |
| | | 72 | | } |
| | 0 | 73 | | if (-not $DisableGzip.IsPresent) { |
| | 0 | 74 | | $gzipCompressionProviderOptions = [Microsoft.AspNetCore.ResponseCompression.GzipCompressionProviderOptio |
| | 0 | 75 | | $gzipOptionsWrapper = [Microsoft.Extensions.Options.Options]::Create($gzipCompressionProviderOptions) |
| | 0 | 76 | | $Options.Providers.Add([Microsoft.AspNetCore.ResponseCompression.GzipCompressionProvider]::new($gzipOpti |
| | 0 | 77 | | if (-not $DisableBrotli.IsPresent) { |
| | 0 | 78 | | $brotliCompressionProviderOptions = [Microsoft.AspNetCore.ResponseCompression.BrotliCompressionProvi |
| | 0 | 79 | | $brotliOptionsWrapper = [Microsoft.Extensions.Options.Options]::Create($brotliCompressionProviderOpt |
| | 0 | 80 | | $Options.Providers.Add([Microsoft.AspNetCore.ResponseCompression.BrotliCompressionProvider]::new($br |
| | | 81 | | } |
| | | 82 | | } |
| | | 83 | | } |
| | | 84 | | |
| | 0 | 85 | | [Kestrun.Hosting.KestrunHttpMiddlewareExtensions]::AddResponseCompression($Server, $Options) | Out-Null |
| | | 86 | | |
| | 0 | 87 | | if ($PassThru.IsPresent) { |
| | | 88 | | # if the PassThru switch is specified, return the modified server instance |
| | 0 | 89 | | return $Server |
| | | 90 | | } |
| | | 91 | | } |
| | | 92 | | } |
| | | 93 | | |