Skip to content

Commit

Permalink
feat(upload-abort-controller): add optional abortController param for…
Browse files Browse the repository at this point in the history
… Upload (#3873)
  • Loading branch information
addy authored Sep 26, 2022
1 parent 351d3a0 commit 90c6b11
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
19 changes: 19 additions & 0 deletions lib/lib-storage/src/Upload.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ import { CompleteMultipartUploadCommandOutput, S3, S3Client } from "@aws-sdk/cli
import { createHash } from "crypto";

import { Progress, Upload } from "./index";
import { AbortController } from "@aws-sdk/abort-controller";

const DEFAULT_PART_SIZE = 1024 * 1024 * 5;

Expand Down Expand Up @@ -638,4 +639,22 @@ describe(Upload.name, () => {
expect(mockAddListener).toHaveBeenCalledWith("xhr.upload.progress", expect.any(Function));
expect(mockRemoveListener).toHaveBeenCalledWith("xhr.upload.progress", expect.any(Function));
});

it("should respect external abort signal", async () => {
const abortController = new AbortController();

try {
const upload = new Upload({
params,
client: new S3({}),
abortController,
});

abortController.abort();

await upload.done();
} catch (error) {
expect(error).toBeDefined();
}
});
});
2 changes: 1 addition & 1 deletion lib/lib-storage/src/Upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export class Upload extends EventEmitter {
// set progress defaults
this.totalBytes = byteLength(this.params.Body);
this.bytesUploadedSoFar = 0;
this.abortController = new AbortController();
this.abortController = options.abortController ?? new AbortController();
}

async abort(): Promise<void> {
Expand Down
6 changes: 6 additions & 0 deletions lib/lib-storage/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { PutObjectCommandInput, S3Client, Tag } from "@aws-sdk/client-s3";
import { AbortController } from "@aws-sdk/abort-controller";

export interface Progress {
loaded?: number;
Expand Down Expand Up @@ -40,6 +41,11 @@ export interface Configuration {
* The tags to apply to the object.
*/
tags: Tag[];

/**
* Optional abort controller for controlling this upload's abort signal externally.
*/
abortController?: AbortController;
}

export interface Options extends Partial<Configuration> {
Expand Down

0 comments on commit 90c6b11

Please sign in to comment.