Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebSocket 0.1.2 #40

Merged
merged 16 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
51 changes: 48 additions & 3 deletions Commands/Get-WebSocket.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,24 @@ function Get-WebSocket {
$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')]
Expand Down Expand Up @@ -173,6 +191,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,
Expand Down Expand Up @@ -208,7 +232,7 @@ function Get-WebSocket {

if (-not $WebSocketUri.Scheme) {
$WebSocketUri = [uri]"wss://$WebSocketUri"
}
}

if (-not $BufferSize) {
$BufferSize = 16kb
Expand Down Expand Up @@ -256,6 +280,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
Expand Down Expand Up @@ -293,8 +324,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"
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
~~~

25 changes: 10 additions & 15 deletions WebSocket.psd1
Original file line number Diff line number Diff line change
@@ -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')
Expand All @@ -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

---

Expand Down
12 changes: 0 additions & 12 deletions _config.yml

This file was deleted.

27 changes: 0 additions & 27 deletions _layouts/Default.html

This file was deleted.

11 changes: 11 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
29 changes: 28 additions & 1 deletion docs/Get-WebSocket.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

---

Expand Down Expand Up @@ -231,6 +250,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<br/>Decorate<br/>Decoration|

#### **Maximum**
The maximum number of messages to receive before closing the WebSocket.

Expand Down Expand Up @@ -266,5 +293,5 @@ RunspacePools allow you to limit the scope of the handler to a pool of runspaces

### Syntax
```PowerShell
Get-WebSocket [[-WebSocketUri] <Uri>] [-Handler <ScriptBlock>] [-Variable <IDictionary>] [-Name <String>] [-InitializationScript <ScriptBlock>] [-BufferSize <Int32>] [-OnConnect <ScriptBlock>] [-OnError <ScriptBlock>] [-OnOutput <ScriptBlock>] [-OnWarning <ScriptBlock>] [-Watch] [-RawText] [-Binary] [-WatchFor <IDictionary>] [-TimeOut <TimeSpan>] [-Maximum <Int64>] [-ConnectionTimeout <TimeSpan>] [-Runspace <Runspace>] [-RunspacePool <RunspacePool>] [<CommonParameters>]
Get-WebSocket [[-WebSocketUri] <Uri>] [-Handler <ScriptBlock>] [-Variable <IDictionary>] [-Name <String>] [-InitializationScript <ScriptBlock>] [-BufferSize <Int32>] [-OnConnect <ScriptBlock>] [-OnError <ScriptBlock>] [-OnOutput <ScriptBlock>] [-OnWarning <ScriptBlock>] [-Watch] [-RawText] [-Binary] [-WatchFor <IDictionary>] [-TimeOut <TimeSpan>] [-PSTypeName <String[]>] [-Maximum <Int64>] [-ConnectionTimeout <TimeSpan>] [-Runspace <Runspace>] [-RunspacePool <RunspacePool>] [<CommonParameters>]
```
5 changes: 5 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
~~~
5 changes: 5 additions & 0 deletions docs/_data/Help/Get-WebSocket.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
]
}
2 changes: 1 addition & 1 deletion docs/_data/LastDateBuilt.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
"2024-12-04"
"2024-12-20"
Loading