-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 65b5356
Showing
9 changed files
with
9,133 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
module.exports = { | ||
root: true, | ||
extends: 'prettier', | ||
plugins: ['import', 'prettier'], | ||
env: { | ||
es6: true, | ||
node: true, | ||
jest: true | ||
}, | ||
parserOptions: { | ||
ecmaVersion: 2017, | ||
sourceType: 'module' | ||
}, | ||
rules: { | ||
'array-bracket-spacing': [ | ||
'error', | ||
'never', | ||
{ | ||
objectsInArrays: false, | ||
arraysInArrays: false | ||
} | ||
], | ||
'arrow-parens': ['error', 'always'], | ||
'comma-dangle': ['error', 'never'], | ||
'func-names': 'off', | ||
'no-use-before-define': 'off', | ||
'prefer-destructuring': 'off', | ||
'no-console': 'error', | ||
'no-shadow': 'error', | ||
'no-undef': 'error', | ||
'object-curly-newline': 'off', | ||
'no-unused-vars': 'error', | ||
'semi': 'off', | ||
'object-shorthand': 'off', | ||
'prettier/prettier': 'error', | ||
'prefer-const': 'error' | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# Logs | ||
*.log | ||
npm-debug.log | ||
|
||
# Runtime data | ||
pids | ||
*.pid | ||
*.seed | ||
dist | ||
|
||
# Directory for instrumented libs generated by jscoverage/JSCover | ||
lib-cov | ||
|
||
# Coverage directory used by tools like istanbul | ||
coverage | ||
.nyc_output | ||
|
||
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) | ||
.grunt | ||
|
||
# node-waf configuration | ||
.lock-wscript | ||
|
||
# Compiled binary addons (http://nodejs.org/api/addons.html) | ||
build/Release | ||
|
||
# Dependency directory | ||
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git | ||
node_modules | ||
|
||
# IDE stuff | ||
**/.idea | ||
|
||
# OS stuff | ||
.DS_Store | ||
.tmp | ||
|
||
# Serverless stuff | ||
admin.env | ||
.env | ||
tmp | ||
.coveralls.yml | ||
tmpdirs-serverless | ||
.eslintcache | ||
.serverless | ||
.vscode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = { extends: ['@commitlint/config-conventional'] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
'use strict' | ||
|
||
class ServerlessLambdaEdgePreExistingCloudFront { | ||
constructor(serverless, options) { | ||
this.serverless = serverless | ||
this.options = options || {} | ||
this.provider = this.serverless.getProvider('aws') | ||
this.service = this.serverless.service.service | ||
this.region = this.provider.getRegion() | ||
this.stage = this.provider.getStage() | ||
|
||
this.hooks = { | ||
'after:aws:deploy:finalize:cleanup': async () => { | ||
this.serverless.service.getAllFunctions().forEach(async (functionName) => { | ||
const functionObj = this.serverless.service.getFunction(functionName) | ||
if (functionObj.events) { | ||
functionObj.events.forEach(async (event) => { | ||
if (event.preExistingCloudFront) { | ||
const config = await this.provider.request('CloudFront', 'getDistribution', { | ||
Id: event.preExistingCloudFront.distributionId | ||
}) | ||
|
||
if (event.preExistingCloudFront.pathPattern === '*') { | ||
config.DistributionConfig.DefaultCacheBehavior.LambdaFunctionAssociations = await this.associateFunction( | ||
config.DistributionConfig.DefaultCacheBehavior.LambdaFunctionAssociations, | ||
event, | ||
functionObj.name | ||
) | ||
} else { | ||
config.DistributionConfig.CacheBehaviors = await this.associateNonDefaultCacheBehaviors( | ||
config.DistributionConfig.CacheBehaviors, | ||
event, | ||
functionObj.name | ||
) | ||
} | ||
|
||
this.provider.request('CloudFront', 'updateDistribution', { | ||
Id: event.preExistingCloudFront.distributionId, | ||
IfMatch: config.ETag, | ||
DistributionConfig: config.DistributionConfig | ||
}) | ||
} | ||
}) | ||
} | ||
}) | ||
} | ||
} | ||
} | ||
|
||
async associateNonDefaultCacheBehaviors(cacheBehaviors, event, functionName) { | ||
for (let i = 0; i < cacheBehaviors.Items.length; i++) { | ||
if (event.preExistingCloudFront.pathPattern === cacheBehaviors.Items[i].PathPattern) { | ||
cacheBehaviors.Items[i].LambdaFunctionAssociations = await this.associateFunction( | ||
cacheBehaviors.Items[i].LambdaFunctionAssociations, | ||
event, | ||
functionName | ||
) | ||
} | ||
} | ||
return cacheBehaviors | ||
} | ||
|
||
async associateFunction(lambdaFunctionAssociations, event, functionName) { | ||
const originals = lambdaFunctionAssociations.Items.filter( | ||
(x) => x.EventType !== event.preExistingCloudFront.eventType | ||
) | ||
lambdaFunctionAssociations.Items = originals | ||
lambdaFunctionAssociations.Items.push({ | ||
LambdaFunctionARN: await this.getlatestVersionLambdaArn(functionName), | ||
IncludeBody: event.preExistingCloudFront.includeBody, | ||
EventType: event.preExistingCloudFront.eventType | ||
}) | ||
lambdaFunctionAssociations.Quantity = lambdaFunctionAssociations.Items.length | ||
return lambdaFunctionAssociations | ||
} | ||
|
||
async getlatestVersionLambdaArn(functionName, marker) { | ||
const args = { | ||
FunctionName: functionName, | ||
MaxItems: 50 | ||
} | ||
|
||
if (marker) { | ||
args['Marker'] = marker | ||
} | ||
|
||
const versions = await this.provider.request('Lambda', 'listVersionsByFunction', args) | ||
|
||
if (versions.NextMarker !== null) { | ||
return await this.getlatestVersion(functionName, versions.NextMarker) | ||
} | ||
let arn | ||
versions.Versions.forEach(async (functionObj) => { | ||
arn = functionObj.FunctionArn | ||
}) | ||
return arn | ||
} | ||
} | ||
|
||
module.exports = ServerlessLambdaEdgePreExistingCloudFront |
Oops, something went wrong.