@smithy/middleware-stack
Advanced tools
+5
-285
@@ -1,285 +0,5 @@ | ||
| 'use strict'; | ||
| const getAllAliases = (name, aliases) => { | ||
| const _aliases = []; | ||
| if (name) { | ||
| _aliases.push(name); | ||
| } | ||
| if (aliases) { | ||
| for (const alias of aliases) { | ||
| _aliases.push(alias); | ||
| } | ||
| } | ||
| return _aliases; | ||
| }; | ||
| const getMiddlewareNameWithAliases = (name, aliases) => { | ||
| return `${name || "anonymous"}${aliases && aliases.length > 0 ? ` (a.k.a. ${aliases.join(",")})` : ""}`; | ||
| }; | ||
| const constructStack = () => { | ||
| let absoluteEntries = []; | ||
| let relativeEntries = []; | ||
| let identifyOnResolve = false; | ||
| const entriesNameSet = new Set(); | ||
| const sort = (entries) => entries.sort((a, b) => stepWeights[b.step] - stepWeights[a.step] || | ||
| priorityWeights[b.priority || "normal"] - priorityWeights[a.priority || "normal"]); | ||
| const removeByName = (toRemove) => { | ||
| let isRemoved = false; | ||
| const filterCb = (entry) => { | ||
| const aliases = getAllAliases(entry.name, entry.aliases); | ||
| if (aliases.includes(toRemove)) { | ||
| isRemoved = true; | ||
| for (const alias of aliases) { | ||
| entriesNameSet.delete(alias); | ||
| } | ||
| return false; | ||
| } | ||
| return true; | ||
| }; | ||
| absoluteEntries = absoluteEntries.filter(filterCb); | ||
| relativeEntries = relativeEntries.filter(filterCb); | ||
| return isRemoved; | ||
| }; | ||
| const removeByReference = (toRemove) => { | ||
| let isRemoved = false; | ||
| const filterCb = (entry) => { | ||
| if (entry.middleware === toRemove) { | ||
| isRemoved = true; | ||
| for (const alias of getAllAliases(entry.name, entry.aliases)) { | ||
| entriesNameSet.delete(alias); | ||
| } | ||
| return false; | ||
| } | ||
| return true; | ||
| }; | ||
| absoluteEntries = absoluteEntries.filter(filterCb); | ||
| relativeEntries = relativeEntries.filter(filterCb); | ||
| return isRemoved; | ||
| }; | ||
| const cloneTo = (toStack) => { | ||
| absoluteEntries.forEach((entry) => { | ||
| toStack.add(entry.middleware, { ...entry }); | ||
| }); | ||
| relativeEntries.forEach((entry) => { | ||
| toStack.addRelativeTo(entry.middleware, { ...entry }); | ||
| }); | ||
| toStack.identifyOnResolve?.(stack.identifyOnResolve()); | ||
| return toStack; | ||
| }; | ||
| const expandRelativeMiddlewareList = (from) => { | ||
| const expandedMiddlewareList = []; | ||
| from.before.forEach((entry) => { | ||
| if (entry.before.length === 0 && entry.after.length === 0) { | ||
| expandedMiddlewareList.push(entry); | ||
| } | ||
| else { | ||
| expandedMiddlewareList.push(...expandRelativeMiddlewareList(entry)); | ||
| } | ||
| }); | ||
| expandedMiddlewareList.push(from); | ||
| from.after.reverse().forEach((entry) => { | ||
| if (entry.before.length === 0 && entry.after.length === 0) { | ||
| expandedMiddlewareList.push(entry); | ||
| } | ||
| else { | ||
| expandedMiddlewareList.push(...expandRelativeMiddlewareList(entry)); | ||
| } | ||
| }); | ||
| return expandedMiddlewareList; | ||
| }; | ||
| const getMiddlewareList = (debug = false) => { | ||
| const normalizedAbsoluteEntries = []; | ||
| const normalizedRelativeEntries = []; | ||
| const normalizedEntriesNameMap = {}; | ||
| absoluteEntries.forEach((entry) => { | ||
| const normalizedEntry = { | ||
| ...entry, | ||
| before: [], | ||
| after: [], | ||
| }; | ||
| for (const alias of getAllAliases(normalizedEntry.name, normalizedEntry.aliases)) { | ||
| normalizedEntriesNameMap[alias] = normalizedEntry; | ||
| } | ||
| normalizedAbsoluteEntries.push(normalizedEntry); | ||
| }); | ||
| relativeEntries.forEach((entry) => { | ||
| const normalizedEntry = { | ||
| ...entry, | ||
| before: [], | ||
| after: [], | ||
| }; | ||
| for (const alias of getAllAliases(normalizedEntry.name, normalizedEntry.aliases)) { | ||
| normalizedEntriesNameMap[alias] = normalizedEntry; | ||
| } | ||
| normalizedRelativeEntries.push(normalizedEntry); | ||
| }); | ||
| normalizedRelativeEntries.forEach((entry) => { | ||
| if (entry.toMiddleware) { | ||
| const toMiddleware = normalizedEntriesNameMap[entry.toMiddleware]; | ||
| if (toMiddleware === undefined) { | ||
| if (debug) { | ||
| return; | ||
| } | ||
| throw new Error(`${entry.toMiddleware} is not found when adding ` + | ||
| `${getMiddlewareNameWithAliases(entry.name, entry.aliases)} ` + | ||
| `middleware ${entry.relation} ${entry.toMiddleware}`); | ||
| } | ||
| if (entry.relation === "after") { | ||
| toMiddleware.after.push(entry); | ||
| } | ||
| if (entry.relation === "before") { | ||
| toMiddleware.before.push(entry); | ||
| } | ||
| } | ||
| }); | ||
| const mainChain = sort(normalizedAbsoluteEntries) | ||
| .map(expandRelativeMiddlewareList) | ||
| .reduce((wholeList, expandedMiddlewareList) => { | ||
| wholeList.push(...expandedMiddlewareList); | ||
| return wholeList; | ||
| }, []); | ||
| return mainChain; | ||
| }; | ||
| const stack = { | ||
| add: (middleware, options = {}) => { | ||
| const { name, override, aliases: _aliases } = options; | ||
| const entry = { | ||
| step: "initialize", | ||
| priority: "normal", | ||
| middleware, | ||
| ...options, | ||
| }; | ||
| const aliases = getAllAliases(name, _aliases); | ||
| if (aliases.length > 0) { | ||
| if (aliases.some((alias) => entriesNameSet.has(alias))) { | ||
| if (!override) | ||
| throw new Error(`Duplicate middleware name '${getMiddlewareNameWithAliases(name, _aliases)}'`); | ||
| for (const alias of aliases) { | ||
| const toOverrideIndex = absoluteEntries.findIndex((entry) => entry.name === alias || entry.aliases?.some((a) => a === alias)); | ||
| if (toOverrideIndex === -1) { | ||
| continue; | ||
| } | ||
| const toOverride = absoluteEntries[toOverrideIndex]; | ||
| if (toOverride.step !== entry.step || entry.priority !== toOverride.priority) { | ||
| throw new Error(`"${getMiddlewareNameWithAliases(toOverride.name, toOverride.aliases)}" middleware with ` + | ||
| `${toOverride.priority} priority in ${toOverride.step} step cannot ` + | ||
| `be overridden by "${getMiddlewareNameWithAliases(name, _aliases)}" middleware with ` + | ||
| `${entry.priority} priority in ${entry.step} step.`); | ||
| } | ||
| absoluteEntries.splice(toOverrideIndex, 1); | ||
| } | ||
| } | ||
| for (const alias of aliases) { | ||
| entriesNameSet.add(alias); | ||
| } | ||
| } | ||
| absoluteEntries.push(entry); | ||
| }, | ||
| addRelativeTo: (middleware, options) => { | ||
| const { name, override, aliases: _aliases } = options; | ||
| const entry = { | ||
| middleware, | ||
| ...options, | ||
| }; | ||
| const aliases = getAllAliases(name, _aliases); | ||
| if (aliases.length > 0) { | ||
| if (aliases.some((alias) => entriesNameSet.has(alias))) { | ||
| if (!override) | ||
| throw new Error(`Duplicate middleware name '${getMiddlewareNameWithAliases(name, _aliases)}'`); | ||
| for (const alias of aliases) { | ||
| const toOverrideIndex = relativeEntries.findIndex((entry) => entry.name === alias || entry.aliases?.some((a) => a === alias)); | ||
| if (toOverrideIndex === -1) { | ||
| continue; | ||
| } | ||
| const toOverride = relativeEntries[toOverrideIndex]; | ||
| if (toOverride.toMiddleware !== entry.toMiddleware || toOverride.relation !== entry.relation) { | ||
| throw new Error(`"${getMiddlewareNameWithAliases(toOverride.name, toOverride.aliases)}" middleware ` + | ||
| `${toOverride.relation} "${toOverride.toMiddleware}" middleware cannot be overridden ` + | ||
| `by "${getMiddlewareNameWithAliases(name, _aliases)}" middleware ${entry.relation} ` + | ||
| `"${entry.toMiddleware}" middleware.`); | ||
| } | ||
| relativeEntries.splice(toOverrideIndex, 1); | ||
| } | ||
| } | ||
| for (const alias of aliases) { | ||
| entriesNameSet.add(alias); | ||
| } | ||
| } | ||
| relativeEntries.push(entry); | ||
| }, | ||
| clone: () => cloneTo(constructStack()), | ||
| use: (plugin) => { | ||
| plugin.applyToStack(stack); | ||
| }, | ||
| remove: (toRemove) => { | ||
| if (typeof toRemove === "string") | ||
| return removeByName(toRemove); | ||
| else | ||
| return removeByReference(toRemove); | ||
| }, | ||
| removeByTag: (toRemove) => { | ||
| let isRemoved = false; | ||
| const filterCb = (entry) => { | ||
| const { tags, name, aliases: _aliases } = entry; | ||
| if (tags && tags.includes(toRemove)) { | ||
| const aliases = getAllAliases(name, _aliases); | ||
| for (const alias of aliases) { | ||
| entriesNameSet.delete(alias); | ||
| } | ||
| isRemoved = true; | ||
| return false; | ||
| } | ||
| return true; | ||
| }; | ||
| absoluteEntries = absoluteEntries.filter(filterCb); | ||
| relativeEntries = relativeEntries.filter(filterCb); | ||
| return isRemoved; | ||
| }, | ||
| concat: (from) => { | ||
| const cloned = cloneTo(constructStack()); | ||
| cloned.use(from); | ||
| cloned.identifyOnResolve(identifyOnResolve || cloned.identifyOnResolve() || (from.identifyOnResolve?.() ?? false)); | ||
| return cloned; | ||
| }, | ||
| applyToStack: cloneTo, | ||
| identify: () => { | ||
| return getMiddlewareList(true).map((mw) => { | ||
| const step = mw.step ?? | ||
| mw.relation + | ||
| " " + | ||
| mw.toMiddleware; | ||
| return getMiddlewareNameWithAliases(mw.name, mw.aliases) + " - " + step; | ||
| }); | ||
| }, | ||
| identifyOnResolve(toggle) { | ||
| if (typeof toggle === "boolean") | ||
| identifyOnResolve = toggle; | ||
| return identifyOnResolve; | ||
| }, | ||
| resolve: (handler, context) => { | ||
| for (const middleware of getMiddlewareList() | ||
| .map((entry) => entry.middleware) | ||
| .reverse()) { | ||
| handler = middleware(handler, context); | ||
| } | ||
| if (identifyOnResolve) { | ||
| console.log(stack.identify()); | ||
| } | ||
| return handler; | ||
| }, | ||
| }; | ||
| return stack; | ||
| }; | ||
| const stepWeights = { | ||
| initialize: 5, | ||
| serialize: 4, | ||
| build: 3, | ||
| finalizeRequest: 2, | ||
| deserialize: 1, | ||
| }; | ||
| const priorityWeights = { | ||
| high: 3, | ||
| normal: 2, | ||
| low: 1, | ||
| }; | ||
| exports.constructStack = constructStack; | ||
| "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| exports.constructStack = void 0; | ||
| var client_1 = require("@smithy/core/client"); | ||
| Object.defineProperty(exports, "constructStack", { enumerable: true, get: function () { return client_1.constructStack; } }); |
+1
-1
@@ -1,1 +0,1 @@ | ||
| export * from "./MiddlewareStack"; | ||
| export { constructStack } from "@smithy/core/client"; |
@@ -1,1 +0,2 @@ | ||
| export * from "./MiddlewareStack"; | ||
| /** @deprecated Use @smithy/core/client instead. */ | ||
| export { constructStack } from "@smithy/core/client"; |
+5
-26
| { | ||
| "name": "@smithy/middleware-stack", | ||
| "version": "4.2.14", | ||
| "version": "4.3.0", | ||
| "description": "Provides a means for composing multiple middleware functions into a single handler", | ||
| "scripts": { | ||
| "build": "concurrently 'yarn:build:types' 'yarn:build:es:cjs'", | ||
| "build:es:cjs": "yarn g:tsc -p tsconfig.es.json && node ../../scripts/inline middleware-stack", | ||
| "build:types": "yarn g:tsc -p tsconfig.types.json", | ||
| "build:types:downlevel": "premove dist-types/ts3.4 && downlevel-dts dist-types dist-types/ts3.4", | ||
| "clean": "premove dist-cjs dist-es dist-types tsconfig.cjs.tsbuildinfo tsconfig.es.tsbuildinfo tsconfig.types.tsbuildinfo", | ||
| "extract:docs": "api-extractor run --local", | ||
| "format": "prettier --config ../../prettier.config.js --ignore-path ../../.prettierignore --write \"**/*.{ts,md,json}\"", | ||
| "lint": "eslint -c ../../.eslintrc.js \"src/**/*.ts\"", | ||
| "stage-release": "premove .release && yarn pack && mkdir ./.release && tar zxvf ./package.tgz --directory ./.release && rm ./package.tgz", | ||
| "test": "yarn g:vitest run", | ||
| "test:watch": "yarn g:vitest watch" | ||
| "build": "yarn g:tsc -p tsconfig.cjs.json && yarn g:tsc -p tsconfig.es.json && yarn g:tsc -p tsconfig.types.json", | ||
| "clean": "rm -rf dist-cjs dist-es dist-types", | ||
| "stage-release": "rm -rf .release && yarn pack && mkdir ./.release && tar zxvf ./package.tgz --directory ./.release && rm ./package.tgz" | ||
| }, | ||
@@ -29,21 +21,8 @@ "author": { | ||
| "dependencies": { | ||
| "@smithy/types": "^4.14.1", | ||
| "@smithy/core": "^3.24.0", | ||
| "tslib": "^2.6.2" | ||
| }, | ||
| "devDependencies": { | ||
| "concurrently": "7.0.0", | ||
| "downlevel-dts": "0.10.1", | ||
| "premove": "4.0.0", | ||
| "typedoc": "0.23.23" | ||
| }, | ||
| "engines": { | ||
| "node": ">=18.0.0" | ||
| }, | ||
| "typesVersions": { | ||
| "<4.5": { | ||
| "dist-types/*": [ | ||
| "dist-types/ts3.4/*" | ||
| ] | ||
| } | ||
| }, | ||
| "files": [ | ||
@@ -50,0 +29,0 @@ "dist-*/**" |
| const getAllAliases = (name, aliases) => { | ||
| const _aliases = []; | ||
| if (name) { | ||
| _aliases.push(name); | ||
| } | ||
| if (aliases) { | ||
| for (const alias of aliases) { | ||
| _aliases.push(alias); | ||
| } | ||
| } | ||
| return _aliases; | ||
| }; | ||
| const getMiddlewareNameWithAliases = (name, aliases) => { | ||
| return `${name || "anonymous"}${aliases && aliases.length > 0 ? ` (a.k.a. ${aliases.join(",")})` : ""}`; | ||
| }; | ||
| export const constructStack = () => { | ||
| let absoluteEntries = []; | ||
| let relativeEntries = []; | ||
| let identifyOnResolve = false; | ||
| const entriesNameSet = new Set(); | ||
| const sort = (entries) => entries.sort((a, b) => stepWeights[b.step] - stepWeights[a.step] || | ||
| priorityWeights[b.priority || "normal"] - priorityWeights[a.priority || "normal"]); | ||
| const removeByName = (toRemove) => { | ||
| let isRemoved = false; | ||
| const filterCb = (entry) => { | ||
| const aliases = getAllAliases(entry.name, entry.aliases); | ||
| if (aliases.includes(toRemove)) { | ||
| isRemoved = true; | ||
| for (const alias of aliases) { | ||
| entriesNameSet.delete(alias); | ||
| } | ||
| return false; | ||
| } | ||
| return true; | ||
| }; | ||
| absoluteEntries = absoluteEntries.filter(filterCb); | ||
| relativeEntries = relativeEntries.filter(filterCb); | ||
| return isRemoved; | ||
| }; | ||
| const removeByReference = (toRemove) => { | ||
| let isRemoved = false; | ||
| const filterCb = (entry) => { | ||
| if (entry.middleware === toRemove) { | ||
| isRemoved = true; | ||
| for (const alias of getAllAliases(entry.name, entry.aliases)) { | ||
| entriesNameSet.delete(alias); | ||
| } | ||
| return false; | ||
| } | ||
| return true; | ||
| }; | ||
| absoluteEntries = absoluteEntries.filter(filterCb); | ||
| relativeEntries = relativeEntries.filter(filterCb); | ||
| return isRemoved; | ||
| }; | ||
| const cloneTo = (toStack) => { | ||
| absoluteEntries.forEach((entry) => { | ||
| toStack.add(entry.middleware, { ...entry }); | ||
| }); | ||
| relativeEntries.forEach((entry) => { | ||
| toStack.addRelativeTo(entry.middleware, { ...entry }); | ||
| }); | ||
| toStack.identifyOnResolve?.(stack.identifyOnResolve()); | ||
| return toStack; | ||
| }; | ||
| const expandRelativeMiddlewareList = (from) => { | ||
| const expandedMiddlewareList = []; | ||
| from.before.forEach((entry) => { | ||
| if (entry.before.length === 0 && entry.after.length === 0) { | ||
| expandedMiddlewareList.push(entry); | ||
| } | ||
| else { | ||
| expandedMiddlewareList.push(...expandRelativeMiddlewareList(entry)); | ||
| } | ||
| }); | ||
| expandedMiddlewareList.push(from); | ||
| from.after.reverse().forEach((entry) => { | ||
| if (entry.before.length === 0 && entry.after.length === 0) { | ||
| expandedMiddlewareList.push(entry); | ||
| } | ||
| else { | ||
| expandedMiddlewareList.push(...expandRelativeMiddlewareList(entry)); | ||
| } | ||
| }); | ||
| return expandedMiddlewareList; | ||
| }; | ||
| const getMiddlewareList = (debug = false) => { | ||
| const normalizedAbsoluteEntries = []; | ||
| const normalizedRelativeEntries = []; | ||
| const normalizedEntriesNameMap = {}; | ||
| absoluteEntries.forEach((entry) => { | ||
| const normalizedEntry = { | ||
| ...entry, | ||
| before: [], | ||
| after: [], | ||
| }; | ||
| for (const alias of getAllAliases(normalizedEntry.name, normalizedEntry.aliases)) { | ||
| normalizedEntriesNameMap[alias] = normalizedEntry; | ||
| } | ||
| normalizedAbsoluteEntries.push(normalizedEntry); | ||
| }); | ||
| relativeEntries.forEach((entry) => { | ||
| const normalizedEntry = { | ||
| ...entry, | ||
| before: [], | ||
| after: [], | ||
| }; | ||
| for (const alias of getAllAliases(normalizedEntry.name, normalizedEntry.aliases)) { | ||
| normalizedEntriesNameMap[alias] = normalizedEntry; | ||
| } | ||
| normalizedRelativeEntries.push(normalizedEntry); | ||
| }); | ||
| normalizedRelativeEntries.forEach((entry) => { | ||
| if (entry.toMiddleware) { | ||
| const toMiddleware = normalizedEntriesNameMap[entry.toMiddleware]; | ||
| if (toMiddleware === undefined) { | ||
| if (debug) { | ||
| return; | ||
| } | ||
| throw new Error(`${entry.toMiddleware} is not found when adding ` + | ||
| `${getMiddlewareNameWithAliases(entry.name, entry.aliases)} ` + | ||
| `middleware ${entry.relation} ${entry.toMiddleware}`); | ||
| } | ||
| if (entry.relation === "after") { | ||
| toMiddleware.after.push(entry); | ||
| } | ||
| if (entry.relation === "before") { | ||
| toMiddleware.before.push(entry); | ||
| } | ||
| } | ||
| }); | ||
| const mainChain = sort(normalizedAbsoluteEntries) | ||
| .map(expandRelativeMiddlewareList) | ||
| .reduce((wholeList, expandedMiddlewareList) => { | ||
| wholeList.push(...expandedMiddlewareList); | ||
| return wholeList; | ||
| }, []); | ||
| return mainChain; | ||
| }; | ||
| const stack = { | ||
| add: (middleware, options = {}) => { | ||
| const { name, override, aliases: _aliases } = options; | ||
| const entry = { | ||
| step: "initialize", | ||
| priority: "normal", | ||
| middleware, | ||
| ...options, | ||
| }; | ||
| const aliases = getAllAliases(name, _aliases); | ||
| if (aliases.length > 0) { | ||
| if (aliases.some((alias) => entriesNameSet.has(alias))) { | ||
| if (!override) | ||
| throw new Error(`Duplicate middleware name '${getMiddlewareNameWithAliases(name, _aliases)}'`); | ||
| for (const alias of aliases) { | ||
| const toOverrideIndex = absoluteEntries.findIndex((entry) => entry.name === alias || entry.aliases?.some((a) => a === alias)); | ||
| if (toOverrideIndex === -1) { | ||
| continue; | ||
| } | ||
| const toOverride = absoluteEntries[toOverrideIndex]; | ||
| if (toOverride.step !== entry.step || entry.priority !== toOverride.priority) { | ||
| throw new Error(`"${getMiddlewareNameWithAliases(toOverride.name, toOverride.aliases)}" middleware with ` + | ||
| `${toOverride.priority} priority in ${toOverride.step} step cannot ` + | ||
| `be overridden by "${getMiddlewareNameWithAliases(name, _aliases)}" middleware with ` + | ||
| `${entry.priority} priority in ${entry.step} step.`); | ||
| } | ||
| absoluteEntries.splice(toOverrideIndex, 1); | ||
| } | ||
| } | ||
| for (const alias of aliases) { | ||
| entriesNameSet.add(alias); | ||
| } | ||
| } | ||
| absoluteEntries.push(entry); | ||
| }, | ||
| addRelativeTo: (middleware, options) => { | ||
| const { name, override, aliases: _aliases } = options; | ||
| const entry = { | ||
| middleware, | ||
| ...options, | ||
| }; | ||
| const aliases = getAllAliases(name, _aliases); | ||
| if (aliases.length > 0) { | ||
| if (aliases.some((alias) => entriesNameSet.has(alias))) { | ||
| if (!override) | ||
| throw new Error(`Duplicate middleware name '${getMiddlewareNameWithAliases(name, _aliases)}'`); | ||
| for (const alias of aliases) { | ||
| const toOverrideIndex = relativeEntries.findIndex((entry) => entry.name === alias || entry.aliases?.some((a) => a === alias)); | ||
| if (toOverrideIndex === -1) { | ||
| continue; | ||
| } | ||
| const toOverride = relativeEntries[toOverrideIndex]; | ||
| if (toOverride.toMiddleware !== entry.toMiddleware || toOverride.relation !== entry.relation) { | ||
| throw new Error(`"${getMiddlewareNameWithAliases(toOverride.name, toOverride.aliases)}" middleware ` + | ||
| `${toOverride.relation} "${toOverride.toMiddleware}" middleware cannot be overridden ` + | ||
| `by "${getMiddlewareNameWithAliases(name, _aliases)}" middleware ${entry.relation} ` + | ||
| `"${entry.toMiddleware}" middleware.`); | ||
| } | ||
| relativeEntries.splice(toOverrideIndex, 1); | ||
| } | ||
| } | ||
| for (const alias of aliases) { | ||
| entriesNameSet.add(alias); | ||
| } | ||
| } | ||
| relativeEntries.push(entry); | ||
| }, | ||
| clone: () => cloneTo(constructStack()), | ||
| use: (plugin) => { | ||
| plugin.applyToStack(stack); | ||
| }, | ||
| remove: (toRemove) => { | ||
| if (typeof toRemove === "string") | ||
| return removeByName(toRemove); | ||
| else | ||
| return removeByReference(toRemove); | ||
| }, | ||
| removeByTag: (toRemove) => { | ||
| let isRemoved = false; | ||
| const filterCb = (entry) => { | ||
| const { tags, name, aliases: _aliases } = entry; | ||
| if (tags && tags.includes(toRemove)) { | ||
| const aliases = getAllAliases(name, _aliases); | ||
| for (const alias of aliases) { | ||
| entriesNameSet.delete(alias); | ||
| } | ||
| isRemoved = true; | ||
| return false; | ||
| } | ||
| return true; | ||
| }; | ||
| absoluteEntries = absoluteEntries.filter(filterCb); | ||
| relativeEntries = relativeEntries.filter(filterCb); | ||
| return isRemoved; | ||
| }, | ||
| concat: (from) => { | ||
| const cloned = cloneTo(constructStack()); | ||
| cloned.use(from); | ||
| cloned.identifyOnResolve(identifyOnResolve || cloned.identifyOnResolve() || (from.identifyOnResolve?.() ?? false)); | ||
| return cloned; | ||
| }, | ||
| applyToStack: cloneTo, | ||
| identify: () => { | ||
| return getMiddlewareList(true).map((mw) => { | ||
| const step = mw.step ?? | ||
| mw.relation + | ||
| " " + | ||
| mw.toMiddleware; | ||
| return getMiddlewareNameWithAliases(mw.name, mw.aliases) + " - " + step; | ||
| }); | ||
| }, | ||
| identifyOnResolve(toggle) { | ||
| if (typeof toggle === "boolean") | ||
| identifyOnResolve = toggle; | ||
| return identifyOnResolve; | ||
| }, | ||
| resolve: (handler, context) => { | ||
| for (const middleware of getMiddlewareList() | ||
| .map((entry) => entry.middleware) | ||
| .reverse()) { | ||
| handler = middleware(handler, context); | ||
| } | ||
| if (identifyOnResolve) { | ||
| console.log(stack.identify()); | ||
| } | ||
| return handler; | ||
| }, | ||
| }; | ||
| return stack; | ||
| }; | ||
| const stepWeights = { | ||
| initialize: 5, | ||
| serialize: 4, | ||
| build: 3, | ||
| finalizeRequest: 2, | ||
| deserialize: 1, | ||
| }; | ||
| const priorityWeights = { | ||
| high: 3, | ||
| normal: 2, | ||
| low: 1, | ||
| }; |
| export {}; |
| import type { MiddlewareStack } from "@smithy/types"; | ||
| /** | ||
| * @internal | ||
| */ | ||
| export declare const constructStack: <Input extends object, Output extends object>() => MiddlewareStack<Input, Output>; |
| import type { AbsoluteLocation, HandlerOptions, MiddlewareType, Priority, RelativeLocation, Step } from "@smithy/types"; | ||
| export interface MiddlewareEntry<Input extends object, Output extends object> extends HandlerOptions { | ||
| middleware: MiddlewareType<Input, Output>; | ||
| } | ||
| export interface AbsoluteMiddlewareEntry<Input extends object, Output extends object> extends MiddlewareEntry<Input, Output>, AbsoluteLocation { | ||
| step: Step; | ||
| priority: Priority; | ||
| } | ||
| export interface RelativeMiddlewareEntry<Input extends object, Output extends object> extends MiddlewareEntry<Input, Output>, RelativeLocation { | ||
| } | ||
| export type Normalized<T extends MiddlewareEntry<Input, Output>, Input extends object = {}, Output extends object = {}> = T & { | ||
| after: Normalized<RelativeMiddlewareEntry<Input, Output>, Input, Output>[]; | ||
| before: Normalized<RelativeMiddlewareEntry<Input, Output>, Input, Output>[]; | ||
| }; | ||
| export interface NormalizedRelativeEntry<Input extends object, Output extends object> extends HandlerOptions { | ||
| step: Step; | ||
| middleware: MiddlewareType<Input, Output>; | ||
| next?: NormalizedRelativeEntry<Input, Output>; | ||
| prev?: NormalizedRelativeEntry<Input, Output>; | ||
| priority: null; | ||
| } | ||
| export type NamedMiddlewareEntriesMap<Input extends object, Output extends object> = Record<string, MiddlewareEntry<Input, Output>>; |
-91
| # @smithy/middleware-stack | ||
| [](https://www.npmjs.com/package/@smithy/middleware-stack) | ||
| [](https://www.npmjs.com/package/@smithy/middleware-stack) | ||
| ### :warning: Internal API :warning: | ||
| > This is an internal package. | ||
| > That means this is used as a dependency for other, public packages, but | ||
| > should not be taken directly as a dependency in your application's `package.json`. | ||
| > If you are updating the version of this package, for example to bring in a | ||
| > bug-fix, you should do so by updating your application lockfile with | ||
| > e.g. `npm up @scope/package` or equivalent command in another | ||
| > package manager, rather than taking a direct dependency. | ||
| --- | ||
| 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, { | ||
| relation: "before", //or 'after' | ||
| toMiddleware: "myMiddleware", | ||
| }); | ||
| ``` | ||
| ## 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"); | ||
| ``` |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
Trivial Package
Supply chain riskPackages less than 10 lines of code are easily copied into your own project and may not warrant the additional supply chain risk of an external dependency.
Found 1 instance in 1 package
0
-100%13105
-68.76%5
-50%8
-98.65%1
Infinity%0
-100%2
Infinity%+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
- Removed