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

Error when POSTing large files #37

Open
maximedupre opened this issue Oct 26, 2017 · 7 comments
Open

Error when POSTing large files #37

maximedupre opened this issue Oct 26, 2017 · 7 comments
Labels
Pkg: koa-better-body Priority: Critical This should be dealt with ASAP. Not fixing this issue would be a serious error. Status: Accepted It's clear what the subject of the issue is about, and what the resolution should be. Status: Blocked Another issue needs to be resolved first or an external blocker. Type: Bug Inconsistencies or issues which will cause an issue or problem for users or implementors. Type: Enhancement Most issues will probably be for additions or changes. Expected that this will result in a PR.

Comments

@maximedupre
Copy link

This error seems to be similar to helapkg/hela#3. When I try to upload a large video file (622MB), I get this error:

TypeError: Cannot read property 'fields' of undefined
    at Object.parseBody 
    (/api/node_modules/koa-better-body/utils.js:222:33)
    at parseBody.next (<anonymous>)
    at Object.plugin 
    (/api/node_modules/koa-better-body/index.js:50:21)
    at plugin.next (<anonymous>)
    at onFulfilled
    (/api/node_modules/co/index.js:65:19)
    at <anonymous>

My configs are as follow:

let bodyParserMiddleWare = bodyParser({fields: 'body', formLimit: '1000mb'});

My request is multipart and my code looks like this:

let create = async (ctx, next) => {
    let file = ctx.request.files[0]; // I have access to the file I am uploading

    await ... // code that writes file to disk

    someFunction() // never executed as the error posted above occurs while the "await" is being executed
}

router.post('/', create);

This bug doesn't occur with smaller files. Is there a file limit in koa-better-body? A request timeout?

Thanks.

@tunnckoCore
Copy link
Owner

Is there a file limit in koa-better-body? A request timeout?

No. Everything is defined by the user. Probably something is wrong in the node-formidable. I'm thinking to switch the things under the hood, in v4 tunnckoCore/koa-better-body#90, but don't know when.

@tunnckoCore
Copy link
Owner

tunnckoCore commented Oct 26, 2017

And btw, error isn't related to the file limits or so. Can you just try to remove the fields: 'body' and try again?

edit: Actually yea, it seems that the multipart thing (which is formidable) returns undefined and on that line we can't read the result.fields. So yea, some formidable problem with large files, you can open an issue there.

@maximedupre
Copy link
Author

I tried removing fields: 'body' and it still doesn't work.

I'll post an issue on the node-formidable repo, but in the meantime, what other good body parser for koajs would you recommend?

Cheers!

@tunnckoCore
Copy link
Owner

For multipart body parsing... don't know, there is not such that not need manual work. You may try koa-body which i wrote too, but is maintained by another person, but it is also using Formidable, so i think it won't work in your case too.

So the only way is to use directly busboy or such. I'm heavily deciding to switch koa-better-body to use busboy under the hood in v4.

@alwxkxk
Copy link

alwxkxk commented Dec 14, 2017

Here is a success example use async-busboy:

const asyncBusboy = require('async-busboy')
const path = require('path')
const fs = require('fs')

router.post('/upload', async function (ctx, next) {
  let { fields, files } = await asyncBusboy(ctx.req)
  console.log(fields, files)
  if (files) {
    files.map(file => {
      const saveTo = path.join(__dirname, path.basename(file.filename))
      file.pipe(fs.createWriteStream(saveTo))
    })
  }
  return next()
}, function * (next) {
  yield next
})

btw I find another problem that if I use koa-better-body,the files become empty array [].

const body = require('koa-better-body')
router.post('/upload',body() async function (ctx, next) {
  let { fields, files } = await asyncBusboy(ctx.req)
  console.log(fields, files)//files is []
  if (files) {
    files.map(file => {
      const saveTo = path.join(__dirname, path.basename(file.filename))
      file.pipe(fs.createWriteStream(saveTo))
    })
  }
  return next()
}, function * (next) {
  yield next
})

@tunnckoCore
Copy link
Owner

yea, busboy works.

as about the other part of the comment.. it totally not make sense to me to use koa better body and busboy in one place.

dont know.. I really hope to get this done soon ;/

@tunnckoCore
Copy link
Owner

tunnckoCore commented Jun 24, 2018

Aaah.. Partially got it.
Probably goes to error or aborted events here and so result is undefined.

Further debugging may continue once i convert it to async/await instead of generators.
So probably the v4 at tunnckoCore/koa-better-body#90.

@tunnckoCore tunnckoCore transferred this issue from helapkg/hela Sep 18, 2019
@tunnckoCore tunnckoCore changed the title Error when POSTing large files koa-better-body: Error when POSTing large files Sep 18, 2019
@tunnckoCore tunnckoCore added Priority: High Type: Bug Inconsistencies or issues which will cause an issue or problem for users or implementors. Type: Enhancement Most issues will probably be for additions or changes. Expected that this will result in a PR. Type: Maintenance Updating phrasing or wording to make things clearer or removing ambiguity, without a functionality. Priority: Critical This should be dealt with ASAP. Not fixing this issue would be a serious error. Status: Accepted It's clear what the subject of the issue is about, and what the resolution should be. Status: Blocked Another issue needs to be resolved first or an external blocker. and removed prs welcome Type: Maintenance Updating phrasing or wording to make things clearer or removing ambiguity, without a functionality. labels Oct 18, 2019
@tunnckoCore tunnckoCore changed the title koa-better-body: Error when POSTing large files Error when POSTing large files Oct 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Pkg: koa-better-body Priority: Critical This should be dealt with ASAP. Not fixing this issue would be a serious error. Status: Accepted It's clear what the subject of the issue is about, and what the resolution should be. Status: Blocked Another issue needs to be resolved first or an external blocker. Type: Bug Inconsistencies or issues which will cause an issue or problem for users or implementors. Type: Enhancement Most issues will probably be for additions or changes. Expected that this will result in a PR.
Projects
None yet
Development

No branches or pull requests

3 participants