-
-
Notifications
You must be signed in to change notification settings - Fork 120
feat: add transformRequest option for custom request streams #604
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
7b5cdc7
da7d4a0
ba39e00
36ca0ad
1c2341c
38aac28
88a2615
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -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') | ||||||||||||||
|
|
||||||||||||||
|
|
@@ -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 | ||||||||||||||
|
|
@@ -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, | ||||||||||||||
|
|
@@ -297,7 +300,12 @@ function fastifyMultipart (fastify, options, done) { | |||||||||||||
| process.nextTick(() => cleanup(err)) | ||||||||||||||
| }) | ||||||||||||||
|
|
||||||||||||||
| request.pipe(bb) | ||||||||||||||
| const stream = transformRequest(request) | ||||||||||||||
|
||||||||||||||
| const stream = transformRequest(request) | |
| const stream = transformRequest(request) | |
| if (stream !== request && typeof stream?.on === 'function') { | |
| stream.on('close', cleanup) | |
| stream.on('error', cleanup) | |
| } |
There was a problem hiding this comment.
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
Uh oh!
There was an error while loading. Please reload this page.