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

[Recommend] Let FileStream inherit fs.ReadStream #173

Open
ahuigo opened this issue Jul 30, 2018 · 0 comments
Open

[Recommend] Let FileStream inherit fs.ReadStream #173

ahuigo opened this issue Jul 30, 2018 · 0 comments

Comments

@ahuigo
Copy link

ahuigo commented Jul 30, 2018

I found that busboy's FileStream inherits ReadableStream, so FileStream is missing bytesRead.

Could FileStream inherits fs.ReadStream to support bytesRead property like this:

busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
          const fs = require('fs')
          const ws = fs.createWriteStream('busboy.txt')
          const ps = file.pipe(ws)
          ps.on('finish', ()=>{
            console.log(file.bytesRead); // support bytesRead here
            console.log(ws.bytesWritten) 
            console.log(ps.bytesWritten) 
          })
    });

Full demo:

var http = require('http'),
    inspect = require('util').inspect;

var Busboy = require('busboy');

http.createServer(function(req, res) {
  if (req.method === 'POST') {
    var busboy = new Busboy({ headers: req.headers });
    busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
          const fs = require('fs')
          const ws = fs.createWriteStream('busboy.txt')
          const ps = file.pipe(ws)
          ps.on('finish', ()=>{
            console.log(file.bytesRead) 
            console.log(ws.bytesWritten) 
            console.log(ps.bytesWritten) 
          })
    });
    busboy.on('field', console.log);
    busboy.on('finish', function() {
      console.log('Done parsing form!');
      res.writeHead(303, { Connection: 'close', Location: '/' });
      res.end();
    });
    req.pipe(busboy);
  } else if (req.method === 'GET') {
    res.writeHead(200, { Connection: 'close' });
    res.end('<html><head></head><body>\
               <form method="POST" enctype="multipart/form-data">\
                <input type="file" name="filefield"><br />\
                <input type="submit">\
              </form>\
            </body></html>');
  }
}).listen(8000, function() {
  console.log('Listening for requests: http://localhost:8000');
});

@ahuigo ahuigo changed the title [Recommend] Let FileStream inherits fs.ReadStream [Recommend] Let FileStream inherit fs.ReadStream Jul 30, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants