Skip to content
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

fix: large streaming uploads could fail with an invalid SHA-256 hash error #15

Merged
merged 1 commit into from
Jun 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,7 @@ Deno.test({
// First generate a 32MiB file in memory, 1 MiB at a time, as a stream
const dataStream = readableStreamFromIterable(async function* () {
for (let i = 0; i < 32; i++) {
await new Promise((r) => setTimeout(r, 10)); // Wait 10ms
yield new Uint8Array(1024 * 1024).fill(0b01010101); // Yield 1MB of data (alternating ones and zeroes)
yield new Uint8Array(1024 * 1024).fill(i % 256); // Yield 1MB of data
}
}());

Expand All @@ -110,7 +109,7 @@ Deno.test({
const response = await client.putObject(key, dataStream, { partSize: 5 * 1024 * 1024, metadata });
// The etag is generated by the server, based on the contents. Also, etags for multi-part uploads are
// different than for regular uploads, so the "-7" confirms it worked and used a multi-part upload.
assertEquals(response.etag, "4581589392ae60eafdb031f441858c7a-7");
assertEquals(response.etag, "ca6d977b6e7dc87ab5c5892e124c7277-7");
// Validate that the metadata was set:
const stat = await client.statObject(key);
assertEquals(stat.metadata, metadata);
Expand Down
4 changes: 1 addition & 3 deletions transform-chunk-sizes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,13 @@ export class TransformChunkSizes extends TransformStream<Uint8Array, Uint8Array>
const buffer = new Buffer();
buffer.grow(outChunkSize);

// This is a chunk-sized buffer that gets re-used each time we pass a new chunk out of this transform stream.
const outChunk = new Uint8Array(outChunkSize);

super({
start() {}, // required
async transform(chunk, controller) {
buffer.write(chunk);

while (buffer.length >= outChunkSize) {
const outChunk = new Uint8Array(outChunkSize);
const readFromBuffer = await buffer.read(outChunk);
if (readFromBuffer !== outChunkSize) {
throw new Error(
Expand Down