Skip to content

Commit

Permalink
Bind original request to onFile function this (#431)
Browse files Browse the repository at this point in the history
* Bind `req` to `onFile` function `this`

* Type fix

* Readme change

* Use `call` instead of `bind`

Co-authored-by: Manuel Spigolon <behemoth89@gmail.com>

---------

Co-authored-by: Manuel Spigolon <behemoth89@gmail.com>
  • Loading branch information
digitalmio and Eomm committed Apr 7, 2023
1 parent ebe06f3 commit 6aef4e1
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 4 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,8 @@ You can also define an `onFile` handler to avoid accumulating all files in memor

```js
async function onFile(part) {
// you have access to original request via `this`
console.log(this.id)
await pump(part.file, fs.createWriteStream(part.filename))
}

Expand Down
4 changes: 2 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Busboy, BusboyConfig, BusboyFileStream } from "@fastify/busboy";
import { FastifyPluginCallback } from "fastify";
import { FastifyPluginCallback, FastifyRequest } from "fastify";
import { Readable } from "stream";
import { FastifyErrorConstructor } from "@fastify/error";

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

export const fastifyMultipart: FastifyMultipartPlugin;
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ function fastifyMultipart (fastify, options, done) {
req.body = part.fields
if (part.file) {
if (options.onFile) {
await options.onFile(part)
await options.onFile.call(req, part)
} else {
await part.toBuffer()
}
Expand Down
49 changes: 48 additions & 1 deletion test/multipart-attach-body.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,6 @@ test('should be able to attach all parsed field values and files with custom "on

fastify.post('/', async function (req, reply) {
t.ok(req.isMultipart())

t.same(Object.keys(req.body), ['upload', 'hello'])

t.equal(req.body.upload, original)
Expand Down Expand Up @@ -340,3 +339,51 @@ test('should manage array fields', async function (t) {
await once(res, 'end')
t.pass('res ended successfully')
})

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) {
t.plan(6)

const fastify = Fastify()
t.teardown(fastify.close.bind(fastify))

async function onFile (part) {
t.pass('custom onFile handler')
t.equal(this.id, 'req-1')
t.equal(typeof this, 'object')
const buff = await part.toBuffer()
const decoded = Buffer.from(buff.toString(), 'base64').toString()
part.value = decoded
}

fastify.register(multipart, { attachFieldsToBody: 'keyValues', onFile })

const original = 'test upload content'

fastify.post('/', async function (req, reply) {
t.ok(req.isMultipart())
reply.code(200).send()
})

await fastify.listen({ port: 0 })

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

const req = http.request(opts)
form.append('upload', Readable.from(Buffer.from(original).toString('base64')))
form.pipe(req)

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

0 comments on commit 6aef4e1

Please sign in to comment.