-
Notifications
You must be signed in to change notification settings - Fork 104
feat(fetch-http-handler): improve performance, replace FileReader with Blob.arrayBuffer() #1179
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
"@smithy/chunked-blob-reader": major | ||
"@smithy/fetch-http-handler": major | ||
"@smithy/chunked-blob-reader-native": patch | ||
--- | ||
|
||
replace FileReader with Blob.arrayBuffer() where possible |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,18 @@ | ||
/** | ||
* @internal | ||
* Reads the blob data into the onChunk consumer. | ||
*/ | ||
export function blobReader( | ||
export async function blobReader( | ||
blob: Blob, | ||
onChunk: (chunk: Uint8Array) => void, | ||
chunkSize: number = 1024 * 1024 | ||
): Promise<void> { | ||
return new Promise((resolve, reject) => { | ||
const fileReader = new FileReader(); | ||
const size = blob.size; | ||
let totalBytesRead = 0; | ||
|
||
fileReader.addEventListener("error", reject); | ||
fileReader.addEventListener("abort", reject); | ||
|
||
const size = blob.size; | ||
let totalBytesRead = 0; | ||
|
||
function read() { | ||
if (totalBytesRead >= size) { | ||
resolve(); | ||
return; | ||
} | ||
fileReader.readAsArrayBuffer(blob.slice(totalBytesRead, Math.min(size, totalBytesRead + chunkSize))); | ||
} | ||
|
||
fileReader.addEventListener("load", (event) => { | ||
const result = <ArrayBuffer>(event.target as any).result; | ||
onChunk(new Uint8Array(result)); | ||
totalBytesRead += result.byteLength; | ||
// read the next block | ||
read(); | ||
}); | ||
|
||
// kick off the read | ||
read(); | ||
}); | ||
while (totalBytesRead < size) { | ||
const slice: Blob = blob.slice(totalBytesRead, Math.min(size, totalBytesRead + chunkSize)); | ||
onChunk(new Uint8Array(await slice.arrayBuffer())); | ||
totalBytesRead += slice.size; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,29 @@ | ||
import { Blob as BlobPolyfill } from "buffer"; | ||
|
||
import { streamCollector } from "./stream-collector"; | ||
|
||
/** | ||
* Have to mock the FileReader behavior in IE, where | ||
* reader.result is null if reads an empty blob. | ||
*/ | ||
// jsdom inaccurate Blob https://github.com/jsdom/jsdom/issues/2555. | ||
global.Blob = BlobPolyfill as any; | ||
|
||
describe("streamCollector", () => { | ||
let originalFileReader = (global as any).FileReader; | ||
let originalBlob = (global as any).Blob; | ||
beforeAll(() => { | ||
originalFileReader = (global as any).FileReader; | ||
originalBlob = (global as any).Blob; | ||
}); | ||
afterAll(() => { | ||
(global as any).FileReader = originalFileReader; | ||
(global as any).Blob = originalBlob; | ||
const blobAvailable = typeof Blob === "function"; | ||
const readableStreamAvailable = typeof ReadableStream === "function"; | ||
|
||
(blobAvailable ? it : it.skip)("collects Blob into bytearray", async () => { | ||
const blobby = new Blob([new Uint8Array([1, 2]), new Uint8Array([3, 4])]); | ||
const collected = await streamCollector(blobby); | ||
expect(collected).toEqual(new Uint8Array([1, 2, 3, 4])); | ||
}); | ||
|
||
it("returns a Uint8Array when blob is empty and when FileReader data is null(in IE)", (done) => { | ||
(global as any).FileReader = function FileReader() { | ||
this.result = null; //In IE, FileReader.result is null after reading empty blob | ||
this.readAsDataURL = jest.fn().mockImplementation(() => { | ||
if (this.onloadend) { | ||
this.readyState = 2; | ||
this.onloadend(); | ||
} | ||
}); | ||
}; | ||
(global as any).Blob = function Blob() {}; | ||
const dataPromise = streamCollector(new Blob()); | ||
dataPromise.then((data: any) => { | ||
expect(data).toEqual(Uint8Array.from([])); | ||
done(); | ||
(readableStreamAvailable ? it : it.skip)("collects ReadableStream into bytearray", async () => { | ||
const stream = new ReadableStream({ | ||
start(controller) { | ||
controller.enqueue(new Uint8Array([1, 2])); | ||
controller.enqueue(new Uint8Array([3, 4])); | ||
controller.close(); | ||
}, | ||
}); | ||
const collected = await streamCollector(stream); | ||
expect(collected).toEqual(new Uint8Array([1, 2, 3, 4])); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.