aotrautils 0.0.2019 → 0.0.2022

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 (06/10/2026-00:37:28)»*/
3
+ /*utils COMMONS library associated with aotra version : «1_29072022-2359 (12/10/2026-21:52:30)»*/
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 (06/10/2026-00:37:28)»*/
5488
+ /*utils CLIENT library associated with aotra version : «1_29072022-2359 (12/10/2026-21:52:30)»*/
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 (06/10/2026-00:37:28)»*/
13044
+ /*utils GEOMETRY library associated with aotra version : «1_29072022-2359 (12/10/2026-21:52:30)»*/
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 (06/10/2026-00:37:28)»*/
14133
+ /*utils 3D library associated with aotra version : «1_29072022-2359 (12/10/2026-21:52:30)»*/
14093
14134
  /*-----------------------------------------------------------------------------*/
14094
14135
 
14095
- /*utils AI library associated with aotra version : «1_29072022-2359 (06/10/2026-00:37:28)»*/
14136
+ /*utils AI library associated with aotra version : «1_29072022-2359 (12/10/2026-21:52:30)»*/
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 (06/10/2026-00:37:28)»*/
14400
+ /*utils CONSOLE library associated with aotra version : «1_29072022-2359 (12/10/2026-21:52:30)»*/
14360
14401
  /*-----------------------------------------------------------------------------*/
14361
14402
 
14362
14403
 
@@ -16763,11 +16804,19 @@ window.WebsocketImplementation={
16763
16804
  useFlatStrings:false,
16764
16805
 
16765
16806
  // COMMON METHODS
16766
- /*private static*/isInRoom(clientSocket, clientsRoomsTag){
16767
- 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));
16768
16811
  },
16769
16812
 
16770
-
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
+
16771
16820
  //
16772
16821
  // CONSOLE NODE SERVER/CLIENT
16773
16822
  //
@@ -17069,7 +17118,8 @@ window.WebsocketImplementation={
17069
17118
  };
17070
17119
 
17071
17120
 
17072
- 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,
17073
17123
  addCORSHeader=ADD_CORS_HEADER_BY_DEFAULT){
17074
17124
 
17075
17125
  const EXCLUDED_FILENAMES_PARTS=[".keyHash.",".pem"];
@@ -17087,28 +17137,32 @@ window.launchNodeHTTPServer=function(port, doOnConnect=null, doOnFinalizeServer=
17087
17137
  }
17088
17138
 
17089
17139
 
17090
- const DEFAULT_HANDLER=function(request, response){
17091
-
17140
+ const EXCLUSION_HANDLER=function(request, response){
17092
17141
  const url=request.url;
17093
-
17094
- let isURLInExclusionZone=!!foreach(EXCLUDED_FILENAMES_PARTS,(excludedStr)=>{
17142
+
17143
+ const isURLInExclusionZone=!!foreach(EXCLUDED_FILENAMES_PARTS,(excludedStr)=>{
17095
17144
  if(contains(url,excludedStr)){
17096
17145
  return true;
17097
17146
  }
17098
17147
  });
17099
-
17100
17148
  if(isURLInExclusionZone){
17101
- // TRACE
17102
- lognow("ERROR 403 forbidden access error :");
17103
- lognow("ERROR : request:",request);
17104
- lognow("ERROR : response:",response);
17105
-
17106
- response.writeHead(403);
17107
- response.end("Sorry, cannot access resource : error: "+error.code+" ..\n");
17108
- response.end();
17109
- 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.");
17110
17159
  }
17160
+ };
17161
+
17162
+
17163
+ const DEFAULT_HANDLER_HTTP_FILE_RETRIEVAL=function(request, response){
17111
17164
 
17165
+ const url=request.url;
17112
17166
  const urlFile="." + url;
17113
17167
 
17114
17168
  let filePath=urlFile.indexOf("?")!==-1?urlFile.split("?")[0]:urlFile;
@@ -17124,26 +17178,26 @@ window.launchNodeHTTPServer=function(port, doOnConnect=null, doOnFinalizeServer=
17124
17178
 
17125
17179
  window.fs.readFile(filePath, function(error, fileContent){
17126
17180
  if(error){
17127
- if(error.code=="ENOENT"){
17128
- // TRACE
17129
- console.log("ERROR 404 file not found :"+filePath);
17130
-
17131
- window.fs.readFile("./404.html", function(error, fileContent){
17132
- response.writeHead(200, headers);
17133
- response.end(fileContent, "utf-8");
17134
- });
17135
-
17136
- }else {
17137
-
17138
- // TRACE
17139
- lognow("ERROR 500 server error :");
17140
- lognow("ERROR : error:",error);
17141
-
17142
- response.writeHead(500);
17143
- response.end("Sorry, check with the site admin for error: "+error.code+" ..\n");
17144
- 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
+ });
17145
17189
 
17146
- }
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
+ }
17147
17201
  }else {
17148
17202
 
17149
17203
  // TRACE
@@ -17159,11 +17213,77 @@ window.launchNodeHTTPServer=function(port, doOnConnect=null, doOnFinalizeServer=
17159
17213
 
17160
17214
  }
17161
17215
  });
17162
-
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;
17163
17266
  };
17164
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
+ };
17165
17286
 
17166
- const handler=nonull(httpHandlerParam, DEFAULT_HANDLER);
17167
17287
 
17168
17288
  let listenableServer;
17169
17289
  if(sslOptions){
@@ -17193,6 +17313,12 @@ window.launchNodeHTTPServer=function(port, doOnConnect=null, doOnFinalizeServer=
17193
17313
  // TRACE
17194
17314
  console.log("INFO : SERVER : Generic Nodejs server launched and listening on port:" + port + "!");
17195
17315
 
17316
+
17317
+ // Endpoints handling :
17318
+ server.httpEndpoints=[];
17319
+ handler.httpEndpoints=server.httpEndpoints;
17320
+
17321
+
17196
17322
  return server;
17197
17323
  }
17198
17324
 
@@ -17856,12 +17982,12 @@ if(typeof(window)==="undefined") window=global;
17856
17982
  //
17857
17983
  //window.WebsocketImplementation.implementationToUse.initOnReceiveBrowserContext(channelNameParam, (eventOrMessage)=>{ });
17858
17984
  //
17859
- //window.WebsocketImplementation.implementationToUse.initOnReceiveNodeContext(channelNameParam, serverReceptionEntryPoint);
17985
+ // //OLD: window.WebsocketImplementation.implementationToUse.initOnReceiveNodeContext(channelNameParam, serverReceptionEntryPoint);
17860
17986
  //
17861
17987
  //window.WebsocketImplementation.implementationToUse.getServerClients();
17862
17988
  //
17863
17989
  //if(window.WebsocketImplementation.implementationToUse.isReady(clientSocket)) return;
17864
- // window.WebsocketImplementation.implementationToUse.initOnMessageReceived(doOnMessage);
17990
+ // //OLD : window.WebsocketImplementation.implementationToUse.initOnMessageReceived(doOnMessage);
17865
17991
  // window.WebsocketImplementation.implementationToUse.terminate();
17866
17992
  //
17867
17993
  //window.WebsocketImplementation.implementationToUse.ping();
@@ -18038,9 +18164,7 @@ window.ClientReceptionEntryPoint=class ClientReceptionEntryPoint{
18038
18164
  remove(clientReceptionEntryPoints, this);
18039
18165
 
18040
18166
  if(this.doOnIncomingMessage){
18041
-
18042
18167
  const dataRestored=restoreClassesInformation(dataLocal);
18043
-
18044
18168
  this.doOnIncomingMessage(dataRestored, clientSocket);
18045
18169
  }
18046
18170
 
@@ -18132,7 +18256,6 @@ window.ClientInstance=class ClientInstance{
18132
18256
  return this;
18133
18257
  }
18134
18258
 
18135
-
18136
18259
  send(channelNameParam, data, clientsRoomsTag=null){
18137
18260
 
18138
18261
  // // DBG
@@ -18151,17 +18274,17 @@ window.ClientInstance=class ClientInstance{
18151
18274
  // CAUTION : WE CAN ONLY SEND STRINGS !!!
18152
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)
18153
18276
  // (Don't worry, the underlying sub-system will turn it back into Objects without you need to do anything!)
18154
- dataWrapped=stringifyObject(dataWrapped);
18277
+ const dataWrappedStr=stringifyObject(dataWrapped);
18155
18278
 
18156
18279
  // if(!window.WebsocketImplementation.useSocketIOImplementation)
18157
18280
  if(!window.WebsocketImplementation.implementationToUse){
18158
- clientSocket.send(dataWrapped);
18281
+ clientSocket.send(dataWrappedStr);
18159
18282
  }else{
18160
- window.WebsocketImplementation.implementationToUse.send(dataWrapped);
18283
+ window.WebsocketImplementation.implementationToUse.send(dataWrappedStr);
18161
18284
  // clientSocket.emit(channelNameParam,dataWrapped);
18162
18285
  }
18163
18286
 
18164
- return this;
18287
+ return dataWrapped;
18165
18288
  }
18166
18289
 
18167
18290
 
@@ -18261,16 +18384,114 @@ window.ClientInstance=class ClientInstance{
18261
18384
 
18262
18385
  // ==================================================================================================================
18263
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
+
18264
18485
  window.ServerReceptionEntryPoint=class ServerReceptionEntryPoint{
18265
18486
 
18266
- constructor(channelNameParam, clientsRoomsTag, listenerConfig=null, doOnIncomingMessage){
18487
+ constructor(channelNameParam, clientsRoomsTag, listenerConfig=null, asyncDoOnIncomingMessage){
18267
18488
  this.channelName=channelNameParam;
18268
18489
  this.clientsRoomsTag=clientsRoomsTag;
18269
18490
  this.listenerConfig=listenerConfig;
18270
- this.doOnIncomingMessage=doOnIncomingMessage;
18491
+ this.asyncDoOnIncomingMessage=asyncDoOnIncomingMessage;
18271
18492
  }
18272
18493
 
18273
- execute(eventOrMessage, clientSocketParam){
18494
+ execute(eventOrMessage, clientSocketParam=null){
18274
18495
 
18275
18496
  if(!eventOrMessage){
18276
18497
  // TRACE
@@ -18278,13 +18499,13 @@ window.ServerReceptionEntryPoint=class ServerReceptionEntryPoint{
18278
18499
  return;
18279
18500
  }
18280
18501
 
18281
- if(!clientSocketParam){
18282
- // TRACE
18283
- lognow("ERROR : No clientSocket to client ! Cannot do reception treatment.");
18284
- return;
18285
- }
18502
+ // OLD :
18503
+ // if(!clientSocketParam){
18504
+ // // TRACE
18505
+ // lognow("ERROR : No clientSocket to client ! Cannot do reception treatment.");
18506
+ // return;
18507
+ // }
18286
18508
 
18287
-
18288
18509
  // With «ws» library we have no choive than register message events inside a «connection» event !
18289
18510
 
18290
18511
  const dataWrapped=window.WebsocketImplementation.getMessageDataBothImplementations(eventOrMessage);
@@ -18294,28 +18515,23 @@ window.ServerReceptionEntryPoint=class ServerReceptionEntryPoint{
18294
18515
 
18295
18516
  // Room information is stored in client socket object :
18296
18517
  const isClientInRoom=window.WebsocketImplementation.isInRoom(clientSocketParam, this.clientsRoomsTag);
18297
-
18298
- // // DBG
18299
- // lognow("(SERVER) isClientInRoom:",isClientInRoom);
18300
-
18301
18518
  if(!isClientInRoom) return;
18302
18519
 
18303
- // We check if the message matches the required message type :
18304
- if( this.listenerConfig && this.listenerConfig.listenerMessageType
18305
- && dataWrapped.data
18306
- && dataWrapped.data.type && this.listenerConfig.listenerMessageType!==dataWrapped.data.type){
18307
- 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
+ }
18308
18529
  }
18309
18530
 
18310
- if(this.doOnIncomingMessage){
18311
- // // DBG
18312
- // lognow("(SERVER) this.doOnIncomingMessage:");
18313
-
18531
+ if(this.asyncDoOnIncomingMessage){
18314
18532
  const dataLocal=dataWrapped.data;
18315
-
18316
18533
  const dataRestored=restoreClassesInformation(dataLocal);
18317
-
18318
- this.doOnIncomingMessage(dataRestored, clientSocketParam);
18534
+ this.asyncDoOnIncomingMessage(dataRestored, clientSocketParam);
18319
18535
  }
18320
18536
 
18321
18537
  }
@@ -18334,8 +18550,12 @@ window.NodeServerInstance=class NodeServerInstance{
18334
18550
  this.serverSocket=serverSocket;
18335
18551
 
18336
18552
  this.listenableServer=listenableServer;
18553
+
18337
18554
  this.serverReceptionEntryPoints=[];
18555
+ this.serverReceptionEntryPointsBags=[];
18338
18556
 
18557
+ this.httpEndpoints=null;
18558
+
18339
18559
  }
18340
18560
 
18341
18561
  setPingPongTimeout(clientPingIntervalMillis){
@@ -18362,34 +18582,54 @@ window.NodeServerInstance=class NodeServerInstance{
18362
18582
  //sendChainable(channelNameParam, data, clientsRoomsTag=null){
18363
18583
  //}
18364
18584
 
18365
- // I-
18585
+ // I- (NOT ASYNC)
18586
+ // DEPRECATED, USE THE NEW API INSTEAD :
18366
18587
  receive(channelNameParam, doOnIncomingMessage, listenerConfig=null, clientsRoomsTag=null){
18367
18588
 
18368
18589
  // DBG
18369
- lognow("(SERVER) Registering receive for «"+channelNameParam+"»...");
18590
+ lognow("(SERVER) Registering receive (OLD API) for channel «"+channelNameParam+"»...");
18370
18591
 
18371
- 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
+ );
18372
18597
  this.serverReceptionEntryPoints.push(serverReceptionEntryPoint);
18373
18598
 
18374
- // SPECIAL FOR THE SOCKETIO IMPLEMENTATION :
18599
+ //OLD: // SPECIAL FOR THE SOCKETIO IMPLEMENTATION :
18375
18600
  // if(window.WebsocketImplementation.useSocketIOImplementation){
18376
- if(window.WebsocketImplementation.implementationToUse){
18377
- window.WebsocketImplementation.implementationToUse.initOnReceiveNodeContext(channelNameParam, serverReceptionEntryPoint);
18601
+ //OLD: if(window.WebsocketImplementation.implementationToUse){
18602
+ //OLD: window.WebsocketImplementation.implementationToUse.initOnReceiveNodeContext(channelNameParam, serverReceptionEntryPoint);
18378
18603
  // const channelName=serverReceptionEntryPoint.channelName;
18379
18604
  // clientSocket.on(channelName, (eventOrMessage)=>{
18380
18605
  // serverReceptionEntryPoint.execute(eventOrMessage, clientSocket);
18381
18606
  // });
18382
- }
18607
+ //OLD: }
18383
18608
 
18384
18609
  return this;
18385
18610
  }
18386
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
+
18387
18624
 
18388
18625
  send(channelName, data, clientsRoomsTag=null, clientSocketParam=null){
18389
18626
 
18390
18627
  // DBG
18391
18628
  lognow("(SERVER) SERVER TRIES TO SEND !");
18392
18629
 
18630
+ // Channel information is stored in exchanged data :
18631
+ let dataWrapped=saveClassesInformation({channelName:channelName, data:data});
18632
+
18393
18633
 
18394
18634
  if(!clientSocketParam){
18395
18635
 
@@ -18405,6 +18645,7 @@ window.NodeServerInstance=class NodeServerInstance{
18405
18645
  // serverClients=this.serverSocket.sockets.sockets;
18406
18646
  }
18407
18647
 
18648
+
18408
18649
  serverClients.forEach((clientSocket)=>{
18409
18650
 
18410
18651
  if(!window.isConnected(clientSocket)) return;
@@ -18412,20 +18653,20 @@ window.NodeServerInstance=class NodeServerInstance{
18412
18653
  // Room information is stored in client socket object :
18413
18654
  if(!window.WebsocketImplementation.isInRoom(clientSocket,clientsRoomsTag)) return;
18414
18655
 
18415
- // Channel information is stored in exchanged data :
18416
- let dataWrapped=saveClassesInformation({channelName:channelName, data:data});
18417
18656
 
18418
18657
  // CAUTION : WE CAN ONLY SEND STRINGS !!!
18419
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)
18420
18659
  // (Don't worry, the underlying sub-system will turn it back into Objects without you need to do anything!)
18421
- dataWrapped=stringifyObject(dataWrapped);
18660
+ const dataWrappedStr=stringifyObject(dataWrapped);
18422
18661
 
18662
+ if(clientSocketParam){
18423
18663
  // if(!window.WebsocketImplementation.useSocketIOImplementation)
18424
- if(!window.WebsocketImplementation.implementationToUse){
18425
- clientSocket.send(dataWrapped);
18426
- }else{
18427
- window.WebsocketImplementation.implementationToUse.serverSend(clientSocket, dataWrapped);
18428
- // 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
+ }
18429
18670
  }
18430
18671
 
18431
18672
  });
@@ -18433,7 +18674,7 @@ window.NodeServerInstance=class NodeServerInstance{
18433
18674
  }else{
18434
18675
 
18435
18676
  // DBG
18436
- lognow("(SERVER) (server sends to a client)");
18677
+ lognow("(SERVER) (server sends to a single client)");
18437
18678
 
18438
18679
 
18439
18680
  let clientSocket=clientSocketParam;
@@ -18449,27 +18690,26 @@ window.NodeServerInstance=class NodeServerInstance{
18449
18690
  // Room information is stored in client socket object :
18450
18691
  if(!window.WebsocketImplementation.isInRoom(clientSocket,clientsRoomsTag)) return;
18451
18692
 
18452
- // Channel information is stored in exchanged data :
18453
- let dataWrapped=saveClassesInformation({channelName:channelName, data:data});
18454
18693
 
18455
18694
  // CAUTION : WE CAN ONLY SEND STRINGS !!!
18456
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)
18457
18696
  // (Don't worry, the underlying sub-system will turn it back into Objects without you need to do anything!)
18458
- dataWrapped=stringifyObject(dataWrapped);
18697
+ const dataWrappedStr=stringifyObject(dataWrapped);
18459
18698
 
18460
-
18461
- // if(!window.WebsocketImplementation.useSocketIOImplementation)
18462
- if(!window.WebsocketImplementation.implementationToUse){
18463
- clientSocket.send(dataWrapped);
18464
- }else{
18465
- window.WebsocketImplementation.implementationToUse.serverSend(clientSocket, dataWrapped);
18466
- // 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
+ }
18467
18707
  }
18468
18708
 
18469
18709
  }
18470
18710
 
18471
18711
 
18472
- return this;
18712
+ return dataWrapped;
18473
18713
  }
18474
18714
 
18475
18715
 
@@ -18509,9 +18749,16 @@ window.NodeServerInstance=class NodeServerInstance{
18509
18749
 
18510
18750
 
18511
18751
  // We execute the events registration listeners entry points:
18512
- foreach(self.serverReceptionEntryPoints,(serverReceptionEntryPoint,i)=>{
18513
- 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);
18514
18760
  });
18761
+
18515
18762
  };
18516
18763
 
18517
18764
  // if(!window.WebsocketImplementation.useSocketIOImplementation){
@@ -18519,8 +18766,8 @@ window.NodeServerInstance=class NodeServerInstance{
18519
18766
  // DOES NOT WORK WITH BUN : clientSocket.addEventListener("message", doOnMessage);
18520
18767
  // DOES NOT WORK AT ALL : clientSocket.on("message", doOnMessage);
18521
18768
  clientSocket.onmessage=doOnMessage;
18522
- }else{
18523
- window.WebsocketImplementation.implementationToUse.initOnMessageReceived(doOnMessage);
18769
+ // OLD : }else{
18770
+ // OLD : window.WebsocketImplementation.implementationToUse.initOnMessageReceived(doOnMessage);
18524
18771
  }
18525
18772
 
18526
18773
  doOnConnection(this, clientSocket);
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "aotrautils",
3
- "version": "0.0.2019",
3
+ "version": "0.0.2022",
4
4
  "lockfileVersion": 3,
5
5
  "requires": true,
6
6
  "packages": {
7
7
  "": {
8
8
  "name": "aotrautils",
9
- "version": "0.0.2019",
9
+ "version": "0.0.2022",
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.2019",
3
+ "version": "0.0.2022",
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)",