summaryrefslogtreecommitdiff
path: root/js
diff options
context:
space:
mode:
Diffstat (limited to 'js')
-rw-r--r--js/net.ts24
-rw-r--r--js/net_test.ts7
2 files changed, 23 insertions, 8 deletions
diff --git a/js/net.ts b/js/net.ts
index b478ae613..809dd4092 100644
--- a/js/net.ts
+++ b/js/net.ts
@@ -9,8 +9,10 @@ export type Network = "tcp";
// TODO support other types:
// export type Network = "tcp" | "tcp4" | "tcp6" | "unix" | "unixpacket";
-// TODO Support finding network from Addr, see https://golang.org/pkg/net/#Addr
-export type Addr = string;
+export interface Addr {
+ network: Network;
+ address: string;
+}
/** A Listener is a generic network listener for stream-oriented protocols. */
export interface Listener extends AsyncIterator<Conn> {
@@ -75,12 +77,15 @@ class ConnImpl implements Conn {
}
class ListenerImpl implements Listener {
- constructor(readonly rid: number) {}
+ constructor(
+ readonly rid: number,
+ private network: Network,
+ private localAddr: string
+ ) {}
async accept(): Promise<Conn> {
const res = await sendAsync(dispatch.OP_ACCEPT, { rid: this.rid });
- // 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);
}
close(): void {
@@ -88,7 +93,10 @@ class ListenerImpl implements Listener {
}
addr(): Addr {
- return notImplemented();
+ return {
+ network: this.network,
+ address: this.localAddr
+ };
}
async next(): Promise<IteratorResult<Conn>> {
@@ -136,8 +144,8 @@ export interface Conn extends Reader, Writer, Closer {
* See `dial()` for a description of the network and address parameters.
*/
export function listen(network: Network, address: string): Listener {
- const rid = sendSync(dispatch.OP_LISTEN, { network, address });
- return new ListenerImpl(rid);
+ const res = sendSync(dispatch.OP_LISTEN, { network, address });
+ return new ListenerImpl(res.rid, network, res.localAddr);
}
/** Dial connects to the address on the named network.
diff --git a/js/net_test.ts b/js/net_test.ts
index 10652136c..23387001f 100644
--- a/js/net_test.ts
+++ b/js/net_test.ts
@@ -3,6 +3,9 @@ import { testPerm, assert, assertEquals } from "./test_util.ts";
testPerm({ net: true }, function netListenClose(): void {
const listener = Deno.listen("tcp", "127.0.0.1:4500");
+ const addr = listener.addr();
+ assertEquals(addr.network, "tcp");
+ assertEquals(addr.address, "127.0.0.1:4500");
listener.close();
});
@@ -46,11 +49,15 @@ testPerm({ net: true }, async function netDialListen(): Promise<void> {
const listener = Deno.listen("tcp", ":4500");
listener.accept().then(
async (conn): Promise<void> => {
+ assert(conn.remoteAddr != null);
+ assertEquals(conn.localAddr, "127.0.0.1:4500");
await conn.write(new Uint8Array([1, 2, 3]));
conn.close();
}
);
const conn = await Deno.dial("tcp", "127.0.0.1:4500");
+ assertEquals(conn.remoteAddr, "127.0.0.1:4500");
+ assert(conn.localAddr != null);
const buf = new Uint8Array(1024);
const readResult = await conn.read(buf);
assertEquals(3, readResult);