fix(macos): use safe FileHandle reads

This commit is contained in:
Peter Steinberger
2025-12-16 10:41:18 +01:00
parent b443c20cef
commit 64d6d25d65
10 changed files with 77 additions and 14 deletions

View File

@@ -263,7 +263,7 @@ enum BrowserCLI {
return try String(contentsOfFile: jsFile, encoding: .utf8)
}
if options.jsStdin {
let data = FileHandle.standardInput.readDataToEndOfFile()
let data = FileHandle.standardInput.readToEndSafely()
return String(data: data, encoding: .utf8) ?? ""
}
if let js = options.js, !js.isEmpty {

View File

@@ -0,0 +1,25 @@
import Foundation
extension FileHandle {
/// Reads until EOF using the throwing FileHandle API and returns empty `Data` on failure.
///
/// Important: Avoid legacy, non-throwing FileHandle read APIs (e.g. `readDataToEndOfFile()` and
/// `availableData`). They can raise Objective-C exceptions when the handle is closed/invalid, which
/// will abort the process.
func readToEndSafely() -> Data {
do {
return try self.readToEnd() ?? Data()
} catch {
return Data()
}
}
/// Reads up to `count` bytes using the throwing FileHandle API and returns empty `Data` on failure/EOF.
func readSafely(upToCount count: Int) -> Data {
do {
return try self.read(upToCount: count) ?? Data()
} catch {
return Data()
}
}
}