mirror of
https://github.com/denoland/deno.git
synced 2024-11-21 15:04:11 -05:00
rename dial to connect and dialTLS to connectTLS (#3710)
This commit is contained in:
parent
4f1fa82d1d
commit
34b99fec8e
14 changed files with 61 additions and 72 deletions
|
@ -81,14 +81,13 @@ export { FileInfo } from "./file_info.ts";
|
|||
export { openPlugin } from "./plugins.ts";
|
||||
export {
|
||||
connect,
|
||||
dial,
|
||||
listen,
|
||||
Listener,
|
||||
Conn,
|
||||
ShutdownMode,
|
||||
shutdown
|
||||
} from "./net.ts";
|
||||
export { dialTLS, listenTLS } from "./tls.ts";
|
||||
export { connectTLS, listenTLS } from "./tls.ts";
|
||||
export { metrics, Metrics } from "./metrics.ts";
|
||||
export { resources } from "./resources.ts";
|
||||
export {
|
||||
|
|
|
@ -29,7 +29,7 @@ export let OP_REPL_START: number;
|
|||
export let OP_REPL_READLINE: number;
|
||||
export let OP_ACCEPT: number;
|
||||
export let OP_ACCEPT_TLS: number;
|
||||
export let OP_DIAL: number;
|
||||
export let OP_CONNECT: number;
|
||||
export let OP_SHUTDOWN: number;
|
||||
export let OP_LISTEN: number;
|
||||
export let OP_LISTEN_TLS: number;
|
||||
|
@ -69,7 +69,7 @@ export let OP_READ_LINK: number;
|
|||
export let OP_TRUNCATE: number;
|
||||
export let OP_MAKE_TEMP_DIR: number;
|
||||
export let OP_CWD: number;
|
||||
export let OP_DIAL_TLS: number;
|
||||
export let OP_CONNECT_TLS: number;
|
||||
export let OP_HOSTNAME: number;
|
||||
export let OP_OPEN_PLUGIN: number;
|
||||
export let OP_COMPILE: number;
|
||||
|
|
28
cli/js/lib.deno_runtime.d.ts
vendored
28
cli/js/lib.deno_runtime.d.ts
vendored
|
@ -1307,7 +1307,7 @@ declare namespace Deno {
|
|||
|
||||
interface Addr {
|
||||
transport: Transport;
|
||||
/** UNSTABLE: Address is unstable because inconsistent with DialOptions. */
|
||||
/** UNSTABLE: Address is unstable because inconsistent with ConnectOptions. */
|
||||
address: string;
|
||||
}
|
||||
|
||||
|
@ -1419,15 +1419,13 @@ declare namespace Deno {
|
|||
*/
|
||||
export function listenTLS(options: ListenTLSOptions): Listener;
|
||||
|
||||
/** UNSTABLE rename to ConnectOptions */
|
||||
export interface DialOptions {
|
||||
export interface ConnectOptions {
|
||||
port: number;
|
||||
hostname?: string;
|
||||
transport?: Transport;
|
||||
}
|
||||
|
||||
/** UNSTABLE: Rename to connect.
|
||||
*
|
||||
/**
|
||||
* Dial connects to the address on the named transport.
|
||||
*
|
||||
* @param options
|
||||
|
@ -1440,25 +1438,23 @@ declare namespace Deno {
|
|||
*
|
||||
* Examples:
|
||||
*
|
||||
* dial({ port: 80 })
|
||||
* dial({ hostname: "192.0.2.1", port: 80 })
|
||||
* dial({ hostname: "[2001:db8::1]", port: 80 });
|
||||
* dial({ hostname: "golang.org", port: 80, transport: "tcp" })
|
||||
* connect({ port: 80 })
|
||||
* connect({ hostname: "192.0.2.1", port: 80 })
|
||||
* connect({ hostname: "[2001:db8::1]", port: 80 });
|
||||
* connect({ hostname: "golang.org", port: 80, transport: "tcp" })
|
||||
*/
|
||||
export function dial(options: DialOptions): Promise<Conn>;
|
||||
export function connect(options: ConnectOptions): Promise<Conn>;
|
||||
|
||||
/** UNSTABLE: rename to ConnectTLSOptions */
|
||||
export interface DialTLSOptions {
|
||||
export interface ConnectTLSOptions {
|
||||
port: number;
|
||||
hostname?: string;
|
||||
certFile?: string;
|
||||
}
|
||||
|
||||
/** UNSTABLE: rename to connectTLS.
|
||||
*
|
||||
* dialTLS establishes a secure connection over TLS (transport layer security).
|
||||
/**
|
||||
* Establishes a secure connection over TLS (transport layer security).
|
||||
*/
|
||||
export function dialTLS(options: DialTLSOptions): Promise<Conn>;
|
||||
export function connectTLS(options: ConnectTLSOptions): Promise<Conn>;
|
||||
|
||||
/** UNSTABLE: not sure if broken or not */
|
||||
export interface Metrics {
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
||||
import { EOF, Reader, Writer, Closer } from "./io.ts";
|
||||
import { notImplemented } from "./util.ts";
|
||||
import { read, write, close } from "./files.ts";
|
||||
import * as dispatch from "./dispatch.ts";
|
||||
import { sendSync, sendAsync } from "./dispatch_json.ts";
|
||||
|
@ -9,7 +8,7 @@ export type Transport = "tcp";
|
|||
// TODO support other types:
|
||||
// export type Transport = "tcp" | "tcp4" | "tcp6" | "unix" | "unixpacket";
|
||||
|
||||
// TODO(ry) Replace 'address' with 'hostname' and 'port', similar to DialOptions
|
||||
// TODO(ry) Replace 'address' with 'hostname' and 'port', similar to ConnectOptions
|
||||
// and ListenOptions.
|
||||
export interface Addr {
|
||||
transport: Transport;
|
||||
|
@ -184,7 +183,7 @@ export function listen(options: ListenOptions): Listener {
|
|||
return new ListenerImpl(res.rid, transport, res.localAddr);
|
||||
}
|
||||
|
||||
export interface DialOptions {
|
||||
export interface ConnectOptions {
|
||||
port: number;
|
||||
hostname?: string;
|
||||
transport?: Transport;
|
||||
|
@ -202,24 +201,16 @@ export interface DialOptions {
|
|||
*
|
||||
* Examples:
|
||||
*
|
||||
* dial({ port: 80 })
|
||||
* dial({ hostname: "192.0.2.1", port: 80 })
|
||||
* dial({ hostname: "[2001:db8::1]", port: 80 });
|
||||
* dial({ hostname: "golang.org", port: 80, transport: "tcp" })
|
||||
* connect({ port: 80 })
|
||||
* connect({ hostname: "192.0.2.1", port: 80 })
|
||||
* connect({ hostname: "[2001:db8::1]", port: 80 });
|
||||
* connect({ hostname: "golang.org", port: 80, transport: "tcp" })
|
||||
*/
|
||||
export async function dial(options: DialOptions): Promise<Conn> {
|
||||
const res = await sendAsync(dispatch.OP_DIAL, {
|
||||
export async function connect(options: ConnectOptions): Promise<Conn> {
|
||||
const res = await sendAsync(dispatch.OP_CONNECT, {
|
||||
hostname: options.hostname || "127.0.0.1",
|
||||
port: options.port,
|
||||
transport: options.transport || "tcp"
|
||||
});
|
||||
return new ConnImpl(res.rid, res.remoteAddr!, res.localAddr!);
|
||||
}
|
||||
|
||||
/** **RESERVED** */
|
||||
export async function connect(
|
||||
_transport: Transport,
|
||||
_address: string
|
||||
): Promise<Conn> {
|
||||
return notImplemented();
|
||||
}
|
||||
|
|
|
@ -6,7 +6,7 @@ testPerm({ net: true }, function netListenClose(): void {
|
|||
const addr = listener.addr();
|
||||
assertEquals(addr.transport, "tcp");
|
||||
// TODO(ry) Replace 'address' with 'hostname' and 'port', similar to
|
||||
// DialOptions and ListenOptions.
|
||||
// ConnectOptions and ListenOptions.
|
||||
assertEquals(addr.address, "127.0.0.1:4500");
|
||||
listener.close();
|
||||
});
|
||||
|
@ -57,7 +57,7 @@ testPerm({ net: true }, async function netDialListen(): Promise<void> {
|
|||
conn.close();
|
||||
}
|
||||
);
|
||||
const conn = await Deno.dial({ hostname: "127.0.0.1", port: 4500 });
|
||||
const conn = await Deno.connect({ hostname: "127.0.0.1", port: 4500 });
|
||||
assertEquals(conn.remoteAddr, "127.0.0.1:4500");
|
||||
assert(conn.localAddr != null);
|
||||
const buf = new Uint8Array(1024);
|
||||
|
@ -99,7 +99,7 @@ testPerm({ net: true }, async function netListenAsyncIterator(): Promise<void> {
|
|||
}
|
||||
};
|
||||
runAsyncIterator();
|
||||
const conn = await Deno.dial("127.0.0.1:4500");
|
||||
const conn = await Deno.connect("127.0.0.1:4500");
|
||||
const buf = new Uint8Array(1024);
|
||||
const readResult = await conn.read(buf);
|
||||
assertEquals(3, readResult);
|
||||
|
@ -136,7 +136,7 @@ testPerm({ net: true }, async function netCloseReadSuccess() {
|
|||
conn.close();
|
||||
closeDeferred.resolve();
|
||||
});
|
||||
const conn = await Deno.dial(addr);
|
||||
const conn = await Deno.connect(addr);
|
||||
conn.closeRead(); // closing read
|
||||
closeReadDeferred.resolve();
|
||||
const buf = new Uint8Array(1024);
|
||||
|
@ -160,7 +160,7 @@ testPerm({ net: true }, async function netDoubleCloseRead() {
|
|||
await closeDeferred.promise;
|
||||
conn.close();
|
||||
});
|
||||
const conn = await Deno.dial(addr);
|
||||
const conn = await Deno.connect(addr);
|
||||
conn.closeRead(); // closing read
|
||||
let err;
|
||||
try {
|
||||
|
@ -188,7 +188,7 @@ testPerm({ net: true }, async function netCloseWriteSuccess() {
|
|||
await closeDeferred.promise;
|
||||
conn.close();
|
||||
});
|
||||
const conn = await Deno.dial(addr);
|
||||
const conn = await Deno.connect(addr);
|
||||
conn.closeWrite(); // closing write
|
||||
const buf = new Uint8Array(1024);
|
||||
// Check read not impacted
|
||||
|
@ -222,7 +222,7 @@ testPerm({ net: true }, async function netDoubleCloseWrite() {
|
|||
await closeDeferred.promise;
|
||||
conn.close();
|
||||
});
|
||||
const conn = await Deno.dial(addr);
|
||||
const conn = await Deno.connect(addr);
|
||||
conn.closeWrite(); // closing write
|
||||
let err;
|
||||
try {
|
||||
|
|
|
@ -11,7 +11,7 @@ test(function resourcesStdio(): void {
|
|||
|
||||
testPerm({ net: true }, async function resourcesNet(): Promise<void> {
|
||||
const listener = Deno.listen({ port: 4501 });
|
||||
const dialerConn = await Deno.dial({ port: 4501 });
|
||||
const dialerConn = await Deno.connect({ port: 4501 });
|
||||
const listenerConn = await listener.accept();
|
||||
|
||||
const res = Deno.resources();
|
||||
|
|
|
@ -5,19 +5,19 @@ import { Listener, Transport, Conn, ConnImpl, ListenerImpl } from "./net.ts";
|
|||
|
||||
// TODO(ry) There are many configuration options to add...
|
||||
// https://docs.rs/rustls/0.16.0/rustls/struct.ClientConfig.html
|
||||
interface DialTLSOptions {
|
||||
interface ConnectTLSOptions {
|
||||
port: number;
|
||||
hostname?: string;
|
||||
certFile?: string;
|
||||
}
|
||||
const dialTLSDefaults = { hostname: "127.0.0.1", transport: "tcp" };
|
||||
const connectTLSDefaults = { hostname: "127.0.0.1", transport: "tcp" };
|
||||
|
||||
/**
|
||||
* dialTLS establishes a secure connection over TLS (transport layer security).
|
||||
* Establishes a secure connection over TLS (transport layer security).
|
||||
*/
|
||||
export async function dialTLS(options: DialTLSOptions): Promise<Conn> {
|
||||
options = Object.assign(dialTLSDefaults, options);
|
||||
const res = await sendAsync(dispatch.OP_DIAL_TLS, options);
|
||||
export async function connectTLS(options: ConnectTLSOptions): Promise<Conn> {
|
||||
options = Object.assign(connectTLSDefaults, options);
|
||||
const res = await sendAsync(dispatch.OP_CONNECT_TLS, options);
|
||||
return new ConnImpl(res.rid, res.remoteAddr!, res.localAddr!);
|
||||
}
|
||||
|
||||
|
|
|
@ -7,10 +7,10 @@ import { runIfMain } from "../../std/testing/mod.ts";
|
|||
const encoder = new TextEncoder();
|
||||
const decoder = new TextDecoder();
|
||||
|
||||
test(async function dialTLSNoPerm(): Promise<void> {
|
||||
test(async function connectTLSNoPerm(): Promise<void> {
|
||||
let err;
|
||||
try {
|
||||
await Deno.dialTLS({ hostname: "github.com", port: 443 });
|
||||
await Deno.connectTLS({ hostname: "github.com", port: 443 });
|
||||
} catch (e) {
|
||||
err = e;
|
||||
}
|
||||
|
@ -18,10 +18,10 @@ test(async function dialTLSNoPerm(): Promise<void> {
|
|||
assertEquals(err.name, "PermissionDenied");
|
||||
});
|
||||
|
||||
test(async function dialTLSCertFileNoReadPerm(): Promise<void> {
|
||||
test(async function connectTLSCertFileNoReadPerm(): Promise<void> {
|
||||
let err;
|
||||
try {
|
||||
await Deno.dialTLS({
|
||||
await Deno.connectTLS({
|
||||
hostname: "github.com",
|
||||
port: 443,
|
||||
certFile: "cli/tests/tls/RootCA.crt"
|
||||
|
@ -173,7 +173,7 @@ testPerm({ read: true, net: true }, async function dialAndListenTLS(): Promise<
|
|||
}
|
||||
);
|
||||
|
||||
const conn = await Deno.dialTLS({
|
||||
const conn = await Deno.connectTLS({
|
||||
hostname,
|
||||
port,
|
||||
certFile: "cli/tests/tls/RootCA.pem"
|
||||
|
|
|
@ -22,7 +22,7 @@ use tokio::net::TcpStream;
|
|||
|
||||
pub fn init(i: &mut Isolate, s: &ThreadSafeState) {
|
||||
i.register_op("accept", s.core_op(json_op(s.stateful_op(op_accept))));
|
||||
i.register_op("dial", s.core_op(json_op(s.stateful_op(op_dial))));
|
||||
i.register_op("connect", s.core_op(json_op(s.stateful_op(op_connect))));
|
||||
i.register_op("shutdown", s.core_op(json_op(s.stateful_op(op_shutdown))));
|
||||
i.register_op("listen", s.core_op(json_op(s.stateful_op(op_listen))));
|
||||
}
|
||||
|
@ -126,18 +126,18 @@ fn op_accept(
|
|||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct DialArgs {
|
||||
struct ConnectArgs {
|
||||
transport: String,
|
||||
hostname: String,
|
||||
port: u16,
|
||||
}
|
||||
|
||||
fn op_dial(
|
||||
fn op_connect(
|
||||
state: &ThreadSafeState,
|
||||
args: Value,
|
||||
_zero_copy: Option<PinnedBuf>,
|
||||
) -> Result<JsonOp, ErrBox> {
|
||||
let args: DialArgs = serde_json::from_value(args)?;
|
||||
let args: ConnectArgs = serde_json::from_value(args)?;
|
||||
assert_eq!(args.transport, "tcp"); // TODO Support others.
|
||||
let state_ = state.clone();
|
||||
state.check_net(&args.hostname, args.port)?;
|
||||
|
|
|
@ -36,7 +36,10 @@ use webpki::DNSNameRef;
|
|||
use webpki_roots;
|
||||
|
||||
pub fn init(i: &mut Isolate, s: &ThreadSafeState) {
|
||||
i.register_op("dial_tls", s.core_op(json_op(s.stateful_op(op_dial_tls))));
|
||||
i.register_op(
|
||||
"connect_tls",
|
||||
s.core_op(json_op(s.stateful_op(op_connect_tls))),
|
||||
);
|
||||
i.register_op(
|
||||
"listen_tls",
|
||||
s.core_op(json_op(s.stateful_op(op_listen_tls))),
|
||||
|
@ -49,18 +52,18 @@ pub fn init(i: &mut Isolate, s: &ThreadSafeState) {
|
|||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
struct DialTLSArgs {
|
||||
struct ConnectTLSArgs {
|
||||
hostname: String,
|
||||
port: u16,
|
||||
cert_file: Option<String>,
|
||||
}
|
||||
|
||||
pub fn op_dial_tls(
|
||||
pub fn op_connect_tls(
|
||||
state: &ThreadSafeState,
|
||||
args: Value,
|
||||
_zero_copy: Option<PinnedBuf>,
|
||||
) -> Result<JsonOp, ErrBox> {
|
||||
let args: DialTLSArgs = serde_json::from_value(args)?;
|
||||
let args: ConnectTLSArgs = serde_json::from_value(args)?;
|
||||
let cert_file = args.cert_file.clone();
|
||||
let state_ = state.clone();
|
||||
state.check_net(&args.hostname, args.port)?;
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
const { dial, run } = Deno;
|
||||
const { connect, run } = Deno;
|
||||
|
||||
import { test, runIfMain } from "../testing/mod.ts";
|
||||
import { assert, assertEquals } from "../testing/asserts.ts";
|
||||
|
@ -50,7 +50,7 @@ World 4
|
|||
test(async function serverPipelineRace(): Promise<void> {
|
||||
await startServer();
|
||||
|
||||
const conn = await dial({ port: 4501 });
|
||||
const conn = await connect({ port: 4501 });
|
||||
const r = new TextProtoReader(new BufReader(conn));
|
||||
await conn.write(new TextEncoder().encode(input));
|
||||
const outLines = output.split("\n");
|
||||
|
|
|
@ -595,7 +595,7 @@ test({
|
|||
await delay(100);
|
||||
|
||||
// Reqeusts to the server and immediately closes the connection
|
||||
const conn = await Deno.dial({ port: 4502 });
|
||||
const conn = await Deno.connect({ port: 4502 });
|
||||
await conn.write(new TextEncoder().encode("GET / HTTP/1.0\n\n"));
|
||||
conn.close();
|
||||
|
||||
|
@ -637,7 +637,7 @@ test({
|
|||
.catch((_): void => {}); // Ignores the error when closing the process.
|
||||
|
||||
// Requests to the server and immediately closes the connection
|
||||
const conn = await Deno.dialTLS({
|
||||
const conn = await Deno.connectTLS({
|
||||
hostname: "localhost",
|
||||
port: 4503,
|
||||
certFile: "http/testdata/tls/RootCA.pem"
|
||||
|
@ -721,7 +721,7 @@ if (Deno.build.os !== "win") {
|
|||
assert(!(connRid in resources));
|
||||
};
|
||||
const p = serverRoutine();
|
||||
const conn = await Deno.dial({
|
||||
const conn = await Deno.connect({
|
||||
hostname: "127.0.0.1",
|
||||
port: 8124
|
||||
});
|
||||
|
|
|
@ -484,10 +484,10 @@ export async function connectWebSocket(
|
|||
let conn: Conn;
|
||||
if (url.protocol === "http:" || url.protocol === "ws:") {
|
||||
const port = parseInt(url.port || "80");
|
||||
conn = await Deno.dial({ hostname, port });
|
||||
conn = await Deno.connect({ hostname, port });
|
||||
} else if (url.protocol === "https:" || url.protocol === "wss:") {
|
||||
const port = parseInt(url.port || "443");
|
||||
conn = await Deno.dialTLS({ hostname, port });
|
||||
conn = await Deno.connectTLS({ hostname, port });
|
||||
} else {
|
||||
throw new Error("ws: unsupported protocol: " + url.protocol);
|
||||
}
|
||||
|
|
|
@ -8,7 +8,7 @@ const [originHostname, originPort] = originAddr.split(":");
|
|||
const listener = Deno.listen({ hostname, port: Number(port) });
|
||||
|
||||
async function handle(conn: Deno.Conn): Promise<void> {
|
||||
const origin = await Deno.dial({
|
||||
const origin = await Deno.connect({
|
||||
hostname: originHostname,
|
||||
port: Number(originPort)
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue