Skip to content
This repository was archived by the owner on Aug 11, 2021. It is now read-only.

Commit f11a002

Browse files
aredridelothiym23
authored andcommitted
request: add support for streaming bodies
1 parent 0b595c4 commit f11a002

File tree

2 files changed

+86
-1
lines changed

2 files changed

+86
-1
lines changed

lib/request.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,39 @@ function makeRequest (uri, params, cb_) {
146146
this.log.http('request', params.method, parsed.href || '/')
147147

148148
var done = requestDone.call(this, params.method, uri, cb)
149-
var req = request(opts, decodeResponseBody(done))
149+
var req = request(opts, params.streaming ? undefined : decodeResponseBody(done))
150150

151151
req.on('error', cb)
152152
req.on('socket', function (s) {
153153
s.on('error', cb)
154154
})
155155

156+
if (params.streaming) {
157+
req.on('response', function (response) {
158+
159+
if (response.statusCode >= 400) {
160+
var parts = []
161+
response.on('data', function (data) {
162+
parts.push(data)
163+
})
164+
response.on('end', function () {
165+
decodeResponseBody(done)(null, response, Buffer.concat(parts))
166+
})
167+
} else {
168+
169+
response.on('end', function () {
170+
// don't ever re-use connections that had server errors.
171+
// those sockets connect to the Bad Place!
172+
if (response.socket && response.statusCode > 500) {
173+
response.socket.destroy()
174+
}
175+
})
176+
177+
return cb(null, response)
178+
}
179+
})
180+
}
181+
156182
if (params.body && (params.body instanceof Stream)) {
157183
params.body.pipe(req)
158184
}

test/fetch-streaming.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
var test = require('tap').test
2+
var concat = require('concat-stream')
3+
4+
var server = require('./lib/server.js')
5+
var common = require('./lib/common.js')
6+
var client = common.freshClient()
7+
8+
var testData = JSON.stringify({test: true})
9+
var errorData = JSON.stringify({error: 'it went bad'})
10+
11+
test('streaming fetch', function (t) {
12+
server.expect('/test', function (req, res) {
13+
t.equal(req.method, 'GET', 'got expected method')
14+
15+
res.writeHead(200, {
16+
'content-type': 'application/json'
17+
})
18+
19+
res.end(testData)
20+
})
21+
22+
server.expect('/error', function (req, res) {
23+
t.equal(req.method, 'GET', 'got expected method')
24+
25+
res.writeHead(401, {
26+
'content-type': 'application/json'
27+
})
28+
29+
res.end(errorData)
30+
})
31+
32+
client.fetch(
33+
'http://localhost:1337/test',
34+
{ streaming: true },
35+
function (er, res) {
36+
t.ifError(er, 'loaded successfully')
37+
38+
var sink = concat(function (data) {
39+
t.deepEqual(data.toString(), testData)
40+
client.fetch(
41+
'http://localhost:1337/error',
42+
{ streaming: true },
43+
function (er, res) {
44+
t.ok(er, 'got an error')
45+
server.close()
46+
t.end()
47+
}
48+
)
49+
50+
})
51+
52+
res.on('error', function (error) {
53+
t.ifError(error, 'no errors on stream')
54+
})
55+
56+
res.pipe(sink)
57+
}
58+
)
59+
})

0 commit comments

Comments
 (0)