Skip to content

Commit

Permalink
feat: remove absolute priority numbers from middleware stack (#434)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
AllanZhengYP authored and trivikr committed Jan 3, 2020
1 parent 7d3274f commit 2ad5796
Show file tree
Hide file tree
Showing 13 changed files with 1,204 additions and 508 deletions.
17 changes: 12 additions & 5 deletions packages/middleware-content-length/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import {
BodyLengthCalculator,
MetadataBearer,
BuildHandlerOutput,
Pluggable
Pluggable,
BuildHandlerOptions
} from "@aws-sdk/types";
import { HttpRequest } from "@aws-sdk/protocol-http";

Expand Down Expand Up @@ -44,13 +45,19 @@ export function contentLengthMiddleware(
};
}

export const contentLengthMiddlewareOptions: BuildHandlerOptions = {
step: "build",
tags: ["SET_CONTENT_LENGTH", "CONTENT_LENGTH"],
name: "contentLengthMiddleware"
};

export const getContentLengthPlugin = (options: {
bodyLengthChecker: BodyLengthCalculator;
}): Pluggable<any, any> => ({
applyToStack: clientStack => {
clientStack.add(contentLengthMiddleware(options.bodyLengthChecker), {
step: "build",
tags: { SET_CONTENT_LENGTH: true }
});
clientStack.add(
contentLengthMiddleware(options.bodyLengthChecker),
contentLengthMiddlewareOptions
);
}
});
17 changes: 12 additions & 5 deletions packages/middleware-retry/src/retryMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import {
FinalizeHandlerArguments,
MetadataBearer,
FinalizeHandlerOutput,
Pluggable
Pluggable,
FinalizeRequestHandlerOptions,
AbsoluteLocation
} from "@aws-sdk/types";
import { RetryResolvedConfig } from "./configurations";

Expand All @@ -17,15 +19,20 @@ export function retryMiddleware(options: RetryResolvedConfig) {
};
}

export const retryMiddlewareOptions: FinalizeRequestHandlerOptions &
AbsoluteLocation = {
name: "retryMiddleware",
tags: ["RETRY"],
step: "finalizeRequest",
priority: "high"
};

export const getRetryPlugin = (
options: RetryResolvedConfig
): Pluggable<any, any> => ({
applyToStack: clientStack => {
if (options.maxRetries > 0) {
clientStack.add(retryMiddleware(options), {
step: "finalizeRequest",
tags: { RETRY: true }
});
clientStack.add(retryMiddleware(options), retryMiddlewareOptions);
}
}
});
32 changes: 23 additions & 9 deletions packages/middleware-serde/src/serdePlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,25 @@ import {
MetadataBearer,
MiddlewareStack,
EndpointBearer,
RequestHandler
RequestHandler,
DeserializeHandlerOptions,
SerializeHandlerOptions
} from "@aws-sdk/types";
import { deserializerMiddleware } from "./deserializerMiddleware";
import { serializerMiddleware } from "./serializerMiddleware";

export const deserializerMiddlewareOption: DeserializeHandlerOptions = {
name: "deserializerMiddleware",
step: "deserialize",
tags: ["DESERIALIZER"]
};

export const serializerMiddlewareOption: SerializeHandlerOptions = {
name: "serializerMiddleware",
step: "serialize",
tags: ["SERIALIZER"]
};

export function getSerdePlugin<
InputType extends object,
SerDeContext extends EndpointBearer,
Expand All @@ -24,14 +38,14 @@ export function getSerdePlugin<
): Pluggable<InputType, OutputType> {
return {
applyToStack: (commandStack: MiddlewareStack<InputType, OutputType>) => {
commandStack.add(deserializerMiddleware(config, deserializer), {
step: "deserialize",
tags: { DESERIALIZER: true }
});
commandStack.add(serializerMiddleware(config, serializer), {
step: "serialize",
tags: { SERIALIZER: true }
});
commandStack.add(
deserializerMiddleware(config, deserializer),
deserializerMiddlewareOption
);
commandStack.add(
serializerMiddleware(config, serializer),
serializerMiddlewareOption
);
}
};
}
9 changes: 6 additions & 3 deletions packages/middleware-signing/src/middleware.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { signingMiddleware } from "./middleware";
import { awsAuthMiddleware } from "./middleware";
import { RequestSigner } from "@aws-sdk/types";
import { HttpRequest } from '@aws-sdk/protocol-http';
import { HttpRequest } from "@aws-sdk/protocol-http";

