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

fix: ignore request error if request is done #384

Merged
merged 5 commits into from
Jul 5, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
f
  • Loading branch information
killagu committed Jul 5, 2022
commit 400488483be3c1d53e6db583df73793e7d25bd8f
26 changes: 18 additions & 8 deletions lib/urllib.js
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,13 @@ function requestWithCallback(url, args, callback) {
}

function decodeContent(res, body, cb) {
if (responseAborted) {
// err = new Error('Remote socket was terminated before `response.end()` was called');
// err.name = 'RemoteSocketClosedError';
debug('Request#%d %s: Remote socket was terminated before `response.end()` was called', reqId, url);
var err = responseError || new Error('Remote socket was terminated before `response.end()` was called');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

responseError 的变量定义应该放在 decodeContent 前面。

return cb(err);
}
var encoding = res.headers['content-encoding'];
if (body.length === 0 || !encoding) {
return cb(null, body, encoding);
Expand Down Expand Up @@ -758,7 +765,10 @@ function requestWithCallback(url, args, callback) {

args.requestUrls.push(parsedUrl.href);

var hasResponse = false;
var responseError;
function onResponse(res) {
hasResponse = true;
socketHandledResponses = res.socket[SOCKET_RESPONSE_COUNT] = (res.socket[SOCKET_RESPONSE_COUNT] || 0) + 1;
if (timing) {
timing.waiting = Date.now() - requestStartTime;
Expand All @@ -781,7 +791,8 @@ function requestWithCallback(url, args, callback) {
return done(null, null, res);
}

res.on('error', function () {
res.on('error', function (err) {
responseError = err;
debug('Request#%d %s: `res error` event emit, total size %d, socket handled %s requests and %s responses',
reqId, url, responseSize, socketHandledRequests, socketHandledResponses);
});
Expand Down Expand Up @@ -939,12 +950,6 @@ function requestWithCallback(url, args, callback) {
}
}

if (responseAborted) {
// err = new Error('Remote socket was terminated before `response.end()` was called');
// err.name = 'RemoteSocketClosedError';
debug('Request#%d %s: Remote socket was terminated before `response.end()` was called', reqId, url);
}

done(err, data, res);
});
}
Expand Down Expand Up @@ -1163,7 +1168,12 @@ function requestWithCallback(url, args, callback) {

var isRequestDone = false;
function handleRequestError(err) {
if (isRequestDone || !err) {
if (!err) {
return;
}
// only ignore request error if response has been received
// if response has not received, socket error will emit on req
if (isRequestDone && hasResponse) {
return;
}
isRequestDone = true;
Expand Down
9 changes: 3 additions & 6 deletions test/proxy.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var proxy = require('./fixtures/reverse-proxy');
var isNode010 = /^v0\.10\.\d+$/.test(process.version);
var isNode012 = /^v0\.12\.\d+$/.test(process.version);

var testUrl = process.env.CI ? 'https://registry.npmjs.com' : 'https://registry.npm.taobao.org';
var testUrl = process.env.CI ? 'https://registry.npmjs.com' : 'https://registry.npmmirror.com';

if (!isNode010 && !isNode012) {
describe('test/proxy.test.js', function() {
Expand All @@ -25,15 +25,12 @@ if (!isNode010 && !isNode012) {
});

it('should proxy http work', function(done) {
urllib.request('http://registry.npm.taobao.org/pedding/1.0.0', {
dataType: 'json',
urllib.request('http://registry.npmmirror.com/pedding/1.0.0', {
enableProxy: true,
proxy: proxyUrl,
}, function(err, data, res) {
assert(!err);
console.log(res.headers);
assert(data.name === 'pedding');
assert(res.status === 200);
assert(res.status === 301);
done();
});
});
Expand Down
10 changes: 4 additions & 6 deletions test/urllib.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -411,11 +411,9 @@ describe('test/urllib.test.js', function () {
it('should handle server socket end("balabal") will error', function (done) {
urllib.request(host + '/socket.end.error', function (err, data) {
assert(err);
assert(err.name === 'ResponseError');
err.code && assert(err.code === 'HPE_INVALID_CHUNK_SIZE');
assert(/Parse Error.*GET http:\/\/127\.0\.0\.1:/.test(err.message) >= 0);
assert(err.bytesParsed === 2);
assert(!data);
assert(err.code === 'ECONNRESET');
assert(err.data);
assert(data);
done();
});
});
Expand Down Expand Up @@ -993,7 +991,7 @@ describe('test/urllib.test.js', function () {
done();
});

req.on('finish', () => {
req.on('response', () => {
req.emit('error', new Error('mock error'));
});
});
Expand Down