puvox-library 1.0.5 → 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.
- package/library_standard.js +144 -108
- package/package.json +1 -1
package/library_standard.js
CHANGED
@@ -2636,21 +2636,19 @@ const puvox_library =
|
|
2636
2636
|
|
2637
2637
|
|
2638
2638
|
|
2639
|
-
|
2640
|
-
|
2641
|
-
|
2642
|
-
|
2643
|
-
return this._fs_instance;
|
2644
|
-
}
|
2645
|
-
else {
|
2646
|
-
let name='fs';
|
2647
|
-
this._fs_instance = require(name);
|
2648
|
-
return this._fs_instance;
|
2649
|
-
}
|
2650
|
-
},
|
2651
|
-
async getRemoteData(url){
|
2639
|
+
async fetch(url, postOptions = null, opts = {}){
|
2640
|
+
return await this.getRemoteData(url, postOptions, opts);
|
2641
|
+
},
|
2642
|
+
async getRemoteData(url, postOptions = null, opts = {}){
|
2652
2643
|
// https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch
|
2653
|
-
|
2644
|
+
const options = {};
|
2645
|
+
if (postOptions) {
|
2646
|
+
options['method'] = 'POST';
|
2647
|
+
options['body'] = JSON.stringify(postOptions);
|
2648
|
+
}
|
2649
|
+
options['headers'] = ('headers' in opts) ? opts.headers : {'Content-Type': 'application/json'};
|
2650
|
+
const fetched = await fetch(url, options);
|
2651
|
+
return (await (fetched).text());
|
2654
2652
|
// return new Promise ((resolve, reject) => {
|
2655
2653
|
// try {
|
2656
2654
|
// // const https = require('https');
|
@@ -2659,25 +2657,8 @@ const puvox_library =
|
|
2659
2657
|
// } catch (ex) { reject (ex); }
|
2660
2658
|
// });
|
2661
2659
|
},
|
2662
|
-
|
2663
|
-
|
2664
|
-
var callback = callback || function(){};
|
2665
|
-
var self = this;
|
2666
|
-
this.fs().readFile(filePath, 'utf8', function(err,data) {
|
2667
|
-
let json = {};
|
2668
|
-
if (typeof data !="undefined" && data!=''){
|
2669
|
-
json=JSON.parse(data);
|
2670
|
-
}
|
2671
|
-
let jsonNew = self.jsonConcat(json, jsonContent);
|
2672
|
-
let content = JSON.stringify(jsonNew);
|
2673
|
-
this.fs().writeFile(filePath, content, 'utf8', function(callback_) {
|
2674
|
-
});
|
2675
|
-
});
|
2676
|
-
}
|
2677
|
-
catch(e){
|
2678
|
-
console.log("writeFileAppendJson", e);
|
2679
|
-
}
|
2680
|
-
},
|
2660
|
+
|
2661
|
+
|
2681
2662
|
// if(setHashInAddress) { window.location.hash = id_or_Name; }
|
2682
2663
|
|
2683
2664
|
|
@@ -2703,94 +2684,149 @@ const puvox_library =
|
|
2703
2684
|
parsed.itemName =defaultValue;
|
2704
2685
|
return this.cacheSet( arrayName, JSON.stringify(parsed) );
|
2705
2686
|
},
|
2706
|
-
// ################################################
|
2707
2687
|
|
2708
|
-
|
2709
|
-
|
2710
|
-
|
2711
|
-
|
2712
|
-
|
2713
|
-
|
2714
|
-
|
2715
|
-
|
2716
|
-
|
2717
|
-
filemtime(filePath){
|
2718
|
-
return (this.fs().statSync(filePath)).mtime;
|
2719
|
-
},
|
2720
|
-
unlink(filePath){
|
2721
|
-
return (this.fs().unlinkSync(filePath));
|
2722
|
-
},
|
2723
|
-
fileGetContents(filePath, defaultt = ''){
|
2724
|
-
if (!this.fileExists(filePath)){
|
2725
|
-
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];
|
2726
2697
|
}
|
2727
|
-
|
2728
|
-
|
2729
|
-
|
2730
|
-
|
2731
|
-
|
2732
|
-
|
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(); },
|
2733
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
|
+
},
|
2734
2747
|
|
2735
|
-
|
2736
|
-
// ########## CACHE DIRS (server-side JS) ##########
|
2737
|
-
getDirTempOS(){ let name='os'; return require(name).tmpdir(); },
|
2738
|
-
customCacheDir:null,
|
2739
|
-
cacheDirGet(appName){
|
2740
|
-
if (!this.customCacheDir){
|
2741
|
-
this.customCacheDir = this.getDirTempOS() + '/_cache_dir_px/';
|
2742
|
-
}
|
2743
|
-
let finaldir = this.customCacheDir + appName + '/';
|
2744
|
-
this.createDirIfNotExists(finaldir);
|
2745
|
-
return finaldir;
|
2746
|
-
},
|
2747
|
-
cacheFileLocation(uniqFileName){
|
2748
|
-
uniqFileName = this.isString(uniqFileName) || this.isNumeric(uniqFileName) ? uniqFileName : JSON.stringify(uniqFileName);
|
2749
|
-
uniqFileName = this.sanitize_key_dashed(this.getCharsFromStart(uniqFileName, 15)) + "_"+ this.md5($uniqFileName);
|
2750
|
-
filePath= this.cacheDirGet() + uniqFileName + "_tmp"; //"/".
|
2751
|
-
return filePath;
|
2752
|
-
},
|
2753
|
-
//
|
2754
|
-
cacheGetFile(uniqFileName, defaultt ='', expire_seconds=8640000, decode = true)
|
2755
|
-
{
|
2756
|
-
let filePath = this.cacheFileLocation(uniqFileName);
|
2757
|
-
if ( filePath.length < 3) return "too tiny filename";
|
2758
2748
|
|
2759
|
-
|
2760
|
-
|
2761
|
-
|
2762
|
-
|
2749
|
+
// ########## CACHE DIRS (server-side JS) ##########
|
2750
|
+
customCacheDir:null,
|
2751
|
+
cacheDirGet(appName = ''){
|
2752
|
+
if (!this.customCacheDir){
|
2753
|
+
this.customCacheDir = this.getTempDir() + '/_cache_dir_px/';
|
2763
2754
|
}
|
2764
|
-
|
2765
|
-
|
2766
|
-
|
2767
|
-
|
2768
|
-
|
2769
|
-
|
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;
|
2770
2775
|
}
|
2771
|
-
|
2772
|
-
|
2773
|
-
|
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;
|
2774
2782
|
}
|
2775
|
-
|
2783
|
+
if (decode){
|
2784
|
+
try{
|
2785
|
+
return JSON.parse(cont);
|
2786
|
+
}
|
2787
|
+
catch(ex){
|
2788
|
+
return cont;
|
2789
|
+
}
|
2790
|
+
}
|
2791
|
+
else{
|
2776
2792
|
return cont;
|
2777
2793
|
}
|
2778
2794
|
}
|
2779
|
-
else{
|
2780
|
-
return cont;
|
2781
|
-
}
|
2782
2795
|
}
|
2783
|
-
|
2784
|
-
|
2785
|
-
|
2786
|
-
}
|
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
|
+
|
2787
2829
|
},
|
2788
|
-
cacheSetFile(uniqFileName, content, encode=true)
|
2789
|
-
{
|
2790
|
-
let filePath= this.cacheFileLocation(uniqFileName);
|
2791
|
-
let contentFinal = (encode && (this.isArray(content) || this.isObject(content)) ) ? JSON.stringify($content): content;
|
2792
|
-
return this.saveFile(filePath, contentFinal);
|
2793
|
-
}
|
2794
2830
|
};
|
2795
2831
|
|
2796
2832
|
|