Skip to content

Commit 88d747e

Browse files
box-sdk-buildbox-sdk-build
and
box-sdk-build
authored
fix: Fix upload in browser (box/box-codegen#524) (#248)
Co-authored-by: box-sdk-build <[email protected]>
1 parent 27ceb35 commit 88d747e

File tree

3 files changed

+13
-19
lines changed

3 files changed

+13
-19
lines changed

.codegen.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{ "engineHash": "89557a9", "specHash": "e95d6fa", "version": "1.1.0" }
1+
{ "engineHash": "8ad81db", "specHash": "e95d6fa", "version": "1.1.0" }

src/box/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
export { BoxCcgAuth, CcgConfig } from './ccgAuth.generated';
22
export { BoxDeveloperTokenAuth } from './developerTokenAuth.generated';
33
export { BoxJwtAuth, JwtConfig } from './jwtAuth.generated';
4-
export { BoxOAuth } from './oauth.generated';
4+
export { BoxOAuth, OAuthConfig } from './oauth.generated';

src/networking/fetch.ts

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
CancellationToken,
88
generateByteStreamFromBuffer,
99
isBrowser,
10+
readByteStream,
1011
} from '../internal/utils';
1112
import { sdkVersion } from './version';
1213
import {
@@ -124,7 +125,7 @@ async function createRequestInit(options: FetchOptions): Promise<RequestInit> {
124125
} = options;
125126

126127
const { contentType, body } = await (async (): Promise<{
127-
contentType: string;
128+
contentType: string | undefined;
128129
body: Readable | string;
129130
}> => {
130131
if (options.multipartData) {
@@ -134,9 +135,10 @@ async function createRequestInit(options: FetchOptions): Promise<RequestInit> {
134135
const formData = new FormData();
135136
for (const item of options.multipartData) {
136137
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;
138140
headers['content-md5'] = await calculateMD5Hash(buffer);
139-
formData.append(item.partName, buffer, {
141+
formData.append(item.partName, blob, {
140142
filename: item.fileName ?? 'file',
141143
contentType: item.contentType ?? 'application/octet-stream',
142144
});
@@ -150,7 +152,9 @@ async function createRequestInit(options: FetchOptions): Promise<RequestInit> {
150152
}
151153

152154
return {
153-
contentType: `multipart/form-data; boundary=${formData.getBoundary()}`,
155+
contentType: !isBrowser()
156+
? `multipart/form-data; boundary=${formData.getBoundary()}`
157+
: undefined,
154158
body: formData,
155159
};
156160
}
@@ -183,8 +187,7 @@ async function createRequestInit(options: FetchOptions): Promise<RequestInit> {
183187
return {
184188
method,
185189
headers: {
186-
...options.networkSession?.additionalHeaders,
187-
'Content-Type': contentType,
190+
...(contentType && { 'Content-Type': contentType }),
188191
...headers,
189192
...(options.auth && {
190193
Authorization: await options.auth.retrieveAuthorizationHeader(
@@ -193,6 +196,8 @@ async function createRequestInit(options: FetchOptions): Promise<RequestInit> {
193196
}),
194197
'User-Agent': userAgentHeader,
195198
'X-Box-UA': xBoxUaHeader,
199+
// Additional headers will override the default headers
200+
...options.networkSession?.additionalHeaders,
196201
},
197202
body,
198203
signal: options.cancellationToken as RequestInit['signal'],
@@ -361,17 +366,6 @@ async function calculateMD5Hash(data: string | Buffer): Promise<string> {
361366
return createHash('sha1').update(data).digest('hex');
362367
}
363368

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-
375369
function constructBoxUAHeader() {
376370
const analyticsIdentifiers = {
377371
agent: `box-javascript-generated-sdk/${sdkVersion}`,

0 commit comments

Comments
 (0)