Skip to content

Commit

Permalink
Run swiftlint --fix
Browse files Browse the repository at this point in the history
  • Loading branch information
kean committed Jul 14, 2024
1 parent d88dc7c commit 973a1f2
Show file tree
Hide file tree
Showing 52 changed files with 136 additions and 161 deletions.
2 changes: 1 addition & 1 deletion Sources/Pulse/Helpers/CoreData+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ extension NSManagedObjectContext {
return try fetch(request)
}

func fetch<T: NSManagedObject, Value>(_ entity: T.Type, sortedBy keyPath: KeyPath<T, Value>, ascending: Bool = true, _ configure: (NSFetchRequest<T>) -> Void = { _ in }) throws -> [T] {
func fetch<T: NSManagedObject, Value>(_ entity: T.Type, sortedBy keyPath: KeyPath<T, Value>, ascending: Bool = true, _ configure: (NSFetchRequest<T>) -> Void = { _ in }) throws -> [T] {
try fetch(entity) {
$0.sortDescriptors = [NSSortDescriptor(keyPath: keyPath, ascending: ascending)]
}
Expand Down
4 changes: 0 additions & 4 deletions Sources/Pulse/Helpers/Regex.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ final class Regex {
struct Options: OptionSet {
let rawValue: Int

init(rawValue: Int) {
self.rawValue = rawValue
}

static let caseInsensitive = Options(rawValue: 1 << 0)
static let multiline = Options(rawValue: 1 << 1)
static let dotMatchesLineSeparators = Options(rawValue: 1 << 2)
Expand Down
2 changes: 1 addition & 1 deletion Sources/Pulse/LoggerStore/LoggerStore+Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ extension LoggerStore {
/// Opens store in a readonly mode. It won't perform sweeps and will
/// disallow any other modifications.
public static let readonly = Options(rawValue: 1 << 3)

/// Logs are not persistent on the device storage and new log file is created on each app session.
/// Core Data is configured with container type as `NSInMemoryStoreType
///
Expand Down
4 changes: 2 additions & 2 deletions Sources/Pulse/LoggerStore/LoggerStore+Event.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ extension LoggerStore {
self.function = function
self.line = line
}

init(_ entity: LoggerMessageEntity) {
self.createdAt = entity.createdAt
self.label = entity.label
Expand Down Expand Up @@ -116,7 +116,7 @@ extension LoggerStore {
self.label = label
self.taskDescription = taskDescription
}

init(_ entity: NetworkTaskEntity) {
self.taskId = entity.taskId
self.taskType = NetworkLogger.TaskType(rawValue: entity.taskType) ?? .dataTask
Expand Down
4 changes: 2 additions & 2 deletions Sources/Pulse/LoggerStore/LoggerStore+Info.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ extension LoggerStore.Info.AppInfo {
}

private func getAppIcon() -> Data? {
guard let icons = Bundle.main.infoDictionary?["CFBundleIcons"] as? [String:Any],
let primaryIcons = icons["CFBundlePrimaryIcon"] as? [String:Any],
guard let icons = Bundle.main.infoDictionary?["CFBundleIcons"] as? [String: Any],
let primaryIcons = icons["CFBundlePrimaryIcon"] as? [String: Any],
let files = primaryIcons["CFBundleIconFiles"] as? [String],
let lastIcon = files.last,
let image = PlatformImage(named: lastIcon),
Expand Down
2 changes: 1 addition & 1 deletion Sources/Pulse/LoggerStore/LoggerStore+Model.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ extension LoggerStore {

response.properties = [
Attribute(name: "statusCode", type: .integer16AttributeType),
Attribute(name: "httpHeaders", type: .stringAttributeType),
Attribute(name: "httpHeaders", type: .stringAttributeType)
]

progress.properties = [
Expand Down
2 changes: 1 addition & 1 deletion Sources/Pulse/LoggerStore/LoggerStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ extension LoggerStore {
entity.response = event.response.map(makeResponse)
entity.rawMetadata = {
guard let responseBody = event.responseBody,
(responseContentType?.isImage ?? false),
responseContentType?.isImage ?? false,
let metadata = Graphics.makeMetadata(from: responseBody) else {
return nil
}
Expand Down
10 changes: 5 additions & 5 deletions Sources/Pulse/NetworkLogger/NetworkLogger+Entities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ extension NetworkLogger {
if request.httpShouldHandleCookies { insert(.httpShouldHandleCookies) }
if request.httpShouldUsePipelining { insert(.httpShouldUsePipelining) }
}

init(_ entity: NetworkRequestEntity) {
self = []
if entity.allowsCellularAccess { insert(.allowsCellularAccess) }
Expand All @@ -58,7 +58,7 @@ extension NetworkLogger {
self.timeout = urlRequest.timeoutInterval
self.options = Options(urlRequest)
}

init(_ entity: NetworkRequestEntity) {
self.url = entity.url.flatMap(URL.init)
self.httpMethod = entity.httpMethod
Expand Down Expand Up @@ -87,7 +87,7 @@ extension NetworkLogger {
self.statusCode = httpResponse?.statusCode
self.headers = httpResponse?.allHeaderFields as? [String: String]
}

init(_ entity: NetworkResponseEntity) {
self.statusCode = Int(entity.statusCode)
self.headers = entity.headers
Expand Down Expand Up @@ -291,7 +291,7 @@ extension NetworkLogger {
if metrics.isConstrained { insert(.isConstrained) }
if metrics.isMultipath { insert(.isMultipath) }
}

init(_ entity: NetworkTransactionMetricsEntity) {
self = []
if entity.isProxyConnection { insert(.isProxyConnection) }
Expand All @@ -302,7 +302,7 @@ extension NetworkLogger {
if entity.isMultipath { insert(.isMultipath) }
}
}

init(_ entity: NetworkTransactionMetricsEntity) {
self.request = NetworkLogger.Request(entity.request)
self.response = entity.response.map(NetworkLogger.Response.init)
Expand Down
2 changes: 1 addition & 1 deletion Sources/Pulse/NetworkLogger/URLSessionProxyDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public final class URLSessionProxyDelegate: NSObject, URLSessionTaskDelegate, UR
#selector(URLSessionTaskDelegate.urlSession(_:task:didFinishCollecting:)),
#selector(URLSessionTaskDelegate.urlSession(_:task:didSendBodyData:totalBytesSent:totalBytesExpectedToSend:)),
#selector(URLSessionDownloadDelegate.urlSession(_:downloadTask:didFinishDownloadingTo:)),
#selector(URLSessionDownloadDelegate.urlSession(_:downloadTask:didWriteData:totalBytesWritten:totalBytesExpectedToWrite:)),
#selector(URLSessionDownloadDelegate.urlSession(_:downloadTask:didWriteData:totalBytesWritten:totalBytesExpectedToWrite:))
]
if #available(iOS 16.0, tvOS 16.0, macOS 13.0, watchOS 9.0, *) {
self.interceptedSelectors.insert(
Expand Down
18 changes: 9 additions & 9 deletions Sources/Pulse/RemoteLogger/RemoteLogger-Connection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ extension RemoteLogger {
let isLogEnabled = UserDefaults.standard.bool(forKey: "com.github.kean.pulse.debug")
self.log = isLogEnabled ? OSLog(subsystem: "com.github.kean.pulse", category: "RemoteLogger") : .disabled
}

func start(on queue: DispatchQueue) {
connection.stateUpdateHandler = { [weak self] state in
guard let self = self else { return }
Expand Down Expand Up @@ -126,7 +126,7 @@ extension RemoteLogger {
}
}
}

func send(code: UInt8, data: Data) {
do {
let data = try encode(code: code, body: data)
Expand All @@ -149,24 +149,24 @@ extension RemoteLogger {
os_log("Failed to encode a packet: %{public}@", log: log, type: .error, "\(error)")
}
}

func sendMessage<T: Encodable>(path: Path, entity: T, _ completion: ((Data?, Error?) -> Void)? = nil) {
do {
sendMessage(path: path, data: try JSONEncoder().encode(entity), completion)
} catch {
os_log("Failed to send a message: %{public}@", log: log, type: .error, "\(error)")
}
}

func sendMessage(path: Path, data: Data? = nil, _ completion: ((Data?, Error?) -> Void)? = nil) {
let message = Message(id: id, options: [], path: path, data: data ?? Data())

if id == UInt32.max {
id = 0
} else {
id += 1
}

if let completion = completion {
let id = message.id
handlers[message.id] = completion
Expand All @@ -176,7 +176,7 @@ extension RemoteLogger {
}
}
}

do {
let data = try Message.encode(message)
send(code: .message, data: data)
Expand All @@ -192,7 +192,7 @@ extension RemoteLogger {
os_log("Failed to encode a response: %{public}@", log: log, type: .error, "\(error)")
}
}

func sendResponse(for message: Message, data: Data) {
let message = Message(id: message.id, options: [.response], path: message.path, data: data)
do {
Expand All @@ -202,7 +202,7 @@ extension RemoteLogger {
os_log("Failed to encode a response: %{public}@", log: log, type: .error, "\(error)")
}
}

func cancel() {
connection.cancel()
}
Expand Down
48 changes: 21 additions & 27 deletions Sources/Pulse/RemoteLogger/RemoteLogger-Protocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,29 +38,28 @@ extension RemoteLogger {
}

struct Empty: Codable {
init() {}
}

struct PacketNetworkMessage {
private struct Manifest: Codable {
let messageSize: UInt32
let requestBodySize: UInt32
let responseBodySize: UInt32

static let size = 12

var totalSize: Int {
Manifest.size + Int(messageSize) + Int(requestBodySize) + Int(responseBodySize)
}
}

static func encode(_ event: LoggerStore.Event.NetworkTaskCompleted) throws -> Data {
var contents = [Data]()

var slimEvent = event
slimEvent.requestBody = nil // Sent separately using binary
slimEvent.responseBody = nil

let messageData = try JSONEncoder().encode(slimEvent)
contents.append(messageData)
if let requestBody = event.requestBody, requestBody.count < Int32.max {
Expand All @@ -69,7 +68,7 @@ extension RemoteLogger {
if let responseBody = event.responseBody, responseBody.count < Int32.max {
contents.append(responseBody)
}

var data = Data()
data.append(Data(UInt32(messageData.count)))
data.append(Data(UInt32(event.requestBody?.count ?? 0)))
Expand All @@ -79,48 +78,48 @@ extension RemoteLogger {
}
return data
}

static func decode(_ data: Data) throws -> LoggerStore.Event.NetworkTaskCompleted {
guard data.count >= Manifest.size else {
throw PacketParsingError.notEnoughData // Should never happen
}

let manifest = Manifest(
messageSize: UInt32(data.from(0, size: 4)),
requestBodySize: UInt32(data.from(4, size: 4)),
responseBodySize: UInt32(data.from(8, size: 4))
)

guard data.count >= manifest.totalSize else {
throw PacketParsingError.notEnoughData // This should never happen
}

let event = try JSONDecoder().decode(
LoggerStore.Event.NetworkTaskCompleted.self,
from: data.from(Manifest.size, size: Int(manifest.messageSize))
)

var requestBody: Data?
if manifest.requestBodySize > 0 {
requestBody = data.from(Manifest.size + Int(manifest.messageSize), size: Int(manifest.requestBodySize))
}

var responseBody: Data?
if manifest.responseBodySize > 0 {
responseBody = data.from(Manifest.size + Int(manifest.messageSize) + Int(manifest.requestBodySize), size: Int(manifest.responseBodySize))
}

return LoggerStore.Event.NetworkTaskCompleted(taskId: event.taskId, taskType: event.taskType, createdAt: event.createdAt, originalRequest: event.originalRequest, currentRequest: event.currentRequest, response: event.response, error: event.error, requestBody: requestBody, responseBody: responseBody, metrics: event.metrics, label: event.label, taskDescription: event.taskDescription)
}
}

struct Message {
struct Header {
let id: UInt32
let options: Options
let pathSize: UInt32
let dataSize: UInt32

init?(_ data: Data) {
guard data.count >= headerSize else { return nil }
self.id = UInt32(data.from(0, size: 4))
Expand All @@ -129,25 +128,24 @@ extension RemoteLogger {
self.dataSize = UInt32(data.from(9, size: 4))
}
}

struct Options: OptionSet {
let rawValue: UInt8
init(rawValue: UInt8) { self.rawValue = rawValue }

static let response = Options(rawValue: 1 << 0)
}

let id: UInt32
let options: Options
let path: Path
let data: Data

// - id (UInt32)
// - options (UInt8)
// - path size (UInt32)
// - data size (UInt32)
private static let headerSize = 13

static func encode(_ message: Message) throws -> Data {
guard let path = try? JSONEncoder().encode(message.path) else {
throw URLError(.unknown, userInfo: [:]) // Should never happen
Expand Down Expand Up @@ -182,7 +180,7 @@ extension RemoteLogger {
case notEnoughData
case unsupportedContentSize
}

enum Path: Codable {
case updateMocks
case getMockedResponse(mockID: UUID)
Expand All @@ -194,10 +192,6 @@ extension RemoteLogger {

struct ServerHelloResponse: Codable {
let version: String

init(version: String) {
self.version = version
}
}
}

Expand Down
Loading

0 comments on commit 973a1f2

Please sign in to comment.