@futdevpro/nts-dynamo 1.6.48 → 1.6.50

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.
@@ -14,9 +14,9 @@ export const dynamoNTS_globalSettings: DynamoNTS_GlobalSettings = {
14
14
  logRequestsContent: false,
15
15
  logResponseContent: false,
16
16
 
17
- logMainSocketEvents: true,
18
17
  logAllSocketEvent: true,
19
- logSocketEventContent: true,
18
+ logMainSocketEvents: true,
19
+ logSocketEventContent: false,
20
20
 
21
21
  logDetailedApiEvents: true,
22
22
  };
@@ -13,6 +13,7 @@ export type DynamoNTS_SocketEventTask<T> = (content?: T, issuer?: string) => Pro
13
13
  export class DynamoNTS_SocketEvent<T>{
14
14
  socketName?: string;
15
15
  eventKey: DynamoNTS_SocketEventKey | string;
16
+ serviceName?: string;
16
17
 
17
18
  preProcessess?: DynamoNTS_SocketEventPreprocessTask<any, any>[];
18
19
  tasks?: DynamoNTS_SocketEventTask<T>[];
@@ -32,6 +33,10 @@ export class DynamoNTS_SocketEvent<T>{
32
33
  * connection, connect, disconnect, message, error, or anything else
33
34
  */
34
35
  eventKey: DynamoNTS_SocketEventKey | string,
36
+ /**
37
+ * serviceName is the name of the service that will be used for logging
38
+ */
39
+ serviceName?: string,
35
40
  /**
36
41
  * preprocesses are the functions the service needs to run before the actual function,
37
42
  * these can be used for authentications, or translating data
@@ -56,6 +61,7 @@ export class DynamoNTS_SocketEvent<T>{
56
61
  try {
57
62
  this.socketName = set.name ?? set.eventKey;
58
63
  this.eventKey = set.eventKey;
64
+ this.serviceName = set.serviceName ?? 'UnkwownSocketService';
59
65
 
60
66
  this.preProcessess = set.preProcessess ?? [];
61
67
  this.tasks = set.tasks ?? [];
@@ -78,7 +84,7 @@ export class DynamoNTS_SocketEvent<T>{
78
84
  this.logEventContent = set.logEventContent !== undefined ? set.logEventContent : dynamoNTS_globalSettings.logSocketEventContent;
79
85
  } catch (error) {
80
86
  Dynamo_Log.error(
81
- `\nSocket Event params setup failed (${this.socketName}): ${set.eventKey}`, error);
87
+ `\nSocket Event params setup failed (${this.serviceName}): ${set.eventKey}`, error);
82
88
  throw error;
83
89
  }
84
90
  }
@@ -90,12 +96,12 @@ export class DynamoNTS_SocketEvent<T>{
90
96
  private async getPreLog(content: T, issuer?: string): Promise<void> {
91
97
  try {
92
98
  if (this.logEventContent && this.eventKey !== DynamoNTS_SocketEventKey.connection) {
93
- Dynamo_Log.log(`---> incoming socket(${this.socketName}) event: ${this.eventKey};\ncontent:`, content);
99
+ Dynamo_Log.log(`---> incoming socket(${this.serviceName}) event: ${this.eventKey};\ncontent:`, content);
94
100
  } else {
95
- Dynamo_Log.log(`---> incoming socket(${this.socketName}) event: ${this.eventKey}...`);
101
+ Dynamo_Log.log(`---> incoming socket(${this.serviceName}) event: ${this.eventKey}...`);
96
102
  }
97
103
  } catch (error) {
98
- Dynamo_Log.error(`PreLog failed... (socket: ${this.socketName})`, error);
104
+ Dynamo_Log.error(`PreLog failed... (socket: ${this.serviceName})`, error);
99
105
  }
100
106
  }
101
107
 
@@ -117,7 +123,7 @@ export class DynamoNTS_SocketEvent<T>{
117
123
  await task(content, issuer);
118
124
  });
119
125
  } catch (error) {
120
- Dynamo_Log.error(`Socket Event tasks failed to execute on ${this.socketName}.... (${this.eventKey})`, error, 'content:', content);
126
+ Dynamo_Log.error(`Socket Event tasks failed to execute on ${this.serviceName}.... (${this.eventKey})`, error, 'content:', content);
121
127
  }
122
128
  }
123
129
  }
@@ -45,11 +45,11 @@ export interface DynamoNTS_GlobalSettings {
45
45
  /**
46
46
  * this is an application wide default setting for socket debug logs
47
47
  */
48
- logMainSocketEvents?: boolean;
48
+ logAllSocketEvent?: boolean;
49
49
  /**
50
50
  * this is an application wide default setting for socket debug logs
51
51
  */
52
- logAllSocketEvent?: boolean;
52
+ logMainSocketEvents?: boolean;
53
53
  /**
54
54
  * this is an application wide default setting for socket debug logs
55
55
  */
@@ -103,7 +103,7 @@ export class DynamoNTS_DBService<T extends Dynamo_Metadata> {
103
103
  if (typeof newData._id !== 'string' || typeof newData._id === 'object') {
104
104
  newData._id = `${newData._id}`;
105
105
  if (typeof data._id !== 'string' || typeof data._id === 'object') {
106
- Dynamo_Log.error('data._id stringifying failed! Please notfiy the developer! (DynamoNTS_DBService.findOne)')
106
+ Dynamo_Log.error('data._id stringifying failed! Please notfiy the developer! (DynamoNTS_DBService.createData)')
107
107
  }
108
108
  }
109
109
  data._id = newData._id;
@@ -151,7 +151,7 @@ export class DynamoNTS_DBService<T extends Dynamo_Metadata> {
151
151
  if (typeof newData._id !== 'string' || typeof newData._id === 'object') {
152
152
  newData._id = `${newData._id}`;
153
153
  if (typeof data._id !== 'string' || typeof data._id === 'object') {
154
- Dynamo_Log.error('data._id stringifying failed! Please notfiy the developer! (DynamoNTS_DBService.findOne)')
154
+ Dynamo_Log.error('data._id stringifying failed! Please notfiy the developer! (DynamoNTS_DBService.modifyData)')
155
155
  }
156
156
  }
157
157
  data._id = newData._id;
@@ -180,7 +180,7 @@ export class DynamoNTS_DBService<T extends Dynamo_Metadata> {
180
180
  if (data && (typeof data._id !== 'string' || typeof data._id === 'object')) {
181
181
  data._id = `${data._id}`;
182
182
  if (typeof data._id !== 'string' || typeof data._id === 'object') {
183
- Dynamo_Log.error('data._id stringifying failed! Please notfiy the developer! (DynamoNTS_DBService.findOne)')
183
+ Dynamo_Log.error('data._id stringifying failed! Please notfiy the developer! (DynamoNTS_DBService.getDataById)')
184
184
  }
185
185
  }
186
186
  return data;
@@ -220,7 +220,7 @@ export class DynamoNTS_DBService<T extends Dynamo_Metadata> {
220
220
  if (data && (typeof data._id !== 'string' || typeof data._id === 'object')) {
221
221
  data._id = `${data._id}`;
222
222
  if (typeof data._id !== 'string' || typeof data._id === 'object') {
223
- Dynamo_Log.error('data._id stringifying failed! Please notfiy the developer! (DynamoNTS_DBService.findOne)')
223
+ Dynamo_Log.error('data._id stringifying failed! Please notfiy the developer! (DynamoNTS_DBService.getDataByDependencyId)')
224
224
  }
225
225
  }
226
226
  return data;
@@ -262,7 +262,7 @@ export class DynamoNTS_DBService<T extends Dynamo_Metadata> {
262
262
  if (data && (typeof data._id !== 'string' || typeof data._id === 'object')) {
263
263
  data._id = `${data._id}`;
264
264
  if (typeof data._id !== 'string' || typeof data._id === 'object') {
265
- Dynamo_Log.error('data._id stringifying failed! Please notfiy the developer! (DynamoNTS_DBService.findOne)')
265
+ Dynamo_Log.error('data._id stringifying failed! Please notfiy the developer! (DynamoNTS_DBService.getDataListByDependencyId)')
266
266
  }
267
267
  }
268
268
  });
@@ -307,7 +307,7 @@ export class DynamoNTS_DBService<T extends Dynamo_Metadata> {
307
307
  if (data && (typeof data._id !== 'string' || typeof data._id === 'object')) {
308
308
  data._id = `${data._id}`;
309
309
  if (typeof data._id !== 'string' || typeof data._id === 'object') {
310
- Dynamo_Log.error('data._id stringifying failed! Please notfiy the developer! (DynamoNTS_DBService.findOne)')
310
+ Dynamo_Log.error('data._id stringifying failed! Please notfiy the developer! (DynamoNTS_DBService.getDataListByDependencyIds)')
311
311
  }
312
312
  }
313
313
  });
@@ -340,7 +340,7 @@ export class DynamoNTS_DBService<T extends Dynamo_Metadata> {
340
340
  if (data && (typeof data._id !== 'string' || typeof data._id === 'object')) {
341
341
  data._id = `${data._id}`;
342
342
  if (typeof data._id !== 'string' || typeof data._id === 'object') {
343
- Dynamo_Log.error('data._id stringifying failed! Please notfiy the developer! (DynamoNTS_DBService.findOne)')
343
+ Dynamo_Log.error('data._id stringifying failed! Please notfiy the developer! (DynamoNTS_DBService.getAll)')
344
344
  }
345
345
  }
346
346
  });
@@ -481,7 +481,7 @@ export class DynamoNTS_DBService<T extends Dynamo_Metadata> {
481
481
  if (data && (typeof data._id !== 'string' || typeof data._id === 'object')) {
482
482
  data._id = `${data._id}`;
483
483
  if (typeof data._id !== 'string' || typeof data._id === 'object') {
484
- Dynamo_Log.error('data._id stringifying failed! Please notfiy the developer! (DynamoNTS_DBService.findOne)')
484
+ Dynamo_Log.error('data._id stringifying failed! Please notfiy the developer! (DynamoNTS_DBService.searchData)')
485
485
  }
486
486
  }
487
487
  });
@@ -583,7 +583,7 @@ export class DynamoNTS_DBService<T extends Dynamo_Metadata> {
583
583
  if (data && (typeof data._id !== 'string' || typeof data._id === 'object')) {
584
584
  data._id = `${data._id}`;
585
585
  if (typeof data._id !== 'string' || typeof data._id === 'object') {
586
- Dynamo_Log.error('data._id stringifying failed! Please notfiy the developer! (DynamoNTS_DBService.findOne)')
586
+ Dynamo_Log.error('data._id stringifying failed! Please notfiy the developer! (DynamoNTS_DBService.find)')
587
587
  }
588
588
  }
589
589
  });
@@ -645,7 +645,7 @@ export class DynamoNTS_DBService<T extends Dynamo_Metadata> {
645
645
  if (data && (typeof data._id !== 'string' || typeof data._id === 'object')) {
646
646
  data._id = `${data._id}`;
647
647
  if (typeof data._id !== 'string' || typeof data._id === 'object') {
648
- Dynamo_Log.error('data._id stringifying failed! Please notfiy the developer! (DynamoNTS_DBService.findOne)')
648
+ Dynamo_Log.error('data._id stringifying failed! Please notfiy the developer! (DynamoNTS_DBService.findWithPaging)')
649
649
  }
650
650
  }
651
651
  });
