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
359 lines (339 loc) · 13.1 KB
/
Copy pathMessage.ts
File metadata and controls
359 lines (339 loc) · 13.1 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
function isNumber(value: string | number): boolean {
return ((value != null) && !isNaN(Number(value.toString())));
}
module openflow {
"use strict";
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;
}
}
export class SigninMessage {
public error: string;
public impersonate: string;
public realm: string;
public firebasetoken: string;
public onesignalid: string;
public device: any;
public gpslocation: any;
public username: string;
public password: string;
public user: TokenUser;
public jwt: string;
public rawAssertion: string;
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 ListCollectionsMessage {
public error: string;
public jwt: string;
public result: any;
}
export class DropCollectionMessage {
public error: string;
public jwt: string;
public collectionname: string;
}
export class QueryMessage {
public error: string;
public query: any;
public projection: Object;
public top: number;
public skip: number;
public orderby: Object | string;
public collectionname: string;
public result: any[];
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 MapReduceMessage implements IReplyMessage {
public error: string;
public jwt: string;
public scope: any;
public collectionname: string;
public result: any[];
constructor(public map: mapFunc, public reduce: reduceFunc, public finalize: finalizeFunc, public query: any, public out: string) {
}
static assign<T>(o: any): MapReduceMessage {
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 AggregateMessage {
public error: string;
public jwt: any;
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: any;
public item: object;
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: any;
public item: object;
public collectionname: string;
public result: 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 DeleteOneMessage {
public error: string;
public jwt: any;
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 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;
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 GetNoderedInstanceMessage {
public error: string;
public jwt: any;
public name: string;
public result: 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 GetNoderedInstanceLogMessage {
public error: string;
public jwt: any;
public name: string;
public result: string;
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 EnsureNoderedInstanceMessage {
public error: string;
public jwt: any;
public name: string;
static assign(o: any): EnsureNoderedInstanceMessage {
if (typeof o === "string" || o instanceof String) {
return Object.assign(new EnsureNoderedInstanceMessage(), JSON.parse(o.toString()));
}
return Object.assign(new EnsureNoderedInstanceMessage(), o);
}
}
export class DeleteNoderedInstanceMessage {
public error: string;
public jwt: any;
public name: string;
static assign(o: any): DeleteNoderedInstanceMessage {
if (typeof o === "string" || o instanceof String) {
return Object.assign(new DeleteNoderedInstanceMessage(), JSON.parse(o.toString()));
}
return Object.assign(new DeleteNoderedInstanceMessage(), o);
}
}
export class RestartNoderedInstanceMessage {
public error: string;
public jwt: any;
public name: string;
static assign(o: any): RestartNoderedInstanceMessage {
if (typeof o === "string" || o instanceof String) {
return Object.assign(new RestartNoderedInstanceMessage(), JSON.parse(o.toString()));
}
return Object.assign(new RestartNoderedInstanceMessage(), o);
}
}
export class StartNoderedInstanceMessage {
public error: string;
public jwt: any;
public name: string;
static assign(o: any): StartNoderedInstanceMessage {
if (typeof o === "string" || o instanceof String) {
return Object.assign(new StartNoderedInstanceMessage(), JSON.parse(o.toString()));
}
return Object.assign(new StartNoderedInstanceMessage(), o);
}
}
export class StopNoderedInstanceMessage {
public error: string;
public jwt: any;
public name: string;
static assign(o: any): StopNoderedInstanceMessage {
if (typeof o === "string" || o instanceof String) {
return Object.assign(new StopNoderedInstanceMessage(), JSON.parse(o.toString()));
}
return Object.assign(new StopNoderedInstanceMessage(), o);
}
}
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) {
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];
}
return;
}
}
switch (command) {
case "ping":
this.Ping(cli);
break;
case "refreshtoken":
this.RefreshToken(cli);
break;
case "queuemessage":
this.QueueMessage(cli);
break;
// case "signin":
// this.Signin(cli);
// break;
default:
console.error("Unknown command " + command);
this.UnknownCommand(cli);
break;
}
} catch (error) {
console.log(this);
console.error(error);
}
}
public async Send(cli: WebSocketClient): Promise<void> {
await cli.Send(this);
}
private async UnknownCommand(cli: WebSocketClient): Promise<void> {
this.Reply("error");
this.data = "Unknown command";
await this.Send(cli);
}
private async Ping(cli: WebSocketClient): Promise<void> {
this.Reply("pong");
await this.Send(cli);
}
public Reply(command: string): void {
this.command = command;
this.replyto = this.id;
this.id = Math.random().toString(36).substr(2, 9);
}
private RefreshToken(cli: WebSocketClient): void {
var msg: SigninMessage = SigninMessage.assign(this.data);
cli.jwt = msg.jwt;
cli.user = msg.user;
console.debug("Message::RefreshToken: Updated jwt");
}
private QueueMessage(cli: WebSocketClient): void {
var msg: QueueMessage = QueueMessage.assign(this.data);
msg.replyto = msg.correlationId;
cli.$rootScope.$broadcast("queuemessage", msg);
this.Reply("queuemessage");
this.Send(cli);
}
}
}