Skip to content

Commit

Permalink
Don't throw on early lookups
Browse files Browse the repository at this point in the history
Fixes #857
  • Loading branch information
szmarczak committed Sep 9, 2019
1 parent 6f188c9 commit 4faf5c7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
14 changes: 7 additions & 7 deletions source/utils/timed-out.ts
Expand Up @@ -85,11 +85,11 @@ export default (request: ClientRequest, delays: Delays, options: TimedOutOptions
once(response, 'end', cancelTimeouts);
});

if (delays.request !== undefined) {
if (typeof delays.request !== 'undefined') {
addTimeout(delays.request, timeoutHandler, 'request');
}

if (delays.socket !== undefined) {
if (typeof delays.socket !== 'undefined') {
const socketTimeoutHandler = (): void => {
timeoutHandler(delays.socket, 'socket');
};
Expand All @@ -110,12 +110,12 @@ export default (request: ClientRequest, delays: Delays, options: TimedOutOptions

/* istanbul ignore next: hard to test */
if (socket.connecting) {
if (delays.lookup !== undefined && !socketPath && !net.isIP(hostname || host)) {
if (typeof delays.lookup !== 'undefined' && !socketPath && !net.isIP(hostname || host) && typeof (socket.address() as net.AddressInfo).address === 'undefined') {
const cancelTimeout = addTimeout(delays.lookup, timeoutHandler, 'lookup');
once(socket, 'lookup', cancelTimeout);
}

if (delays.connect !== undefined) {
if (typeof delays.connect !== 'undefined') {
const timeConnect = (): (() => void) => addTimeout(delays.connect, timeoutHandler, 'connect');

if (socketPath || net.isIP(hostname || host)) {
Expand All @@ -129,15 +129,15 @@ export default (request: ClientRequest, delays: Delays, options: TimedOutOptions
}
}

if (delays.secureConnect !== undefined && options.protocol === 'https:') {
if (typeof delays.secureConnect !== 'undefined' && options.protocol === 'https:') {
once(socket, 'connect', (): void => {
const cancelTimeout = addTimeout(delays.secureConnect, timeoutHandler, 'secureConnect');
once(socket, 'secureConnect', cancelTimeout);
});
}
}

if (delays.send !== undefined) {
if (typeof delays.send !== 'undefined') {
const timeRequest = (): (() => void) => addTimeout(delays.send, timeoutHandler, 'send');
/* istanbul ignore next: hard to test */
if (socket.connecting) {
Expand All @@ -150,7 +150,7 @@ export default (request: ClientRequest, delays: Delays, options: TimedOutOptions
}
});

if (delays.response !== undefined) {
if (typeof delays.response !== 'undefined') {
once(request, 'upload-complete', (): void => {
const cancelTimeout = addTimeout(delays.response!, timeoutHandler, 'response');
once(request, 'response', cancelTimeout);
Expand Down
18 changes: 18 additions & 0 deletions test/timeout.ts
Expand Up @@ -466,3 +466,21 @@ test('double calling timedOut has no effect', t => {

t.is(emitter.listenerCount('socket'), 1);
});

test('doesn\'t throw on early lookup', withServer, async (t, server, got) => {
server.get('/', defaultHandler);

await t.notThrowsAsync(got('', {
timeout: {
lookup: 100
},
retry: 0,
lookup: (_hostname, options, callback) => {
if (typeof options === 'function') {
callback = options;
}

callback(null, '127.0.0.1', 4);
}
}));
});

0 comments on commit 4faf5c7

Please sign in to comment.