Skip to content

Commit 1615e0b

Browse files
Merge pull request #40 from PowerShellWeb/WebSocket-Decoration
WebSocket 0.1.2
2 parents b53081d + 0804308 commit 1615e0b

11 files changed

+124
-59
lines changed

CHANGELOG.md

+11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
> Like It? [Star It](https://github.com/PowerShellWeb/WebSocket)
22
> Love It? [Support It](https://github.com/sponsors/StartAutomating)
33
4+
## WebSocket 0.1.2
5+
6+
* WebSocket now decorates (#34)
7+
* Added a -PSTypeName(s) parameter to Get-WebSocket, so we can extend the output.
8+
* Reusing WebSockets (#35)
9+
* If a WebSocketUri is already open, we will reuse it.
10+
* Explicitly exporting commands (#38)
11+
* This should enable automatic import and enable Find-Command
12+
13+
---
14+
415
## WebSocket 0.1.1
516

617
* WebSocket GitHub Action

Commands/Get-WebSocket.ps1

+48-3
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,24 @@ function Get-WebSocket {
8585
$matches.0
8686
}
8787
}
88+
.EXAMPLE
89+
# We can decorate a type returned from a WebSocket, allowing us to add additional properties.
90+
91+
# For example, let's add a `Tags` property to the `app.bsky.feed.post` type.
92+
$typeName = 'app.bsky.feed.post'
93+
Update-TypeData -TypeName $typeName -MemberName 'Tags' -MemberType ScriptProperty -Value {
94+
@($this.commit.record.facets.features.tag)
95+
} -Force
96+
97+
# Now, let's get 10kb posts ( this should not take too long )
98+
$somePosts =
99+
websocket "wss://jetstream2.us-west.bsky.network/subscribe?wantedCollections=$typeName" -PSTypeName $typeName -Maximum 10kb -Watch
100+
$somePosts |
101+
? Tags |
102+
Select -ExpandProperty Tags |
103+
Group |
104+
Sort Count -Descending |
105+
Select -First 10
88106
#>
89107
[CmdletBinding(PositionalBinding=$false)]
90108
[Alias('WebSocket')]
@@ -173,6 +191,12 @@ function Get-WebSocket {
173191
[TimeSpan]
174192
$TimeOut,
175193

194+
# If provided, will decorate the objects outputted from a websocket job.
195+
# This will only decorate objects converted from JSON.
196+
[Alias('PSTypeNames','Decorate','Decoration')]
197+
[string[]]
198+
$PSTypeName,
199+
176200
# The maximum number of messages to receive before closing the WebSocket.
177201
[long]
178202
$Maximum,
@@ -208,7 +232,7 @@ function Get-WebSocket {
208232

209233
if (-not $WebSocketUri.Scheme) {
210234
$WebSocketUri = [uri]"wss://$WebSocketUri"
211-
}
235+
}
212236

213237
if (-not $BufferSize) {
214238
$BufferSize = 16kb
@@ -256,6 +280,13 @@ function Get-WebSocket {
256280
if ([string]::IsNullOrWhitespace($JS)) { continue }
257281
ConvertFrom-Json $JS
258282
}
283+
if ($PSTypeName) {
284+
$webSocketMessage.pstypenames.clear()
285+
[Array]::Reverse($PSTypeName)
286+
foreach ($psType in $psTypeName) {
287+
$webSocketMessage.pstypenames.add($psType)
288+
}
289+
}
259290
if ($handler) {
260291
$psCmd =
261292
if ($runspace.LanguageMode -eq 'NoLanguage' -or
@@ -293,8 +324,22 @@ function Get-WebSocket {
293324
if (-not $name) {
294325
$Name = $WebSocketUri
295326
}
296-
297-
Start-ThreadJob -ScriptBlock $SocketJob -Name $Name -InitializationScript $InitializationScript -ArgumentList $Variable
327+
328+
$existingJob = foreach ($jobWithThisName in (Get-Job -Name $Name)) {
329+
if (
330+
$jobWithThisName.State -in 'Running','NotStarted' -and
331+
$jobWithThisName.WebSocket -is [Net.WebSockets.ClientWebSocket]
332+
) {
333+
$jobWithThisName
334+
break
335+
}
336+
}
337+
338+
if ($existingJob) {
339+
$existingJob
340+
} else {
341+
Start-ThreadJob -ScriptBlock $SocketJob -Name $Name -InitializationScript $InitializationScript -ArgumentList $Variable
342+
}
298343
} elseif ($WebSocket) {
299344
if (-not $name) {
300345
$name = "websocket"

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -149,4 +149,9 @@ websocket wss://jetstream2.us-west.bsky.network/subscribe?wantedCollections=app.
149149
}
150150
}
151151
~~~
152+
#### Get-WebSocket Example 11
153+
154+
~~~powershell
155+
# We can decorate a type returned from a WebSocket, allowing us to add additional properties.
156+
~~~
152157

WebSocket.psd1

+10-15
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
@{
2-
ModuleVersion = '0.1.1'
2+
ModuleVersion = '0.1.2'
33
RootModule = 'WebSocket.psm1'
44
Guid = '75c70c8b-e5eb-4a60-982e-a19110a1185d'
55
Author = 'James Brundage'
66
CompanyName = 'StartAutomating'
77
Copyright = '2024 StartAutomating'
88
Description = 'Work with WebSockets in PowerShell'
9+
FunctionsToExport = @('Get-WebSocket')
10+
AliasesToExport = @('WebSocket')
911
PrivateData = @{
1012
PSData = @{
1113
Tags = @('WebSocket', 'WebSockets', 'Networking', 'Web')
@@ -15,21 +17,14 @@
1517
> Like It? [Star It](https://github.com/PowerShellWeb/WebSocket)
1618
> Love It? [Support It](https://github.com/sponsors/StartAutomating)
1719
18-
## WebSocket 0.1.1
20+
## WebSocket 0.1.2
1921
20-
* WebSocket GitHub Action
21-
* Run any `*.WebSocket.ps1` files in a repository (#24)
22-
* WebSocket container updates
23-
* Container now runs mounted `*.WebSocket.ps1` files (#26)
24-
* Get-WebSocket improvements:
25-
* New Parameters:
26-
* -Maximum (#22)
27-
* -TimeOut (#23)
28-
* -WatchFor (#29)
29-
* -RawText (#30)
30-
* -Binary (#31)
31-
* WebSocket Testing (#25)
32-
* Adding FUNDING.yml (#14)
22+
* WebSocket now decorates (#34)
23+
* Added a -PSTypeName(s) parameter to Get-WebSocket, so we can extend the output.
24+
* Reusing WebSockets (#35)
25+
* If a WebSocketUri is already open, we will reuse it.
26+
* Explicitly exporting commands (#38)
27+
* This should enable automatic import and enable Find-Command
3328
3429
---
3530

_config.yml

-12
This file was deleted.

_layouts/Default.html

-27
This file was deleted.

docs/CHANGELOG.md

+11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
> Like It? [Star It](https://github.com/PowerShellWeb/WebSocket)
22
> Love It? [Support It](https://github.com/sponsors/StartAutomating)
33
4+
## WebSocket 0.1.2
5+
6+
* WebSocket now decorates (#34)
7+
* Added a -PSTypeName(s) parameter to Get-WebSocket, so we can extend the output.
8+
* Reusing WebSockets (#35)
9+
* If a WebSocketUri is already open, we will reuse it.
10+
* Explicitly exporting commands (#38)
11+
* This should enable automatic import and enable Find-Command
12+
13+
---
14+
415
## WebSocket 0.1.1
516

617
* WebSocket GitHub Action

docs/Get-WebSocket.md

+28-1
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,25 @@ websocket wss://jetstream2.us-west.bsky.network/subscribe?wantedCollections=app.
118118
}
119119
}
120120
```
121+
We can decorate a type returned from a WebSocket, allowing us to add additional properties.
122+
For example, let's add a `Tags` property to the `app.bsky.feed.post` type.
123+
124+
```PowerShell
125+
$typeName = 'app.bsky.feed.post'
126+
Update-TypeData -TypeName $typeName -MemberName 'Tags' -MemberType ScriptProperty -Value {
127+
@($this.commit.record.facets.features.tag)
128+
} -Force
129+
130+
# Now, let's get 10kb posts ( this should not take too long )
131+
$somePosts =
132+
websocket "wss://jetstream2.us-west.bsky.network/subscribe?wantedCollections=$typeName" -PSTypeName $typeName -Maximum 10kb -Watch
133+
$somePosts |
134+
? Tags |
135+
Select -ExpandProperty Tags |
136+
Group |
137+
Sort Count -Descending |
138+
Select -First 10
139+
```
121140

122141
---
123142

@@ -231,6 +250,14 @@ The timeout for the WebSocket connection. If this is provided, after the timeou
231250
|------------|--------|--------|-------------|
232251
|`[TimeSpan]`|false |named |false |
233252

253+
#### **PSTypeName**
254+
If provided, will decorate the objects outputted from a websocket job.
255+
This will only decorate objects converted from JSON.
256+
257+
|Type |Required|Position|PipelineInput|Aliases |
258+
|------------|--------|--------|-------------|---------------------------------------|
259+
|`[String[]]`|false |named |false |PSTypeNames<br/>Decorate<br/>Decoration|
260+
234261
#### **Maximum**
235262
The maximum number of messages to receive before closing the WebSocket.
236263

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

267294
### Syntax
268295
```PowerShell
269-
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>]
296+
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>]
270297
```

docs/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -149,3 +149,8 @@ websocket wss://jetstream2.us-west.bsky.network/subscribe?wantedCollections=app.
149149
}
150150
}
151151
~~~
152+
#### Get-WebSocket Example 11
153+
154+
~~~powershell
155+
# We can decorate a type returned from a WebSocket, allowing us to add additional properties.
156+
~~~

docs/_data/Help/Get-WebSocket.json

+5
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@
7979
"Title": "EXAMPLE 10",
8080
"Markdown": "",
8181
"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}"
82+
},
83+
{
84+
"Title": "EXAMPLE 11",
85+
"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.",
86+
"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"
8287
}
8388
]
8489
}

docs/_data/LastDateBuilt.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
"2024-12-04"
1+
"2024-12-20"

0 commit comments

Comments
 (0)