-
Notifications
You must be signed in to change notification settings - Fork 586
/
DynamoDBDocumentClientCommand.ts
105 lines (93 loc) · 4.16 KB
/
DynamoDBDocumentClientCommand.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import { setFeature } from "@aws-sdk/core";
import { Command as $Command } from "@smithy/smithy-client";
import {
DeserializeHandler,
DeserializeHandlerArguments,
DeserializeHandlerOutput,
HandlerExecutionContext,
InitializeHandler,
InitializeHandlerArguments,
InitializeHandlerOutput,
MiddlewareStack,
} from "@smithy/types";
import { KeyNodeChildren, marshallInput, unmarshallOutput } from "../commands/utils";
import { DynamoDBDocumentClientResolvedConfig } from "../DynamoDBDocumentClient";
// /** @public */
// export { $Command, DynamoDBDocumentClientResolvedConfig };
/**
* Base class for Commands in lib-dynamodb used to pass middleware to
* the underlying DynamoDBClient Commands.
*
* @public
*/
export abstract class DynamoDBDocumentClientCommand<
Input extends object,
Output extends object,
BaseInput extends object,
BaseOutput extends object,
ResolvedClientConfiguration
> extends $Command<Input | BaseInput, Output | BaseOutput, ResolvedClientConfiguration> {
protected abstract readonly inputKeyNodes: KeyNodeChildren;
protected abstract readonly outputKeyNodes: KeyNodeChildren;
protected abstract clientCommand: $Command<Input | BaseInput, Output | BaseOutput, ResolvedClientConfiguration>;
public abstract middlewareStack: MiddlewareStack<Input | BaseInput, Output | BaseOutput>;
private static defaultLogFilterOverrides = {
overrideInputFilterSensitiveLog(...args: any[]) {},
overrideOutputFilterSensitiveLog(...args: any[]) {},
};
protected addMarshallingMiddleware(configuration: DynamoDBDocumentClientResolvedConfig): void {
const { marshallOptions = {}, unmarshallOptions = {} } = configuration.translateConfig || {};
marshallOptions.convertTopLevelContainer = marshallOptions.convertTopLevelContainer ?? true;
unmarshallOptions.convertWithoutMapWrapper = unmarshallOptions.convertWithoutMapWrapper ?? true;
this.clientCommand.middlewareStack.addRelativeTo(
(next: InitializeHandler<Input | BaseInput, Output | BaseOutput>, context: HandlerExecutionContext) =>
async (
args: InitializeHandlerArguments<Input | BaseInput>
): Promise<InitializeHandlerOutput<Output | BaseOutput>> => {
setFeature(context, "DDB_MAPPER", "d");
args.input = marshallInput(this.input, this.inputKeyNodes, marshallOptions);
context.dynamoDbDocumentClientOptions =
context.dynamoDbDocumentClientOptions || DynamoDBDocumentClientCommand.defaultLogFilterOverrides;
const input = args.input;
context.dynamoDbDocumentClientOptions.overrideInputFilterSensitiveLog = () => {
return context.inputFilterSensitiveLog?.(input);
};
return next(args);
},
{
name: "DocumentMarshall",
relation: "before",
toMiddleware: "serializerMiddleware",
override: true,
}
);
this.clientCommand.middlewareStack.addRelativeTo(
(next: DeserializeHandler<Input | BaseInput, Output | BaseOutput>, context: HandlerExecutionContext) =>
async (
args: DeserializeHandlerArguments<Input | BaseInput>
): Promise<DeserializeHandlerOutput<Output | BaseOutput>> => {
const deserialized = await next(args);
/**
* The original filter function is based on the shape of the
* base DynamoDB type, whereas the returned output will be
* unmarshalled. Therefore the filter log needs to be modified
* to act on the original output structure.
*/
const output = deserialized.output;
context.dynamoDbDocumentClientOptions =
context.dynamoDbDocumentClientOptions || DynamoDBDocumentClientCommand.defaultLogFilterOverrides;
context.dynamoDbDocumentClientOptions.overrideOutputFilterSensitiveLog = () => {
return context.outputFilterSensitiveLog?.(output);
};
deserialized.output = unmarshallOutput(deserialized.output, this.outputKeyNodes, unmarshallOptions);
return deserialized;
},
{
name: "DocumentUnmarshall",
relation: "before",
toMiddleware: "deserializerMiddleware",
override: true,
}
);
}
}