question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Improve developer experience

See original GitHub issue

🚀 Feature Proposal

Today, I needed to develop a simple image upload service in fastify and it felt unsatisfying.

Motivation

Handling multipart form data should be dead simple and intuitive in fastify.

Example

In https://eggjs.org/en/basics/controller.html#acquiring-the-submitted-files you can easily switch different modes without dealing with too much boilerplate.

// Single file in disk storage mode
app.post("/images", async (req, res) => {
  req.file.filename
  req.file.mimetype
  // async / await
})

// Multiple files in disk storage mode
app.post("/images", async (req, res) => {
  req.files
})

// Single file in stream mode
app.post("/images", async (req, res) => {
   const stream = await ctx.getFileStream();
})

// Multiple files in stream mode
app.post("/images", async (req, res) => {
 const parts = ctx.multipart();
    let part;
    // parts() return a promise
    while ((part = await parts()) != null) {}
})

in fastify you need for the same behaviour a lot of boilerplate and you are responsible to handle all cases manually.

fastify.register(require('fastify-multipart'))

fastify.post('/upload', function (req, reply) {
  const mp = req.multipart(handler, function (err) {
    console.log('###2');
    if (err) {
      reply.send(err)
      return
    }
    console.log('upload completed', process.memoryUsage().rss)
    reply.code(200).send()
  })

  mp.on('field', function (key, value) {
    console.log('form-data', key, value)
  })

  function handler (field, file, filename, encoding, mimetype) {
    pump(file, fs.createWriteStream('a-destination'), (err) => {
      console.log('###1');
    })
  }
})

Logs:

server listening on 3000
###2
upload completed 31477760
###1

Additional problems for the users:

  • onEnd is fired before the file was written.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:12 (12 by maintainers)

github_iconTop GitHub Comments

2reactions
StarpTechcommented, Aug 4, 2020

Since this is the core package we should try to improve this package or switch to fastify-multer entirely.

1reaction
fox1tcommented, Aug 4, 2020

We might also move fastify-multer under fastify organization here on GitHub.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Five proven approaches for a better Developer Experience in ...
Five proven approaches for a better Developer Experience in your organisation · 1. Adopt product thinking for technical products and platforms · 2 ......
Read more >
How to Build a Great Developer Experience: A Practical Guide
1. Optimize all processes. Every process matters when building a great developer experience. · 2. Automate developers' workflow · 3. Provide customizable tools ......
Read more >
Top 11 Developer Experience Tools to Improve DX | Clockwise
11 Developer experience tools and strategies to improve DX · Start with company culture · Developer onboarding · Understand the developer journey ·...
Read more >
Good Developer Experience
The Developer Experience (DX) describes the experience developers have while using or working on your product. It is a package of positive and...
Read more >
How can I improve my developer experience? - Launchable
Five approaches that answer “How can I improve my developer experience?” · 1. Know thy enemy: Identify existing and potential risks to your ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found