Skip to content

Commit 778c5b0

Browse files
committed
fix: improve idempotency token validation and standardize token removal logic during resumable uploads
1 parent fad3b47 commit 778c5b0

2 files changed

Lines changed: 10 additions & 13 deletions

File tree

handwritten/storage/src/nodejs-common/service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ export class Service {
274274
key => key.toLowerCase() === 'x-goog-gcs-idempotency-token'
275275
);
276276
const userTokenValue = userTokenKey ? headers[userTokenKey] : undefined;
277-
const hasValidUserToken = typeof userTokenValue === 'string' && userTokenValue;
277+
const hasValidUserToken = typeof userTokenValue === 'string' && userTokenValue !== '';
278278
const idempotencyToken = hasValidUserToken
279279
? (userTokenValue as string)
280280
: crypto.randomUUID();

handwritten/storage/src/resumable-upload.ts

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -814,9 +814,11 @@ export class Upload extends Writable {
814814
key => key.toLowerCase() === 'x-goog-gcs-idempotency-token'
815815
);
816816
const userTokenValue = userTokenKey ? this.customRequestOptions?.headers?.[userTokenKey] : undefined;
817-
const hasValidUserToken = typeof userTokenValue === 'string' && userTokenValue;
817+
const hasValidUserToken = typeof userTokenValue === 'string' && userTokenValue !== '';
818818
if (hasValidUserToken) {
819819
this.currentInvocationId.uri = userTokenValue as string;
820+
} else if (userTokenKey && this.customRequestOptions?.headers) {
821+
delete this.customRequestOptions.headers[userTokenKey];
820822
}
821823

822824
let googAPIClient = `${getRuntimeTrackingString()} gccl/${
@@ -847,9 +849,6 @@ export class Upload extends Writable {
847849
};
848850

849851
if (!hasValidUserToken) {
850-
if (userTokenKey) {
851-
delete reqOpts.headers![userTokenKey];
852-
}
853852
reqOpts.headers!['x-goog-gcs-idempotency-token'] = this.currentInvocationId.uri;
854853
}
855854

@@ -1019,9 +1018,11 @@ export class Upload extends Writable {
10191018
key => key.toLowerCase() === 'x-goog-gcs-idempotency-token'
10201019
);
10211020
const userTokenValue = userTokenKey ? this.customRequestOptions?.headers?.[userTokenKey] : undefined;
1022-
const hasValidUserToken = typeof userTokenValue === 'string' && userTokenValue;
1021+
const hasValidUserToken = typeof userTokenValue === 'string' && userTokenValue !== '';
10231022
if (hasValidUserToken) {
10241023
this.currentInvocationId.chunk = userTokenValue as string;
1024+
} else if (userTokenKey && this.customRequestOptions?.headers) {
1025+
delete this.customRequestOptions.headers[userTokenKey];
10251026
}
10261027

10271028
let googAPIClient = `${getRuntimeTrackingString()} gccl/${
@@ -1040,9 +1041,6 @@ export class Upload extends Writable {
10401041
};
10411042

10421043
if (!hasValidUserToken) {
1043-
if (userTokenKey) {
1044-
delete headers[userTokenKey];
1045-
}
10461044
headers['x-goog-gcs-idempotency-token'] = this.currentInvocationId.chunk;
10471045
}
10481046

@@ -1249,9 +1247,11 @@ export class Upload extends Writable {
12491247
key => key.toLowerCase() === 'x-goog-gcs-idempotency-token'
12501248
);
12511249
const userTokenValue = userTokenKey ? this.customRequestOptions?.headers?.[userTokenKey] : undefined;
1252-
const hasValidUserToken = typeof userTokenValue === 'string' && userTokenValue;
1250+
const hasValidUserToken = typeof userTokenValue === 'string' && userTokenValue !== '';
12531251
if (hasValidUserToken) {
12541252
this.currentInvocationId.checkUploadStatus = userTokenValue as string;
1253+
} else if (userTokenKey && this.customRequestOptions?.headers) {
1254+
delete this.customRequestOptions.headers[userTokenKey];
12551255
}
12561256

12571257
let googAPIClient = `${getRuntimeTrackingString()} gccl/${
@@ -1276,9 +1276,6 @@ export class Upload extends Writable {
12761276
};
12771277

12781278
if (!hasValidUserToken) {
1279-
if (userTokenKey) {
1280-
delete opts.headers![userTokenKey];
1281-
}
12821279
opts.headers!['x-goog-gcs-idempotency-token'] = this.currentInvocationId.checkUploadStatus;
12831280
}
12841281

0 commit comments

Comments
 (0)