verteilen-core 1.2.3 → 1.2.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 (94) hide show
  1. package/dist/index.d.ts +3 -0
  2. package/dist/jest.config.d.ts +2 -0
  3. package/dist/src/client/analysis.d.ts +35 -0
  4. package/dist/src/client/analysis.js +318 -319
  5. package/dist/src/client/client.d.ts +25 -0
  6. package/dist/src/client/client.js +162 -168
  7. package/dist/src/client/cluster.d.ts +1 -0
  8. package/dist/src/client/cluster.js +1 -2
  9. package/dist/src/client/database.d.ts +13 -0
  10. package/dist/src/client/database.js +28 -27
  11. package/dist/src/client/execute.d.ts +24 -0
  12. package/dist/src/client/execute.js +61 -51
  13. package/dist/src/client/http.d.ts +7 -0
  14. package/dist/src/client/http.js +29 -26
  15. package/dist/src/client/javascript.d.ts +47 -0
  16. package/dist/src/client/javascript.js +110 -136
  17. package/dist/src/client/job_database.d.ts +10 -0
  18. package/dist/src/client/job_database.js +27 -29
  19. package/dist/src/client/job_execute.d.ts +20 -0
  20. package/dist/src/client/job_execute.js +148 -146
  21. package/dist/src/client/os.d.ts +29 -0
  22. package/dist/src/client/os.js +131 -136
  23. package/dist/src/client/resource.d.ts +7 -0
  24. package/dist/src/client/resource.js +93 -104
  25. package/dist/src/client/shell.d.ts +16 -0
  26. package/dist/src/client/shell.js +102 -98
  27. package/dist/src/computed.d.ts +13 -0
  28. package/dist/src/interface/base.d.ts +70 -0
  29. package/dist/src/interface/bus.d.ts +90 -0
  30. package/dist/src/interface/enum.d.ts +143 -0
  31. package/dist/src/interface/enum.js +5 -5
  32. package/dist/src/interface/execute.d.ts +20 -0
  33. package/dist/src/interface/record.d.ts +93 -0
  34. package/dist/src/interface/server.d.ts +76 -0
  35. package/dist/src/interface/struct.d.ts +182 -0
  36. package/dist/src/interface/table.d.ts +20 -0
  37. package/dist/src/interface/ui.d.ts +29 -0
  38. package/dist/src/interface.d.ts +29 -0
  39. package/dist/src/plugins/i18n.d.ts +1666 -0
  40. package/dist/src/script/console_manager.d.ts +18 -0
  41. package/dist/src/script/console_manager.js +97 -91
  42. package/dist/src/script/console_server_manager.d.ts +13 -0
  43. package/dist/src/script/console_server_manager.js +29 -26
  44. package/dist/src/script/execute/base.d.ts +45 -0
  45. package/dist/src/script/execute/base.js +165 -162
  46. package/dist/src/script/execute/feedback.d.ts +12 -0
  47. package/dist/src/script/execute/feedback.js +162 -171
  48. package/dist/src/script/execute/interface.d.ts +8 -0
  49. package/dist/src/script/execute/region_job.d.ts +5 -0
  50. package/dist/src/script/execute/region_job.js +1 -0
  51. package/dist/src/script/execute/region_project.d.ts +10 -0
  52. package/dist/src/script/execute/region_project.js +4 -0
  53. package/dist/src/script/execute/region_subtask.d.ts +5 -0
  54. package/dist/src/script/execute/region_subtask.js +1 -0
  55. package/dist/src/script/execute/region_task.d.ts +10 -0
  56. package/dist/src/script/execute/region_task.js +4 -0
  57. package/dist/src/script/execute/runner.d.ts +14 -0
  58. package/dist/src/script/execute/runner.js +129 -139
  59. package/dist/src/script/execute/util_parser.d.ts +14 -0
  60. package/dist/src/script/execute/util_parser.js +119 -121
  61. package/dist/src/script/execute_manager.d.ts +25 -0
  62. package/dist/src/script/execute_manager.js +266 -278
  63. package/dist/src/script/socket_manager.d.ts +31 -0
  64. package/dist/src/script/socket_manager.js +196 -193
  65. package/dist/src/script/webhook_manager.d.ts +3 -0
  66. package/dist/src/script/webhook_server_manager.d.ts +13 -0
  67. package/dist/src/script/webhook_server_manager.js +79 -86
  68. package/dist/src/server/detail.d.ts +72 -0
  69. package/dist/src/server/detail.js +401 -378
  70. package/dist/src/server/io.d.ts +46 -0
  71. package/dist/src/server/io.js +42 -51
  72. package/dist/src/server/plugin.d.ts +18 -0
  73. package/dist/src/server/plugin.js +45 -54
  74. package/dist/src/server/server.d.ts +27 -0
  75. package/dist/src/server/server.js +38 -36
  76. package/dist/src/server.d.ts +4 -0
  77. package/dist/src/util/console_handle.d.ts +20 -0
  78. package/dist/src/util/console_handle.js +214 -218
  79. package/dist/src/util/log_handle.d.ts +23 -0
  80. package/dist/src/util/log_handle.js +158 -164
  81. package/dist/test/TEST.d.ts +1 -0
  82. package/dist/test/TEST.js +37 -54
  83. package/dist/test/client/execute.test.d.ts +1 -0
  84. package/dist/test/client/execute.test.js +6 -15
  85. package/dist/test/client/javascript.test.d.ts +1 -0
  86. package/dist/test/client/javascript.test.js +21 -30
  87. package/dist/test/client/server.test.d.ts +1 -0
  88. package/dist/test/client/task.test.d.ts +1 -0
  89. package/dist/test/client/task.test.js +1 -1
  90. package/dist/test/script/parser.test.d.ts +1 -0
  91. package/dist/test/script/socket.test.d.ts +1 -0
  92. package/package.json +1 -1
  93. package/src/interface/enum.ts +1 -1
  94. package/tsconfig.json +5 -3
