Skip to content

Commit

Permalink
Dependency Performance Improvements (middyjs#442)
Browse files Browse the repository at this point in the history
* fix: move dep include out of middleware functions

* fix: only include relevent clients

* version bump
  • Loading branch information
willfarrell authored and benjifs committed May 21, 2020
1 parent b7cb9ee commit e04c823
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 35 deletions.
2 changes: 1 addition & 1 deletion packages/http-security-headers/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 15 additions & 15 deletions packages/http-urlencode-body-parser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
})
}
2 changes: 1 addition & 1 deletion packages/ssm/__tests__/index.js
Original file line number Diff line number Diff line change
@@ -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('../')

Expand Down
20 changes: 2 additions & 18 deletions packages/ssm/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const SSM = require('aws-sdk/clients/ssm')
let ssmInstance

module.exports = opts => {
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit e04c823

Please sign in to comment.