file author tngan
*  desc import { pki , util , asn1 } from 'node-forge' ; import { inflate , deflate } from 'pako' ; const BASE64_STR = 'base64' ; desc param arr1 { string [ ] } param arr2 { [ ] } export function zipObject ( arr1 , arr2 , skipDuplicated = true ) { return arr1 . reduce ( ( res , l , i ) => { if ( skipDuplicated ) { res [ l ] = arr2 [ i ] ; return res ; } if ( res [ l ] !== undefined ) { res [ l ] = Array . isArray ( res [ l ] ) ? res [ l ] . concat ( arr2 [ i ] ) : [ res [ l ] ] . concat ( arr2 [ i ] ) ; return res ; } res [ l ] = arr2 [ i ] ; return res ; } , { } ) ; } desc reference param input { [ ] } export function flattenDeep ( input ) { return Array . isArray ( input ) ? input . reduce ( ( a , b ) => a . concat ( flattenDeep ( b ) ) , [ ] ) : [ input ] ; } desc reference param input { [ ] } export function last ( input ) { return input . slice ( - 1 ) [ 0 ] ; } desc reference param input { string [ ] } export function uniq ( input ) { const set = new Set ( input ) ; return [ ... set ] ; } desc reference param obj param path param defaultValue export function get ( obj , path , defaultValue ) { return path . split ( '.' ) . reduce ( ( a , c ) => ( a && a [ c ] ? a [ c ] : ( defaultValue || null ) ) , obj ) ; } desc param { any } input export function isString ( input ) { return typeof input === 'string' ; } desc param { string } message return { string } function base64Encode ( message ) { return Buffer . from ( message as string ) . toString ( BASE64_STR ) ; } desc param { string } base64Message param { boolean } isBytes return { bytes export function base64Decode ( base64Message , isBytes ? ) { const bytes = Buffer . from ( base64Message , BASE64_STR ) ; return Boolean ( isBytes ) ? bytes : bytes . toString ( ) ; } desc param { string } message return { string } function deflateString ( message ) { const input = Array . prototype . map . call ( message , char => char . charCodeAt ( 0 ) ) ; return Array . from ( deflate ( input , { raw : true } ) ) ; } desc param { string } compressedString return { string } export function inflateString ( compressedString ) { const inputBuffer = Buffer . from ( compressedString , BASE64_STR ) ; const input = Array . prototype . map . call ( inputBuffer . toString ( 'binary' ) , char => char . charCodeAt ( 0 ) ) ; return Array . from ( inflate ( input , { raw : true } ) ) . map ( ( byte ) => String . fromCharCode ( byte ) ) . join ( '' ) ; } desc param { buffer } File param { string } String return { string } function _normalizeCerString ( bin , format ) { return bin . toString ( ) . replace ( /\n/g , '' ) . replace ( /\r/g , '' ) . replace ( `-----BEGIN ${ format }-----` , '' ) . replace ( `-----END ${ format }-----` , '' ) . replace ( / /g , '' ) ; } desc param { string } certString return { string } function normalizeCerString ( certString ) { return _normalizeCerString ( certString , 'CERTIFICATE' ) ; } desc param { string } pemString return { string } function normalizePemString ( pemString ) { return _normalizeCerString ( pemString . toString ( ) , 'RSA PRIVATE KEY' ) ; } desc param { object } req return { string } function getFullURL ( req ) { return `${ req . protocol }://${ req . get ( 'host' ) }${ req . originalUrl }` ; } desc param { string return { boolean } function parseString ( str , defaultValue = '' ) { return str || defaultValue ; } desc param { object } default param { object } object return { object } function applyDefault ( obj1 , obj2 ) { return Object . assign ( { } , obj1 , obj2 ) ; } desc param { string } x509 return { string } function getPublicKeyPemFromCertificate ( x509Certificate ) { const certDerBytes = util . decode64 ( x509Certificate ) ; const obj = asn1 . fromDer ( certDerBytes ) ; const cert = pki . certificateFromAsn1 ( obj ) ; return pki . publicKeyToPem ( cert . publicKey ) ; } desc param { string | Buffer } keyString param { string } protected return { string } export function readPrivateKey ( keyString , passphrase , isOutputString ? ) { return isString ( passphrase ) ? this . convertToString ( pki . privateKeyToPem ( pki . decryptRsaPrivateKey ( String ( keyString ) , passphrase ) ) , isOutputString ) : keyString ; } desc function convertToString ( input , isOutputString ) { return Boolean ( isOutputString ) ? String ( input ) : input ; } desc export function isNonEmptyArray ( a ) { return Array . isArray ( a ) && a . length > 0 ; } export function notEmpty < TValue > ( value ) { return value !== null && value !== undefined ; } const utility = { isString , base64Encode , base64Decode , deflateString , inflateString , normalizeCerString , normalizePemString , getFullURL , parseString , applyDefault , getPublicKeyPemFromCertificate , readPrivateKey , convertToString , isNonEmptyArray , } ; export default utility ;