From 01b3691bbfc4a93e2229ea07184637479c515183 Mon Sep 17 00:00:00 2001 From: Stephen Date: Thu, 11 Jun 2020 17:54:04 -0400 Subject: [PATCH] feat: allow setting timeouts on uploads (#1208) Co-authored-by: Jonathan Lui --- src/bucket.ts | 4 ++++ src/file.ts | 13 +++++++++---- test/file.ts | 2 ++ 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/bucket.ts b/src/bucket.ts index d43934cca..2f333fc7f 100644 --- a/src/bucket.ts +++ b/src/bucket.ts @@ -347,6 +347,7 @@ export interface UploadOptions encryptionKey?: string | Buffer; kmsKeyName?: string; resumable?: boolean; + timeout?: number; // eslint-disable-next-line @typescript-eslint/no-explicit-any onUploadProgress?: (progressEvent: any) => void; } @@ -3239,6 +3240,9 @@ class Bucket extends ServiceObject { * `options.predefinedAcl = 'publicRead'`) * @param {boolean} [options.resumable] Force a resumable upload. (default: * true for files larger than 5 MB). + * @param {number} [options.timeout=60000] Set the HTTP request timeout in + * milliseconds. This option is not available for resumable uploads. + * Default: `60000` * @param {string} [options.uri] The URI for an already-created resumable * upload. See {@link File#createResumableUpload}. * @param {string} [options.userProject] The ID of the project which will be diff --git a/src/file.ts b/src/file.ts index 1e52483e7..d3c88cf7b 100644 --- a/src/file.ts +++ b/src/file.ts @@ -211,6 +211,7 @@ export interface CreateWriteStreamOptions extends CreateResumableUploadOptions { contentType?: string; gzip?: string | boolean; resumable?: boolean; + timeout?: number; validation?: string | boolean; } @@ -1573,6 +1574,9 @@ class File extends ServiceObject { * working with streams, the file format and size is unknown until it's * completely consumed. Because of this, it's best for you to be explicit * for what makes sense given your input. + * @param {number} [timeout=60000] Set the HTTP request timeout in + * milliseconds. This option is not available for resumable uploads. + * Default: `60000` * @property {string} [uri] The URI for an already-created resumable * upload. See {@link File#createResumableUpload}. * @property {string} [userProject] The ID of the project which will be @@ -3564,10 +3568,7 @@ class File extends ServiceObject { * * @private */ - startSimpleUpload_( - dup: Duplexify, - options?: CreateResumableUploadOptions - ): void { + startSimpleUpload_(dup: Duplexify, options?: CreateWriteStreamOptions): void { options = Object.assign( { metadata: {}, @@ -3594,6 +3595,10 @@ class File extends ServiceObject { reqOpts.qs.kmsKeyName = this.kmsKeyName; } + if (typeof options.timeout === 'number') { + reqOpts.timeout = options.timeout; + } + if (options.userProject || this.userProject) { reqOpts.qs.userProject = options.userProject || this.userProject; } diff --git a/test/file.ts b/test/file.ts index 2fd24a9f2..32e449c12 100644 --- a/test/file.ts +++ b/test/file.ts @@ -4151,6 +4151,7 @@ describe('File', () => { predefinedAcl: 'allUsers', private: true, public: true, + timeout: 99, }; // eslint-disable-next-line @typescript-eslint/no-explicit-any @@ -4161,6 +4162,7 @@ describe('File', () => { name: file.name, predefinedAcl: options.predefinedAcl, }, + timeout: options.timeout, uri: 'https://storage.googleapis.com/upload/storage/v1/b/' + file.bucket.name +