Skip to content

Commit 28094e4

Browse files
author
James Brundage
committed
feat: WebSocket Parameter Sets ( Fixes #73, Fixes #74, Fixes #76 )
1 parent 952518c commit 28094e4

1 file changed

Lines changed: 47 additions & 24 deletions

File tree

Commands/Get-WebSocket.ps1

Lines changed: 47 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ function Get-WebSocket {
1010
If the `-Watch` parameter is provided, will output a continous stream of objects.
1111
.EXAMPLE
1212
# Create a WebSocket job that connects to a WebSocket and outputs the results.
13-
Get-WebSocket -WebSocketUri "wss://localhost:9669/"
13+
Get-WebSocket -SocketUrl "wss://localhost:9669/"
1414
.EXAMPLE
1515
# Get is the default verb, so we can just say WebSocket.
1616
# `-Watch` will output a continous stream of objects from the websocket.
@@ -106,36 +106,41 @@ function Get-WebSocket {
106106
Sort Count -Descending |
107107
Select -First 10
108108
#>
109-
[CmdletBinding(PositionalBinding=$false,SupportsPaging)]
109+
[CmdletBinding(
110+
PositionalBinding=$false,
111+
SupportsPaging,
112+
DefaultParameterSetName='WebSocketClient'
113+
)]
110114
[Alias('WebSocket','ws','wss')]
111115
param(
112116
# The WebSocket Uri.
113-
[Parameter(Position=0,ValueFromPipelineByPropertyName)]
114-
[Alias('Url','Uri','WebSocketUrl')]
117+
[Parameter(Position=0,ParameterSetName='WebSocketClient',ValueFromPipelineByPropertyName)]
118+
[Alias('Url','Uri','WebSocketUri','WebSocketUrl')]
115119
[uri]
116-
$WebSocketUri,
120+
$SocketUrl,
117121

118122
# One or more root urls.
119123
# If these are provided, a WebSocket server will be created with these listener prefixes.
120-
[Parameter(Position=1,ValueFromPipelineByPropertyName)]
121-
[Alias('HostHeader','Host','CNAME','ServerURL','ListenerPrefix','ListenerPrefixes','ListenerUrl')]
124+
[Parameter(Mandatory,Position=0,ValueFromPipelineByPropertyName,ParameterSetName='WebSocketServer')]
125+
[Alias('HostHeader','Host','CNAME','ListenerPrefix','ListenerPrefixes','ListenerUrl')]
122126
[string[]]
123127
$RootUrl,
124128

125129
# A route table for all requests.
126-
[Parameter(ValueFromPipelineByPropertyName)]
130+
[Parameter(ValueFromPipelineByPropertyName,ParameterSetName='WebSocketServer')]
127131
[Alias('Routes','RouteTable','WebHook','WebHooks')]
128132
[Collections.IDictionary]
129133
$Route,
130134

131135
# The Default HTML.
132136
# This will be displayed when visiting the root url.
133-
[Parameter(ValueFromPipelineByPropertyName)]
137+
[Parameter(ValueFromPipelineByPropertyName,ParameterSetName='WebSocketServer')]
134138
[Alias('DefaultHTML','Home','Index','IndexHTML','DefaultPage')]
135139
[string]
136140
$HTML,
137141

138142
# The name of the palette to use. This will include the [4bitcss](https://4bitcss.com) stylesheet.
143+
[Parameter(ValueFromPipelineByPropertyName,ParameterSetName='WebSocketServer')]
139144
[Alias('Palette','ColorScheme','ColorPalette')]
140145
[ArgumentCompleter({
141146
param ($commandName,$parameterName,$wordToComplete,$commandAst,$fakeBoundParameters )
@@ -152,35 +157,37 @@ function Get-WebSocket {
152157
$PaletteName,
153158

154159
# The [Google Font](https://fonts.google.com/) name.
155-
[Parameter(ValueFromPipelineByPropertyName)]
160+
[Parameter(ValueFromPipelineByPropertyName,ParameterSetName='WebSocketServer')]
156161
[Alias('FontName')]
157162
[string]
158163
$GoogleFont,
159164

160165
# The Google Font name to use for code blocks.
161166
# (this should be a [monospace font](https://fonts.google.com/?classification=Monospace))
162-
[Parameter(ValueFromPipelineByPropertyName)]
167+
[Parameter(ValueFromPipelineByPropertyName,ParameterSetName='WebSocketServer')]
163168
[Alias('PreFont','CodeFontName','PreFontName')]
164169
[string]
165170
$CodeFont,
166171

167172
# A list of javascript files or urls to include in the content.
168-
[Parameter(ValueFromPipelineByPropertyName)]
173+
[Parameter(ValueFromPipelineByPropertyName,ParameterSetName='WebSocketServer')]
169174
[string[]]
170175
$JavaScript,
171176

172177
# A javascript import map. This allows you to import javascript modules.
173-
[Parameter(ValueFromPipelineByPropertyName)]
178+
[Parameter(ValueFromPipelineByPropertyName,ParameterSetName='WebSocketServer')]
174179
[Alias('ImportsJavaScript','JavaScriptImports','JavaScriptImportMap')]
175180
[Collections.IDictionary]
176181
$ImportMap,
177182

178183
# A collection of query parameters.
179-
# These will be appended onto the `-WebSocketUri`.
184+
# These will be appended onto the `-SocketUrl`.
185+
[Parameter(ValueFromPipelineByPropertyName,ParameterSetName='WebSocketClient')]
180186
[Collections.IDictionary]
181187
$QueryParameter,
182188

183189
# A ScriptBlock that will handle the output of the WebSocket.
190+
[Parameter(ParameterSetName='WebSocketServer')]
184191
[ScriptBlock]
185192
$Handler,
186193

@@ -206,32 +213,39 @@ function Get-WebSocket {
206213

207214
# The ScriptBlock to run after connection to a websocket.
208215
# This can be useful for making any initial requests.
216+
[Parameter(ParameterSetName='WebSocketClient')]
209217
[ScriptBlock]
210218
$OnConnect,
211219

212220
# The ScriptBlock to run when an error occurs.
221+
[Parameter(ParameterSetName='WebSocketClient')]
213222
[ScriptBlock]
214223
$OnError,
215224

216225
# The ScriptBlock to run when the WebSocket job outputs an object.
226+
[Parameter(ParameterSetName='WebSocketClient')]
217227
[ScriptBlock]
218228
$OnOutput,
219229

220230
# The Scriptblock to run when the WebSocket job produces a warning.
231+
[Parameter(ParameterSetName='WebSocketClient')]
221232
[ScriptBlock]
222233
$OnWarning,
223234

224235
# If set, will watch the output of the WebSocket job, outputting results continuously instead of outputting a websocket job.
236+
[Parameter(ParameterSetName='WebSocketClient')]
225237
[Alias('Tail')]
226238
[switch]
227239
$Watch,
228240

229241
# If set, will output the raw text that comes out of the WebSocket.
242+
[Parameter(ParameterSetName='WebSocketClient')]
230243
[Alias('Raw')]
231244
[switch]
232245
$RawText,
233246

234247
# If set, will output the raw bytes that come out of the WebSocket.
248+
[Parameter(ParameterSetName='WebSocketClient')]
235249
[Alias('RawByte','RawBytes','Bytes','Byte')]
236250
[switch]
237251
$Binary,
@@ -241,6 +255,7 @@ function Get-WebSocket {
241255
$Force,
242256

243257
# The subprotocol used by the websocket. If not provided, this will default to `json`.
258+
[Parameter(ValueFromPipelineByPropertyName,ParameterSetName='WebSocketClient')]
244259
[string]
245260
$SubProtocol,
246261

@@ -249,12 +264,14 @@ function Get-WebSocket {
249264
# If they are strings or regexes, they will be applied to the raw text.
250265
# If they are scriptblocks, they will be applied to the deserialized JSON.
251266
# These filters will be run within the WebSocket job.
267+
[Parameter(ValueFromPipelineByPropertyName,ParameterSetName='WebSocketClient')]
252268
[PSObject[]]
253269
$Filter,
254270

255271
# If set, will watch the output of a WebSocket job for one or more conditions.
256272
# The conditions are the keys of the dictionary, and can be a regex, a string, or a scriptblock.
257273
# The values of the dictionary are what will happen when a match is found.
274+
[Parameter(ValueFromPipelineByPropertyName,ParameterSetName='WebSocketClient')]
258275
[ValidateScript({
259276
$keys = $_.Keys
260277
$values = $_.values
@@ -280,6 +297,7 @@ function Get-WebSocket {
280297

281298
# If provided, will decorate the objects outputted from a websocket job.
282299
# This will only decorate objects converted from JSON.
300+
[Parameter(ValueFromPipelineByPropertyName,ParameterSetName='WebSocketClient')]
283301
[Alias('PSTypeNames','Decorate','Decoration')]
284302
[string[]]
285303
$PSTypeName,
@@ -293,17 +311,20 @@ function Get-WebSocket {
293311
$ThrottleLimit = 64,
294312

295313
# The maximum time to wait for a connection to be established.
296-
# By default, this is 7 seconds.
314+
# By default, this is 7 seconds.
315+
[Parameter(ValueFromPipelineByPropertyName,ParameterSetName='WebSocketClient')]
297316
[TimeSpan]
298317
$ConnectionTimeout = '00:00:07',
299318

300319
# The Runspace where the handler should run.
301320
# Runspaces allow you to limit the scope of the handler.
321+
[Parameter(ValueFromPipelineByPropertyName,ParameterSetName='WebSocketClient')]
302322
[Runspace]
303323
$Runspace,
304324

305325
# The RunspacePool where the handler should run.
306326
# RunspacePools allow you to limit the scope of the handler to a pool of runspaces.
327+
[Parameter(ValueFromPipelineByPropertyName,ParameterSetName='WebSocketClient')]
307328
[Management.Automation.Runspaces.RunspacePool]
308329
[Alias('Pool')]
309330
$RunspacePool
@@ -325,16 +346,18 @@ function Get-WebSocket {
325346
$ExecutionContext.SessionState.PSVariable.Set($keyValue.Key, $keyValue.Value)
326347
}
327348

328-
if ((-not $WebSocketUri)) {
329-
throw "No WebSocketUri"
349+
if ((-not $SocketUrl)) {
350+
throw "No SocketUrl"
330351
}
331352

332-
if (-not $WebSocketUri.Scheme) {
333-
$WebSocketUri = [uri]"wss://$WebSocketUri"
353+
if (-not $SocketUrl.Scheme) {
354+
$SocketUrl = [uri]"wss://$SocketUrl"
355+
} elseif ($SocketUrl.Scheme -match '^https?') {
356+
$SocketUrl = $SocketUrl -replace '^http', 'ws'
334357
}
335358

336359
if ($QueryParameter) {
337-
$WebSocketUri = [uri]"$($webSocketUri)$($WebSocketUri.Query ? '&' : '?')$(@(
360+
$SocketUrl = [uri]"$($SocketUrl)$($SocketUrl.Query ? '&' : '?')$(@(
338361
foreach ($keyValuePair in $QueryParameter.GetEnumerator()) {
339362
if ($keyValuePair.Value -is [Collections.IList]) {
340363
foreach ($value in $keyValuePair.Value) {
@@ -359,7 +382,7 @@ function Get-WebSocket {
359382
} else {
360383
$ws.Options.AddSubProtocol('json')
361384
}
362-
$null = $ws.ConnectAsync($WebSocketUri, $CT).Wait()
385+
$null = $ws.ConnectAsync($SocketUrl, $CT).Wait()
363386
} else {
364387
$ws = $WebSocket
365388
}
@@ -850,7 +873,7 @@ function Get-WebSocket {
850873
}
851874

852875
process {
853-
if ((-not $WebSocketUri) -and (-not $RootUrl)) {
876+
if ((-not $SocketUrl) -and (-not $RootUrl)) {
854877
$socketAndListenerJobs =
855878
foreach ($job in Get-Job) {
856879
if (
@@ -966,9 +989,9 @@ function Get-WebSocket {
966989
return
967990
}
968991
$webSocketJob =
969-
if ($WebSocketUri) {
992+
if ($SocketUrl) {
970993
if (-not $name) {
971-
$Name = $WebSocketUri
994+
$Name = $SocketUrl
972995
}
973996

974997
$existingJob = foreach ($jobWithThisName in (Get-Job -Name $Name -ErrorAction Ignore)) {

0 commit comments

Comments
 (0)