/
write-get-object-response-endpoint.ts
83 lines (77 loc) · 2.57 KB
/
write-get-object-response-endpoint.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import { getSuffixForArnEndpoint } from "@aws-sdk/middleware-bucket-endpoint";
import { HttpRequest } from "@aws-sdk/protocol-http";
import {
BuildHandler,
BuildHandlerArguments,
BuildHandlerOutput,
BuildMiddleware,
HandlerExecutionContext,
MetadataBearer,
Pluggable,
Provider,
RelativeMiddlewareOptions,
} from "@aws-sdk/types";
type PreviouslyResolved = {
region: Provider<string>;
isCustomEndpoint?: boolean;
disableHostPrefix: boolean;
runtime: string;
};
type Input = {
RequestRoute: string | undefined;
};
/**
* @internal
*/
export const writeGetObjectResponseEndpointMiddleware =
(config: PreviouslyResolved): BuildMiddleware<any, any> =>
<Output extends MetadataBearer>(
next: BuildHandler<Input, Output>,
context: HandlerExecutionContext
): BuildHandler<Input, Output> =>
async (args: BuildHandlerArguments<Input>): Promise<BuildHandlerOutput<Output>> => {
const { region: regionProvider, isCustomEndpoint, disableHostPrefix } = config;
const region = await regionProvider();
const { request, input } = args;
if (!HttpRequest.isInstance(request)) return next({ ...args });
let hostname = request.hostname;
if (hostname.endsWith("s3.amazonaws.com") || hostname.endsWith("s3-external-1.amazonaws.com")) {
return next({ ...args });
}
if (!isCustomEndpoint) {
const [, suffix] = getSuffixForArnEndpoint(request.hostname);
hostname = `s3-object-lambda.${region}.${suffix}`;
}
if (!disableHostPrefix && input.RequestRoute) {
hostname = `${input.RequestRoute}.${hostname}`;
}
request.hostname = hostname;
context["signing_service"] = "s3-object-lambda";
// Set the chunked transfer encoding when content-length cannot be inferred
// Only set in node because this header is forbidden in browser.
if (config.runtime === "node" && !request.headers["content-length"]) {
request.headers["transfer-encoding"] = "chunked";
}
return next({ ...args });
};
/**
* @internal
*/
export const writeGetObjectResponseEndpointMiddlewareOptions: RelativeMiddlewareOptions = {
relation: "after",
toMiddleware: "contentLengthMiddleware",
tags: ["WRITE_GET_OBJECT_RESPONSE", "S3", "ENDPOINT"],
name: "writeGetObjectResponseEndpointMiddleware",
override: true,
};
/**
* @internal
*/
export const getWriteGetObjectResponseEndpointPlugin = (config: PreviouslyResolved): Pluggable<any, any> => ({
applyToStack: (clientStack) => {
clientStack.addRelativeTo(
writeGetObjectResponseEndpointMiddleware(config),
writeGetObjectResponseEndpointMiddlewareOptions
);
},
});