h3 icon indicating copy to clipboard operation
h3 copied to clipboard

toWebRequest is not creating abort signal

Open IlyaSemenov opened this issue 3 months ago • 3 comments

Environment

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


IlyaSemenov avatar Sep 14 '25 05:09 IlyaSemenov

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

pi0 avatar Sep 15 '25 10:09 pi0

@IlyaSemenov Do you have any plans to work on this? If not, I'd be happy to fix it ❤️

kricsleo avatar Oct 12 '25 12:10 kricsleo

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!

IlyaSemenov avatar Oct 12 '25 14:10 IlyaSemenov