< Summary - Kestrun — Combined Coverage

Information
Class: Public.SharedState.Export-KrSharedState
Assembly: Kestrun.PowerShell.Public
File(s): /home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/SharedState/Export-KrSharedState.ps1
Tag: Kestrun/Kestrun@971dd53fc1f17b61ce476aa4cec36c172d4f02e4
Line coverage
83%
Covered lines: 25
Uncovered lines: 5
Coverable lines: 30
Total lines: 149
Line coverage: 83.3%
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 04/08/2026 - 23:41:36 Line coverage: 83.3% (25/30) Total lines: 149 Tag: Kestrun/Kestrun@971dd53fc1f17b61ce476aa4cec36c172d4f02e4

Metrics

File(s)

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

#LineLine coverage
 1<#
 2.SYNOPSIS
 3    Exports a PowerShell object to a serialized XML representation.
 4
 5.DESCRIPTION
 6    Export-KrSharedState serializes a PowerShell object using
 7    [System.Management.Automation.PSSerializer] and returns the serialized
 8    data as a string, as a byte array, or writes it to a file.
 9
 10    Access is synchronized through a shared lock so that callers using the
 11    same lock serialize shared state in a thread-safe manner within the
 12    current process.
 13
 14.PARAMETER InputObject
 15    The object to serialize.
 16
 17.PARAMETER Path
 18    The destination file path. When provided, Export-KrSharedState writes the
 19    serialized XML to this file and returns the written file.
 20
 21.PARAMETER OutputType
 22    Specifies how the serialized XML is returned:
 23    - String
 24    - ByteArray
 25    - File
 26
 27    When Path is provided, the effective output type is File. Supplying Path
 28    with a different OutputType value results in an error.
 29
 30.PARAMETER Lock
 31    The semaphore used to synchronize access to shared state. If not provided,
 32    the default shared-state lock is used.
 33
 34.PARAMETER TimeoutMilliseconds
 35    The maximum time to wait for the lock. Use -1 to wait indefinitely.
 36
 37.PARAMETER Encoding
 38    The text encoding used when converting to bytes or writing to a file.
 39
 40.EXAMPLE
 41    $xml = Export-KrSharedState -InputObject $state
 42
 43.EXAMPLE
 44    $bytes = Export-KrSharedState -InputObject $state -OutputType ByteArray
 45
 46.EXAMPLE
 47    Export-KrSharedState -InputObject $state -OutputType File -Path '.\state.xml'
 48
 49.EXAMPLE
 50    Export-KrSharedState -InputObject $state -Path '.\state.xml'
 51
 52.EXAMPLE
 53    $lock = Get-KrLock 'sharedstate:cache'
 54    Export-KrSharedState -InputObject $state -Lock $lock
 55#>
 56function Export-KrSharedState {
 57    [KestrunRuntimeApi('Everywhere')]
 58    [OutputType([string], [byte[]], [System.IO.FileInfo])]
 59    [CmdletBinding(DefaultParameterSetName = 'ToString')]
 60    param(
 61        [Parameter(Mandatory, Position = 0)]
 62        [AllowNull()]
 63        [object]$InputObject,
 64
 65        [Parameter(ParameterSetName = 'ToFile', Mandatory)]
 66        [string]$Path,
 67
 68        [Parameter()]
 69        [ValidateSet('String', 'ByteArray', 'File')]
 70        [string]$OutputType = 'String',
 71
 72        [Parameter()]
 73        [System.Threading.SemaphoreSlim]$Lock,
 74
 75        [Parameter()]
 76        [int]$TimeoutMilliseconds = 30000,
 77
 78        [Parameter()]
 79        [System.Text.Encoding]$Encoding = [System.Text.Encoding]::UTF8
 80    )
 81
 182    $stateLock = $null
 183    $lockTaken = $false
 184    $resolvedOutputType = $OutputType
 85
 86    try {
 187        if ($PSCmdlet.ParameterSetName -eq 'ToFile') {
 188            if ($PSBoundParameters.ContainsKey('OutputType') -and $OutputType -ne 'File') {
 189                throw "OutputType '$OutputType' cannot be used when Path is provided."
 90            }
 91
 192            $resolvedOutputType = 'File'
 93        }
 94
 95        # Resolve lock
 296        $stateLock = ($Lock)? $Lock : [Kestrun.Utilities.KestrunLockRegistry]::Default
 97
 98        # Acquire lock
 199        if ($TimeoutMilliseconds -lt 0) {
 0100            $stateLock.Wait()
 0101            $lockTaken = $true
 102        } else {
 1103            $lockTaken = $stateLock.Wait($TimeoutMilliseconds)
 1104            if (-not $lockTaken) {
 1105                throw 'Timeout waiting for shared state lock.'
 106            }
 107        }
 108
 1109        $xml = [System.Management.Automation.PSSerializer]::Serialize($InputObject)
 110
 1111        switch ($resolvedOutputType) {
 112            'String' {
 1113                return $xml
 114            }
 115
 116            'ByteArray' {
 1117                return $Encoding.GetBytes($xml)
 118            }
 119
 120            'File' {
 1121                if ([string]::IsNullOrWhiteSpace($Path)) {
 0122                    throw "Path is required when OutputType is 'File'."
 123                }
 124
 1125                $fullPath = [System.IO.Path]::GetFullPath($Path)
 1126                $directory = [System.IO.Path]::GetDirectoryName($fullPath)
 127
 2128                if (-not [string]::IsNullOrWhiteSpace($directory) -and -not (Test-Path -LiteralPath $directory)) {
 2129                    [System.IO.Directory]::CreateDirectory($directory) | Out-Null
 130                }
 131
 1132                [System.IO.File]::WriteAllText($fullPath, $xml, $Encoding)
 1133                return Get-Item -LiteralPath $fullPath
 134            }
 135
 136            default {
 0137                throw "Unsupported OutputType '$resolvedOutputType'."
 138            }
 139        }
 140    } finally {
 1141        if ($stateLock -and $lockTaken) {
 142            try {
 1143                $null = $stateLock.Release()
 144            } catch {
 0145                Write-KrLog -Level Verbose -Message 'Failed to release shared state lock' -ErrorRecord $_
 146            }
 147        }
 148    }
 149}

Methods/Properties

Export-KrSharedState()