Skip to content

When apply { attachFieldsToBody: 'keyValues' }, stringified buffer may not a valid file #367

Closed
@iketiunn

Description

@iketiunn

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Fastify version

4.1.0

Plugin version

7.1.0

Node.js version

v16.14.2

Operating system

macOS

Operating system version (i.e. 20.04, 11.3, 10)

12.3

Description

In my case, when I apply { attachFieldsToBody: 'keyValues' }, the uploaded image was been stringified from buffer with buffer.toString() which is default to uft-8 encoding. And it might not able to be converted correctly back to the buffer.

body[key] = field._buf.toString()

Maybe buffer.toString('binary') or buffer.toString('base64') is more general and acceptable encoding.
If there is no other concern I could make a PR for it.

Steps to Reproduce

import fastifyMultipart from '@fastify/multipart'
import { writeFile} from 'fs/promises'

app.register(fastifyMultipart, { attachFieldsToBody: 'keyValues' })
app.post('upload', async (req, reply) => {
  const buffer = Buffer.from(req.image)
  await writeFile('path', buffer) // which is not a valid image
  reply.send()
})

Expected Behavior

The written image should be read correctly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions