1
- import { UnifiedFormData } from " types" ;
2
- import { FilePathOrFileObject } from " types/rag" ;
3
- import { BaseFilesHandler } from " ./BaseFilesHandler" ;
1
+ import { UnifiedFormData } from ' types' ;
2
+ import { FilePathOrFileObject } from ' types/rag' ;
3
+ import { BaseFilesHandler } from ' ./BaseFilesHandler' ;
4
4
5
- export class NodeFilesHandler extends BaseFilesHandler {
5
+ export class NodeFilesHandler extends BaseFilesHandler {
6
+ async convertReadableStream ( whatwgStream : ReadableStream ) : Promise < NodeJS . ReadableStream > {
7
+ const { Readable } = await import ( 'stream' ) ;
8
+ const reader = whatwgStream . getReader ( ) ;
6
9
7
- async convertReadableStream ( whatwgStream : ReadableStream ) : Promise < NodeJS . ReadableStream > {
8
- const { Readable } = await import ( 'stream' ) ;
9
- const reader = whatwgStream . getReader ( ) ;
10
-
11
- return new Readable ( {
12
- async read ( ) {
13
- const { done, value } = await reader . read ( ) ;
14
- if ( done ) {
15
- this . push ( null ) ;
16
- } else {
17
- this . push ( value ) ;
18
- }
19
- } ,
20
- } ) ;
21
- }
22
-
23
- async getBoundary ( formData : UnifiedFormData ) : Promise < string | undefined > {
24
- const { default : FormDataNode } = await import ( 'form-data' ) ;
25
- if ( formData instanceof FormDataNode ) {
26
- return formData . getBoundary ( ) ;
27
- }
28
- else {
29
- throw new Error ( 'getBoundary invoked with native browser FormData instance instead of NodeJS form-data' ) ;
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 ) ;
30
17
}
18
+ } ,
19
+ } ) ;
20
+ }
21
+
22
+ async getBoundary ( formData : UnifiedFormData ) : Promise < string | undefined > {
23
+ const { default : FormDataNode } = await import ( 'form-data' ) ;
24
+ if ( formData instanceof FormDataNode ) {
25
+ return formData . getBoundary ( ) ;
26
+ } else {
27
+ throw new Error (
28
+ 'getBoundary invoked with native browser FormData instance instead of NodeJS form-data' ,
29
+ ) ;
31
30
}
32
-
31
+ }
33
32
34
- async createFormData ( file : FilePathOrFileObject ) : Promise < UnifiedFormData > {
35
- const { default : FormDataNode } = await import ( 'form-data' ) ;
36
- const formData = new FormDataNode ( ) ;
37
-
38
- if ( typeof file === 'string' ) {
39
- const fs = ( await import ( 'fs' ) ) . default ;
40
- formData . append ( 'file' , fs . createReadStream ( file ) , { filename : file . split ( '/' ) . pop ( ) } ) ;
41
- } else if ( Buffer . isBuffer ( file ) ) {
42
- formData . append ( 'file' , file , { filename : 'TODO - add filename to buffer flow' } ) ;
43
- } else if ( file instanceof File ) {
44
- const nodeStream = await this . convertReadableStream ( file . stream ( ) ) ;
45
- formData . append ( 'file' , nodeStream , file . name ) ;
46
- } else {
47
- throw new Error ( `Unsupported file type for Node.js file upload flow: ${ file } ` ) ;
48
- }
49
-
50
- return formData ;
33
+ async createFormData ( file : FilePathOrFileObject ) : Promise < UnifiedFormData > {
34
+ const { default : FormDataNode } = await import ( 'form-data' ) ;
35
+ const formData = new FormDataNode ( ) ;
36
+
37
+ if ( typeof file === 'string' ) {
38
+ const fs = ( await import ( 'fs' ) ) . default ;
39
+ formData . append ( 'file' , fs . createReadStream ( file ) , { filename : file . split ( '/' ) . pop ( ) } ) ;
40
+ } else if ( Buffer . isBuffer ( file ) ) {
41
+ formData . append ( 'file' , file , { filename : 'TODO - add filename to buffer flow' } ) ;
42
+ } else if ( file instanceof File ) {
43
+ const nodeStream = await this . convertReadableStream ( file . stream ( ) ) ;
44
+ formData . append ( 'file' , nodeStream , file . name ) ;
45
+ } else {
46
+ throw new Error ( `Unsupported file type for Node.js file upload flow: ${ file } ` ) ;
51
47
}
52
48
53
- getMultipartFormDataHeaders ( formData : UnifiedFormData ) : Record < string , string > | null {
54
- if ( formData instanceof FormData ) {
55
- return null ;
56
- }
49
+ return formData ;
50
+ }
57
51
58
- const boundary = formData . getBoundary ( ) ;
59
- return {
60
- 'Content-Type' : `multipart/form-data; boundary=${ boundary } ` ,
61
- } ;
62
- }
63
-
52
+ getMultipartFormDataHeaders ( formData : UnifiedFormData ) : Record < string , string > | null {
53
+ if ( formData instanceof FormData ) {
54
+ return null ;
64
55
}
65
-
56
+
57
+ const boundary = formData . getBoundary ( ) ;
58
+ return {
59
+ 'Content-Type' : `multipart/form-data; boundary=${ boundary } ` ,
60
+ } ;
61
+ }
62
+ }
0 commit comments