mirror of
https://github.com/cranci1/Sora.git
synced 2026-03-11 17:45:37 +00:00
fixed fetchV2 crash
This commit is contained in:
parent
0cd3be5401
commit
c7e7672bf3
1 changed files with 30 additions and 10 deletions
|
|
@ -111,19 +111,33 @@ extension JSContext {
|
|||
let task = URLSession.fetchData(allowRedirects: redirect.boolValue).downloadTask(with: request) { tempFileURL, response, error in
|
||||
if let error = error {
|
||||
Logger.shared.log("Network error in fetchV2NativeFunction: \(error.localizedDescription)", type: "Error")
|
||||
reject.call(withArguments: [error.localizedDescription])
|
||||
DispatchQueue.main.async {
|
||||
reject.call(withArguments: [error.localizedDescription])
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
guard let tempFileURL = tempFileURL else {
|
||||
Logger.shared.log("No data in response", type: "Error")
|
||||
reject.call(withArguments: ["No data"])
|
||||
DispatchQueue.main.async {
|
||||
reject.call(withArguments: ["No data"])
|
||||
}
|
||||
return
|
||||
}
|
||||
// initialise return Object
|
||||
|
||||
var safeHeaders: [String: String] = [:]
|
||||
if let httpResponse = response as? HTTPURLResponse {
|
||||
for (key, value) in httpResponse.allHeaderFields {
|
||||
if let keyString = key as? String,
|
||||
let valueString = value as? String {
|
||||
safeHeaders[keyString] = valueString
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var responseDict: [String: Any] = [
|
||||
"status": (response as? HTTPURLResponse)?.statusCode ?? 0,
|
||||
"headers": (response as? HTTPURLResponse)?.allHeaderFields ?? [:],
|
||||
"headers": safeHeaders,
|
||||
"body": ""
|
||||
]
|
||||
|
||||
|
|
@ -132,23 +146,29 @@ extension JSContext {
|
|||
|
||||
if data.count > 10_000_000 {
|
||||
Logger.shared.log("Response exceeds maximum size", type: "Error")
|
||||
reject.call(withArguments: ["Response exceeds maximum size"])
|
||||
DispatchQueue.main.async {
|
||||
reject.call(withArguments: ["Response exceeds maximum size"])
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if let text = String(data: data, encoding: .utf8) {
|
||||
|
||||
responseDict["body"] = text
|
||||
resolve.call(withArguments: [responseDict])
|
||||
DispatchQueue.main.async {
|
||||
resolve.call(withArguments: [responseDict])
|
||||
}
|
||||
} else {
|
||||
// rather than reject -> resolve with empty body as user can utilise reponse headers.
|
||||
Logger.shared.log("Unable to decode data to text", type: "Error")
|
||||
resolve.call(withArguments: [responseDict])
|
||||
DispatchQueue.main.async {
|
||||
resolve.call(withArguments: [responseDict])
|
||||
}
|
||||
}
|
||||
|
||||
} catch {
|
||||
Logger.shared.log("Error reading downloaded file: \(error.localizedDescription)", type: "Error")
|
||||
reject.call(withArguments: ["Error reading downloaded file"])
|
||||
DispatchQueue.main.async {
|
||||
reject.call(withArguments: ["Error reading downloaded file"])
|
||||
}
|
||||
}
|
||||
}
|
||||
task.resume()
|
||||
|
|
|
|||
Loading…
Reference in a new issue