Skip to content

Commit

Permalink
chore(middleware-websocket): create static factory for Websocket hand…
Browse files Browse the repository at this point in the history
…ler (#5816)
  • Loading branch information
kuhe committed Feb 20, 2024
1 parent a3b7053 commit 17fd869
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion packages/middleware-websocket/src/websocket-fetch-handler.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { formatUrl } from "@aws-sdk/util-format-url";
import { iterableToReadableStream, readableStreamtoIterable } from "@smithy/eventstream-serde-browser";
import { FetchHttpHandler } from "@smithy/fetch-http-handler";
import { HttpRequest, HttpResponse } from "@smithy/protocol-http";
import { HttpHandler, HttpRequest, HttpResponse } from "@smithy/protocol-http";
import { Provider, RequestHandler, RequestHandlerMetadata } from "@smithy/types";

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

/**
* @returns the input if it is an HttpHandler of any class,
* or instantiates a new instance of this handler.
*/
public static create(
instanceOrOptions?: HttpHandler<any> | WebSocketFetchHandlerOptions | Provider<WebSocketFetchHandlerOptions | void>,
httpHandler: RequestHandler<any, any> = new FetchHttpHandler()
) {
if (typeof (instanceOrOptions as any)?.handle === "function") {
// is already an instance of HttpHandler.
return instanceOrOptions as HttpHandler<any>;
}
// input is ctor options or undefined.
return new WebSocketFetchHandler(
instanceOrOptions as undefined | WebSocketFetchHandlerOptions | Provider<WebSocketFetchHandlerOptions>,
httpHandler
);
}

constructor(
options?: WebSocketFetchHandlerOptions | Provider<WebSocketFetchHandlerOptions>,
httpHandler: RequestHandler<any, any> = new FetchHttpHandler()
Expand Down

0 comments on commit 17fd869

Please sign in to comment.