Add download file API

Summary: Changelog: Expose "downloadFile" API to Flipper plugins. Allow them to download files form the web to Flipper Server.

Reviewed By: mweststrate

Differential Revision: D32950685

fbshipit-source-id: 7b7f666e165ff7bf209230cdc96078272ede3616
This commit is contained in:
Andrey Goncharov
2021-12-10 06:34:37 -08:00
committed by Facebook GitHub Bot
parent 4cb80a452f
commit 92f0ed67f4
12 changed files with 271 additions and 10 deletions

View File

@@ -20,6 +20,7 @@ export * from './server-types';
export {sleep} from './utils/sleep';
export {timeout} from './utils/timeout';
export {isTest} from './utils/isTest';
export {assertNever} from './utils/assertNever';
export {
logPlatformSuccessRate,
reportPlatformFailures,

View File

@@ -121,6 +121,7 @@ export type FlipperServerEvents = {
id: string;
message: string;
};
'download-file-update': DownloadFileUpdate;
};
export type IOSDeviceParams = {
@@ -151,6 +152,11 @@ export type FlipperServerCommands = {
command: string,
options?: ExecOptions & {encoding?: BufferEncoding},
) => Promise<ExecOut<string>>;
'download-file-start': (
url: string,
dest: string,
options?: DownloadFileStartOptions,
) => Promise<DownloadFileStartResponse>;
'get-config': () => Promise<FlipperServerConfig>;
'get-changelog': () => Promise<string>;
'device-list': () => Promise<DeviceDescription[]>;
@@ -326,6 +332,54 @@ export interface MkdirOptions {
mode?: string | number;
}
export interface DownloadFileStartOptions {
method?: 'GET' | 'POST';
timeout?: number;
maxRedirects?: number;
headers?: Record<string, string>;
overwrite?: boolean;
}
export type DownloadFileUpdate = {
id: string;
downloaded: number;
/**
* Set to 0 if unknown
*/
totalSize: number;
} & (
| {
status: 'downloading';
}
| {
status: 'success';
}
| {status: 'error'; message: string; stack?: string}
);
export interface DownloadFileStartResponse {
/**
* Download ID
*/
id: string;
/**
* Response status
*/
status: number;
/**
* Response status text
*/
statusText: string;
/**
* Response headers
*/
headers: Record<string, string>;
/**
* Size of the file, being downloaded, in bytes. Inferred from the "Content-Length" header. Set to 0 if unknown.
*/
totalSize: number;
}
export type FlipperServerConfig = {
gatekeepers: Record<string, boolean>;
env: Partial<Record<ENVIRONMENT_VARIABLES, string>>;

View File

@@ -0,0 +1,14 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
export const assertNever: (val: never) => asserts val = (val) => {
if (val) {
throw new Error(`Assert never failed. Received ${val}`);
}
};