import { StorageServiceClient } from "azure-storage" ; import { randomBytes } from "crypto" ; import { createWriteStream , readFileSync } from "fs" ; import { sign } from "jsonwebtoken" ; import { join } from "path" ; import rimraf from "rimraf" ; import { URL } from "url" ; export const EMULATOR_ACCOUNT_NAME = "devstoreaccount1" ; export const EMULATOR_ACCOUNT_KEY = "Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==" ; export function getUniqueName ( prefix ) { return `${ prefix }${ new Date ( ) . getTime ( ) }${ padStart ( Math . floor ( Math . random ( ) * 10000 ) . toString ( ) , 5 , "00000" ) }` ; } export param { string } currentString param { number } targetLength param { string } padString returns { string } export function padStart ( currentString , targetLength , padString = " " ) { return currentString . padStart ( targetLength , padString ) ; } export async function rmRecursive ( path ) { return new Promise < void > ( ( resolve , reject ) => { rimraf ( path , ( err ) => { if ( err ) { resolve ( ) ; } else { resolve ( ) ; } } ) ; } ) ; } param response param length export async function bodyToString ( response , length ? ) { if ( response . contentLength === 0 ) { return "" ; } return new Promise < string > ( ( resolve , reject ) => { response . readableStreamBody ! . on ( "readable" , ( ) => { let chunk ; chunk = response . readableStreamBody ! . read ( length ) ; if ( chunk ) { resolve ( chunk . toString ( ) ) ; } } ) ; response . readableStreamBody ! . on ( "error" , reject ) ; } ) ; } export async function sleep ( time ) { return new Promise < void > ( ( resolve ) => { setTimeout ( resolve , time ) ; } ) ; } export function base64encode ( content ) { return Buffer . from ( content ) . toString ( "base64" ) ; } export param { string } url param { string } name returns { string } export function appendToURLPath ( url , name ) { const urlParsed = new URL ( url ) ; let path = urlParsed . pathname ; path = path ? path . endsWith ( "/" ) ? `${ path }${ name }` : `${ path }/${ name }` : name ; urlParsed . pathname = path ; return urlParsed . href ; } export async function createRandomLocalFile ( folder , blockNumber , blockSize ) { return new Promise < string > ( ( resolve , reject ) => { const destFile = join ( folder , getUniqueName ( "tempfile." ) ) ; const ws = createWriteStream ( destFile ) ; let offsetInMB = 0 ; function randomValueHex ( len = blockSize ) { return randomBytes ( Math . ceil ( len / 2 ) ) . toString ( "hex" ) . slice ( 0 , len ) ; } ws . on ( "open" , ( ) => { while ( offsetInMB ++ < blockNumber && ws . write ( randomValueHex ( ) ) ) { } if ( offsetInMB >= blockNumber ) { ws . end ( ) ; } } ) ; ws . on ( "drain" , ( ) => { while ( offsetInMB ++ < blockNumber && ws . write ( randomValueHex ( ) ) ) { } if ( offsetInMB >= blockNumber ) { ws . end ( ) ; } } ) ; ws . on ( "finish" , ( ) => resolve ( destFile ) ) ; ws . on ( "error" , reject ) ; } ) ; } export async function readStreamToLocalFile ( rs , file ) { return new Promise < void > ( ( resolve , reject ) => { const ws = createWriteStream ( file ) ; rs . pipe ( ws ) ; rs . on ( "error" , reject ) ; ws . on ( "error" , reject ) ; ws . on ( "finish" , resolve ) ; } ) ; } export function generateJWTToken ( nbf , iat , exp , iss , aud , scp ) { const privateKey = readFileSync ( "./tests/server.key" ) ; const token = sign ( { nbf : Math . floor ( nbf . getTime ( ) / 1000 ) , iat : Math . floor ( iat . getTime ( ) / 1000 ) , exp : Math . floor ( exp . getTime ( ) / 1000 ) , iss , aud , scp } , privateKey , { algorithm : "RS256" } ) ; return token ; } export function restoreBuildRequestOptions ( service ) { if ( ( service as any ) . __proto__ . __proto__ . __original_buildRequestOptions ) { ( service as any ) . __proto__ . __proto__ . _buildRequestOptions = ( service as any ) . __proto__ . __proto__ . __original_buildRequestOptions ; } } export function overrideRequest ( override = { headers : { } } , service ) { const hasOriginal = ! ! ( service as any ) . __proto__ . __proto__ . __original_buildRequestOptions ; const original = hasOriginal ? ( service as any ) . __proto__ . __proto__ . __original_buildRequestOptions : ( service as any ) . __proto__ . __proto__ . _buildRequestOptions ; if ( ! hasOriginal ) { ( service as any ) . __proto__ . __proto__ . __original_buildRequestOptions = original ; } const _buildRequestOptions = original . bind ( service ) ; ( service as any ) . __proto__ . __proto__ . _buildRequestOptions = ( webResource , body , options , callback ) => { _buildRequestOptions ( webResource , body , options , ( err , finalRequestOptions ) => { for ( const key in override . headers ) { if ( Object . prototype . hasOwnProperty . call ( override . headers , key ) ) { const element = override . headers [ key ] ; finalRequestOptions . headers [ key ] = element ; } } callback ( err , finalRequestOptions ) ; } ) ; } ; }