Skip to content

Commit

Permalink
Remove totalBytes property from TransferProgressEvent (#207)
Browse files Browse the repository at this point in the history
  • Loading branch information
RikkiGibson authored Aug 30, 2018
1 parent 16f681c commit fc9d854
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 16 deletions.
15 changes: 8 additions & 7 deletions lib/axiosHttpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});
Expand Down Expand Up @@ -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 });
}
}
}

Expand Down
8 changes: 1 addition & 7 deletions lib/webResource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
};

/**
Expand Down
3 changes: 1 addition & 2 deletions lib/xhrHttpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}));
}
}
Expand Down

0 comments on commit fc9d854

Please sign in to comment.