Orca is the place for working with a fleet of agents (e.g. Grok Bot, Claude Code). Run any coding agent with your own subscription. Available on desktop, mobile and remote runtime.
TypeScript adeagent-ideai-agentsautomationbotclaude-codecodexghosttygrok-bothermes-agentmobile-appmuseopenclawopencodeorchestrationpiruntimeterminalworktreesyc-backed
fix(emulator): publish scrcpy JAR only after download completes (#35503)
OrcaWin895fd4324ae6
diff --git a/src/main/emulator/android/scrcpy-server-download.test.ts b/src/main/emulator/android/scrcpy-server-download.test.tsnew file mode 100644index 00000000000..f6c28e7e327--- /dev/null+++ b/src/main/emulator/android/scrcpy-server-download.test.ts@@ -0,0 +1,99 @@+import { EventEmitter } from 'node:events'+import { mkdtemp, readFile, readdir, rm, stat } from 'node:fs/promises'+import { tmpdir } from 'node:os'+import { basename, dirname, join } from 'node:path'+import { PassThrough } from 'node:stream'+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'++const state = vi.hoisted(() => ({ root: '', get: vi.fn() }))+vi.mock('../../../shared/app-environment', () => ({+ getAppEnvironment: () => ({ getPath: () => state.root })+}))+vi.mock('node:https', () => ({ get: state.get }))++class DownloadResponse extends PassThrough {+ statusCode = 200+ headers: { location?: string } = {}+}++class DownloadRequest extends EventEmitter {+ setTimeout(): this {+ return this+ }+}++beforeEach(async () => {+ vi.resetModules()+ state.root = await mkdtemp(join(tmpdir(), 'orca-scrcpy-download-'))+})++afterEach(async () => {+ vi.restoreAllMocks()+ await rm(state.root, { recursive: true, force: true })+})++async function prepareDownload() {+ const responses: DownloadResponse[] = []+ state.get.mockReset()+ state.get.mockImplementation((_url: string, callback: (response: DownloadResponse) => void) => {+ const response = new DownloadResponse()+ responses.push(response)+ queueMicrotask(() => callback?.(response))+ return new DownloadRequest()+ })+ return { get: state.get, responses, ...(await import('./scrcpy-server-download')) }+}++describe('scrcpy server download publication', () => {+ it('keeps concurrent callers waiting for the complete JAR', async () => {+ const download = await prepareDownload()+ const first = download.ensureScrcpyServerJar()+ const response = download.responses[0]!+ response.write(Buffer.alloc(16_384, 1))+ const directory = dirname(download.scrcpyServerJarPath())+ await vi.waitFor(async () => {+ const files = await readdir(directory)+ expect(files).toHaveLength(1)+ expect((await stat(join(directory, files[0]!))).size).toBe(16_384)+ })++ const completed: string[] = []+ const waiting = Array.from({ length: 2 }, () =>+ download.ensureScrcpyServerJar().then((path) => {+ completed.push(path)+ return path+ })+ )+ await new Promise<void>((resolve) => setImmediate(resolve))+ try {+ expect(download.isScrcpyServerJarReady()).toBe(false)+ expect(completed).toEqual([])+ } finally {+ response.end(Buffer.alloc(49_152, 2))+ await Promise.all([first, ...waiting])+ }++ const path = download.scrcpyServerJarPath()+ expect(completed).toEqual([path, path])+ expect(await readFile(path)).toEqual(+ Buffer.concat([Buffer.alloc(16_384, 1), Buffer.alloc(49_152, 2)])+ )+ expect(await readdir(directory)).toEqual([basename(path)])+ expect(download.get).toHaveBeenCalledTimes(1)+ })++ it('removes a failed partial download and permits a fresh attempt', async () => {+ const download = await prepareDownload()+ const failed = download.ensureScrcpyServerJar()+ const rejection = expect(failed).rejects.toThrow('interrupted')+ download.responses[0]!.write(Buffer.alloc(16_384))+ download.responses[0]!.destroy(new Error('interrupted'))+ await rejection+ expect(download.isScrcpyServerJarReady()).toBe(false)+ expect(await readdir(dirname(download.scrcpyServerJarPath()))).toEqual([])++ const retry = download.ensureScrcpyServerJar()+ download.responses[1]!.end(Buffer.alloc(32_768, 3))+ expect(await readFile(await retry)).toEqual(Buffer.alloc(32_768, 3))+ })+})diff --git a/src/main/emulator/android/scrcpy-server-download.ts b/src/main/emulator/android/scrcpy-server-download.tsindex 547fee95bdd..dc8e5248cc0 100644--- a/src/main/emulator/android/scrcpy-server-download.ts+++ b/src/main/emulator/android/scrcpy-server-download.ts@@ -1,4 +1,4 @@-import { createWriteStream, existsSync, mkdirSync, rmSync, statSync } from 'node:fs'+import { createWriteStream, existsSync, mkdirSync, renameSync, rmSync, statSync } from 'node:fs'import { dirname, join } from 'node:path'import { get } from 'node:https'import { pipeline } from 'node:stream/promises'@@ -48,20 +48,25 @@ export async function ensureScrcpyServerJar(): Promise<string> {async function downloadScrcpyServerJar(path: string): Promise<string> {emulatorProbe('scrcpy.jar.download.start', { url: DOWNLOAD_URL, dest: path })mkdirSync(dirname(path), { recursive: true })+ const downloadPath = `${path}.download`try {- await downloadTo(DOWNLOAD_URL, path)+ await downloadTo(DOWNLOAD_URL, downloadPath)+ if (statSync(downloadPath).size < MIN_VALID_BYTES) {+ throw new EmulatorError('emulator_helper_failed', emulatorFailureText('scrcpyServerInvalid'))+ }+ renameSync(downloadPath, path)} catch (error) {- rmSync(path, { force: true })emulatorProbeError('scrcpy.jar.download.fail', error, { url: DOWNLOAD_URL })+ if (error instanceof EmulatorError) {+ throw error+ }const detail = error instanceof Error ? error.message : 'unknown error'throw new EmulatorError('emulator_helper_failed',emulatorFailureText('scrcpyDownloadFailed', { detail }))- }- if (!isScrcpyServerJarReady()) {- rmSync(path, { force: true })- throw new EmulatorError('emulator_helper_failed', emulatorFailureText('scrcpyServerInvalid'))+ } finally {+ rmSync(downloadPath, { force: true })}emulatorProbe('scrcpy.jar.download.ok', { dest: path, bytes: statSync(path).size })return path