describe("SigningHandler", () => {
const noOpSigner: RequestSigner = {
Expand All @@ -20,7 +20,10 @@ describe("SigningHandler", () => {
});

it("should sign the request and pass it to the next handler", async () => {
const signingHandler = signingMiddleware({ signer: noOpSigner } as any)(noOpNext, {} as any);
const signingHandler = awsAuthMiddleware({ signer: noOpSigner } as any)(
noOpNext,
{} as any
);
await signingHandler({
input: {},
request: new HttpRequest({
Expand Down
23 changes: 17 additions & 6 deletions packages/middleware-signing/src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ import {
FinalizeHandlerArguments,
FinalizeRequestMiddleware,
FinalizeHandlerOutput,
Pluggable
Pluggable,
RelativeLocation,
FinalizeRequestHandlerOptions
} from "@aws-sdk/types";
import { AwsAuthResolvedConfig } from "./configurations";
import { HttpRequest } from "@aws-sdk/protocol-http";

export function signingMiddleware<Input extends object, Output extends object>(
export function awsAuthMiddleware<Input extends object, Output extends object>(
options: AwsAuthResolvedConfig
): FinalizeRequestMiddleware<Input, Output> {
return (
Expand All @@ -25,13 +27,22 @@ export function signingMiddleware<Input extends object, Output extends object>(
};
}

export const awsAuthMiddlewareOptions: FinalizeRequestHandlerOptions &
RelativeLocation<any, any> = {
name: "awsAuthMiddleware",
step: "finalizeRequest",
tags: ["SIGNATURE", "AWSAUTH"],
relation: "after",
toMiddleware: "retryMiddleware"
};

export const getAwsAuthPlugin = (
options: AwsAuthResolvedConfig
): Pluggable<any, any> => ({
applyToStack: clientStack => {
clientStack.add(signingMiddleware(options), {
step: "finalizeRequest",
tags: { SIGNATURE: true }
});
clientStack.addRelativeTo(
awsAuthMiddleware(options),
awsAuthMiddlewareOptions
);
}
});
94 changes: 94 additions & 0 deletions packages/middleware-stack/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,97 @@

[![NPM version](https://img.shields.io/npm/v/@aws-sdk/middleware-stack/preview.svg)](https://www.npmjs.com/package/@aws-sdk/middleware-stack)
[![NPM downloads](https://img.shields.io/npm/dm/@aws-sdk/middleware-stack.svg)](https://www.npmjs.com/package/@aws-sdk/middleware-stack)

The package contains an implementation of middleware stack interface. Middleware
stack is a structure storing middleware in specified order and resolve these
middleware into a single handler.

A middleware stack has five `Step`s, each of them represents a specific request life cycle:

- **initialize**: The input is being prepared. Examples of typical initialization tasks include injecting default options computing derived parameters.

- **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.

- **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.

- **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.

- **deserialize**: The response has arrived, the middleware here will deserialize the raw response object to structured response

## Adding Middleware

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.

### Absolute Location

You can add middleware to specified step with:

```javascript
stack.add(middleware, {
step: "finalizeRequest"
});
```

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`:

```javascript
stack.add(middleware, {
step: "finalizeRequest",
priority: "high"
});
```

If multiple middleware is added to same `step` with same `priority`, the order of them is determined by the order of adding them.

### Relative Location

In some cases, you might want to execute your middleware before some other known middleware, then you can use `addRelativeTo()`:

```javascript
stack.add(middleware, {
step: "finalizeRequest",
name: "myMiddleware"
});
stack.addRelativeTo(anotherMiddleware, {
step: "finalizeRequest",
relation: "before", //or 'after'
toMiddleware: "myMiddleware"
});
```

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.

You can do this:

```javascript
stack.addRelativeTo(middleware1, {
step: "finalizeRequest",
name: "Middleware1",
relation: 'before',
toMiddleware: 'middleware2'
}); //this will fall back to `add()`
stack.addRelativeTo(middleware2, {
step: "finalizeRequest",
name: 'Middleware2'
relation: "after",
toMiddleware: "Middleware1"
}); //this will be added after middleware1
```

## Removing Middleware

You can remove middleware by name one at a time:

```javascript
stack.remove("Middleware1");
```

If you specify tags for middleware, you can remove multiple middleware at a time according to tag:

```javascript
stack.add(middleware, {
step: "finalizeRequest",
tags: ["final"]
});
stack.removeByTag("final");
```
Loading

0 comments on commit 2ad5796

Please sign in to comment.