aotrautils-srv 0.0.801 → 0.0.802

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 (15/02/2024-00:44:20)»*/
3
+ /*utils COMMONS library associated with aotra version : «1_29072022-2359 (16/02/2024-00:55:32)»*/
4
4
  /*-----------------------------------------------------------------------------*/
5
5
 
6
6
 
@@ -5167,7 +5167,7 @@ AOTRAUTILS_LIB_IS_LOADED=true;
5167
5167
 
5168
5168
 
5169
5169
 
5170
- /*utils SERVER library associated with aotra version : «1_29072022-2359 (15/02/2024-00:44:20)»*/
5170
+ /*utils SERVER library associated with aotra version : «1_29072022-2359 (16/02/2024-00:55:32)»*/
5171
5171
  /*-----------------------------------------------------------------------------*/
5172
5172
 
5173
5173
 
@@ -6314,7 +6314,7 @@ WebsocketImplementation={
6314
6314
 
6315
6315
 
6316
6316
 
6317
- launchNodeHTTPServer=function(port, doOnConnect=null, doOnFinalizeServer=null, /*OPTIONAL*/sslOptions=null, httpHandlerParam=null){
6317
+ launchNodeHTTPServer=function(port, doOnConnect=null, doOnFinalizeServer=null, /*OPTIONAL*/sslOptions=null, httpHandlerParam=null, addCORSHeader=ADD_CORS_HEADER){
6318
6318
 
6319
6319
  const EXCLUDED_FILENAMES_PARTS=[".keyHash."];
6320
6320
 
@@ -6365,7 +6365,7 @@ launchNodeHTTPServer=function(port, doOnConnect=null, doOnFinalizeServer=null, /
6365
6365
  const headers={ "Content-Type": contentType };
6366
6366
 
6367
6367
  // To remove the CORS error message (cf. https://medium.com/@dtkatz/3-ways-to-fix-the-cors-error-and-how-access-control-allow-origin-works-d97d55946d9)
6368
- if(ADD_CORS_HEADER) headers["Access-Control-Allow-Origin"]="*";
6368
+ if(addCORSHeader) headers["Access-Control-Allow-Origin"]="*";
6369
6369
 
6370
6370
 
6371
6371
  fs.readFile(filePath, function(error, fileContent){
@@ -6836,18 +6836,18 @@ class ListManager{
6836
6836
  return getArraySize(this.itemsInfos);
6837
6837
  }
6838
6838
 
6839
- }
6839
+ };
6840
6840
 
6841
6841
  getListManager=function(config){
6842
6842
  return new ListManager(config);
6843
- }
6843
+ };
6844
+
6844
6845
 
6845
6846
 
6846
6847
  // NO : IN A NODE CONTEXT WITH requre("") WILL RESULT IN AN UNDEFINED FUNCTION ERROR !!!
6847
- // function performHTTPRequestNode(url,httpMethod="GET",headers={},postRequestBodyOrNamedArgs=null){
6848
+ // function performHTTPRequestNode(...){...
6848
6849
  // USE THIS INSTEAD :
6849
- performHTTPRequestNode=function(completeURL,httpMethod="GET",headers={},postRequestBodyOrNamedArgs=null){
6850
-
6850
+ performHTTPRequestNode=function(completeURL,httpMethod="GET",headers={},requestBodyOrNamedArgs=null,addCORSHeader=ADD_CORS_HEADER){
6851
6851
 
6852
6852
  if(!require){
6853
6853
  // TODO : FIXME : Support also a browser context!!
@@ -6855,7 +6855,7 @@ performHTTPRequestNode=function(completeURL,httpMethod="GET",headers={},postRequ
6855
6855
  throw new Error("Unsupported context non-nodejs for performHTTPRequestNode().");
6856
6856
  }
6857
6857
 
6858
- const isSecure=completeURL.includes("https://");
6858
+ const isSecure=(!empty(completeURL) && contains(completeURL.toLowerCase(),"https://"));
6859
6859
  const httpHandler = isSecure?require("https"):require("http");
6860
6860
 
6861
6861
  // Options for the HTTP request
@@ -6868,9 +6868,19 @@ performHTTPRequestNode=function(completeURL,httpMethod="GET",headers={},postRequ
6868
6868
  options.json=true;
6869
6869
  headers["Content-Type"]="application/json";
6870
6870
  // To remove the CORS error message (cf. https://medium.com/@dtkatz/3-ways-to-fix-the-cors-error-and-how-access-control-allow-origin-works-d97d55946d9)
6871
- if(ADD_CORS_HEADER) headers["Access-Control-Allow-Origin"]="*";
6871
+ if(addCORSHeader) headers["Access-Control-Allow-Origin"]="*";
6872
+ } else if(httpMethod==="GET" && requestBodyOrNamedArgs){
6873
+ // Not the same way to send parameters in GET http method :
6874
+ // DBG
6875
+ lognow("unformatted API URL : "+completeURL);
6876
+
6877
+ completeURL=appendGetParameters(completeURL, requestBodyOrNamedArgs);
6878
+
6879
+ // DBG
6880
+ lognow("formatted API URL : "+completeURL);
6872
6881
  }
6873
6882
 
6883
+
6874
6884
  options.headers=headers;
6875
6885
 
6876
6886
  return new Promise((resolve,reject)=>{
@@ -6889,8 +6899,17 @@ performHTTPRequestNode=function(completeURL,httpMethod="GET",headers={},postRequ
6889
6899
 
6890
6900
  // The whole response has been received.
6891
6901
  response.on("end", () => {
6892
- const responseData=parseJSON(responseDataStr);
6893
- resolve(responseData);
6902
+
6903
+ try{
6904
+ const responseData=parseJSON(responseDataStr);
6905
+ resolve(responseData, response, responseDataStr);
6906
+ }catch(error){
6907
+ // DBG
6908
+ lognow("WARN : Could not JSON parse the response data ! Must deal with the string:«"+responseDataStr+"»", error);
6909
+ resolve(responseDataStr, response, responseDataStr);
6910
+ return;
6911
+ }
6912
+
6894
6913
  });
6895
6914
  });
6896
6915
 
@@ -6903,8 +6922,8 @@ performHTTPRequestNode=function(completeURL,httpMethod="GET",headers={},postRequ
6903
6922
  // Not the same way to send parameters in POST http method :
6904
6923
  if(contains(["POST","PUT"],httpMethod)){
6905
6924
  // (We need to stringify parameters or else we'll have an error :)
6906
- if(!empty(postRequestBodyOrNamedArgs)){
6907
- request.write(stringifyObject(postRequestBodyOrNamedArgs));
6925
+ if(!empty(requestBodyOrNamedArgs)){
6926
+ request.write(stringifyObject(requestBodyOrNamedArgs));
6908
6927
  }
6909
6928
  }
6910
6929
 
@@ -6913,11 +6932,31 @@ performHTTPRequestNode=function(completeURL,httpMethod="GET",headers={},postRequ
6913
6932
 
6914
6933
  });
6915
6934
 
6916
- }
6935
+ };
6917
6936
 
6918
6937
 
6919
6938
 
6920
6939
 
6940
+ replacePathVariablesNamesWithValuesIfPossible=function(apiURL, namedArgs){
6941
+ let result=apiURL;
6942
+ foreach(namedArgs,(namedArgValue, namedArgKey)=>{
6943
+ result=result.replace("{"+namedArgKey+"}",namedArgValue);
6944
+ });
6945
+ return result;
6946
+ };
6947
+
6948
+ appendGetParameters=function(apiURL, namedArgs){
6949
+ let result=apiURL;
6950
+
6951
+ const paramCouples=[];
6952
+ foreach(namedArgs,(value,key)=>{paramCouples.push(key+"="+value);});
6953
+
6954
+ if(!empty(paramCouples)) result+=("?"+paramCouples.join("&"));
6955
+
6956
+ return result;
6957
+ };
6958
+
6959
+
6921
6960
  /* INCLUDED EXTERNAL LIBRAIRIES
6922
6961
  */
6923
6962
  "use strict";var sjcl={cipher:{},hash:{},keyexchange:{},mode:{},misc:{},codec:{},exception:{corrupt:function(a){this.toString=function(){return"CORRUPT: "+this.message};this.message=a},invalid:function(a){this.toString=function(){return"INVALID: "+this.message};this.message=a},bug:function(a){this.toString=function(){return"BUG: "+this.message};this.message=a},notReady:function(a){this.toString=function(){return"NOT READY: "+this.message};this.message=a}}};
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aotrautils-srv",
3
- "version": "0.0.801",
3
+ "version": "0.0.802",
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)",