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

fix: make sure the handler resolves in all cases #507

Merged
merged 28 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,16 +226,16 @@ function fastifyMultipart (fastify, options, done) {
this.log.trace({ busboyOptions }, 'Providing options to busboy')
const bb = busboy(busboyOptions)

request.on('close', cleanup)
request.on('error', cleanup)
request.on('close', cleanup)

bb
.on('field', onField)
.on('file', onFile)
.on('close', cleanup)
.on('error', onEnd)
.on('end', onEnd)
.on('finish', onEnd)
.on('error', onEnd)
.on('close', cleanup)

bb.on('partsLimit', function () {
const err = new PartsLimitError()
Expand Down Expand Up @@ -379,17 +379,19 @@ function fastifyMultipart (fastify, options, done) {
}

function onEnd (err) {
cleanup()
cleanup(err)

ch(err || lastError)
ch(null)
}

function cleanup (err) {
request.unpipe(bb)
// in node 10 it seems that error handler is not called but request.aborted is set
if ((err || request.aborted) && currentFile) {

if ((err || lastError || request.aborted) && currentFile) {
currentFile.destroy()
}

ch(err || lastError)
}

return parts
Expand Down
55 changes: 55 additions & 0 deletions test/multipart.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -634,3 +634,58 @@ test('should not miss fields if part handler takes much time than formdata parsi
await once(res, 'end')
t.pass('res ended successfully')
})

test('should not freeze when error is thrown during processing', async function (t) {
t.plan(2)
const app = Fastify()

app
.register(multipart)

app
.post('/', async (request, reply) => {
const files = request.files()

for await (const { file } of files) {
try {
const storage = new stream.Writable({
write (chunk, encoding, callback) {
// trigger error:
callback(new Error('write error'))
}
})

await pump(file, storage)
} catch {}
}

return { message: 'done' }
})

await app.listen()

const { port } = app.server.address()

const form = new FormData()
const opts = {
hostname: '127.0.0.1',
port,
path: '/',
headers: form.getHeaders(),
method: 'POST'
}
const req = http.request(opts)

try {
form.append('upload', fs.createReadStream(filePath))
form.pipe(req)
} catch {}

const [res] = await once(req, 'response')
t.equal(res.statusCode, 200)
res.resume()
await once(res, 'end')
t.pass('res ended successfully')

await app.close()
})
Loading