@@ -9,7 +9,6 @@ import tls = require('tls');
99import http = require( 'http' ) ;
1010import http2 = require( 'http2' ) ;
1111import https = require( 'https' ) ;
12- import ProxyAgent = require( 'proxy-agent' ) ;
1312import * as h2Client from 'http2-wrapper' ;
1413import CacheableLookup from 'cacheable-lookup' ;
1514import { encode as encodeBase64 , decode as decodeBase64 } from 'base64-arraybuffer' ;
@@ -18,6 +17,18 @@ import { stripIndent, oneLine } from 'common-tags';
1817import { TypedError } from 'typed-error' ;
1918import { encodeBuffer , SUPPORTED_ENCODING } from 'http-encoding' ;
2019
20+ import {
21+ Headers ,
22+ OngoingRequest ,
23+ CompletedRequest ,
24+ OngoingResponse ,
25+ CompletedBody ,
26+ Explainable
27+ } from "../../types" ;
28+
29+ import { byteLength } from '../../util/util' ;
30+ import { MaybePromise , Replace } from '../../util/type-utils' ;
31+ import { readFile } from '../../util/fs' ;
2132import {
2233 waitForCompletedRequest ,
2334 setHeaders ,
@@ -29,8 +40,7 @@ import {
2940 h2HeadersToH1 ,
3041 isAbsoluteUrl ,
3142 cleanUpHeaders ,
32- isMockttpBody ,
33- matchesNoProxy
43+ isMockttpBody
3444} from '../../util/request-utils' ;
3545import { streamToBuffer , asBuffer } from '../../util/buffer-utils' ;
3646import { isLocalPortActive , isSocketLoop } from '../../util/socket-util' ;
@@ -45,18 +55,7 @@ import {
4555 serializeBuffer ,
4656 deserializeBuffer
4757} from "../../util/serialization" ;
48- import { MaybePromise , Replace } from '../../util/type-utils' ;
49- import { readFile } from '../../util/fs' ;
50-
51- import {
52- Headers ,
53- OngoingRequest ,
54- CompletedRequest ,
55- OngoingResponse ,
56- CompletedBody ,
57- Explainable
58- } from "../../types" ;
59- import { byteLength , isNode } from '../../util/util' ;
58+ import { getAgent , ProxyConfig } from '../../util/http-agents' ;
6059
6160// An error that indicates that the handler is aborting the request.
6261// This could be intentional, or an upstream server aborting the request.
@@ -416,36 +415,6 @@ export interface ForwardingOptions {
416415 updateHostHeader ?: true | false | string // Change automatically/ignore/change to custom value
417416}
418417
419- export interface ProxyConfig {
420- /**
421- * The URL for the proxy to pass through.
422- *
423- * This can be any URL supported by https://www.npmjs.com/package/proxy-agent.
424- * For example: http://..., socks5://..., pac+http://...
425- */
426- proxyUrl : string ;
427-
428- /**
429- * A list of no-proxy values, matching URLs which should not be proxied.
430- *
431- * This is a common proxy feature, but unfortunately isn't standardized. See
432- * https://about.gitlab.com/blog/2021/01/27/we-need-to-talk-no-proxy/ for some
433- * background. This implementation is intended to match Curl's behaviour, and
434- * any differences are a bug.
435- *
436- * The currently supported formats are:
437- * - example.com (matches domain and all subdomains)
438- * - example.com:443 (matches domain and all subdomains, but only on that port)
439- * - 10.0.0.1 (matches IP, but only when used directly - does not resolve domains)
440- *
441- * Some other formats (e.g. leading dots or *.) will work, but the leading
442- * characters are ignored. More formats may be added in future, e.g. CIDR ranges.
443- * To maximize compatibility with values used elsewhere, unrecognized formats
444- * will generally be ignored, but may match in unexpected ways.
445- */
446- noProxy ?: string [ ] ;
447- }
448-
449418export interface PassThroughLookupOptions {
450419 /**
451420 * The maximum time to cache a DNS response. Up to this limit,
@@ -883,50 +852,6 @@ function validateCustomHeaders(
883852const OMIT_SYMBOL = Symbol ( 'omit-value' ) ;
884853const SERIALIZED_OMIT = "__mockttp__transform__omit__" ;
885854
886- const KeepAliveAgents = isNode
887- ? { // These are only used (and only available) on the node server side
888- 'http:' : new http . Agent ( {
889- keepAlive : true
890- } ) ,
891- 'https:' : new https . Agent ( {
892- keepAlive : true
893- } )
894- } : { } ;
895-
896- function getAgent ( {
897- protocol, hostname, port, tryHttp2, keepAlive, proxyConfig
898- } : {
899- protocol : 'http:' | 'https:' | undefined ,
900- hostname : string ,
901- port : number ,
902- tryHttp2 : boolean ,
903- keepAlive : boolean
904- proxyConfig : ProxyConfig | undefined ,
905- } ) : { } | undefined {
906- if ( proxyConfig && proxyConfig . proxyUrl ) {
907- // If there's a (non-empty) proxy configured, use it. We require non-empty because empty strings
908- // will fall back to detecting from the environment, which is likely to behave unexpectedly.
909-
910- if ( ! matchesNoProxy ( hostname , port , proxyConfig . noProxy ) ) {
911- // We notably ignore HTTP/2 upstream in this case: it's complicated to mix that up with proxying
912- // so for now we ignore it entirely.
913- return new ProxyAgent ( proxyConfig . proxyUrl ) ;
914- }
915- }
916-
917- if ( tryHttp2 && protocol === 'https:' ) {
918- // H2 wrapper takes multiple agents, uses the appropriate one for the detected protocol.
919- // We notably never use H2 upstream for plaintext, it's rare and we can't use ALPN to detect it.
920- return { https : KeepAliveAgents [ 'https:' ] , http2 : undefined } ;
921- } else if ( keepAlive ) {
922- // HTTP/1.1 or HTTP/1 with explicit keep-alive
923- return KeepAliveAgents [ protocol || 'http:' ]
924- } else {
925- // HTTP/1 without KA - just send the request with no agent
926- return undefined ;
927- }
928- }
929-
930855export class PassThroughHandler extends Serializable implements RequestHandler {
931856 readonly type = 'passthrough' ;
932857
@@ -1265,7 +1190,7 @@ export class PassThroughHandler extends Serializable implements RequestHandler {
12651190 tryHttp2 : shouldTryH2Upstream ,
12661191 keepAlive : shouldKeepAlive ( clientReq ) ,
12671192 proxyConfig : this . proxyConfig
1268- } ) as http . Agent ;
1193+ } ) ;
12691194
12701195 if ( isH2Downstream && shouldTryH2Upstream ) {
12711196 // We drop all incoming pseudoheaders, and regenerate them (except legally modified ones)
0 commit comments