@makano/rew 1.1.73 → 1.1.81

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. package/lib/rew/cli/cli.js +189 -144
  2. package/lib/rew/cli/log.js +18 -19
  3. package/lib/rew/cli/run.js +7 -9
  4. package/lib/rew/cli/utils.js +109 -95
  5. package/lib/rew/const/config_path.js +4 -0
  6. package/lib/rew/const/default.js +21 -3
  7. package/lib/rew/const/files.js +11 -8
  8. package/lib/rew/const/opt.js +6 -6
  9. package/lib/rew/css/theme.css +1 -1
  10. package/lib/rew/functions/core.js +7 -9
  11. package/lib/rew/functions/emitter.js +2 -2
  12. package/lib/rew/functions/exec.js +19 -15
  13. package/lib/rew/functions/export.js +4 -6
  14. package/lib/rew/functions/fs.js +21 -18
  15. package/lib/rew/functions/future.js +1 -1
  16. package/lib/rew/functions/import.js +55 -25
  17. package/lib/rew/functions/map.js +2 -5
  18. package/lib/rew/functions/match.js +21 -5
  19. package/lib/rew/functions/path.js +2 -2
  20. package/lib/rew/functions/require.js +16 -13
  21. package/lib/rew/functions/stdout.js +11 -11
  22. package/lib/rew/functions/types.js +67 -42
  23. package/lib/rew/html/ui.html +5 -6
  24. package/lib/rew/html/ui.js +100 -58
  25. package/lib/rew/main.js +3 -3
  26. package/lib/rew/misc/findAppInfo.js +16 -0
  27. package/lib/rew/misc/findAppPath.js +21 -0
  28. package/lib/rew/misc/seededid.js +13 -0
  29. package/lib/rew/models/struct.js +1 -1
  30. package/lib/rew/modules/compiler.js +90 -58
  31. package/lib/rew/modules/context.js +35 -22
  32. package/lib/rew/modules/runtime.js +1 -1
  33. package/lib/rew/pkgs/conf.js +50 -33
  34. package/lib/rew/pkgs/data.js +7 -2
  35. package/lib/rew/pkgs/date.js +8 -9
  36. package/lib/rew/pkgs/env.js +3 -5
  37. package/lib/rew/pkgs/modules/data/bintree.js +1 -1
  38. package/lib/rew/pkgs/modules/data/doublylinked.js +1 -1
  39. package/lib/rew/pkgs/modules/data/linkedList.js +1 -1
  40. package/lib/rew/pkgs/modules/data/queue.js +1 -2
  41. package/lib/rew/pkgs/modules/data/stack.js +1 -1
  42. package/lib/rew/pkgs/modules/threads/worker.js +31 -21
  43. package/lib/rew/pkgs/modules/ui/classes.js +68 -61
  44. package/lib/rew/pkgs/pkgs.js +5 -6
  45. package/lib/rew/pkgs/rune.js +437 -0
  46. package/lib/rew/pkgs/threads.js +30 -22
  47. package/lib/rew/pkgs/ui.js +68 -44
  48. package/package.json +8 -2
@@ -1,8 +1,8 @@
1
- const { Worker } = require('worker_threads');
2
- const emitter = require('../functions/emitter');
3
- const future = require('../functions/future');
4
- const { struct } = require('../models/struct');
5
- const path = require('path');
1
+ const { Worker } = require("worker_threads");
2
+ const emitter = require("../functions/emitter");
3
+ const future = require("../functions/future");
4
+ const { struct } = require("../models/struct");
5
+ const path = require("path");
6
6
 
