7
7
CancellationToken ,
8
8
generateByteStreamFromBuffer ,
9
9
isBrowser ,
10
+ readByteStream ,
10
11
} from '../internal/utils' ;
11
12
import { sdkVersion } from './version' ;
12
13
import {
@@ -124,7 +125,7 @@ async function createRequestInit(options: FetchOptions): Promise<RequestInit> {
124
125
} = options ;
125
126
126
127
const { contentType, body } = await ( async ( ) : Promise < {
127
- contentType : string ;
128
+ contentType : string | undefined ;
128
129
body : Readable | string ;
129
130
} > => {
130
131
if ( options . multipartData ) {
@@ -134,9 +135,10 @@ async function createRequestInit(options: FetchOptions): Promise<RequestInit> {
134
135
const formData = new FormData ( ) ;
135
136
for ( const item of options . multipartData ) {
136
137
if ( item . fileStream ) {
137
- const buffer = await readStream ( item . fileStream ) ;
138
+ const buffer = await readByteStream ( item . fileStream ) ;
139
+ const blob = isBrowser ( ) ? new Blob ( [ buffer ] ) : buffer ;
138
140
headers [ 'content-md5' ] = await calculateMD5Hash ( buffer ) ;
139
- formData . append ( item . partName , buffer , {
141
+ formData . append ( item . partName , blob , {
140
142
filename : item . fileName ?? 'file' ,
141
143
contentType : item . contentType ?? 'application/octet-stream' ,
142
144
} ) ;
@@ -150,7 +152,9 @@ async function createRequestInit(options: FetchOptions): Promise<RequestInit> {
150
152
}
151
153
152
154
return {
153
- contentType : `multipart/form-data; boundary=${ formData . getBoundary ( ) } ` ,
155
+ contentType : ! isBrowser ( )
156
+ ? `multipart/form-data; boundary=${ formData . getBoundary ( ) } `
157
+ : undefined ,
154
158
body : formData ,
155
159
} ;
156
160
}
@@ -183,8 +187,7 @@ async function createRequestInit(options: FetchOptions): Promise<RequestInit> {
183
187
return {
184
188
method,
185
189
headers : {
186
- ...options . networkSession ?. additionalHeaders ,
187
- 'Content-Type' : contentType ,
190
+ ...( contentType && { 'Content-Type' : contentType } ) ,
188
191
...headers ,
189
192
...( options . auth && {
190
193
Authorization : await options . auth . retrieveAuthorizationHeader (
@@ -193,6 +196,8 @@ async function createRequestInit(options: FetchOptions): Promise<RequestInit> {
193
196
} ) ,
194
197
'User-Agent' : userAgentHeader ,
195
198
'X-Box-UA' : xBoxUaHeader ,
199
+ // Additional headers will override the default headers
200
+ ...options . networkSession ?. additionalHeaders ,
196
201
} ,
197
202
body,
198
203
signal : options . cancellationToken as RequestInit [ 'signal' ] ,
@@ -361,17 +366,6 @@ async function calculateMD5Hash(data: string | Buffer): Promise<string> {
361
366
return createHash ( 'sha1' ) . update ( data ) . digest ( 'hex' ) ;
362
367
}
363
368
364
- async function readStream ( fileStream : Readable ) : Promise < Buffer > {
365
- return new Promise ( ( resolve , reject ) => {
366
- const chunks : any [ ] = [ ] ;
367
- fileStream . on ( 'data' , ( chunk : Buffer ) => chunks . push ( chunk ) ) ;
368
- fileStream . on ( 'end' , ( ) => {
369
- resolve ( Buffer . concat ( chunks ) ) ;
370
- } ) ;
371
- fileStream . on ( 'error' , ( err : Error ) => reject ( err ) ) ;
372
- } ) ;
373
- }
374
-
375
369
function constructBoxUAHeader ( ) {
376
370
const analyticsIdentifiers = {
377
371
agent : `box-javascript-generated-sdk/${ sdkVersion } ` ,
0 commit comments