-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from SeptBlast/master
Code has me modified according to JS coding standards
- Loading branch information
Showing
13 changed files
with
432 additions
and
147 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,5 @@ | ||
node_modules/ | ||
package-lock.json | ||
/src/* | ||
/brotli/* | ||
/gzip/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`); | ||
} | ||
}); | ||
}); |
Oops, something went wrong.