Skip to content

Commit 2ad5796

Browse files
AllanZhengYPtrivikr
authored andcommitted
feat: remove absolute priority numbers from middleware stack (#434)
* fix: rename handlers middleware and arguments rename handler to specific InitializeHandler, same to middleware and arguments * feat: implement add() and addRelativeTo() method * feat: implement concat; remove; removeByTag; resolve * fix: fix interface incompatibility with mw stack class * feat: support recursively adding relative middleware
1 parent 7d3274f commit 2ad5796

File tree

13 files changed

+1204
-508
lines changed

13 files changed

+1204
-508
lines changed

Diff for: packages/middleware-content-length/src/index.ts

+12-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import {
55
BodyLengthCalculator,
66
MetadataBearer,
77
BuildHandlerOutput,
8-
Pluggable
8+
Pluggable,
9+
BuildHandlerOptions
910
} from "@aws-sdk/types";
1011
import { HttpRequest } from "@aws-sdk/protocol-http";
1112

@@ -44,13 +45,19 @@ export function contentLengthMiddleware(
4445
};
4546
}
4647

48+
export const contentLengthMiddlewareOptions: BuildHandlerOptions = {
49+
step: "build",
50+
tags: ["SET_CONTENT_LENGTH", "CONTENT_LENGTH"],
51+
name: "contentLengthMiddleware"
52+
};
53+
4754
export const getContentLengthPlugin = (options: {
4855
bodyLengthChecker: BodyLengthCalculator;
4956
}): Pluggable<any, any> => ({
5057
applyToStack: clientStack => {
51-
clientStack.add(contentLengthMiddleware(options.bodyLengthChecker), {
52-
step: "build",
53-
tags: { SET_CONTENT_LENGTH: true }
54-
});
58+
clientStack.add(
59+
contentLengthMiddleware(options.bodyLengthChecker),
60+
contentLengthMiddlewareOptions
61+
);
5562
}
5663
});

Diff for: packages/middleware-retry/src/retryMiddleware.ts

+12-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import {
33
FinalizeHandlerArguments,
44
MetadataBearer,
55
FinalizeHandlerOutput,
6-
Pluggable
6+
Pluggable,
7+
FinalizeRequestHandlerOptions,
8+
AbsoluteLocation
79
} from "@aws-sdk/types";
810
import { RetryResolvedConfig } from "./configurations";
911

@@ -17,15 +19,20 @@ export function retryMiddleware(options: RetryResolvedConfig) {
1719
};
1820
}
1921

22+
export const retryMiddlewareOptions: FinalizeRequestHandlerOptions &
23+
AbsoluteLocation = {
24+
name: "retryMiddleware",
25+
tags: ["RETRY"],
26+
step: "finalizeRequest",
27+
priority: "high"
28+
};
29+
2030
export const getRetryPlugin = (
2131
options: RetryResolvedConfig
2232
): Pluggable<any, any> => ({
2333
applyToStack: clientStack => {
2434
if (options.maxRetries > 0) {
25-
clientStack.add(retryMiddleware(options), {
26-
step: "finalizeRequest",
27-
tags: { RETRY: true }
28-
});
35+
clientStack.add(retryMiddleware(options), retryMiddlewareOptions);
2936
}
3037
}
3138
});

Diff for: packages/middleware-serde/src/serdePlugin.ts

+23-9
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,25 @@ import {
55
MetadataBearer,
66
MiddlewareStack,
77
EndpointBearer,
8-
RequestHandler
8+
RequestHandler,
9+
DeserializeHandlerOptions,
10+
SerializeHandlerOptions
911
} from "@aws-sdk/types";
1012
import { deserializerMiddleware } from "./deserializerMiddleware";
1113
import { serializerMiddleware } from "./serializerMiddleware";
1214

