Skip to content

feat: add computeTTL and sanitizeValue utilities#252

Closed
chsrimanaswi wants to merge 2 commits into
adobe:mainfrom
chsrimanaswi:test/pr-reviewer-validation
Closed

feat: add computeTTL and sanitizeValue utilities#252
chsrimanaswi wants to merge 2 commits into
adobe:mainfrom
chsrimanaswi:test/pr-reviewer-validation

Conversation

@chsrimanaswi
Copy link
Copy Markdown
Contributor

Description

Related Issue

Motivation and Context

How Has This Been Tested?

Screenshots (if appropriate):

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • I have signed the Adobe Open Source CLA.
  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

Copy link
Copy Markdown

@github-actions github-actions Bot left a comment

Choose a reason for hiding this comment

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

🤖 PR Reviewer

Two utility functions added with reasonable documentation and logic. Minor issues exist: sanitizeValue may produce invalid UTF-8 output when truncating multibyte characters, and computeTTL silently returns negative values for past dates without any warning or validation.

📝 2 suggestion(s) - Please review inline comments below.


💡 How to re-trigger

Comment /review or /pr-reviewer on this PR

Comment thread lib/utils.js
const encoded = Buffer.from(value)
if (encoded.length <= maxBytes) return value
return encoded.slice(0, maxBytes).toString()
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Buffer.from(value) defaults to UTF-8 encoding, but slicing at an arbitrary byte boundary can split a multibyte character, producing an invalid/garbled string. Use a TextDecoder or ensure truncation happens on a character boundary.

Suggested change
}
function sanitizeValue (value, maxBytes) {
const encoded = Buffer.from(value, 'utf8')
if (encoded.length <= maxBytes) return value
// Truncate safely on a character boundary by decoding with replacement
return new TextDecoder('utf-8', { fatal: false }).decode(encoded.slice(0, maxBytes)).replace(/\uFFFD$/, '')
}

Comment thread lib/utils.js
*/
function computeTTL (expiry) {
if (expiry === undefined || expiry === null) return undefined
if (expiry instanceof Date) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

computeTTL can return a negative TTL if the provided Date is in the past. Callers will likely get unexpected behavior (e.g., negative cache TTLs). Consider returning 0 or throwing an error for expired dates.

Suggested change
if (expiry instanceof Date) {
function computeTTL (expiry) {
if (expiry === undefined || expiry === null) return undefined
if (expiry instanceof Date) {
const ttl = Math.floor((expiry.getTime() - Date.now()) / 1000)
if (ttl < 0) throw new Error('expiry date is in the past')
return ttl
}
if (typeof expiry !== 'number' || expiry < 0) throw new Error('expiry must be a non-negative number of seconds')
return expiry
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant