puvox-library 1.0.6 → 1.0.7

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.
Files changed (2) hide show
  1. package/library_standard.js +135 -115
  2. package/package.json +1 -1
@@ -2636,17 +2636,8 @@ const puvox_library =
2636
2636
 
2637
2637
 
2638
2638
 
2639
- // for node packs:
2640
- _fs_instance :null,
2641
- fs(){
2642
- if (this._fs_instance){
2643
- return this._fs_instance;
2644
- }
2645
- else {
2646
- let name='fs';
2647
- this._fs_instance = require(name);
2648
- return this._fs_instance;
2649
- }
2639
+ async fetch(url, postOptions = null, opts = {}){
2640
+ return await this.getRemoteData(url, postOptions, opts);
2650
2641
  },
2651
2642
  async getRemoteData(url, postOptions = null, opts = {}){
2652
2643
  // https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch
@@ -2657,7 +2648,7 @@ const puvox_library =
2657
2648
  }
2658
2649
  options['headers'] = ('headers' in opts) ? opts.headers : {'Content-Type': 'application/json'};
2659
2650
  const fetched = await fetch(url, options);
2660
- return await (fetched).text();
2651
+ return (await (fetched).text());
2661
2652
  // return new Promise ((resolve, reject) => {
2662
2653
  // try {
2663
2654
  // // const https = require('https');
@@ -2666,34 +2657,8 @@ const puvox_library =
2666
2657
  // } catch (ex) { reject (ex); }
2667
2658
  // });
2668
2659
  },
2669
- writeFileAppendJson(filePath, jsonContent, callback){
2670
- try{
2671
- var callback = callback || function(){};
2672
- var self = this;
2673
- this.fs().readFile(filePath, 'utf8', function(err,data) {
2674
- let json = {};
2675
- if (typeof data !="undefined" && data!=''){
2676
- json=JSON.parse(data);
2677
- }
2678
- let jsonNew = self.jsonConcat(json, jsonContent);
2679
- let content = JSON.stringify(jsonNew);
2680
- self.fs().writeFile(filePath, content, 'utf8', function(callback_) {
2681
- });
2682
- });
2683
- }
2684
- catch(e){
2685
- console.log("writeFileAppendJson", e);
2686
- }
2687
- },
2688
- getFilesListFromDir (dir) {
2689
- const filesList = [];
2690
- this.fs().readdirSync(dir, (err, files) => {
2691
- files.forEach(file => {
2692
- filesList.push(file);
2693
- });
2694
- });
2695
- return filesList;
2696
- },
2660
+
2661
+
2697
2662
  // if(setHashInAddress) { window.location.hash = id_or_Name; }
2698
2663
 
2699
2664
 
@@ -2719,94 +2684,149 @@ const puvox_library =
2719
2684
  parsed.itemName =defaultValue;
2720
2685
  return this.cacheSet( arrayName, JSON.stringify(parsed) );
2721
2686
  },
2722
- // ################################################
2723
2687
 
