-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
93 lines (81 loc) · 2.08 KB
/
index.js
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
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
};