import uuidr = require ( "uuid" ) ; export function getArgs ( func ) { return ( func + "" ) . replace ( /[/][/].*$/mg , "" ) . replace ( /\s+/g , "" ) . replace ( /[/][*][^/*]*[*][/]/g , "" ) . split ( "){" , 1 ) [ 0 ] . replace ( /^[^(]*[(]/ , "" ) . replace ( /=[^,]+/g , "" ) . split ( "," ) . filter ( Boolean ) ; } export function uuid ( ) { return uuidr . v4 ( ) ; } export function password ( ) { let u = uuid ( ) + uuid ( ) ; u = u . split ( "-" ) . join ( "" ) ; let b = new Buffer ( u , "hex" ) . toString ( "base64" ) ; b = b . split ( "+" ) . join ( "" ) . split ( "/" ) . join ( "" ) ; let p = b . substr ( 0 , 4 ) + "-" + b . substr ( 4 , 4 ) + "-" + b . substr ( 8 , 4 ) + "-" + b . substr ( 12 , 4 ) ; return p ; } const notBase64 = /[^A-Z0-9+\/=\n\r]/i ; export function isBase64 ( str ) { let len = str . length ; let firstPaddingIndex = str . indexOf ( "=" ) ; let firstPaddingChar = firstPaddingIndex ; let firstCorrect = 0 ; for ( let i = 0 ; i < str . length ; i ++ ) { let c = str [ i ] ; if ( c === "\r" || c === "\n" ) { len -- ; if ( i < firstPaddingChar ) firstCorrect ++ ; } } if ( firstPaddingChar > firstCorrect ) firstPaddingChar -= firstCorrect ; if ( ! len || len % 4 !== 0 || notBase64 . test ( str ) ) { return false ; } return firstPaddingChar === - 1 || firstPaddingChar === len - 1 || ( firstPaddingChar === len - 2 && str [ firstPaddingIndex + 1 ] === "=" ) ; } const gzipPrefix = Buffer . from ( "1F8B" , "hex" ) ; export function isGzip ( buf ) { if ( typeof buf === "string" ) { return buf . startsWith ( gzipPrefix . toString ( ) ) ; } if ( typeof buf === "object" && buf instanceof Buffer ) { return ( buf [ 0 ] === gzipPrefix [ 0 ] && buf [ 1 ] === gzipPrefix [ 1 ] ) ; } return false ; } export function wildcardMatch ( rule , value ) { return new RegExp ( "^" + rule . split ( "*" ) . join ( ".*" ) + "$" ) . test ( value ) ; } export function parseMap ( map , prefix ? ) { prefix = prefix || "" ; let res = { } ; let parts = map . split ( ";" ) . map ( x => x . trim ( ) ) . filter ( x => x ) ; for ( const part of parts ) { let key ; let value ; [ key , value ] = part . split ( "=" ) . map ( x => x . trim ( ) ) . filter ( x => x ) ; res [ prefix + key ] = value ; } return res ; }