-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompression.js
63 lines (59 loc) · 1.34 KB
/
compression.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
import { writeFile, existsSync, mkdirSync } from "fs";
import { dirname as _dirname } from "path";
import { gzipSync } from "zlib";
import { compress } from "brotli";
/**
* @description Helper functions
* @param {any} data
* @returns {any}
*/
export const compressBR = (data) => {
try {
let brCompressed = compress(data);
if (brCompressed) {
return Buffer.from(brCompressed.buffer);
}
} catch (ex) {
console.log("[Error] Unable to compress brotli");
}
return null;
};
/**
* @description
* @param {any} data
* @returns {any}
*/
export const compressGZ = (data) => {
try {
return gzipSync(data);
} catch (ex) {
console.log("[Error] Unable to compress gzip");
}
return null;
};
/**
* @description
* @param {any} path
* @param {any} data
* @returns {any}
*/
export const writeToFile = (path, data) => {
ensureDirectoryExistence(path);
writeFile(path, data, (err) => {
if (err) console.log(err);
console.log("Successfully Written to File " + path);
});
};
/**
* @description
* @param {any} filePath
* @returns {any}
*/
export const ensureDirectoryExistence = (filePath) => {
var dirname = _dirname(filePath);
if (existsSync(dirname)) {
return true;
}
ensureDirectoryExistence(dirname);
mkdirSync(dirname);
};