-
-
Notifications
You must be signed in to change notification settings - Fork 967
Make the TypeScript types strict and better #849
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,9 @@ import {ClientRequest, IncomingMessage} from 'http'; | |
import {Delays} from './types'; | ||
import unhandler from './unhandle'; | ||
|
||
// Help TypeScript understand how we are passing arguments through `setImmediate`. | ||
declare function setImmediate<T extends any[]>(callback: (arg0: number, ...args: T) => void, arg0: number, ...args: T): NodeJS.Immediate; | ||
sindresorhus marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
const reentry = Symbol('reentry'); | ||
const noop = (): void => {}; | ||
|
||
|
@@ -32,7 +35,7 @@ export default (request: ClientRequest, delays: Delays, options: TimedOutOptions | |
const cancelers: Array<typeof noop> = []; | ||
const {once, unhandleAll} = unhandler(); | ||
|
||
const addTimeout = (delay: number, callback: (...args: unknown[]) => void, ...args: unknown[]): (typeof noop) => { | ||
const addTimeout = <T extends any[]>(delay: number, callback: (delay: number, ...args: T) => void, ...args: T): (typeof noop) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does the callback actually take the delay as the first argument? it doesn't seem like it |
||
// Event loop order is timers, poll, immediates. | ||
// The timed event may emit during the current tick poll phase, so | ||
// defer calling the handler until the poll phase completes. | ||
|
@@ -91,7 +94,7 @@ export default (request: ClientRequest, delays: Delays, options: TimedOutOptions | |
|
||
if (delays.socket !== undefined) { | ||
const socketTimeoutHandler = (): void => { | ||
timeoutHandler(delays.socket, 'socket'); | ||
timeoutHandler(delays.socket!, 'socket'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't the if statement act as a type guard, thus removing the need for the non-null assertion? Likewise in a few other places. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Already fixed in master: 640c30b#diff-7ab9a52ac2fccf434c1a34ce48f2883bL95-R95 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, my mistake! 👍 |
||
}; | ||
|
||
request.setTimeout(delays.socket, socketTimeoutHandler); | ||
|
@@ -110,15 +113,15 @@ 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 (delays.lookup !== undefined && !socketPath && !net.isIP(hostname || host || '')) { | ||
sindresorhus marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const cancelTimeout = addTimeout(delays.lookup, timeoutHandler, 'lookup'); | ||
once(socket, 'lookup', cancelTimeout); | ||
} | ||
|
||
if (delays.connect !== undefined) { | ||
const timeConnect = (): (() => void) => addTimeout(delays.connect, timeoutHandler, 'connect'); | ||
const timeConnect = (): (() => void) => addTimeout(delays.connect!, timeoutHandler, 'connect'); | ||
|
||
if (socketPath || net.isIP(hostname || host)) { | ||
if (socketPath || net.isIP(hostname || host || '')) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see that this net.isIP check is used at least twice, can't we abstract it to one central variable? |
||
once(socket, 'connect', timeConnect()); | ||
} else { | ||
once(socket, 'lookup', (error: Error): void => { | ||
|
@@ -131,14 +134,14 @@ export default (request: ClientRequest, delays: Delays, options: TimedOutOptions | |
|
||
if (delays.secureConnect !== undefined && options.protocol === 'https:') { | ||
once(socket, 'connect', (): void => { | ||
const cancelTimeout = addTimeout(delays.secureConnect, timeoutHandler, 'secureConnect'); | ||
const cancelTimeout = addTimeout(delays.secureConnect!, timeoutHandler, 'secureConnect'); | ||
once(socket, 'secureConnect', cancelTimeout); | ||
}); | ||
} | ||
} | ||
|
||
if (delays.send !== undefined) { | ||
const timeRequest = (): (() => void) => addTimeout(delays.send, timeoutHandler, 'send'); | ||
const timeRequest = (): (() => void) => addTimeout(delays.send!, timeoutHandler, 'send'); | ||
/* istanbul ignore next: hard to test */ | ||
if (socket.connecting) { | ||
once(socket, 'connect', (): void => { | ||
|
Uh oh!
There was an error while loading. Please reload this page.