This repository was archived by the owner on Jun 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocal.js
More file actions
56 lines (48 loc) · 1.58 KB
/
local.js
File metadata and controls
56 lines (48 loc) · 1.58 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
/**
* A minimal web server that converts the request
* object to something the Lambda normalizer understands.
*/
const api = require('./api')
const http = require('http')
const serverWrapper = http.createServer(function (request, response) {
const url = new URL(request.url, `http://${request.headers.host}/`)
// 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: request.body,
}
api.run(event, {})
.then((res) => {
let {
body,
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}/`)
})