Skip to content

Commit bd5a7be

Browse files
authored
feat: Use ES modules (#727)
1 parent e4731cd commit bd5a7be

53 files changed

Lines changed: 1622 additions & 1385 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
### Unreleased 3.0
4+
5+
* feat: Use ES modules ([727](https://github.com/node-formidable/formidable/pull/727))
6+
* options.enabledPlugins must contain the plugin themselves instead of the plugins names
7+
8+
39
### Unreleased (`canary` & `dev` dist-tags)
410

511
* feat: add options.filter ([#716](https://github.com/node-formidable/formidable/pull/716))

README.md

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ Parse an incoming file upload, with the
104104
[Node.js's built-in `http` module](https://nodejs.org/api/http.html).
105105

106106
```js
107-
const http = require('http');
108-
const formidable = require('formidable');
107+
import http from 'http';
108+
import formidable from 'formidable';
109109

110110
const server = http.createServer((req, res) => {
111111
if (req.url === '/api/upload' && req.method.toLowerCase() === 'post') {
@@ -152,8 +152,8 @@ Or try the
152152
[examples/with-express.js](https://github.com/node-formidable/formidable/blob/master/examples/with-express.js)
153153

154154
```js
155-
const express = require('express');
156-
const formidable = require('formidable');
155+
import express from 'express';
156+
import formidable from 'formidable';
157157

158158
const app = express();
159159

@@ -198,8 +198,8 @@ which is Node.js's Request, and **NOT** the `ctx.request` which is Koa's Request
198198
object - there is a difference._
199199

200200
```js
201-
const Koa = require('koa');
202-
const formidable = require('formidable');
201+
import Koa from 'Koa';
202+
import formidable from 'formidable';
203203

204204
const app = new Koa();
205205

@@ -298,20 +298,8 @@ _Please pass [`options`](#options) to the function/constructor, not by assigning
298298
them to the instance `form`_
299299

300300
```js
301-
const formidable = require('formidable');
301+
import formidable from 'formidable';
302302
const form = formidable(options);
303-
304-
// or
305-
const { formidable } = require('formidable');
306-
const form = formidable(options);
307-
308-
// or
309-
const { IncomingForm } = require('formidable');
310-
const form = new IncomingForm(options);
311-
312-
// or
313-
const { Formidable } = require('formidable');
314-
const form = new Formidable(options);
315303
```
316304

317305
### Options
@@ -396,8 +384,6 @@ Parses an incoming Node.js `request` containing form data. If `callback` is
396384
provided, all fields and files are collected and passed to the callback.
397385

398386
```js
399-
const formidable = require('formidable');
400-
401387
const form = formidable({ multiples: true, uploadDir: __dirname });
402388

403389
form.parse(req, (err, fields, files) => {
@@ -521,8 +507,6 @@ Formidable instance (the `form` across the README examples) and the options.
521507
**Note:** the plugin function's `this` context is also the same instance.
522508

523509
```js
524-
const formidable = require('formidable');
525-
526510
const form = formidable({ keepExtensions: true });
527511

528512
form.use((self, options) => {
@@ -547,11 +531,10 @@ which is used in [src/plugins/multipart.js](./src/plugins/multipart.js)), then
547531
you can remove it from the `options.enabledPlugins`, like so
548532

549533
```js
550-
const { Formidable } = require('formidable');
551-
552-
const form = new Formidable({
534+
import formidable, {octetstream, querystring, json} from "formidable";
535+
const form = formidable({
553536
hashAlgorithm: 'sha1',
554-
enabledPlugins: ['octetstream', 'querystring', 'json'],
537+
enabledPlugins: [octetstream, querystring, json],
555538
});
556539
```
557540

examples/json.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
'use strict';
1+
import http from 'http';
2+
import util from 'util';
3+
import formidable from '../src/index.js';
24

3-
const http = require('http');
4-
const util = require('util');
5-
const { formidable } = require('../src/index');
65

76
const PORT = 3000;
87
const server = http.createServer((req, res) => {

examples/log-file-content-to-console.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
'use strict';
1+
import http from 'http';
2+
import { Writable } from 'stream';
3+
import formidable from '../src/index.js';
24

3-
const http = require('http');
4-
const { Writable } = require('stream');
5-
const formidable = require('../src/index');
65

76
const server = http.createServer((req, res) => {
87
if (req.url === '/api/upload' && req.method.toLowerCase() === 'post') {

examples/multipart-parser.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
'use strict';
1+
import { MultipartParser } from '../src/index.js';
22

3-
const { MultipartParser } = require('../src/index');
43

54
// hand crafted multipart
65
const boundary = '--abcxyz';

examples/multiples.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
'use strict';
1+
import http from 'http';
2+
import os from 'os';
3+
import formidable from '../src/index.js';
24

3-
const os = require('os');
4-
const http = require('http');
5-
const { Formidable } = require('../src/index');
65

76
const server = http.createServer((req, res) => {
87
if (req.url === '/') {
@@ -28,7 +27,7 @@ const server = http.createServer((req, res) => {
2827
</form>
2928
`);
3029
} else if (req.url === '/upload') {
31-
const form = new Formidable({ multiples: true, uploadDir: os.tmpdir() });
30+
const form = formidable({ multiples: true, uploadDir: os.tmpdir() });
3231

3332
form.parse(req, (err, fields, files) => {
3433
res.writeHead(200, { 'Content-Type': 'application/json' });

examples/store-files-on-s3.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
// To test this example you have to install aws-sdk nodejs package and create a bucket named "demo-bucket"
22

3-
'use strict';
3+
import http from 'http';
4+
import { PassThrough } from 'stream';
5+
import AWS from 'aws-sdk';
6+
import formidable from '../src/index.js';
47

5-
const http = require('http');
6-
const { PassThrough } = require('stream');
7-
// eslint-disable-next-line import/no-unresolved
8-
const AWS = require('aws-sdk');
9-
const formidable = require('../src/index');
108

119
const s3Client = new AWS.S3({
1210
credentials: {

examples/upload-multiple-files.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
'use strict';
1+
import http from 'http';
2+
import util from 'util';
3+
import os from 'os';
4+
import formidable from '../src/index.js';
25

3-
const os = require('os');
4-
const http = require('http');
5-
const util = require('util');
6-
7-
const { Formidable } = require('../src/index');
86

97
const server = http.createServer((req, res) => {
108
if (req.url === '/') {
@@ -17,7 +15,7 @@ const server = http.createServer((req, res) => {
1715
</form>
1816
`);
1917
} else if (req.url === '/upload') {
20-
const form = new Formidable({ multiples: true, uploadDir: os.tmpdir() });
18+
const form = formidable({ multiples: true, uploadDir: os.tmpdir() });
2119
const files = [];
2220
const fields = [];
2321

examples/urlencoded-no-enctype.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
'use strict';
1+
import http from 'http';
2+
import util from 'util';
3+
import formidable from '../src/index.js';
24

3-
const http = require('http');
4-
const util = require('util');
5-
6-
const { Formidable } = require('../src/index');
75

86
const server = http.createServer((req, res) => {
97
if (req.url === '/') {
@@ -16,7 +14,7 @@ const server = http.createServer((req, res) => {
1614
</form>
1715
`);
1816
} else if (req.url === '/post') {
19-
const form = new Formidable();
17+
const form = formidable();
2018
const fields = [];
2119

2220
form

examples/with-express.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
'use strict';
2-
3-
const express = require('express');
4-
const formidable = require('../src/index');
1+
import express from 'express';
2+
import formidable from '../src/index.js';
53

64
const app = express();
75

0 commit comments

Comments
 (0)