rtjscomp 0.8.5 → 0.8.7

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 (2) hide show
  1. package/package.json +1 -1
  2. package/rtjscomp.js +50 -47
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rtjscomp",
3
- "version": "0.8.5",
3
+ "version": "0.8.7",
4
4
  "description": "php-like server but with javascript",
5
5
  "repository": {
6
6
  "type": "git",
package/rtjscomp.js CHANGED
@@ -42,6 +42,7 @@ const file_privates = new Set;
42
42
  const file_blocks = new Set;
43
43
  /// any path -> file
44
44
  const path_aliases = new Map;
45
+ const path_aliases_reverse = new Map;
45
46
  const path_aliases_templates = new Map;
46
47
  const services = new Set;
47
48
  /// compiled file handlers
@@ -446,39 +447,39 @@ const request_handle = async (request, response, https) => {
446
447
  let path_params = null;
447
448
  let request_body_promise = null;
448
449
 
450
+ if (path_aliases_reverse.has(path)) {
451
+ response.setHeader('Location', path_aliases_reverse.get(path));
452
+ throw 301;
453
+ }
449
454
  if (path_aliases.has(path)) {
450
- response.setHeader(
451
- 'Content-Location',
452
- path = path_aliases.get(path)
453
- );
455
+ path = path_aliases.get(path)
454
456
  }
455
457
  else { // aliases with *
456
458
  const path_split = path.split('/');
457
- const templates = path_aliases_templates.get(path_split[0]);
458
- if (templates) {
459
- path_split.shift();
460
- template: for (const template_pair of templates) {
461
- const template = template_pair[0];
462
- const template_length = template.length;
463
- if (template_length !== path_split.length) continue;
464
- const params = {};
465
- for (let i = 0; i < template_length; ++i) {
466
- if (template[i].charCodeAt(0) === 42) {
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;
459
+ const templates = path_aliases_templates.get(
460
+ path_split.shift()
461
+ );
462
+ if (templates != null)
463
+ template: for (const template of templates) {
464
+ const template_path = template.path_split;
465
+ const template_path_length = template_path.length;
466
+ if (template_path_length !== path_split.length) continue;
467
+ const params = {};
468
+ for (let i = 0; i < template_path_length; ++i) {
469
+ if (template_path[i].charCodeAt(0) === 42) {
470
+ if (template_path[i].length > 1) {
471
+ params[
472
+ template_path[i].slice(1)
473
+ ] = path_split[i];
473
474
  }
474
475
  }
475
- response.setHeader(
476
- 'Content-Location',
477
- path = template_pair[1]
478
- );
479
- path_params = params;
480
- break;
476
+ else if (template_path[i] !== path_split[i]) {
477
+ continue template;
478
+ }
481
479
  }
480
+ path = template.value;
481
+ path_params = params;
482
+ break;
482
483
  }
483
484
  }
484
485
 
@@ -504,11 +505,9 @@ const request_handle = async (request, response, https) => {
504
505
  !file_raws.has(path)
505
506
  );
506
507
 
507
- if (file_dyn_enabled) {
508
- if (
509
- request_method !== 'GET' &&
510
- 'content-length' in request_headers
511
- ) {
508
+ if (request_method !== 'GET') {
509
+ if (!file_dyn_enabled) throw 405;
510
+ if ('content-length' in request_headers) {
512
511
  request_body_promise = new Promise(resolve => {
513
512
  const request_body_chunks = [];
514
513
  request.on('data', chunk => {
@@ -521,9 +520,6 @@ const request_handle = async (request, response, https) => {
521
520
  });
522
521
  }
523
522
  }
524
- else if (request_method !== 'GET') {
525
- throw 400;
526
- }
527
523
 
528
524
  let file_function = null;
529
525
  let file_stat = null;
@@ -939,12 +935,13 @@ file_keep_new(PATH_CONFIG + 'services.txt', data => (
939
935
  ));
940
936
 
941
937
  await Promise.all([
942
- file_keep_new(PATH_CONFIG + 'init.js', data => {
938
+ file_keep_new(PATH_CONFIG + 'init.js', async data => {
943
939
  if (!data) return;
944
940
  log('[deprecated] run global init script');
945
941
  try {
946
- var require = custom_require;
947
- (0, eval)(data);
942
+ await (
943
+ new AsyncFunction('require', data)
944
+ )(custom_require);
948
945
  }
949
946
  catch (err) {
950
947
  log('[error] init.js: ' + err.message);
@@ -958,20 +955,26 @@ await Promise.all([
958
955
  }),
959
956
  file_keep_new(PATH_CONFIG + 'path_aliases.txt', data => {
960
957
  map_generate_equ(path_aliases, data);
958
+ path_aliases_reverse.clear();
961
959
  path_aliases_templates.clear();
962
960
  for (const [key, value] of path_aliases.entries()) {
963
- const star_index = key.indexOf('*');
964
- if (star_index < 0) continue;
965
- path_aliases.delete(key);
966
- const template = key.split('/');
967
- const first = template.shift();
968
- if (path_aliases_templates.has(first)) {
969
- path_aliases_templates.get(first).push([template, value]);
961
+ if (key.includes('*')) {
962
+ path_aliases.delete(key);
963
+ const path_split = key.split('/');
964
+ const first = path_split.shift();
965
+ if (path_aliases_templates.has(first)) {
966
+ path_aliases_templates.get(first).push(
967
+ {path_split, value}
968
+ );
969
+ }
970
+ else {
971
+ path_aliases_templates.set(first, [
972
+ {path_split, value},
973
+ ]);
974
+ }
970
975
  }
971
976
  else {
972
- path_aliases_templates.set(first, [
973
- [template, value],
974
- ]);
977
+ path_aliases_reverse.set(value, key);
975
978
  }
976
979
  }
977
980
  }),