import { randomBytes } from "crypto" ; import * as xml2js from "xml2js" ; import StorageErrorFactory from "../errors/StorageErrorFactory" ; export function checkApiVersion ( inputApiVersion , validApiVersions , requestId ) { if ( ! validApiVersions . includes ( inputApiVersion ) ) { throw StorageErrorFactory . getInvalidHeaderValue ( requestId , { HeaderName : "x-ms-version" , HeaderValue : inputApiVersion } ) ; } } export param { string } rangeHeaderValue param { string } xMsRangeHeaderValue returns { [ number , number ] } export function deserializeRangeHeader ( rangeHeaderValue ? , xMsRangeHeaderValue ? ) { const range = xMsRangeHeaderValue || rangeHeaderValue ; if ( ! range ) { return [ 0 , Infinity ] ; } let parts = range . split ( "=" ) ; if ( parts === undefined || parts . length !== 2 ) { throw new RangeError ( `deserializeRangeHeader: raw range value ${ range } is wrong.` ) ; } parts = parts [ 1 ] . split ( "-" ) ; if ( parts === undefined || parts . length < 1 || parts . length > 2 ) { throw new RangeError ( `deserializeRangeHeader: raw range value ${ range } is wrong.` ) ; } const startInclusive = parseInt ( parts [ 0 ] , 10 ) ; let endInclusive = Infinity ; if ( parts . length > 1 && parts [ 1 ] !== "" ) { endInclusive = parseInt ( parts [ 1 ] , 10 ) ; } if ( startInclusive > endInclusive ) { throw new RangeError ( `deserializeRangeHeader: raw range value ${ range } is wrong.` ) ; } return [ startInclusive , endInclusive ] ; } private param { string } rangeHeaderValue param { string } xMsRangeHeaderValue returns { ( [ number , number ] | undefined ) } see https export param { string } name returns { nameValidateCode } export function isValidName ( name ) { if ( name === "" ) { return nameValidateCode . invalidName ; } if ( name . length < 3 || name . length > 63 ) { return nameValidateCode . outOfRange ; } if ( name . split ( "-" ) . indexOf ( "" ) !== - 1 ) { return nameValidateCode . invalidName ; } const reg = new RegExp ( "^[0-9|a-z|-]*$" ) ; if ( reg . test ( name ) ) { return nameValidateCode . valid ; } return nameValidateCode . invalidName ; } export enum nameValidateCode { valid , outOfRange , invalidUri , invalidName } public param { number } len returns { string } memberof export function randomValueHex ( len ) { return randomBytes ( Math . ceil ( len / 2 ) ) . toString ( "hex" ) . slice ( 0 , len ) ; } public param { Date } requestDate returns { string } memberof export function getPopReceipt ( requestDate ) { const encodedStr = requestDate . toUTCString ( ) . split ( " " ) . slice ( 1 , 5 ) . join ( "" ) + randomValueHex ( 4 ) ; return Buffer . from ( encodedStr ) . toString ( "base64" ) ; } export param { NodeJS . ReadableStream } data returns { Promise < string > } export async function readStreamToString ( data ) { return new Promise < string > ( ( resolve , reject ) => { let res = "" ; data . on ( "readable" , ( ) => { let chunk ; chunk = data . read ( ) ; if ( chunk ) { res += chunk . toString ( ) ; } } ) . on ( "end" , ( ) => { resolve ( res ) ; } ) . on ( "error" , reject ) ; } ) ; } public param { Date } requestDate returns { string } memberof export function getUTF8ByteSize ( text ) { return Buffer . from ( text , "utf8" ) . length ; } export param { string } param param { boolean } explicitChildrenWithOrder returns { Promise < any > } export function parseXMLwithEmpty ( param , explicitChildrenWithOrder = false ) { const xmlParser = new xml2js . Parser ( { explicitArray : false , explicitCharkey : false , explicitRoot : false , preserveChildrenOrder : explicitChildrenWithOrder , explicitChildren : explicitChildrenWithOrder } ) ; return new Promise ( ( resolve , reject ) => { xmlParser . parseString ( param , ( err ? , res ? ) => { if ( err ) { reject ( err ) ; } else { resolve ( res ) ; } } ) ; } ) ; }