Skip to content

Commit b26c51e

Browse files
Juan Sotosindresorhus
authored andcommitted
Allow calling of got.stream via options (#452)
Fixes #362
1 parent 5a88943 commit b26c51e

3 files changed

Lines changed: 29 additions & 1 deletion

File tree

index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,13 @@ function normalizeArguments(url, opts) {
586586

587587
function got(url, opts) {
588588
try {
589-
return asPromise(normalizeArguments(url, opts));
589+
const normalizedArgs = normalizeArguments(url, opts);
590+
591+
if (normalizedArgs.stream) {
592+
return asStream(normalizedArgs);
593+
}
594+
595+
return asPromise(normalizedArgs);
590596
} catch (err) {
591597
return Promise.reject(err);
592598
}

readme.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,13 @@ Type: `Object`
105105

106106
Any of the [`http.request`](http://nodejs.org/api/http.html#http_http_request_options_callback) options.
107107

108+
###### stream
109+
110+
Type: `boolean`<br>
111+
Default: `false`
112+
113+
Returns a `Stream` instead of a `Promise`. This is equivalent to calling `got.stream(url, [options])`.
114+
108115
###### body
109116

110117
Type: `string` `Buffer` `stream.Readable`

test/arguments.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {URL} from 'universal-url';
22
import test from 'ava';
3+
import pEvent from 'p-event';
34
import got from '..';
45
import {createServer} from './helpers/server';
56

@@ -21,6 +22,10 @@ test.before('setup', async () => {
2122
res.end(req.url);
2223
});
2324

25+
s.on('/stream', (req, res) => {
26+
res.end('ok');
27+
});
28+
2429
await s.listen(s.port);
2530
});
2631

@@ -81,6 +86,16 @@ test('WHATWG URL support', async t => {
8186
await t.notThrows(got(wURL));
8287
});
8388

89+
test('should return streams when using stream option', async t => {
90+
const data = await pEvent(got(`${s.url}/stream`, {stream: true}), 'data');
91+
t.is(data.toString(), 'ok');
92+
});
93+
94+
test('should not allow stream and JSON option at the same time', async t => {
95+
const error = await t.throws(got(`${s.url}/stream`, {stream: true, json: true}));
96+
t.is(error.message, 'Got can not be used as a stream when the `json` option is used');
97+
});
98+
8499
test.after('cleanup', async () => {
85100
await s.close();
86101
});

0 commit comments

Comments
 (0)