Skip to content

Commit

Permalink
add support for WebSocket protocols option (#3769)
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-smart authored Oct 11, 2024
1 parent 61a99b2 commit 8c33087
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 8 deletions.
7 changes: 7 additions & 0 deletions .changeset/red-flies-share.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@effect/platform-node": patch
"@effect/platform-bun": patch
"@effect/platform": patch
---

add support for WebSocket protocols option
2 changes: 1 addition & 1 deletion packages/platform-bun/src/BunSocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ export const layerWebSocket = (url: string, options?: {
*/
export const layerWebSocketConstructor: Layer.Layer<Socket.WebSocketConstructor> = Layer.succeed(
Socket.WebSocketConstructor,
(url) => new globalThis.WebSocket(url)
(url, protocols) => new globalThis.WebSocket(url, protocols)
)
4 changes: 2 additions & 2 deletions packages/platform-node/src/NodeSocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ export const layerWebSocketConstructor: Layer.Layer<Socket.WebSocketConstructor>
Socket.WebSocketConstructor,
() => {
if ("WebSocket" in globalThis) {
return (url) => new globalThis.WebSocket(url)
return (url, protocols) => new globalThis.WebSocket(url, protocols)
}
return (url) => new WS.WebSocket(url) as unknown as globalThis.WebSocket
return (url, protocols) => new WS.WebSocket(url, protocols) as unknown as globalThis.WebSocket
}
)
14 changes: 9 additions & 5 deletions packages/platform/src/Socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,10 @@ export interface WebSocketConstructor {
* @since 1.0.0
* @category tags
*/
export const WebSocketConstructor: Context.Tag<WebSocketConstructor, (url: string) => globalThis.WebSocket> = Context
export const WebSocketConstructor: Context.Tag<
WebSocketConstructor,
(url: string, protocols?: string | Array<string> | undefined) => globalThis.WebSocket
> = Context
.GenericTag("@effect/platform/Socket/WebSocketConstructor")

/**
Expand All @@ -359,21 +362,22 @@ export const WebSocketConstructor: Context.Tag<WebSocketConstructor, (url: strin
*/
export const layerWebSocketConstructorGlobal: Layer.Layer<WebSocketConstructor> = Layer.succeed(
WebSocketConstructor,
(url: string) => new globalThis.WebSocket(url)
(url, protocols) => new globalThis.WebSocket(url, protocols)
)

/**
* @since 1.0.0
* @category constructors
*/
export const makeWebSocket = (url: string | Effect.Effect<string>, options?: {
readonly closeCodeIsError?: (code: number) => boolean
readonly openTimeout?: DurationInput
readonly closeCodeIsError?: ((code: number) => boolean) | undefined
readonly openTimeout?: DurationInput | undefined
readonly protocols?: string | Array<string> | undefined
}): Effect.Effect<Socket, never, WebSocketConstructor> =>
fromWebSocket(
Effect.acquireRelease(
(typeof url === "string" ? Effect.succeed(url) : url).pipe(
Effect.flatMap((url) => Effect.map(WebSocketConstructor, (f) => f(url)))
Effect.flatMap((url) => Effect.map(WebSocketConstructor, (f) => f(url, options?.protocols)))
),
(ws) => Effect.sync(() => ws.close())
),
Expand Down

0 comments on commit 8c33087

Please sign in to comment.