From e04c82383456095c83096662b86319a0bda874a9 Mon Sep 17 00:00:00 2001 From: will Farrell Date: Wed, 11 Dec 2019 07:00:56 -0700 Subject: [PATCH] Dependency Performance Improvements (#442) * fix: move dep include out of middleware functions * fix: only include relevent clients * version bump --- .../http-security-headers/package-lock.json | 2 +- packages/http-urlencode-body-parser/index.js | 30 +++++++++---------- packages/ssm/__tests__/index.js | 2 +- packages/ssm/index.js | 20 ++----------- 4 files changed, 19 insertions(+), 35 deletions(-) diff --git a/packages/http-security-headers/package-lock.json b/packages/http-security-headers/package-lock.json index 4e760140e..6e864c776 100644 --- a/packages/http-security-headers/package-lock.json +++ b/packages/http-security-headers/package-lock.json @@ -1,6 +1,6 @@ { "name": "@middy/http-security-headers", - "version": "1.0.0-alpha.55", + "version": "1.0.0-alpha.56", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/packages/http-urlencode-body-parser/index.js b/packages/http-urlencode-body-parser/index.js index 6d691d442..1bb23d2b1 100644 --- a/packages/http-urlencode-body-parser/index.js +++ b/packages/http-urlencode-body-parser/index.js @@ -4,23 +4,23 @@ const defaults = { extended: false } -module.exports = (opts) => ({ - before: (handler, next) => { - const options = Object.assign({}, defaults, opts) +module.exports = (opts) => { + const options = Object.assign({}, defaults, opts) + const parserFn = options.extended ? require('qs').parse : require('querystring').decode + return { + before: (handler, next) => { + if (handler.event.headers) { + const contentTypeHeader = handler.event.headers['content-type'] || handler.event.headers['Content-Type'] + if (contentTypeHeader) { + const { type } = contentType.parse(contentTypeHeader) - const parserFn = options.extended ? require('qs').parse : require('querystring').decode - - if (handler.event.headers) { - const contentTypeHeader = handler.event.headers['content-type'] || handler.event.headers['Content-Type'] - if (contentTypeHeader) { - const { type } = contentType.parse(contentTypeHeader) - - if (type === 'application/x-www-form-urlencoded') { - handler.event.body = parserFn(handler.event.body) + if (type === 'application/x-www-form-urlencoded') { + handler.event.body = parserFn(handler.event.body) + } } } - } - next() + next() + } } -}) +} diff --git a/packages/ssm/__tests__/index.js b/packages/ssm/__tests__/index.js index 84a3b0b9a..9a066643d 100644 --- a/packages/ssm/__tests__/index.js +++ b/packages/ssm/__tests__/index.js @@ -1,6 +1,6 @@ jest.mock('aws-sdk') -const { SSM } = require('aws-sdk') +const SSM = require('aws-sdk/clients/ssm') const middy = require('../../core') const ssm = require('../') diff --git a/packages/ssm/index.js b/packages/ssm/index.js index a3f8a591a..dd261b167 100644 --- a/packages/ssm/index.js +++ b/packages/ssm/index.js @@ -1,3 +1,4 @@ +const SSM = require('aws-sdk/clients/ssm') let ssmInstance module.exports = opts => { @@ -31,7 +32,7 @@ module.exports = opts => { return next() } - ssmInstance = ssmInstance || getSSMInstance(options.awsSdkOptions) + ssmInstance = ssmInstance || new SSM(options.awsSdkOptions) const ssmPromises = Object.keys(options.paths).reduce( (aggregator, prefix) => { @@ -144,23 +145,6 @@ const getTargetObjectToAssign = (handler, options) => const getSSMParamValues = userParamsMap => [...new Set(Object.keys(userParamsMap).map(key => userParamsMap[key]))] -/** - * Lazily load aws-sdk and initialize SSM constructor - * to avoid performance penalties for those who doesn't use - * this middleware. Sets ssmInstance var at the top of the module - * or returns if it's already initialized - * @param {Object} awsSdkOptions Options to use to initialize aws sdk constructor - */ -const getSSMInstance = awsSdkOptions => { - // lazy load aws-sdk and SSM constructor to avoid performance - // penalties if you don't use this middleware - - // AWS Lambda has aws-sdk included version 2.176.0 - // see https://docs.aws.amazon.com/lambda/latest/dg/current-supported-versions.html - const { SSM } = require('aws-sdk') - return new SSM(awsSdkOptions) -} - /** * Throw error if SSM returns an error because we asked for params that don't exist * @throws {Error} When any invalid parameters found in response