@futdevpro/nts-dynamo 1.6.50 → 1.6.52

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.
@@ -7,6 +7,7 @@ import * as SocketIO from 'socket.io';
7
7
  */
8
8
  export class DynamoNTS_SocketPresence {
9
9
  issuerId: string;
10
+ serviceName?: string;
10
11
  sockets?: SocketIO.Socket[] = [];
11
12
  onDestroy?: (issuerId: string) => void = id => {};
12
13
 
@@ -24,6 +25,7 @@ export class DynamoNTS_SocketPresence {
24
25
  }
25
26
 
26
27
  this.issuerId = set.issuerId;
28
+ this.serviceName = set.serviceName ?? 'unknown';
27
29
  this.onDestroy = set.onDestroy ?? (id => {});
28
30
 
29
31
  if (!set?.sockets?.length) {
@@ -51,39 +53,45 @@ export class DynamoNTS_SocketPresence {
51
53
 
52
54
  this.sockets.forEach((socket: SocketIO.Socket, index: number) => {
53
55
  if (!socket.connected) {
54
- Dynamo_Log.error(`Emitting event '${event}' on socket(${index}) failed! socket[${socket.id}] is not connected!`);
56
+ Dynamo_Log.error(`Emitting event '${event}' on socket(${this.serviceName})-(${index}) failed! socket[${socket.id}] is not connected! (it will be removed...)`);
55
57
  anyFailed = true;
58
+
56
59
  errors.push(
57
60
  new Dynamo_Error({
58
61
  status: 500,
59
62
  errorCode: 'NTS-SPC-EE1',
60
63
  addECToUserMsg: true,
61
- message: `Emitting event '${event}' on socket(${index}) failed! socket[${socket.id}] is not connected!`,
64
+ message: `Emitting event '${event}' on socket(${this.serviceName})-(${index}) failed! socket[${socket.id}] is not connected!`,
62
65
  userMessage: 'We encountered an unhandled Server Error, please contact the responsible development team.',
63
66
  })
64
67
  );
68
+
65
69
  inactiveSockets.push(socket);
70
+ socket.disconnect(true);
66
71
  }
72
+ });
73
+
74
+ this.sockets = this.sockets.filter(socket => !inactiveSockets.includes(socket) && socket?.connected);
67
75
 
76
+ this.sockets.forEach((socket: SocketIO.Socket, index: number) => {
68
77
  const success: boolean = socket.emit(event, content, error => {
69
- Dynamo_Log.error(`Emitting event '${event}' on socket(${index}) failed! (0)\nerror:`, error);
78
+ Dynamo_Log.error(`Emitting event '${event}' on socket(${this.serviceName})-(${index}) failed! (0)\nerror:`, error);
70
79
  errors.push(error);
71
80
  anyFailed = true;
72
81
  });
82
+
73
83
  if (!success) {
74
- Dynamo_Log.error(`Emitting event '${event}' on socket(${index}) failed! (1)`);
84
+ Dynamo_Log.error(`Emitting event '${event}' on socket(${this.serviceName})-(${index}) failed! (1)`);
75
85
  anyFailed = true;
76
86
  }
77
87
  });
78
88
 
79
89
  if (anyFailed) {
80
- /* this.sockets = this.sockets.filter(socket => !inactiveSockets.includes(socket)); */
81
-
82
90
  throw new Dynamo_Error({
83
91
  status: 500,
84
92
  errorCode: 'NTS-SPC-EE2',
85
93
  addECToUserMsg: true,
86
- message: `Emitting event '${event}' on socket(s) failed!`,
94
+ message: `Emitting event '${event}' on socket(s)(${this.serviceName}) failed!`,
87
95
  userMessage: 'We encountered an unhandled Server Error, please contact the responsible development team.',
88
96
  /* errors: errors, */
89
97
  additionalContent: {
@@ -337,12 +337,7 @@ export class DynamoNTS_DBService<T extends Dynamo_Metadata> {
337
337
 
338
338
  if (0 < dataList.length && typeof dataList[0]._id === 'object') {
339
339
  dataList.forEach((data: T) => {
340
- if (data && (typeof data._id !== 'string' || typeof data._id === 'object')) {
341
- data._id = `${data._id}`;
342
- if (typeof data._id !== 'string' || typeof data._id === 'object') {
343
- Dynamo_Log.error('data._id stringifying failed! Please notfiy the developer! (DynamoNTS_DBService.getAll)')
344
- }
345
- }
340
+ data = this.stringifyDataId(data, 'getAll');
346
341
  });
347
342
  }
348
343
 
@@ -533,7 +528,7 @@ export class DynamoNTS_DBService<T extends Dynamo_Metadata> {
533
528
  });
534
529
  });
535
530
 
536
- data = this.stringifyDataId(data);
531
+ data = this.stringifyDataId(data, 'findOne');
537
532
 
538
533
  return data;
539
534
  }
@@ -827,7 +822,7 @@ export class DynamoNTS_DBService<T extends Dynamo_Metadata> {
827
822
  // ----------------------------------------------------------------------------------
828
823
  // PRIVATE FUNCTIONS
829
824
 
830
- private stringifyDataId(data: T): T {
825
+ private stringifyDataId(data: T, fnName: string): T {
831
826
  if (data && (typeof data._id !== 'string' || typeof data._id === 'object')) {
832
827
  data._id = `${data._id}`;
833
828
 
@@ -835,7 +830,7 @@ export class DynamoNTS_DBService<T extends Dynamo_Metadata> {
835
830
  data = JSON.parse(JSON.stringify(data));
836
831
 
837
832
  if (typeof data._id !== 'string' || typeof data._id === 'object') {
838
- Dynamo_Log.error('data._id stringifying failed! Please notfiy the developers!', new Error());
833
+ Dynamo_Log.error(`data._id stringifying failed! Please notfiy the developers! (${fnName})`, new Error());
839
834
  }
840
835
  }
841
836
  }
@@ -106,7 +106,7 @@ export abstract class DynamoNTS_SocketServerService<
106
106
  this.subscriptionEvent = this.incomingEvents.splice(subscriptionEventIndex)[0];
107
107
  Dynamo_Log.error(
108
108
  `You should not set the subscription event, but the subscriptions tasks, ` +
109
- `in case you need additional steps for your subscripotions.`,
109
+ `in case you need additional steps for your subscriptions.`,
110
110
  `${this.params.name} (${this.params.port})`,
111
111
  );
112
112
  } else {
@@ -126,14 +126,14 @@ export abstract class DynamoNTS_SocketServerService<
126
126
  const disconnectEventIndex = this.incomingEvents.findIndex((event: DynamoNTS_SocketEvent<any>) => event.eventKey === DynamoNTS_SocketEventKey.disconnect);
127
127
  if (0 <= disconnectEventIndex) {
128
128
  this.incomingEvents[disconnectEventIndex].serviceName = this.params.name;
129
- this.incomingEvents[disconnectEventIndex].tasks.push(this.removeSubscription);
129
+ this.incomingEvents[disconnectEventIndex].tasks.push(this.disconnectBaseTask);
130
130
  } else {
131
131
  this.incomingEvents.push(
132
132
  new DynamoNTS_SocketEvent({
133
133
  serviceName: this.params.name,
134
134
  eventKey: DynamoNTS_SocketEventKey.disconnect,
135
135
  tasks: [
136
- this.removeSubscription
136
+ this.disconnectBaseTask
137
137
  ]
138
138
  })
139
139
  );
@@ -191,6 +191,7 @@ export abstract class DynamoNTS_SocketServerService<
191
191
  }
192
192
 
193
193
  const presence: T_Presence = await this.getPresenceFromSubscrioptionEventContent(content, socket);
194
+ presence.serviceName = this.params.name;
194
195
  issuer = presence.issuerId;
195
196
 
196
197
  this.addSocketToPresence(presence);
@@ -200,7 +201,7 @@ export abstract class DynamoNTS_SocketServerService<
200
201
  Dynamo_Log.error(`Emitting subscriptionSuccessful event failed!\nerror:`, error);
201
202
  });
202
203
 
203
- Dynamo_Log.success(`<===> socket subscription successfull (${issuer})`);
204
+ Dynamo_Log.success(`<===> socket(${this.params.name}) subscription successfull (${issuer})`);
204
205
  } catch (error) {
205
206
  Dynamo_Log.error(`Socket Subscription failed: ${this.params.name} (${this.params.port}) will disconnect now...`, error);
206
207
  socket.emit(DynamoNTS_SocketEventKey.error, error);
@@ -221,7 +222,7 @@ export abstract class DynamoNTS_SocketServerService<
221
222
  });
222
223
 
223
224
  if (dynamoNTS_globalSettings.logMainSocketEvents) {
224
- Dynamo_Log.success(`< > Socket (${this.params.name}): new CONNECTION established`);
225
+ Dynamo_Log.success(`< > socket(${this.params.name}): new CONNECTION established`);
225
226
  }
226
227
  } catch (error) {
227
228
  Dynamo_Log.error(`Socket Connection failed: ${this.params.name} (${this.params.port}) will disconnect now...`, error);
@@ -267,6 +268,24 @@ export abstract class DynamoNTS_SocketServerService<
267
268
  }
268
269
  }
269
270
 
271
+ private async disconnectBaseTask(socket: SocketIO.Socket, issuer: string): Promise<void> {
272
+ try {
273
+ await this.removeSubscription(socket, issuer);
274
+
275
+ Dynamo_Log.info(`< x > socket(${this.params.name}) disconnected (${issuer})`);
276
+ } catch (error) {
277
+ throw new Dynamo_Error({
278
+ ...this._getDefaultErrorSettings(
279
+ 'disconnectBaseTask',
280
+ error,
281
+ issuer,
282
+ ),
283
+
284
+ errorCode: 'NTS-SSS-109'
285
+ });
286
+ }
287
+ }
288
+
270
289
  private async removeSubscription(socket: SocketIO.Socket, issuer?: string): Promise<void> {
271
290
  try {
272
291
  const activePresenceIndex: number = this.presences.findIndex((pres: DynamoNTS_SocketPresence) => pres.sockets.includes(socket));
@@ -275,7 +294,7 @@ export abstract class DynamoNTS_SocketServerService<
275
294
  throw new Dynamo_Error({
276
295
  ...this._getDefaultErrorSettings(
277
296
  'sendEventForId',
278
- new Error(`closing Socket does not match any in the activePresences`),
297
+ new Error(`closing socket(${this.params.name}) does not match any in the activePresences`),
279
298
  issuer
280
299
  ),
281
300
 
@@ -301,7 +320,7 @@ export abstract class DynamoNTS_SocketServerService<
301
320
  throw new Dynamo_Error({
302
321
  ...this._getDefaultErrorSettings(
303
322
  'sendEventForId',
304
- new Error(`closing Socket does not match any in the activePresences`),
323
+ new Error(`closing socket(${this.params.name}) does not match any in the activePresences`),
305
324
  issuer
306
325
  ),
307
326
 
@@ -321,6 +340,7 @@ export abstract class DynamoNTS_SocketServerService<
321
340
  }
322
341
 
323
342
  socket.disconnect();
343
+ Dynamo_Log.info(`<=x=> socket(${this.params.name}) unsubscription successfull (${issuer})`);
324
344
  } catch (error) {
325
345
  throw new Dynamo_Error({
326
346
  ...this._getDefaultErrorSettings(
@@ -330,14 +350,14 @@ export abstract class DynamoNTS_SocketServerService<
330
350
  ),
331
351
 
332
352
  errorCode: 'NTS-SSS-200',
333
- message: `Socket Subscription Removal failed: ${this.params?.name} (${this.params?.port})`,
353
+ message: `socket(${this.params.name}) Subscription Removal failed: ${this.params?.name}`,
334
354
  });
335
355
  }
336
356
  }
337
357
 
338
358
  emitServerEvent(event: string, content: any): void {
339
359
  try {
340
- Dynamo_Log.log(` <=-- emitting server socket event: ${event} (${this.params.name})`);
360
+ Dynamo_Log.log(` <=-- emitting server socket(${this.params.name}) event: ${event}`);
341
361
  this.openSocketServer.emit(event, content, error => {
342
362
  Dynamo_Log.error(`Emitting server event '${event}' failed!\nerror:`, error);
343
363
  });
@@ -349,7 +369,7 @@ export abstract class DynamoNTS_SocketServerService<
349
369
  ),
350
370
 
351
371
  errorCode: 'NTS-SSS-500',
352
- message: `Socket Event Emit (${event}) failed: ${this.params?.name} (${this.params?.port})`,
372
+ message: `socket(${this.params.name}) Event Emit (${event}) failed: ${this.params?.name}`,
353
373
  });
354
374
  }
355
375
  }
@@ -365,7 +385,7 @@ export abstract class DynamoNTS_SocketServerService<
365
385
  ),
