@@ -4,45 +4,56 @@ import { FormDataRequest } from 'types/API';
4
4
5
5
export class NodeFilesHandler extends BaseFilesHandler {
6
6
private async convertReadableStream ( readableStream : ReadableStream ) : Promise < NodeJS . ReadableStream > {
7
- const { Readable } = await import ( 'stream' ) ;
8
- const reader = readableStream . getReader ( ) ;
7
+ try {
8
+ if ( typeof window === 'undefined' ) {
9
+ const { Readable } = await import ( 'stream' ) ;
10
+ const reader = readableStream . getReader ( ) ;
9
11
10
- return new Readable ( {
11
- async read ( ) {
12
- const { done, value } = await reader . read ( ) ;
13
- if ( done ) {
14
- this . push ( null ) ;
15
- } else {
16
- this . push ( value ) ;
17
- }
18
- } ,
19
- } ) ;
12
+ return new Readable ( {
13
+ async read ( ) {
14
+ const { done, value } = await reader . read ( ) ;
15
+ if ( done ) {
16
+ this . push ( null ) ;
17
+ } else {
18
+ this . push ( value ) ;
19
+ }
20
+ } ,
21
+ } ) ;
22
+ } else {
23
+ throw new Error ( 'Stream conversion is not supported in browser environment' ) ;
24
+ }
25
+ } catch ( error ) {
26
+ console . error ( 'Error in convertReadableStream:' , error ) ;
27
+ throw error ;
28
+ }
20
29
}
21
30
22
31
async prepareFormDataRequest ( file : FilePathOrFileObject ) : Promise < FormDataRequest > {
23
32
console . log ( 'Preparing form data request for Node.js' ) ;
24
33
try {
25
- const FormData = await import ( 'form-data' ) . then ( ( m ) => m . default || m ) ;
34
+ const FormData = await import ( 'form-data' ) . then ( m => m . default || m ) ;
26
35
console . log ( 'Successfully imported form-data module' ) ;
27
-
36
+
28
37
const formData = new FormData ( ) ;
29
38
console . log ( 'Created new FormData instance' ) ;
30
39
31
40
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 } ` ) ;
41
+ if ( typeof window === 'undefined' ) {
42
+ const fs = await import ( 'fs' ) . then ( m => m . default || m ) ;
43
+ if ( ! fs . existsSync ( file ) ) {
44
+ throw new Error ( `File not found: ${ file } ` ) ;
45
+ }
46
+ console . log ( `Appending file from path: ${ file } ` ) ;
47
+ formData . append ( 'file' , fs . createReadStream ( file ) , { filename : file . split ( '/' ) . pop ( ) } ) ;
48
+ } else {
49
+ throw new Error ( 'File system operations are not supported in browser environment' ) ;
35
50
}
36
- console . log ( `Appending file from path: ${ file } ` ) ;
37
- formData . append ( 'file' , fs . createReadStream ( file ) , { filename : file . split ( '/' ) . pop ( ) } ) ;
38
51
} else if ( file && typeof file === 'object' ) {
39
52
console . log ( 'Processing file object:' , file ) ;
40
53
if ( 'buffer' in file ) {
41
- // Handle Node.js file-like object
42
54
console . log ( 'Appending file from buffer' ) ;
43
55
formData . append ( 'file' , file . buffer , { filename : file . name , contentType : file . type } ) ;
44
56
} else if ( 'stream' in file && typeof file . stream === 'function' ) {
45
- // Handle File object
46
57
console . log ( 'Converting and appending file from stream' ) ;
47
58
const nodeStream = await this . convertReadableStream ( file . stream ( ) ) ;
48
59
formData . append ( 'file' , nodeStream , { filename : file . name , contentType : file . type } ) ;
0 commit comments