From 20a092014680acfc6d30691fe324072e558d4e63 Mon Sep 17 00:00:00 2001 From: Anton Kastritskiy Date: Tue, 8 Oct 2024 12:32:59 -0700 Subject: [PATCH] Allow to download files from interncache Summary: Added a query param to gate inerncache urls from the existing controller to the newer plugin veresions only Reviewed By: mweststrate Differential Revision: D64055855 fbshipit-source-id: 2ad02f688b0eeb1bb1f86673167ac6b0a7cb5e7c --- desktop/flipper-common/src/server-types.tsx | 1 + .../flipper-server/src/FlipperServerImpl.tsx | 25 ++++++++++++++++--- .../src/commands/DownloadFile.tsx | 3 ++- .../src/fb-stubs/internRequests.tsx | 12 +++++++++ 4 files changed, 37 insertions(+), 4 deletions(-) diff --git a/desktop/flipper-common/src/server-types.tsx b/desktop/flipper-common/src/server-types.tsx index a57dd091670..6cbc8333b52 100644 --- a/desktop/flipper-common/src/server-types.tsx +++ b/desktop/flipper-common/src/server-types.tsx @@ -506,6 +506,7 @@ export interface DownloadFileStartOptions { maxRedirects?: number; headers?: Record; overwrite?: boolean; + proxy?: {host: string; port: number; protocol?: string}; } export type DownloadFileUpdate = { diff --git a/desktop/flipper-server/src/FlipperServerImpl.tsx b/desktop/flipper-server/src/FlipperServerImpl.tsx index be844f7e288..1fde030ff7f 100644 --- a/desktop/flipper-server/src/FlipperServerImpl.tsx +++ b/desktop/flipper-server/src/FlipperServerImpl.tsx @@ -32,6 +32,7 @@ import { Settings, ClientQuery, RmOptions, + DownloadFileStartOptions, } from 'flipper-common'; import {ServerDevice} from './devices/ServerDevice'; import {Base64} from 'js-base64'; @@ -49,6 +50,7 @@ import {sendScribeLogs} from './fb-stubs/sendScribeLogs'; import { internGraphGETAPIRequest, internGraphPOSTAPIRequest, + rewriteInternRequest, } from './fb-stubs/internRequests'; import {commandNodeApiExec} from './commands/NodeApiExec'; import {commandDownloadFileStartFactory} from './commands/DownloadFile'; @@ -501,9 +503,26 @@ export class FlipperServerImpl implements FlipperServer { 'node-api-fs-writefile-binary': (path, base64contents) => writeFile(path, Base64.toUint8Array(base64contents), 'binary'), // TODO: Do we need API to cancel an active download? - 'download-file-start': commandDownloadFileStartFactory( - this.emit.bind(this), - ), + 'download-file-start': async ( + endpoint: string, + dest: string, + options?: DownloadFileStartOptions, + ) => { + const downloadFileStart = commandDownloadFileStartFactory( + this.emit.bind(this), + ); + + const {url, headers, proxy} = await rewriteInternRequest( + endpoint, + options?.headers ?? {}, + ); + + return downloadFileStart(url, dest, { + ...options, + proxy: proxy ?? undefined, + headers: headers as Record, + }); + }, 'get-config': async () => this.config, 'get-changelog': getChangelog, 'device-find': async (deviceSerial) => { diff --git a/desktop/flipper-server/src/commands/DownloadFile.tsx b/desktop/flipper-server/src/commands/DownloadFile.tsx index 75d5923e13b..5427f7f3a96 100644 --- a/desktop/flipper-server/src/commands/DownloadFile.tsx +++ b/desktop/flipper-server/src/commands/DownloadFile.tsx @@ -31,7 +31,7 @@ export const commandDownloadFileStartFactory = async ( url, dest, - {method = 'GET', timeout, maxRedirects, headers, overwrite} = {}, + {method = 'GET', timeout, maxRedirects, headers, overwrite, proxy} = {}, ) => { const destExists = await pathExists(dest); @@ -70,6 +70,7 @@ export const commandDownloadFileStartFactory = timeout, maxRedirects, headers, + proxy, }); let totalSize = parseInt(response.headers['content-length'], 10); if (Number.isNaN(totalSize)) { diff --git a/desktop/flipper-server/src/fb-stubs/internRequests.tsx b/desktop/flipper-server/src/fb-stubs/internRequests.tsx index d811f4ba631..c307d34290f 100644 --- a/desktop/flipper-server/src/fb-stubs/internRequests.tsx +++ b/desktop/flipper-server/src/fb-stubs/internRequests.tsx @@ -7,6 +7,7 @@ * @format */ +import type {AxiosProxyConfig} from 'axios'; import {GraphFileUpload, GraphResponse} from 'flipper-common'; /* eslint-disable @typescript-eslint/no-unused-vars */ @@ -43,3 +44,14 @@ export async function internGraphGETAPIRequest( ): Promise { throw new Error('Feature not implemented'); } + +export async function rewriteInternRequest( + url: string, + headers: Record, +): Promise<{ + url: string; + headers: Record; + proxy: AxiosProxyConfig | null; +}> { + return {url, headers, proxy: null}; +}