Skip to content
This repository was archived by the owner on Jul 30, 2024. It is now read-only.

Commit c952308

Browse files
authored
Merge pull request #76 from exoego/dgram
Overhaul dgram module
2 parents aecdc8b + 69afcf2 commit c952308

File tree

6 files changed

+137
-43
lines changed

6 files changed

+137
-43
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ The following core Node.js modules (v8.7.0+) have been implemented:
2626
| [cluster](https://nodejs.org/api/cluster.html) | :heavy_check_mark: |
2727
| [console](https://nodejs.org/api/console.html) | :heavy_check_mark: |
2828
| [crypto](https://nodejs.org/api/crypto.html) | :heavy_check_mark: |
29-
| [dgram](https://nodejs.org/api/dgram.html) | |
29+
| [dgram](https://nodejs.org/api/dgram.html) | :heavy_check_mark: |
3030
| [dns](https://nodejs.org/api/dns.html) | :heavy_check_mark: |
3131
| [events](https://nodejs.org/api/events.html) | :heavy_check_mark: |
3232
| [fs](https://nodejs.org/api/fs.html) | :heavy_check_mark: |
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package io.scalajs.nodejs.dgram
2+
3+
import scala.scalajs.js
4+
import scala.scalajs.js.annotation.JSImport
5+
6+
@js.native
7+
trait Dgram extends js.Object {
8+
def createSocket(options: SocketOptions, callback: js.Function = js.native): Socket = js.native
9+
def createSocket(`type`: String, callback: js.Function): Socket = js.native
10+
def createSocket(`type`: String): Socket = js.native
11+
}
12+
13+
class SocketOptions(
14+
var `type`: String,
15+
var reuseAddr: js.UndefOr[Boolean] = js.undefined,
16+
var ipv6Only: js.UndefOr[Boolean] = js.undefined,
17+
var recvBufferSize: js.UndefOr[Int] = js.undefined,
18+
var sendBufferSize: js.UndefOr[Int] = js.undefined,
19+
var lookup: js.UndefOr[js.Function1[String, Any]] = js.undefined
20+
) extends js.Object
21+
22+
@js.native
23+
@JSImport("dgram", JSImport.Namespace)
24+
object Dgram extends Dgram
Lines changed: 66 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
package io.scalajs.nodejs
22
package dgram
33

4-
import io.scalajs.RawOptions
5-
import io.scalajs.nodejs.buffer.Buffer
4+
import com.thoughtworks.enableIf
65
import io.scalajs.nodejs.events.IEventEmitter
76
import io.scalajs.nodejs.net.Address
87

98
import scala.scalajs.js
109
import scala.scalajs.js.annotation.JSImport
11-
import scala.scalajs.js.|
1210

1311
/**
1412
* The dgram.Socket object is an EventEmitter that encapsulates the datagram functionality.
@@ -18,7 +16,7 @@ import scala.scalajs.js.|
1816
*/
1917
@js.native
2018
@JSImport("dgram", "Socket")
21-
class Socket extends IEventEmitter {
19+
class Socket private[this] () extends IEventEmitter {
2220

2321
/**
2422
* Tells the kernel to join a multicast group at the given multicastAddress and multicastInterface using the
@@ -48,7 +46,13 @@ class Socket extends IEventEmitter {
4846
* @param callback the callback
4947
* @example bind([port][, address][, callback])
5048
*/
51-
def bind(port: Int, address: String, callback: js.Function): Unit = js.native
49+
def bind(port: Int, address: String, callback: js.Function0[Any]): Unit = js.native
50+
def bind(port: Int, address: String): Unit = js.native
51+
def bind(port: Int, callback: js.Function0[Any]): Unit = js.native
52+
def bind(address: String, callback: js.Function0[Any]): Unit = js.native
53+
def bind(port: Int): Unit = js.native
54+
def bind(callback: js.Function0[Any]): Unit = js.native
55+
def bind(address: String): Unit = js.native
5256

5357
/**
5458
* For UDP sockets, causes the dgram.Socket to listen for datagram messages on a named port and optional address.
@@ -59,41 +63,44 @@ class Socket extends IEventEmitter {
5963
* @param callback the callback
6064
* @example bind(options[, callback])
6165
*/
62-
def bind(options: RawOptions, callback: js.Function): Unit = js.native
66+
def bind(options: BindOptions, callback: js.Function = js.native): Unit = js.native
6367

64-
/**
65-
* For UDP sockets, causes the dgram.Socket to listen for datagram messages on a named port and optional address.
66-
* If port is not specified or is 0, the operating system will attempt to bind to a random port. If address is not
67-
* specified, the operating system will attempt to listen on all addresses. Once binding is complete, a 'listening'
68-
* event is emitted and the optional callback function is called.
69-
* @param options the optional settings
70-
* @example bind(options[, callback])
71-
*/
72-
def bind(options: RawOptions): Unit = js.native
68+
def close(callback: js.Function = js.native): Unit = js.native
7369

74-
/**
75-
* Broadcasts a datagram on the socket. The destination port and address must be specified.
76-
*
77-
* The msg argument contains the message to be sent. Depending on its type, different behavior can apply.
78-
* If msg is a Buffer, the offset and length specify the offset within the Buffer where the message begins
79-
* and the number of bytes in the message, respectively. If msg is a String, then it is automatically converted
80-
* to a Buffer with 'utf8' encoding. With messages that contain multi-byte characters, offset and length will
81-
* be calculated with respect to byte length and not the character position. If msg is an array, offset and
82-
* length must not be specified.
83-
* @param msg Message to be sent (<Buffer> | <String> | <Array>)
84-
* @param offset Optional. Offset in the buffer where the message starts.
85-
* @param length Optional. Number of bytes in the message.
86-
* @param port Destination port.
87-
* @param address Destination hostname or IP address.
88-
* @param callback Optional. Called when the message has been sent.
89-
* @example send(msg, [offset, length,] port, address[, callback])
90-
*/
91-
def send(msg: Buffer | String | js.Array[Buffer] | js.Array[String],
92-
offset: Int = js.native,
93-
length: Int = js.native,
94-
port: Int,
95-
address: String,
96-
callback: js.Function): Unit = js.native
70+
@enableIf(io.scalajs.nodejs.CompilerSwitches.gteNodeJs12)
71+
def connect(port: Int, address: String = js.native, callback: js.Function0[Any] = js.native): Unit = js.native
72+
73+
@enableIf(io.scalajs.nodejs.CompilerSwitches.gteNodeJs12)
74+
def disconnect(): Unit = js.native
75+
76+
def dropMembership(multicastAddress: String, multicastInterface: String = js.native): Unit = js.native
77+
78+
def getRecvBufferSize(): Int = js.native
79+
def getSendBufferSize(): Int = js.native
80+
81+
def ref(): this.type = js.native
82+
83+
@enableIf(io.scalajs.nodejs.CompilerSwitches.gteNodeJs12)
84+
def remoteAddress(): RemoteAddress = js.native
85+
86+
def send(msg: BufferMessage, offset: Int, length: Int, port: Int, address: String, callback: js.Function): Unit =
87+
js.native
88+
def send(msg: BufferMessage, offset: Int, length: Int, port: Int, address: String): Unit = js.native
89+
def send(msg: BufferMessage, offset: Int, length: Int, port: Int, callback: js.Function): Unit = js.native
90+
def send(msg: BufferMessage, offset: Int, length: Int, address: String, callback: js.Function): Unit = js.native
91+
def send(msg: BufferMessage, offset: Int, length: Int, port: Int): Unit = js.native
92+
def send(msg: BufferMessage, offset: Int, length: Int, address: String): Unit = js.native
93+
def send(msg: BufferMessage, offset: Int, length: Int, callback: js.Function): Unit = js.native
94+
def send(msg: BufferMessage, offset: Int, length: Int): Unit = js.native
95+
96+
def send(msg: Message, port: Int, address: String, callback: js.Function): Unit = js.native
97+
def send(msg: Message, address: String, callback: js.Function): Unit = js.native
98+
def send(msg: Message, port: Int, callback: js.Function): Unit = js.native
99+
def send(msg: Message, port: Int, address: String): Unit = js.native
100+
def send(msg: Message, callback: js.Function): Unit = js.native
101+
def send(msg: Message, port: Int): Unit = js.native
102+
def send(msg: Message, address: String): Unit = js.native
103+
def send(msg: Message): Unit = js.native
97104

98105
/**
99106
* Sets or clears the SO_BROADCAST socket option. When set to true, UDP packets may be sent to a
@@ -102,4 +109,25 @@ class Socket extends IEventEmitter {
102109
*/
103110
def setBroadcast(flag: Boolean): Unit = js.native
104111

112+
def setMulticastInterface(multicastInterface: String): Unit = js.native
113+
def setMulticastLoopback(flag: Boolean): Unit = js.native
114+
def setMulticastTTL(ttl: Int): Unit = js.native
115+
def setRecvBufferSize(size: Int): Unit = js.native
116+
def setSendBufferSize(size: Int): Unit = js.native
117+
def setTTL(ttl: Int): Unit = js.native
118+
def unref(): this.type = js.native
119+
}
120+
121+
class BindOptions(
122+
var port: js.UndefOr[Int] = js.undefined,
123+
var address: js.UndefOr[String] = js.undefined,
124+
var exclusive: js.UndefOr[Boolean] = js.undefined,
125+
var fd: js.UndefOr[Int] = js.undefined
126+
) extends js.Object {}
127+
128+
@js.native
129+
trait RemoteAddress extends js.Object {
130+
var address: String = js.native
131+
var family: String = js.native
132+
var port: Int = js.native
105133
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package io.scalajs.nodejs
2+
3+
import scala.scalajs.js
4+
import scala.scalajs.js.typedarray.Uint8Array
5+
import scala.scalajs.js.|
6+
7+
package object dgram {
8+
type StringMessage = String | js.Array[String]
9+
type Message = BufferMessage | StringMessage
10+
type BufferMessage = Uint8Array | js.Array[Uint8Array]
11+
}

core/src/main/scala/io/scalajs/util/PromiseHelper.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ object PromiseHelper {
261261
case Success(_) =>
262262
val elapsedTime = System.currentTimeMillis - startTime
263263
println(f"$action took $elapsedTime msecs")
264-
case Failure(e) =>
264+
case Failure(_) =>
265265
val elapsedTime = System.currentTimeMillis - startTime
266266
println(f"$action took $elapsedTime msecs")
267267
}

project/MySettings.scala

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,46 @@ import sbtrelease.ReleaseStateTransformations._
66

77
object MySettings {
88

9+
private val lintSettings = Def.setting({
10+
val isScala212 = scalaVersion.value.startsWith("2.12")
11+
val lints = (Seq(
12+
"adapted-args",
13+
"nullary-unit",
14+
"inaccessible",
15+
"nullary-override",
16+
"infer-any",
17+
"missing-interpolator",
18+
"doc-detached",
19+
"private-shadow",
20+
"type-parameter-shadow",
21+
"poly-implicit-overload",
22+
"option-implicit",
23+
"delayedinit-select",
24+
"package-object-classes",
25+
"stars-align",
26+
"constant"
27+
) ++ Seq(
28+
"nonlocal-return",
29+
"implicit-not-found",
30+
"serial",
31+
"valpattern",
32+
"eta-zero",
33+
"eta-sam",
34+
"deprecation"
35+
).filter(_ => !isScala212)).map(s => s"-Xlint:${s}")
36+
// no privates to allow private constructor
37+
val unused = Seq("imports", "implicits", "locals", "patvars").filter(_ => !isScala212).map(s => s"-Wunused:${s}")
38+
lints ++ unused
39+
})
40+
941
lazy val commonSettings = Seq(
1042
autoCompilerPlugins := true,
1143
scalacOptions ++= Seq(
1244
"-deprecation",
1345
"-unchecked",
1446
"-feature",
15-
"-language:implicitConversions",
16-
"-Xlint"
17-
),
47+
"-language:implicitConversions"
48+
) ++ lintSettings.value,
1849
scalacOptions in Compile in compile ++= Seq(
1950
"-Xfatal-warnings"
2051
),

0 commit comments

Comments
 (0)