Skip to content

Commit 8e1be05

Browse files
author
James Brundage
committed
feat: Get-WebSocket -Filter ( Fixes #42 )
1 parent f2c26f0 commit 8e1be05

1 file changed

Lines changed: 47 additions & 9 deletions

File tree

Commands/Get-WebSocket.ps1

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ function Get-WebSocket {
165165
# If set, will output the raw text that comes out of the WebSocket.
166166
[Alias('Raw')]
167167
[switch]
168-
$RawText,
168+
$RawText,
169169

170170
# If set, will output the raw bytes that come out of the WebSocket.
171171
[Alias('RawByte','RawBytes','Bytes','Byte')]
@@ -175,6 +175,14 @@ function Get-WebSocket {
175175
# The subprotocol used by the websocket. If not provided, this will default to `json`.
176176
[string]
177177
$SubProtocol,
178+
179+
# One or more filters to apply to the output of the WebSocket.
180+
# These can be strings, regexes, scriptblocks, or commands.
181+
# If they are strings or regexes, they will be applied to the raw text.
182+
# If they are scriptblocks, they will be applied to the deserialized JSON.
183+
# These filters will be run within the WebSocket job.
184+
[PSObject[]]
185+
$Filter,
178186

179187
# If set, will watch the output of a WebSocket job for one or more conditions.
180188
# The conditions are the keys of the dictionary, and can be a regex, a string, or a scriptblock.
@@ -286,8 +294,9 @@ function Get-WebSocket {
286294
$Variable.WebSocket = $ws
287295

288296
$MessageCount = [long]0
289-
290-
while ($true) {
297+
$FilteredCount = [long]0
298+
299+
:WebSocketMessageLoop while ($true) {
291300
if ($ws.State -ne 'Open') {break }
292301
if ($TimeOut -and ([DateTime]::Now - $webSocketStartTime) -gt $TimeOut) {
293302
$ws.CloseAsync([Net.WebSockets.WebSocketCloseStatus]::NormalClosure, 'Timeout', $CT).Wait()
@@ -308,12 +317,38 @@ function Get-WebSocket {
308317
$webSocketMessage =
309318
if ($Binary) {
310319
$Buf -gt 0
311-
} elseif ($RawText) {
312-
$OutputEncoding.GetString($Buf, 0, $Buf.Count)
313320
} else {
314-
$JS = $OutputEncoding.GetString($Buf, 0, $Buf.Count)
315-
if ([string]::IsNullOrWhitespace($JS)) { continue }
316-
ConvertFrom-Json $JS
321+
$messageString = $OutputEncoding.GetString($Buf, 0, $Buf.Count)
322+
if ($Filter) {
323+
foreach ($fil in $filter) {
324+
if ($fil -is [string] -and $messageString -like "*$fil*") {
325+
$FilteredCount++
326+
continue WebSocketMessageLoop
327+
}
328+
if ($fil -is [regex] -and $fil.IsMatch($messageString)) {
329+
$FilteredCount++
330+
continue WebSocketMessageLoop
331+
}
332+
}
333+
}
334+
if ($RawText) {
335+
$messageString
336+
} else {
337+
$MessageObject = ConvertFrom-Json -InputObject $messageString
338+
if ($filter) {
339+
foreach ($fil in $Filter) {
340+
if ($fil -is [ScriptBlock] -or
341+
$fil -is [Management.Automation.CommandInfo]
342+
) {
343+
if (& $fil $MessageObject) {
344+
$FilteredCount++
345+
continue WebSocketMessageLoop
346+
}
347+
}
348+
}
349+
}
350+
$MessageObject
351+
}
317352
}
318353
if ($PSTypeName) {
319354
$webSocketMessage.pstypenames.clear()
@@ -351,14 +386,17 @@ function Get-WebSocket {
351386
}
352387

353388
process {
389+
# First, let's pack all of the parameters into a dictionary of variables.
354390
foreach ($keyValuePair in $PSBoundParameters.GetEnumerator()) {
355391
$Variable[$keyValuePair.Key] = $keyValuePair.Value
356392
}
393+
# If `-Debug` was passed,
357394
if ($DebugPreference -notin 'SilentlyContinue','Ignore') {
395+
# run the job in the current scope (so we can debug it).
358396
. $SocketJob -Variable $Variable
359397
return
360398
}
361-
$webSocketJob =
399+
$webSocketJob =
362400
if ($WebSocketUri) {
363401
if (-not $name) {
364402
$Name = $WebSocketUri

0 commit comments

Comments
 (0)