uNetworking / uWebSockets.js

μWebSockets for Node.js back-ends :metal:
Apache License 2.0
7.76k stars 563 forks source link

async then res.cork(() => { res.writeStatus('101').writeHeader('test', '123'); res.upgrade(...); }) fail #911

Closed e3dio closed 1 year ago

e3dio commented 1 year ago

after async calling res.cork(() => { res.writeStatus('101').writeHeader('test', '123'); res.upgrade(...); }) fails. If you separate the res.upgrade into its own cork it works

e3dio commented 1 year ago

Nevermind works fine sorry I had wrong code

uNetworkingAB commented 1 year ago

Maybe you were on an older version? It was broken before

e3dio commented 1 year ago

Not sure but is good now, this is my current upgrade handler:

app.ws('/ws', {
   upgrade: auth({ new: true }, (res, context) => {
      res.cork(() => {
         if (res.setCookie) res.writeStatus('101').writeHeader('Set-Cookie', res.setCookie);
         res.upgrade({ user: res.user }, ...res.wsHeaders, context);
      });
   })
})
uNetworkingAB commented 1 year ago

That looks good but you shouldn't need to call cork since you are executing inside upgrade handler which already is default corked. But it doesn't harm but still

uNetworkingAB commented 1 year ago

Oh wait you posted this as an example for failure my brain just locked up ńvm

e3dio commented 1 year ago

the auth middleware is async so it needs to cork

uNetworkingAB commented 1 year ago

Oh, cool!

uNetworkingAB commented 1 year ago

That usage looks pretty nice, clean and simple and now with proper corking you get a gold star ⭐