|
| 1 | +import {Handler, Middleware} from '@aws/types'; |
| 2 | + |
| 3 | +interface Step< |
| 4 | + InputType extends object, |
| 5 | + OutputType extends object, |
| 6 | + StreamType |
| 7 | +> { |
| 8 | + middleware: Middleware<InputType, OutputType, StreamType>; |
| 9 | + name?: string; |
| 10 | +} |
| 11 | + |
| 12 | +/** |
| 13 | + * Builds a single handler function from zero or more middleware functions and a |
| 14 | + * handler. The single handler is meant to send command objects to AWS services |
| 15 | + * and return promises that will resolve with the operation result or be |
| 16 | + * rejected with an error. |
| 17 | + * |
| 18 | + * When a composed handler is invoked, the arguments will pass through all |
| 19 | + * middleware in a defined order, and the return from the innermost handler will |
| 20 | + * pass through all middleware in the reverse of that order. Handlers are |
| 21 | + * ordered using a "step" that describes the step at which the SDK is when |
| 22 | + * sending a command. The available steps are: |
| 23 | + * |
| 24 | + * - init: The command is being initialized, allowing you to do things like add |
| 25 | + * default options. |
| 26 | + * - build: The command is being serialized into an HTTP request. |
| 27 | + * - sign: The request is being signed and prepared to be sent over the wire. |
| 28 | + * |
| 29 | + * You can add middleware to the top of the stack (i.e., make middleware the |
| 30 | + * first to receive incoming arguments and the last to receive a response) by |
| 31 | + * using the "prependInit" method or to the bottom of the stack (i.e., the first |
| 32 | + * to receive the response and the last to receive incoming arguments) by using |
| 33 | + * the "appendSign" method. Each step has comparable "prepend" and "append" |
| 34 | + * methods. |
| 35 | + * |
| 36 | + * Middleware can be registered with a name to allow you to easily add a |
| 37 | + * middleware before or after another middleware by name. This also allows you |
| 38 | + * to remove a middleware by name (in addition to removing by instance). |
| 39 | + */ |
| 40 | +export class MiddlewareStack< |
| 41 | + InputType extends object, |
| 42 | + OutputType extends object, |
| 43 | + StreamType = Uint8Array |
| 44 | +> { |
| 45 | + private initSteps: Array<Step<InputType, OutputType, StreamType>> = []; |
| 46 | + private buildSteps: Array<Step<InputType, OutputType, StreamType>> = []; |
| 47 | + private signSteps: Array<Step<InputType, OutputType, StreamType>> = []; |
| 48 | + |
| 49 | + /** |
| 50 | + * Add middleware to be executed after other members of the build phase. |
| 51 | + */ |
| 52 | + appendBuild( |
| 53 | + middleware: Middleware<InputType, OutputType, StreamType>, |
| 54 | + name?: string |
| 55 | + ): void { |
| 56 | + this.buildSteps.unshift({middleware, name}); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Add middleware to be executed after other members of the init phase. |
| 61 | + */ |
| 62 | + appendInit( |
| 63 | + middleware: Middleware<InputType, OutputType, StreamType>, |
| 64 | + name?: string |
| 65 | + ): void { |
| 66 | + this.initSteps.unshift({middleware, name}); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Add middleware to be executed after other members of the sign phase. |
| 71 | + */ |
| 72 | + appendSign( |
| 73 | + middleware: Middleware<InputType, OutputType, StreamType>, |
| 74 | + name?: string |
| 75 | + ): void { |
| 76 | + this.signSteps.unshift({middleware, name}); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Copy the stack into a new instance. |
| 81 | + */ |
| 82 | + clone(): MiddlewareStack<InputType, OutputType, StreamType> { |
| 83 | + const stack = new MiddlewareStack<InputType, OutputType, StreamType>(); |
| 84 | + stack.initSteps = this.initSteps.slice(0); |
| 85 | + stack.buildSteps = this.buildSteps.slice(0); |
| 86 | + stack.signSteps = this.signSteps.slice(0); |
| 87 | + return stack; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Add middleware to be executed before other members of the build phase. |
| 92 | + */ |
| 93 | + prependBuild( |
| 94 | + middleware: Middleware<InputType, OutputType, StreamType>, |
| 95 | + name?: string |
| 96 | + ): void { |
| 97 | + this.buildSteps.push({middleware, name}); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Add middleware to be executed before other members of the init phase. |
| 102 | + */ |
| 103 | + prependInit( |
| 104 | + middleware: Middleware<InputType, OutputType, StreamType>, |
| 105 | + name?: string |
| 106 | + ): void { |
| 107 | + this.initSteps.push({middleware, name}); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Add middleware to be executed before other members of the sign phase. |
| 112 | + */ |
| 113 | + prependSign( |
| 114 | + middleware: Middleware<InputType, OutputType, StreamType>, |
| 115 | + name?: string |
| 116 | + ): void { |
| 117 | + this.signSteps.push({middleware, name}); |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Remove middleware with the provided name or identity. |
| 122 | + */ |
| 123 | + remove(nameOrType: string|Middleware<InputType, OutputType, StreamType>): void { |
| 124 | + if (typeof nameOrType === 'string') { |
| 125 | + this.filterSteps(named => nameOrType !== named.name); |
| 126 | + } else { |
| 127 | + this.filterSteps(named => nameOrType !== named.middleware); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Reduce the stack into a composite handler function. |
| 133 | + */ |
| 134 | + resolve( |
| 135 | + handler: Handler<InputType, OutputType, StreamType> |
| 136 | + ): Handler<InputType, OutputType, StreamType> { |
| 137 | + for (let steps of [this.signSteps, this.buildSteps, this.initSteps]) { |
| 138 | + for (let step of steps) { |
| 139 | + handler = step.middleware(handler); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + return handler; |
| 144 | + } |
| 145 | + |
| 146 | + private filterSteps( |
| 147 | + predicate: ( |
| 148 | + named: Step<InputType, OutputType, StreamType> |
| 149 | + ) => boolean |
| 150 | + ): void { |
| 151 | + this.initSteps = this.initSteps.filter(predicate); |
| 152 | + this.buildSteps = this.buildSteps.filter(predicate); |
| 153 | + this.signSteps = this.signSteps.filter(predicate); |
| 154 | + } |
| 155 | +} |
0 commit comments