Skip to content

Commit

Permalink
Merge pull request #285 from kean/task/url-session-protocol
Browse files Browse the repository at this point in the history
Add URLSessionProxy
  • Loading branch information
kean authored Sep 3, 2024
2 parents 97a4491 + 2d3043a commit 42db2eb
Show file tree
Hide file tree
Showing 46 changed files with 1,029 additions and 587 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ jobs:
- uses: actions/checkout@v2
- name: Build Demo
run: |
cd Demo
set -o pipefail
xcodebuild build -scheme "Pulse Demo iOS" -destination "OS=17.4,name=iPhone 15 Pro" | xcpretty
build-demo-tvos:
Expand All @@ -103,6 +104,7 @@ jobs:
- uses: actions/checkout@v2
- name: Build Demo
run: |
cd Demo
set -o pipefail
xcodebuild build -scheme "Pulse Demo tvOS" -destination "OS=17.4,name=Apple TV" | xcpretty
build-integration-examples-ios:
Expand All @@ -114,5 +116,6 @@ jobs:
- uses: actions/checkout@v2
- name: Build Integration Tests
run: |
cd Demo
set -o pipefail
xcodebuild build -scheme "Pulse Integration Examples iOS" -destination "OS=17.4,name=iPhone 15 Pro" | xcpretty
11 changes: 9 additions & 2 deletions Demo/Pulse.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
0C9F04F92884F34A0035239F /* Pulse_Demo_macOSApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0C9F04F82884F34A0035239F /* Pulse_Demo_macOSApp.swift */; };
0C9F04FD2884F34A0035239F /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 0C9F04FC2884F34A0035239F /* Assets.xcassets */; };
0C9F05002884F34A0035239F /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 0C9F04FF2884F34A0035239F /* Preview Assets.xcassets */; };
0CA245732C85E87A00B432DA /* PulseProxy in Frameworks */ = {isa = PBXBuildFile; productRef = 0CA245722C85E87A00B432DA /* PulseProxy */; };
0CDACDE529EC6607007C15CD /* repos.json in Resources */ = {isa = PBXBuildFile; fileRef = 0CDACDE129EC6607007C15CD /* repos.json */; };
0CDACDE629EC6607007C15CD /* repos.json in Resources */ = {isa = PBXBuildFile; fileRef = 0CDACDE129EC6607007C15CD /* repos.json */; };
/* End PBXBuildFile section */
Expand Down Expand Up @@ -152,6 +153,7 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
0CA245732C85E87A00B432DA /* PulseProxy in Frameworks */,
0C8FCB522C45F05400C4FD84 /* PulseUI in Frameworks */,
0C8FCB502C45F05400C4FD84 /* Pulse in Frameworks */,
);
Expand Down Expand Up @@ -382,6 +384,7 @@
packageProductDependencies = (
0C8FCB4F2C45F05400C4FD84 /* Pulse */,
0C8FCB512C45F05400C4FD84 /* PulseUI */,
0CA245722C85E87A00B432DA /* PulseProxy */,
);
productName = "Pulse Demo iOS";
productReference = 0C70EA732A3F611B000B1071 /* Pulse Demo iOS.app */;
Expand Down Expand Up @@ -791,7 +794,7 @@
INFOPLIST_KEY_UILaunchScreen_Generation = YES;
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
IPHONEOS_DEPLOYMENT_TARGET = 14.0;
IPHONEOS_DEPLOYMENT_TARGET = 15.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand Down Expand Up @@ -835,7 +838,7 @@
INFOPLIST_KEY_UILaunchScreen_Generation = YES;
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
IPHONEOS_DEPLOYMENT_TARGET = 14.0;
IPHONEOS_DEPLOYMENT_TARGET = 15.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand Down Expand Up @@ -1033,6 +1036,10 @@
isa = XCSwiftPackageProductDependency;
productName = PulseUI;
};
0CA245722C85E87A00B432DA /* PulseProxy */ = {
isa = XCSwiftPackageProductDependency;
productName = PulseProxy;
};
/* End XCSwiftPackageProductDependency section */
};
rootObject = 0C57AD45245F0EFB005B3400 /* Project object */;
Expand Down
53 changes: 39 additions & 14 deletions Demo/Sources/iOS/Pulse_Demo_iOSApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import SwiftUI
import Pulse
import PulseUI
import OSLog
import PulseProxy

@main
struct PulseDemo_iOS: App {
Expand All @@ -21,27 +21,52 @@ struct PulseDemo_iOS: App {
}

private final class AppViewModel: ObservableObject {
let log = OSLog(subsystem: "app", category: "AppViewModel")

init() {
// URLSessionProxyDelegate.enableAutomaticRegistration()
// URLSessionProxy.enable()
// NetworkLogger.enableProxy()

DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(3)) {
sendRequest()
}
// DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(6)) {
// sendRequest()
// }
// DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(9)) {
// sendRequest()
// }
}
}


private func sendRequest() {
let session = URLSession(configuration: .default, delegate: DemoSessionDelegate(), delegateQueue: nil)
let task = session.dataTask(with: URLRequest(url: URL(string: "https://github.com/kean/Nuke/archive/refs/tags/11.0.0.zip")!))
testSwiftConcurrency()

// let task = session.dataTask(with: URLRequest(url: URL(string: "https://github.com/kean/Nuke/archive/refs/tags/11.0.0.zip")!))
// task.resume()
}

private func testClosures() {
let session = URLSessionProxy(configuration: .default)
let task = session.dataTask(with: URLRequest(url: URL(string: "https://api.github.com/repos/octocat/Spoon-Knife/issues?per_page=2")!)) { data, _, _ in
NSLog("didFinish: \(data?.count ?? 0)")
}
task.resume()
}

private final class DemoSessionDelegate: NSObject, URLSessionDelegate {}
private func testSwiftConcurrency() {
Task {
let demoDelegate = DemoSessionDelegate()
let session = URLSessionProxy(configuration: .default, delegate: demoDelegate, delegateQueue: nil)
// let session = URLSession(configuration: .default)

let (data, _) = try await session.data(from: URL(string: "https://api.github.com/repos/octocat/Spoon-Knife/issues?per_page=2")!) //, delegate: demoDelegate)
NSLog("didFinish: \(data.count)")
}
}

private final class DemoSessionDelegate: NSObject, URLSessionDelegate, URLSessionDataDelegate {
func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {
NSLog("[\(dataTask.taskIdentifier)] didReceive: \(data.count)")
}

func urlSession(_ session: URLSession, task: URLSessionTask, didFinishCollecting metrics: URLSessionTaskMetrics) {
NSLog("[\(task.taskIdentifier)] didFinishCollectingMetrics: \(metrics)")
}

func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: (any Error)?) {
NSLog("[\(task.taskIdentifier)] didCompleteWithError: \(String(describing: error))")
}
}
6 changes: 4 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
// swift-tools-version:5.9
// swift-tools-version:5.10
import PackageDescription

