-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
148 lines (143 loc) · 5.2 KB
/
server.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
const port = process.env.PORT || 7778;
const http = require('http');
const fs = require("fs");
const path = require('path');
const JSDOM = require("jsdom");
const vm = require('node:vm');
const myRequire = require;
const allowJS = process.env.NO_JS == undefined ? !(process.argv[2] == "false") : (process.env.NO_JS == "true" ? false : !(process.argv[2] == "false"));
/**
* @param {http.IncomingMessage} req
* @param {http.ServerResponse} res
*/
const requestHandler = (req, res) => {
// const url = new URL(req.url, `http://${req.headers.host}`);
// const convertedPathname = path.parse(url.pathname);
// console.log(convertedPathname);
// if (convertedPathname.base == "selfHost" && convertedPathname.dir == '/') {
// res.writeHead(200);
// res.end();
// return;
// }
// if (fs.existsSync("." + convertedPathname.dir) && url.pathname != '/') {
// res.end(fs.readFileSync("." + url.pathname));
// return;
// }
const test = path.join(__dirname, req.url);
if (path.normalize(decodeURI(req.url)) !== decodeURI(req.url)) {
res.statusCode = 403;
res.end();
return;
}
// console.log(test);
const parsed = path.parse(test);
// console.log(parsed);
if (parsed.base == "selfHost" && parsed.dir == __dirname) {
res.writeHead(200);
res.end();
return;
}
if (parsed.base.startsWith("searchRedirect") && parsed.dir == __dirname) {
const parsedQuery = new URL(parsed.base, "http://localhost").searchParams.get("query").replace(/\s/g, "+");
// const target = new URL(`/szukaj,${parsedQuery}.html`, "http://" + req.headers.host + req.url.split(parsed.base)[0]);
const target = `./szukaj,${parsedQuery}.html`;
res.setHeader("Location", target);
res.writeHead(301);
res.end();
return;
}
if (!allowJS) {
if (test.endsWith(".js") || test.endsWith(".ico")) {
res.writeHead(404);
res.end();
return;
}
}
if (fs.existsSync(test) && !test.endsWith(path.sep)) {
fs.createReadStream(test).pipe(res);
return;
}
// res.end(fs.readFileSync("./index.html"));
if (!allowJS) {
const rawHTML = fs.readFileSync("./index.html", "utf8");
const root = new JSDOM.JSDOM(rawHTML);
const finishedDeferred = {
resolve: undefined,
reject: undefined,
promise: undefined,
};
finishedDeferred.promise = new Promise((resolve, reject) => {
finishedDeferred.resolve = resolve;
finishedDeferred.reject = reject;
});
const bridgeTest = {
finishedDeferred,
};
const globals = {
require(...args) {
if (args[0] == "./TekstowoAPI")
return myRequire("tekstowo-api");
return myRequire(args[0]);
},
globalThis: {
Node: root.window.Node,
URL,
get document() {
return root.window.document;
},
localStorage: undefined,
window: {
NO_JS: true,
bridgeTest,
},
location: {
href: req.url,
},
fetch(...args) {
if (typeof args[0] == "string")
if (args[0].startsWith("./")) {
console.log(args[0], req.headers.host + req.url.split(parsed.base)[0]);
args[0] = new URL(args[0], "http://" + req.headers.host + req.url.split(parsed.base)[0]);
}
return fetch(...args);
},
alert(...args) {
return console.log(...args);
},
console: {
get log() {
return console.log;
},
get error() {
return console.error;
},
},
settingsManager: {
settings: new Proxy({}, {
get() {
return {
value: undefined,
};
},
}),
},
},
};
const ctx = { ...globals, ...globals.globalThis };
vm.createContext(ctx);
vm.runInContext(fs.readFileSync("./main.js", "utf8"), ctx);
bridgeTest.finishedDeferred.promise.then(() => {
res.write(root.window.document.documentElement.outerHTML);
res.end();
});
return;
}
fs.createReadStream("./index.html").pipe(res);
};
const server = http.createServer(requestHandler);
server.listen(port, '0.0.0.0', (err) => {
if (err) {
return console.log('something bad happened', err);
}
console.log(`server is listening on ${port}` + (allowJS ? "" : " with JS disabled"));
});