Skip to content

Commit 17fd869

Browse files
authored
chore(middleware-websocket): create static factory for Websocket handler (#5816)
1 parent a3b7053 commit 17fd869

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

packages/middleware-websocket/src/websocket-fetch-handler.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { formatUrl } from "@aws-sdk/util-format-url";
22
import { iterableToReadableStream, readableStreamtoIterable } from "@smithy/eventstream-serde-browser";
33
import { FetchHttpHandler } from "@smithy/fetch-http-handler";
4-
import { HttpRequest, HttpResponse } from "@smithy/protocol-http";
4+
import { HttpHandler, HttpRequest, HttpResponse } from "@smithy/protocol-http";
55
import { Provider, RequestHandler, RequestHandlerMetadata } from "@smithy/types";
66

77
import { isWebSocketRequest } from "./utils";
@@ -30,6 +30,25 @@ export class WebSocketFetchHandler {
3030
private readonly httpHandler: RequestHandler<any, any>;
3131
private readonly sockets: Record<string, WebSocket[]> = {};
3232

33+
/**
34+
* @returns the input if it is an HttpHandler of any class,
35+
* or instantiates a new instance of this handler.
36+
*/
37+
public static create(
38+
instanceOrOptions?: HttpHandler<any> | WebSocketFetchHandlerOptions | Provider<WebSocketFetchHandlerOptions | void>,
39+
httpHandler: RequestHandler<any, any> = new FetchHttpHandler()
40+
) {
41+
if (typeof (instanceOrOptions as any)?.handle === "function") {
42+
// is already an instance of HttpHandler.
43+
return instanceOrOptions as HttpHandler<any>;
44+
}
45+
// input is ctor options or undefined.
46+
return new WebSocketFetchHandler(
47+
instanceOrOptions as undefined | WebSocketFetchHandlerOptions | Provider<WebSocketFetchHandlerOptions>,
48+
httpHandler
49+
);
50+
}
51+
3352
constructor(
3453
options?: WebSocketFetchHandlerOptions | Provider<WebSocketFetchHandlerOptions>,
3554
httpHandler: RequestHandler<any, any> = new FetchHttpHandler()

0 commit comments

Comments
 (0)