diff --git a/source/core/index.ts b/source/core/index.ts index eb845facf..9373851cc 100644 --- a/source/core/index.ts +++ b/source/core/index.ts @@ -2729,7 +2729,9 @@ export default class Request extends Duplex implements RequestEvents { 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; } /** @@ -2740,7 +2742,9 @@ export default class Request extends Duplex implements RequestEvents { } get socket(): Socket | undefined { - return this[kRequest]?.socket; + const socket = this[kRequest]?.socket; + if(!socket) return undefined; + return socket; } /** diff --git a/test/retry.ts b/test/retry.ts index 580a92705..f312f2d8d 100644 --- a/test/retry.ts +++ b/test/retry.ts @@ -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); diff --git a/test/timeout.ts b/test/timeout.ts index d06041a00..a364e37d3 100644 --- a/test/timeout.ts +++ b/test/timeout.ts @@ -229,7 +229,7 @@ test.serial('connect timeout', withServerAndFakeTimers, async (t, _server, got, createConnection: options => { const socket = new net.Socket(options as Record 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'); }); @@ -256,7 +256,7 @@ test.serial('connect timeout (ip address)', withServerAndFakeTimers, async (t, _ createConnection: options => { const socket = new net.Socket(options as Record 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}, @@ -279,7 +279,7 @@ test.serial('secureConnect timeout', withHttpsServer({}, true), async (t, _serve createConnection: options => { const socket = new net.Socket(options as Record 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'); @@ -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;