Add verified Ktor-backed networking modules to lyngio
This commit is contained in:
1 parent
f168e9f6ed
commit
d409a4bb8b
35 files changed
+4025
-23
No files matched your search
@@ -0,0 +1,72 @@
|
||||
package lyng.io.http
|
||||
|
||||
/*
|
||||
Response/header view that behaves like a map for the first value of each header name.
|
||||
Multi-valued headers are exposed through `getAll`.
|
||||
*/
|
||||
extern class HttpHeaders : Map<String, String> {
|
||||
/* Return the first value for the given header name, or null when absent. */
|
||||
fun get(name: String): String?
|
||||
/* Return all values for the given header name, preserving wire order when available. */
|
||||
fun getAll(name: String): List<String>
|
||||
/* Return distinct header names present in this response. */
|
||||
fun names(): List<String>
|
||||
}
|
||||
|
||||
/* Mutable request descriptor for programmatic HTTP calls. */
|
||||
extern class HttpRequest {
|
||||
/* Request method, e.g. GET, POST, PUT, DELETE. */
|
||||
var method: String
|
||||
/* Absolute URL including scheme. */
|
||||
var url: String
|
||||
/* Simple request headers map. */
|
||||
var headers: Map<String, String>
|
||||
/* Optional UTF-8 request body. */
|
||||
var bodyText: String?
|
||||
/* Optional binary request body. */
|
||||
var bodyBytes: Buffer?
|
||||
/* Optional total request timeout in milliseconds. */
|
||||
var timeoutMillis: Int?
|
||||
}
|
||||
|
||||
/* HTTP response snapshot with cached body accessors. */
|
||||
extern class HttpResponse {
|
||||
/* Numeric status code, e.g. 200, 404, 500. */
|
||||
val status: Int
|
||||
/* Human-readable status text when available. */
|
||||
val statusText: String
|
||||
/* Response headers. */
|
||||
val headers: HttpHeaders
|
||||
/* Decode response body as text. Cached after first call. */
|
||||
fun text(): String
|
||||
/* Return response body as bytes. Cached after first call. */
|
||||
fun bytes(): Buffer
|
||||
}
|
||||
|
||||
/* HTTP/HTTPS client singleton. */
|
||||
extern object Http {
|
||||
/* True when HTTP client support is available on this runtime. */
|
||||
fun isSupported(): Bool
|
||||
|
||||
/* Execute a request described by `HttpRequest`. */
|
||||
fun request(req: HttpRequest): HttpResponse
|
||||
|
||||
/*
|
||||
Convenience GET request.
|
||||
`headers...` accepts `MapEntry` values such as `"Accept" => "text/plain"`
|
||||
and 2-item lists such as `["Accept", "text/plain"]`.
|
||||
*/
|
||||
fun get(url: String, headers...): HttpResponse
|
||||
|
||||
/*
|
||||
Convenience POST request with UTF-8 body text.
|
||||
`headers...` follows the same rules as `get`.
|
||||
*/
|
||||
fun post(url: String, bodyText: String = "", contentType: String? = null, headers...): HttpResponse
|
||||
|
||||
/*
|
||||
Convenience POST request with binary body.
|
||||
`headers...` follows the same rules as `get`.
|
||||
*/
|
||||
fun postBytes(url: String, body: Buffer, contentType: String? = null, headers...): HttpResponse
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
package lyng.io.net
|
||||
|
||||
/* Address family for resolved or bound endpoints. */
|
||||
enum IpVersion {
|
||||
IPV4,
|
||||
IPV6
|
||||
}
|
||||
|
||||
/* Concrete socket endpoint. */
|
||||
extern class SocketAddress {
|
||||
/* Numeric or host-form address string. */
|
||||
val host: String
|
||||
/* Transport port number. */
|
||||
val port: Int
|
||||
/* Address family. */
|
||||
val ipVersion: IpVersion
|
||||
/* True when obtained from DNS resolution rather than raw bind input. */
|
||||
val resolved: Bool
|
||||
/* Stable printable form such as `127.0.0.1:4040` or `[::1]:4040`. */
|
||||
override fun toString(): String
|
||||
}
|
||||
|
||||
/* Datagram payload paired with sender/peer address. */
|
||||
extern class Datagram {
|
||||
val data: Buffer
|
||||
val address: SocketAddress
|
||||
}
|
||||
|
||||
/* Connected TCP socket. */
|
||||
extern class TcpSocket {
|
||||
/* True while the socket remains open. */
|
||||
fun isOpen(): Bool
|
||||
/* Local bound address after connect/bind. */
|
||||
fun localAddress(): SocketAddress
|
||||
/* Connected peer address. */
|
||||
fun remoteAddress(): SocketAddress
|
||||
/* Read up to `maxBytes`; returns null on clean EOF before any data. */
|
||||
fun read(maxBytes: Int = 65536): Buffer?
|
||||
/* Read one UTF-8 line without trailing newline; null on clean EOF. */
|
||||
fun readLine(): String?
|
||||
/* Write all bytes from the buffer. */
|
||||
fun write(data: Buffer): void
|
||||
/* Write UTF-8 encoded text. */
|
||||
fun writeUtf8(text: String): void
|
||||
/* Flush buffered output if needed by the backend. */
|
||||
fun flush(): void
|
||||
/* Close the socket. */
|
||||
fun close(): void
|
||||
}
|
||||
|
||||
/* Listening TCP server socket. */
|
||||
extern class TcpServer {
|
||||
/* True while the listening socket remains open. */
|
||||
fun isOpen(): Bool
|
||||
/* Actual local bind address. */
|
||||
fun localAddress(): SocketAddress
|
||||
/* Accept next incoming client connection. */
|
||||
fun accept(): TcpSocket
|
||||
/* Close the listening socket. */
|
||||
fun close(): void
|
||||
}
|
||||
|
||||
/* Bound UDP socket. */
|
||||
extern class UdpSocket {
|
||||
/* True while the socket remains open. */
|
||||
fun isOpen(): Bool
|
||||
/* Actual local bind address. */
|
||||
fun localAddress(): SocketAddress
|
||||
/* Receive one datagram up to `maxBytes`, or null on clean shutdown. */
|
||||
fun receive(maxBytes: Int = 65536): Datagram?
|
||||
/* Send one datagram to the provided host and port. */
|
||||
fun send(data: Buffer, host: String, port: Int): void
|
||||
/* Close the socket. */
|
||||
fun close(): void
|
||||
}
|
||||
|
||||
/* Raw transport networking singleton. */
|
||||
extern object Net {
|
||||
/* True when at least one transport feature is implemented on this runtime. */
|
||||
fun isSupported(): Bool
|
||||
/* True when outbound TCP client sockets are available. */
|
||||
fun isTcpAvailable(): Bool
|
||||
/* True when listening TCP server sockets are available. */
|
||||
fun isTcpServerAvailable(): Bool
|
||||
/* True when UDP datagram sockets are available. */
|
||||
fun isUdpAvailable(): Bool
|
||||
|
||||
/* Resolve host name and port into concrete addresses. */
|
||||
fun resolve(host: String, port: Int): List<SocketAddress>
|
||||
|
||||
/* Connect outbound TCP socket. */
|
||||
fun tcpConnect(
|
||||
host: String,
|
||||
port: Int,
|
||||
timeoutMillis: Int? = null,
|
||||
noDelay: Bool = true
|
||||
): TcpSocket
|
||||
|
||||
/* Start listening TCP server socket. */
|
||||
fun tcpListen(
|
||||
port: Int,
|
||||
host: String? = null,
|
||||
backlog: Int = 128,
|
||||
reuseAddress: Bool = true
|
||||
): TcpServer
|
||||
|
||||
/* Bind UDP socket, using an ephemeral port by default. */
|
||||
fun udpBind(
|
||||
port: Int = 0,
|
||||
host: String? = null,
|
||||
reuseAddress: Bool = true
|
||||
): UdpSocket
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package lyng.io.ws
|
||||
|
||||
/* Received WebSocket message. */
|
||||
extern class WsMessage {
|
||||
/* True when this message carries text payload. */
|
||||
val isText: Bool
|
||||
/* Text payload for text messages, otherwise null. */
|
||||
val text: String?
|
||||
/* Binary payload for binary messages, otherwise null. */
|
||||
val data: Buffer?
|
||||
}
|
||||
|
||||
/* Active WebSocket client session. */
|
||||
extern class WsSession {
|
||||
/* True while the session remains open. */
|
||||
fun isOpen(): Bool
|
||||
/* Connected URL string. */
|
||||
fun url(): String
|
||||
/* Send a text message. */
|
||||
fun sendText(text: String): void
|
||||
/* Send a binary message. */
|
||||
fun sendBytes(data: Buffer): void
|
||||
/* Receive next message, or null on clean close. */
|
||||
fun receive(): WsMessage?
|
||||
/* Close the session with optional status code and reason. */
|
||||
fun close(code: Int = 1000, reason: String = ""): void
|
||||
}
|
||||
|
||||
/* WebSocket client singleton. */
|
||||
extern object Ws {
|
||||
/* True when WebSocket client support is available on this runtime. */
|
||||
fun isSupported(): Bool
|
||||
|
||||
/*
|
||||
Open a client WebSocket session.
|
||||
`headers...` accepts `MapEntry` values such as `"Authorization" => "Bearer x"`
|
||||
and 2-item lists such as `["Authorization", "Bearer x"]`.
|
||||
*/
|
||||
fun connect(url: String, headers...): WsSession
|
||||
}
|
||||
Reference in new issue
Block a user