@@ -338,7 +338,11 @@ export class Upload extends Writable {
338338 timeOfFirstRequest : number ;
339339 isPartialUpload : boolean ;
340340
341- private currentInvocationId = {
341+ private currentInvocationId : {
342+ checkUploadStatus : string ;
343+ chunk : string ;
344+ uri : string ;
345+ } = {
342346 checkUploadStatus : crypto . randomUUID ( ) ,
343347 chunk : crypto . randomUUID ( ) ,
344348 uri : crypto . randomUUID ( ) ,
@@ -806,6 +810,13 @@ export class Upload extends Writable {
806810 delete metadata . contentType ;
807811 }
808812
813+ const userTokenKey = Object . keys ( this . customRequestOptions . headers || { } ) . find (
814+ key => key . toLowerCase ( ) === 'x-goog-gcs-idempotency-token'
815+ ) ;
816+ if ( userTokenKey ) {
817+ this . currentInvocationId . uri = this . customRequestOptions . headers ! [ userTokenKey ] as string ;
818+ }
819+
809820 let googAPIClient = `${ getRuntimeTrackingString ( ) } gccl/${
810821 packageJson . version
811822 } -${ getModuleFormat ( ) } gccl-invocation-id/${ this . currentInvocationId . uri } `;
@@ -829,11 +840,14 @@ export class Upload extends Writable {
829840 headers : {
830841 'User-Agent' : getUserAgentString ( ) ,
831842 'x-goog-api-client' : googAPIClient ,
832- 'x-goog-gcs-idempotency-token' : this . currentInvocationId . uri ,
833843 ...headers ,
834844 } ,
835845 } ;
836846
847+ if ( ! userTokenKey ) {
848+ reqOpts . headers ! [ 'x-goog-gcs-idempotency-token' ] = this . currentInvocationId . uri ;
849+ }
850+
837851 if ( metadata . contentLength ) {
838852 reqOpts . headers ! [ 'X-Upload-Content-Length' ] =
839853 metadata . contentLength . toString ( ) ;
@@ -996,6 +1010,13 @@ export class Upload extends Writable {
9961010 } ,
9971011 } ) ;
9981012
1013+ const userTokenKey = Object . keys ( this . customRequestOptions . headers || { } ) . find (
1014+ key => key . toLowerCase ( ) === 'x-goog-gcs-idempotency-token'
1015+ ) ;
1016+ if ( userTokenKey ) {
1017+ this . currentInvocationId . chunk = this . customRequestOptions . headers ! [ userTokenKey ] as string ;
1018+ }
1019+
9991020 let googAPIClient = `${ getRuntimeTrackingString ( ) } gccl/${
10001021 packageJson . version
10011022 } -${ getModuleFormat ( ) } gccl-invocation-id/${
@@ -1009,9 +1030,12 @@ export class Upload extends Writable {
10091030 const headers : GaxiosOptions [ 'headers' ] = {
10101031 'User-Agent' : getUserAgentString ( ) ,
10111032 'x-goog-api-client' : googAPIClient ,
1012- 'x-goog-gcs-idempotency-token' : this . currentInvocationId . chunk ,
10131033 } ;
10141034
1035+ if ( ! userTokenKey ) {
1036+ headers [ 'x-goog-gcs-idempotency-token' ] = this . currentInvocationId . chunk ;
1037+ }
1038+
10151039 // If using multiple chunk upload, set appropriate header
10161040 if ( multiChunkMode ) {
10171041 // We need to know how much data is available upstream to set the `Content-Range` header.
@@ -1211,6 +1235,13 @@ export class Upload extends Writable {
12111235 async checkUploadStatus (
12121236 config : CheckUploadStatusConfig = { } ,
12131237 ) : Promise < GaxiosResponse < FileMetadata | void > > {
1238+ const userTokenKey = Object . keys ( this . customRequestOptions . headers || { } ) . find (
1239+ key => key . toLowerCase ( ) === 'x-goog-gcs-idempotency-token'
1240+ ) ;
1241+ if ( userTokenKey ) {
1242+ this . currentInvocationId . checkUploadStatus = this . customRequestOptions . headers ! [ userTokenKey ] as string ;
1243+ }
1244+
12141245 let googAPIClient = `${ getRuntimeTrackingString ( ) } gccl/${
12151246 packageJson . version
12161247 } -${ getModuleFormat ( ) } gccl-invocation-id/${
@@ -1229,10 +1260,13 @@ export class Upload extends Writable {
12291260 'Content-Range' : 'bytes */*' ,
12301261 'User-Agent' : getUserAgentString ( ) ,
12311262 'x-goog-api-client' : googAPIClient ,
1232- 'x-goog-gcs-idempotency-token' : this . currentInvocationId . checkUploadStatus ,
12331263 } ,
12341264 } ;
12351265
1266+ if ( ! userTokenKey ) {
1267+ opts . headers ! [ 'x-goog-gcs-idempotency-token' ] = this . currentInvocationId . checkUploadStatus ;
1268+ }
1269+
12361270 try {
12371271 const resp = await this . makeRequest ( opts ) ;
12381272
0 commit comments