aotrautils 0.0.2018 → 0.0.2021

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:01:49)»*/
3
+ /*utils COMMONS library associated with aotra version : «1_29072022-2359 (12/10/2026-21:34:33)»*/
4
4
  /*-----------------------------------------------------------------------------*/
5
5
 
6
6
 
@@ -5485,7 +5485,7 @@ window.AOTRAUTILS_LIB_IS_LOADED=true;
5485
5485
 
5486
5486
 
5487
5487
 
5488
- /*utils CLIENT library associated with aotra version : «1_29072022-2359 (05/10/2026-23:01:49)»*/
5488
+ /*utils CLIENT library associated with aotra version : «1_29072022-2359 (12/10/2026-21:34:33)»*/
5489
5489
  /*-----------------------------------------------------------------------------*/
5490
5490
  /* ## Utility global methods in a browser (htmljs) client environment.
5491
5491
  *
@@ -5567,6 +5567,47 @@ window.getURLParameter=(nameParam)=>{
5567
5567
  return null;
5568
5568
  }
5569
5569
 
5570
+ window.getRequestURLParameters=(request)=>{
5571
+ const url=new URL(request.url, `https://${request.headers.host}`);
5572
+
5573
+ const parameters={};
5574
+ for(const key of new Set(url.searchParams.keys())) {
5575
+ const values=url.searchParams.getAll(key);
5576
+ if(empty(values)) continue;
5577
+ parameters[key]=values[0]; // We ignore duplicate parameters
5578
+ }
5579
+
5580
+ return parameters;
5581
+ };
5582
+
5583
+
5584
+ window.getRequestBody=async function(request, maxSize = 1_000_000){
5585
+ const chunks=[];
5586
+ let size=0;
5587
+ for await(const chunk of request){
5588
+ size+=chunk.length;
5589
+ if(maxSize<size) throw { status: 413, message: "ERROR : Request body too large" };
5590
+ chunks.push(chunk);
5591
+ }
5592
+ const text=Buffer.concat(chunks).toString("utf8");
5593
+ if(!text) return {};
5594
+
5595
+ const type=request.headers["content-type"] || "";
5596
+ if(type.includes("application/json")){
5597
+ try{
5598
+ return JSON.parse(text);
5599
+ }catch{
5600
+ throw { status: 400, message: "ERROR : Invalid JSON" };
5601
+ }
5602
+ }
5603
+
5604
+ if(type.includes("application/x-www-form-urlencoded")){
5605
+ return Object.fromEntries(new URLSearchParams(text));
5606
+ }
5607
+
5608
+ throw { status: 415, message: `ERROR : Unsupported Content-Type: ${type}` };
5609
+ }
5610
+
5570
5611
 
5571
5612
  window.arePathsSimilar=function(path1, path2){
5572
5613
  if(!path1 && !path2) return true;
@@ -13000,7 +13041,7 @@ window.createOritaMicroClient=function(url, port, isNode=false, staticMicroClien
13000
13041
 
13001
13042
 
13002
13043
 
13003
- /*utils GEOMETRY library associated with aotra version : «1_29072022-2359 (05/10/2026-23:01:49)»*/
13044
+ /*utils GEOMETRY library associated with aotra version : «1_29072022-2359 (12/10/2026-21:34:33)»*/
13004
13045
  /*-----------------------------------------------------------------------------*/
13005
13046
 
13006
13047
 
