forked from openiap/opencore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebSocketServerClient.ts
More file actions
624 lines (620 loc) · 31.2 KB
/
Copy pathWebSocketServerClient.ts
File metadata and controls
624 lines (620 loc) · 31.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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
import * as WebSocket from "ws";
import { SocketMessage } from "./SocketMessage";
import { Message, JSONfn } from "./Messages/Message";
import { Config } from "./Config";
import { amqpwrapper, QueueMessageOptions, amqpqueue, amqpexchange, exchangealgorithm } from "./amqpwrapper";
import { NoderedUtil, Base, InsertOneMessage, QueueMessage, MapReduceMessage, QueryMessage, UpdateOneMessage, UpdateManyMessage, DeleteOneMessage, User, mapFunc, reduceFunc, finalizeFunc, QueuedMessage, QueuedMessageCallback, WatchEventMessage, QueueClosedMessage, ExchangeClosedMessage, TokenUser } from "@openiap/openflow-api";
import { ChangeStream } from "mongodb";
import { WebSocketServer } from "./WebSocketServer";
import { Span } from "@opentelemetry/api";
import { Logger } from "./Logger";
import { clientType } from "./Audit";
import express = require("express");
interface IHashTable<T> {
[key: string]: T;
}
const Semaphore = (n) => ({
n,
async down() {
while (this.n <= 0) await this.wait();
this.n--;
},
up() {
this.n++;
},
async wait() {
if (this.n <= 0) return await new Promise((res, req) => {
setImmediate(async () => res(await this.wait()))
});
return;
},
});
const semaphore = Semaphore(1);
export declare class RegisterExchangeResponse {
exchangename: string;
queuename: string;
}
export class clsstream {
public stream: ChangeStream;
public id: string;
public callback: any;
aggregates: object[];
collectionname: string;
}
export class WebSocketServerClient {
public jwt: string;
public _socketObject: WebSocket;
private _receiveQueue: SocketMessage[];
private _sendQueue: SocketMessage[];
public messageQueue: IHashTable<QueuedMessage> = {};
public remoteip: string;
public clientagent: clientType;
public clientversion: string;
public lastheartbeat: Date = new Date();
public lastheartbeatstr: string = new Date().toISOString();
public lastheartbeatsec: string = "0";
public metrics: string = "";
public id: string = "";
public user: User;
public username: string;
public _queues: amqpqueue[] = [];
public _queuescounter: number = 0;
public _queuescurrent: number = 0;
public _queuescounterstr: string = "0";
public _queuescurrentstr: string = "0";
public _exchanges: amqpexchange[] = [];
public devnull: boolean = false;
public commandcounter: object = {};
private _amqpdisconnected: any = null;
private _dbdisconnected: any = null;
private _dbconnected: any = null;
public inccommandcounter(command: string): number {
let result: number = 0;
if (!NoderedUtil.IsNullUndefinded(this.commandcounter[command])) result = this.commandcounter[command];
result++;
this.commandcounter[command] = result;
return result;
}
public static remoteip(req: express.Request) {
let remoteip: string = req.socket.remoteAddress;
if (req.headers["X-Forwarded-For"] != null) remoteip = req.headers["X-Forwarded-For"] as string;
if (req.headers["X-real-IP"] != null) remoteip = req.headers["X-real-IP"] as string;
if (req.headers["x-forwarded-for"] != null) remoteip = req.headers["x-forwarded-for"] as string;
if (req.headers["x-real-ip"] != null) remoteip = req.headers["x-real-ip"] as string;
return remoteip;
}
constructor(socketObject: WebSocket, req: any) {
this.id = NoderedUtil.GetUniqueIdentifier();
this._socketObject = socketObject;
this._receiveQueue = [];
this._sendQueue = [];
const sock: any = ((socketObject as any)._socket);
if (sock != undefined) {
this.remoteip = sock.remoteAddress;
}
//if (NoderedUtil.IsNullEmpty(this.remoteip) && !NoderedUtil.IsNullUndefinded(req) && !NoderedUtil.IsNullUndefinded(req.headers)) {
if (!NoderedUtil.IsNullUndefinded(req)) {
this.remoteip = WebSocketServerClient.remoteip(req);
}
Logger.instanse.info("WebSocketServerClient", "constructor", "new client " + this.id + " from " + this.remoteip);
socketObject.on("open", this.open.bind(this));
socketObject.on("message", this.message.bind(this)); // e: MessageEvent
socketObject.on("error", this.error.bind(this));
socketObject.on("close", this.close.bind(this));
this._dbdisconnected = this.dbdisconnected.bind(this);
this._dbconnected = this.dbconnected.bind(this);
Config.db.on("disconnected", this._dbdisconnected);
Config.db.on("connected", this._dbconnected);
this._amqpdisconnected = this.amqpdisconnected.bind(this);
amqpwrapper.Instance().on("disconnected", this._amqpdisconnected);
}
private async dbdisconnected(e: Event): Promise<void> {
var keys = Object.keys(this.watches);
for (var i = 0; i < keys.length; i++) {
let id = keys[i];
let w = this.watches[id];
await this.Watch(w.aggregates, w.collectionname, this.jwt, id);
}
}
private dbconnected(e: Event): void {
}
private amqpdisconnected(e: Event): void {
for (var i = 0; i < this._queues.length; i++) {
let msg: SocketMessage = SocketMessage.fromcommand("queueclosed");
let q: QueueClosedMessage = new QueueClosedMessage();
q.queuename = this._queues[i].queue;
msg.data = JSON.stringify(q);
this._socketObject.send(msg.tojson());
Logger.instanse.debug("WebSocketServerClient", "amqpdisconnected", "Send queue closed message to " + this.id + " for queue " + q.queuename);
}
for (var i = 0; i < this._exchanges.length; i++) {
let msg: SocketMessage = SocketMessage.fromcommand("exchangeclosed");
let q: ExchangeClosedMessage = new ExchangeClosedMessage();
q.queuename = this._exchanges[i].queue.queue; q.exchangename = this._exchanges[i].exchange;
msg.data = JSON.stringify(q);
this._socketObject.send(msg.tojson());
Logger.instanse.debug("WebSocketServerClient", "amqpdisconnected", "Send queue closed message to " + this.id + " for exchange " + q.exchangename);
}
this._exchanges = [];
this.CloseConsumers(null);
}
private open(e: Event): void {
Logger.instanse.info("WebSocketServerClient", "open", "Connection opened " + e + " " + this.id);
}
private close(e: CloseEvent): void {
Logger.instanse.info("WebSocketServerClient", "close", "Connection closed " + e + " " + this.id + "/" + this.clientagent);
this.Close();
Config.db.removeListener("disconnected", this._dbdisconnected);
Config.db.removeListener("connected", this._dbconnected);
}
private error(e: Event): void {
Logger.instanse.error("WebSocketServerClient", "error", e + " " + this.id + "/" + this.clientagent);
}
public queuecount(): number {
if (this._queues == null) return 0;
return this._queues.length;
}
public connected(): boolean {
if (this._socketObject == null) return false;
if (this._socketObject.readyState === this._socketObject.OPEN || this._socketObject.readyState === this._socketObject.CONNECTING) {
return true;
}
if (this._socketObject.readyState === this._socketObject.CLOSED) {
delete this._socketObject;
}
return false;
}
public ping(parent: Span): void {
const span: Span = (Config.otel_trace_pingclients && parent != null ? Logger.otel.startSubSpan("WebSocketServerClient.ping", parent) : null);
try {
let msg: SocketMessage = SocketMessage.fromcommand("ping");
if (this._socketObject == null) {
if (this.queuecount() > 0) {
this.CloseConsumers(span);
}
// if (this.streamcount() > 0) {
// this.CloseStreams();
// }
return;
}
if (this._socketObject.readyState === this._socketObject.CLOSED || this._socketObject.readyState === this._socketObject.CLOSING) {
if (this.queuecount() > 0) {
this.CloseConsumers(span);
}
return;
}
} catch (error) {
Logger.instanse.error("WebSocketServerClient", "ping", error);
span?.recordException(error);
this._receiveQueue = [];
this._sendQueue = [];
if (this._socketObject != null) {
this.Close().catch((err) => {
Logger.instanse.error("WebSocketServerClient", "ping", error);
});
}
} finally {
Logger.otel.endSpan(span);
}
}
private _message(message: string): void {
Logger.instanse.silly("WebSocketServerClient", "_message", "WebSocket message received " + message);
let msg: SocketMessage = SocketMessage.fromjson(message);
Logger.instanse.silly("WebSocketServerClient", "_message", "WebSocket message received id: " + msg.id + " index: " + msg.index + " count: " + msg.count);
this._receiveQueue.push(msg);
if ((msg.index + 1) >= msg.count) this.ProcessQueue();
}
private async message(message: string): Promise<void> {
let username: string = "Unknown";
try {
if (!NoderedUtil.IsNullUndefinded(this.user)) { username = this.user.username; }
this._message(message);
} catch (error) {
Logger.instanse.error("WebSocketServerClient", "message", "[" + username + "/" + this.clientagent + "/" + this.id + "] " + (error.message ? error.message : error));
const errormessage: Message = new Message(); errormessage.command = "error"; errormessage.data = (error.message ? error.message : error);
this._socketObject.send(JSON.stringify(errormessage));
}
}
public async CloseConsumers(parent: Span): Promise<void> {
await semaphore.down();
for (let i = this._queues.length - 1; i >= 0; i--) {
try {
// await this.CloseConsumer(this._queues[i]);
await amqpwrapper.Instance().RemoveQueueConsumer(this.user, this._queues[i], undefined);
this._queues.splice(i, 1);
this._queuescurrent--;
this._queuescurrentstr = this._queuescurrent.toString();
} catch (error) {
Logger.instanse.error("WebSocketServerClient", "CloseConsumers", "WebSocketclient::closeconsumers " + error);
}
}
if (!NoderedUtil.IsNullUndefinded(WebSocketServer.websocket_queue_count)) WebSocketServer.websocket_queue_count.bind({ ...Logger.otel.defaultlabels, clientid: this.id }).update(this._queues.length);
semaphore.up();
}
public async Close(): Promise<void> {
const span: Span = Logger.otel.startSpan("WebSocketServerClient.Close");
try {
await this.CloseConsumers(span);
// await this.CloseStreams();
if (this._socketObject != null) {
try {
this._socketObject.removeAllListeners();
} catch (error) {
Logger.instanse.error("WebSocketServerClient", "Close", error);
}
try {
this._socketObject.close();
} catch (error) {
Logger.instanse.error("WebSocketServerClient", "Close", error);
}
}
try {
amqpwrapper.Instance().removeListener("disconnected", this._amqpdisconnected);
} catch (error) {
Logger.instanse.error("WebSocketServerClient", "Close", error);
}
var keys = Object.keys(this.watches);
for (var i = 0; i < keys.length; i++) {
await this.UnWatch(keys[i], this.jwt);
}
} catch (error) {
span?.recordException(error);
throw error;
} finally {
WebSocketServer.update_mongodb_watch_count(this);
Logger.otel.endSpan(span);
}
}
public async CloseConsumer(user: TokenUser | User, queuename: string, parent: Span): Promise<void> {
const span: Span = Logger.otel.startSubSpan("WebSocketServerClient.CloseConsumer", parent);
await semaphore.down();
try {
var old = this._queues.length;
for (let i = this._queues.length - 1; i >= 0; i--) {
const q = this._queues[i];
if (q && (q.queue == queuename || q.queuename == queuename)) {
try {
amqpwrapper.Instance().RemoveQueueConsumer(user, this._queues[i], span).catch((err) => {
Logger.instanse.error("WebSocketServerClient", "CloseConsumer", err);
});
this._queues.splice(i, 1);
this._queuescurrent--;
this._queuescurrentstr = this._queuescurrent.toString();
if (!NoderedUtil.IsNullUndefinded(WebSocketServer.websocket_queue_count)) WebSocketServer.websocket_queue_count.bind({ ...Logger.otel.defaultlabels, clientid: this.id }).update(this._queues.length);
} catch (error) {
Logger.instanse.error("WebSocketServerClient", "CloseConsumer", error);
}
}
}
} catch (error) {
span?.recordException(error);
throw error;
} finally {
semaphore.up();
Logger.otel.endSpan(span);
}
}
public async RegisterExchange(user: TokenUser | User, exchangename: string, algorithm: exchangealgorithm, routingkey: string = "", addqueue: boolean, parent: Span): Promise<RegisterExchangeResponse> {
const span: Span = Logger.otel.startSubSpan("WebSocketServerClient.CreateConsumer", parent);
try {
let exclusive: boolean = false; // Should we keep the queue around ? for robots and roles
let exchange = exchangename;
if (NoderedUtil.IsNullEmpty(exchange)) {
if (this.clientagent == "nodered") {
exchange = "nodered." + NoderedUtil.GetUniqueIdentifier(); exclusive = true;
} else if (this.clientagent == "webapp") {
exchange = "webapp." + NoderedUtil.GetUniqueIdentifier(); exclusive = true;
} else if (this.clientagent == "openrpa") {
exchange = "openrpa." + NoderedUtil.GetUniqueIdentifier(); exclusive = true;
} else if (this.clientagent == "powershell") {
exchange = "powershell." + NoderedUtil.GetUniqueIdentifier(); exclusive = true;
} else {
exchange = "unknown." + NoderedUtil.GetUniqueIdentifier(); exclusive = true;
}
}
let exchangequeue: amqpexchange = null;
try {
const AssertExchangeOptions: any = Object.assign({}, (amqpwrapper.Instance().AssertExchangeOptions));
AssertExchangeOptions.exclusive = exclusive;
exchangequeue = await amqpwrapper.Instance().AddExchangeConsumer(user, exchange, algorithm, routingkey, AssertExchangeOptions, this.jwt, addqueue, async (msg: any, options: QueueMessageOptions, ack: any, done: any) => {
const _data = msg;
try {
const result = await this.Queue(msg, exchange, options);
done(result);
ack();
} catch (error) {
setTimeout(() => {
ack(false);
Logger.instanse.error("WebSocketServerClient", "RegisterExchange", exchange + " failed message queue message, nack and re queue message: ");
Logger.instanse.error("WebSocketServerClient", "RegisterExchange", error);
}, Config.amqp_requeue_time);
}
}, span);
if (exchangequeue) {
await semaphore.down();
if (exchangequeue.queue) exchange = exchangequeue.queue.queue;
this._exchanges.push(exchangequeue);
if (exchangequeue.queue) {
this._queues.push(exchangequeue.queue);
this._queuescounter++;
this._queuescurrent++;
}
this._queuescounterstr = this._queuescounter.toString();
this._queuescurrentstr = this._queuescurrent.toString();
}
} catch (error) {
Logger.instanse.error("WebSocketServerClient", "RegisterExchange", error);
}
if (exchangequeue) semaphore.up();
if (exchangequeue != null) return { exchangename: exchangequeue.exchange, queuename: exchangequeue.queue?.queue };
return null;
} catch (error) {
span?.recordException(error);
throw error;
} finally {
Logger.otel.endSpan(span);
}
}
public async CreateConsumer(queuename: string, parent: Span): Promise<string> {
const span: Span = Logger.otel.startSubSpan("WebSocketServerClient.CreateConsumer", parent);
try {
let exclusive: boolean = false; // Should we keep the queue around ? for robots and roles
let qname = queuename;
if (NoderedUtil.IsNullEmpty(qname)) {
if (this.clientagent == "nodered") {
qname = "nodered." + NoderedUtil.GetUniqueIdentifier(); exclusive = true;
} else if (this.clientagent == "webapp") {
qname = "webapp." + NoderedUtil.GetUniqueIdentifier(); exclusive = true;
} else if (this.clientagent == "openrpa") {
qname = "openrpa." + NoderedUtil.GetUniqueIdentifier(); exclusive = true;
} else if (this.clientagent == "powershell") {
qname = "powershell." + NoderedUtil.GetUniqueIdentifier(); exclusive = true;
} else {
qname = "unknown." + NoderedUtil.GetUniqueIdentifier(); exclusive = true;
}
}
await this.CloseConsumer(this.user, qname, span);
let queue: amqpqueue = null;
try {
const AssertQueueOptions: any = Object.assign({}, (amqpwrapper.Instance().AssertQueueOptions));
AssertQueueOptions.exclusive = exclusive;
if (NoderedUtil.IsNullEmpty(queuename)) {
AssertQueueOptions.autoDelete = true;
}
var exists = this._queues.filter(x => x.queuename == qname || x.queue == qname);
if (exists.length > 0) {
Logger.instanse.warn("WebSocketServerClient", "CreateConsumer", qname + " already exists, removing before re-creating");
for (let i = 0; i < exists.length; i++) {
await amqpwrapper.Instance().RemoveQueueConsumer(this.user, exists[i], span);
}
}
queue = await amqpwrapper.Instance().AddQueueConsumer(this.user, qname, AssertQueueOptions, this.jwt, async (msg: any, options: QueueMessageOptions, ack: any, done: any) => {
// const _data = msg;
var _data = msg;
try {
Logger.instanse.verbose("WebSocketServerClient", "CreateConsumer", "[preack] queuename: " + queuename + " qname: " + qname + " replyto: " + options.replyTo + " correlationId: " + options.correlationId)
_data = await this.Queue(msg, qname, options);;
ack();
// const result = await this.Queue(msg, qname, options);
// done(result);
Logger.instanse.debug("WebSocketServerClient", "CreateConsumer", "[ack] queuename: " + queuename + " qname: " + qname + " replyto: " + options.replyTo + " correlationId: " + options.correlationId)
} catch (error) {
setTimeout(() => {
ack(false);
Logger.instanse.warn("WebSocketServerClient", "CreateConsumer", "[nack] queuename: " + queuename + " qname: " + qname + " replyto: " + options.replyTo + " correlationId: " + options.correlationId + " error: " + (error.message ? error.message : error))
}, Config.amqp_requeue_time);
} finally {
try {
done(_data);
} catch (error) {
}
}
}, span);
if (queue) {
await semaphore.down();
qname = queue.queue;
this._queuescounter++;
this._queuescurrent++;
this._queuescounterstr = this._queuescounter.toString();
this._queuescurrentstr = this._queuescurrent.toString();
this._queues.push(queue);
}
if (!NoderedUtil.IsNullUndefinded(WebSocketServer.websocket_queue_count)) WebSocketServer.websocket_queue_count.bind({ ...Logger.otel.defaultlabels, clientid: this.id }).update(this._queues.length);
} catch (error) {
throw error
}
finally {
if (queue) semaphore.up();
}
if (queue != null) {
return queue.queue;
}
return null;
} catch (error) {
span?.recordException(error);
throw error;
} finally {
Logger.otel.endSpan(span);
}
}
sleep(ms) {
return new Promise(resolve => {
setTimeout(resolve, ms)
})
}
private ProcessQueue(): void {
if (this.devnull) {
this._receiveQueue = [];
this._sendQueue = [];
return;
}
let username: string = "Unknown";
if (!NoderedUtil.IsNullUndefinded(this.user)) { username = this.user.username; }
let ids: string[] = [];
this._receiveQueue.forEach(msg => {
if (ids.indexOf(msg.id) === -1) { ids.push(msg.id); }
});
ids.forEach(id => {
const msgs: SocketMessage[] = this._receiveQueue.filter(function (msg: SocketMessage): boolean { return msg.id === id; });
if (this._receiveQueue.length > Config.websocket_max_package_count) {
if (Config.websocket_disconnect_out_of_sync) {
Logger.instanse.error("WebSocketServerClient", "ProcessQueue", "[" + username + "/" + this.clientagent + "/" + this.id + "] _receiveQueue containers more than " + Config.websocket_max_package_count + " messages for id '" + id + "', disconnecting");
this.Close();
} else {
Logger.instanse.error("WebSocketServerClient", "ProcessQueue", "[" + username + "/" + this.clientagent + "/" + this.id + "] _receiveQueue containers more than " + Config.websocket_max_package_count + " messages for id '" + id + "' so discarding all !!!!!!!");
this._receiveQueue = this._receiveQueue.filter(function (msg: SocketMessage): boolean { return msg.id !== id; });
}
}
const first: SocketMessage = msgs[0];
if (first.count === msgs.length) {
msgs.sort((a, b) => a.index - b.index);
if (msgs.length === 1) {
this._receiveQueue = this._receiveQueue.filter(function (msg: SocketMessage): boolean { return msg.id !== id; });
const singleresult: Message = Message.frommessage(first, first.data);
singleresult.priority = first.priority;
singleresult.Process(this);
} else {
let buffer: string = "";
msgs.forEach(msg => {
if (!NoderedUtil.IsNullUndefinded(msg.data)) { buffer += msg.data; }
});
this._receiveQueue = this._receiveQueue.filter(function (msg: SocketMessage): boolean { return msg.id !== id; });
const result: Message = Message.frommessage(first, buffer);
result.priority = first.priority;
result.Process(this);
}
}
});
this._sendQueue.forEach(msg => {
let id: string = msg.id;
try {
if (this._socketObject != null) this._socketObject.send(JSON.stringify(msg));
} catch (error) {
Logger.instanse.error("WebSocketServerClient", "ProcessQueue", error);
}
this._sendQueue = this._sendQueue.filter(function (msg: SocketMessage): boolean { return msg.id !== id; });
});
}
public async Send<T>(message: Message): Promise<T> {
return new Promise<T>(async (resolve, reject) => {
this._Send(message, ((msg) => {
if (!NoderedUtil.IsNullUndefinded(msg.error)) { return reject(msg.error); }
resolve(msg);
}).bind(this));
});
}
private _Send(message: Message, cb: QueuedMessageCallback): void {
const messages: string[] = this.chunkString(message.data, Config.websocket_package_size);
if (NoderedUtil.IsNullUndefinded(messages) || messages.length === 0) {
const singlemessage: SocketMessage = SocketMessage.frommessage(message, "", 1, 0);
if (NoderedUtil.IsNullEmpty(message.replyto)) {
this.messageQueue[singlemessage.id] = new QueuedMessage(singlemessage, cb);
WebSocketServer.update_message_queue_count(this);
}
this._sendQueue.push(singlemessage);
return;
}
if (NoderedUtil.IsNullEmpty(message.id)) { message.id = NoderedUtil.GetUniqueIdentifier(); }
for (let i: number = 0; i < messages.length; i++) {
const _message: SocketMessage = SocketMessage.frommessage(message, messages[i], messages.length, i);
this._sendQueue.push(_message);
}
if (NoderedUtil.IsNullEmpty(message.replyto)) {
this.messageQueue[message.id] = new QueuedMessage(message, cb);
WebSocketServer.update_message_queue_count(this);
}
this.ProcessQueue();
}
public chunkString(str: string, length: number): string[] | null {
if (NoderedUtil.IsNullEmpty(str)) { return null; }
// tslint:disable-next-line: quotemark
return str.match(new RegExp('.{1,' + length + '}', 'g'));
}
async Queue(data: string, queuename: string, options: QueueMessageOptions): Promise<any[]> {
const d: any = JSON.parse(data);
const q: QueueMessage = new QueueMessage();
if (this.clientversion == "1.0.80.0" || this.clientversion == "1.0.81.0" || this.clientversion == "1.0.82.0" || this.clientversion == "1.0.83.0" || this.clientversion == "1.0.84.0" || this.clientversion == "1.0.85.0") {
q.data = d.payload;
} else {
q.data = d;
}
q.replyto = options.replyTo;
q.error = d.error;
q.correlationId = options.correlationId; q.queuename = queuename;
q.consumerTag = options.consumerTag;
q.routingkey = options.routingKey;
q.exchange = options.exchange;
let m: Message = Message.fromcommand("queuemessage");
if (NoderedUtil.IsNullEmpty(q.correlationId)) { q.correlationId = m.id; }
m.data = JSON.stringify(q);
const q2 = await this.Send<QueueMessage>(m);
if ((q2 as any).command == "error") throw new Error(q2.data);
return q2.data;
}
async Query<T extends Base>(collection: string, query: any, projection: any = null, orderby: any = { _created: -1 }, top: number = 500, skip: number = 0): Promise<any[]> {
const q: QueryMessage = new QueryMessage();
q.collectionname = collection; q.query = query;
q.projection = projection; q.orderby = orderby; q.top = top; q.skip = skip;
const msg: Message = new Message(); msg.command = "query"; msg.data = JSON.stringify(q);
const q2 = await this.Send<QueryMessage>(msg);
return q2.result as T[];
}
async MapReduce(collection: string, map: mapFunc, reduce: reduceFunc, finalize: finalizeFunc, query: any, out: string | any, scope: any): Promise<any> {
const q: MapReduceMessage = new MapReduceMessage(map, reduce, finalize, query, out);
q.collectionname = collection; q.scope = scope;
const msg: Message = new Message(); msg.command = "mapreduce"; q.out = out;
msg.data = JSONfn.stringify(q);
const q2 = await this.Send<MapReduceMessage>(msg);
return q2.result;
}
async Insert<T extends Base>(collection: string, model: any): Promise<any> {
const q: InsertOneMessage = new InsertOneMessage();
q.collectionname = collection; q.item = model;
const msg: Message = new Message(); msg.command = "insertone"; msg.data = JSONfn.stringify(q);
const q2 = await this.Send<InsertOneMessage>(msg);
return q2.result as T[];
}
async Update<T extends Base>(collection: string, model: any): Promise<any> {
const q: UpdateOneMessage = new UpdateOneMessage();
q.collectionname = collection; q.item = model;
const msg: Message = new Message(); msg.command = "updateone"; msg.data = JSONfn.stringify(q);
const q2 = await this.Send<UpdateOneMessage>(msg);
return q2.result as T[];
}
async UpdateMany<T extends Base>(collection: string, query: any, document: any): Promise<any> {
const q: UpdateManyMessage = new UpdateManyMessage();
q.collectionname = collection; q.item = document; q.query = query;
const msg: Message = new Message(); msg.command = "updateone"; msg.data = JSONfn.stringify(q);
const q2 = await this.Send<UpdateManyMessage>(msg);
return q2.result as T[];
}
async Delete(collection: string, id: any): Promise<void> {
const q: DeleteOneMessage = new DeleteOneMessage();
q.collectionname = collection; q.id = id;
const msg: Message = new Message(); msg.command = "deleteone"; msg.data = JSON.stringify(q);
await this.Send<DeleteOneMessage>(msg);
}
async UnWatch(id: string, jwt: string): Promise<void> {
if (this.watches[id]) {
delete this.watches[id];
}
}
public watches: IHashTable<ClientWatch> = {};
async Watch(aggregates: object[], collectionname: string, jwt: string, id: string = null): Promise<string> {
const stream: clsstream = new clsstream();
stream.id = NoderedUtil.GetUniqueIdentifier();
stream.collectionname = collectionname;
stream.aggregates = aggregates;
if (id == null) id = NoderedUtil.GetUniqueIdentifier();
WebSocketServer.update_mongodb_watch_count(this);
this.watches[id] = {
aggregates, collectionname //, streamid: stream.id
} as ClientWatch;
return id;
}
}
export class ClientWatch {
public streamid: string;
public aggregates: object[];
public collectionname: string;
}