summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBartek IwaƄczuk <biwanczuk@gmail.com>2022-02-27 15:18:30 +0100
committerGitHub <noreply@github.com>2022-02-27 15:18:30 +0100
commit7e3d9084b69d12119b2ad6ef7ac2681e66e36aa0 (patch)
tree623cc4785c7fc18f91cf9a51763e5517a2aca640
parenta65ce33fabb44bb2d9ed04773f7f334ed9c9a6b5 (diff)
feat: Add Deno.TcpConn class, change return type from Deno.connect (#13714)
-rw-r--r--cli/dts/lib.deno.unstable.d.ts20
-rw-r--r--cli/tests/unit/tls_test.ts10
-rw-r--r--cli/tsc.rs3
-rw-r--r--ext/net/01_net.js24
-rw-r--r--ext/net/lib.deno_net.d.ts17
5 files changed, 43 insertions, 31 deletions
diff --git a/cli/dts/lib.deno.unstable.d.ts b/cli/dts/lib.deno.unstable.d.ts
index 08b3ca80c..221ecee60 100644
--- a/cli/dts/lib.deno.unstable.d.ts
+++ b/cli/dts/lib.deno.unstable.d.ts
@@ -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.
*
diff --git a/cli/tests/unit/tls_test.ts b/cli/tests/unit/tls_test.ts
index 26a166801..07ffcd487 100644
--- a/cli/tests/unit/tls_test.ts
+++ b/cli/tests/unit/tls_test.ts
@@ -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();
},
);
diff --git a/cli/tsc.rs b/cli/tsc.rs
index 9500aae74..bff2f3404 100644
--- a/cli/tsc.rs
+++ b/cli/tsc.rs
@@ -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());
}
}
diff --git a/ext/net/01_net.js b/ext/net/01_net.js
index 2970dd817..8c17942da 100644
--- a/ext/net/01_net.js
+++ b/ext/net/01_net.js
@@ -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,
diff --git a/ext/net/lib.deno_net.d.ts b/ext/net/lib.deno_net.d.ts
index 038452055..ab779a1ff 100644
--- a/ext/net/lib.deno_net.d.ts
+++ b/ext/net/lib.deno_net.d.ts
@@ -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. */