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

NextApiRequest is not pipe-able readable stream #7912

Closed
xlambda opened this issue Jul 12, 2019 · 6 comments
Closed

NextApiRequest is not pipe-able readable stream #7912

xlambda opened this issue Jul 12, 2019 · 6 comments

Comments

@xlambda
Copy link

xlambda commented Jul 12, 2019

NextApiRequest is suposed to be a Readable Stream by extending http.IncomingMessage, but no data sends to the Writable Stream when piping. Sample code:

export default function upload(req:NextApiRequest, res:NextApiResponse) {
  var busboy = new Busboy({ headers: req.headers });
  busboy.on('file', () => console.log("this line of code will not be reached"));
  return req.pipe(busboy);
}

I guess the Readable Stream data is consumed when the body attribute is initiated, so I modify the above code as to

export default function upload(req:NextApiRequest, res:NextApiResponse) {
  var busboy = new Busboy({ headers: req.headers });
  busboy.on('file', () => console.log("OK"));
  const s = new Readable();
  s.push(req.body);
  s.push(null);
  return s.pipe(busboy)
}

and it does work.

@Janpot
Copy link
Contributor

Janpot commented Jul 12, 2019

#7661

@huv1k
Copy link
Contributor

huv1k commented Jul 12, 2019

Hey @xlambda, you can read stream but you need to provide [config] (https://github.com/zeit/next.js#api-middlewares) to opt-out from body parsing.

export const config = {
  api: {
    bodyParser: false,
  },
}

@huv1k huv1k closed this as completed Jul 12, 2019
@Janpot
Copy link
Contributor

Janpot commented Jul 12, 2019

@huv1k Would it make sense to add a property in next.config.js that overrides the default values for the config export in all the routes in the project?

// next.config.js
module.exports = {
  defaultRouteConfig: {
    amp: 'hybrid',
    api: {
      bodyParser: false
    }
  }
}

which then still can be overriden on a per route basis

// pages/the-one-that-is-not-amp.js
export const config = {
  amp: false
}

// api/the-one-that-wants-a-parsed-body.js
export const config = {
  api: {
    bodyParser: true
  }
}

Reason is that I might use my own library to parse these requests throughout my project, and it wouldn't be very ergonomic to have to declare this export everywhere.

@huv1k
Copy link
Contributor

huv1k commented Jul 12, 2019

Hey @Janpot, we have a plan for this feature

@Janpot
Copy link
Contributor

Janpot commented Jul 12, 2019

Is there a ticket I can follow?

@balazsorban44
Copy link
Member

This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@vercel vercel locked as resolved and limited conversation to collaborators Jan 31, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants