-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
134 lines (118 loc) · 6.48 KB
/
Copy pathindex.ts
File metadata and controls
134 lines (118 loc) · 6.48 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
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
import * as fs from 'fs';
import * as http from 'http';
import * as ws from 'ws';
import * as yaml from 'js-yaml';
import * as fetch from 'node-fetch';
import * as pluginLoader from './plugins';
import * as utils from './utils';
export const serverConfig: import('./types').ServerConfig = Object(yaml.load(fs.readFileSync('./config/server_config.yaml', 'utf-8')));
const AsyncFunction: new (...args: string[]) => (...args: any[]) => Promise<any> = Object.getPrototypeOf(async function () { }).constructor
let serverInfos = {
port: process.argv.includes('--port') ? process.argv[process.argv.indexOf('--port') + 1] : serverConfig.httpOptions.port,
hostname: 'localhost'
}
serverConfig.pluginsOptions.loadPlugins ? pluginLoader.loadAllModules() : null;
const server = http.createServer(async (req, res) => {
let req_ = (await import('url')).parse(req.url);
let resCode: number = 404;
let res_: string = `<h1>404 Not Found</h1><hr/><i>JulMan Http Web Server</i>`;
let headers: {
[header: string]: string
} = {
"Content-Type": "text/html; charset=utf-8"
}
if ((fs.existsSync(`./server${req_.path}`) && !utils.isDirectory(`./server${req_.path}`) && serverConfig.requireAbsulutePath) ||
(!serverConfig.requireAbsulutePath && fs.existsSync(`./server${req_.path}${req_.path.endsWith('/') ? '' : '/'}index.html`)) ||
(!serverConfig.requireAbsulutePath && (fs.existsSync(`./server${req_.path}.server.js`) && !utils.isDirectory(`./server${req_.path}.server.js`)))) {
let file =
(fs.existsSync(`./server${req_.path}`) && !utils.isDirectory(`./server${req_.path}`) && serverConfig.requireAbsulutePath) ? `./server${req_.path}` :
fs.existsSync(`./server${req_.path}${req_.path.endsWith('/') ? '' : '/'}index.html`) && !serverConfig.requireAbsulutePath ?
`./server${req_.path}${req_.path.endsWith('/') ? '' : '/'}index.html` : `./server${req_.path}.server.js`;
let request = '';
if (req.readableLength) request = req.read(req.readableLength);
try {
if (file.endsWith('.server.js')) {
if (serverConfig.executeServerScripts) {
resCode = 200;
res_ = "";
const serverJsArgs: {
argsName: string[],
argsValue: (((...args: any[]) => void) | object | string)[]
} = {
argsName: [
'write',
'req',
'setHeader',
'setResponseCode',
'setInterval',
'requestData',
],
argsValue: [
(data: any) => {
res_ += String(typeof data == 'function' ? '[function Function]' : data);
},
req,
(headerName: string, value: string): void => {
headers[headerName] = value;
},
(responseCode: number): void => {
resCode = responseCode;
},
() => { },
request
]
};
serverConfig.serverScriptsOptions.allowFilesystem ? (() => {
serverJsArgs.argsName.push('fs');
serverJsArgs.argsValue.push(fs);
})() : null;
serverConfig.serverScriptsOptions.allowOtherScriptsExecution ? (() => {
serverJsArgs.argsName.push('require');
serverJsArgs.argsValue.push(require);
})() : null;
try {
await new AsyncFunction(...serverJsArgs.argsName, fs.readFileSync(file, { encoding: 'utf-8' })).call(null, ...serverJsArgs.argsValue);
} catch (err) {
let str = `<h1>Internal Server Error</h1><p>An error occurred while executing script for render page</p><hr><code>Current error : ${err.toString()}</code><br/<br/><br/><i>JulMan Http Web Server</i>`;
res.writeHead(500, "Internal Server Error", {
'Content-Type': 'text/html; charset=utf-8',
'Content-Length': str.length,
'Server': 'JulMan Http Web Server'
});
res.write(str);
}
}
} else {
resCode = 200;
res_ = fs.readFileSync(file, { encoding: 'utf-8' })
.replace(/@{{\s*[\w\.\(\)\"\'\s\{\}]*\s*}}/gi, (match) => {
if (serverConfig.executeServerScriptsFileWidget) {
let match_: string = match.substr(3);
match_ = match_.substring(0, match_.length - 2)
try {
return String(new Function('server', 'req', 'return ' + match_).call(server, serverInfos, req));
} catch (err) {
let str = `<h1>Internal Server Error</h1><p>An error occurred while pre-filling the page</p><hr><code>Current error : ${err.toString()}</code><br/<br/><br/><i>JulMan Http Web Server</i>`;
res.writeHead(500, "Internal Server Error", {
'Content-Type': 'text/html; charset=utf-8',
'Content-Length': str.length,
'Server': 'JulMan Http Web Server'
});
res.write(str);
}
} else return '?';
});
}
} catch { }
}
headers['Server'] = 'JulMan Http Web Server'
headers['Content-Length'] = String(res_.length);
res.writeHead(resCode, JSON.parse(fs.readFileSync('./json/responsesCodes.json', { encoding: 'utf-8' }))['' + resCode], headers);
res.write(res_);
});
server.listen(serverInfos.port, async () => {
console.log(`Server ready on "${serverInfos.hostname = await (await fetch.default('https://api.ipify.org/')).text()}:${serverInfos.port}"`);
});
server.on('close', () => {
console.log('Server closed');
})