66import * as http from 'node:http' ;
77import * as https from 'node:https' ;
88import * as net from 'node:net' ;
9+ import * as stream from 'node:stream' ;
910import ipaddr from 'ipaddr.js' ;
1011import CacheableLookup from 'cacheable-lookup' ;
1112import fetch from 'node-fetch' ;
@@ -26,12 +27,6 @@ export type HttpRequestSendOptions = {
2627 validators ?: ( ( res : Response ) => void ) [ ] ;
2728} ;
2829
29- declare module 'node:http' {
30- interface Agent {
31- createConnection ( options : net . NetConnectOpts , callback ?: ( err : Error | null , stream : net . Socket ) => void ) : net . Socket ;
32- }
33- }
34-
3530class HttpRequestServiceAgent extends http . Agent {
3631 constructor (
3732 private config : Config ,
@@ -41,11 +36,11 @@ class HttpRequestServiceAgent extends http.Agent {
4136 }
4237
4338 @bindThis
44- public createConnection ( options : net . NetConnectOpts , callback ?: ( err : Error | null , stream : net . Socket ) => void ) : net . Socket {
39+ public createConnection ( options : http . ClientRequestArgs , callback ?: ( err : Error | null , stream : stream . Duplex ) => void ) : stream . Duplex {
4540 const socket = super . createConnection ( options , callback )
4641 . on ( 'connect' , ( ) => {
47- const address = socket . remoteAddress ;
48- if ( process . env . NODE_ENV === 'production' ) {
42+ if ( socket instanceof net . Socket && process . env . NODE_ENV === 'production' ) {
43+ const address = socket . remoteAddress ;
4944 if ( address && ipaddr . isValid ( address ) ) {
5045 if ( this . isPrivateIp ( address ) ) {
5146 socket . destroy ( new Error ( `Blocked address: ${ address } ` ) ) ;
@@ -80,11 +75,11 @@ class HttpsRequestServiceAgent extends https.Agent {
8075 }
8176
8277 @bindThis
83- public createConnection ( options : net . NetConnectOpts , callback ?: ( err : Error | null , stream : net . Socket ) => void ) : net . Socket {
78+ public createConnection ( options : http . ClientRequestArgs , callback ?: ( err : Error | null , stream : stream . Duplex ) => void ) : stream . Duplex {
8479 const socket = super . createConnection ( options , callback )
8580 . on ( 'connect' , ( ) => {
86- const address = socket . remoteAddress ;
87- if ( process . env . NODE_ENV === 'production' ) {
81+ if ( socket instanceof net . Socket && process . env . NODE_ENV === 'production' ) {
82+ const address = socket . remoteAddress ;
8883 if ( address && ipaddr . isValid ( address ) ) {
8984 if ( this . isPrivateIp ( address ) ) {
9085 socket . destroy ( new Error ( `Blocked address: ${ address } ` ) ) ;
0 commit comments