@@ -682,7 +682,7 @@ export class DynamoNTS_DBService<T extends Dynamo_Metadata> {
682
682
  if (typeof newData._id !== 'string' || typeof newData._id === 'object') {
683
683
  newData._id = `${newData._id}`;
684
684
  if (typeof newData._id !== 'string' || typeof newData._id === 'object') {
685
- Dynamo_Log.error('newData._id stringifying failed! Please notfiy the developer! (DynamoNTS_DBService.findOne)')
685
+ Dynamo_Log.error('newData._id stringifying failed! Please notfiy the developer! (DynamoNTS_DBService.findByIdAndUpdate)')
686
686
  }
687
687
  }
688
688
 
@@ -29,7 +29,6 @@ export abstract class DynamoNTS_SocketClientService extends DynamoNTS_SingletonS
29
29
 
30
30
  private defaultEvents: DynamoNTS_SocketEvent<any>[] = [
31
31
  new DynamoNTS_SocketEvent<any>({
32
- name: 'connect',
33
32
  eventKey: DynamoNTS_SocketEventKey.connect,
34
33
  tasks: [
35
34
  async () => {
@@ -38,8 +37,8 @@ export abstract class DynamoNTS_SocketClientService extends DynamoNTS_SingletonS
38
37
  }
39
38
  ]
40
39
  }),
40
+
41
41
  new DynamoNTS_SocketEvent<any>({
42
- name: 'disconnect',
43
42
  eventKey: DynamoNTS_SocketEventKey.disconnect,
44
43
  tasks: [
45
44
  async () => {
@@ -53,8 +52,17 @@ export abstract class DynamoNTS_SocketClientService extends DynamoNTS_SingletonS
53
52
  }
54
53
  ]
55
54
  }),
55
+
56
+ new DynamoNTS_SocketEvent<any>({
57
+ eventKey: DynamoNTS_SocketEventKey.subscriptionSuccessful,
58
+ tasks: [
59
+ async () => {
60
+ Dynamo_Log.success(`Socket Client subscription successful: ${this.params.name}`);
61
+ }
62
+ ]
63
+ }),
64
+
56
65
  new DynamoNTS_SocketEvent<any>({
57
- name: 'error',
58
66
  eventKey: DynamoNTS_SocketEventKey.error,
59
67
  tasks: [
60
68
  async (content: any) => {
@@ -96,7 +104,8 @@ export abstract class DynamoNTS_SocketClientService extends DynamoNTS_SingletonS
96
104
 
97
105
  private async setupDefaultEvents(): Promise<void> {
98
106
  try {
99
- if (this.logFn) console.log('\nFn:. setupDefaultEvents')
107
+ if (this.logFn) console.log('\nFn:. setupDefaultEvents');
108
+
100
109
  this.defaultEvents.forEach((defaultEvent: DynamoNTS_SocketEvent<any>) => {
101
110
  const eventDeclared = this.incomingEvents.find((event: DynamoNTS_SocketEvent<any>) => event.eventKey === defaultEvent.eventKey);
102
111
  if (!eventDeclared) {
@@ -170,7 +179,7 @@ export abstract class DynamoNTS_SocketClientService extends DynamoNTS_SingletonS
170
179
  */
171
180
  protected async emitEvent(eventType: string, content: any): Promise<void> {
172
181
  try {
173
- Dynamo_Log.log(`\n<-=- outgoing ${this.name} socket event: ${eventType}`);
182
+ Dynamo_Log.log(`<-=- outgoing ${this.name} socket event: ${eventType}`);
174
183
  this.socket.emit(eventType, content);
175
184
  } catch (error) {
176
185
  Dynamo_Log.error(`Socket Client Service Emit Event failed: ${this.params?.name} (${this.params?.port})`, error);
@@ -88,6 +88,7 @@ export abstract class DynamoNTS_SocketServerService<
88
88
  */
89
89
  private async prepareEvents(): Promise<void> {
90
90
  try {
91
+ /** Setup connect event */
91
92
  const connectEventIndex = this.incomingEvents.findIndex((event: DynamoNTS_SocketEvent<any>) => event.eventKey === DynamoNTS_SocketEventKey.connection);
92
93
  if (0 <= connectEventIndex) {
93
94
  this.connectEvent = this.incomingEvents.splice(connectEventIndex)[0];
@@ -96,8 +97,9 @@ export abstract class DynamoNTS_SocketServerService<
96
97
  eventKey: DynamoNTS_SocketEventKey.connection,
97
98
  });
98
99
  }
99
- this.connectEvent.socketName = this.params.name;
100
+ this.connectEvent.serviceName = this.params.name;
100
101
 
102
+ /** Setup subscription event */
101
103
  if (this.getPresenceFromSubscrioptionEventContent) {
102
104
  const subscriptionEventIndex = this.incomingEvents.findIndex((event: DynamoNTS_SocketEvent<any>) => event.eventKey === DynamoNTS_SocketEventKey.subscribe);
103
105
  if (0 <= subscriptionEventIndex) {
@@ -118,13 +120,17 @@ export abstract class DynamoNTS_SocketServerService<
118
120
  if (this.getSubscriptionTasks) {
119
121
  this.subscriptionEvent.tasks.push(...this.getSubscriptionTasks());
120
122
  }
123
+ this.subscriptionEvent.serviceName = this.params.name;
121
124
 
125
+ /** Setup disconnect event */
122
126
  const disconnectEventIndex = this.incomingEvents.findIndex((event: DynamoNTS_SocketEvent<any>) => event.eventKey === DynamoNTS_SocketEventKey.disconnect);
123
127
  if (0 <= disconnectEventIndex) {
128
+ this.incomingEvents[disconnectEventIndex].serviceName = this.params.name;
124
129
  this.incomingEvents[disconnectEventIndex].tasks.push(this.removeSubscription);
125
130
  } else {
126
131
  this.incomingEvents.push(
127
132
  new DynamoNTS_SocketEvent({
133
+ serviceName: this.params.name,
128
134
  eventKey: DynamoNTS_SocketEventKey.disconnect,
129
135
  tasks: [
130
136
  this.removeSubscription
@@ -133,9 +139,11 @@ export abstract class DynamoNTS_SocketServerService<
133
139
  );
134
140
  }
135
141
 
142
+ /** Setup error event */
136
143
  const errorEventIndex = this.incomingEvents.findIndex((event: DynamoNTS_SocketEvent<any>) => event.eventKey === DynamoNTS_SocketEventKey.error);
137
144
  if (errorEventIndex == -1 && this.errorHandling) {
138
145
  this.incomingEvents.push(new DynamoNTS_SocketEvent<T_ErrorContent>({
146
+ serviceName: this.params.name,
139
147
  eventKey: DynamoNTS_SocketEventKey.error,
140
148
  tasks: [
141
149
  this.errorHandling
@@ -148,8 +156,9 @@ export abstract class DynamoNTS_SocketServerService<
148
156
  Dynamo_Log.error('getPresenceFromSubscrioptionEventContent is not set', `${this.params.name} (${this.params.port})`);
149
157
  }
150
158
 
159
+ /** Set incoming events serviceName */
151
160
  this.incomingEvents.forEach((event: DynamoNTS_SocketEvent<any>) => {
152
- event.socketName = this.params.name;
161
+ event.serviceName = this.params.name;
153
162
  });
154
163
  } catch (error) {
155
164
  Dynamo_Log.error(`Socket Server Service - Event Preparation setup failed: ${this.params?.name} (${this.params?.port})`, error);
@@ -176,9 +185,9 @@ export abstract class DynamoNTS_SocketServerService<
176
185
  * to be able to check content before getPresenceFromSubscrioptionEventContent
177
186
  */
178
187
  if (dynamoNTS_globalSettings.logMainSocketEvents || dynamoNTS_globalSettings.logAllSocketEvent || dynamoNTS_globalSettings.logSocketEventContent) {
179
- Dynamo_Log.log(`---> incoming socket(${this.params.name}) event: ${DynamoNTS_SocketEventKey.subscribe};\ncontent:`, content);
188
+ Dynamo_Log.log(`---> incoming socket(${this.params.name}) event: ${DynamoNTS_SocketEventKey.subscribe};\ncontent:`, content);
180
189
  } else {
181
- Dynamo_Log.log(`---> incoming socket(${this.params.name}) event: ${DynamoNTS_SocketEventKey.subscribe}`);
190
+ Dynamo_Log.log(`---> incoming socket(${this.params.name}) event: ${DynamoNTS_SocketEventKey.subscribe}`);
182
191
  }
183
192
 
184
193
  const presence: T_Presence = await this.getPresenceFromSubscrioptionEventContent(content, socket);
@@ -191,7 +200,7 @@ export abstract class DynamoNTS_SocketServerService<
191
200
  Dynamo_Log.error(`Emitting subscriptionSuccessful event failed!\nerror:`, error);
192
201
  });
193
202
 
194
- Dynamo_Log.success(`<===> socket subscription successfull (${issuer})`);
203
+ Dynamo_Log.success(`<===> socket subscription successfull (${issuer})`);
195
204
  } catch (error) {
196
205
  Dynamo_Log.error(`Socket Subscription failed: ${this.params.name} (${this.params.port}) will disconnect now...`, error);
197
206
  socket.emit(DynamoNTS_SocketEventKey.error, error);
@@ -212,7 +221,7 @@ export abstract class DynamoNTS_SocketServerService<
212
221
  });
213
222
 
214
223
  if (dynamoNTS_globalSettings.logMainSocketEvents) {
215
- Dynamo_Log.success(`< > Socket (${this.params.name}): new CONNECTION established`);
224
+ Dynamo_Log.success(`< > Socket (${this.params.name}): new CONNECTION established`);
216
225
  }
217
226
  } catch (error) {
218
227
  Dynamo_Log.error(`Socket Connection failed: ${this.params.name} (${this.params.port}) will disconnect now...`, error);
@@ -328,7 +337,7 @@ export abstract class DynamoNTS_SocketServerService<
328
337
 
329
338
  emitServerEvent(event: string, content: any): void {
330
339
  try {
331
- Dynamo_Log.log(` <=-- emitting server socket event: ${event} (${this.params.name})`);
340
+ Dynamo_Log.log(` <=-- emitting server socket event: ${event} (${this.params.name})`);
332
341
  this.openSocketServer.emit(event, content, error => {
333
342
  Dynamo_Log.error(`Emitting server event '${event}' failed!\nerror:`, error);
334
343
  });
@@ -382,14 +391,7 @@ export abstract class DynamoNTS_SocketServerService<
382
391
  try {
383
392
  const presence: DynamoNTS_SocketPresence = this.presences.find((pres: DynamoNTS_SocketPresence) => pres.issuerId === id);
384
393
 
385
- if (presence) {
386
- presence.emitEvent(event, content);
387
- if (dynamoNTS_globalSettings.logSocketEventContent) {
388
- Dynamo_Log.log(` <--- emitted socket event for presence: ${event} (${this.params.name}, presenceId: ${id}) \ncontent:`, content);
389
- } else {
390
- Dynamo_Log.log(` <--- emitted socket event for presence: ${event} (${this.params.name}, presenceId: ${id})`);
391
- }
392
- } else {
394
+ if (!presence) {
393
395
  throw new Dynamo_Error({
394
396
  ...this._getDefaultErrorSettings(
395
397
  'sendEventForId',
@@ -401,8 +403,15 @@ export abstract class DynamoNTS_SocketServerService<
401
403
  errorCode: 'NTS-SSS-301',
402
404
  });
403
405
  }
404
- } catch (error) {
405
406
 
407
+ presence.emitEvent(event, content);
408
+
409
+ if (dynamoNTS_globalSettings.logSocketEventContent) {
410
+ Dynamo_Log.log(` <--- emitted socket event for presence: ${event} (${this.params.name}, presenceId: ${id}) \ncontent:`, content);
411
+ } else {
412
+ Dynamo_Log.log(` <--- emitted socket event for presence: ${event} (${this.params.name}, presenceId: ${id})`);
413
+ }
414
+ } catch (error) {
406
415
  try {
407
416
  if (error.flag.includes('DYNAMO') && error?.accitionalInfo?.inactiveSockets) {
408
417
  const sockets: SocketIO.Socket[] = error.accitionalInfo.inactiveSockets;
@@ -410,6 +419,20 @@ export abstract class DynamoNTS_SocketServerService<
410
419
  await Dynamo_Array.asyncForEach(sockets, async (socket: SocketIO.Socket) => {
411
420
  await this.removeSubscription(socket);
412
421
  });
422
+
423
+ const presence: DynamoNTS_SocketPresence = this.presences.find((pres: DynamoNTS_SocketPresence) => pres.issuerId === id);
424
+ if (!presence) {
425
+ throw new Dynamo_Error({
426
+ ...this._getDefaultErrorSettings(
427
+ 'sendEventForId',
428
+ new Error(`No active socket with this specific ID: ${id}`),
429
+ content?.source
430
+ ),
431
+
432
+ status: 404,
433
+ errorCode: 'NTS-SSS-302',
434
+ });
435
+ }
413
436
  }
414
437
  } catch (error) {
415
438
  throw new Dynamo_Error({