@@ -20,24 +20,38 @@ export class NodeFilesHandler extends BaseFilesHandler {
20
20
}
21
21
22
22
async prepareFormDataRequest ( file : FilePathOrFileObject ) : Promise < FormDataRequest > {
23
- const { default : FormDataNode } = await import ( 'form-data' ) ;
24
- const formData = new FormDataNode ( ) ;
23
+ console . log ( 'Preparing form data request for Node.js' ) ;
24
+ try {
25
+ const FormData = await import ( 'form-data' ) . then ( m => m . default || m ) ;
26
+ console . log ( 'Successfully imported form-data module' ) ;
27
+
28
+ const formData = new FormData ( ) ;
29
+ console . log ( 'Created new FormData instance' ) ;
25
30
26
- if ( typeof file === 'string' ) {
27
- const fs = ( await import ( 'fs' ) ) . default ;
28
- if ( ! fs . existsSync ( file ) ) {
29
- throw new Error ( `File not found: ${ file } ` ) ;
31
+ if ( typeof file === 'string' ) {
32
+ const fs = await import ( 'fs' ) . then ( m => m . default || m ) ;
33
+ if ( ! fs . existsSync ( file ) ) {
34
+ throw new Error ( `File not found: ${ file } ` ) ;
35
+ }
36
+ console . log ( `Appending file from path: ${ file } ` ) ;
37
+ formData . append ( 'file' , fs . createReadStream ( file ) , { filename : file . split ( '/' ) . pop ( ) } ) ;
38
+ } else if ( file instanceof File ) {
39
+ console . log ( 'Converting ReadableStream to Node stream' ) ;
40
+ const nodeStream = await this . convertReadableStream ( file . stream ( ) ) ;
41
+ console . log ( 'Appending file from File instance' ) ;
42
+ formData . append ( 'file' , nodeStream , file . name ) ;
43
+ } else {
44
+ throw new Error ( `Unsupported file type for Node.js file upload flow: ${ file } ` ) ;
30
45
}
31
- formData . append ( 'file' , fs . createReadStream ( file ) , { filename : file . split ( '/' ) . pop ( ) } ) ;
32
- } else if ( file instanceof File ) {
33
- const nodeStream = await this . convertReadableStream ( file . stream ( ) ) ;
34
- formData . append ( 'file' , nodeStream , file . name ) ;
35
- } else {
36
- throw new Error ( `Unsupported file type for Node.js file upload flow: ${ file } ` ) ;
37
- }
38
46
39
- const formDataHeaders = { 'Content-Type' : `multipart/form-data; boundary=${ formData . getBoundary ( ) } ` } ;
47
+ const formDataHeaders = { 'Content-Type' : `multipart/form-data; boundary=${ formData . getBoundary ( ) } ` } ;
48
+ console . log ( 'FormData preparation completed successfully' ) ;
40
49
41
- return { formData, headers : formDataHeaders } ;
50
+ return { formData, headers : formDataHeaders } ;
51
+ } catch ( error ) {
52
+ console . error ( 'Error in prepareFormDataRequest:' , error ) ;
53
+ console . error ( 'Error details:' , error instanceof Error ? error . message : String ( error ) ) ;
54
+ throw error ;
55
+ }
42
56
}
43
57
}
0 commit comments