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
348 lines (329 loc) · 12.2 KB
/
Copy pathMessage.ts
File metadata and controls
348 lines (329 loc) · 12.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
import { WebSocketClient, QueuedMessage } from "./WebSocketClient";
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 validate_only: boolean = false;
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 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 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[];
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 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 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) {
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 "pong":
break;
case "signin":
this.Signin(cli);
break;
case "refreshtoken":
this.RefreshToken(cli);
break;
default:
console.error("Unknown command " + command);
this.UnknownCommand(cli);
break;
}
} catch (error) {
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 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];
}
}
}