@jim-brighter/lambda-local-runner 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +1 -0
  2. package/index.js +43 -0
  3. package/package.json +27 -0
package/README.md ADDED
@@ -0,0 +1 @@
1
+ # lambda-local-runner
package/index.js ADDED
@@ -0,0 +1,43 @@
1
+ #!/usr/bin/env node
2
+
3
+ import { createServer } from 'node:http';
4
+
5
+ const port = process.env.PORT ?? 3000;
6
+
7
+ const handlerFile = process.argv[2];
8
+ const handlerFunc = process.argv[3] || 'handler';
9
+ const handlerModule = await import(`${process.cwd()}/${handlerFile}`);
10
+ const handler = handlerModule[handlerFunc];
11
+
12
+ const server = createServer(async (req, res) => {
13
+ try {
14
+ const [path, queryParams] = req.url?.split('?');
15
+
16
+ if (path === '/favicon.ico') {
17
+ res.writeHead(200);
18
+ res.end();
19
+ return;
20
+ }
21
+
22
+ const queryParamsObj = queryParams ? Object.fromEntries(queryParams.split('&').map((param) => param.split('='))) : {};
23
+
24
+ const event = {
25
+ queryStringParameters: queryParamsObj
26
+ };
27
+
28
+ const response = await handler(event);
29
+ res.writeHead(response.statusCode, {
30
+ 'Content-Type': 'application/json',
31
+ ...response.headers
32
+ });
33
+ res.end(response.body);
34
+ } catch (e) {
35
+ console.error(e);
36
+ res.writeHead(500, { 'Content-Type': 'text/plain' });
37
+ res.end('Something went catastrophically wrong');
38
+ }
39
+ });
40
+
41
+ server.listen(port, 'localhost', () => {
42
+ console.log(`Server listening at http://localhost:${port}/`);
43
+ });
package/package.json ADDED
@@ -0,0 +1,27 @@
1
+ {
2
+ "name": "@jim-brighter/lambda-local-runner",
3
+ "version": "0.0.1",
4
+ "description": "Simple CLI tool to run a lambda handler locally",
5
+ "homepage": "https://github.com/jim-brighter/lambda-local-runner#readme",
6
+ "bugs": {
7
+ "url": "https://github.com/jim-brighter/lambda-local-runner/issues"
8
+ },
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/jim-brighter/lambda-local-runner.git"
12
+ },
13
+ "license": "ISC",
14
+ "author": "Jim Brighter",
15
+ "type": "module",
16
+ "main": "index.js",
17
+ "bin": {
18
+ "lambda-local-runner": "index.js"
19
+ },
20
+ "files": [
21
+ "index.js",
22
+ "README.md"
23
+ ],
24
+ "scripts": {
25
+ "test": "echo \"Error: no test specified\" && exit 1"
26
+ }
27
+ }