15+
export const deserializerMiddlewareOption: DeserializeHandlerOptions = {
16+
name: "deserializerMiddleware",
17+
step: "deserialize",
18+
tags: ["DESERIALIZER"]
19+
};
20+
21+
export const serializerMiddlewareOption: SerializeHandlerOptions = {
22+
name: "serializerMiddleware",
23+
step: "serialize",
24+
tags: ["SERIALIZER"]
25+
};
26+
1327
export function getSerdePlugin<
1428
InputType extends object,
1529
SerDeContext extends EndpointBearer,
@@ -24,14 +38,14 @@ export function getSerdePlugin<
2438
): Pluggable<InputType, OutputType> {
2539
return {
2640
applyToStack: (commandStack: MiddlewareStack<InputType, OutputType>) => {
27-
commandStack.add(deserializerMiddleware(config, deserializer), {
28-
step: "deserialize",
29-
tags: { DESERIALIZER: true }
30-
});
31-
commandStack.add(serializerMiddleware(config, serializer), {
32-
step: "serialize",
33-
tags: { SERIALIZER: true }
34-
});
41+
commandStack.add(
42+
deserializerMiddleware(config, deserializer),
43+
deserializerMiddlewareOption
44+
);
45+
commandStack.add(
46+
serializerMiddleware(config, serializer),
47+
serializerMiddlewareOption
48+
);
3549
}
3650
};
3751
}

Diff for: packages/middleware-signing/src/middleware.spec.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { signingMiddleware } from "./middleware";
1+
import { awsAuthMiddleware } from "./middleware";
22
import { RequestSigner } from "@aws-sdk/types";
3-
import { HttpRequest } from '@aws-sdk/protocol-http';
3+
import { HttpRequest } from "@aws-sdk/protocol-http";
44

