Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

formidable

Package Overview
Dependencies
Maintainers
5
Versions
79
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

formidable - npm Package Compare versions

Comparing version 3.3.2 to 3.4.0

2

package.json
{
"name": "formidable",
"version": "3.3.2",
"version": "3.4.0",
"license": "MIT",

@@ -5,0 +5,0 @@ "description": "A node.js module for parsing form data, especially file uploads.",

@@ -103,9 +103,11 @@ <p align="center">

const server = http.createServer((req, res) => {
const server = http.createServer(async (req, res) => {
if (req.url === '/api/upload' && req.method.toLowerCase() === 'post') {
// parse a file upload
const form = formidable({});
form.parse(req, (err, fields, files) => {
if (err) {
let fields;
let files;
try {
[fields, files] = await form.parse(req);
} catch (err) {
// example to check for a very specific error

@@ -115,10 +117,9 @@ if (err.code === formidableErrors.maxFieldsExceeded) {

}
console.error(err);
res.writeHead(err.httpCode || 400, { 'Content-Type': 'text/plain' });
res.end(String(err));
return;
}
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ fields, files }, null, 2));
});
}
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ fields, files }, null, 2));
return;

@@ -387,6 +388,5 @@ }

### .parse(request, callback)
### .parse(request, ?callback)
Parses an incoming Node.js `request` containing form data. If `callback` is
provided, all fields and files are collected and passed to the callback.
Parses an incoming Node.js `request` containing form data. If `callback` is not provided a promise is returned.

@@ -400,2 +400,5 @@ ```js

});
// with Promise
const [fields, files] = await form.parse(req);
```

@@ -402,0 +405,0 @@

@@ -181,37 +181,52 @@ /* eslint-disable class-methods-use-this */

// returns a promise if no callback is provided
async parse(req, cb) {
this.req = req;
let promise;
// Setup callback first, so we don't miss anything from data events emitted immediately.
if (cb) {
const callback = once(dezalgo(cb));
this.fields = {};
const files = {};
this.on('field', (name, value) => {
if (this.type === 'multipart' || this.type === 'urlencoded') {
if (!hasOwnProp(this.fields, name)) {
this.fields[name] = [value];
} else {
this.fields[name].push(value);
}
} else {
this.fields[name] = value;
}
if (!cb) {
let resolveRef;
let rejectRef;
promise = new Promise((resolve, reject) => {
resolveRef = resolve;
rejectRef = reject;
});
this.on('file', (name, file) => {
if (!hasOwnProp(files, name)) {
files[name] = [file];
cb = (err, fields, files) => {
if (err) {
rejectRef(err);
} else {
files[name].push(file);
resolveRef([fields, files]);
}
});
this.on('error', (err) => {
callback(err, this.fields, files);
});
this.on('end', () => {
callback(null, this.fields, files);
});
}
}
const callback = once(dezalgo(cb));
this.fields = {};
const files = {};
this.on('field', (name, value) => {
if (this.type === 'multipart' || this.type === 'urlencoded') {
if (!hasOwnProp(this.fields, name)) {
this.fields[name] = [value];
} else {
this.fields[name].push(value);
}
} else {
this.fields[name] = value;
}
});
this.on('file', (name, file) => {
if (!hasOwnProp(files, name)) {
files[name] = [file];
} else {
files[name].push(file);
}
});
this.on('error', (err) => {
callback(err, this.fields, files);
});
this.on('end', () => {
callback(null, this.fields, files);
});
// Parse headers and setup the parser, ready to start listening for data.

@@ -244,3 +259,5 @@ await this.writeHeaders(req.headers);

});
if (promise) {
return promise;
}
return this;

@@ -247,0 +264,0 @@ }

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc