node.jsnestjsfastifynode.js-fscreatewritestream

fs.createWriteStream cut file to 1mib


I use NestJS and fastify for my api-server. For files uploading used @fastify/multipart. This is what the function for downloading a file looks like

async uploadFile(path, file): Promise<void> {
    const normalPath = resolve(`${process.cwd()}/uploads/files/${path}/${file.filename}`)
    
    await pump(file.file, createWriteStream(normalPath))
}

In the "file" comes the fastify-type

interface MultipartFile {
      toBuffer: () => Promise<Buffer>
      file: NodeJS.ReadableStream
      filepath: string
      fieldname: string
      filename: string
      encoding: string
      mimetype: string
      fields: import('@fastify/multipart').MultipartFields
    }

Uploading a file via HTTP passes in its entirety, but as soon as the file is written to disk there is only 1024 kb (in some cases less, I do not understand the pattern)

I also tried to pass a ReadableStream to a function like this:

export const writeFileStream = (stream, path) => {
  return new Promise((resolve, reject) =>
    stream
      .on('error', error => {
        if (stream.truncated) unlinkSync(path)
        reject(error)
      })
      .pipe(createWriteStream(path))
      .on('error', error => reject(error))
      .on('finish', () => resolve({ path }))
  )
}

No errors, but the result is the same: 1 megabyte of the entire file. This code used to work, the environment did not change. What can it be? The bug is observed in both local (windows) and container (alpine) environments


Solution

  • The problem is solved by increasing the limit in the @fastify/multipart configuration

    app.register(require('@fastify/multipart'), { limits: { fileSize: 5242880 } })