55
describe("SigningHandler", () => {
66
const noOpSigner: RequestSigner = {
@@ -20,7 +20,10 @@ describe("SigningHandler", () => {
2020
});
2121

2222
it("should sign the request and pass it to the next handler", async () => {
23-
const signingHandler = signingMiddleware({ signer: noOpSigner } as any)(noOpNext, {} as any);
23+
const signingHandler = awsAuthMiddleware({ signer: noOpSigner } as any)(
24+
noOpNext,
25+
{} as any
26+
);
2427
await signingHandler({
2528
input: {},
2629
request: new HttpRequest({

Diff for: packages/middleware-signing/src/middleware.ts

+17-6
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ import {
33
FinalizeHandlerArguments,
44
FinalizeRequestMiddleware,
55
FinalizeHandlerOutput,
6-
Pluggable
6+
Pluggable,
7+
RelativeLocation,
8+
FinalizeRequestHandlerOptions
79
} from "@aws-sdk/types";
810
import { AwsAuthResolvedConfig } from "./configurations";
911
import { HttpRequest } from "@aws-sdk/protocol-http";
1012

11-
export function signingMiddleware<Input extends object, Output extends object>(
13+
export function awsAuthMiddleware<Input extends object, Output extends object>(
1214
options: AwsAuthResolvedConfig
1315
): FinalizeRequestMiddleware<Input, Output> {
1416
return (
@@ -25,13 +27,22 @@ export function signingMiddleware<Input extends object, Output extends object>(
2527
};
2628
}
2729

30+
export const awsAuthMiddlewareOptions: FinalizeRequestHandlerOptions &
31+
RelativeLocation<any, any> = {
32+
name: "awsAuthMiddleware",
33+
step: "finalizeRequest",
34+
tags: ["SIGNATURE", "AWSAUTH"],
35+
relation: "after",
36+
toMiddleware: "retryMiddleware"
37+
};
38+
2839
export const getAwsAuthPlugin = (
2940
options: AwsAuthResolvedConfig
3041
): Pluggable<any, any> => ({
3142
applyToStack: clientStack => {
32-
clientStack.add(signingMiddleware(options), {
33-
step: "finalizeRequest",
34-
tags: { SIGNATURE: true }
35-
});
43+
clientStack.addRelativeTo(
44+
awsAuthMiddleware(options),
45+
awsAuthMiddlewareOptions
46+
);
3647
}
3748
});

Diff for: packages/middleware-stack/README.md

+94
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,97 @@
22

33
[![NPM version](https://img.shields.io/npm/v/@aws-sdk/middleware-stack/preview.svg)](https://www.npmjs.com/package/@aws-sdk/middleware-stack)
44
[![NPM downloads](https://img.shields.io/npm/dm/@aws-sdk/middleware-stack.svg)](https://www.npmjs.com/package/@aws-sdk/middleware-stack)
5+
6+
The package contains an implementation of middleware stack interface. Middleware
7+
stack is a structure storing middleware in specified order and resolve these
8+
middleware into a single handler.
9+
10+
A middleware stack has five `Step`s, each of them represents a specific request life cycle:
11+
12+
- **initialize**: The input is being prepared. Examples of typical initialization tasks include injecting default options computing derived parameters.
13+
14+
- **serialize**: The input is complete and ready to be serialized. Examples of typical serialization tasks include input validation and building an HTTP request from user input.
15+
16+
- **build**: The input has been serialized into an HTTP request, but that request may require further modification. Any request alterations will be applied to all retries. Examples of typical build tasks include injecting HTTP headers that describe a stable aspect of the request, such as `Content-Length` or a body checksum.
17+
18+
- **finalizeRequest**: The request is being prepared to be sent over the wire. The request in this stage should already be semantically complete and should therefore only be altered to match the recipient's expectations. Examples of typical finalization tasks include request signing and injecting hop-by-hop headers.
19+
20+
- **deserialize**: The response has arrived, the middleware here will deserialize the raw response object to structured response
21+
22+
## Adding Middleware
23+
24+
There are two ways to add middleware to a middleware stack. They both add middleware to specified `Step` but they provide fine-grained location control differently.
25+
26+
### Absolute Location
27+
28+
You can add middleware to specified step with:
29+
30+
```javascript
31+
stack.add(middleware, {
32+
step: "finalizeRequest"
33+
});
34+
```
35+
36+
This approach works for most cases. Sometimes you want your middleware to be executed in the front of the `Step`, you can set the `Priority` to `high`. Set the `Priority` to `low` then this middleware will be executed at the end of `Step`:
37+
38+
```javascript
39+
stack.add(middleware, {
40+
step: "finalizeRequest",
41+
priority: "high"
42+
});
43+
```
44+
45+
If multiple middleware is added to same `step` with same `priority`, the order of them is determined by the order of adding them.
46+
47+
### Relative Location
48+
49+
In some cases, you might want to execute your middleware before some other known middleware, then you can use `addRelativeTo()`:
50+
51+
```javascript
52+
stack.add(middleware, {
53+
step: "finalizeRequest",
54+
name: "myMiddleware"
55+
});
56+
stack.addRelativeTo(anotherMiddleware, {
57+
step: "finalizeRequest",
58+
relation: "before", //or 'after'
59+
toMiddleware: "myMiddleware"
60+
});
61+
```
62+
63+
You need to specify the `step` in `addRelativeTo()`. This is because the middleware function signature of each step is different, middleware for different step should not be mixed. The previous middleware **must** have a unique name, this is the only way to refer a known middleware when adding middleware relatively. Note that if specified `step` doesn't have a middleware named as the value in `toMiddleware`, this middleware will fallback to be added with absolute location.
64+
65+
You can do this:
66+
67+
```javascript
68+
stack.addRelativeTo(middleware1, {
69+
step: "finalizeRequest",
70+
name: "Middleware1",
71+
relation: 'before',
72+
toMiddleware: 'middleware2'
73+
}); //this will fall back to `add()`
74+
stack.addRelativeTo(middleware2, {
75+
step: "finalizeRequest",
76+
name: 'Middleware2'
77+
relation: "after",
78+
toMiddleware: "Middleware1"
79+
}); //this will be added after middleware1
80+
```
81+
82+
## Removing Middleware
83+
84+
You can remove middleware by name one at a time:
85+
86+
```javascript
87+
stack.remove("Middleware1");
88+
```
89+
90+
If you specify tags for middleware, you can remove multiple middleware at a time according to tag:
91+
92+
```javascript
93+
stack.add(middleware, {
94+
step: "finalizeRequest",
95+
tags: ["final"]
96+
});
97+
stack.removeByTag("final");
98+
```

0 commit comments

Comments
 (0)