rtjscomp 0.8.4 → 0.8.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +2 -2
  2. package/package.json +1 -1
  3. package/rtjscomp.js +42 -29
package/README.md CHANGED
@@ -11,13 +11,13 @@ easy to use http server that allows for using javascript just as php was used ba
11
11
  go into the directory where you want to have your project and run
12
12
 
13
13
  ```console
14
- $ npx rtjscomp
14
+ $ npx --yes rtjscomp@latest
15
15
  ```
16
16
 
17
17
  or in case you prefer [bun](https://bun.sh):
18
18
 
19
19
  ```console
20
- $ bunx rtjscomp
20
+ $ bunx --bun rtjscomp@latest
21
21
  ```
22
22
 
23
23
  and now http://localhost:8080 offers a greeting!
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rtjscomp",
3
- "version": "0.8.4",
3
+ "version": "0.8.6",
4
4
  "description": "php-like server but with javascript",
5
5
  "repository": {
6
6
  "type": "git",
package/rtjscomp.js CHANGED
@@ -422,22 +422,23 @@ const request_handle = async (request, response, https) => {
422
422
  try {
423
423
  const request_url_parsed = url.parse(request.url, false);
424
424
 
425
- let path = request_url_parsed.pathname || '';
426
-
427
- // ignore (timeout) many hack attempts
428
- if (path.includes('php') || path.includes('sql')) return;
429
-
430
- // remove leading/trailing /
431
- while (path.charCodeAt(0) === 47) {
432
- path = path.substring(1);
433
- }
434
- while (path.charCodeAt(path.length - 1) === 47) {
435
- path = path.substring(0, path.length - 1);
425
+ let path = request_url_parsed.pathname || '/';
426
+ if (
427
+ path.charCodeAt(0) !== 47 ||
428
+ path.includes('//')
429
+ ) throw 404;
430
+ if (path.length > 1 && path.endsWith('/')) {
431
+ response.setHeader('Location', path.slice(0, -1));
432
+ throw 301;
436
433
  }
434
+ path = path.slice(1);
437
435
 
438
- if (path.includes('..') || path.includes('~')) throw 403;
439
-
440
- if (file_blocks.has(path)) return;
436
+ // ignore (timeout) many hack attempts
437
+ if (
438
+ path.includes('php') ||
439
+ path.includes('sql') ||
440
+ file_blocks.has(path)
441
+ ) return;
441
442
 
442
443
  response.setHeader('Server', 'l3p3 rtjscomp v' + VERSION);
443
444
  response.setHeader('Access-Control-Allow-Origin', '*');
@@ -463,11 +464,18 @@ const request_handle = async (request, response, https) => {
463
464
  const params = {};
464
465
  for (let i = 0; i < template_length; ++i) {
465
466
  if (template[i].charCodeAt(0) === 42) {
466
- if (template[i].length > 1) params[template[i].substr(1)] = path_split[i];
467
+ if (template[i].length > 1) {
468
+ params[template[i].slice(1)] = path_split[i];
469
+ }
470
+ }
471
+ else if (template[i] !== path_split[i]) {
472
+ continue template;
467
473
  }
468
- else if (template[i] !== path_split[i]) continue template;
469
474
  }
470
- response.setHeader('Content-Location', path = template_pair[1]);
475
+ response.setHeader(
476
+ 'Content-Location',
477
+ path = template_pair[1]
478
+ );
471
479
  path_params = params;
472
480
  break;
473
481
  }
@@ -475,9 +483,13 @@ const request_handle = async (request, response, https) => {
475
483
  }
476
484
 
477
485
  const file_type_index = path.lastIndexOf('.');
478
- // no type ending -> dir?
479
- if (file_type_index <= path.lastIndexOf('/')) throw 404;
480
- const file_type = path.substring(
486
+ if (
487
+ path.includes('..') ||
488
+ path.includes('~') ||
489
+ // no type ending -> dir?
490
+ file_type_index <= path.lastIndexOf('/')
491
+ ) throw 404;
492
+ const file_type = path.slice(
481
493
  file_type_index + 1
482
494
  ).toLowerCase();
483
495
 
@@ -575,7 +587,7 @@ const request_handle = async (request, response, https) => {
575
587
  if (file_content.charCodeAt(index_start) !== 61) {
576
588
  code += (
577
589
  file_content
578
- .substring(
590
+ .slice(
579
591
  index_start,
580
592
  index_end
581
593
  )
@@ -586,7 +598,7 @@ const request_handle = async (request, response, https) => {
586
598
  else { // `<?=`?
587
599
  code += `output.write(''+(${
588
600
  file_content
589
- .substring(
601
+ .slice(
590
602
  ++index_start,
591
603
  index_end
592
604
  )
@@ -617,7 +629,7 @@ const request_handle = async (request, response, https) => {
617
629
  if (index_start < index_end) {
618
630
  code += `output.write(${
619
631
  JSON.stringify(
620
- file_content.substring(index_start, index_end)
632
+ file_content.slice(index_start, index_end)
621
633
  )
622
634
  });`;
623
635
  }
@@ -667,11 +679,11 @@ const request_handle = async (request, response, https) => {
667
679
  if (index_equ > 0) {
668
680
  file_function_input[
669
681
  cookie
670
- .substring(0, index_equ)
682
+ .slice(0, index_equ)
671
683
  .trimRight()
672
684
  ] = decodeURI(
673
685
  cookie
674
- .substr(index_equ + 1)
686
+ .slice(index_equ + 1)
675
687
  .trimLeft()
676
688
  );
677
689
  }
@@ -766,7 +778,7 @@ const request_handle = async (request, response, https) => {
766
778
  .map(e => e[0] === 'password' ? [e[0], '***'] : e)
767
779
  .map(e => e[0] === 'file' ? [e[0], '...'] : e)
768
780
  .map(e => (typeof e[1] === 'object' && !e[1].length) ? [e[0], Object.keys(e[1]).slice(0, 20)] : e)
769
- .map(e => (e[0] !== 'user_agent' && typeof e[1] === 'string' && e[1].length > 20) ? [e[0], e[1].substr(0, 20) + '...'] : e)
781
+ .map(e => (e[0] !== 'user_agent' && typeof e[1] === 'string' && e[1].length > 20) ? [e[0], e[1].slice(0, 20) + '...'] : e)
770
782
  )
771
783
  ]);
772
784
 
@@ -927,12 +939,13 @@ file_keep_new(PATH_CONFIG + 'services.txt', data => (
927
939
  ));
928
940
 
929
941
  await Promise.all([
930
- file_keep_new(PATH_CONFIG + 'init.js', data => {
942
+ file_keep_new(PATH_CONFIG + 'init.js', async data => {
931
943
  if (!data) return;
932
944
  log('[deprecated] run global init script');
933
945
  try {
934
- var require = custom_require;
935
- (0, eval)(data);
946
+ await (
947
+ new AsyncFunction('require', data)
948
+ )(custom_require);
936
949
  }
937
950
  catch (err) {
938
951
  log('[error] init.js: ' + err.message);