fix: snapshot hook arrays at route registration to preserve order#1832
fix: snapshot hook arrays at route registration to preserve order#1832eL1fe wants to merge 1 commit intoelysiajs:mainfrom
Conversation
Routes registered between onBeforeHandle calls picked up hooks that
were added after them. Root cause: mergeHook's early return path used
shallow spread ({ ...a }) which copies array references, not arrays.
Later pushes to the event arrays mutated already-registered routes.
Now the early return path copies arrays to prevent shared references.
Fixes elysiajs#1826
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
WalkthroughThe Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Summary
Routes picked up hooks registered after them due to shared array references.
Problem
Request to
/btriggered both hooks1and2, but should only trigger1.Root cause
mergeHookhas an early return when the local hook object is empty:return { ...a }. The spread operator copies array properties by reference, not value. Soroute.hooks.beforeHandleandthis.event.beforeHandlepointed to the same array. LateronBeforeHandlecalls pushed into that shared array, retroactively adding hooks to already-registered routes.Changes
src/utils.ts: the early return inmergeHooknow copies array values to prevent shared referencesTest plan
/ahas 0 hooks,/bhas 1,/chas 2Fixes #1826
Summary by CodeRabbit