forked from openiap/opencore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessage.ts
More file actions
556 lines (517 loc) · 19.2 KB
/
Copy pathMessage.ts
File metadata and controls
556 lines (517 loc) · 19.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
import { WebSocketClient, QueuedMessage } from "./WebSocketClient";
import { Base } from "./Base";
import { NoderedUtil } from "./NoderedUtil";
import { Config } from "../Config";
function isNumber(value: string | number): boolean {
return ((value != null) && !isNaN(Number(value.toString())));
}
export class SocketMessage {
public id: string;
public replyto: string;
public command: string;
public data: string;
public count: number;
public index: number;
public static fromjson(json: string): SocketMessage {
let result: SocketMessage = new SocketMessage();
let obj: any = JSON.parse(json);
result.command = obj.command;
result.id = obj.id;
result.replyto = obj.replyto;
result.count = 1;
result.index = 0;
result.data = obj.data;
if (isNumber(obj.count)) { result.count = obj.count; }
if (isNumber(obj.index)) { result.index = obj.index; }
if (result.id === null || result.id === undefined || result.id === "") {
// result.id = crypto.randomBytes(16).toString("hex");
result.id = Math.random().toString(36).substr(2, 9);
}
return result;
}
public static frommessage(msg: Message, data: string, count: number, index: number): SocketMessage {
var result: SocketMessage = new SocketMessage();
result.id = msg.id;
result.replyto = msg.replyto;
result.command = msg.command;
result.count = count;
result.index = index;
result.data = data;
return result;
}
public static fromcommand(command: string): SocketMessage {
var result: SocketMessage = new SocketMessage();
result.command = command;
result.count = 1;
result.index = 0;
result.id = Math.random().toString(36).substr(2, 9);
return result;
}
}
export class SigninMessage {
public error: string;
public username: string;
public password: string;
public validate_only: boolean = false;
public clientagent: string;
public clientversion: string;
public user: TokenUser;
public jwt: string;
public rawAssertion: string;
public longtoken: boolean = false;
static assign(o: any): SigninMessage {
if (typeof o === "string" || o instanceof String) {
return Object.assign(new SigninMessage(), JSON.parse(o.toString()));
}
return Object.assign(new SigninMessage(), o);
}
}
export class TokenUser {
_type: string;
_id: string;
name: string;
username: string;
roles: Rolemember[] = [];
static assign<T>(o: T): T {
var newo: TokenUser = new TokenUser();
return Object.assign(newo, o);
}
}
export class Rolemember {
constructor(name: string, _id: string) {
this.name = name;
this._id = _id;
}
name: string;
_id: string;
}
export class Role extends Base {
constructor(name: string, _id: string) {
super();
this.name = name;
this._id = _id;
this._type = "role";
}
name: string;
_id: string;
members: Rolemember[];
}
export class QueryMessage {
public error: string;
public jwt: string;
public query: any;
public projection: Object;
public top: number;
public skip: number;
public orderby: Object | string;
public collectionname: string;
public result: any[];
public queryas: string;
static assign(o: any): QueryMessage {
if (typeof o === "string" || o instanceof String) {
return Object.assign(new QueryMessage(), JSON.parse(o.toString()));
}
return Object.assign(new QueryMessage(), o);
}
}
export class AggregateMessage {
public error: string;
public jwt: string;
public aggregates: object[];
public collectionname: string;
public result: any[];
static assign(o: any): AggregateMessage {
if (typeof o === "string" || o instanceof String) {
return Object.assign(new AggregateMessage(), JSON.parse(o.toString()));
}
return Object.assign(new AggregateMessage(), o);
}
}
export class InsertOneMessage {
public error: string;
public jwt: string;
// w: 1 - Requests acknowledgment that the write operation has propagated
// w: 0 - Requests no acknowledgment of the write operation
// w: 2 would require acknowledgment from the primary and one of the secondaries
// w: 3 would require acknowledgment from the primary and both secondaries
public w: number;
// true, requests acknowledgment that the mongod instances have written to the on-disk journal
public j: boolean;
public item: any;
public collectionname: string;
public result: any;
static assign(o: any): InsertOneMessage {
if (typeof o === "string" || o instanceof String) {
return Object.assign(new InsertOneMessage(), JSON.parse(o.toString()));
}
return Object.assign(new InsertOneMessage(), o);
}
}
export class UpdateOneMessage {
public error: string;
public jwt: string;
// w: 1 - Requests acknowledgment that the write operation has propagated
// w: 0 - Requests no acknowledgment of the write operation
// w: 2 would require acknowledgment from the primary and one of the secondaries
// w: 3 would require acknowledgment from the primary and both secondaries
public w: number;
// true, requests acknowledgment that the mongod instances have written to the on-disk journal
public j: boolean;
public item: object;
public collectionname: string;
public query: object;
public result: any;
public opresult: any;
static assign(o: any): UpdateOneMessage {
if (typeof o === "string" || o instanceof String) {
return Object.assign(new UpdateOneMessage(), JSON.parse(o.toString()));
}
return Object.assign(new UpdateOneMessage(), o);
}
}
export class UpdateManyMessage {
public error: string;
public jwt: string;
// w: 1 - Requests acknowledgment that the write operation has propagated
// w: 0 - Requests no acknowledgment of the write operation
// w: 2 would require acknowledgment from the primary and one of the secondaries
// w: 3 would require acknowledgment from the primary and both secondaries
public w: number;
// true, requests acknowledgment that the mongod instances have written to the on-disk journal
public j: boolean;
public query: object;
public item: object;
public collectionname: string;
public result: any[];
static assign(o: any): UpdateManyMessage {
if (typeof o === "string" || o instanceof String) {
return Object.assign(new UpdateManyMessage(), JSON.parse(o.toString()));
}
return Object.assign(new UpdateManyMessage(), o);
}
}
export class InsertOrUpdateOneMessage {
public error: string;
public jwt: string;
// w: 1 - Requests acknowledgment that the write operation has propagated
// w: 0 - Requests no acknowledgment of the write operation
// w: 2 would require acknowledgment from the primary and one of the secondaries
// w: 3 would require acknowledgment from the primary and both secondaries
public w: number;
// true, requests acknowledgment that the mongod instances have written to the on-disk journal
public j: boolean;
public item: object;
public collectionname: string;
public uniqeness: string
public result: any;
static assign(o: any): InsertOrUpdateOneMessage {
if (typeof o === "string" || o instanceof String) {
return Object.assign(new InsertOrUpdateOneMessage(), JSON.parse(o.toString()));
}
return Object.assign(new InsertOrUpdateOneMessage(), o);
}
}
export class DeleteOneMessage {
public error: string;
public jwt: string;
public _id: string;
public collectionname: string;
static assign(o: any): DeleteOneMessage {
if (typeof o === "string" || o instanceof String) {
return Object.assign(new DeleteOneMessage(), JSON.parse(o.toString()));
}
return Object.assign(new DeleteOneMessage(), o);
}
}
export class GetFileMessage {
public error: string;
public jwt: string;
public filename: string;
public mimeType: string;
public id: string;
public metadata: any;
public file: string;
static assign(o: any): GetFileMessage {
if (typeof o === "string" || o instanceof String) {
return Object.assign(new GetFileMessage(), JSON.parse(o.toString()));
}
return Object.assign(new GetFileMessage(), o);
}
}
export class SaveFileMessage {
public error: string;
public jwt: string;
public filename: string;
public mimeType: string;
public id: string;
public metadata: any;
public file: string;
static assign(o: any): SaveFileMessage {
if (typeof o === "string" || o instanceof String) {
return Object.assign(new SaveFileMessage(), JSON.parse(o.toString()));
}
return Object.assign(new SaveFileMessage(), o);
}
}
export declare function emit(k, v);
export type mapFunc = () => void;
export type reduceFunc = (key: string, values: any[]) => any;
export type finalizeFunc = (key: string, value: any) => any;
export class MapReduceMessage<T> {
public error: string;
public jwt: string;
public collectionname: string;
public result: T[];
public scope: any;
constructor(public map: mapFunc, public reduce: reduceFunc, public finalize: finalizeFunc, public query: any, public out: string | any) {
}
static assign<T>(o: any): MapReduceMessage<T> {
if (typeof o === "string" || o instanceof String) {
return Object.assign(new MapReduceMessage(null, null, null, null, null), JSON.parse(o.toString()));
}
return Object.assign(new MapReduceMessage(null, null, null, null, null), o);
}
}
export class GetNoderedInstanceMessage {
public error: string;
public jwt: any;
public name: string;
public _id: string;
public result: any;
public results: any[];
static assign(o: any): GetNoderedInstanceMessage {
if (typeof o === "string" || o instanceof String) {
return Object.assign(new GetNoderedInstanceMessage(), JSON.parse(o.toString()));
}
return Object.assign(new GetNoderedInstanceMessage(), o);
}
}
export class CreateWorkflowInstanceMessage {
public error: string;
public jwt: any;
public correlationId: string;
public newinstanceid: string;
public state: string;
public queue: string;
public workflowid: string;
public resultqueue: string;
public targetid: string;
public parentid: string;
public initialrun: boolean;
public payload: any;
static assign<T>(o: any): CreateWorkflowInstanceMessage {
if (typeof o === "string" || o instanceof String) {
return Object.assign(new CreateWorkflowInstanceMessage(), JSON.parse(o.toString()));
}
return Object.assign(new CreateWorkflowInstanceMessage(), o);
}
}
export class RegisterQueueMessage {
public error: string;
public jwt: any;
public queuename: string;
static assign(o: any): RegisterQueueMessage {
if (typeof o === "string" || o instanceof String) {
return Object.assign(new RegisterQueueMessage(), JSON.parse(o.toString()));
}
return Object.assign(new RegisterQueueMessage(), o);
}
}
export class QueueMessage {
public error: string;
public jwt: any;
public correlationId: string;
public replyto: string;
public queuename: string;
public data: any;
public expiration: number;
public consumerTag: string;
public routingkey: string;
public exchange: string;
static assign(o: any): QueueMessage {
if (typeof o === "string" || o instanceof String) {
return Object.assign(new QueueMessage(), JSON.parse(o.toString()));
}
return Object.assign(new QueueMessage(), o);
}
}
export class JSONfn {
public static stringify(obj) {
return JSON.stringify(obj, function (key, value) {
return (typeof value === 'function') ? value.toString() : value;
});
}
public static parse(str) {
return JSON.parse(str, function (key, value) {
if (typeof value != 'string') return value;
return (value.substring(0, 8) == 'function') ? eval('(' + value + ')') : value;
});
}
}
export class Message {
public id: string;
public replyto: string;
public command: string;
public data: string;
public static frommessage(msg: SocketMessage, data: string): Message {
var result: Message = new Message();
result.id = msg.id;
result.replyto = msg.replyto;
result.command = msg.command;
result.data = data;
return result;
}
public Process(cli: WebSocketClient): void {
try {
var command: string = "";
if (this.command !== null && this.command !== undefined) { command = this.command.toLowerCase(); }
if (this.command !== "ping" && this.command !== "pong") {
if (this.replyto !== null && this.replyto !== undefined) {
var qmsg: QueuedMessage = cli.messageQueue[this.replyto];
if (qmsg !== undefined && qmsg !== null) {
cli._logger.verbose("[RESC][" + this.command + "][" + this.id + "][" + this.replyto + "][CB]");
qmsg.message = Object.assign(qmsg.message, JSON.parse(this.data));
if (qmsg.cb !== undefined && qmsg.cb !== null) { qmsg.cb(qmsg.message); }
delete cli.messageQueue[this.id];
} else {
cli._logger.verbose("[RESC][" + this.command + "][" + this.id + "][" + this.replyto + "][NO CB!]");
}
return;
} else {
cli._logger.verbose("[RESC][" + this.command + "][" + this.id + "][" + this.replyto + "]");
}
}
switch (command) {
case "ping":
this.Ping(cli);
break;
case "pong":
break;
case "signin":
this.Signin(cli);
break;
case "refreshtoken":
this.RefreshToken(cli);
break;
case "queuemessage":
this.QueueMessage(cli);
break;
default:
console.error("Unknown command " + command);
this.UnknownCommand(cli);
break;
}
} catch (error) {
if (error.message) { cli._logger.error(error.message); }
else { cli._logger.error(error); }
}
}
public async Send(cli: WebSocketClient): Promise<void> {
try {
await cli.Send(this);
} catch (error) {
throw error;
}
}
private async UnknownCommand(cli: WebSocketClient): Promise<void> {
this.Reply("error");
this.data = "Unknown command";
try {
await this.Send(cli);
} catch (error) {
throw error;
}
}
private async Ping(cli: WebSocketClient): Promise<void> {
this.Reply("pong");
try {
await this.Send(cli);
} catch (error) {
throw error;
}
}
public Reply(command: string): void {
this.command = command;
this.replyto = this.id;
this.id = Math.random().toString(36).substr(2, 9);
}
private Signin(cli: WebSocketClient): void {
var msg: SigninMessage = SigninMessage.assign(this.data);
cli.jwt = msg.jwt;
cli.user = msg.user;
var qmsg: QueuedMessage = cli.messageQueue[this.replyto];
if (qmsg !== undefined && qmsg !== null) {
if (qmsg.cb !== undefined && qmsg.cb !== null) { qmsg.cb(msg); }
delete cli.messageQueue[this.id];
}
}
private RefreshToken(cli: WebSocketClient): void {
var msg: SigninMessage = SigninMessage.assign(this.data);
cli.jwt = msg.jwt;
cli.user = msg.user;
}
private Query(cli: WebSocketClient): void {
var msg: QueryMessage = QueryMessage.assign(this.data);
var qmsg: QueuedMessage = cli.messageQueue[this.replyto];
if (qmsg !== undefined && qmsg !== null) {
if (qmsg.cb !== undefined && qmsg.cb !== null) { qmsg.cb(msg); }
delete cli.messageQueue[this.id];
}
}
private async QueueMessage(cli: WebSocketClient): Promise<void> {
this.Reply(this.command);
var handled: boolean = false;
var msg: QueueMessage = QueueMessage.assign(this.data);
if (!NoderedUtil.IsNullEmpty(msg.queuename)) {
if (NoderedUtil.messageQueuecb[msg.queuename] != null) {
NoderedUtil.messageQueuecb[msg.queuename](msg, async (nack: boolean, result: any) => {
if (nack == false) {
this.Reply("error");
this.data = "nack message";
if (typeof result === 'string' || result instanceof String) {
this.data = (result as any);
}
} else {
if (result != null && !NoderedUtil.IsNullEmpty(msg.replyto) && this.command != "error") {
try {
await NoderedUtil.QueueMessage(cli, msg.replyto, "", result, msg.correlationId, Config.amqp_reply_expiration);
} catch (error) {
console.error("Error sending response to " + msg.replyto + " " + JSON.stringify(error))
}
}
// ROLLBACK
// if (this.command != "error" && !NoderedUtil.IsNullEmpty(msg.replyto)) {
// try {
// await NoderedUtil.QueueMessage(msg.replyto, "", result, msg.correlationId, Config.amqp_reply_expiration);
// } catch (error) {
// console.error("Error sending response to " + msg.replyto + " " + JSON.stringify(error))
// }
// }
//this.data = "";
}
try {
await this.Send(cli);
} catch (error) {
throw error;
}
});
handled = true;
}
}
// ROLLBACK
// if (!handled) {
// if (!NoderedUtil.IsNullEmpty(msg.correlationId)) {
// if (NoderedUtil.messageQueue[msg.correlationId] != null) {
// NoderedUtil.messageQueue[msg.correlationId].callback(msg);
// delete NoderedUtil.messageQueue[msg.correlationId];
// handled = true;
// try {
// await this.Send(cli);
// } catch (error) {
// throw error;
// }
// }
// }
// }
}
}