-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsnub-http.js
130 lines (115 loc) · 4.24 KB
/
snub-http.js
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
const http = require('http');
const querystring = require('querystring');
// const url = require('url');
module.exports = function (config) {
var headers = {
'X-powered-by': 'Snub-HTTP',
'Access-Control-Allow-Origin': '*'
};
var optionHeaders = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'POST, GET, PUT, DELETE, OPTIONS',
'Access-Control-Allow-Credentials': false,
'Access-Control-Max-Age': '86400', // 24 hours
'Access-Control-Allow-Headers': 'X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept'
};
config = Object.assign({
port: 8484,
debug: false,
timeout: 5000,
headers: {},
optionHeaders: {},
requestMutator: reqObj => { return reqObj; }
}, config || {});
config.headers = Object.assign({}, headers, config.headers);
config.optionHeaders = Object.assign({}, optionHeaders, config.optionHeaders);
return function (snub) {
const requestHandler = (request, response) => {
if (request.method === 'OPTIONS') {
response.writeHead(200, config.optionHeaders);
return response.end();
}
var urlParsed = new URL('http://null' + request.url);
request.headers['x-forwarded-for'] = request.headers['x-real-ip'] || request.headers['x-forwarded-for'] || request.connection.remoteAddress;
var reqObj = {
method: request.method,
path: urlParsed.pathname,
body: [],
headers: request.headers
};
if (urlParsed.query)
reqObj.query = querystring.parse(urlParsed.query);
if (urlParsed.search)
reqObj.query = querystring.parse(urlParsed.search.substring(1));
request.on('data', function (chunk) {
reqObj.body.push(chunk);
}).on('end', async function () {
reqObj.body = Buffer.concat(reqObj.body).toString();
reqObj = await config.requestMutator(reqObj);
if (!reqObj) {
response.statusCode = 503;
response.setHeader('Content-Type', 'application/json');
response.end(JSON.stringify({
message: 'Event handler unavailable'
}));
return;
}
snub
.mono('http:' + reqObj.method + ':' + reqObj.path, reqObj)
.replyAt((reply, error) => {
if (!reply && error) {
try {
response.statusCode = 500;
response.setHeader('Content-Type', 'application/json');
response.end(JSON.stringify({
message: 'Server Error'
}));
} catch (error) {
// suppress this as there is a chance the delivered check as already run.
}
return;
}
if (!reply)
reply = {};
try {
reply.headers = Object.assign({}, config.headers, reply.headers);
Object.keys(reply.headers).forEach(i => {
response.setHeader(i, reply.headers[i]);
});
response.statusCode = reply.statusCode || 200;
if (typeof reply.body === 'string')
return response.end(reply.body);
response.setHeader('Content-Type', 'application/json');
response.end(JSON.stringify(reply.body));
} catch (error) {
console.error(error, reqObj, reply);
}
})
.send(delivered => {
if (delivered > 0) return;
response.statusCode = 404;
response.setHeader('Content-Type', 'application/json');
response.end(JSON.stringify({
message: 'Event handler not found'
}));
});
setTimeout(() => {
if (!response.finished) {
response.statusCode = 504;
response.setHeader('Content-Type', 'application/json');
response.end(JSON.stringify({
message: 'Event handler timed out'
}));
}
}, config.timeout);
});
};
const server = http.createServer(requestHandler);
server.listen(config.port, (err) => {
if (err)
return console.error('Snub HTTP => something bad happened', err);
if (config.debug)
console.log(`Snub HTTP Listening on ${config.port}`);
});
};
};