-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathindex.js
96 lines (68 loc) · 2.75 KB
/
index.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/* eslint-disable no-plusplus */
/* eslint-disable max-len */
/* eslint-disable no-console */
/* eslint indent: "error" */
const fs = require('fs');
const { spawn } = require('child_process');
const isFunction = callback => callback instanceof Function
const checkIsParamsString = params => typeof params === 'string'
const checkIfParamsArrayIsEmpty = params => Boolean(params.length)
const checkBufferObject = element => element instanceof Object && element.name && Buffer.isBuffer(element.buffer)
const checkCommandForIO = element => element.includes('-in') || element.includes('-out')
|| element.includes('-keyout') || element.includes('-signkey') || element.includes('-key')
const checkDataTypeCompatibility = (params) => {
const allowedParamsDataTypes = ['string', 'object']
return allowedParamsDataTypes.includes(typeof params)
}
module.exports = function openssl(params, outdir, callback = () => undefined) {
const stdout = [];
const stderr = [];
let dir = 'openssl/';
if(typeof outdir == 'function'){
callback = outdir;
} else {
dir = outdir;
}
let parameters = params
if (!isFunction(callback)) {
throw new Error(`Callback must be a function, but got a ${typeof callback}`)
}
if (!checkDataTypeCompatibility(parameters)) {
throw new Error(`Parameters must be string or an array, but got ${typeof parameters}`);
}
if (checkIsParamsString(parameters)) {
parameters = parameters.split(' ')
}
if (!checkIfParamsArrayIsEmpty(parameters)) throw new Error('Array of params must contain at least one parameter');
if (parameters[0] === 'openssl') parameters.shift()
for (let i = 0; i <= parameters.length - 1; i++) {
if (checkBufferObject(parameters[i])) {
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
const filename = dir + parameters[i].name
fs.writeFileSync(filename, parameters[i].buffer, (err) => {
if (err) {
throw new Error(err);
}
});
parameters[i] = parameters[i].name
parameters[i] = dir + parameters[i];
}
if (checkCommandForIO(parameters[i]) && typeof parameters[i + 1] !== 'object') {
parameters[i + 1] = dir + parameters[i + 1];
}
}
const openSSLProcess = spawn('openssl', parameters);
openSSLProcess.stdout.on('data', (data) => {
stdout.push(data);
});
openSSLProcess.stderr.on('data', (data) => {
stderr.push(data)
});
openSSLProcess.on('close', (code) => {
console.log(`OpenSSL process ends with code ${code}`)
callback.call(null, stderr, stdout);
});
return openSSLProcess
}