puvox-library 1.0.6 → 1.0.8

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 +204 -138
  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,8 @@ 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
+ const text = (await (fetched).text());
2652
+ return text;
2661
2653
  // return new Promise ((resolve, reject) => {
2662
2654
  // try {
2663
2655
  // // const https = require('https');
@@ -2666,147 +2658,221 @@ const puvox_library =
2666
2658
  // } catch (ex) { reject (ex); }
2667
2659
  // });
2668
2660
  },
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
- },
2661
+
2662
+
2697
2663
  // if(setHashInAddress) { window.location.hash = id_or_Name; }
2698
2664
 
2699
2665
 
2700
2666
 
2701
2667
 
2702
2668
  // ######## CACHE ITEMS (client-side JS) ########
2703
- cacheStorageGet(itemName, defaultValue){
2704
- let curr= window.localStorage.getItem('puvox_'+itemName);
2705
- return (curr==null ? defaultValue : curr);
2706
- },
2707
- cacheStorageGetItem(arrayName, itemName, defaultValue){
2708
- let curr = this.cacheStorageGet(arrayName, '{}' );
2709
- let parsed = JSON.parse(curr);
2710
- return (itemName in parsed ? parsed.itemName : defaultValue);
2711
- },
2712
- cacheStorageSet(itemName, value){
2713
- try{ window.localStorage.setItem('puvox_'+itemName, value); return true; }
2714
- catch(ex){ alert("Cache storage quote exceeded. can't save value. err598"); return false; }
2715
- },
2716
- cacheSetItem(arrayName, itemName, value){
2717
- let curr = this.cacheGet(arrayName, '{}' );
2718
- let parsed = JSON.parse(curr);
2719
- parsed.itemName =defaultValue;
2720
- return this.cacheSet( arrayName, JSON.stringify(parsed) );
2721
- },
2722
- // ################################################
2723
-
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;
2742
- }
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
- },
2749
-
2750
-
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
-
2775
- if ( this.fileExists(filePath) ){
2776
- if (this.filemtime(filePath)+expire_seconds<time() ){
2777
- this.unlink(filePath);
2778
- return defaultt;
2669
+ AppName : 'puvox_', //override with anything you want
2670
+ setAppName (name){ this.AppName = name; },
2671
+
2672
+ cache : {
2673
+ localStorage : {
2674
+ parent(){ return puvox_library; },
2675
+ AppName(){ return puvox_library.AppName; },
2676
+
2677
+ storage: typeof window !== 'undefined' ? window.localStorage : null,
2678
+
2679
+ get(name, defaultValue, expireSeconds = 0){
2680
+ let appName = this.AppName();
2681
+ let val = this.storage.getItem(appName + '_' + name);
2682
+ let expireVal = this.storage.getItem(appName + '_createtime_' + name);
2683
+ if (val === null) {
2684
+ return defaultValue;
2685
+ } else {
2686
+ if (expireSeconds === 0){
2687
+ return val;
2688
+ } else {
2689
+ let now = (new Date()).getTime();
2690
+ if (now - expireVal > expireSeconds*1000){
2691
+ this.storage.removeItem(appName + '_' + name);
2692
+ this.storage.removeItem(appName + '_createtime_' + name);
2693
+ return defaultValue;
2694
+ }
2695
+ }
2696
+ }
2697
+ },
2698
+ set(name, value){
2699
+ try{
2700
+ this.storage.setItem(this.AppName() + '_' +name, value);
2701
+ this.storage.setItem(this.AppName() + '_createtime_' +name, (new Date()).getTime());
2702
+ return true;
2703
+ }
2704
+ catch(ex){ alert("Cache storage quote exceeded. can't save value. err598"); return false; }
2705
+ },
2706
+ remove(name, value){
2707
+ this.storage.removeItem(this.AppName() + '_' +name);
2708
+ this.storage.removeItem(this.AppName() + '_createtime_' +name);
2709
+ },
2710
+ getItem(name, subItemName, defaultValue){
2711
+ let val = this.get(name, '{}');
2712
+ let parsed = JSON.parse(val);
2713
+ return (subItemName in parsed ? parsed[subItemName] : defaultValue);
2714
+ },
2715
+ setItem(name, subItemName, value){
2716
+ let curr = this.get(name, '{}' );
2717
+ let parsed = JSON.parse(curr);
2718
+ parsed[subItemName] = value;
2719
+ return this.set(name, JSON.stringify(parsed) );
2720
+ },
2721
+ removeItem(name, subItemName, value){
2722
+ let curr = this.get(name, '{}' );
2723
+ let parsed = JSON.parse(curr);
2724
+ if (subItemName in parsed)
2725
+ delete parsed[subItemName];
2726
+ return this.set(name, JSON.stringify(parsed) );
2779
2727
  }
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;
2728
+ },
2729
+ file : {
2730
+ // ########## CACHE DIRS (server-side JS) ##########
2731
+ parent(){ return puvox_library; },
2732
+ AppName(){ return puvox_library.AppName; },
2733
+
2734
+ customCacheDir:null,
2735
+ dirPath(){
2736
+ if (!this.customCacheDir){
2737
+ this.customCacheDir = this.parent().file.getTempDir() + '/';
2786
2738
  }
2787
- if (decode){
2788
- try{
2789
- return JSON.parse(cont);
2739
+ let finaldir = this.customCacheDir + '_cache' + this.AppName() + '/';
2740
+ return finaldir;
2741
+ },
2742
+ filePath(uniqFileName){
2743
+ const parent = this.parent();
2744
+ uniqFileName = parent.isString(uniqFileName) || parent.isNumeric(uniqFileName) ? uniqFileName : JSON.stringify(uniqFileName);
2745
+ uniqFileName = parent.sanitize_key_dashed(parent.getCharsFromStart(uniqFileName, 15)) + "_"+ parent.md5(uniqFileName);
2746
+ filePath= this.dirPath() + uniqFileName + "_tmp"; //"/".
2747
+ return filePath;
2748
+ },
2749
+ //
2750
+ get(uniqFileName, defaultContent ='', expire_seconds=8640000, decode = true)
2751
+ {
2752
+ const parent = this.parent();
2753
+ let filePath = this.filePath(uniqFileName);
2754
+ if ( filePath.length < 3) return "too tiny filename";
2755
+
2756
+ if ( parent.file.exists(filePath) ){
2757
+ if ( parent.file.mtime(filePath) + expire_seconds *1000 < (new Date()).getTime() ){
2758
+ parent.file.unlink(filePath);
2759
+ return defaultContent;
2790
2760
  }
2791
- catch(ex){
2792
- return cont;
2761
+ else{
2762
+ cont = parent.file.read(filePath, null);
2763
+ // if specifically array, then on empty, reckon as array
2764
+ if (cont===null)
2765
+ {
2766
+ return defaultContent;
2767
+ }
2768
+ if (decode){
2769
+ try{
2770
+ return JSON.parse(cont);
2771
+ }
2772
+ catch(ex){
2773
+ return cont;
2774
+ }
2775
+ }
2776
+ else{
2777
+ return cont;
2778
+ }
2793
2779
  }
2794
2780
  }
2795
- else{
2796
- return cont;
2781
+ else {
2782
+ return defaultContent;
2797
2783
  }
2798
- }
2784
+ },
2785
+ set(uniqFileName, content)
2786
+ {
2787
+ const parent = this.parent();
2788
+ let filePath= this.filePath(uniqFileName);
2789
+ let contentFinal = parent.isString(content) ? content : ((parent.isArray(content) || parent.isObject(content)) ? JSON.stringify(content) : content);
2790
+ return parent.file.write(filePath, contentFinal);
2791
+ },
2792
+
2793
+ //
2794
+ // writeFileAppendJson(filePath, jsonContent, callback){
2795
+ // try{
2796
+ // var callback = callback || function(){};
2797
+ // var self = this;
2798
+ // puvox_library.modules('fs').readFile(filePath, 'utf8', function(err,data) {
2799
+ // let json = {};
2800
+ // if (typeof data !="undefined" && data!=''){
2801
+ // json=JSON.parse(data);
2802
+ // }
2803
+ // let jsonNew = self.jsonConcat(json, jsonContent);
2804
+ // let content = JSON.stringify(jsonNew);
2805
+ // puvox_library.modules('fs').writeFile(filePath, content, 'utf8', function(callback_) {
2806
+ // });
2807
+ // });
2808
+ // }
2809
+ // catch(e){
2810
+ // console.log("writeFileAppendJson", e);
2811
+ // }
2812
+ // },
2799
2813
  }
2800
- else {
2801
- return defaultt;
2814
+ },
2815
+
2816
+ // ################################################
2817
+ // for node packs:_fs_instance :null,
2818
+ _required_instances : {},
2819
+ modules(name){
2820
+ if (name in this._required_instances){
2821
+ return this._required_instances[name];
2822
+ } else {
2823
+ this._required_instances[name] = require(name);
2824
+ return this._required_instances[name];
2802
2825
  }
2826
+ },
2827
+ file : {
2828
+ parent() {return puvox_library;},
2829
+ fs() {return puvox_library.modules('fs');},
2830
+ os() {return puvox_library.modules('os');},
2831
+ path() {return puvox_library.modules('path');},
2832
+ //
2833
+ getTempDir(){ return this.os().tmpdir(); },
2834
+
2835
+ exists(filePath){
2836
+ return this.fs().existsSync(filePath);
2837
+ },
2838
+ mtime(filePath){
2839
+ if (this.exists(filePath)) {
2840
+ return (this.fs().statSync(filePath)).mtimeMs;
2841
+ } else {
2842
+ return null;
2843
+ }
2844
+ },
2845
+ unlink(filePath){
2846
+ return (this.fs().unlinkSync(filePath));
2847
+ },
2848
+ createDirectory(dirPath, force = false){
2849
+ if (!this.exists(dirPath) || force){
2850
+ this.fs().mkdirSync(dirPath, { recursive: true });
2851
+ }
2852
+ },
2853
+ read(filePath, defaultContent = ''){
2854
+ if (!this.exists(filePath)){
2855
+ return defaultContent;
2856
+ }
2857
+ return this.fs().readFileSync(filePath);
2858
+ },
2859
+ write(filePath, content){
2860
+ const dir = this.path().dirname(filePath);
2861
+ this.createDirectory(dir);
2862
+ this.fs().writeFileSync(filePath, content, 'utf8', function(err){
2863
+ if (err) throw err;
2864
+ });
2865
+ },
2866
+ getFilesListFromDir (dir) {
2867
+ const filesList = [];
2868
+ this.fs().readdirSync(dir, (err, files) => {
2869
+ files.forEach(file => {
2870
+ filesList.push(file);
2871
+ });
2872
+ });
2873
+ return filesList;
2874
+ },
2803
2875
  },
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
2876
  };
2811
2877
 
2812
2878
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "puvox-library",
3
- "version": "1.0.6",
3
+ "version": "1.0.8",
4
4
  "description": "library-class-javascript",
5
5
  "main": "library_standard.js",
6
6
  "scripts": {