Repository Public
1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Packages/LocalChatModelHub/Sources/TransferDriver.swift3// Description: Delegate-based URLSession transfers that append to a partial file from a byte offset (HTTP Range), in foreground (data tasks) or background (download tasks) sessions.4import CryptoKit5import Foundation6import LocalChatCore78/// Thread-safe byte counter shared between the URLSession delegate queue and the download actor.9final class ByteCounter: @unchecked Sendable {10 private let lock = NSLock()11 private var value: Int64 = 012 func add(_ n: Int64) { lock.lock(); value = max(0, value + n); lock.unlock() }13 func set(_ n: Int64) { lock.lock(); value = n; lock.unlock() }14 var current: Int64 { lock.lock(); defer { lock.unlock() }; return value }15}1617enum TransferOutcome: Sendable {18 case complete19 case rangeNotSatisfiable // the server says the offset is at or past the end of the file20}2122final class TransferDriver: NSObject, URLSessionDataDelegate, URLSessionDownloadDelegate, @unchecked Sendable {23 private final class Transfer {24 let partial: URL25 let offset: Int6426 let repo: String27 let hasToken: Bool28 let onBytes: @Sendable (Int64) -> Void29 var handle: FileHandle?30 var continuation: CheckedContinuation<TransferOutcome, Error>?31 var failure: HubError?32 var outcome: TransferOutcome = .complete33 var provisional: Int64 = 0 // bytes reported by a download task that are not in the partial file yet34 var status = 035 init(partial: URL, offset: Int64, repo: String, hasToken: Bool, onBytes: @escaping @Sendable (Int64) -> Void) {36 self.partial = partial; self.offset = offset; self.repo = repo; self.hasToken = hasToken; self.onBytes = onBytes37 }38 }3940 private let lock = NSLock()41 private var transfers: [Int: Transfer] = [:]42 private var backgroundCompletion: (@Sendable () -> Void)?43 private var session: URLSession!44 let isBackground: Bool4546 init(backgroundIdentifier: String?, protocolClasses: [AnyClass]?) {47 isBackground = backgroundIdentifier != nil48 super.init()49 let configuration: URLSessionConfiguration50 if let backgroundIdentifier {51 configuration = .background(withIdentifier: backgroundIdentifier)52 configuration.sessionSendsLaunchEvents = true53 configuration.isDiscretionary = false54 } else {55 configuration = .ephemeral56 configuration.urlCache = nil57 }58 configuration.timeoutIntervalForRequest = 6059 configuration.httpAdditionalHeaders = ["User-Agent": "LocalChat"]60 configuration.httpMaximumConnectionsPerHost = 661 if let protocolClasses { configuration.protocolClasses = protocolClasses }62 let queue = OperationQueue(); queue.maxConcurrentOperationCount = 1; queue.name = "localchat.downloads"63 session = URLSession(configuration: configuration, delegate: self, delegateQueue: queue)64 // tasks left over from a previous launch have no owner any more: their bytes were never appended, so they are dropped65 if isBackground { session.getAllTasks { $0.forEach { $0.cancel() } } }66 }6768 func invalidate() { session.invalidateAndCancel() }6970 /// iOS: hand over the completion handler received in `application(_:handleEventsForBackgroundURLSession:completionHandler:)`.71 func setBackgroundCompletion(_ handler: @escaping @Sendable () -> Void) { lock.lock(); backgroundCompletion = handler; lock.unlock() }7273 /// Downloads `url` starting at `offset` (optionally up to `end`, inclusive) and appends the bytes to `partial`.74 func fetch(url: URL, partial: URL, offset: Int64, end: Int64? = nil, token: String?, repo: String,75 onBytes: @escaping @Sendable (Int64) -> Void) async throws -> TransferOutcome {76 var request = URLRequest(url: url)77 request.cachePolicy = .reloadIgnoringLocalCacheData78 if offset > 0 || end != nil { request.setValue("bytes=\(offset)-\(end.map(String.init) ?? "")", forHTTPHeaderField: "Range") }79 request.setValue("identity", forHTTPHeaderField: "Accept-Encoding") // byte offsets must be offsets in the stored file80 if let token, !token.isEmpty { request.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization") }81 let task: URLSessionTask = isBackground ? session.downloadTask(with: request) : session.dataTask(with: request)82 let transfer = Transfer(partial: partial, offset: offset, repo: repo, hasToken: token?.isEmpty == false, onBytes: onBytes)83 return try await withTaskCancellationHandler {84 try await withCheckedThrowingContinuation { continuation in85 transfer.continuation = continuation86 lock.lock(); transfers[task.taskIdentifier] = transfer; lock.unlock()87 task.resume()88 }89 } onCancel: { task.cancel() }90 }9192 private func transfer(_ task: URLSessionTask) -> Transfer? { lock.lock(); defer { lock.unlock() }; return transfers[task.taskIdentifier] }9394 /// Validates the status line. Returns false when the body must not be written.95 private func accept(_ response: URLResponse, _ t: Transfer) -> Bool {96 guard let http = response as? HTTPURLResponse else { t.failure = .invalidResponse("not HTTP"); return false }97 t.status = http.statusCode98 switch http.statusCode {99 case 206:100 // "bytes 100-199/1000": the first byte must be the one we asked for101 let range = http.value(forHTTPHeaderField: "Content-Range") ?? ""102 if let first = range.split(separator: " ").last?.split(separator: "-").first, let start = Int64(first), start != t.offset {103 t.failure = .invalidResponse("server resumed at byte \(start) instead of \(t.offset)"); return false104 }105 return true106 case 200:107 if t.offset > 0 {108 // the server ignored the Range header: start the file over109 try? FileManager.default.removeItem(at: t.partial)110 t.onBytes(-t.offset)111 }112 return true113 case 416: t.outcome = .rangeNotSatisfiable; return false114 default:115 t.failure = HuggingFaceAPI.error(status: http.statusCode, repo: t.repo, hasToken: t.hasToken,116 errorCode: http.value(forHTTPHeaderField: "X-Error-Code")) ?? .http(http.statusCode)117 return false118 }119 }120121 private func open(_ t: Transfer) -> Bool {122 if t.handle != nil { return true }123 let fm = FileManager.default124 try? fm.createDirectory(at: t.partial.deletingLastPathComponent(), withIntermediateDirectories: true)125 if !fm.fileExists(atPath: t.partial.path) { fm.createFile(atPath: t.partial.path, contents: nil) }126 guard let handle = try? FileHandle(forWritingTo: t.partial), (try? handle.seekToEnd()) != nil else {127 t.failure = .network("cannot write to \(t.partial.lastPathComponent)"); return false128 }129 t.handle = handle130 return true131 }132133 // MARK: redirects (the Hub redirects LFS files to a presigned CDN URL)134135 func urlSession(_ session: URLSession, task: URLSessionTask, willPerformHTTPRedirection response: HTTPURLResponse, newRequest request: URLRequest,136 completionHandler: @escaping @Sendable (URLRequest?) -> Void) {137 var next = request138 let original = task.originalRequest139 if let range = original?.value(forHTTPHeaderField: "Range") { next.setValue(range, forHTTPHeaderField: "Range") }140 next.setValue("identity", forHTTPHeaderField: "Accept-Encoding")141 if next.url?.host == original?.url?.host, let auth = original?.value(forHTTPHeaderField: "Authorization") {142 next.setValue(auth, forHTTPHeaderField: "Authorization")143 } else {144 next.setValue(nil, forHTTPHeaderField: "Authorization") // never send the token to another host145 }146 completionHandler(next)147 }148149 // MARK: foreground (data tasks, bytes are appended as they arrive)150151 func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive response: URLResponse,152 completionHandler: @escaping @Sendable (URLSession.ResponseDisposition) -> Void) {153 guard let t = transfer(dataTask) else { completionHandler(.cancel); return }154 completionHandler(accept(response, t) && open(t) ? .allow : .cancel)155 }156157 func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {158 guard let t = transfer(dataTask), t.failure == nil, let handle = t.handle else { return }159 do { try handle.write(contentsOf: data); t.onBytes(Int64(data.count)) }160 catch { t.failure = .network("write failed: \(error.localizedDescription)"); dataTask.cancel() }161 }162163 // MARK: background (download tasks, one Range segment at a time, appended when the segment is complete)164165 func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64,166 totalBytesExpectedToWrite: Int64) {167 guard let t = transfer(downloadTask) else { return }168 t.provisional += bytesWritten; t.onBytes(bytesWritten)169 }170171 func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {172 guard let t = transfer(downloadTask) else { return }173 let reported = t.provisional174 guard let response = downloadTask.response, accept(response, t), open(t), let handle = t.handle,175 let source = try? FileHandle(forReadingFrom: location) else {176 t.onBytes(-reported); t.provisional = 0; return177 }178 defer { try? source.close() }179 var appended: Int64 = 0180 do {181 while let chunk = try source.read(upToCount: 4 << 20), !chunk.isEmpty { try handle.write(contentsOf: chunk); appended += Int64(chunk.count) }182 } catch { t.failure = .network("write failed: \(error.localizedDescription)") }183 t.onBytes(appended - reported) // correct the provisional count with what really reached the partial file184 t.provisional = 0185 }186187 // MARK: completion188189 func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {190 lock.lock(); let t = transfers.removeValue(forKey: task.taskIdentifier); lock.unlock()191 guard let t else { return }192 try? t.handle?.close(); t.handle = nil193 if t.provisional != 0 { t.onBytes(-t.provisional); t.provisional = 0 }194 let continuation = t.continuation; t.continuation = nil195 if let failure = t.failure { continuation?.resume(throwing: failure); return }196 if t.outcome == .rangeNotSatisfiable { continuation?.resume(returning: .rangeNotSatisfiable); return }197 if let error {198 let cancelled = (error as? URLError)?.code == .cancelled199 continuation?.resume(throwing: cancelled ? HubError.cancelled : HubError.network(error.localizedDescription))200 } else { continuation?.resume(returning: .complete) }201 }202203 func urlSessionDidFinishEvents(forBackgroundURLSession session: URLSession) {204 lock.lock(); let handler = backgroundCompletion; backgroundCompletion = nil; lock.unlock()205 handler?()206 }207}208209enum FileHasher {210 /// Streaming SHA-256 of a file (lower-case hex). Checks for cancellation between chunks.211 static func sha256(of url: URL) throws -> String {212 let handle = try FileHandle(forReadingFrom: url)213 defer { try? handle.close() }214 var hasher = SHA256()215 while let chunk = try handle.read(upToCount: 4 << 20), !chunk.isEmpty {216 try Task.checkCancellation()217 hasher.update(data: chunk)218 }219 return hasher.finalize().map { String(format: "%02x", $0) }.joined()220 }221}222