Skip to content

Commit 1a9e20a

Browse files
IqbalLxmcollina
andcommitted
Delete file when user cancel request (#567)
* adding new test * fixup Signed-off-by: Matteo Collina <hello@matteocollina.com> * fixup Signed-off-by: Matteo Collina <hello@matteocollina.com> * fixup Signed-off-by: Matteo Collina <hello@matteocollina.com> * fixup Signed-off-by: Matteo Collina <hello@matteocollina.com> --------- Signed-off-by: Matteo Collina <hello@matteocollina.com> Co-authored-by: Matteo Collina <hello@matteocollina.com>
1 parent 55b4dd3 commit 1a9e20a

File tree

3 files changed

+57
-9
lines changed

3 files changed

+57
-9
lines changed

index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -452,13 +452,14 @@ function fastifyMultipart (fastify, options, done) {
452452
this.savedRequestFiles = []
453453
const tmpdir = (options && options.tmpdir) || os.tmpdir()
454454
this.tmpUploads = []
455+
let i = 0
455456
for await (const file of files) {
456-
const filepath = path.join(tmpdir, generateId() + path.extname(file.filename))
457+
const filepath = path.join(tmpdir, generateId() + path.extname(file.filename || ('file' + i++)))
457458
const target = createWriteStream(filepath)
458459
try {
460+
this.tmpUploads.push(filepath)
459461
await pump(file.file, target)
460462
this.savedRequestFiles.push({ ...file, filepath })
461-
this.tmpUploads.push(filepath)
462463
} catch (err) {
463464
this.log.error({ err }, 'save request file')
464465
throw err

test/big.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ test('should upload a big file in constant memory', { skip: process.env.CI }, fu
4343
}
4444

4545
const memory = process.memoryUsage()
46-
t.ok(memory.rss < 400 * 1024 * 1024) // 200MB
47-
t.ok(memory.heapTotal < 400 * 1024 * 1024) // 200MB
46+
t.ok(memory.rss < 500 * 1024 * 1024)
47+
t.ok(memory.heapTotal < 500 * 1024 * 1024)
4848

4949
reply.send()
5050
})

test/multipart-incomplete-upload.test.js

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
'use strict'
22

3-
const util = require('node:util')
43
const test = require('tap').test
54
const FormData = require('form-data')
65
const Fastify = require('fastify')
76
const multipart = require('..')
87
const http = require('node:http')
9-
const sleep = util.promisify(setTimeout)
8+
const { setTimeout: sleep } = require('node:timers/promises')
109
const { writableNoopStream } = require('noop-stream')
11-
const stream = require('node:stream')
12-
const pipeline = util.promisify(stream.pipeline)
10+
const { pipeline } = require('node:stream/promises')
11+
const { once } = require('node:events')
12+
const fs = require('node:fs/promises')
1313

14-
test('should finish with error on partial upload', async function (t) {
14+
test('should finish with error on partial upload - files api', async function (t) {
1515
t.plan(4)
1616

1717
const fastify = Fastify()
@@ -60,3 +60,50 @@ test('should finish with error on partial upload', async function (t) {
6060
await sleep(100)
6161
t.end()
6262
})
63+
64+
test('should finish with error on partial upload - saveRequestFiles', async function (t) {
65+
t.plan(3)
66+
67+
const fastify = Fastify()
68+
t.teardown(fastify.close.bind(fastify))
69+
70+
await fastify.register(multipart)
71+
72+
let tmpUploads
73+
fastify.post('/', async function (req) {
74+
t.ok(req.isMultipart())
75+
try {
76+
await req.saveRequestFiles()
77+
} finally {
78+
tmpUploads = req.tmpUploads
79+
}
80+
})
81+
82+
await fastify.listen({ port: 0 })
83+
const dataSize = 1024 * 1024 * 1024
84+
85+
// request
86+
const form = new FormData()
87+
form.append('upload', Buffer.alloc(dataSize))
88+
const opts = {
89+
protocol: 'http:',
90+
hostname: 'localhost',
91+
port: fastify.server.address().port,
92+
path: '/',
93+
headers: form.getHeaders(),
94+
method: 'POST'
95+
}
96+
97+
const req = http.request(opts)
98+
const data = form.getBuffer()
99+
req.write(data.slice(0, dataSize / 4))
100+
req.write(data.slice(dataSize / 4, dataSize / 2))
101+
req.end()
102+
103+
const [res] = await once(req, 'response')
104+
t.equal(res.statusCode, 500)
105+
106+
for (const tmpUpload of tmpUploads) {
107+
await t.rejects(fs.access(tmpUpload))
108+
}
109+
})

0 commit comments

Comments
 (0)