Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
467 changes: 192 additions & 275 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "lambda-framework",
"version": "1.0.16",
"version": "1.0.19",
"description": "Framework to create web apps in AWS Lambda",
"main": "dist/src/index.js",
"types": "dist/src/index.d.ts",
Expand Down Expand Up @@ -30,6 +30,7 @@
"@types/chai": "^4.0.4",
"@types/mocha": "^2.2.43",
"@types/node": "^8.0.28",
"@types/sinon": "^2.3.7",
"chai": "^4.1.2",
"coveralls": "^3.0.0",
"mocha": "^3.5.3",
Expand All @@ -40,10 +41,7 @@
"typescript": "^2.5.2"
},
"dependencies": {
"@types/aws-lambda": "0.0.16",
"@types/sinon": "^2.3.7",
"accepts": "^1.3.4",
"aws-sdk": "^2.141.0",
"bytes": "^3.0.0",
"content-type": "^1.0.4",
"cookie": "^0.3.1",
Expand Down
1 change: 0 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,5 @@ export { default as ITemplate } from "./lib/types/http/renderEngine/ITemplate";
export { default as ITemplateEngine } from "./lib/types/http/renderEngine/ITemplateEngine";
export { default as ITemplateLoader } from "./lib/types/http/renderEngine/ITemplateLoader";
export { default as ITemplateRenderer } from "./lib/types/http/renderEngine/ITemplateRenderer";
export { default as DefaultTemplateLoader } from "./lib/http/renderEngine/DefaultTemplateLoader";
export { default as DevTemplateLoader } from "./lib/http/renderEngine/DevTemplateLoader";
export { default as Template } from "./lib/http/renderEngine/Template";
9 changes: 4 additions & 5 deletions src/lib/App.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Callback, Context } from "aws-lambda";
import configuration from "./configuration/configuration";
import defaultConfiguration from "./configuration/defaultConfiguration";
import eventFinalHandler from "./event/eventFinalHandler";
Expand All @@ -18,8 +17,9 @@ import IHttpResponse from "./types/http/IHttpResponse";
import IHttpRoute from "./types/http/IHttpRoute";
import ITemplateRenderer from "./types/http/renderEngine/ITemplateRenderer";
import IApp from "./types/IApp";
import IRawCallback from "./types/IRawCallback";
import IRawEvent from "./types/IRawEvent";
import IRouter from "./types/IRouter";
import { getEventType } from "./utils/utils";