@@ -14089,10 +14130,10 @@ function rayVsUnitSphereClosestPoint(p, r) {
14089
14130
  // MUST REMAIN AT THE END OF THIS LIBRARY FILE !
14090
14131
 
14091
14132
  window.AOTRAUTILS_GEOMETRY_LIB_IS_LOADED=true;
14092
- /*utils 3D library associated with aotra version : «1_29072022-2359 (05/10/2026-23:01:49)»*/
14133
+ /*utils 3D library associated with aotra version : «1_29072022-2359 (12/10/2026-21:34:33)»*/
14093
14134
  /*-----------------------------------------------------------------------------*/
14094
14135
 
14095
- /*utils AI library associated with aotra version : «1_29072022-2359 (05/10/2026-23:01:49)»*/
14136
+ /*utils AI library associated with aotra version : «1_29072022-2359 (12/10/2026-21:34:33)»*/
14096
14137
  /*-----------------------------------------------------------------------------*/
14097
14138
 
14098
14139
 
@@ -14356,7 +14397,7 @@ window.getLLMAPIClient=(modelName, apiURL, agentRole, defaultPrompt, llmProvider
14356
14397
 
14357
14398
 
14358
14399
 
14359
- /*utils CONSOLE library associated with aotra version : «1_29072022-2359 (05/10/2026-23:01:49)»*/
14400
+ /*utils CONSOLE library associated with aotra version : «1_29072022-2359 (12/10/2026-21:34:33)»*/
14360
14401
  /*-----------------------------------------------------------------------------*/
14361
14402
 
14362
14403
 
@@ -16407,12 +16448,24 @@ function toConvention(type="snake",str){
16407
16448
 
16408
16449
 
16409
16450
 
16410
- function capitalize(str){
16451
+ window.capitalize=function(str){
16411
16452
  if(nothing(str)) return "";
16412
16453
  return str.replace(/(?:^|\s)\S/m, firstChar => firstChar.toUpperCase()); // We replace only the first character.
16454
+ };
16455
+
16456
+
16457
+ window.unCamelCase=function(str, forceLowerCase=true){
16458
+ if(nothing(str)) return "";
16459
+ const result=str
16460
+ .replace(/([a-z0-9])([A-Z])/g, "$1 $2") // camelCase -> camel Case
16461
+ .replace(/([A-Z]+)([A-Z][a-z])/g, "$1 $2"); // HTTPResponse -> HTTP Response
16462
+ if(forceLowerCase)
16463
+ return result.toLowerCase();
16464
+ return result;
16413
16465
  }
16414
16466
 
16415
16467
 
16468
+
16416
16469
  /* ## Utility global methods in a javascript, console (nodejs) or vanilla javascript with no browser environment.
16417
16470
  *
16418
16471
  * This set of methods gathers utility generic-purpose methods usable in any JS project.
@@ -16751,11 +16804,19 @@ window.WebsocketImplementation={
16751
16804
  useFlatStrings:false,
16752
16805
 
16753
16806
  // COMMON METHODS
16754
- /*private static*/isInRoom(clientSocket, clientsRoomsTag){
16755
- return (!clientsRoomsTag || empty(clientsRoomsTag) || contains(clientsRoomsTag, clientSocket.clientRoomTag));
16807
+ /*public static*/isInRoom(clientSocket, clientsRoomsTag){
16808
+ return (!clientsRoomsTag || empty(clientsRoomsTag)
16809
+ || !clientSocket || !clientSocket.clientRoomTag || empty(clientSocket.clientRoomTag)
16810
+ || contains(clientsRoomsTag, clientSocket.clientRoomTag));
16756
16811
  },
16757
16812
 
16758
-
16813
+ /*public static*/isInRightChannel(eventOrMessage, channelName){
16814
+ return (!eventOrMessage || empty(eventOrMessage)
16815
+ || !eventOrMessage.channelName || empty(eventOrMessage.channelName)
16816
+ || !channelName || empty(channelName)
16817
+ || eventOrMessage.channelName==channelName);
16818
+ },
16819
+
16759
16820
  //
16760
16821
  // CONSOLE NODE SERVER/CLIENT
16761
16822
  //
@@ -17057,7 +17118,8 @@ window.WebsocketImplementation={
17057
17118
  };
17058
17119
 
17059
17120
 
17060
- window.launchNodeHTTPServer=function(port, doOnConnect=null, doOnFinalizeServer=null, doOnConnectionLost=null, /*OPTIONAL*/sslOptions=null, httpHandlerParam=null,
17121
+ window.launchNodeHTTPServer=function(port, doOnConnect=null, doOnFinalizeServer=null, doOnConnectionLost=null,
17122
+ /*OPTIONAL*/sslOptions=null, httpHandlerParam=null,
17061
17123
  addCORSHeader=ADD_CORS_HEADER_BY_DEFAULT){
17062
17124
 
17063
17125
  const EXCLUDED_FILENAMES_PARTS=[".keyHash.",".pem"];
@@ -17075,28 +17137,32 @@ window.launchNodeHTTPServer=function(port, doOnConnect=null, doOnFinalizeServer=
17075
17137
  }
17076
17138
 
17077
17139
 
17078
- const DEFAULT_HANDLER=function(request, response){
17079
-
17140
+ const EXCLUSION_HANDLER=function(request, response){
17080
17141
  const url=request.url;
17081
-
17082
- let isURLInExclusionZone=!!foreach(EXCLUDED_FILENAMES_PARTS,(excludedStr)=>{
17142
+
17143
+ const isURLInExclusionZone=!!foreach(EXCLUDED_FILENAMES_PARTS,(excludedStr)=>{
17083
17144
  if(contains(url,excludedStr)){
17084
17145
  return true;
17085
17146
  }
17086
17147
  });
17087
-
17088
17148
  if(isURLInExclusionZone){
17089
- // TRACE
17090
- lognow("ERROR 403 forbidden access error :");
17091
- lognow("ERROR : request:",request);
17092
- lognow("ERROR : response:",response);
17093
-
17094
- response.writeHead(403);
17095
- response.end("Sorry, cannot access resource : error: "+error.code+" ..\n");
17096
- response.end();
17097
- return;
17149
+ // TRACE
17150
+ lognow("ERROR 403 forbidden access error :");
17151
+ lognow("ERROR : request:",request);
17152
+ lognow("ERROR : response:",response);
17153
+
17154
+ response.writeHead(403);
17155
+ response.end("Sorry, cannot access resource : error: "+error.code+" ..\n");
17156
+ response.end();
17157
+
17158
+ throw new Error("ERROR : Not allowed to access this ressource.");
17098
17159
  }
17160
+ };
17099
17161
 
17162
+
17163
+ const DEFAULT_HANDLER_HTTP_FILE_RETRIEVAL=function(request, response){
17164
+
17165
+ const url=request.url;
17100
17166
  const urlFile="." + url;
17101
17167
 
17102
17168
  let filePath=urlFile.indexOf("?")!==-1?urlFile.split("?")[0]:urlFile;
@@ -17112,26 +17178,26 @@ window.launchNodeHTTPServer=function(port, doOnConnect=null, doOnFinalizeServer=
17112
17178
 
17113
17179
  window.fs.readFile(filePath, function(error, fileContent){
17114
17180
  if(error){
17115
- if(error.code=="ENOENT"){
17116
- // TRACE
17117
- console.log("ERROR 404 file not found :"+filePath);
17118
-
17119
- window.fs.readFile("./404.html", function(error, fileContent){
17120
- response.writeHead(200, headers);
17121
- response.end(fileContent, "utf-8");
17122
- });
17123
-
17124
- }else {
17125
-
17126
- // TRACE
17127
- lognow("ERROR 500 server error :");
17128
- lognow("ERROR : error:",error);
17129
-
17130
- response.writeHead(500);
17131
- response.end("Sorry, check with the site admin for error: "+error.code+" ..\n");
17132
- response.end();
17181
+ if(error.code=="ENOENT"){
17182
+ // TRACE
17183
+ console.log("ERROR 404 file not found :"+filePath);
17184
+
17185
+ window.fs.readFile("./404.html", function(error, fileContent){
17186
+ response.writeHead(200, headers);
17187
+ response.end(fileContent, "utf-8");
17188
+ });
17133
17189
 
17134
- }
17190
+ }else {
17191
+
17192
+ // TRACE
17193
+ lognow("ERROR 500 server error :");
17194
+ lognow("ERROR : error:",error);
17195
+
17196
+ response.writeHead(500);
17197
+ response.end("Sorry, check with the site admin for error: "+error.code+" ..\n");
17198
+ response.end();
17199
+
17200
+ }
17135
17201
  }else {
17136
17202
 
17137
17203
  // TRACE
@@ -17147,11 +17213,77 @@ window.launchNodeHTTPServer=function(port, doOnConnect=null, doOnFinalizeServer=
17147
17213
 
17148
17214
  }
17149
17215
  });
17150
-
17216
+
17217
+ };
17218
+
17219
+
17220
+ /*private static*/handleHTTPEndpoints=async function(request, response){
17221
+ const url=new URL(request.url, `https://${request.headers.host}`);
17222
+
17223
+ for(const httpEndpoint of handler.httpEndpoints){
17224
+
17225
+
17226
+ // EXPECTED : httpEndpoint={urlPath:"",httpMethod:""}
17227
+
17228
+ if(!httpEndpoint.urlPath || empty(httpEndpoint.urlPath) || url.pathname===httpEndpoint.urlPath){
17229
+ const requestHttpMethod=request.method;
17230
+
17231
+ try{
17232
+ let messageIn=null;
17233
+ if( requestHttpMethod=="GET" &&
17234
+ (!httpEndpoint.httpMethod || empty(httpEndpoint.httpMethod) || httpEndpoint.httpMethod.toUpperCase()=="GET")){
17235
+
17236
+ messageIn={...getRequestURLParameters(request)};
17237
+
17238
+ return true;
17239
+ }else if(requestHttpMethod=="POST"
17240
+ && contains(["POST","PUT","PATCH","DELETE"],httpEndpoint.httpMethod.toUpperCase())){
17241
+
17242
+ messageIn={...getRequestURLParameters(request), ...(await getRequestBody(request))};
17243
+ }
17244
+
17245
+ if(messageIn){
17246
+ let httpEndpointResultObject={httpStatus:200, body:{}};
17247
+ const serverReceptionEntryPoint=httpEndpoint.serverReceptionEntryPoint;
17248
+ if(serverReceptionEntryPoint){
17249
+ httpEndpointResultObject.body=serverReceptionEntryPoint.execute(messageIn, null, false);
17250
+ }
17251
+
17252
+ response.writeHead(httpEndpointResultObject.httpStatus, { "Content-Type": "application/json" });
17253
+ response.end(JSON.stringify(httpEndpointResultObject.body));
17254
+ }
17255
+
17256
+
17257
+ }catch(error) {
17258
+ response.writeHead(erreur.error || 500, { "Content-Type": "application/json" });
17259
+ response.end(JSON.stringify({ error: erreur.error || "ERROR : HTTPO Endpoints encountered an error" }));
17260
+ }
17261
+ }
17262
+
17263
+ }
17264
+
17265
+ return false;
17151
17266
  };
17152
17267
 
17268
+ const handler=function(request, response){
17269
+
17270
+ try{
17271
+ EXCLUSION_HANDLER(request, response);
17272
+ handleHTTPEndpoints(request, response).then(httpEndpointHasInterceptedRequest=>{
17273
+ if(!httpEndpointHasInterceptedRequest){
17274
+ nonull(httpHandlerParam, DEFAULT_HANDLER_HTTP_FILE_RETRIEVAL)(request, response);
17275
+ }
17276
+ }).catch(error=>{
17277
+ // TRACE
17278
+ lognow("ERROR : An error occurred when trying to execute HTTP endpoint :error:",error);
17279
+ });
17280
+ }catch(error){
17281
+ // TRACE
17282
+ lognow("ERROR : An error occurred when trying to process request :error:",error);
17283
+ }
17284
+
17285
+ };
17153
17286
 
17154
- const handler=nonull(httpHandlerParam, DEFAULT_HANDLER);
17155
17287
 
17156
17288
  let listenableServer;
17157
17289
  if(sslOptions){
@@ -17181,6 +17313,12 @@ window.launchNodeHTTPServer=function(port, doOnConnect=null, doOnFinalizeServer=
17181
17313
  // TRACE
17182
17314
  console.log("INFO : SERVER : Generic Nodejs server launched and listening on port:" + port + "!");
17183
17315
 
17316
+
17317
+ // Endpoints handling :
17318
+ server.httpEndpoints=[];
17319
+ handler.httpEndpoints=server.httpEndpoints;
17320
+
17321
+
17184
17322
  return server;
17185
17323
  }
17186
17324
 
@@ -17844,12 +17982,12 @@ if(typeof(window)==="undefined") window=global;
17844
17982
  //
17845
17983
  //window.WebsocketImplementation.implementationToUse.initOnReceiveBrowserContext(channelNameParam, (eventOrMessage)=>{ });
17846
17984
  //
17847
- //window.WebsocketImplementation.implementationToUse.initOnReceiveNodeContext(channelNameParam, serverReceptionEntryPoint);
17985
+ // //OLD: window.WebsocketImplementation.implementationToUse.initOnReceiveNodeContext(channelNameParam, serverReceptionEntryPoint);
17848
17986
  //
17849
17987
  //window.WebsocketImplementation.implementationToUse.getServerClients();
17850
17988
  //
17851
17989
  //if(window.WebsocketImplementation.implementationToUse.isReady(clientSocket)) return;
17852
- // window.WebsocketImplementation.implementationToUse.initOnMessageReceived(doOnMessage);
17990
+ // //OLD : window.WebsocketImplementation.implementationToUse.initOnMessageReceived(doOnMessage);
17853
17991
  // window.WebsocketImplementation.implementationToUse.terminate();
17854
17992
  //
17855
17993
  //window.WebsocketImplementation.implementationToUse.ping();
@@ -18026,9 +18164,7 @@ window.ClientReceptionEntryPoint=class ClientReceptionEntryPoint{
18026
18164
  remove(clientReceptionEntryPoints, this);
18027
18165
 
18028
18166
  if(this.doOnIncomingMessage){
18029
-
18030
18167
  const dataRestored=restoreClassesInformation(dataLocal);
18031
-
18032
18168
  this.doOnIncomingMessage(dataRestored, clientSocket);
18033
18169
  }
18034
18170
 
@@ -18120,7 +18256,6 @@ window.ClientInstance=class ClientInstance{
18120
18256
  return this;
18121
18257
  }
18122
18258
 
18123
-
18124
18259
  send(channelNameParam, data, clientsRoomsTag=null){
18125
18260
 
18126
18261
  // // DBG
@@ -18139,17 +18274,17 @@ window.ClientInstance=class ClientInstance{
18139
18274
  // CAUTION : WE CAN ONLY SEND STRINGS !!!
18140
18275
  // (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)
18141
18276
  // (Don't worry, the underlying sub-system will turn it back into Objects without you need to do anything!)
18142
- dataWrapped=stringifyObject(dataWrapped);
18277
+ const dataWrappedStr=stringifyObject(dataWrapped);
18143
18278
 
18144
18279
  // if(!window.WebsocketImplementation.useSocketIOImplementation)
18145
18280
  if(!window.WebsocketImplementation.implementationToUse){
18146
- clientSocket.send(dataWrapped);
18281
+ clientSocket.send(dataWrappedStr);
18147
18282
  }else{
18148
- window.WebsocketImplementation.implementationToUse.send(dataWrapped);
18283
+ window.WebsocketImplementation.implementationToUse.send(dataWrappedStr);
18149
18284
  // clientSocket.emit(channelNameParam,dataWrapped);
18150
18285
  }
18151
18286
 
18152
- return this;
18287
+ return dataWrapped;
18153
18288
  }
18154
18289
 
18155
18290
 
@@ -18249,16 +18384,114 @@ window.ClientInstance=class ClientInstance{
18249
18384
 
18250
18385
  // ==================================================================================================================
18251
18386
 
18387
+ // NEW API :
18388
+ window.ServerReceptionEntryPointsBag=class ServerReceptionEntryPointsBag{
18389
+
18390
+ constructor(channelNameParam, server, clientsRoomsTag=null){
18391
+ this.channelName=channelNameParam;
18392
+ this.server=server;
18393
+ this.clientsRoomsTag=clientsRoomsTag;
18394
+
18395
+ this.serverReceptionEntryPoints=[];
18396
+ this.prerequisiteMethods=[];
18397
+ }
18398
+
18399
+ // Executed whenever any message is received on any channel :
18400
+ executeAll(eventOrMessage, clientSocket=null){ // FOR REAL-TIME ONLY :
18401
+
18402
+
18403
+ const clientsRoomsTag=this.clientsRoomsTag;
18404
+ if(!window.WebsocketImplementation.isInRoom(clientSocket, clientsRoomsTag)) return;
18405
+
18406
+ const channelName=this.channelName;
18407
+ if(!window.WebsocketImplementation.isInRightChannel(eventOrMessage, channelName)) return;
18408
+
18409
+ executeAllPrerequisites(eventOrMessage).then(prequisiteResult=>{
18410
+ const messageModifiedByPrerequisites=prequisiteResult;
18411
+ foreach(this.serverReceptionEntryPoints, (serverReceptionEntryPoint,i)=>{
18412
+
18413
+ // All listeners are executed IN PARALLEL !
18414
+ serverReceptionEntryPoint.execute(messageModifiedByPrerequisites, clientSocket).then((message)=>{
18415
+ /*DO NOTHING*/
18416
+ lognow("SERVER : DEBUG : Server sent message:",message);
18417
+ }).catch(treatmentsChannelError=>{
18418
+
18419
+ const errorMessage="INFO : An error has occured during the processing of a client message on channel treatments.";
18420
+
18421
+ // TRACE
18422
+ lognow(errorMessage, treatmentsChannelError);
18423
+
18424
+ const errorDataWrapped={errorMessage:errorMessage, cause:treatmentsChannelError};
18425
+ self.sendError(errorDataWrapped, clientSocketParam);
18426
+
18427
+ });
18428
+
18429
+ });
18430
+
18431
+ }).catch(error=>{
18432
+ // TRACE
18433
+ lognow("ERROR : (.executeAll()) A prerequisite encountered an error :", error);
18434
+ });
18435
+
18436
+
18437
+
18438
+ }
18439
+
18440
+ addReceiveListenerPrerequisite(prerequisiteMethod){
18441
+ this.prerequisiteMethods.push(prerequisiteMethod);
18442
+ }
18443
+
18444
+ async executeAllPrerequisites(eventOrMessage){
18445
+ let prequisiteResult=eventOrMessage;
18446
+ let i=1;
18447
+ try {
18448
+ for(const prerequisiteMethod of this.prerequisiteMethods) {
18449
+ prequisiteResult=await prerequisiteMethod(prequisiteResult, clientSocket);
18450
+ i++;
18451
+ }
18452
+ }catch(error){
18453
+ // TRACE
18454
+ lognow("ERROR : (.executeAllPrerequisites()) Prerequisite #"+i+" encountered an error :", error);
18455
+ }
18456
+ return prequisiteResult;
18457
+ }
18458
+
18459
+
18460
+ // ASYNC
18461
+ addReceiveListener(messageType, asyncDoOnIncomingMessage, httpEndpointDefinition=null){
18462
+ const channelNameParam=this.channelNameParam;
18463
+ const clientsRoomsTag=this.clientsRoomsTag;
18464
+ const messageTypes=(!messageType || empty(messageType) ? [] :
18465
+ (isArray(messageType) ? messageType : [messageType])
18466
+ );
18467
+ const listenerConfig={listenerMessageTypes:messageTypes, httpEndpointDefinition:httpEndpointDefinition};
18468
+ const serverReceptionEntryPoint=new window.ServerReceptionEntryPoint(channelNameParam, clientsRoomsTag, listenerConfig, asyncDoOnIncomingMessage);
18469
+ this.serverReceptionEntryPoints.push(serverReceptionEntryPoint);
18470
+
18471
+ if(httpEndpointDefinition){
18472
+ // TRACE
18473
+ lognow("INFO : Registered HTTP listener:",httpEndpointDefinition);
18474
+ this.server.httpEndpoints.push(httpEndpointDefinition);
18475
+ }
18476
+
18477
+ }
18478
+
18479
+
18480
+
18481
+ }
18482
+
18483
+
18484
+
18252
18485
  window.ServerReceptionEntryPoint=class ServerReceptionEntryPoint{
18253
18486
 
18254
- constructor(channelNameParam, clientsRoomsTag, listenerConfig=null, doOnIncomingMessage){
18487
+ constructor(channelNameParam, clientsRoomsTag, listenerConfig=null, asyncDoOnIncomingMessage){
18255
18488
  this.channelName=channelNameParam;
18256
18489
  this.clientsRoomsTag=clientsRoomsTag;
18257
18490
  this.listenerConfig=listenerConfig;
18258
- this.doOnIncomingMessage=doOnIncomingMessage;
18491
+ this.asyncDoOnIncomingMessage=asyncDoOnIncomingMessage;
18259
18492
  }
18260
18493
 
18261
- execute(eventOrMessage, clientSocketParam){
18494
+ execute(eventOrMessage, clientSocketParam=null){
18262
18495
 
18263
18496
  if(!eventOrMessage){
18264
18497
  // TRACE
@@ -18266,13 +18499,13 @@ window.ServerReceptionEntryPoint=class ServerReceptionEntryPoint{
18266
18499
  return;
18267
18500
  }
18268
18501
 
18269
- if(!clientSocketParam){
18270
- // TRACE
18271
- lognow("ERROR : No clientSocket to client ! Cannot do reception treatment.");
18272
- return;
18273
- }
18502
+ // OLD :
18503
+ // if(!clientSocketParam){
18504
+ // // TRACE
18505
+ // lognow("ERROR : No clientSocket to client ! Cannot do reception treatment.");
18506
+ // return;
18507
+ // }
18274
18508
 
18275
-
18276
18509
  // With «ws» library we have no choive than register message events inside a «connection» event !
18277
18510
 
18278
18511
  const dataWrapped=window.WebsocketImplementation.getMessageDataBothImplementations(eventOrMessage);
@@ -18282,28 +18515,23 @@ window.ServerReceptionEntryPoint=class ServerReceptionEntryPoint{
18282
18515
 
18283
18516
  // Room information is stored in client socket object :
18284
18517
  const isClientInRoom=window.WebsocketImplementation.isInRoom(clientSocketParam, this.clientsRoomsTag);
18285
-
18286
- // // DBG
18287
- // lognow("(SERVER) isClientInRoom:",isClientInRoom);
18288
-
18289
18518
  if(!isClientInRoom) return;
18290
18519
 
18291
- // We check if the message matches the required message type :
18292
- if( this.listenerConfig && this.listenerConfig.listenerMessageType
18293
- && dataWrapped.data
18294
- && dataWrapped.data.type && this.listenerConfig.listenerMessageType!==dataWrapped.data.type){
18295
- return;
18520
+
18521
+ if(this.listenerConfig){
18522
+ // (CAUTION : if no message types is configured, then we accept this server listener)
18523
+ if(this.listenerConfig.listenerMessageTypes && !empty(this.listenerConfig.listenerMessageTypes)){
18524
+ // Else we check if the message matches the required message types :
18525
+ if( dataWrapped.data && dataWrapped.data.type
18526
+ && !contains(this.listenerConfig.listenerMessageTypes, dataWrapped.data.type))
18527
+ return;
18528
+ }
18296
18529
  }
18297
18530
 
18298
- if(this.doOnIncomingMessage){
18299
- // // DBG
18300
- // lognow("(SERVER) this.doOnIncomingMessage:");
18301
-
18531
+ if(this.asyncDoOnIncomingMessage){
18302
18532
  const dataLocal=dataWrapped.data;
18303
-
18304
18533
  const dataRestored=restoreClassesInformation(dataLocal);
18305
-
18306
- this.doOnIncomingMessage(dataRestored, clientSocketParam);
18534
+ this.asyncDoOnIncomingMessage(dataRestored, clientSocketParam);
18307
18535
  }
18308
18536
 
18309
18537
  }
@@ -18322,8 +18550,12 @@ window.NodeServerInstance=class NodeServerInstance{
18322
18550
  this.serverSocket=serverSocket;
18323
18551
 
18324
18552
  this.listenableServer=listenableServer;
18553
+
18325
18554
  this.serverReceptionEntryPoints=[];
18555
+ this.serverReceptionEntryPointsBags=[];
18326
18556
 
18557
+ this.httpEndpoints=null;
18558
+
18327
18559
  }
18328
18560
 
18329
18561
  setPingPongTimeout(clientPingIntervalMillis){
@@ -18350,34 +18582,54 @@ window.NodeServerInstance=class NodeServerInstance{
18350
18582
  //sendChainable(channelNameParam, data, clientsRoomsTag=null){
18351
18583
  //}
18352
18584
 
18353
- // I-
18585
+ // I- (NOT ASYNC)
18586
+ // DEPRECATED, USE THE NEW API INSTEAD :
18354
18587
  receive(channelNameParam, doOnIncomingMessage, listenerConfig=null, clientsRoomsTag=null){
18355
18588
 
18356
18589
  // DBG
18357
- lognow("(SERVER) Registering receive for «"+channelNameParam+"»...");
18590
+ lognow("(SERVER) Registering receive (OLD API) for channel «"+channelNameParam+"»...");
18358
18591
 
18359
- const serverReceptionEntryPoint=new window.ServerReceptionEntryPoint(channelNameParam, clientsRoomsTag, listenerConfig, doOnIncomingMessage);
18592
+ const serverReceptionEntryPoint=new window.ServerReceptionEntryPoint(channelNameParam, clientsRoomsTag, listenerConfig
18593
+ ,((doOnIncomingMessage && isAsyncFunction(doOnIncomingMessage)) ?
18594
+ doOnIncomingMessage
18595
+ : new Promise((resolve,reject)=>{if(doOnIncomingMessage) doOnIncomingMessage();resolve();}))
18596
+ );
18360
18597
  this.serverReceptionEntryPoints.push(serverReceptionEntryPoint);
18361
18598
 
18362
- // SPECIAL FOR THE SOCKETIO IMPLEMENTATION :
18599
+ //OLD: // SPECIAL FOR THE SOCKETIO IMPLEMENTATION :
18363
18600
  // if(window.WebsocketImplementation.useSocketIOImplementation){
18364
- if(window.WebsocketImplementation.implementationToUse){
18365
- window.WebsocketImplementation.implementationToUse.initOnReceiveNodeContext(channelNameParam, serverReceptionEntryPoint);
18601
+ //OLD: if(window.WebsocketImplementation.implementationToUse){
18602
+ //OLD: window.WebsocketImplementation.implementationToUse.initOnReceiveNodeContext(channelNameParam, serverReceptionEntryPoint);
18366
18603
  // const channelName=serverReceptionEntryPoint.channelName;
18367
18604
  // clientSocket.on(channelName, (eventOrMessage)=>{
18368
18605
  // serverReceptionEntryPoint.execute(eventOrMessage, clientSocket);
18369
18606
  // });
18370
- }
18607
+ //OLD: }
18371
18608
 
18372
18609
  return this;
18373
18610
  }
18374
18611
 
18612
+ // NEW API :
18613
+ initReceiveListener(channelName, clientsRoomsTag=null){
18614
+ // DBG
18615
+ lognow("(SERVER) Registering receive (NEW API) for channel «"+channelName+"»...");
18616
+
18617
+ const serverReceptionEntryPointsBag=new window.ServerReceptionEntryPointsBag(channelName, this, clientsRoomsTag);
18618
+ this.serverReceptionEntryPointsBags.push(serverReceptionEntryPointsBag);
18619
+
18620
+ return serverReceptionEntryPointsBag;
18621
+ }
18622
+
18623
+
18375
18624
 
18376
18625
  send(channelName, data, clientsRoomsTag=null, clientSocketParam=null){
18377
18626
 
18378
18627
  // DBG
18379
18628
  lognow("(SERVER) SERVER TRIES TO SEND !");
18380
18629
 
18630
+ // Channel information is stored in exchanged data :
18631
+ let dataWrapped=saveClassesInformation({channelName:channelName, data:data});
18632
+
18381
18633
 
18382
18634
  if(!clientSocketParam){
18383
18635
 
@@ -18393,6 +18645,7 @@ window.NodeServerInstance=class NodeServerInstance{
18393
18645
  // serverClients=this.serverSocket.sockets.sockets;
18394
18646
  }
18395
18647
 
18648
+
18396
18649
  serverClients.forEach((clientSocket)=>{
18397
18650
 
18398
18651
  if(!window.isConnected(clientSocket)) return;
@@ -18400,20 +18653,20 @@ window.NodeServerInstance=class NodeServerInstance{
18400
18653
  // Room information is stored in client socket object :
18401
18654
  if(!window.WebsocketImplementation.isInRoom(clientSocket,clientsRoomsTag)) return;
18402
18655
 
18403
- // Channel information is stored in exchanged data :
18404
- let dataWrapped=saveClassesInformation({channelName:channelName, data:data});
18405
18656
 
18406
18657
  // CAUTION : WE CAN ONLY SEND STRINGS !!!
18407
18658
  // (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)
18408
18659
  // (Don't worry, the underlying sub-system will turn it back into Objects without you need to do anything!)
18409
- dataWrapped=stringifyObject(dataWrapped);
18660
+ const dataWrappedStr=stringifyObject(dataWrapped);
18410
18661
 
18662
+ if(clientSocketParam){
18411
18663
  // if(!window.WebsocketImplementation.useSocketIOImplementation)
18412
- if(!window.WebsocketImplementation.implementationToUse){
18413
- clientSocket.send(dataWrapped);
18414
- }else{
18415
- window.WebsocketImplementation.implementationToUse.serverSend(clientSocket, dataWrapped);
18416
- // clientSocket.emit(channelName, dataWrapped);
18664
+ if(!window.WebsocketImplementation.implementationToUse){
18665
+ clientSocket.send(dataWrappedStr);
18666
+ }else{
18667
+ window.WebsocketImplementation.implementationToUse.serverSend(clientSocket, dataWrappedStr);
18668
+ // clientSocket.emit(channelName, dataWrapped);
18669
+ }
18417
18670
  }
18418
18671
 
18419
18672
  });
@@ -18421,7 +18674,7 @@ window.NodeServerInstance=class NodeServerInstance{
18421
18674
  }else{
18422
18675
 
18423
18676
  // DBG
18424
- lognow("(SERVER) (server sends to a client)");
18677
+ lognow("(SERVER) (server sends to a single client)");
18425
18678
 
18426
18679
 
18427
18680
  let clientSocket=clientSocketParam;
@@ -18437,27 +18690,26 @@ window.NodeServerInstance=class NodeServerInstance{
18437
18690
  // Room information is stored in client socket object :
18438
18691
  if(!window.WebsocketImplementation.isInRoom(clientSocket,clientsRoomsTag)) return;
18439
18692
 
18440
- // Channel information is stored in exchanged data :
18441
- let dataWrapped=saveClassesInformation({channelName:channelName, data:data});
18442
18693
 
18443
18694
  // CAUTION : WE CAN ONLY SEND STRINGS !!!
18444
18695
  // (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)
18445
18696
  // (Don't worry, the underlying sub-system will turn it back into Objects without you need to do anything!)
18446
- dataWrapped=stringifyObject(dataWrapped);
18447
-
18697
+ const dataWrappedStr=stringifyObject(dataWrapped);
18448
18698
 
18449
- // if(!window.WebsocketImplementation.useSocketIOImplementation)
18450
- if(!window.WebsocketImplementation.implementationToUse){
18451
- clientSocket.send(dataWrapped);
18452
- }else{
18453
- window.WebsocketImplementation.implementationToUse.serverSend(clientSocket, dataWrapped);
18454
- // clientSocket.emit(channelName,dataWrapped);
18699
+ if(clientSocketParam){
18700
+ // if(!window.WebsocketImplementation.useSocketIOImplementation)
18701
+ if(!window.WebsocketImplementation.implementationToUse){
18702
+ clientSocket.send(dataWrappedStr);
18703
+ }else{
18704
+ window.WebsocketImplementation.implementationToUse.serverSend(clientSocket, dataWrappedStr);
18705
+ // clientSocket.emit(channelName,dataWrapped);
18706
+ }
18455
18707
  }
18456
18708
 
18457
18709
  }
18458
18710
 
18459
18711
 
18460
- return this;
18712
+ return dataWrapped;
18461
18713
  }
18462
18714
 
18463
18715
 
@@ -18497,9 +18749,16 @@ window.NodeServerInstance=class NodeServerInstance{
18497
18749
 
18498
18750
 
18499
18751
  // We execute the events registration listeners entry points:
18500
- foreach(self.serverReceptionEntryPoints,(serverReceptionEntryPoint,i)=>{
18501
- serverReceptionEntryPoint.execute(eventOrMessage, clientSocket);
18752
+ // DEPRECATED : OLD API :
18753
+ foreach(self.serverReceptionEntryPoints, (serverReceptionEntryPoint,i)=>{
18754
+ serverReceptionEntryPoint.execute(eventOrMessage, clientSocket, true);
18755
+ });
18756
+
18757
+ // NEW API :
18758
+ foreach(self.serverReceptionEntryPointsBags, (serverReceptionEntryPointsBag,i)=>{
18759
+ serverReceptionEntryPointsBag.executeAll(eventOrMessage, clientSocket, true);
18502
18760
  });
18761
+
18503
18762
  };
18504
18763
 
18505
18764
  // if(!window.WebsocketImplementation.useSocketIOImplementation){
@@ -18507,8 +18766,8 @@ window.NodeServerInstance=class NodeServerInstance{
18507
18766
  // DOES NOT WORK WITH BUN : clientSocket.addEventListener("message", doOnMessage);
18508
18767
  // DOES NOT WORK AT ALL : clientSocket.on("message", doOnMessage);
18509
18768
  clientSocket.onmessage=doOnMessage;
18510
- }else{
18511
- window.WebsocketImplementation.implementationToUse.initOnMessageReceived(doOnMessage);
18769
+ // OLD : }else{
18770
+ // OLD : window.WebsocketImplementation.implementationToUse.initOnMessageReceived(doOnMessage);
18512
18771
  }
18513
18772
 
18514
18773
  doOnConnection(this, clientSocket);
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "aotrautils",
3
- "version": "0.0.2018",
3
+ "version": "0.0.2021",
4
4
  "lockfileVersion": 3,
5
5
  "requires": true,
6
6
  "packages": {
7
7
  "": {
8
8
  "name": "aotrautils",
9
- "version": "0.0.2018",
9
+ "version": "0.0.2021",
10
10
  "license": "HGPL-1.0",
11
11
  "dependencies": {
12
12
  "atob": "^2.1.2",
aotrautils/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aotrautils",
3
- "version": "0.0.2018",
3
+ "version": "0.0.2021",
4
4
  "main": "aotrautils.build.js",
5
5
  "description": "A library for vanilla javascript utils (client-side) used in aotra javascript CMS",
6
6
  "author": "Jeremie Ratomposon <info@alqemia.com> (https://alqemia.com)",