File arriving in larger than expected size in multipart/form-data

We have migrated a large express app to hono + @hono/zod-openapi and everything is great except our multipart/form-data file uploads.

The route looks like this:
import { z, createRoute } from '@hono/zod-openapi'
import  documentService from 'somewhere'
import { HonoPublicAPIApp } from '../types'
import { routePath } from 'hono/route'

const bodySchema = z.object({
  name: z.string(),
  file: z.file(),
  dateIssued: z.iso.date().pipe(z.coerce.date()).optional(),
  dateExpiry: z.iso.date().pipe(z.coerce.date()).optional()
  ...
})

const route = createRoute({
  method: 'post',
  path: '/v1/documents',
  request: {
    body: {
      content: {
        'multipart/form-data': {
          schema: bodySchema
        }
      }
    }
  },
  responses: {
    200: {
      description: 'Document created successfully',
      content: {
        'application/json': {
          schema: responseSchema
        }
      }
    }
  }
})

export const registerAddDocumentRoute = (app: HonoPublicAPIApp) => {
  app.openapi(route, async (c) => {
    const form = c.req.valid('form')

    const document = await documentService.createDocument({
      file: form.file,
      name: form.name,
      dateExpiry: form.dateExpiry,
      dateIssued: form.dateIssued
      ...
    })

    return c.json({
      data: {
        documentId: document.data?.documentId || ''
      }
    })
  })
}


The file is correctly available with all the details and content but they problem is that the content appears to be corrupt in some way since the size of the file uploaded is 1.2mb and the file.size in the route handler comes to around 2mb. When I save the file (an image) in S3 and retrieve it it's not consider an image.

Note that all the code for saving/retrieving file was working in the express app and is 100% the same. What could be the cause here?

Only difference is that in the express app we used multer to parse the request body where as in hono I'm not using anything.
Was this page helpful?