11import FS from "fs" ;
22import path from "path" ;
3- import Forge from "node-forge" ;
4- const { pki, md } = Forge ;
3+ import { pki , md } from "node-forge" ;
54import mkdirp from "mkdirp" ;
65import async from "async" ;
6+ import ErrnoException = NodeJS . ErrnoException ;
77
88const CAattrs = [
99 {
@@ -127,12 +127,12 @@ const ServerExtensions = [
127127 } ,
128128] as any [ ] ;
129129
130- export class CA {
131- baseCAFolder : string ;
132- certsFolder : string ;
133- keysFolder : string ;
134- CAcert : ReturnType < typeof pki . createCertificate > ;
135- CAkeys : ReturnType < typeof pki . rsa . generateKeyPair > ;
130+ class CA {
131+ baseCAFolder ! : string ;
132+ certsFolder ! : string ;
133+ keysFolder ! : string ;
134+ CAcert ! : ReturnType < typeof pki . createCertificate > ;
135+ CAkeys ! : ReturnType < typeof pki . rsa . generateKeyPair > ;
136136
137137 static create ( caFolder , callback ) {
138138 const ca = new CA ( ) ;
@@ -177,7 +177,12 @@ export class CA {
177177 return pki . certificateToPem ( this . CAcert ) ;
178178 }
179179
180- generateCA ( callback ) {
180+ generateCA (
181+ callback : (
182+ err ?: ErrnoException | null | undefined ,
183+ results ?: unknown [ ] | undefined
184+ ) => void
185+ ) {
181186 const self = this ;
182187 pki . rsa . generateKeyPair ( { bits : 2048 } , ( err , keys ) => {
183188 if ( err ) {
@@ -198,30 +203,28 @@ export class CA {
198203 cert . sign ( keys . privateKey , md . sha256 . create ( ) ) ;
199204 self . CAcert = cert ;
200205 self . CAkeys = keys ;
201- async . parallel (
202- [
203- FS . writeFile . bind (
204- null ,
205- path . join ( self . certsFolder , "ca.pem" ) ,
206- pki . certificateToPem ( cert )
207- ) ,
208- FS . writeFile . bind (
209- null ,
210- path . join ( self . keysFolder , "ca.private.key" ) ,
211- pki . privateKeyToPem ( keys . privateKey )
212- ) ,
213- FS . writeFile . bind (
214- null ,
215- path . join ( self . keysFolder , "ca.public.key" ) ,
216- pki . publicKeyToPem ( keys . publicKey )
217- ) ,
218- ] ,
219- callback
220- ) ;
206+ const tasks = [
207+ FS . writeFile . bind (
208+ null ,
209+ path . join ( self . certsFolder , "ca.pem" ) ,
210+ pki . certificateToPem ( cert )
211+ ) ,
212+ FS . writeFile . bind (
213+ null ,
214+ path . join ( self . keysFolder , "ca.private.key" ) ,
215+ pki . privateKeyToPem ( keys . privateKey )
216+ ) ,
217+ FS . writeFile . bind (
218+ null ,
219+ path . join ( self . keysFolder , "ca.public.key" ) ,
220+ pki . publicKeyToPem ( keys . publicKey )
221+ ) ,
222+ ] ;
223+ async . parallel ( tasks , callback ) ;
221224 } ) ;
222225 }
223226
224- loadCA ( callback ) {
227+ loadCA ( callback : Function ) {
225228 const self = this ;
226229 async . auto (
227230 {
@@ -243,21 +246,26 @@ export class CA {
243246 ) ;
244247 } ,
245248 } ,
246- ( err , results ) => {
249+ (
250+ err ,
251+ results :
252+ | { certPEM : string ; keyPrivatePEM : string ; keyPublicPEM : string }
253+ | undefined
254+ ) => {
247255 if ( err ) {
248256 return callback ( err ) ;
249257 }
250- self . CAcert = pki . certificateFromPem ( results . certPEM ) ;
258+ self . CAcert = pki . certificateFromPem ( results ! . certPEM ) ;
251259 self . CAkeys = {
252- privateKey : pki . privateKeyFromPem ( results . keyPrivatePEM ) ,
253- publicKey : pki . publicKeyFromPem ( results . keyPublicPEM ) ,
260+ privateKey : pki . privateKeyFromPem ( results ! . keyPrivatePEM ) ,
261+ publicKey : pki . publicKeyFromPem ( results ! . keyPublicPEM ) ,
254262 } ;
255263 return callback ( ) ;
256264 }
257265 ) ;
258266 }
259267
260- generateServerCertificateKeys ( hosts , cb ) {
268+ generateServerCertificateKeys ( hosts : string | string [ ] , cb ) {
261269 const self = this ;
262270 if ( typeof hosts === "string" ) {
263271 hosts = [ hosts ] ;
0 commit comments