/**
* The main object that describes and configures the lambda application.
Expand Down Expand Up @@ -54,9 +54,8 @@ export default class App implements IApp {
return this._settings[key];
}

public handle(event: any, context: Context, callback?: Callback): void {
const eventType = getEventType(event);
if (eventType === "APIGatewayEvent") {
public handle(event: IRawEvent, callback: IRawCallback): void {
if (event.isHttp) {
const req: IHttpRequest = new HttpRequest(event);
const res: IHttpResponse = new HttpResponse(this, req, callback);
const done = httpFinalHandler(req, res, {
Expand Down
28 changes: 28 additions & 0 deletions src/lib/RawEvent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import IRawEvent from "./types/IRawEvent";

/**
* The raw incoming event of the lambda function.
*/
export default class RawEvent implements IRawEvent {

public type: string;

public original: any;

public isHttp: boolean;

public body: string;

public headers: {[name: string]: string};

public queryParams: {[name: string]: string};

public stageVariables: {[name: string]: string};

public ip: string;

public path: string;

public httpMethod: string;

}
3 changes: 0 additions & 3 deletions src/lib/Router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,6 @@ export default class Router implements IRouter {
public event(event: string|IEventRoutePredicate, handler: IEventHandler): IRouter {
const layer = new EventLayer(
event,
{
end: true
},
handler
);

Expand Down
4 changes: 1 addition & 3 deletions src/lib/event/EventLayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@ import INext from "./../types/INext";
export default class EventLayer implements IEventLayer {

private _event: string|IEventRoutePredicate;
private _options: {};
private _handler: IEventHandler;

constructor(event: string|IEventRoutePredicate, options: {}, handler: IEventHandler) {
constructor(event: string|IEventRoutePredicate, handler: IEventHandler) {
this._event = event;
this._options = options;
this._handler = handler;
}

Expand Down
8 changes: 4 additions & 4 deletions src/lib/event/EventRequest.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import IEventRequest from "./../types/event/IEventRequest";
import INext from "./../types/INext";
import { getEventType } from "./../utils/utils";
import IRawEvent from "./../types/IRawEvent";

/**
* It represents an incoming event.
Expand All @@ -9,10 +9,10 @@ export default class EventRequest implements IEventRequest {

public next: INext;

private _event: any;
private _event: IRawEvent;
private _context: { [name: string]: any };

constructor(event: any) {
constructor(event: IRawEvent) {
this._event = event;
this._context = {};
}
Expand All @@ -22,7 +22,7 @@ export default class EventRequest implements IEventRequest {
}

get eventType(): string {
return getEventType(this._event);
return this._event.type;
}

get context(): { [name: string]: any } {
Expand Down
2 changes: 0 additions & 2 deletions src/lib/http/HttpLayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ export default class HttpLayer implements IHttpLayer {

private _router: IRouter;
private _path: string;
private _options: {};
private _handler: IHttpHandler;

private _regexp: RegExp;
Expand All @@ -43,7 +42,6 @@ export default class HttpLayer implements IHttpLayer {
constructor(router: IRouter, path: string, options: {[name: string]: any}, handler?: IHttpHandler, regexOptions?: RegExpOptions) {
this._router = router;
this._path = path;
this._options = options;
this._handler = handler;

const regexOpts = regexOptions || {};
Expand Down
12 changes: 6 additions & 6 deletions src/lib/http/HttpRequest.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import * as accepts from "accepts";
import { APIGatewayEvent } from "aws-lambda";
import * as fresh from "fresh";
import IHttpRequest from "./../types/http/IHttpRequest";
import IHttpResponse from "./../types/http/IHttpResponse";
import IHttpRoute from "./../types/http/IHttpRoute";
import IHttpUploadedFile from "./../types/http/IHttpUploadedFile";
import INext from "./../types/INext";
import IRawEvent from "./../types/IRawEvent";
import { mergeParams, normalizeType } from "./../utils/utils";

/**
Expand All @@ -21,11 +21,11 @@ export default class HttpRequest implements IHttpRequest {
public params: { [name: string]: string };
public route: IHttpRoute;

private _event: APIGatewayEvent;
private _event: IRawEvent;
private _headers: { [name: string]: string };
private _context: { [name: string]: any };

constructor(event: APIGatewayEvent) {
constructor(event: IRawEvent) {
this.body = event.body; // Default body
this._event = event;
this._context = {};
Expand Down Expand Up @@ -56,7 +56,7 @@ export default class HttpRequest implements IHttpRequest {

return ips[ips.length - 1];
} else {
return this._event.requestContext.identity.sourceIp.replace("\:d+$", "");
return this._event.ip;
}
}

Expand All @@ -77,8 +77,8 @@ export default class HttpRequest implements IHttpRequest {
return val.toLowerCase() === "xmlhttprequest";
}

get event(): APIGatewayEvent {
return this._event;
get event(): any {
return this._event.original;
}

get context(): { [name: string]: any } {
Expand Down
12 changes: 8 additions & 4 deletions src/lib/http/HttpResponse.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Callback } from "aws-lambda";
import { parse, serialize } from "cookie";
import { sign } from "cookie-signature";
import * as encodeUrl from "encodeurl";
Expand All @@ -13,6 +12,7 @@ import IHttpResponse from "./../types/http/IHttpResponse";
import ITemplateEngine from "./../types/http/renderEngine/ITemplateEngine";
import IApp from "./../types/IApp";
import INext from "./../types/INext";
import IRawCallback from "./../types/IRawCallback";
import IRouter from "./../types/IRouter";
import { merge, normalizeType, setCharset, stringify } from "./../utils/utils";

Expand All @@ -26,12 +26,12 @@ export default class HttpResponse implements IHttpResponse {
private _statusCode: number;
private _app: IApp;
private _request: IHttpRequest;
private _callback: Callback;
private _callback: IRawCallback;
private _headers: { [name: string]: string|string[] };
private _error: IHttpError;
private _isSent: boolean;

constructor(app: IApp, request: IHttpRequest, callback: Callback) {
constructor(app: IApp, request: IHttpRequest, callback: IRawCallback) {
this._app = app;
this._request = request;
this._callback = callback;
Expand Down Expand Up @@ -389,7 +389,11 @@ export default class HttpResponse implements IHttpResponse {

const error: Error = this._error && this._error.cause ? this._error.cause : this._error;
this._isSent = true;
this._callback(error, {statusCode, headers, body: resultBody});
if (error) {
this._callback.sendError(error);
} else {
this._callback.send(statusCode, headers, resultBody);
}
}

}
14 changes: 8 additions & 6 deletions src/lib/http/bodyParsers/UrlEncodedParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ const parameterCount = (body: string, limit: number): number => {
return count;
};

const extendedParser = (body: string, parameterLimit: number, arrayLimit: number): {[name: string]: string} => {
type IParser = (body: string, parameterLimit: number, arrayLimit: number) => {[name: string]: string|string[]};

const extendedParser: IParser = (body: string, parameterLimit: number, arrayLimit: number): {[name: string]: string|string[]} => {
return qs.parse(body, {
allowPrototypes: true,
arrayLimit,
Expand All @@ -33,11 +35,11 @@ const extendedParser = (body: string, parameterLimit: number, arrayLimit: number
});
};

const simpleParser = (body: string, parameterLimit: number, arrayLimit: number): {[name: string]: string} => {
const simpleParser: IParser = (body: string, parameterLimit: number, arrayLimit: number): {[name: string]: string|string[]} => {
return querystring.parse(body, undefined, undefined, {maxKeys: parameterLimit});
};

const abstractParser = (options: {[name: string]: any}, parser: (body: string, parameterLimit: number, arrayLimit: number) => {[name: string]: string}): (body: string) => {[name: string]: string} => {
const abstractParser = (options: {[name: string]: any}, parser: IParser): (body: string) => {[name: string]: string|string[]} => {
let parameterLimit = options.parameterLimit !== undefined
? options.parameterLimit
: 1000;
Expand All @@ -50,7 +52,7 @@ const abstractParser = (options: {[name: string]: any}, parser: (body: string, p
parameterLimit = Number.MAX_SAFE_INTEGER - 1;
}

return (body: string): {[name: string]: string} => {
return (body: string): {[name: string]: string|string[]} => {
const paramCount = parameterCount(body, parameterLimit + 1);

if (paramCount === undefined) {
Expand All @@ -76,10 +78,10 @@ export default class UrlEncodedParser implements IBodyParser {
const contentType = opts.type || "application/x-www-form-urlencoded";

// create the appropriate query parser
const queryParser: (body: string, parameterLimit: number, arrayLimit: number) => {[name: string]: string} = extended
const queryParser: (body: string, parameterLimit: number, arrayLimit: number) => {[name: string]: string|string[]} = extended
? extendedParser
: simpleParser;
const parser: (body: string) => {[name: string]: string} = abstractParser(options, queryParser);
const parser: (body: string) => {[name: string]: string|string[]} = abstractParser(options, queryParser);

return parserHelper(
(initialBody: string, req: IHttpRequest): void => {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/http/bodyParsers/parserHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const parserHelper = (func: (body: string, req: IHttpRequest) => void, allowCont
if (!allowContentTypes || req.is(allowContentTypes)) {
const contentType = req.header("content-type");
try {
func(req.event.body, req);
func(req.body as string, req);

req.context._previouslyBodyParsed = true;
} catch (e) {
Expand Down
71 changes: 0 additions & 71 deletions src/lib/http/renderEngine/DefaultTemplateLoader.ts

This file was deleted.

Loading