Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 31 additions & 11 deletions Sources/XcodesKit/Services/Aria2DownloadService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,11 @@ public struct Aria2DownloadService: Sendable {
) -> AsyncThrowingStream<Progress, Error> {
let process = Process()
process.executableURL = aria2Path.url
process.arguments = [
"--header=Cookie: \(cookies.map { "\($0.name)=\($0.value)" }.joined(separator: "; "))",
"--max-connection-per-server=16",
"--split=16",
"--summary-interval=1",
"--stop-with-process=\(ProcessInfo.processInfo.processIdentifier)",
"--dir=\(destination.parent.string)",
"--out=\(destination.basename())",
"--human-readable=false",
url.absoluteString,
]
process.arguments = Self.arguments(
cookieHeader: cookies.map { "\($0.name)=\($0.value)" }.joined(separator: "; "),
destination: destination,
url: url
)

let state = UnauthorizedState()
let runner = ProcessProgressStreamRunner(
Expand Down Expand Up @@ -60,6 +54,32 @@ public struct Aria2DownloadService: Sendable {

return runner.stream()
}

static func arguments(
cookieHeader: String,
destination: Path,
url: URL,
hostProcessID: Int32 = ProcessInfo.processInfo.processIdentifier
) -> [String] {
// aria2c reads ~/.aria2/aria2.conf by default. Options such as enable-rpc
// or daemon can keep the process alive after the download finishes, which
// prevents ProcessProgressStreamRunner from completing the stream.
// Resume is enabled explicitly because the archive services use the .aria2
// sidecar to detect incomplete downloads.
[
"--no-conf",
"--continue=true",
"--header=Cookie: \(cookieHeader)",
"--max-connection-per-server=16",
"--split=16",
"--summary-interval=1",
"--stop-with-process=\(hostProcessID)",
"--dir=\(destination.parent.string)",
"--out=\(destination.basename())",
"--human-readable=false",
url.absoluteString,
]
}
}

private final class UnauthorizedState: Sendable {
Expand Down
30 changes: 30 additions & 0 deletions Tests/XcodesKitTests/Aria2DownloadServiceTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import XCTest
@preconcurrency import Path
@testable import XcodesKit

final class Aria2DownloadServiceTests: XCTestCase {
func testArgumentsIgnoreUserConfigAndKeepResumeEnabled() throws {
let destination = try XCTUnwrap(Path("/tmp/Xcode-26.0.xip"))
let url = try XCTUnwrap(URL(string: "https://example.com/Xcode_26.0.xip"))

let arguments = Aria2DownloadService.arguments(
cookieHeader: "myacinfo=token",
destination: destination,
url: url,
hostProcessID: 4242
)

XCTAssertEqual(arguments.first, "--no-conf")
XCTAssertTrue(arguments.contains("--continue=true"))
XCTAssertFalse(arguments.contains { $0.hasPrefix("--conf-path=") })
XCTAssertTrue(arguments.contains("--header=Cookie: myacinfo=token"))
XCTAssertTrue(arguments.contains("--max-connection-per-server=16"))
XCTAssertTrue(arguments.contains("--split=16"))
XCTAssertTrue(arguments.contains("--summary-interval=1"))
XCTAssertTrue(arguments.contains("--stop-with-process=4242"))
XCTAssertTrue(arguments.contains("--dir=/tmp"))
XCTAssertTrue(arguments.contains("--out=Xcode-26.0.xip"))
XCTAssertTrue(arguments.contains("--human-readable=false"))
XCTAssertEqual(arguments.last, url.absoluteString)
}
}