-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #325 from armanbilge/feature/dns-js
Make `Dns` cross-platform, add `reverse` methods
- Loading branch information
Showing
10 changed files
with
315 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* | ||
* Copyright 2018 Comcast Cable Communications Management, LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.comcast.ip4s | ||
|
||
import cats.effect.kernel.Async | ||
import cats.syntax.all._ | ||
|
||
import scala.scalajs.js | ||
import scala.scalajs.js.| | ||
import scala.scalajs.js.annotation.JSImport | ||
|
||
private[ip4s] trait DnsCompanionPlatform { | ||
implicit def forAsync[F[_]](implicit F: Async[F]): Dns[F] = new UnsealedDns[F] { | ||
def resolve(hostname: Hostname): F[IpAddress] = | ||
F.fromPromise(F.delay(dnsPromises.lookup(hostname.toString, LookupOptions(all = false)))) | ||
.flatMap { address => | ||
IpAddress | ||
.fromString(address.asInstanceOf[LookupResult].address) | ||
.liftTo[F](new RuntimeException("Node.js returned invalid IP address")) | ||
} | ||
.adaptError { | ||
case ex @ js.JavaScriptException(error: js.Error) if error.message.contains("ENOTFOUND") => | ||
new JavaScriptUnknownHostException(hostname.toString, ex) | ||
} | ||
|
||
def resolveOption(hostname: Hostname): F[Option[IpAddress]] = | ||
resolve(hostname).map(_.some).recover { case _: UnknownHostException => None } | ||
|
||
def resolveAll(hostname: Hostname): F[List[IpAddress]] = | ||
F.fromPromise(F.delay(dnsPromises.lookup(hostname.toString, LookupOptions(all = true)))) | ||
.flatMap { addresses => | ||
addresses | ||
.asInstanceOf[js.Array[LookupResult]] | ||
.toList | ||
.traverse { address => | ||
IpAddress | ||
.fromString(address.address) | ||
.liftTo[F](new RuntimeException("Node.js returned invalid IP address")) | ||
} | ||
} | ||
.recover { | ||
case js.JavaScriptException(error: js.Error) if error.message.contains("ENOTFOUND") => | ||
Nil | ||
} | ||
|
||
def reverse(address: IpAddress): F[Hostname] = | ||
reverseAllOrError(address).flatMap(_.headOption.liftTo(new UnknownHostException(address.toString))) | ||
|
||
def reverseOption(address: IpAddress): F[Option[Hostname]] = reverseAll(address).map(_.headOption) | ||
|
||
def reverseAll(address: IpAddress): F[List[Hostname]] = | ||
reverseAllOrError(address).recover { case _: UnknownHostException => Nil } | ||
|
||
private def reverseAllOrError(address: IpAddress): F[List[Hostname]] = | ||
F.fromPromise(F.delay(dnsPromises.reverse(address.toString))) | ||
.flatMap { hostnames => | ||
hostnames.toList.traverse { hostname => | ||
Hostname | ||
.fromString(hostname) | ||
.liftTo[F](new RuntimeException("Node.js returned invalid hostname")) | ||
} | ||
} | ||
.adaptError { | ||
case ex @ js.JavaScriptException(error: js.Error) if error.message.contains("ENOTFOUND") => | ||
new JavaScriptUnknownHostException(address.toString, ex) | ||
} | ||
|
||
def loopback: F[IpAddress] = resolve(Hostname.fromString("localhost").get) | ||
} | ||
} | ||
|
||
@js.native | ||
@JSImport("dns", "promises") | ||
private[ip4s] object dnsPromises extends js.Any { | ||
|
||
def lookup(hostname: String, options: LookupOptions): js.Promise[LookupResult | js.Array[LookupResult]] = js.native | ||
|
||
def reverse(ip: String): js.Promise[js.Array[String]] = js.native | ||
} | ||
|
||
private[ip4s] sealed trait LookupOptions extends js.Object | ||
object LookupOptions { | ||
def apply(all: Boolean): LookupOptions = js.Dynamic.literal(all = all).asInstanceOf[LookupOptions] | ||
} | ||
|
||
@js.native | ||
private[ip4s] sealed trait LookupResult extends js.Object { | ||
def address: String = js.native | ||
def family: Int = js.native | ||
} |
27 changes: 27 additions & 0 deletions
27
js/src/main/scala/com/comcast/ip4s/UnknownHostException.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* Copyright 2018 Comcast Cable Communications Management, LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.comcast.ip4s | ||
|
||
import java.io.IOException | ||
import scala.scalajs.js | ||
import scala.util.control.NoStackTrace | ||
|
||
class UnknownHostException(message: String = null, cause: Throwable = null) extends IOException(message, cause) | ||
|
||
private[ip4s] class JavaScriptUnknownHostException(message: String, cause: js.JavaScriptException) | ||
extends UnknownHostException(message, cause) | ||
with NoStackTrace |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* | ||
* Copyright 2018 Comcast Cable Communications Management, LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.comcast | ||
|
||
private[comcast] trait ip4splatform |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Copyright 2018 Comcast Cable Communications Management, LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.comcast.ip4s | ||
|
||
import cats.effect.kernel.Sync | ||
import cats.syntax.all._ | ||
|
||
import java.net.InetAddress | ||
|
||
private[ip4s] trait DnsCompanionPlatform { | ||
implicit def forSync[F[_]](implicit F: Sync[F]): Dns[F] = new UnsealedDns[F] { | ||
def resolve(hostname: Hostname): F[IpAddress] = | ||
F.blocking { | ||
val addr = InetAddress.getByName(hostname.toString) | ||
IpAddress.fromBytes(addr.getAddress).get | ||
} | ||
|
||
def resolveOption(hostname: Hostname): F[Option[IpAddress]] = | ||
resolve(hostname).map(_.some).recover { case _: UnknownHostException => None } | ||
|
||
def resolveAll(hostname: Hostname): F[List[IpAddress]] = | ||
F.blocking { | ||
try { | ||
val addrs = InetAddress.getAllByName(hostname.toString) | ||
addrs.toList.flatMap(addr => IpAddress.fromBytes(addr.getAddress)) | ||
} catch { | ||
case _: UnknownHostException => Nil | ||
} | ||
} | ||
|
||
def reverse(address: IpAddress): F[Hostname] = | ||
F.blocking { | ||
address.toInetAddress.getCanonicalHostName | ||
} flatMap { hn => | ||
// getCanonicalHostName returns the IP address as a string on failure | ||
Hostname.fromString(hn).liftTo[F](new UnknownHostException(address.toString)) | ||
} | ||
|
||
def reverseOption(address: IpAddress): F[Option[Hostname]] = | ||
reverse(address).map(_.some).recover { case _: UnknownHostException => None } | ||
|
||
def reverseAll(address: IpAddress): F[List[Hostname]] = | ||
reverseOption(address).map(_.toList) | ||
|
||
def loopback: F[IpAddress] = | ||
F.blocking(IpAddress.fromInetAddress(InetAddress.getByName(null))) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* Copyright 2018 Comcast Cable Communications Management, LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.comcast | ||
|
||
private[comcast] trait ip4splatform { | ||
type UnknownHostException = java.net.UnknownHostException | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,4 +16,4 @@ | |
|
||
package com.comcast | ||
|
||
package object ip4s | ||
package object ip4s extends ip4splatform |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.