Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
horike37 committed Mar 17, 2020
0 parents commit 65b5356
Show file tree
Hide file tree
Showing 9 changed files with 9,133 additions and 0 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
38 changes: 38 additions & 0 deletions .eslintrc.js
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'
}
}
46 changes: 46 additions & 0 deletions .gitignore
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
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
1 change: 1 addition & 0 deletions commitlint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = { extends: ['@commitlint/config-conventional'] }
100 changes: 100 additions & 0 deletions index.js
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
Loading

0 comments on commit 65b5356

Please sign in to comment.