aotrautils-srv 0.0.1477 → 0.0.1479

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.
@@ -1,6 +1,6 @@
1
1
 
2
2
 
3
- /*utils COMMONS library associated with aotra version : «1_29072022-2359 (26/04/2025-16:15:53)»*/
3
+ /*utils COMMONS library associated with aotra version : «1_29072022-2359 (27/04/2025-01:02:04)»*/
4
4
  /*-----------------------------------------------------------------------------*/
5
5
 
6
6
 
@@ -4894,7 +4894,7 @@ AOTRAUTILS_LIB_IS_LOADED=true;
4894
4894
 
4895
4895
 
4896
4896
 
4897
- /*utils AI library associated with aotra version : «1_29072022-2359 (26/04/2025-16:15:53)»*/
4897
+ /*utils AI library associated with aotra version : «1_29072022-2359 (27/04/2025-01:02:04)»*/
4898
4898
  /*-----------------------------------------------------------------------------*/
4899
4899
 
4900
4900
 
@@ -5040,7 +5040,7 @@ getOpenAIAPIClient=(modelName, apiURL, agentRole, defaultPrompt)=>{
5040
5040
 
5041
5041
 
5042
5042
 
5043
- /*utils CONSOLE library associated with aotra version : «1_29072022-2359 (26/04/2025-16:15:53)»*/
5043
+ /*utils CONSOLE library associated with aotra version : «1_29072022-2359 (27/04/2025-01:02:04)»*/
5044
5044
  /*-----------------------------------------------------------------------------*/
5045
5045
 
5046
5046
 
@@ -5294,7 +5294,25 @@ WebsocketImplementation={
5294
5294
  WebsocketImplementation.isNodeContext=isNodeContext;
5295
5295
  WebsocketImplementation.useSocketIOImplementation=useSocketIOImplementation;
5296
5296
 
5297
- if(WebsocketImplementation.useSocketIOImplementation){
5297
+ if(!WebsocketImplementation.useSocketIOImplementation){
5298
+ // TRACE
5299
+ lognow("INFO : (SERVER/CLIENT) Using native WebSocket implementation.");
5300
+
5301
+ // https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_client_applications
5302
+ if(isNodeContext){
5303
+ if(typeof(WebSocket)==="undefined"){
5304
+ // TRACE
5305
+ console.log("«ws» SERVER library not called yet, calling it now.");
5306
+
5307
+ WebSocket=require("ws");
5308
+ if(typeof(WebSocket)==="undefined"){
5309
+ // TRACE
5310
+ console.log("ERROR : «ws» CONSOLE/BROWSER CLIENT/SERVER library not found. Cannot launch nodejs server. Aborting.");
5311
+ }
5312
+ }
5313
+ }
5314
+
5315
+ }else{
5298
5316
  // TRACE
5299
5317
  lognow("INFO : (SERVER/CLIENT) Using socket.io websocket implementation.");
5300
5318
 
@@ -5327,23 +5345,6 @@ WebsocketImplementation={
5327
5345
  }
5328
5346
  }
5329
5347
 
5330
- }else{
5331
- // TRACE
5332
- lognow("INFO : (SERVER/CLIENT) Using native WebSocket implementation.");
5333
-
5334
- // https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_client_applications
5335
- if(isNodeContext){
5336
- if(typeof(WebSocket)==="undefined"){
5337
- // TRACE
5338
- console.log("«ws» SERVER library not called yet, calling it now.");
5339
-
5340
- WebSocket=require("ws");
5341
- if(typeof(WebSocket)==="undefined"){
5342
- // TRACE
5343
- console.log("ERROR : «ws» CONSOLE/BROWSER CLIENT/SERVER library not found. Cannot launch nodejs server. Aborting.");
5344
- }
5345
- }
5346
- }
5347
5348
 
5348
5349
  }
5349
5350
 
@@ -7118,6 +7119,47 @@ getAORTACNode=function(nodeId=getUUID(), selfOrigin="ws://127.0.0.1:40000", outc
7118
7119
 
7119
7120
  // ==================================================================================================================
7120
7121
 
7122
+ class ServerReceptionEntryPoint{
7123
+
7124
+ constructor(channelNameParam, clientsRoomsTag){
7125
+ this.channelName=channelNameParam;
7126
+ this.clientsRoomsTag=clientsRoomsTag;
7127
+ }
7128
+
7129
+ execute(eventOrMessage, clientSocketParam){
7130
+
7131
+ // With «ws» library we have no choive than register message events inside a «connection» event !
7132
+
7133
+ const dataWrapped=WebsocketImplementation.getMessageDataBothImplementations(eventOrMessage);
7134
+
7135
+ // Channel information is stored in exchanged data :
7136
+ if(dataWrapped.channelName!==this.channelName) return;
7137
+
7138
+ // TODO : FIXME : Use one single interface !
7139
+ // Room information is stored in client socket object :
7140
+ const isClientInRoom=WebsocketImplementation.isInRoom(clientSocketParam, this.clientsRoomsTag);
7141
+
7142
+ // DBG
7143
+ lognow("(SERVER) isClientInRoom:",isClientInRoom);
7144
+
7145
+ if(!isClientInRoom) return;
7146
+
7147
+ if(doOnIncomingMessage){
7148
+ // DBG
7149
+ lognow("(SERVER) doOnIncomingMessage:");
7150
+ doOnIncomingMessage(dataWrapped.data, clientSocketParam);
7151
+ }
7152
+
7153
+ }
7154
+ }
7155
+
7156
+
7157
+
7158
+
7159
+
7160
+
7161
+
7162
+
7121
7163
  class NodeServerInstance{
7122
7164
 
7123
7165
  constructor(serverSocket, listenableServer){
@@ -7127,7 +7169,7 @@ class NodeServerInstance{
7127
7169
  this.clientPingIntervalMillis=5000;
7128
7170
  this.serverSocket=serverSocket;
7129
7171
  this.listenableServer=listenableServer;
7130
- this.receptionEntryPoints=[];
7172
+ this.serverReceptionEntryPoints=[];
7131
7173
 
7132
7174
  }
7133
7175
 
@@ -7150,47 +7192,16 @@ class NodeServerInstance{
7150
7192
  // DBG
7151
7193
  lognow("(SERVER) Registering receive for «"+channelNameParam+"»...");
7152
7194
 
7153
-
7154
- const receptionEntryPoint={
7155
- channelName:channelNameParam,
7156
- clientsRoomsTag:clientsRoomsTag,
7157
- execute:(eventOrMessage, clientSocketParam)=>{
7158
-
7159
- // With «ws» library we have no choive than register message events inside a «connection» event !
7160
-
7161
- const dataWrapped=WebsocketImplementation.getMessageDataBothImplementations(eventOrMessage);
7162
-
7163
- // Channel information is stored in exchanged data :
7164
- if(dataWrapped.channelName!==receptionEntryPoint.channelName) return;
7165
-
7166
-
7167
- // TODO : FIXME : Use one single interface !
7168
- // Room information is stored in client socket object :
7169
- const isClientInRoom=WebsocketImplementation.isInRoom(clientSocketParam, receptionEntryPoint.clientsRoomsTag);
7170
-
7171
- // DBG
7172
- lognow("(SERVER) isClientInRoom:",isClientInRoom);
7173
-
7174
- if(!isClientInRoom) return;
7175
-
7176
- if(doOnIncomingMessage){
7177
- // DBG
7178
- lognow("(SERVER) doOnIncomingMessage:");
7179
- doOnIncomingMessage(dataWrapped.data, clientSocketParam);
7180
- }
7181
-
7182
- },
7183
- };
7184
-
7195
+ const serverReceptionEntryPoint=new ServerReceptionEntryPoint(channelNameParam,clientsRoomsTag);
7185
7196
 
7186
7197
  ///!!!
7187
- self.receptionEntryPoints.push(receptionEntryPoint);
7198
+ this.serverReceptionEntryPoints.push(serverReceptionEntryPoint);
7188
7199
 
7189
7200
  // SPECIAL FOR THE SOCKETIO IMPLEMENTATION :
7190
7201
  if(WebsocketImplementation.useSocketIOImplementation){
7191
- const channelName=receptionEntryPoint.channelName;
7202
+ const channelName=serverReceptionEntryPoint.channelName;
7192
7203
  clientSocket.on(channelName, (eventOrMessage)=>{
7193
- receptionEntryPoint.execute(eventOrMessage, clientSocket);
7204
+ serverReceptionEntryPoint.execute(eventOrMessage, clientSocket);
7194
7205
  });
7195
7206
  }
7196
7207
 
@@ -7289,13 +7300,14 @@ class NodeServerInstance{
7289
7300
 
7290
7301
  const doOnMessage=(eventOrMessage)=>{
7291
7302
  // We execute the events registration listeners entry points:
7292
- foreach(self.receptionEntryPoints,(receptionEntryPoint,i)=>{
7293
- receptionEntryPoint.execute(eventOrMessage, clientSocket);
7303
+ foreach(self.serverReceptionEntryPoints,(serverReceptionEntryPoint,i)=>{
7304
+ serverReceptionEntryPoint.execute(eventOrMessage, clientSocket);
7294
7305
  });
7295
7306
  };
7296
7307
 
7297
- if(!WebsocketImplementation.useSocketIOImplementation)
7308
+ if(!WebsocketImplementation.useSocketIOImplementation){
7298
7309
  clientSocket.addEventListener("message", doOnMessage);
7310
+ }
7299
7311
 
7300
7312
  doOnConnection(this, clientSocket);
7301
7313
 
@@ -7335,19 +7347,26 @@ class NodeServerInstance{
7335
7347
 
7336
7348
  clientSocket.isConnectionAlive=false;
7337
7349
 
7338
- if(!WebsocketImplementation.useSocketIOImplementation) clientSocket.ping();
7339
- else self.send("protocol",{type:"ping"}, null, clientSocket);
7350
+ if(!WebsocketImplementation.useSocketIOImplementation){
7351
+ // FOR THE WEBSOCKET IMPLEMENTATION :
7352
+ clientSocket.ping();
7353
+ }else{
7354
+ // FOR THE SOCKETIO IMPLEMENTATION :
7355
+ self.send("protocol",{type:"ping"}, null, clientSocket);
7356
+ }
7340
7357
 
7341
7358
 
7342
7359
  }, this.clientPingIntervalMillis);
7343
7360
 
7344
-
7361
+
7362
+ // PING-PONG :
7345
7363
  if(!WebsocketImplementation.useSocketIOImplementation){
7364
+ // FOR THE WEBSOCKET IMPLEMENTATION :
7346
7365
  clientSocket.on("pong",()=>{
7347
7366
  clientSocket.isConnectionAlive=true;
7348
7367
  });
7349
7368
  }else{
7350
-
7369
+ // FOR THE SOCKETIO IMPLEMENTATION :
7351
7370
  this.receive("protocol",(message)=>{
7352
7371
  if(message.type!=="pong") return;
7353
7372
  clientSocket.isConnectionAlive=true;
@@ -7410,6 +7429,48 @@ if(typeof(window)==="undefined") window=global;
7410
7429
 
7411
7430
  // ==================================================================================================================
7412
7431
 
7432
+
7433
+ class ClientReceptionEntryPoint{
7434
+
7435
+ constructor(channelNameParam, entryPointId, clientsRoomsTag, listenerConfig, doOnIncomingMessage){
7436
+
7437
+ this.channelName=channelNameParam;
7438
+ this.entryPointId=entryPointId;
7439
+ this.clientsRoomsTag=clientsRoomsTag;
7440
+ this.listenerConfig=listenerConfig;
7441
+ this.doOnIncomingMessage=doOnIncomingMessage;
7442
+ }
7443
+
7444
+ execute(eventOrMessage, clientSocket, clientReceptionEntryPoints){
7445
+
7446
+ const channelName=this.channelName;
7447
+
7448
+
7449
+ const dataWrapped=WebsocketImplementation.getMessageDataBothImplementations(eventOrMessage);
7450
+
7451
+ // Channel information is stored in exchanged data :
7452
+ if(dataWrapped.channelName && dataWrapped.channelName!==this.channelName) return;
7453
+
7454
+ // Room information is stored in client socket object :
7455
+ if(!WebsocketImplementation.isInRoom(clientSocket, this.clientsRoomsTag)) return;
7456
+
7457
+
7458
+ if( this.listenerConfig && this.listenerConfig.messageType && dataWrapped.type
7459
+ && this.listenerConfig.messageType===dataWrapped.type) return;
7460
+
7461
+ // We remove one-time usage listeners :
7462
+ // We remove BEFORE executing doOnIncomingMessage(), because in this reception entry point function we can have other listener registration UNDER THE SAME ID !
7463
+ if(this.listenerConfig && this.listenerConfig.destroyListenerAfterReceiving)
7464
+ remove(this, clientReceptionEntryPoints);
7465
+
7466
+ if(this.doOnIncomingMessage)
7467
+ this.doOnIncomingMessage(dataWrapped.data, clientSocket);
7468
+
7469
+ }
7470
+
7471
+ }
7472
+
7473
+
7413
7474
  //
7414
7475
  // CLIENT INSTANCE :
7415
7476
  //
@@ -7417,64 +7478,36 @@ class ClientInstance{
7417
7478
 
7418
7479
  constructor(clientSocket){
7419
7480
  this.clientSocket=clientSocket;
7420
- this.receptionEntryPoints=[];
7481
+ this.clientReceptionEntryPoints=[];
7421
7482
 
7422
7483
  }
7423
7484
 
7424
- receive(channelNameParam, doOnIncomingMessage, clientsRoomsTag=null, receptionEntryPointId=null, listenerConfig={destroyListenerAfterReceiving:false}){
7485
+ receive(channelNameParam, doOnIncomingMessage, clientsRoomsTag=null, entryPointId=null, listenerConfig={destroyListenerAfterReceiving:false}){
7425
7486
  const self=this;
7426
7487
 
7427
7488
  // DBG
7428
- lognow("INFO : (CLIENT-BROWSER) SETTING UP RECEIVE for :",channelNameParam);
7429
-
7430
- const receptionEntryPoint={
7431
- channelName:channelNameParam,
7432
- clientsRoomsTag:clientsRoomsTag,
7433
- // TODO : ADD TO ALL OTHER SUBSYSTEMS !
7434
- id:receptionEntryPointId,
7435
- listenerConfig:listenerConfig,
7436
- doOnIncomingMessage:doOnIncomingMessage,
7437
- execute:(eventOrMessage)=>{
7438
-
7439
- const dataWrapped=WebsocketImplementation.getMessageDataBothImplementations(eventOrMessage);
7440
-
7441
- // Channel information is stored in exchanged data :
7442
- if(dataWrapped.channelName && dataWrapped.channelName!==receptionEntryPoint.channelName) return;
7443
-
7444
- // Room information is stored in client socket object :
7445
- const clientSocket=self.clientSocket;
7446
- if(!WebsocketImplementation.isInRoom(clientSocket, receptionEntryPoint.clientsRoomsTag)) return;
7447
-
7448
- // We remove one-time usage listeners :
7449
- // We remove BEFORE executing doOnIncomingMessage(), because in receptionEntryPoint function we can have other listener registration UNDER THE SAME ID !
7450
- if(receptionEntryPoint.listenerConfig && receptionEntryPoint.listenerConfig.destroyListenerAfterReceiving)
7451
- remove(self.receptionEntryPoints, receptionEntryPoint);
7452
-
7453
- if(receptionEntryPoint.doOnIncomingMessage)
7454
- receptionEntryPoint.doOnIncomingMessage(dataWrapped.data, clientSocket);
7455
-
7456
- }
7457
- };
7458
-
7459
- ///!!!
7460
- // TODO : ADD TO ALL OTHER SUBSYSTEMS !
7461
- if(!contains.filter((l)=>(l.id && receptionEntryPoint.id && l.id===receptionEntryPoint.id))(this.receptionEntryPoints))
7462
- this.receptionEntryPoints.push(receptionEntryPoint);
7489
+ lognow("INFO : (CLIENT) SETTING UP RECEIVE for :",channelNameParam);
7463
7490
 
7464
- // SPECIAL FOR THE SOCKETIO IMPLEMENTATION :
7465
- if(WebsocketImplementation.useSocketIOImplementation){
7466
- const channelName=receptionEntryPoint.channelName;
7467
- clientSocket.on(channelName, (eventOrMessage)=>{
7468
- receptionEntryPoint.execute(eventOrMessage, clientSocket);
7491
+ const clientReceptionEntryPoint=new ClientReceptionEntryPoint(channelNameParam, entryPointId, clientsRoomsTag, listenerConfig, doOnIncomingMessage);
7492
+ if(!contains.filter((l)=>(
7493
+ l.entryPointId && clientReceptionEntryPoint.entryPointId
7494
+ && l.entryPointId===clientReceptionEntryPoint.entryPointId
7495
+ ))(this.clientReceptionEntryPoints)){
7496
+
7497
+ this.clientReceptionEntryPoints.push(clientReceptionEntryPoint);
7498
+ }
7499
+
7500
+ const clientSocket=this.clientSocket;
7501
+ if(WebsocketImplementation.useSocketIOImplementation){
7502
+ // FOR THE SOCKETIO IMPLEMENTATION :
7503
+ clientSocket.on(channelNameParam, (eventOrMessage)=>{
7504
+ clientReceptionEntryPoint.execute(eventOrMessage, clientSocket, clientReceptionEntryPoint);
7469
7505
  });
7470
7506
  }
7471
-
7472
-
7507
+
7473
7508
  return this;
7474
7509
  }
7475
7510
 
7476
-
7477
- // TODO : FIXME : DUPLICATED CODE !
7478
7511
  sendChainable(channelNameParam, data, clientsRoomsTag=null){
7479
7512
  const self=this;
7480
7513
 
@@ -7507,6 +7540,7 @@ class ClientInstance{
7507
7540
  }
7508
7541
 
7509
7542
  doOnIncomingMessageForResponse(dataLocal, clientSocket);
7543
+
7510
7544
  }, resultPromise.clientsRoomsTag, listenerId, {destroyListenerAfterReceiving:true});
7511
7545
  //
7512
7546
 
@@ -7569,39 +7603,40 @@ class ClientInstance{
7569
7603
  // DBG
7570
7604
  lognow("DEBUG : CLIENT : doOnConnection !");
7571
7605
 
7572
- const doOnMessage=(eventOrMessage)=>{
7573
-
7574
- // We execute the listeners entry points registration :
7575
- foreach(self.receptionEntryPoints,(receptionEntryPoint)=>{
7576
- receptionEntryPoint.execute(eventOrMessage);
7577
- });
7578
-
7579
- };
7580
-
7581
7606
  const clientSocket=self.clientSocket;
7582
- if(!WebsocketImplementation.useSocketIOImplementation){ clientSocket.addEventListener("message", doOnMessage);
7607
+ if(!WebsocketImplementation.useSocketIOImplementation){
7608
+ // FOR THE WEBSOCKET IMPLEMENTATION :
7609
+ clientSocket.addEventListener("message", (eventOrMessage)=>{
7610
+ foreach(self.clientReceptionEntryPoints,(clientReceptionEntryPoint)=>{
7611
+ clientReceptionEntryPoint.execute(eventOrMessage, clientSocket, self.clientReceptionEntryPoints);
7612
+ });
7613
+ });
7583
7614
  }
7584
7615
 
7585
7616
  doOnConnection(self, clientSocket);
7586
-
7587
7617
  };
7588
7618
 
7589
- if(!WebsocketImplementation.useSocketIOImplementation) this.clientSocket.addEventListener("open",doAllOnConnection);
7590
- else this.clientSocket.on("connect",doAllOnConnection);
7591
-
7592
7619
 
7593
- // Client ping handling : (SocketIO implementation only)
7594
- if(WebsocketImplementation.useSocketIOImplementation){
7620
+ if(!WebsocketImplementation.useSocketIOImplementation){
7621
+ // FOR THE WEBSOCKET IMPLEMENTATION :
7622
+ this.clientSocket.addEventListener("open",doAllOnConnection);
7623
+ }else{
7624
+ // FOR THE SOCKETIO IMPLEMENTATION :
7625
+ this.clientSocket.on("connect",doAllOnConnection);
7626
+
7627
+ // Client ping handling :
7628
+ // (SocketIO implementation only, the websocket implementation sends back a hidden pong !)
7629
+ // PING-PONG :
7595
7630
  this.receive("protocol",(message)=>{
7596
7631
  if(message.type!=="ping") return;
7597
7632
  self.send("protocol",{type:"pong"});
7598
-
7599
- // DBG
7600
- lognow("DEBUG : CLIENT : Pong sent.");
7633
+ // // DBG
7634
+ // lognow("DEBUG : CLIENT : Pong sent.");
7601
7635
  });
7602
7636
  }
7603
7637
 
7604
7638
 
7639
+
7605
7640
  }
7606
7641
 
7607
7642
  }
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aotrautils-srv",
3
- "version": "0.0.1477",
3
+ "version": "0.0.1479",
4
4
  "main": "aotrautils-srv.build.js",
5
5
  "description": "A library for vanilla javascript utils (server-side) used in aotra javascript CMS",
6
6
  "author": "Jeremie Ratomposon <info@alqemia.com> (https://alqemia.com)",