7
7
  module.exports = (context) => ({
8
8
  thread: (cb) => {
@@ -10,7 +10,7 @@ module.exports = (context) => ({
10
10
 
11
11
  const stopWorker = (worker) => {
12
12
  workers.splice(workers.indexOf(worker), 1);
13
- worker.terminate()
13
+ worker.terminate();
14
14
  };
15
15
 
16
16
  const athread = {
@@ -19,53 +19,61 @@ module.exports = (context) => ({
19
19
  workers.forEach(stopWorker);
20
20
  },
21
21
  start: (context) => {
22
- let dataResult = future(() => {}), listener = emitter();
23
- const worker = new Worker(path.resolve(__dirname, './modules/threads/worker.js'), {
24
- workerData: { context, cb: cb.toString() }
25
- });
22
+ let dataResult = future(() => {}),
23
+ listener = emitter();
24
+ const worker = new Worker(
25
+ path.resolve(__dirname, "./modules/threads/worker.js"),
26
+ {
27
+ workerData: { context, cb: cb.toString() },
28
+ },
29
+ );
26
30
  workers.push(worker);
27
31
 
28
32
  const stop = () => stopWorker(worker);
29
33
 
30
- worker.on('message', (data) => {
31
- if (data.type === 'result') {
34
+ worker.on("message", (data) => {
35
+ if (data.type === "result") {
32
36
  dataResult.resolve(data.result);
33
37
  stop();
34
- } else if (data.type === 'error') {
38
+ } else if (data.type === "error") {
35
39
  reject(new Error(data.error));
36
40
  stop();
37
- } else if (data.type === 'event') {
41
+ } else if (data.type === "event") {
38
42
  listener.emit(data.event, data.data);
39
43
  }
40
44
  });
41
45
 
42
- worker.on('error', (error) => {
46
+ worker.on("error", (error) => {
43
47
  console.log(error);
44
48
  stop();
45
49
  });
46
50
 
47
51
  let exiting = false;
48
52
 
49
- worker.on('exit', (code) => {
50
- if(exiting) return;
53
+ worker.on("exit", (code) => {
54
+ if (exiting) return;
51
55
  stop();
52
56
  if (code !== 0) {
53
57
  throw new Error(`Worker stopped with exit code ${code}`);
54
58
  }
55
59
  });
56
60
 
57
- worker.postMessage({ type: 'start' });
61
+ worker.postMessage({ type: "start" });
58
62
 
59
63
  return {
60
64
  on: (e, cb) => listener.on(e, cb),
61
65
  off: (e, cb) => listener.off(e, cb),
62
- emit: (e, data) => worker?.postMessage({ type: 'event', event: e, data }),
66
+ emit: (e, data) =>
67
+ worker?.postMessage({ type: "event", event: e, data }),
63
68
  get: () => dataResult.wait(),
64
- stop: () => { exiting = true; stop(); }
69
+ stop: () => {
70
+ exiting = true;
71
+ stop();
72
+ },
65
73
  };
66
- }
74
+ },
67
75
  };
68
76
 
69
77
  return athread;
70
- }
78
+ },
71
79
  });
@@ -4,7 +4,7 @@ const fs = require("fs");
4
4
  const { uiClasses } = require("./modules/ui/classes");
5
5
  const { generateRandomID } = require("../functions/id");
6
6
  const { THEME_PATH } = require("../const/files");
7
- const readline = require('readline');
7
+ const readline = require("readline");
8
8
  const emitter = require("../functions/emitter");
9
9
 
10
10
  const BIN_PATH = path.resolve(__dirname, "../../../bin/ui");
@@ -12,27 +12,31 @@ const HTML_STRING = fs.readFileSync(
12
12
  path.resolve(__dirname, "../html/ui.html"),
13
13
  { encoding: "utf-8" },
14
14
  );
15
- const JS_STRING = fs.readFileSync(
16
- path.resolve(__dirname, "../html/ui.js"),
17
- { encoding: "utf-8" },
18
- );
15
+ const JS_STRING = fs.readFileSync(path.resolve(__dirname, "../html/ui.js"), {
16
+ encoding: "utf-8",
17
+ });
19
18
 
20
- const replaceString = (string, options) => string.replace(/\$OPTIONS\(([^)]+)\)/g, (_, n) => n.startsWith('json.') ? JSON.stringify(options[n.split('json.')[1]] || '{}') : options[n] || _);
19
+ const replaceString = (string, options) =>
20
+ string.replace(/\$OPTIONS\(([^)]+)\)/g, (_, n) =>
21
+ n.startsWith("json.")
22
+ ? JSON.stringify(options[n.split("json.")[1]] || "{}")
23
+ : options[n] || _,
24
+ );
21
25
 
22
26
  const defaultOptions = {
23
27
  title: "Title",
24
28
  onExit: () => process.exit(),
25
- style: '',
29
+ style: "",
26
30
  stylePath: THEME_PATH,
27
- exec: () => { },
28
- execContext: {}
31
+ exec: () => {},
32
+ execContext: {},
29
33
  };
30
34
 
31
35
  module.exports = (context) => ({
32
36
  start: (o = {}) => {
33
37
  const options = {
34
38
  ...defaultOptions,
35
- ...o
39
+ ...o,
36
40
  };
37
41
 
38
42
  const hookedSocketListeners = {};
@@ -42,9 +46,13 @@ module.exports = (context) => ({
42
46
 
43
47
  options.runId = runId;
44
48
 
45
- if (fs.existsSync(options.stylePath)) options.style = fs.readFileSync(options.stylePath, { encoding: 'utf-8' }) + '\n' + options.style;
49
+ if (fs.existsSync(options.stylePath))
50
+ options.style =
51
+ fs.readFileSync(options.stylePath, { encoding: "utf-8" }) +
52
+ "\n" +
53
+ options.style;
46
54
 
47
- options.style = ' */\n' + options.style + '\n/* ';
55
+ options.style = " */\n" + options.style + "\n/* ";
48
56
 
49
57
  const HTML = replaceString(HTML_STRING, options);
50
58
  const JS = replaceString(JS_STRING, options);
@@ -56,39 +64,44 @@ module.exports = (context) => ({
56
64
  const queue = [];
57
65
 
58
66
  const send = (data) => {
59
- const content = fs.readFileSync(tmpFile, { encoding: 'utf-8' });
67
+ const content = fs.readFileSync(tmpFile, { encoding: "utf-8" });
60
68
  if (content) {
61
69
  queue.push(data);
62
70
  } else {
63
- fs.writeFileSync(tmpFile, typeof data !== "string" ? JSON.stringify(data) : data);
71
+ fs.writeFileSync(
72
+ tmpFile,
73
+ typeof data !== "string" ? JSON.stringify(data) : data,
74
+ );
64
75
  }
65
- }
76
+ };
66
77
 
67
78
  const sendEvent = (data) => {
68
- send({ action: 'JS', data: `window.recieveMessage(${JSON.stringify(data)})` })
69
- }
79
+ send({
80
+ action: "JS",
81
+ data: `window.recieveMessage(${JSON.stringify(data)})`,
82
+ });
83
+ };
70
84
 
71
85
  const g_emitter = emitter();
72
86
 
73
87
  const recieve = (data) => {
74
- g_emitter.emit('recieve', data);
75
- }
88
+ g_emitter.emit("recieve", data);
89
+ };
76
90
 
77
91
  const rl = readline.createInterface({
78
92
  input: process.stdin,
79
- output: process.stdout
93
+ output: process.stdout,
80
94
  });
81
95
 
82
- rl.question('', () => { });
96
+ rl.question("", () => {});
83
97
 
84
- fs.writeFileSync(tmpFile, '');
98
+ fs.writeFileSync(tmpFile, "");
85
99
 
86
- fs.watch(tmpFile, { encoding: 'utf-8' })
87
- .on('change', () => {
88
- if (queue.length) {
89
- send(queue.pop());
90
- }
91
- });
100
+ fs.watch(tmpFile, { encoding: "utf-8" }).on("change", () => {
101
+ if (queue.length) {
102
+ send(queue.pop());
103
+ }
104
+ });
92
105
 
93
106
  const p = spawn(options.bin || BIN_PATH, [runId]);
94
107
 
@@ -102,10 +115,10 @@ module.exports = (context) => ({
102
115
  fs.unlinkSync(tmpFile);
103
116
  });
104
117
 
105
- g_emitter.on('recieve', (edata) => {
106
- if (edata.action.startsWith('hook:')) {
118
+ g_emitter.on("recieve", (edata) => {
119
+ if (edata.action.startsWith("hook:")) {
107
120
  const hook = hookedSocketListeners[edata.data.rid];
108
- const type = edata.action.split('hook:')[1];
121
+ const type = edata.action.split("hook:")[1];
109
122
  if (hook && hook.type == type) {
110
123
  hookedSocketListeners[edata.data.rid].cb(edata.data.object);
111
124
  if (hook.once) delete hookedSocketListeners[edata.data.rid];
@@ -119,19 +132,30 @@ module.exports = (context) => ({
119
132
  const d = data.toString().split("RESPONSE::")[1];
120
133
  const jd = JSON.parse(d);
121
134
  recieve(jd);
122
- } else if (data.toString().trim().endsWith('SETUP::READY')) {
123
- console.log('READY');
124
- r(uiClasses(context, options, sendEvent, (cb) => {
125
- g_emitter.on('recieve', cb);
126
- }, (rid, type, cb, once = true) => { // Add hook
127
- hookedSocketListeners[rid] = { type, cb, once };
128
- }, (rid) => { // Remove hook
129
- delete hookedSocketListeners[rid];
130
- }));
131
- } else if (data.toString().endsWith('SETUP::HTML')) {
132
- send({ action: 'JS2', data: JS, isSetup: true });
133
- } else if (data.toString() == 'INIT::READY') {
134
- send({ action: 'HTML', data: HTML });
135
+ } else if (data.toString().trim().endsWith("SETUP::READY")) {
136
+ console.log("READY");
137
+ r(
138
+ uiClasses(
139
+ context,
140
+ options,
141
+ sendEvent,
142
+ (cb) => {
143
+ g_emitter.on("recieve", cb);
144
+ },
145
+ (rid, type, cb, once = true) => {
146
+ // Add hook
147
+ hookedSocketListeners[rid] = { type, cb, once };
148
+ },
149
+ (rid) => {
150
+ // Remove hook
151
+ delete hookedSocketListeners[rid];
152
+ },
153
+ ),
154
+ );
155
+ } else if (data.toString().endsWith("SETUP::HTML")) {
156
+ send({ action: "JS2", data: JS, isSetup: true });
157
+ } else if (data.toString() == "INIT::READY") {
158
+ send({ action: "HTML", data: HTML });
135
159
  } else {
136
160
  console.log(data.toString());
137
161
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@makano/rew",
3
- "version": "1.1.73",
3
+ "version": "1.1.81",
4
4
  "description": "A simple coffescript runtime",
5
5
  "main": "lib/rew/main.js",
6
6
  "directories": {
@@ -24,7 +24,11 @@
24
24
  "type": "git",
25
25
  "url": "git+https://github.com/kevinj045/rew.git"
26
26
  },
27
- "keywords": ["coffescript", "rew", "runtime"],
27
+ "keywords": [
28
+ "coffescript",
29
+ "rew",
30
+ "runtime"
31
+ ],
28
32
  "author": "makano",
29
33
  "license": "ISC",
30
34
  "dependencies": {
@@ -34,6 +38,8 @@
34
38
  "chalk": "^5.3.0",
35
39
  "chokidar": "^3.6.0",
36
40
  "js-yaml": "^4.1.0",
41
+ "tiny-msgpack": "^2.2.0",
42
+ "uuid": "^9.0.1",
37
43
  "vm": "^0.1.0",
38
44
  "yargs": "^17.7.2"
39
45
  },