2724
-
2725
- createDirIfNotExists(dirPath){
2726
- if (!this.fs().existsSync(dirPath)){
2727
- this.fs().mkdirSync(dirPath);
2728
- }
2729
- },
2730
- fileExists(filePath){
2731
- return this.fs().existsSync(filePath);
2732
- },
2733
- filemtime(filePath){
2734
- return (this.fs().statSync(filePath)).mtime;
2735
- },
2736
- unlink(filePath){
2737
- return (this.fs().unlinkSync(filePath));
2738
- },
2739
- fileGetContents(filePath, defaultt = ''){
2740
- if (!this.fileExists(filePath)){
2741
- return defaultt;
2688
+ // ################################################
2689
+ // for node packs:_fs_instance :null,
2690
+ _required_instances : {},
2691
+ modules(name){
2692
+ if (name in this._required_instances){
2693
+ return this._required_instances[name];
2694
+ } else {
2695
+ this._required_instances[name] = require(name);
2696
+ return this._required_instances[name];
2742
2697
  }
2743
- return (this.fs().readFileSync(filePath));
2744
- },
2745
- saveFile(filePath, content){ this.fs().writeFileSync(filePath,content, 'utf8', function(err){
2746
- if (err) throw err;
2747
- });
2748
- },
2698
+ },
2699
+ file : {
2700
+ parent() {return puvox_library;},
2701
+ fs() {return puvox_library.modules('fs');},
2702
+ os() {return puvox_library.modules('os');},
2703
+ path() {return puvox_library.modules('path');},
2704
+ //
2705
+ getTempDir(){ return this.os().tmpdir(); },
2749
2706
 
2707
+ exists(filePath){
2708
+ return this.fs().existsSync(filePath);
2709
+ },
2710
+ mtime(filePath){
2711
+ if (this.exists(filePath)) {
2712
+ return (this.fs().statSync(filePath)).mtimeMs;
2713
+ } else {
2714
+ return null;
2715
+ }
2716
+ },
2717
+ unlink(filePath){
2718
+ return (this.fs().unlinkSync(filePath));
2719
+ },
2720
+ createDirectory(dirPath, force = false){
2721
+ if (!this.exists(dirPath) || force){
2722
+ this.fs().mkdirSync(dirPath, { recursive: true });
2723
+ }
2724
+ },
2725
+ read(filePath, defaultContent = ''){
2726
+ if (!this.exists(filePath)){
2727
+ return defaultContent;
2728
+ }
2729
+ return this.fs().readFileSync(filePath);
2730
+ },
2731
+ write(filePath, content){
2732
+ const dir = this.path().dirname(filePath);
2733
+ this.createDirectory(dir);
2734
+ this.fs().writeFileSync(filePath, content, 'utf8', function(err){
2735
+ if (err) throw err;
2736
+ });
2737
+ },
2738
+ getFilesListFromDir (dir) {
2739
+ const filesList = [];
2740
+ this.fs().readdirSync(dir, (err, files) => {
2741
+ files.forEach(file => {
2742
+ filesList.push(file);
2743
+ });
2744
+ });
2745
+ return filesList;
2746
+ },
2750
2747
 
2751
-
2752
- // ########## CACHE DIRS (server-side JS) ##########
2753
- getDirTempOS(){ let name='os'; return require(name).tmpdir(); },
2754
- customCacheDir:null,
2755
- cacheDirGet(appName){
2756
- if (!this.customCacheDir){
2757
- this.customCacheDir = this.getDirTempOS() + '/_cache_dir_px/';
2758
- }
2759
- let finaldir = this.customCacheDir + appName + '/';
2760
- this.createDirIfNotExists(finaldir);
2761
- return finaldir;
2762
- },
2763
- cacheFileLocation(uniqFileName){
2764
- uniqFileName = this.isString(uniqFileName) || this.isNumeric(uniqFileName) ? uniqFileName : JSON.stringify(uniqFileName);
2765
- uniqFileName = this.sanitize_key_dashed(this.getCharsFromStart(uniqFileName, 15)) + "_"+ this.md5($uniqFileName);
2766
- filePath= this.cacheDirGet() + uniqFileName + "_tmp"; //"/".
2767
- return filePath;
2768
- },
2769
- //
2770
- cacheGetFile(uniqFileName, defaultt ='', expire_seconds=8640000, decode = true)
2771
- {
2772
- let filePath = this.cacheFileLocation(uniqFileName);
2773
- if ( filePath.length < 3) return "too tiny filename";
2774
2748
 
2775
- if ( this.fileExists(filePath) ){
2776
- if (this.filemtime(filePath)+expire_seconds<time() ){
2777
- this.unlink(filePath);
2778
- return defaultt;
2749
+ // ########## CACHE DIRS (server-side JS) ##########
2750
+ customCacheDir:null,
2751
+ cacheDirGet(appName = ''){
2752
+ if (!this.customCacheDir){
2753
+ this.customCacheDir = this.getTempDir() + '/_cache_dir_px/';
2779
2754
  }
2780
- else{
2781
- cont = this.fileGetContents(filePath, null);
2782
- // if specifically array, then on empty, reckon as array
2783
- if (cont===null)
2784
- {
2785
- return defaultt;
2755
+ let finaldir = this.customCacheDir + appName + '/';
2756
+ return finaldir;
2757
+ },
2758
+ cacheFilePath(uniqFileName){
2759
+ const parent = this.parent();
2760
+ uniqFileName = parent.isString(uniqFileName) || parent.isNumeric(uniqFileName) ? uniqFileName : JSON.stringify(uniqFileName);
2761
+ uniqFileName = parent.sanitize_key_dashed(parent.getCharsFromStart(uniqFileName, 15)) + "_"+ parent.md5(uniqFileName);
2762
+ filePath= this.cacheDirGet() + uniqFileName + "_tmp"; //"/".
2763
+ return filePath;
2764
+ },
2765
+ //
2766
+ cacheGet(uniqFileName, defaultContent ='', expire_seconds=8640000, decode = true)
2767
+ {
2768
+ let filePath = this.cacheFilePath(uniqFileName);
2769
+ if ( filePath.length < 3) return "too tiny filename";
2770
+
2771
+ if ( this.exists(filePath) ){
2772
+ if ( this.mtime(filePath) + expire_seconds *1000 < (new Date()).getTime() ){
2773
+ this.unlink(filePath);
2774
+ return defaultContent;
2786
2775
  }
2787
- if (decode){
2788
- try{
2789
- return JSON.parse(cont);
2776
+ else{
2777
+ cont = this.read(filePath, null);
2778
+ // if specifically array, then on empty, reckon as array
2779
+ if (cont===null)
2780
+ {
2781
+ return defaultContent;
2782
+ }
2783
+ if (decode){
2784
+ try{
2785
+ return JSON.parse(cont);
2786
+ }
2787
+ catch(ex){
2788
+ return cont;
2789
+ }
2790
2790
  }
2791
- catch(ex){
2791
+ else{
2792
2792
  return cont;
2793
2793
  }
2794
2794
  }
2795
- else{
2796
- return cont;
2797
- }
2798
2795
  }
2799
- }
2800
- else {
2801
- return defaultt;
2802
- }
2796
+ else {
2797
+ return defaultContent;
2798
+ }
2799
+ },
2800
+ cacheSet(uniqFileName, content, encode=true)
2801
+ {
2802
+ const parent = this.parent();
2803
+ let filePath= this.cacheFilePath(uniqFileName);
2804
+ let contentFinal = (encode && (parent.isArray(content) || parent.isObject(content)) ) ? JSON.stringify(content) : content;
2805
+ return this.write(filePath, contentFinal);
2806
+ },
2807
+
2808
+ //
2809
+ // writeFileAppendJson(filePath, jsonContent, callback){
2810
+ // try{
2811
+ // var callback = callback || function(){};
2812
+ // var self = this;
2813
+ // puvox_library.modules('fs').readFile(filePath, 'utf8', function(err,data) {
2814
+ // let json = {};
2815
+ // if (typeof data !="undefined" && data!=''){
2816
+ // json=JSON.parse(data);
2817
+ // }
2818
+ // let jsonNew = self.jsonConcat(json, jsonContent);
2819
+ // let content = JSON.stringify(jsonNew);
2820
+ // puvox_library.modules('fs').writeFile(filePath, content, 'utf8', function(callback_) {
2821
+ // });
2822
+ // });
2823
+ // }
2824
+ // catch(e){
2825
+ // console.log("writeFileAppendJson", e);
2826
+ // }
2827
+ // },
2828
+
2803
2829
  },
2804
- cacheSetFile(uniqFileName, content, encode=true)
2805
- {
2806
- let filePath= this.cacheFileLocation(uniqFileName);
2807
- let contentFinal = (encode && (this.isArray(content) || this.isObject(content)) ) ? JSON.stringify($content): content;
2808
- return this.saveFile(filePath, contentFinal);
2809
- }
2810
2830
  };
2811
2831
 
2812
2832
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "puvox-library",
3
- "version": "1.0.6",
3
+ "version": "1.0.7",
4
4
  "description": "library-class-javascript",
5
5
  "main": "library_standard.js",
6
6
  "scripts": {