@@ -774,24 +774,72 @@ final class WebViewNetworkMessageHandler: NSObject, WKScriptMessageHandler {
774774
775775// MARK: - Request Cache
776776
777+ struct WebViewRequestInfo : Sendable {
778+ let requestId : String
779+ let url : String
780+ let method : String
781+ let startTime : Date
782+ let headers : [ String : String ]
783+ let body : String ?
784+
785+ init ( from dictionary: [ String : Any ] ) {
786+ self . requestId = dictionary [ " requestId " ] as? String ?? " "
787+ self . url = dictionary [ " url " ] as? String ?? " "
788+ self . method = dictionary [ " method " ] as? String ?? " GET "
789+ self . startTime = dictionary [ " startTime " ] as? Date ?? Date ( )
790+ self . headers = dictionary [ " headers " ] as? [ String : String ] ?? [ : ]
791+
792+ // Convert body to string if needed
793+ if let bodyData = dictionary [ " body " ] {
794+ if let bodyString = bodyData as? String {
795+ self . body = bodyString
796+ } else if let bodyDict = bodyData as? [ String : Any ] ,
797+ let data = try ? JSONSerialization . data ( withJSONObject: bodyDict) ,
798+ let jsonString = String ( data: data, encoding: . utf8) {
799+ self . body = jsonString
800+ } else {
801+ self . body = " \( bodyData) "
802+ }
803+ } else {
804+ self . body = nil
805+ }
806+ }
807+
808+ func toDictionary( ) -> [ String : Any ] {
809+ var dict : [ String : Any ] = [
810+ " requestId " : requestId,
811+ " url " : url,
812+ " method " : method,
813+ " startTime " : startTime,
814+ " headers " : headers
815+ ]
816+
817+ if let body = body {
818+ dict [ " body " ] = body
819+ }
820+
821+ return dict
822+ }
823+ }
824+
777825final class WebViewRequestCache : @unchecked Sendable {
778826 static let shared = WebViewRequestCache ( )
779827
780- private var cache : [ String : [ String : Any ] ] = [ : ]
828+ private var cache : [ String : WebViewRequestInfo ] = [ : ]
781829 private let queue = DispatchQueue ( label: " com.debugswift.webview.cache " , attributes: . concurrent)
782830
783831 private init ( ) { }
784832
785833 nonisolated func store( requestId: String , requestInfo: [ String : Any ] ) {
786- queue . async ( flags : . barrier ) { [ weak self , requestInfo] in
787- // Safe: We control the threading and dictionary access via barriers
788- self ? . cache [ requestId] = requestInfo
834+ let sendableInfo = WebViewRequestInfo ( from : requestInfo)
835+ queue . async ( flags : . barrier ) { [ weak self ] in
836+ self ? . cache [ requestId] = sendableInfo
789837 }
790838 }
791839
792840 nonisolated func retrieve( requestId: String ) -> [ String : Any ] ? {
793841 return queue. sync { [ weak self] in
794- return self ? . cache [ requestId]
842+ return self ? . cache [ requestId] ? . toDictionary ( )
795843 }
796844 }
797845
0 commit comments