Skip to content

Commit

Permalink
Merge pull request #1424 from mdaneri/remove-duplicated-functions
Browse files Browse the repository at this point in the history
Refactor Byte Conversion Functions and Remove Duplicate Task Process Definitions
  • Loading branch information
Badgerati authored Oct 18, 2024
2 parents 696cc43 + d8606d3 commit a181172
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 132 deletions.
45 changes: 3 additions & 42 deletions src/Private/Streams.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ function Get-PodeByteLinesFromByteArray {
Converts a stream to a byte array.
.DESCRIPTION
The `ConvertFrom-PodeValueToByteArray` function reads data from a stream and converts it to a byte array.
The `ConvertFrom-PodeStreamToByteArray` function reads data from a stream and converts it to a byte array.
It's useful for scenarios where you need to work with binary data from a stream.
.PARAMETER Stream
Expand All @@ -111,13 +111,13 @@ function Get-PodeByteLinesFromByteArray {
# Example usage:
# Read data from a file stream and convert it to a byte array
$stream = [System.IO.File]::OpenRead("C:\path\to\file.bin")
$byteArray = ConvertFrom-PodeValueToByteArray -Stream $stream
$byteArray = ConvertFrom-PodeStreamToByteArray -Stream $stream
$stream.Close()
.NOTES
This is an internal function and may change in future releases of Pode.
#>
function ConvertFrom-PodeValueToByteArray {
function ConvertFrom-PodeStreamToByteArray {
param(
[Parameter(Mandatory = $true)]
$Stream
Expand All @@ -137,45 +137,6 @@ function ConvertFrom-PodeValueToByteArray {
$ms.Close()
return $ms.ToArray()
}
<#
.SYNOPSIS
Converts a string value to a byte array using the specified encoding.
.DESCRIPTION
The `ConvertFrom-PodeValueToByteArray` function takes a string value and converts it to a byte array.
You can specify the desired encoding (default is UTF-8).
.PARAMETER Value
Specifies the input string value to convert.
.PARAMETER Encoding
Specifies the encoding to use when converting the string to bytes.
Default value is UTF-8.
.OUTPUTS
Returns a byte array containing the encoded representation of the input string.
.EXAMPLE
# Example usage:
$inputString = "Hello, world!"
$byteArray = ConvertFrom-PodeValueToByteArray -Value $inputString
# Now you can work with the byte array as needed.
.NOTES
This is an internal function and may change in future releases of Pode.
#>
function ConvertFrom-PodeValueToByteArray {
param(
[Parameter()]
[string]
$Value,

[Parameter()]
$Encoding = [System.Text.Encoding]::UTF8
)

return $Encoding.GetBytes($Value)
}

function ConvertFrom-PodeBytesToString {
param(
Expand Down
2 changes: 1 addition & 1 deletion src/Public/Responses.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ function Write-PodeTextResponse {
else {
# convert string to bytes
if ($isStringValue) {
$Bytes = ConvertFrom-PodeValueToByteArray -Value $Value
$Bytes = [System.Text.Encoding]::UTF8.GetBytes($Value)
}

# check if we only need a range of the bytes
Expand Down
89 changes: 0 additions & 89 deletions src/Public/Tasks.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -486,95 +486,6 @@ function Wait-PodeTask {
}
}

<#
.SYNOPSIS
Get all Task Processes.
.DESCRIPTION
Get all Task Processes, with support for filtering. These are the processes created when using Invoke-PodeTask.
.PARAMETER Name
An optional Name of the Task to filter by, can be one or more.
.PARAMETER Id
An optional ID of the Task process to filter by, can be one or more.
.PARAMETER State
An optional State of the Task process to filter by, can be one or more.
.EXAMPLE
Get-PodeTaskProcess
.EXAMPLE
Get-PodeTaskProcess -Name 'TaskName'
.EXAMPLE
Get-PodeTaskProcess -Id 'TaskId'
.EXAMPLE
Get-PodeTaskProcess -State 'Running'
#>
function Get-PodeTaskProcess {
[CmdletBinding()]
param(
[Parameter()]
[string[]]
$Name,

[Parameter()]
[string[]]
$Id,

[Parameter()]
[ValidateSet('All', 'Pending', 'Running', 'Completed', 'Failed')]
[string[]]
$State = 'All'
)

$processes = $PodeContext.Tasks.Processes.Values

# filter processes by name
if (($null -ne $Name) -and ($Name.Length -gt 0)) {
$processes = @(foreach ($_name in $Name) {
foreach ($process in $processes) {
if ($process.Task -ine $_name) {
continue
}

$process
}
})
}

# filter processes by id
if (($null -ne $Id) -and ($Id.Length -gt 0)) {
$processes = @(foreach ($_id in $Id) {
foreach ($process in $processes) {
if ($process.ID -ine $_id) {
continue
}

$process
}
})
}

# filter processes by status
if ($State -inotcontains 'All') {
$processes = @(foreach ($process in $processes) {
if ($State -inotcontains $process.State) {
continue
}

$process
})
}

# return processes
return $processes
}


<#
.SYNOPSIS
Get all Task Processes.
Expand Down

0 comments on commit a181172

Please sign in to comment.