Repository navigation
fix(ext/node): implement v8.promiseHooks API - #36281
Conversation
Implement the Node.js `v8.promiseHooks` API (`onInit`, `onBefore`, `onAfter`, `onSettled`, `createHook`) on top of Deno core's existing `core.setPromiseHooks` V8 bindings. Details: - Async function / async generator validation (they cannot be hooks) - Re-entrancy guard, since V8 promise hooks fire for all promises, including those created inside the hooks themselves - Single-registration dispatcher pattern, because the core API is additive-only (no removal); the permanent dispatchers consult the current hook arrays and stop functions splice hooks back out Enables the `test-promise-hook-on-init.js` and `test-promise-hook-on-before.js` node_compat tests. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
|
Reviewed the promiseHooks implementation β the core mechanism is sound (correct 1. A hook that removes itself mid-dispatch skips a sibling hook. The dispatcher loops re-read for (let i = 0; i < initHooks.length; i++) {
initHooks[i](promise, parent);
}Register init hooks A and B, and have A's body call its own 2.
createHook({ init: goodFn, before: 123 })pushes Everything else checked out: double-calling a returned |
- Dispatchers now iterate a snapshot of each hook list, so a hook that removes itself (or a sibling) mid-dispatch no longer shifts indices and skips a still-registered hook. - `promiseHooks.createHook` validates every provided callback before registering any, so a later validation failure can't leave earlier hooks permanently registered with no stop function. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
|
Thanks for the review β both addressed in cc6add4:
|
Summary
Implements the Node.js
v8.promiseHooksAPI βonInit,onBefore,onAfter,onSettled, andcreateHookβ on top of Deno core's existingcore.setPromiseHooksV8 bindings.This is the clean, self-contained half of #32905, carved out and reimplemented against current
main(the original PR was based on a since-rewrittenv8.tsandprocess.ts). The--unhandled-rejectionshalf will follow separately, rebuilt on top of the currentsynchronizeListeners.Implementation notes
The "<name>" argument must be of type function).core.setPromiseHooksis additive-only (no removal), so we install permanent dispatchers once that consult the current hook arrays. The stop function returned by each registration splices the hook back out.Tests
Enables two node_compat tests, both passing:
parallel/test-promise-hook-on-init.jsparallel/test-promise-hook-on-before.jsThe remaining hook tests (
on-after,on-resolve,create-hook) are left disabled: they assert exact hook-invocation counts that Deno's internal runtime promises perturb, and need further work to reconcile.Test plan
cargo test -p node_compat_tests test-promise-hook-on-initβ passescargo test -p node_compat_tests test-promise-hook-on-beforeβ passes./tools/format.js&&./tools/lint.js --jscleanπ€ Generated with Claude Code