From 8c1c4de3eb9e73eb95eb9f6c120913564d047a7c Mon Sep 17 00:00:00 2001 From: James Brundage <+@noreply.github.com> Date: Fri, 6 Dec 2024 16:25:54 -0800 Subject: [PATCH 01/15] feat: Get-WebSocket -PSTypeName ( Fixes #34 ) --- Commands/Get-WebSocket.ps1 | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/Commands/Get-WebSocket.ps1 b/Commands/Get-WebSocket.ps1 index 45e28eb..d596788 100644 --- a/Commands/Get-WebSocket.ps1 +++ b/Commands/Get-WebSocket.ps1 @@ -173,6 +173,12 @@ function Get-WebSocket { [TimeSpan] $TimeOut, + # If provided, will decorate the objects outputted from a websocket job. + # This will only decorate objects converted from JSON. + [Alias('PSTypeNames','Decorate','Decoration')] + [string[]] + $PSTypeName, + # The maximum number of messages to receive before closing the WebSocket. [long] $Maximum, @@ -256,6 +262,13 @@ function Get-WebSocket { if ([string]::IsNullOrWhitespace($JS)) { continue } ConvertFrom-Json $JS } + if ($PSTypeName) { + $webSocketMessage.pstypenames.clear() + [Array]::Reverse($PSTypeName) + foreach ($psType in $psTypeName) { + $webSocketMessage.pstypenames.add($psType) + } + } if ($handler) { $psCmd = if ($runspace.LanguageMode -eq 'NoLanguage' -or From db1771d28321515f2aa8638097ab2b7fedd0120c Mon Sep 17 00:00:00 2001 From: StartAutomating Date: Sat, 7 Dec 2024 00:27:05 +0000 Subject: [PATCH 02/15] feat: Get-WebSocket -PSTypeName ( Fixes #34 ) --- docs/Get-WebSocket.md | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/docs/Get-WebSocket.md b/docs/Get-WebSocket.md index 5ad9b74..6dcf6d8 100644 --- a/docs/Get-WebSocket.md +++ b/docs/Get-WebSocket.md @@ -231,6 +231,14 @@ The timeout for the WebSocket connection. If this is provided, after the timeou |------------|--------|--------|-------------| |`[TimeSpan]`|false |named |false | +#### **PSTypeName** +If provided, will decorate the objects outputted from a websocket job. +This will only decorate objects converted from JSON. + +|Type |Required|Position|PipelineInput|Aliases | +|------------|--------|--------|-------------|---------------------------------------| +|`[String[]]`|false |named |false |PSTypeNames
Decorate
Decoration| + #### **Maximum** The maximum number of messages to receive before closing the WebSocket. @@ -266,5 +274,5 @@ RunspacePools allow you to limit the scope of the handler to a pool of runspaces ### Syntax ```PowerShell -Get-WebSocket [[-WebSocketUri] ] [-Handler ] [-Variable ] [-Name ] [-InitializationScript ] [-BufferSize ] [-OnConnect ] [-OnError ] [-OnOutput ] [-OnWarning ] [-Watch] [-RawText] [-Binary] [-WatchFor ] [-TimeOut ] [-Maximum ] [-ConnectionTimeout ] [-Runspace ] [-RunspacePool ] [] +Get-WebSocket [[-WebSocketUri] ] [-Handler ] [-Variable ] [-Name ] [-InitializationScript ] [-BufferSize ] [-OnConnect ] [-OnError ] [-OnOutput ] [-OnWarning ] [-Watch] [-RawText] [-Binary] [-WatchFor ] [-TimeOut ] [-PSTypeName ] [-Maximum ] [-ConnectionTimeout ] [-Runspace ] [-RunspacePool ] [] ``` From e53530acb546e2f7d8db5f9d49617d708f8146ed Mon Sep 17 00:00:00 2001 From: StartAutomating Date: Sat, 7 Dec 2024 00:27:06 +0000 Subject: [PATCH 03/15] feat: Get-WebSocket -PSTypeName ( Fixes #34 ) --- docs/_layouts/Default.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/_layouts/Default.html b/docs/_layouts/Default.html index 9d0f2cb..9d96e1c 100644 --- a/docs/_layouts/Default.html +++ b/docs/_layouts/Default.html @@ -24,4 +24,4 @@ {{content}} {% include Footer.html %} - + \ No newline at end of file From ab1f6298beb6126f9b5becf51b5cae469f48a401 Mon Sep 17 00:00:00 2001 From: James Brundage Date: Sat, 7 Dec 2024 00:27:10 +0000 Subject: [PATCH 04/15] feat: Get-WebSocket -PSTypeName ( Fixes #34 ) --- docs/_data/LastDateBuilt.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/_data/LastDateBuilt.json b/docs/_data/LastDateBuilt.json index 8dd806a..5a540ad 100644 --- a/docs/_data/LastDateBuilt.json +++ b/docs/_data/LastDateBuilt.json @@ -1 +1 @@ -"2024-12-04" \ No newline at end of file +"2024-12-07" \ No newline at end of file From eeac693c5ca652db107e175c1a60cd95263e4368 Mon Sep 17 00:00:00 2001 From: James Brundage Date: Sat, 7 Dec 2024 00:27:10 +0000 Subject: [PATCH 05/15] feat: Get-WebSocket -PSTypeName ( Fixes #34 ) --- docs/_layouts/Default.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/_layouts/Default.html b/docs/_layouts/Default.html index 9d96e1c..9d0f2cb 100644 --- a/docs/_layouts/Default.html +++ b/docs/_layouts/Default.html @@ -24,4 +24,4 @@ {{content}} {% include Footer.html %} - \ No newline at end of file + From 408967e20f9336cb7bd5907a2cbad42002ba6472 Mon Sep 17 00:00:00 2001 From: James Brundage <+@noreply.github.com> Date: Thu, 19 Dec 2024 20:50:44 -0800 Subject: [PATCH 06/15] chore: Vestigial File Cleanup ( Fixes #33 ) --- _config.yml | 12 ------------ _layouts/Default.html | 27 --------------------------- 2 files changed, 39 deletions(-) delete mode 100644 _config.yml delete mode 100644 _layouts/Default.html diff --git a/_config.yml b/_config.yml deleted file mode 100644 index f894bca..0000000 --- a/_config.yml +++ /dev/null @@ -1,12 +0,0 @@ - -title: WebSocket -description: Work with WebSockets in PowerShell -url: https://websocket.powershellweb.com -permalink: pretty -palette: Konsolas -analyticsId: G-R5C30737B2 -googleFont: Noto Sans -stylesheet: https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css -defaults: - - values: - layout: Default diff --git a/_layouts/Default.html b/_layouts/Default.html deleted file mode 100644 index 9d96e1c..0000000 --- a/_layouts/Default.html +++ /dev/null @@ -1,27 +0,0 @@ ---- - -title: Default.html ---- - - - - - - - {% include GoogleAnalytics.html %} - {% include ImportMap.html %} - {% include OpenGraph.html %} - {% include GoogleFont.html %} - {% include 4bitcss.html %} - {% include Margin.html %} - {% include Stylesheet.html %} - {% include Htmx.html %} - - - -{% include Menu.html %} - -{{content}} - -{% include Footer.html %} - \ No newline at end of file From da697e1599b61bc52626779a8649fe333bbe6a26 Mon Sep 17 00:00:00 2001 From: James Brundage Date: Fri, 20 Dec 2024 04:52:17 +0000 Subject: [PATCH 07/15] chore: Vestigial File Cleanup ( Fixes #33 ) --- docs/_data/LastDateBuilt.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/_data/LastDateBuilt.json b/docs/_data/LastDateBuilt.json index 5a540ad..a7d8667 100644 --- a/docs/_data/LastDateBuilt.json +++ b/docs/_data/LastDateBuilt.json @@ -1 +1 @@ -"2024-12-07" \ No newline at end of file +"2024-12-20" \ No newline at end of file From 123d554358eae45aff02a59d5665b295f44866ac Mon Sep 17 00:00:00 2001 From: James Brundage <+@noreply.github.com> Date: Thu, 19 Dec 2024 21:30:58 -0800 Subject: [PATCH 08/15] feat: WebSocket reuse ( Fixes #35 ) --- Commands/Get-WebSocket.ps1 | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/Commands/Get-WebSocket.ps1 b/Commands/Get-WebSocket.ps1 index d596788..75010ef 100644 --- a/Commands/Get-WebSocket.ps1 +++ b/Commands/Get-WebSocket.ps1 @@ -84,7 +84,7 @@ function Get-WebSocket { {$args.commit.record.text -match '[\p{IsHighSurrogates}\p{IsLowSurrogates}]+'}={ $matches.0 } - } + } #> [CmdletBinding(PositionalBinding=$false)] [Alias('WebSocket')] @@ -214,7 +214,7 @@ function Get-WebSocket { if (-not $WebSocketUri.Scheme) { $WebSocketUri = [uri]"wss://$WebSocketUri" - } + } if (-not $BufferSize) { $BufferSize = 16kb @@ -306,8 +306,22 @@ function Get-WebSocket { if (-not $name) { $Name = $WebSocketUri } - - Start-ThreadJob -ScriptBlock $SocketJob -Name $Name -InitializationScript $InitializationScript -ArgumentList $Variable + + $existingJob = foreach ($jobWithThisName in (Get-Job -Name $Name)) { + if ( + $jobWithThisName.State -in 'Running','NotStarted' -and + $jobWithThisName.WebSocket -is [Net.WebSockets.ClientWebSocket] + ) { + $jobWithThisName + break + } + } + + if ($existingJob) { + $existingJob + } else { + Start-ThreadJob -ScriptBlock $SocketJob -Name $Name -InitializationScript $InitializationScript -ArgumentList $Variable + } } elseif ($WebSocket) { if (-not $name) { $name = "websocket" From fff034459e0518f3608ce1551a28591d9a26900f Mon Sep 17 00:00:00 2001 From: James Brundage <+@noreply.github.com> Date: Thu, 19 Dec 2024 21:33:55 -0800 Subject: [PATCH 09/15] docs: WebSocket decoration example ( re #34 ) --- Commands/Get-WebSocket.ps1 | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/Commands/Get-WebSocket.ps1 b/Commands/Get-WebSocket.ps1 index 75010ef..a2635c8 100644 --- a/Commands/Get-WebSocket.ps1 +++ b/Commands/Get-WebSocket.ps1 @@ -84,7 +84,25 @@ function Get-WebSocket { {$args.commit.record.text -match '[\p{IsHighSurrogates}\p{IsLowSurrogates}]+'}={ $matches.0 } - } + } + .EXAMPLE + # We can decorate a type returned from a WebSocket, allowing us to add additional properties. + + # For example, let's add a `Tags` property to the `app.bsky.feed.post` type. + $typeName = 'app.bsky.feed.post' + Update-TypeData -TypeName $typeName -MemberName 'Tags' -MemberType ScriptProperty -Value { + @($this.commit.record.facets.features.tag) + } -Force + + # Now, let's get 10kb posts ( this should not take too long ) + $somePosts = + websocket "wss://jetstream2.us-west.bsky.network/subscribe?wantedCollections=$typeName" -PSTypeName $typeName -Maximum 10kb -Watch + $somePosts | + ? Tags | + Select -ExpandProperty Tags | + Group | + Sort Count -Descending | + Select -First 10 #> [CmdletBinding(PositionalBinding=$false)] [Alias('WebSocket')] From eadf4f0bc541c168ba567f2a7b2146d1fa3fb14b Mon Sep 17 00:00:00 2001 From: StartAutomating Date: Fri, 20 Dec 2024 05:35:07 +0000 Subject: [PATCH 10/15] docs: WebSocket decoration example ( re #34 ) --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index 2180a22..3270710 100644 --- a/README.md +++ b/README.md @@ -149,4 +149,9 @@ websocket wss://jetstream2.us-west.bsky.network/subscribe?wantedCollections=app. } } ~~~ + #### Get-WebSocket Example 11 + +~~~powershell +# We can decorate a type returned from a WebSocket, allowing us to add additional properties. +~~~ From 88f5d2cbfa043fe8d4f66200a4c502fbe7175318 Mon Sep 17 00:00:00 2001 From: StartAutomating Date: Fri, 20 Dec 2024 05:35:15 +0000 Subject: [PATCH 11/15] docs: WebSocket decoration example ( re #34 ) --- docs/Get-WebSocket.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/docs/Get-WebSocket.md b/docs/Get-WebSocket.md index 6dcf6d8..f252bef 100644 --- a/docs/Get-WebSocket.md +++ b/docs/Get-WebSocket.md @@ -118,6 +118,25 @@ websocket wss://jetstream2.us-west.bsky.network/subscribe?wantedCollections=app. } } ``` +We can decorate a type returned from a WebSocket, allowing us to add additional properties. +For example, let's add a `Tags` property to the `app.bsky.feed.post` type. + +```PowerShell +$typeName = 'app.bsky.feed.post' +Update-TypeData -TypeName $typeName -MemberName 'Tags' -MemberType ScriptProperty -Value { + @($this.commit.record.facets.features.tag) +} -Force + +# Now, let's get 10kb posts ( this should not take too long ) +$somePosts = + websocket "wss://jetstream2.us-west.bsky.network/subscribe?wantedCollections=$typeName" -PSTypeName $typeName -Maximum 10kb -Watch +$somePosts | + ? Tags | + Select -ExpandProperty Tags | + Group | + Sort Count -Descending | + Select -First 10 +``` --- From 7065b8b271a04bf0a72b3f6d9dc6442fda0ba106 Mon Sep 17 00:00:00 2001 From: StartAutomating Date: Fri, 20 Dec 2024 05:35:15 +0000 Subject: [PATCH 12/15] docs: WebSocket decoration example ( re #34 ) --- docs/_data/Help/Get-WebSocket.json | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/docs/_data/Help/Get-WebSocket.json b/docs/_data/Help/Get-WebSocket.json index 912942c..8d58a08 100644 --- a/docs/_data/Help/Get-WebSocket.json +++ b/docs/_data/Help/Get-WebSocket.json @@ -79,6 +79,11 @@ "Title": "EXAMPLE 10", "Markdown": "", "Code": "websocket wss://jetstream2.us-west.bsky.network/subscribe?wantedCollections=app.bsky.feed.post -WatchFor @{\n {$args.commit.record.text -match \"\\#\\w+\"}={\n $matches.0\n }\n {$args.commit.record.text -match '[\\p{IsHighSurrogates}\\p{IsLowSurrogates}]+'}={\n $matches.0\n }\n}" + }, + { + "Title": "EXAMPLE 11", + "Markdown": "We can decorate a type returned from a WebSocket, allowing us to add additional properties.\nFor example, let's add a `Tags` property to the `app.bsky.feed.post` type.", + "Code": "$typeName = 'app.bsky.feed.post'\nUpdate-TypeData -TypeName $typeName -MemberName 'Tags' -MemberType ScriptProperty -Value {\n @($this.commit.record.facets.features.tag)\n} -Force\n\n# Now, let's get 10kb posts ( this should not take too long )\n$somePosts =\n websocket \"wss://jetstream2.us-west.bsky.network/subscribe?wantedCollections=$typeName\" -PSTypeName $typeName -Maximum 10kb -Watch\n$somePosts |\n ? Tags |\n Select -ExpandProperty Tags |\n Group |\n Sort Count -Descending |\n Select -First 10" } ] } \ No newline at end of file From 563a88d813d7f1f3c613d733a7cd211cff946d3d Mon Sep 17 00:00:00 2001 From: StartAutomating Date: Fri, 20 Dec 2024 05:35:16 +0000 Subject: [PATCH 13/15] docs: WebSocket decoration example ( re #34 ) --- docs/README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/docs/README.md b/docs/README.md index 8ef65a9..dce8d2f 100644 --- a/docs/README.md +++ b/docs/README.md @@ -149,3 +149,8 @@ websocket wss://jetstream2.us-west.bsky.network/subscribe?wantedCollections=app. } } ~~~ + #### Get-WebSocket Example 11 + +~~~powershell +# We can decorate a type returned from a WebSocket, allowing us to add additional properties. +~~~ From 032df410547256ab8fa057eb248b01a8de84b067 Mon Sep 17 00:00:00 2001 From: James Brundage <+@noreply.github.com> Date: Thu, 19 Dec 2024 21:57:13 -0800 Subject: [PATCH 14/15] release: WebSocket 0.1.2 ( Fixes #38 ) Updating Release Notes and CHANGELOG, explicitly exporting commands --- CHANGELOG.md | 11 +++++++++++ WebSocket.psd1 | 25 ++++++++++--------------- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6f73da7..0eacffa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,17 @@ > Like It? [Star It](https://github.com/PowerShellWeb/WebSocket) > Love It? [Support It](https://github.com/sponsors/StartAutomating) +## WebSocket 0.1.2 + +* WebSocket now decorates (#34) + * Added a -PSTypeName(s) parameter to Get-WebSocket, so we can extend the output. +* Reusing WebSockets (#35) + * If a WebSocketUri is already open, we will reuse it. +* Explicitly exporting commands (#38) + * This should enable automatic import and enable Find-Command + +--- + ## WebSocket 0.1.1 * WebSocket GitHub Action diff --git a/WebSocket.psd1 b/WebSocket.psd1 index 0441f61..4eb77b6 100644 --- a/WebSocket.psd1 +++ b/WebSocket.psd1 @@ -1,11 +1,13 @@ @{ - ModuleVersion = '0.1.1' + ModuleVersion = '0.1.2' RootModule = 'WebSocket.psm1' Guid = '75c70c8b-e5eb-4a60-982e-a19110a1185d' Author = 'James Brundage' CompanyName = 'StartAutomating' Copyright = '2024 StartAutomating' Description = 'Work with WebSockets in PowerShell' + FunctionsToExport = @('Get-WebSocket') + AliasesToExport = @('WebSocket') PrivateData = @{ PSData = @{ Tags = @('WebSocket', 'WebSockets', 'Networking', 'Web') @@ -15,21 +17,14 @@ > Like It? [Star It](https://github.com/PowerShellWeb/WebSocket) > Love It? [Support It](https://github.com/sponsors/StartAutomating) -## WebSocket 0.1.1 +## WebSocket 0.1.2 -* WebSocket GitHub Action - * Run any `*.WebSocket.ps1` files in a repository (#24) -* WebSocket container updates - * Container now runs mounted `*.WebSocket.ps1` files (#26) -* Get-WebSocket improvements: - * New Parameters: - * -Maximum (#22) - * -TimeOut (#23) - * -WatchFor (#29) - * -RawText (#30) - * -Binary (#31) -* WebSocket Testing (#25) -* Adding FUNDING.yml (#14) +* WebSocket now decorates (#34) + * Added a -PSTypeName(s) parameter to Get-WebSocket, so we can extend the output. +* Reusing WebSockets (#35) + * If a WebSocketUri is already open, we will reuse it. +* Explicitly exporting commands (#38) + * This should enable automatic import and enable Find-Command --- From 0804308d1ef4fc09a70c9a090b3c01eba98f8b16 Mon Sep 17 00:00:00 2001 From: StartAutomating Date: Fri, 20 Dec 2024 05:58:24 +0000 Subject: [PATCH 15/15] release: WebSocket 0.1.2 ( Fixes #38 ) Updating Release Notes and CHANGELOG, explicitly exporting commands --- docs/CHANGELOG.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 1020c9d..a2ac339 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -1,6 +1,17 @@ > Like It? [Star It](https://github.com/PowerShellWeb/WebSocket) > Love It? [Support It](https://github.com/sponsors/StartAutomating) +## WebSocket 0.1.2 + +* WebSocket now decorates (#34) + * Added a -PSTypeName(s) parameter to Get-WebSocket, so we can extend the output. +* Reusing WebSockets (#35) + * If a WebSocketUri is already open, we will reuse it. +* Explicitly exporting commands (#38) + * This should enable automatic import and enable Find-Command + +--- + ## WebSocket 0.1.1 * WebSocket GitHub Action