From fcc5405120ff60107a04d7d49a1acc22f5ff3e8a Mon Sep 17 00:00:00 2001 From: Manthan Mallikarjun Date: Wed, 1 May 2019 12:33:28 -0700 Subject: [PATCH] fix(serverless): add experimental support for aws #5 and google cloud #6 --- package-lock.json | 15 +++++++++++++++ package.json | 1 + src/route.ts | 21 ++++++++++++++++++++- src/types/aws-serverless-micro.d.ts | 1 + 4 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 src/types/aws-serverless-micro.d.ts diff --git a/package-lock.json b/package-lock.json index fa945407..c75cd523 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1118,6 +1118,21 @@ "resolved": "https://registry.npmjs.org/atob/-/atob-2.1.2.tgz", "integrity": "sha512-Wm6ukoaOGJi/73p/cl2GvLjTI5JM1k/O14isD73YML8StrH/7/lRFgmg8nICZgD3bZZvjwCGxtMOD3wWNAu8cg==" }, + "aws-serverless-micro": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/aws-serverless-micro/-/aws-serverless-micro-1.0.2.tgz", + "integrity": "sha512-1keK7qp+9SDOnIlQd/dXRgEC+pruDK01ip+5XVSTOgG6JwJbEFP8Md5vjZKjB6RtRzOkc3eymvPHqnWZpbWf2g==", + "requires": { + "is-stream": "^2.0.0" + }, + "dependencies": { + "is-stream": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/is-stream/-/is-stream-2.0.0.tgz", + "integrity": "sha512-XCoy+WlUr7d1+Z8GgSuXmpuUFC9fOhRXglJMx+dwLKTkL44Cjd4W1Z5P+BQZpr+cR93aGP4S/s7Ftw6Nd/kiEw==" + } + } + }, "aws-sign2": { "version": "0.7.0", "resolved": "https://registry.npmjs.org/aws-sign2/-/aws-sign2-0.7.0.tgz", diff --git a/package.json b/package.json index a25a984c..a17ab5a1 100644 --- a/package.json +++ b/package.json @@ -35,6 +35,7 @@ }, "homepage": "https://github.com/ludicrousxyz/light#readme", "dependencies": { + "aws-serverless-micro": "^1.0.2", "chokidar": "^2.1.5", "colorette": "^1.0.7", "emojic": "^1.1.15", diff --git a/src/route.ts b/src/route.ts index 79e0260e..aae3a06e 100644 --- a/src/route.ts +++ b/src/route.ts @@ -1,5 +1,6 @@ import { run } from 'micro'; import { IncomingMessage, ServerResponse } from 'http'; +import AWSServerlessMicro from 'aws-serverless-micro'; import pino from 'pino-http'; interface Route { @@ -8,7 +9,8 @@ interface Route { handler: Handler; } -type Handler = (req: IncomingMessage, res: ServerResponse) => {}; +// TODO: Define types for micro and aws +type Handler = any; type IM = IncomingMessage; type SR = ServerResponse; type AP = Promise; @@ -37,5 +39,22 @@ export default (route: Route): Handler => { fn.path = route.path; fn.log = true; fn.module = __dirname; + + const isAWS: boolean = !!( + (process.env.LAMBDA_TASK_ROOT && process.env.AWS_EXECUTION_ENV) || false + ); + if (isAWS) { + return { + handler: AWSServerlessMicro(fn), + }; + } + + const isGCP: boolean = !!(process.env.X_GOOGLE_FUNCTION_NAME || false); + if (isGCP) { + return { + handler: fn, + }; + } + return fn; }; diff --git a/src/types/aws-serverless-micro.d.ts b/src/types/aws-serverless-micro.d.ts new file mode 100644 index 00000000..e3e55e5e --- /dev/null +++ b/src/types/aws-serverless-micro.d.ts @@ -0,0 +1 @@ +declare module 'aws-serverless-micro';