1
0
Fork 0

Linting fixes

pull/1140/head
Sankalp Kotewar 2022-08-03 14:12:16 +00:00
parent 7cb82599d4
commit c89375df9f
2 changed files with 30 additions and 35 deletions

View File

@ -12,7 +12,7 @@ import {SocketTimeout} from './constants'
import {DownloadOptions} from '../options' import {DownloadOptions} from '../options'
import {retryHttpClientResponse} from './requestUtils' import {retryHttpClientResponse} from './requestUtils'
import { AbortController } from "@azure/abort-controller"; import {AbortController} from '@azure/abort-controller'
/** /**
* Pipes the body of a HTTP response to a stream * Pipes the body of a HTTP response to a stream
@ -249,8 +249,7 @@ export async function downloadCacheStorageSDK(
try { try {
downloadProgress.startDisplayTimer() downloadProgress.startDisplayTimer()
const controller = new AbortController(); const controller = new AbortController()
const abortSignal = controller.signal;
while (!downloadProgress.isDone()) { while (!downloadProgress.isDone()) {
const segmentStart = const segmentStart =
downloadProgress.segmentOffset + downloadProgress.segmentSize downloadProgress.segmentOffset + downloadProgress.segmentSize
@ -261,24 +260,22 @@ export async function downloadCacheStorageSDK(
) )
downloadProgress.nextSegment(segmentSize) downloadProgress.nextSegment(segmentSize)
const abortTimeInMs = options.abortTimeInMs == undefined ? 2700000 : options.abortTimeInMs; const abortTimeInMs =
const result = await promiseWithTimeout(abortTimeInMs, client.downloadToBuffer( options.abortTimeInMs === undefined ? 2700000 : options.abortTimeInMs
segmentStart, const result = await promiseWithTimeout(
segmentSize, abortTimeInMs,
{ client.downloadToBuffer(segmentStart, segmentSize, {
abortSignal: abortSignal, abortSignal: controller.signal,
concurrency: options.downloadConcurrency, concurrency: options.downloadConcurrency,
onProgress: downloadProgress.onProgress() onProgress: downloadProgress.onProgress()
} })
)); )
if(result === 'timeout') { if (result === 'timeout') {
controller.abort(); controller.abort()
throw new Error("Download aborted, segment download timed out."); throw new Error('Download aborted, segment download timed out.')
} else { } else if (Buffer.isBuffer(result)) {
core.debug("Download completely successfully, cancelling timer.") fs.writeFileSync(fd, result)
} }
fs.writeFileSync(fd, result)
} }
} finally { } finally {
downloadProgress.stopDisplayTimer() downloadProgress.stopDisplayTimer()
@ -287,17 +284,17 @@ export async function downloadCacheStorageSDK(
} }
} }
const promiseWithTimeout = (timeoutMs: number, promise: Promise<any>) => { const promiseWithTimeout = async (
let timeoutHandle: NodeJS.Timeout; timeoutMs: number,
const timeoutPromise = new Promise((resolve, reject) => { promise: Promise<Buffer>
timeoutHandle = setTimeout(() => resolve('timeout'), timeoutMs); ): Promise<unknown> => {
}); let timeoutHandle: NodeJS.Timeout
const timeoutPromise = new Promise(resolve => {
timeoutHandle = setTimeout(() => resolve('timeout'), timeoutMs)
})
return Promise.race([ return Promise.race([promise, timeoutPromise]).then(result => {
promise, clearTimeout(timeoutHandle)
timeoutPromise, return result
]).then((result) => { })
clearTimeout(timeoutHandle); }
return result;
});
}

View File

@ -45,16 +45,14 @@ export interface DownloadOptions {
* *
* @default 30000 * @default 30000
*/ */
timeoutInMs?: number timeoutInMs?: number
/** /**
* Time after which download should be aborted if stuck * Time after which download should be aborted if stuck
* *
* @default 2700000 * @default 2700000
*/ */
abortTimeInMs?: number abortTimeInMs?: number
} }
/** /**