lambda-router-adapter
Advanced tools
Comparing version 1.0.1 to 1.0.3
24
index.js
var request = require('request'); | ||
var response = require('response'); | ||
/** | ||
* This callback is displayed as part of the Requester class. | ||
* @callback httpCallback | ||
* @param {object} req an http request | ||
* @param {object} res an http response | ||
*/ | ||
/** | ||
* @param {httpCallback} callback | ||
* @returns {Function} | ||
*/ | ||
function newHandler(callback) { | ||
return function(event, context) { | ||
let req = request.convert(event); | ||
let res = response.newResponse((value) => { | ||
context.done(null, response.convert(value)); | ||
}); | ||
callback(req, res); | ||
} | ||
} | ||
module.exports = { | ||
convertRequest: request.convert, | ||
convertResponse: response.convert, | ||
newResponse: response.newResponse | ||
newResponse: response.newResponse, | ||
newHandler: newHandler | ||
} |
{ | ||
"name": "lambda-router-adapter", | ||
"version": "1.0.1", | ||
"description": "", | ||
"version": "1.0.3", | ||
"description": "Adapts Lambda Router Requests and Responses to NodeJS HTTP Request and Responses", | ||
"main": "index.js", | ||
@@ -6,0 +6,0 @@ "scripts": { |
# lambda-router-adapter | ||
Adapts Lamba Router Requests and Responses to NodeJS HTTP Request and Responses | ||
Adapts Lambda Router Requests and Responses to NodeJS HTTP Request and Responses. | ||
This is a utility library for implementing AWS Lambda functions that receive | ||
events from the | ||
[http://github.com/jamiemccrindle/lambda-router](github.com/jamiemccrindle/lambda-router) | ||
project. |
@@ -7,28 +7,35 @@ 'use strict'; | ||
function convert(lambdaRequest) { | ||
/** | ||
* Converts a Lambda Event to a NodeJS Http Request. | ||
* | ||
* @param {{ body: string, method: string, headers: Object.<string,string>, url: string}} lambdaRequest | ||
* @constructor | ||
*/ | ||
function LambdaHttpRequest(lambdaRequest) { | ||
streams.ReadableStream.call(this, lambdaRequest.body); | ||
this.httpVersionMajor = 1; | ||
this.httpVersionMinor = 1; | ||
this.httpVersion = 1.1; | ||
this.complete = false; | ||
this.trailers = {}; | ||
this.readable = true; | ||
var httpRequest = new streams.ReadableStream(lambdaRequest.body); | ||
this.method = lambdaRequest.method; | ||
this.url = lambdaRequest.url; | ||
this.originalUrl = lambdaRequest.url; | ||
this.path = url.parse(lambdaRequest.url).pathname; | ||
httpRequest.httpVersionMajor = 1; | ||
httpRequest.httpVersionMinor = 1; | ||
httpRequest.httpVersion = 1.1; | ||
httpRequest.complete = false; | ||
httpRequest.trailers = {}; | ||
httpRequest.readable = true; | ||
this.headers = lambdaRequest.headers; | ||
this.body = lambdaRequest.body; | ||
} | ||
httpRequest.method = lambdaRequest.method; | ||
httpRequest.url = lambdaRequest.url; | ||
httpRequest.originalUrl = lambdaRequest.url; | ||
httpRequest.path = url.parse(lambdaRequest.url).pathname; | ||
LambdaHttpRequest.prototype.setTimeout = function(msecs, callback) { } | ||
LambdaHttpRequest.prototype.done = function(error) { } | ||
httpRequest.headers = lambdaRequest.headers; | ||
httpRequest.body = lambdaRequest.body; | ||
util.inherits(LambdaHttpRequest, streams.ReadableStream); | ||
httpRequest.setTimeout = function(msecs, callback) { } | ||
httpRequest.destroy = function() { } | ||
return httpRequest; | ||
function convert(lambdaRequest) { | ||
return new LambdaHttpRequest(lambdaRequest); | ||
} | ||
module.exports.convert = convert; |
@@ -6,44 +6,72 @@ 'use strict'; | ||
function newResponse() { | ||
/** | ||
* An in memory HTTP Response that captures any data written to it | ||
* so that it can be easily serialised into a lambda payload | ||
* | ||
* @callback done called when end is called on this response | ||
* | ||
* @param done | ||
* @constructor | ||
*/ | ||
function LambdaHttpResponse(done) { | ||
streams.WritableStream.call(this); | ||
this.done = done; | ||
this.headers = {}; | ||
this.headersSet = false; | ||
this.statusCode = 200; | ||
this.statusMessage = undefined; | ||
} | ||
var httpResponse = new streams.WritableStream(); | ||
util.inherits(LambdaHttpResponse, streams.WritableStream); | ||
httpResponse.headers = {}; | ||
httpResponse.headersSet = false; | ||
httpResponse.statusCode = 200; | ||
httpResponse.statusMessage = undefined; | ||
httpResponse.setTimeout = function setTimeout(msecs, callback) {} | ||
httpResponse.destroy = function destroy(error) {} | ||
httpResponse.setHeader = function(name, value) { | ||
httpResponse.headers[name] = value; | ||
LambdaHttpResponse.prototype.setTimeout = function(msecs, callback) {} | ||
LambdaHttpResponse.prototype.destroy = function(error) {} | ||
LambdaHttpResponse.prototype.setHeader = function(name, value) { | ||
this.headers[name] = value; | ||
} | ||
LambdaHttpResponse.prototype.getHeader = function(name) { | ||
return this.headers[name]; | ||
} | ||
LambdaHttpResponse.prototype.removeHeader = function(name) { | ||
delete this.headers[name] | ||
} | ||
LambdaHttpResponse.prototype.flushHeaders = function() {} | ||
LambdaHttpResponse.prototype.writeHead = function(statusCode, reason, obj) { | ||
if (typeof reason === 'string') { | ||
// writeHead(statusCode, reasonPhrase[, headers]) | ||
this.statusMessage = reason; | ||
} else { | ||
// writeHead(statusCode[, headers]) | ||
this.statusMessage = | ||
this.statusMessage || ''; | ||
obj = reason; | ||
} | ||
httpResponse.getHeader = function(name) { | ||
return httpResponse.headers[name]; | ||
} | ||
httpResponse.removeHeader = function(name) { | ||
delete httpResponse.headers[name]; | ||
} | ||
httpResponse.flushHeaders = function() { } | ||
httpResponse.writeHead = function(statusCode, reason, obj) { | ||
if (typeof reason === 'string') { | ||
// writeHead(statusCode, reasonPhrase[, headers]) | ||
this.statusMessage = reason; | ||
} else { | ||
// writeHead(statusCode[, headers]) | ||
this.statusMessage = | ||
this.statusMessage || STATUS_CODES[statusCode] || 'unknown'; | ||
obj = reason; | ||
this.statusCode = statusCode; | ||
if (obj) { | ||
var keys = Object.keys(obj); | ||
for (var i = 0; i < keys.length; i++) { | ||
var k = keys[i]; | ||
if (k) this.setHeader(k, obj[k]); | ||
} | ||
this.statusCode = statusCode; | ||
if (obj) { | ||
var keys = Object.keys(obj); | ||
for (var i = 0; i < keys.length; i++) { | ||
var k = keys[i]; | ||
if (k) httpResponse.setHeader(k, obj[k]); | ||
} | ||
} | ||
} | ||
return httpResponse; | ||
} | ||
LambdaHttpResponse.prototype.end = function(data, encoding, callback) { | ||
LambdaHttpResponse.super_.prototype.end.apply(this, data, encoding, callback); | ||
this.done(this); | ||
} | ||
/** | ||
* @callback done called when end is called on this response | ||
* @param done | ||
* @returns {LambdaHttpResponse} | ||
*/ | ||
function newResponse(done) { | ||
return new LambdaHttpResponse(done); | ||
} | ||
/** | ||
* Serialises a LambdaHttpResponse | ||
* @param {LambdaHttpResponse} httpResponse | ||
* @returns {{headers: Object.<string, string>, statusCode: number, bodyBase64: string}} | ||
*/ | ||
function convert(httpResponse) { | ||
@@ -63,3 +91,4 @@ var lambdaResponse = { | ||
newResponse: newResponse, | ||
LambdaHttpResponse: LambdaHttpResponse, | ||
convert: convert | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
17571
154
8