Browse Source

Merge pull request #26 from floatdrop/accept-readable-stream-in-body-option

Add readable streams support in options.body
http2
Sindre Sorhus 10 years ago
parent
commit
6625d84ac3
  1. 6
      index.js
  2. 2
      package.json
  3. 2
      readme.md
  4. 7
      test/test-post.js

6
index.js

@ -7,6 +7,7 @@ var duplexify = require('duplexify');
var assign = require('object-assign');
var read = require('read-all-stream');
var timeout = require('timed-out');
var isReadableStream = require('isstream').isReadable;
function got(url, opts, cb) {
if (typeof opts === 'function') {
@ -98,7 +99,7 @@ function got(url, opts, cb) {
}
if (!proxy) {
req.end(body);
isReadableStream(body) ? body.pipe(req) : req.end(body);
return;
}
@ -106,7 +107,8 @@ function got(url, opts, cb) {
proxy.write = function () {
throw new Error('got\'s stream is not writable when options.body is used');
};
req.end(body);
isReadableStream(body) ? body.pipe(req) : req.end(body);
return;
}

2
package.json

@ -32,11 +32,13 @@
],
"dependencies": {
"duplexify": "^3.2.0",
"isstream": "^0.1.1",
"object-assign": "^2.0.0",
"read-all-stream": "^0.1.0",
"timed-out": "^2.0.0"
},
"devDependencies": {
"from2-array": "0.0.3",
"tape": "^3.0.3",
"taper": "^0.3.0"
}

2
readme.md

@ -63,7 +63,7 @@ Encoding to be used on `setEncoding` of the response data. If null, the body is
##### options.body
Type: `string`, `Buffer`
Type: `string`, `Buffer`, `ReadableStream`
Body, that will be sent with `POST` request. If present in `options` and `options.method` is not set - `options.method` will be set to `POST`.

7
test/test-post.js

@ -3,6 +3,7 @@
var tape = require('tape');
var got = require('../');
var server = require('./server.js');
var from = require('from2-array');
var s = server.createServer();
@ -17,7 +18,7 @@ tape('setup', function (t) {
});
tape('send data from options with post request', function (t) {
t.plan(2);
t.plan(3);
got(s.url, {body: 'wow'}, function (err, data) {
t.equal(data, 'wow');
@ -26,6 +27,10 @@ tape('send data from options with post request', function (t) {
got(s.url, {body: new Buffer('wow')}, function (err, data) {
t.equal(data, 'wow');
});
got(s.url, {body: from(['wow'])}, function (err, data) {
t.equal(data, 'wow');
});
});
tape('return writeable stream', function (t) {

Loading…
Cancel
Save