Skip to content

Commit 7d1166e

Browse files
authored
feat: support minChunkSize options (#26)
Some web servers have a limit on the number of chunks, and you can set `minChunkSize` to ensure the size of chunk sent to the server. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Introduced a new feature to set the minimum chunk buffer size for web servers in the `formstream` library. - **Documentation** - Updated README with CI/CD badges, improved code formatting, and enhanced API documentation. - Updated LICENSE file to include MIT License text. - **Tests** - Added new test case for handling specific `minChunkSize` setting in `formstream`. - Modified existing test cases with new field values, event handling, and error management. - **Chores** - Added `node-hex` dependency to the project. - Updated CI workflow configuration with `CODECOV_TOKEN`. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent 350c6b9 commit 7d1166e

10 files changed

Lines changed: 263 additions & 86 deletions

File tree

.github/workflows/ci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@ jobs:
1313
with:
1414
os: 'ubuntu-latest, macos-latest'
1515
version: '14, 16, 18, 20, 22'
16+
secrets:
17+
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

LICENSE

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
MIT License
2+
3+
Copyright (c) 2012 - 2014 fengmk2 <fengmk2@gmail.com>
4+
Copyright (c) 2014-present node-modules and other contributors.
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.

README.md

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
# formstream
22

33
[![NPM version][npm-image]][npm-url]
4+
[![CI](https://github.com/node-modules/formstream/actions/workflows/ci.yml/badge.svg)](https://github.com/node-modules/formstream/actions/workflows/ci.yml)
5+
[![Test coverage][codecov-image]][codecov-url]
46
[![npm download][download-image]][download-url]
57

68
[npm-image]: https://img.shields.io/npm/v/formstream.svg?style=flat-square
79
[npm-url]: https://npmjs.org/package/formstream
10+
[codecov-image]: https://codecov.io/github/node-modules/formstream/coverage.svg?branch=master
11+
[codecov-url]: https://codecov.io/github/node-modules/formstream?branch=master
812
[download-image]: https://img.shields.io/npm/dm/formstream.svg?style=flat-square
913
[download-url]: https://npmjs.org/package/formstream
1014

@@ -13,7 +17,7 @@ A [multipart/form-data](http://tools.ietf.org/html/rfc2388) encoded stream, help
1317
## Install
1418

1519
```bash
16-
$ npm install formstream
20+
npm install formstream
1721
```
1822

1923
## Quick Start
@@ -58,19 +62,44 @@ var formstream = require('formstream');
5862

5963
var filepath = './logo.png';
6064
fs.stat(filepath, function (err, stat) {
61-
formstream().field('status', 'share picture')
62-
.field('access_token', 'your access token')
63-
.file('pic', filepath, 'logo.png', stat.size)
64-
.pipe(process.stdout); // your request stream
65+
formstream()
66+
.field('status', 'share picture')
67+
.field('access_token', 'your access token')
68+
.file('pic', filepath, 'logo.png', stat.size)
69+
.pipe(process.stdout); // your request stream
70+
});
71+
```
72+
73+
### Set min chunk buffer size
74+
75+
Some web servers have a limit on the number of chunks, and you can set `minChunkSize` to ensure the size of chunk sent to the server.
76+
77+
```js
78+
var fs = require('fs');
79+
var FormStream = require('formstream');
80+
81+
var filepath = './big-file.zip';
82+
fs.stat(filepath, function (err, stat) {
83+
new FormStream({
84+
// send >= 2MB chunk buffer size to the server
85+
minChunkSize: 1024 * 1024 * 2,
86+
}).field('status', 'share file')
87+
.field('access_token', 'your access token')
88+
.file('file', filepath, 'big-file.zip', stat.size)
89+
.pipe(process.stdout); // your request stream
6590
});
6691
```
6792

6893
## API Doc
6994

70-
### formstream()
95+
### formstream([options])
7196

7297
Create a form instance.
7398

99+
#### Arguments
100+
101+
- **options.minChunkSize** Number - min chunk size to emit data event
102+
74103
#### Returns
75104

76105
Form - form instance
@@ -140,7 +169,7 @@ Get headers for the request.
140169

141170
#### Arguments
142171

143-
- ***headers*** Object - Additional headers
172+
- **headers** Object - Additional headers
144173

145174
#### Example
146175

@@ -173,29 +202,8 @@ See [Node.js Documentation](http://nodejs.org/api/stream.html#stream_event_end)
173202

174203
## License
175204

176-
(The MIT License)
177-
178-
Copyright (c) 2012 - 2014 fengmk2 <fengmk2@gmail.com>
179-
Copyright(c) node-modules and other contributors.
180-
181-
Permission is hereby granted, free of charge, to any person obtaining
182-
a copy of this software and associated documentation files (the
183-
'Software'), to deal in the Software without restriction, including
184-
without limitation the rights to use, copy, modify, merge, publish,
185-
distribute, sublicense, and/or sell copies of the Software, and to
186-
permit persons to whom the Software is furnished to do so, subject to
187-
the following conditions:
188-
189-
The above copyright notice and this permission notice shall be
190-
included in all copies or substantial portions of the Software.
191-
192-
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
193-
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
194-
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
195-
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
196-
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
197-
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
198-
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
205+
[MIT](LICENSE)
206+
199207
<!-- GITCONTRIBUTOR_START -->
200208

201209
## Contributors
@@ -206,4 +214,4 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
206214

207215
This project follows the git-contributor [spec](https://github.com/xudafeng/git-contributor), auto updated at `Wed May 15 2024 00:34:12 GMT+0800`.
208216

209-
<!-- GITCONTRIBUTOR_END -->
217+
<!-- GITCONTRIBUTOR_END -->

example/upload.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
var http = require('http');
44
var fs = require('fs');
55
var path = require('path');
6-
var formstream = require('../');
6+
var formstream = require('..');
77

8-
var imagepath = path.join(path.dirname(__dirname), 'logo.png');
8+
var imagepath = path.join(path.dirname(__dirname), 'test/fixtures/logo.png');
99

10-
var form = formstream();
10+
var form = formstream({
11+
// minChunkSize: 1024 * 1024,
12+
});
1113
// form.file('file', filepath, filename);
1214
form.stream('file', fs.createReadStream(imagepath), 'logo.png');
1315
form.field('foo', 'hello world');

0 commit comments

Comments
 (0)