-
Notifications
You must be signed in to change notification settings - Fork 0
/
launch.js
106 lines (97 loc) · 3.07 KB
/
launch.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
/* eslint-disable @typescript-eslint/no-var-requires */
/* eslint-disable no-undef */
const esbuild = require("esbuild");
const http = require("http");
// Start esbuild's server
esbuild
.serve(
{
servedir: "www",
port: 8000,
},
{
bundle: true,
entryPoints: ["src/index.tsx"],
outfile: "www/main.js",
sourcemap: true,
define: { "process.env.NODE_ENV": '"development"' },
}
)
.then((result) => {
const { port } = result;
console.log("Running at http://127.0.0.1:" + port);
// Proxy server
http
.createServer((req, res) => {
const options = {
path: req.url,
headers: req.headers,
method: req.method,
};
let body = "";
req.set;
req.on("readable", () => {
const chunk = req.read();
if (chunk) {
body += chunk;
}
});
req.on("end", () => {
if (options.method !== "OPTIONS") {
let data;
try {
data = JSON.parse(body);
} catch (e) {
data = null;
}
const proxyHeaders = {};
if (options.headers["content-type"]) {
proxyHeaders["content-type"] = options.headers["content-type"];
}
if (options.headers["authorization"]) {
proxyHeaders["authorization"] = options.headers["authorization"];
}
const proxyReq = http.request(
{
hostname: "188.225.83.80",
port: "6719",
path: options.path,
method: options.method,
headers: proxyHeaders,
},
(proxyRes) => {
let proxyData = "";
console.log(`Proxy ${options.method} request ${options.path}`);
proxyRes.setEncoding("utf8");
proxyRes.on("data", (chunk) => {
proxyData += chunk;
});
proxyRes.on("error", (error) => {
console.log(error);
});
proxyRes.on("end", () => {
const response = JSON.parse(proxyData);
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Allow-Methods", "*");
res.setHeader("Access-Control-Allow-Headers", "*");
res.write(JSON.stringify(response));
res.end();
});
}
);
proxyReq.on("error", (e) => {
console.error(`problem with request: ${e.message}`);
});
if (data) proxyReq.write(JSON.stringify(data));
proxyReq.end();
} else {
console.log("skip OPTIONS request");
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Allow-Methods", "*");
res.setHeader("Access-Control-Allow-Headers", "*");
res.end();
}
});
})
.listen(3000);
});