toWebRequest is not creating abort signal
Environment
- [email protected]
- Node v22.16.0
Reproduction
The problem is clear by looking at the code — the signal property is not created:
https://github.com/h3js/h3/blob/ec77d6bc148e4ff7629ba56577697055cc0fcf2e/src/utils/request.ts#L354-L365
I would have created a demo reproduction with trpc-nuxt but StackBlitz currently has a problem running Nuxt apps, will do once https://github.com/nuxt/nuxt/issues/33120 is fixed.
Describe the bug
toWebRequest is not creating the abort signal (which other libraries depend upon). That makes the code that uses the web requests created from h3 events to stuck forever when e.g. handling subscriptions.
Related issue: https://github.com/wobsoriano/trpc-nuxt/issues/191
Additional context
I extended toWebRequest to pass the AbortSignal to work around the issue:
function toWebRequest(event: H3Event) {
return event.web?.request || new Request(getRequestURL(event), {
// @ts-ignore Undici option
duplex: "half",
method: event.method,
headers: event.headers,
body: getRequestWebStream(event),
signal: (() => {
const abortController = new AbortController()
event.node.req.on("close", () => abortController.abort())
return abortController.signal
})(),
})
}
Logs
Thanks for the report. Feel free to open a PR for adding support.
Only we have to make sure signal is lazy created. Check https://github.com/h3js/srvx/pull/76 as reference
@IlyaSemenov Do you have any plans to work on this? If not, I'd be happy to fix it ❤️
Sure, please do. If it were hanging like this for some time more, I could at some point pick it up, but if you're willing to fix this, please go ahead!