From 161a358c492032745c42c702e08d0e6929f10892 Mon Sep 17 00:00:00 2001 From: Daniel James Smith <2670567+djsmith85@users.noreply.github.com> Date: Tue, 10 Dec 2024 13:17:00 +0100 Subject: [PATCH] [PM-5214] [CLI] Add proxy support for file uploads (#7342) * Add proxy support for file uploads Instead of using node's native fetch we extend ApiService with NodeApiService to add support for proxies using `node-fetch` * Fix constructors for FileUploadService in browser * Fix dependency on ApiService within jslib-services.module --------- Co-authored-by: Daniel James Smith --- apps/browser/src/background/main.background.ts | 2 +- apps/cli/src/service-container/service-container.ts | 2 +- libs/angular/src/services/jslib-services.module.ts | 2 +- .../file-upload/azure-file-upload.service.ts | 12 ++++++++---- .../services/file-upload/file-upload.service.ts | 8 ++++++-- 5 files changed, 17 insertions(+), 9 deletions(-) diff --git a/apps/browser/src/background/main.background.ts b/apps/browser/src/background/main.background.ts index 5745d21892..019be2923b 100644 --- a/apps/browser/src/background/main.background.ts +++ b/apps/browser/src/background/main.background.ts @@ -693,7 +693,7 @@ export default class MainBackground { ); this.domainSettingsService = new DefaultDomainSettingsService(this.stateProvider); - this.fileUploadService = new FileUploadService(this.logService); + this.fileUploadService = new FileUploadService(this.logService, this.apiService); this.cipherFileUploadService = new CipherFileUploadService( this.apiService, this.fileUploadService, diff --git a/apps/cli/src/service-container/service-container.ts b/apps/cli/src/service-container/service-container.ts index 83f7443cc4..91b75a14ff 100644 --- a/apps/cli/src/service-container/service-container.ts +++ b/apps/cli/src/service-container/service-container.ts @@ -485,7 +485,7 @@ export class ServiceContainer { this.domainSettingsService = new DefaultDomainSettingsService(this.stateProvider); - this.fileUploadService = new FileUploadService(this.logService); + this.fileUploadService = new FileUploadService(this.logService, this.apiService); this.sendStateProvider = new SendStateProvider(this.stateProvider); diff --git a/libs/angular/src/services/jslib-services.module.ts b/libs/angular/src/services/jslib-services.module.ts index 4e2911ec2e..92042a4162 100644 --- a/libs/angular/src/services/jslib-services.module.ts +++ b/libs/angular/src/services/jslib-services.module.ts @@ -447,7 +447,7 @@ const safeProviders: SafeProvider[] = [ safeProvider({ provide: FileUploadServiceAbstraction, useClass: FileUploadService, - deps: [LogService], + deps: [LogService, ApiServiceAbstraction], }), safeProvider({ provide: CipherFileUploadServiceAbstraction, diff --git a/libs/common/src/platform/services/file-upload/azure-file-upload.service.ts b/libs/common/src/platform/services/file-upload/azure-file-upload.service.ts index cbea4f2a6e..02adcfee22 100644 --- a/libs/common/src/platform/services/file-upload/azure-file-upload.service.ts +++ b/libs/common/src/platform/services/file-upload/azure-file-upload.service.ts @@ -1,5 +1,6 @@ // FIXME: Update this file to be type safe and remove this and next line // @ts-strict-ignore +import { ApiService } from "../../../abstractions/api.service"; import { LogService } from "../../abstractions/log.service"; import { Utils } from "../../misc/utils"; import { EncArrayBuffer } from "../../models/domain/enc-array-buffer"; @@ -8,7 +9,10 @@ const MAX_SINGLE_BLOB_UPLOAD_SIZE = 256 * 1024 * 1024; // 256 MiB const MAX_BLOCKS_PER_BLOB = 50000; export class AzureFileUploadService { - constructor(private logService: LogService) {} + constructor( + private logService: LogService, + private apiService: ApiService, + ) {} async upload(url: string, data: EncArrayBuffer, renewalCallback: () => Promise) { if (data.buffer.byteLength <= MAX_SINGLE_BLOB_UPLOAD_SIZE) { @@ -33,7 +37,7 @@ export class AzureFileUploadService { headers: headers, }); - const blobResponse = await fetch(request); + const blobResponse = await this.apiService.nativeFetch(request); if (blobResponse.status !== 201) { throw new Error(`Failed to create Azure blob: ${blobResponse.status}`); @@ -79,7 +83,7 @@ export class AzureFileUploadService { headers: blockHeaders, }); - const blockResponse = await fetch(blockRequest); + const blockResponse = await this.apiService.nativeFetch(blockRequest); if (blockResponse.status !== 201) { const message = `Unsuccessful block PUT. Received status ${blockResponse.status}`; @@ -108,7 +112,7 @@ export class AzureFileUploadService { headers: headers, }); - const response = await fetch(request); + const response = await this.apiService.nativeFetch(request); if (response.status !== 201) { const message = `Unsuccessful block list PUT. Received status ${response.status}`; diff --git a/libs/common/src/platform/services/file-upload/file-upload.service.ts b/libs/common/src/platform/services/file-upload/file-upload.service.ts index b76fc8f2fa..e09d8afac4 100644 --- a/libs/common/src/platform/services/file-upload/file-upload.service.ts +++ b/libs/common/src/platform/services/file-upload/file-upload.service.ts @@ -1,5 +1,6 @@ // FIXME: Update this file to be type safe and remove this and next line // @ts-strict-ignore +import { ApiService } from "../../../abstractions/api.service"; import { FileUploadApiMethods, FileUploadService as FileUploadServiceAbstraction, @@ -16,8 +17,11 @@ export class FileUploadService implements FileUploadServiceAbstraction { private azureFileUploadService: AzureFileUploadService; private bitwardenFileUploadService: BitwardenFileUploadService; - constructor(protected logService: LogService) { - this.azureFileUploadService = new AzureFileUploadService(logService); + constructor( + protected logService: LogService, + apiService: ApiService, + ) { + this.azureFileUploadService = new AzureFileUploadService(logService, apiService); this.bitwardenFileUploadService = new BitwardenFileUploadService(); }