Skip to content

Commit

Permalink
feat: add setBoundary method
Browse files Browse the repository at this point in the history
  • Loading branch information
C-Saunders committed Jun 2, 2020
1 parent d702625 commit 55d90ce
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
7 changes: 6 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ form.submit({
- [_Void_ append( **String** _field_, **Mixed** _value_ [, **Mixed** _options_] )](https://github.com/form-data/form-data#void-append-string-field-mixed-value--mixed-options-).
- [_Headers_ getHeaders( [**Headers** _userHeaders_] )](https://github.com/form-data/form-data#array-getheaders-array-userheaders-)
- [_String_ getBoundary()](https://github.com/form-data/form-data#string-getboundary)
- [_Void_ setBoundary()](https://github.com/form-data/form-data#void-setboundary)
- [_Buffer_ getBuffer()](https://github.com/form-data/form-data#buffer-getbuffer)
- [_Integer_ getLengthSync()](https://github.com/form-data/form-data#integer-getlengthsync)
- [_Integer_ getLength( **function** _callback_ )](https://github.com/form-data/form-data#integer-getlength-function-callback-)
Expand Down Expand Up @@ -220,11 +221,15 @@ form.append( 'my_file', fs.createReadStream('/foo/bar.jpg'), {filename: 'bar.jpg
This method adds the correct `content-type` header to the provided array of `userHeaders`.

#### _String_ getBoundary()
Return the boundary of the formData. A boundary consists of 26 `-` followed by 24 numbers
Return the boundary of the formData. By default, the boundary consists of 26 `-` followed by 24 numbers
for example:
```javascript
--------------------------515890814546601021194782
```

#### _Void_ setBoundary(String _boundary_)
Set the boundary string, overriding the default behavior described above.

_Note: The boundary must be unique and may not appear in the data._

#### _Buffer_ getBuffer()
Expand Down
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ declare class FormData extends stream.Readable {
callback?: (error: Error | null, response: http.IncomingMessage) => void
): http.ClientRequest;
getBuffer(): Buffer;
setBoundary(boundary: string): void;
getBoundary(): string;
getLength(callback: (err: Error | null, length: number) => void): void;
getLengthSync(): number;
Expand Down
4 changes: 4 additions & 0 deletions lib/form_data.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,10 @@ FormData.prototype.getHeaders = function(userHeaders) {
return formHeaders;
};

FormData.prototype.setBoundary = function(boundary) {
this._boundary = boundary;
};

FormData.prototype.getBoundary = function() {
if (!this._boundary) {
this._generateBoundary();
Expand Down
23 changes: 23 additions & 0 deletions test/integration/test-set-boundary.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
var common = require('../common');
var assert = common.assert;

var FormData = require(common.dir.lib + '/form_data');

(function testSetBoundary() {
var userBoundary = '---something';
var form = new FormData();
form.setBoundary(userBoundary);

assert.equal(form.getBoundary(), userBoundary);
})();

(function testUniqueBoundaryPerFormAfterSet() {
var userBoundary = '---something';
var formA = new FormData();
formA.setBoundary(userBoundary);

var formB = new FormData();

assert.equal(formA.getBoundary(), userBoundary);
assert.notEqual(formA.getBoundary(), formB.getBoundary());
})();

0 comments on commit 55d90ce

Please sign in to comment.