Skip to content

feat: bitrate utility function#37244

Merged
mergify[bot] merged 8 commits intoaws:mainfrom
jamiepmullan:feat/common-bitrate-helper
Mar 17, 2026
Merged

feat: bitrate utility function#37244
mergify[bot] merged 8 commits intoaws:mainfrom
jamiepmullan:feat/common-bitrate-helper

Conversation

@jamiepmullan
Copy link
Copy Markdown
Contributor

Issue # (if applicable)

Closes #.

Reason for this change

Description of changes

Introduction of a Bitrate utility function (similar to Duration) which can be used in AWS Elemental Media Services.
Example of usage:
video_bitrate in https://docs.aws.amazon.com/mediapackage/latest/ug/manifest-filtering.html
maxBitrate in https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_mediaconnect.CfnFlow.SourceProperty.html

Describe any new or updated permissions being added

Description of how you validated changes

Added tests

Checklist


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

@jamiepmullan jamiepmullan requested a review from a team as a code owner March 13, 2026 13:11
@github-actions github-actions Bot added beginning-contributor [Pilot] contributed between 0-2 PRs to the CDK p2 labels Mar 13, 2026
@aws-cdk-automation aws-cdk-automation requested a review from a team March 13, 2026 13:11
Copy link
Copy Markdown
Collaborator

@aws-cdk-automation aws-cdk-automation left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(This review is outdated)

@jamiepmullan jamiepmullan changed the title bitrate utility function feat: bitrate utility function Mar 13, 2026
@jamiepmullan
Copy link
Copy Markdown
Contributor Author

Exemption Request - I don't believe there are integration tests for core lib (following existing style for Duration in the lib)

@aws-cdk-automation aws-cdk-automation added the pr-linter/exemption-requested The contributor has requested an exemption to the PR Linter feedback. label Mar 13, 2026
}

private constructor(private readonly bps: number) {
if (bps < 0) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This validation will throw on CDK tokens (CloudFormation parameters, Fn.ref, etc.) because token-encoded numbers use special representations like -1.8881545897087626e+289 that incorrectly trigger the negative check.

Both Duration and Size guard their constructor validation with Token.isUnresolved(). This needs the same treatment:

private constructor(private readonly bps: number) {
  if (!Token.isUnresolved(bps) && bps < 0) {
    throw new UnscopedValidationError(`Bitrate amounts cannot be negative. Received: ${bps}`);
  }
}

Also note the suggested code already includes the value the customer send, so its clearer, please preserve that.

* This class provides a type-safe way to specify bitrates with clear units,
* preventing confusion between bits per second, kilobits per second, etc.
*/
export class Bitrate {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add an isUnresolved() method (both Duration and Size have one) so consumers can check if the value they are working with is a token and if it is unresolved

*
* This class provides a type-safe way to specify bitrates with clear units,
* preventing confusion between bits per second, kilobits per second, etc.
*/
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The class docstring doesn't mention token behavior. Both Duration and Size document this explicitly since it affects how the class can be used

@alvazjor alvazjor self-assigned this Mar 13, 2026
@alvazjor alvazjor added the pr-linter/exempt-integ-test The PR linter will not require integ test changes label Mar 13, 2026
@aws-cdk-automation aws-cdk-automation dismissed their stale review March 13, 2026 14:14

✅ Updated pull request passes all PRLinter validations. Dismissing previous PRLinter review.


An instance of `Bitrate` is initialized through one of its static factory methods:

```ts
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rosetta is complaining about this examples missing the correct referece. Please also review that

@mergify mergify Bot dismissed alvazjor’s stale review March 13, 2026 15:15

Pull request has been modified.

Copy link
Copy Markdown
Contributor

@alvazjor alvazjor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general, all the new additions (token validation, conversion checks, etc) need to have unit tests. Since this is not a construct per se, we cannot do integ test yet, but then we should make sure unit test provide the highest coverage possible. That needs to be changed too, since we are missing test scenarios


private constructor(private readonly bps: number) {
if (!Token.isUnresolved(bps) && bps < 0) {
throw new UnscopedValidationError('Bitrate cannot be negative');
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add here in the error message the the value they are sending, so the customers understand what is wrong

return this.bps;
}

/**
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The conversion methods (this and all the ones below) perform arithmetic on the stored value without checking for tokens. When the underlying value is a token, this produces corrupted results that silently generate invalid CloudFormation templates.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment was not addressed. Validations are required before trying do the conversion

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks! I've fixed this one properly now

@mergify
Copy link
Copy Markdown
Contributor

mergify Bot commented Mar 17, 2026

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@mergify
Copy link
Copy Markdown
Contributor

mergify Bot commented Mar 17, 2026

Merge Queue Status

  • Entered queue2026-03-17 14:13 UTC · Rule: default-squash
  • Checks passed · in-place
  • Merged2026-03-17 14:55 UTC · at a26d08158e0dd92a8f4ba03d27bd654609edea38

This pull request spent 41 minutes 59 seconds in the queue, including 30 minutes 40 seconds running CI.

Required conditions to merge
  • #approved-reviews-by >= 1 [🛡 GitHub branch protection]
  • #changes-requested-reviews-by = 0 [🛡 GitHub branch protection]
  • any of [🛡 GitHub branch protection]:
    • check-success = validate-pr
    • check-neutral = validate-pr
    • check-skipped = validate-pr
  • any of [🛡 GitHub branch protection]:
    • check-success = build
    • check-neutral = build
    • check-skipped = build

@aws-cdk-automation aws-cdk-automation removed the pr/needs-community-review This PR needs a review from a Trusted Community Member or Core Team Member. label Mar 17, 2026
@mergify
Copy link
Copy Markdown
Contributor

mergify Bot commented Mar 17, 2026

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@mergify
Copy link
Copy Markdown
Contributor

mergify Bot commented Mar 17, 2026

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@mergify mergify Bot merged commit 2a21279 into aws:main Mar 17, 2026
18 of 19 checks passed
@github-actions
Copy link
Copy Markdown
Contributor

Comments on closed issues and PRs are hard for our team to see.
If you need help, please open a new issue that references this one.

@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Mar 17, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

beginning-contributor [Pilot] contributed between 0-2 PRs to the CDK p2 pr-linter/exempt-integ-test The PR linter will not require integ test changes pr-linter/exemption-requested The contributor has requested an exemption to the PR Linter feedback.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants