@aj-shadow/z-abs-corelayer-server 0.0.0-aj-beta.221
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.
- package/.gitattributes +26 -0
- package/LICENSE.txt +96 -0
- package/README.md +5 -0
- package/npm-shrinkwrap.json +13 -0
- package/package.json +10 -0
- package/project/server/_build/Server-CoreLayer-server.bld +12 -0
- package/project/server/_build/z-abs-corelayer-server.prj +12 -0
- package/project/server/communication/core-protocol/decoder.js +404 -0
- package/project/server/communication/core-protocol/encoder.js +503 -0
- package/project/server/communication/messages/messages-s-to-c/message-persistent-init-response.js +14 -0
- package/project/server/communication/messages/messages-s-to-c/message-persistent-publish.js +16 -0
- package/project/server/communication/messages/messages-s-to-c/message-ws-init.js +14 -0
- package/project/server/communication/messages/messages-s-to-s/service-init-request.js +17 -0
- package/project/server/communication/messages/messages-s-to-s/service-init-response.js +18 -0
- package/project/server/communication/messages/messages-s-to-s/service-offer-off.js +15 -0
- package/project/server/communication/messages/messages-s-to-s/service-offer-on.js +15 -0
- package/project/server/communication/messages/messages-s-to-s/worker-init-request.js +15 -0
- package/project/server/data-response.js +588 -0
- package/project/server/high-resolution-timestamp.js +36 -0
- package/project/server/log/log-config.js +107 -0
- package/project/server/log/log-types.js +14 -0
- package/project/server/log/logger-console.js +36 -0
- package/project/server/log/logger.js +114 -0
- package/project/server/node/channel-input.js +65 -0
- package/project/server/node/channel-output.js +39 -0
- package/project/server/node/clients.js +80 -0
- package/project/server/node/connection-address.js +24 -0
- package/project/server/node/connection-data-ip.js +82 -0
- package/project/server/node/connection-data.js +25 -0
- package/project/server/node/const.js +44 -0
- package/project/server/node/external-services.js +209 -0
- package/project/server/node/http-cache.js +109 -0
- package/project/server/node/http-proxy.js +97 -0
- package/project/server/node/http-request-handler.js +219 -0
- package/project/server/node/http-server.js +36 -0
- package/project/server/node/http2-server.js +67 -0
- package/project/server/node/https-server.js +63 -0
- package/project/server/node/ip-client.js +98 -0
- package/project/server/node/ip-server.js +63 -0
- package/project/server/node/ip-subscription.js +46 -0
- package/project/server/node/message-channel.js +98 -0
- package/project/server/node/node-admin.js +513 -0
- package/project/server/node/node-data.js +25 -0
- package/project/server/node/node-settings.js +352 -0
- package/project/server/node/node-worker.js +76 -0
- package/project/server/node/node.js +156 -0
- package/project/server/node/servers.js +168 -0
- package/project/server/node/ws-client.js +12 -0
- package/project/server/node/ws-server.js +20 -0
- package/project/server/node/ws-web-server.js +52 -0
- package/project/server/node/wss-server.js +20 -0
- package/project/server/node/wss-web-server.js +82 -0
- package/project/server/path/actor-path-build.js +29 -0
- package/project/server/path/actor-path-content.js +51 -0
- package/project/server/path/actor-path-creator.js +90 -0
- package/project/server/path/actor-path-data.js +487 -0
- package/project/server/path/actor-path-dist.js +132 -0
- package/project/server/path/actor-path-generated.js +202 -0
- package/project/server/path/actor-path-project.js +181 -0
- package/project/server/path/actor-path.js +57 -0
- package/project/server/path/paths/actor-content-paths.js +34 -0
- package/project/server/path/paths/actor-data-paths.js +302 -0
- package/project/server/path/paths/actor-generated-paths.js +99 -0
- package/project/server/path/paths/actor-paths.js +22 -0
- package/project/server/plugin-base-multi.js +484 -0
- package/project/server/plugin-base.js +233 -0
- package/project/server/plugin-component.js +92 -0
- package/project/server/plugin-data/client/plugin_data_ClientGet.js +18 -0
- package/project/server/plugin-data/dialog-file/plugin_data_DialogFileGet.js +197 -0
- package/project/server/plugin-data/platform/plugin_data_PlatformPing.js +19 -0
- package/project/server/plugin-factor-protocol.js +35 -0
- package/project/server/plugin-factory.js +127 -0
- package/project/server/plugin-lock.js +164 -0
- package/project/server/response-queue.js +46 -0
- package/project/server/responses.js +20 -0
- package/project/server/service/plugin-service.js +264 -0
- package/project/server/service/service-export.js +47 -0
- package/project/server/service/service-manager.js +270 -0
- package/project/server/service/service-requests.js +49 -0
- package/project/server/session-cache.js +39 -0
- package/project/server/session.js +23 -0
- package/project/server/worker/worker-channel.js +175 -0
- package/project/server/worker/worker-core.js +58 -0
- package/project/server/worker/worker-main.js +165 -0
- package/project/server/worker/worker-pool.js +94 -0
- package/project/server/worker/worker-thread.js +128 -0
- package/project/z-abs-corelayer-server.tree +99 -0
|
@@ -0,0 +1,484 @@
|
|
|
1
|
+
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
const PluginBase = require('./plugin-base');
|
|
5
|
+
const ResponseQueue = require('./response-queue');
|
|
6
|
+
const Logger = require('./log/logger');
|
|
7
|
+
const Os = require('os');
|
|
8
|
+
const Sudo = 'win32' === Os.platform() ? require('sudo-prompt') : require('sudo-js');
|
|
9
|
+
const Fs = require('fs');
|
|
10
|
+
const Path = require('path');
|
|
11
|
+
const ChildProcess = require('child_process');
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class PluginBaseMulti extends PluginBase {
|
|
15
|
+
constructor(restType, ...authorization) {
|
|
16
|
+
super(restType, ...authorization);
|
|
17
|
+
this.responseQueue = null;
|
|
18
|
+
this.index = 0;
|
|
19
|
+
this.responseCbs = [];
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
asynchReadBinFile(file, done) {
|
|
23
|
+
this.expectAsynchResponseTemp();
|
|
24
|
+
this.readLock(file, (unlock) => {
|
|
25
|
+
Fs.readFile(file, (err, data) => {
|
|
26
|
+
unlock();
|
|
27
|
+
this._safeCallback(done, err, data);
|
|
28
|
+
this.unExpectAsynchResponseTemp();
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
asynchReadTextFile(file, done) {
|
|
34
|
+
this.expectAsynchResponseTemp();
|
|
35
|
+
this.readLock(file, (unlock) => {
|
|
36
|
+
Fs.readFile(file, 'utf8', (err, data) => {
|
|
37
|
+
unlock();
|
|
38
|
+
this._safeCallback(done, err, data);
|
|
39
|
+
this.unExpectAsynchResponseTemp();
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
asynchReadTextFileResponse(file, done) {
|
|
45
|
+
const index = this.expectAsynchResponse();
|
|
46
|
+
this.asynchReadTextFile(file, (err, text) => {
|
|
47
|
+
this._safeCallback(done, err, text);
|
|
48
|
+
if(!err) {
|
|
49
|
+
this.asynchResponseSuccess(index, text);
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
this.asynchResponseError(`Could not read file: \'${file}\'. error: ${err}`, index);
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
asynchReadFilePromise(file) {
|
|
58
|
+
return new Promise((resolve, reject) => {
|
|
59
|
+
this.readLock(file, (unlock) => {
|
|
60
|
+
Fs.readFile(file, (err, data) => {
|
|
61
|
+
unlock();
|
|
62
|
+
this._safeJsonParseCallback((err, data) => {
|
|
63
|
+
if(!err) {
|
|
64
|
+
resolve(data);
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
reject(err);
|
|
68
|
+
}
|
|
69
|
+
}, err, file, data);
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
asynchReadFile(file, done) {
|
|
76
|
+
this.expectAsynchResponseTemp();
|
|
77
|
+
this.readLock(file, (unlock) => {
|
|
78
|
+
Fs.readFile(file, (err, data) => {
|
|
79
|
+
unlock();
|
|
80
|
+
if(undefined !== done) {
|
|
81
|
+
this._safeJsonParseCallback(done, err, file, data);
|
|
82
|
+
}
|
|
83
|
+
this.unExpectAsynchResponseTemp();
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
asynchReadFileResponse(file, done, outerIndex = -1) {
|
|
89
|
+
const index = -1 === outerIndex ? this.expectAsynchResponse() : outerIndex;
|
|
90
|
+
this.asynchReadFile(file, (err, object) => {
|
|
91
|
+
this._safeCallback(done, err, object);
|
|
92
|
+
if(!err) {
|
|
93
|
+
this.asynchResponseSuccess(index, object);
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
this.asynchResponseError(`Could not read file: \'${file}\'. error: ${err}`, index);
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
asynchWriteTextFile(file, data, done) {
|
|
102
|
+
this.expectAsynchResponseTemp();
|
|
103
|
+
this.writeLock(file, (unlock) => {
|
|
104
|
+
Fs.writeFile(file, data, (err) => {
|
|
105
|
+
unlock();
|
|
106
|
+
this._safeCallback(done, err, data);
|
|
107
|
+
this.unExpectAsynchResponseTemp();
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
asynchWriteTextFileResponse(file, data, returnValue, done) {
|
|
113
|
+
const index = this.expectAsynchResponse(); // Should only be used if there i s a return value.
|
|
114
|
+
this.asynchWriteTextFile(file, data, (err) => {
|
|
115
|
+
this._safeCallback(done, err, data);
|
|
116
|
+
if(!err) {
|
|
117
|
+
this.asynchResponseSuccess(index, returnValue, 'asynchWriteTextFileResponse');
|
|
118
|
+
}
|
|
119
|
+
else {
|
|
120
|
+
this.asynchResponseError(`Could not save the file: '${file}'.`, index);
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
asynchWriteFile(file, data, done) {
|
|
126
|
+
this.expectAsynchResponseTemp();
|
|
127
|
+
this.writeLock(file, (unlock) => {
|
|
128
|
+
Fs.writeFile(file, JSON.stringify(data, null, 2), (err) => {
|
|
129
|
+
unlock();
|
|
130
|
+
this._safeCallback(done, err, data);
|
|
131
|
+
this.unExpectAsynchResponseTemp();
|
|
132
|
+
});
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
asynchWriteFileResponse(file, data, returnValue, done) {
|
|
137
|
+
let index = 0;
|
|
138
|
+
if(returnValue) {
|
|
139
|
+
index = this.expectAsynchResponse(); // Should only be used if there is a return value.
|
|
140
|
+
}
|
|
141
|
+
this.asynchWriteFile(file, data, (err) => {
|
|
142
|
+
this._safeCallback(done, err, data);
|
|
143
|
+
if(!err) {
|
|
144
|
+
this.asynchResponseSuccess(index, returnValue ? data : undefined);
|
|
145
|
+
}
|
|
146
|
+
else {
|
|
147
|
+
this.asynchResponseError(`Could not save the file: '${file}'.`, index);
|
|
148
|
+
}
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
_safeCallback(done, err, data) {
|
|
153
|
+
try {
|
|
154
|
+
done && done(err, data);
|
|
155
|
+
}
|
|
156
|
+
catch(e) {
|
|
157
|
+
LOG_ERROR(Logger.ERROR.CATCH, 'Exception in safe callback: ', e, e.stack);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
_safeJsonParse(data, onError) {
|
|
162
|
+
try {
|
|
163
|
+
return JSON.parse(data);
|
|
164
|
+
}
|
|
165
|
+
catch(e) {
|
|
166
|
+
LOG_ERROR(Logger.ERROR.CATCH, 'Exception in safe JSON parse: ', e, e.stack);
|
|
167
|
+
onError(e);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
_safeJsonParseCallback(done, err, file, data) {
|
|
172
|
+
if(!err) {
|
|
173
|
+
try {
|
|
174
|
+
this._safeCallback(done, err, JSON.parse(data));
|
|
175
|
+
}
|
|
176
|
+
catch(e) {
|
|
177
|
+
const text = `Exception in safe JSON parse callback, file: '${file}'.`;
|
|
178
|
+
LOG_ERROR(Logger.ERROR.CATCH, text, e, e.stack);
|
|
179
|
+
this._safeCallback(done, e);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
else {
|
|
183
|
+
this._safeCallback(done, err);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
_asynchMkdir(file, done) {
|
|
188
|
+
this.expectAsynchResponseTemp();
|
|
189
|
+
Fs.mkdir(file, (err) => {
|
|
190
|
+
this._safeCallback(done, err);
|
|
191
|
+
this.unExpectAsynchResponseTemp();
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
asynchMkdir(file, done, recursive = false) {
|
|
196
|
+
this.expectAsynchResponseTemp();
|
|
197
|
+
this.writeLock(file, (unlock) => {
|
|
198
|
+
this._asynchMkdirImpl(file, (err) => {
|
|
199
|
+
unlock();
|
|
200
|
+
done(err);
|
|
201
|
+
this.unExpectAsynchResponseTemp();
|
|
202
|
+
}, recursive);
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// DONE
|
|
207
|
+
_asynchMkdirImpl(path, done, recursive) {
|
|
208
|
+
this.expectAsynchResponseTemp();
|
|
209
|
+
if(recursive) {
|
|
210
|
+
Fs.access(path, Fs.constants.F_OK, (err) => {
|
|
211
|
+
if(!err) {
|
|
212
|
+
done();
|
|
213
|
+
this.unExpectAsynchResponseTemp();
|
|
214
|
+
}
|
|
215
|
+
else if('ENOENT' === err.code) {
|
|
216
|
+
const parentPath = path.substring(0, path.lastIndexOf(Path.sep));
|
|
217
|
+
this._asynchMkdirImpl(parentPath, (err) => {
|
|
218
|
+
this._asynchMkdir(path, (err) => {
|
|
219
|
+
done(err);
|
|
220
|
+
this.unExpectAsynchResponseTemp();
|
|
221
|
+
});
|
|
222
|
+
}, true);
|
|
223
|
+
}
|
|
224
|
+
else {
|
|
225
|
+
done(err);
|
|
226
|
+
this.unExpectAsynchResponseTemp();
|
|
227
|
+
}
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
else {
|
|
231
|
+
this._asynchMkdir(path, (err) => {
|
|
232
|
+
done(err);
|
|
233
|
+
this.unExpectAsynchResponseTemp();
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
// DONE
|
|
239
|
+
asynchMkdirResponse(path, done, recursive = false) {
|
|
240
|
+
const index = this.expectAsynchResponse();
|
|
241
|
+
this.asynchMkdir(path, (err) => {
|
|
242
|
+
if(undefined !== done) {
|
|
243
|
+
done(err);
|
|
244
|
+
}
|
|
245
|
+
if(!err) {
|
|
246
|
+
this.asynchResponseSuccess(index);
|
|
247
|
+
}
|
|
248
|
+
else {
|
|
249
|
+
this.asynchResponseError(`Could not create directory: '${path}'.`, index);
|
|
250
|
+
}
|
|
251
|
+
}, recursive);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
asynchRmdirResponse(path, recursive, force) {
|
|
255
|
+
const index = this.expectAsynchResponse();
|
|
256
|
+
this.writeLock(path, (unlock) => {
|
|
257
|
+
Fs.rm(path, {recursive: !!recursive, force: !!force}, (err) => {
|
|
258
|
+
unlock();
|
|
259
|
+
if(!err) {
|
|
260
|
+
this.asynchResponseSuccess(index);
|
|
261
|
+
}
|
|
262
|
+
else {
|
|
263
|
+
this.asynchResponseError(`Could not remove directory: '${path}'.`, index);
|
|
264
|
+
}
|
|
265
|
+
});
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
asynchRmFileResponse(file) {
|
|
270
|
+
const index = this.expectAsynchResponse();
|
|
271
|
+
this.writeLock(file, (unlock) => {
|
|
272
|
+
Fs.unlink(file, (err) => {
|
|
273
|
+
unlock();
|
|
274
|
+
if(!err) {
|
|
275
|
+
this.asynchResponseSuccess(index);
|
|
276
|
+
}
|
|
277
|
+
else {
|
|
278
|
+
this.asynchResponseError(`Could not remove file: '${file}'.`, index);
|
|
279
|
+
}
|
|
280
|
+
});
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
asynchRename(oldPath, newPath, cb) {
|
|
285
|
+
this.expectAsynchResponseTemp();
|
|
286
|
+
this.writeLock(oldPath, (unlock) => {
|
|
287
|
+
Fs.rename(oldPath, newPath, (err) => {
|
|
288
|
+
unlock();
|
|
289
|
+
if(cb) {
|
|
290
|
+
this._safeCallback(cb, err);
|
|
291
|
+
}
|
|
292
|
+
this.unExpectAsynchResponseTemp();
|
|
293
|
+
});
|
|
294
|
+
});
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
asynchRenameResponse(oldPath, newPath, cb) {
|
|
298
|
+
const index = this.expectAsynchResponse();
|
|
299
|
+
this.asynchRename(oldPath, newPath, (err) => {
|
|
300
|
+
if(cb) {
|
|
301
|
+
this._safeCallback(cb, err);
|
|
302
|
+
}
|
|
303
|
+
if(!err) {
|
|
304
|
+
this.asynchResponseSuccess(index);
|
|
305
|
+
}
|
|
306
|
+
else {
|
|
307
|
+
this.asynchResponseError(`Could not rename oldPath: '${oldPath}' to newPath '${newPath}'.`, index);
|
|
308
|
+
}
|
|
309
|
+
});
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
asynchExists(file, done) {
|
|
313
|
+
this.expectAsynchResponseTemp();
|
|
314
|
+
this.readLock(file, (unlock) => {
|
|
315
|
+
Fs.access(file, Fs.constants.F_OK, (err) => {
|
|
316
|
+
unlock();
|
|
317
|
+
let exists = true;
|
|
318
|
+
if(err) {
|
|
319
|
+
if('ENOENT' === err.code) {
|
|
320
|
+
exists = false;
|
|
321
|
+
err = null;
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
this._safeCallback(done, err, exists);
|
|
325
|
+
this.unExpectAsynchResponseTemp();
|
|
326
|
+
});
|
|
327
|
+
});
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
asynchExistsResponse(file, cb) {
|
|
331
|
+
const index = this.expectAsynchResponse();
|
|
332
|
+
this.asynchExists(file, (err, exists) => {
|
|
333
|
+
if(cb) {
|
|
334
|
+
this._safeCallback(cb, err, exists);
|
|
335
|
+
}
|
|
336
|
+
if(!err) {
|
|
337
|
+
this.asynchResponseSuccess(index, exists);
|
|
338
|
+
}
|
|
339
|
+
else {
|
|
340
|
+
this.asynchResponseError(`Could not see if file exists. Error: ${err.code}`, index);
|
|
341
|
+
}
|
|
342
|
+
});
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
asynchChildProcess(command, done) {
|
|
346
|
+
this.expectAsynchResponseTemp();
|
|
347
|
+
ChildProcess.exec(command, (err, stdout) => {
|
|
348
|
+
this._safeCallback(done, err, stdout);
|
|
349
|
+
this.unExpectAsynchResponseTemp();
|
|
350
|
+
});
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
asynchChildProcessResponse(command) {
|
|
354
|
+
const index = this.expectAsynchResponse();
|
|
355
|
+
this.asynchChildProcess(command, (err, stdout, stderr) => {
|
|
356
|
+
if(!err) {
|
|
357
|
+
this.asynchResponseSuccess(index, stdout);
|
|
358
|
+
}
|
|
359
|
+
else {
|
|
360
|
+
this.asynchResponseError(stdout, index);
|
|
361
|
+
}
|
|
362
|
+
});
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
asynchSudoProcess(command, password, done) {
|
|
366
|
+
this.expectAsynchResponseTemp();
|
|
367
|
+
if('win32' === Os.platform()) {
|
|
368
|
+
Sudo.exec(command, {
|
|
369
|
+
name: 'ActorJs'
|
|
370
|
+
}, (err, stdout, stderr) => {
|
|
371
|
+
this._safeCallback(done, err, {
|
|
372
|
+
stdout: stdout,
|
|
373
|
+
stderr: stderr
|
|
374
|
+
});
|
|
375
|
+
this.unExpectAsynchResponseTemp();
|
|
376
|
+
});
|
|
377
|
+
}
|
|
378
|
+
else {
|
|
379
|
+
Sudo.setPassword(password);
|
|
380
|
+
const options = {check: false, withResult: true};
|
|
381
|
+
Sudo.exec(command.split(' '), options, (err, pid, result) => {
|
|
382
|
+
this._safeCallback(done, err, {
|
|
383
|
+
stdout: result.stdout,
|
|
384
|
+
stderr: result.stderr
|
|
385
|
+
});
|
|
386
|
+
this.unExpectAsynchResponseTemp();
|
|
387
|
+
});
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
expectAsynchResponseSuccess(value) {
|
|
392
|
+
const index = this.expectAsynchResponse();
|
|
393
|
+
process.nextTick(this.asynchResponseSuccess.bind(this), index, value);
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
expectAsynchResponseError(message) {
|
|
397
|
+
const index = this.expectAsynchResponse();
|
|
398
|
+
process.nextTick(this.asynchResponseError.bind(this), message, index);
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
expectAsynchResponseTemp() {
|
|
402
|
+
this.responseQueue.add();
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
expectAsynchResponse() {
|
|
406
|
+
this.responseQueue.add();
|
|
407
|
+
return this.index++;
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
unExpectAsynchResponseTemp() {
|
|
411
|
+
this.responseQueue.remove();
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
asynchResponseSuccess(index, value) {
|
|
415
|
+
this.responseQueue.done({result: true, value: value, index: index});
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
asynchResponseError(message, index) {
|
|
419
|
+
this.responseQueue.done({result: false, message: message, index: index});
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
addResponseCb(cb) {
|
|
423
|
+
this.responseCbs.push(cb);
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
handleRequest(requestData, cb, cbMessage) {
|
|
427
|
+
this.responseQueue = new ResponseQueue(this.callback.bind(this), cb, this.constructor.name);
|
|
428
|
+
super.handleRequest(requestData, cb, cbMessage);
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
// TODO: add the externalCb parameter???
|
|
432
|
+
callback(results) {
|
|
433
|
+
this.responseCbs.forEach((cb) => {
|
|
434
|
+
cb();
|
|
435
|
+
});
|
|
436
|
+
const responseResult = results.responses.every((response) => {
|
|
437
|
+
return response.result;
|
|
438
|
+
});
|
|
439
|
+
const responseValues = results.responses.filter((response) => {
|
|
440
|
+
return undefined !== response.value;
|
|
441
|
+
});
|
|
442
|
+
if(responseResult) {
|
|
443
|
+
if(0 === responseValues.length) {
|
|
444
|
+
return this.responsePartSuccess();
|
|
445
|
+
}
|
|
446
|
+
else if(1 === responseValues.length) {
|
|
447
|
+
return this.responsePartSuccess(responseValues[0].value);
|
|
448
|
+
}
|
|
449
|
+
else {
|
|
450
|
+
responseValues.sort((a, b) => {
|
|
451
|
+
if(a.index > b.index) {
|
|
452
|
+
return 1;
|
|
453
|
+
}
|
|
454
|
+
else if(a.index < b.index) {
|
|
455
|
+
return -1;
|
|
456
|
+
}
|
|
457
|
+
return 0;
|
|
458
|
+
});
|
|
459
|
+
const responseValueValues = responseValues.map((item) => {
|
|
460
|
+
return item.value;
|
|
461
|
+
});
|
|
462
|
+
return this.responsePartSuccess(responseValueValues);
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
else {
|
|
466
|
+
results.responses.sort((a, b) => {
|
|
467
|
+
if(a.index > b.index) {
|
|
468
|
+
return 1;
|
|
469
|
+
}
|
|
470
|
+
else if(a.index < b.index) {
|
|
471
|
+
return -1;
|
|
472
|
+
}
|
|
473
|
+
return 0;
|
|
474
|
+
});
|
|
475
|
+
const messages = results.responses.map((response) => {
|
|
476
|
+
return response.message;
|
|
477
|
+
});
|
|
478
|
+
return this.responsePartError(messages);
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
|
|
484
|
+
module.exports = PluginBaseMulti;
|
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
const PluginLock = require('./plugin-lock');
|
|
5
|
+
const ActionResponseError = require('z-abs-corelayer-cs/clientServer/communication/action-response-error');
|
|
6
|
+
const ActionResponseSuccess = require('z-abs-corelayer-cs/clientServer/communication/action-response-success');
|
|
7
|
+
const GuidGenerator = require('z-abs-corelayer-cs/clientServer/guid-generator');
|
|
8
|
+
const ChildProcess = require('child_process');
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class PluginBase {
|
|
12
|
+
constructor(restType, ...authorization) {
|
|
13
|
+
this.dataName = this.constructor.name;
|
|
14
|
+
this.restType = restType;
|
|
15
|
+
this.authorization = authorization;
|
|
16
|
+
this.requestData = null;
|
|
17
|
+
this.cb = null;
|
|
18
|
+
this.cbMessage = null;
|
|
19
|
+
this.dataResponse = null;
|
|
20
|
+
this.tokenIn = null;
|
|
21
|
+
this.tokenOut = null;
|
|
22
|
+
this._id = ++PluginBase.instance;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
static instance = 0;
|
|
26
|
+
static ADD = 'Add';
|
|
27
|
+
static DELETE = 'Delete';
|
|
28
|
+
static GET = 'Get';
|
|
29
|
+
static UPDATE = 'Update';
|
|
30
|
+
|
|
31
|
+
initData(dataResponse) {
|
|
32
|
+
this.dataResponse = dataResponse;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
readLock(name, cb) {
|
|
36
|
+
PluginLock.readLock(name, cb);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
writeLock(name, cb) {
|
|
40
|
+
PluginLock.writeLock(name, cb);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
setTokenIn(token) {
|
|
44
|
+
this.tokenIn = token;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
setTokenOut(token) {
|
|
48
|
+
this.tokenOut = token;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
responsePartSuccess(data) {
|
|
52
|
+
if(this.cb) {
|
|
53
|
+
this.cb(new ActionResponseSuccess(this.dataName, this.requestData.index, data));
|
|
54
|
+
this.cb = null;
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
ddb.warning(`responsePartSuccess[${this._id}]:`, this.constructor.name, this.cb);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
responsePartError(msg) {
|
|
62
|
+
if(this.cb) {
|
|
63
|
+
this.cb(new ActionResponseError(this.dataName,this.requestData.index, msg));
|
|
64
|
+
this.cb = null;
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
ddb.warning(`responsePartSuccess[${this._id}]:`, this.constructor.name, this.cb);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
forkWorkerProcess(name, pluginPath, debug, execArgv, done) {
|
|
72
|
+
this.dataResponse.forkWorkerProcess(name, this.requestData.sessionId, pluginPath, debug, execArgv, (pid, sessionData) => {
|
|
73
|
+
try {
|
|
74
|
+
if(done) {
|
|
75
|
+
done(pid, sessionData);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
catch(err) {
|
|
79
|
+
ddb.error('Exception in forkWorkerProcess callback: ', err, err.stack);
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
killWorkerProcess(done) {
|
|
85
|
+
this.dataResponse.killWorkerProcess(this.requestData.sessionId, (code) => {
|
|
86
|
+
try {
|
|
87
|
+
if(done) {
|
|
88
|
+
done(code);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
catch(err) {
|
|
92
|
+
ddb.error('Exception in killWorkerProcess callback: ', err, err.stack);
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
sendRequestToWorkerProcess(request, cb) {
|
|
98
|
+
const session = this.dataResponse.sessionCache.getSession(this.requestData.sessionId);
|
|
99
|
+
const id = GuidGenerator.create();
|
|
100
|
+
request.setIds(id, this.requestData.sessionId);
|
|
101
|
+
this.dataResponse.sendRequestToWorkerProcess(session, id, request, cb, this.cbMessage);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
sendMessageToWorkerProcess(msg) {
|
|
105
|
+
const session = this.dataResponse.sessionCache.getSession(this.requestData.sessionId);
|
|
106
|
+
this.dataResponse.sendMessageToWorkerProcess(session, msg);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
getNode() {
|
|
110
|
+
return this.dataResponse.getNode();
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
getService(name) {
|
|
114
|
+
return this.dataResponse.getService(name);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
getSession() {
|
|
118
|
+
return this.dataResponse.sessionCache.getSession(this.requestData.sessionId);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
setSessionData(name, data) {
|
|
122
|
+
this.dataResponse.sessionCache.setSessionData(this.requestData.sessionId, name, data);
|
|
123
|
+
return data;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
getSessionData(name) {
|
|
127
|
+
return this.dataResponse.sessionCache.getSessionData(this.requestData.sessionId, name);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
clearSession(done) {
|
|
131
|
+
this.dataResponse.clearSession(this.requestData.sessionId, done);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
sendMessage(msg, dataBuffers, debug) {
|
|
135
|
+
this.cbMessage(msg, dataBuffers, debug);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
handleRequest(requestData, cb, cbMessage) {
|
|
139
|
+
if(this.cb) {
|
|
140
|
+
// Make an error callback
|
|
141
|
+
ddb.error(`handleRequest[${this._id}]:`, this.constructor.name, this.cb);
|
|
142
|
+
}
|
|
143
|
+
this.requestData = requestData;
|
|
144
|
+
this.cb = cb;
|
|
145
|
+
this.cbMessage = cbMessage;
|
|
146
|
+
try {
|
|
147
|
+
if(!this.onRequest) {
|
|
148
|
+
return ddb.warning(`${this.constructor.name}.onRequest is not implemented.`);
|
|
149
|
+
}
|
|
150
|
+
this.onRequest(...requestData.params);
|
|
151
|
+
}
|
|
152
|
+
catch(err) {
|
|
153
|
+
ddb.error(`Error in ${this.dataName}.onRequest()`, err, err.stack);
|
|
154
|
+
return this.responsePartError(`${this.dataName}.onRequest exception: ${err}`);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
handleMessageFromClient(msg) {
|
|
159
|
+
if(this.onMessageFromClient && typeof this.onMessageFromClient === 'function') {
|
|
160
|
+
try {
|
|
161
|
+
this.onMessageFromClient(msg);
|
|
162
|
+
}
|
|
163
|
+
catch(err) {
|
|
164
|
+
ddb.error(`Error in ${this.dataName}.onMessageFromClient()`, err, err.stack);
|
|
165
|
+
}
|
|
166
|
+
return true;
|
|
167
|
+
}
|
|
168
|
+
return false;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
handleMessageToClient(msg, dataBuffers, cbMessage) {
|
|
172
|
+
if(this.onMessageToClient && typeof this.onMessageToClient === 'function') {
|
|
173
|
+
try {
|
|
174
|
+
this.onMessageToClient(msg, dataBuffers, cbMessage);
|
|
175
|
+
}
|
|
176
|
+
catch(err) {
|
|
177
|
+
ddb.error(`Error in ${this.dataName}.onMessageToClient()`, err, err.stack);
|
|
178
|
+
}
|
|
179
|
+
return true;
|
|
180
|
+
}
|
|
181
|
+
return false;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
handleMessageFromWorker(msg, dataBuffers, debug, cbMessage) {
|
|
185
|
+
if(this.onMessageFromWorker && typeof this.onMessageFromWorker === 'function') {
|
|
186
|
+
try {
|
|
187
|
+
this.onMessageFromWorker(msg, dataBuffers, debug, cbMessage);
|
|
188
|
+
}
|
|
189
|
+
catch(err) {
|
|
190
|
+
ddb.error(`Error in ${this.dataName}.onMessageFromWorker()`, err, err.stack);
|
|
191
|
+
}
|
|
192
|
+
return true;
|
|
193
|
+
}
|
|
194
|
+
else {
|
|
195
|
+
return false;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
handleMessageFromParent(msg, cbMessage) {
|
|
200
|
+
if(msg && this.onMessageFromParent && typeof this.onMessageFromParent === 'function') {
|
|
201
|
+
try {
|
|
202
|
+
this.onMessageFromParent(msg, cbMessage);
|
|
203
|
+
}
|
|
204
|
+
catch(err) {
|
|
205
|
+
ddb.error(`Error in ${this.dataName}.onMessageFromParent()`, err, err.stack);
|
|
206
|
+
}
|
|
207
|
+
return true;
|
|
208
|
+
}
|
|
209
|
+
else {
|
|
210
|
+
return false;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
handleClose(done) {
|
|
215
|
+
if(this.onClose && typeof this.onClose === 'function') {
|
|
216
|
+
try {
|
|
217
|
+
this.onClose((e) => {
|
|
218
|
+
done(e);
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
catch(err) {
|
|
222
|
+
ddb.error(`Error in ${this.dataName}.onClose()`, err, err.stack);
|
|
223
|
+
done(err);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
else {
|
|
227
|
+
done();
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
module.exports = PluginBase;
|