1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-21 15:04:11 -05:00

feat: Add Deno.TcpConn class, change return type from Deno.connect (#13714)

This commit is contained in:
Bartek Iwańczuk 2022-02-27 15:18:30 +01:00 committed by GitHub
parent a65ce33fab
commit 7e3d9084b6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 43 additions and 31 deletions

View file

@ -1049,7 +1049,10 @@ declare namespace Deno {
*
* Requires `allow-net` permission for "tcp" and `allow-read` for "unix". */
export function connect(
options: ConnectOptions | UnixConnectOptions,
options: ConnectOptions,
): Promise<TcpConn>;
export function connect(
options: UnixConnectOptions,
): Promise<Conn>;
export interface ConnectTlsOptions {
@ -1066,21 +1069,6 @@ declare namespace Deno {
alpnProtocols?: string[];
}
export interface Conn {
/**
* **UNSTABLE**: new API, see https://github.com/denoland/deno/issues/13617.
*
* Enable/disable the use of Nagle's algorithm. Defaults to true.
*/
setNoDelay(nodelay?: boolean): void;
/**
* **UNSTABLE**: new API, see https://github.com/denoland/deno/issues/13617.
*
* Enable/disable keep-alive functionality.
*/
setKeepAlive(keepalive?: boolean): void;
}
export interface TlsHandshakeInfo {
/** **UNSTABLE**: new API, yet to be vetted.
*

View file

@ -1075,7 +1075,7 @@ Deno.test(
const port = 587;
const encoder = new TextEncoder();
let conn = await Deno.connect({
const conn = await Deno.connect({
hostname,
port,
});
@ -1102,9 +1102,9 @@ Deno.test(
// Received the message that the server is ready to establish TLS
assertEquals(line, "220 2.0.0 Ready to start TLS");
conn = await Deno.startTls(conn, { hostname });
writer = new BufWriter(conn);
reader = new TextProtoReader(new BufReader(conn));
const tlsConn = await Deno.startTls(conn, { hostname });
writer = new BufWriter(tlsConn);
reader = new TextProtoReader(new BufReader(tlsConn));
// After that use TLS communication again
await writer.write(encoder.encode(`EHLO ${hostname}\r\n`));
@ -1115,7 +1115,7 @@ Deno.test(
if (line.startsWith("250 ")) break;
}
conn.close();
tlsConn.close();
},
);

View file

@ -1148,6 +1148,7 @@ mod tests {
let actual = test_exec(&specifier)
.await
.expect("exec should not have errored");
eprintln!("diagnostics {:#?}", actual.diagnostics);
assert!(actual.diagnostics.is_empty());
assert!(actual.emitted_files.is_empty());
assert!(actual.maybe_tsbuildinfo.is_some());
@ -1160,6 +1161,7 @@ mod tests {
let actual = test_exec(&specifier)
.await
.expect("exec should not have errored");
eprintln!("diagnostics {:#?}", actual.diagnostics);
assert!(actual.diagnostics.is_empty());
assert!(actual.emitted_files.is_empty());
assert!(actual.maybe_tsbuildinfo.is_some());
@ -1172,6 +1174,7 @@ mod tests {
let actual = test_exec(&specifier)
.await
.expect("exec should not have errored");
eprintln!("diagnostics {:#?}", actual.diagnostics);
assert!(actual.diagnostics.is_empty());
}
}

View file

@ -163,14 +163,6 @@
return shutdown(this.rid);
}
setNoDelay(nodelay = true) {
return core.opSync("op_set_nodelay", this.rid, nodelay);
}
setKeepAlive(keepalive = true) {
return core.opSync("op_set_keepalive", this.rid, keepalive);
}
get readable() {
if (this.#readable === undefined) {
this.#readable = readableStreamForRid(this.rid);
@ -186,6 +178,16 @@
}
}
class TcpConn extends Conn {
setNoDelay(nodelay = true) {
return core.opSync("op_set_nodelay", this.rid, nodelay);
}
setKeepAlive(keepalive = true) {
return core.opSync("op_set_keepalive", this.rid, keepalive);
}
}
class Listener {
#rid = 0;
#addr = null;
@ -205,6 +207,9 @@
async accept() {
const res = await opAccept(this.rid, this.addr.transport);
if (this.addr.transport == "tcp") {
return new TcpConn(res.rid, res.remoteAddr, res.localAddr);
}
return new Conn(res.rid, res.remoteAddr, res.localAddr);
}
@ -318,12 +323,13 @@
});
}
return new Conn(res.rid, res.remoteAddr, res.localAddr);
return new TcpConn(res.rid, res.remoteAddr, res.localAddr);
}
window.__bootstrap.net = {
connect,
Conn,
TcpConn,
opConnect,
listen,
opListen,

View file

@ -143,7 +143,22 @@ declare namespace Deno {
* ```
*
* Requires `allow-net` permission for "tcp". */
export function connect(options: ConnectOptions): Promise<Conn>;
export function connect(options: ConnectOptions): Promise<TcpConn>;
export interface TcpConn extends Conn {
/**
* **UNSTABLE**: new API, see https://github.com/denoland/deno/issues/13617.
*
* Enable/disable the use of Nagle's algorithm. Defaults to true.
*/
setNoDelay(nodelay?: boolean): void;
/**
* **UNSTABLE**: new API, see https://github.com/denoland/deno/issues/13617.
*
* Enable/disable keep-alive functionality.
*/
setKeepAlive(keepalive?: boolean): void;
}
export interface ConnectTlsOptions {
/** The port to connect to. */