Skip to content

Commit b1d7923

Browse files
author
CryptosByte
committed
🩰🐗 Base64 Encoding Command
0 parents  commit b1d7923

File tree

11 files changed

+1435
-0
lines changed

11 files changed

+1435
-0
lines changed

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Image Buffer -> Base64
2+
3+
This program returns a Base64 encoding of an image buffer (from either the web or local file) as STDOUT (a standard output), meaning that you can run the file and use whatever it logs/outputs.
4+
5+
## Help Menu
6+
- `-h <url>` sends a http get request to the url and returns Base64 encoding of the image
7+
- `--http <url>` same as above
8+
- `-r <path/to/file>` fs module reads file and returns Base64 encoding of the image
9+
- `--readFile <path/to/file>` same as above
10+
11+
## How To Build
12+
13+
Install all of the modules...
14+
```sh
15+
$ npm install
16+
```
17+
18+
Compile the TypeScript source code into Commonjs, create executables and store them locally...
19+
```sh
20+
$ npm run build
21+
```
22+
23+
## Why not use the Base64 Linux command?
24+
Because... why not? 😈
25+
26+
(Pull Requests are also accepted | also needing help on how to add commands in NT & OSX)

build/index.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"use strict";
2+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4+
return new (P || (P = Promise))(function (resolve, reject) {
5+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8+
step((generator = generator.apply(thisArg, _arguments || [])).next());
9+
});
10+
};
11+
var __importDefault = (this && this.__importDefault) || function (mod) {
12+
return (mod && mod.__esModule) ? mod : { "default": mod };
13+
};
14+
Object.defineProperty(exports, "__esModule", { value: true });
15+
const fs_1 = require("fs");
16+
const FileError_1 = require("./lib/FileError");
17+
const Base64Encode_1 = require("./lib/Base64Encode");
18+
const node_fetch_1 = __importDefault(require("node-fetch"));
19+
const args = process.argv;
20+
(() => __awaiter(void 0, void 0, void 0, function* () {
21+
if (args.length == 2)
22+
throw new FileError_1.MissingFileError();
23+
if (args.length == 4) {
24+
if (["-h", "--http"].includes(args[2].toLowerCase())) {
25+
const res = yield node_fetch_1.default(args[3]);
26+
const buffer = yield res.buffer();
27+
console.log(`data:${res.headers.get('content-type')};base64,${Base64Encode_1.Base64Encode(buffer)}`);
28+
}
29+
;
30+
if (["-r", "--readFile"].includes(args[2].toLowerCase())) {
31+
try {
32+
fs_1.readFile(args[3], "utf-8", (err, data) => {
33+
if (err)
34+
throw console.error(err);
35+
console.log(Base64Encode_1.Base64Encode(data));
36+
});
37+
}
38+
catch (error) {
39+
throw new FileError_1.FileError();
40+
}
41+
;
42+
}
43+
;
44+
}
45+
;
46+
}))();

build/lib/Base64Encode.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
exports.Base64Encode = void 0;
4+
const Base64Encode = (data) => {
5+
return Buffer.from(data, "binary").toString("base64");
6+
};
7+
exports.Base64Encode = Base64Encode;

build/lib/FileError.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
exports.MissingFileError = exports.FileError = void 0;
4+
class FileError extends Error {
5+
constructor(errorMessage = "File Error: Image file not given") {
6+
super(errorMessage);
7+
this.name = errorMessage;
8+
}
9+
;
10+
}
11+
exports.FileError = FileError;
12+
;
13+
class MissingFileError extends Error {
14+
constructor(errorMessage = "File was not given during execution!") {
15+
super(errorMessage);
16+
this.name = errorMessage;
17+
}
18+
;
19+
}
20+
exports.MissingFileError = MissingFileError;

0 commit comments

Comments
 (0)