fix(template-function-uuid): use the current time when uuid.v6 has no timestamp - #647
Merged
gschier merged 2 commits intoSep 15, 2026
Conversation
… timestamp
The timestamp argument is optional, but onRender always passed
new Date(String(args.values.timestamp)).getTime() as msecs. With nothing
supplied that is new Date("undefined") -- NaN -- and the uuid package does
not treat NaN as absent: it masks it down to a zero timestamp. Every default
uuid.v6() therefore rendered as 00000000-0000-6000-..., embedding
1582-10-15 instead of now, so consecutive UUIDs no longer sort by generation.
Only pass msecs when the value parses; otherwise let v6() use its own clock.
|
Thanks for the PR. This appears to match Yaak's contribution policy and is awaiting review by @gschier. This only means the PR is in scope for review. It does not mean the change has been reviewed or accepted for merge. |
|
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
uuid.v6()with its optional timestamp left empty renders a zeroed timestamp —00000000-0000-6000-…, i.e. 1582-10-15 — instead of the current time. Fall back to the uuid package's own clock when no parseable timestamp is given.Submission
CONTRIBUTING.md.Detail
The
timestampargument isoptional: true, butonRenderalways does:With nothing supplied,
String(undefined)is"undefined",new Date("undefined").getTime()isNaN, andNaNis notundefined— so the uuid package does not fall back toDate.now(). It runsNaNthrough its bit-packing, where every masked field comes out zero.Running the plugin's own
onRenderand decoding the timestamp each result carries:The template renderer only inserts arguments that literally appear in the tag, so the plain
${[ uuid.v6() ]}reaches the plugin with notimestampat all — the default usage is the broken one. v6 exists to be time-sortable, and these all share one fixed timestamp, so consecutive values no longer sort by generation.After the change the first three embed the current time, and a supplied timestamp is unchanged.
An unparseable non-empty timestamp also falls back to the clock rather than silently producing the 1582 value. If you would prefer that case to surface an error instead, that is a one-line change.
Test plan
plugins/template-function-uuid/tests/uuid.test.ts— the plugin had no tests — following thetemplate-function-regexpattern (vite-plus/test,onRender({} as Context, { values, purpose: "send" })). It decodes the embedded timestamp and asserts it falls between the times read before and after the call, for{},""andnull, and that a given timestamp is embedded exactly.main, and pass with the change.