summaryrefslogtreecommitdiff
path: root/js/net.ts
diff options
context:
space:
mode:
authorBartek IwaƄczuk <biwanczuk@gmail.com>2019-08-27 17:35:32 +0200
committerRyan Dahl <ry@tinyclouds.org>2019-08-27 11:35:32 -0400
commit20739a8111658c088e291503866110117e117792 (patch)
tree9147e731ffb014e32fa29656cd6d24417aba616d /js/net.ts
parentb6a4ec7d163810d52750f04ec2073b13f8943991 (diff)
feat: implement Addr interface (#2821)
Diffstat (limited to 'js/net.ts')
-rw-r--r--js/net.ts24
1 files changed, 16 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.