m4nuC/async-busboy

writing unfinished while get files

youngerheart opened this issue · 8 comments

var {files, fields} = await parseFile(ctx.req, {autoFields: true});
fs.readFileSync(files[0].path); // <buffer > (a empty buffer, because writing is unfinished)

is that current?

Now I have to code like this:

return new Promise((resolve, reject) => {
  let index = 0;
  let interval = setInterval(() => {
    let data = fs.readFileSync(file.path);
    if (data) {
      resolve(parse(data));
      clearInterval(interval);
    } else if (index > 10) {
      reject(new RestError(400, 'FILE_LOAD_ERR', 'no file loaded'));
      clearInterval(interval);
    }
    index++;
  }, 100);
});

and it returned current buffer.

m4nuC commented

Hi, Can you describe what you are looking to achieve with your sample code ?

Hello, I want to read a xls file's buffer, use fs.readFileSync, and the file path is from

var {files, fields} = await parseFile(ctx.req, {autoFields: true});
fs.readFileSync(files[0].path); // <buffer > (I get a empty buffer)

So is file writing finished after await?
After I set a interval I get the current buffer.

m4nuC commented

After await you are insured that the upload stream has finished, but the actual files may still being written to disk.

Maybe you could send a PR to expose the write stream for these kind of use cases?

m4nuC commented

I miss understood your use case, getting access to the FileStreams wouldn't help you much here. How did you end up working around that ?

'After await you are insured that the upload stream has finished, but the actual files may still being written to disk.' - Is this still the case? While using asnyc-busboy, I am experiencing that randomly, the file return from await doesn't emit 'data' event and only emits 'end' event. The fs.readFileSync logs empty buffer as well. <- With more rounds of testing, I noticed that there are success cases with empty buffer logged as well.

I am using async-busboy@0.3.4 and node@6.9.1 on windows.

Thank you! Also please let me know if this should be opened as a new ticket.

const { files } = await asyncBusboy(ctx.req);
const file = files[0];
console.log(fs.readFileSync(files[0].path)); // logs empty buffer in the case that no 'data' event is emitted.
     
const csvString = await new Promise((resolve, reject) => { // eslint-disable-line
    let data = '';
    file.on('data', (chunk) => {
        data += chunk.toString();
    });
    file.on('error', ()=> {
        // No error logged 
    });
    file.on('end', () => {
        return resolve(data);
    })
});

Same for me.
Please reopen this issue.

m4nuC commented

@mijikim Please go ahead a submit a new issue.

I won't be able to look into that this week so if anyone wants to get stab at it, help is welcome

same problem on windows just like @mijikim