import { createWriteStream , PathLike } from "fs" ; 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 async function streamToLocalFile ( stream , path ) { return new Promise < void > ( ( resolve , reject ) => { const writeStream = createWriteStream ( path ) ; stream . on ( "error" , reject ) . pipe ( writeStream ) . on ( "close" , resolve ) . on ( "error" , reject ) ; } ) ; } 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 ) } export function deserializePageBlobRangeHeader ( rangeHeaderValue ? , xMsRangeHeaderValue ? , force512boundary = true ) { const ranges = deserializeRangeHeader ( rangeHeaderValue , xMsRangeHeaderValue ) ; const startInclusive = ranges [ 0 ] ; const endInclusive = ranges [ 1 ] ; if ( force512boundary && startInclusive % 512 !== 0 ) { throw new RangeError ( `deserializePageBlobRangeHeader: range start value ${ startInclusive } doesn't align with 512 boundary.` ) ; } if ( force512boundary && endInclusive !== Infinity && ( endInclusive + 1 ) % 512 !== 0 ) { throw new RangeError ( `deserializePageBlobRangeHeader: range end value ${ endInclusive } doesn't align with 512 boundary.` ) ; } return [ startInclusive , endInclusive ] ; } param { string } inputEtag returns { string } export function removeQuotationFromListBlobEtag ( inputEtag ) { if ( inputEtag === undefined ) { return inputEtag ; } if ( inputEtag [ 0 ] === '"' && inputEtag [ inputEtag . length - 1 ] === '"' ) { return inputEtag . substring ( 1 , inputEtag . length - 1 ) ; } return inputEtag ; } export function validateContainerName ( requestID , containerName ) { if ( containerName !== "" && ( containerName ! . length < 3 || containerName ! . length > 63 ) ) { throw StorageErrorFactory . getOutOfRangeName ( requestID ) ; } const reg = new RegExp ( "^[a-z0-9](?!.*--)[a-z0-9-]{1,61}[a-z0-9]$" ) ; if ( ! reg . test ( containerName ! ) ) { throw StorageErrorFactory . getInvalidResourceName ( requestID ) ; } }