22 * SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Juno contributors
33 * SPDX-License-Identifier: Apache-2.0
44 */
5- import https from "https"
65import { buildUrl } from "./urlHelpers"
76import * as logger from "./logger"
87import { K8sApiError } from "./apiErrorHandler"
8+ import https from "https"
99
1010// Define the shape of the options parameter
1111interface RequestOptions {
@@ -16,8 +16,8 @@ interface RequestOptions {
1616 mode ?: RequestMode
1717 cache ?: RequestCache
1818 credentials ?: RequestCredentials
19- ignoreSsl ?: boolean // New option to ignore SSL certificate validation
20- debug ?: boolean // Optional debug flag
19+ ignoreSsl ?: boolean
20+ debug ?: boolean
2121 [ key : string ] : any
2222}
2323
@@ -27,11 +27,31 @@ const checkStatus = (response: Response): Response => {
2727 return response
2828 } else {
2929 const error = new Error ( response . statusText || `${ response . status } ` )
30- ; ( error as K8sApiError ) . response = response // Type assertion to attach the response to the error
30+ ; ( error as K8sApiError ) . response = response
3131 throw error
3232 }
3333}
3434
35+ // Helper function to create HTTPS agent only in Node.js
36+ function createHttpsAgent ( ignoreSsl : boolean , url : string ) : https . Agent | undefined {
37+ if ( ! ignoreSsl || ! url . startsWith ( "https:" ) || typeof window !== "undefined" ) {
38+ return undefined
39+ }
40+
41+ try {
42+ if ( ! https ?. Agent ) {
43+ return undefined
44+ }
45+
46+ return new https . Agent ( {
47+ rejectUnauthorized : false ,
48+ } )
49+ } catch ( _error ) {
50+ // https module not available (browser environment or bundle)
51+ return undefined
52+ }
53+ }
54+
3555/**
3656 * Creates a request.
3757 *
@@ -41,57 +61,50 @@ const checkStatus = (response: Response): Response => {
4161 * @return {Promise<Response> } The response promise.
4262 */
4363function request ( method : string , url : string , options : RequestOptions = { } ) : Promise < Response > {
44- // add params to url
64+ // Add params to url
4565 if ( options . params ) url = buildUrl ( url , options . params )
4666
4767 // Handle SSL ignore option
48- const { ignoreSsl, ...restOptions } = options
68+ const { ignoreSsl, debug , ...restOptions } = options
4969
50- // Create HTTPS agent if SSL should be ignored for HTTPS URLs
51- let agent : https . Agent | undefined
70+ // Create HTTPS agent if needed (Node.js only)
71+ const agent = createHttpsAgent ( ignoreSsl || false , url )
5272
53- if ( ignoreSsl && url . startsWith ( "https:" ) && typeof window === "undefined" ) {
54- agent = new https . Agent ( {
55- rejectUnauthorized : false ,
56- } )
57-
58- // Log warning when SSL verification is disabled
59- if ( process . env . NODE_ENV !== "test" && options . debug === true ) {
60- // Avoid spam in tests
61- logger . debug ( `⚠️ SSL verification disabled for request to: ${ url } ` )
62- }
73+ // Log warning when SSL verification is disabled
74+ if ( agent && debug === true && process . env . NODE_ENV !== "test" ) {
75+ logger . debug ( `⚠️ SSL verification disabled for request to: ${ url } ` )
6376 }
6477
65- // add allowed options to fetch (excluding ignoreSsl as it's handled separately)
78+ // Add allowed options to fetch (excluding ignoreSsl as it's handled separately)
6679 const requestFields = [ "signal" , "headers" , "body" , "mode" , "cache" , "credentials" ] as const
67- const fetchOptions : RequestInit & { agent ?: https . Agent } = requestFields . reduce (
80+ const fetchOptions : RequestInit & { agent ?: typeof agent } = requestFields . reduce (
6881 ( map , key ) => {
6982 if ( restOptions [ key ] ) {
7083 return { ...map , [ key ] : restOptions [ key ] }
7184 }
7285 return map
7386 } ,
74- { credentials : "same-origin" , method } as RequestInit & { agent ?: https . Agent }
87+ { credentials : "same-origin" , method } as RequestInit & { agent ?: typeof agent }
7588 )
7689
77- // Add agent if SSL should be ignored (Node.js environment only)
90+ // Add agent if available
7891 if ( agent ) {
7992 fetchOptions . agent = agent
8093 }
8194
82- // stringify body if it's an object
95+ // Stringify body if it's an object
8396 if ( fetchOptions . body && typeof fetchOptions . body !== "string" ) {
8497 fetchOptions . body = JSON . stringify ( fetchOptions . body )
8598 }
8699
87- if ( options . debug === true ) {
100+ if ( debug === true ) {
88101 logger . debug ( "fetch >" , url , {
89102 ...fetchOptions ,
90103 agent : agent ? "HTTPS Agent (SSL ignored)" : fetchOptions . agent ,
91104 } )
92105 }
93106
94- // make the call
107+ // Make the call
95108 return fetch ( url , fetchOptions ) . then ( checkStatus )
96109}
97110
0 commit comments