Skip to content

Commit f3338f8

Browse files
authored
feat: allow to set mimeType on field data (#25)
use on alipay openapi upload file see https://opendocs.alipay.com/open-v3/054oog#%E6%96%87%E4%BB%B6%E4%B8%8A%E4%BC%A0 <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Introduced `isFormStream` property in `FormStream` for enhanced data handling. - Updated `FormStream` field method to accept an additional `mimeType` parameter. - Added new script for contributors in `package.json`. - **Bug Fixes** - Updated Node.js CI and release workflows to use the latest configurations from a different repository. - **Documentation** - Updated `README.md` to remove outdated badges and add new contributor information. - Added a note about the project following the git-contributor spec. - **Tests** - Enhanced form stream tests to include new JSON data field validation. - **Chores** - Added `git-contributor` dependency for automated contributor updates. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent d314b0e commit f3338f8

11 files changed

Lines changed: 119 additions & 108 deletions

File tree

.github/workflows/ci.yml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,13 @@ name: CI
33
on:
44
push:
55
branches: [ master ]
6-
76
pull_request:
87
branches: [ master ]
98

10-
workflow_dispatch: {}
11-
129
jobs:
1310
Job:
1411
name: Node.js
15-
uses: artusjs/github-actions/.github/workflows/node-test.yml@v1
12+
uses: node-modules/github-actions/.github/workflows/node-test.yml@master
1613
with:
1714
os: 'ubuntu-latest, macos-latest'
18-
version: '14, 16, 18, 20'
15+
version: '14, 16, 18, 20, 22'

.github/workflows/release.yml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,10 @@ on:
44
push:
55
branches: [ master ]
66

7-
workflow_dispatch: {}
8-
97
jobs:
108
release:
119
name: Node.js
12-
uses: artusjs/github-actions/.github/workflows/node-release.yml@v1
10+
uses: node-modules/github-actions/.github/workflows/node-release.yml@master
1311
secrets:
1412
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
1513
GIT_TOKEN: ${{ secrets.GIT_TOKEN }}
16-
with:
17-
checkTest: false

AUTHORS

Lines changed: 0 additions & 6 deletions
This file was deleted.

CHANGELOG.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,89 @@
2020
### Features
2121

2222
* Porting from new Buffer() to Buffer.from() ([#22](https://github.com/node-modules/formstream/issues/22)) ([f268e86](https://github.com/node-modules/formstream/commit/f268e86e6b55d5b981bb7daf9c8662bdcd820f5c))
23+
24+
---
25+
26+
27+
1.2.0 / 2023-03-17
28+
==================
29+
30+
**features**
31+
* [[`f268e86`](http://github.com/node-modules/formstream/commit/f268e86e6b55d5b981bb7daf9c8662bdcd820f5c)] - feat: Porting from new Buffer() to Buffer.from() (#22) (Matthew Speiser <<mattrspeiser@gmail.com>>)
32+
33+
**others**
34+
* [[`3c09f70`](http://github.com/node-modules/formstream/commit/3c09f700b2440b8f391650c939d91c2d5e4ae9b3)] - test: run on github action (#21) (fengmk2 <<fengmk2@gmail.com>>)
35+
* [[`2d5ed81`](http://github.com/node-modules/formstream/commit/2d5ed81e95b4fde270b5db2720fff0e19effd2dc)] - test: run test on node 12, 14, 16 (#20) (fengmk2 <<fengmk2@gmail.com>>)
36+
37+
1.1.1 / 2021-04-28
38+
==================
39+
40+
**fixes**
41+
* [[`c96ba5b`](http://github.com/node-modules/formstream/commit/c96ba5bace0e96bf39770769e43d9de4271971d8)] - fix: upgrade mime to fix wrong mime type (#19) (shaozj <<shaozj@users.noreply.github.com>>)
42+
43+
**others**
44+
* [[`21918e5`](http://github.com/node-modules/formstream/commit/21918e5fc37a4cea5aae82659d39587f099e805b)] - chore: upgrade devDeps express to latest version (#15) (fengmk2 <<fengmk2@gmail.com>>)
45+
46+
1.1.0 / 2016-12-19
47+
==================
48+
49+
* deps: upgrade mime to latest version (#9)
50+
51+
1.0.0 / 2014-11-04
52+
==================
53+
54+
* fix(field): support stream.field(String, Number)
55+
* chore: fix links
56+
* chore: use npm scripts instead of Makefile
57+
* fix test case on node@0.8
58+
59+
0.0.8 / 2014-01-17
60+
==================
61+
62+
* destroy source stream when formstream destroy()
63+
* add more test cases
64+
65+
0.0.7 / 2013-07-25
66+
==================
67+
68+
* feature: always try to infer `Content-Length` (@xingrz)
69+
* doc improve
70+
71+
0.0.6 / 2013-07-15
72+
==================
73+
74+
* added test cases for chaining call (@xingrz)
75+
* improved docs (@xingrz)
76+
* added chaining support (@xingrz)
77+
* api doc
78+
* fixed test causes
79+
* update dependencies version
80+
81+
0.0.5 / 2012-11-06
82+
==================
83+
84+
* fixed stream error not catch bug
85+
86+
0.0.4 / 2012-11-06
87+
==================
88+
89+
* fixed #2 support form.buffer()
90+
* add doc for setTotalStreamSize()
91+
92+
0.0.3 / 2012-11-06
93+
==================
94+
95+
* support content-length use form.setTotalStreamSize(size)
96+
* update readme
97+
98+
0.0.2 / 2012-10-11
99+
==================
100+
101+
* use buffer-concat support node < 0.8
102+
103+
0.0.1 / 2012-10-11
104+
==================
105+
106+
* support multi streams.
107+
* add one stream support now.
108+
* Initial commit

History.md

Lines changed: 0 additions & 83 deletions
This file was deleted.

README.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
# formstream
22

33
[![NPM version][npm-image]][npm-url]
4-
[![Test coverage][coveralls-image]][coveralls-url]
54
[![npm download][download-image]][download-url]
65

76
[npm-image]: https://img.shields.io/npm/v/formstream.svg?style=flat-square
87
[npm-url]: https://npmjs.org/package/formstream
9-
[coveralls-image]: https://img.shields.io/coveralls/node-modules/formstream.svg?style=flat-square
10-
[coveralls-url]: https://coveralls.io/r/node-modules/formstream?branch=master
118
[download-image]: https://img.shields.io/npm/dm/formstream.svg?style=flat-square
129
[download-url]: https://npmjs.org/package/formstream
1310

@@ -178,7 +175,7 @@ See [Node.js Documentation](http://nodejs.org/api/stream.html#stream_event_end)
178175

179176
(The MIT License)
180177

181-
Copyright (c) 2012 - 2014 fengmk2 &lt;fengmk2@gmail.com&gt;
178+
Copyright (c) 2012 - 2014 fengmk2 <fengmk2@gmail.com>
182179
Copyright(c) node-modules and other contributors.
183180

184181
Permission is hereby granted, free of charge, to any person obtaining
@@ -199,3 +196,14 @@ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
199196
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
200197
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
201198
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
199+
<!-- GITCONTRIBUTOR_START -->
200+
201+
## Contributors
202+
203+
|[<img src="https://avatars.githubusercontent.com/u/156269?v=4" width="100px;"/><br/><sub><b>fengmk2</b></sub>](https://github.com/fengmk2)<br/>|[<img src="https://avatars.githubusercontent.com/u/288288?v=4" width="100px;"/><br/><sub><b>xingrz</b></sub>](https://github.com/xingrz)<br/>|[<img src="https://avatars.githubusercontent.com/u/32174276?v=4" width="100px;"/><br/><sub><b>semantic-release-bot</b></sub>](https://github.com/semantic-release-bot)<br/>|[<img src="https://avatars.githubusercontent.com/u/13151189?v=4" width="100px;"/><br/><sub><b>fjc0k</b></sub>](https://github.com/fjc0k)<br/>|[<img src="https://avatars.githubusercontent.com/u/18096247?v=4" width="100px;"/><br/><sub><b>mrspeiser</b></sub>](https://github.com/mrspeiser)<br/>|[<img src="https://avatars.githubusercontent.com/u/985607?v=4" width="100px;"/><br/><sub><b>dead-horse</b></sub>](https://github.com/dead-horse)<br/>|
204+
| :---: | :---: | :---: | :---: | :---: | :---: |
205+
[<img src="https://avatars.githubusercontent.com/u/7326406?v=4" width="100px;"/><br/><sub><b>shaozj</b></sub>](https://github.com/shaozj)<br/>
206+
207+
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`.
208+
209+
<!-- GITCONTRIBUTOR_END -->

lib/formstream.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
/**
2-
* Data format:
2+
* Form Data format:
33
*
44
55
--FormStreamBoundary1349886663601\r\n
66
Content-Disposition: form-data; name="foo"\r\n
77
\r\n
88
<FIELD-CONTENT>\r\n
99
--FormStreamBoundary1349886663601\r\n
10+
Content-Disposition: form-data; name="data"\r\n
11+
Content-Type: application/json\r\n
12+
\r\n
13+
<JSON-FORMAT-CONTENT>\r\n
14+
--FormStreamBoundary1349886663601\r\n
1015
Content-Disposition: form-data; name="file"; filename="formstream.test.js"\r\n
1116
Content-Type: application/javascript\r\n
1217
\r\n
@@ -49,6 +54,8 @@ function FormStream() {
4954
this._contentLength = 0;
5055
this._isAllStreamSizeKnown = true;
5156
this._knownStreamSize = 0;
57+
58+
this.isFormStream = true;
5259
}
5360

5461
util.inherits(FormStream, Stream);
@@ -128,9 +135,10 @@ FormStream.prototype.file = function (name, filepath, filename, filesize) {
128135
* Add a form field
129136
* @param {String} name field name
130137
* @param {String|Buffer} value field value
138+
* @param {String} [mimeType] field mimeType
131139
* @return {this}
132140
*/
133-
FormStream.prototype.field = function (name, value) {
141+
FormStream.prototype.field = function (name, value, mimeType) {
134142
if (!Buffer.isBuffer(value)) {
135143
// field(String, Number)
136144
// https://github.com/qiniu/nodejs-sdk/issues/123
@@ -139,7 +147,7 @@ FormStream.prototype.field = function (name, value) {
139147
}
140148
value = Buffer.from(value);
141149
}
142-
return this.buffer(name, value);
150+
return this.buffer(name, value, null, mimeType);
143151
};
144152

145153
FormStream.prototype.stream = function (name, stream, filename, mimeType, size) {

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"cov": "egg-bin cov",
1414
"ci": "npm run lint && npm run tsd && npm run cov",
1515
"lint": "jshint .",
16-
"tsd": "tsd"
16+
"tsd": "tsd",
17+
"contributor": "git-contributor"
1718
},
1819
"repository": {
1920
"type": "git",
@@ -38,6 +39,7 @@
3839
"connect-multiparty": "1",
3940
"egg-bin": "^5.6.1",
4041
"express": "^4.16.4",
42+
"git-contributor": "^2.1.5",
4143
"jshint": "^2.13.6",
4244
"pedding": "1",
4345
"should": "4",

test/formstream.test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,14 @@ describe('formstream.test.js', function () {
8181
done = pedding(2, done);
8282
var form = formstream();
8383
form.field('foo', 'bar');
84+
form.field('data', JSON.stringify({ foo: 'bar' }), 'application/json');
8485
form.field('name', '中文名字');
8586
form.field('pwd', '哈哈pwd');
8687
form.on('destroy', done);
8788
post(port, '/post', form, function (err, data) {
8889
should.not.exist(err);
8990
data.body.should.eql({
91+
data: '{"foo":"bar"}',
9092
foo: 'bar',
9193
name: '中文名字',
9294
pwd: '哈哈pwd'

types/formstream.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ declare class FormStream extends Stream {
77
* @param name Name of field
88
* @param value Value of field
99
*/
10-
field(name: string, value: string): this
10+
field(name: string, value: string, contentType?: string): this
1111

1212
/**
1313
* Add a local file to be uploaded to the form.

0 commit comments

Comments
 (0)