-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(cli): trace output (-vv) is useless when files are uploaded (#33104)
### Reason for this change When running the CDK CLI in trace mode (with `-vv`), e.g. `cdk deploy -vv` we log all SDK calls and their inputs. For file uploads, the input contains the buffer of the file to be uploaded. This results in really bad output: ``` [20:14:31] [sdk info] S3.PutObject({"Bucket":"cdk-hnb659fds-assets-us-east-1","Key":"220c435f80d2dd2cfc5dd0e8e29bfbb6ec58892bd813f5d6a008fdda38f6c02f.zip","Body":{"type":"Buffer","data":[80,75,3,4,20,0,8,0,8,0,0,0,33,0,0,0,0,0,0,0,0,0,0,0,0,0,8,0,0,0,100,97,116,97,46,116,120,116,75,73,44,73,52,227,2,0,80,75,7,8,165,92,116,66,8,0,0,0,6,0,0,0,80,75,1,2,45,3,20,0,8,0,8,0,0,0,33,0,165,92,116,66,8,0,0,0,6,0,0,0,8,0,0,0,0,0,0,0,0,0,32,0,164,129,0,0,0,0,100,97,116,97,46,116,120,116,80,75,5,6,0,0,0,0,1,0,1,0,54,0,0,0,62,0,0,0,0,0]},"ContentType":"application/zip","ChecksumAlgorithm":"SHA256","ServerSideEncryption":"aws:kms"}) -> OK ``` Now in this case we are only uploading 138 bytes. Imagine what this log looks like for files of 10MiB or 100MiB. Reportedly this also crashed every terminal. ### Description of changes There's no need to log the buffer bytes. Instead replace it with a string indicating its a buffer and the buffer length: ``` [20:14:31] [sdk info] S3.PutObject({"Body":"<Buffer 138 Bytes>"}) -> OK ``` ### Describe any new or updated permissions being added n/a ### Description of how you validated changes Tests ### Checklist - [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
- Loading branch information
Showing
5 changed files
with
81 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/** | ||
* Format bytes as a human readable string | ||
* | ||
* @param bytes Number of bytes to format | ||
* @param decimals Number of decimal places to show (default 2) | ||
* @returns Formatted string with appropriate unit suffix | ||
*/ | ||
export function formatBytes(bytes: number, decimals: number = 2): string { | ||
decimals = decimals < 0 ? 0 : decimals; | ||
|
||
if (bytes === 0) { | ||
return '0 Bytes'; | ||
} | ||
|
||
const k = 1024; | ||
const sizes = ['Bytes', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB']; | ||
|
||
const i = Math.floor(Math.log(bytes) / Math.log(k)); | ||
|
||
return `${parseFloat((bytes / Math.pow(k, i)).toFixed(decimals))} ${sizes[i]}`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { formatBytes } from '../../lib/util/bytes'; | ||
|
||
test.each([ | ||
[0, '0 Bytes'], | ||
[10, '10 Bytes'], | ||
[1024, '1 KiB'], | ||
[10.5 * 1024 * 1024, '10.5 MiB'], | ||
])('converts %s bytes to %s', (bytes: number, expected: string) => { | ||
expect(formatBytes(bytes)).toEqual(expected); | ||
}); | ||
|
||
test('can format many decimals', () => { | ||
expect(formatBytes(10.51234 * 1024 * 1024, 5)).toEqual('10.51234 MiB'); | ||
}); | ||
|
||
test('can deal with negative decimals', () => { | ||
expect(formatBytes(10.5 * 1024 * 1024, -1)).toEqual('11 MiB'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { replacerBufferWithInfo } from '../../lib/serialize'; | ||
|
||
test('converts buffer to information', () => { | ||
const res = JSON.stringify({ data: Buffer.from('test data') }, replacerBufferWithInfo); | ||
|
||
expect(res).toEqual('{"data":"<Buffer: 9 Bytes>"}'); | ||
}); |