Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Enable gzip support for event payloads #379

Merged
merged 1 commit into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .sourcery.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ args:
imports:
- Foundation
- LDSwiftEventSource
- GZIP
3 changes: 2 additions & 1 deletion ContractTests/Source/Controllers/SdkController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ final class SdkController: RouteCollection {
"etag-caching",
"inline-context",
"anonymous-redaction",
"evaluation-hooks"
"evaluation-hooks",
"event-gzip"
]

return StatusResponse(
Expand Down
3 changes: 3 additions & 0 deletions LaunchDarkly.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,8 @@ Pod::Spec.new do |ld|

ld.subspec 'Core' do |es|
es.dependency 'LDSwiftEventSource', '3.1.1'
es.ios.dependency 'GZIP', '1.3.2'
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This gzip library doesn't support watchos. Which is probably reasonable since CPU cycles are going to be more precious on that device.

es.macos.dependency 'GZIP', '1.3.2'
es.tvos.dependency 'GZIP', '1.3.2'
end
end
71 changes: 67 additions & 4 deletions LaunchDarkly.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,9 @@
A3C6F7652B84EF0C005B3B61 /* IdentifyResult.swift in Sources */ = {isa = PBXBuildFile; fileRef = A3C6F7632B84EF0C005B3B61 /* IdentifyResult.swift */; };
A3C6F7662B84EF0C005B3B61 /* IdentifyResult.swift in Sources */ = {isa = PBXBuildFile; fileRef = A3C6F7632B84EF0C005B3B61 /* IdentifyResult.swift */; };
A3C6F7672B84EF0C005B3B61 /* IdentifyResult.swift in Sources */ = {isa = PBXBuildFile; fileRef = A3C6F7632B84EF0C005B3B61 /* IdentifyResult.swift */; };
A3D748122BF3CED10025824F /* GZIP in Frameworks */ = {isa = PBXBuildFile; productRef = A3D748112BF3CED10025824F /* GZIP */; };
A3D748162BF3CEF10025824F /* GZIP in Frameworks */ = {isa = PBXBuildFile; productRef = A3D748152BF3CEF10025824F /* GZIP */; };
A3D748182BF3CEF50025824F /* GZIP in Frameworks */ = {isa = PBXBuildFile; productRef = A3D748172BF3CEF50025824F /* GZIP */; };
A3FFE1132B7D4BA2009EF93F /* LDValueDecoderSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = A3FFE1122B7D4BA2009EF93F /* LDValueDecoderSpec.swift */; };
B40B419C249ADA6B00CD0726 /* DiagnosticCacheSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = B40B419B249ADA6B00CD0726 /* DiagnosticCacheSpec.swift */; };
B4265EB124E7390C001CFD2C /* TestUtil.swift in Sources */ = {isa = PBXBuildFile; fileRef = B4265EB024E7390C001CFD2C /* TestUtil.swift */; };
Expand Down Expand Up @@ -529,6 +532,7 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
A3D748182BF3CEF50025824F /* GZIP in Frameworks */,
A30EF4F328C24AB600CD220E /* LDSwiftEventSource in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand All @@ -537,6 +541,7 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
A3D748162BF3CEF10025824F /* GZIP in Frameworks */,
A30EF4F128C24AAD00CD220E /* LDSwiftEventSource in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand All @@ -545,6 +550,7 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
A3D748122BF3CED10025824F /* GZIP in Frameworks */,
A30EF4ED28C24A9A00CD220E /* LDSwiftEventSource in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -981,11 +987,13 @@
buildRules = (
);
dependencies = (
A3D748202BF3CF160025824F /* PBXTargetDependency */,
B445A6FB24C0D237000BAD6D /* PBXTargetDependency */,
);
name = LaunchDarkly_tvOS;
packageProductDependencies = (
A30EF4F228C24AB600CD220E /* LDSwiftEventSource */,
A3D748172BF3CEF50025824F /* GZIP */,
);
productName = Darkly_tvOS;
productReference = 831188382113A16900D77CB5 /* LaunchDarkly_tvOS.framework */;
Expand All @@ -1005,11 +1013,13 @@
buildRules = (
);
dependencies = (
A3D7481E2BF3CF100025824F /* PBXTargetDependency */,
B445A6F924C0D232000BAD6D /* PBXTargetDependency */,
);
name = LaunchDarkly_macOS;
packageProductDependencies = (
A30EF4F028C24AAD00CD220E /* LDSwiftEventSource */,
A3D748152BF3CEF10025824F /* GZIP */,
);
productName = Darkly_macOS;
productReference = 831EF33B20655D700001C643 /* LaunchDarkly_macOS.framework */;
Expand All @@ -1029,11 +1039,13 @@
buildRules = (
);
dependencies = (
A3D7481A2BF3CF060025824F /* PBXTargetDependency */,
B445A6E424C0D1E3000BAD6D /* PBXTargetDependency */,
);
name = LaunchDarkly_iOS;
packageProductDependencies = (
A30EF4EC28C24A9A00CD220E /* LDSwiftEventSource */,
A3D748112BF3CED10025824F /* GZIP */,
);
productName = Darkly;
productReference = 8354EFC21F22491C00C05156 /* LaunchDarkly.framework */;
Expand Down Expand Up @@ -1100,17 +1112,17 @@
TargetAttributes = {
831188372113A16900D77CB5 = {
CreatedOnToolsVersion = 9.2;
LastSwiftMigration = 1020;
LastSwiftMigration = 1540;
ProvisioningStyle = Automatic;
};
831EF33A20655D700001C643 = {
CreatedOnToolsVersion = 9.2;
LastSwiftMigration = 1020;
LastSwiftMigration = 1540;
ProvisioningStyle = Automatic;
};
8354EFC11F22491C00C05156 = {
CreatedOnToolsVersion = 8.3.3;
LastSwiftMigration = 1020;
LastSwiftMigration = 1540;
ProvisioningStyle = Automatic;
};
8354EFCA1F22491C00C05156 = {
Expand All @@ -1120,7 +1132,7 @@
};
83D9EC6A2062DBB7004D7FA6 = {
CreatedOnToolsVersion = 9.2;
LastSwiftMigration = 1020;
LastSwiftMigration = 1540;
ProvisioningStyle = Automatic;
};
};
Expand All @@ -1139,6 +1151,7 @@
B4903D9924BD61D000F087C4 /* XCRemoteSwiftPackageReference "Nimble" */,
B4903D9C24BD61EF00F087C4 /* XCRemoteSwiftPackageReference "Quick" */,
B445A6DE24C0D1CD000BAD6D /* XCRemoteSwiftPackageReference "swift-eventsource" */,
A3D748102BF3CED10025824F /* XCRemoteSwiftPackageReference "GZIP" */,
);
productRefGroup = 8354EFC31F22491C00C05156 /* Products */;
projectDirPath = "";
Expand Down Expand Up @@ -1625,6 +1638,18 @@
target = 8354EFC11F22491C00C05156 /* LaunchDarkly_iOS */;
targetProxy = 8354EFCD1F22491C00C05156 /* PBXContainerItemProxy */;
};
A3D7481A2BF3CF060025824F /* PBXTargetDependency */ = {
isa = PBXTargetDependency;
productRef = A3D748192BF3CF060025824F /* GZIP */;
};
A3D7481E2BF3CF100025824F /* PBXTargetDependency */ = {
isa = PBXTargetDependency;
productRef = A3D7481D2BF3CF100025824F /* GZIP */;
};
A3D748202BF3CF160025824F /* PBXTargetDependency */ = {
isa = PBXTargetDependency;
productRef = A3D7481F2BF3CF160025824F /* GZIP */;
};
B445A6E424C0D1E3000BAD6D /* PBXTargetDependency */ = {
isa = PBXTargetDependency;
productRef = B445A6E324C0D1E3000BAD6D /* LDSwiftEventSource */;
Expand Down Expand Up @@ -2051,6 +2076,14 @@
/* End XCConfigurationList section */

/* Begin XCRemoteSwiftPackageReference section */
A3D748102BF3CED10025824F /* XCRemoteSwiftPackageReference "GZIP" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/nicklockwood/GZIP";
requirement = {
kind = exactVersion;
version = 1.3.2;
};
};
B445A6DE24C0D1CD000BAD6D /* XCRemoteSwiftPackageReference "swift-eventsource" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/LaunchDarkly/swift-eventsource.git";
Expand Down Expand Up @@ -2106,6 +2139,36 @@
package = B445A6DE24C0D1CD000BAD6D /* XCRemoteSwiftPackageReference "swift-eventsource" */;
productName = LDSwiftEventSource;
};
A3D748112BF3CED10025824F /* GZIP */ = {
isa = XCSwiftPackageProductDependency;
package = A3D748102BF3CED10025824F /* XCRemoteSwiftPackageReference "GZIP" */;
productName = GZIP;
};
A3D748152BF3CEF10025824F /* GZIP */ = {
isa = XCSwiftPackageProductDependency;
package = A3D748102BF3CED10025824F /* XCRemoteSwiftPackageReference "GZIP" */;
productName = GZIP;
};
A3D748172BF3CEF50025824F /* GZIP */ = {
isa = XCSwiftPackageProductDependency;
package = A3D748102BF3CED10025824F /* XCRemoteSwiftPackageReference "GZIP" */;
productName = GZIP;
};
A3D748192BF3CF060025824F /* GZIP */ = {
isa = XCSwiftPackageProductDependency;
package = A3D748102BF3CED10025824F /* XCRemoteSwiftPackageReference "GZIP" */;
productName = GZIP;
};
A3D7481D2BF3CF100025824F /* GZIP */ = {
isa = XCSwiftPackageProductDependency;
package = A3D748102BF3CED10025824F /* XCRemoteSwiftPackageReference "GZIP" */;
productName = GZIP;
};
A3D7481F2BF3CF160025824F /* GZIP */ = {
isa = XCSwiftPackageProductDependency;
package = A3D748102BF3CED10025824F /* XCRemoteSwiftPackageReference "GZIP" */;
productName = GZIP;
};
B445A6E324C0D1E3000BAD6D /* LDSwiftEventSource */ = {
isa = XCSwiftPackageProductDependency;
package = B445A6DE24C0D1CD000BAD6D /* XCRemoteSwiftPackageReference "swift-eventsource" */;
Expand Down
15 changes: 14 additions & 1 deletion LaunchDarkly/LaunchDarkly/Networking/DarklyService.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import Foundation
import LDSwiftEventSource
import OSLog
#if !os(watchOS)
import GZIP
#endif

typealias ServiceResponse = (data: Data?, urlResponse: URLResponse?, error: Error?, etag: String?)
typealias ServiceCompletionHandler = (ServiceResponse) -> Void
Expand Down Expand Up @@ -204,9 +207,19 @@ final class DarklyService: DarklyServiceProvider {
}

private func doPublish(url: URL, headers: [String: String], body: Data, completion: ServiceCompletionHandler?) {
var headers = headers

var httpBody = body
#if !os(watchOS)
if let compressedData = NSData(data: body).gzipped() {
httpBody = compressedData
headers.updateValue("gzip", forKey: "Content-Encoding")
}
#endif

var request = URLRequest(url: url, ldHeaders: headers, ldConfig: config)
request.httpMethod = URLRequest.HTTPMethods.post
request.httpBody = body
request.httpBody = httpBody

session.dataTask(with: request) { data, response, error in
completion?((data: data, urlResponse: response, error: error, etag: nil))
Expand Down
4 changes: 3 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ let package = Package(
.package(url: "https://github.com/AliSoftware/OHHTTPStubs.git", .exact("9.1.0")),
.package(url: "https://github.com/Quick/Quick.git", .exact("4.0.0")),
.package(url: "https://github.com/Quick/Nimble.git", .exact("9.2.1")),
.package(name: "LDSwiftEventSource", url: "https://github.com/LaunchDarkly/swift-eventsource.git", .exact("3.1.1"))
.package(name: "LDSwiftEventSource", url: "https://github.com/LaunchDarkly/swift-eventsource.git", .exact("3.1.1")),
.package(name: "GZIP", url: "https://github.com/nicklockwood/GZIP", .exact("1.3.2"))
],
targets: [
.target(
name: "LaunchDarkly",
dependencies: [
.product(name: "GZIP", package: "GZIP", condition: .when(platforms: [.iOS, .macOS, .tvOS])),
.product(name: "LDSwiftEventSource", package: "LDSwiftEventSource")
],
path: "LaunchDarkly/LaunchDarkly",
Expand Down
Loading