Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@middy/cache

Package Overview
Dependencies
Maintainers
10
Versions
87
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@middy/cache - npm Package Compare versions

Comparing version 1.0.0-alpha.58 to 1.0.0-alpha.59

67

__tests__/index.js

@@ -55,2 +55,69 @@ const { invoke } = require('../../test-helpers')

})
test('It should cache things using custom cache settings and discard result of a resolve promised with setValue', async () => {
const calculateCacheId = (event) => Promise.resolve(event.id)
const myStorage = {}
const getValue = (key) => new Promise((resolve, reject) => {
setTimeout(() => resolve(myStorage[key]), 50)
})
const setValue = (key, value) => new Promise((resolve, reject) => {
setTimeout(() => {
myStorage[key] = value
return resolve('Value to discard')
}, 50)
})
const originalHandler = jest.fn((event, context, cb) => {
cb(null, event.a + event.b)
})
const handler = middy(originalHandler)
.use(cache({
calculateCacheId,
getValue,
setValue
}))
const event = { id: 'some_unique_id', a: 2, b: 3 }
const response = await invoke(handler, event)
const response2 = await invoke(handler, event)
expect(response).toEqual(response2)
expect(originalHandler.mock.calls.length).toBe(1)
})
test('It should throw en error if setValue return a rejected promise', async () => {
const errMessage = 'Something bad happened'
const calculateCacheId = (event) => Promise.resolve(event.id)
const myStorage = {}
const getValue = (key) => new Promise((resolve, reject) => {
setTimeout(() => resolve(myStorage[key]), 50)
})
const setValue = (key, value) => new Promise((resolve, reject) => {
setTimeout(() => {
myStorage[key] = value
return reject(new Error(errMessage))
}, 50)
})
const originalHandler = jest.fn((event, context, cb) => {
cb(null, event.a + event.b)
})
const handler = middy(originalHandler)
.use(cache({
calculateCacheId,
getValue,
setValue
}))
const event = { id: 'some_unique_id', a: 2, b: 3 }
try {
await invoke(handler, event)
} catch (e) {
expect(e.message).toEqual(errMessage)
expect(originalHandler.mock.calls.length).toBe(1)
}
})
})

3

index.js

@@ -35,5 +35,6 @@ const { createHash } = require('crypto')

options.setValue(currentCacheKey, handler.response)
.then(next)
.then(() => next())
.catch(err => next(err))
}
})
}
{
"name": "@middy/cache",
"version": "1.0.0-alpha.58",
"version": "1.0.0-alpha.59",
"description": "Cache middleware for the middy framework",

@@ -44,6 +44,6 @@ "engines": {

"devDependencies": {
"@middy/core": "^1.0.0-alpha.58",
"@middy/core": "^1.0.0-alpha.59",
"es6-promisify": "^6.0.2"
},
"gitHead": "df2dd587d4a43540950eaba77e913ade322ca32b"
"gitHead": "338ed037a83b0ae46af3ed5b94bedea85fd6156a"
}
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