question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add a stop or abort method

See original GitHub issue

Right now the only way I have found to stop a file form uploading is to call busboy._parser.parser._ignore(). It would be great if you exposed some sort of abort method.

Use case: you want to do some validation on the image, but as soon as the image is not valid, stop uploading the image and return the error to the client. If you’re uploading a 1 gig image and you can figure out 1/4 of the way through parsing it that it’s the wrong width then it’s much better to stop then as opposed to making the user wait until finish event is called.

Example pseudoish code of I’d like to do:

if (self.req.method === 'POST') {
    var busboy = this.busboy = new Busboy({
        headers: self.req.headers
    });

    busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
        file.on('data', function(data) {

            if(IMAGE IS THE WRONG WIDTH) {
                busboy.abort();
            }

            if(IMAGE IS THE WRONG DPI) {
                busboy.abort();
            }

            if(IMAGE IS THE WRONG SOMETHING ELSE) {
                busboy.abort();
            }

        });
    });

    busboy.on('aborted', function() {
        // DO SOMETHING HERE ... maybe return error to the client
    });
}

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:4
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
Kikketercommented, Sep 24, 2015

@mscdex So this is what I have:

    var busboy = new Busboy({
      headers: req.headers,
      limits: {
        files: 1,
        fileSize: 1024
      }
    });
    var saveTo = path.join(__dirname, '../teamPhotos', req.params.id + '.jpg');

    busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
      file.on('limit', function() {
        console.log('limit reached!');
        req.unpipe(busboy);
        res.status(500).end('Limit Reached');  // Not doing anything...
      });
      file.pipe(fs.createWriteStream(saveTo));
    });
    busboy.on('finish', function() {
       res.writeHead(200, {'Connection': 'close'});
       res.end();
    });

    // This doesn't work without piping here, but also always returns 200
    return req.pipe(busboy);

Also I was wrong, 200 isn’t returned. It’s just sitting in pending (never returning). Is there a nice way to end the pipe with the exception? .end() ?

Ok so dur… it was just my status bit I had incorrect:

req.unpipe(busboy);
res.writeHead(500, {'Connection': 'close'});
res.end('Limit Reached');

Thanks

2reactions
mscdexcommented, Oct 2, 2015

A better solution though to avoid processing unnecessary data might be to do something like:

self.req.unpipe(busboy);
// `Connection: close` is important here, to ensure the socket is closed after
// we write our response.
self.res.writeHead(400, { 'Connection': 'close' });
self.res.end();
Read more comments on GitHub >

github_iconTop Results From Across the Web

Steps to Stop or Abort - Informatica Documentation
Click Tasks > Stop or Tasks > Abort. The Workflow Monitor displays the status of the stop or abort command in the Output...
Read more >
How to abort a Task like aborting a Thread ... - Stack Overflow
The Thread. Abort() method is (severely) deprecated. Both Threads and Tasks should cooperate when being stopped, otherwise you run the risk of leaving...
Read more >
XMLHttpRequest.abort() - Web APIs - MDN Web Docs
abort () method aborts the request if it has already been sent. When a request is aborted, its readyState is changed to XMLHttpRequest.UNSENT...
Read more >
ABORT Statement - SAS Help Center
The ABORT statement causes SAS to stop processing the current DATA step. What happens next depends on the following information: the method ......
Read more >
Fetch: Abort - The Modern JavaScript Tutorial
It has a single method abort() , · And a single property signal that allows to set event listeners on it.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found