1- import * as fetch from 'make-fetch-happen' ;
21import { HttpProxyAgent } from 'http-proxy-agent' ;
32import { HttpsProxyAgent } from 'https-proxy-agent' ;
43import * as http from 'http' ;
@@ -7,6 +6,7 @@ import { URL } from 'url';
76import { getProxyForUrl } from 'proxy-from-env' ;
87import { CustomHeaders } from './headers' ;
98import { HttpOptions } from './http-options' ;
9+ import { defaultRetry , getKyClient } from './http-client' ;
1010const details = require ( './details.json' ) ;
1111
1212export interface RequestOptions {
@@ -38,7 +38,9 @@ export interface PostRequestOptions extends RequestOptions {
3838 httpOptions ?: HttpOptions ;
3939}
4040
41- const httpAgentOptions : http . AgentOptions = {
41+ type AgentOptions = http . AgentOptions & https . AgentOptions ;
42+
43+ const httpAgentOptions : AgentOptions = {
4244 keepAlive : true ,
4345 keepAliveMsecs : 30 * 1000 ,
4446 timeout : 10 * 1000 ,
@@ -47,17 +49,24 @@ const httpAgentOptions: http.AgentOptions = {
4749const httpNoProxyAgent = new http . Agent ( httpAgentOptions ) ;
4850const httpsNoProxyAgent = new https . Agent ( httpAgentOptions ) ;
4951
50- export const getDefaultAgent = ( url : URL ) => {
52+ export const getDefaultAgent = ( url : URL , rejectUnauthorized ?: boolean ) => {
5153 const proxy = getProxyForUrl ( url . href ) ;
5254 const isHttps = url . protocol === 'https:' ;
55+ const agentOptions =
56+ rejectUnauthorized === undefined
57+ ? httpAgentOptions
58+ : { ...httpAgentOptions , rejectUnauthorized } ;
5359
5460 if ( ! proxy || proxy === '' ) {
61+ if ( isHttps && rejectUnauthorized !== undefined ) {
62+ return new https . Agent ( agentOptions ) ;
63+ }
5564 return isHttps ? httpsNoProxyAgent : httpNoProxyAgent ;
5665 }
5766
5867 return isHttps
59- ? new HttpsProxyAgent ( proxy , httpAgentOptions )
60- : new HttpProxyAgent ( proxy , httpAgentOptions ) ;
68+ ? new HttpsProxyAgent ( proxy , agentOptions )
69+ : new HttpProxyAgent ( proxy , agentOptions ) ;
6170} ;
6271
6372type HeaderOptions = {
@@ -119,7 +128,11 @@ export const buildHeaders = ({
119128 return head ;
120129} ;
121130
122- export const post = ( {
131+ const resolveAgent = ( httpOptions ?: HttpOptions ) =>
132+ httpOptions ?. agent ||
133+ ( ( targetUrl : URL ) => getDefaultAgent ( targetUrl , httpOptions ?. rejectUnauthorized ) ) ;
134+
135+ export const post = async ( {
123136 url,
124137 appName,
125138 timeout,
@@ -129,11 +142,10 @@ export const post = ({
129142 headers,
130143 json,
131144 httpOptions,
132- } : PostRequestOptions ) =>
133- fetch ( url , {
134- timeout : timeout || 10000 ,
135- method : 'POST' ,
136- agent : httpOptions ?. agent || getDefaultAgent ,
145+ } : PostRequestOptions ) => {
146+ const ky = await getKyClient ( ) ;
147+ const requestOptions = {
148+ timeout : timeout || 10_000 ,
137149 headers : buildHeaders ( {
138150 appName,
139151 instanceId,
@@ -143,11 +155,21 @@ export const post = ({
143155 contentType : 'application/json' ,
144156 custom : headers ,
145157 } ) ,
146- body : JSON . stringify ( json ) ,
147- strictSSL : httpOptions ?. rejectUnauthorized ,
158+ json,
159+ // ky's types are browser-centric; agent is supported by the underlying fetch in Node.
160+ agent : resolveAgent ( httpOptions ) ,
161+ retry : defaultRetry ,
162+ } as const ;
163+
164+ return ky . post ( url , requestOptions as any ) . catch ( ( err : any ) => {
165+ if ( err ?. response ) {
166+ return err . response ;
167+ }
168+ throw err ;
148169 } ) ;
170+ } ;
149171
150- export const get = ( {
172+ export const get = async ( {
151173 url,
152174 etag,
153175 appName,
@@ -158,11 +180,10 @@ export const get = ({
158180 headers,
159181 httpOptions,
160182 supportedSpecVersion,
161- } : GetRequestOptions ) =>
162- fetch ( url , {
163- method : 'GET' ,
183+ } : GetRequestOptions ) => {
184+ const ky = await getKyClient ( ) ;
185+ const requestOptions = {
164186 timeout : timeout || 10_000 ,
165- agent : httpOptions ?. agent || getDefaultAgent ,
166187 headers : buildHeaders ( {
167188 appName,
168189 instanceId,
@@ -173,9 +194,14 @@ export const get = ({
173194 specVersionSupported : supportedSpecVersion ,
174195 connectionId,
175196 } ) ,
176- retry : {
177- retries : 2 ,
178- maxTimeout : timeout || 10_000 ,
179- } ,
180- strictSSL : httpOptions ?. rejectUnauthorized ,
197+ agent : resolveAgent ( httpOptions ) ,
198+ retry : defaultRetry ,
199+ } as const ;
200+
201+ return ky . get ( url , requestOptions as any ) . catch ( ( err : any ) => {
202+ if ( err ?. response ) {
203+ return err . response ;
204+ }
205+ throw err ;
181206 } ) ;
207+ } ;
0 commit comments