< Summary - Kestrun — Combined Coverage

Information
Class: Public.SharedState.Set-KrSharedState
Assembly: Kestrun.PowerShell.Public
File(s): /home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/SharedState/Set-KrSharedState.ps1
Tag: Kestrun/Kestrun@5f1d2b981c9d7292c11fd448428c6ab6c811c5de
Line coverage
85%
Covered lines: 6
Uncovered lines: 1
Coverable lines: 7
Total lines: 80
Line coverage: 85.7%
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 11/19/2025 - 17:40:50 Line coverage: 57.1% (4/7) Total lines: 79 Tag: Kestrun/Kestrun@fcf33342333cef0516fe0d0912a86709874fd02604/08/2026 - 23:41:36 Line coverage: 85.7% (6/7) Total lines: 80 Tag: Kestrun/Kestrun@971dd53fc1f17b61ce476aa4cec36c172d4f02e4

Coverage delta

Coverage delta 29 -29

Metrics

File(s)

/home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/SharedState/Set-KrSharedState.ps1

#LineLine coverage
 1<#
 2.SYNOPSIS
 3    Defines or updates a global variable accessible across Kestrun scripts.
 4
 5.DESCRIPTION
 6    Stores a value in the Kestrun global variable table. Variables may be marked
 7    as read-only to prevent accidental modification.
 8    If the variable already exists, its value is updated. If it does not exist,
 9    it is created.
 10.PARAMETER Server
 11    The Kestrun server instance.
 12.PARAMETER Global
 13    If specified, the variable is stored in the global shared state.
 14.PARAMETER Name
 15    Name of the variable to create or update.
 16.PARAMETER Value
 17    Value to assign to the variable.
 18.PARAMETER AllowsValueType
 19    If specified, allows the variable to hold value types (e.g., int, bool).
 20.PARAMETER ThreadSafe
 21    If specified, the value is converted to a thread-safe representation.
 22.EXAMPLE
 23    Set-KrSharedState -Name "MyVariable" -Value "Hello, World!"
 24    This creates a global variable "MyVariable" with the value "Hello, World!".
 25.EXAMPLE
 26    Set-KrSharedState -Name "MyNamespace.MyVariable" -Value @{item=42}
 27    This creates a global variable "MyNamespace.MyVariable" with the value @{item=42}.
 28.NOTES
 29    This function is part of the Kestrun.SharedState module and is used to define or update global variables.
 30#>
 31function Set-KrSharedState {
 32    [KestrunRuntimeApi('Definition')]
 33    [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseShouldProcessForStateChangingFunctions', '')]
 34    [CmdletBinding(defaultParameterSetName = 'Server')]
 35    param(
 36        [Parameter(ValueFromPipeline = $true, ParameterSetName = 'Server')]
 37        [Kestrun.Hosting.KestrunHost]$Server,
 38
 39        [Parameter(Mandatory = $true, ParameterSetName = 'Global')]
 40        [switch]$Global,
 41
 42        [Parameter(Mandatory)]
 43        [string]$Name,
 44
 45        [Parameter(Mandatory)]
 46        [object]$Value,
 47
 48        [Parameter()]
 49        [switch]$AllowsValueType,
 50
 51        [Parameter()]
 52        [switch]$ThreadSafe
 53
 54    )
 55    begin {
 156        if (-not $Global.IsPresent) {
 57            # Ensure the server instance is resolved
 158            $Server = Resolve-KestrunServer -Server $Server
 59        }
 60    }
 61    process {
 162        if ($ThreadSafe.IsPresent) {
 063            $Value = ConvertTo-KrThreadSafeValue -Value $Value
 64        }
 165        if ($Global.IsPresent) {
 66            # Store in the global shared-state registry.
 167            $null = [Kestrun.SharedState.GlobalStore]::Set($Name,
 68                $Value,
 69                $AllowsValueType.IsPresent  # Allow value types if specified
 70            )
 71            return
 72        }
 73        # Define or update the variable; throws if it was already read-only
 174        $null = $Server.SharedState.Set(
 75            $Name,
 76            $Value,
 77            $AllowsValueType.IsPresent  # Allow value types if specified
 78        )
 79    }
 80}

Methods/Properties

Set-KrSharedState()