1-
2- import { spawn } from 'child_process' ;
1+ import { spawn } from 'child_process'
32
43const isFunction = maybeFunction => typeof maybeFunction === 'function' ;
54
@@ -12,13 +11,14 @@ const expectedStderrForAction = {
1211 'rsa' : / ^ w r i t i n g r s a k e y / i,
1312 'smime.verify' : / ^ v e r i f i c a t i o n s u c c e s s f u l / i,
1413 'x509.req' : / ^ s i g n a t u r e o k / i
15- } ;
14+ }
1615
1716export default function exec ( action , maybeBuffer , maybeOptions , maybeCallback ) {
1817 // Support option re-ordering
19- let buffer = maybeBuffer ;
20- let options = maybeOptions ;
21- let callback = maybeCallback ;
18+ let
19+ buffer = maybeBuffer ,
20+ options = maybeOptions ,
21+ callback = maybeCallback
2222 if ( ! Buffer . isBuffer ( buffer ) ) {
2323 callback = options ;
2424 options = buffer ;
@@ -32,64 +32,67 @@ export default function exec(action, maybeBuffer, maybeOptions, maybeCallback) {
3232 // Build initial params with passed action
3333 let params = action . split ( '.' ) . map ( ( value , key ) => ( ! key ? value : `-${ value } ` ) ) ;
3434 const lastParams = [ ] ;
35- Object . keys ( options ) . forEach ( key => {
36- if ( options [ key ] === false ) {
37- lastParams . push ( key ) ;
38- } else if ( options [ key ] === true ) {
39- params . push ( `-${ key } ` ) ;
40- } else {
41- if ( Array . isArray ( options [ key ] ) ) {
42- options [ key ] . forEach ( value => {
43- params . push ( `-${ key } ` , value ) ;
44- } ) ;
45- } else {
46- params . push ( `-${ key } ` , options [ key ] ) ;
35+ for ( const [ key , value ] of Object . entries ( options ) ) {
36+ switch ( value ) {
37+ case false : lastParams . push ( key ) ; break
38+ case true : params . push ( `-${ key } ` ) ; break
39+ default : {
40+ let itr = (
41+ typeof value === "object"
42+ || typeof value === "function"
43+ ? value [ Symbol . iterator ] : void 0
44+ )
45+ if ( typeof itr !== "function" ) {
46+ params . push ( `${ key } ` , value ) ;
47+ } else for ( const itrValue of itr . call ( value ) ) {
48+ params . push ( `-${ key } ` , itrValue ) ;
49+ }
4750 }
4851 }
49- } ) ;
52+ }
5053 // Append last params
5154 params = params . concat ( lastParams ) ;
5255
5356 // Actually spawn openssl command
54- const openssl = spawn ( 'openssl' , params ) ;
55- const outResult = [ ] ;
56- let outLength = 0 ;
57- const errResult = [ ] ;
58- let errLength = 0 ;
57+ const
58+ openssl = spawn ( 'openssl' , params ) ,
59+ outResult = [ ] , errResult = [ ]
60+ let outLength = 0 , errLength = 0 ;
5961
6062 openssl . stdout . on ( 'data' , data => {
61- outLength += data . length ;
62- outResult . push ( data ) ;
63- } ) ;
63+ outLength += data . length
64+ outResult . push ( data )
65+ } )
6466
6567 openssl . stderr . on ( 'data' , data => {
66- errLength += data . length ;
67- errResult . push ( data ) ;
68- } ) ;
68+ errLength += data . length
69+ errResult . push ( data )
70+ } )
6971
7072 openssl . on ( 'close' , code => {
71- const stdout = Buffer . concat ( outResult , outLength ) ;
72- const stderr = Buffer . concat ( errResult , errLength ) . toString ( 'utf8' ) ;
73- const expectedStderr = expectedStderrForAction [ action ] ;
74- let err = null ;
73+ const
74+ stdout = Buffer . concat ( outResult , outLength ) ,
75+ stderr = Buffer . concat ( errResult , errLength ) . toString ( 'utf8' ) ,
76+ expectedStderr = expectedStderrForAction [ action ]
77+ let err = null
7578
7679 if ( code || ( stderr && expectedStderr && ! stderr . match ( expectedStderr ) ) ) {
77- err = new Error ( stderr ) ;
78- err . code = code ;
80+ err = new Error ( stderr )
81+ err . code = code
7982 }
8083
8184 if ( isFunction ( callback ) ) {
82- callback . apply ( null , [ err , stdout ] ) ;
85+ callback . apply ( null , [ err , stdout ] )
8386 }
84- } ) ;
87+ } )
8588
8689 if ( buffer ) {
87- openssl . stdin . write ( buffer ) ;
90+ openssl . stdin . write ( buffer )
8891 }
8992
90- openssl . stdin . end ( ) ;
93+ openssl . stdin . end ( )
9194
92- return openssl ;
95+ return openssl
9396}
9497
95- export { exec } ;
98+ export { exec }
0 commit comments