This repository has been archived by the owner on Jun 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
122 lines (108 loc) · 3.72 KB
/
app.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
// requires the multiple libraries
const https = require("https");
const express = require("express");
const process = require("process");
const util = require("hive-js-util");
const info = require("./package");
const lib = require("./lib");
const request = require("request");
// builds the initial application object to be used
// by the application for serving
const app = express();
// creates the HTTP connection pool/agent that is going
// to be used by the proxy
const agent = new https.Agent({
protocol: "https:",
keepAlive: true,
keepAliveMsecs: lib.conf.PRX_KEEPALIVE
});
process.on("SIGINT", function() {
process.exit();
});
process.on("SIGTERM", function() {
process.exit();
});
process.on("exit", () => {
util.Logging.info("Exiting on user's request");
lib.destroy();
});
app.use(
express.raw({
type: () => true,
limit: "50mb"
})
);
app.get("/info", (req, res, next) => {
res.json({
name: info.name,
version: info.version,
node: process.version
});
});
app.options("*", (req, res, next) => {
const headers = lib.proxyHeaders(req);
Object.assign(headers, {
"Content-Security-Policy":
"default-src * ws://* wss://* data: blob:; script-src * 'unsafe-inline' 'unsafe-eval'; style-src * 'unsafe-inline';",
"Access-Control-Allow-Headers": "*",
"Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS",
"Access-Control-Allow-Origin": "*"
});
res.set(headers);
res.send();
});
app.all("*", (req, res, next) => {
async function clojure() {
// makes sure that in case there's a secret key defined then
// it must be properly set in the request
lib.verifyKey(req);
// makes sure that if there's GET query validation required
// then it's ensured and exceptions are thrown in case of errors
lib.verifyQuery(req);
await new Promise((resolve, reject) => {
try {
// constructs the initial options object with the
// processed headers and query string
const options = {
agent: agent,
baseUrl: lib.conf.PRX_TARGET,
uri: req.path,
method: req.method,
headers: lib.proxyHeaders(req),
qs: req.query,
qsStringifyOptions: {
arrayFormat: "repeat"
},
forever: true
};
// in case there's a valid body defined for the request
// then sets the body in the options
if (req.body && Buffer.isBuffer(req.body) && req.body.length > 0) {
options.body = req.body;
}
// runs the changed request with the transformed values so
// that they become compliant with the expected API
request(options).on("finish", resolve).on("error", reject).pipe(res);
} catch (err) {
reject(err);
}
});
}
clojure().catch(next);
});
(async () => {
await lib.start();
try {
app.listen(lib.conf.PORT, lib.conf.HOST, () => {
try {
util.Logging.info(`Running v${info.version} in Node.js ${process.version}`);
util.Logging.info(`Listening on ${lib.conf.HOST}:${String(lib.conf.PORT)}`);
lib.init();
} catch (err) {
util.Logging.error(err);
}
});
} finally {
await lib.stop();
}
})();