Skip to content

Commit b627d79

Browse files
authored
refactor: use formdata-polyfill library in example (#806)
1 parent e19a4cf commit b627d79

File tree

3 files changed

+43
-14
lines changed

3 files changed

+43
-14
lines changed

examples/multipart-parser.js

+23-14
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
1+
import { Blob } from 'node:buffer';
2+
import { Readable } from 'node:stream';
3+
import { FormData, formDataToBlob } from 'formdata-polyfill/esm.min.js'
14
import { MultipartParser } from '../src/index.js';
25

6+
const blob1 = new Blob(
7+
['Content of a.txt.'],
8+
{ type: 'text/plain' }
9+
);
310

4-
// hand crafted multipart
5-
const boundary = '--abcxyz';
6-
const next = '\r\n';
7-
const formData = 'Content-Disposition: form-data; ';
8-
const bufferToWrite = Buffer.from(
9-
`${boundary}${next}${formData}name="text"${next}${next}some text ...${next}${next}${boundary}${next}${formData}name="z"${next}${next}text inside z${next}${next}${boundary}${next}${formData}name="file1"; filename="a.txt"${next}Content-Type: text/plain${next}${next}Content of a.txt.${next}${next}${boundary}${next}${formData}name="file2"; filename="a.html"${next}Content-Type: text/html${next}${next}<!DOCTYPE html><title>Content of a.html.</title>${next}${next}${boundary}--`,
11+
const blob2 = new Blob(
12+
['<!DOCTYPE html><title>Content of a.html.</title>'],
13+
{ type: 'text/html' }
1014
);
1115

16+
const fd = new FormData();
17+
fd.set('text', 'some text ...');
18+
fd.set('z', 'text inside z');
19+
fd.set('file1', blob1, 'a.txt');
20+
fd.set('file2', blob2, 'a.html');
21+
1222
const multipartParser = new MultipartParser();
1323
multipartParser.on('data', ({ name, buffer, start, end }) => {
1424
console.log(`${name}:`);
@@ -17,12 +27,11 @@ multipartParser.on('data', ({ name, buffer, start, end }) => {
1727
}
1828
console.log();
1929
});
20-
multipartParser.on('error', (error) => {
21-
console.error(error);
22-
});
30+
multipartParser.on('error', console.error);
31+
32+
const blob = formDataToBlob(fd);
33+
const boundary = blob.type.split('boundary=')[1];
34+
35+
multipartParser.initWithBoundary(boundary);
2336

24-
multipartParser.initWithBoundary(boundary.substring(2)); // todo make better error message when it is forgotten
25-
// const shouldWait = !multipartParser.write(buffer);
26-
multipartParser.write(bufferToWrite);
27-
multipartParser.end();
28-
// multipartParser.destroy();
37+
Readable.from(blob.stream()).pipe(multipartParser);

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"eslint-plugin-import": "2.20.2",
4646
"eslint-plugin-prettier": "3.1.3",
4747
"express": "4.17.1",
48+
"formdata-polyfill": "^4.0.10",
4849
"husky": "4.2.5",
4950
"jest": "27.2.4",
5051
"koa": "2.11.0",

yarn.lock

+19
Original file line numberDiff line numberDiff line change
@@ -2298,6 +2298,13 @@ fb-watchman@^2.0.0:
22982298
dependencies:
22992299
bser "2.1.1"
23002300

2301+
fetch-blob@^3.1.2:
2302+
version "3.1.3"
2303+
resolved "https://registry.yarnpkg.com/fetch-blob/-/fetch-blob-3.1.3.tgz#a7dca4855e39d3e3c5a1da62d4ee335c37d26012"
2304+
integrity sha512-ax1Y5I9w+9+JiM+wdHkhBoxew+zG4AJ2SvAD1v1szpddUIiPERVGBxrMcB2ZqW0Y3PP8bOWYv2zqQq1Jp2kqUQ==
2305+
dependencies:
2306+
web-streams-polyfill "^3.0.3"
2307+
23012308
figures@^3.0.0, figures@^3.2.0:
23022309
version "3.2.0"
23032310
resolved "https://registry.yarnpkg.com/figures/-/figures-3.2.0.tgz#625c18bd293c604dc4a8ddb2febf0c88341746af"
@@ -2408,6 +2415,13 @@ form-data@~2.3.2:
24082415
combined-stream "^1.0.6"
24092416
mime-types "^2.1.12"
24102417

2418+
formdata-polyfill@^4.0.10:
2419+
version "4.0.10"
2420+
resolved "https://registry.yarnpkg.com/formdata-polyfill/-/formdata-polyfill-4.0.10.tgz#24807c31c9d402e002ab3d8c720144ceb8848423"
2421+
integrity sha512-buewHzMvYL29jdeQTVILecSaZKnt/RJWjoZCF5OW60Z67/GmSLBkOFM7qh1PI3zFNtJbaZL5eQu1vLfazOwj4g==
2422+
dependencies:
2423+
fetch-blob "^3.1.2"
2424+
24112425
formidable@^1.2.2:
24122426
version "1.2.2"
24132427
resolved "https://registry.yarnpkg.com/formidable/-/formidable-1.2.2.tgz#bf69aea2972982675f00865342b982986f6b8dd9"
@@ -5548,6 +5562,11 @@ wcwidth@^1.0.1:
55485562
dependencies:
55495563
defaults "^1.0.3"
55505564

5565+
web-streams-polyfill@^3.0.3:
5566+
version "3.2.0"
5567+
resolved "https://registry.yarnpkg.com/web-streams-polyfill/-/web-streams-polyfill-3.2.0.tgz#a6b74026b38e4885869fb5c589e90b95ccfc7965"
5568+
integrity sha512-EqPmREeOzttaLRm5HS7io98goBgZ7IVz79aDvqjD0kYXLtFZTc0T/U6wHTPKyIjb+MdN7DFIIX6hgdBEpWmfPA==
5569+
55515570
webidl-conversions@^5.0.0:
55525571
version "5.0.0"
55535572
resolved "https://registry.yarnpkg.com/webidl-conversions/-/webidl-conversions-5.0.0.tgz#ae59c8a00b121543a2acc65c0434f57b0fc11aff"

0 commit comments

Comments
 (0)