@@ -50,9 +50,16 @@ function shouldDisableSSLVerification(): boolean {
5050function createHttpsAgent ( ) : https . Agent | undefined {
5151 if ( shouldDisableSSLVerification ( ) ) {
5252 logger . warn ( 'SSL certificate verification is disabled. This should only be used in development.' ) ;
53- return new https . Agent ( {
54- rejectUnauthorized : false
55- } ) ;
53+ // Create agent with options that bypass both certificate and hostname verification
54+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
55+ const agentOptions : any = {
56+ rejectUnauthorized : false ,
57+ checkServerIdentity : ( ) => {
58+ // Return undefined to indicate the check passed
59+ return undefined ;
60+ }
61+ } ;
62+ return new https . Agent ( agentOptions ) ;
5663 }
5764 return undefined ;
5865}
@@ -69,35 +76,74 @@ export async function initImport(fileName: string, fileSize: number): Promise<In
6976 const apiEndpoint = getApiEndpoint ( ) ;
7077 const url = `${ apiEndpoint } /v1/import/init` ;
7178
72- const agent = createHttpsAgent ( ) ;
73- const response = await fetch ( url , {
74- method : 'POST' ,
75- headers : {
76- 'Content-Type' : 'application/json'
77- } ,
78- body : JSON . stringify ( {
79- fileName,
80- fileSize
81- } ) ,
82- agent
83- } ) ;
79+ // Temporarily disable SSL verification at the process level if configured
80+ const originalEnvValue = process . env . NODE_TLS_REJECT_UNAUTHORIZED ;
81+ if ( shouldDisableSSLVerification ( ) ) {
82+ process . env . NODE_TLS_REJECT_UNAUTHORIZED = '0' ;
83+ logger . debug ( 'Set NODE_TLS_REJECT_UNAUTHORIZED=0' ) ;
84+ }
8485
85- if ( ! response . ok ) {
86- const responseBody = await response . text ( ) ;
87- logger . error ( `Init import failed - Status: ${ response . status } , URL: ${ url } , Body: ${ responseBody } ` ) ;
86+ try {
87+ const agent = createHttpsAgent ( ) ;
88+ logger . debug ( `SSL verification disabled: ${ shouldDisableSSLVerification ( ) } ` ) ;
89+ logger . debug ( `Agent created: ${ ! ! agent } ` ) ;
90+ if ( agent ) {
91+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
92+ logger . debug ( `Agent rejectUnauthorized: ${ ( agent as any ) . options ?. rejectUnauthorized } ` ) ;
93+ }
8894
89- const error : ApiError = {
90- message : responseBody ,
91- statusCode : response . status
95+ interface FetchOptions {
96+ method : string ;
97+ headers : Record < string , string > ;
98+ body : string ;
99+ agent ?: https . Agent ;
100+ }
101+
102+ const options : FetchOptions = {
103+ method : 'POST' ,
104+ headers : {
105+ 'Content-Type' : 'application/json'
106+ } ,
107+ body : JSON . stringify ( {
108+ fileName,
109+ fileSize
110+ } )
92111 } ;
93- throw error ;
94- }
95112
96- return await response . json ( ) ;
113+ if ( agent ) {
114+ options . agent = agent ;
115+ logger . debug ( 'Agent attached to request' ) ;
116+ logger . debug ( `Options agent set: ${ ! ! options . agent } ` ) ;
117+ }
118+
119+ const response = await fetch ( url , options ) ;
120+
121+ if ( ! response . ok ) {
122+ const responseBody = await response . text ( ) ;
123+ logger . error ( `Init import failed - Status: ${ response . status } , URL: ${ url } , Body: ${ responseBody } ` ) ;
124+
125+ const error : ApiError = {
126+ message : responseBody ,
127+ statusCode : response . status
128+ } ;
129+ throw error ;
130+ }
131+
132+ return await response . json ( ) ;
133+ } finally {
134+ // Restore original SSL verification setting
135+ if ( shouldDisableSSLVerification ( ) ) {
136+ if ( originalEnvValue === undefined ) {
137+ delete process . env . NODE_TLS_REJECT_UNAUTHORIZED ;
138+ } else {
139+ process . env . NODE_TLS_REJECT_UNAUTHORIZED = originalEnvValue ;
140+ }
141+ }
142+ }
97143}
98144
99145/**
100- * Uploads a file to the presigned S3 URL using XMLHttpRequest for progress tracking
146+ * Uploads a file to the presigned S3 URL using node-fetch
101147 *
102148 * @param uploadUrl - Presigned S3 URL for uploading
103149 * @param fileBuffer - File contents as a Buffer
@@ -110,64 +156,34 @@ export async function uploadFile(
110156 fileBuffer : Buffer ,
111157 onProgress ?: ( progress : number ) => void
112158) : Promise < void > {
113- return new Promise ( ( resolve , reject ) => {
114- const xhr = new XMLHttpRequest ( ) ;
115-
116- // Track upload progress
117- if ( onProgress ) {
118- xhr . upload . addEventListener ( 'progress' , ( event ) => {
119- if ( event . lengthComputable ) {
120- const percentComplete = Math . round ( ( event . loaded / event . total ) * 100 ) ;
121- onProgress ( percentComplete ) ;
122- }
123- } ) ;
124- }
159+ // Note: Progress tracking is limited in Node.js without additional libraries
160+ // For now, we'll report 50% at start and 100% at completion
161+ if ( onProgress ) {
162+ onProgress ( 50 ) ;
163+ }
125164
126- // Handle completion
127- xhr . addEventListener ( 'load' , ( ) => {
128- if ( xhr . status >= 200 && xhr . status < 300 ) {
129- resolve ( ) ;
130- } else {
131- logger . error ( `Upload failed - Status: ${ xhr . status } , Response: ${ xhr . responseText } , URL: ${ uploadUrl } ` ) ;
132- const error : ApiError = {
133- message : xhr . responseText || 'Upload failed' ,
134- statusCode : xhr . status
135- } ;
136- reject ( error ) ;
137- }
138- } ) ;
165+ const response = await fetch ( uploadUrl , {
166+ method : 'PUT' ,
167+ headers : {
168+ 'Content-Type' : 'application/octet-stream' ,
169+ 'Content-Length' : fileBuffer . length . toString ( )
170+ } ,
171+ body : fileBuffer
172+ } ) ;
139173
140- // Handle errors
141- xhr . addEventListener ( 'error' , ( ) => {
142- logger . error ( `Network error during upload to : ${ uploadUrl } ` ) ;
143- const error : ApiError = {
144- message : 'Network error during upload ',
145- statusCode : 0
146- } ;
147- reject ( error ) ;
148- } ) ;
174+ if ( ! response . ok ) {
175+ const responseText = await response . text ( ) ;
176+ logger . error ( `Upload failed - Status: ${ response . status } , Response: ${ responseText } , URL : ${ uploadUrl } ` ) ;
177+ const error : ApiError = {
178+ message : responseText || 'Upload failed ',
179+ statusCode : response . status
180+ } ;
181+ throw error ;
182+ }
149183
150- xhr . addEventListener ( 'abort' , ( ) => {
151- const error : ApiError = {
152- message : 'Upload aborted' ,
153- statusCode : 0
154- } ;
155- reject ( error ) ;
156- } ) ;
157-
158- // Start upload
159- xhr . open ( 'PUT' , uploadUrl ) ;
160- xhr . setRequestHeader ( 'Content-Type' , 'application/octet-stream' ) ;
161-
162- // Convert Buffer to Uint8Array then Blob for XMLHttpRequest
163- const uint8Array = new Uint8Array (
164- fileBuffer . buffer as ArrayBuffer ,
165- fileBuffer . byteOffset ,
166- fileBuffer . byteLength
167- ) ;
168- const blob = new Blob ( [ uint8Array ] , { type : 'application/octet-stream' } ) ;
169- xhr . send ( blob ) ;
170- } ) ;
184+ if ( onProgress ) {
185+ onProgress ( 100 ) ;
186+ }
171187}
172188
173189/**
0 commit comments