Skip to content

Commit

Permalink
Merge pull request #1 from SeptBlast/master
Browse files Browse the repository at this point in the history
Code has me modified according to JS coding standards
  • Loading branch information
Devesh Kumar authored Mar 10, 2021
2 parents 9f75895 + e948fb2 commit 4653e9a
Show file tree
Hide file tree
Showing 13 changed files with 432 additions and 147 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
node_modules/
package-lock.json
/src/*
/brotli/*
/gzip/*
29 changes: 21 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
# kit-br-gzip-compression-lib
Kitsune brotli and gzip compression utils library for nodejs

# Steps
- npm install
- Put all files into src folder
- node ./compression.js
- see the output in brotli and gzip folder
# <div align="center"> kit-br-gzip-compression-lib </div>

<div align="center"> Kitsune brotli and gzip compression utils library for nodejs</div>
<br/>
<br/>

---

### <div align="center"> **Steps** </div>

---

> - `npm install`
> - Put all files\* into src folder
> - `npm run start` ||
> - `nodemon run start` for debug purpose
> - see the compressed output files in brotli and gzip folder
---

\*Note: There is no limitation on type of files, you can use this tool for .html, .css, .js, .png, .jpg, .jpeg, .pdf and many more.
Binary file removed brotli/index.html
Binary file not shown.
Binary file removed brotli/script.js
Binary file not shown.
90 changes: 42 additions & 48 deletions compression.js
Original file line number Diff line number Diff line change
@@ -1,69 +1,63 @@


const fs = require("fs");
const path = require('path');
const zlib = require('zlib');
const brotli = require('brotli');
const srcFolder = "./src";
const brotliFolder = "./brotli/";
const gzipFolder = "./gzip/";

fs.readdir(srcFolder, (err, fileNames) => {
//Read all files from src folder and compress it
fileNames.forEach(function (fileName) {

//Brotli compression
let brCompressed = compressBR(fs.readFileSync("src/" + fileName));
if (brCompressed) {
writeToFile(brotliFolder + fileName, brCompressed);
} else {
console.log(`Brotli compression failed for ${fileName}`);
}

//GZIP compression
let gzipCompressed = compressGZ(fs.readFileSync("src/" + fileName));
if (gzipCompressed) {
writeToFile(gzipFolder + fileName, gzipCompressed);
} else {
console.log(`GZIP compression failed for ${fileName}`);
}
});
});

//Helper functions
function compressBR(data) {
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 = brotli.compress(data);
let brCompressed = compress(data);
if (brCompressed) {
return Buffer.from(brCompressed.buffer);
}
} catch (ex) {
console.log("[Error] Unable to compress brotli")
console.log("[Error] Unable to compress brotli");
}
return null;
};

function compressGZ(data) {
/**
* @description
* @param {any} data
* @returns {any}
*/
export const compressGZ = (data) => {
try {
return zlib.gzipSync(data);
return gzipSync(data);
} catch (ex) {
console.log("[Error] Unable to compress gzip")
console.log("[Error] Unable to compress gzip");
}
return null;
};

function writeToFile(path, data){
/**
* @description
* @param {any} path
* @param {any} data
* @returns {any}
*/
export const writeToFile = (path, data) => {
ensureDirectoryExistence(path);
fs.writeFile(path, data, (err) => {
writeFile(path, data, (err) => {
if (err) console.log(err);
console.log("Successfully Written to File " + path);
});
}
function ensureDirectoryExistence(filePath) {
var dirname = path.dirname(filePath);
if (fs.existsSync(dirname)) {
return true;
};

/**
* @description
* @param {any} filePath
* @returns {any}
*/
export const ensureDirectoryExistence = (filePath) => {
var dirname = _dirname(filePath);
if (existsSync(dirname)) {
return true;
}
ensureDirectoryExistence(dirname);
fs.mkdirSync(dirname);
}
mkdirSync(dirname);
};
26 changes: 19 additions & 7 deletions compressionHelper.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
const zlib = require('zlib');
const brotli = require('brotli');
import { gzipSync } from "zlib";
import { compress } from "brotli";

const compressBR = async function (data){
return brotli.compress(data);
/**
* @description
* @param {any} data
* @returns {any}
*/
const compressBR = async function (data) {
return compress(data);
};

/**
* @description
* @param {any} data
* @returns {any}
*/
const compressGZ = async function (data) {
return zlib.gzipSync(data);
return gzipSync(data);
};

exports.compressBR = compressBR;
exports.compressGZ = compressGZ;
const _compressBR = compressBR;
export { _compressBR as compressBR };
const _compressGZ = compressGZ;
export { _compressGZ as compressGZ };
Binary file removed gzip/index.html
Binary file not shown.
Binary file removed gzip/script.js
Binary file not shown.
34 changes: 34 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { readdir, readFileSync } from "fs";
import { compressBR, compressGZ, writeToFile } from "./compression.js";

const srcFolder = "./src";
const brotliFolder = "./brotli/";
const gzipFolder = "./gzip/";

/**
* @description
* @param {any} srcFolder
* @param {any} err
* @param {any} fileNames
* @returns {any}
*/
readdir(srcFolder, (err, fileNames) => {
//Read all files from src folder and compress it
fileNames.forEach(function (fileName) {
//Brotli compression
let brCompressed = compressBR(readFileSync("src/" + fileName));
if (brCompressed) {
writeToFile(brotliFolder + fileName, brCompressed);
} else {
console.log(`Brotli compression failed for ${fileName}`);
}

//GZIP compression
let gzipCompressed = compressGZ(readFileSync("src/" + fileName));
if (gzipCompressed) {
writeToFile(gzipFolder + fileName, gzipCompressed);
} else {
console.log(`GZIP compression failed for ${fileName}`);
}
});
});
Loading

0 comments on commit 4653e9a

Please sign in to comment.