Skip to content

fix: await thenables to support cross-realm promises#139

Merged
pi0 merged 3 commits intomainfrom
fix/cross-realm-thenable
Apr 14, 2026
Merged

fix: await thenables to support cross-realm promises#139
pi0 merged 3 commits intomainfrom
fix/cross-realm-thenable

Conversation

@pi0
Copy link
Copy Markdown
Member

@pi0 pi0 commented Apr 14, 2026

Replace result instanceof Promise with a thenable check (typeof result.then === "function") in callHooks, so hooks returning a Promise from a different realm (e.g. jiti/vm contexts) are awaited correctly instead of silently dropped.

Reported by @brandonroberts in nitrojs/nitro#4203 ❤️

Summary by CodeRabbit

  • Bug Fixes
    • Improved handling of asynchronous results from external or sandboxed execution contexts so async operations are reliably detected and awaited, preserving correct execution order.
  • Tests
    • Added a regression test covering cross-context async behavior and adjusted a bundle-size benchmark to reflect updated measurements.

Replace `instanceof Promise` check with a thenable check so async hooks
whose returned promise originates from a different realm (e.g. jiti/vm
contexts) are awaited instead of silently dropped.

Reported by @brandonroberts in nitrojs/nitro#4203.
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Apr 14, 2026

Caution

Review failed

The head commit changed during the review from 0032ac3 to 031c2e1.

📝 Walkthrough

Walkthrough

callHooks in src/utils.ts now detects thenables via result && typeof result.then === "function" and wraps them with Promise.resolve(...) before chaining. Tests were added to validate cross-realm thenable behavior and a bundle size assertion was tweaked.

Changes

Cohort / File(s) Summary
Async Detection Enhancement
src/utils.ts
Replaced result instanceof Promise check with thenable detection (typeof result.then === "function") and normalize returned thenables with Promise.resolve(...) before continuing hook chain.
Cross-Realm Hook Testing
test/hookable.test.ts
Added regression test using node:vm to create hooks in a separate realm and assert that foreign thenables are detected and awaited, preserving hook execution order.
Bundle Size Assertion
test/bundle.test.ts
Loosened bundle size upper bound from < 630 to < 642 bytes for new HookableCore() benchmark test; gzip size assertion unchanged.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 A thenable whisper from a distant VM shore,
It promises motion yet is seen no more,
Not by instanceof, but by .then it’s known—
The rabbit hops, awaits, and watches hooks be sewn. ✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: replacing instanceof Promise with thenable detection to support cross-realm promises.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/cross-realm-thenable

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@pi0 pi0 force-pushed the fix/cross-realm-thenable branch from 0032ac3 to 031c2e1 Compare April 14, 2026 21:36
@pi0 pi0 merged commit 66a8adf into main Apr 14, 2026
3 checks passed
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