Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,26 @@ try {

```

You can provide a custom stream using the `transformRequest` option. This is useful for environments like Google Cloud Functions where the request body has already been consumed:

```js
const { Readable } = require('node:stream')

// In environments like Google Cloud Functions/Firebase, `request.rawBody`
// contains the full raw request body buffer/string.
fastify.register(require('@fastify/multipart'), {
transformRequest: (request) => Readable.from(request.rawBody)
})

// In a standard Fastify/Node.js server, `request.raw` is already a readable
// stream for the incoming HTTP request, so you can usually omit
// `transformRequest` entirely or simply return `request.raw`:
//
// fastify.register(require('@fastify/multipart'), {
// transformRequest: (request) => request.raw
// })
```

Additionally, you can pass per-request options to the `req.file`, `req.files`, `req.saveRequestFiles` or `req.parts` function.

```js
Expand Down
14 changes: 10 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const { generateId } = require('./lib/generateId')
const createError = require('@fastify/error')
const streamToNull = require('./lib/stream-consumer')
const deepmergeAll = require('@fastify/deepmerge')({ all: true })
const { PassThrough, Readable } = require('node:stream')
const { PassThrough, Readable, pipeline } = require('node:stream')
const { pipeline: pump } = require('node:stream/promises')
const secureJSON = require('secure-json-parse')

Expand All @@ -26,6 +26,7 @@ const InvalidMultipartContentTypeError = createError('FST_INVALID_MULTIPART_CONT
const InvalidJSONFieldError = createError('FST_INVALID_JSON_FIELD_ERROR', 'a request field is not a valid JSON as declared by its Content-Type', 406)
const FileBufferNotFoundError = createError('FST_FILE_BUFFER_NOT_FOUND', 'the file buffer was not found', 500)
const NoFormData = createError('FST_NO_FORM_DATA', 'FormData is not available', 500)
const InvalidTransformRequestError = createError('FST_INVALID_TRANSFORM_REQUEST', 'transformRequest must return a readable stream', 500)

function setMultipart (req, _payload, done) {
req[kMultipart] = true
Expand Down Expand Up @@ -171,6 +172,8 @@ function fastifyMultipart (fastify, options, done) {
? options.throwFileSizeLimit
: true

const transformRequest = typeof options.transformRequest === 'function' ? options.transformRequest : (request) => request

fastify.decorate('multipartErrors', {
PartsLimitError,
FilesLimitError,
Expand Down Expand Up @@ -297,7 +300,12 @@ function fastifyMultipart (fastify, options, done) {
process.nextTick(() => cleanup(err))
})

request.pipe(bb)
const stream = transformRequest(request)
Copy link

Copilot AI Feb 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When transformRequest returns a different stream than the original request, event listeners for 'close' and 'error' are only attached to the original request object (lines 275-276, outside this diff). This means errors or close events from the transformed stream won't trigger cleanup, potentially leading to resource leaks.

Consider also attaching event listeners to the transformed stream when it differs from the original request, similar to how event listeners are attached to the request object.

Suggested change
const stream = transformRequest(request)
const stream = transformRequest(request)
if (stream !== request && typeof stream?.on === 'function') {
stream.on('close', cleanup)
stream.on('error', cleanup)
}

Copilot uses AI. Check for mistakes.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is correct to fix, but possibly with a different implementation

if (!stream || typeof stream.pipe !== 'function') throw new InvalidTransformRequestError()

pipeline(stream, bb, (err) => {
cleanup(err)
})

function onField (name, fieldValue, fieldnameTruncated, valueTruncated, encoding, contentType) {
// don't overwrite prototypes
Expand Down Expand Up @@ -435,8 +443,6 @@ function fastifyMultipart (fastify, options, done) {
}

function cleanup (err) {
request.unpipe(bb)

if ((err || request.aborted) && currentFile) {
currentFile.destroy()
currentFile = null
Expand Down
Loading