Skip to content

Commit bd06a55

Browse files
committed
following some linting rules , clean unused parts of code
1 parent 99007d9 commit bd06a55

File tree

6 files changed

+663
-518
lines changed

6 files changed

+663
-518
lines changed

cli.js

Lines changed: 113 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
/* eslint-disable no-console */
12
const prompts = require('prompts');
23
const { program } = require('commander');
34
const fs = require('fs');
45
const envfile = require('envfile');
5-
const path = require('path');
66
const { version } = require('./package.json');
77
const Master = require('./src/index');
88

@@ -12,123 +12,134 @@ const sourcePath = '.env';
1212

1313
program.version(version);
1414

15-
function getBase64(file) {
16-
return fs.readFileSync(file, { encoding: 'base64' });
17-
}
1815
function makeid(length) {
19-
let result = '';
20-
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
21-
const charactersLength = characters.length;
22-
for (let i = 0; i < length; i += 1) {
23-
result += characters.charAt(Math.floor(Math.random() * charactersLength));
24-
}
25-
return result;
16+
let result = '';
17+
const characters =
18+
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
19+
const charactersLength = characters.length;
20+
for (let i = 0; i < length; i += 1) {
21+
result += characters.charAt(
22+
Math.floor(Math.random() * charactersLength)
23+
);
24+
}
25+
return result;
2626
}
2727
async function setup() {
28-
console.log('Transfer Encrypt Token is used to encrypt data communications with workers.');
29-
console.log('Master and Workers have to pick the same Transfer Encrypt Token ..');
30-
console.log('Dont share it with dont trusty parties.');
31-
const questions = [
32-
{
33-
type: 'confirm',
34-
name: 'choice',
35-
message: 'Generate new random Transfer Encrypt Token ?',
36-
initial: true,
37-
},
38-
];
39-
const genTransferEncryptToken = await prompts(questions);
40-
if (genTransferEncryptToken.choice) {
41-
const transferEncryptToken = { transferEncryptToken: makeid(32) };
42-
console.log(`Share this token with your workers ${FgYellow}transferEncryptToken${Reset}: ${transferEncryptToken.transferEncryptToken}`);
43-
fs.appendFileSync(sourcePath, envfile.stringify(transferEncryptToken));
44-
} else {
45-
const transferEncryptTokenFromUser = await prompts({
46-
type: 'text',
47-
name: 'transferEncryptToken',
48-
message: 'ENTER Transfer Encrypt Token [32 length string]',
49-
validate: (transferEncryptToken) => (transferEncryptToken.length < 32 ? 'Minimum length is 32' : true),
50-
});
51-
fs.appendFileSync(sourcePath, envfile.stringify(transferEncryptTokenFromUser));
52-
}
28+
console.log(
29+
'Transfer Encrypt Token is used to encrypt data communications with workers.'
30+
);
31+
console.log(
32+
'Master and Workers have to pick the same Transfer Encrypt Token ..'
33+
);
34+
console.log('Dont share it with dont trusty parties.');
35+
const questions = [
36+
{
37+
type: 'confirm',
38+
name: 'choice',
39+
message: 'Generate new random Transfer Encrypt Token ?',
40+
initial: true,
41+
},
42+
];
43+
const genTransferEncryptToken = await prompts(questions);
44+
if (genTransferEncryptToken.choice) {
45+
const transferEncryptToken = { transferEncryptToken: makeid(32) };
46+
console.log(
47+
`Share this token with your workers ${FgYellow}transferEncryptToken${Reset}: ${transferEncryptToken.transferEncryptToken}`
48+
);
49+
fs.appendFileSync(sourcePath, envfile.stringify(transferEncryptToken));
50+
} else {
51+
const transferEncryptTokenFromUser = await prompts({
52+
type: 'text',
53+
name: 'transferEncryptToken',
54+
message: 'ENTER Transfer Encrypt Token [32 length string]',
55+
validate: (transferEncryptToken) =>
56+
transferEncryptToken.length < 32
57+
? 'Minimum length is 32'
58+
: true,
59+
});
60+
fs.appendFileSync(
61+
sourcePath,
62+
envfile.stringify(transferEncryptTokenFromUser)
63+
);
64+
}
5365

54-
const genKeys = await prompts(
55-
{
56-
type: 'confirm',
57-
name: 'gen_keys',
58-
message: 'Create new random token [Will used to find your workers and them you]?',
59-
initial: true,
60-
},
61-
);
62-
if (!genKeys.gen_keys) {
63-
const hashFromUser = await prompts({
64-
type: 'text',
65-
name: 'token',
66-
message: 'Enter your token [atleast 20 length string]',
67-
validate: (token) => (token.length < 20 ? 'Minimum length is 20' : true),
66+
const genKeys = await prompts({
67+
type: 'confirm',
68+
name: 'gen_keys',
69+
message:
70+
'Create new random token [Will used to find your workers and them you]?',
71+
initial: true,
6872
});
69-
fs.appendFileSync(sourcePath, envfile.stringify(hashFromUser));
70-
return;
71-
}
72-
const key = { token: makeid(20) };
73-
console.log(`Share this token with your workers ${FgYellow}token${Reset}: ${key.token}`);
74-
fs.appendFileSync(sourcePath, envfile.stringify(key));
73+
if (!genKeys.gen_keys) {
74+
const hashFromUser = await prompts({
75+
type: 'text',
76+
name: 'token',
77+
message: 'Enter your token [atleast 20 length string]',
78+
validate: (token) =>
79+
token.length < 20 ? 'Minimum length is 20' : true,
80+
});
81+
fs.appendFileSync(sourcePath, envfile.stringify(hashFromUser));
82+
return;
83+
}
84+
const key = { token: makeid(20) };
85+
console.log(
86+
`Share this token with your workers ${FgYellow}token${Reset}: ${key.token}`
87+
);
88+
fs.appendFileSync(sourcePath, envfile.stringify(key));
7589

76-
console.log('Settings stored in .env');
90+
console.log('Settings stored in .env');
7791
}
7892

7993
function resultCollect(result) {
80-
console.dir(result);
94+
console.dir(result);
8195
}
8296
async function run() {
83-
const mm = new Master({
84-
onResults: resultCollect,
85-
execAssets: {
86-
dependencies: [], // pass Worker dependencies like : ['big.js', 'moment']
87-
files: [
88-
// { masterPath: '/src/Logger.js', name: 'Logger.js', workerPath: '/workplace/Logger.js' },
89-
// { masterPath: '/src/Helper.js', name: 'Helper.js', workerPath: '/workplace/Helper.js' },
90-
// { masterPath: '/src/task.js', name: 'task.js', workerPath: '/workplace/task.js' },
91-
],
92-
},
93-
});
94-
const dummy = {
95-
96-
};
97-
98-
let cnt = 0;
99-
for (let i = 0; i < 5; i++) {
100-
const payload = {
101-
id: cnt,
102-
data: JSON.stringify(dummy),
97+
const mm = new Master({
98+
onResults: resultCollect,
99+
execAssets: {
100+
dependencies: [], // pass Worker dependencies like : ['big.js', 'moment']
101+
files: [
102+
// { masterPath: '/src/Logger.js', name: 'Logger.js', workerPath: '/workplace/Logger.js' },
103+
// { masterPath: '/src/Helper.js', name: 'Helper.js', workerPath: '/workplace/Helper.js' },
104+
// { masterPath: '/src/task.js', name: 'task.js', workerPath: '/workplace/task.js' },
105+
],
106+
},
107+
});
108+
const dummy = {};
103109

104-
};
105-
await mm.pushNewJob(payload).catch((e) => console.log(e));
106-
// mm.pushNewJob();
107-
cnt += 1;
108-
}
109-
// setInterval(async () => {
110-
// // console.dir(mm.getQueueLength());
111-
// // const file = getBase64(path.join(process.cwd(), '/task.js'));
110+
let cnt = 0;
111+
for (let i = 0; i < 5; i += 1) {
112+
const payload = {
113+
id: cnt,
114+
data: JSON.stringify(dummy),
115+
};
116+
// eslint-disable-next-line no-await-in-loop
117+
await mm.pushNewJob(payload).catch((e) => console.log(e));
118+
// mm.pushNewJob();
119+
cnt += 1;
120+
}
121+
// setInterval(async () => {
122+
// // console.dir(mm.getQueueLength());
123+
// // const file = getBase64(path.join(process.cwd(), '/task.js'));
112124

113-
// }, 100);
125+
// }, 100);
114126
}
115127

116-
(async function () {
117-
program
118-
.option('-s, --setup', 'Setup/Register master settings');
128+
(async function main() {
129+
program.option('-s, --setup', 'Setup/Register master settings');
119130

120-
program.parse(process.argv);
121-
const options = program.opts();
122-
switch (true) {
123-
case (options.setup):
124-
await setup();
125-
break;
126-
default:
127-
await run();
128-
break;
129-
}
130-
}());
131+
program.parse(process.argv);
132+
const options = program.opts();
133+
switch (true) {
134+
case options.setup:
135+
await setup();
136+
break;
137+
default:
138+
await run();
139+
break;
140+
}
141+
})();
131142

132143
process.on('uncaughtException', (error) => {
133-
console.log(error.message);
144+
console.log(error.message);
134145
});

src/Crypt.js

Lines changed: 46 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,58 @@
1+
/* eslint-disable no-console */
12
const crypto = require('crypto');
23

34
class Crypt {
4-
constructor(key) {
5-
this.key = key || new Error('transferEncryptToken not spcified..');
6-
this.iv = crypto.randomBytes(16);
7-
}
5+
constructor(key) {
6+
this.key = key || new Error('transferEncryptToken not specified..');
7+
this.iv = crypto.randomBytes(16);
8+
}
89

9-
encrypt(text) {
10-
try {
11-
const cipher = crypto.createCipheriv('aes-256-cbc', Buffer.from(this.getKey()), this.getIv());
12-
let encrypted = cipher.update(text);
13-
encrypted = Buffer.concat([encrypted, cipher.final()]);
14-
return { iv: this.getIv().toString('hex'), encryptedData: encrypted.toString('hex') };
15-
} catch (e) {
16-
if (e.message.includes('Invalid key length')) {
17-
console.log('Crypt:', e.message, ' pick a 32 length key');
18-
} else console.log('Crypt:', e.message);
19-
return null;
10+
encrypt(text) {
11+
try {
12+
const cipher = crypto.createCipheriv(
13+
'aes-256-cbc',
14+
Buffer.from(this.getKey()),
15+
this.getIv()
16+
);
17+
let encrypted = cipher.update(text);
18+
encrypted = Buffer.concat([encrypted, cipher.final()]);
19+
return {
20+
iv: this.getIv().toString('hex'),
21+
encryptedData: encrypted.toString('hex'),
22+
};
23+
} catch (e) {
24+
if (e.message.includes('Invalid key length')) {
25+
console.log('Crypt:', e.message, ' pick a 32 length key');
26+
} else console.log('Crypt:', e.message);
27+
return null;
28+
}
2029
}
21-
}
2230

23-
decrypt(text) {
24-
try {
25-
const iv = Buffer.from(text.iv, 'hex');
26-
const encryptedText = Buffer.from(text.encryptedData, 'hex');
27-
const decipher = crypto.createDecipheriv('aes-256-cbc', Buffer.from(this.getKey()), iv);
28-
let decrypted = decipher.update(encryptedText);
29-
decrypted = Buffer.concat([decrypted, decipher.final()]);
30-
return decrypted.toString();
31-
} catch (e) {
32-
console.log('Crypt:', e.message);
33-
return null;
31+
decrypt(text) {
32+
try {
33+
const iv = Buffer.from(text.iv, 'hex');
34+
const encryptedText = Buffer.from(text.encryptedData, 'hex');
35+
const decipher = crypto.createDecipheriv(
36+
'aes-256-cbc',
37+
Buffer.from(this.getKey()),
38+
iv
39+
);
40+
let decrypted = decipher.update(encryptedText);
41+
decrypted = Buffer.concat([decrypted, decipher.final()]);
42+
return decrypted.toString();
43+
} catch (e) {
44+
console.log('Crypt:', e.message);
45+
return null;
46+
}
3447
}
35-
}
3648

37-
getKey() {
38-
return this.key;
39-
}
49+
getKey() {
50+
return this.key;
51+
}
4052

41-
getIv() {
42-
return this.iv;
43-
}
53+
getIv() {
54+
return this.iv;
55+
}
4456
}
4557

4658
module.exports = Crypt;

0 commit comments

Comments
 (0)