Skip to content

Commit

Permalink
Test calling finished if one of the streams closes prematurely (#213)
Browse files Browse the repository at this point in the history
* Test calling finished if one of the streams closes prematurely

* Simplify eos callback logic

- remove callDoneOnNextEos and count
  • Loading branch information
Vunovati authored Mar 15, 2021
1 parent 54d6a16 commit c489c4b
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 16 deletions.
17 changes: 1 addition & 16 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,6 @@ function fastifyMultipart (fastify, options, done) {
const stream = busboy(busboyOptions)
let completed = false
let files = 0
let count = 0
let callDoneOnNextEos = false

req.on('error', function (err) {
stream.destroy()
Expand All @@ -240,11 +238,9 @@ function fastifyMultipart (fastify, options, done) {

stream.on('finish', function () {
log.debug('finished receiving stream, total %d files', files)
if (!completed && count === files) {
if (!completed) {
completed = true
setImmediate(done)
} else {
callDoneOnNextEos = true
}
})

Expand All @@ -270,17 +266,6 @@ function fastifyMultipart (fastify, options, done) {
if (err) {
completed = true
done(err)
return
}

if (completed) {
return
}

++count
if (callDoneOnNextEos && count === files) {
completed = true
done()
}
}

Expand Down
72 changes: 72 additions & 0 deletions test/legacy/multipart.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,78 @@ test('should call finished when both files are pumped', { skip: process.platform
})
})

test('should call finished if one of the streams closes prematurely', { skip: process.platform === 'win32' }, function (t) {
t.plan(5)

const fastify = Fastify({
logger: {
level: 'debug'
}
})
t.tearDown(fastify.close.bind(fastify))

fastify.register(multipart)

fastify.post('/', function (req, reply) {
let fileCount = 0
t.ok(req.isMultipart())

req.multipart(handler, function () {
t.equal(fileCount, 1)
reply.code(200).send()
})

function handler (field, file, filename, encoding, mimetype) {
const saveTo = path.join(os.tmpdir(), path.basename(filename))
eos(file, function () {
fileCount++
})

file.on('data', function () {
if (fileCount === 0) {
this.destroy()
}
})

pump(file, fs.createWriteStream(saveTo), () => {})
}
})

fastify.listen(0, function () {
// request
const form = new FormData()
const opts = {
protocol: 'http:',
hostname: 'localhost',
port: fastify.server.address().port,
path: '/',
headers: form.getHeaders(),
method: 'POST'
}

const stream1 = fs.createReadStream(filePath)

const req = http.request(opts, (res) => {
t.equal(res.statusCode, 200)
res.resume()
res.on('end', () => {
t.pass('res ended successfully')
})
})

form.append('upload1', stream1, {
filename: 'random-data1'
})
form.append('upload2', stream1, {
filename: 'random-data2'
})

pump(form, req, function (err) {
t.error(err, 'client pump: no err')
})
})
})

test('should error if it is not multipart', { skip: process.platform === 'win32' }, function (t) {
t.plan(4)

Expand Down

0 comments on commit c489c4b

Please sign in to comment.