< Summary - Kestrun — Combined Coverage

Information
Class: Public.SharedState.Import-KrSharedState
Assembly: Kestrun.PowerShell.Public
File(s): /home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/SharedState/Import-KrSharedState.ps1
Tag: Kestrun/Kestrun@971dd53fc1f17b61ce476aa4cec36c172d4f02e4
Line coverage
77%
Covered lines: 21
Uncovered lines: 6
Coverable lines: 27
Total lines: 134
Line coverage: 77.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 04/08/2026 - 23:41:36 Line coverage: 77.7% (21/27) Total lines: 134 Tag: Kestrun/Kestrun@971dd53fc1f17b61ce476aa4cec36c172d4f02e4

Metrics

File(s)

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

#LineLine coverage
 1<#
 2.SYNOPSIS
 3    Imports a PowerShell object from a serialized XML representation.
 4
 5.DESCRIPTION
 6    Import-KrSharedState deserializes a PowerShell object that was previously
 7    created by Export-KrSharedState. The serialized content can come from a
 8    string, a byte array, or a file.
 9
 10    Access is synchronized through a shared lock so that callers using the
 11    same lock deserialize shared state in a thread-safe manner within the
 12    current process.
 13
 14.PARAMETER InputString
 15    The serialized XML content as a string.
 16    If the string is null or empty, Import-KrSharedState returns null.
 17
 18.PARAMETER InputBytes
 19    The serialized XML content as a byte array.
 20    If the byte array is null or empty, Import-KrSharedState returns null.
 21
 22.PARAMETER Path
 23    The file path containing the serialized XML content.
 24    If the path is null, empty, or whitespace, Import-KrSharedState returns null.
 25    If the file does not exist, an error is thrown.
 26
 27.PARAMETER Lock
 28    The semaphore used to synchronize access to shared state. If not provided,
 29    the default shared-state lock is used.
 30
 31.PARAMETER TimeoutMilliseconds
 32    The maximum time to wait for the lock. Use -1 to wait indefinitely.
 33
 34.PARAMETER Encoding
 35    The text encoding used when converting bytes to text or reading from a file.
 36
 37.EXAMPLE
 38    $state = Import-KrSharedState -InputString $xml
 39
 40.EXAMPLE
 41    $state = Import-KrSharedState -InputBytes $bytes
 42
 43.EXAMPLE
 44    $state = Import-KrSharedState -Path '.\state.xml'
 45
 46.EXAMPLE
 47    $lock = Get-KrLock 'sharedstate:cache'
 48    $state = Import-KrSharedState -InputString $xml -Lock $lock
 49#>
 50function Import-KrSharedState {
 51    [KestrunRuntimeApi('Everywhere')]
 52    [CmdletBinding(DefaultParameterSetName = 'FromString')]
 53    param(
 54        [Parameter(ParameterSetName = 'FromString', Mandatory, Position = 0)]
 55        [AllowEmptyString()]
 56        [string]$InputString,
 57
 58        [Parameter(ParameterSetName = 'FromBytes', Mandatory, Position = 0)]
 59        [byte[]]$InputBytes,
 60
 61        [Parameter(ParameterSetName = 'FromFile', Mandatory, Position = 0)]
 62        [string]$Path,
 63
 64        [Parameter()]
 65        [System.Threading.SemaphoreSlim]$Lock,
 66
 67        [Parameter()]
 68        [int]$TimeoutMilliseconds = 30000,
 69
 70        [Parameter()]
 71        [System.Text.Encoding]$Encoding = [System.Text.Encoding]::UTF8
 72    )
 73
 174    $stateLock = $null
 175    $lockTaken = $false
 76
 77    try {
 78        # Resolve lock
 279        $stateLock = ($Lock)? $Lock : [Kestrun.Utilities.KestrunLockRegistry]::Default
 80
 81        # Acquire
 182        if ($TimeoutMilliseconds -lt 0) {
 083            $stateLock.Wait()
 084            $lockTaken = $true
 85        } else {
 186            $lockTaken = $stateLock.Wait($TimeoutMilliseconds)
 187            if (-not $lockTaken) {
 188                throw 'Timeout waiting for shared state lock.'
 89            }
 90        }
 91
 192        switch ($PSCmdlet.ParameterSetName) {
 93            'FromString' {
 194                if ([string]::IsNullOrEmpty($InputString)) {
 095                    return $null
 96                }
 97
 198                return [System.Management.Automation.PSSerializer]::Deserialize($InputString)
 99            }
 100
 101            'FromBytes' {
 1102                if ($null -eq $InputBytes -or $InputBytes.Length -eq 0) {
 0103                    return $null
 104                }
 105
 1106                $xml = $Encoding.GetString($InputBytes)
 1107                return [System.Management.Automation.PSSerializer]::Deserialize($xml)
 108            }
 109
 110            'FromFile' {
 1111                if ([string]::IsNullOrWhiteSpace($Path)) {
 0112                    return $null
 113                }
 114
 1115                $fullPath = [System.IO.Path]::GetFullPath($Path)
 116
 2117                if (-not (Test-Path -LiteralPath $fullPath)) {
 1118                    throw "File not found: $fullPath"
 119                }
 120
 1121                $xml = [System.IO.File]::ReadAllText($fullPath, $Encoding)
 1122                return [System.Management.Automation.PSSerializer]::Deserialize($xml)
 123            }
 124        }
 125    } finally {
 1126        if ($stateLock -and $lockTaken) {
 127            try {
 1128                $null = $stateLock.Release()
 129            } catch {
 0130                Write-KrLog -Level Verbose -Message 'Failed to release shared state lock' -ErrorRecord $_
 131            }
 132        }
 133    }
 134}

Methods/Properties

Import-KrSharedState()