Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RequestInit in fetch duplex option is required when sending a body #4

Closed
KararTY opened this issue Apr 26, 2023 · 2 comments
Closed

Comments

@KararTY
Copy link

KararTY commented Apr 26, 2023

Related nodejs/node#46221

{
  "type": "TypeError",
  "message": "RequestInit: duplex option is required when sending a body.",
  "stack":
    TypeError: RequestInit: duplex option is required when sending a body.
      at new Request (node:internal/deps/undici/undici:7225:19)
      at createNodeRequest (file:///___/node_modules/authey/dist/index.mjs:32:10)
      at file:///___/node_modules/authey/dist/index.mjs:105:23
      at Holder.done (___\node_modules\@fastify\middie\engine.js:112:11)
      at Object.run (___\node_modules\@fastify\middie\engine.js:59:12)
      at Object.runMiddie (file:///___/node_modules/fastify-next-auth/dist/index.mjs:21:12)
      at hookIterator (___\node_modules\fastify\lib\hooks.js:293:10)
      at next (___\node_modules\fastify\lib\hooks.js:185:16)
      at handleResolve (___\node_modules\fastify\lib\hooks.js:196:5)
}

Adding duplex: 'half' between line 30 & 31 in src/fetch.ts fixes it.

https://github.com/wobsoriano/authey/blob/main/src/fetch.ts#L24-L37

Running Node.js v18.16.0, fastify 4.16.3, fastify-next-auth 0.3.0

@Benzolio
Copy link
Contributor

Benzolio commented Apr 29, 2023

Typescript 5.0.4 considers use of duplex as an error. The property duplex does not exist in type RequestInit as defined in typescript/lib/lib.dom.d.ts, but this is due to an omission on the type which is pointed out at microsoft/TypeScript-DOM-lib-generator#1483

This doesn't cause an error for GET and HEAD requests, since duplex only needs to be specified when there is a body in the request, so it could alternately be added around line 34:

  if (req.method !== 'GET' && req.method !== 'HEAD'){
    init.body = req as any
    init.duplex = 'half';
  }

Either way makes the resulting javascript functional, but both approaches run into the type checking error due to the incomplete type definition issue, bu

Judging by the conversation around an issue in node itself nodejs/node#46221 it sounds like the duplex option requirement is not likely to be reverted.

@wobsoriano
Copy link
Owner

Released. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants