Skip to content

Commit 90c6b11

Browse files
authored
feat(upload-abort-controller): add optional abortController param for Upload (#3873)
1 parent 351d3a0 commit 90c6b11

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

Diff for: lib/lib-storage/src/Upload.spec.ts

+19
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ import { CompleteMultipartUploadCommandOutput, S3, S3Client } from "@aws-sdk/cli
6868
import { createHash } from "crypto";
6969

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

7273
const DEFAULT_PART_SIZE = 1024 * 1024 * 5;
7374

@@ -638,4 +639,22 @@ describe(Upload.name, () => {
638639
expect(mockAddListener).toHaveBeenCalledWith("xhr.upload.progress", expect.any(Function));
639640
expect(mockRemoveListener).toHaveBeenCalledWith("xhr.upload.progress", expect.any(Function));
640641
});
642+
643+
it("should respect external abort signal", async () => {
644+
const abortController = new AbortController();
645+
646+
try {
647+
const upload = new Upload({
648+
params,
649+
client: new S3({}),
650+
abortController,
651+
});
652+
653+
abortController.abort();
654+
655+
await upload.done();
656+
} catch (error) {
657+
expect(error).toBeDefined();
658+
}
659+
});
641660
});

Diff for: lib/lib-storage/src/Upload.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ export class Upload extends EventEmitter {
8383
// set progress defaults
8484
this.totalBytes = byteLength(this.params.Body);
8585
this.bytesUploadedSoFar = 0;
86-
this.abortController = new AbortController();
86+
this.abortController = options.abortController ?? new AbortController();
8787
}
8888

8989
async abort(): Promise<void> {

Diff for: lib/lib-storage/src/types.ts

+6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { PutObjectCommandInput, S3Client, Tag } from "@aws-sdk/client-s3";
2+
import { AbortController } from "@aws-sdk/abort-controller";
23

34
export interface Progress {
45
loaded?: number;
@@ -40,6 +41,11 @@ export interface Configuration {
4041
* The tags to apply to the object.
4142
*/
4243
tags: Tag[];
44+
45+
/**
46+
* Optional abort controller for controlling this upload's abort signal externally.
47+
*/
48+
abortController?: AbortController;
4349
}
4450

4551
export interface Options extends Partial<Configuration> {

0 commit comments

Comments
 (0)