Skip to content

feat(upload-abort-controller): add optional abortController param for Upload #3873

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 1 commit into from
Sep 26, 2022
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
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 @@ -77,7 +77,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";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add this as a devDep in package.json?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's actually already there:

"@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