366
386
 
367
387
  errorCode: 'NTS-SSS-600',
368
- message: `Socket ID Subscription Check (${id}) failed: ${this.params?.name} (${this.params?.port})`,
388
+ message: `socket(${this.params.name}) ID Subscription Check (${id}) failed`,
369
389
  });
370
390
  }
371
391
  }
@@ -382,7 +402,7 @@ export abstract class DynamoNTS_SocketServerService<
382
402
  ),
383
403
 
384
404
  errorCode: 'NTS-SSS-700',
385
- message: `Socket Error Emit (id: ${presenceIssuerId}) failed: ${this.params?.name} (${this.params?.port})`,
405
+ message: `socket(${this.params.name}) Error Emit (id: ${presenceIssuerId}) failed`,
386
406
  });
387
407
  }
388
408
  }
@@ -407,9 +427,9 @@ export abstract class DynamoNTS_SocketServerService<
407
427
  presence.emitEvent(event, content);
408
428
 
409
429
  if (dynamoNTS_globalSettings.logSocketEventContent) {
410
- Dynamo_Log.log(` <--- emitted socket event for presence: ${event} (${this.params.name}, presenceId: ${id}) \ncontent:`, content);
430
+ Dynamo_Log.success(` <--- emitted socket(${this.params.name}) event for presence: ${event}, presenceId: ${id}) \ncontent:`, content);
411
431
  } else {
412
- Dynamo_Log.log(` <--- emitted socket event for presence: ${event} (${this.params.name}, presenceId: ${id})`);
432
+ Dynamo_Log.success(` <--- emitted socket(${this.params.name}) event for presence: ${event}, presenceId: ${id})`);
413
433
  }
