mirror of
https://github.com/denoland/deno.git
synced 2024-12-25 16:49:18 -05:00
dial/listen API change (#3000)
Previously: dial("tcp", "deno.land:80") Now: dial({ hostname: "deno.land", port: 80, transport: "tcp" }) Similarly with listen().
This commit is contained in:
parent
93b7acf99d
commit
97bb2bdb79
14 changed files with 166 additions and 144 deletions
|
@ -49,8 +49,9 @@ pub fn op_accept(
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
struct DialArgs {
|
struct DialArgs {
|
||||||
network: String,
|
transport: String,
|
||||||
address: String,
|
hostname: String,
|
||||||
|
port: u16,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn op_dial(
|
pub fn op_dial(
|
||||||
|
@ -59,9 +60,11 @@ pub fn op_dial(
|
||||||
_zero_copy: Option<PinnedBuf>,
|
_zero_copy: Option<PinnedBuf>,
|
||||||
) -> Result<JsonOp, ErrBox> {
|
) -> Result<JsonOp, ErrBox> {
|
||||||
let args: DialArgs = serde_json::from_value(args)?;
|
let args: DialArgs = serde_json::from_value(args)?;
|
||||||
let network = args.network;
|
assert_eq!(args.transport, "tcp"); // TODO Support others.
|
||||||
assert_eq!(network, "tcp"); // TODO Support others.
|
|
||||||
let address = args.address;
|
// TODO(ry) Using format! is suboptimal here. Better would be if
|
||||||
|
// state.check_net and resolve_addr() took hostname and port directly.
|
||||||
|
let address = format!("{}:{}", args.hostname, args.port);
|
||||||
|
|
||||||
state.check_net(&address)?;
|
state.check_net(&address)?;
|
||||||
|
|
||||||
|
@ -117,8 +120,9 @@ pub fn op_shutdown(
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
struct ListenArgs {
|
struct ListenArgs {
|
||||||
network: String,
|
transport: String,
|
||||||
address: String,
|
hostname: String,
|
||||||
|
port: u16,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn op_listen(
|
pub fn op_listen(
|
||||||
|
@ -127,10 +131,11 @@ pub fn op_listen(
|
||||||
_zero_copy: Option<PinnedBuf>,
|
_zero_copy: Option<PinnedBuf>,
|
||||||
) -> Result<JsonOp, ErrBox> {
|
) -> Result<JsonOp, ErrBox> {
|
||||||
let args: ListenArgs = serde_json::from_value(args)?;
|
let args: ListenArgs = serde_json::from_value(args)?;
|
||||||
|
assert_eq!(args.transport, "tcp");
|
||||||
|
|
||||||
let network = args.network;
|
// TODO(ry) Using format! is suboptimal here. Better would be if
|
||||||
assert_eq!(network, "tcp");
|
// state.check_net and resolve_addr() took hostname and port directly.
|
||||||
let address = args.address;
|
let address = format!("{}:{}", args.hostname, args.port);
|
||||||
|
|
||||||
state.check_net(&address)?;
|
state.check_net(&address)?;
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
const { args, listen, copy } = Deno;
|
const { args, listen, copy } = Deno;
|
||||||
const addr = args[1] || "127.0.0.1:4544";
|
const addr = args[1] || "127.0.0.1:4544";
|
||||||
const listener = listen("tcp", addr);
|
const [hostname, port] = addr.split(":");
|
||||||
|
const listener = listen({ hostname, port: Number(port) });
|
||||||
console.log("listening on", addr);
|
console.log("listening on", addr);
|
||||||
listener.accept().then(
|
listener.accept().then(
|
||||||
async (conn): Promise<void> => {
|
async (conn): Promise<void> => {
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
Subproject commit 6663e698d08147ae0a736d1df52ffd857729d3da
|
Subproject commit 43aafbf33285753e7b42230f0eb7969b300f71cf
|
|
@ -264,7 +264,7 @@ testPerm({ net: true }, async function fetchUserAgent(): Promise<void> {
|
||||||
|
|
||||||
/*
|
/*
|
||||||
function bufferServer(addr: string): Deno.Buffer {
|
function bufferServer(addr: string): Deno.Buffer {
|
||||||
const listener = Deno.listen("tcp", addr);
|
const listener = Deno.listen(addr);
|
||||||
const buf = new Deno.Buffer();
|
const buf = new Deno.Buffer();
|
||||||
listener.accept().then(async conn => {
|
listener.accept().then(async conn => {
|
||||||
const p1 = buf.readFrom(conn);
|
const p1 = buf.readFrom(conn);
|
||||||
|
|
91
js/lib.deno_runtime.d.ts
vendored
91
js/lib.deno_runtime.d.ts
vendored
|
@ -914,11 +914,12 @@ declare namespace Deno {
|
||||||
|
|
||||||
// @url js/net.d.ts
|
// @url js/net.d.ts
|
||||||
|
|
||||||
type Network = "tcp";
|
type Transport = "tcp";
|
||||||
interface Addr {
|
interface Addr {
|
||||||
network: Network;
|
transport: Transport;
|
||||||
address: string;
|
address: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** A Listener is a generic network listener for stream-oriented protocols. */
|
/** A Listener is a generic network listener for stream-oriented protocols. */
|
||||||
export interface Listener extends AsyncIterator<Conn> {
|
export interface Listener extends AsyncIterator<Conn> {
|
||||||
/** Waits for and resolves to the next connection to the `Listener`. */
|
/** Waits for and resolves to the next connection to the `Listener`. */
|
||||||
|
@ -947,52 +948,56 @@ declare namespace Deno {
|
||||||
*/
|
*/
|
||||||
closeWrite(): void;
|
closeWrite(): void;
|
||||||
}
|
}
|
||||||
/** Listen announces on the local network address.
|
|
||||||
|
export interface ListenOptions {
|
||||||
|
port: number;
|
||||||
|
hostname?: string;
|
||||||
|
transport?: Transport;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Listen announces on the local transport address.
|
||||||
*
|
*
|
||||||
* The network must be `tcp`, `tcp4`, `tcp6`, `unix` or `unixpacket`.
|
* @param options
|
||||||
*
|
* @param options.port The port to connect to. (Required.)
|
||||||
* For TCP networks, if the host in the address parameter is empty or a literal
|
* @param options.hostname A literal IP address or host name that can be
|
||||||
* unspecified IP address, `listen()` listens on all available unicast and
|
* resolved to an IP address. If not specified, defaults to 0.0.0.0
|
||||||
* anycast IP addresses of the local system. To only use IPv4, use network
|
* @param options.transport Defaults to "tcp". Later we plan to add "tcp4",
|
||||||
* `tcp4`. The address can use a host name, but this is not recommended,
|
* "tcp6", "udp", "udp4", "udp6", "ip", "ip4", "ip6", "unix", "unixgram" and
|
||||||
* because it will create a listener for at most one of the host's IP
|
* "unixpacket".
|
||||||
* addresses. If the port in the address parameter is empty or `0`, as in
|
|
||||||
* `127.0.0.1:` or `[::1]:0`, a port number is automatically chosen. The
|
|
||||||
* `addr()` method of `Listener` can be used to discover the chosen port.
|
|
||||||
*
|
|
||||||
* See `dial()` for a description of the network and address parameters.
|
|
||||||
*/
|
|
||||||
export function listen(network: Network, address: string): Listener;
|
|
||||||
/** Dial connects to the address on the named network.
|
|
||||||
*
|
|
||||||
* Supported networks are only `tcp` currently.
|
|
||||||
*
|
|
||||||
* TODO: `tcp4` (IPv4-only), `tcp6` (IPv6-only), `udp`, `udp4` (IPv4-only),
|
|
||||||
* `udp6` (IPv6-only), `ip`, `ip4` (IPv4-only), `ip6` (IPv6-only), `unix`,
|
|
||||||
* `unixgram` and `unixpacket`.
|
|
||||||
*
|
|
||||||
* For TCP and UDP networks, the address has the form `host:port`. The host must
|
|
||||||
* be a literal IP address, or a host name that can be resolved to IP addresses.
|
|
||||||
* The port must be a literal port number or a service name. If the host is a
|
|
||||||
* literal IPv6 address it must be enclosed in square brackets, as in
|
|
||||||
* `[2001:db8::1]:80` or `[fe80::1%zone]:80`. The zone specifies the scope of
|
|
||||||
* the literal IPv6 address as defined in RFC 4007. The functions JoinHostPort
|
|
||||||
* and SplitHostPort manipulate a pair of host and port in this form. When using
|
|
||||||
* TCP, and the host resolves to multiple IP addresses, Dial will try each IP
|
|
||||||
* address in order until one succeeds.
|
|
||||||
*
|
*
|
||||||
* Examples:
|
* Examples:
|
||||||
*
|
*
|
||||||
* dial("tcp", "golang.org:http")
|
* listen({ port: 80 })
|
||||||
* dial("tcp", "192.0.2.1:http")
|
* listen({ hostname: "192.0.2.1", port: 80 })
|
||||||
* dial("tcp", "198.51.100.1:80")
|
* listen({ hostname: "[2001:db8::1]", port: 80 });
|
||||||
* dial("udp", "[2001:db8::1]:domain")
|
* listen({ hostname: "golang.org", port: 80, transport: "tcp" })
|
||||||
* dial("udp", "[fe80::1%lo0]:53")
|
|
||||||
* dial("tcp", ":80")
|
|
||||||
*/
|
*/
|
||||||
export function dial(network: Network, address: string): Promise<Conn>;
|
export function listen(options: ListenOptions): Listener;
|
||||||
/** **RESERVED** */
|
|
||||||
export function connect(_network: Network, _address: string): Promise<Conn>;
|
export interface DialOptions {
|
||||||
|
port: number;
|
||||||
|
hostname?: string;
|
||||||
|
transport?: Transport;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Dial connects to the address on the named transport.
|
||||||
|
*
|
||||||
|
* @param options
|
||||||
|
* @param options.port The port to connect to. (Required.)
|
||||||
|
* @param options.hostname A literal IP address or host name that can be
|
||||||
|
* resolved to an IP address. If not specified, defaults to 127.0.0.1
|
||||||
|
* @param options.transport Defaults to "tcp". Later we plan to add "tcp4",
|
||||||
|
* "tcp6", "udp", "udp4", "udp6", "ip", "ip4", "ip6", "unix", "unixgram" and
|
||||||
|
* "unixpacket".
|
||||||
|
*
|
||||||
|
* 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" })
|
||||||
|
*/
|
||||||
|
export function dial(options: DialOptions): Promise<Conn>;
|
||||||
|
|
||||||
// @url js/metrics.d.ts
|
// @url js/metrics.d.ts
|
||||||
|
|
||||||
|
|
108
js/net.ts
108
js/net.ts
|
@ -5,16 +5,18 @@ import { read, write, close } from "./files.ts";
|
||||||
import * as dispatch from "./dispatch.ts";
|
import * as dispatch from "./dispatch.ts";
|
||||||
import { sendSync, sendAsync } from "./dispatch_json.ts";
|
import { sendSync, sendAsync } from "./dispatch_json.ts";
|
||||||
|
|
||||||
export type Network = "tcp";
|
export type Transport = "tcp";
|
||||||
// TODO support other types:
|
// TODO support other types:
|
||||||
// export type Network = "tcp" | "tcp4" | "tcp6" | "unix" | "unixpacket";
|
// export type Transport = "tcp" | "tcp4" | "tcp6" | "unix" | "unixpacket";
|
||||||
|
|
||||||
|
// TODO(ry) Replace 'address' with 'hostname' and 'port', similar to DialOptions
|
||||||
|
// and ListenOptions.
|
||||||
export interface Addr {
|
export interface Addr {
|
||||||
network: Network;
|
transport: Transport;
|
||||||
address: string;
|
address: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** A Listener is a generic network listener for stream-oriented protocols. */
|
/** A Listener is a generic transport listener for stream-oriented protocols. */
|
||||||
export interface Listener extends AsyncIterator<Conn> {
|
export interface Listener extends AsyncIterator<Conn> {
|
||||||
/** Waits for and resolves to the next connection to the `Listener`. */
|
/** Waits for and resolves to the next connection to the `Listener`. */
|
||||||
accept(): Promise<Conn>;
|
accept(): Promise<Conn>;
|
||||||
|
@ -79,7 +81,7 @@ class ConnImpl implements Conn {
|
||||||
class ListenerImpl implements Listener {
|
class ListenerImpl implements Listener {
|
||||||
constructor(
|
constructor(
|
||||||
readonly rid: number,
|
readonly rid: number,
|
||||||
private network: Network,
|
private transport: Transport,
|
||||||
private localAddr: string
|
private localAddr: string
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
|
@ -94,7 +96,7 @@ class ListenerImpl implements Listener {
|
||||||
|
|
||||||
addr(): Addr {
|
addr(): Addr {
|
||||||
return {
|
return {
|
||||||
network: this.network,
|
transport: this.transport,
|
||||||
address: this.localAddr
|
address: this.localAddr
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -128,62 +130,70 @@ export interface Conn extends Reader, Writer, Closer {
|
||||||
closeWrite(): void;
|
closeWrite(): void;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Listen announces on the local network address.
|
export interface ListenOptions {
|
||||||
*
|
port: number;
|
||||||
* The network must be `tcp`, `tcp4`, `tcp6`, `unix` or `unixpacket`.
|
hostname?: string;
|
||||||
*
|
transport?: Transport;
|
||||||
* For TCP networks, if the host in the address parameter is empty or a literal
|
|
||||||
* unspecified IP address, `listen()` listens on all available unicast and
|
|
||||||
* anycast IP addresses of the local system. To only use IPv4, use network
|
|
||||||
* `tcp4`. The address can use a host name, but this is not recommended,
|
|
||||||
* because it will create a listener for at most one of the host's IP
|
|
||||||
* addresses. If the port in the address parameter is empty or `0`, as in
|
|
||||||
* `127.0.0.1:` or `[::1]:0`, a port number is automatically chosen. The
|
|
||||||
* `addr()` method of `Listener` can be used to discover the chosen port.
|
|
||||||
*
|
|
||||||
* See `dial()` for a description of the network and address parameters.
|
|
||||||
*/
|
|
||||||
export function listen(network: Network, address: string): Listener {
|
|
||||||
const res = sendSync(dispatch.OP_LISTEN, { network, address });
|
|
||||||
return new ListenerImpl(res.rid, network, res.localAddr);
|
|
||||||
}
|
}
|
||||||
|
const listenDefaults = { hostname: "0.0.0.0", transport: "tcp" };
|
||||||
|
|
||||||
/** Dial connects to the address on the named network.
|
/** Listen announces on the local transport address.
|
||||||
*
|
*
|
||||||
* Supported networks are only `tcp` currently.
|
* @param options
|
||||||
*
|
* @param options.port The port to connect to. (Required.)
|
||||||
* TODO: `tcp4` (IPv4-only), `tcp6` (IPv6-only), `udp`, `udp4` (IPv4-only),
|
* @param options.hostname A literal IP address or host name that can be
|
||||||
* `udp6` (IPv6-only), `ip`, `ip4` (IPv4-only), `ip6` (IPv6-only), `unix`,
|
* resolved to an IP address. If not specified, defaults to 0.0.0.0
|
||||||
* `unixgram` and `unixpacket`.
|
* @param options.transport Defaults to "tcp". Later we plan to add "tcp4",
|
||||||
*
|
* "tcp6", "udp", "udp4", "udp6", "ip", "ip4", "ip6", "unix", "unixgram" and
|
||||||
* For TCP and UDP networks, the address has the form `host:port`. The host must
|
* "unixpacket".
|
||||||
* be a literal IP address, or a host name that can be resolved to IP addresses.
|
|
||||||
* The port must be a literal port number or a service name. If the host is a
|
|
||||||
* literal IPv6 address it must be enclosed in square brackets, as in
|
|
||||||
* `[2001:db8::1]:80` or `[fe80::1%zone]:80`. The zone specifies the scope of
|
|
||||||
* the literal IPv6 address as defined in RFC 4007. The functions JoinHostPort
|
|
||||||
* and SplitHostPort manipulate a pair of host and port in this form. When using
|
|
||||||
* TCP, and the host resolves to multiple IP addresses, Dial will try each IP
|
|
||||||
* address in order until one succeeds.
|
|
||||||
*
|
*
|
||||||
* Examples:
|
* Examples:
|
||||||
*
|
*
|
||||||
* dial("tcp", "golang.org:http")
|
* listen({ port: 80 })
|
||||||
* dial("tcp", "192.0.2.1:http")
|
* listen({ hostname: "192.0.2.1", port: 80 })
|
||||||
* dial("tcp", "198.51.100.1:80")
|
* listen({ hostname: "[2001:db8::1]", port: 80 });
|
||||||
* dial("udp", "[2001:db8::1]:domain")
|
* listen({ hostname: "golang.org", port: 80, transport: "tcp" })
|
||||||
* dial("udp", "[fe80::1%lo0]:53")
|
|
||||||
* dial("tcp", ":80")
|
|
||||||
*/
|
*/
|
||||||
export async function dial(network: Network, address: string): Promise<Conn> {
|
export function listen(options: ListenOptions): Listener {
|
||||||
const res = await sendAsync(dispatch.OP_DIAL, { network, address });
|
options = Object.assign(listenDefaults, options);
|
||||||
|
const res = sendSync(dispatch.OP_LISTEN, options);
|
||||||
|
return new ListenerImpl(res.rid, options.transport, res.localAddr);
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface DialOptions {
|
||||||
|
port: number;
|
||||||
|
hostname?: string;
|
||||||
|
transport?: Transport;
|
||||||
|
}
|
||||||
|
const dialDefaults = { hostname: "127.0.0.1", transport: "tcp" };
|
||||||
|
|
||||||
|
/** Dial connects to the address on the named transport.
|
||||||
|
*
|
||||||
|
* @param options
|
||||||
|
* @param options.port The port to connect to. (Required.)
|
||||||
|
* @param options.hostname A literal IP address or host name that can be
|
||||||
|
* resolved to an IP address. If not specified, defaults to 127.0.0.1
|
||||||
|
* @param options.transport Defaults to "tcp". Later we plan to add "tcp4",
|
||||||
|
* "tcp6", "udp", "udp4", "udp6", "ip", "ip4", "ip6", "unix", "unixgram" and
|
||||||
|
* "unixpacket".
|
||||||
|
*
|
||||||
|
* 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" })
|
||||||
|
*/
|
||||||
|
export async function dial(options: DialOptions): Promise<Conn> {
|
||||||
|
options = Object.assign(dialDefaults, options);
|
||||||
|
const res = await sendAsync(dispatch.OP_DIAL, options);
|
||||||
// TODO(bartlomieju): add remoteAddr and localAddr on Rust side
|
// TODO(bartlomieju): add remoteAddr and localAddr on Rust side
|
||||||
return new ConnImpl(res.rid, res.remoteAddr!, res.localAddr!);
|
return new ConnImpl(res.rid, res.remoteAddr!, res.localAddr!);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** **RESERVED** */
|
/** **RESERVED** */
|
||||||
export async function connect(
|
export async function connect(
|
||||||
_network: Network,
|
_transport: Transport,
|
||||||
_address: string
|
_address: string
|
||||||
): Promise<Conn> {
|
): Promise<Conn> {
|
||||||
return notImplemented();
|
return notImplemented();
|
||||||
|
|
|
@ -2,15 +2,17 @@
|
||||||
import { testPerm, assert, assertEquals } from "./test_util.ts";
|
import { testPerm, assert, assertEquals } from "./test_util.ts";
|
||||||
|
|
||||||
testPerm({ net: true }, function netListenClose(): void {
|
testPerm({ net: true }, function netListenClose(): void {
|
||||||
const listener = Deno.listen("tcp", "127.0.0.1:4500");
|
const listener = Deno.listen({ hostname: "127.0.0.1", port: 4500 });
|
||||||
const addr = listener.addr();
|
const addr = listener.addr();
|
||||||
assertEquals(addr.network, "tcp");
|
assertEquals(addr.transport, "tcp");
|
||||||
|
// TODO(ry) Replace 'address' with 'hostname' and 'port', similar to
|
||||||
|
// DialOptions and ListenOptions.
|
||||||
assertEquals(addr.address, "127.0.0.1:4500");
|
assertEquals(addr.address, "127.0.0.1:4500");
|
||||||
listener.close();
|
listener.close();
|
||||||
});
|
});
|
||||||
|
|
||||||
testPerm({ net: true }, async function netCloseWhileAccept(): Promise<void> {
|
testPerm({ net: true }, async function netCloseWhileAccept(): Promise<void> {
|
||||||
const listener = Deno.listen("tcp", ":4501");
|
const listener = Deno.listen({ port: 4501 });
|
||||||
const p = listener.accept();
|
const p = listener.accept();
|
||||||
listener.close();
|
listener.close();
|
||||||
let err;
|
let err;
|
||||||
|
@ -25,7 +27,7 @@ testPerm({ net: true }, async function netCloseWhileAccept(): Promise<void> {
|
||||||
});
|
});
|
||||||
|
|
||||||
testPerm({ net: true }, async function netConcurrentAccept(): Promise<void> {
|
testPerm({ net: true }, async function netConcurrentAccept(): Promise<void> {
|
||||||
const listener = Deno.listen("tcp", ":4502");
|
const listener = Deno.listen({ port: 4502 });
|
||||||
let acceptErrCount = 0;
|
let acceptErrCount = 0;
|
||||||
const checkErr = (e): void => {
|
const checkErr = (e): void => {
|
||||||
assertEquals(e.kind, Deno.ErrorKind.Other);
|
assertEquals(e.kind, Deno.ErrorKind.Other);
|
||||||
|
@ -46,7 +48,7 @@ testPerm({ net: true }, async function netConcurrentAccept(): Promise<void> {
|
||||||
});
|
});
|
||||||
|
|
||||||
testPerm({ net: true }, async function netDialListen(): Promise<void> {
|
testPerm({ net: true }, async function netDialListen(): Promise<void> {
|
||||||
const listener = Deno.listen("tcp", ":4500");
|
const listener = Deno.listen({ port: 4500 });
|
||||||
listener.accept().then(
|
listener.accept().then(
|
||||||
async (conn): Promise<void> => {
|
async (conn): Promise<void> => {
|
||||||
assert(conn.remoteAddr != null);
|
assert(conn.remoteAddr != null);
|
||||||
|
@ -55,7 +57,7 @@ testPerm({ net: true }, async function netDialListen(): Promise<void> {
|
||||||
conn.close();
|
conn.close();
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
const conn = await Deno.dial("tcp", "127.0.0.1:4500");
|
const conn = await Deno.dial({ hostname: "127.0.0.1", port: 4500 });
|
||||||
assertEquals(conn.remoteAddr, "127.0.0.1:4500");
|
assertEquals(conn.remoteAddr, "127.0.0.1:4500");
|
||||||
assert(conn.localAddr != null);
|
assert(conn.localAddr != null);
|
||||||
const buf = new Uint8Array(1024);
|
const buf = new Uint8Array(1024);
|
||||||
|
@ -77,7 +79,7 @@ testPerm({ net: true }, async function netDialListen(): Promise<void> {
|
||||||
|
|
||||||
/* TODO(ry) Re-enable this test.
|
/* TODO(ry) Re-enable this test.
|
||||||
testPerm({ net: true }, async function netListenAsyncIterator(): Promise<void> {
|
testPerm({ net: true }, async function netListenAsyncIterator(): Promise<void> {
|
||||||
const listener = Deno.listen("tcp", ":4500");
|
const listener = Deno.listen(":4500");
|
||||||
const runAsyncIterator = async (): Promise<void> => {
|
const runAsyncIterator = async (): Promise<void> => {
|
||||||
for await (let conn of listener) {
|
for await (let conn of listener) {
|
||||||
await conn.write(new Uint8Array([1, 2, 3]));
|
await conn.write(new Uint8Array([1, 2, 3]));
|
||||||
|
@ -85,7 +87,7 @@ testPerm({ net: true }, async function netListenAsyncIterator(): Promise<void> {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
runAsyncIterator();
|
runAsyncIterator();
|
||||||
const conn = await Deno.dial("tcp", "127.0.0.1:4500");
|
const conn = await Deno.dial("127.0.0.1:4500");
|
||||||
const buf = new Uint8Array(1024);
|
const buf = new Uint8Array(1024);
|
||||||
const readResult = await conn.read(buf);
|
const readResult = await conn.read(buf);
|
||||||
assertEquals(3, readResult);
|
assertEquals(3, readResult);
|
||||||
|
@ -107,7 +109,7 @@ testPerm({ net: true }, async function netListenAsyncIterator(): Promise<void> {
|
||||||
/* TODO Fix broken test.
|
/* TODO Fix broken test.
|
||||||
testPerm({ net: true }, async function netCloseReadSuccess() {
|
testPerm({ net: true }, async function netCloseReadSuccess() {
|
||||||
const addr = "127.0.0.1:4500";
|
const addr = "127.0.0.1:4500";
|
||||||
const listener = Deno.listen("tcp", addr);
|
const listener = Deno.listen(addr);
|
||||||
const closeDeferred = deferred();
|
const closeDeferred = deferred();
|
||||||
const closeReadDeferred = deferred();
|
const closeReadDeferred = deferred();
|
||||||
listener.accept().then(async conn => {
|
listener.accept().then(async conn => {
|
||||||
|
@ -122,7 +124,7 @@ testPerm({ net: true }, async function netCloseReadSuccess() {
|
||||||
conn.close();
|
conn.close();
|
||||||
closeDeferred.resolve();
|
closeDeferred.resolve();
|
||||||
});
|
});
|
||||||
const conn = await Deno.dial("tcp", addr);
|
const conn = await Deno.dial(addr);
|
||||||
conn.closeRead(); // closing read
|
conn.closeRead(); // closing read
|
||||||
closeReadDeferred.resolve();
|
closeReadDeferred.resolve();
|
||||||
const buf = new Uint8Array(1024);
|
const buf = new Uint8Array(1024);
|
||||||
|
@ -139,14 +141,14 @@ testPerm({ net: true }, async function netCloseReadSuccess() {
|
||||||
/* TODO Fix broken test.
|
/* TODO Fix broken test.
|
||||||
testPerm({ net: true }, async function netDoubleCloseRead() {
|
testPerm({ net: true }, async function netDoubleCloseRead() {
|
||||||
const addr = "127.0.0.1:4500";
|
const addr = "127.0.0.1:4500";
|
||||||
const listener = Deno.listen("tcp", addr);
|
const listener = Deno.listen(addr);
|
||||||
const closeDeferred = deferred();
|
const closeDeferred = deferred();
|
||||||
listener.accept().then(async conn => {
|
listener.accept().then(async conn => {
|
||||||
await conn.write(new Uint8Array([1, 2, 3]));
|
await conn.write(new Uint8Array([1, 2, 3]));
|
||||||
await closeDeferred.promise;
|
await closeDeferred.promise;
|
||||||
conn.close();
|
conn.close();
|
||||||
});
|
});
|
||||||
const conn = await Deno.dial("tcp", addr);
|
const conn = await Deno.dial(addr);
|
||||||
conn.closeRead(); // closing read
|
conn.closeRead(); // closing read
|
||||||
let err;
|
let err;
|
||||||
try {
|
try {
|
||||||
|
@ -167,14 +169,14 @@ testPerm({ net: true }, async function netDoubleCloseRead() {
|
||||||
/* TODO Fix broken test.
|
/* TODO Fix broken test.
|
||||||
testPerm({ net: true }, async function netCloseWriteSuccess() {
|
testPerm({ net: true }, async function netCloseWriteSuccess() {
|
||||||
const addr = "127.0.0.1:4500";
|
const addr = "127.0.0.1:4500";
|
||||||
const listener = Deno.listen("tcp", addr);
|
const listener = Deno.listen(addr);
|
||||||
const closeDeferred = deferred();
|
const closeDeferred = deferred();
|
||||||
listener.accept().then(async conn => {
|
listener.accept().then(async conn => {
|
||||||
await conn.write(new Uint8Array([1, 2, 3]));
|
await conn.write(new Uint8Array([1, 2, 3]));
|
||||||
await closeDeferred.promise;
|
await closeDeferred.promise;
|
||||||
conn.close();
|
conn.close();
|
||||||
});
|
});
|
||||||
const conn = await Deno.dial("tcp", addr);
|
const conn = await Deno.dial(addr);
|
||||||
conn.closeWrite(); // closing write
|
conn.closeWrite(); // closing write
|
||||||
const buf = new Uint8Array(1024);
|
const buf = new Uint8Array(1024);
|
||||||
// Check read not impacted
|
// Check read not impacted
|
||||||
|
@ -202,13 +204,13 @@ testPerm({ net: true }, async function netCloseWriteSuccess() {
|
||||||
/* TODO Fix broken test.
|
/* TODO Fix broken test.
|
||||||
testPerm({ net: true }, async function netDoubleCloseWrite() {
|
testPerm({ net: true }, async function netDoubleCloseWrite() {
|
||||||
const addr = "127.0.0.1:4500";
|
const addr = "127.0.0.1:4500";
|
||||||
const listener = Deno.listen("tcp", addr);
|
const listener = Deno.listen(addr);
|
||||||
const closeDeferred = deferred();
|
const closeDeferred = deferred();
|
||||||
listener.accept().then(async conn => {
|
listener.accept().then(async conn => {
|
||||||
await closeDeferred.promise;
|
await closeDeferred.promise;
|
||||||
conn.close();
|
conn.close();
|
||||||
});
|
});
|
||||||
const conn = await Deno.dial("tcp", addr);
|
const conn = await Deno.dial(addr);
|
||||||
conn.closeWrite(); // closing write
|
conn.closeWrite(); // closing write
|
||||||
let err;
|
let err;
|
||||||
try {
|
try {
|
||||||
|
|
|
@ -10,10 +10,8 @@ test(function resourcesStdio(): void {
|
||||||
});
|
});
|
||||||
|
|
||||||
testPerm({ net: true }, async function resourcesNet(): Promise<void> {
|
testPerm({ net: true }, async function resourcesNet(): Promise<void> {
|
||||||
const addr = "127.0.0.1:4501";
|
const listener = Deno.listen({ port: 4501 });
|
||||||
const listener = Deno.listen("tcp", addr);
|
const dialerConn = await Deno.dial({ port: 4501 });
|
||||||
|
|
||||||
const dialerConn = await Deno.dial("tcp", addr);
|
|
||||||
const listenerConn = await listener.accept();
|
const listenerConn = await listener.accept();
|
||||||
|
|
||||||
const res = Deno.resources();
|
const res = Deno.resources();
|
||||||
|
|
|
@ -211,8 +211,8 @@ def run_http(build_dir, new_data):
|
||||||
|
|
||||||
def bundle_benchmark(deno_exe):
|
def bundle_benchmark(deno_exe):
|
||||||
bundles = {
|
bundles = {
|
||||||
"file_server": "https://deno.land/std/http/file_server.ts",
|
"file_server": "./js/deps/https/deno.land/std/http/file_server.ts",
|
||||||
"gist": "https://deno.land/std/examples/gist.ts",
|
"gist": "./js/deps/https/deno.land/std/examples/gist.ts",
|
||||||
}
|
}
|
||||||
|
|
||||||
sizes = {}
|
sizes = {}
|
||||||
|
|
|
@ -16,13 +16,15 @@ const test: (args: string[]) => void = {
|
||||||
},
|
},
|
||||||
netListen(hosts: string[]): void {
|
netListen(hosts: string[]): void {
|
||||||
hosts.forEach(host => {
|
hosts.forEach(host => {
|
||||||
const listener = Deno.listen("tcp", host);
|
const [hostname, port] = host.split(":");
|
||||||
|
const listener = Deno.listen({ hostname, port: Number(port) });
|
||||||
listener.close();
|
listener.close();
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
async netDial(hosts: string[]): Promise<void> {
|
async netDial(hosts: string[]): Promise<void> {
|
||||||
for (const host of hosts) {
|
for (const host of hosts) {
|
||||||
const listener = await Deno.dial("tcp", host);
|
const [hostname, port] = host.split(":");
|
||||||
|
const listener = await Deno.dial({ hostname, port: Number(port) });
|
||||||
listener.close();
|
listener.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,8 @@
|
||||||
// https://github.com/denoland/deno/issues/726 is completed.
|
// https://github.com/denoland/deno/issues/726 is completed.
|
||||||
// Note: this is a keep-alive server.
|
// Note: this is a keep-alive server.
|
||||||
const addr = Deno.args[1] || "127.0.0.1:4500";
|
const addr = Deno.args[1] || "127.0.0.1:4500";
|
||||||
const listener = Deno.listen("tcp", addr);
|
const [hostname, port] = addr.split(":");
|
||||||
|
const listener = Deno.listen({ hostname, port: Number(port) });
|
||||||
const response = new TextEncoder().encode(
|
const response = new TextEncoder().encode(
|
||||||
"HTTP/1.1 200 OK\r\nContent-Length: 12\r\n\r\nHello World\n"
|
"HTTP/1.1 200 OK\r\nContent-Length: 12\r\n\r\nHello World\n"
|
||||||
);
|
);
|
||||||
|
|
|
@ -2,10 +2,16 @@
|
||||||
const addr = Deno.args[1] || "127.0.0.1:4500";
|
const addr = Deno.args[1] || "127.0.0.1:4500";
|
||||||
const originAddr = Deno.args[2] || "127.0.0.1:4501";
|
const originAddr = Deno.args[2] || "127.0.0.1:4501";
|
||||||
|
|
||||||
const listener = Deno.listen("tcp", addr);
|
const [hostname, port] = addr.split(":");
|
||||||
|
const [originHostname, originPort] = originAddr.split(":");
|
||||||
|
|
||||||
|
const listener = Deno.listen({ hostname, port: Number(port) });
|
||||||
|
|
||||||
async function handle(conn: Deno.Conn): Promise<void> {
|
async function handle(conn: Deno.Conn): Promise<void> {
|
||||||
const origin = await Deno.dial("tcp", originAddr);
|
const origin = await Deno.dial({
|
||||||
|
hostname: originHostname,
|
||||||
|
port: Number(originPort)
|
||||||
|
});
|
||||||
try {
|
try {
|
||||||
await Promise.all([Deno.copy(conn, origin), Deno.copy(origin, conn)]);
|
await Promise.all([Deno.copy(conn, origin), Deno.copy(origin, conn)]);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
|
|
@ -49,13 +49,7 @@ def deno_http(deno_exe):
|
||||||
"js/deps/https/deno.land/std/http/http_bench.ts", addr
|
"js/deps/https/deno.land/std/http/http_bench.ts", addr
|
||||||
]
|
]
|
||||||
print "http_benchmark testing DENO using net/http."
|
print "http_benchmark testing DENO using net/http."
|
||||||
return run(
|
return run(deno_cmd, addr)
|
||||||
deno_cmd,
|
|
||||||
addr,
|
|
||||||
merge_env={
|
|
||||||
# Load from //js/deps/https/deno.land/net/ submodule.
|
|
||||||
"DENO_DIR": os.path.join(util.root_path, "js")
|
|
||||||
})
|
|
||||||
|
|
||||||
|
|
||||||
def deno_tcp_proxy(deno_exe, hyper_hello_exe):
|
def deno_tcp_proxy(deno_exe, hyper_hello_exe):
|
||||||
|
@ -69,7 +63,6 @@ def deno_tcp_proxy(deno_exe, hyper_hello_exe):
|
||||||
return run(
|
return run(
|
||||||
deno_cmd,
|
deno_cmd,
|
||||||
addr,
|
addr,
|
||||||
merge_env={"DENO_DIR": os.path.join(util.root_path, "js")},
|
|
||||||
origin_cmd=http_proxy_origin(hyper_hello_exe, origin_addr))
|
origin_cmd=http_proxy_origin(hyper_hello_exe, origin_addr))
|
||||||
|
|
||||||
|
|
||||||
|
@ -84,7 +77,6 @@ def deno_http_proxy(deno_exe, hyper_hello_exe):
|
||||||
return run(
|
return run(
|
||||||
deno_cmd,
|
deno_cmd,
|
||||||
addr,
|
addr,
|
||||||
merge_env={"DENO_DIR": os.path.join(util.root_path, "js")},
|
|
||||||
origin_cmd=http_proxy_origin(hyper_hello_exe, origin_addr))
|
origin_cmd=http_proxy_origin(hyper_hello_exe, origin_addr))
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
||||||
const { args, listen, env, exit, makeTempDirSync, readFileSync, run } = Deno;
|
const { args, env, exit, makeTempDirSync, readFileSync, run } = Deno;
|
||||||
|
|
||||||
const firstCheckFailedMessage = "First check failed";
|
const firstCheckFailedMessage = "First check failed";
|
||||||
|
|
||||||
|
@ -31,11 +31,11 @@ const test = {
|
||||||
},
|
},
|
||||||
needsNet(): void {
|
needsNet(): void {
|
||||||
try {
|
try {
|
||||||
listen("tcp", "127.0.0.1:4540");
|
Deno.listen({ hostname: "127.0.0.1", port: 4540 });
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log(firstCheckFailedMessage);
|
console.log(firstCheckFailedMessage);
|
||||||
}
|
}
|
||||||
listen("tcp", "127.0.0.1:4541");
|
Deno.listen({ hostname: "127.0.0.1", port: 4541 });
|
||||||
},
|
},
|
||||||
needsRun(): void {
|
needsRun(): void {
|
||||||
try {
|
try {
|
||||||
|
|
Loading…
Reference in a new issue