-
Notifications
You must be signed in to change notification settings - Fork 9
feat: add computeTTL and sanitizeValue utilities #253
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
7bfec7f
e52baca
5a87479
6f9e249
58424a3
bb6f8c2
8bfd359
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -117,8 +117,40 @@ function formatAjvErrors (errors) { | |||||||||||||||||||||||||
| return stringErrors | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||
| * Computes the TTL in seconds from an expiry date or a number of seconds. | ||||||||||||||||||||||||||
| * Returns undefined if no expiry is provided. | ||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||
| * @private | ||||||||||||||||||||||||||
| * @param {Date|number} expiry a Date object or seconds from now | ||||||||||||||||||||||||||
| * @returns {number|undefined} TTL in seconds | ||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||
| function computeTTL (expiry) { | ||||||||||||||||||||||||||
| if (expiry === undefined || expiry === null) return undefined | ||||||||||||||||||||||||||
| if (expiry instanceof Date) { | ||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [Re-raised] When a Date in the past is provided, this returns a negative TTL, which will likely cause silent bugs or unexpected behavior downstream. Consider returning 0 (or throwing) for expired dates.
Suggested change
|
||||||||||||||||||||||||||
| return Math.floor((expiry.getTime() - Date.now()) / 1000) | ||||||||||||||||||||||||||
|
Comment on lines
+129
to
+131
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [Re-raised] When a Date in the past is provided, this returns a negative TTL, which will likely cause silent bugs or unexpected behavior downstream. Consider returning 0 (or throwing) for expired dates.
Suggested change
|
||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| return expiry | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||
| * Truncates a value to the max allowed byte size for state storage. | ||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||
| * @private | ||||||||||||||||||||||||||
| * @param {string} value the value to sanitize | ||||||||||||||||||||||||||
| * @param {number} maxBytes max allowed size in bytes | ||||||||||||||||||||||||||
| * @returns {string} the value, truncated if necessary | ||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||
| function sanitizeValue (value, maxBytes) { | ||||||||||||||||||||||||||
| const encoded = Buffer.from(value) | ||||||||||||||||||||||||||
| if (encoded.length <= maxBytes) return value | ||||||||||||||||||||||||||
|
Comment on lines
+143
to
+146
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [Re-raised] Slicing a UTF-8 Buffer at an arbitrary byte boundary can split a multi-byte character, producing an invalid UTF-8 sequence and corrupting the last character. Also, the missing type guard means non-string values will cause Buffer.from to throw or behave unexpectedly.
Suggested change
|
||||||||||||||||||||||||||
| return encoded.slice(0, maxBytes).toString() | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
|
Comment on lines
+145
to
+149
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [Re-raised] Slicing a UTF-8 Buffer at an arbitrary byte boundary can split a multi-byte character, producing an invalid UTF-8 sequence and corrupting the last character. Also, the missing type guard means non-string values will cause Buffer.from to throw or behave unexpectedly.
Suggested change
|
||||||||||||||||||||||||||
| module.exports = { | ||||||||||||||||||||||||||
| withHiddenFields, | ||||||||||||||||||||||||||
| isInternalToAdobeRuntime, | ||||||||||||||||||||||||||
| formatAjvErrors | ||||||||||||||||||||||||||
| formatAjvErrors, | ||||||||||||||||||||||||||
| computeTTL, | ||||||||||||||||||||||||||
| sanitizeValue | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pinning the reusable workflow to a feature branch (fix/pr-review-pedantic-and-human-replies) rather than a stable ref like main or a tagged version means any force-push or deletion of that branch could break CI. Ensure this is reverted to main or a versioned tag once the fix is merged.