serverless-api-gateway-caching
Advanced tools
Comparing version 1.0.0-rc15 to 1.0.0-rc16
{ | ||
"name": "serverless-api-gateway-caching", | ||
"version": "1.0.0-rc15", | ||
"version": "1.0.0-rc16", | ||
"description": "A plugin for the serverless framework which helps with configuring caching for API Gateway endpoints.", | ||
@@ -33,5 +33,7 @@ "main": "src/apiGatewayCachingPlugin.js", | ||
"devDependencies": { | ||
"aws-sdk-mock": "^4.1.0", | ||
"chai": "^4.1.2", | ||
"chance": "^1.0.16", | ||
"mocha": "^5.2.0" | ||
} | ||
} |
@@ -6,2 +6,6 @@ # serverless-api-gateway-caching | ||
## Good to know | ||
If you enable caching globally, it does NOT automatically enable caching for your endpoints - you have to be explicit about which endpoints should have caching enabled. | ||
However, disabling caching globally disables it across endpoints. | ||
## Example | ||
@@ -50,3 +54,5 @@ | ||
## Limitations | ||
Currently not supported: | ||
- lambda functions with many http events | ||
* For HTTP method `ANY`, caching will be enabled only for the `GET` method and disabled for the other methods. | ||
## Currently not supported: | ||
* lambda functions with many http events |
@@ -24,2 +24,3 @@ 'use strict'; | ||
updateCloudFormationTemplate() { | ||
// if caching is not defined or disabled | ||
if (!this.settings.cachingEnabled) { | ||
@@ -44,6 +45,2 @@ return; | ||
updateStage() { | ||
if (!this.settings.cachingEnabled) { | ||
return; | ||
} | ||
return updateStageCacheSettings(this.settings, this.serverless); | ||
@@ -50,0 +47,0 @@ } |
@@ -14,3 +14,3 @@ const isEmpty = require('lodash.isempty'); | ||
} | ||
this.cachingEnabled = cachingConfig.enabled; | ||
this.cachingEnabled = globalSettings.cachingEnabled ? cachingConfig.enabled : false; | ||
this.cacheTtlInSeconds = cachingConfig.ttlInSeconds || globalSettings.cacheTtlInSeconds; | ||
@@ -23,10 +23,6 @@ this.cacheKeyParameters = cachingConfig.cacheKeyParameters; | ||
constructor(serverless, options) { | ||
if (!get(serverless, "service.custom.apiGatewayCaching")) { | ||
this.cachingEnabled = false; | ||
if (!get(serverless, 'service.custom.apiGatewayCaching')) { | ||
return; | ||
} | ||
this.cachingEnabled = serverless.service.custom.apiGatewayCaching.enabled; | ||
if (!this.cachingEnabled) { | ||
return; | ||
} | ||
@@ -41,6 +37,7 @@ if (options) { | ||
this.endpointSettings = []; | ||
this.cacheClusterSize = serverless.service.custom.apiGatewayCaching.clusterSize; | ||
this.cacheTtlInSeconds = serverless.service.custom.apiGatewayCaching.ttlInSeconds; | ||
this.endpointSettings = []; | ||
for (let functionName in serverless.service.functions) { | ||
@@ -47,0 +44,0 @@ let functionSettings = serverless.service.functions[functionName]; |
@@ -34,19 +34,19 @@ const isEmpty = require('lodash.isempty'); | ||
const createPatchForStage = (settings) => { | ||
return [ | ||
{ | ||
let patch = [{ | ||
op: 'replace', | ||
path: '/cacheClusterEnabled', | ||
value: `${settings.cachingEnabled}` | ||
}] | ||
if (settings.cachingEnabled) { | ||
patch.push({ | ||
op: 'replace', | ||
path: '/cacheClusterEnabled', | ||
value: `${settings.cachingEnabled}` | ||
}, | ||
{ | ||
op: 'replace', | ||
path: '/cacheClusterSize', | ||
value: `${settings.cacheClusterSize}` | ||
} | ||
] | ||
}); | ||
} | ||
return patch; | ||
} | ||
const createPatchForEndpoint = (endpointSettings, serverless) => { | ||
const patchPath = patchPathFor(endpointSettings, serverless); | ||
if (!patchPath) return []; | ||
const patchForMethod = (path, method, endpointSettings) => { | ||
let patchPath = patchPathFor(path, method); | ||
let patch = [{ | ||
@@ -56,3 +56,3 @@ op: 'replace', | ||
value: `${endpointSettings.cachingEnabled}` | ||
}] | ||
}]; | ||
if (endpointSettings.cachingEnabled) { | ||
@@ -68,3 +68,3 @@ patch.push({ | ||
const patchPathFor = (endpointSettings, serverless) => { | ||
const createPatchForEndpoint = (endpointSettings, serverless) => { | ||
let lambda = serverless.service.getFunction(endpointSettings.functionName); | ||
@@ -80,4 +80,25 @@ if (isEmpty(lambda.events)) { | ||
let { path, method } = httpEvents[0].http; | ||
let patch = []; | ||
if (method.toUpperCase() == 'ANY') { | ||
let httpMethodsToDisableCacheFor = ['DELETE', 'HEAD', 'OPTIONS', 'PATCH', 'POST', 'PUT']; // TODO could come from settings, vNext | ||
for (let methodWithCacheDisabled of httpMethodsToDisableCacheFor) { | ||
patch = patch.concat(patchForMethod(path, methodWithCacheDisabled, | ||
{ cachingEnabled: false })); | ||
}; | ||
patch = patch.concat(patchForMethod(path, 'GET', endpointSettings)); | ||
} | ||
else { | ||
patch = patch.concat(patchForMethod(path, method, endpointSettings)); | ||
} | ||
return patch; | ||
} | ||
const patchPathFor = (path, method) => { | ||
let escapedPath = escapeJsonPointer(path); | ||
let patchPath = `~1${escapedPath}/${method.toUpperCase()}`; | ||
if (!escapedPath.startsWith('~1')) { | ||
escapedPath = `~1${escapedPath}`; | ||
} | ||
let patchPath = `${escapedPath}/${method.toUpperCase()}`; | ||
return patchPath; | ||
@@ -87,2 +108,7 @@ } | ||
const updateStageCacheSettings = async (settings, serverless) => { | ||
// do nothing if caching settings are not defined | ||
if (settings.cachingEnabled == undefined) { | ||
return; | ||
} | ||
let restApiId = await getRestApiId(settings, serverless); | ||
@@ -95,2 +121,8 @@ | ||
let patchOps = createPatchForStage(settings); | ||
let endpointsWithCachingEnabled = settings.endpointSettings.filter(e => e.cachingEnabled); | ||
if (settings.cachingEnabled && isEmpty(endpointsWithCachingEnabled)) { | ||
serverless.cli.log(`[serverless-api-gateway-caching] [WARNING] API Gateway caching is enabled but none of the endpoints have caching enabled`); | ||
} | ||
for (let endpointSettings of settings.endpointSettings) { | ||
@@ -97,0 +129,0 @@ let endpointPatch = createPatchForEndpoint(endpointSettings, serverless); |
13790
276
57
4