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

Overhaul net module #84

Merged
merged 3 commits into from
Sep 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@ The following core Node.js modules (v8.7.0+) have been implemented:
| [events](https://nodejs.org/api/events.html) | :heavy_check_mark: |
| [fs](https://nodejs.org/api/fs.html) | :heavy_check_mark: |
| [http](https://nodejs.org/api/http.html) | :heavy_check_mark: |
| [http2](https://nodejs.org/api/http2.html) | |
| [https](https://nodejs.org/api/https.html) | :heavy_check_mark: |
| [net](https://nodejs.org/api/net.html) | |
| [net](https://nodejs.org/api/net.html) | :heavy_check_mark: |
| [os](https://nodejs.org/api/os.html) | :heavy_check_mark: |
| [path](https://nodejs.org/api/path.html) | :heavy_check_mark: |
| [process](https://nodejs.org/api/process.html) | :heavy_check_mark: |
Expand All @@ -43,6 +42,7 @@ The following core Node.js modules (v8.7.0+) have been implemented:
| [stream](https://nodejs.org/api/stream.html) | :heavy_check_mark: |
| [string-decoder](https://nodejs.org/api/string_decoder.html) | :heavy_check_mark: |
| [timers](https://nodejs.org/api/timers.html) | :heavy_check_mark: |
| [tls](https://nodejs.org/api/tls.html) | |
| [tty](https://nodejs.org/api/tty.html) | :heavy_check_mark: |
| [url](https://nodejs.org/api/url.html) | :heavy_check_mark: |
| [util](https://nodejs.org/api/util.html) | :heavy_check_mark: |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ class ConnectionOptions(
var hints: js.UndefOr[Int] = js.undefined,
var lookup: js.UndefOr[js.Function1[String, Any]] = js.undefined,
var onread: js.UndefOr[OnreadObject] = js.undefined,
// for new Socket(option)
// for IPC connections
var path: js.UndefOr[String],
// for new Socket(option)
var fd: js.UndefOr[Int] = js.undefined,
var allowHalfOpen: js.UndefOr[Boolean] = js.undefined,
var readable: js.UndefOr[Boolean] = js.undefined,
Expand All @@ -23,5 +25,5 @@ class ConnectionOptions(

class OnreadObject(
var buffer: Uint8Array | js.Function0[Uint8Array],
var calback: js.Function2[Int, Uint8Array, Boolean]
var callback: js.Function2[Int, Uint8Array, Boolean]
) extends js.Object
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ package object http {
/**
* Http Extensions
*/
implicit class HttpExtensions(val http: Http) extends AnyVal {
implicit final class HttpExtensions(val http: Http) extends AnyVal {

/**
* @see [[Http.createServer()]]
Expand Down Expand Up @@ -63,7 +63,7 @@ package object http {
/**
* Server Events
*/
implicit class ServerEvents(val server: Server) extends AnyVal {
implicit final class ServerEvents(val server: Server) extends AnyVal {

/**
* Emitted each time a request with an http Expect: 100-continue is received. If this event isn't listened for,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package io.scalajs.nodejs.net

import scala.scalajs.js

/**
* Listener Options
*/
class ListenerOptions(var host: js.UndefOr[String] = js.undefined,
var port: js.UndefOr[Int] = js.undefined,
var path: js.UndefOr[String] = js.undefined,
var backlog: js.UndefOr[Int] = js.undefined,
var exclusive: js.UndefOr[Boolean] = js.undefined)
extends js.Object
class ListenerOptions(
var host: js.UndefOr[String] = js.undefined,
var port: js.UndefOr[Int] = js.undefined,
var path: js.UndefOr[String] = js.undefined,
var backlog: js.UndefOr[Int] = js.undefined,
var exclusive: js.UndefOr[Boolean] = js.undefined,
var readableAll: js.UndefOr[Boolean] = js.undefined,
var writableAll: js.UndefOr[Boolean] = js.undefined,
var ipv6Only: js.UndefOr[Boolean] = js.undefined
) extends js.Object
102 changes: 15 additions & 87 deletions app/current/src/main/scala/io/scalajs/nodejs/net/Net.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,52 +24,15 @@ trait Net extends IEventEmitter {
* @example net.connect(options[, connectListener])
*/
def connect(options: ConnectOptions, callback: js.Function): Socket = js.native
def connect(options: ConnectOptions): Socket = js.native

/**
* A factory function, which returns a new net.Socket and automatically connects with the supplied options.
* The options are passed to both the net.Socket constructor and the socket.connect method.
* The connectListener parameter will be added as a listener for the 'connect' event once.
* @example net.connect(options[, connectListener])
*/
def connect(options: ConnectOptions): Socket = js.native

/**
* A factory function, which returns a new unix net.Socket and automatically connects to the supplied path.
* The connectListener parameter will be added as a listener for the 'connect' event once.
* @example net.connect(path[, connectListener])
*/
def connect(path: String, connectListener: js.Function): Socket = js.native
def connect(path: String): Socket = js.native

/**
* A factory function, which returns a new unix net.Socket and automatically connects to the supplied path.
* The connectListener parameter will be added as a listener for the 'connect' event once.
* @example net.connect(path[, connectListener])
*/
def connect(path: String): Socket = js.native

/**
* A factory function, which returns a new net.Socket and automatically connects to the supplied port and host.
* If host is omitted, 'localhost' will be assumed.
* The connectListener parameter will be added as a listener for the 'connect' event once.
* @example net.connect(port[, host][, connectListener])
*/
def connect(port: Int, host: String, connectListener: js.Function): Socket = js.native

/**
* A factory function, which returns a new net.Socket and automatically connects to the supplied port and host.
* If host is omitted, 'localhost' will be assumed.
* The connectListener parameter will be added as a listener for the 'connect' event once.
* @example net.connect(port[, host][, connectListener])
*/
def connect(port: Int, host: String): Socket = js.native

/**
* A factory function, which returns a new net.Socket and automatically connects to the supplied port and host.
* If host is omitted, 'localhost' will be assumed.
* The connectListener parameter will be added as a listener for the 'connect' event once.
* @example net.connect(port[, host][, connectListener])
*/
def connect(port: Int): Socket = js.native
def connect(port: Int, connectListener: js.Function): Socket = js.native
def connect(port: Int, host: String): Socket = js.native
def connect(port: Int): Socket = js.native

/**
* A factory function, which returns a new net.Socket and automatically connects with the supplied options.
Expand All @@ -78,58 +41,23 @@ trait Net extends IEventEmitter {
* @example net.createConnection(options[, connectListener])
*/
def createConnection(options: ConnectOptions, connectListener: js.Function = js.native): Socket = js.native
def createConnection(path: String, connectListener: js.Function): Socket = js.native
def createConnection(path: String): Socket = js.native
def createConnection(port: Int, host: String, connectListener: js.Function): Socket = js.native
def createConnection(port: Int, connectListener: js.Function): Socket = js.native
def createConnection(port: Int, host: String): Socket = js.native
def createConnection(port: Int): Socket = js.native

/**
* @example net.createConnection(path[, connectListener])
*/
def createConnection(path: String, connectListener: js.Function): Socket = js.native

/**
* @example net.createConnection(path[, connectListener])
*/
def createConnection(path: String): Socket = js.native

/**
* @example net.createConnection(port[, host][, connectListener])
*/
def createConnection(port: Int, host: String, connectListener: js.Function): Socket = js.native

/**
* @example net.createConnection(port[, host][, connectListener])
*/
def createConnection(port: Int, host: String): Socket = js.native

/**
* @example net.createConnection(port[, host][, connectListener])
*/
def createConnection(port: Int): Socket = js.native

/**
* @example net.createServer([options][, connectionListener])
*/
def createServer(options: ServerOptions, connectionListener: js.Function): Server = js.native

/**
* @example net.createServer([options][, connectionListener])
*/
def createServer(options: ServerOptions): Server = js.native

/**
* @example net.createServer([options][, connectionListener])
*/
def createServer(connectionListener: js.Function): Server = js.native

/**
* @example net.createServer([options][, connectionListener])
*/
def createServer(): Server = js.native
def createServer(options: ServerOptions, connectionListener: js.Function = js.native): Server = js.native
def createServer(connectionListener: js.Function): Server = js.native
def createServer(): Server = js.native

/**
* Tests if input is an IP address. Returns 0 for invalid strings, returns 4 for IP version 4 addresses,
* and returns 6 for IP version 6 addresses.
* @example net.isIP(input)
*/
def isIP(input: String): Boolean = js.native
def isIP(input: String): Int = js.native

/**
* Returns true if input is a version 4 IP address, otherwise returns false.
Expand Down
79 changes: 19 additions & 60 deletions app/current/src/main/scala/io/scalajs/nodejs/net/Server.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ import io.scalajs.nodejs.events.IEventEmitter

import scala.scalajs.js
import scala.scalajs.js.annotation.JSImport
import scala.scalajs.js.|

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

/////////////////////////////////////////////////////////////////////////////////
// Properties
/////////////////////////////////////////////////////////////////////////////////
def this(options: ServerOptions, connectionListener: js.Function) = this()
def this(connectionListener: js.Function) = this()

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

/////////////////////////////////////////////////////////////////////////////////
// Methods
/////////////////////////////////////////////////////////////////////////////////

/**
* Returns the bound address, the address family name and port of the server as reported by the operating system.
* Useful to find which port was assigned when giving getting an OS-assigned address. Returns an object with
* three properties, e.g. { port: 12346, family: 'IPv4', address: '127.0.0.1' }
* @example server.address()
*/
def address(): Address = js.native
def address(): Address | String = js.native

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

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

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

/**
* @example server.listen(port[, hostname][, backlog][, callback])
*/
def listen(port: Int, hostname: String, backlog: Int, callback: js.Function): Unit = js.native

/**
* @example server.listen(port[, hostname][, backlog][, callback])
*/
def listen(port: Int, hostname: String, backlog: Int): Unit = js.native

/**
* @example server.listen(port[, hostname][, backlog][, callback])
*/
def listen(port: Int, hostname: String): Unit = js.native

/**
* @example server.listen(port[, hostname][, backlog][, callback])
*/
def listen(port: Int): Unit = js.native
def listen(port: Int, hostname: String, backlog: Int): Unit = js.native
def listen(port: Int, hostname: String, callback: js.Function): Unit = js.native
def listen(port: Int, hostname: String): Unit = js.native
def listen(port: Int, callback: js.Function): Unit = js.native
def listen(port: Int): Unit = js.native
def listen(callback: js.Function): Unit = js.native
def listen(): Unit = js.native

/**
* Opposite of unref, calling ref on a previously unrefd server will not let the program exit if it's
Expand All @@ -116,23 +99,6 @@ class Server() extends IEventEmitter {
* @example server.unref()
*/
def unref(): this.type = js.native

/**
* Sets the timeout value for sockets, and emits a 'timeout' event on the Server object, passing the socket as
* an argument, if a timeout occurs.
*
* If there is a 'timeout' event listener on the Server object, then it will be called with the timed-out socket
* as an argument.
*
* By default, the Server's timeout value is 2 minutes, and sockets are destroyed automatically if they time out.
* However, if you assign a callback to the Server's 'timeout' event, then you are responsible for handling socket
* timeouts.
* @param msecs the timeout in milliseconds
* @param callback the callback
* @return [[Server]]
*/
def setTimeout(msecs: Double, callback: js.Function): this.type = js.native

}

/**
Expand All @@ -144,7 +110,7 @@ object Server extends {
* Server Events
* @param server the given [[Server]]
*/
implicit class ServerEvents(val server: Server) extends AnyVal {
implicit final class ServerEvents(val server: Server) extends AnyVal {

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

}

/**
* Server Options
*/
class ServerOptions(val allowHalfOpen: js.UndefOr[Boolean] = js.undefined,
val pauseOnConnect: js.UndefOr[Boolean] = js.undefined)
extends js.Object
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.scalajs.nodejs.net

import scala.scalajs.js

class ServerOptions(val allowHalfOpen: js.UndefOr[Boolean] = js.undefined,
val pauseOnConnect: js.UndefOr[Boolean] = js.undefined)
extends js.Object
Loading