Skip to content

Commit

Permalink
feat: Support compression in non Apple platforms
Browse files Browse the repository at this point in the history
  • Loading branch information
amosavian committed May 1, 2024
1 parent d37087e commit 8f03e99
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ let package = Package(
.package(url: "https://github.com/apple/swift-crypto.git", .upToNextMajor(from: "3.3.0")),
.package(url: "https://github.com/apple/swift-certificates", .upToNextMajor(from: "1.2.0")),
.package(url: "https://github.com/krzyzanowskim/CryptoSwift.git", .upToNextMajor(from: "1.8.2")),
.package(url: "https://github.com/tsolomko/SWCompression.git", .upToNextMajor(from: "4.8.6")),
],
targets: [
.target(
Expand All @@ -41,6 +42,7 @@ let package = Package(
.product(name: "Crypto", package: "swift-crypto", condition: .when(platforms: .nonDarwin)),
.product(name: "_CryptoExtras", package: "swift-crypto", condition: .when(platforms: .nonDarwin)),
.product(name: "CryptoSwift", package: "CryptoSwift", condition: .when(platforms: .nonDarwin)),
.product(name: "SWCompression", package: "SWCompression", condition: .when(platforms: .nonDarwin)),
],
resources: [
.process("PrivacyInfo.xcprivacy"),
Expand Down
4 changes: 3 additions & 1 deletion Sources/JWSETKit/Cryptography/Algorithms/Compression.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ extension JSONWebCompressionAlgorithm {
.deflate: AppleCompressor<DeflateCompressionCodec>.self,
]
#else
private static let compressors: PthreadReadWriteLockedValue<[Self: any JSONWebCompressor.Type]> = [:]
private static let compressors: PthreadReadWriteLockedValue<[Self: any JSONWebCompressor.Type]> = [
.deflate: Compressor<DeflateCompressionCodec>.self,
]
#endif

/// Returns provided compressor for this algorithm.
Expand Down
46 changes: 46 additions & 0 deletions Sources/JWSETKit/Cryptography/Compression/Compressor.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//
// File.swift
//
//
// Created by Amir Abbas Mousavian on 5/1/24.
//

import Foundation
#if canImport(SWCompression)
import SWCompression

extension JSONWebCompressionAlgorithm {
var swCompressor: any CompressionAlgorithm.Type {
get throws {
switch self {
case .deflate:
return Deflate.self
default:
throw JSONWebKeyError.unknownAlgorithm
}
}
}

var swDecompressor: any DecompressionAlgorithm.Type {
get throws {
switch self {
case .deflate:
return Deflate.self
default:
throw JSONWebKeyError.unknownAlgorithm
}
}
}
}

/// Compressor contain compress and decompress implementation using `Compression` framework.
public struct Compressor<Codec>: JSONWebCompressor, Sendable where Codec: CompressionCodec {
public static func compress<D>(_ data: D) throws -> Data where D: DataProtocol {
try Codec.algorithm.swCompressor.compress(data: Data(data))
}

public static func decompress<D>(_ data: D) throws -> Data where D: DataProtocol {
try Codec.algorithm.swDecompressor.decompress(data: Data(data))
}
}
#endif
6 changes: 5 additions & 1 deletion Sources/JWSETKit/Extensions/LockValue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,11 @@ public final class PthreadReadWriteLock: Locking, @unchecked Sendable {
result = pthread_rwlock_wrlock(lock)
}
if result != 0 {
throw POSIXError(POSIXError.Code(rawValue: result) ?? .ELAST)
#if canImport(Darwin)
throw POSIXError(.init(rawValue: result) ?? .ELAST)
#else
throw NSError(domain: NSPOSIXErrorDomain, code: Int(result))
#endif
}
}

Expand Down

0 comments on commit 8f03e99

Please sign in to comment.