-
Notifications
You must be signed in to change notification settings - Fork 546
/
index.ts
46 lines (42 loc) · 1.79 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import { HttpRequest } from "@aws-sdk/protocol-http";
import { AbsoluteLocation, BuildHandlerOptions, BuildMiddleware, Pluggable, RequestHandler } from "@aws-sdk/types";
export interface HostHeaderInputConfig {}
interface PreviouslyResolved {
requestHandler: RequestHandler<any, any>;
}
export interface HostHeaderResolvedConfig {
requestHandler: RequestHandler<any, any>;
}
export function resolveHostHeaderConfig<T>(
input: T & PreviouslyResolved & HostHeaderInputConfig
): T & HostHeaderResolvedConfig {
return input;
}
export const hostHeaderMiddleware = <Input extends object, Output extends object>(
options: HostHeaderResolvedConfig
): BuildMiddleware<Input, Output> => (next) => async (args) => {
if (!HttpRequest.isInstance(args.request)) return next(args);
const { request } = args;
const { handlerProtocol = "" } = options.requestHandler.metadata || {};
//For H2 request, remove 'host' header and use ':authority' header instead
//reference: https://nodejs.org/dist/latest-v13.x/docs/api/errors.html#ERR_HTTP2_INVALID_CONNECTION_HEADERS
if (handlerProtocol.indexOf("h2") >= 0 && !request.headers[":authority"]) {
delete request.headers["host"];
request.headers[":authority"] = "";
//non-H2 request and 'host' header is not set, set the 'host' header to request's hostname.
} else if (!request.headers["host"]) {
request.headers["host"] = request.hostname;
}
return next(args);
};
export const hostHeaderMiddlewareOptions: BuildHandlerOptions & AbsoluteLocation = {
name: "hostHeaderMiddleware",
step: "build",
priority: "low",
tags: ["HOST"],
};
export const getHostHeaderPlugin = (options: HostHeaderResolvedConfig): Pluggable<any, any> => ({
applyToStack: (clientStack) => {
clientStack.add(hostHeaderMiddleware(options), hostHeaderMiddlewareOptions);
},
});