Skip to content

Commit 6aef4e1

Browse files
digitalmioEomm
andauthored
Bind original request to onFile function this (#431)
* Bind `req` to `onFile` function `this` * Type fix * Readme change * Use `call` instead of `bind` Co-authored-by: Manuel Spigolon <[email protected]> --------- Co-authored-by: Manuel Spigolon <[email protected]>
1 parent ebe06f3 commit 6aef4e1

File tree

4 files changed

+53
-4
lines changed

4 files changed

+53
-4
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,8 @@ You can also define an `onFile` handler to avoid accumulating all files in memor
255255

256256
```js
257257
async function onFile(part) {
258+
// you have access to original request via `this`
259+
console.log(this.id)
258260
await pump(part.file, fs.createWriteStream(part.filename))
259261
}
260262

index.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Busboy, BusboyConfig, BusboyFileStream } from "@fastify/busboy";
2-
import { FastifyPluginCallback } from "fastify";
2+
import { FastifyPluginCallback, FastifyRequest } from "fastify";
33
import { Readable } from "stream";
44
import { FastifyErrorConstructor } from "@fastify/error";
55

@@ -200,7 +200,7 @@ declare namespace fastifyMultipart {
200200
/**
201201
* Manage the file stream like you need
202202
*/
203-
onFile?: (part: MultipartFile) => void | Promise<void>;
203+
onFile?: (this: FastifyRequest, part: MultipartFile) => void | Promise<void>;
204204
}
205205

206206
export const fastifyMultipart: FastifyMultipartPlugin;

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ function fastifyMultipart (fastify, options, done) {
157157
req.body = part.fields
158158
if (part.file) {
159159
if (options.onFile) {
160-
await options.onFile(part)
160+
await options.onFile.call(req, part)
161161
} else {
162162
await part.toBuffer()
163163
}

test/multipart-attach-body.test.js

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,6 @@ test('should be able to attach all parsed field values and files with custom "on
171171

172172
fastify.post('/', async function (req, reply) {
173173
t.ok(req.isMultipart())
174-
175174
t.same(Object.keys(req.body), ['upload', 'hello'])
176175

177176
t.equal(req.body.upload, original)
@@ -340,3 +339,51 @@ test('should manage array fields', async function (t) {
340339
await once(res, 'end')
341340
t.pass('res ended successfully')
342341
})
342+
343+
test('should be able to attach all parsed field values and files with custom "onFile" handler with access to request object bind to "this"', async function (t) {
344+
t.plan(6)
345+
346+
const fastify = Fastify()
347+
t.teardown(fastify.close.bind(fastify))
348+
349+
async function onFile (part) {
350+
t.pass('custom onFile handler')
351+
t.equal(this.id, 'req-1')
352+
t.equal(typeof this, 'object')
353+
const buff = await part.toBuffer()
354+
const decoded = Buffer.from(buff.toString(), 'base64').toString()
355+
part.value = decoded
356+
}
357+
358+
fastify.register(multipart, { attachFieldsToBody: 'keyValues', onFile })
359+
360+
const original = 'test upload content'
361+
362+
fastify.post('/', async function (req, reply) {
363+
t.ok(req.isMultipart())
364+
reply.code(200).send()
365+
})
366+
367+
await fastify.listen({ port: 0 })
368+
369+
// request
370+
const form = new FormData()
371+
const opts = {
372+
protocol: 'http:',
373+
hostname: 'localhost',
374+
port: fastify.server.address().port,
375+
path: '/',
376+
headers: form.getHeaders(),
377+
method: 'POST'
378+
}
379+
380+
const req = http.request(opts)
381+
form.append('upload', Readable.from(Buffer.from(original).toString('base64')))
382+
form.pipe(req)
383+
384+
const [res] = await once(req, 'response')
385+
t.equal(res.statusCode, 200)
386+
res.resume()
387+
await once(res, 'end')
388+
t.pass('res ended successfully')
389+
})

0 commit comments

Comments
 (0)