From fc9d8549a6f5780ff01e57e315d50c81809ad7a6 Mon Sep 17 00:00:00 2001 From: Rikki Gibson Date: Thu, 30 Aug 2018 12:46:54 -0700 Subject: [PATCH] Remove totalBytes property from TransferProgressEvent (#207) --- lib/axiosHttpClient.ts | 15 ++++++++------- lib/webResource.ts | 8 +------- lib/xhrHttpClient.ts | 3 +-- 3 files changed, 10 insertions(+), 16 deletions(-) diff --git a/lib/axiosHttpClient.ts b/lib/axiosHttpClient.ts index 95b6b128..11c9ac29 100644 --- a/lib/axiosHttpClient.ts +++ b/lib/axiosHttpClient.ts @@ -101,12 +101,11 @@ export class AxiosHttpClient implements HttpClient { const onUploadProgress = httpRequest.onUploadProgress; if (onUploadProgress && axiosBody) { - const totalBytes = parseInt(httpRequest.headers.get("Content-Length")!) || undefined; let loadedBytes = 0; const uploadReportStream = new Transform({ transform: (chunk: string | Buffer, _encoding, callback) => { loadedBytes += chunk.length; - onUploadProgress({ loadedBytes, totalBytes }); + onUploadProgress({ loadedBytes }); callback(undefined, chunk); } }); @@ -152,21 +151,23 @@ export class AxiosHttpClient implements HttpClient { const onDownloadProgress = httpRequest.onDownloadProgress; let responseBody: Readable | string = res.data; if (onDownloadProgress) { - const totalBytes = parseInt(headers.get("Content-Length")!) || (responseBody as string).length || undefined; if (isReadableStream(responseBody)) { let loadedBytes = 0; const downloadReportStream = new Transform({ transform: (chunk: string | Buffer, _encoding, callback) => { loadedBytes += chunk.length; - onDownloadProgress({ loadedBytes, totalBytes }); + onDownloadProgress({ loadedBytes }); callback(undefined, chunk); } }); responseBody.pipe(downloadReportStream); responseBody = downloadReportStream; - } else if (totalBytes) { - // Calling callback for non-stream response for consistency with browser - onDownloadProgress({ loadedBytes: totalBytes, totalBytes }); + } else { + const length = parseInt(headers.get("Content-Length")!) || (responseBody as string).length || undefined; + if (length) { + // Calling callback for non-stream response for consistency with browser + onDownloadProgress({ loadedBytes: length }); + } } } diff --git a/lib/webResource.ts b/lib/webResource.ts index dbddd2e5..0dd435ce 100644 --- a/lib/webResource.ts +++ b/lib/webResource.ts @@ -18,13 +18,7 @@ export type TransferProgressEvent = { /** * The number of bytes loaded so far. */ - loadedBytes: number, - - /** - * The total number of bytes that will be loaded. - * If the total number of bytes is unknown, this property will be undefined. - */ - totalBytes?: number + loadedBytes: number }; /** diff --git a/lib/xhrHttpClient.ts b/lib/xhrHttpClient.ts index ff5e3dcd..f2c0f25e 100644 --- a/lib/xhrHttpClient.ts +++ b/lib/xhrHttpClient.ts @@ -107,8 +107,7 @@ export class XhrHttpClient implements HttpClient { function addProgressListener(xhr: XMLHttpRequestEventTarget, listener?: (progress: TransferProgressEvent) => void) { if (listener) { xhr.addEventListener("progress", rawEvent => listener({ - loadedBytes: rawEvent.loaded, - totalBytes: rawEvent.lengthComputable ? rawEvent.total : undefined + loadedBytes: rawEvent.loaded })); } }