-
Notifications
You must be signed in to change notification settings - Fork 167
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Lambda@Edge Origin Request Support #196
Open
lolJS
wants to merge
4
commits into
dougmoscrop:master
Choose a base branch
from
lolJS:lambda-edge-support
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
53491ad
feat: add lambda edge origin request to AWS
eelayoubi 5b6429d
Merge pull request #1 from eelayoubi/feature/create-lambda-edge-type-…
eelayoubi 1ca1b45
Merge branch 'master' of github.com:dougmoscrop/serverless-http into …
lolJS 47fc7f8
clean up
lolJS File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
File renamed without changes.
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
4 changes: 2 additions & 2 deletions
4
lib/provider/aws/format-response.js → lib/provider/aws/api-gw/format-response.js
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
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,15 @@ | ||
const cleanUpEvent = require('./clean-up-event'); | ||
|
||
const createRequest = require('./create-request'); | ||
const formatResponse = require('./format-response'); | ||
|
||
module.exports = options => { | ||
return getResponse => async (event_, context = {}) => { | ||
const event = cleanUpEvent(event_, options); | ||
|
||
const request = createRequest(event, context, options); | ||
const response = await getResponse(request, event, context); | ||
|
||
return formatResponse(event, response, options); | ||
}; | ||
}; |
File renamed without changes.
File renamed without changes.
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 |
---|---|---|
@@ -1,15 +1,11 @@ | ||
const cleanUpEvent = require('./clean-up-event'); | ||
|
||
const createRequest = require('./create-request'); | ||
const formatResponse = require('./format-response'); | ||
const apiGw = require('./api-gw'); | ||
const lambdaEdgeOriginRequest = require('./lambda-edge-origin-request'); | ||
|
||
module.exports = options => { | ||
return getResponse => async (event_, context = {}) => { | ||
const event = cleanUpEvent(event_, options); | ||
|
||
const request = createRequest(event, context, options); | ||
const response = await getResponse(request, event, context); | ||
|
||
return formatResponse(event, response, options); | ||
}; | ||
switch (options.type) { | ||
case 'lambda-edge-origin-request': | ||
return lambdaEdgeOriginRequest(options) | ||
default: | ||
return apiGw(options) | ||
} | ||
}; |
28 changes: 28 additions & 0 deletions
28
lib/provider/aws/lambda-edge-origin-request/clean-up-event.js
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,28 @@ | ||
'use strict'; | ||
|
||
function removeBasePath(path = '/', basePath) { | ||
if (basePath) { | ||
const basePathIndex = path.indexOf(basePath); | ||
|
||
if (basePathIndex > -1) { | ||
return path.substr(basePathIndex + basePath.length) || '/'; | ||
} | ||
} | ||
|
||
return path; | ||
} | ||
|
||
module.exports = function cleanupEvent(evt, options) { | ||
const event = evt || {}; | ||
|
||
event.config = event.config || {}; | ||
|
||
event.request = event.request || {}; | ||
event.request.body = event.request.body || {}; | ||
event.request.headers = event.request.headers || {}; | ||
event.request.method = event.request.method || 'GET'; | ||
console.debug(`url is ${event.request.uri} basePath is ${options.basePath}`); | ||
event.request.uri = removeBasePath(event.uri, options.basePath); | ||
|
||
return event; | ||
}; |
64 changes: 64 additions & 0 deletions
64
lib/provider/aws/lambda-edge-origin-request/create-request.js
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,64 @@ | ||
'use strict'; | ||
|
||
const crypto = require('crypto'); | ||
const Request = require('../../../request'); | ||
|
||
function requestHeaders(event) { | ||
let headers = Object.keys(event.request.headers).reduce((headers, key) => { | ||
headers[event.request.headers[key][0].key.toLowerCase()] = event.request.headers[key][0].value; | ||
return headers; | ||
}, {}); | ||
|
||
headers['x-request-id'] = crypto.randomBytes(30).toString('base64') | ||
|
||
return headers; | ||
} | ||
|
||
function requestBody(event) { | ||
const body = event && event.request && event.request.body && event.request.body.data; | ||
const type = typeof body; | ||
|
||
console.debug('hello world'); | ||
if (!body) return ''; | ||
|
||
if (Buffer.isBuffer(body)) return body; | ||
|
||
switch (type) { | ||
case 'string': | ||
return Buffer.from(body, event.request.body.encoding === 'base64' ? 'base64' : 'utf8'); | ||
case 'object': | ||
return Buffer.from(JSON.stringify(body)); | ||
default: | ||
throw new Error(`Unexpected event.body type: ${typeof event.request.body.data}`); | ||
} | ||
} | ||
|
||
function getUrl(path, queryString) { | ||
if (queryString) { | ||
return `${path}?${queryString}` | ||
} | ||
|
||
return path; | ||
} | ||
|
||
module.exports = (event, options) => { | ||
const method = event.request.method; | ||
const remoteAddress = event.request.clientIp; | ||
const headers = requestHeaders(event); | ||
const body = requestBody(event); | ||
|
||
if (typeof options.requestId === 'string' && options.requestId.length > 0) { | ||
const header = options.requestId.toLowerCase(); | ||
headers[header] = headers[header] || event.config.requestId; | ||
} | ||
|
||
const req = new Request({ | ||
method, | ||
headers, | ||
body, | ||
remoteAddress, | ||
url: getUrl(event.request.uri, event.request.querystring) | ||
}); | ||
|
||
return req; | ||
}; |
25 changes: 25 additions & 0 deletions
25
lib/provider/aws/lambda-edge-origin-request/format-response.js
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,25 @@ | ||
'use strict'; | ||
|
||
const isBinary = require('./is-binary'); | ||
const Response = require('../../../response'); | ||
const sanitizeHeaders = require('./sanitize-headers'); | ||
|
||
module.exports = (response, options) => { | ||
const { statusCode } = response; | ||
const headers = Response.headers(response); | ||
|
||
if (headers['transfer-encoding'] === 'chunked' || response.chunkedEncoding) { | ||
throw new Error('chunked encoding not supported'); | ||
} | ||
|
||
const isBase64Encoded = isBinary(headers, options); | ||
const encoding = isBase64Encoded ? 'base64' : 'utf8'; | ||
let body = Response.body(response).toString(encoding); | ||
|
||
return { | ||
statusCode, | ||
headers: sanitizeHeaders(headers), | ||
body, | ||
bodyEncoding: isBase64Encoded ? 'base64' : 'text' | ||
}; | ||
}; |
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,16 @@ | ||
const cleanUpEvent = require('./clean-up-event'); | ||
|
||
const createRequest = require('./create-request'); | ||
const formatResponse = require('./format-response'); | ||
|
||
module.exports = options => { | ||
return getResponse => async (event_, context = {}) => { | ||
const event = cleanUpEvent(event_, options); | ||
console.debug(event); | ||
const request = createRequest(event, options); | ||
const response = await getResponse(request, event, context); | ||
const formattedResponse = formatResponse(response, options); | ||
|
||
return formattedResponse; | ||
}; | ||
}; |
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 @@ | ||
'use strict'; | ||
|
||
const BINARY_ENCODINGS = ['gzip', 'deflate', 'br']; | ||
const BINARY_CONTENT_TYPES = (process.env.BINARY_CONTENT_TYPES || '').split(','); | ||
|
||
function isBinaryEncoding(headers) { | ||
const contentEncoding = headers['content-encoding']; | ||
|
||
if (typeof contentEncoding === 'string') { | ||
return contentEncoding.split(',').some(value => | ||
BINARY_ENCODINGS.some(binaryEncoding => value.indexOf(binaryEncoding) !== -1) | ||
); | ||
} | ||
} | ||
|
||
function isBinaryContent(headers, options) { | ||
const contentTypes = [].concat(options.binary | ||
? options.binary | ||
: BINARY_CONTENT_TYPES | ||
).map(candidate => | ||
new RegExp(`^${candidate.replace(/\*/g, '.*')}$`) | ||
); | ||
|
||
const contentType = (headers['content-type'] || '').split(';')[0]; | ||
return !!contentType && contentTypes.some(candidate => candidate.test(contentType)); | ||
} | ||
|
||
module.exports = function isBinary(headers, options) { | ||
if (options.binary === false) { | ||
return false; | ||
} | ||
|
||
if (typeof options.binary === 'function') { | ||
return options.binary(headers); | ||
} | ||
|
||
return isBinaryEncoding(headers) || isBinaryContent(headers, options); | ||
}; |
61 changes: 61 additions & 0 deletions
61
lib/provider/aws/lambda-edge-origin-request/sanitize-headers.js
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,61 @@ | ||
'use strict'; | ||
|
||
// See: https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-requirements-limits.html#lambda-read-only-headers | ||
const readOnlyHeaders = [ | ||
'accept-encoding', | ||
'content-length', | ||
'if-modified-since', | ||
'if-none-Match', | ||
'if-range', | ||
'if-unmodified-since', | ||
'range', | ||
'transfer-encoding', | ||
'via' | ||
]; | ||
|
||
// See: https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-requirements-limits.html#lambda-blacklisted-headers | ||
const blacklistedHeaders = [ | ||
'connection', | ||
'expect', | ||
'keep-alive', | ||
'proxy-authenticate', | ||
'proxy-authorization', | ||
'proxy-connection', | ||
'trailer', | ||
'upgrade', | ||
'x-accel-buffering', | ||
'x-accel-charset', | ||
'x-accel-limit-rate', | ||
'x-accel-redirect', | ||
'x-cache', | ||
'x-forwarded-proto', | ||
'x-real-ip', | ||
] | ||
|
||
const omittedHeaders = [...readOnlyHeaders, ...blacklistedHeaders] | ||
|
||
module.exports = function sanitizeHeaders(headers) { | ||
return Object.keys(headers).reduce((memo, key) => { | ||
const value = headers[key]; | ||
const normalizedKey = key.toLowerCase(); | ||
|
||
if (omittedHeaders.includes(normalizedKey)) { | ||
return memo; | ||
} | ||
|
||
if (memo[normalizedKey] === undefined) { | ||
memo[normalizedKey] = [] | ||
} | ||
|
||
const valueArray = Array.isArray(value) ? value : [value] | ||
|
||
valueArray.forEach(valueElement => { | ||
memo[normalizedKey].push({ | ||
key: key, | ||
value: valueElement | ||
}); | ||
}); | ||
|
||
return memo; | ||
}, {}); | ||
}; |
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be here?