Skip to content

Commit 4c4f2f2

Browse files
wmsmacdonaldTimothyGu
authored andcommitted
Wrap ArrayBuffer with Buffer internally to fix Body methods (node-fetch#426)
1 parent fb86ab3 commit 4c4f2f2

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/body.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ function consumeBody() {
206206

207207
// body is buffer
208208
if (Object.prototype.toString.call(this.body) === '[object ArrayBuffer]') {
209-
return Body.Promise.resolve(this.body);
209+
return Body.Promise.resolve(Buffer.from(this.body));
210210
}
211211

212212
// istanbul ignore if: should never happen

test/test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1771,6 +1771,13 @@ describe('Response', function () {
17711771
});
17721772
});
17731773

1774+
it('should support ArrayBuffer as body', function() {
1775+
const res = new Response(stringToArrayBuffer('a=1'));
1776+
return res.text().then(result => {
1777+
expect(result).to.equal('a=1');
1778+
});
1779+
});
1780+
17741781
it('should support blob as body', function() {
17751782
const res = new Response(new Blob(['a=1']));
17761783
return res.text().then(result => {
@@ -1973,6 +1980,16 @@ describe('Request', function () {
19731980
expect(results[1]).to.equal('a=1');
19741981
});
19751982
});
1983+
1984+
it('should support ArrayBuffer as body', function() {
1985+
const req = new Request('', {
1986+
method: 'POST',
1987+
body: stringToArrayBuffer('a=1')
1988+
});
1989+
return req.text().then(result => {
1990+
expect(result).to.equal('a=1');
1991+
});
1992+
});
19761993
});
19771994

19781995
function streamToPromise(stream, dataHandler) {

0 commit comments

Comments
 (0)