summaryrefslogtreecommitdiff
path: root/cli
diff options
context:
space:
mode:
Diffstat (limited to 'cli')
-rw-r--r--cli/tests/unit/net_test.ts135
-rw-r--r--cli/tsc/dts/lib.deno.unstable.d.ts49
2 files changed, 182 insertions, 2 deletions
diff --git a/cli/tests/unit/net_test.ts b/cli/tests/unit/net_test.ts
index 652d07d7d..935a6f846 100644
--- a/cli/tests/unit/net_test.ts
+++ b/cli/tests/unit/net_test.ts
@@ -426,6 +426,141 @@ Deno.test(
);
Deno.test(
+ { permissions: { net: true }, ignore: true },
+ async function netUdpMulticastV4() {
+ const listener = Deno.listenDatagram({
+ hostname: "0.0.0.0",
+ port: 5353,
+ transport: "udp",
+ reuseAddress: true,
+ });
+
+ const membership = await listener.joinMulticastV4(
+ "224.0.0.251",
+ "127.0.0.1",
+ );
+
+ membership.setLoopback(true);
+ membership.setLoopback(false);
+ membership.setTTL(50);
+ membership.leave();
+ listener.close();
+ },
+);
+
+Deno.test(
+ { permissions: { net: true }, ignore: true },
+ async function netUdpMulticastV6() {
+ const listener = Deno.listenDatagram({
+ hostname: "::",
+ port: 5353,
+ transport: "udp",
+ reuseAddress: true,
+ });
+
+ const membership = await listener.joinMulticastV6(
+ "ff02::fb",
+ 1,
+ );
+
+ membership.setLoopback(true);
+ membership.setLoopback(false);
+ membership.leave();
+ listener.close();
+ },
+);
+
+Deno.test(
+ { permissions: { net: true }, ignore: true },
+ async function netUdpSendReceiveMulticastv4() {
+ const alice = Deno.listenDatagram({
+ hostname: "0.0.0.0",
+ port: 5353,
+ transport: "udp",
+ reuseAddress: true,
+ loopback: true,
+ });
+
+ const bob = Deno.listenDatagram({
+ hostname: "0.0.0.0",
+ port: 5353,
+ transport: "udp",
+ reuseAddress: true,
+ });
+
+ const aliceMembership = await alice.joinMulticastV4(
+ "224.0.0.1",
+ "0.0.0.0",
+ );
+
+ const bobMembership = await bob.joinMulticastV4("224.0.0.1", "0.0.0.0");
+
+ const sent = new Uint8Array([1, 2, 3]);
+
+ await alice.send(sent, {
+ hostname: "224.0.0.1",
+ port: 5353,
+ transport: "udp",
+ });
+
+ const [recvd, remote] = await bob.receive();
+
+ assert(remote.transport === "udp");
+ assertEquals(remote.port, 5353);
+ assertEquals(recvd.length, 3);
+ assertEquals(1, recvd[0]);
+ assertEquals(2, recvd[1]);
+ assertEquals(3, recvd[2]);
+
+ aliceMembership.leave();
+ bobMembership.leave();
+
+ alice.close();
+ bob.close();
+ },
+);
+
+Deno.test(
+ { permissions: { net: true }, ignore: true },
+ async function netUdpMulticastLoopbackOption() {
+ // Must bind sender to an address that can send to the broadcast address on MacOS.
+ // Macos will give us error 49 when sending the broadcast packet if we omit hostname here.
+ const listener = Deno.listenDatagram({
+ port: 5353,
+ transport: "udp",
+ hostname: "0.0.0.0",
+ loopback: true,
+ reuseAddress: true,
+ });
+
+ const membership = await listener.joinMulticastV4(
+ "224.0.0.1",
+ "0.0.0.0",
+ );
+
+ // await membership.setLoopback(true);
+
+ const sent = new Uint8Array([1, 2, 3]);
+ const byteLength = await listener.send(sent, {
+ hostname: "224.0.0.1",
+ port: 5353,
+ transport: "udp",
+ });
+
+ assertEquals(byteLength, 3);
+ const [recvd, remote] = await listener.receive();
+ assert(remote.transport === "udp");
+ assertEquals(remote.port, 5353);
+ assertEquals(recvd.length, 3);
+ assertEquals(1, recvd[0]);
+ assertEquals(2, recvd[1]);
+ assertEquals(3, recvd[2]);
+ membership.leave();
+ listener.close();
+ },
+);
+
+Deno.test(
{ permissions: { net: true } },
async function netUdpConcurrentSendReceive() {
const socket = Deno.listenDatagram({ port: 3500, transport: "udp" });
diff --git a/cli/tsc/dts/lib.deno.unstable.d.ts b/cli/tsc/dts/lib.deno.unstable.d.ts
index ed7e682f1..62426ca35 100644
--- a/cli/tsc/dts/lib.deno.unstable.d.ts
+++ b/cli/tsc/dts/lib.deno.unstable.d.ts
@@ -163,7 +163,7 @@ declare namespace Deno {
*/
type ToNativeResultType<T extends NativeResultType = NativeResultType> =
T extends NativeStructType ? BufferSource
- : ToNativeResultTypeMap[Exclude<T, NativeStructType>];
+ : ToNativeResultTypeMap[Exclude<T, NativeStructType>];
/** **UNSTABLE**: New API, yet to be vetted.
*
@@ -225,7 +225,7 @@ declare namespace Deno {
*/
type FromNativeResultType<T extends NativeResultType = NativeResultType> =
T extends NativeStructType ? Uint8Array
- : FromNativeResultTypeMap[Exclude<T, NativeStructType>];
+ : FromNativeResultTypeMap[Exclude<T, NativeStructType>];
/** **UNSTABLE**: New API, yet to be vetted.
*
@@ -852,11 +852,51 @@ declare namespace Deno {
/** **UNSTABLE**: New API, yet to be vetted.
*
+ * Represents membership of a IPv4 multicast group.
+ *
+ * @category Network
+ */
+ interface MulticastV4Membership {
+ /** Leaves the multicast group. */
+ leave: () => Promise<void>;
+ /** Sets the multicast loopback option. If enabled, multicast packets will be looped back to the local socket. */
+ setLoopback: (loopback: boolean) => Promise<void>;
+ /** Sets the time-to-live of outgoing multicast packets for this socket. */
+ setTTL: (ttl: number) => Promise<void>;
+ }
+
+ /** **UNSTABLE**: New API, yet to be vetted.
+ *
+ * Represents membership of a IPv6 multicast group.
+ *
+ * @category Network
+ */
+ interface MulticastV6Membership {
+ /** Leaves the multicast group. */
+ leave: () => Promise<void>;
+ /** Sets the multicast loopback option. If enabled, multicast packets will be looped back to the local socket. */
+ setLoopback: (loopback: boolean) => Promise<void>;
+ }
+
+ /** **UNSTABLE**: New API, yet to be vetted.
+ *
* A generic transport listener for message-oriented protocols.
*
* @category Network
*/
export interface DatagramConn extends AsyncIterable<[Uint8Array, Addr]> {
+ /** Joins an IPv4 multicast group. */
+ joinMulticastV4(
+ address: string,
+ networkInterface: string,
+ ): Promise<MulticastV4Membership>;
+
+ /** Joins an IPv6 multicast group. */
+ joinMulticastV6(
+ address: string,
+ networkInterface: number,
+ ): Promise<MulticastV6Membership>;
+
/** Waits for and resolves to the next message to the instance.
*
* Messages are received in the format of a tuple containing the data array
@@ -918,6 +958,11 @@ declare namespace Deno {
*
* @default {false} */
reuseAddress?: boolean;
+
+ /** When `true`, sent multicast packets will be looped back to the local socket.
+ *
+ * @default {false} */
+ loopback?: boolean;
}
/** **UNSTABLE**: New API, yet to be vetted.