Skip to content

Commit

Permalink
Resolve TypeScript compiler errors
Browse files Browse the repository at this point in the history
  • Loading branch information
EvanDarwin committed Oct 7, 2020
1 parent c15bbe8 commit 83f3c4d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
8 changes: 6 additions & 2 deletions source/core/index.ts
Expand Up @@ -2729,7 +2729,9 @@ export default class Request extends Duplex implements RequestEvents<Request> {
The remote IP address.
*/
get ip(): string | undefined {
return this[kRequest]?.socket.remoteAddress;
const socket = this[kRequest]?.socket;
if(!socket) return undefined;
return socket.remoteAddress;
}

/**
Expand All @@ -2740,7 +2742,9 @@ export default class Request extends Duplex implements RequestEvents<Request> {
}

get socket(): Socket | undefined {
return this[kRequest]?.socket;
const socket = this[kRequest]?.socket;
if(!socket) return undefined;
return socket;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion test/retry.ts
Expand Up @@ -413,7 +413,7 @@ test('does not destroy the socket on HTTP error', withServer, async (t, server,
http: agent
}
}).on('request', request => {
sockets.push(request.socket);
sockets.push(request.socket!);
});

t.is(sockets.length, 2);
Expand Down
8 changes: 4 additions & 4 deletions test/timeout.ts
Expand Up @@ -229,7 +229,7 @@ test.serial('connect timeout', withServerAndFakeTimers, async (t, _server, got,
createConnection: options => {
const socket = new net.Socket(options as Record<string, unknown> as net.SocketConstructorOpts);
// @ts-expect-error We know that it is readonly, but we have to test it
socket.connecting = true;
socket.connecting! = true;
setImmediate(() => {
socket.emit('lookup', null, '127.0.0.1', 4, 'localhost');
});
Expand All @@ -256,7 +256,7 @@ test.serial('connect timeout (ip address)', withServerAndFakeTimers, async (t, _
createConnection: options => {
const socket = new net.Socket(options as Record<string, unknown> as net.SocketConstructorOpts);
// @ts-expect-error We know that it is readonly, but we have to test it
socket.connecting = true;
socket.connecting! = true;
return socket;
},
timeout: {connect: 1},
Expand All @@ -279,7 +279,7 @@ test.serial('secureConnect timeout', withHttpsServer({}, true), async (t, _serve
createConnection: options => {
const socket = new net.Socket(options as Record<string, unknown> as net.SocketConstructorOpts);
// @ts-expect-error We know that it is readonly, but we have to test it
socket.connecting = true;
socket.connecting! = true;
setImmediate(() => {
socket.emit('lookup', null, '127.0.0.1', 4, 'localhost');

Expand Down Expand Up @@ -444,7 +444,7 @@ test.serial('no unhandled timeout errors', withServer, async (t, _server, got) =
const result = http.request(...args);

result.once('socket', () => {
result.socket.destroy();
result.socket!.destroy();
});

return result;
Expand Down

0 comments on commit 83f3c4d

Please sign in to comment.