Visitar URL original
Treat the destruction of the request stream as a cancellation of the … · axios/axios@77012c3 · GitHub
Skip to content

Commit 77012c3

Browse files
Treat the destruction of the request stream as a cancellation of the request;
Fixed tests;
1 parent 511356e commit 77012c3

2 files changed

Lines changed: 45 additions & 22 deletions

File tree

‎lib/adapters/http.js‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -555,11 +555,25 @@ module.exports = function httpAdapter(config) {
555555

556556
// Send the request
557557
if (utils.isStream(data)) {
558-
stream.pipeline(data, req, function(err) {
559-
if (err) {
560-
reject(AxiosError.from(err, config, null, req));
558+
var ended = false;
559+
var errored = false;
560+
561+
data.on('end', function() {
562+
ended = true;
563+
});
564+
565+
data.once('error', function(err) {
566+
errored = true;
567+
req.destroy(err);
568+
});
569+
570+
data.on('close', function() {
571+
if (!ended && !errored) {
572+
abort(new CanceledError('Request stream has been aborted', config, req));
561573
}
562574
});
575+
576+
data.pipe(req);
563577
} else {
564578
req.end(data);
565579
}

‎test/unit/adapters/http.js‎

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,9 @@ function generateReadableStream(length = 1024 * 1024, chunkSize = 10 * 1024, sle
9090

9191
yield chunk;
9292

93-
await setTimeoutAsync(sleep);
93+
if (sleep) {
94+
await setTimeoutAsync(sleep);
95+
}
9496
}
9597
}());
9698
}
@@ -733,21 +735,19 @@ describe('supports http with nodejs', function () {
733735
});
734736
});
735737

736-
it('should pass errors for a failed stream', function (done) {
738+
it('should pass errors for a failed stream', async function () {
739+
server = await startHTTPServer();
740+
737741
var notExitPath = path.join(__dirname, 'does_not_exist');
738742

739-
server = http.createServer(function (req, res) {
740-
req.pipe(res);
741-
}).listen(4444, function () {
742-
axios.post('http://localhost:4444/',
743+
try {
744+
await axios.post(LOCAL_SERVER_URL,
743745
fs.createReadStream(notExitPath)
744-
).then(function (res) {
745-
assert.fail('expected ENOENT error');
746-
}).catch(function (err) {
747-
assert.equal(err.message, `ENOENT: no such file or directory, open \'${notExitPath}\'`);
748-
done();
749-
}).catch(done);
750-
});
746+
);
747+
assert.fail('expected ENOENT error');
748+
} catch (err) {
749+
assert.equal(err.message, `ENOENT: no such file or directory, open \'${notExitPath}\'`);
750+
}
751751
});
752752

753753
it('should destroy the response stream with an error on request stream destroying', async function () {
@@ -761,10 +761,19 @@ describe('supports http with nodejs', function () {
761761

762762
const {data} = await axios.post(LOCAL_SERVER_URL, stream, {responseType: 'stream'});
763763

764+
let streamError;
765+
766+
data.on('error', function(err) {
767+
streamError = err;
768+
});
769+
764770
try {
765771
await pipelineAsync(data, devNull());
766-
} catch (err){
767-
assert.strictEqual(err.code, 'ERR_STREAM_PREMATURE_CLOSE');
772+
assert.fail('stream was not aborted');
773+
} catch(e) {
774+
console.log(`pipeline error: ${e}`);
775+
} finally {
776+
assert.strictEqual(streamError && streamError.code, 'ERR_CANCELED');
768777
}
769778
});
770779
});
@@ -1873,11 +1882,11 @@ describe('supports http with nodejs', function () {
18731882
try {
18741883
await pipelineAsync(data, devNull());
18751884
assert.fail('stream was not aborted');
1876-
} catch (err) {
1877-
assert.ok(err.code === axios.AxiosError.ERR_CANCELED || err.code === 'ERR_STREAM_PREMATURE_CLOSE');
1885+
} catch(e) {
1886+
console.log(`pipeline error: ${e}`);
1887+
} finally {
1888+
assert.strictEqual(streamError && streamError.code, 'ERR_CANCELED');
18781889
}
1879-
1880-
assert.strictEqual(streamError && streamError.code, 'ERR_CANCELED');
18811890
});
18821891
})
18831892
});

0 commit comments

Comments
 (0)