From 8c330879425e80bed2f65e407cd59e991f0d7bec Mon Sep 17 00:00:00 2001 From: Tim Date: Fri, 11 Oct 2024 21:06:10 +1300 Subject: [PATCH] add support for WebSocket protocols option (#3769) --- .changeset/red-flies-share.md | 7 +++++++ packages/platform-bun/src/BunSocket.ts | 2 +- packages/platform-node/src/NodeSocket.ts | 4 ++-- packages/platform/src/Socket.ts | 14 +++++++++----- 4 files changed, 19 insertions(+), 8 deletions(-) create mode 100644 .changeset/red-flies-share.md diff --git a/.changeset/red-flies-share.md b/.changeset/red-flies-share.md new file mode 100644 index 00000000000..63851fb07c8 --- /dev/null +++ b/.changeset/red-flies-share.md @@ -0,0 +1,7 @@ +--- +"@effect/platform-node": patch +"@effect/platform-bun": patch +"@effect/platform": patch +--- + +add support for WebSocket protocols option diff --git a/packages/platform-bun/src/BunSocket.ts b/packages/platform-bun/src/BunSocket.ts index 0bf56a8e74f..e4f6f19efb4 100644 --- a/packages/platform-bun/src/BunSocket.ts +++ b/packages/platform-bun/src/BunSocket.ts @@ -26,5 +26,5 @@ export const layerWebSocket = (url: string, options?: { */ export const layerWebSocketConstructor: Layer.Layer = Layer.succeed( Socket.WebSocketConstructor, - (url) => new globalThis.WebSocket(url) + (url, protocols) => new globalThis.WebSocket(url, protocols) ) diff --git a/packages/platform-node/src/NodeSocket.ts b/packages/platform-node/src/NodeSocket.ts index b39bad6b6fd..08fc8c45140 100644 --- a/packages/platform-node/src/NodeSocket.ts +++ b/packages/platform-node/src/NodeSocket.ts @@ -29,8 +29,8 @@ export const layerWebSocketConstructor: Layer.Layer 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 } ) diff --git a/packages/platform/src/Socket.ts b/packages/platform/src/Socket.ts index 2a76950b1d2..41fd1146300 100644 --- a/packages/platform/src/Socket.ts +++ b/packages/platform/src/Socket.ts @@ -350,7 +350,10 @@ export interface WebSocketConstructor { * @since 1.0.0 * @category tags */ -export const WebSocketConstructor: Context.Tag globalThis.WebSocket> = Context +export const WebSocketConstructor: Context.Tag< + WebSocketConstructor, + (url: string, protocols?: string | Array | undefined) => globalThis.WebSocket +> = Context .GenericTag("@effect/platform/Socket/WebSocketConstructor") /** @@ -359,7 +362,7 @@ export const WebSocketConstructor: Context.Tag = Layer.succeed( WebSocketConstructor, - (url: string) => new globalThis.WebSocket(url) + (url, protocols) => new globalThis.WebSocket(url, protocols) ) /** @@ -367,13 +370,14 @@ export const layerWebSocketConstructorGlobal: Layer.Layer * @category constructors */ export const makeWebSocket = (url: string | Effect.Effect, options?: { - readonly closeCodeIsError?: (code: number) => boolean - readonly openTimeout?: DurationInput + readonly closeCodeIsError?: ((code: number) => boolean) | undefined + readonly openTimeout?: DurationInput | undefined + readonly protocols?: string | Array | undefined }): Effect.Effect => 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()) ),