@@ -32,15 +32,6 @@ var __importStar = (this && this.__importStar) || (function () {
32
32
  return result;
33
33
  };
34
34
  })();
35
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
36
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
37
- return new (P || (P = Promise))(function (resolve, reject) {
38
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
39
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
40
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
41
- step((generator = generator.apply(thisArg, _arguments || [])).next());
42
- });
43
- };
44
35
  Object.defineProperty(exports, "__esModule", { value: true });
45
36
  exports.ClientAnalysis = void 0;
46
37
  const child_process_1 = require("child_process");
@@ -52,332 +43,340 @@ const fs_1 = require("fs");
52
43
  const path = __importStar(require("path"));
53
44
  const os = __importStar(require("os"));
54
45
  class ClientAnalysis {
46
+ messager;
47
+ messager_log;
48
+ client;
49
+ exec;
50
+ shell;
51
+ resource_wanter = [];
52
+ resource_thread = undefined;
53
+ resource_cache = undefined;
55
54
  constructor(_messager, _messager_log, _client) {
56
- this.resource_wanter = [];
57
- this.resource_thread = undefined;
58
- this.resource_cache = undefined;
59
- this.analysis = (h, source) => {
60
- const typeMap = {
61
- 'execute_job': this.execute_job,
62
- 'release': this.release,
63
- 'stop_job': this.stop_all,
64
- 'set_database': this.set_database,
65
- 'set_libs': this.set_libs,
66
- 'shell_folder': this.shell.shell_folder,
67
- 'open_shell': this.shell.open_shell,
68
- 'close_shell': this.shell.close_shell,
69
- 'enter_shell': this.shell.enter_shell,
70
- 'resource_start': this.resource_start,
71
- 'resource_end': this.resource_end,
72
- 'ping': this.pong,
73
- 'plugin_info': this.plugin_info,
74
- 'plugin_download': this.plugin_download,
75
- 'plugin_remove': this.plugin_remove,
76
- };
77
- if (h == undefined) {
78
- this.messager_log('[Client Analysis] Analysis Failed, Value is undefined');
79
- return 1;
80
- }
81
- if (h.message != undefined && h.message.length > 0) {
82
- this.messager_log(`[Client Analysis] ${h.message}`);
83
- }
84
- if (h.data == undefined) {
85
- this.messager_log('[Client Analysis] Analysis Warn, Data is undefined');
86
- h.data = 0;
87
- }
88
- if (typeMap.hasOwnProperty(h.name)) {
89
- const castingFunc = typeMap[h.name];
90
- castingFunc(h.data, source, h.channel);
91
- return 0;
92
- }
93
- else {
94
- this.messager_log(`[Client Analysis] Analysis Failed, Unknowed header, name: ${h.name}, meta: ${h.meta}`);
95
- return 2;
96
- }
97
- };
98
- this.execute_job = (job, source, channel) => {
99
- if (channel == undefined)
100
- return;
101
- const target = this.exec_checker(channel);
102
- target.execute_job(job, source);
103
- };
104
- this.release = (dummy, source, channel) => {
105
- if (channel == undefined)
106
- return;
107
- const index = this.exec.findIndex(x => x.uuid == channel);
108
- if (index == -1)
109
- return;
110
- this.exec.splice(index, 1);
111
- };
112
- this.set_database = (data, source, channel) => {
113
- if (channel == undefined)
114
- return;
115
- const target = this.exec_checker(channel);
116
- target.set_database(data);
117
- };
118
- this.set_libs = (data, source, channel) => {
119
- if (channel == undefined)
120
- return;
121
- const target = this.exec_checker(channel);
122
- target.set_libs(data);
123
- };
124
- this.exec_checker = (uuid) => {
125
- let r = undefined;
126
- const index = this.exec.findIndex(x => x.uuid == uuid);
127
- if (index == -1) {
128
- r = new execute_1.ClientExecute(uuid, this.messager, this.messager_log, this.client);
129
- this.exec.push(r);
130
- }
131
- else {
132
- r = this.exec[index];
133
- }
134
- return r;
55
+ this.client = _client;
56
+ this.messager = _messager;
57
+ this.messager_log = _messager_log;
58
+ this.shell = new shell_1.ClientShell(_messager, _messager_log, this.client);
59
+ this.exec = [];
60
+ }
61
+ analysis = (h, source) => {
62
+ const typeMap = {
63
+ 'execute_job': this.execute_job,
64
+ 'release': this.release,
65
+ 'stop_job': this.stop_all,
66
+ 'set_database': this.set_database,
67
+ 'set_libs': this.set_libs,
68
+ 'shell_folder': this.shell.shell_folder,
69
+ 'open_shell': this.shell.open_shell,
70
+ 'close_shell': this.shell.close_shell,
71
+ 'enter_shell': this.shell.enter_shell,
72
+ 'resource_start': this.resource_start,
73
+ 'resource_end': this.resource_end,
74
+ 'ping': this.pong,
75
+ 'plugin_info': this.plugin_info,
76
+ 'plugin_download': this.plugin_download,
77
+ 'plugin_remove': this.plugin_remove,
135
78
  };
136
- this.pong = (data, source) => {
137
- const h = { name: 'pong', data: data };
79
+ if (h == undefined) {
80
+ this.messager_log('[Client Analysis] Analysis Failed, Value is undefined');
81
+ return 1;
82
+ }
83
+ if (h.message != undefined && h.message.length > 0) {
84
+ this.messager_log(`[Client Analysis] ${h.message}`);
85
+ }
86
+ if (h.data == undefined) {
87
+ this.messager_log('[Client Analysis] Analysis Warn, Data is undefined');
88
+ h.data = 0;
89
+ }
90
+ if (typeMap.hasOwnProperty(h.name)) {
91
+ const castingFunc = typeMap[h.name];
92
+ castingFunc(h.data, source, h.channel);
93
+ return 0;
94
+ }
95
+ else {
96
+ this.messager_log(`[Client Analysis] Analysis Failed, Unknowed header, name: ${h.name}, meta: ${h.meta}`);
97
+ return 2;
98
+ }
99
+ };
100
+ execute_job = (job, source, channel) => {
101
+ if (channel == undefined)
102
+ return;
103
+ const target = this.exec_checker(channel);
104
+ target.execute_job(job, source);
105
+ };
106
+ release = (dummy, source, channel) => {
107
+ if (channel == undefined)
108
+ return;
109
+ const index = this.exec.findIndex(x => x.uuid == channel);
110
+ if (index == -1)
111
+ return;
112
+ this.exec.splice(index, 1);
113
+ };
114
+ set_database = (data, source, channel) => {
115
+ if (channel == undefined)
116
+ return;
117
+ const target = this.exec_checker(channel);
118
+ target.set_database(data);
119
+ };
120
+ set_libs = (data, source, channel) => {
121
+ if (channel == undefined)
122
+ return;
123
+ const target = this.exec_checker(channel);
124
+ target.set_libs(data);
125
+ };
126
+ exec_checker = (uuid) => {
127
+ let r = undefined;
128
+ const index = this.exec.findIndex(x => x.uuid == uuid);
129
+ if (index == -1) {
130
+ r = new execute_1.ClientExecute(uuid, this.messager, this.messager_log, this.client);
131
+ this.exec.push(r);
132
+ }
133
+ else {
134
+ r = this.exec[index];
135
+ }
136
+ return r;
137
+ };
138
+ pong = (data, source) => {
139
+ const h = { name: 'pong', data: data };
140
+ source.send(JSON.stringify(h));
141
+ };
142
+ plugin_info = (data, source) => {
143
+ const pat = path.join(os.homedir(), interface_1.DATA_FOLDER, "plugin.json");
144
+ if ((0, fs_1.existsSync)(pat)) {
145
+ const p = JSON.parse((0, fs_1.readFileSync)(pat).toString());
146
+ const h = { name: 'plugin_info_reply', data: p.plugins };
138
147
  source.send(JSON.stringify(h));
139
- };
140
- this.plugin_info = (data, source) => {
141
- const pat = path.join(os.homedir(), interface_1.DATA_FOLDER, "plugin.json");
142
- if ((0, fs_1.existsSync)(pat)) {
143
- const p = JSON.parse((0, fs_1.readFileSync)(pat).toString());
144
- const h = { name: 'plugin_info_reply', data: p.plugins };
145
- source.send(JSON.stringify(h));
146
- }
147
- else {
148
- const p = { plugins: [] };
149
- const h = { name: 'plugin_info_reply', data: p.plugins };
150
- (0, fs_1.writeFileSync)(pat, JSON.stringify(p));
151
- source.send(JSON.stringify(h));
148
+ }
149
+ else {
150
+ const p = { plugins: [] };
151
+ const h = { name: 'plugin_info_reply', data: p.plugins };
152
+ (0, fs_1.writeFileSync)(pat, JSON.stringify(p));
153
+ source.send(JSON.stringify(h));
154
+ }
155
+ };
156
+ get_releases = async (repo, token) => {
157
+ const qu = await fetch(`https://api.github.com/repos/${repo}/releases`, {
158
+ headers: {
159
+ Authorization: token ? `token ${token}` : '',
160
+ Accept: "application/vnd.github.v3.raw",
152
161
  }
153
- };
154
- this.get_releases = (repo, token) => __awaiter(this, void 0, void 0, function* () {
155
- const qu = yield fetch(`https://api.github.com/repos/${repo}/releases`, {
156
- headers: {
157
- Authorization: token ? `token ${token}` : '',
158
- Accept: "application/vnd.github.v3.raw",
159
- }
160
- });
161
- return qu.text();
162
- });
163
- this.filterout = (repo, token, version, filename) => __awaiter(this, void 0, void 0, function* () {
164
- const text = yield this.get_releases(repo, token);
165
- const json = JSON.parse(text);
166
- const v = json.find(x => x.tag_name == version);
167
- if (!v)
168
- return;
169
- const f = v.assets.find(x => x.name == filename);
170
- if (!f)
171
- return;
172
- return f.id;
173
162
  });
174
- this.write_plugin = (t, plugin, source) => {
175
- const list = this.client.plugins.plugins;
176
- const index = list.findIndex(x => x.name == plugin.name);
177
- plugin.token = t ? [t] : [];
178
- plugin.progress = 0;
179
- if (index == -1) {
180
- list.push(plugin);
181
- }
182
- else {
183
- list[index] = plugin;
163
+ return qu.text();
164
+ };
165
+ filterout = async (repo, token, version, filename) => {
166
+ const text = await this.get_releases(repo, token);
167
+ const json = JSON.parse(text);
168
+ const v = json.find(x => x.tag_name == version);
169
+ if (!v)
170
+ return;
171
+ const f = v.assets.find(x => x.name == filename);
172
+ if (!f)
173
+ return;
174
+ return f.id;
175
+ };
176
+ write_plugin = (t, plugin, source) => {
177
+ const list = this.client.plugins.plugins;
178
+ const index = list.findIndex(x => x.name == plugin.name);
179
+ plugin.token = t ? [t] : [];
180
+ plugin.progress = 0;
181
+ if (index == -1) {
182
+ list.push(plugin);
183
+ }
184
+ else {
185
+ list[index] = plugin;
186
+ }
187
+ this.client.savePlugin();
188
+ this.plugin_info(0, source);
189
+ };
190
+ finish_plugin = (plugin, source) => {
191
+ const list = this.client.plugins.plugins;
192
+ const index = list.findIndex(x => x.name == plugin.name);
193
+ plugin.progress = 1;
194
+ if (index == -1) {
195
+ list.push(plugin);
196
+ }
197
+ else {
198
+ list[index] = plugin;
199
+ }
200
+ this.client.savePlugin();
201
+ this.plugin_info(0, source);
202
+ };
203
+ plugin_download = async (plugin, source) => {
204
+ const target = plugin.contents.find(x => x.arch == process.arch && x.platform == process.platform);
205
+ if (target == undefined) {
206
+ this.messager_log(`[Plugin] Cannot find target plugin for ${plugin.name} on ${process.platform} ${process.arch}`);
207
+ return;
208
+ }
209
+ const links = target.url.split('/');
210
+ const filename = links[links.length - 1];
211
+ const version = links[links.length - 2];
212
+ const REPO = `${links[3]}/${links[4]}`;
213
+ const dir = path.join(os.homedir(), interface_1.DATA_FOLDER, "exe");
214
+ if (!(0, fs_1.existsSync)(dir))
215
+ (0, fs_1.mkdirSync)(dir, { recursive: true });
216
+ let req = {};
217
+ const tokens = [undefined, ...plugin.token];
218
+ const fileStream = (0, fs_1.createWriteStream)(path.join(dir, target.filename), { flags: 'a' });
219
+ let pass = false;
220
+ for (let t of tokens) {
221
+ if (pass)
222
+ break;
223
+ try {
224
+ const id = await this.filterout(REPO, t, version, filename);
225
+ req = {
226
+ method: 'GET',
227
+ credentials: 'include',
228
+ headers: {
229
+ Authorization: t ? `token ${t}` : '',
230
+ Accept: "application/octet-stream"
231
+ }
232
+ };
233
+ const url = `https://api.github.com/repos/${REPO}/releases/assets/${id}`;
234
+ fetch(url, req).then(async (res) => {
235
+ if (!res.ok) {
236
+ throw new Error(`Failed to download file: ${res.status} ${res.statusText}`);
237
+ }
238
+ this.write_plugin(t, plugin, source);
239
+ return res.blob();
240
+ }).then(blob => {
241
+ return blob.stream().getReader().read();
242
+ })
243
+ .then(reader => {
244
+ if (!reader.done) {
245
+ fileStream.write(Buffer.from(reader.value));
246
+ }
247
+ }).finally(() => {
248
+ this.messager_log(`[Plugin] Downloaded ${plugin.name} successfully`);
249
+ fileStream.end();
250
+ if (process.platform == 'linux') {
251
+ (0, child_process_1.exec)(`chmod +x ${path.join(dir, target.filename)}`, (err) => {
252
+ this.messager_log(`[Plugin] Permission failed ${err?.message}`);
253
+ });
254
+ }
255
+ this.finish_plugin(plugin, source);
256
+ pass = true;
257
+ });
184
258
  }
185
- this.client.savePlugin();
186
- this.plugin_info(0, source);
187
- };
188
- this.finish_plugin = (plugin, source) => {
189
- const list = this.client.plugins.plugins;
190
- const index = list.findIndex(x => x.name == plugin.name);
191
- plugin.progress = 1;
192
- if (index == -1) {
193
- list.push(plugin);
259
+ catch (err) {
260
+ this.messager_log(`[Plugin] Download failed for ${plugin.name}: ${err.message}`);
194
261
  }
195
- else {
196
- list[index] = plugin;
262
+ }
263
+ };
264
+ plugin_remove = (plugin, source) => {
265
+ this.client.plugins.plugins = this.client.plugins.plugins.filter(x => x.name != plugin.name);
266
+ this.client.savePlugin();
267
+ const dir = path.join(os.homedir(), interface_1.DATA_FOLDER, "exe");
268
+ if (!(0, fs_1.existsSync)(dir))
269
+ (0, fs_1.mkdirSync)(dir, { recursive: true });
270
+ plugin.contents.forEach(x => {
271
+ if ((0, fs_1.existsSync)(path.join(dir, x.filename))) {
272
+ (0, fs_1.rmSync)(path.join(dir, x.filename));
197
273
  }
198
- this.client.savePlugin();
199
- this.plugin_info(0, source);
200
- };
201
- this.plugin_download = (plugin, source) => __awaiter(this, void 0, void 0, function* () {
202
- const target = plugin.contents.find(x => x.arch == process.arch && x.platform == process.platform);
203
- if (target == undefined) {
204
- this.messager_log(`[Plugin] Cannot find target plugin for ${plugin.name} on ${process.platform} ${process.arch}`);
205
- return;
274
+ });
275
+ this.plugin_info(0, source);
276
+ };
277
+ resource_start = (data, source) => {
278
+ this.resource_wanter.push(source);
279
+ this.messager_log(`Register resource_wanter!, count: ${this.resource_wanter.length}`);
280
+ if (this.resource_cache != undefined)
281
+ source.send(JSON.stringify(this.resource_cache));
282
+ };
283
+ resource_end = (data, source) => {
284
+ const index = this.resource_wanter.findIndex(x => x == source);
285
+ if (index != -1) {
286
+ this.resource_wanter.splice(index, 1);
287
+ this.messager_log(`UnRegister resource_wanter!, count: ${this.resource_wanter.length}`);
288
+ }
289
+ };
290
+ update = (client) => {
291
+ this.resource_require();
292
+ if (this.resource_cache != undefined) {
293
+ this.resource_wanter.forEach(x => x.send(JSON.stringify(this.resource_cache)));
294
+ }
295
+ };
296
+ disconnect = (source) => {
297
+ this.shell.disconnect(source);
298
+ this.exec.forEach(x => x.stop_job());
299
+ };
300
+ stop_all = () => {
301
+ this.exec.forEach(x => x.stop_job());
302
+ };
303
+ destroy = () => {
304
+ if (this.resource_thread != undefined)
305
+ this.resource_thread.kill();
306
+ };
307
+ resource_require = () => {
308
+ if (this.resource_thread != undefined)
309
+ return;
310
+ const shouldRun = this.resource_thread == undefined && (this.resource_cache == undefined || this.resource_wanter.length > 0);
311
+ if (!shouldRun)
312
+ return;
313
+ this.resource_thread = (0, child_process_1.spawn)(client_1.Client.workerPath(), [], {
314
+ stdio: ['inherit', 'pipe', 'pipe'],
315
+ shell: true,
316
+ windowsHide: true,
317
+ env: {
318
+ ...process.env,
319
+ type: "RESOURCE",
320
+ cache: this.resource_cache == undefined ? undefined : JSON.stringify(this.resource_cache.data)
206
321
  }
207
- const links = target.url.split('/');
208
- const filename = links[links.length - 1];
209
- const version = links[links.length - 2];
210
- const REPO = `${links[3]}/${links[4]}`;
211
- const dir = path.join(os.homedir(), interface_1.DATA_FOLDER, "exe");
212
- if (!(0, fs_1.existsSync)(dir))
213
- (0, fs_1.mkdirSync)(dir, { recursive: true });
214
- let req = {};
215
- const tokens = [undefined, ...plugin.token];
216
- const fileStream = (0, fs_1.createWriteStream)(path.join(dir, target.filename), { flags: 'a' });
217
- let pass = false;
218
- for (let t of tokens) {
219
- if (pass)
220
- break;
221
- try {
222
- const id = yield this.filterout(REPO, t, version, filename);
223
- req = {
224
- method: 'GET',
225
- credentials: 'include',
226
- headers: {
227
- Authorization: t ? `token ${t}` : '',
228
- Accept: "application/octet-stream"
229
- }
230
- };
231
- const url = `https://api.github.com/repos/${REPO}/releases/assets/${id}`;
232
- fetch(url, req).then((res) => __awaiter(this, void 0, void 0, function* () {
233
- if (!res.ok) {
234
- throw new Error(`Failed to download file: ${res.status} ${res.statusText}`);
235
- }
236
- this.write_plugin(t, plugin, source);
237
- return res.blob();
238
- })).then(blob => {
239
- return blob.stream().getReader().read();
240
- })
241
- .then(reader => {
242
- if (!reader.done) {
243
- fileStream.write(Buffer.from(reader.value));
244
- }
245
- }).finally(() => {
246
- this.messager_log(`[Plugin] Downloaded ${plugin.name} successfully`);
247
- fileStream.end();
248
- if (process.platform == 'linux') {
249
- (0, child_process_1.exec)(`chmod +x ${path.join(dir, target.filename)}`, (err) => {
250
- this.messager_log(`[Plugin] Permission failed ${err === null || err === void 0 ? void 0 : err.message}`);
251
- });
252
- }
253
- this.finish_plugin(plugin, source);
254
- pass = true;
255
- });
322
+ });
323
+ let k = "";
324
+ const workerFeedbackExec = (str) => {
325
+ try {
326
+ const msg = JSON.parse(str);
327
+ if (msg.name == 'messager') {
328
+ this.messager(msg.data, "RESOURCE");
256
329
  }
257
- catch (err) {
258
- this.messager_log(`[Plugin] Download failed for ${plugin.name}: ${err.message}`);
330
+ else if (msg.name == 'messager_log') {
331
+ this.messager_log(msg.data, "RESOURCE");
259
332
  }
260
- }
261
- });
262
- this.plugin_remove = (plugin, source) => {
263
- this.client.plugins.plugins = this.client.plugins.plugins.filter(x => x.name != plugin.name);
264
- this.client.savePlugin();
265
- const dir = path.join(os.homedir(), interface_1.DATA_FOLDER, "exe");
266
- if (!(0, fs_1.existsSync)(dir))
267
- (0, fs_1.mkdirSync)(dir, { recursive: true });
268
- plugin.contents.forEach(x => {
269
- if ((0, fs_1.existsSync)(path.join(dir, x.filename))) {
270
- (0, fs_1.rmSync)(path.join(dir, x.filename));
333
+ else if (msg.name == 'resource') {
334
+ const h = {
335
+ name: 'system_info',
336
+ data: msg.data
337
+ };
338
+ this.resource_cache = h;
339
+ this.resource_wanter.forEach(x => x.send(JSON.stringify(h)));
340
+ }
341
+ else if (msg.name == 'error') {
342
+ if (msg.data instanceof String)
343
+ this.messager_log(msg.data.toString(), "RESOURCE");
344
+ else
345
+ this.messager_log(JSON.stringify(msg.data), "RESOURCE");
271
346
  }
272
- });
273
- this.plugin_info(0, source);
274
- };
275
- this.resource_start = (data, source) => {
276
- this.resource_wanter.push(source);
277
- this.messager_log(`Register resource_wanter!, count: ${this.resource_wanter.length}`);
278
- if (this.resource_cache != undefined)
279
- source.send(JSON.stringify(this.resource_cache));
280
- };
281
- this.resource_end = (data, source) => {
282
- const index = this.resource_wanter.findIndex(x => x == source);
283
- if (index != -1) {
284
- this.resource_wanter.splice(index, 1);
285
- this.messager_log(`UnRegister resource_wanter!, count: ${this.resource_wanter.length}`);
286
347
  }
287
- };
288
- this.update = (client) => {
289
- this.resource_require();
290
- if (this.resource_cache != undefined) {
291
- this.resource_wanter.forEach(x => x.send(JSON.stringify(this.resource_cache)));
348
+ catch (err) {
349
+ console.log("str: " + str);
350
+ console.log(err.name + "\n" + err.message);
292
351
  }
293
352
  };
294
- this.disconnect = (source) => {
295
- this.shell.disconnect(source);
296
- this.exec.forEach(x => x.stop_job());
297
- };
298
- this.stop_all = () => {
299
- this.exec.forEach(x => x.stop_job());
300
- };
301
- this.destroy = () => {
302
- if (this.resource_thread != undefined)
303
- this.resource_thread.kill();
304
- };
305
- this.resource_require = () => {
306
- var _a, _b, _c, _d;
307
- if (this.resource_thread != undefined)
308
- return;
309
- const shouldRun = this.resource_thread == undefined && (this.resource_cache == undefined || this.resource_wanter.length > 0);
310
- if (!shouldRun)
311
- return;
312
- this.resource_thread = (0, child_process_1.spawn)(client_1.Client.workerPath(), [], {
313
- stdio: ['inherit', 'pipe', 'pipe'],
314
- shell: true,
315
- windowsHide: true,
316
- env: Object.assign(Object.assign({}, process.env), { type: "RESOURCE", cache: this.resource_cache == undefined ? undefined : JSON.stringify(this.resource_cache.data) })
317
- });
318
- let k = "";
319
- const workerFeedbackExec = (str) => {
320
- try {
321
- const msg = JSON.parse(str);
322
- if (msg.name == 'messager') {
323
- this.messager(msg.data, "RESOURCE");
324
- }
325
- else if (msg.name == 'messager_log') {
326
- this.messager_log(msg.data, "RESOURCE");
327
- }
328
- else if (msg.name == 'resource') {
329
- const h = {
330
- name: 'system_info',
331
- data: msg.data
332
- };
333
- this.resource_cache = h;
334
- this.resource_wanter.forEach(x => x.send(JSON.stringify(h)));
335
- }
336
- else if (msg.name == 'error') {
337
- if (msg.data instanceof String)
338
- this.messager_log(msg.data.toString(), "RESOURCE");
339
- else
340
- this.messager_log(JSON.stringify(msg.data), "RESOURCE");
341
- }
342
- }
343
- catch (err) {
344
- console.log("str: " + str);
345
- console.log(err.name + "\n" + err.message);
353
+ const workerFeedback = (str) => {
354
+ for (let i = 0; i < str.length; i++) {
355
+ if (str[i] != '\n')
356
+ k += str[i];
357
+ else {
358
+ workerFeedbackExec(k);
359
+ k = '';
346
360
  }
347
- };
348
- const workerFeedback = (str) => {
349
- for (let i = 0; i < str.length; i++) {
350
- if (str[i] != '\n')
351
- k += str[i];
352
- else {
353
- workerFeedbackExec(k);
354
- k = '';
355
- }
356
- }
357
- };
358
- this.resource_thread.on('error', (err) => {
359
- this.messager_log(`[Worker Error] ${err}`);
360
- });
361
- this.resource_thread.on('exit', (code, signal) => {
362
- this.resource_thread = undefined;
363
- });
364
- this.resource_thread.on('message', (message, sendHandle) => {
365
- workerFeedback(message.toString());
366
- });
367
- (_a = this.resource_thread.stdout) === null || _a === void 0 ? void 0 : _a.setEncoding('utf8');
368
- (_b = this.resource_thread.stdout) === null || _b === void 0 ? void 0 : _b.on('data', (chunk) => {
369
- workerFeedback(chunk.toString());
370
- });
371
- (_c = this.resource_thread.stderr) === null || _c === void 0 ? void 0 : _c.setEncoding('utf8');
372
- (_d = this.resource_thread.stderr) === null || _d === void 0 ? void 0 : _d.on('data', (chunk) => {
373
- workerFeedback(chunk.toString());
374
- });
361
+ }
375
362
  };
376
- this.client = _client;
377
- this.messager = _messager;
378
- this.messager_log = _messager_log;
379
- this.shell = new shell_1.ClientShell(_messager, _messager_log, this.client);
380
- this.exec = [];
381
- }
363
+ this.resource_thread.on('error', (err) => {
364
+ this.messager_log(`[Worker Error] ${err}`);
365
+ });
366
+ this.resource_thread.on('exit', (code, signal) => {
367
+ this.resource_thread = undefined;
368
+ });
369
+ this.resource_thread.on('message', (message, sendHandle) => {
370
+ workerFeedback(message.toString());
371
+ });
372
+ this.resource_thread.stdout?.setEncoding('utf8');
373
+ this.resource_thread.stdout?.on('data', (chunk) => {
374
+ workerFeedback(chunk.toString());
375
+ });
376
+ this.resource_thread.stderr?.setEncoding('utf8');
377
+ this.resource_thread.stderr?.on('data', (chunk) => {
378
+ workerFeedback(chunk.toString());
379
+ });
380
+ };
382
381
  }
383
382
  exports.ClientAnalysis = ClientAnalysis;