-
Notifications
You must be signed in to change notification settings - Fork 598
/
Copy pathindex.ts
73 lines (68 loc) · 2.08 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
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
import {
ChecksumConstructor,
Decoder,
Encoder,
HashConstructor,
InitializeHandler,
InitializeHandlerArguments,
InitializeHandlerOptions,
InitializeHandlerOutput,
InitializeMiddleware,
MetadataBearer,
Pluggable,
SourceData,
} from "@aws-sdk/types";
interface PreviouslyResolved {
base64Encoder: Encoder;
md5: ChecksumConstructor | HashConstructor;
utf8Decoder: Decoder;
}
export function ssecMiddleware(options: PreviouslyResolved): InitializeMiddleware<any, any> {
return <Output extends MetadataBearer>(next: InitializeHandler<any, Output>): InitializeHandler<any, Output> =>
async (args: InitializeHandlerArguments<any>): Promise<InitializeHandlerOutput<Output>> => {
let input = { ...args.input };
const properties = [
{
target: "SSECustomerKey",
hash: "SSECustomerKeyMD5",
},
{
target: "CopySourceSSECustomerKey",
hash: "CopySourceSSECustomerKeyMD5",
},
];
for (const prop of properties) {
const value: SourceData | undefined = (input as any)[prop.target];
if (value) {
const valueView: Uint8Array = ArrayBuffer.isView(value)
? new Uint8Array(value.buffer, value.byteOffset, value.byteLength)
: typeof value === "string"
? options.utf8Decoder(value)
: new Uint8Array(value);
const encoded = options.base64Encoder(valueView);
const hash = new options.md5();
hash.update(valueView);
input = {
...(input as any),
[prop.target]: encoded,
[prop.hash]: options.base64Encoder(await hash.digest()),
};
}
}
return next({
...args,
input,
});
};
}
export const ssecMiddlewareOptions: InitializeHandlerOptions = {
name: "ssecMiddleware",
step: "initialize",
tags: ["SSE"],
override: true,
};
export const getSsecPlugin = (config: PreviouslyResolved): Pluggable<any, any> => ({
applyToStack: (clientStack) => {
clientStack.add(ssecMiddleware(config), ssecMiddlewareOptions);
},
});