rtjscomp 0.8.2 → 0.8.3

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 +59 -21
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rtjscomp",
3
- "version": "0.8.2",
3
+ "version": "0.8.3",
4
4
  "description": "php-like server but with javascript",
5
5
  "repository": {
6
6
  "type": "git",
package/rtjscomp.js CHANGED
@@ -25,6 +25,7 @@ const GZIP_OPTIONS = {level: 9};
25
25
  const AGENT_CHECK_BOT = /bot|googlebot|crawler|spider|robot|crawling|favicon/i;
26
26
  const AGENT_CHECK_MOBIL = /(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows ce|xda|xiino|android|ipad|playbook|silk/i;
27
27
  const HTTP_LIST_REG = /,\s*/;
28
+ const IMPORT_REG = /import\(/g;
28
29
 
29
30
  let log_verbose = process.argv.includes('-v');
30
31
  let port_http = 0;
@@ -62,7 +63,7 @@ if (!Object.fromEntries) {
62
63
  global.globals = rtjscomp;
63
64
  global.actions = rtjscomp.actions;
64
65
  global.data_load = name => {
65
- log('[deprecated] load: ' + name);
66
+ log('[deprecated] synchronous load: ' + PATH_DATA + name);
66
67
  try {
67
68
  return fs.readFileSync(PATH_DATA + name, 'utf8');
68
69
  }
@@ -71,7 +72,7 @@ global.data_load = name => {
71
72
  }
72
73
  }
73
74
  global.data_save = (name, data) => (
74
- log('[deprecated] save: ' + name),
75
+ log('[deprecated] synchronous save: ' + PATH_DATA + name),
75
76
  fs.writeFileSync(PATH_DATA + name, data, 'utf8')
76
77
  )
77
78
  global.number_check_int = number => (
@@ -104,19 +105,42 @@ rtjscomp.data_save = (name, data) => (
104
105
  )
105
106
  )
106
107
 
108
+ const custom_require_paths = new Set;
107
109
  const custom_require_cache = new Map;
110
+ const custom_import_cache = new Map;
108
111
  const custom_require = path => {
109
112
  let result = custom_require_cache.get(path);
110
113
  if (result != null) return result;
111
114
 
115
+ log_verbose && log('require: ' + path);
116
+ const path_real = require.resolve(path, resolve_options);
117
+ custom_require_paths.add(path_real);
112
118
  custom_require_cache.set(
113
119
  path,
114
- result = require(
115
- require.resolve(path, resolve_options)
120
+ result = require(path_real)
121
+ );
122
+ return result;
123
+ }
124
+ const custom_import = async path => {
125
+ let result = custom_import_cache.get(path);
126
+ if (result != null) return result;
127
+
128
+ log_verbose && log('import: ' + path);
129
+ custom_import_cache.set(
130
+ path,
131
+ result = await import(
132
+ 'file://' + require.resolve(path, resolve_options)
116
133
  )
117
134
  );
118
135
  return result;
119
136
  }
137
+ actions.module_cache_clear = () => {
138
+ for (const path of custom_require_paths) {
139
+ delete require.cache[path];
140
+ }
141
+ custom_require_cache.clear();
142
+ custom_import_cache.clear();
143
+ }
120
144
 
121
145
  const services_active = new Map;
122
146
  const services_loading = new Set;
@@ -161,16 +185,22 @@ const service_start_inner = async (path, service_object, file_content) => {
161
185
  log('start service: ' + path);
162
186
 
163
187
  const start_interval = setInterval(() => {
164
- log('[warning] still starting service: ' + path);
188
+ log(`[warning] ${path}: still starting`);
165
189
  }, 1e3);
166
190
 
191
+ if (file_content.includes('globals.')) {
192
+ log(`[deprecated] ${path}: uses globals object`);
193
+ }
194
+
167
195
  try {
168
196
  const fun = (0, eval)(
169
- `(async function(require){const log=a=>rtjscomp.log(${
197
+ `(async function(require,custom_import){const log=a=>rtjscomp.log(${
170
198
  JSON.stringify(path + ': ')
171
- }+a);${file_content + '\n'}})`
199
+ }+a);${
200
+ file_content.replace(IMPORT_REG, 'custom_import(') + '\n'
201
+ }})`
172
202
  );
173
- const result = await fun.call(content_object, custom_require);
203
+ const result = await fun.call(content_object, custom_require, custom_import);
174
204
  if (service_object.stopped) {
175
205
  clearInterval(start_interval);
176
206
  return;
@@ -188,7 +218,7 @@ const service_start_inner = async (path, service_object, file_content) => {
188
218
 
189
219
  const handler_start = content_object.start;
190
220
  if (handler_start) {
191
- log('[deprecated] service has start method: ' + path);
221
+ log(`[deprecated] ${path}: has start method`);
192
222
  delete content_object.start;
193
223
  try {
194
224
  await handler_start();
@@ -202,7 +232,7 @@ const service_start_inner = async (path, service_object, file_content) => {
202
232
  clearInterval(start_interval);
203
233
 
204
234
  if (content_object.stop) {
205
- log('[deprecated] service has stop method: ' + path);
235
+ log(`[deprecated] ${path}: has stop method`);
206
236
  service_object.handler_stop = content_object.stop;
207
237
  delete content_object.stop;
208
238
  }
@@ -228,7 +258,7 @@ const service_stop_handler = async service_object => {
228
258
  const handler_stop = service_object.handler_stop;
229
259
  if (handler_stop) {
230
260
  const stop_interval = setInterval(() => {
231
- log('[warning] still stopping service: ' + service_object.path);
261
+ log(`[warning] ${service_object.path}: still stopping`);
232
262
  }, 1e3);
233
263
  try {
234
264
  service_object.handler_stop = null;
@@ -485,9 +515,12 @@ const request_handle = async (request, response, https) => {
485
515
  if (file_content.includes('\r')) {
486
516
  throw 'illegal line break, must be unix';
487
517
  }
518
+ if (file_content.includes('globals.')) {
519
+ log(`[deprecated] ${path}: uses globals object`);
520
+ }
488
521
  const file_content_length = file_content.length;
489
522
 
490
- let code = `async (input,output,request,response,require)=>{const log=a=>rtjscomp.log(${
523
+ let code = `async (input,output,request,response,require,custom_import)=>{const log=a=>rtjscomp.log(${
491
524
  JSON.stringify(path + ': ')
492
525
  }+a);`;
493
526
 
@@ -512,19 +545,23 @@ const request_handle = async (request, response, https) => {
512
545
  // `<?`?
513
546
  if (file_content.charCodeAt(index_start) !== 61) {
514
547
  code += (
515
- file_content.substring(
516
- index_start,
517
- index_end
518
- ) +
548
+ file_content
549
+ .substring(
550
+ index_start,
551
+ index_end
552
+ )
553
+ .replace(IMPORT_REG, 'custom_import(') +
519
554
  ';'
520
555
  );
521
556
  }
522
557
  else { // `<?=`?
523
558
  code += `output.write(''+(${
524
- file_content.substring(
525
- ++index_start,
526
- index_end
527
- )
559
+ file_content
560
+ .substring(
561
+ ++index_start,
562
+ index_end
563
+ )
564
+ .replace(IMPORT_REG, 'custom_import(')
528
565
  }));`;
529
566
  }
530
567
  }
@@ -706,7 +743,8 @@ const request_handle = async (request, response, https) => {
706
743
  file_function_output,
707
744
  request,
708
745
  response,
709
- custom_require
746
+ custom_require,
747
+ custom_import
710
748
  );
711
749
  file_function_output.end();
712
750
  }