Skip to content

Commit c2a728c

Browse files
authored
Merge pull request #64 from http4s/topic/reach-for-async
Deprecate `Priority`
2 parents b8e66c3 + 826ae87 commit c2a728c

File tree

5 files changed

+64
-40
lines changed

5 files changed

+64
-40
lines changed

crypto/js/src/main/scala/org/http4s/crypto/CryptoPlatform.scala

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@ import cats.effect.kernel.Async
2020
import cats.effect.kernel.Sync
2121

2222
private[crypto] trait CryptoCompanionPlatform {
23-
implicit def forAsyncOrSync[F[_]](implicit F: Priority[Async[F], Sync[F]]): Crypto[F] =
23+
24+
@deprecated("Preserved for bincompat", "0.2.3")
25+
def forAsyncOrSync[F[_]](implicit F: Priority[Async[F], Sync[F]]): Crypto[F] =
26+
forSync(F.join)
27+
28+
implicit def forSync[F[_]](implicit F: Sync[F]): Crypto[F] =
2429
new UnsealedCrypto[F] {
2530
override def hash: Hash[F] = Hash[F]
2631
override def hmac: Hmac[F] = Hmac[F]

crypto/js/src/main/scala/org/http4s/crypto/HashPlatform.scala

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,33 @@
1616

1717
package org.http4s.crypto
1818

19+
import cats.ApplicativeThrow
1920
import cats.MonadThrow
2021
import cats.effect.kernel.Async
2122
import cats.syntax.all._
2223
import scodec.bits.ByteVector
2324

2425
private[crypto] trait HashCompanionPlatform {
25-
implicit def forAsyncOrMonadThrow[F[_]](
26-
implicit F: Priority[Async[F], MonadThrow[F]]): Hash[F] =
26+
@deprecated("Preserved for bincompat", "0.2.3")
27+
def forAsyncOrMonadThrow[F[_]](implicit F: Priority[Async[F], MonadThrow[F]]): Hash[F] =
28+
forApplicativeThrow(F.join)
29+
30+
implicit def forApplicativeThrow[F[_]](implicit F: ApplicativeThrow[F]): Hash[F] =
2731
if (facade.isNodeJSRuntime)
2832
new UnsealedHash[F] {
2933
override def digest(algorithm: HashAlgorithm, data: ByteVector): F[ByteVector] =
30-
F.join[MonadThrow[F]].catchNonFatal {
34+
F.catchNonFatal {
3135
val hash = facade.node.crypto.createHash(algorithm.toStringNodeJS)
3236
hash.update(data.toUint8Array)
3337
ByteVector.view(hash.digest())
3438
}
3539
}
3640
else
37-
F.getPreferred
38-
.map { implicit F: Async[F] =>
41+
Some(F)
42+
.collect { case f: Async[F] => f }
43+
.fold(
44+
throw new UnsupportedOperationException("Hash[F] on browsers requires Async[F]")
45+
) { implicit F: Async[F] =>
3946
new UnsealedHash[F] {
4047
import facade.browser._
4148
override def digest(algorithm: HashAlgorithm, data: ByteVector): F[ByteVector] =
@@ -44,7 +51,5 @@ private[crypto] trait HashCompanionPlatform {
4451
.map(ByteVector.view)
4552
}
4653
}
47-
.getOrElse(throw new UnsupportedOperationException(
48-
"Hash[F] on browsers requires Async[F]"))
4954

5055
}

crypto/js/src/main/scala/org/http4s/crypto/HmacKeyGenPlatform.scala

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,36 +24,45 @@ import scodec.bits.ByteVector
2424
import scala.scalajs.js
2525

2626
private[crypto] trait HmacKeyGenCompanionPlatform {
27-
implicit def forAsyncOrSync[F[_]](implicit F0: Priority[Async[F], Sync[F]]): HmacKeyGen[F] =
27+
@deprecated("Preserved for bincompat", "0.2.3")
28+
def forAsyncOrSync[F[_]](implicit F0: Priority[Async[F], Sync[F]]): HmacKeyGen[F] =
29+
forSync(F0.join)
30+
31+
implicit def forSync[F[_]](implicit F: Sync[F]): HmacKeyGen[F] =
2832
if (facade.isNodeJSRuntime)
2933
new UnsealedHmacKeyGen[F] {
3034
import facade.node._
3135

3236
override def generateKey[A <: HmacAlgorithm](algorithm: A): F[SecretKey[A]] =
33-
F0.fold { F =>
34-
F.async_[SecretKey[A]] { cb =>
35-
crypto.generateKey(
36-
"hmac",
37-
GenerateKeyOptions(algorithm.minimumKeyLength),
38-
(err, key) =>
39-
cb(
40-
Option(err)
41-
.map(js.JavaScriptException)
42-
.toLeft(SecretKeySpec(ByteVector.view(key.`export`()), algorithm)))
43-
)
44-
}
45-
} { F =>
46-
F.delay {
47-
val key =
48-
crypto.generateKeySync("hmac", GenerateKeyOptions(algorithm.minimumKeyLength))
49-
SecretKeySpec(ByteVector.view(key.`export`()), algorithm)
37+
Some(F)
38+
.collect { case f: Async[F] => f }
39+
.fold {
40+
F.delay[SecretKey[A]] {
41+
val key =
42+
crypto.generateKeySync("hmac", GenerateKeyOptions(algorithm.minimumKeyLength))
43+
SecretKeySpec(ByteVector.view(key.`export`()), algorithm)
44+
}
45+
} { F =>
46+
F.async_[SecretKey[A]] { cb =>
47+
crypto.generateKey(
48+
"hmac",
49+
GenerateKeyOptions(algorithm.minimumKeyLength),
50+
(err, key) =>
51+
cb(
52+
Option(err)
53+
.map(js.JavaScriptException)
54+
.toLeft(SecretKeySpec(ByteVector.view(key.`export`()), algorithm)))
55+
)
56+
}
5057
}
51-
}
5258

5359
}
5460
else
55-
F0.getPreferred
56-
.map { implicit F: Async[F] =>
61+
Some(F)
62+
.collect { case f: Async[F] => f }
63+
.fold(
64+
throw new UnsupportedOperationException("HmacKeyGen[F] on browsers requires Async[F]")
65+
) { implicit F =>
5766
new UnsealedHmacKeyGen[F] {
5867
import facade.browser._
5968
override def generateKey[A <: HmacAlgorithm](algorithm: A): F[SecretKey[A]] =
@@ -70,7 +79,5 @@ private[crypto] trait HmacKeyGenCompanionPlatform {
7079
} yield SecretKeySpec(ByteVector.view(exported), algorithm)
7180
}
7281
}
73-
.getOrElse(throw new UnsupportedOperationException(
74-
"HmacKeyGen[F] on browsers requires Async[F]"))
7582

7683
}

crypto/js/src/main/scala/org/http4s/crypto/HmacPlatform.scala

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,16 @@ import scala.scalajs.js
2626
private[crypto] trait HmacPlatform[F[_]]
2727

2828
private[crypto] trait HmacCompanionPlatform {
29-
implicit def forAsyncOrApplicativeThrow[F[_]](
29+
30+
@deprecated("Preserved for bincompat", "0.2.3")
31+
def forAsyncOrApplicativeThrow[F[_]](
3032
implicit F0: Priority[Async[F], ApplicativeThrow[F]]): Hmac[F] =
33+
forApplicativeThrow(F0.join)
34+
35+
implicit def forApplicativeThrow[F[_]](implicit F: ApplicativeThrow[F]): Hmac[F] =
3136
if (facade.isNodeJSRuntime)
3237
new UnsealedHmac[F] {
3338
import facade.node._
34-
implicit val F: ApplicativeThrow[F] = F0.join[ApplicativeThrow[F]]
3539

3640
override def digest(key: SecretKey[HmacAlgorithm], data: ByteVector): F[ByteVector] =
3741
key match {
@@ -51,8 +55,11 @@ private[crypto] trait HmacCompanionPlatform {
5155

5256
}
5357
else
54-
F0.getPreferred
55-
.map { implicit F: Async[F] =>
58+
Some(F)
59+
.collect { case f: Async[F] => f }
60+
.fold(
61+
throw new UnsupportedOperationException("Hmac[F] on browsers requires Async[F]")
62+
) { implicit F: Async[F] =>
5663
new UnsealedHmac[F] {
5764
import facade.browser._
5865
override def digest(
@@ -82,7 +89,4 @@ private[crypto] trait HmacCompanionPlatform {
8289
F.pure(SecretKeySpec(key, algorithm))
8390
}
8491
}
85-
.getOrElse(throw new UnsupportedOperationException(
86-
"Hmac[F] on browsers requires Async[F]"))
87-
8892
}

crypto/js/src/main/scala/org/http4s/crypto/Priority.scala

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ package org.http4s.crypto
2626
* This type can be useful for problems where multiple algorithms can be used, depending on the
2727
* type classes available.
2828
*/
29+
@deprecated("Unneeded when P and F belong to the same hiearchy", "0.2.3")
2930
private[http4s] sealed trait Priority[+P, +F] {
3031

3132
import Priority.{Fallback, Preferred}
@@ -70,11 +71,13 @@ private[http4s] object Priority extends FindPreferred {
7071
}
7172

7273
private[crypto] trait FindPreferred extends FindFallback {
73-
implicit def preferred[P](implicit ev: P): Priority[P, Nothing] =
74+
@deprecated("Priority is deprecated", "0.2.3")
75+
def preferred[P](implicit ev: P): Priority[P, Nothing] =
7476
Priority.Preferred(ev)
7577
}
7678

7779
private[crypto] trait FindFallback {
78-
implicit def fallback[F](implicit ev: F): Priority[Nothing, F] =
80+
@deprecated("Priority is deprecated", "0.2.3")
81+
def fallback[F](implicit ev: F): Priority[Nothing, F] =
7982
Priority.Fallback(ev)
8083
}

0 commit comments

Comments
 (0)