Compare commits

...
14 Commits
Author SHA1 Message Date
Expo CI c72ae9f9f6 v24.11.0 2026-10-05 22:00:12 +00:00
SJ Kim bf0420cd29 [eas-cli] Send the request origin and coding agent when creating a simulator session (#4523)
* [eas-cli] Regenerate GraphQL types (ENG-27135)

* [eas-cli] Send the request origin when eas simulator creates a session (ENG-26853)

* [eas-cli] Send the coding agent when eas simulator creates a session (ENG-27135)

* [eas-cli] Add the PR number to the changelog entry (ENG-27135)

* [eas-cli] Leave out the coding agent when analytics are disabled (ENG-27135)
2026-10-05 13:49:07 -04:00
Szymon Dziedzic 738ccb1ff6 Revert "[build-tools] Confine agent-device sessions to the session device (#4…" (#4540)
This reverts commit c487bcf1df.
2026-10-05 19:03:12 +02:00
Szymon DziedzicandClaude Opus 5.5 c487bcf1df [build-tools] Confine agent-device sessions to the session device (#4499)
* [build-tools] Confine agent-device sessions to the session device

Launch the agent-device daemon with a daemon policy
(AGENT_DEVICE_DAEMON_POLICY, agent-device 0.21.17 and later) that allows
only the booted device and denies booting and shutting down devices.

agent-device still installs during the boot. The daemon launches when
the boot reports the device. The policy file is removed when the daemon
stops or fails to launch. A daemon without `policyDigest` in daemon.json
does not enforce the policy, so the step logs a warning.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* [build-tools] Serve the booted Simulator in agent-device sessions

Pass the booted UDID to serve-sim, so the web preview shows the device
that the daemon policy allows. Without it, serve-sim picks a simulator
itself and can show another booted one. The session host fails when
serve-sim reports another device.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* [build-tools] Launch the agent-device daemon during the iOS Simulator boot

Look up the Simulator UDID before the boot and boot that UDID. The
daemon policy needs only the UDID, so on iOS the daemon launches during
the boot again, as before the policy. On Android the emulator serial is
known only after the boot, so the daemon still launches after it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
2026-10-05 16:25:51 +02:00
Szymon DziedzicandClaude Opus 5.5 bcaaaddab8 [build-tools] Report the web preview as webPreviewUrl and webPreviewToken (#4467)
* [build-tools] Report the web preview as webPreviewUrl and webPreviewToken

Also keep the legacy previewUrl and previewToken keys, because SERVE_SIM
sessions still read them.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

* [build-tools] Add changelog entry

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

---------

Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
2026-10-05 16:23:24 +02:00
Szymon DziedzicandClaude Opus 5.5 4e432fa537 [build-tools] Upload the agent-device screenshot display rotation as artifact metadata (#4508)
* [build-tools] Upload the agent-device screenshot display rotation as artifact metadata

agent-device reports displayRotation on screenshot entries in its /artifacts
inventory. Read it and pass it to the device run session artifact upload as
metadata.displayRotation. An unknown value is dropped so it cannot block the
upload, and an artifact without the field uploads no metadata, as before.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* [changelog] Add entry for #4508

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
2026-10-05 15:49:47 +02:00
Szymon DziedzicandClaude Opus 5.5 817d06e66e [build-tools] Remove the eas/start_agent_device_remote_session step (#4518)
* [build-tools] Remove the eas/start_agent_device_remote_session step

The server now submits the single `eas/start_agent_device_session` step
for every agent-device session (expo/universe#31785), so remove the old
`eas/start_agent_device_remote_session` step.

- `runAgentDeviceRemoteSessionAsync` and the daemon helpers stay; the
  single step uses them. Its `launch` option goes away: only the old step
  passed serve-sim launch inputs, and the single step launches the app
  with simctl / adb.
- The tests that ran the shared session code through the old step now
  call `runAgentDeviceRemoteSessionAsync`, or run the single step with a
  mocked boot.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* [build-tools] Add changelog entry

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
2026-10-05 15:19:54 +02:00
Stanisław Chmiela 76a9d17ea7 [build-tools] Diagnose TestFlight group assignment failures (#4521)
## Why

Group assignment can fail after upload processing completes because the build needs export compliance information. The error should identify the Apple state and explain how to resolve it on the already-uploaded build.

Example: https://expo.dev/uuid/01a0f7f6-42df-7673-af23-da067af13847

## How

After any failed group assignment, call `verifyInternalBuildStateValidForGroupAssignmentAsync` to fetch `buildBetaDetail`, log the internal TestFlight state, and capture a Sentry event. Cache its promise so subsequent failures do not repeat the lookup, log, or capture. The function throws a specific error for missing or pending export compliance. Otherwise it returns, and each group retains its own assignment error and the existing internal-group guidance.

Capture all internal states before throwing. Include the state in the Sentry message title so states have separate message groups, with the build ID and original assignment error as context. Use UNKNOWN when the diagnostic lookup fails.

Continue trying other groups before failing the job. Successful assignments do not fetch the state. There is no polling loop.

## Test Plan

- All 32 metadata tests passed, including two failed assignments followed by a successful assignment with one shared lookup, log, and capture. Export-compliance failures share the rejection; other failures retain each group's original error.
- Build-tools typecheck, repository lint, formatting, and diff checks passed.
- No new live Apple test for this change.
2026-10-05 11:36:22 +02:00
Hirbod 8ce87bfa56 [build-tools] allow expo.test subdomains for simulator previews (#4480) 2026-10-03 14:31:52 +02:00
2b7e8454e3 [build-tools] Replace record-sim with serve-sim recording (#4474)
* [build-tools] Replace record-sim with serve-sim recording

Co-authored-by: Codex <[email protected]>
Co-Authored-By: Claude <[email protected]>

* [build-tools] Avoid failure warnings for saved iOS recordings
2026-10-02 15:33:19 -07:00
Stanisław Chmiela 962a2a733e [build-tools] Retry temporary Apple request failures safely (#4520)
* [build-tools] retry temporary Apple request failures safely

* [build-tools] distinguish complete assets from uploaded chunks

* [build-tools] document Apple request recovery

* [build-tools] use connection retry policy and isolate commit recovery

* [build-tools] simplify upload commit recovery and limit reset retries to GET

* [build-tools] accept object-only retry options

* [build-tools] define simple explicit retry options

* [build-tools] adapt commit tests to the key-based ASC client

* [build-tools] keep Apple retry changes in the unreleased changelog

* [build-tools] recover uncertain UPLOAD_COMPLETE commits

* [build-tools] require upload commit logger

* [build-tools] retry uncertain upload commits through the outer retry

* [build-tools] check upload state after the final uncertain commit

* [build-tools] use try catch and classify pending commits as system errors

* [build-tools] limit upload recovery to interrupted connections
2026-10-02 21:40:18 +00:00
Eliot Hertenstein 9e13c1e820 [eas-cli] Add resumable device login (#4502) 2026-10-02 14:37:02 -07:00
Expo CI cadb7b2d1e bump STAGING and PRODUCTION to 24.10.0 in cli-versions.json 2026-10-02 20:58:58 +00:00
Expo CI 718c82d8e1 update CHANGELOG.md for v24.10.0 2026-10-02 20:55:55 +00:00
59 changed files with 2743 additions and 2000 deletions

No files matched your search

+17 -1
View File
@@ -8,12 +8,26 @@ This is the log of notable changes to EAS CLI and related packages.
### 🎉 New features
- [eas-cli] Add `--network-capture` to `eas simulator` to record HTTP(S) traffic from apps on the device, and `--network-capture-field` to also keep headers, query values, or request and response bodies. ([#4308](https://github.com/expo/eas-cli/pull/4308) by [@gwdp](https://github.com/gwdp))
- [build-tools] Record iOS Simulator sessions through serve-sim. ([#4474](https://github.com/expo/eas-cli/pull/4474) by [@gwdp](https://github.com/gwdp))
- [eas-cli] Add browser device login with resumable commands for chat agents. ([#4502](https://github.com/expo/eas-cli/pull/4502) by [@eiiot](https://github.com/eiiot))
- [build-tools] Upload the display rotation agent-device reports for a screenshot as device run session artifact metadata. ([#4508](https://github.com/expo/eas-cli/pull/4508) by [@szdziedzic](https://github.com/szdziedzic))
### 🐛 Bug fixes
- [build-tools] Allow expo.test subdomains to open local and staging simulator previews. ([#4480](https://github.com/expo/eas-cli/pull/4480) by [@hirbod](https://github.com/hirbod))
- [build-tools] Retry App Store Connect connection failures and safely recover interrupted status reads and upload commits. ([#4520](https://github.com/expo/eas-cli/pull/4520) by [@sjchmiela](https://github.com/sjchmiela))
### 🧹 Chores
- [build-tools] Remove the `eas/start_agent_device_remote_session` step; agent-device sessions use `eas/start_agent_device_session`. ([#4518](https://github.com/expo/eas-cli/pull/4518) by [@szdziedzic](https://github.com/szdziedzic))
- [eas-cli] Report `eas-cli` as the request origin, and the coding agent when one is detected, when `eas simulator` creates a session, for analytics. ([#4523](https://github.com/expo/eas-cli/pull/4523) by [@sjkim-expo](https://github.com/sjkim-expo))
## [24.10.0](https://github.com/expo/eas-cli/releases/tag/v24.10.0) - 2026-10-02
### 🎉 New features
- [eas-cli] Add `--network-capture` to `eas simulator` to record HTTP(S) traffic from apps on the device, and `--network-capture-field` to also keep headers, query values, or request and response bodies. ([#4308](https://github.com/expo/eas-cli/pull/4308) by [@gwdp](https://github.com/gwdp))
## [24.9.0](https://github.com/expo/eas-cli/releases/tag/v24.9.0) - 2026-10-02
### 🎉 New features
@@ -42,6 +56,7 @@ This is the log of notable changes to EAS CLI and related packages.
- [eas-cli] Correct the `eas simulator:start --max-duration-minutes` help text: every plan can set a custom duration up to the plan's maximum, which is also the default. ([#4524](https://github.com/expo/eas-cli/pull/4524) by [@brentvatne](https://github.com/brentvatne))
- [eas-cli] Fix `eas workflow:validate` crashing with certain workflow schemas, and exit with a non-zero code when validation fails. ([#4507](https://github.com/expo/eas-cli/pull/4507) by [@yuvalgnessin-qz](https://github.com/yuvalgnessin-qz))
- [build-tools] Reuse App Store Connect tokens and refresh them before expiry so long uploads and processing waits remain authenticated. ([#4497](https://github.com/expo/eas-cli/pull/4497) by [@sjchmiela](https://github.com/sjchmiela))
- [build-tools] Explain TestFlight group assignment failures with the Apple build state and report unexpected readiness failures. ([#4521](https://github.com/expo/eas-cli/pull/4521) by [@sjchmiela](https://github.com/sjchmiela))
- [build-tools] Clear old Maestro CLI HTML reports before each test attempt so retries cannot upload an earlier report. ([#4409](https://github.com/expo/eas-cli/pull/4409) by [@sjchmiela](https://github.com/sjchmiela))
- [build-tools] Poll the web preview readiness every 250 ms instead of every second, so a session goes live sooner after its host is ready. ([#4496](https://github.com/expo/eas-cli/pull/4496) by [@gwdp](https://github.com/gwdp))
- [eas-cli] Keep the `eas simulator` session spinner on one line, so resizing the terminal no longer leaves copies of it behind. The `eas simulator:stop` command is now printed once before the spinner starts. ([#4488](https://github.com/expo/eas-cli/pull/4488) by [@szdziedzic](https://github.com/szdziedzic))
@@ -77,6 +92,7 @@ This is the log of notable changes to EAS CLI and related packages.
- [eas-cli] Stop creating `CLAUDE.md` in new apps; use `AGENTS.md` for agent instructions. ([#4446](https://github.com/expo/eas-cli/pull/4446) by [@davidmokos](https://github.com/davidmokos))
- Upgrade `typescript` to 6.0.3, `ts-jest` to 29.4.12, `@types/node` to 20.19.43, `gql.tada` to 1.11.3, and `https-proxy-agent` to 7.0.6. ([#4400](https://github.com/expo/eas-cli/pull/4400) by [@wschurman](https://github.com/wschurman))
- [build-tools] Use the website origin for simulator preview CORS and framing, and allow deploy previews and local website dev servers on staging. ([#4437](https://github.com/expo/eas-cli/pull/4437) by [@gwdp](https://github.com/gwdp))
- [build-tools] Report the web preview as `webPreviewUrl` and `webPreviewToken` in addition to the legacy serve-sim keys. ([#4467](https://github.com/expo/eas-cli/pull/4467) by [@szdziedzic](https://github.com/szdziedzic))
## [24.7.0](https://github.com/expo/eas-cli/releases/tag/v24.7.0) - 2026-09-16
+2 -2
View File
@@ -1,4 +1,4 @@
{
"STAGING": "24.9.0",
"PRODUCTION": "24.9.0"
"STAGING": "24.10.0",
"PRODUCTION": "24.10.0"
}
+1 -1
View File
@@ -1,5 +1,5 @@
{
"npmClient": "yarn",
"version": "24.10.0",
"version": "24.11.0",
"$schema": "node_modules/lerna/schemas/lerna-schema.json"
}
-1
View File
@@ -1,6 +1,5 @@
node_modules/
dist/
bin/record-sim
.secrets*
!.secrets.example
+1 -2
View File
@@ -1,6 +1,6 @@
{
"name": "@expo/build-tools",
"version": "24.9.0",
"version": "24.11.0",
"bugs": "https://github.com/expo/eas-cli/issues",
"license": "BUSL-1.1",
"author": "Expo <[email protected]>",
@@ -23,7 +23,6 @@
"watch": "tsc --watch --preserveWatchOutput",
"prebuild": "yarn gql",
"build": "tsc",
"build:record-sim": "mkdir -p bin && record_sim_bin_path=$(swift build -c release --package-path resources/record-sim --build-path resources/record-sim/.build --show-bin-path) && swift build -c release --package-path resources/record-sim --build-path resources/record-sim/.build && cp \"$record_sim_bin_path/record-sim\" bin/record-sim && chmod +x bin/record-sim",
"build:egress-guard": "resources/egress-guard/build.sh",
"test:egress-guard": "resources/egress-guard/tests/run-policy-tests.sh && resources/egress-guard/tests/run-guard-tests.sh",
"typecheck": "tsc",
@@ -1,4 +0,0 @@
.build/
.swiftpm/
DerivedData/
.DS_Store
@@ -1,28 +0,0 @@
// swift-tools-version: 5.9
import PackageDescription
let package = Package(
name: "record-sim",
platforms: [
.macOS(.v14),
],
products: [
.library(name: "RecordSim", targets: ["RecordSim"]),
.executable(name: "record-sim", targets: ["record-sim"]),
],
targets: [
.target(
name: "RecordSim",
linkerSettings: [
.linkedFramework("AVFoundation"),
.linkedFramework("CoreGraphics"),
.linkedFramework("CoreMedia"),
.linkedFramework("CoreVideo"),
.linkedFramework("IOSurface"),
.linkedFramework("UniformTypeIdentifiers"),
]
),
.executableTarget(name: "record-sim", dependencies: ["RecordSim"]),
]
)
@@ -1,79 +0,0 @@
# RecordSim
Small Swift library for non-exclusive iOS Simulator screen recording.
It implements one recording path:
1. Listen to SimRenderServer framebuffer callbacks.
2. Copy the live `IOSurface` immediately into an owned `CVPixelBuffer`.
3. Feed one continuous `AVAssetWriter`.
4. Emit Apple HLS/CMAF-style fragmented MP4 segments.
The output is suitable for uploading while recording. `session.json` contains
the ordered segment metadata needed to build a playlist later.
## Output
```text
session/
init.mp4
session.json
segments/
segment-000000.m4s
segment-000001.m4s
```
`init.mp4` is the fMP4 initialization segment. Individual `.m4s` files are not
standalone MP4 files. `session.json` stores the wall-clock timestamp for the
first video frame plus the metadata needed to build a playlist: HLS version,
target duration, media sequence, `initSegment`, and the ordered `segments` array
with file path and duration for each media segment.
## CLI
```bash
swift run record-sim \
--udid <BOOTED_SIMULATOR_UDID> \
--output /tmp/sim-recording \
--segment-duration 120 \
--fps 30 \
--bitrate 30000000 \
--codec h264
```
For sparse 2h sessions, `--segment-duration 120` is a reasonable default: about
60 media objects plus the init segment. The recorder appends a single duplicate
hold frame near each segment boundary when the simulator is idle, so long idle
periods still produce uploadable segments without encoding idle frames at 60fps.
Use `--segment-duration 0` to write one continuous `recording.mp4` instead of
`init.mp4` plus media segments.
Frame timestamps use monotonic host time, not wall-clock time. The recorder also
probes the framebuffer seed once per second; if the surface changes without
callbacks for 5 seconds, it captures that frame and rewires the private callback
registration.
## Library
```swift
let recorder = SimulatorRecorder(
configuration: SimulatorRecordingConfiguration(
deviceUDID: udid,
outputDirectory: sessionDirectory,
segmentDuration: 120
)
)
recorder.onSegment = { segment in
// Enqueue upload of segment.url.
}
try recorder.start()
try recorder.waitUntilFirstFrame()
// ...
let manifest = try recorder.stop()
```
`session.json` contains the precise wall-clock time for video PTS zero. In
segmented mode it also contains the init segment and ordered media segment
file/duration entries; in single-file mode it points at `recording.mp4`.
@@ -1,192 +0,0 @@
import Foundation
import IOSurface
import ObjectiveC
final class FramebufferDisplaySource {
struct SurfaceSnapshot {
let surface: IOSurface
let width: Int
let height: Int
let seed: UInt32
}
private let deviceUDID: String
private let callbackQueue: DispatchQueue
private let onFrame: () -> Void
private let onSurfaceChange: () -> Void
private var descriptors: [NSObject] = []
private var callbackUUIDs: [ObjectIdentifier: NSUUID] = [:]
private var retainedBlocks: [AnyObject] = []
private var ioClient: NSObject?
init(
deviceUDID: String,
callbackQueue: DispatchQueue,
onFrame: @escaping () -> Void,
onSurfaceChange: @escaping () -> Void
) {
self.deviceUDID = deviceUDID
self.callbackQueue = callbackQueue
self.onFrame = onFrame
self.onSurfaceChange = onSurfaceChange
}
func start() throws {
try PrivateSimulatorFrameworks.load()
guard let device = SimulatorDeviceLookup.find(udid: deviceUDID) else {
throw RecorderError.make(1, "Simulator \(deviceUDID) not found")
}
let state = device.value(forKey: "stateString") as? String ?? "unknown"
guard state == "Booted" else {
throw RecorderError.make(2, "Simulator \(deviceUDID) is not booted (state: \(state))")
}
guard
let io = device.perform(NSSelectorFromString("io"))?.takeUnretainedValue() as? NSObject
else {
throw RecorderError.make(3, "Failed to get simulator IO client")
}
ioClient = io
try wireUpFramebuffer()
}
func stop() {
unregisterCallbacks()
descriptors.removeAll()
retainedBlocks.removeAll()
ioClient = nil
}
func surfaceSnapshot() -> SurfaceSnapshot? {
let surfaceSelector = NSSelectorFromString("framebufferSurface")
var bestSnapshot: SurfaceSnapshot?
var bestArea = 0
for descriptor in descriptors {
guard let surfaceObject = descriptor.perform(surfaceSelector)?.takeUnretainedValue()
else {
continue
}
let surface = unsafeBitCast(surfaceObject, to: IOSurface.self)
let width = IOSurfaceGetWidth(surface)
let height = IOSurfaceGetHeight(surface)
let area = width * height
if area > bestArea {
bestSnapshot = SurfaceSnapshot(
surface: surface,
width: width,
height: height,
seed: IOSurfaceGetSeed(surface)
)
bestArea = area
}
}
return bestSnapshot
}
func rewireFramebuffer() throws {
try wireUpFramebuffer()
}
private func wireUpFramebuffer() throws {
guard let io = ioClient else {
throw RecorderError.make(3, "No simulator IO client")
}
io.perform(NSSelectorFromString("updateIOPorts"))
let nextDescriptors = try findFramebufferDescriptors(io: io)
unregisterCallbacks()
descriptors = nextDescriptors
retainedBlocks.removeAll()
do {
for descriptor in descriptors {
try registerCallbacks(descriptor: descriptor)
}
} catch {
unregisterCallbacks()
descriptors.removeAll()
retainedBlocks.removeAll()
throw error
}
}
private func findFramebufferDescriptors(io: NSObject) throws -> [NSObject] {
guard let ports = io.value(forKey: "deviceIOPorts") as? [NSObject] else {
throw RecorderError.make(4, "Failed to read simulator IO ports")
}
let portIdentifierSelector = NSSelectorFromString("portIdentifier")
let descriptorSelector = NSSelectorFromString("descriptor")
let surfaceSelector = NSSelectorFromString("framebufferSurface")
var candidates: [NSObject] = []
for port in ports {
guard port.responds(to: portIdentifierSelector),
let portIdentifier = port.perform(portIdentifierSelector)?.takeUnretainedValue(),
"\(portIdentifier)" == "com.apple.framebuffer.display",
port.responds(to: descriptorSelector),
let descriptor = port.perform(descriptorSelector)?.takeUnretainedValue()
as? NSObject,
descriptor.responds(to: surfaceSelector)
else {
continue
}
candidates.append(descriptor)
}
if candidates.isEmpty {
throw RecorderError.make(5, "No framebuffer display descriptor found")
}
return candidates
}
private func registerCallbacks(descriptor: NSObject) throws {
let selector = NSSelectorFromString(
"registerScreenCallbacksWithUUID:callbackQueue:frameCallback:surfacesChangedCallback:propertiesChangedCallback:"
)
guard descriptor.responds(to: selector) else {
throw RecorderError.make(6, "Framebuffer descriptor does not support screen callbacks")
}
guard let msgSendPointer = dlsym(UnsafeMutableRawPointer(bitPattern: -2), "objc_msgSend")
else {
throw RecorderError.make(7, "objc_msgSend not found")
}
typealias MsgSend =
@convention(c) (
AnyObject, Selector, AnyObject, AnyObject, AnyObject, AnyObject, AnyObject
) -> Void
let msgSend = unsafeBitCast(msgSendPointer, to: MsgSend.self)
let uuid = NSUUID()
callbackUUIDs[ObjectIdentifier(descriptor)] = uuid
let frameCallback: @convention(block) () -> Void = { [weak self] in
self?.onFrame()
}
let surfacesCallback: @convention(block) (AnyObject?, AnyObject?) -> Void = {
[weak self] _, _ in
self?.onSurfaceChange()
}
let propertiesCallback: @convention(block) () -> Void = {}
retainedBlocks.append(frameCallback as AnyObject)
retainedBlocks.append(surfacesCallback as AnyObject)
retainedBlocks.append(propertiesCallback as AnyObject)
msgSend(
descriptor,
selector,
uuid,
callbackQueue as AnyObject,
frameCallback as AnyObject,
surfacesCallback as AnyObject,
propertiesCallback as AnyObject
)
}
private func unregisterCallbacks() {
let selector = NSSelectorFromString("unregisterScreenCallbacksWithUUID:")
for descriptor in descriptors {
if let uuid = callbackUUIDs[ObjectIdentifier(descriptor)],
descriptor.responds(to: selector)
{
descriptor.perform(selector, with: uuid)
}
}
callbackUUIDs.removeAll()
}
}
@@ -1,96 +0,0 @@
import Foundation
import ObjectiveC
enum XcodeDeveloperDirectory {
static let current: String = {
if let developerDir = ProcessInfo.processInfo.environment["DEVELOPER_DIR"],
!developerDir.isEmpty
{
return developerDir
}
let fallback = "/Applications/Xcode.app/Contents/Developer"
let pipe = Pipe()
let process = Process()
process.executableURL = URL(fileURLWithPath: "/usr/bin/xcode-select")
process.arguments = ["-p"]
process.standardOutput = pipe
do {
try process.run()
} catch {
return fallback
}
process.waitUntilExit()
return String(data: pipe.fileHandleForReading.readDataToEndOfFile(), encoding: .utf8)?
.trimmingCharacters(in: .whitespacesAndNewlines) ?? fallback
}()
}
enum PrivateSimulatorFrameworks {
static func load() throws {
try loadResult.get()
}
private static let loadResult: Result<Void, Error> = {
let developerDir = XcodeDeveloperDirectory.current
let candidates = [
"/Library/Developer/PrivateFrameworks/CoreSimulator.framework/CoreSimulator",
"\(developerDir)/Library/PrivateFrameworks/CoreSimulator.framework/CoreSimulator",
"\(developerDir)/../SharedFrameworks/SimulatorKit.framework/SimulatorKit",
"\(developerDir)/Library/PrivateFrameworks/SimulatorKit.framework/SimulatorKit",
]
var failures: [String] = []
for path in candidates {
if dlopen(path, RTLD_NOW) == nil {
let message = dlerror().map { String(cString: $0) } ?? "unknown error"
failures.append("\(path): \(message)")
}
}
guard NSClassFromString("SimServiceContext") != nil else {
let tried = candidates.joined(separator: ", ")
let errors = failures.isEmpty ? "no dlopen errors" : failures.joined(separator: "; ")
return .failure(
RecorderError.make(
8,
"Failed to load CoreSimulator/SimulatorKit (tried: \(tried); errors: \(errors))"
)
)
}
return .success(())
}()
}
enum SimulatorDeviceLookup {
private static let context: NSObject? = {
guard let contextClass = NSClassFromString("SimServiceContext") as? NSObject.Type else {
return nil
}
let sharedSelector = NSSelectorFromString("sharedServiceContextForDeveloperDir:error:")
return contextClass.perform(
sharedSelector, with: XcodeDeveloperDirectory.current, with: nil)?
.takeUnretainedValue() as? NSObject
}()
static func find(udid: String) -> NSObject? {
guard let context else {
return nil
}
let deviceSetSelector = NSSelectorFromString("defaultDeviceSetWithError:")
guard
let deviceSet = context.perform(deviceSetSelector, with: nil)?
.takeUnretainedValue() as? NSObject
else {
return nil
}
guard let devices = deviceSet.value(forKey: "devices") as? [NSObject] else {
return nil
}
return devices.first {
($0.value(forKey: "UDID") as? NSUUID)?.uuidString == udid
}
}
static func stateString(udid: String) -> String? {
find(udid: udid)?.value(forKey: "stateString") as? String
}
}
@@ -1,77 +0,0 @@
import Foundation
public enum RecorderCodec: String, Codable, Sendable {
case h264
case hevc
}
public struct SimulatorRecordingConfiguration: Sendable {
public var deviceUDID: String
public var outputDirectory: URL
public var fps: Int
public var bitrate: Int
public var codec: RecorderCodec
public var segmentDuration: TimeInterval
public var maxPendingFrames: Int
public var overwrite: Bool
public init(
deviceUDID: String,
outputDirectory: URL,
fps: Int = 30,
bitrate: Int = 30_000_000,
codec: RecorderCodec = .h264,
segmentDuration: TimeInterval = 120,
maxPendingFrames: Int = 8,
overwrite: Bool = true
) {
self.deviceUDID = deviceUDID
self.outputDirectory = outputDirectory
self.fps = fps
self.bitrate = bitrate
self.codec = codec
self.segmentDuration = segmentDuration
self.maxPendingFrames = maxPendingFrames
self.overwrite = overwrite
}
}
public enum SegmentKind: String, Codable, Sendable {
case initialization
case media
}
public struct SegmentOutput: Sendable {
public let kind: SegmentKind
public let relativePath: String
public let url: URL
public let byteCount: Int
public let durationSeconds: Double?
}
public struct MediaSegmentRecord: Codable, Sendable {
public let file: String
public let durationSeconds: Double
}
public struct FirstFrameWallClock: Codable, Sendable {
public let unixMs: Int64
public let iso8601: String
}
public enum RecordingFinalizationStage: Sendable {
case captureStopped
case videoSaved
}
public struct RecordingManifest: Codable, Sendable {
public let firstFrameWallClock: FirstFrameWallClock
public let width: Int
public let height: Int
public let hlsVersion: Int?
public let hlsTargetDurationSeconds: Int?
public let hlsMediaSequence: Int?
public let recording: String?
public let initSegment: String?
public let segments: [MediaSegmentRecord]
}
@@ -1,140 +0,0 @@
import AVFoundation
import Foundation
final class RecordingOutputWriter: NSObject, AVAssetWriterDelegate {
private let rootDirectory: URL
private let segmentsDirectory: URL
let singleRecordingURL: URL
private let stateQueue = DispatchQueue(label: "record-sim.segment-writer")
private let notificationQueue = DispatchQueue(label: "record-sim.segment-notifications")
private let onSegment: ((SegmentOutput) -> Void)?
private var initSegmentPath: String?
private var segments: [MediaSegmentRecord] = []
private var nextMediaSegmentIndex = 0
private var firstError: Error?
init(rootDirectory: URL, onSegment: ((SegmentOutput) -> Void)?) {
self.rootDirectory = rootDirectory
self.segmentsDirectory = rootDirectory.appendingPathComponent("segments", isDirectory: true)
self.singleRecordingURL = rootDirectory.appendingPathComponent("recording.mp4")
self.onSegment = onSegment
super.init()
}
func prepare(overwrite: Bool, segmented: Bool) throws {
if overwrite {
try? FileManager.default.removeItem(at: rootDirectory)
}
try FileManager.default.createDirectory(at: rootDirectory, withIntermediateDirectories: true)
if segmented {
try FileManager.default.createDirectory(at: segmentsDirectory, withIntermediateDirectories: true)
}
}
var error: Error? {
stateQueue.sync { firstError }
}
func assetWriter(
_ writer: AVAssetWriter,
didOutputSegmentData segmentData: Data,
segmentType: AVAssetSegmentType,
segmentReport: AVAssetSegmentReport?
) {
var notification: SegmentOutput?
stateQueue.sync {
if firstError != nil {
return
}
do {
switch segmentType {
case .initialization:
let relativePath = "init.mp4"
let url = rootDirectory.appendingPathComponent(relativePath)
try segmentData.write(to: url, options: .atomic)
initSegmentPath = relativePath
notification = SegmentOutput(
kind: .initialization,
relativePath: relativePath,
url: url,
byteCount: segmentData.count,
durationSeconds: nil
)
case .separable:
let index = nextMediaSegmentIndex
let relativePath = String(format: "segments/segment-%06d.m4s", index)
let url = rootDirectory.appendingPathComponent(relativePath)
try segmentData.write(to: url, options: .atomic)
let videoReport = segmentReport?.trackReports.first(where: { $0.mediaType == .video })
let duration = videoReport.flatMap { timeSeconds($0.duration) } ?? 0
let record = MediaSegmentRecord(
file: relativePath,
durationSeconds: duration
)
nextMediaSegmentIndex += 1
segments.append(record)
notification = SegmentOutput(
kind: .media,
relativePath: relativePath,
url: url,
byteCount: segmentData.count,
durationSeconds: duration
)
default:
break
}
} catch {
firstError = error
}
}
if let notification, let onSegment {
notificationQueue.async {
onSegment(notification)
}
}
}
@discardableResult
func writeManifest(
configuration: SimulatorRecordingConfiguration,
firstFrameWallClock: Date,
width: Int,
height: Int
) throws -> RecordingManifest {
try stateQueue.sync {
if let firstError {
throw firstError
}
let segmented = configuration.segmentDuration > 0
if segmented, initSegmentPath == nil {
throw RecorderError.make(50, "Missing initialization segment")
}
if segmented, segments.isEmpty {
throw RecorderError.make(51, "No media segments were written")
}
let targetDuration = segmented
? max(1, Int(ceil(segments.map(\.durationSeconds).max() ?? configuration.segmentDuration)))
: nil
let manifest = RecordingManifest(
firstFrameWallClock: FirstFrameWallClock(
unixMs: unixMs(firstFrameWallClock),
iso8601: iso8601(firstFrameWallClock)
),
width: width,
height: height,
hlsVersion: segmented ? 7 : nil,
hlsTargetDurationSeconds: targetDuration,
hlsMediaSequence: segmented ? 0 : nil,
recording: segmented ? nil : "recording.mp4",
initSegment: segmented ? initSegmentPath : nil,
segments: segments
)
let data = try JSONEncoder.prettySorted.encode(manifest)
try data.write(to: rootDirectory.appendingPathComponent("session.json"), options: .atomic)
return manifest
}
}
}
@@ -1,644 +0,0 @@
import AVFoundation
import CoreVideo
import Foundation
import IOSurface
import UniformTypeIdentifiers
public final class SimulatorRecorder {
public var onSegment: ((SegmentOutput) -> Void)?
public var onSimulatorStopped: ((String) -> Void)?
public var onFinalizationStage: ((RecordingFinalizationStage) -> Void)?
private static let callbackStalenessTimeout: TimeInterval = 5
private static let firstFrameRewireInterval: TimeInterval = 1
private let configuration: SimulatorRecordingConfiguration
private let callbackQueue = DispatchQueue(
label: "record-sim.frame-callbacks", qos: .userInteractive)
private let writerQueue = DispatchQueue(label: "record-sim.asset-writer", qos: .userInitiated)
private let eventQueue = DispatchQueue(label: "record-sim.events", qos: .utility)
private let pendingLock = NSLock()
private var pendingFrames = 0
private var displaySource: FramebufferDisplaySource?
private var outputWriter: RecordingOutputWriter?
private var writer: AVAssetWriter?
private var input: AVAssetWriterInput?
private var adaptor: AVAssetWriterInputPixelBufferAdaptor?
private var boundaryTimer: DispatchSourceTimer?
private var monotonicClock = MonotonicClock()
private var firstAcceptedCaptureTime: CMTime?
private var firstAcceptedWallClock: Date?
private var recordingWidth: Int?
private var recordingHeight: Int?
private var lastPTS: CMTime?
private var lastSeed: UInt32?
private var lastFrameCallbackElapsed: TimeInterval?
private var lastFirstFrameRewireElapsed: TimeInterval = 0
private var lastAppendedPixelBuffer: CVPixelBuffer?
private var nextBoundaryElapsed: TimeInterval = 0
private var simulatorStoppedReason: String?
private var stopped = false
private var firstFrameReady = false
private let firstFrameSemaphore = DispatchSemaphore(value: 0)
private var firstError: Error?
public init(configuration: SimulatorRecordingConfiguration) {
self.configuration = configuration
}
public func start() throws {
try validateConfiguration()
let outputWriter = RecordingOutputWriter(
rootDirectory: configuration.outputDirectory,
onSegment: { [weak self] segment in
self?.onSegment?(segment)
})
try outputWriter.prepare(
overwrite: configuration.overwrite, segmented: configuration.segmentDuration > 0)
self.outputWriter = outputWriter
monotonicClock = MonotonicClock()
nextBoundaryElapsed = configuration.segmentDuration
lastFirstFrameRewireElapsed = 0
stopped = false
let source = FramebufferDisplaySource(
deviceUDID: configuration.deviceUDID,
callbackQueue: callbackQueue,
onFrame: { [weak self] in
self?.captureFrame(force: false, reason: .callback)
},
onSurfaceChange: { [weak self] in
self?.captureFrame(force: true, reason: .surfaceChange)
}
)
try source.start()
displaySource = source
startBoundaryTimer()
}
public func waitUntilFirstFrame(timeoutSeconds: TimeInterval = 15) throws {
let alreadyReady = try writerQueue.sync {
if let firstError {
throw firstError
}
return firstFrameReady
}
if alreadyReady {
return
}
if firstFrameSemaphore.wait(timeout: .now() + timeoutSeconds) == .timedOut {
throw RecorderError.make(20, "Timed out waiting for first frame")
}
try writerQueue.sync {
if let firstError {
throw firstError
}
if !firstFrameReady {
throw RecorderError.make(21, "Recorder stopped before first frame")
}
}
}
public func simulatorStopReason() -> String? {
callbackQueue.sync {
simulatorStoppedReason
}
}
@discardableResult
public func stop() throws -> RecordingManifest {
callbackQueue.sync {
stopped = true
boundaryTimer?.cancel()
boundaryTimer = nil
displaySource?.stop()
displaySource = nil
}
onFinalizationStage?(.captureStopped)
return try writerQueue.sync {
if let firstError {
throw firstError
}
guard let writer, let input, let outputWriter else {
let error = RecorderError.make(22, "No frames were captured")
firstError = error
signalFirstFrameReadyIfNeeded()
throw error
}
try appendTailFrameIfNeeded(finalPTS: monotonicClock.elapsedTime())
input.markAsFinished()
let semaphore = DispatchSemaphore(value: 0)
writer.finishWriting {
semaphore.signal()
}
if semaphore.wait(timeout: .now() + 60) == .timedOut {
throw RecorderError.make(23, "Timed out finishing AVAssetWriter")
}
if writer.status == .failed {
throw writer.error ?? RecorderError.make(24, "AVAssetWriter failed")
}
if let error = outputWriter.error {
throw error
}
onFinalizationStage?(.videoSaved)
guard let firstAcceptedWallClock else {
throw RecorderError.make(25, "Missing first frame wall-clock timestamp")
}
guard let recordingWidth, let recordingHeight else {
throw RecorderError.make(27, "Missing recording dimensions")
}
return try outputWriter.writeManifest(
configuration: configuration,
firstFrameWallClock: firstAcceptedWallClock,
width: recordingWidth,
height: recordingHeight
)
}
}
private enum CaptureReason {
case callback
case surfaceChange
case boundary
case healthProbe
}
private func validateConfiguration() throws {
guard !configuration.deviceUDID.isEmpty else {
throw RecorderError.make(30, "deviceUDID must not be empty")
}
guard configuration.fps > 0, configuration.fps <= 120 else {
throw RecorderError.make(31, "fps must be between 1 and 120")
}
guard configuration.bitrate > 0 else {
throw RecorderError.make(32, "bitrate must be positive")
}
guard configuration.segmentDuration >= 0 else {
throw RecorderError.make(33, "segmentDuration must be non-negative")
}
guard configuration.maxPendingFrames > 0 else {
throw RecorderError.make(34, "maxPendingFrames must be positive")
}
}
private func startBoundaryTimer() {
let timer = DispatchSource.makeTimerSource(queue: callbackQueue)
timer.schedule(deadline: .now() + 1, repeating: 1, leeway: .milliseconds(100))
timer.setEventHandler { [weak self] in
self?.probeSimulatorState()
self?.probeFramebufferHealth()
self?.appendBoundaryFrameIfNeeded()
}
timer.resume()
boundaryTimer = timer
}
private func appendBoundaryFrameIfNeeded() {
if stopped {
return
}
guard configuration.segmentDuration > 0 else {
return
}
writerQueue.sync {
guard firstAcceptedCaptureTime != nil else {
return
}
let currentPTSSeconds = CMTimeGetSeconds(
normalizedPresentationTime(for: monotonicClock.elapsedTime()))
guard currentPTSSeconds >= nextBoundaryElapsed else {
return
}
let boundary = nextBoundaryElapsed
guard let lastPTS else {
return
}
do {
if CMTimeGetSeconds(lastPTS) < boundary {
let boundaryPTS = CMTime(seconds: boundary, preferredTimescale: 1_000_000_000)
guard try appendHeldFrame(at: boundaryPTS) else {
return
}
}
nextBoundaryElapsed = boundary + configuration.segmentDuration
} catch {
firstError = error
signalFirstFrameReadyIfNeeded()
}
}
}
private func captureFrame(force: Bool, reason: CaptureReason) {
if stopped {
return
}
if reason == .callback {
lastFrameCallbackElapsed = monotonicClock.elapsedSeconds()
}
guard let snapshot = displaySource?.surfaceSnapshot() else {
return
}
let surface = snapshot.surface
let surfaceWidth = snapshot.width
let surfaceHeight = snapshot.height
guard surfaceWidth > 0, surfaceHeight > 0 else {
return
}
let seed = snapshot.seed
let matchesLastAppendedSeed = writerQueue.sync { lastSeed == seed }
if !force, matchesLastAppendedSeed {
return
}
guard reservePendingFrame() else {
return
}
let capturedAt = monotonicClock.elapsedTime()
let capturedAtWallClock = Date()
do {
let pixelBuffer = try copySurfaceToOwnedPixelBuffer(
surface,
width: surfaceWidth,
height: surfaceHeight
)
appendOwned(
pixelBuffer: pixelBuffer,
seed: seed,
capturedAt: capturedAt,
capturedAtWallClock: capturedAtWallClock
)
} catch {
releasePendingFrame()
writerQueue.async {
self.firstError = error
self.signalFirstFrameReadyIfNeeded()
}
}
}
private func probeFramebufferHealth() {
guard !stopped, let displaySource else {
return
}
let elapsed = monotonicClock.elapsedSeconds()
if !isFirstFrameReady(),
elapsed - lastFirstFrameRewireElapsed >= Self.firstFrameRewireInterval
{
lastFirstFrameRewireElapsed = elapsed
rewireFramebuffer()
captureFrame(force: true, reason: .healthProbe)
return
}
guard let snapshot = displaySource.surfaceSnapshot(),
snapshot.width > 0,
snapshot.height > 0
else {
rewireFramebuffer()
return
}
let observedSeed = snapshot.seed
let lastAppendedSeed = writerQueue.sync { lastSeed }
guard let lastAppendedSeed, observedSeed != lastAppendedSeed else {
return
}
let lastCallback = lastFrameCallbackElapsed ?? 0
if elapsed - lastCallback >= Self.callbackStalenessTimeout {
captureFrame(force: false, reason: .healthProbe)
rewireFramebuffer()
}
}
private func probeSimulatorState() {
guard !stopped, simulatorStoppedReason == nil else {
return
}
let state = SimulatorDeviceLookup.stateString(udid: configuration.deviceUDID) ?? "missing"
guard state != "Booted" else {
return
}
simulatorStoppedReason = state
let error = RecorderError.make(26, "Simulator stopped before first frame (state: \(state))")
writerQueue.async {
if self.firstError == nil, !self.firstFrameReady {
self.firstError = error
self.signalFirstFrameReadyIfNeeded()
}
}
let onSimulatorStopped = onSimulatorStopped
eventQueue.async {
onSimulatorStopped?(state)
}
}
private func rewireFramebuffer() {
guard let displaySource else {
return
}
do {
try displaySource.rewireFramebuffer()
} catch {
writerQueue.async {
self.firstError = error
self.signalFirstFrameReadyIfNeeded()
}
}
}
private func reservePendingFrame() -> Bool {
pendingLock.lock()
defer { pendingLock.unlock() }
if pendingFrames >= configuration.maxPendingFrames {
return false
}
pendingFrames += 1
return true
}
private func releasePendingFrame() {
pendingLock.lock()
pendingFrames = max(0, pendingFrames - 1)
pendingLock.unlock()
}
private func copySurfaceToOwnedPixelBuffer(
_ surface: IOSurface,
width: Int,
height: Int
) throws -> CVPixelBuffer {
let pool = try writerQueue.sync {
if let firstError {
throw firstError
}
if writer == nil {
try startWriter(width: width, height: height)
}
guard let pool = adaptor?.pixelBufferPool else {
throw RecorderError.make(40, "AVAssetWriter pixel buffer pool is unavailable")
}
return pool
}
var output: CVPixelBuffer?
let status = CVPixelBufferPoolCreatePixelBuffer(kCFAllocatorDefault, pool, &output)
guard status == kCVReturnSuccess, let destination = output else {
throw RecorderError.make(41, "Failed to allocate pixel buffer from pool: \(status)")
}
let destinationLockStatus = CVPixelBufferLockBaseAddress(destination, [])
guard destinationLockStatus == kCVReturnSuccess else {
throw RecorderError.make(50, "Failed to lock pixel buffer: \(destinationLockStatus)")
}
let surfaceLockStatus = IOSurfaceLock(surface, .readOnly, nil)
guard surfaceLockStatus == KERN_SUCCESS else {
CVPixelBufferUnlockBaseAddress(destination, [])
throw RecorderError.make(49, "Failed to lock IOSurface: \(surfaceLockStatus)")
}
defer {
IOSurfaceUnlock(surface, .readOnly, nil)
CVPixelBufferUnlockBaseAddress(destination, [])
}
guard let destinationAddress = CVPixelBufferGetBaseAddress(destination) else {
throw RecorderError.make(42, "Pixel buffer has no base address")
}
let sourceAddress = IOSurfaceGetBaseAddress(surface)
let sourceStride = IOSurfaceGetBytesPerRow(surface)
let destinationStride = CVPixelBufferGetBytesPerRow(destination)
let copyWidth = min(width, IOSurfaceGetWidth(surface), CVPixelBufferGetWidth(destination))
let copyHeight = min(
height, IOSurfaceGetHeight(surface), CVPixelBufferGetHeight(destination))
let copyBytes = min(copyWidth * 4, sourceStride, destinationStride)
for row in 0..<copyHeight {
memcpy(
destinationAddress.advanced(by: row * destinationStride),
sourceAddress.advanced(by: row * sourceStride),
copyBytes
)
}
return destination
}
private func appendOwned(
pixelBuffer: CVPixelBuffer,
seed: UInt32,
capturedAt: CMTime,
capturedAtWallClock: Date
) {
writerQueue.async {
defer { self.releasePendingFrame() }
if self.firstError != nil {
return
}
do {
try self.appendOnWriterQueue(
pixelBuffer: pixelBuffer,
seed: seed,
capturedAt: capturedAt,
capturedAtWallClock: capturedAtWallClock
)
} catch {
self.firstError = error
self.signalFirstFrameReadyIfNeeded()
}
}
}
private func appendOnWriterQueue(
pixelBuffer: CVPixelBuffer,
seed: UInt32,
capturedAt: CMTime,
capturedAtWallClock: Date
) throws {
guard let writer, let input, let adaptor else {
throw RecorderError.make(43, "AVAssetWriter is not initialized")
}
let isFirstFrame = firstAcceptedCaptureTime == nil
let pts = isFirstFrame ? .zero : normalizedPresentationTime(for: capturedAt)
if let lastPTS, CMTimeCompare(pts, lastPTS) <= 0 {
return
}
guard try appendPendingBoundaryFrames(before: pts) else {
return
}
guard input.isReadyForMoreMediaData else {
return
}
if !adaptor.append(pixelBuffer, withPresentationTime: pts) {
throw writer.error ?? RecorderError.make(44, "Failed to append pixel buffer")
}
if isFirstFrame {
firstAcceptedCaptureTime = capturedAt
firstAcceptedWallClock = capturedAtWallClock
}
lastPTS = pts
lastSeed = seed
lastAppendedPixelBuffer = pixelBuffer
signalFirstFrameReadyIfNeeded()
}
private func appendTailFrameIfNeeded(finalPTS: CMTime) throws {
let deadline = Date().addingTimeInterval(5)
while true {
if try appendTailFrame(finalPTS: finalPTS) {
return
}
if Date() >= deadline {
throw RecorderError.make(45, "Timed out waiting to append tail frame")
}
Thread.sleep(forTimeInterval: 0.05)
}
}
private func appendTailFrame(finalPTS: CMTime) throws -> Bool {
let pts = normalizedPresentationTime(for: finalPTS)
return try appendHeldFrame(at: pts)
}
private func appendPendingBoundaryFrames(before pts: CMTime) throws -> Bool {
guard configuration.segmentDuration > 0,
firstAcceptedCaptureTime != nil
else {
return true
}
while CMTimeGetSeconds(pts) > nextBoundaryElapsed {
if let lastPTS, CMTimeGetSeconds(lastPTS) >= nextBoundaryElapsed {
nextBoundaryElapsed += configuration.segmentDuration
continue
}
let boundaryPTS = CMTime(
seconds: nextBoundaryElapsed, preferredTimescale: 1_000_000_000)
guard try appendHeldFrame(at: boundaryPTS) else {
return false
}
nextBoundaryElapsed += configuration.segmentDuration
}
return true
}
private func appendHeldFrame(at pts: CMTime) throws -> Bool {
guard let input,
let adaptor,
let lastPTS,
let lastAppendedPixelBuffer
else {
return true
}
if CMTimeCompare(pts, lastPTS) <= 0 {
return true
}
guard input.isReadyForMoreMediaData else {
return false
}
if !adaptor.append(lastAppendedPixelBuffer, withPresentationTime: pts) {
throw writer?.error ?? RecorderError.make(45, "Failed to append held frame")
}
self.lastPTS = pts
return true
}
private func normalizedPresentationTime(for capturedAt: CMTime) -> CMTime {
let start = firstAcceptedCaptureTime ?? capturedAt
return CMTimeSubtract(capturedAt, start)
}
private func startWriter(width: Int, height: Int) throws {
guard let outputWriter else {
throw RecorderError.make(46, "Missing output writer")
}
let writer: AVAssetWriter
if configuration.segmentDuration > 0 {
writer = AVAssetWriter(contentType: .mpeg4Movie)
writer.delegate = outputWriter
writer.outputFileTypeProfile = .mpeg4AppleHLS
writer.preferredOutputSegmentInterval = CMTime(
seconds: configuration.segmentDuration,
preferredTimescale: 600
)
} else {
writer = try AVAssetWriter(outputURL: outputWriter.singleRecordingURL, fileType: .mp4)
}
writer.initialSegmentStartTime = .zero
let input = AVAssetWriterInput(
mediaType: .video,
outputSettings: assetWriterSettings(width: width, height: height)
)
input.expectsMediaDataInRealTime = true
let attributes: [String: Any] = [
kCVPixelBufferPixelFormatTypeKey as String: kCVPixelFormatType_32BGRA,
kCVPixelBufferWidthKey as String: width,
kCVPixelBufferHeightKey as String: height,
kCVPixelBufferIOSurfacePropertiesKey as String: [:],
]
let adaptor = AVAssetWriterInputPixelBufferAdaptor(
assetWriterInput: input,
sourcePixelBufferAttributes: attributes
)
guard writer.canAdd(input) else {
throw RecorderError.make(47, "AVAssetWriter cannot add video input")
}
writer.add(input)
guard writer.startWriting() else {
throw writer.error ?? RecorderError.make(48, "AVAssetWriter failed to start")
}
writer.startSession(atSourceTime: .zero)
self.writer = writer
self.input = input
self.adaptor = adaptor
recordingWidth = width
recordingHeight = height
}
private func assetWriterSettings(width: Int, height: Int) -> [String: Any] {
let codecType: AVVideoCodecType = configuration.codec == .hevc ? .hevc : .h264
var compression: [String: Any] = [
AVVideoAverageBitRateKey: configuration.bitrate,
AVVideoExpectedSourceFrameRateKey: configuration.fps,
AVVideoAllowFrameReorderingKey: true,
]
if configuration.segmentDuration > 0 {
compression[AVVideoMaxKeyFrameIntervalKey] = max(
1,
Int((configuration.segmentDuration * Double(configuration.fps)).rounded())
)
}
if configuration.codec == .h264 {
compression[AVVideoProfileLevelKey] = AVVideoProfileLevelH264HighAutoLevel
}
return [
AVVideoCodecKey: codecType,
AVVideoWidthKey: width,
AVVideoHeightKey: height,
AVVideoCompressionPropertiesKey: compression,
]
}
private func signalFirstFrameReadyIfNeeded() {
if firstFrameReady {
return
}
firstFrameReady = true
firstFrameSemaphore.signal()
}
private func isFirstFrameReady() -> Bool {
writerQueue.sync {
firstFrameReady
}
}
}
@@ -1,60 +0,0 @@
import CoreMedia
import Foundation
enum RecorderError {
static func make(_ code: Int, _ message: String) -> NSError {
NSError(
domain: "RecordSim",
code: code,
userInfo: [NSLocalizedDescriptionKey: message]
)
}
}
extension JSONEncoder {
static var prettySorted: JSONEncoder {
let encoder = JSONEncoder()
encoder.outputFormatting = [.prettyPrinted, .sortedKeys]
return encoder
}
}
func timeSeconds(_ time: CMTime) -> Double? {
guard time.isValid, !time.isIndefinite, !time.isNegativeInfinity, !time.isPositiveInfinity else {
return nil
}
return CMTimeGetSeconds(time)
}
func iso8601(_ date: Date) -> String {
let formatter = ISO8601DateFormatter()
formatter.formatOptions = [.withInternetDateTime, .withFractionalSeconds]
formatter.timeZone = TimeZone(secondsFromGMT: 0)
return formatter.string(from: date)
}
func unixMs(_ date: Date) -> Int64 {
Int64((date.timeIntervalSince1970 * 1000).rounded())
}
struct MonotonicClock {
private static let timescale: CMTimeScale = 1_000_000_000
let startedAtUptimeNanoseconds: UInt64
init() {
startedAtUptimeNanoseconds = DispatchTime.now().uptimeNanoseconds
}
func elapsedSeconds() -> TimeInterval {
TimeInterval(elapsedNanoseconds()) / TimeInterval(Self.timescale)
}
func elapsedTime() -> CMTime {
CMTime(value: CMTimeValue(elapsedNanoseconds()), timescale: Self.timescale)
}
private func elapsedNanoseconds() -> UInt64 {
DispatchTime.now().uptimeNanoseconds - startedAtUptimeNanoseconds
}
}
@@ -1,159 +0,0 @@
import Foundation
import RecordSim
struct CLIOptions {
var udid: String?
var output: String?
var segmentDuration: TimeInterval = 120
var fps = 30
var bitrate = 30_000_000
var codec: RecorderCodec = .h264
}
private var requestedStopSignal: CInt = 0
private var simulatorStopped: CInt = 0
private func handleStopSignal(_ signal: CInt) {
requestedStopSignal = signal
}
private func isStopRequested() -> Bool {
requestedStopSignal != 0 || simulatorStopped != 0
}
func usage() -> String {
"""
Usage:
record-sim --udid <UDID> --output <session-dir> [options]
Options:
--segment-duration <seconds> Target fMP4 media segment duration. Use 0 for one MP4. Default: 120
--fps <fps> Expected source frame rate. Default: 30
--bitrate <bits/sec> AVAssetWriter average bitrate. Default: 30000000
--codec <h264|hevc> Video codec. Default: h264
-h, --help Show this help
"""
}
func parseOptions(_ args: [String]) throws -> CLIOptions {
var options = CLIOptions()
var i = 0
func next(_ flag: String) throws -> String {
i += 1
guard i < args.count else {
throw NSError(domain: "record-sim", code: 2, userInfo: [NSLocalizedDescriptionKey: "Missing value for \(flag)"])
}
return args[i]
}
while i < args.count {
let arg = args[i]
switch arg {
case "--udid", "-u":
options.udid = try next(arg)
case "--output", "--output-dir", "--out", "-o":
options.output = try next(arg)
case "--segment-duration":
options.segmentDuration = Double(try next(arg)) ?? -1
case "--fps":
options.fps = Int(try next(arg)) ?? 0
case "--bitrate":
options.bitrate = Int(try next(arg)) ?? 0
case "--codec":
let raw = try next(arg)
guard let codec = RecorderCodec(rawValue: raw) else {
throw NSError(domain: "record-sim", code: 2, userInfo: [NSLocalizedDescriptionKey: "--codec must be h264 or hevc"])
}
options.codec = codec
case "--help", "-h":
print(usage())
exit(0)
default:
throw NSError(domain: "record-sim", code: 2, userInfo: [NSLocalizedDescriptionKey: "Unknown argument: \(arg)"])
}
i += 1
}
guard options.udid != nil else {
throw NSError(domain: "record-sim", code: 2, userInfo: [NSLocalizedDescriptionKey: "Missing required --udid"])
}
guard options.output != nil else {
throw NSError(domain: "record-sim", code: 2, userInfo: [NSLocalizedDescriptionKey: "Missing required --output"])
}
guard options.segmentDuration >= 0 else {
throw NSError(domain: "record-sim", code: 2, userInfo: [NSLocalizedDescriptionKey: "--segment-duration must be non-negative"])
}
guard options.fps > 0, options.fps <= 120 else {
throw NSError(domain: "record-sim", code: 2, userInfo: [NSLocalizedDescriptionKey: "--fps must be between 1 and 120"])
}
guard options.bitrate > 0 else {
throw NSError(domain: "record-sim", code: 2, userInfo: [NSLocalizedDescriptionKey: "--bitrate must be positive"])
}
return options
}
do {
signal(SIGINT, handleStopSignal)
signal(SIGTERM, handleStopSignal)
let options = try parseOptions(Array(CommandLine.arguments.dropFirst()))
let configuration = SimulatorRecordingConfiguration(
deviceUDID: options.udid!,
outputDirectory: URL(fileURLWithPath: options.output!),
fps: options.fps,
bitrate: options.bitrate,
codec: options.codec,
segmentDuration: options.segmentDuration
)
let recorder = SimulatorRecorder(configuration: configuration)
recorder.onSimulatorStopped = { state in
simulatorStopped = 1
fputs("[record-sim] simulator stopped state=\(state); finalizing recording\n", stderr)
}
recorder.onFinalizationStage = { stage in
switch stage {
case .captureStopped:
fputs("[record-sim] screen capture stopped; saving final video\n", stderr)
case .videoSaved:
fputs("[record-sim] final video saved; writing recording details\n", stderr)
}
}
recorder.onSegment = { segment in
switch segment.kind {
case .initialization:
fputs("[record-sim] init \(segment.relativePath) bytes=\(segment.byteCount)\n", stderr)
case .media:
let duration = segment.durationSeconds ?? 0
fputs(String(format: "[record-sim] segment %@ duration=%.3fs bytes=%d\n", segment.relativePath, duration, segment.byteCount), stderr)
}
}
try recorder.start()
try recorder.waitUntilFirstFrame()
fputs("[record-sim] recording -> \(options.output!)\n", stderr)
while !isStopRequested() {
Thread.sleep(forTimeInterval: 0.1)
}
if requestedStopSignal != 0 {
let signalName = switch requestedStopSignal {
case SIGINT: "SIGINT"
case SIGTERM: "SIGTERM"
default: "signal \(requestedStopSignal)"
}
fputs("[record-sim] received \(signalName); finishing recording\n", stderr)
}
let manifest = try recorder.stop()
if let recording = manifest.recording {
fputs("[record-sim] done recording=\(recording)\n", stderr)
} else {
fputs("[record-sim] done segments=\(manifest.segments.count)\n", stderr)
}
fputs("[record-sim] manifest \(URL(fileURLWithPath: options.output!).appendingPathComponent("session.json").path)\n", stderr)
} catch {
fputs("error: \(error.localizedDescription)\n", stderr)
exit(1)
}
@@ -45,7 +45,6 @@ import { createSaveBuildCacheFunction } from './functions/saveBuildCache';
import { createRolloutPosthogFlagFunction } from './functions/rolloutPosthogFlag';
import { createSaveCacheFunction } from './functions/saveCache';
import { createSendSlackMessageFunction } from './functions/sendSlackMessage';
import { createStartAgentDeviceRemoteSessionBuildFunction } from './functions/startAgentDeviceRemoteSession';
import { createStartAgentDeviceSessionBuildFunction } from './functions/startAgentDeviceSession';
import { createStartAndroidEmulatorBuildFunction } from './functions/startAndroidEmulator';
import { createStartArgentRemoteSessionBuildFunction } from './functions/startArgentRemoteSession';
@@ -104,7 +103,6 @@ export function getEasFunctions(ctx: CustomBuildContext): BuildFunction[] {
generateGymfileFromTemplateFunction(),
runFastlaneFunction(),
parseXcactivitylogFunction(),
createStartAgentDeviceRemoteSessionBuildFunction(ctx),
createStartAgentDeviceSessionBuildFunction(ctx),
createStartArgentRemoteSessionBuildFunction(ctx),
createStartAppiumRemoteSessionBuildFunction(ctx),
@@ -16,7 +16,7 @@ import {
waitForDeviceRunSessionStoppedAsync,
waitForFileAsync,
} from '../../utils/remoteDeviceRunSession';
import { createStartAgentDeviceRemoteSessionBuildFunction } from '../startAgentDeviceRemoteSession';
import { createStartAgentDeviceSessionBuildFunction } from '../startAgentDeviceSession';
import { createStartAppiumRemoteSessionBuildFunction } from '../startAppiumRemoteSession';
jest.mock('@expo/turtle-spawn');
@@ -27,6 +27,14 @@ jest.mock('../../utils/deviceSessionHost');
jest.mock('../../utils/agentDeviceEvents');
jest.mock('../../utils/appiumEvents');
jest.mock('../../utils/agentDeviceArtifacts');
// The agent-device session step boots the emulator itself; this test covers the session host.
jest.mock('../startAndroidEmulator', () => ({
startAndroidEmulatorAsync: jest.fn().mockResolvedValue({
serialId: 'emulator-5554',
emulatorPromise: Promise.resolve(),
shouldAdjustAnimationScale: true,
}),
}));
jest.mock('../../utils/remoteDeviceRunSession', () => ({
...jest.requireActual('../../utils/remoteDeviceRunSession'),
spawnDetached: jest.fn(),
@@ -48,6 +56,15 @@ beforeEach(() => {
jest.clearAllMocks();
// The daemon package is external. No installation or daemon process runs in these tests.
jest.spyOn(fs, 'existsSync').mockReturnValue(true);
// Only the emulator logcat folder: the Appium step writes into its own temp folder.
const mkdtemp = fs.promises.mkdtemp.bind(fs.promises);
jest
.spyOn(fs.promises, 'mkdtemp')
.mockImplementation(async prefix =>
String(prefix).includes('eas-android-emulator-logcat-')
? '/tmp/eas-android-emulator-logcat-test'
: mkdtemp(prefix)
);
jest.mocked(spawn).mockResolvedValue({ stdout: '{}' } as never);
jest
.mocked(AndroidEmulatorUtils.getAttachedDevicesAsync)
@@ -89,7 +106,7 @@ afterEach(() => {
describe.each([
['Appium', createStartAppiumRemoteSessionBuildFunction],
['Agent Device', createStartAgentDeviceRemoteSessionBuildFunction],
['Agent Device', createStartAgentDeviceSessionBuildFunction],
] as const)('%s host ownership', (name, createFunction) => {
async function runAsync() {
const fn = createFunction({} as CustomBuildContext);
@@ -99,10 +116,10 @@ describe.each([
global: { runtimePlatform: BuildRuntimePlatform.LINUX },
} as unknown as BuildStepContext,
{
inputs: {
package_version: { value: undefined },
max_idle_time_minutes: { value: undefined },
},
// Inputs a step does not get default to undefined, like in a real step call.
inputs: new Proxy({} as Record<string, { value: unknown }>, {
get: (target, id: string) => target[id] ?? { value: undefined },
}),
outputs: {},
env: {
DEVICE_RUN_SESSION_ID: 'session-id',
@@ -18,7 +18,7 @@ import {
uploadRemoteSessionConfigAsync,
waitForDeviceRunSessionStoppedAsync,
} from '../../utils/remoteDeviceRunSession';
import { createStartAgentDeviceRemoteSessionBuildFunction } from '../startAgentDeviceRemoteSession';
import { createStartAgentDeviceSessionBuildFunction } from '../startAgentDeviceSession';
import { createStartAppiumRemoteSessionBuildFunction } from '../startAppiumRemoteSession';
import { createStartArgentRemoteSessionBuildFunction } from '../startArgentRemoteSession';
import { createStartWebPreviewRemoteSessionBuildFunction } from '../startWebPreviewRemoteSession';
@@ -29,6 +29,12 @@ jest.mock('../../../utils/processes');
jest.mock('../../../utils/turtleFetch');
jest.mock('../../utils/deviceSessionHost');
jest.mock('../../utils/agentDeviceArtifacts');
// The agent-device session step boots the Simulator itself; this test covers local egress.
jest.mock('../startIosSimulator', () => ({
bootIosSimulatorAsync: jest
.fn()
.mockResolvedValue({ deviceIdentifier: 'sim', udid: 'sim', displayName: 'iPhone' }),
}));
jest.mock('../../utils/argentArtifacts');
jest.mock('../../utils/agentDeviceEvents', () => ({
startAgentDeviceEventCollectionAsync: async () => ({ stopAsync: jest.fn() }),
@@ -78,7 +84,7 @@ const fields = {
egressPort: handoff.port,
};
const controllers = [
['agent-device', createStartAgentDeviceRemoteSessionBuildFunction, 'agentDeviceRemoteSessionUrl'],
['agent-device', createStartAgentDeviceSessionBuildFunction, 'agentDeviceRemoteSessionUrl'],
['Appium', createStartAppiumRemoteSessionBuildFunction, 'appiumUrl'],
['Argent', createStartArgentRemoteSessionBuildFunction, 'toolsUrl'],
['web preview', createStartWebPreviewRemoteSessionBuildFunction, 'previewUrl'],
@@ -93,10 +99,10 @@ describe.each(controllers)('%s local egress', (_name, createFunction, controller
await createFunction(ctx).fn!(
{ logger, global: { runtimePlatform: BuildRuntimePlatform.DARWIN } } as BuildStepContext,
{
inputs: {
package_version: { value: undefined },
max_idle_time_minutes: { value: undefined },
},
// Inputs a step does not get default to undefined, like in a real step call.
inputs: new Proxy({} as Record<string, { value: unknown }>, {
get: (target, id: string) => target[id] ?? { value: undefined },
}),
outputs: {},
env,
signal,
@@ -1,10 +1,9 @@
import { BuildRuntimePlatform, type BuildStepContext } from '@expo/steps';
import { BuildRuntimePlatform } from '@expo/steps';
import spawn from '@expo/turtle-spawn';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { createGlobalContextMock } from '../../../__tests__/utils/context';
import { type CustomBuildContext } from '../../../customBuildContext';
import { pollAgentDeviceArtifactsForUploadAsync } from '../../utils/agentDeviceArtifacts';
import { startAgentDeviceEventCollectionAsync } from '../../utils/agentDeviceEvents';
@@ -21,10 +20,7 @@ import {
waitForFileAsync,
} from '../../utils/remoteDeviceRunSession';
import { type StartupTasks, createStartupTasks } from '../../utils/startupTasks';
import {
createStartAgentDeviceRemoteSessionBuildFunction,
runAgentDeviceRemoteSessionAsync,
} from '../startAgentDeviceRemoteSession';
import { runAgentDeviceRemoteSessionAsync } from '../startAgentDeviceRemoteSession';
// The daemon entry path and the state directory are resolved from the home directory when
// the module loads, so point it at a temp home we can populate.
@@ -70,28 +66,34 @@ const mockTunnelStopAsync = jest.fn();
const mockDaemonStopAsync = jest.fn();
const mockEventCollectionStopAsync = jest.fn();
type CaptureInputs = Parameters<typeof runAgentDeviceRemoteSessionAsync>[1]['capture'];
const NO_CAPTURE: CaptureInputs = { networkCapture: false, networkCaptureFields: [] };
/** Runs the session for a device that is already booted, with the app (if any) launched. */
async function runAsync(
logger: { info: jest.Mock; warn: jest.Mock },
logger: { info: jest.Mock; warn: jest.Mock; child: jest.Mock },
runtimePlatform: BuildRuntimePlatform,
launchInputs: Record<string, { value: unknown }> = {}
capture: CaptureInputs = NO_CAPTURE
): Promise<void> {
const buildFunction = createStartAgentDeviceRemoteSessionBuildFunction(ctx);
await buildFunction.fn!(
{ logger, global: { runtimePlatform } } as unknown as BuildStepContext,
{
inputs: {
package_version: { value: undefined },
max_idle_time_minutes: { value: undefined },
max_duration_seconds: { value: undefined },
...launchInputs,
},
outputs: {},
env: {},
} as never
);
await runAgentDeviceRemoteSessionAsync(ctx, {
env: {},
logger: logger as never,
runtimePlatform,
sessionEnv: {
deviceRunSessionId: 'device-run-session-id',
ngrokTunnelDomain: 'tunnel.example.com',
ngrokAuthtoken: 'ngrok-token',
},
packageVersion: undefined,
maxIdleTimeMinutes: undefined,
maxDurationSeconds: undefined,
capture,
tasks: createStartupTasks(logger as never),
device: { booted: Promise.resolve(), ready: Promise.resolve() },
});
}
describe('createStartAgentDeviceRemoteSessionBuildFunction orchestration', () => {
describe('runAgentDeviceRemoteSessionAsync orchestration', () => {
beforeEach(async () => {
jest.clearAllMocks();
@@ -141,7 +143,6 @@ describe('createStartAgentDeviceRemoteSessionBuildFunction orchestration', () =>
await runAsync(logger, BuildRuntimePlatform.LINUX);
expect(selectXcodeDeveloperDirectoryAsync).not.toHaveBeenCalled();
expect(startDeviceSessionHostAsync).toHaveBeenCalledWith(
ctx,
expect.objectContaining({ runtimePlatform: BuildRuntimePlatform.LINUX })
@@ -162,36 +163,12 @@ describe('createStartAgentDeviceRemoteSessionBuildFunction orchestration', () =>
expect(mockDaemonStopAsync).toHaveBeenCalledTimes(1);
});
it('hands the launch inputs to serve-sim and announces them on an iOS session', async () => {
it('hands network capture to serve-sim', async () => {
const logger = { info: jest.fn(), warn: jest.fn(), child: jest.fn().mockReturnThis() };
await runAsync(logger, BuildRuntimePlatform.DARWIN, {
launch_app_identifier: { value: 'host.exp.Exponent' },
launch_args: { value: ['-EXDevMenuIsOnboardingFinished', '1'] },
open_url: { value: 'exp://127.0.0.1:8081' },
});
expect(startDeviceSessionHostAsync).toHaveBeenCalledWith(
ctx,
expect.objectContaining({
runtimePlatform: BuildRuntimePlatform.DARWIN,
launchAppIdentifier: 'host.exp.Exponent',
launchArgs: ['-EXDevMenuIsOnboardingFinished', '1'],
openUrl: 'exp://127.0.0.1:8081',
})
);
expect(logger.info).toHaveBeenCalledWith(
'serve-sim will launch host.exp.Exponent with arguments ' +
'["-EXDevMenuIsOnboardingFinished","1"], then open exp://127.0.0.1:8081.'
);
});
it('hands network capture to serve-sim on an iOS session', async () => {
const logger = { info: jest.fn(), warn: jest.fn(), child: jest.fn().mockReturnThis() };
await runAsync(logger, BuildRuntimePlatform.DARWIN, {
network_capture: { value: true },
network_capture_fields: { value: ['header'] },
networkCapture: true,
networkCaptureFields: ['header'],
});
expect(startDeviceSessionHostAsync).toHaveBeenCalledWith(
@@ -200,26 +177,6 @@ describe('createStartAgentDeviceRemoteSessionBuildFunction orchestration', () =>
);
});
it('fails before starting the daemon when network capture is asked for on Android', async () => {
const logger = { info: jest.fn(), warn: jest.fn(), child: jest.fn().mockReturnThis() };
await expect(
runAsync(logger, BuildRuntimePlatform.LINUX, { network_capture: { value: true } })
).rejects.toThrow('records traffic through serve-sim on an iOS simulator');
expect(spawnDetached).not.toHaveBeenCalled();
});
it('fails before starting the daemon when a launch is asked for on Android', async () => {
const logger = { info: jest.fn(), warn: jest.fn(), child: jest.fn().mockReturnThis() };
await expect(
runAsync(logger, BuildRuntimePlatform.LINUX, {
launch_app_identifier: { value: 'host.exp.Exponent' },
})
).rejects.toThrow('runs on linux');
expect(spawnDetached).not.toHaveBeenCalled();
});
it('starts the session host without waiting for the agent-device daemon', async () => {
const logger = { info: jest.fn(), warn: jest.fn(), child: jest.fn().mockReturnThis() };
const sessionHost = await jest.mocked(startDeviceSessionHostAsync).getMockImplementation()!(
@@ -323,8 +280,7 @@ describe('createStartAgentDeviceRemoteSessionBuildFunction orchestration', () =>
packageVersion: undefined,
maxIdleTimeMinutes: undefined,
maxDurationSeconds: undefined,
launch: {},
capture: { networkCapture: false, networkCaptureFields: [] },
capture: NO_CAPTURE,
tasks,
device: typeof device === 'function' ? device(tasks) : device,
});
@@ -415,13 +371,4 @@ describe('createStartAgentDeviceRemoteSessionBuildFunction orchestration', () =>
expect(mockPreviewStopAsync).toHaveBeenCalledTimes(1);
});
});
it('declares the launch inputs', () => {
const buildFunction = createStartAgentDeviceRemoteSessionBuildFunction(ctx);
const globalCtx = createGlobalContextMock();
expect(
buildFunction.inputProviders?.map(provider => provider(globalCtx, 'Test step').id)
).toEqual(expect.arrayContaining(['launch_app_identifier', 'launch_args', 'open_url']));
});
});
@@ -85,8 +85,10 @@ describe(createStartWebPreviewRemoteSessionBuildFunction, () => {
expect(uploadRemoteSessionConfigAsync).toHaveBeenCalledWith(
expect.objectContaining({
remoteConfig: {
webPreviewUrl: 'https://expo.dev/simulator-preview/preview-id',
previewUrl: 'https://expo.dev/simulator-preview/preview-id',
previewApiUrl: 'https://web-preview.example.test',
webPreviewToken: 'tok-1',
previewToken: 'tok-1',
},
})
@@ -132,6 +134,7 @@ describe(createStartWebPreviewRemoteSessionBuildFunction, () => {
ctx,
deviceRunSessionId: 'device-run-session-id',
remoteConfig: {
webPreviewUrl: 'https://expo.dev/simulator-preview/preview-id',
previewUrl: 'https://expo.dev/simulator-preview/preview-id',
previewApiUrl: 'https://web-preview.example.test',
},
@@ -3,6 +3,7 @@ import { generateKeyPairSync } from 'node:crypto';
import { createGlobalContextMock } from '../../../__tests__/utils/context';
import { createMockLogger } from '../../../__tests__/utils/logger';
import { Sentry } from '../../../sentry';
import { AscApiClient } from '../../utils/ios/AscApiClient';
import {
createUpdateTestFlightMetadataBuildFunction,
@@ -10,6 +11,7 @@ import {
} from '../updateTestFlightMetadata';
jest.unmock('node-fetch');
jest.mock('../../../sentry', () => ({ Sentry: { capture: jest.fn() } }));
const { privateKey } = generateKeyPairSync('ec', { namedCurve: 'prime256v1' });
const key = { keyId: 'TESTKEY', privateKey };
@@ -56,8 +58,21 @@ function mockAssignedGroups(ids: string[] = []): void {
.reply(200, { data: ids.map(id => ({ id })) });
}
function mockInternalBuildState(state: string | null = 'READY_FOR_BETA_TESTING'): void {
api()
.get('/v1/builds/build/buildBetaDetail')
.query(true)
.reply(
state ? 200 : 503,
state
? { data: { id: 'detail', attributes: { internalBuildState: state } } }
: { errors: [{ code: 'UNAVAILABLE' }] }
);
}
beforeAll(() => nock.disableNetConnect());
beforeEach(() => {
jest.mocked(Sentry.capture).mockClear();
options.logger = createMockLogger();
options.client = new AscApiClient({ key });
});
@@ -378,6 +393,7 @@ it('attempts all localization changes and reports every failed update', async ()
it.each([1, 2])('explains automatic internal group rejection (%s errors)', async errorCount => {
mockBuild();
mockInternalBuildState();
mockAssignedGroups();
api()
.get('/v1/betaGroups')
@@ -438,6 +454,7 @@ it('tries group assignment if the changelog update fails', async () => {
it('reports both write failures', async () => {
mockBuild();
mockInternalBuildState();
mockAssignedGroups();
api()
.get('/v1/betaGroups')
@@ -459,6 +476,7 @@ it.each([false, true])(
'attempts all groups and reports assignment errors (multiple: %s)',
async multiple => {
mockBuild();
mockInternalBuildState();
mockAssignedGroups();
api()
.get('/v1/betaGroups')
@@ -484,7 +502,7 @@ it.each([false, true])(
.reply(204);
await expect(
updateTestFlightMetadataAsync({ ...options, changelog: '', groups: ['A', 'B', 'C'] })
).rejects.toThrow(multiple ? /FIRST_GROUP_FAILED.*Partial assignment/ : 'Partial assignment');
).rejects.toThrow(multiple ? 'FIRST_GROUP_FAILED' : 'Partial assignment');
expect(options.logger.error).toHaveBeenCalledWith(
expect.stringContaining('Group "B" (B): assignment failed.')
);
@@ -531,3 +549,73 @@ it('propagates Apple write failures', async () => {
.reply(403, { errors: [{ code: 'FORBIDDEN', detail: 'Not allowed' }] });
await expect(updateTestFlightMetadataAsync(options)).rejects.toThrow('403');
});
it.each([
['MISSING_EXPORT_COMPLIANCE', 'Complete the export compliance questions'],
['IN_EXPORT_COMPLIANCE_REVIEW', 'Wait for Apple to approve'],
['EXPIRED', 'ASSIGNMENT_FAILED'],
['PROCESSING', 'ASSIGNMENT_FAILED'],
['READY_FOR_BETA_TESTING', 'ASSIGNMENT_FAILED'],
['IN_BETA_TESTING', 'ASSIGNMENT_FAILED'],
[null, 'ASSIGNMENT_FAILED'],
] as const)(
'shares one diagnostic lookup across failed assignments (state: %s)',
async (state, guidance) => {
mockBuild();
mockAssignedGroups();
api()
.get('/v1/betaGroups')
.query({ 'filter[app]': 'app', limit: '200' })
.reply(200, {
data: ['A', 'B', 'C'].map(id => ({ id, attributes: { name: id } })),
});
api()
.post('/v1/builds/build/relationships/betaGroups', {
data: [{ type: 'betaGroups', id: 'A' }],
})
.reply(403, { errors: [{ code: 'ASSIGNMENT_FAILED' }] });
mockInternalBuildState(state);
api()
.post('/v1/builds/build/relationships/betaGroups', {
data: [{ type: 'betaGroups', id: 'B' }],
})
.reply(422, { errors: [{ code: 'ASSIGNMENT_FAILED' }] });
// A failed assignment must not prevent the next group from being assigned.
api()
.post('/v1/builds/build/relationships/betaGroups', {
data: [{ type: 'betaGroups', id: 'C' }],
})
.reply(204);
const failure = await updateTestFlightMetadataAsync({
...options,
changelog: '',
groups: ['A', 'B', 'C'],
}).catch(error => error);
expect(failure).toBeInstanceOf(AggregateError);
expect(failure.errors).toHaveLength(2);
if (state === 'MISSING_EXPORT_COMPLIANCE' || state === 'IN_EXPORT_COMPLIANCE_REVIEW') {
expect(failure.errors[0]).toBe(failure.errors[1]);
} else {
expect(failure.errors[0]).toMatchObject({ status: 403 });
expect(failure.errors[1]).toMatchObject({ status: 422 });
}
expect(failure.errors[0].message).toContain(guidance);
expect(
jest
.mocked(options.logger.info)
.mock.calls.filter(([message]) => String(message).includes('internal TestFlight state ='))
).toEqual([[`Apple build build: internal TestFlight state = ${state ?? 'UNKNOWN'}.`]]);
expect(Sentry.capture).toHaveBeenCalledTimes(1);
expect(Sentry.capture).toHaveBeenCalledWith(
`TestFlight group assignment failed (state: ${state ?? 'UNKNOWN'})`,
{
level: 'error',
tags: { step: 'eas/update_testflight_metadata', internal_build_state: state ?? 'UNKNOWN' },
extras: {
buildId: 'build',
assignmentError: expect.stringContaining('ASSIGNMENT_FAILED'),
},
}
);
}
);
@@ -1,22 +1,13 @@
import { SystemError } from '@expo/eas-build-job';
import { type bunyan } from '@expo/logger';
import {
BuildFunction,
BuildRuntimePlatform,
type BuildStepEnv,
BuildStepInput,
BuildStepInputValueTypeName,
} from '@expo/steps';
import { type BuildRuntimePlatform, type BuildStepEnv } from '@expo/steps';
import spawn from '@expo/turtle-spawn';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { type CustomBuildContext } from '../../customBuildContext';
import {
uploadRemoteSessionConfigWithLocalEgressAsync,
withLocalEgressSession,
} from '../utils/localEgressSession';
import { uploadRemoteSessionConfigWithLocalEgressAsync } from '../utils/localEgressSession';
import { type DeviceSessionHost, startDeviceSessionHostAsync } from '../utils/deviceSessionHost';
import { Sentry } from '../../sentry';
import {
@@ -26,22 +17,15 @@ import {
resolvePackageInstall,
} from '../../utils/packageManager';
import { pollAgentDeviceArtifactsForUploadAsync } from '../utils/agentDeviceArtifacts';
import {
createNetworkCaptureInputProviders,
parseNetworkCaptureInputs,
} from '../utils/networkCaptureFields';
import { type parseNetworkCaptureInputs } from '../utils/networkCaptureFields';
import { startAgentDeviceEventCollectionAsync } from '../utils/agentDeviceEvents';
import { type StartupTasks, createStartupTasks } from '../utils/startupTasks';
import { type StartupTasks } from '../utils/startupTasks';
import {
type DetachedProcessHandle,
createServeSimLaunchInputProviders,
describeServeSimLaunch,
finishRemoteSessionAsync,
getDeviceRunSessionIdOrThrow,
getNgrokAuthtokenOrThrow,
getNgrokTunnelDomainOrThrow,
parseServeSimLaunchInputs,
selectXcodeDeveloperDirectoryAsync,
spawnDetached,
startNgrokTunnelAsync,
waitForDeviceRunSessionStoppedAsync,
@@ -66,81 +50,6 @@ const AGENT_DEVICE_DAEMON_ENV = {
AGENT_DEVICE_SESSION_IDLE_TIMEOUT_MS: '0',
};
export function createStartAgentDeviceRemoteSessionBuildFunction(
ctx: CustomBuildContext
): BuildFunction {
return new BuildFunction({
namespace: 'eas',
id: 'start_agent_device_remote_session',
name: 'Start agent device remote session',
__metricsId: 'eas/start_agent_device_remote_session',
inputProviders: [
...createServeSimLaunchInputProviders(),
...createNetworkCaptureInputProviders(),
BuildStepInput.createProvider({
id: 'package_version',
required: false,
allowedValueTypeName: BuildStepInputValueTypeName.STRING,
}),
BuildStepInput.createProvider({
id: 'max_idle_time_minutes',
required: false,
allowedValueTypeName: BuildStepInputValueTypeName.NUMBER,
}),
BuildStepInput.createProvider({
id: 'max_duration_seconds',
required: false,
allowedValueTypeName: BuildStepInputValueTypeName.NUMBER,
}),
],
fn: withLocalEgressSession(async ({ logger, global }, { inputs, env, signal }) => {
// Fail fast before any expensive setup if the injected env vars are missing.
const sessionEnv = getAgentDeviceRemoteSessionEnvOrThrow(env);
const packageVersion = inputs.package_version.value as string | undefined;
// A missing or non-positive value disables the idle timeout (opt-in feature).
const maxIdleTimeMinutes = inputs.max_idle_time_minutes.value as number | undefined;
const maxDurationSeconds = inputs.max_duration_seconds?.value as number | undefined;
const { runtimePlatform } = global;
const launch = parseServeSimLaunchInputs(
{
launchAppIdentifier: inputs.launch_app_identifier?.value as string | undefined,
launchArgs: inputs.launch_args?.value,
openUrl: inputs.open_url?.value as string | undefined,
},
{ runtimePlatform }
);
const capture = parseNetworkCaptureInputs(
{
networkCapture: inputs.network_capture?.value,
networkCaptureFields: inputs.network_capture_fields?.value,
},
{ runtimePlatform }
);
if (runtimePlatform === BuildRuntimePlatform.DARWIN) {
await selectXcodeDeveloperDirectoryAsync({ env, logger });
}
// Earlier steps booted the device and installed and launched the app.
await runAgentDeviceRemoteSessionAsync(ctx, {
env,
logger,
signal,
runtimePlatform,
sessionEnv,
packageVersion,
maxIdleTimeMinutes,
maxDurationSeconds,
launch,
capture,
tasks: createStartupTasks(logger),
device: { booted: Promise.resolve(), ready: Promise.resolve() },
});
}),
});
}
export type AgentDeviceRemoteSessionEnv = {
deviceRunSessionId: string;
ngrokTunnelDomain: string;
@@ -186,7 +95,6 @@ export async function runAgentDeviceRemoteSessionAsync(
packageVersion,
maxIdleTimeMinutes,
maxDurationSeconds,
launch,
capture,
tasks,
device,
@@ -199,7 +107,6 @@ export async function runAgentDeviceRemoteSessionAsync(
packageVersion: string | undefined;
maxIdleTimeMinutes: number | undefined;
maxDurationSeconds: number | undefined;
launch: ReturnType<typeof parseServeSimLaunchInputs>;
capture: ReturnType<typeof parseNetworkCaptureInputs>;
tasks: StartupTasks;
device: { booted: Promise<unknown>; ready: Promise<unknown> };
@@ -247,18 +154,11 @@ export async function runAgentDeviceRemoteSessionAsync(
// A boot cannot be cancelled, so stop waiting for it when startup is aborted.
await tasks.untilAborted(device.booted);
tasks.signal.throwIfAborted();
const launchDescription = describeServeSimLaunch(launch);
if (launchDescription) {
taskLogger.info(launchDescription);
}
sessionHost = await startDeviceSessionHostAsync(ctx, {
runtimePlatform,
env,
logger: taskLogger,
timeoutMs: STARTUP_TIMEOUT_MS,
launchAppIdentifier: launch.launchAppIdentifier,
launchArgs: launch.launchArgs,
openUrl: launch.openUrl,
networkCapture: capture.networkCapture,
networkCaptureFields: capture.networkCaptureFields,
});
@@ -17,10 +17,7 @@ import {
} from '../../utils/AndroidEmulatorUtils';
import { IosSimulatorName, IosSimulatorUuid } from '../../utils/IosSimulatorUtils';
import { withLocalEgressSession } from '../utils/localEgressSession';
import {
parseServeSimLaunchInputs,
selectXcodeDeveloperDirectoryAsync,
} from '../utils/remoteDeviceRunSession';
import { selectXcodeDeveloperDirectoryAsync } from '../utils/remoteDeviceRunSession';
import {
createNetworkCaptureInputProviders,
parseNetworkCaptureInputs,
@@ -47,9 +44,9 @@ const ANDROID_DEVICE_NAME = 'EasAndroidDevice01' as AndroidVirtualDeviceName;
* One step for a whole agent-device session: boot the device, download, install and
* launch the app, and start the agent-device daemon and the web preview.
*
* It replaces eas/start_ios_simulator or eas/start_android_emulator, eas/download_build,
* eas/install_build, eas/launch_application and eas/start_agent_device_remote_session,
* so the parts that do not depend on each other can run at the same time:
* It does the work of eas/start_ios_simulator or eas/start_android_emulator,
* eas/download_build, eas/install_build and eas/launch_application, plus the agent-device
* session, so the parts that do not depend on each other can run at the same time:
*
* boot ─────────────┬─► install ─► launch ─┐
* download ─────────┘ │
@@ -261,8 +258,6 @@ export function createStartAgentDeviceSessionBuildFunction(ctx: CustomBuildConte
// A missing or non-positive value disables the idle timeout (opt-in feature).
maxIdleTimeMinutes: inputs.max_idle_time_minutes.value as number | undefined,
maxDurationSeconds: inputs.max_duration_seconds.value as number | undefined,
// The app is launched with simctl / adb above, not by serve-sim.
launch: parseServeSimLaunchInputs({}, { runtimePlatform }),
capture,
tasks,
device: { booted, ready },
@@ -102,10 +102,16 @@ export function createStartWebPreviewRemoteSessionBuildFunction(
signal,
ctx,
deviceRunSessionId,
// WEB_PREVIEW_ONLY sessions read webPreviewUrl and webPreviewToken. Legacy SERVE_SIM
// sessions read previewUrl and previewToken. The server drops the keys that the
// session type does not use.
remoteConfig: {
webPreviewUrl: webPreview.previewPageUrl,
previewUrl: webPreview.previewPageUrl,
previewApiUrl: webPreview.apiUrl,
...(webPreview.previewToken ? { previewToken: webPreview.previewToken } : {}),
...(webPreview.previewToken
? { webPreviewToken: webPreview.previewToken, previewToken: webPreview.previewToken }
: {}),
},
logger,
});
@@ -5,6 +5,7 @@ import path from 'node:path';
import limitFactory from 'promise-limit';
import { z } from 'zod';
import { Sentry } from '../../sentry';
import { AscApiClient, AscApiRequestError } from '../utils/ios/AscApiClient';
import { AscApiUtils } from '../utils/ios/AscApiUtils';
@@ -191,6 +192,7 @@ export async function updateTestFlightMetadataAsync({
})
: [];
const assignedIds = new Set(assignedGroups.map(group => group.id));
let internalBuildStateVerification: Promise<void> | undefined;
const groupLimit = limitFactory<void>(1);
const groupResults = await Promise.allSettled(
requestedGroups.map(group =>
@@ -219,6 +221,17 @@ export async function updateTestFlightMetadataAsync({
);
} catch (error) {
logger.error(`❌ Group ${label}: assignment failed. ${String(error)}`);
internalBuildStateVerification ??=
verifyInternalBuildStateValidForGroupAssignmentAsync({
client,
buildId,
appId: app.id,
logger,
error,
});
await internalBuildStateVerification;
if (isInternalGroupAssignmentError(error)) {
throw new UserError(
'EAS_TESTFLIGHT_INTERNAL_GROUP_ASSIGNMENT_FAILED',
@@ -229,6 +242,7 @@ export async function updateTestFlightMetadataAsync({
{ cause: error }
);
}
throw error;
}
logger.info(`✅ Group ${label}: assignment completed.`);
@@ -269,6 +283,48 @@ export async function updateTestFlightMetadataAsync({
}
}
async function verifyInternalBuildStateValidForGroupAssignmentAsync({
client,
appId,
buildId,
logger,
error,
}: {
client: AscApiClient;
appId: string;
buildId: string;
logger: bunyan;
error: unknown;
}): Promise<void> {
let state = 'UNKNOWN';
try {
const { data } = await client.getAsync('/v1/builds/:id/buildBetaDetail', {}, { id: buildId });
state = data.attributes.internalBuildState;
} catch (diagnosticError) {
logger.warn(`Could not read the TestFlight state: ${String(diagnosticError)}`);
}
logger.info(`Apple build ${buildId}: internal TestFlight state = ${state}.`);
Sentry.capture(`TestFlight group assignment failed (state: ${state})`, {
level: 'error',
tags: { step: 'eas/update_testflight_metadata', internal_build_state: state },
extras: { buildId, assignmentError: String(error) },
});
if (state === 'MISSING_EXPORT_COMPLIANCE' || state === 'IN_EXPORT_COMPLIANCE_REVIEW') {
throw new UserError(
'EAS_TESTFLIGHT_GROUP_ASSIGNMENT_FAILED',
`Apple could not add the uploaded build to a TestFlight group (state: ${state}). ` +
(state === 'MISSING_EXPORT_COMPLIANCE'
? 'Complete the export compliance questions for this build in App Store Connect. For future builds, set ITSAppUsesNonExemptEncryption in the iOS Info.plist to the value that matches your app. '
: 'Wait for Apple to approve the export compliance information, then assign the group in App Store Connect. ') +
`The binary is already uploaded; do not upload it again to fix group assignment. Manage this build at https://appstoreconnect.apple.com/apps/${appId}/testflight`,
{ cause: error }
);
}
}
// Apple returns a generic 422 code, so match the title or detail too.
function isInternalGroupAssignmentError(error: unknown): boolean {
if (error instanceof AggregateError) {
@@ -155,21 +155,11 @@ export function createUploadToAscBuildFunction(): BuildFunction {
});
stepsCtx.logger.info('Committing upload...');
await client.patchAsync(
`/v1/buildUploadFiles/:id`,
{
data: {
type: 'buildUploadFiles',
id: buildFileResponse.data.id,
attributes: {
uploaded: true,
},
},
},
{
id: buildFileResponse.data.id,
}
);
await AscApiUtils.commitBuildUploadFileAsync({
client,
fileId: buildFileResponse.data.id,
logger: stepsCtx.logger,
});
stepsCtx.logger.info('Checking upload file status...');
const waitingForFileStartedAt = Date.now();
@@ -10,12 +10,27 @@ import { setTimeout } from 'node:timers/promises';
import { Sentry } from '../../sentry';
import { IosSimulatorUtils, type IosSimulatorUuid } from '../../utils/IosSimulatorUtils';
import {
PackageManager,
resolveConfiguredPackageManager,
resolvePackageExec,
} from '../../utils/packageManager';
import { isProcessGroupRunning, killProcessGroup } from '../../utils/processes';
import { SERVE_SIM_STATE_DIR, readServeSimServersAsync } from './serveSimMetricsRecorder';
const IOS_SIMULATOR_RECORDING_POLL_INTERVAL_MS = 2_000;
const RECORD_SIM_FINISH_TIMEOUT_MS = 70_000;
const RECORD_SIM_FORCE_STOP_TIMEOUT_MS = 5_000;
const RECORD_SIM_MAX_ATTEMPTS_PER_BOOT = 3;
const RECORD_SIM_COMMAND = 'record-sim';
// Once the poller stops, each recording stops in parallel within 60 + 130 + 5 + 30 = 225 s.
const SERVE_SIM_START_TIMEOUT_MS = 60_000;
/** serve-sim finalizes an active recording within this grace period after SIGTERM. */
export const SERVE_SIM_STOP_GRACE_PERIOD_MS = 90_000;
const SERVE_SIM_LEASE_FINALIZE_TIMEOUT_MS = 30_000;
// serve-sim may be stopping at the same time: its grace period, the manifest wait, and 10 s.
const SERVE_SIM_FINISH_TIMEOUT_MS =
SERVE_SIM_STOP_GRACE_PERIOD_MS + SERVE_SIM_LEASE_FINALIZE_TIMEOUT_MS + 10_000;
const SERVE_SIM_FORCE_STOP_TIMEOUT_MS = 5_000;
const SERVE_SIM_RECORDING_STARTED = 'serve-sim:recording-started';
// The serve-sim package the session host started; every recorder runs `record-video` from it.
let serveSimPackageSpec: string | null = null;
type IosSimulatorRecording = {
id: string;
@@ -25,6 +40,7 @@ type IosSimulatorRecording = {
outputDirectory: string;
startedAt: Date;
getOutput: () => string;
hasStarted: () => boolean;
};
type ActiveIosSimulatorRecording = IosSimulatorRecording & {
@@ -35,11 +51,15 @@ type ActiveIosSimulatorRecording = IosSimulatorRecording & {
type IosSimulatorRecordingSession = {
env: Env;
logger: bunyan;
recordSimCommand: string;
recordingsRootDirectory: string;
activeRecordings: Map<IosSimulatorUuid, ActiveIosSimulatorRecording>;
completedRecordings: IosSimulatorRecording[];
recordingFailureCounts: Map<IosSimulatorUuid, number>;
/** Failed attempts for the whole session; unlike recordingFailureCounts, never reset. */
failedAttemptCounts: Map<IosSimulatorUuid, number>;
recordingRetryAt: Map<IosSimulatorUuid, number>;
serveBearerTokens: Map<IosSimulatorUuid, string>;
completedServeBearerTokens: Map<IosSimulatorUuid, string>;
pollingPromise: Promise<void>;
abortController: AbortController;
};
@@ -47,31 +67,31 @@ type IosSimulatorRecordingSession = {
let activeIosSimulatorRecordingSession: IosSimulatorRecordingSession | null = null;
export namespace IosSimulatorRecordingUtils {
/** One session host serves every device of a job, so the first package wins. */
export function useServeSimPackage(packageSpec: string): void {
serveSimPackageSpec ??= packageSpec;
}
export async function startAsync({ env, logger }: { env: Env; logger: bunyan }): Promise<void> {
if (activeIosSimulatorRecordingSession) {
logger.info('iOS Simulator screen recording polling is already running.');
return;
}
const recordSimCommand = await resolveRecordSimCommandAsync({ env });
if (!recordSimCommand) {
logger.warn(
'record-sim binary is not available; iOS Simulator screen recordings are disabled.'
);
return;
}
const recordingsRootDirectory = await mkdtemp(
path.join(os.tmpdir(), 'ios-simulator-recordings-')
);
const session: IosSimulatorRecordingSession = {
env,
logger,
recordSimCommand,
recordingsRootDirectory,
activeRecordings: new Map(),
completedRecordings: [],
recordingFailureCounts: new Map(),
failedAttemptCounts: new Map(),
recordingRetryAt: new Map(),
serveBearerTokens: new Map(),
completedServeBearerTokens: new Map(),
pollingPromise: Promise.resolve(),
abortController: new AbortController(),
};
@@ -94,6 +114,7 @@ export namespace IosSimulatorRecordingUtils {
}[]
> {
const session = activeIosSimulatorRecordingSession;
serveSimPackageSpec = null;
if (!session) {
logger.info('No iOS Simulator screen recordings are running.');
return [];
@@ -105,41 +126,55 @@ export namespace IosSimulatorRecordingUtils {
await Promise.all(
[...session.activeRecordings.values()].map(async recording => {
logger.info(`Stopping screen recording for ${recording.deviceName}.`);
recording.recordingProcess.kill('SIGINT');
const startState = await waitForRecorderStartAsync(recording, SERVE_SIM_START_TIMEOUT_MS);
if (startState === 'exited') {
return;
}
if (startState === 'timeout') {
logger.warn(
{ recorderOutput: recording.getOutput().trim() },
`Screen recording for ${recording.deviceName} did not report its start within ${Math.round(
SERVE_SIM_START_TIMEOUT_MS / 1_000
)} seconds and will be stopped.`
);
}
const finishTimeoutMs =
startState === 'started' ? SERVE_SIM_FINISH_TIMEOUT_MS : SERVE_SIM_FORCE_STOP_TIMEOUT_MS;
signalRecorder(recording.recordingProcess, 'SIGINT');
const finished = await Promise.race([
recording.completionPromise.then(() => true),
setTimeout(RECORD_SIM_FINISH_TIMEOUT_MS, false, { ref: false }),
setTimeout(finishTimeoutMs, false, { ref: false }),
]);
if (finished) {
return;
}
const recordSimOutput = recording.getOutput().trim();
const finishTimeoutSeconds = Math.round(RECORD_SIM_FINISH_TIMEOUT_MS / 1_000);
const recorderOutput = recording.getOutput().trim();
const finishTimeoutSeconds = Math.round(finishTimeoutMs / 1_000);
const stopFailure =
startState === 'started'
? `did not finish within ${finishTimeoutSeconds} seconds`
: `did not stop within ${finishTimeoutSeconds} seconds of SIGINT`;
logger.warn(
{ recordSimOutput },
`Screen recording for ${recording.deviceName} did not finish within ${finishTimeoutSeconds} seconds and will be stopped.${
recordSimOutput
? `\nRecent recorder messages:\n${recordSimOutput}`
{ recorderOutput },
`Screen recording for ${recording.deviceName} ${stopFailure} and will be killed.${
recorderOutput
? `\nRecent recorder messages:\n${recorderOutput}`
: '\nNo recorder messages were captured.'
}`
);
recording.recordingProcess.kill('SIGKILL');
signalRecorder(recording.recordingProcess, 'SIGKILL');
const killed = await Promise.race([
recording.completionPromise.then(() => true),
setTimeout(RECORD_SIM_FORCE_STOP_TIMEOUT_MS, false, { ref: false }),
setTimeout(SERVE_SIM_FORCE_STOP_TIMEOUT_MS, false, { ref: false }),
]);
if (!killed) {
logger.warn(
`iOS Simulator recording process for ${recording.deviceName} did not exit after SIGKILL.`
);
// Recorder output stays in the job log: it can contain the serve-sim bearer token.
Sentry.capture(
`iOS Simulator recording process for ${recording.deviceName} did not exit after SIGKILL.`,
{
extras: {
output: recording.getOutput(),
},
}
`iOS Simulator recording process for ${recording.deviceName} did not exit after SIGKILL.`
);
}
})
@@ -148,7 +183,31 @@ export namespace IosSimulatorRecordingUtils {
const completedRecordings = [...session.completedRecordings].sort(
(a, b) => a.startedAt.getTime() - b.startedAt.getTime()
);
return completedRecordings.map(recording => ({
const recordingsWithManifests = await Promise.all(
completedRecordings.map(async recording => {
if (
await waitForRecordingManifestAsync(
recording.outputDirectory,
SERVE_SIM_LEASE_FINALIZE_TIMEOUT_MS
)
) {
return recording;
}
reportLostRecording(logger, recording.deviceName, recording.getOutput().trim());
return null;
})
);
const finalizedRecordings = recordingsWithManifests.filter(recording => recording !== null);
const recordedUdids = new Set(finalizedRecordings.map(recording => recording.udid));
for (const [failedUdid, attempts] of session.failedAttemptCounts) {
if (!recordedUdids.has(failedUdid)) {
Sentry.capture('iOS Simulator screen recording failed on every attempt', {
level: 'warning',
extras: { attempts },
});
}
}
return finalizedRecordings.map(recording => ({
udid: recording.udid,
deviceName: recording.deviceName,
runtimeDisplayName: recording.runtimeDisplayName,
@@ -174,18 +233,38 @@ async function pollIosSimulatorRecordingsAsync(
break;
}
listDevicesErrorCount = 0;
const readyServers = new Map(
(await readServeSimServersAsync(SERVE_SIM_STATE_DIR)).flatMap(server =>
server.token ? [[server.udid, server.token] as const] : []
)
);
const bootedUdids = new Set(bootedDevices.map(device => device.udid));
for (const udid of session.recordingFailureCounts.keys()) {
for (const udid of session.serveBearerTokens.keys()) {
if (!bootedUdids.has(udid)) {
session.recordingFailureCounts.delete(udid);
session.recordingRetryAt.delete(udid);
session.serveBearerTokens.delete(udid);
session.completedServeBearerTokens.delete(udid);
}
}
for (const device of bootedDevices) {
const token = readyServers.get(device.udid);
const packageSpec = serveSimPackageSpec;
if (!token || !packageSpec) {
continue;
}
if (session.serveBearerTokens.get(device.udid) !== token) {
session.serveBearerTokens.set(device.udid, token);
session.recordingFailureCounts.delete(device.udid);
session.recordingRetryAt.delete(device.udid);
session.completedServeBearerTokens.delete(device.udid);
}
if (
session.completedServeBearerTokens.get(device.udid) === token ||
session.activeRecordings.has(device.udid) ||
(session.recordingFailureCounts.get(device.udid) ?? 0) >= RECORD_SIM_MAX_ATTEMPTS_PER_BOOT
Date.now() < (session.recordingRetryAt.get(device.udid) ?? 0)
) {
continue;
}
@@ -193,6 +272,7 @@ async function pollIosSimulatorRecordingsAsync(
udid: device.udid,
deviceName: device.name,
runtimeDisplayName: device.runtimeDisplayName,
packageSpec,
});
}
} catch (err) {
@@ -227,50 +307,90 @@ async function startIosSimulatorRecordingAsync(
udid,
deviceName,
runtimeDisplayName,
packageSpec,
}: {
udid: IosSimulatorUuid;
deviceName: string;
runtimeDisplayName: string;
packageSpec: string;
}
): Promise<void> {
const startedAt = new Date();
const serveBearerToken = session.serveBearerTokens.get(udid);
const recordingId = randomUUID();
const outputDirectory = path.join(session.recordingsRootDirectory, recordingId);
await mkdir(outputDirectory, { recursive: true });
session.logger.info(`Starting screen recording for ${deviceName}.`);
const recorderExec = resolvePackageExec(
resolveConfiguredPackageManager(session.env, PackageManager.NPM),
[packageSpec, 'record-video']
);
const recordingSpawn = spawn(
session.recordSimCommand,
['--udid', udid, '--output', outputDirectory, '--segment-duration', '0'],
recorderExec.command,
[...recorderExec.args, '--udid', udid, '--output', outputDirectory],
{
env: session.env,
stdio: ['ignore', 'pipe', 'pipe'],
detached: true,
}
);
const getOutput = captureProcessOutput(recordingSpawn.child);
const completionPromise = recordingSpawn
.then(() => undefined)
.catch((err: unknown) => {
session.recordingFailureCounts.set(udid, (session.recordingFailureCounts.get(udid) ?? 0) + 1);
const { getOutput, hasStarted } = captureProcessOutput(recordingSpawn.child);
const saveFinalizedRecordingAsync = async (): Promise<boolean> => {
if (
!(await waitForRecordingManifestAsync(outputDirectory, SERVE_SIM_LEASE_FINALIZE_TIMEOUT_MS))
) {
return false;
}
if (serveBearerToken) {
session.completedServeBearerTokens.set(udid, serveBearerToken);
}
session.completedRecordings.push({
id: recordingId,
udid,
deviceName,
runtimeDisplayName,
outputDirectory,
startedAt,
getOutput,
hasStarted,
});
return true;
};
const completionPromise = (async () => {
let spawnError: unknown;
try {
await recordingSpawn;
} catch (err) {
spawnError = err;
}
// A package-manager wrapper can exit while its record-video child still
// runs in the detached group. Keep this device active until the group exits.
await waitForRecordingProcessGroupExitAsync(recordingSpawn.child);
// Host shutdown can end the recorder's lease after its video has been saved.
if ((hasStarted() || !spawnError) && (await saveFinalizedRecordingAsync())) {
return;
}
if (spawnError) {
const err = spawnError;
const error = err instanceof Error ? err : new Error(String(err));
Sentry.capture('iOS Simulator screen recording process failed', error);
session.logger.warn(
{ err: error, recordSimOutput: getOutput() },
{ err: error, recorderOutput: getOutput() },
`Screen recording process failed for ${deviceName}.`
);
})
.finally(() => {
session.activeRecordings.delete(udid);
session.completedRecordings.push({
id: recordingId,
udid,
deviceName,
runtimeDisplayName,
outputDirectory,
startedAt,
getOutput,
});
});
}
if (hasStarted()) {
reportLostRecording(session.logger, deviceName, getOutput().trim());
} else if (!spawnError) {
session.logger.warn(
{ recorderOutput: getOutput().trim() },
`Screen recording for ${deviceName} exited before it started; it will be retried.`
);
}
scheduleRecordingRetry(session, udid);
})().finally(() => {
session.activeRecordings.delete(udid);
});
session.activeRecordings.set(udid, {
id: recordingId,
@@ -282,31 +402,103 @@ async function startIosSimulatorRecordingAsync(
completionPromise,
startedAt,
getOutput,
hasStarted,
});
}
async function resolveRecordSimCommandAsync({ env }: { env: Env }): Promise<string | null> {
try {
await spawn('which', [RECORD_SIM_COMMAND], { env });
return RECORD_SIM_COMMAND;
} catch {}
async function waitForRecordingManifestAsync(
outputDirectory: string,
timeoutMs: number
): Promise<boolean> {
const deadline = Date.now() + timeoutMs;
while (Date.now() < deadline) {
try {
await access(path.join(outputDirectory, 'session.json'));
return true;
} catch {
await setTimeout(1_000);
}
}
return false;
}
const packagedRecordSimPath = path.join(__dirname, '..', '..', '..', 'bin', RECORD_SIM_COMMAND);
try {
await access(packagedRecordSimPath);
return packagedRecordSimPath;
} catch {
return null;
async function waitForRecorderStartAsync(
recording: ActiveIosSimulatorRecording,
timeoutMs: number
): Promise<'started' | 'exited' | 'timeout'> {
const deadline = Date.now() + timeoutMs;
while (Date.now() < deadline) {
if (recording.hasStarted()) {
return 'started';
}
const exited = await Promise.race([
recording.completionPromise.then(() => true),
setTimeout(100, false),
]);
if (exited) {
return 'exited';
}
}
return 'timeout';
}
function reportLostRecording(logger: bunyan, deviceName: string, recorderOutput: string): void {
logger.warn(
{ recorderOutput },
`Screen recording for ${deviceName} ended without a manifest; its footage cannot be uploaded.`
);
Sentry.capture('iOS Simulator screen recording ended without a manifest', {
level: 'warning',
extras: { deviceName },
});
}
function scheduleRecordingRetry(
session: IosSimulatorRecordingSession,
udid: IosSimulatorUuid
): void {
session.failedAttemptCounts.set(udid, (session.failedAttemptCounts.get(udid) ?? 0) + 1);
const failures = (session.recordingFailureCounts.get(udid) ?? 0) + 1;
session.recordingFailureCounts.set(udid, failures);
session.recordingRetryAt.set(
udid,
Date.now() + Math.min(300_000, 25_000 * 2 ** Math.min(failures - 1, 4))
);
}
function signalRecorder(child: ChildProcess, signal: NodeJS.Signals): void {
// killProcessGroup skips a child without a pid.
if (child.pid === undefined) {
child.kill(signal);
return;
}
killProcessGroup(child, signal);
}
async function waitForRecordingProcessGroupExitAsync(child: ChildProcess): Promise<void> {
if (child.pid === undefined) {
return;
}
// Bounded by finishAsync's SIGKILL; unref'd so it cannot keep the worker alive.
while (isProcessGroupRunning(child.pid)) {
await setTimeout(100, undefined, { ref: false });
}
}
function captureProcessOutput(recordingProcess: ChildProcess): () => string {
function captureProcessOutput(recordingProcess: ChildProcess): {
getOutput: () => string;
hasStarted: () => boolean;
} {
let output = '';
let started = false;
const appendChunk = (chunk: Buffer | string): void => {
// Keep enough recorder stderr/stdout for diagnostics without retaining unbounded output.
output = `${output}${chunk.toString()}`.slice(-16_384);
output = `${output}${chunk.toString()}`;
if (output.includes(SERVE_SIM_RECORDING_STARTED)) {
started = true;
}
output = output.slice(-16_384);
};
recordingProcess.stdout?.on('data', appendChunk);
recordingProcess.stderr?.on('data', appendChunk);
return () => output;
return { getOutput: () => output, hasStarted: () => started };
}
@@ -0,0 +1,539 @@
import { type Env } from '@expo/eas-build-job';
import { type bunyan } from '@expo/logger';
import spawn from '@expo/turtle-spawn';
import { type ChildProcess } from 'node:child_process';
import { EventEmitter } from 'node:events';
import { rm, writeFile } from 'node:fs/promises';
import { PassThrough } from 'node:stream';
import { setTimeout } from 'node:timers/promises';
import { IosSimulatorUtils } from '../../../utils/IosSimulatorUtils';
import { Sentry } from '../../../sentry';
import { IosSimulatorRecordingUtils } from '../IosSimulatorRecordingUtils';
import { readServeSimServersAsync } from '../serveSimMetricsRecorder';
jest.mock('@expo/turtle-spawn');
jest.mock('../serveSimMetricsRecorder');
jest.mock('../../../sentry');
const logger = {
info: jest.fn(),
warn: jest.fn(),
error: jest.fn(),
debug: jest.fn(),
} as unknown as bunyan;
const env = {} as Env;
const udid = '06B546AC-7B06-4BED-83DB-83603E8EB537';
beforeEach(() => {
IosSimulatorRecordingUtils.useServeSimPackage('@expo/serve-sim@next');
jest
.spyOn(IosSimulatorUtils, 'getAvailableDevicesAsync')
.mockResolvedValue([{ udid, name: 'iPhone 17 Pro', runtimeDisplayName: 'iOS 26.4' } as never]);
jest.mocked(readServeSimServersAsync).mockReset();
jest.mocked(spawn).mockReset();
});
afterEach(async () => {
await IosSimulatorRecordingUtils.finishAsync({ logger });
jest.restoreAllMocks();
});
async function waitForRecorderSpawnAsync(): Promise<void> {
for (let attempt = 0; attempt < 50; attempt++) {
if (jest.mocked(spawn).mock.calls.length > 0) {
return;
}
await setTimeout(10);
}
throw new Error('serve-sim recorder did not start');
}
// A recorder the test controls: `exit` ends it cleanly, `fail` ends it with an error.
function mockRecorder({ kill }: { kill?: (signal: NodeJS.Signals) => void } = {}): {
child: ChildProcess;
exit: () => void;
fail: (error: Error) => void;
} {
let exit: () => void = () => {};
let fail: (error: Error) => void = () => {};
const child = Object.assign(new EventEmitter(), {
stdout: new PassThrough(),
stderr: new PassThrough(),
kill: jest.fn((signal: NodeJS.Signals) => {
kill?.(signal);
return true;
}),
}) as unknown as ChildProcess;
const done = new Promise<void>((resolve, reject) => {
exit = resolve;
fail = reject;
});
jest.mocked(spawn).mockReturnValue(Object.assign(done, { child }) as never);
return { child, exit: () => exit(), fail: error => fail(error) };
}
function mockFailingRecorder(): void {
jest.mocked(spawn).mockImplementation((_command, args) => {
void writeFile(`${args[6]}/session.json`, JSON.stringify({ recording: 'recording.mp4' }));
return Object.assign(Promise.reject(new Error('recorder exited')), {
child: {
stdout: new PassThrough(),
stderr: new PassThrough(),
kill: jest.fn(),
},
}) as never;
});
}
test('waits for a token-gated serve-sim session before starting a recorder', async () => {
jest
.mocked(readServeSimServersAsync)
.mockResolvedValue([{ udid, url: 'http://127.0.0.1:43563' }]);
await IosSimulatorRecordingUtils.startAsync({ env, logger });
await IosSimulatorRecordingUtils.finishAsync({ logger });
expect(jest.mocked(spawn)).not.toHaveBeenCalled();
});
test('starts serve-sim record-video and returns only a finalized recording', async () => {
jest
.mocked(readServeSimServersAsync)
.mockResolvedValue([{ udid, url: 'http://127.0.0.1:43563', token: 'session-token' }]);
let resolveProcess: () => void = () => {};
const stderr = new PassThrough();
const child = Object.assign(new EventEmitter(), {
stdout: new PassThrough(),
stderr,
kill: jest.fn(),
}) as unknown as ChildProcess;
const processPromise = Object.assign(
new Promise<void>(resolve => {
resolveProcess = resolve;
}),
{
child,
}
);
jest.mocked(spawn).mockReturnValue(processPromise as never);
await IosSimulatorRecordingUtils.startAsync({ env, logger });
await waitForRecorderSpawnAsync();
const [command, args, options] = jest.mocked(spawn).mock.calls[0];
expect(command).toBe('npx');
expect(args.slice(0, 5)).toEqual([
'--yes',
'@expo/serve-sim@next',
'record-video',
'--udid',
udid,
]);
expect(args[5]).toBe('--output');
expect(args).not.toContain('--segment-duration');
expect(options?.detached).toBe(true);
const outputDirectory = args[6];
jest.mocked(child.kill).mockImplementation(() => {
resolveProcess();
void setTimeout(20).then(() =>
writeFile(`${outputDirectory}/session.json`, JSON.stringify({ recording: 'recording.mp4' }))
);
return true;
});
const finishing = IosSimulatorRecordingUtils.finishAsync({ logger });
await setTimeout(20);
expect(child.kill).not.toHaveBeenCalled();
stderr.write('serve-sim:recording-started\n');
stderr.write('x'.repeat(17_000));
const recordings = await finishing;
expect(child.kill).toHaveBeenCalledWith('SIGINT');
expect(recordings).toEqual([
{
udid,
deviceName: 'iPhone 17 Pro',
runtimeDisplayName: 'iOS 26.4',
directory: outputDirectory,
},
]);
});
test('reports a started recording that ends without a manifest to Sentry', async () => {
let currentTime = Date.now();
jest.spyOn(Date, 'now').mockImplementation(() => currentTime);
jest
.mocked(readServeSimServersAsync)
.mockResolvedValue([{ udid, url: 'http://127.0.0.1:43563', token: 'session-token' }]);
let exitRecorder: () => void = () => {};
const child = Object.assign(new EventEmitter(), {
stdout: new PassThrough(),
stderr: new PassThrough(),
kill: jest.fn(),
}) as unknown as ChildProcess;
jest.mocked(spawn).mockReturnValue(
Object.assign(
new Promise<void>(resolve => {
exitRecorder = resolve;
}),
{ child }
) as never
);
await IosSimulatorRecordingUtils.startAsync({ env, logger });
await waitForRecorderSpawnAsync();
child.stderr?.emit('data', 'serve-sim:recording-started\n');
child.stderr?.emit('data', 'GET http://127.0.0.1:43563/record?token=session-token failed\n');
exitRecorder();
await setTimeout(50);
currentTime += 31_000;
await setTimeout(1_100);
expect(Sentry.capture).toHaveBeenCalledWith(
'iOS Simulator screen recording ended without a manifest',
{ level: 'warning', extras: { deviceName: 'iPhone 17 Pro' } }
);
expect(JSON.stringify(jest.mocked(Sentry.capture).mock.calls)).not.toContain('session-token');
});
test('says when a recorder never reported its start before it is stopped', async () => {
let currentTime = Date.now();
jest.spyOn(Date, 'now').mockImplementation(() => currentTime);
jest
.mocked(readServeSimServersAsync)
.mockResolvedValue([{ udid, url: 'http://127.0.0.1:43563', token: 'session-token' }]);
let exitRecorder: () => void = () => {};
const child = Object.assign(new EventEmitter(), {
stdout: new PassThrough(),
stderr: new PassThrough(),
kill: jest.fn(() => {
exitRecorder();
return true;
}),
}) as unknown as ChildProcess;
jest.mocked(spawn).mockReturnValue(
Object.assign(
new Promise<void>(resolve => {
exitRecorder = resolve;
}),
{ child }
) as never
);
await IosSimulatorRecordingUtils.startAsync({ env, logger });
await waitForRecorderSpawnAsync();
const finishing = IosSimulatorRecordingUtils.finishAsync({ logger });
await setTimeout(50);
currentTime += 61_000;
await setTimeout(200);
currentTime += 31_000;
expect(await finishing).toEqual([]);
expect(child.kill).toHaveBeenCalledWith('SIGINT');
expect(logger.warn).toHaveBeenCalledWith(
expect.anything(),
'Screen recording for iPhone 17 Pro did not report its start within 60 seconds and will be stopped.'
);
});
test('retries after a failed recorder when the serve-sim session token changes', async () => {
jest
.mocked(readServeSimServersAsync)
.mockResolvedValueOnce([{ udid, url: 'http://127.0.0.1:43563', token: 'old-token' }])
.mockResolvedValue([{ udid, url: 'http://127.0.0.1:43563', token: 'new-token' }]);
mockFailingRecorder();
await IosSimulatorRecordingUtils.startAsync({ env, logger });
await waitForRecorderSpawnAsync();
await setTimeout(2_100);
expect(spawn).toHaveBeenCalledTimes(2);
expect(Sentry.capture).not.toHaveBeenCalled();
expect(await IosSimulatorRecordingUtils.finishAsync({ logger })).toEqual([]);
expect(Sentry.capture).toHaveBeenCalledTimes(1);
expect(Sentry.capture).toHaveBeenCalledWith(
'iOS Simulator screen recording failed on every attempt',
expect.objectContaining({ level: 'warning', extras: { attempts: 2 } })
);
});
test('reports a device that failed and then shut down', async () => {
jest
.mocked(readServeSimServersAsync)
.mockResolvedValue([{ udid, url: 'http://127.0.0.1:43563', token: 'session-token' }]);
mockFailingRecorder();
await IosSimulatorRecordingUtils.startAsync({ env, logger });
await waitForRecorderSpawnAsync();
jest.mocked(IosSimulatorUtils.getAvailableDevicesAsync).mockResolvedValue([]);
await setTimeout(2_100);
expect(await IosSimulatorRecordingUtils.finishAsync({ logger })).toEqual([]);
expect(Sentry.capture).toHaveBeenCalledWith(
'iOS Simulator screen recording failed on every attempt',
expect.objectContaining({ extras: { attempts: 1 } })
);
});
test('retries after the server lease expires without a token change', async () => {
const startTime = Date.now();
let currentTime = startTime;
jest.spyOn(Date, 'now').mockImplementation(() => currentTime);
jest
.mocked(readServeSimServersAsync)
.mockResolvedValue([{ udid, url: 'http://127.0.0.1:43563', token: 'session-token' }]);
mockFailingRecorder();
await IosSimulatorRecordingUtils.startAsync({ env, logger });
await waitForRecorderSpawnAsync();
currentTime += 26_000;
await setTimeout(2_100);
expect(spawn).toHaveBeenCalledTimes(2);
});
test('does not start a second recording for a server token after a late manifest', async () => {
jest
.mocked(readServeSimServersAsync)
.mockResolvedValue([{ udid, url: 'http://127.0.0.1:43563', token: 'session-token' }]);
jest.mocked(spawn).mockImplementation((_command, args) => {
const child = Object.assign(new EventEmitter(), {
stdout: new PassThrough(),
stderr: new PassThrough(),
kill: jest.fn(),
}) as unknown as ChildProcess;
void setTimeout(20).then(() =>
writeFile(`${args[6]}/session.json`, JSON.stringify({ recording: 'recording.mp4' }))
);
return Object.assign(Promise.resolve(), { child }) as never;
});
await IosSimulatorRecordingUtils.startAsync({ env, logger });
await waitForRecorderSpawnAsync();
await setTimeout(2_100);
expect(spawn).toHaveBeenCalledTimes(1);
expect(await IosSimulatorRecordingUtils.finishAsync({ logger })).toHaveLength(1);
});
test('keeps a recorder active after its package-manager wrapper exits', async () => {
jest
.mocked(readServeSimServersAsync)
.mockResolvedValue([{ udid, url: 'http://127.0.0.1:43563', token: 'session-token' }]);
let groupRunning = true;
const originalKill = process.kill.bind(process);
const signalGroup = jest.spyOn(process, 'kill').mockImplementation((pid, signal) => {
if (pid !== -4321) {
return originalKill(pid, signal);
}
if (signal === 0) {
if (groupRunning) {
return true;
}
throw Object.assign(new Error('No such process group'), { code: 'ESRCH' });
}
if (signal === 'SIGINT') {
void setTimeout(20).then(async () => {
const args = jest.mocked(spawn).mock.calls[0][1];
await writeFile(`${args[6]}/session.json`, JSON.stringify({ recording: 'recording.mp4' }));
groupRunning = false;
});
return true;
}
throw new Error(`Unexpected signal: ${signal}`);
});
const child = Object.assign(new EventEmitter(), {
pid: 4321,
stdout: new PassThrough(),
stderr: new PassThrough(),
kill: jest.fn(),
}) as unknown as ChildProcess;
jest.mocked(spawn).mockReturnValue(Object.assign(Promise.resolve(), { child }) as never);
await IosSimulatorRecordingUtils.startAsync({ env, logger });
await waitForRecorderSpawnAsync();
child.stderr?.emit('data', 'serve-sim:recording-started\n');
await setTimeout(2_100);
expect(spawn).toHaveBeenCalledTimes(1);
const recordings = await IosSimulatorRecordingUtils.finishAsync({ logger });
expect(signalGroup).toHaveBeenCalledWith(-4321, 'SIGINT');
expect(child.kill).not.toHaveBeenCalled();
expect(recordings).toHaveLength(1);
});
test('does not signal a recorder that exits before it reports its start', async () => {
let currentTime = Date.now();
jest.spyOn(Date, 'now').mockImplementation(() => currentTime);
jest
.mocked(readServeSimServersAsync)
.mockResolvedValue([{ udid, url: 'http://127.0.0.1:43563', token: 'session-token' }]);
const recorder = mockRecorder();
await IosSimulatorRecordingUtils.startAsync({ env, logger });
await waitForRecorderSpawnAsync();
const finishing = IosSimulatorRecordingUtils.finishAsync({ logger });
await setTimeout(50);
recorder.exit();
await setTimeout(50);
// Past the manifest wait, so the recorder's completion settles.
currentTime += 31_000;
expect(await finishing).toEqual([]);
expect(recorder.child.kill).not.toHaveBeenCalled();
expect(logger.warn).toHaveBeenCalledWith(
expect.anything(),
'Screen recording for iPhone 17 Pro exited before it started; it will be retried.'
);
});
test('kills a recorder that ignores SIGINT and reports one that survives SIGKILL', async () => {
let currentTime = Date.now();
jest.spyOn(Date, 'now').mockImplementation(() => currentTime);
jest
.mocked(readServeSimServersAsync)
.mockResolvedValue([{ udid, url: 'http://127.0.0.1:43563', token: 'session-token' }]);
const recorder = mockRecorder();
await IosSimulatorRecordingUtils.startAsync({ env, logger });
await waitForRecorderSpawnAsync();
recorder.child.stderr?.emit('data', 'GET /record?token=session-token\n');
const finishing = IosSimulatorRecordingUtils.finishAsync({ logger });
await setTimeout(50);
currentTime += 61_000;
expect(await finishing).toEqual([]);
expect(recorder.child.kill).toHaveBeenCalledWith('SIGINT');
expect(recorder.child.kill).toHaveBeenCalledWith('SIGKILL');
expect(logger.warn).toHaveBeenCalledWith(
expect.anything(),
expect.stringContaining(
'Screen recording for iPhone 17 Pro did not stop within 5 seconds of SIGINT and will be killed.'
)
);
expect(Sentry.capture).toHaveBeenCalledWith(
'iOS Simulator recording process for iPhone 17 Pro did not exit after SIGKILL.'
);
expect(JSON.stringify(jest.mocked(Sentry.capture).mock.calls)).not.toContain('session-token');
}, 20_000);
test('reports a finalized recording whose manifest is gone at finish', async () => {
let currentTime = Date.now();
jest.spyOn(Date, 'now').mockImplementation(() => currentTime);
jest
.mocked(readServeSimServersAsync)
.mockResolvedValue([{ udid, url: 'http://127.0.0.1:43563', token: 'session-token' }]);
const recorder = mockRecorder();
await IosSimulatorRecordingUtils.startAsync({ env, logger });
await waitForRecorderSpawnAsync();
const manifest = `${jest.mocked(spawn).mock.calls[0][1][6]}/session.json`;
recorder.child.stderr?.emit('data', 'serve-sim:recording-started\n');
await writeFile(manifest, JSON.stringify({ recording: 'recording.mp4' }));
recorder.exit();
await setTimeout(100);
await rm(manifest);
const finishing = IosSimulatorRecordingUtils.finishAsync({ logger });
await setTimeout(50);
currentTime += 31_000;
expect(await finishing).toEqual([]);
expect(Sentry.capture).toHaveBeenCalledWith(
'iOS Simulator screen recording ended without a manifest',
{ level: 'warning', extras: { deviceName: 'iPhone 17 Pro' } }
);
});
test.each([0, 20])(
'keeps a started recording that exits with an error when its manifest appears after %i ms',
async manifestDelayMs => {
jest
.mocked(readServeSimServersAsync)
.mockResolvedValue([{ udid, url: 'http://127.0.0.1:43563', token: 'session-token' }]);
const recorder = mockRecorder();
await IosSimulatorRecordingUtils.startAsync({ env, logger });
await waitForRecorderSpawnAsync();
const outputDirectory = jest.mocked(spawn).mock.calls[0][1][6];
recorder.child.stderr?.emit('data', 'serve-sim:recording-started\n');
const manifestWritten = setTimeout(manifestDelayMs).then(() =>
writeFile(`${outputDirectory}/session.json`, JSON.stringify({ recording: 'recording.mp4' }))
);
if (manifestDelayMs === 0) {
await manifestWritten;
}
recorder.fail(new Error('record-video exited with code 1'));
const recordings = await IosSimulatorRecordingUtils.finishAsync({ logger });
await manifestWritten;
expect(recordings).toEqual([
{
udid,
deviceName: 'iPhone 17 Pro',
runtimeDisplayName: 'iOS 26.4',
directory: outputDirectory,
},
]);
expect(logger.warn).not.toHaveBeenCalled();
expect(Sentry.capture).not.toHaveBeenCalled();
}
);
test('reports a started recorder that fails without a manifest', async () => {
let currentTime = Date.now();
jest.spyOn(Date, 'now').mockImplementation(() => currentTime);
jest
.mocked(readServeSimServersAsync)
.mockResolvedValue([{ udid, url: 'http://127.0.0.1:43563', token: 'session-token' }]);
const recorder = mockRecorder();
await IosSimulatorRecordingUtils.startAsync({ env, logger });
await waitForRecorderSpawnAsync();
recorder.child.stderr?.emit('data', 'serve-sim:recording-started\n');
recorder.fail(new Error('record-video exited with code 1'));
await setTimeout(50);
currentTime += 31_000;
await setTimeout(1_100);
expect(logger.warn).toHaveBeenCalledWith(
expect.anything(),
'Screen recording process failed for iPhone 17 Pro.'
);
expect(Sentry.capture).toHaveBeenCalledWith(
'iOS Simulator screen recording ended without a manifest',
expect.objectContaining({ level: 'warning' })
);
});
test('records every booted device with the first serve-sim package', async () => {
let currentTime = Date.now();
jest.spyOn(Date, 'now').mockImplementation(() => currentTime);
const secondUdid = '1C17CD51-5926-4838-951B-6D941C7BEDFF';
IosSimulatorRecordingUtils.useServeSimPackage('@expo/serve-sim@later');
jest
.mocked(IosSimulatorUtils.getAvailableDevicesAsync)
.mockResolvedValue([
{ udid, name: 'iPhone 17 Pro', runtimeDisplayName: 'iOS 26.4' } as never,
{ udid: secondUdid, name: 'iPhone 17', runtimeDisplayName: 'iOS 26.4' } as never,
]);
jest.mocked(readServeSimServersAsync).mockResolvedValue([
{ udid, url: 'http://127.0.0.1:43563', token: 'first-token' },
{ udid: secondUdid, url: 'http://127.0.0.1:43563', token: 'second-token' },
]);
const recorder = mockRecorder();
await IosSimulatorRecordingUtils.startAsync({ env, logger });
for (let attempt = 0; attempt < 50 && jest.mocked(spawn).mock.calls.length < 2; attempt++) {
await setTimeout(10);
}
const calls = jest.mocked(spawn).mock.calls.map(([, args]) => args);
expect(calls.map(args => args[4]).sort()).toEqual([udid, secondUdid].sort());
expect(calls.every(args => args[1] === '@expo/serve-sim@next')).toBe(true);
recorder.exit();
await setTimeout(50);
currentTime += 31_000;
await IosSimulatorRecordingUtils.finishAsync({ logger });
});
@@ -104,6 +104,39 @@ describe(listAgentDeviceArtifactsAsync, () => {
})
).rejects.toThrow('agent-device daemon does not expose artifact inventory.');
});
it('keeps a screenshot display rotation and drops an unknown one', async () => {
jest.mocked(fetch).mockResolvedValue(
new Response(
JSON.stringify({
artifacts: [
{
id: 'rotated',
artifactType: 'screenshot',
filename: 'rotated.png',
displayRotation: 'landscape-left',
},
{
id: 'future',
artifactType: 'screenshot',
filename: 'future.png',
displayRotation: 'sideways',
},
],
})
)
);
const artifacts = await listAgentDeviceArtifactsAsync({
daemonUrl: 'http://127.0.0.1:1234',
daemonToken: 'daemon-token',
});
expect(artifacts.map(artifact => artifact.displayRotation)).toEqual([
'landscape-left',
undefined,
]);
});
});
describe(uploadAgentDeviceArtifactAsync, () => {
@@ -181,6 +214,63 @@ describe(uploadAgentDeviceArtifactAsync, () => {
})
);
});
it('uploads a screenshot display rotation as artifact metadata', async () => {
const data = Buffer.from('png-data');
const ctx = {} as unknown as CustomBuildContext;
jest.mocked(fetch).mockResolvedValueOnce(new Response(Readable.from([data])));
jest
.mocked(uploadDeviceRunSessionArtifactAsync)
.mockImplementationOnce(async (_ctx, { stream }) => {
await readStreamAsync(stream);
});
await uploadAgentDeviceArtifactAsync(ctx, {
deviceRunSessionId: 'drs-id',
daemonUrl: 'http://127.0.0.1:1234',
daemonToken: 'daemon-token',
logger: createLoggerMock(),
artifact: {
id: 'artifact-id',
artifactType: 'screenshot',
filename: 'shot.png',
displayRotation: 'landscape-right',
},
});
expect(jest.mocked(uploadDeviceRunSessionArtifactAsync)).toHaveBeenCalledWith(
ctx,
expect.objectContaining({
kind: 'screenshot',
metadata: { displayRotation: 'landscape-right' },
})
);
});
it('uploads no metadata when the artifact has no display rotation', async () => {
const data = Buffer.from('artifact-data');
const ctx = {} as unknown as CustomBuildContext;
jest.mocked(fetch).mockResolvedValueOnce(new Response(Readable.from([data])));
jest
.mocked(uploadDeviceRunSessionArtifactAsync)
.mockImplementationOnce(async (_ctx, { stream }) => {
await readStreamAsync(stream);
});
await uploadAgentDeviceArtifactAsync(ctx, {
deviceRunSessionId: 'drs-id',
daemonUrl: 'http://127.0.0.1:1234',
daemonToken: 'daemon-token',
logger: createLoggerMock(),
artifact: { id: 'artifact-id', artifactType: 'screenshot', filename: 'shot.png' },
});
expect(jest.mocked(uploadDeviceRunSessionArtifactAsync).mock.calls[0][1]).not.toHaveProperty(
'metadata'
);
});
});
describe(pollAgentDeviceArtifactsForUploadAsync, () => {
@@ -16,6 +16,11 @@ import { createGlobalContextMock } from '../../../__tests__/utils/context';
import { CustomBuildContext } from '../../../customBuildContext';
import { Sentry } from '../../../sentry';
import { turtleFetch } from '../../../utils/turtleFetch';
import {
IosSimulatorRecordingUtils,
SERVE_SIM_STOP_GRACE_PERIOD_MS,
} from '../IosSimulatorRecordingUtils';
import * as remoteDeviceRunSession from '../remoteDeviceRunSession';
import { readServeSimServersAsync } from '../serveSimMetricsRecorder';
import { sleepAsync } from '../../../utils/retry';
import { uploadDeviceRunSessionScreenRecordingsAsync } from '../deviceRunSessionScreenRecordings';
@@ -592,19 +597,26 @@ describe(websiteOriginServeSimArgs, () => {
]);
});
it('names staging, its deploy previews and each website dev port on staging', () => {
it('names staging, its deploy previews and local website subdomains on staging', () => {
const args = websiteOriginServeSimArgs({ EXPO_STAGING: '1' } as BuildStepEnv);
expect(args.slice(0, 4)).toEqual([
expect(args).toEqual([
'--cors-origin',
'https://staging.expo.dev',
'--frame-ancestor',
'https://staging.expo.dev',
'--cors-origin',
'https://*.expo.dev',
'--frame-ancestor',
'https://*.expo.dev',
'--cors-origin',
'https://expo.test',
'--frame-ancestor',
'https://expo.test',
'--cors-origin',
'https://*.expo.test',
'--frame-ancestor',
'https://*.expo.test',
]);
expect(args).toContain('https://*.expo.dev');
expect(args).toContain('https://expo.test:13001');
expect(args).toContain('https://expo.test:13215');
expect(args).not.toContain('https://expo.test:13216');
expect(args).not.toContain('https://expo.dev');
});
it('names only https origins', () => {
@@ -614,12 +626,18 @@ describe(websiteOriginServeSimArgs, () => {
}
});
it('names the website dev ports on local, without the deploy-preview wildcard', () => {
it('names local website subdomains without the deploy-preview wildcard on local', () => {
for (const env of [{ EXPO_LOCAL: '1' }, { EXPO_LOCAL: '1', EXPO_STAGING: '1' }]) {
const args = websiteOriginServeSimArgs(env as BuildStepEnv);
expect(args).toContain('https://expo.test:13001');
expect(args).not.toContain('https://*.expo.dev');
expect(args.filter(value => value === 'https://expo.test')).toHaveLength(2);
expect(websiteOriginServeSimArgs(env as BuildStepEnv)).toEqual([
'--cors-origin',
'https://expo.test',
'--frame-ancestor',
'https://expo.test',
'--cors-origin',
'https://*.expo.test',
'--frame-ancestor',
'https://*.expo.test',
]);
}
});
});
@@ -692,6 +710,56 @@ describe(startNgrokTunnelAsync, () => {
});
});
describe('spawnDetached process group shutdown', () => {
const env = {} as BuildStepEnv;
beforeEach(() => {
const spawned = Object.assign(Promise.resolve(undefined), {
child: { pid: 4321, unref: jest.fn(), once: jest.fn() },
});
jest.mocked(spawn).mockReturnValue(spawned as never);
});
afterEach(() => {
jest.restoreAllMocks();
});
it('waits for a detached child after its package-manager wrapper exits', async () => {
let groupChecks = 0;
const kill = jest.spyOn(process, 'kill').mockImplementation((pid, signal) => {
if (pid === -4321 && signal === 0) {
groupChecks += 1;
if (groupChecks < 4) {
return true;
}
throw new Error('Process group exited');
}
if (pid === -4321 && signal === 'SIGTERM') {
return true;
}
throw new Error(`Unexpected process signal: ${pid} ${signal}`);
});
const detached = spawnDetached({ command: 'npx', args: [], env, stopGracePeriodMs: 90_000 });
await detached.stopAsync();
expect(jest.mocked(sleepAsync)).toHaveBeenCalledWith(100);
expect(kill).toHaveBeenCalledWith(-4321, 'SIGTERM');
expect(kill).not.toHaveBeenCalledWith(-4321, 'SIGKILL');
expect(kill).not.toHaveBeenCalledWith(4321, 0);
});
it('kills a detached child that outlives the shutdown deadline', async () => {
const kill = jest.spyOn(process, 'kill').mockReturnValue(true);
const detached = spawnDetached({ command: 'npx', args: [], env, stopGracePeriodMs: 0 });
await detached.stopAsync();
expect(kill).toHaveBeenCalledWith(-4321, 'SIGTERM');
expect(kill).toHaveBeenCalledWith(-4321, 'SIGKILL');
});
});
describe(startDeviceSessionHostAsync, () => {
const baseDomain = 'eas-simulator.ngrok.dev';
const turnArgs = [
@@ -748,6 +816,25 @@ describe(startDeviceSessionHostAsync, () => {
});
});
afterEach(() => {
jest.restoreAllMocks();
});
it('gives serve-sim its recording grace period on shutdown', async () => {
const spawnDetachedSpy = jest.spyOn(remoteDeviceRunSession, 'spawnDetached');
const host = await startDeviceSessionHostAsync(createCtxMock(), {
runtimePlatform: BuildRuntimePlatform.DARWIN,
env,
logger: createLoggerMock(),
timeoutMs: 10_000,
});
expect(spawnDetachedSpy).toHaveBeenCalledWith(
expect.objectContaining({ stopGracePeriodMs: SERVE_SIM_STOP_GRACE_PERIOD_MS })
);
await host.finishAsync();
});
it('installs ffmpeg before starting expo-device-hub for Linux', async () => {
const packageVersion = '1.2.3';
const close = jest.fn().mockResolvedValue(undefined);
@@ -940,6 +1027,7 @@ describe(startDeviceSessionHostAsync, () => {
});
it('launches serve-sim with bun x when EAS_OVERRIDE_PACKAGE_MANAGER is bun', async () => {
const usePackage = jest.spyOn(IosSimulatorRecordingUtils, 'useServeSimPackage');
const close = jest.fn().mockResolvedValue(undefined);
jest.mocked(ngrok.forward).mockResolvedValue({
url: () => 'https://ios-preview.example.test',
@@ -969,6 +1057,7 @@ describe(startDeviceSessionHostAsync, () => {
packageVersion: '4.5.6',
}),
]);
expect(usePackage).toHaveBeenCalledWith('@expo/[email protected]');
await host.finishAsync();
expect(close).toHaveBeenCalledTimes(1);
@@ -20,6 +20,11 @@ const AgentDeviceArtifactSchema = z.object({
id: z.string(),
artifactType: z.string().nullish(),
filename: z.string(),
// Display rotation of a screenshot. An unknown value is dropped so it cannot block the upload.
displayRotation: z
.enum(['portrait', 'portrait-upside-down', 'landscape-left', 'landscape-right'])
.nullish()
.catch(undefined),
});
const AgentDeviceArtifactsListResponseSchema = z.object({
artifacts: z.array(AgentDeviceArtifactSchema),
@@ -154,6 +159,9 @@ export async function uploadAgentDeviceArtifactAsync(
name: `${artifact.filename} (${artifact.id})`,
filename: artifact.filename,
kind: artifact.artifactType ?? undefined,
...(artifact.displayRotation
? { metadata: { displayRotation: artifact.displayRotation } }
: {}),
size,
stream: createReadStream(temporaryArtifactPath),
});
@@ -35,6 +35,10 @@ import {
startNgrokTunnelAsync,
} from './remoteDeviceRunSession';
import { withDeviceRunSessionTimeoutAsync } from './deviceRunSessionTimeout';
import {
IosSimulatorRecordingUtils,
SERVE_SIM_STOP_GRACE_PERIOD_MS,
} from './IosSimulatorRecordingUtils';
import { SERVE_SIM_STATE_DIR, readServeSimServersAsync } from './serveSimMetricsRecorder';
const WEB_PREVIEW_HOST = '127.0.0.1';
@@ -68,12 +72,8 @@ export function simulatorPreviewPageUrl(env: BuildStepEnv, subdomainId: string):
return new URL(`/simulator-preview/${subdomainId}`, websiteOrigin(env)).toString();
}
// Website dev servers on expo.test use staging sessions, and CORS matches ports exactly.
const WEBSITE_DEV_ORIGINS = [
'https://expo.test',
'https://expo.test:13001',
...Array.from({ length: 16 }, (_, index) => `https://expo.test:${13200 + index}`),
];
// Local website hosts are shared by local and staging simulator sessions.
const WEBSITE_DEV_ORIGINS = ['https://expo.test', 'https://*.expo.test'];
export function websiteOriginServeSimArgs(env: BuildStepEnv): string[] {
const origins = new Set([websiteOrigin(env)]);
@@ -364,7 +364,10 @@ export async function startDeviceSessionHostAsync(
EXPO_DEVICE_HUB_SCREENSHOT_DIRECTORY: screenshots.directory,
...(recording ? { EXPO_DEVICE_HUB_RECORDING_CONTROL_TOKEN: recording.controlToken } : {}),
},
stopGracePeriodMs: recording ? RECORDING_STOP_GRACE_PERIOD_MS : undefined,
// Only an Android host records through expo-device-hub; serve-sim records on iOS.
stopGracePeriodMs: recording
? RECORDING_STOP_GRACE_PERIOD_MS
: SERVE_SIM_STOP_GRACE_PERIOD_MS,
});
} catch (error) {
// Nothing was spawned, so nothing can still write into the directory.
@@ -462,6 +465,7 @@ export async function startDeviceSessionHostAsync(
'report it if it repeats.'
);
}
IosSimulatorRecordingUtils.useServeSimPackage(packageSpec);
}
return host;
} catch (error) {
@@ -5,6 +5,9 @@ import * as jose from 'jose';
import fetch from 'node-fetch';
import { ZodError, z } from 'zod';
import { isConnectFailure, isConnectionInterruptedError } from '../../../utils/networkErrors';
import { promiseRetryWithCondition } from '../../../utils/promiseRetryWithCondition';
const TOKEN_LIFETIME_SECONDS = 20 /* minutes */ * 60 /* seconds */;
const TOKEN_REFRESH_MARGIN_SECONDS = 60 /* seconds */;
@@ -32,6 +35,27 @@ const AscErrorResponseSchema = z.object({
});
const GetApi = {
// https://developer.apple.com/documentation/appstoreconnectapi/get-v1-builds-_id_-buildbetadetail
'/v1/builds/:id/buildBetaDetail': {
path: z.object({ id: z.string() }),
request: z.object({}),
response: z.object({
data: z.object({
id: z.string(),
attributes: z.object({
internalBuildState: z.enum([
'PROCESSING',
'PROCESSING_EXCEPTION',
'MISSING_EXPORT_COMPLIANCE',
'READY_FOR_BETA_TESTING',
'IN_BETA_TESTING',
'EXPIRED',
'IN_EXPORT_COMPLIANCE_REVIEW',
]),
}),
}),
}),
},
// https://developer.apple.com/documentation/appstoreconnectapi/get-v1-builds-_id_-app
'/v1/builds/:id/app': {
path: z.object({ id: z.string() }),
@@ -538,7 +562,22 @@ export class AscApiClient {
});
}
private async sendRequestAsync({
private async sendRequestAsync(
args: Parameters<AscApiClient['sendRequestOnceAsync']>[0]
): Promise<any> {
return await promiseRetryWithCondition(
() => this.sendRequestOnceAsync(args),
error =>
isConnectFailure(error) || (args.method === 'GET' && isConnectionInterruptedError(error)),
{ retries: 3, factor: 2, minTimeout: 100 },
({ attemptNumber, maxAttemptsCount, error }) =>
this.logger?.warn(
`Retrying Apple request (${args.method} ${args.path}, attempt ${attemptNumber}/${maxAttemptsCount}): ${String(error)}`
)
)();
}
private async sendRequestOnceAsync({
path,
method,
body,
@@ -1,8 +1,12 @@
import { SystemError, UserError } from '@expo/eas-build-job';
import { bunyan } from '@expo/logger';
import fs from 'fs-extra';
import * as jose from 'jose';
import { z } from 'zod';
import { isConnectionInterruptedError } from '../../../utils/networkErrors';
import { promiseRetryWithCondition } from '../../../utils/promiseRetryWithCondition';
import {
AscApiClient,
AscApiClientGetApi,
@@ -13,6 +17,65 @@ import {
} from './AscApiClient';
export namespace AscApiUtils {
export async function commitBuildUploadFileAsync({
client,
fileId,
logger,
}: {
client: AscApiClient;
fileId: string;
logger: bunyan;
}): Promise<void> {
try {
await promiseRetryWithCondition(
async () => {
const { data } = await client.getAsync(
'/v1/buildUploadFiles/:id',
{ 'fields[buildUploadFiles]': ['assetDeliveryState'] },
{ id: fileId }
);
if (['COMPLETE', 'FAILED'].includes(data.attributes.assetDeliveryState.state)) {
return;
}
await client.patchAsync(
'/v1/buildUploadFiles/:id',
{ data: { type: 'buildUploadFiles', id: fileId, attributes: { uploaded: true } } },
{ id: fileId }
);
},
error => isConnectionInterruptedError(error),
{ retries: 3, factor: 1, minTimeout: 2000 },
({ attemptNumber, maxAttemptsCount, error }) =>
logger.warn(
{ err: error },
`Checking upload file before commit attempt ${attemptNumber}/${maxAttemptsCount}.`
)
)();
} catch (error) {
if (!isConnectionInterruptedError(error)) {
throw error;
}
// Apple may have accepted the final commit even though its response was lost.
try {
const { data } = await client.getAsync(
'/v1/buildUploadFiles/:id',
{ 'fields[buildUploadFiles]': ['assetDeliveryState'] },
{ id: fileId }
);
if (data.attributes.assetDeliveryState.state === 'COMPLETE') {
logger.info(`Upload file ${fileId}: COMPLETE confirmed after the final commit error.`);
return;
}
} catch (readError) {
logger.warn(
{ err: readError },
`Could not check upload file ${fileId} after the final commit error.`
);
}
throw error;
}
}
export async function getAllBetaBuildLocalizationsAsync({
client,
buildId,
@@ -468,14 +468,15 @@ describe(AscApiClient, () => {
it('throws regular Error for non-structured ASC error payload', async () => {
const appId = '1491144534';
nock('https://api.appstoreconnect.apple.com')
const scope = nock('https://api.appstoreconnect.apple.com')
.get(`/v1/apps/${appId}`)
.query({ 'fields[apps]': 'bundleId,name' })
.reply(503, 'Service unavailable');
await expect(
client.getAsync('/v1/apps/:id', { 'fields[apps]': ['bundleId', 'name'] }, { id: appId })
).rejects.not.toBeInstanceOf(AscApiRequestError);
).rejects.toThrow('Unexpected response (503)');
expect(scope.isDone()).toBe(true);
});
it('throws controlled error when success response body is not valid JSON', async () => {
@@ -490,4 +491,108 @@ describe(AscApiClient, () => {
client.getAsync('/v1/apps/:id', { 'fields[apps]': ['bundleId', 'name'] }, { id: appId })
).rejects.toThrow('Malformed JSON response from App Store Connect (200): not-json');
});
it.each(['ECONNRESET', 'EAI_AGAIN'])('recovers a status read after %s', async code => {
const fixture = require('./fixtures/buildUploadFiles/get-buildUploadFiles-200.json');
const scope = nock('https://api.appstoreconnect.apple.com')
.get('/v1/buildUploadFiles/file')
.query(true)
.replyWithError({ code, message: 'Temporary network failure' })
.get('/v1/buildUploadFiles/file')
.query(true)
.reply(200, fixture);
await expect(
client.getAsync(
'/v1/buildUploadFiles/:id',
{ 'fields[buildUploadFiles]': ['assetDeliveryState'] },
{ id: 'file' }
)
).resolves.toMatchObject({ data: { id: fixture.data.id } });
expect(scope.isDone()).toBe(true);
});
it.each([401, 422, 429, 503])('does not retry permanent HTTP %s errors', async status => {
const scope = nock('https://api.appstoreconnect.apple.com')
.get('/v1/apps/app')
.query(true)
.reply(status, { errors: [{ status: String(status) }] });
await expect(
client.getAsync(
'/v1/apps/:id',
{
'fields[apps]': ['bundleId', 'name'],
},
{ id: 'app' }
)
).rejects.toBeInstanceOf(AscApiRequestError);
expect(scope.isDone()).toBe(true);
});
it('does not retry creating a localization after a lost response', async () => {
const scope = nock('https://api.appstoreconnect.apple.com')
.post('/v1/betaBuildLocalizations')
.replyWithError({ code: 'ECONNRESET', message: 'Lost response' });
await expect(
client.postAsync('/v1/betaBuildLocalizations', {
data: {
type: 'betaBuildLocalizations',
attributes: { locale: 'en-US', whatsNew: 'Hello' },
relationships: { build: { data: { type: 'builds', id: 'build' } } },
},
})
).rejects.toMatchObject({ code: 'ECONNRESET' });
expect(scope.isDone()).toBe(true);
});
it('retries connection establishment failure before a creation request', async () => {
const scope = nock('https://api.appstoreconnect.apple.com')
.post('/v1/betaBuildLocalizations')
.replyWithError({ code: 'EAI_AGAIN', message: 'DNS failure' })
.post('/v1/betaBuildLocalizations')
.reply(201, { data: { id: 'localization' } });
await expect(
client.postAsync('/v1/betaBuildLocalizations', {
data: {
type: 'betaBuildLocalizations',
attributes: { locale: 'en-US', whatsNew: 'Hello' },
relationships: { build: { data: { type: 'builds', id: 'build' } } },
},
})
).resolves.toEqual({ data: { id: 'localization' } });
expect(scope.isDone()).toBe(true);
});
it('stops after three retries of an interrupted read', async () => {
const scope = nock('https://api.appstoreconnect.apple.com')
.get('/v1/apps/app')
.query(true)
.times(4)
.replyWithError({ code: 'ECONNRESET', message: 'Connection reset' });
await expect(
client.getAsync(
'/v1/apps/:id',
{
'fields[apps]': ['bundleId', 'name'],
},
{ id: 'app' }
)
).rejects.toMatchObject({ code: 'ECONNRESET' });
expect(scope.isDone()).toBe(true);
});
it('does not repeat an interrupted changelog update', async () => {
const body = {
data: { type: 'betaBuildLocalizations', id: 'locale', attributes: { whatsNew: 'Hello' } },
} as const;
const scope = nock('https://api.appstoreconnect.apple.com')
.patch('/v1/betaBuildLocalizations/locale', body)
.replyWithError({ code: 'ECONNRESET', message: 'Response lost' })
.patch('/v1/betaBuildLocalizations/locale', body)
.optionally()
.reply(200, { data: { id: 'locale' } });
await expect(
client.patchAsync('/v1/betaBuildLocalizations/:id', body, { id: 'locale' })
).rejects.toMatchObject({ code: 'ECONNRESET' });
expect(scope.isDone()).toBe(true);
});
});
@@ -1,3 +1,4 @@
import { createLogger } from '@expo/logger';
import fs from 'fs-extra';
import * as jose from 'jose';
import nock from 'nock';
@@ -321,3 +322,112 @@ describe('AscApiUtils', () => {
});
});
});
describe('commitBuildUploadFileAsync', () => {
const logger = createLogger({ name: 'test' });
const fileResponse = (state: string) => ({
data: {
type: 'buildUploadFiles',
id: 'file',
attributes: { assetDeliveryState: { state } },
},
});
let client: AscApiClient;
beforeAll(async () => {
const { privateKey } = await jose.generateKeyPair('ES256');
client = new AscApiClient({ key: { keyId: 'TESTKEY', privateKey } });
nock.disableNetConnect();
});
afterAll(() => nock.enableNetConnect());
afterEach(() => {
try {
expect(nock.pendingMocks()).toEqual([]);
} finally {
nock.cleanAll();
}
});
it.each(['COMPLETE', 'FAILED'])('does not commit a terminal file: %s', async state => {
nock('https://api.appstoreconnect.apple.com')
.get('/v1/buildUploadFiles/file')
.query(true)
.reply(200, fileResponse(state));
await expect(
AscApiUtils.commitBuildUploadFileAsync({ client, fileId: 'file', logger })
).resolves.toBeUndefined();
});
it.each(['COMPLETE', 'AWAITING_UPLOAD'])(
'reads the file before retrying an interrupted commit: %s',
async state => {
const scope = nock('https://api.appstoreconnect.apple.com')
.get('/v1/buildUploadFiles/file')
.query(true)
.reply(200, fileResponse('AWAITING_UPLOAD'))
.patch('/v1/buildUploadFiles/file')
.replyWithError({ code: 'ECONNRESET', message: 'Lost response' })
.get('/v1/buildUploadFiles/file')
.query(true)
.reply(200, fileResponse(state));
if (state === 'AWAITING_UPLOAD') {
scope.patch('/v1/buildUploadFiles/file').reply(200, fileResponse('COMPLETE'));
}
await expect(
AscApiUtils.commitBuildUploadFileAsync({ client, fileId: 'file', logger })
).resolves.toBeUndefined();
}
);
it.each([
[401, 'NOT_AUTHORIZED'],
[403, 'FORBIDDEN'],
[409, 'STATE_ERROR.INVALID_STATE'],
])('preserves Apple errors without retrying: %s %s', async (status, code) => {
nock('https://api.appstoreconnect.apple.com')
.get('/v1/buildUploadFiles/file')
.query(true)
.reply(200, fileResponse('UPLOAD_COMPLETE'))
.patch('/v1/buildUploadFiles/file')
.reply(status, { errors: [{ code }] });
await expect(
AscApiUtils.commitBuildUploadFileAsync({ client, fileId: 'file', logger })
).rejects.toMatchObject({ status, code });
});
it('does not commit when the initial state read fails', async () => {
nock('https://api.appstoreconnect.apple.com')
.get('/v1/buildUploadFiles/file')
.query(true)
.reply(503, 'Unavailable');
await expect(
AscApiUtils.commitBuildUploadFileAsync({ client, fileId: 'file', logger })
).rejects.toThrow('503');
});
it.each(['COMPLETE', 'AWAITING_UPLOAD', 'UPLOAD_COMPLETE', 'FAILED', 'read-error'])(
'checks the file after the final interrupted commit: %s',
async finalState => {
const scope = nock('https://api.appstoreconnect.apple.com')
.get('/v1/buildUploadFiles/file')
.query(true)
.times(4)
.reply(200, fileResponse('AWAITING_UPLOAD'))
.patch('/v1/buildUploadFiles/file')
.times(4)
.replyWithError({ code: 'ECONNRESET', message: 'Lost response' });
const finalRead = scope.get('/v1/buildUploadFiles/file').query(true);
if (finalState === 'read-error') {
finalRead.reply(503, 'Unavailable');
} else {
finalRead.reply(200, fileResponse(finalState));
}
const result = AscApiUtils.commitBuildUploadFileAsync({ client, fileId: 'file', logger });
if (finalState === 'COMPLETE') {
await expect(result).resolves.toBeUndefined();
} else {
await expect(result).rejects.toMatchObject({ code: 'ECONNRESET' });
}
},
15_000
);
});
@@ -97,7 +97,6 @@ async function resolvePackagedFileAsync(binDir: string, file: string): Promise<s
}
}
/** The packaged library, next to the compiled package like record-sim. */
export async function resolveEgressGuardLibraryAsync(
binDir: string = PACKAGED_BIN_DIR
): Promise<string | null> {
@@ -26,6 +26,7 @@ import {
parseOpenUrlInput,
} from '../functions/launchApplication';
import { Sentry } from '../../sentry';
import { isProcessGroupRunning } from '../../utils/processes';
import { sleepAsync } from '../../utils/retry';
import { turtleFetch } from '../../utils/turtleFetch';
@@ -548,7 +549,7 @@ async function stopDetachedProcessAsync(
pid: number | undefined,
gracePeriodMs = 5_000
): Promise<void> {
if (pid === undefined || !isProcessRunning(pid)) {
if (pid === undefined || !isProcessGroupRunning(pid)) {
return;
}
try {
@@ -564,10 +565,10 @@ async function stopDetachedProcessAsync(
}
const deadline = Date.now() + gracePeriodMs;
while (Date.now() < deadline && isProcessRunning(pid)) {
while (Date.now() < deadline && isProcessGroupRunning(pid)) {
await sleepAsync(100);
}
if (!isProcessRunning(pid)) {
if (!isProcessGroupRunning(pid)) {
return;
}
try {
@@ -579,7 +580,7 @@ async function stopDetachedProcessAsync(
}
// kill(pid, 0) succeeds on the zombie until libuv reaps it on a later loop turn.
const killDeadline = Date.now() + 5_000;
while (Date.now() < killDeadline && isProcessRunning(pid)) {
while (Date.now() < killDeadline && isProcessGroupRunning(pid)) {
await sleepAsync(100);
}
}
@@ -0,0 +1,36 @@
import { isConnectFailure, isConnectionInterruptedError } from '../networkErrors';
it.each([
'EAI_AGAIN',
'ECONNREFUSED',
'EHOSTDOWN',
'EHOSTUNREACH',
'ENETDOWN',
'ENETUNREACH',
'ENOTFOUND',
])('recognizes wrapped %s as a connection establishment failure', code => {
const error = new Error('Request failed', {
cause: new TypeError('fetch failed', {
cause: Object.assign(new Error('connect failed'), { code }),
}),
});
expect(isConnectFailure(error)).toBe(true);
expect(isConnectionInterruptedError(error)).toBe(false);
});
it.each(['ECONNRESET', 'UND_ERR_SOCKET'])(
'requires caller-specific recovery for wrapped %s',
code => {
const error = new Error('Request failed', {
cause: Object.assign(new Error('response lost'), { code }),
});
expect(isConnectFailure(error)).toBe(false);
expect(isConnectionInterruptedError(error)).toBe(true);
}
);
it.each(['ETIMEDOUT', 'CERT_HAS_EXPIRED', undefined])('does not automatically retry %s', code => {
const error = Object.assign(new Error('Request failed'), { code });
expect(isConnectFailure(error)).toBe(false);
expect(isConnectionInterruptedError(error)).toBe(false);
});
@@ -1,6 +1,6 @@
import { ChildProcess } from 'node:child_process';
import { isChildProcessAlive } from '../processes';
import { isChildProcessAlive, isProcessGroupRunning } from '../processes';
function child(partial: {
exitCode: number | null;
@@ -41,3 +41,29 @@ describe(isChildProcessAlive, () => {
);
});
});
describe(isProcessGroupRunning, () => {
afterEach(() => {
jest.restoreAllMocks();
});
it('signals the group, not the leader', () => {
const kill = jest.spyOn(process, 'kill').mockReturnValue(true);
expect(isProcessGroupRunning(4321)).toBe(true);
expect(kill).toHaveBeenCalledWith(-4321, 0);
});
it('is not running once the group is gone', () => {
jest.spyOn(process, 'kill').mockImplementation(() => {
throw Object.assign(new Error('No such process'), { code: 'ESRCH' });
});
expect(isProcessGroupRunning(4321)).toBe(false);
});
it('is running when the group exists but cannot be signaled', () => {
jest.spyOn(process, 'kill').mockImplementation(() => {
throw Object.assign(new Error('Operation not permitted'), { code: 'EPERM' });
});
expect(isProcessGroupRunning(4321)).toBe(true);
});
});
@@ -0,0 +1,29 @@
import { promiseRetryWithCondition } from '../promiseRetryWithCondition';
it('reports the previous error and retry budget only when the next attempt starts', async () => {
const errors = [new Error('First failure'), new Error('Second failure')];
const fn = jest
.fn()
.mockRejectedValueOnce(errors[0])
.mockRejectedValueOnce(errors[1])
.mockResolvedValue('done');
const onRetry = jest.fn();
await expect(
promiseRetryWithCondition(fn, () => true, { retries: 2, minTimeout: 1 }, onRetry)()
).resolves.toBe('done');
expect(onRetry.mock.calls).toEqual([
[{ attemptNumber: 2, maxAttemptsCount: 3, error: errors[0] }],
[{ attemptNumber: 3, maxAttemptsCount: 3, error: errors[1] }],
]);
});
it('does not report a retry after the final failure', async () => {
const error = new Error('Persistent failure');
const fn = jest.fn().mockRejectedValue(error);
const onRetry = jest.fn();
await expect(
promiseRetryWithCondition(fn, () => true, { retries: 1, minTimeout: 1 }, onRetry)()
).rejects.toBe(error);
expect(onRetry.mock.calls).toEqual([[{ attemptNumber: 2, maxAttemptsCount: 2, error }]]);
expect(fn).toHaveBeenCalledTimes(2);
});
@@ -0,0 +1,32 @@
const CONNECT_FAILURE_CODES = new Set([
'EAI_AGAIN',
'ECONNREFUSED',
'EHOSTDOWN',
'EHOSTUNREACH',
'ENETDOWN',
'ENETUNREACH',
'ENOTFOUND',
]);
export function isConnectFailure(error: unknown): boolean {
for (let current = error; current instanceof Error; current = current.cause) {
if (
'code' in current &&
typeof current.code === 'string' &&
CONNECT_FAILURE_CODES.has(current.code)
) {
return true;
}
}
return false;
}
// The server may already have applied a request. The caller must decide whether repetition is safe.
export function isConnectionInterruptedError(error: unknown): boolean {
for (let current = error; current instanceof Error; current = current.cause) {
if ('code' in current && (current.code === 'ECONNRESET' || current.code === 'UND_ERR_SOCKET')) {
return true;
}
}
return false;
}
@@ -6,6 +6,16 @@ export function isChildProcessAlive(child: ChildProcess): boolean {
return child.exitCode === null && child.signalCode === null && !child.killed;
}
/** Whether any process in the group led by `pgid` exists. EPERM means it does. */
export function isProcessGroupRunning(pgid: number): boolean {
try {
process.kill(-pgid, 0);
return true;
} catch (error: any) {
return error?.code === 'EPERM';
}
}
/**
* Kill a detached spawn's process group. Negated pid targets the group so bash/sleep
* children cannot survive after the parent is gone (e.g. across upterm redial).
@@ -1,20 +1,30 @@
import promiseRetry from 'promise-retry';
import { OperationOptions } from 'retry';
export function promiseRetryWithCondition<TFn extends (...args: any[]) => Promise<any>>(
fn: TFn,
retryConditionFn: (error: any) => boolean,
options: OperationOptions = { retries: 3, factor: 2 }
options: { retries: number; factor?: number; minTimeout?: number; maxTimeout?: number } = {
retries: 3,
factor: 2,
},
onRetry?: (params: { attemptNumber: number; maxAttemptsCount: number; error: unknown }) => void
): (...funcArgs: Parameters<TFn>) => Promise<ReturnType<TFn>> {
return (...funcArgs) =>
promiseRetry<ReturnType<TFn>>(async retry => {
return (...funcArgs) => {
let lastError: unknown;
const maxAttemptsCount = options.retries + 1;
return promiseRetry<ReturnType<TFn>>(async (retry, attemptNumber) => {
if (attemptNumber > 1) {
onRetry?.({ attemptNumber, maxAttemptsCount, error: lastError });
}
try {
return await fn(...funcArgs);
} catch (e) {
if (retryConditionFn(e)) {
lastError = e;
retry(e);
}
throw e;
}
}, options);
};
}
+170 -148
View File
File diff suppressed because it is too large. Load diff
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "eas-cli",
"version": "24.10.0",
"version": "24.11.0",
"description": "EAS command line tool",
"keywords": [
"cli",
@@ -121,6 +121,16 @@ export async function getAnalyticsEnabledAsync(): Promise<boolean> {
return !!analyticsEnabled;
}
/**
* Returns true when the user opted out of analytics with DISABLE_EAS_ANALYTICS or `eas analytics off`.
*/
export async function isAnalyticsOptedOutAsync(): Promise<boolean> {
if (process.env.DISABLE_EAS_ANALYTICS) {
return true;
}
return (await UserSettings.getAsync(USER_SETTINGS_KEY_ANALYTICS_ENABLED, true)) === false;
}
/**
* Create an instance of Analytics based on the user's analytics enabled preferences.
*/
@@ -1,7 +1,7 @@
import RudderAnalytics from '@expo/rudder-sdk-node';
import UserSettings from '../../user/UserSettings';
import { CommandEvent, createAnalyticsAsync } from '../AnalyticsManager';
import { CommandEvent, createAnalyticsAsync, isAnalyticsOptedOutAsync } from '../AnalyticsManager';
import { getAgentTelemetryContext } from '../agent';
import { getSandboxTelemetryContext } from '../sandbox';
@@ -122,3 +122,21 @@ it('adds detected sandbox context to analytics events', async () => {
})
);
});
describe(isAnalyticsOptedOutAsync, () => {
it('is false by default', async () => {
await expect(isAnalyticsOptedOutAsync()).resolves.toBe(false);
});
it('is true when DISABLE_EAS_ANALYTICS is set', async () => {
process.env.DISABLE_EAS_ANALYTICS = '1';
await expect(isAnalyticsOptedOutAsync()).resolves.toBe(true);
});
it('is true when analytics were turned off with eas analytics off', async () => {
userSettingsMock.getAsync.mockImplementation(async (key, defaultValue) =>
key === 'analyticsEnabled' ? false : defaultValue
);
await expect(isAnalyticsOptedOutAsync()).resolves.toBe(true);
});
});
@@ -0,0 +1,180 @@
import { getMockOclifConfig } from '../../../__tests__/commands/utils';
import Log from '../../../log';
import { confirmAsync, promptAsync } from '../../../prompts';
import SessionManager from '../../../user/SessionManager';
import { resumeDeviceLoginAsync, startDeviceLoginAsync } from '../../../user/deviceLogin';
import AccountLogin from '../login';
jest.mock('../../../prompts');
jest.mock('../../../log');
jest.mock('../../../user/deviceLogin', () => ({
...jest.requireActual('../../../user/deviceLogin'),
startDeviceLoginAsync: jest.fn(),
resumeDeviceLoginAsync: jest.fn(),
}));
const pending = {
request_id: 'request-id',
status: 'authorization_pending',
verification_uri: 'https://expo.dev/oauth/device',
verification_uri_complete: 'https://expo.dev/oauth/device?user_code=BCDF-GHJK',
user_code: 'BCDF-GHJK',
expires_at: '2026-10-01T00:00:00Z',
retry_after: 5,
} satisfies Awaited<ReturnType<typeof startDeviceLoginAsync>>;
const resumeCommand = `eas login --device --non-interactive --resume ${pending.request_id}`;
const sessionManager = new SessionManager({
logEvent: jest.fn(),
setActor: jest.fn(),
flushAsync: jest.fn(),
});
const originalIsTTY = process.stdin.isTTY;
const originalCI = process.env.CI;
function command(args: string[]): AccountLogin {
const instance = new AccountLogin(['--device', ...args], getMockOclifConfig());
// @ts-expect-error Mock the protected context without initializing a real session.
jest.spyOn(instance, 'getContextAsync').mockResolvedValue({
sessionManager,
maybeLoggedIn: { actor: null },
});
return instance;
}
beforeEach(() => {
jest.clearAllMocks();
process.stdin.isTTY = false;
delete process.env.CI;
jest.spyOn(sessionManager, 'getAccessToken').mockReturnValue(null);
jest.spyOn(global, 'setTimeout');
});
afterEach(() => {
try {
expect(confirmAsync).not.toHaveBeenCalled();
expect(promptAsync).not.toHaveBeenCalled();
expect(setTimeout).not.toHaveBeenCalled();
} finally {
process.stdin.isTTY = originalIsTTY;
if (originalCI === undefined) {
delete process.env.CI;
} else {
process.env.CI = originalCI;
}
jest.restoreAllMocks();
}
});
it('starts once with closed stdin and explains optional matching', async () => {
jest.mocked(startDeviceLoginAsync).mockResolvedValue(pending);
await command([]).runAsync();
expect(Log.log).toHaveBeenCalledWith(`Open ${pending.verification_uri_complete}`);
expect(Log.log).toHaveBeenCalledWith(`Code: ${pending.user_code}`);
expect(Log.log).toHaveBeenCalledWith(
'Ask the user to approve the login in their browser and send you any number shown.'
);
expect(Log.log).toHaveBeenCalledWith(
`Then run ${resumeCommand}, adding --match NUMBER_FROM_USER if they saw a number.`
);
expect(startDeviceLoginAsync).toHaveBeenCalledTimes(1);
expect(resumeDeviceLoginAsync).not.toHaveBeenCalled();
});
it.each([
['--non-interactive', undefined],
[undefined, '1'],
] as const)('runs one step with flag %s and CI=%s in a TTY', async (flag, ci) => {
process.stdin.isTTY = true;
if (ci) {
process.env.CI = ci;
}
jest.mocked(startDeviceLoginAsync).mockResolvedValue(pending);
await command(flag ? [flag] : []).runAsync();
expect(startDeviceLoginAsync).toHaveBeenCalledTimes(1);
expect(Log.log).toHaveBeenCalledWith(
'Ask the user to approve the login in their browser and send you any number shown.'
);
expect(Log.log).toHaveBeenCalledWith(
`Then run ${resumeCommand}, adding --match NUMBER_FROM_USER if they saw a number.`
);
});
it('reports pending approval and when to retry', async () => {
jest.mocked(resumeDeviceLoginAsync).mockResolvedValue(pending);
await command(['--resume', pending.request_id]).runAsync();
expect(resumeDeviceLoginAsync).toHaveBeenCalledTimes(1);
expect(resumeDeviceLoginAsync).toHaveBeenCalledWith(
pending.request_id,
sessionManager,
undefined
);
expect(Log.log).toHaveBeenCalledWith('Approval pending. Retry after 5 seconds:');
expect(Log.log).toHaveBeenCalledWith(resumeCommand);
});
it('requests the browser number when matching is required', async () => {
jest.mocked(resumeDeviceLoginAsync).mockResolvedValue({
...pending,
status: 'matching_required',
match_options: ['12', '42', '87'],
});
await command(['--resume', pending.request_id]).runAsync();
expect(Log.log).toHaveBeenCalledWith(
'Ask the user for the number shown in their browser, then run:'
);
expect(Log.log).toHaveBeenCalledWith(`${resumeCommand} --match NUMBER_FROM_USER`);
expect(resumeDeviceLoginAsync).toHaveBeenCalledTimes(1);
});
it('preserves the supplied number when matching must be retried', async () => {
jest.mocked(resumeDeviceLoginAsync).mockResolvedValue({
...pending,
status: 'slow_down',
retry_after: 10,
});
await command(['--resume', pending.request_id, '--match', '42']).runAsync();
expect(resumeDeviceLoginAsync).toHaveBeenCalledWith(pending.request_id, sessionManager, '42');
expect(Log.log).toHaveBeenCalledWith('Approval pending. Retry after 10 seconds:');
expect(Log.log).toHaveBeenCalledWith(`${resumeCommand} --match 42`);
});
it.each([
{ label: 'with a browser number', args: ['--match', '42'], match: '42' },
{ label: 'without a browser number', args: [], match: undefined },
])('authenticates on the first resume $label', async ({ args, match }) => {
jest.mocked(resumeDeviceLoginAsync).mockResolvedValue({
request_id: pending.request_id,
status: 'authenticated',
username: 'demo-user',
});
await command(['--resume', pending.request_id, ...args]).runAsync();
expect(Log.log).toHaveBeenCalledWith('Logged in as demo-user');
expect(resumeDeviceLoginAsync).toHaveBeenCalledWith(pending.request_id, sessionManager, match);
expect(resumeDeviceLoginAsync).toHaveBeenCalledTimes(1);
});
it.each(['access_denied', 'expired_token', 'invalid_grant'] as const)(
'fails on %s without retrying',
async status => {
jest
.mocked(resumeDeviceLoginAsync)
.mockResolvedValue({ request_id: pending.request_id, status });
await expect(command(['--resume', pending.request_id]).runAsync()).rejects.toThrow(
`Device login failed (${status}). Start again with eas login --device.`
);
expect(resumeDeviceLoginAsync).toHaveBeenCalledTimes(1);
}
);
it('rejects the removed JSON flag', async () => {
await expect(command(['--json']).runAsync()).rejects.toThrow();
expect(startDeviceLoginAsync).not.toHaveBeenCalled();
});
it('requires device login for non-interactive sign-in', async () => {
const instance = new AccountLogin(['--non-interactive'], getMockOclifConfig());
await expect(instance.runAsync()).rejects.toThrow(
'Use eas login --device --non-interactive to log in without prompts.'
);
});
+107 -8
View File
@@ -2,15 +2,40 @@ import { Errors, Flags } from '@oclif/core';
import chalk from 'chalk';
import EasCommand from '../../commandUtils/EasCommand';
import { EASNonInteractiveFlag } from '../../commandUtils/flags';
import Log from '../../log';
import { confirmAsync } from '../../prompts';
import { confirmAsync, promptAsync } from '../../prompts';
import SessionManager from '../../user/SessionManager';
import { getActorDisplayName } from '../../user/User';
import {
isDeviceLoginFailure,
resumeDeviceLoginAsync,
startDeviceLoginAsync,
} from '../../user/deviceLogin';
export default class AccountLogin extends EasCommand {
static override description = 'log in with your Expo account';
static override aliases = ['login'];
static override examples = [
'<%= config.bin %> login --device',
'<%= config.bin %> login --device --non-interactive',
'<%= config.bin %> login --device --non-interactive --resume REQUEST_ID --match NUMBER_FROM_USER',
];
static override flags = {
device: Flags.boolean({
description: 'Log in using a code in a browser on any device',
exclusive: ['sso'],
}),
...EASNonInteractiveFlag,
resume: Flags.string({
description: 'Resume a saved device login using its request ID (one command at a time)',
dependsOn: ['device'],
}),
match: Flags.string({
description: 'Submit the number the user sees in their browser',
dependsOn: ['device', 'resume'],
}),
// can pass either --sso or -s
sso: Flags.boolean({
description: 'Log in with SSO',
@@ -32,32 +57,106 @@ export default class AccountLogin extends EasCommand {
async runAsync(): Promise<void> {
const {
flags: { sso, browser },
flags: { sso, browser, device, 'non-interactive': nonInteractive, resume, match },
} = await this.parse(AccountLogin);
if (nonInteractive && !device) {
throw new Error('Use eas login --device --non-interactive to log in without prompts.');
}
const {
sessionManager,
maybeLoggedIn: { actor },
} = await this.getContextAsync(AccountLogin, { nonInteractive: false });
} = await this.getContextAsync(AccountLogin, { nonInteractive });
if (sessionManager.getAccessToken()) {
throw new Error(
'EXPO_TOKEN is set in your environment, and is being used for all EAS authentication. To use username/password authentication, unset EXPO_TOKEN in your environment and re-run the command.'
'EXPO_TOKEN is set in your environment, and is being used for all EAS authentication. Unset EXPO_TOKEN to log in with an Expo account.'
);
}
if (actor) {
if (actor && !resume) {
Log.warn(`You are already logged in as ${chalk.bold(getActorDisplayName(actor))}.`);
const shouldContinue = await confirmAsync({
message: 'Do you want to continue?',
});
const shouldContinue =
nonInteractive ||
(await confirmAsync({
message: 'Do you want to continue?',
}));
if (!shouldContinue) {
Errors.error('Aborted', { exit: 1 });
}
}
if (device) {
await this.runDeviceLoginAsync(sessionManager, { nonInteractive, resume, match });
return;
}
await sessionManager.showLoginPromptAsync({ sso, browser });
Log.log('Logged in');
}
private async runDeviceLoginAsync(
sessionManager: SessionManager,
options: { nonInteractive: boolean; resume?: string; match?: string }
): Promise<void> {
let result = options.resume
? await resumeDeviceLoginAsync(options.resume, sessionManager, options.match)
: await startDeviceLoginAsync();
if (options.nonInteractive) {
if (isDeviceLoginFailure(result)) {
throw new Error(
`Device login failed (${result.status}). Start again with eas login --device.`
);
}
if (result.status === 'authenticated') {
Log.log(`Logged in as ${result.username}`);
return;
}
const resumeCommand = `eas login --device --non-interactive --resume ${result.request_id}`;
if (!options.resume) {
Log.log(`Open ${result.verification_uri_complete}`);
Log.log(`Code: ${result.user_code}`);
Log.log(
'Ask the user to approve the login in their browser and send you any number shown.'
);
Log.log(`Then run ${resumeCommand}, adding --match NUMBER_FROM_USER if they saw a number.`);
} else if (result.status === 'matching_required') {
Log.log('Ask the user for the number shown in their browser, then run:');
Log.log(`${resumeCommand} --match NUMBER_FROM_USER`);
} else {
Log.log(`Approval pending. Retry after ${result.retry_after} seconds:`);
Log.log(`${resumeCommand}${options.match ? ` --match ${options.match}` : ''}`);
}
return;
}
if ('verification_uri_complete' in result) {
Log.log(`Open ${result.verification_uri_complete}`);
Log.log(`Code: ${result.user_code}`);
Log.log(`You can resume with eas login --device --resume ${result.request_id}`);
}
let match = options.match;
while (result.status !== 'authenticated') {
if (isDeviceLoginFailure(result)) {
throw new Error(
`Device login failed (${result.status}). Start again with eas login --device.`
);
}
if (result.status === 'matching_required') {
const answer = await promptAsync({
type: 'select',
name: 'match',
message: 'Select the number shown in your browser',
choices: result.match_options.map(value => ({ title: value, value })),
});
match = answer.match;
}
const delayMs = result.retry_after * 1000;
await new Promise(resolve => setTimeout(resolve, delayMs));
result = await resumeDeviceLoginAsync(result.request_id, sessionManager, match);
}
Log.log(`Logged in as ${result.username}`);
}
}
@@ -1,12 +1,15 @@
import { Config } from '@oclif/core';
import * as fs from 'fs-extra';
import { isAnalyticsOptedOutAsync } from '../../../analytics/AnalyticsManager';
import { getAgentTelemetryContext } from '../../../analytics/agent';
import { ExpoGraphqlClient } from '../../../commandUtils/context/contextUtils/createGraphqlClient';
import {
AppPlatform,
CreateDeviceRunSessionMutation,
DeviceRunSessionByIdQuery,
DeviceRunSessionEgress,
DeviceRunSessionRequestOrigin,
DeviceRunSessionResourceClass,
DeviceRunSessionStatus,
DeviceRunSessionType,
@@ -32,6 +35,11 @@ import { enableJsonOutput, printJsonOnlyOutput } from '../../../utils/json';
import Simulator from '../index';
jest.mock('fs-extra');
jest.mock('../../../analytics/AnalyticsManager', () => ({
...jest.requireActual('../../../analytics/AnalyticsManager'),
isAnalyticsOptedOutAsync: jest.fn(),
}));
jest.mock('../../../analytics/agent');
jest.mock('../../../graphql/mutations/DeviceRunSessionMutation');
jest.mock('../../../graphql/queries/DeviceRunSessionAvailabilityQuery');
jest.mock('../../../graphql/queries/DeviceRunSessionQuery');
@@ -99,6 +107,8 @@ const mockResetSimulatorEnvAsync = jest.mocked(resetSimulatorEnvAsync);
const mockResolveExpoGoSdkVersionAsync = jest.mocked(resolveExpoGoSdkVersionAsync);
const mockOra = jest.mocked(ora);
const mockPromptAsync = jest.mocked(promptAsync);
const mockGetAgentTelemetryContext = jest.mocked(getAgentTelemetryContext);
const mockIsAnalyticsOptedOutAsync = jest.mocked(isAnalyticsOptedOutAsync);
const mockEnableJsonOutput = jest.mocked(enableJsonOutput);
const mockPrintJsonOnlyOutput = jest.mocked(printJsonOnlyOutput);
@@ -186,6 +196,8 @@ describe(Simulator, () => {
mockResetSimulatorEnvAsync.mockResolvedValue();
mockResolveExpoGoSdkVersionAsync.mockResolvedValue('55.0.0');
jest.mocked(fs.writeFile).mockResolvedValue(undefined as never);
mockGetAgentTelemetryContext.mockReturnValue(null);
mockIsAnalyticsOptedOutAsync.mockResolvedValue(false);
});
afterAll(() => {
@@ -258,6 +270,7 @@ describe(Simulator, () => {
packageVersion: undefined,
platform: AppPlatform.Ios,
type: DeviceRunSessionType.AgentDevice,
requestOrigin: DeviceRunSessionRequestOrigin.EasCli,
});
expect(fs.writeFile).not.toHaveBeenCalled();
expect(mockOra.mock.results[0]?.value.succeed).toHaveBeenCalledWith(
@@ -420,6 +433,7 @@ describe(Simulator, () => {
packageVersion: undefined,
platform: AppPlatform.Ios,
type: DeviceRunSessionType.AgentDevice,
requestOrigin: DeviceRunSessionRequestOrigin.EasCli,
});
});
@@ -440,6 +454,7 @@ describe(Simulator, () => {
packageVersion: undefined,
platform: AppPlatform.Ios,
type: DeviceRunSessionType.AgentDevice,
requestOrigin: DeviceRunSessionRequestOrigin.EasCli,
});
});
@@ -460,6 +475,7 @@ describe(Simulator, () => {
platform: AppPlatform.Ios,
type: DeviceRunSessionType.AgentDevice,
maxIdleTimeMinutes: 30,
requestOrigin: DeviceRunSessionRequestOrigin.EasCli,
});
});
@@ -479,6 +495,7 @@ describe(Simulator, () => {
packageVersion: undefined,
platform: AppPlatform.Ios,
type: DeviceRunSessionType.AgentDevice,
requestOrigin: DeviceRunSessionRequestOrigin.EasCli,
});
});
@@ -636,6 +653,43 @@ describe(Simulator, () => {
);
});
it('sends the detected coding agent with the create request', async () => {
mockGetAgentTelemetryContext.mockReturnValue({ id: 'claude', sessionId: 'agent-session-1' });
const { command } = createCommand(['--platform', 'ios', '--non-interactive']);
await command.runAsync();
expect(mockCreateDeviceRunSessionAsync).toHaveBeenCalledWith(
graphqlClient,
expect.objectContaining({
requestOrigin: DeviceRunSessionRequestOrigin.EasCli,
agentId: 'claude',
agentSessionId: 'agent-session-1',
})
);
});
it('leaves out the agent session when the detected agent has none', async () => {
mockGetAgentTelemetryContext.mockReturnValue({ id: 'codex', sessionId: undefined });
const { command } = createCommand(['--platform', 'ios', '--non-interactive']);
await command.runAsync();
const input = mockCreateDeviceRunSessionAsync.mock.calls[0]?.[1];
expect(input).toMatchObject({ agentId: 'codex' });
expect(input).not.toHaveProperty('agentSessionId');
});
it('leaves out the coding agent when the user opted out of analytics', async () => {
mockIsAnalyticsOptedOutAsync.mockResolvedValue(true);
mockGetAgentTelemetryContext.mockReturnValue({ id: 'claude', sessionId: 'agent-session-1' });
const { command } = createCommand(['--platform', 'ios', '--non-interactive']);
await command.runAsync();
const input = mockCreateDeviceRunSessionAsync.mock.calls[0]?.[1];
expect(input).toMatchObject({ requestOrigin: DeviceRunSessionRequestOrigin.EasCli });
expect(input).not.toHaveProperty('agentId');
expect(input).not.toHaveProperty('agentSessionId');
});
it('forwards --device in the iOS create options', async () => {
const { command } = createCommand([
'--platform',
@@ -1,6 +1,8 @@
import { Flags } from '@oclif/core';
import nullthrows from 'nullthrows';
import { isAnalyticsOptedOutAsync } from '../../analytics/AnalyticsManager';
import { getAgentTelemetryContext } from '../../analytics/agent';
import { getDeviceRunSessionUrl } from '../../build/utils/url';
import EasCommand from '../../commandUtils/EasCommand';
import { ExpoGraphqlClient } from '../../commandUtils/context/contextUtils/createGraphqlClient';
@@ -12,6 +14,7 @@ import {
import {
AppPlatform,
DeviceRunSessionEgress,
DeviceRunSessionRequestOrigin,
DeviceRunSessionStatus,
DeviceRunSessionType,
JobRunStatus,
@@ -298,6 +301,7 @@ export default class Simulator extends EasCommand {
let deviceRunSessionUrl: string;
let sessionInterrupt: SessionInterrupt | undefined;
try {
const agentIdentity = await agentIdentityInputAsync();
const session = await DeviceRunSessionMutation.createDeviceRunSessionAsync(graphqlClient, {
appId: projectId,
name,
@@ -322,6 +326,8 @@ export default class Simulator extends EasCommand {
...(openUrl ? { openUrl } : {}),
...(resourceClass ? { resourceClass } : {}),
...(egress ? { egress } : {}),
requestOrigin: DeviceRunSessionRequestOrigin.EasCli,
...agentIdentity,
maxRunTimeMinutes: flags['max-duration-minutes'],
maxIdleTimeMinutes: flags['max-idle-time-minutes'],
});
@@ -761,3 +767,14 @@ async function ensureDeviceRunSessionStoppedSafelyAsync(
return false;
}
}
async function agentIdentityInputAsync(): Promise<{ agentId?: string; agentSessionId?: string }> {
if (await isAnalyticsOptedOutAsync()) {
return {};
}
const agent = getAgentTelemetryContext();
if (!agent) {
return {};
}
return { agentId: agent.id, ...(agent.sessionId ? { agentSessionId: agent.sessionId } : {}) };
}
@@ -5,6 +5,7 @@ import nullthrows from 'nullthrows';
import { fetchSessionSecretAndUserAsync } from './fetchSessionSecretAndUser';
import { fetchSessionSecretAndUserFromBrowserAuthFlowAsync } from './fetchSessionSecretAndUserFromBrowserAuthFlow';
import { fetchUserAsync } from './fetchUser';
import { ApiV2Error } from '../ApiV2Error';
import { AnalyticsWithOrchestration } from '../analytics/AnalyticsManager';
import { ApiV2Client } from '../api';
@@ -211,6 +212,18 @@ export default class SessionManager {
});
}
public async loginWithDeviceSessionAsync(sessionSecret: string): Promise<string> {
const { id, username } = await fetchUserAsync({ sessionSecret });
await this.setSessionAsync({
sessionSecret,
userId: id,
username,
currentConnection: 'Browser-Flow-Authentication',
});
this.currentActor = undefined;
return username;
}
private async loginAsync(input: {
username: string;
password: string;
@@ -0,0 +1,209 @@
import fs from 'fs-extra';
import MockDate from 'mockdate';
import nock from 'nock';
import os from 'os';
import path from 'path';
import { getExpoApiBaseUrl } from '../../api';
import { getStateJsonPath } from '../../utils/paths';
import SessionManager from '../SessionManager';
import { resumeDeviceLoginAsync, startDeviceLoginAsync } from '../deviceLogin';
import { fetchUserAsync } from '../fetchUser';
jest.mock('../../utils/paths');
jest.mock('../fetchUser');
const apiUrl = getExpoApiBaseUrl();
const now = Date.parse('2026-09-29T12:00:00Z');
let directory: string;
function manager(): SessionManager {
return new SessionManager({ logEvent: jest.fn(), setActor: jest.fn(), flushAsync: jest.fn() });
}
async function startAsync(): Promise<string> {
nock(apiUrl)
.post('/v2/auth/device_authorization', body => body.client_id === 'eas-cli')
.reply(200, {
data: {
device_code: 'PRIVATE_DEVICE_CODE',
user_code: 'BCDF-GHJK',
verification_uri: 'https://expo.dev/oauth/device',
expires_in: 600,
interval: 5,
},
});
const result = await startDeviceLoginAsync();
expect(JSON.stringify(result)).not.toContain('PRIVATE_DEVICE_CODE');
expect(result.verification_uri_complete).toBe(
'https://expo.dev/oauth/device?user_code=BCDF-GHJK'
);
return result.request_id;
}
function token(data: object, match?: string): nock.Scope {
return nock(apiUrl)
.post('/v2/auth/token', {
client_id: 'eas-cli',
grant_type: 'urn:ietf:params:oauth:grant-type:device_code',
device_code: 'PRIVATE_DEVICE_CODE',
...(match ? { match_value: match } : {}),
})
.reply(200, { data });
}
beforeEach(async () => {
directory = await fs.mkdtemp(path.join(os.tmpdir(), 'eas-device-login-test-'));
jest.mocked(getStateJsonPath).mockReturnValue(path.join(directory, 'state.json'));
jest.mocked(fetchUserAsync).mockResolvedValue({ id: 'user-id', username: 'demo-user' });
MockDate.set(now);
nock.disableNetConnect();
});
afterEach(async () => {
const pending = nock.pendingMocks();
nock.cleanAll();
nock.enableNetConnect();
MockDate.reset();
jest.restoreAllMocks();
jest.clearAllMocks();
await fs.remove(directory);
expect(pending).toEqual([]);
});
it('resumes across fresh sessions, keeps secrets private, and installs a normal Expo session', async () => {
await fs.writeJson(getStateJsonPath(), { unrelatedSetting: true });
const id = await startAsync();
const requestPath = path.join(directory, 'device-login', `${id}.json`);
expect((await fs.stat(requestPath)).mode & 0o777).toBe(0o600);
expect((await fs.stat(path.dirname(requestPath))).mode & 0o777).toBe(0o700);
MockDate.set(now + 5000);
token({ error: 'authorization_pending' });
expect((await resumeDeviceLoginAsync(id, manager())).status).toBe('authorization_pending');
expect((await fs.stat(requestPath)).mode & 0o777).toBe(0o600);
MockDate.set(now + 10000);
token({ error: 'matching_required', match_options: ['12', '42', '87'] });
expect(await resumeDeviceLoginAsync(id, manager())).toMatchObject({
status: 'matching_required',
match_options: ['12', '42', '87'],
});
MockDate.set(now + 15000);
token({ session_secret: 'PRIVATE_SESSION', expires_at: '2027-01-01T00:00:00Z' }, '42');
expect(await resumeDeviceLoginAsync(id, manager(), '42')).toEqual({
request_id: id,
status: 'authenticated',
username: 'demo-user',
});
expect(await fs.readJson(getStateJsonPath())).toEqual({
unrelatedSetting: true,
auth: {
sessionSecret: 'PRIVATE_SESSION',
userId: 'user-id',
username: 'demo-user',
currentConnection: 'Browser-Flow-Authentication',
},
});
expect(await fs.readdir(path.dirname(requestPath))).toEqual([]);
});
it('accepts a direct session secret without a matching response', async () => {
const id = await startAsync();
MockDate.set(now + 5000);
token({ session_secret: 'PRIVATE_SESSION', expires_at: '2027-01-01T00:00:00Z' });
expect(await resumeDeviceLoginAsync(id, manager())).toEqual({
request_id: id,
status: 'authenticated',
username: 'demo-user',
});
expect(await fs.readJson(getStateJsonPath())).toMatchObject({
auth: { sessionSecret: 'PRIVATE_SESSION' },
});
expect(await fs.readdir(path.join(directory, 'device-login'))).toEqual([]);
});
it('keeps the poll interval across invocations, including slow_down', async () => {
const id = await startAsync();
expect(await resumeDeviceLoginAsync(id, manager())).toMatchObject({ retry_after: 5 });
MockDate.set(now + 5000);
token({ error: 'slow_down' });
expect(await resumeDeviceLoginAsync(id, manager())).toMatchObject({
status: 'slow_down',
retry_after: 10,
});
MockDate.set(now + 6000);
expect(await resumeDeviceLoginAsync(id, manager())).toMatchObject({ retry_after: 9 });
});
it.each([
{ header: '30', delay: 30 },
{ header: new Date(now + 35000).toUTCString(), delay: 30 },
{ header: undefined, delay: 60 },
{ header: 'invalid', delay: 60 },
])('honors HTTP 429 Retry-After ($header) across invocations', async ({ header, delay }) => {
const id = await startAsync();
MockDate.set(now + 5000);
nock(apiUrl)
.post('/v2/auth/token')
.reply(429, {}, header ? { 'Retry-After': header } : {});
expect(await resumeDeviceLoginAsync(id, manager())).toMatchObject({
status: 'slow_down',
retry_after: delay,
});
expect(await resumeDeviceLoginAsync(id, manager())).toMatchObject({ retry_after: delay });
});
it('expires without a network call and removes the private request', async () => {
const id = await startAsync();
MockDate.set(now + 600000);
expect((await resumeDeviceLoginAsync(id, manager())).status).toBe('expired_token');
await expect(resumeDeviceLoginAsync(id, manager())).rejects.toThrow('not found');
});
it.each(['access_denied', 'expired_token', 'invalid_grant'])(
'stops on %s without retrying or choosing another number',
async error => {
const id = await startAsync();
MockDate.set(now + 5000);
token({ error }, '12');
expect((await resumeDeviceLoginAsync(id, manager(), '12')).status).toBe(error);
await expect(resumeDeviceLoginAsync(id, manager(), '42')).rejects.toThrow('not found');
expect(fetchUserAsync).not.toHaveBeenCalled();
}
);
it('recovers after token exchange succeeds but fetching the user fails', async () => {
const id = await startAsync();
MockDate.set(now + 5000);
token({ session_secret: 'PRIVATE_SESSION', expires_at: '2027-01-01T00:00:00Z' }, '42');
jest.mocked(fetchUserAsync).mockRejectedValueOnce(new Error('offline'));
await expect(resumeDeviceLoginAsync(id, manager(), '42')).rejects.toThrow('offline');
const requestPath = path.join(directory, 'device-login', `${id}.json`);
expect((await fs.stat(requestPath)).mode & 0o777).toBe(0o600);
expect((await fs.readJson(requestPath)).sessionSecret).toBe('PRIVATE_SESSION');
// No second token exchange; the one-use grant was already consumed.
expect((await resumeDeviceLoginAsync(id, manager())).status).toBe('authenticated');
});
it('rejects another API environment, invalid request IDs, and invalid number input', async () => {
const id = await startAsync();
const requestPath = path.join(directory, 'device-login', `${id}.json`);
const state = await fs.readJson(requestPath);
await fs.writeJson(requestPath, { ...state, apiUrl: 'https://another.example.com' });
await expect(resumeDeviceLoginAsync(id, manager())).rejects.toThrow('same Expo API environment');
await expect(resumeDeviceLoginAsync('../state', manager())).rejects.toThrow(
'Invalid device login request ID'
);
await expect(resumeDeviceLoginAsync(id, manager(), 'guess')).rejects.toThrow(
'number the user sees'
);
});
it('preserves a pending request after a network failure', async () => {
const id = await startAsync();
MockDate.set(now + 5000);
nock(apiUrl).post('/v2/auth/token').replyWithError('offline');
await expect(resumeDeviceLoginAsync(id, manager())).rejects.toThrow('offline');
MockDate.set(now + 10000);
token({ error: 'matching_required', match_options: ['12', '42', '87'] });
expect((await resumeDeviceLoginAsync(id, manager())).status).toBe('matching_required');
});
+216
View File
@@ -0,0 +1,216 @@
import JsonFile from '@expo/json-file';
import { randomUUID } from 'crypto';
import fs from 'fs-extra';
import os from 'os';
import path from 'path';
import { z } from 'zod';
import SessionManager from './SessionManager';
import { getExpoApiBaseUrl } from '../api';
import fetch, { RequestError } from '../fetch';
import { getStateJsonPath } from '../utils/paths';
const grantSchema = z.object({
device_code: z.string().min(1),
user_code: z.string().min(1),
verification_uri: z.url(),
expires_in: z.number().positive(),
interval: z.number().positive(),
});
const terminalErrorSchema = z.enum(['access_denied', 'expired_token', 'invalid_grant']);
const tokenSchema = z.union([
z.object({ session_secret: z.string().min(1), expires_at: z.iso.datetime() }),
z.object({ error: z.literal('matching_required'), match_options: z.array(z.string()).min(1) }),
z.object({ error: z.enum(['authorization_pending', 'slow_down']).or(terminalErrorSchema) }),
]);
const stateSchema = z.object({
apiUrl: z.string(),
deviceCode: z.string(),
userCode: z.string(),
verificationUri: z.string(),
expiresAt: z.number(),
interval: z.number(),
nextPollAt: z.number(),
sessionSecret: z.string().optional(),
});
type DeviceLoginState = z.infer<typeof stateSchema>;
type PendingDeviceLogin = {
request_id: string;
verification_uri: string;
verification_uri_complete: string;
user_code: string;
expires_at: string;
retry_after: number;
};
export type DeviceLoginResult =
| { request_id: string; status: 'authenticated'; username: string }
| { request_id: string; status: z.infer<typeof terminalErrorSchema> }
| (PendingDeviceLogin &
(
| { status: 'authorization_pending' | 'slow_down' }
| { status: 'matching_required'; match_options: string[] }
));
export function isDeviceLoginFailure(result: {
status: string;
}): result is { status: z.infer<typeof terminalErrorSchema> } {
return terminalErrorSchema.safeParse(result.status).success;
}
function getRequestPath(requestId: string): string {
if (!z.uuid().safeParse(requestId).success) {
throw new Error('Invalid device login request ID. Start again with eas login --device.');
}
return path.join(path.dirname(getStateJsonPath()), 'device-login', `${requestId}.json`);
}
async function postAsync(endpoint: string, body: object): Promise<unknown> {
const response = await fetch(`${getExpoApiBaseUrl()}/v2/auth/${endpoint}`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ client_id: 'eas-cli', ...body }),
timeout: 30_000,
});
return (await response.json()).data;
}
function publicResult(
requestId: string,
state: DeviceLoginState
): PendingDeviceLogin & { status: 'authorization_pending' } {
const verificationUrl = new URL(state.verificationUri);
verificationUrl.searchParams.set('user_code', state.userCode);
return {
request_id: requestId,
status: 'authorization_pending',
verification_uri: state.verificationUri,
verification_uri_complete: verificationUrl.toString(),
user_code: state.userCode,
expires_at: new Date(state.expiresAt).toISOString(),
retry_after: Math.max(0, Math.ceil((state.nextPollAt - Date.now()) / 1000)),
};
}
export async function startDeviceLoginAsync(): Promise<
PendingDeviceLogin & { status: 'authorization_pending' }
> {
const result = grantSchema.safeParse(
await postAsync('device_authorization', {
device_name: os.hostname().slice(0, 255),
device_platform: os.platform(),
device_os_version: os.release().slice(0, 64),
})
);
if (!result.success) {
throw new Error('Unexpected device authorization response. Try again or update EAS CLI.');
}
const grant = result.data;
const requestId = randomUUID();
const requestPath = getRequestPath(requestId);
await fs.mkdir(path.dirname(requestPath), { recursive: true, mode: 0o700 });
const state: DeviceLoginState = {
apiUrl: getExpoApiBaseUrl(),
deviceCode: grant.device_code,
userCode: grant.user_code,
verificationUri: grant.verification_uri,
expiresAt: Date.now() + grant.expires_in * 1000,
interval: Math.max(5, grant.interval),
nextPollAt: Date.now() + Math.max(5, grant.interval) * 1000,
};
// JsonFile preserves this mode when atomically replacing the file on subsequent writes.
await fs.writeFile(requestPath, '{}', { mode: 0o600, flag: 'wx' });
await JsonFile.writeAsync(requestPath, state);
return publicResult(requestId, state);
}
// Resume one request at a time, as with other CLI session updates. State survives between
// invocations; concurrent commands for the same request are not serialized.
export async function resumeDeviceLoginAsync(
requestId: string,
sessionManager: SessionManager,
match?: string
): Promise<DeviceLoginResult> {
if (match !== undefined && !/^\d{1,8}$/.test(match)) {
throw new Error('--match must be the number the user sees in their browser.');
}
const requestPath = getRequestPath(requestId);
if (!(await fs.pathExists(requestPath))) {
throw new Error('Device login request not found. Start again with eas login --device.');
}
const parsed = stateSchema.safeParse(await JsonFile.readAsync(requestPath));
if (!parsed.success) {
throw new Error('Invalid saved device login. Start again with eas login --device.');
}
const state = parsed.data;
if (state.apiUrl !== getExpoApiBaseUrl()) {
throw new Error('Resume device login in the same Expo API environment where it was started.');
}
if (state.expiresAt <= Date.now()) {
await fs.remove(requestPath);
return { request_id: requestId, status: 'expired_token' };
}
if (!state.sessionSecret) {
if (state.nextPollAt > Date.now()) {
return publicResult(requestId, state);
}
// Persist the deadline before polling so errors and process restarts respect the interval.
state.nextPollAt = Date.now() + state.interval * 1000;
await JsonFile.writeAsync(requestPath, state);
let data: unknown;
let retryAfter = 0;
try {
data = await postAsync('token', {
grant_type: 'urn:ietf:params:oauth:grant-type:device_code',
device_code: state.deviceCode,
...(match !== undefined ? { match_value: match } : {}),
});
} catch (error) {
if (!(error instanceof RequestError) || error.response.status !== 429) {
throw error;
}
const header = error.response.headers.get('retry-after');
const seconds = header ? Number(header) : NaN;
const delay = Number.isFinite(seconds)
? seconds
: (Date.parse(header ?? '') - Date.now()) / 1000;
retryAfter = Number.isFinite(delay) ? delay : 60;
data = { error: 'slow_down' };
}
const result = tokenSchema.safeParse(data);
if (!result.success) {
throw new Error('Unexpected device token response. Resume this request to retry.');
}
if ('error' in result.data) {
const response = result.data;
const failure = { request_id: requestId, status: response.error };
if (isDeviceLoginFailure(failure)) {
await fs.remove(requestPath);
return failure;
}
switch (response.error) {
case 'slow_down':
state.interval += 5;
state.nextPollAt = Date.now() + Math.max(state.interval, retryAfter) * 1000;
await JsonFile.writeAsync(requestPath, state);
return { ...publicResult(requestId, state), status: 'slow_down' };
case 'matching_required':
return {
...publicResult(requestId, state),
status: response.error,
match_options: response.match_options,
};
}
return publicResult(requestId, state);
}
// The grant is single-use. Persist its secret before fetching the user/installing the
// session, so a later invocation can finish if those operations fail.
state.sessionSecret = result.data.session_secret;
state.expiresAt = Date.parse(result.data.expires_at);
await JsonFile.writeAsync(requestPath, state);
}
const username = await sessionManager.loginWithDeviceSessionAsync(state.sessionSecret);
await fs.remove(requestPath);
return { request_id: requestId, status: 'authenticated', username };
}
+2 -2
View File
@@ -1,6 +1,6 @@
{
"name": "eas-cli-local-build-plugin",
"version": "24.9.0",
"version": "24.11.0",
"description": "Tool for running EAS compatible builds on a local machine.",
"bugs": "https://github.com/expo/eas-cli/issues",
"license": "BUSL-1.1",
@@ -26,7 +26,7 @@
"clean": "rimraf node_modules dist coverage \"*.tsbuildinfo\""
},
"dependencies": {
"@expo/build-tools": "24.9.0",
"@expo/build-tools": "24.11.0",
"@expo/bunyan": "^4.0.1",
"@expo/eas-build-job": "24.9.0",
"@expo/logger": "24.8.0",
+2 -2
View File
@@ -1,6 +1,6 @@
{
"name": "@expo/worker",
"version": "24.9.0",
"version": "24.11.0",
"private": true,
"scripts": {
"start": "yarn watch",
@@ -19,7 +19,7 @@
},
"dependencies": {
"@babel/core": "7.24.7",
"@expo/build-tools": "24.9.0",
"@expo/build-tools": "24.11.0",
"@expo/config-plugins": "55.0.7",
"@expo/config-types": "55.0.5",
"@expo/eas-build-job": "24.9.0",
+3 -21
View File
@@ -23,21 +23,16 @@ fi
echo "Building $OUTPUT_FILE"
tmp_dir=""
record_sim_build_dir=""
cleanup() {
if [[ -n "$tmp_dir" ]]; then
rm -rf "$tmp_dir"
fi
if [[ -n "$record_sim_build_dir" ]]; then
rm -rf "$record_sim_build_dir"
fi
}
trap cleanup EXIT
tmp_dir=$(mktemp -d)
target_root_dir="$tmp_dir"
target_worker_dir="$tmp_dir/packages/worker"
record_sim_build_dir=$(mktemp -d)
mkdir -p "$target_worker_dir"
@@ -98,29 +93,16 @@ rm -rf tsconfig.json tsconfig.build.json
popd >/dev/null 2>&1
if [[ "$PLATFORM" != "ios" ]]; then
rm -f "$target_root_dir/packages/build-tools/bin/record-sim"
rm -f "$target_root_dir/packages/build-tools/bin/egress-guard.dylib"
fi
if [[ "$PLATFORM" == "ios" ]]; then
record_sim_package_dir="$ROOT_DIR/packages/build-tools/resources/record-sim"
record_sim_bin_dir="$target_root_dir/packages/build-tools/bin"
mkdir -p "$record_sim_bin_dir"
record_sim_bin_path=$(swift build \
-c release \
--package-path "$record_sim_package_dir" \
--build-path "$record_sim_build_dir" \
--show-bin-path)
swift build \
-c release \
--package-path "$record_sim_package_dir" \
--build-path "$record_sim_build_dir"
cp "$record_sim_bin_path/record-sim" "$record_sim_bin_dir/record-sim"
chmod +x "$record_sim_bin_dir/record-sim"
build_tools_bin_dir="$target_root_dir/packages/build-tools/bin"
mkdir -p "$build_tools_bin_dir"
# The local egress guard, injected into simulator processes; see
# packages/build-tools/resources/egress-guard/README.md.
"$ROOT_DIR/packages/build-tools/resources/egress-guard/build.sh" "$record_sim_bin_dir"
"$ROOT_DIR/packages/build-tools/resources/egress-guard/build.sh" "$build_tools_bin_dir"
# build plugin
pushd "$ROOT_DIR/packages/expo-cocoapods-proxy" >/dev/null 2>&1
Generated
+3 -3
View File
@@ -2303,7 +2303,7 @@ __metadata:
languageName: node
linkType: hard
"@expo/build-tools@npm:24.9.0, @expo/build-tools@workspace:packages/build-tools":
"@expo/build-tools@npm:24.11.0, @expo/build-tools@workspace:packages/build-tools":
version: 0.0.0-use.local
resolution: "@expo/build-tools@workspace:packages/build-tools"
dependencies:
@@ -2984,7 +2984,7 @@ __metadata:
resolution: "@expo/worker@workspace:packages/worker"
dependencies:
"@babel/core": "npm:7.24.7"
"@expo/build-tools": "npm:24.9.0"
"@expo/build-tools": "npm:24.11.0"
"@expo/config-plugins": "npm:55.0.7"
"@expo/config-types": "npm:55.0.5"
"@expo/eas-build-job": "npm:24.9.0"
@@ -10418,7 +10418,7 @@ __metadata:
version: 0.0.0-use.local
resolution: "eas-cli-local-build-plugin@workspace:packages/local-build-plugin"
dependencies:
"@expo/build-tools": "npm:24.9.0"
"@expo/build-tools": "npm:24.11.0"
"@expo/bunyan": "npm:^4.0.1"
"@expo/eas-build-job": "npm:24.9.0"
"@expo/logger": "npm:24.8.0"