diff --git a/Sources/XcodesKit/Services/Aria2DownloadService.swift b/Sources/XcodesKit/Services/Aria2DownloadService.swift index 11ea699..3feeb02 100644 --- a/Sources/XcodesKit/Services/Aria2DownloadService.swift +++ b/Sources/XcodesKit/Services/Aria2DownloadService.swift @@ -15,17 +15,11 @@ public struct Aria2DownloadService: Sendable { ) -> AsyncThrowingStream { 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( @@ -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 { diff --git a/Tests/XcodesKitTests/Aria2DownloadServiceTests.swift b/Tests/XcodesKitTests/Aria2DownloadServiceTests.swift new file mode 100644 index 0000000..5bece53 --- /dev/null +++ b/Tests/XcodesKitTests/Aria2DownloadServiceTests.swift @@ -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) + } +}