@fastify/multipart
Advanced tools
Comparing version 7.5.0 to 7.6.0
import { Busboy, BusboyConfig, BusboyFileStream } from "@fastify/busboy"; | ||
import { FastifyPluginCallback } from "fastify"; | ||
import { FastifyPluginCallback, FastifyRequest } from "fastify"; | ||
import { Readable } from "stream"; | ||
@@ -203,3 +203,3 @@ import { FastifyErrorConstructor } from "@fastify/error"; | ||
*/ | ||
onFile?: (part: MultipartFile) => void | Promise<void>; | ||
onFile?: (this: FastifyRequest, part: MultipartFile) => void | Promise<void>; | ||
} | ||
@@ -206,0 +206,0 @@ |
@@ -160,3 +160,3 @@ 'use strict' | ||
if (options.onFile) { | ||
await options.onFile(part) | ||
await options.onFile.call(req, part) | ||
} else { | ||
@@ -163,0 +163,0 @@ await part.toBuffer() |
{ | ||
"name": "@fastify/multipart", | ||
"version": "7.5.0", | ||
"version": "7.6.0", | ||
"description": "Multipart plugin for Fastify", | ||
@@ -38,3 +38,3 @@ "main": "index.js", | ||
"tap": "^16.0.0", | ||
"tsd": "^0.27.0" | ||
"tsd": "^0.28.0" | ||
}, | ||
@@ -41,0 +41,0 @@ "scripts": { |
@@ -86,3 +86,3 @@ # @fastify/multipart | ||
files: 1, // Max number of file fields | ||
headerPairs: 2000 // Max number of header key=>value pairs | ||
headerPairs: 2000, // Max number of header key=>value pairs | ||
parts: 1000 // For multipart forms, the max number of parts (fields + files) | ||
@@ -259,2 +259,4 @@ } | ||
async function onFile(part) { | ||
// you have access to original request via `this` | ||
console.log(this.id) | ||
await pump(part.file, fs.createWriteStream(part.filename)) | ||
@@ -261,0 +263,0 @@ } |
@@ -174,3 +174,2 @@ 'use strict' | ||
t.ok(req.isMultipart()) | ||
t.same(Object.keys(req.body), ['upload', 'hello']) | ||
@@ -344,1 +343,49 @@ | ||
}) | ||
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') | ||
}) |
172383
4915
485