| | | 1 | | <# |
| | | 2 | | .SYNOPSIS |
| | | 3 | | Adds an SSE broadcast endpoint to the server. |
| | | 4 | | .DESCRIPTION |
| | | 5 | | Registers an in-memory SSE broadcaster service and maps an SSE endpoint that keeps connections open. |
| | | 6 | | Clients connect (e.g. via browser EventSource) and receive events broadcast by Send-KrSseBroadcastEvent. |
| | | 7 | | .PARAMETER Server |
| | | 8 | | The Kestrun server instance. If not provided, the default server is used. |
| | | 9 | | .PARAMETER Path |
| | | 10 | | The URL path where the SSE broadcast endpoint will be accessible. Defaults to '/sse/broadcast'. |
| | | 11 | | .PARAMETER DocId |
| | | 12 | | The OpenAPI document IDs to which the SSE broadcast endpoint should be added. Default is 'Default'. |
| | | 13 | | .PARAMETER KeepAliveSeconds |
| | | 14 | | If greater than 0, sends periodic SSE comments (keep-alives) to keep intermediaries from closing idle connection |
| | | 15 | | .PARAMETER OperationId |
| | | 16 | | Optional OpenAPI operationId override for the broadcast endpoint. |
| | | 17 | | .PARAMETER Summary |
| | | 18 | | Optional OpenAPI summary override for the broadcast endpoint. |
| | | 19 | | .PARAMETER Description |
| | | 20 | | Optional OpenAPI description override for the broadcast endpoint. |
| | | 21 | | .PARAMETER Tags |
| | | 22 | | Optional OpenAPI tags override for the broadcast endpoint. |
| | | 23 | | .PARAMETER StatusCode |
| | | 24 | | Optional OpenAPI response status code override (default: 200). |
| | | 25 | | .PARAMETER ResponseDescription |
| | | 26 | | Optional OpenAPI response description override. |
| | | 27 | | .PARAMETER ItemSchemaType |
| | | 28 | | Optional OpenAPI schema type for the stream payload (default: String). |
| | | 29 | | This only applies when -OpenApi is not provided. |
| | | 30 | | .PARAMETER SkipOpenApi |
| | | 31 | | If specified, the OpenAPI documentation for this endpoint will be skipped. |
| | | 32 | | .PARAMETER Options |
| | | 33 | | Full OpenAPI customization object (Kestrun.Hosting.Options.SseBroadcastOptions). |
| | | 34 | | When provided, it takes precedence over the individual parameters. |
| | | 35 | | .PARAMETER PassThru |
| | | 36 | | If specified, returns the modified server instance. |
| | | 37 | | .EXAMPLE |
| | | 38 | | Add-KrSseBroadcastMiddleware -Path '/sse/broadcast' -PassThru |
| | | 39 | | Adds an SSE broadcast endpoint at '/sse/broadcast' and returns the server instance. |
| | | 40 | | .EXAMPLE |
| | | 41 | | $server = New-KrServer -Name 'MyServer' |
| | | 42 | | Add-KrSseBroadcastMiddleware -Server $server -Path '/events' -KeepAliveSeconds 30 |
| | | 43 | | Adds an SSE broadcast endpoint at '/events' with 30-second keep-alives to the specified server. |
| | | 44 | | .EXAMPLE |
| | | 45 | | Add-KrSseBroadcastMiddleware -SkipOpenApi |
| | | 46 | | Adds an SSE broadcast endpoint without OpenAPI documentation. |
| | | 47 | | .EXAMPLE |
| | | 48 | | $options = [Kestrun.Hosting.Options.SseBroadcastOptions]::new() |
| | | 49 | | $options.Path = '/sse/updates' |
| | | 50 | | $options.KeepAliveSeconds = 15 |
| | | 51 | | Add-KrSseBroadcastMiddleware -Options $options -PassThru |
| | | 52 | | Adds an SSE broadcast endpoint at '/sse/updates' with 15-second keep-alives using the provided options object. |
| | | 53 | | .NOTES |
| | | 54 | | Call this before Enable-KrConfiguration. |
| | | 55 | | #> |
| | | 56 | | function Add-KrSseBroadcastMiddleware { |
| | | 57 | | [KestrunRuntimeApi('Definition')] |
| | | 58 | | [CmdletBinding(DefaultParameterSetName = 'Items')] |
| | | 59 | | param( |
| | | 60 | | [Parameter(ValueFromPipeline)] |
| | | 61 | | [Kestrun.Hosting.KestrunHost]$Server, |
| | | 62 | | |
| | | 63 | | [Parameter(Mandatory = $false, ParameterSetName = 'Items')] |
| | | 64 | | [Parameter(Mandatory = $false, ParameterSetName = 'ItemsSkipOpenApi')] |
| | | 65 | | [string]$Path, |
| | | 66 | | |
| | | 67 | | [Parameter(Mandatory = $false, ParameterSetName = 'Items')] |
| | | 68 | | [Parameter(Mandatory = $false, ParameterSetName = 'ItemsSkipOpenApi')] |
| | | 69 | | [string[]]$DocId = [Kestrun.OpenApi.OpenApiDocDescriptor]::DefaultDocumentationIds, |
| | | 70 | | |
| | | 71 | | [Parameter(Mandatory = $false, ParameterSetName = 'Items')] |
| | | 72 | | [ValidateRange(0, 3600)] |
| | | 73 | | [int]$KeepAliveSeconds, |
| | | 74 | | |
| | | 75 | | [Parameter(Mandatory = $false, ParameterSetName = 'Items')] |
| | | 76 | | [string]$OperationId, |
| | | 77 | | |
| | | 78 | | [Parameter(Mandatory = $false, ParameterSetName = 'Items')] |
| | | 79 | | [string]$Summary, |
| | | 80 | | |
| | | 81 | | [Parameter(Mandatory = $false, ParameterSetName = 'Items')] |
| | | 82 | | [string]$Description, |
| | | 83 | | |
| | | 84 | | [Parameter(Mandatory = $false, ParameterSetName = 'Items')] |
| | | 85 | | [string[]]$Tags, |
| | | 86 | | |
| | | 87 | | [Parameter(Mandatory = $false, ParameterSetName = 'Items')] |
| | | 88 | | [ValidatePattern('^(default|\\d{3})$')] |
| | | 89 | | [string]$StatusCode, |
| | | 90 | | |
| | | 91 | | [Parameter(Mandatory = $false, ParameterSetName = 'Items')] |
| | | 92 | | [string]$ResponseDescription, |
| | | 93 | | |
| | | 94 | | [Parameter(Mandatory = $false, ParameterSetName = 'Items')] |
| | | 95 | | [object]$ItemSchemaType = [string], |
| | | 96 | | |
| | | 97 | | [Parameter(Mandatory = $false, ParameterSetName = 'ItemsSkipOpenApi')] |
| | | 98 | | [switch]$SkipOpenApi, |
| | | 99 | | |
| | | 100 | | [Parameter(Mandatory = $true, ParameterSetName = 'Options')] |
| | | 101 | | [Kestrun.Hosting.Options.SseBroadcastOptions]$Options, |
| | | 102 | | |
| | | 103 | | [Parameter()] |
| | | 104 | | [switch]$PassThru |
| | | 105 | | ) |
| | | 106 | | begin { |
| | 0 | 107 | | $Server = Resolve-KestrunServer -Server $Server |
| | | 108 | | } |
| | | 109 | | process { |
| | 0 | 110 | | if ($PSCmdlet.ParameterSetName -eq 'Items') { |
| | 0 | 111 | | $Options = [Kestrun.Hosting.Options.SseBroadcastOptions]::new() |
| | 0 | 112 | | if ( $SkipOpenApi.IsPresent ) { |
| | 0 | 113 | | $Options.SkipOpenApi = $true |
| | | 114 | | }# Set the documentation IDs for the SSE broadcast endpoint |
| | 0 | 115 | | $Options.DocId = $DocId |
| | | 116 | | |
| | | 117 | | # Set the path for the SSE broadcast endpoint |
| | 0 | 118 | | if ($PSBoundParameters.ContainsKey('Path')) { $Options.Path = $Path } |
| | 0 | 119 | | if ($PSBoundParameters.ContainsKey('KeepAliveSeconds')) { $Options.KeepAliveSeconds = $KeepAliveSeconds } |
| | 0 | 120 | | if ($PSBoundParameters.ContainsKey('OperationId')) { $Options.OperationId = $OperationId } |
| | 0 | 121 | | if ($PSBoundParameters.ContainsKey('Summary')) { $Options.Summary = $Summary } |
| | 0 | 122 | | if ($PSBoundParameters.ContainsKey('Description')) { $Options.Description = $Description } |
| | 0 | 123 | | if ($PSBoundParameters.ContainsKey('Tags')) { $Options.Tags = $Tags } |
| | 0 | 124 | | if ($PSBoundParameters.ContainsKey('StatusCode')) { $Options.StatusCode = $StatusCode } |
| | 0 | 125 | | if ($PSBoundParameters.ContainsKey('ResponseDescription')) { $Options.ResponseDescription = $ResponseDescrip |
| | 0 | 126 | | if ($PSBoundParameters.ContainsKey('ItemSchemaType')) { |
| | 0 | 127 | | $Options.ItemSchemaType = $ItemSchemaType |
| | | 128 | | } |
| | | 129 | | } |
| | | 130 | | |
| | 0 | 131 | | $Server.AddSseBroadcast($Options) | Out-Null |
| | | 132 | | |
| | 0 | 133 | | if ($PassThru.IsPresent) { |
| | 0 | 134 | | return $Server |
| | | 135 | | } |
| | | 136 | | } |
| | | 137 | | } |