Socket
Socket
Sign inDemoInstall

middy

Package Overview
Dependencies
Maintainers
8
Versions
147
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

middy - npm Package Compare versions

Comparing version 0.6.0 to 0.6.1

2

package.json
{
"name": "middy",
"version": "0.6.0",
"version": "0.6.1",
"description": "The simple (but cool 😎) middleware engine for AWS lambda in Node.js",

@@ -5,0 +5,0 @@ "main": "src/index.js",

@@ -233,2 +233,49 @@ <div align="center">

### Interrupt middleware execution early
Some middlewares might need to stop the whole execution flow and return a response immediately.
If you want to do this you can invoke `handler.callback` in your middleware and return early without invoking `next`.
**Note**: this will totally stop the execution of successive middlewares in any phase (`before` and `after`) and returns
and early response (or an error) directly at the lambda level. If you middlewares that do specific task on every requests
like output serialization or error handling, those won't be invoked in this case.
In this example we can use this capability for building a sample caching middleware:
```javascript
// some function that calculates the cache id based on the current event
const calculateCacheId = (event) => { /* ... */ }
const storage = {}
// middleware
const cacheMiddleware = (options) => {
let cacheKey
return ({
before: (handler, next) => {
cacheKey = options.calculateCacheId(handler.event)
if (options.storage.hasOwnProperty(cacheKey)) {
// exits early and returns the value from the cache if it's already there
return handler.callback(null, options.storage[cacheKey])
}
return next()
},
after: (handler, next) => {
// stores the calculated response in the cache
options.storage[cacheKey] = handler.response
next()
}
})
}
// sample Usage
const handler = middy((event, context, callback) => { /* ... */ })
.use(cacheMiddleware({
calculateCacheId, storage
}))
```
### Handling errors

@@ -563,3 +610,3 @@

| --- | --- | --- |
| handler | <code>function</code> | the original handler function. It will expose properties `event`, `context`, `response` and `error` that can be used to interact with the middleware lifecycle |
| handler | <code>function</code> | the original handler function. It will expose properties `event`, `context`, `response`, `error` and `callback` that can be used to interact with the middleware lifecycle |
| next | [<code>middlewareNextFunction</code>](#middlewareNextFunction) | the callback to invoke to pass the control to the next middleware |

@@ -566,0 +613,0 @@

@@ -567,2 +567,27 @@ const middy = require('../middy')

})
test('Middlewares can be stopped by calling the callback from the context', (endTest) => {
const beforeMiddleware = (handler, next) => {
// calling the handler.callback directly and not calling next()
return handler.callback(null, 'ending early')
}
const beforeMiddleware2 = jest.fn()
const originalHandler = jest.fn()
const afterMiddleware = jest.fn()
const handler = middy(originalHandler)
.before(beforeMiddleware)
.before(beforeMiddleware2)
.after(afterMiddleware)
handler({}, {}, (err, response) => {
expect(err).toBeNull()
expect(response).toEqual('ending early')
expect(beforeMiddleware2).not.toHaveBeenCalled()
expect(originalHandler).not.toHaveBeenCalled()
expect(afterMiddleware).not.toHaveBeenCalled()
endTest()
})
})
})

@@ -42,3 +42,3 @@ const isPromise = require('./isPromise')

* @param {function} handler - the original handler function.
* It will expose properties `event`, `context`, `response` and `error` that can
* It will expose properties `event`, `context`, `response`, `error` and `callback` that can
* be used to interact with the middleware lifecycle

@@ -146,2 +146,3 @@ * @param {middlewareNextFunction} next - the callback to invoke to pass the control to the next middleware

instance.context = context
instance.callback = callback
instance.response = null

@@ -148,0 +149,0 @@ instance.error = null

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc