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.

Okay, some thoughts.

I think we can just wrap the Node’s stream.pipeline() and provide it to our users, but they still will be able to do whatever they want, using the Node’s pipeline(). Something like this

const { pipeline, MultipartParser, formidable } = require('formidable');

// accepts a callback, or returns a promise if no cb
const { fields, files } = await pipeline(req, formidableOptions);

// if they want more transform or whatever,
// they can add more things, just like in the Node's pipeline()
const { fields, files } = await pipeline(
  req,
  ...streams,
  ...iterators,
);

// or directly using the native pipeline()

const parser = new MultipartParser(); // it's Transform Stream

await promisify(stream.pipeline)(
  req,
  parser,
  formidable(),
);

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:41 (32 by maintainers)

github_iconTop GitHub Comments

2reactions
GrosSacASaccommented, Sep 29, 2021

I see it as breaking changes (fields and files output changen new dependency) so we can start with version 4.x I add you to formdiable github so you can create new branches and make commits if you want to try to implement your ideas in a new branch, (use 3.x as a starting point)

Overall I like the ideas to move towards web apis when possible.

2reactions
jimmywartingcommented, Sep 29, 2021

FormData package is for sending formdata, and formidable is for receiving. I am not sure if we can reuse some code. Or are you proposing to use FormData package to make it easy to forward a received form ? In that case feel free to add an example inside ./examples first.

FormData could be used for both sending and receiving, We wouldn’t have Response.formData() or Request.formData() otherwise. They exist so you can intercept the payload from Service Worker. FormData did not have any method for reading/manipulating a FormData earlier but now we have .has(), .delete(), .get(), .entries(), for..of and so on

So yes, I’m proposing to use a FormData package to make it easy to forward a received form. But also for parsing and consuming a received form

I think it would be cool if form.formData() could utilize a FormData package and resolve the stream with a FormData instance. Here is an example from the Readme that i have rewritten:

import { createServer } from 'node:http'
import formidable from 'formidable'

const server = createServer(async (req, res) => {
  if (req.url === '/api/upload' && req.method.toLowerCase() === 'post') {
    // parse a file upload
    const form = formidable({ uploadDir: '/tmp' });

    // Similar to whatwg `request.fromData()`
    const formData = await form.formData(req)
    console.log(...formData.entries())

    // file is a web File object
    const file = formData.get('file') // ( The file could be backed up by the filesystem - in /tmp dir )
    console.log(file.name)

    res.writeHead(200, { 
      'Content-Type': file.type,
      'Content-Length': file.size
    })

    // file.stream() is a whatwg:stream
    for await (const uint8 of file.stream()) {
      res.send(uint8)
    }
		
    return res.end()
  }

  // show a file upload form
  res.writeHead(200, { 'Content-Type': 'text/html' });
  res.end(`
    <h2>With Node.js <code>"http"</code> module</h2>
    <form action="/api/upload" enctype="multipart/form-data" method="post">
      <div>Text field title: <input type="text" name="title" /></div>
      <div>File: <input type="file" name="file" /></div>
      <input type="submit" value="Upload" />
    </form>
  `)
})

server.listen(8080, () => {
  console.log('Server listening on http://localhost:8080/ ...')
});

(calling form.formData() would work for both urlencoded and multipart payloads)


On a side note: you said you wanted to handle a (web)stream… that would be 👍, it would be great if formidable was not so bound to HttpIncomingRequest so you can just hand it some iterable object (wheater it be stream, req, webstream or any asyncIterator) - so something like: const formData = await form.formData(iterable, contentType) maybe? idk

Read more comments on GitHub >

github_iconTop Results From Across the Web

Planning - V3 Companies
V3's highly-skilled planning team utilizes its diverse local, national and international experience to provide professional advice that results in pragmatic ...
Read more >
Plan V1 V2 V3 V4 V5, Past Simple and Past Participle Form of ...
Plan V1 V2 V3 V4 V5, Past Simple and Past Participle Form of Plan. Verb; Plan. Meaning; scheme, layout, plot, program. V1, V2,...
Read more >
App Service Pricing | Microsoft Azure
Free and Shared (preview) Azure App Service plans provide different options to test ... App Service Environment v3 is now generally available -...
Read more >
V3 plans - TBM
Find here the V3 plan of the TBM network. ... My account. The TBM network. Fil d'Ariane. Home · V3; Maps. Map of...
Read more >
My v3 ads || all plan details - YouTube
myv3ads #tamil #v3onlinetv #tamil #workfromhomejob #onlinejobs My V3 ads zero investment plan more details https://w.
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