414
434
  } catch (error) {
415
435
  try {
@@ -425,7 +445,7 @@ export abstract class DynamoNTS_SocketServerService<
425
445
  throw new Dynamo_Error({
426
446
  ...this._getDefaultErrorSettings(
427
447
  'sendEventForId',
428
- new Error(`No active socket with this specific ID: ${id}`),
448
+ new Error(`No active socket(${this.params.name}) with this specific ID: ${id}`),
429
449
  content?.source
430
450
  ),
431
451
 
@@ -440,8 +460,8 @@ export abstract class DynamoNTS_SocketServerService<
440
460
 
441
461
  errorCode: 'NTS-SSS-310',
442
462
  message:
443
- `Error handling of inactive sockets failed!` +
444
- `\n(Socket Event Emit for id (${id}, ${event}) failed: ${this.params?.name} (${this.params?.port}))`,
463
+ `Error handling of inactive sockets(${this.params.name}) failed!` +
464
+ `\n(Socket Event Emit for id (${id}, ${event}) failed)`,
445
465
  });
446
466
  }
447
467
 
@@ -449,24 +469,25 @@ export abstract class DynamoNTS_SocketServerService<
449
469
  ...this._getDefaultErrorSettings('sendEventForId', error, content?.source),
450
470
 
451
471
  errorCode: 'NTS-SSS-310',
452
- message: `Socket Event Emit for id (${id}, ${event}) failed: ${this.params?.name} (${this.params?.port})`,
472
+ message: `socket(${this.params.name}) Event Emit for id (${id}, ${event}) failed`,
453
473
  });
454
474
  }
455
475
  }
456
476
 
457
477
  broadcastEvent(event: string, content: any): void {
458
478
  try {
459
- Dynamo_Log.log(` <==-- broadcasting socket event: ${event} (${this.params.name})`);
479
+ Dynamo_Log.log(` <==-- broadcasting socket(${this.params.name}) event: ${event}`);
480
+
460
481
  this.presences.forEach((presence: DynamoNTS_SocketPresence) => {
461
482
  presence.emitEvent(event, content);
462
483
  });
463
484
  } catch (error) {
464
- Dynamo_Log.error(`Socket Event Broadcast (${event}) failed: ${this.params?.name} (${this.params?.port})`, error);
485
+ Dynamo_Log.error(`socket(${this.params.name}) Event Broadcast (${event}) failed`, error);
465
486
  throw new Dynamo_Error({
466
487
  ...this._getDefaultErrorSettings('broadcastEvent', error),
467
488
 
468
489
  errorCode: 'NTS-SSS-400',
469
- message: `Socket Event Broadcast (${event}) failed: ${this.params?.name} (${this.params?.port})`,
490
+ message: `socket(${this.params.name}) Event Broadcast (${event}) failed`,
470
491
  });
471
492
  }
472
493
  }