aotrautils-srv 0.0.2015 → 0.0.2017

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 (05/10/2026-23:02:09)»*/
3
+ /*utils COMMONS library associated with aotra version : «1_29072022-2359 (12/10/2026-21:35:03)»*/
4
4
  /*-----------------------------------------------------------------------------*/
5
5
 
6
6
 
@@ -5485,7 +5485,7 @@ window.AOTRAUTILS_LIB_IS_LOADED=true;
5485
5485
 
5486
5486
 
5487
5487
 
5488
- /*utils AI library associated with aotra version : «1_29072022-2359 (05/10/2026-23:02:09)»*/
5488
+ /*utils AI library associated with aotra version : «1_29072022-2359 (12/10/2026-21:35:03)»*/
5489
5489
  /*-----------------------------------------------------------------------------*/
5490
5490
 
5491
5491
 
@@ -5749,7 +5749,7 @@ window.getLLMAPIClient=(modelName, apiURL, agentRole, defaultPrompt, llmProvider
5749
5749
 
5750
5750
 
5751
5751
 
5752
- /*utils CONSOLE library associated with aotra version : «1_29072022-2359 (05/10/2026-23:02:09)»*/
5752
+ /*utils CONSOLE library associated with aotra version : «1_29072022-2359 (12/10/2026-21:35:03)»*/
5753
5753
  /*-----------------------------------------------------------------------------*/
5754
5754
 
5755
5755
 
@@ -7800,12 +7800,24 @@ function toConvention(type="snake",str){
7800
7800
 
7801
7801
 
7802
7802
 
7803
- function capitalize(str){
7803
+ window.capitalize=function(str){
7804
7804
  if(nothing(str)) return "";
7805
7805
  return str.replace(/(?:^|\s)\S/m, firstChar => firstChar.toUpperCase()); // We replace only the first character.
7806
+ };
7807
+
7808
+
7809
+ window.unCamelCase=function(str, forceLowerCase=true){
7810
+ if(nothing(str)) return "";
7811
+ const result=str
7812
+ .replace(/([a-z0-9])([A-Z])/g, "$1 $2") // camelCase -> camel Case
7813
+ .replace(/([A-Z]+)([A-Z][a-z])/g, "$1 $2"); // HTTPResponse -> HTTP Response
7814
+ if(forceLowerCase)
7815
+ return result.toLowerCase();
7816
+ return result;
7806
7817
  }
7807
7818
 
7808
7819
 
7820
+
7809
7821
  /* ## Utility global methods in a javascript, console (nodejs) or vanilla javascript with no browser environment.
7810
7822
  *
7811
7823
  * This set of methods gathers utility generic-purpose methods usable in any JS project.
@@ -8144,11 +8156,19 @@ window.WebsocketImplementation={
8144
8156
  useFlatStrings:false,
8145
8157
 
8146
8158
  // COMMON METHODS
8147
- /*private static*/isInRoom(clientSocket, clientsRoomsTag){
8148
- return (!clientsRoomsTag || empty(clientsRoomsTag) || contains(clientsRoomsTag, clientSocket.clientRoomTag));
8159
+ /*public static*/isInRoom(clientSocket, clientsRoomsTag){
8160
+ return (!clientsRoomsTag || empty(clientsRoomsTag)
8161
+ || !clientSocket || !clientSocket.clientRoomTag || empty(clientSocket.clientRoomTag)
8162
+ || contains(clientsRoomsTag, clientSocket.clientRoomTag));
8149
8163
  },
8150
8164
 
8151
-
8165
+ /*public static*/isInRightChannel(eventOrMessage, channelName){
8166
+ return (!eventOrMessage || empty(eventOrMessage)
8167
+ || !eventOrMessage.channelName || empty(eventOrMessage.channelName)
8168
+ || !channelName || empty(channelName)
8169
+ || eventOrMessage.channelName==channelName);
8170
+ },
8171
+
8152
8172
  //
8153
8173
  // CONSOLE NODE SERVER/CLIENT
8154
8174
  //
@@ -8450,7 +8470,8 @@ window.WebsocketImplementation={
8450
8470
  };
8451
8471
 
8452
8472
 
8453
- window.launchNodeHTTPServer=function(port, doOnConnect=null, doOnFinalizeServer=null, doOnConnectionLost=null, /*OPTIONAL*/sslOptions=null, httpHandlerParam=null,
8473
+ window.launchNodeHTTPServer=function(port, doOnConnect=null, doOnFinalizeServer=null, doOnConnectionLost=null,
8474
+ /*OPTIONAL*/sslOptions=null, httpHandlerParam=null,
8454
8475
  addCORSHeader=ADD_CORS_HEADER_BY_DEFAULT){
8455
8476
 
8456
8477
  const EXCLUDED_FILENAMES_PARTS=[".keyHash.",".pem"];
@@ -8468,28 +8489,32 @@ window.launchNodeHTTPServer=function(port, doOnConnect=null, doOnFinalizeServer=
8468
8489
  }
8469
8490
 
8470
8491
 
8471
- const DEFAULT_HANDLER=function(request, response){
8472
-
8492
+ const EXCLUSION_HANDLER=function(request, response){
8473
8493
  const url=request.url;
8474
-
8475
- let isURLInExclusionZone=!!foreach(EXCLUDED_FILENAMES_PARTS,(excludedStr)=>{
8494
+
8495
+ const isURLInExclusionZone=!!foreach(EXCLUDED_FILENAMES_PARTS,(excludedStr)=>{
8476
8496
  if(contains(url,excludedStr)){
8477
8497
  return true;
8478
8498
  }
8479
8499
  });
8480
-
8481
8500
  if(isURLInExclusionZone){
8482
- // TRACE
8483
- lognow("ERROR 403 forbidden access error :");
8484
- lognow("ERROR : request:",request);
8485
- lognow("ERROR : response:",response);
8486
-
8487
- response.writeHead(403);
8488
- response.end("Sorry, cannot access resource : error: "+error.code+" ..\n");
8489
- response.end();
8490
- return;
8501
+ // TRACE
8502
+ lognow("ERROR 403 forbidden access error :");
8503
+ lognow("ERROR : request:",request);
8504
+ lognow("ERROR : response:",response);
8505
+
8506
+ response.writeHead(403);
8507
+ response.end("Sorry, cannot access resource : error: "+error.code+" ..\n");
8508
+ response.end();
8509
+
8510
+ throw new Error("ERROR : Not allowed to access this ressource.");
8491
8511
  }
8512
+ };
8492
8513
 
8514
+
8515
+ const DEFAULT_HANDLER_HTTP_FILE_RETRIEVAL=function(request, response){
8516
+
8517
+ const url=request.url;
8493
8518
  const urlFile="." + url;
8494
8519
 
8495
8520
  let filePath=urlFile.indexOf("?")!==-1?urlFile.split("?")[0]:urlFile;
@@ -8505,26 +8530,26 @@ window.launchNodeHTTPServer=function(port, doOnConnect=null, doOnFinalizeServer=
8505
8530
 
8506
8531
  window.fs.readFile(filePath, function(error, fileContent){
8507
8532
  if(error){
8508
- if(error.code=="ENOENT"){
8509
- // TRACE
8510
- console.log("ERROR 404 file not found :"+filePath);
8511
-
8512
- window.fs.readFile("./404.html", function(error, fileContent){
8513
- response.writeHead(200, headers);
8514
- response.end(fileContent, "utf-8");
8515
- });
8516
-
8517
- }else {
8518
-
8519
- // TRACE
8520
- lognow("ERROR 500 server error :");
8521
- lognow("ERROR : error:",error);
8522
-
8523
- response.writeHead(500);
8524
- response.end("Sorry, check with the site admin for error: "+error.code+" ..\n");
8525
- response.end();
8533
+ if(error.code=="ENOENT"){
8534
+ // TRACE
8535
+ console.log("ERROR 404 file not found :"+filePath);
8536
+
8537
+ window.fs.readFile("./404.html", function(error, fileContent){
8538
+ response.writeHead(200, headers);
8539
+ response.end(fileContent, "utf-8");
8540
+ });
8526
8541
 
8527
- }
8542
+ }else {
8543
+
8544
+ // TRACE
8545
+ lognow("ERROR 500 server error :");
8546
+ lognow("ERROR : error:",error);
8547
+
8548
+ response.writeHead(500);
8549
+ response.end("Sorry, check with the site admin for error: "+error.code+" ..\n");
8550
+ response.end();
8551
+
8552
+ }
8528
8553
  }else {
8529
8554
 
8530
8555
  // TRACE
@@ -8540,11 +8565,77 @@ window.launchNodeHTTPServer=function(port, doOnConnect=null, doOnFinalizeServer=
8540
8565
 
8541
8566
  }
8542
8567
  });
8543
-
8568
+
8544
8569
  };
8545
8570
 
8546
8571
 
8547
- const handler=nonull(httpHandlerParam, DEFAULT_HANDLER);
8572
+ /*private static*/handleHTTPEndpoints=async function(request, response){
8573
+ const url=new URL(request.url, `https://${request.headers.host}`);
8574
+
8575
+ for(const httpEndpoint of handler.httpEndpoints){
8576
+
8577
+
8578
+ // EXPECTED : httpEndpoint={urlPath:"",httpMethod:""}
8579
+
8580
+ if(!httpEndpoint.urlPath || empty(httpEndpoint.urlPath) || url.pathname===httpEndpoint.urlPath){
8581
+ const requestHttpMethod=request.method;
8582
+
8583
+ try{
8584
+ let messageIn=null;
8585
+ if( requestHttpMethod=="GET" &&
8586
+ (!httpEndpoint.httpMethod || empty(httpEndpoint.httpMethod) || httpEndpoint.httpMethod.toUpperCase()=="GET")){
8587
+
8588
+ messageIn={...getRequestURLParameters(request)};
8589
+
8590
+ return true;
8591
+ }else if(requestHttpMethod=="POST"
8592
+ && contains(["POST","PUT","PATCH","DELETE"],httpEndpoint.httpMethod.toUpperCase())){
8593
+
8594
+ messageIn={...getRequestURLParameters(request), ...(await getRequestBody(request))};
8595
+ }
8596
+
8597
+ if(messageIn){
8598
+ let httpEndpointResultObject={httpStatus:200, body:{}};
8599
+ const serverReceptionEntryPoint=httpEndpoint.serverReceptionEntryPoint;
8600
+ if(serverReceptionEntryPoint){
8601
+ httpEndpointResultObject.body=serverReceptionEntryPoint.execute(messageIn, null, false);
8602
+ }
8603
+
8604
+ response.writeHead(httpEndpointResultObject.httpStatus, { "Content-Type": "application/json" });
8605
+ response.end(JSON.stringify(httpEndpointResultObject.body));
8606
+ }
8607
+
8608
+
8609
+ }catch(error) {
8610
+ response.writeHead(erreur.error || 500, { "Content-Type": "application/json" });
8611
+ response.end(JSON.stringify({ error: erreur.error || "ERROR : HTTPO Endpoints encountered an error" }));
8612
+ }
8613
+ }
8614
+
8615
+ }
8616
+
8617
+ return false;
8618
+ };
8619
+
8620
+ const handler=function(request, response){
8621
+
8622
+ try{
8623
+ EXCLUSION_HANDLER(request, response);
8624
+ handleHTTPEndpoints(request, response).then(httpEndpointHasInterceptedRequest=>{
8625
+ if(!httpEndpointHasInterceptedRequest){
8626
+ nonull(httpHandlerParam, DEFAULT_HANDLER_HTTP_FILE_RETRIEVAL)(request, response);
8627
+ }
8628
+ }).catch(error=>{
8629
+ // TRACE
8630
+ lognow("ERROR : An error occurred when trying to execute HTTP endpoint :error:",error);
8631
+ });
8632
+ }catch(error){
8633
+ // TRACE
8634
+ lognow("ERROR : An error occurred when trying to process request :error:",error);
8635
+ }
8636
+
8637
+ };
8638
+
8548
8639
 
8549
8640
  let listenableServer;
8550
8641
  if(sslOptions){
@@ -8574,6 +8665,12 @@ window.launchNodeHTTPServer=function(port, doOnConnect=null, doOnFinalizeServer=
8574
8665
  // TRACE
8575
8666
  console.log("INFO : SERVER : Generic Nodejs server launched and listening on port:" + port + "!");
8576
8667
 
8668
+
8669
+ // Endpoints handling :
8670
+ server.httpEndpoints=[];
8671
+ handler.httpEndpoints=server.httpEndpoints;
8672
+
8673
+
8577
8674
  return server;
8578
8675
  }
8579
8676
 
@@ -9237,12 +9334,12 @@ if(typeof(window)==="undefined") window=global;
9237
9334
  //
9238
9335
  //window.WebsocketImplementation.implementationToUse.initOnReceiveBrowserContext(channelNameParam, (eventOrMessage)=>{ });
9239
9336
  //
9240
- //window.WebsocketImplementation.implementationToUse.initOnReceiveNodeContext(channelNameParam, serverReceptionEntryPoint);
9337
+ // //OLD: window.WebsocketImplementation.implementationToUse.initOnReceiveNodeContext(channelNameParam, serverReceptionEntryPoint);
9241
9338
  //
9242
9339
  //window.WebsocketImplementation.implementationToUse.getServerClients();
9243
9340
  //
9244
9341
  //if(window.WebsocketImplementation.implementationToUse.isReady(clientSocket)) return;
9245
- // window.WebsocketImplementation.implementationToUse.initOnMessageReceived(doOnMessage);
9342
+ // //OLD : window.WebsocketImplementation.implementationToUse.initOnMessageReceived(doOnMessage);
9246
9343
  // window.WebsocketImplementation.implementationToUse.terminate();
9247
9344
  //
9248
9345
  //window.WebsocketImplementation.implementationToUse.ping();
@@ -9419,9 +9516,7 @@ window.ClientReceptionEntryPoint=class ClientReceptionEntryPoint{
9419
9516
  remove(clientReceptionEntryPoints, this);
9420
9517
 
9421
9518
  if(this.doOnIncomingMessage){
9422
-
9423
9519
  const dataRestored=restoreClassesInformation(dataLocal);
9424
-
9425
9520
  this.doOnIncomingMessage(dataRestored, clientSocket);
9426
9521
  }
9427
9522
 
@@ -9513,7 +9608,6 @@ window.ClientInstance=class ClientInstance{
9513
9608
  return this;
9514
9609
  }
9515
9610
 
9516
-
9517
9611
  send(channelNameParam, data, clientsRoomsTag=null){
9518
9612
 
9519
9613
  // // DBG
@@ -9532,17 +9626,17 @@ window.ClientInstance=class ClientInstance{
9532
9626
  // CAUTION : WE CAN ONLY SEND STRINGS !!!
9533
9627
  // (or else we'll have TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be of type string or an instance of Buffer, ArrayBuffer, or Array or an Array-like Object. Received an instance of Object)
9534
9628
  // (Don't worry, the underlying sub-system will turn it back into Objects without you need to do anything!)
9535
- dataWrapped=stringifyObject(dataWrapped);
9629
+ const dataWrappedStr=stringifyObject(dataWrapped);
9536
9630
 
9537
9631
  // if(!window.WebsocketImplementation.useSocketIOImplementation)
9538
9632
  if(!window.WebsocketImplementation.implementationToUse){
9539
- clientSocket.send(dataWrapped);
9633
+ clientSocket.send(dataWrappedStr);
9540
9634
  }else{
9541
- window.WebsocketImplementation.implementationToUse.send(dataWrapped);
9635
+ window.WebsocketImplementation.implementationToUse.send(dataWrappedStr);
9542
9636
  // clientSocket.emit(channelNameParam,dataWrapped);
9543
9637
  }
9544
9638
 
9545
- return this;
9639
+ return dataWrapped;
9546
9640
  }
9547
9641
 
9548
9642
 
@@ -9642,16 +9736,114 @@ window.ClientInstance=class ClientInstance{
9642
9736
 
9643
9737
  // ==================================================================================================================
9644
9738
 
9739
+ // NEW API :
9740
+ window.ServerReceptionEntryPointsBag=class ServerReceptionEntryPointsBag{
9741
+
9742
+ constructor(channelNameParam, server, clientsRoomsTag=null){
9743
+ this.channelName=channelNameParam;
9744
+ this.server=server;
9745
+ this.clientsRoomsTag=clientsRoomsTag;
9746
+
9747
+ this.serverReceptionEntryPoints=[];
9748
+ this.prerequisiteMethods=[];
9749
+ }
9750
+
9751
+ // Executed whenever any message is received on any channel :
9752
+ executeAll(eventOrMessage, clientSocket=null){ // FOR REAL-TIME ONLY :
9753
+
9754
+
9755
+ const clientsRoomsTag=this.clientsRoomsTag;
9756
+ if(!window.WebsocketImplementation.isInRoom(clientSocket, clientsRoomsTag)) return;
9757
+
9758
+ const channelName=this.channelName;
9759
+ if(!window.WebsocketImplementation.isInRightChannel(eventOrMessage, channelName)) return;
9760
+
9761
+ executeAllPrerequisites(eventOrMessage).then(prequisiteResult=>{
9762
+ const messageModifiedByPrerequisites=prequisiteResult;
9763
+ foreach(this.serverReceptionEntryPoints, (serverReceptionEntryPoint,i)=>{
9764
+
9765
+ // All listeners are executed IN PARALLEL !
9766
+ serverReceptionEntryPoint.execute(messageModifiedByPrerequisites, clientSocket).then((message)=>{
9767
+ /*DO NOTHING*/
9768
+ lognow("SERVER : DEBUG : Server sent message:",message);
9769
+ }).catch(treatmentsChannelError=>{
9770
+
9771
+ const errorMessage="INFO : An error has occured during the processing of a client message on channel treatments.";
9772
+
9773
+ // TRACE
9774
+ lognow(errorMessage, treatmentsChannelError);
9775
+
9776
+ const errorDataWrapped={errorMessage:errorMessage, cause:treatmentsChannelError};
9777
+ self.sendError(errorDataWrapped, clientSocketParam);
9778
+
9779
+ });
9780
+
9781
+ });
9782
+
9783
+ }).catch(error=>{
9784
+ // TRACE
9785
+ lognow("ERROR : (.executeAll()) A prerequisite encountered an error :", error);
9786
+ });
9787
+
9788
+
9789
+
9790
+ }
9791
+
9792
+ addReceiveListenerPrerequisite(prerequisiteMethod){
9793
+ this.prerequisiteMethods.push(prerequisiteMethod);
9794
+ }
9795
+
9796
+ async executeAllPrerequisites(eventOrMessage){
9797
+ let prequisiteResult=eventOrMessage;
9798
+ let i=1;
9799
+ try {
9800
+ for(const prerequisiteMethod of this.prerequisiteMethods) {
9801
+ prequisiteResult=await prerequisiteMethod(prequisiteResult, clientSocket);
9802
+ i++;
9803
+ }
9804
+ }catch(error){
9805
+ // TRACE
9806
+ lognow("ERROR : (.executeAllPrerequisites()) Prerequisite #"+i+" encountered an error :", error);
9807
+ }
9808
+ return prequisiteResult;
9809
+ }
9810
+
9811
+
9812
+ // ASYNC
9813
+ addReceiveListener(messageType, asyncDoOnIncomingMessage, httpEndpointDefinition=null){
9814
+ const channelNameParam=this.channelNameParam;
9815
+ const clientsRoomsTag=this.clientsRoomsTag;
9816
+ const messageTypes=(!messageType || empty(messageType) ? [] :
9817
+ (isArray(messageType) ? messageType : [messageType])
9818
+ );
9819
+ const listenerConfig={listenerMessageTypes:messageTypes, httpEndpointDefinition:httpEndpointDefinition};
9820
+ const serverReceptionEntryPoint=new window.ServerReceptionEntryPoint(channelNameParam, clientsRoomsTag, listenerConfig, asyncDoOnIncomingMessage);
9821
+ this.serverReceptionEntryPoints.push(serverReceptionEntryPoint);
9822
+
9823
+ if(httpEndpointDefinition){
9824
+ // TRACE
9825
+ lognow("INFO : Registered HTTP listener:",httpEndpointDefinition);
9826
+ this.server.httpEndpoints.push(httpEndpointDefinition);
9827
+ }
9828
+
9829
+ }
9830
+
9831
+
9832
+
9833
+ }
9834
+
9835
+
9836
+
9645
9837
  window.ServerReceptionEntryPoint=class ServerReceptionEntryPoint{
9646
9838
 
9647
- constructor(channelNameParam, clientsRoomsTag, listenerConfig=null, doOnIncomingMessage){
9839
+ constructor(channelNameParam, clientsRoomsTag, listenerConfig=null, asyncDoOnIncomingMessage){
9648
9840
  this.channelName=channelNameParam;
9649
9841
  this.clientsRoomsTag=clientsRoomsTag;
9650
9842
  this.listenerConfig=listenerConfig;
9651
- this.doOnIncomingMessage=doOnIncomingMessage;
9843
+ this.asyncDoOnIncomingMessage=asyncDoOnIncomingMessage;
9652
9844
  }
9653
9845
 
9654
- execute(eventOrMessage, clientSocketParam){
9846
+ execute(eventOrMessage, clientSocketParam=null){
9655
9847
 
9656
9848
  if(!eventOrMessage){
9657
9849
  // TRACE
@@ -9659,13 +9851,13 @@ window.ServerReceptionEntryPoint=class ServerReceptionEntryPoint{
9659
9851
  return;
9660
9852
  }
9661
9853
 
9662
- if(!clientSocketParam){
9663
- // TRACE
9664
- lognow("ERROR : No clientSocket to client ! Cannot do reception treatment.");
9665
- return;
9666
- }
9854
+ // OLD :
9855
+ // if(!clientSocketParam){
9856
+ // // TRACE
9857
+ // lognow("ERROR : No clientSocket to client ! Cannot do reception treatment.");
9858
+ // return;
9859
+ // }
9667
9860
 
9668
-
9669
9861
  // With «ws» library we have no choive than register message events inside a «connection» event !
9670
9862
 
9671
9863
  const dataWrapped=window.WebsocketImplementation.getMessageDataBothImplementations(eventOrMessage);
@@ -9675,28 +9867,23 @@ window.ServerReceptionEntryPoint=class ServerReceptionEntryPoint{
9675
9867
 
9676
9868
  // Room information is stored in client socket object :
9677
9869
  const isClientInRoom=window.WebsocketImplementation.isInRoom(clientSocketParam, this.clientsRoomsTag);
9678
-
9679
- // // DBG
9680
- // lognow("(SERVER) isClientInRoom:",isClientInRoom);
9681
-
9682
9870
  if(!isClientInRoom) return;
9683
9871
 
9684
- // We check if the message matches the required message type :
9685
- if( this.listenerConfig && this.listenerConfig.listenerMessageType
9686
- && dataWrapped.data
9687
- && dataWrapped.data.type && this.listenerConfig.listenerMessageType!==dataWrapped.data.type){
9688
- return;
9872
+
9873
+ if(this.listenerConfig){
9874
+ // (CAUTION : if no message types is configured, then we accept this server listener)
9875
+ if(this.listenerConfig.listenerMessageTypes && !empty(this.listenerConfig.listenerMessageTypes)){
9876
+ // Else we check if the message matches the required message types :
9877
+ if( dataWrapped.data && dataWrapped.data.type
9878
+ && !contains(this.listenerConfig.listenerMessageTypes, dataWrapped.data.type))
9879
+ return;
9880
+ }
9689
9881
  }
9690
9882
 
9691
- if(this.doOnIncomingMessage){
9692
- // // DBG
9693
- // lognow("(SERVER) this.doOnIncomingMessage:");
9694
-
9883
+ if(this.asyncDoOnIncomingMessage){
9695
9884
  const dataLocal=dataWrapped.data;
9696
-
9697
9885
  const dataRestored=restoreClassesInformation(dataLocal);
9698
-
9699
- this.doOnIncomingMessage(dataRestored, clientSocketParam);
9886
+ this.asyncDoOnIncomingMessage(dataRestored, clientSocketParam);
9700
9887
  }
9701
9888
 
9702
9889
  }
@@ -9715,8 +9902,12 @@ window.NodeServerInstance=class NodeServerInstance{
9715
9902
  this.serverSocket=serverSocket;
9716
9903
 
9717
9904
  this.listenableServer=listenableServer;
9905
+
9718
9906
  this.serverReceptionEntryPoints=[];
9907
+ this.serverReceptionEntryPointsBags=[];
9719
9908
 
9909
+ this.httpEndpoints=null;
9910
+
9720
9911
  }
9721
9912
 
9722
9913
  setPingPongTimeout(clientPingIntervalMillis){
@@ -9743,34 +9934,54 @@ window.NodeServerInstance=class NodeServerInstance{
9743
9934
  //sendChainable(channelNameParam, data, clientsRoomsTag=null){
9744
9935
  //}
9745
9936
 
9746
- // I-
9937
+ // I- (NOT ASYNC)
9938
+ // DEPRECATED, USE THE NEW API INSTEAD :
9747
9939
  receive(channelNameParam, doOnIncomingMessage, listenerConfig=null, clientsRoomsTag=null){
9748
9940
 
9749
9941
  // DBG
9750
- lognow("(SERVER) Registering receive for «"+channelNameParam+"»...");
9942
+ lognow("(SERVER) Registering receive (OLD API) for channel «"+channelNameParam+"»...");
9751
9943
 
9752
- const serverReceptionEntryPoint=new window.ServerReceptionEntryPoint(channelNameParam, clientsRoomsTag, listenerConfig, doOnIncomingMessage);
9944
+ const serverReceptionEntryPoint=new window.ServerReceptionEntryPoint(channelNameParam, clientsRoomsTag, listenerConfig
9945
+ ,((doOnIncomingMessage && isAsyncFunction(doOnIncomingMessage)) ?
9946
+ doOnIncomingMessage
9947
+ : new Promise((resolve,reject)=>{if(doOnIncomingMessage) doOnIncomingMessage();resolve();}))
9948
+ );
9753
9949
  this.serverReceptionEntryPoints.push(serverReceptionEntryPoint);
9754
9950
 
9755
- // SPECIAL FOR THE SOCKETIO IMPLEMENTATION :
9951
+ //OLD: // SPECIAL FOR THE SOCKETIO IMPLEMENTATION :
9756
9952
  // if(window.WebsocketImplementation.useSocketIOImplementation){
9757
- if(window.WebsocketImplementation.implementationToUse){
9758
- window.WebsocketImplementation.implementationToUse.initOnReceiveNodeContext(channelNameParam, serverReceptionEntryPoint);
9953
+ //OLD: if(window.WebsocketImplementation.implementationToUse){
9954
+ //OLD: window.WebsocketImplementation.implementationToUse.initOnReceiveNodeContext(channelNameParam, serverReceptionEntryPoint);
9759
9955
  // const channelName=serverReceptionEntryPoint.channelName;
9760
9956
  // clientSocket.on(channelName, (eventOrMessage)=>{
9761
9957
  // serverReceptionEntryPoint.execute(eventOrMessage, clientSocket);
9762
9958
  // });
9763
- }
9959
+ //OLD: }
9764
9960
 
9765
9961
  return this;
9766
9962
  }
9767
9963
 
9964
+ // NEW API :
9965
+ initReceiveListener(channelName, clientsRoomsTag=null){
9966
+ // DBG
9967
+ lognow("(SERVER) Registering receive (NEW API) for channel «"+channelName+"»...");
9968
+
9969
+ const serverReceptionEntryPointsBag=new window.ServerReceptionEntryPointsBag(channelName, this, clientsRoomsTag);
9970
+ this.serverReceptionEntryPointsBags.push(serverReceptionEntryPointsBag);
9971
+
9972
+ return serverReceptionEntryPointsBag;
9973
+ }
9974
+
9975
+
9768
9976
 
9769
9977
  send(channelName, data, clientsRoomsTag=null, clientSocketParam=null){
9770
9978
 
9771
9979
  // DBG
9772
9980
  lognow("(SERVER) SERVER TRIES TO SEND !");
9773
9981
 
9982
+ // Channel information is stored in exchanged data :
9983
+ let dataWrapped=saveClassesInformation({channelName:channelName, data:data});
9984
+
9774
9985
 
9775
9986
  if(!clientSocketParam){
9776
9987
 
@@ -9786,6 +9997,7 @@ window.NodeServerInstance=class NodeServerInstance{
9786
9997
  // serverClients=this.serverSocket.sockets.sockets;
9787
9998
  }
9788
9999
 
10000
+
9789
10001
  serverClients.forEach((clientSocket)=>{
9790
10002
 
9791
10003
  if(!window.isConnected(clientSocket)) return;
@@ -9793,20 +10005,20 @@ window.NodeServerInstance=class NodeServerInstance{
9793
10005
  // Room information is stored in client socket object :
9794
10006
  if(!window.WebsocketImplementation.isInRoom(clientSocket,clientsRoomsTag)) return;
9795
10007
 
9796
- // Channel information is stored in exchanged data :
9797
- let dataWrapped=saveClassesInformation({channelName:channelName, data:data});
9798
10008
 
9799
10009
  // CAUTION : WE CAN ONLY SEND STRINGS !!!
9800
10010
  // (or else we'll have TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be of type string or an instance of Buffer, ArrayBuffer, or Array or an Array-like Object. Received an instance of Object)
9801
10011
  // (Don't worry, the underlying sub-system will turn it back into Objects without you need to do anything!)
9802
- dataWrapped=stringifyObject(dataWrapped);
10012
+ const dataWrappedStr=stringifyObject(dataWrapped);
9803
10013
 
10014
+ if(clientSocketParam){
9804
10015
  // if(!window.WebsocketImplementation.useSocketIOImplementation)
9805
- if(!window.WebsocketImplementation.implementationToUse){
9806
- clientSocket.send(dataWrapped);
9807
- }else{
9808
- window.WebsocketImplementation.implementationToUse.serverSend(clientSocket, dataWrapped);
9809
- // clientSocket.emit(channelName, dataWrapped);
10016
+ if(!window.WebsocketImplementation.implementationToUse){
10017
+ clientSocket.send(dataWrappedStr);
10018
+ }else{
10019
+ window.WebsocketImplementation.implementationToUse.serverSend(clientSocket, dataWrappedStr);
10020
+ // clientSocket.emit(channelName, dataWrapped);
10021
+ }
9810
10022
  }
9811
10023
 
9812
10024
  });
@@ -9814,7 +10026,7 @@ window.NodeServerInstance=class NodeServerInstance{
9814
10026
  }else{
9815
10027
 
9816
10028
  // DBG
9817
- lognow("(SERVER) (server sends to a client)");
10029
+ lognow("(SERVER) (server sends to a single client)");
9818
10030
 
9819
10031
 
9820
10032
  let clientSocket=clientSocketParam;
@@ -9830,27 +10042,26 @@ window.NodeServerInstance=class NodeServerInstance{
9830
10042
  // Room information is stored in client socket object :
9831
10043
  if(!window.WebsocketImplementation.isInRoom(clientSocket,clientsRoomsTag)) return;
9832
10044
 
9833
- // Channel information is stored in exchanged data :
9834
- let dataWrapped=saveClassesInformation({channelName:channelName, data:data});
9835
10045
 
9836
10046
  // CAUTION : WE CAN ONLY SEND STRINGS !!!
9837
10047
  // (or else we'll have TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be of type string or an instance of Buffer, ArrayBuffer, or Array or an Array-like Object. Received an instance of Object)
9838
10048
  // (Don't worry, the underlying sub-system will turn it back into Objects without you need to do anything!)
9839
- dataWrapped=stringifyObject(dataWrapped);
10049
+ const dataWrappedStr=stringifyObject(dataWrapped);
9840
10050
 
9841
-
9842
- // if(!window.WebsocketImplementation.useSocketIOImplementation)
9843
- if(!window.WebsocketImplementation.implementationToUse){
9844
- clientSocket.send(dataWrapped);
9845
- }else{
9846
- window.WebsocketImplementation.implementationToUse.serverSend(clientSocket, dataWrapped);
9847
- // clientSocket.emit(channelName,dataWrapped);
10051
+ if(clientSocketParam){
10052
+ // if(!window.WebsocketImplementation.useSocketIOImplementation)
10053
+ if(!window.WebsocketImplementation.implementationToUse){
10054
+ clientSocket.send(dataWrappedStr);
10055
+ }else{
10056
+ window.WebsocketImplementation.implementationToUse.serverSend(clientSocket, dataWrappedStr);
10057
+ // clientSocket.emit(channelName,dataWrapped);
10058
+ }
9848
10059
  }
9849
10060
 
9850
10061
  }
9851
10062
 
9852
10063
 
9853
- return this;
10064
+ return dataWrapped;
9854
10065
  }
9855
10066
 
9856
10067
 
@@ -9890,9 +10101,16 @@ window.NodeServerInstance=class NodeServerInstance{
9890
10101
 
9891
10102
 
9892
10103
  // We execute the events registration listeners entry points:
9893
- foreach(self.serverReceptionEntryPoints,(serverReceptionEntryPoint,i)=>{
9894
- serverReceptionEntryPoint.execute(eventOrMessage, clientSocket);
10104
+ // DEPRECATED : OLD API :
10105
+ foreach(self.serverReceptionEntryPoints, (serverReceptionEntryPoint,i)=>{
10106
+ serverReceptionEntryPoint.execute(eventOrMessage, clientSocket, true);
10107
+ });
10108
+
10109
+ // NEW API :
10110
+ foreach(self.serverReceptionEntryPointsBags, (serverReceptionEntryPointsBag,i)=>{
10111
+ serverReceptionEntryPointsBag.executeAll(eventOrMessage, clientSocket, true);
9895
10112
  });
10113
+
9896
10114
  };
9897
10115
 
9898
10116
  // if(!window.WebsocketImplementation.useSocketIOImplementation){
@@ -9900,8 +10118,8 @@ window.NodeServerInstance=class NodeServerInstance{
9900
10118
  // DOES NOT WORK WITH BUN : clientSocket.addEventListener("message", doOnMessage);
9901
10119
  // DOES NOT WORK AT ALL : clientSocket.on("message", doOnMessage);
9902
10120
  clientSocket.onmessage=doOnMessage;
9903
- }else{
9904
- window.WebsocketImplementation.implementationToUse.initOnMessageReceived(doOnMessage);
10121
+ // OLD : }else{
10122
+ // OLD : window.WebsocketImplementation.implementationToUse.initOnMessageReceived(doOnMessage);
9905
10123
  }
9906
10124
 
9907
10125
  doOnConnection(this, clientSocket);
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "aotrautils-srv",
3
- "version": "0.0.2015",
3
+ "version": "0.0.2017",
4
4
  "lockfileVersion": 3,
5
5
  "requires": true,
6
6
  "packages": {
7
7
  "": {
8
8
  "name": "aotrautils-srv",
9
- "version": "0.0.2015",
9
+ "version": "0.0.2017",
10
10
  "license": "HGPL-1.0",
11
11
  "dependencies": {
12
12
  "atob": "^2.1.2",
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aotrautils-srv",
3
- "version": "0.0.2015",
3
+ "version": "0.0.2017",
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)",