Browse Source

Add readable streams support in options.body

http2
Vsevolod Strukchinsky 10 years ago
parent
commit
7693a7d645
  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 assign = require('object-assign');
var read = require('read-all-stream'); var read = require('read-all-stream');
var timeout = require('timed-out'); var timeout = require('timed-out');
var isReadableStream = require('isstream').isReadable;
function got(url, opts, cb) { function got(url, opts, cb) {
if (typeof opts === 'function') { if (typeof opts === 'function') {
@ -98,7 +99,7 @@ function got(url, opts, cb) {
} }
if (!proxy) { if (!proxy) {
req.end(body); isReadableStream(body) ? body.pipe(req) : req.end(body);
return; return;
} }
@ -106,7 +107,8 @@ function got(url, opts, cb) {
proxy.write = function () { proxy.write = function () {
throw new Error('got\'s stream is not writable when options.body is used'); 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; return;
} }

2
package.json

@ -32,11 +32,13 @@
], ],
"dependencies": { "dependencies": {
"duplexify": "^3.2.0", "duplexify": "^3.2.0",
"isstream": "^0.1.1",
"object-assign": "^2.0.0", "object-assign": "^2.0.0",
"read-all-stream": "^0.1.0", "read-all-stream": "^0.1.0",
"timed-out": "^2.0.0" "timed-out": "^2.0.0"
}, },
"devDependencies": { "devDependencies": {
"from2-array": "0.0.3",
"tape": "^3.0.3", "tape": "^3.0.3",
"taper": "^0.3.0" "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 ##### 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`. 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 tape = require('tape');
var got = require('../'); var got = require('../');
var server = require('./server.js'); var server = require('./server.js');
var from = require('from2-array');
var s = server.createServer(); var s = server.createServer();
@ -17,7 +18,7 @@ tape('setup', function (t) {
}); });
tape('send data from options with post request', 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) { got(s.url, {body: 'wow'}, function (err, data) {
t.equal(data, 'wow'); 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) { got(s.url, {body: new Buffer('wow')}, function (err, data) {
t.equal(data, 'wow'); t.equal(data, 'wow');
}); });
got(s.url, {body: from(['wow'])}, function (err, data) {
t.equal(data, 'wow');
});
}); });
tape('return writeable stream', function (t) { tape('return writeable stream', function (t) {

Loading…
Cancel
Save