Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

check stream readable #58

Merged
merged 1 commit into from
Feb 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ unreleased
==========

* Prevent loss of async hooks context
* Prevent hanging when stream is not readable
* deps: http-errors@2.0.0
- deps: depd@2.0.0
- deps: statuses@2.0.1
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ This error will occur when the given stream has an encoding set on it, making it
a decoded stream. The stream should not have an encoding set and is expected to
emit `Buffer` objects.

#### stream.not.readable

This error will occur when the given stream is not readable.

## Examples

### Simple Express example
Expand Down
6 changes: 6 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,12 @@ function readStream (stream, encoding, length, limit, callback) {
}))
}

if (typeof stream.readable !== 'undefined' && !stream.readable) {
return done(createError(500, 'stream is not readable', {
type: 'stream.not.readable'
}))
}

var received = 0
var decoder

Expand Down
40 changes: 40 additions & 0 deletions test/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,46 @@ describe('using http streams', function () {
})
})

it('should throw if stream is not readable', function (done) {
var server = http.createServer(function onRequest (req, res) {
getRawBody(req, { length: req.headers['content-length'] }, function (err) {
if (err) {
req.resume()
res.statusCode = 500
res.end(err.message)
return
}

getRawBody(req, { length: req.headers['content-length'] }, function (err) {
if (err) {
res.statusCode = 500
res.end('[' + err.type + '] ' + err.message)
} else {
res.statusCode = 200
res.end()
}
})
})
})

server.listen(function onListen () {
var addr = server.address()
var client = http.request({ method: 'POST', port: addr.port })

client.end('hello, world!')

client.on('response', function onResponse (res) {
getRawBody(res, { encoding: true }, function (err, str) {
server.close(function onClose () {
assert.ifError(err)
assert.strictEqual(str, '[stream.not.readable] stream is not readable')
done()
})
})
})
})
})

it('should throw if connection ends', function (done) {
var socket
var server = http.createServer(function onRequest (req, res) {
Expand Down
15 changes: 15 additions & 0 deletions test/native.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,21 @@ run('using native streams', function () {
process.nextTick(done)
})
})

it('should throw if stream is not readable', function (done) {
var stream = createStream(Buffer.from('hello, streams!'))

stream.resume()
stream.on('end', function () {
getRawBody(stream, function (err) {
assert.ok(err)
assert.strictEqual(err.status, 500)
assert.strictEqual(err.type, 'stream.not.readable')
assert.strictEqual(err.message, 'stream is not readable')
process.nextTick(done)
})
})
})
})

function createStream (buf) {
Expand Down