All files index.js

97.85% Statements 91/93
88.89% Branches 8/9
75% Functions 6/8
97.85% Lines 91/93

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 941x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
import {readFile,readFileSync,writeFile,writeFileSync} from 'fs';
import fetch from 'node-fetch';
import Is from 'strong-type';
 
const is=new Is;
 
 
class Base64File{
    constructor(){
 
    }
 
    load(
        path,
        fileName,
        callback=function(err,data){}
    ){
        return readFile(
            `${path}${fileName}`,
            //finess required
            function(err,data){
                if(err){
                    return callback(err,data);
                }
 
                const base64=Buffer.from(data).toString('base64');
 
                return callback(err,base64);
            }
        );
    }
 
    loadSync(path,fileName) {
        //console.log(`${path}${fileName}`);
        const file = readFileSync(`${path}${fileName}`);
        return Buffer.from(file).toString('base64');
    }
 
    save(
        data='err',
        path='./',
        fileName='error.txt',
        callback=function(err){}
    ){
        return writeFile(
            `${path}${fileName}`,
            data,
            {
                encoding: 'base64'
            },
            //no finessing required
            callback
        );
    }
 
    saveSync(
        data='err',
        path='./',
        fileName='error.txt'
    ){
        return writeFileSync(
            `${path}${fileName}`,
            data,
            {
                encoding: 'base64'
            }
        );
    }
 
    async loadRemote(
        url,
        file,
        fetchParams={
            // These properties are part of the Fetch Standard
            // https://github.com/node-fetch/node-fetch#options
            method: 'GET',
            headers: {},            // Request headers. format is the identical to that accepted by the Headers constructor (see below)
            body: null
        }
    ){
        is.string(url);
        is.string(file);
        is.object(fetchParams);
        const response = await fetch(`${url}${file}`,fetchParams);
        const buffer = await response.buffer();     
        return buffer.toString('base64');        
    }
}
 
export {
    Base64File as default,
    Base64File
};