Last active
May 29, 2018 09:05
-
-
Save joona/137359d622a0d37fe71a3460963c73b2 to your computer and use it in GitHub Desktop.
AWS lambda proxy request handler for https://www.npmjs.com/package/micro.
This file contains hidden or 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
const { send } = require('micro'); | |
module.exports = (req, res) => { | |
send(res, 200, { message: 'Hello world!' }); | |
} |
This file contains hidden or 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
const api = require('./api'); | |
const microHandler = require('./micro_handler'); | |
module.exports.handler = microHandler(api); |
This file contains hidden or 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
const Stream = require('stream'); | |
const { run } = require('micro'); | |
const qs = require('query-string'); | |
function createRequest(event, context) { | |
var request = new Stream.Readable(); | |
request._read = function noop(){}; | |
if(event.body){ | |
request.push(event.body); | |
} | |
request.push(null); | |
request.url = event.path; | |
if(event.queryStringParameters) { | |
request.url += "?" + qs.stringify(event.queryStringParameters); | |
} | |
request.apiGatewayContext = event.requestContext; | |
request.lambdaContext = context; | |
request.headers = {}; | |
request.rawHeaders = []; | |
request.httpVersion = "1.1"; | |
request.method = event.httpMethod; | |
Object.keys(event.headers || {}) | |
.forEach(key => { | |
request.headers[key.toLowerCase()] = event.headers[key]; | |
request.rawHeaders.push(key); | |
request.rawHeaders.push(event.headers[key]); | |
}); | |
return request; | |
} | |
function createResponse() { | |
var response = new Stream.Writable(); | |
response.statusCode = 200; | |
response.headers = {}; | |
response.body = ""; | |
response.setHeader = function(key,val) { | |
response.headers[key] = val; | |
}; | |
response.getHeader = function(key) { | |
return response.headers[key]; | |
}; | |
response._write = function (chunk, encoding, done) { | |
response.body += chunk.toString(); | |
done(); | |
}; | |
var _end = response.end; | |
response.end = function(data) { | |
response.headersSent = true; | |
_end.call(response, data); | |
}; | |
response.toLambdaResponse = function(){ | |
return { | |
statusCode: response.statusCode, | |
headers: response.headers, | |
body: response.body | |
}; | |
} | |
return response; | |
} | |
function microToLambdaHandler(fn) { | |
return function lambdaHandler(event, context, callback) { | |
context.callbackWaitsForEmptyEventLoop = false; | |
var req = createRequest(event, context); | |
var res = createResponse(); | |
run(req,res,fn) | |
.then((x) => { | |
return Promise.resolve(); | |
}) | |
.then(() => callback(null,res.toLambdaResponse())) | |
} | |
} | |
module.exports = microToLambdaHandler; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment