diff --git a/desktop/app/src/server/devices/android/AndroidDevice.tsx b/desktop/app/src/server/devices/android/AndroidDevice.tsx index 07929b359..d7b6a556e 100644 --- a/desktop/app/src/server/devices/android/AndroidDevice.tsx +++ b/desktop/app/src/server/devices/android/AndroidDevice.tsx @@ -130,15 +130,20 @@ export default class AndroidDevice extends BaseDevice { return Buffer.from([]); } return new Promise((resolve, reject) => { - this.adb.screencap(this.serial).then((stream) => { - const chunks: Array = []; - stream - .on('data', (chunk: Buffer) => chunks.push(chunk)) - .once('end', () => { - resolve(Buffer.concat(chunks)); - }) - .once('error', reject); - }); + this.adb + .screencap(this.serial) + .then((stream) => { + const chunks: Array = []; + stream + .on('data', (chunk: Buffer) => chunks.push(chunk)) + .once('end', () => { + resolve(Buffer.concat(chunks)); + }) + .once('error', reject); + }) + .catch((e) => { + reject(e); + }); }); } @@ -224,13 +229,15 @@ export default class AndroidDevice extends BaseDevice { }) .then( (_) => - new Promise((resolve, reject) => { - this.adb.pull(this.serial, recordingLocation).then((stream) => { - stream.on('end', resolve as () => void); - stream.on('error', reject); - stream.pipe(createWriteStream(destination, {autoClose: true}), { - end: true, - }); + new Promise(async (resolve, reject) => { + const stream: PullTransfer = await this.adb.pull( + this.serial, + recordingLocation, + ); + stream.on('end', resolve as () => void); + stream.on('error', reject); + stream.pipe(createWriteStream(destination, {autoClose: true}), { + end: true, }); }), ) diff --git a/desktop/app/src/server/devices/android/adbClient.tsx b/desktop/app/src/server/devices/android/adbClient.tsx index ff0a47115..e5d96ae02 100644 --- a/desktop/app/src/server/devices/android/adbClient.tsx +++ b/desktop/app/src/server/devices/android/adbClient.tsx @@ -31,15 +31,17 @@ export function getAdbClient(config: Config): Promise { /* Adbkit will attempt to start the adb server if it's not already running, however, it sometimes fails with ENOENT errors. So instead, we start it manually before requesting a client. */ -function createClient(config: Config): Promise { +async function createClient(config: Config): Promise { const androidHome = config.androidHome; const adbPath = path.resolve(androidHome, 'platform-tools/adb'); - return reportPlatformFailures( - execFile(adbPath, ['start-server']).then(() => - adbkit.createClient(adbConfig()), - ), - 'createADBClient.shell', - ).catch((err) => { + try { + return reportPlatformFailures( + execFile(adbPath, ['start-server']).then(() => + adbkit.createClient(adbConfig()), + ), + 'createADBClient.shell', + ); + } catch (err) { console.log( 'Failed to create adb client using shell adb command. Trying with adbkit.\n' + err.toString(), @@ -48,23 +50,19 @@ function createClient(config: Config): Promise { /* In the event that starting adb with the above method fails, fallback to using adbkit, though its known to be unreliable. */ const unsafeClient: Client = adbkit.createClient(adbConfig()); - return reportPlatformFailures( + return await reportPlatformFailures( promiseRetry( - (retry, attempt): Promise => { - return unsafeClient - .listDevices() - .then(() => { - return unsafeClient; - }) - .catch((e: Error) => { - console.warn( - `Failed to start adb client. Retrying. ${e.message}`, - ); - if (attempt <= MAX_RETRIES) { - retry(e); - } - throw e; - }); + async (retry, attempt): Promise => { + try { + await unsafeClient.listDevices(); + return unsafeClient; + } catch (e) { + console.warn(`Failed to start adb client. Retrying. ${e.message}`); + if (attempt <= MAX_RETRIES) { + retry(e); + } + throw e; + } }, { minTimeout: 200, @@ -73,5 +71,5 @@ function createClient(config: Config): Promise { ), 'createADBClient.adbkit', ); - }); + } } diff --git a/desktop/app/src/server/devices/android/androidContainerUtility.tsx b/desktop/app/src/server/devices/android/androidContainerUtility.tsx index f171ced2d..04dd707cf 100644 --- a/desktop/app/src/server/devices/android/androidContainerUtility.tsx +++ b/desktop/app/src/server/devices/android/androidContainerUtility.tsx @@ -16,31 +16,26 @@ import { } from './androidContainerUtilityInternal'; import {Client} from 'adbkit'; -export function push( +export async function push( client: Client, deviceId: string, app: string, filepath: string, contents: string, ): Promise { - return validateAppName(app).then((validApp) => - validateFilePath(filepath).then((validFilepath) => - validateFileContent(contents).then((validContent) => - _push(client, deviceId, validApp, validFilepath, validContent), - ), - ), - ); + const validApp = await validateAppName(app); + const validFilepath = await validateFilePath(filepath); + const validContent = await validateFileContent(contents); + return await _push(client, deviceId, validApp, validFilepath, validContent); } -export function pull( +export async function pull( client: Client, deviceId: string, app: string, path: string, ): Promise { - return validateAppName(app).then((validApp) => - validateFilePath(path).then((validPath) => - _pull(client, deviceId, validApp, validPath), - ), - ); + const validApp = await validateAppName(app); + const validPath = await validateFilePath(path); + return await _pull(client, deviceId, validApp, validPath); } diff --git a/desktop/app/src/server/devices/android/androidContainerUtilityInternal.tsx b/desktop/app/src/server/devices/android/androidContainerUtilityInternal.tsx index 21c9bf859..8d16e7bb6 100644 --- a/desktop/app/src/server/devices/android/androidContainerUtilityInternal.tsx +++ b/desktop/app/src/server/devices/android/androidContainerUtilityInternal.tsx @@ -78,12 +78,8 @@ export function _push( .catch((error) => { if (error instanceof RunAsError) { // Fall back to running the command directly. This will work if adb is running as root. - return executeCommandWithSu(client, deviceId, app, command) - .then((_) => undefined) - .catch((e) => { - console.debug(e); - throw error; - }); + executeCommandWithSu(client, deviceId, app, command, error); + return undefined; } throw error; }); @@ -99,11 +95,7 @@ export function _pull( return executeCommandAsApp(client, deviceId, app, command).catch((error) => { if (error instanceof RunAsError) { // Fall back to running the command directly. This will work if adb is running as root. - return executeCommandWithSu(client, deviceId, app, command).catch((e) => { - // Throw the original error. - console.debug(e); - throw error; - }); + return executeCommandWithSu(client, deviceId, app, command, error); } throw error; }); @@ -125,13 +117,19 @@ function executeCommandAsApp( ); } -function executeCommandWithSu( +async function executeCommandWithSu( client: Client, deviceId: string, app: string, command: string, + originalErrorToThrow: RunAsError, ): Promise { - return _executeCommandWithRunner(client, deviceId, app, command, 'su'); + try { + return _executeCommandWithRunner(client, deviceId, app, command, 'su'); + } catch (e) { + console.debug(e); + throw originalErrorToThrow; + } } function _executeCommandWithRunner( diff --git a/desktop/app/src/server/devices/ios/__tests__/iOSDevice.node.tsx b/desktop/app/src/server/devices/ios/__tests__/iOSDevice.node.tsx index 6015663d5..6034b65b2 100644 --- a/desktop/app/src/server/devices/ios/__tests__/iOSDevice.node.tsx +++ b/desktop/app/src/server/devices/ios/__tests__/iOSDevice.node.tsx @@ -17,7 +17,6 @@ import {FlipperServer} from '../../../FlipperServer'; const mockStore = configureStore([])( createRootReducer()(undefined, {type: 'INIT'}), ); -const logger = getInstance(); const standardCoresimulatorLog = 'username 1264 0.0 0.1 5989740 41648 ?? Ss 2:23PM 0:12.92 /Applications/Xcode_12.4.0_fb.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/usr/libexec/mobileassetd';