diff --git a/package.json b/package.json index 770428d..c4d4023 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,6 @@ "ini": "^4.1.1", "plist": "^3.1.0", "split2": "^4.2.0", - "through2": "^4.0.2", "tslib": "^2.6.2", "yauzl": "^2.10.0" }, @@ -56,7 +55,6 @@ "@types/node": "^16.0.0", "@types/plist": "^3.0.4", "@types/split2": "^4.2.2", - "@types/through2": "^2.0.40", "@types/yauzl": "^2.10.2", "eslint": "^8.57.0", "jest": "^26.4.2", diff --git a/src/android/utils/adb.ts b/src/android/utils/adb.ts index 3d0a81f..ef8f549 100644 --- a/src/android/utils/adb.ts +++ b/src/android/utils/adb.ts @@ -3,7 +3,7 @@ import * as Debug from 'debug'; import * as os from 'os'; import * as path from 'path'; import * as split2 from 'split2'; -import * as through2 from 'through2'; +import { Transform } from 'stream'; import { ADBException, @@ -188,27 +188,29 @@ export async function installApk(sdk: SDK, device: Device, apk: string): Promise }); p.stderr.pipe(split2()).pipe( - through2((chunk, enc, cb) => { - const line = chunk.toString(); - - debug('adb install: %O', line); - const event = parseAdbInstallOutput(line); - - if (event === ADBEvent.IncompatibleUpdateFailure) { - reject(new ADBException(`Encountered adb error: ${ADBEvent[event]}.`, ERR_INCOMPATIBLE_UPDATE)); - } else if (event === ADBEvent.NewerVersionOnDeviceFailure) { - reject(new ADBException(`Encountered adb error: ${ADBEvent[event]}.`, ERR_VERSION_DOWNGRADE)); - } else if (event === ADBEvent.NewerSdkRequiredOnDeviceFailure) { - reject(new ADBException(`Encountered adb error: ${ADBEvent[event]}.`, ERR_MIN_SDK_VERSION)); - } else if (event === ADBEvent.NoCertificates) { - reject(new ADBException(`Encountered adb error: ${ADBEvent[event]}.`, ERR_NO_CERTIFICATES)); - } else if (event === ADBEvent.NotEnoughSpace) { - reject(new ADBException(`Encountered adb error: ${ADBEvent[event]}.`, ERR_NOT_ENOUGH_SPACE)); - } else if (event === ADBEvent.DeviceOffline) { - reject(new ADBException(`Encountered adb error: ${ADBEvent[event]}.`, ERR_DEVICE_OFFLINE)); - } - - cb(); + new Transform({ + transform: (chunk, enc, cb) => { + const line = chunk.toString(); + + debug('adb install: %O', line); + const event = parseAdbInstallOutput(line); + + if (event === ADBEvent.IncompatibleUpdateFailure) { + reject(new ADBException(`Encountered adb error: ${ADBEvent[event]}.`, ERR_INCOMPATIBLE_UPDATE)); + } else if (event === ADBEvent.NewerVersionOnDeviceFailure) { + reject(new ADBException(`Encountered adb error: ${ADBEvent[event]}.`, ERR_VERSION_DOWNGRADE)); + } else if (event === ADBEvent.NewerSdkRequiredOnDeviceFailure) { + reject(new ADBException(`Encountered adb error: ${ADBEvent[event]}.`, ERR_MIN_SDK_VERSION)); + } else if (event === ADBEvent.NoCertificates) { + reject(new ADBException(`Encountered adb error: ${ADBEvent[event]}.`, ERR_NO_CERTIFICATES)); + } else if (event === ADBEvent.NotEnoughSpace) { + reject(new ADBException(`Encountered adb error: ${ADBEvent[event]}.`, ERR_NOT_ENOUGH_SPACE)); + } else if (event === ADBEvent.DeviceOffline) { + reject(new ADBException(`Encountered adb error: ${ADBEvent[event]}.`, ERR_DEVICE_OFFLINE)); + } + + cb(); + }, }), ); }); diff --git a/src/android/utils/emulator.ts b/src/android/utils/emulator.ts index 93da3b1..5e4a900 100644 --- a/src/android/utils/emulator.ts +++ b/src/android/utils/emulator.ts @@ -5,7 +5,7 @@ import * as net from 'net'; import * as os from 'os'; import * as path from 'path'; import * as split2 from 'split2'; -import * as through2 from 'through2'; +import { Transform } from 'stream'; import { ERR_ALREADY_RUNNING, @@ -74,20 +74,22 @@ export async function spawnEmulator(sdk: SDK, avd: AVD, port: number): Promise reject(err), ); - const eventParser = through2((chunk: string, enc, cb) => { - const line = chunk.toString(); - - debug('Android Emulator: %O', line); - const event = parseEmulatorOutput(line); - if (event === EmulatorEvent.AlreadyRunning) { - reject(new EmulatorException(`Emulator already running with AVD [${avd.id}]`, ERR_ALREADY_RUNNING)); - } else if (event === EmulatorEvent.UnknownAVD) { - reject(new EmulatorException(`Unknown AVD name [${avd.id}]`, ERR_UNKNOWN_AVD)); - } else if (event === EmulatorEvent.AVDHomeNotFound) { - reject(new EmulatorException(`Emulator cannot find AVD home`, ERR_AVD_HOME_NOT_FOUND)); - } + const eventParser = new Transform({ + transform: (chunk: string, enc, cb) => { + const line = chunk.toString(); - cb(); + debug('Android Emulator: %O', line); + const event = parseEmulatorOutput(line); + if (event === EmulatorEvent.AlreadyRunning) { + reject(new EmulatorException(`Emulator already running with AVD [${avd.id}]`, ERR_ALREADY_RUNNING)); + } else if (event === EmulatorEvent.UnknownAVD) { + reject(new EmulatorException(`Unknown AVD name [${avd.id}]`, ERR_UNKNOWN_AVD)); + } else if (event === EmulatorEvent.AVDHomeNotFound) { + reject(new EmulatorException(`Emulator cannot find AVD home`, ERR_AVD_HOME_NOT_FOUND)); + } + + cb(); + }, }); const stdoutStream = p.stdout.pipe(split2()); @@ -199,37 +201,39 @@ export async function getAVDFromEmulator(emulator: Device, avds: readonly AVD[]) }); sock.pipe(split2()).pipe( - through2((chunk: string, enc, cb) => { - const line = chunk.toString(); - - debug('Android Console: %O', line); - - if (stage === Stage.Initial && line.includes('Authentication required')) { - stage = Stage.Auth; - } else if (stage === Stage.Auth && line.trim() === 'OK') { - readAuthFile.then( - (token) => sock.write(`auth ${token}\n`, 'utf8'), - (err) => reject(err), - ); - stage = Stage.AuthSuccess; - } else if (stage === Stage.AuthSuccess && line.trim() === 'OK') { - sock.write('avd name\n', 'utf8'); - stage = Stage.Response; - } else if (stage === Stage.Response) { - const avdId = line.trim(); - const avd = avds.find((avd) => avd.id === avdId); - - if (avd) { - resolve(avd); - } else { - reject(new EmulatorException(`Unknown AVD name [${avdId}]`, ERR_UNKNOWN_AVD)); + new Transform({ + transform: (chunk: string, enc, cb) => { + const line = chunk.toString(); + + debug('Android Console: %O', line); + + if (stage === Stage.Initial && line.includes('Authentication required')) { + stage = Stage.Auth; + } else if (stage === Stage.Auth && line.trim() === 'OK') { + readAuthFile.then( + (token) => sock.write(`auth ${token}\n`, 'utf8'), + (err) => reject(err), + ); + stage = Stage.AuthSuccess; + } else if (stage === Stage.AuthSuccess && line.trim() === 'OK') { + sock.write('avd name\n', 'utf8'); + stage = Stage.Response; + } else if (stage === Stage.Response) { + const avdId = line.trim(); + const avd = avds.find((avd) => avd.id === avdId); + + if (avd) { + resolve(avd); + } else { + reject(new EmulatorException(`Unknown AVD name [${avdId}]`, ERR_UNKNOWN_AVD)); + } + + stage = Stage.Complete; + cleanup(); } - stage = Stage.Complete; - cleanup(); - } - - cb(); + cb(); + }, }), ); });