-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
397 lines (352 loc) · 14.7 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/*************************
* Node.js configuration *
************************/
Error.stackTraceLimit = Infinity;
const pathLib = require('path');
const concoleWarnError = (console, /** @type { any } */ b = {error: e => e.toString(), util: {email: (...a) => Promise.resolve()}}) => {
module.constructor.prototype.require = function (path = '') {
if (path.substr(0, 7) == 'client/') { // IDE 'requires' helpers are skipped.
try {
var file = new Error('').stack
.match(new RegExp('\\(' + pathLib.resolve('') + '([\\s\\S]+)'))[1]
.match(new RegExp('\\(' + pathLib.resolve('') + '([^\\)]+)\\)'))[1];
} catch (err) {}
console.debug(`WARNING: Path require('${path}') in ${file} should not load in server side. This require is skyped.`);
return undefined;
} else return this.constructor._load(path, this);
};
let error;
process.on('uncaughtException', (err) => {
if (error != err) {
let error = 'Uncaught exception: ' + b.error(err);
console.error(error);
b.util.email(error, {group: 'sendEmailAfterError'}).catch(err => { console.error(err); });
}
error = err;
});
process.on('unhandledRejection', (err, promise) => {
if (error != err) {
let error = 'Unhandled Rejection: ' + b.error(err);
console.error(error)
b.util.email(error, {group: 'sendEmailAfterError'}).catch(err => { console.error(err); });
}
error = err;
});
};
concoleWarnError(console);
const b = require('server/src/_index.js');
const console0 = b.util.console.configure({userErrorFunction: b.util.error});
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('error', async err => { console.error(err); });
// process.stdin.on('end', async () => { console.log('end'); process.stdin.reopen; });
process.stdin.on('data', async data => {
// let toTMPFile = str => b.modul.fs.writeFileSync('.tmp', str);
if (data.toString() == b.config.manager.shortcuts.serverRestart) {
process.exit(2);
}
else if (data.toString() == b.config.manager.shortcuts.serverRestartAndTerminalClear) {
await b.util.promisify(b.modul.child_process.execFile, 'osascript', ['-e', `
tell application "System Events" to tell process "Terminal" to keystroke "k" using command down
`]);
process.exit(2);
}
else { try { eval(data.toString()); } catch (err) { console.error(err); } }
});
let debugFileRegExp;
b.storage.edit(s => s.server.help.push(
{prop: 'debuging', desc: 'show all console.debug'},
{prop: 'debuging=/app\\.js/i', desc: 'show console.debug of specific files'},
// {prop: 'inputFifo=nohup.in', desc: 'redirect fifo to app input'},
));
for (let i in process.argv) {
if (process.argv[i].substr(0, 8) === 'debuging') {
if (process.argv[i].substr(8, 1) === '=') {
let match = process.argv[i].match(/=\/?([^\/]+)\/?(.*)$/);
debugFileRegExp = new RegExp(match[1], match[2]);
} else debugFileRegExp = /.*/;
}
}
debugFileRegExp && console0.configure({debugFileRegExp: debugFileRegExp});
concoleWarnError(console0, b);
/************************/
(async () => {
const console = console0;
const app = require(b.config.startFile);
if ((process.env.npm_lifecycle_script || '').indexOf('nohup') > -1) {
b.util.promisify(b.modul.fs.writeFile, '.serverPID', process.pid + '' || '');
}
if (!b.modul.fs.existsSync('.gitBase.JS') && b.modul.fs.existsSync('.git')) {
b.modul.fs.renameSync('.git', '.gitBase.JS');
b.modul.fs.writeFileSync('.gitBase.JS/info/exclude', b.modul.fs.readFileSync('.github/info/exclude'));
}
await b.util.promisify(b.modul.child_process.exec, 'alias gitb="git --git-dir=.gitBase.JS"');
await b.util.promisify(b.modul.child_process.exec, `
git --git-dir=.gitBase.JS rev-parse HEAD;
git --git-dir=.gitBase.JS fetch origin master;
git --git-dir=.gitBase.JS log master..origin/master --format="%H %B"
`).then(data => {
/** @type {String[]} */
let newCommits = data.split('\n');
let showNewCommits = [];
let hashNew = '';
let hashOld = newCommits.shift();
for (let i in newCommits) {
if (!newCommits[i]) continue;
let match = newCommits[i].match(/^([0-9a-fA-F]+) (new|upd|fix|del|dep|dpr)?( |: )?(.+)$/i);
if (!match || !match[2]) continue;
if (!hashNew) hashNew = match[1];
if (['new', 'upd'].indexOf(match[2].toLowerCase()) > -1) {
showNewCommits.push(' ' + console.colors.green + console.colors.bold + match[2].toUpperCase() + ': ' + console.colors.reset + match[4]);
}
if (['fix', 'del'].indexOf(match[2].toLowerCase()) > -1) {
showNewCommits.push(' ' + console.colors.red + console.colors.bold + match[2].toUpperCase() + ': ' + console.colors.reset + match[4]);
}
if (['dep', 'dpr'].indexOf(match[2].toLowerCase()) > -1) {
showNewCommits.push(' ' + console.colors.blue + console.colors.bold + match[2].toUpperCase() + ': ' + console.colors.reset + match[4]);
}
}
if (showNewCommits.length) console.info(
'New version Base.JS framework contains:\n',
showNewCommits.join('\n').substr(1),
'\n ', hashOld, '-->', hashNew,
console.colors.blue, '\n Command for update:', console.colors.reset,
console.colors.green, console.colors.bold, 'npm run update',
);
});
await b.util.killPort(b.config.server.port);
await app.callBeforeServerStarting();
let serverFunction = async (
/** @type { import('http').IncomingMessage } */ req,
/** @type { import('http').ServerResponse } */ res,
) => {
res.statusCode = 0;
try {
let postData = await new Promise((res, rej) => {
if (req.method == 'POST') {
let result = '';
req.on('data', (data) => result += data );
req.on('end', () => res(result) );
req.on('error', err => rej(err) );
} else res('');
});
let input = b.util.urlParser(req.url);
let file = input.parts[input.parts.length-1];
let fileSufix = file.substr(file.lastIndexOf('.') + 1);
for (let suffix of b.config.server.publicHTTPsuffixes) {
if (file.substring((-1 * suffix.length) -1) == '.' + suffix) {
if (!b.modul.fs.existsSync(input.parts.join('/'))) {
res.statusCode = 404;
throw `File "${input.parts.join('/')}" is not exists.`;
}
let etag = '' + b.modul.fs.statSync(input.parts.join('/')).mtime.getTime();
if (etag === req.headers['if-none-match']) {
res.statusCode = 304;
res.end();
return;
}
var img = b.modul.fs.readFileSync(input.parts.join('/'));
res.setHeader('Content-Type', b.modul.mime.getType(fileSufix));
res.setHeader('ETag', etag);
if (!b.storage.of(b.config, a => a.client.maxAgeDisableForRegExp, ['']).find(a => new RegExp(a).test(input.parts.join('/')))
&& !res.getHeader('Cache-Control')) { // with max-age is ignored etag
res.setHeader('Cache-Control', `public, max-age=${eval(b.config.client.refresh)}`);
}
res.statusCode = 200;
res.end(img, 'binary');
return;
}
}
b.storage.edit(storage => storage.server.response = res);
b.storage.edit(storage => storage.server.request = req);
b.storage.edit(storage => storage.server.getData = input.queries);
b.storage.edit(storage => storage.server.postData = postData);
let endIsCalled = false;
// @ts-ignore
if (!res.endIsChanged) {
let endOld = res.end;
res.end = (...args) => {
endIsCalled = true;
return endOld.apply(res, args);
};
// @ts-ignore
res.endIsChanged = true;
}
let response = await app.callPerResponce(req, res, input.queries, postData);
if (!(response instanceof Buffer) && typeof response != 'string') {
response = JSON.stringify(response);
}
if (!res.statusCode) res.statusCode = 200;
!endIsCalled && res.end(response);
}
catch(err) {
if (!res.statusCode) {
res.statusCode = 500;
err = 'Internal error (response): ' + b.error(err);
console.error(err);
b.util.email(err, {group: 'sendEmailAfterError'}).catch(err => { console.error(err); });
}
res.setHeader('Content-Type', 'text/html');
res.end(JSON.stringify(err));
}
};
let httpsExists = b.modul.fs.existsSync(b.get(b.config, 'server.https._privateKey'));
// // https://gist.github.com/bnoordhuis/4740141
// let httpSocket = 'http.sock'; b.modul.fs.existsSync(httpSocket) && b.modul.fs.unlinkSync(httpSocket);
// let httpsSocket = 'https.sock'; b.modul.fs.existsSync(httpsSocket) && b.modul.fs.unlinkSync(httpsSocket);
// b.modul.net.createServer(socket => {
// socket.once('data', function(buffer) {
// let byte = buffer[0];
// let address;
// if (byte === 22 && httpsExists) address = httpsSocket;
// if (byte === 22) address = httpSocket;
// else if (32 < byte && byte < 127) address = httpSocket;
// else throw 'Internal error (not http / not https)';
// let proxy = b.modul.net.createConnection(address, function() {
// proxy.write(buffer);
// socket.pipe(proxy).pipe(socket);
// });
// });
// socket.on("error", err => console.error('Socket error:', err));
// }).listen(+b.config.server.port, b.config.server.hostname, () => {
// console.info('Server socket rooter is runned');
// });
// b.modul.http.createServer((req, res) => {
// if (httpsExists) {
// res.writeHead(301, { "Location": "https://" + req.headers['host'] + req.url });
// res.end();
// }
// else return serverFunction(req, res);
// }).listen(httpSocket, b.config.server.hostname, () => {
// if (httpsExists) console.info('Redirection http to https is actived');
// else console.info('Server running at', console.colors.bold, console.colors.green, `http://${b.config.server.hostname}:${b.config.server.port}`);
// });
// if (httpsExists) b.modul.https.createServer({
// key: b.modul.fs.readFileSync(b.config.server.https._privateKey),
// cert: b.modul.fs.readFileSync(b.config.server.https._certificate),
// ca: b.modul.fs.readFileSync(b.config.server.https._chain)
// }, serverFunction).listen(httpsSocket, b.config.server.hostname, () => {
// console.info('Server running at', console.colors.bold, console.colors.green, `https://${b.config.server.hostname}:${b.config.server.port}`);
// });
// https://stackoverflow.com/questions/22453782/nodejs-http-and-https-over-same-port
let servers = {};
servers.http = b.modul.http.createServer((req, res) => {
if (!b.config.server.disableRedirectToHttps && httpsExists) {
res.writeHead(301, { "Location": "https://" + req.headers['host'] + req.url });
res.end();
}
else return serverFunction(req, res);
});
if (httpsExists) {
servers.https = b.modul.https.createServer({
key: b.modul.fs.readFileSync(b.config.server.https._privateKey),
cert: b.modul.fs.readFileSync(b.config.server.https._certificate),
ca: b.modul.fs.readFileSync(b.config.server.https._chain)
}, serverFunction);
}
let server = b.modul.net.createServer(socket => {
socket.once('data', buffer => {
socket.pause(); // Pause the socket
let byte = buffer[0]; // Determine if this is an HTTP(s) request
let protocol;
if (byte === 22) protocol = 'https';
else if (32 < byte && byte < 127) protocol = 'http';
let proxy = servers[protocol];
if (proxy) {
socket.unshift(buffer); // Push the buffer back onto the front of the data stream
proxy.emit('connection', socket); // Emit the socket to the HTTP(s) server
}
process.nextTick(() => socket.resume());
});
socket.on("error", err => {
// console.error('Socket error:', err)
});
});
// let server;
// if (httpsExists)
// server = b.modul.get('https').createServer({
// key: b.modul.fs.readFileSync(b.config.server.https._privateKey),
// cert: b.modul.fs.readFileSync(b.config.server.https._certificate),
// ca: b.modul.fs.readFileSync(b.config.server.https._chain)
// }, serverFunction);
// else server = b.modul.get('http').createServer(serverFunction);
server.listen(+b.config.server.port, b.config.server.hostname, () => {
console.info('Server running at', console.colors.bold, console.colors.green, `http${httpsExists ? 's' : ''}://${b.config.server.hostname}:${b.config.server.port}`);
});
let refreshAndToBrowser = async () => {
if (process.argv.includes('refresh') || process.argv.includes('toBrowser')) {
await b.util.promisify(b.modul.child_process.execFile, 'osascript', ['-e', `
set urll to "${httpsExists ? 'https' : 'http'}://${b.config.server.hostname}${b.config.server.port ? ':' + b.config.server.port : ''}"
on is_running(appName)
tell application "System Events" to (name of processes) contains appName
end is_running
set chromeRunning to is_running("Google Chrome")
if chromeRunning then
tell application "Google Chrome"
set i to 0
set j to 0
repeat with w in (windows)
set j to j + 1
repeat with t in (tabs of w)
set i to i + 1
if URL of t starts with urll then
set (active tab index of window j) to i
# set active tab index of w to i
# set index of w to 1
${process.argv.includes('toBrowser') ? 'activate' : ''}
${process.argv.includes('refresh') ?
`tell application "Google Chrome" to tell the active tab of its first window
reload
end tell` : ''
}
return
end if
end repeat
end repeat
tell application "Google Chrome"
activate
open location urll
end tell
end tell
else
tell application "Google Chrome"
activate
open location urll
end tell
end if
`]);
}
}
b.storage.edit(s => s.server.help.push(
{prop: 'refresh', desc: 'refresh web page (mac only)'},
{prop: 'toBrowser', desc: 'go to browser web page (mac only)'}
));
refreshAndToBrowser();
let ignnoreWatchFiles = b.config.manager.ignnoreWatchFiles;
b.storage.edit(s => s.server.help.push({prop: 'refreshAfterChange', desc: 'refresh server after its file change'}));
if (process.argv.includes('refreshAfterChange')) {
b.modul.fs.watch('./', {recursive: true}, (eventType, filename) => {
for (let i in ignnoreWatchFiles) {
if (new RegExp(ignnoreWatchFiles[i], 'i').test(filename)) return;
}
// if (/^client\/templates\//i.test(filename)) refreshAndToBrowser(); // user can have disabled full html generation per server call
process.exit(2);
});
}
b.storage.edit(s => s.server.help.push(
{prop: 'testing', desc: 'start tests'},
{prop: 'testing=/fileWithTest/', desc: 'start specific tests'}
));
for (let i in process.argv) {
if (process.argv[i] === 'testing') await b.service.testing.testAll(); // await
else if (process.argv[i].substr(0, 8) === 'testing=') {
let match = process.argv[i].match(/=\/?([^\/]+)\/?(.*)$/);
await b.service.testing.testAll(new RegExp(match[1], match[2]));
}
}
console.info('Help>', b.console.colors.cyan, 'npm start', b.storage.edit(s => s.server.help).map((help) => help.prop).join(' '));
})().catch((err) => {
let error = 'Internal error: ' + b.error(err);
console.error(error);
b.util.email(error, {group: 'sendEmailAfterError'}).catch(err => { console.error(err); });
});