-
Notifications
You must be signed in to change notification settings - Fork 24
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
Make Dns
cross-platform, add reverse
methods
#325
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wasn't sure about this since its a slight abuse, but I don't see why
UnknownHostException
couldn't apply here as well.For precedence, Node.js returns the same
ENOTFOUND
error for bothresolve
andreverse
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Btw, JVM instead of returning an error for
reverse
just returns the IP address as aString
...