koajs / koa-body

koa body parser middleware
MIT License
950 stars 130 forks source link

ctx.req.on("close", fn) koa hook is never called, when defined after `app.use(bodyParser)` #230

Closed iambumblehead closed 1 year ago

iambumblehead commented 1 year ago

Describe the bug

Node.js version: v19.9.0

OS version: Darwin MBP 21.6.0 Darwin Kernel Version 21.6.0: Thu Mar 9 20:08:59 PST 2023; root:xnu-8020.240.18.700.8~1/RELEASE_X86_64 x86_64

Description: functions added to koa's close hook are never called, when the hook is defined after app.use(bodyParser)

Actual behavior

Using the pattern below in a koa service with koa-body, nothing is printed to the process output when a client prematurely disconnects,

  const bodyParser = koaBody({
    patchNode: true,
    text: false, // Only JSON bodies
    urlencoded: false
  })

  app.use(bodyParser)
  app.use(async ctx => {
    ctx.req.on('close', () => {
      console.log('disconnected')
    })
  })

Expected behavior

"disconnected" should be printed to the process output

Code to reproduce

Sorry to leave this empty now If I have energy to setup a more elaborate test sample later, I will. Using console.log, I was able to find that this call mutates the ctx object and seems to originate the problem.

related upstream issue: https://github.com/cojs/co-body/issues/85

the cojs project appears to have been abandoned :/

Checklist

iambumblehead commented 1 year ago

this is likely a configuration issue from my side -- closing