Skip to content

Commit

Permalink
fix(multiput-upload): improve upload performance (#3512)
Browse files Browse the repository at this point in the history
fix(multiput-upload): improve upload performance 1
Change digest from sha-256 to sha1

Co-authored-by: greg-in-a-box <103291617+greg-in-a-box@users.noreply.github.com>
  • Loading branch information
DroidSoul and greg-in-a-box committed Mar 14, 2024
1 parent 4849ac2 commit 2583e94
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
10 changes: 5 additions & 5 deletions src/api/uploads/MultiputPart.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class MultiputPart extends BaseMultiput {

offset: number;

sha256: string;
sha1: string;

partSize: number;

Expand Down Expand Up @@ -136,7 +136,7 @@ class MultiputPart extends BaseMultiput {
uploadedBytes: this.uploadedBytes,
numUploadRetriesPerformed: this.numUploadRetriesPerformed,
numDigestRetriesPerformed: this.numDigestRetriesPerformed,
sha256: this.sha256,
sha1: this.sha1,
timing: this.timing,
});

Expand All @@ -157,8 +157,8 @@ class MultiputPart extends BaseMultiput {
return;
}

if (!this.sha256) {
throw new Error('Part SHA-256 unavailable');
if (!this.sha1) {
throw new Error('Part SHA-1 unavailable');
}

if (!this.blob) {
Expand All @@ -174,7 +174,7 @@ class MultiputPart extends BaseMultiput {

const headers = {
'Content-Type': 'application/octet-stream',
Digest: `sha-256=${this.sha256}`,
Digest: `sha=${this.sha1}`,
'Content-Range': `bytes ${this.offset}-${this.rangeEnd}/${this.fileSize}`,
'X-Box-Client-Event-Info': JSON.stringify(clientEventInfo),
};
Expand Down
8 changes: 4 additions & 4 deletions src/api/uploads/MultiputUpload.js
Original file line number Diff line number Diff line change
Expand Up @@ -784,13 +784,13 @@ class MultiputUpload extends BaseMultiput {
buffer: ArrayBuffer,
readCompleteTimestamp: number,
} = await this.readFile(reader, blob);
const sha256ArrayBuffer = await digest('SHA-256', buffer);
const sha256 = btoa(
[].reduce.call(new Uint8Array(sha256ArrayBuffer), (data, byte) => data + String.fromCharCode(byte), ''),
const sha1ArrayBuffer = await digest('SHA-1', buffer);
const sha1 = btoa(
[].reduce.call(new Uint8Array(sha1ArrayBuffer), (data, byte) => data + String.fromCharCode(byte), ''),
);
this.sendPartToWorker(part, buffer);

part.sha256 = sha256;
part.sha1 = sha1;
part.state = PART_STATE_DIGEST_READY;
part.blob = blob;

Expand Down
8 changes: 4 additions & 4 deletions src/api/uploads/__tests__/MultiputPart.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,21 @@ describe('api/uploads/MultiputPart', () => {
expect(MultiputPartTest.xhr.uploadFile).not.toHaveBeenCalled();
});

test('should throw error if sha256 is not available', () => {
test('should throw error if sha1 is not available', () => {
MultiputPartTest.destroyed = false;
MultiputPartTest.isPaused = false;
MultiputPartTest.blob = {};

MultiputPartTest.xhr.uploadFile = jest.fn();

expect(MultiputPartTest.upload.bind(MultiputPartTest)).toThrowError(/Part SHA-256 unavailable/);
expect(MultiputPartTest.upload.bind(MultiputPartTest)).toThrowError(/Part SHA-1 unavailable/);
expect(MultiputPartTest.xhr.uploadFile).not.toHaveBeenCalled();
});

test('should throw error if blob is not available', () => {
MultiputPartTest.destroyed = false;
MultiputPartTest.isPaused = false;
MultiputPartTest.sha256 = '123';
MultiputPartTest.sha1 = '123';

MultiputPartTest.xhr.uploadFile = jest.fn();

Expand All @@ -66,7 +66,7 @@ describe('api/uploads/MultiputPart', () => {
test('should upload file properly', () => {
MultiputPartTest.destroyed = false;
MultiputPartTest.isPaused = false;
MultiputPartTest.sha256 = '123';
MultiputPartTest.sha1 = '123';
MultiputPartTest.blob = {};
MultiputPartTest.xhr.uploadFile = jest.fn();

Expand Down

0 comments on commit 2583e94

Please sign in to comment.