1
1
import { AbortController , AbortSignal } from "@aws-sdk/abort-controller" ;
2
2
import {
3
+ AbortMultipartUploadCommandOutput ,
3
4
CompletedPart ,
4
5
CompleteMultipartUploadCommand ,
5
6
CompleteMultipartUploadCommandOutput ,
8
9
PutObjectCommand ,
9
10
PutObjectCommandInput ,
10
11
PutObjectTaggingCommand ,
11
- ServiceOutputTypes ,
12
+ S3Client ,
12
13
Tag ,
13
14
UploadPartCommand ,
14
15
} from "@aws-sdk/client-s3" ;
@@ -17,7 +18,7 @@ import { EventEmitter } from "events";
17
18
18
19
import { byteLength } from "./bytelength" ;
19
20
import { getChunk } from "./chunker" ;
20
- import { BodyDataTypes , Options , Progress , ServiceClients } from "./types" ;
21
+ import { BodyDataTypes , Options , Progress } from "./types" ;
21
22
22
23
export interface RawDataPart {
23
24
partNumber : number ;
@@ -39,7 +40,7 @@ export class Upload extends EventEmitter {
39
40
private leavePartsOnError = false ;
40
41
private tags : Tag [ ] = [ ] ;
41
42
42
- private client : ServiceClients ;
43
+ private client : S3Client ;
43
44
private params : PutObjectCommandInput ;
44
45
45
46
// used for reporting progress.
@@ -56,7 +57,7 @@ export class Upload extends EventEmitter {
56
57
uploadEvent ?: string ;
57
58
58
59
private isMultiPart = true ;
59
- private putResponse ?: CompleteMultipartUploadCommandOutput ;
60
+ private singleUploadResult ?: CompleteMultipartUploadCommandOutput ;
60
61
61
62
constructor ( options : Options ) {
62
63
super ( ) ;
@@ -86,16 +87,16 @@ export class Upload extends EventEmitter {
86
87
this . abortController . abort ( ) ;
87
88
}
88
89
89
- async done ( ) : Promise < ServiceOutputTypes > {
90
+ public async done ( ) : Promise < CompleteMultipartUploadCommandOutput | AbortMultipartUploadCommandOutput > {
90
91
return await Promise . race ( [ this . __doMultipartUpload ( ) , this . __abortTimeout ( this . abortController . signal ) ] ) ;
91
92
}
92
93
93
- on ( event : "httpUploadProgress" , listener : ( progress : Progress ) => void ) : any {
94
+ public on ( event : "httpUploadProgress" , listener : ( progress : Progress ) => void ) : this {
94
95
this . uploadEvent = event ;
95
- super . on ( event , listener ) ;
96
+ return super . on ( event , listener ) ;
96
97
}
97
98
98
- async __uploadUsingPut ( dataPart : RawDataPart ) {
99
+ private async __uploadUsingPut ( dataPart : RawDataPart ) : Promise < void > {
99
100
this . isMultiPart = false ;
100
101
const params = { ...this . params , Body : dataPart . data } ;
101
102
const [ putResult , endpoint ] = await Promise . all ( [
@@ -113,7 +114,7 @@ export class Upload extends EventEmitter {
113
114
? `${ endpoint . protocol } //${ endpoint . hostname } /${ locationBucket } /${ locationKey } `
114
115
: `${ endpoint . protocol } //${ locationBucket } .${ endpoint . hostname } /${ locationKey } ` ;
115
116
116
- this . putResponse = {
117
+ this . singleUploadResult = {
117
118
...putResult ,
118
119
Bucket : this . params . Bucket ,
119
120
Key : this . params . Key ,
@@ -129,7 +130,7 @@ export class Upload extends EventEmitter {
129
130
} ) ;
130
131
}
131
132
132
- async __createMultipartUpload ( ) {
133
+ private async __createMultipartUpload ( ) : Promise < void > {
133
134
if ( ! this . createMultiPartPromise ) {
134
135
const createCommandParams = { ...this . params , Body : undefined } ;
135
136
this . createMultiPartPromise = this . client . send ( new CreateMultipartUploadCommand ( createCommandParams ) ) ;
@@ -138,7 +139,7 @@ export class Upload extends EventEmitter {
138
139
this . uploadId = createMultipartUploadResult . UploadId ;
139
140
}
140
141
141
- async __doConcurrentUpload ( dataFeeder : AsyncGenerator < RawDataPart , void , undefined > ) : Promise < void > {
142
+ private async __doConcurrentUpload ( dataFeeder : AsyncGenerator < RawDataPart , void , undefined > ) : Promise < void > {
142
143
for await ( const dataPart of dataFeeder ) {
143
144
if ( this . uploadedParts . length > this . MAX_PARTS ) {
144
145
throw new Error (
@@ -207,7 +208,7 @@ export class Upload extends EventEmitter {
207
208
}
208
209
}
209
210
210
- async __doMultipartUpload ( ) : Promise < ServiceOutputTypes > {
211
+ private async __doMultipartUpload ( ) : Promise < CompleteMultipartUploadCommandOutput > {
211
212
// Set up data input chunks.
212
213
const dataFeeder = getChunk ( this . params . Body , this . partSize ) ;
213
214
@@ -237,7 +238,7 @@ export class Upload extends EventEmitter {
237
238
} ;
238
239
result = await this . client . send ( new CompleteMultipartUploadCommand ( uploadCompleteParams ) ) ;
239
240
} else {
240
- result = this . putResponse ! ;
241
+ result = this . singleUploadResult ! ;
241
242
}
242
243
243
244
// Add tags to the object after it's completed the upload.
@@ -255,13 +256,13 @@ export class Upload extends EventEmitter {
255
256
return result ;
256
257
}
257
258
258
- __notifyProgress ( progress : Progress ) {
259
+ private __notifyProgress ( progress : Progress ) : void {
259
260
if ( this . uploadEvent ) {
260
261
this . emit ( this . uploadEvent , progress ) ;
261
262
}
262
263
}
263
264
264
- async __abortTimeout ( abortSignal : AbortSignal ) : Promise < ServiceOutputTypes > {
265
+ private async __abortTimeout ( abortSignal : AbortSignal ) : Promise < AbortMultipartUploadCommandOutput > {
265
266
return new Promise ( ( resolve , reject ) => {
266
267
abortSignal . onabort = ( ) => {
267
268
const abortError = new Error ( "Upload aborted." ) ;
@@ -271,7 +272,7 @@ export class Upload extends EventEmitter {
271
272
} ) ;
272
273
}
273
274
274
- __validateInput ( ) {
275
+ private __validateInput ( ) : void {
275
276
if ( ! this . params ) {
276
277
throw new Error ( `InputError: Upload requires params to be passed to upload.` ) ;
277
278
}
0 commit comments