Skip to content

Commit

Permalink
Fix form emitting aborted error after close
Browse files Browse the repository at this point in the history
fixes #138
closes #219
  • Loading branch information
ryhinchey authored and dougwilson committed Jul 27, 2020
1 parent 30ba066 commit aa940c7
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
1 change: 1 addition & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ unreleased
==========

* Fix empty files on Node.js 14.x
* Fix form emitting aborted error after close
* Replace `fd-slicer` module with internal transform stream
* deps: http-errors@~1.8.0
- Fix error creating objects in some environments
Expand Down
6 changes: 6 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ Form.prototype.parse = function(req, cb) {
var self = this;
var waitend = true;

self.on('close', onClosed)

if (cb) {
// if the user supplies a callback, this implies autoFields and autoFiles
self.autoFields = true;
Expand Down Expand Up @@ -183,6 +185,10 @@ Form.prototype.parse = function(req, cb) {
setUpParser(self, boundary);
req.pipe(self);

function onClosed () {
req.removeListener('aborted', onReqAborted)
}

function onReqAborted() {
waitend = false;
self.emit('aborted');
Expand Down
41 changes: 41 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,47 @@ var standaloneTests = [
});
}
},
{
name: 'connection aborted after close does error',
fn: function (cb) {
var body =
'--foo\r\n' +
'Content-Disposition: form-data; name="file1"; filename="file1"\r\n' +
'Content-Type: application/octet-stream\r\n' +
'\r\nThis is the file\r\n' +
'--foo--\r\n'
var client = null
var error = null
var server = http.createServer(function (req, res) {
var form = new multiparty.Form()

form.on('close', function () {
client.destroy()
setTimeout(function () {
assert.ifError(error)
server.close(cb)
}, 100)
})

form.on('error', function (err) {
error = err
})

form.on('part', function (part) {
part.resume()
})

form.parse(req)
}).listen(0, 'localhost', function () {
client = net.connect(server.address().port)
client.write(
'POST / HTTP/1.1\r\n' +
'Content-Length: ' + Buffer.byteLength(body) + '\r\n' +
'Content-Type: multipart/form-data; boundary=foo\r\n\r\n' +
body)
});
}
},
{
name: 'content transfer encoding',
fn: function(cb) {
Expand Down

0 comments on commit aa940c7

Please sign in to comment.