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

Commit 12e46ef

Browse files
authored
Merge pull request #84 from exoego/net
Overhaul net module
2 parents d1f7877 + 19a2996 commit 12e46ef

File tree

10 files changed

+98
-263
lines changed

10 files changed

+98
-263
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,8 @@ The following core Node.js modules (v8.7.0+) have been implemented:
3131
| [events](https://nodejs.org/api/events.html) | :heavy_check_mark: |
3232
| [fs](https://nodejs.org/api/fs.html) | :heavy_check_mark: |
3333
| [http](https://nodejs.org/api/http.html) | :heavy_check_mark: |
34-
| [http2](https://nodejs.org/api/http2.html) | |
3534
| [https](https://nodejs.org/api/https.html) | :heavy_check_mark: |
36-
| [net](https://nodejs.org/api/net.html) | |
35+
| [net](https://nodejs.org/api/net.html) | :heavy_check_mark: |
3736
| [os](https://nodejs.org/api/os.html) | :heavy_check_mark: |
3837
| [path](https://nodejs.org/api/path.html) | :heavy_check_mark: |
3938
| [process](https://nodejs.org/api/process.html) | :heavy_check_mark: |
@@ -43,6 +42,7 @@ The following core Node.js modules (v8.7.0+) have been implemented:
4342
| [stream](https://nodejs.org/api/stream.html) | :heavy_check_mark: |
4443
| [string-decoder](https://nodejs.org/api/string_decoder.html) | :heavy_check_mark: |
4544
| [timers](https://nodejs.org/api/timers.html) | :heavy_check_mark: |
45+
| [tls](https://nodejs.org/api/tls.html) | |
4646
| [tty](https://nodejs.org/api/tty.html) | :heavy_check_mark: |
4747
| [url](https://nodejs.org/api/url.html) | :heavy_check_mark: |
4848
| [util](https://nodejs.org/api/util.html) | :heavy_check_mark: |

app/current/src/main/scala/io/scalajs/nodejs/http/ConnectionOptions.scala

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ class ConnectionOptions(
1414
var hints: js.UndefOr[Int] = js.undefined,
1515
var lookup: js.UndefOr[js.Function1[String, Any]] = js.undefined,
1616
var onread: js.UndefOr[OnreadObject] = js.undefined,
17-
// for new Socket(option)
17+
// for IPC connections
18+
var path: js.UndefOr[String],
19+
// for new Socket(option)
1820
var fd: js.UndefOr[Int] = js.undefined,
1921
var allowHalfOpen: js.UndefOr[Boolean] = js.undefined,
2022
var readable: js.UndefOr[Boolean] = js.undefined,
@@ -23,5 +25,5 @@ class ConnectionOptions(
2325

2426
class OnreadObject(
2527
var buffer: Uint8Array | js.Function0[Uint8Array],
26-
var calback: js.Function2[Int, Uint8Array, Boolean]
28+
var callback: js.Function2[Int, Uint8Array, Boolean]
2729
) extends js.Object

app/current/src/main/scala/io/scalajs/nodejs/http/package.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ package object http {
1414
/**
1515
* Http Extensions
1616
*/
17-
implicit class HttpExtensions(val http: Http) extends AnyVal {
17+
implicit final class HttpExtensions(val http: Http) extends AnyVal {
1818

1919
/**
2020
* @see [[Http.createServer()]]
@@ -63,7 +63,7 @@ package object http {
6363
/**
6464
* Server Events
6565
*/
66-
implicit class ServerEvents(val server: Server) extends AnyVal {
66+
implicit final class ServerEvents(val server: Server) extends AnyVal {
6767

6868
/**
6969
* Emitted each time a request with an http Expect: 100-continue is received. If this event isn't listened for,

app/current/src/main/scala/io/scalajs/nodejs/net/ListenerOptions.scala

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ package io.scalajs.nodejs.net
22

33
import scala.scalajs.js
44

5-
/**
6-
* Listener Options
7-
*/
8-
class ListenerOptions(var host: js.UndefOr[String] = js.undefined,
9-
var port: js.UndefOr[Int] = js.undefined,
10-
var path: js.UndefOr[String] = js.undefined,
11-
var backlog: js.UndefOr[Int] = js.undefined,
12-
var exclusive: js.UndefOr[Boolean] = js.undefined)
13-
extends js.Object
5+
class ListenerOptions(
6+
var host: js.UndefOr[String] = js.undefined,
7+
var port: js.UndefOr[Int] = js.undefined,
8+
var path: js.UndefOr[String] = js.undefined,
9+
var backlog: js.UndefOr[Int] = js.undefined,
10+
var exclusive: js.UndefOr[Boolean] = js.undefined,
11+
var readableAll: js.UndefOr[Boolean] = js.undefined,
12+
var writableAll: js.UndefOr[Boolean] = js.undefined,
13+
var ipv6Only: js.UndefOr[Boolean] = js.undefined
14+
) extends js.Object

app/current/src/main/scala/io/scalajs/nodejs/net/Net.scala

Lines changed: 15 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -24,52 +24,15 @@ trait Net extends IEventEmitter {
2424
* @example net.connect(options[, connectListener])
2525
*/
2626
def connect(options: ConnectOptions, callback: js.Function): Socket = js.native
27+
def connect(options: ConnectOptions): Socket = js.native
2728

28-
/**
29-
* A factory function, which returns a new net.Socket and automatically connects with the supplied options.
30-
* The options are passed to both the net.Socket constructor and the socket.connect method.
31-
* The connectListener parameter will be added as a listener for the 'connect' event once.
32-
* @example net.connect(options[, connectListener])
33-
*/
34-
def connect(options: ConnectOptions): Socket = js.native
35-
36-
/**
37-
* A factory function, which returns a new unix net.Socket and automatically connects to the supplied path.
38-
* The connectListener parameter will be added as a listener for the 'connect' event once.
39-
* @example net.connect(path[, connectListener])
40-
*/
4129
def connect(path: String, connectListener: js.Function): Socket = js.native
30+
def connect(path: String): Socket = js.native
4231

43-
/**
44-
* A factory function, which returns a new unix net.Socket and automatically connects to the supplied path.
45-
* The connectListener parameter will be added as a listener for the 'connect' event once.
46-
* @example net.connect(path[, connectListener])
47-
*/
48-
def connect(path: String): Socket = js.native
49-
50-
/**
51-
* A factory function, which returns a new net.Socket and automatically connects to the supplied port and host.
52-
* If host is omitted, 'localhost' will be assumed.
53-
* The connectListener parameter will be added as a listener for the 'connect' event once.
54-
* @example net.connect(port[, host][, connectListener])
55-
*/
5632
def connect(port: Int, host: String, connectListener: js.Function): Socket = js.native
57-
58-
/**
59-
* A factory function, which returns a new net.Socket and automatically connects to the supplied port and host.
60-
* If host is omitted, 'localhost' will be assumed.
61-
* The connectListener parameter will be added as a listener for the 'connect' event once.
62-
* @example net.connect(port[, host][, connectListener])
63-
*/
64-
def connect(port: Int, host: String): Socket = js.native
65-
66-
/**
67-
* A factory function, which returns a new net.Socket and automatically connects to the supplied port and host.
68-
* If host is omitted, 'localhost' will be assumed.
69-
* The connectListener parameter will be added as a listener for the 'connect' event once.
70-
* @example net.connect(port[, host][, connectListener])
71-
*/
72-
def connect(port: Int): Socket = js.native
33+
def connect(port: Int, connectListener: js.Function): Socket = js.native
34+
def connect(port: Int, host: String): Socket = js.native
35+
def connect(port: Int): Socket = js.native
7336

7437
/**
7538
* A factory function, which returns a new net.Socket and automatically connects with the supplied options.
@@ -78,58 +41,23 @@ trait Net extends IEventEmitter {
7841
* @example net.createConnection(options[, connectListener])
7942
*/
8043
def createConnection(options: ConnectOptions, connectListener: js.Function = js.native): Socket = js.native
44+
def createConnection(path: String, connectListener: js.Function): Socket = js.native
45+
def createConnection(path: String): Socket = js.native
46+
def createConnection(port: Int, host: String, connectListener: js.Function): Socket = js.native
47+
def createConnection(port: Int, connectListener: js.Function): Socket = js.native
48+
def createConnection(port: Int, host: String): Socket = js.native
49+
def createConnection(port: Int): Socket = js.native
8150

82-
/**
83-
* @example net.createConnection(path[, connectListener])
84-
*/
85-
def createConnection(path: String, connectListener: js.Function): Socket = js.native
86-
87-
/**
88-
* @example net.createConnection(path[, connectListener])
89-
*/
90-
def createConnection(path: String): Socket = js.native
91-
92-
/**
93-
* @example net.createConnection(port[, host][, connectListener])
94-
*/
95-
def createConnection(port: Int, host: String, connectListener: js.Function): Socket = js.native
96-
97-
/**
98-
* @example net.createConnection(port[, host][, connectListener])
99-
*/
100-
def createConnection(port: Int, host: String): Socket = js.native
101-
102-
/**
103-
* @example net.createConnection(port[, host][, connectListener])
104-
*/
105-
def createConnection(port: Int): Socket = js.native
106-
107-
/**
108-
* @example net.createServer([options][, connectionListener])
109-
*/
110-
def createServer(options: ServerOptions, connectionListener: js.Function): Server = js.native
111-
112-
/**
113-
* @example net.createServer([options][, connectionListener])
114-
*/
115-
def createServer(options: ServerOptions): Server = js.native
116-
117-
/**
118-
* @example net.createServer([options][, connectionListener])
119-
*/
120-
def createServer(connectionListener: js.Function): Server = js.native
121-
122-
/**
123-
* @example net.createServer([options][, connectionListener])
124-
*/
125-
def createServer(): Server = js.native
51+
def createServer(options: ServerOptions, connectionListener: js.Function = js.native): Server = js.native
52+
def createServer(connectionListener: js.Function): Server = js.native
53+
def createServer(): Server = js.native
12654

12755
/**
12856
* Tests if input is an IP address. Returns 0 for invalid strings, returns 4 for IP version 4 addresses,
12957
* and returns 6 for IP version 6 addresses.
13058
* @example net.isIP(input)
13159
*/
132-
def isIP(input: String): Boolean = js.native
60+
def isIP(input: String): Int = js.native
13361

13462
/**
13563
* Returns true if input is a version 4 IP address, otherwise returns false.

app/current/src/main/scala/io/scalajs/nodejs/net/Server.scala

Lines changed: 19 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@ import io.scalajs.nodejs.events.IEventEmitter
55

66
import scala.scalajs.js
77
import scala.scalajs.js.annotation.JSImport
8+
import scala.scalajs.js.|
89

910
/**
1011
* net.Server - This class is used to create a TCP or local server.
1112
* @see https://nodejs.org/api/net.html#net_class_net_server
1213
*/
1314
@js.native
1415
@JSImport("net", "Server")
15-
class Server() extends IEventEmitter {
16+
class Server(options: ServerOptions = js.native) extends IEventEmitter {
1617

17-
/////////////////////////////////////////////////////////////////////////////////
18-
// Properties
19-
/////////////////////////////////////////////////////////////////////////////////
18+
def this(options: ServerOptions, connectionListener: js.Function) = this()
19+
def this(connectionListener: js.Function) = this()
2020

2121
/**
2222
* Returns the current number of concurrent connections on the server.
@@ -37,17 +37,13 @@ class Server() extends IEventEmitter {
3737
*/
3838
var maxConnections: js.UndefOr[Int] = js.native
3939

40-
/////////////////////////////////////////////////////////////////////////////////
41-
// Methods
42-
/////////////////////////////////////////////////////////////////////////////////
43-
4440
/**
4541
* Returns the bound address, the address family name and port of the server as reported by the operating system.
4642
* Useful to find which port was assigned when giving getting an OS-assigned address. Returns an object with
4743
* three properties, e.g. { port: 12346, family: 'IPv4', address: '127.0.0.1' }
4844
* @example server.address()
4945
*/
50-
def address(): Address = js.native
46+
def address(): Address | String = js.native
5147

5248
/**
5349
* Stops the server from accepting new connections and keeps existing connections. This function is asynchronous,
@@ -56,7 +52,7 @@ class Server() extends IEventEmitter {
5652
* its only argument if the server was not open when it was closed.
5753
* @example server.close([callback])
5854
*/
59-
def close(callback: js.Function = js.native): Unit = js.native
55+
def close(callback: js.Function1[io.scalajs.nodejs.SystemError, Any] = js.native): Unit = js.native
6056

6157
/**
6258
* Asynchronously get the number of concurrent connections on the server. Works when sockets were sent to forks.
@@ -72,34 +68,21 @@ class Server() extends IEventEmitter {
7268
* @example server.listen(options[, callback])
7369
*/
7470
def listen(options: ListenerOptions, callback: js.Function): Unit = js.native
71+
def listen(options: ListenerOptions): Unit = js.native
7572

76-
/**
77-
* The port, host, and backlog properties of options, as well as the optional callback function, behave as
78-
* they do on a call to server.listen(port[, hostname][, backlog][, callback]). Alternatively, the path
79-
* option can be used to specify a UNIX socket.
80-
* @example server.listen(options[, callback])
81-
*/
82-
def listen(options: ListenerOptions): Unit = js.native
73+
def listen(handle: Handle, backlog: Int, callback: js.Function): Unit = js.native
74+
def listen(handle: Handle, callback: js.Function): Unit = js.native
75+
def listen(handle: Handle, backlog: Int): Unit = js.native
76+
def listen(handle: Handle): Unit = js.native
8377

84-
/**
85-
* @example server.listen(port[, hostname][, backlog][, callback])
86-
*/
8778
def listen(port: Int, hostname: String, backlog: Int, callback: js.Function): Unit = js.native
88-
89-
/**
90-
* @example server.listen(port[, hostname][, backlog][, callback])
91-
*/
92-
def listen(port: Int, hostname: String, backlog: Int): Unit = js.native
93-
94-
/**
95-
* @example server.listen(port[, hostname][, backlog][, callback])
96-
*/
97-
def listen(port: Int, hostname: String): Unit = js.native
98-
99-
/**
100-
* @example server.listen(port[, hostname][, backlog][, callback])
101-
*/
102-
def listen(port: Int): Unit = js.native
79+
def listen(port: Int, hostname: String, backlog: Int): Unit = js.native
80+
def listen(port: Int, hostname: String, callback: js.Function): Unit = js.native
81+
def listen(port: Int, hostname: String): Unit = js.native
82+
def listen(port: Int, callback: js.Function): Unit = js.native
83+
def listen(port: Int): Unit = js.native
84+
def listen(callback: js.Function): Unit = js.native
85+
def listen(): Unit = js.native
10386

10487
/**
10588
* Opposite of unref, calling ref on a previously unrefd server will not let the program exit if it's
@@ -116,23 +99,6 @@ class Server() extends IEventEmitter {
11699
* @example server.unref()
117100
*/
118101
def unref(): this.type = js.native
119-
120-
/**
121-
* Sets the timeout value for sockets, and emits a 'timeout' event on the Server object, passing the socket as
122-
* an argument, if a timeout occurs.
123-
*
124-
* If there is a 'timeout' event listener on the Server object, then it will be called with the timed-out socket
125-
* as an argument.
126-
*
127-
* By default, the Server's timeout value is 2 minutes, and sockets are destroyed automatically if they time out.
128-
* However, if you assign a callback to the Server's 'timeout' event, then you are responsible for handling socket
129-
* timeouts.
130-
* @param msecs the timeout in milliseconds
131-
* @param callback the callback
132-
* @return [[Server]]
133-
*/
134-
def setTimeout(msecs: Double, callback: js.Function): this.type = js.native
135-
136102
}
137103

138104
/**
@@ -144,7 +110,7 @@ object Server extends {
144110
* Server Events
145111
* @param server the given [[Server]]
146112
*/
147-
implicit class ServerEvents(val server: Server) extends AnyVal {
113+
implicit final class ServerEvents(val server: Server) extends AnyVal {
148114

149115
/**
150116
* Emitted when the server closes. Note that if connections exist, this event is not emitted until all
@@ -177,10 +143,3 @@ object Server extends {
177143
}
178144

179145
}
180-
181-
/**
182-
* Server Options
183-
*/
184-
class ServerOptions(val allowHalfOpen: js.UndefOr[Boolean] = js.undefined,
185-
val pauseOnConnect: js.UndefOr[Boolean] = js.undefined)
186-
extends js.Object
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package io.scalajs.nodejs.net
2+
3+
import scala.scalajs.js
4+
5+
class ServerOptions(val allowHalfOpen: js.UndefOr[Boolean] = js.undefined,
6+
val pauseOnConnect: js.UndefOr[Boolean] = js.undefined)
7+
extends js.Object

0 commit comments

Comments
 (0)