@@ -25,8 +25,8 @@ import * as sjcl from '@bitgo/sjcl';
2525import * as utxolib from '@bitgo/utxo-lib' ;
2626import { bip32 , ECPairInterface } from '@bitgo/utxo-lib' ;
2727import * as bitcoinMessage from 'bitcoinjs-message' ;
28- import { isBrowser , isWebWorker } from 'browser-or-node' ;
2928import { createHmac } from 'crypto' ;
29+ import { type Agent } from 'http' ;
3030import debugLib from 'debug' ;
3131import * as _ from 'lodash' ;
3232import * as secp256k1 from 'secp256k1' ;
@@ -96,14 +96,6 @@ const PendingApprovals = require('./v1/pendingapprovals');
9696const TravelRule = require ( './v1/travelRule' ) ;
9797const TransactionBuilder = require ( './v1/transactionBuilder' ) ;
9898
99- let enableProxyAgent = false ;
100- let proxyAgentModule ;
101- if ( ! isBrowser && ! isWebWorker ) {
102- debug ( 'enabling proxy-agent' ) ;
103- enableProxyAgent = true ;
104- proxyAgentModule = require ( 'proxy-agent' ) ;
105- }
106-
10799const patchedRequestMethods = [ 'get' , 'post' , 'put' , 'del' , 'patch' , 'options' ] as const ;
108100
109101export class BitGoAPI implements BitGoBase {
@@ -138,6 +130,7 @@ export class BitGoAPI implements BitGoBase {
138130 protected readonly _clientSecret ?: string ;
139131 protected _validate : boolean ;
140132 public readonly cookiesPropagationEnabled : boolean ;
133+ private _customProxyAgent ?: Agent ;
141134
142135 constructor ( params : BitGoAPIOptions = { } ) {
143136 this . cookiesPropagationEnabled = false ;
@@ -278,15 +271,12 @@ export class BitGoAPI implements BitGoBase {
278271 debug ( 'HMAC verification explicitly disabled by constructor option' ) ;
279272 this . _hmacVerification = params . hmacVerification ;
280273 }
281- if ( ! params . proxy && process . env . BITGO_USE_PROXY ) {
282- params . proxy = process . env . BITGO_USE_PROXY ;
283- }
284274
285- if ( ( process as any ) . browser && params . proxy ) {
286- throw new Error ( 'cannot use https proxy params while in browser' ) ;
275+ if ( ( process as any ) . browser && params . customProxyAgent ) {
276+ throw new Error ( 'should not use https proxy while in browser' ) ;
287277 }
288278
289- this . _proxy = params . proxy ;
279+ this . _customProxyAgent = params . customProxyAgent ;
290280
291281 // capture outer stack so we have useful debug information if fetch constants fails
292282 const e = new Error ( ) ;
@@ -342,14 +332,10 @@ export class BitGoAPI implements BitGoBase {
342332 */
343333 private requestPatch ( method : ( typeof patchedRequestMethods ) [ number ] , url : string ) {
344334 const req = this . getAgentRequest ( method , url ) ;
345- if ( this . _proxy && enableProxyAgent ) {
346- debug ( 'proxying request through %s' , this . _proxy ) ;
347- const proxyUrl : string = this . _proxy ;
348- const agent = new proxyAgentModule . ProxyAgent ( {
349- getProxyForUrl : ( ) => proxyUrl ,
350- } ) ;
351- if ( agent ) {
352- req . agent ( agent ) ;
335+ if ( this . _customProxyAgent ) {
336+ debug ( 'using custom proxy agent' ) ;
337+ if ( this . _customProxyAgent ) {
338+ req . agent ( this . _customProxyAgent ) ;
353339 }
354340 }
355341
@@ -585,14 +571,8 @@ export class BitGoAPI implements BitGoBase {
585571 // Proxy settings must still be respected however
586572 const resultPromise = this . getAgentRequest ( 'get' , this . url ( '/client/constants' ) ) ;
587573 resultPromise . set ( 'BitGo-SDK-Version' , this . _version ) ;
588- if ( this . _proxy && enableProxyAgent ) {
589- const proxyUrl : string = this . _proxy ;
590- const agent = new proxyAgentModule . ProxyAgent ( {
591- getProxyForUrl : ( ) => proxyUrl ,
592- } ) ;
593- if ( agent ) {
594- resultPromise . agent ( agent ) ;
595- }
574+ if ( this . _customProxyAgent ) {
575+ resultPromise . agent ( this . _customProxyAgent ) ;
596576 }
597577 const result = await resultPromise ;
598578 BitGoAPI . _constants [ env ] = result . body . constants ;
0 commit comments