Skip to content

Commit 14e6ec1

Browse files
committed
minor cleanups
1 parent 1e75767 commit 14e6ec1

2 files changed

Lines changed: 45 additions & 31 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "rtjscomp",
3-
"version": "0.9.10",
3+
"version": "0.9.11",
44
"description": "php-like server but with javascript",
55
"repository": {
66
"type": "git",

rtjscomp.js

Lines changed: 44 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const COMPRESS_METHOD_NONE = 0;
1919
const COMPRESS_METHOD_GZIP = 1;
2020
const COMPRESS_METHOD_BROTLI = 2;
2121
const COMPRESS_METHOD_ZSTD = 3;
22+
const COMPRESS_METHODS = ',gzip,brotli,zstd'.split(',');
2223
const GZIP_OPTIONS = {level: 9};
2324
const HAS_BROTLI = zlib.createBrotliCompress != null;
2425
const HAS_ZSTD = zlib.createZstdCompress != null;
@@ -308,7 +309,7 @@ const services_list_react = async list => {
308309
start_queue.push(service_object);
309310
}
310311
await Promise.all(
311-
start_queue.map(service_start)
312+
start_queue.map(service_update)
312313
);
313314
}
314315
const services_shutdown = () => (
@@ -325,7 +326,7 @@ const services_shutdown = () => (
325326
/**
326327
(re)start service
327328
*/
328-
const service_start = async service_object => {
329+
const service_update = async service_object => {
329330
if (!services_loaded_promise) {
330331
services_loaded_promise = new Promise(resolve => {
331332
services_loaded_promise_resolve = resolve;
@@ -359,7 +360,7 @@ const service_start = async service_object => {
359360
other.dependencies.includes(service_object)
360361
) {
361362
other.dependencies = null;
362-
service_start(other);
363+
service_update(other);
363364
}
364365
service_stop_inner(service_object);
365366
}
@@ -382,7 +383,7 @@ const service_start = async service_object => {
382383
timeout = setTimeout(() => (
383384
log_verbose && log('file updated: ' + path),
384385
service_object.file_function = null,
385-
service_start(service_object)
386+
service_update(service_object)
386387
), 50)
387388
));
388389
}
@@ -548,7 +549,7 @@ const service_stop = async service_object => {
548549
) {
549550
other.dependencies = null;
550551
if (other.status !== SERVICE_STATUS_STOPPING) {
551-
service_start(other);
552+
service_update(other);
552553
}
553554
}
554555

@@ -984,6 +985,7 @@ const request_handle = async (request, response, https) => {
984985
if (spam_enabled) spam('request', [https, request.url, request_ip]);
985986

986987
try {
988+
response.setHeader('Server', 'l3p3 rtjscomp v' + VERSION);
987989
const request_url_parsed = url.parse(request.url, false);
988990

989991
let path = request_url_parsed.pathname || '/';
@@ -1009,7 +1011,6 @@ const request_handle = async (request, response, https) => {
10091011
path_ghosts.has(path)
10101012
) return;
10111013

1012-
response.setHeader('Server', 'l3p3 rtjscomp v' + VERSION);
10131014
response.setHeader('Access-Control-Allow-Origin', '*');
10141015

10151016
let path_params = null;
@@ -1118,14 +1119,24 @@ const request_handle = async (request, response, https) => {
11181119

11191120
if (
11201121
path_hiddens.has(path) ||
1121-
path.endsWith('.service.js')
1122+
path.endsWith('.service.js') ||
1123+
(
1124+
file_stat = await (
1125+
fsp.stat(path_real)
1126+
.catch(() => {
1127+
throw 404;
1128+
})
1129+
)
1130+
).isDirectory()
11221131
) throw 403;
1123-
if (!fs.existsSync(path_real)) throw 404;
1124-
file_stat = fs.statSync(path_real);
1125-
if (file_stat.isDirectory()) throw 403;
11261132

11271133
if (file_dyn_enabled) { // compile file
1128-
const file_content = await fsp.readFile(path_real, 'utf8');
1134+
const file_content = await (
1135+
fsp.readFile(path_real, 'utf8')
1136+
.catch(() => {
1137+
throw 404;
1138+
})
1139+
);
11291140
try {
11301141
if (file_content.includes('\r')) {
11311142
throw 'illegal line break, must be unix';
@@ -1352,7 +1363,6 @@ const request_handle = async (request, response, https) => {
13521363
encodings.includes('zstd')
13531364
) {
13541365
file_compression = COMPRESS_METHOD_ZSTD;
1355-
response.setHeader('Content-Encoding', 'zstd');
13561366
if (!request_method_head) {
13571367
(
13581368
file_function_output = zlib.createZstdCompress(ZSTD_OPTIONS)
@@ -1364,7 +1374,6 @@ const request_handle = async (request, response, https) => {
13641374
encodings.includes('br')
13651375
) {
13661376
file_compression = COMPRESS_METHOD_BROTLI;
1367-
response.setHeader('Content-Encoding', 'br');
13681377
if (!request_method_head) {
13691378
(
13701379
file_function_output = zlib.createBrotliCompress()
@@ -1376,13 +1385,18 @@ const request_handle = async (request, response, https) => {
13761385
encodings.includes('gzip')
13771386
) {
13781387
file_compression = COMPRESS_METHOD_GZIP;
1379-
response.setHeader('Content-Encoding', 'gzip');
13801388
if (!request_method_head) {
13811389
(
13821390
file_function_output = zlib.createGzip(GZIP_OPTIONS)
13831391
).pipe(response);
13841392
}
13851393
}
1394+
if (file_compression > COMPRESS_METHOD_NONE) {
1395+
response.setHeader(
1396+
'Content-Encoding',
1397+
COMPRESS_METHODS[file_compression]
1398+
);
1399+
}
13861400
}
13871401

13881402
if (spam_enabled) spam('execute', [
@@ -1455,11 +1469,16 @@ const request_handle = async (request, response, https) => {
14551469
}ERROR!`);
14561470
}
14571471
}
1458-
else if (file_compression !== COMPRESS_METHOD_NONE) {
1472+
else if (file_compression > COMPRESS_METHOD_NONE) {
14591473
response.removeHeader('Content-Encoding');
14601474
}
1461-
if (typeof returned !== 'number') {
1462-
log(`[error] ${path}: invalid return type: ${typeof returned}`);
1475+
if (
1476+
typeof returned !== 'number' ||
1477+
err < 100 ||
1478+
err > 599 ||
1479+
err % 1 > 0
1480+
) {
1481+
log(`[error] ${path}: invalid status code: ${returned}`);
14631482
throw 500;
14641483
}
14651484
if (response.headersSent) {
@@ -1513,19 +1532,14 @@ const request_handle = async (request, response, https) => {
15131532
response.setHeader('Vary', 'Accept-Encoding');
15141533
}
15151534

1516-
switch (file_compression) {
1517-
case COMPRESS_METHOD_NONE:
1535+
if (file_compression === COMPRESS_METHOD_NONE) {
15181536
response.setHeader('Content-Length', file_stat.size);
1519-
break;
1520-
case COMPRESS_METHOD_GZIP:
1521-
response.setHeader('Content-Encoding', 'gzip');
1522-
break;
1523-
case COMPRESS_METHOD_BROTLI:
1524-
response.setHeader('Content-Encoding', 'br');
1525-
break;
1526-
case COMPRESS_METHOD_ZSTD:
1527-
response.setHeader('Content-Encoding', 'zstd');
1528-
break;
1537+
}
1538+
else {
1539+
response.setHeader(
1540+
'Content-Encoding',
1541+
COMPRESS_METHODS[file_compression]
1542+
);
15291543
}
15301544

15311545
if (request_method_head) {
@@ -1540,7 +1554,7 @@ const request_handle = async (request, response, https) => {
15401554
catch (err) {
15411555
// catch internal errors
15421556
if (typeof err !== 'number') {
1543-
console.error(err);
1557+
console.error('[internal error]', err);
15441558
err = 500;
15451559
}
15461560

0 commit comments

Comments
 (0)