-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocal-server.js
More file actions
77 lines (65 loc) · 2.06 KB
/
local-server.js
File metadata and controls
77 lines (65 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import http from 'http';
import { apiInstance } from './src/util/api-provider';
import './src/main';
/**
* A minimal web server that converts the request
* object to something the lambda-api module understands.
* https://gist.github.com/Sleavely/f87448d2c1c13d467f3ea8fc7e864955
*/
async function readBody(request) {
const body = [];
return new Promise((resolve, reject) => {
request.on('data', chunk => {
body.push(chunk);
}).on('end', () => {
resolve(body.join(''));
}).on('error', reject);
});
}
const serverWrapper = http.createServer(async (request, response) => {
const url = new URL(request.url, `http://${request.headers.host}/`);
const reqBody = await readBody(request);
// The event object we're faking is a lightweight based on:
// https://docs.aws.amazon.com/lambda/latest/dg/eventsources.html#eventsources-api-gateway-request
const event = {
httpMethod: request.method.toUpperCase(),
path: url.pathname,
resource: '/{proxy+}',
queryStringParameters: [...url.searchParams.keys()].reduce((output, key) => {
output[key] = url.searchParams.get(key);
return output;
}, {}),
headers: request.headers,
requestContext: {},
pathParameters: {},
stageVariables: {},
isBase64Encoded: false,
body: reqBody
};
console.log('REQUEST', reqBody);
apiInstance.run(event, {})
.then((res) => {
let { body } = res;
const {
headers,
statusCode,
} = res;
if (res.isBase64Encoded) {
body = Buffer.from(body, 'base64');
}
if (!headers['content-length'] && body) {
headers['content-length'] = body.length;
}
response.writeHead(statusCode, headers);
response.end(body);
})
.catch((err) => {
console.error('Something went horribly, horribly wrong');
response.writeHead(500, { 'content-length': 0 });
response.end('');
throw err;
});
});
serverWrapper.listen(process.env.PORT || 3000, () => {
console.log(`Listening on http://localhost:${serverWrapper.address().port}/`);
});