Skip to content

Commit

Permalink
Format raw bytes to MB, KB, or B
Browse files Browse the repository at this point in the history
  • Loading branch information
mvirgil committed Jan 14, 2025
1 parent 9300109 commit c4b19d1
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/sourcemaps/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { Logger } from '../utils/logger';
import { Spinner } from '../utils/spinner';
import { uploadFile } from '../utils/httpUtils';
import { AxiosError } from 'axios';
import { formatUploadProgress } from '../utils/stringUtils';

export type SourceMapInjectOptions = {
directory: string;
Expand Down Expand Up @@ -172,7 +173,8 @@ export async function runSourcemapUpload(options: SourceMapUploadOptions, ctx: S
url,
file,
onProgress: ({ loaded, total }) => {
spinner.updateText(`Uploading ${loaded} of ${total} bytes for ${path} (${filesRemaining} files remaining)`);
const { loadedFormatted, totalFormatted } = formatUploadProgress(loaded, total);
spinner.updateText(`Uploading ${loadedFormatted} of ${totalFormatted} for ${path} (${filesRemaining} files remaining)`);
},
parameters
});
Expand All @@ -182,22 +184,23 @@ export async function runSourcemapUpload(options: SourceMapUploadOptions, ctx: S
spinner.stop();

const ae = e as AxiosError;
const unableToUploadMessage = `Unable to upload ${path}`;

if (ae.response && ae.response.status === 413) {
logger.warn(ae.response.status, ae.response.statusText);
logger.warn('Unable to upload %s', path);
logger.warn(unableToUploadMessage);
} else if (ae.response) {
logger.error(ae.response.status, ae.response.statusText);
logger.error(ae.response.data);
logger.error('Upload failed for %s', path);
logger.error(unableToUploadMessage);
} else if (ae.request) {
logger.error(`Response from ${url} was not received`);
logger.error(ae.cause);
logger.error('Upload failed for %s', path);
logger.error(unableToUploadMessage);
} else {
logger.error(`Request to ${url} could not be sent`);
logger.error(e);
logger.error('Upload failed for %s', path);
logger.error(unableToUploadMessage);
}
}
}
Expand Down
35 changes: 35 additions & 0 deletions src/utils/stringUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright Splunk Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/** Receives "loaded" and "total" bytes, and returns the respective formatted numbers with appropriate units. */
export function formatUploadProgress(loaded: number, total: number): { loadedFormatted: string, totalFormatted: string } {
if (total >= 1_000_000) {
return {
totalFormatted: (total / 1_000_000).toFixed(1) + 'MB',
loadedFormatted: (loaded / 1_000_000).toFixed(1) + 'MB'
};
} else if (total >= 1_000) {
return {
totalFormatted: (total / 1_000).toFixed(1) + 'KB',
loadedFormatted: (loaded / 1_000).toFixed(1) + 'KB'
};
} else {
return {
totalFormatted: total + 'B',
loadedFormatted: loaded + 'B'
};
}
}
28 changes: 28 additions & 0 deletions test/utils/stringUtils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright Splunk Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { formatUploadProgress } from '../../src/utils/stringUtils';

describe('formatProgress', () => {
it('should format both numbers using the same unit (either B, KB, or MB)', () => {
expect(formatUploadProgress(100, 2_500_000)).toEqual({ loadedFormatted: '0.0MB', totalFormatted: '2.5MB' });
expect(formatUploadProgress(100_000, 2_500_000)).toEqual({ loadedFormatted: '0.1MB', totalFormatted: '2.5MB' });
expect(formatUploadProgress(1_100_000, 2_500_000)).toEqual({ loadedFormatted: '1.1MB', totalFormatted: '2.5MB' });
expect(formatUploadProgress(200, 1_000)).toEqual({ loadedFormatted: '0.2KB', totalFormatted: '1.0KB' });
expect(formatUploadProgress(1_000, 1_000)).toEqual({ loadedFormatted: '1.0KB', totalFormatted: '1.0KB' });
expect(formatUploadProgress(500, 999)).toEqual({ loadedFormatted: '500B', totalFormatted: '999B' });
});
});

0 comments on commit c4b19d1

Please sign in to comment.