let package = Package(
name: "Pulse",
platforms: [
.iOS(.v14),
.iOS(.v15),
.tvOS(.v15),
.macOS(.v12),
.watchOS(.v8)
],
products: [
.library(name: "Pulse", targets: ["Pulse"]),
.library(name: "PulseProxy", targets: ["PulseProxy"]),
.library(name: "PulseUI", targets: ["PulseUI"])
],
targets: [
.target(name: "Pulse"),
.target(name: "PulseProxy", dependencies: ["Pulse"]),
.target(name: "PulseUI", dependencies: ["Pulse"]),
],
swiftLanguageVersions: [
Expand Down
1 change: 1 addition & 0 deletions Sources/Pulse/Helpers/PulseDocument.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ final class PulseDocument {
}
}

/// - warning: Model has to be loaded only once.
static let model: NSManagedObjectModel = {
let model = NSManagedObjectModel()
let blob = NSEntityDescription(class: PulseBlobEntity.self)
Expand Down
2 changes: 1 addition & 1 deletion Sources/Pulse/Helpers/Regex.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import Foundation

final class Regex {
final class Regex: @unchecked Sendable {
private let regex: NSRegularExpression

struct Options: OptionSet {
Expand Down
68 changes: 0 additions & 68 deletions Sources/Pulse/Helpers/Version.swift

This file was deleted.

13 changes: 5 additions & 8 deletions Sources/Pulse/LoggerStore/LoggerStore+Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ extension LoggerStore {

/// The store configuration.
public struct Configuration: @unchecked Sendable {
/// Size limit in bytes. `128 MB` by default.
/// Size limit in bytes. `256 MB` by default.
public var sizeLimit: Int64

var blobSizeLimit: Int64 {
Expand All @@ -75,8 +75,8 @@ extension LoggerStore {
public var isStoringOnlyImageThumbnails = true

/// Limit the maximum response size stored by the logger. The default
/// value is `5 Mb`. The same limit applies to requests.
public var responseBodySizeLimit: Int = 5 * 1048576
/// value is `8 MB`. The same limit applies to requests.
public var responseBodySizeLimit: Int = 8 * 1048576

var inlineLimit = 16384 // 16 KB

Expand All @@ -87,9 +87,6 @@ extension LoggerStore {
/// ``LoggerStore/Options-swift.struct/sweep`` option. The default store supports sweeps.
public var maxAge: TimeInterval = 14 * 86400

/// For testing purposes.
var makeCurrentDate: () -> Date = { Date() }

/// Gets called when the store receives an event. You can use it to
/// modify the event before it is stored in order, for example, filter
/// out some sensitive information. If you return `nil`, the event
Expand All @@ -102,8 +99,8 @@ extension LoggerStore {
///
/// - parameters:
/// - sizeLimit: The approximate limit of the logger store, including
/// both the database and the blobs. `128 Mb` by default.
public init(sizeLimit: Int64 = 128 * 1_000_000) {
/// both the database and the blobs. `256 Mb` by default.
public init(sizeLimit: Int64 = 256 * 1_000_000) {
self.sizeLimit = sizeLimit
}
}
Expand Down
12 changes: 9 additions & 3 deletions Sources/Pulse/LoggerStore/LoggerStore+Info.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,14 @@ private func getAppIcon() -> Data? {
#if os(iOS) || os(tvOS) || os(visionOS)
import UIKit

@MainActor
func getDeviceId() -> UUID? {
UIDevice.current.identifierForVendor
}

extension LoggerStore.Info.DeviceInfo {
static func make() -> LoggerStore.Info.DeviceInfo {
@MainActor
static func make() -> LoggerStore.Info.DeviceInfo {
let device = UIDevice.current
return LoggerStore.Info.DeviceInfo(
name: device.name,
Expand All @@ -123,12 +125,14 @@ extension LoggerStore.Info.DeviceInfo {
#elseif os(watchOS)
import WatchKit

@MainActor
func getDeviceId() -> UUID? {
WKInterfaceDevice.current().identifierForVendor
}

extension LoggerStore.Info.DeviceInfo {
static func make() -> LoggerStore.Info.DeviceInfo {
@MainActor
static func make() -> LoggerStore.Info.DeviceInfo {
let device = WKInterfaceDevice.current()
return LoggerStore.Info.DeviceInfo(
name: device.name,
Expand All @@ -143,7 +147,8 @@ extension LoggerStore.Info.DeviceInfo {
import AppKit

extension LoggerStore.Info.DeviceInfo {
static func make() -> LoggerStore.Info.DeviceInfo {
@MainActor
static func make() -> LoggerStore.Info.DeviceInfo {
return LoggerStore.Info.DeviceInfo(
name: Host.current().name ?? "unknown",
model: "unknown",
Expand All @@ -154,6 +159,7 @@ extension LoggerStore.Info.DeviceInfo {
}
}

@MainActor
func getDeviceId() -> UUID? {
return nil
}
Expand Down
2 changes: 2 additions & 0 deletions Sources/Pulse/LoggerStore/LoggerStore+Model.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import CoreData

extension LoggerStore {
/// Returns Core Data model used by the store.
///
/// - warning: Model has to be loaded only once.
static let model: NSManagedObjectModel = {
typealias Entity = NSEntityDescription
typealias Attribute = NSAttributeDescription
Expand Down
Loading

0 comments on commit 42db2eb

Please sign in to comment.