Permalink
Browse files

Block use of closed stream

  • Loading branch information...
kornelski committed Jul 25, 2018
1 parent 2cfab3b commit b5d894faaf2d79e2308986f4b62bf282ee19b6a7
Showing with 21 additions and 2 deletions.
  1. +7 −2 lib/node/index.js
  2. +14 −0 test/node/pipe.js
@@ -788,6 +788,11 @@ Request.prototype._emitResponse = function(body, files) {
response.body = body;
}
response.files = files;
if (this._endCalled) {
response.pipe = function() {
throw Error("end() has already been called, so it's too late to start piping");
}
}
this.emit('response', response);
return response;
};
@@ -797,8 +802,8 @@ Request.prototype.end = function(fn) {
debug('%s %s', this.method, this.url);
if (this._endCalled) {
console.warn(
'Warning: .end() was called twice. This is not supported in superagent'
throw Error(
'.end() was called twice. This is not supported in superagent'
);
}
this._endCalled = true;
@@ -45,6 +45,20 @@ describe("request pipe", () => {
stream.pipe(req);
});
it("end() stops piping", done => {
const stream = fs.createWriteStream(destPath);
request.get(base)
.end((err, res) => {
try {
res.pipe(stream);
return done(Error("Did not prevent nonsense pipe"));
} catch(_e) {
/* expected error */
}
done()
});
});
it("should act as a readable stream", done => {
const stream = fs.createWriteStream(destPath);

0 comments on commit b5d894f

Please sign in to comment.