|
| 1 | +import { |
| 2 | + MulticastController, |
| 3 | + SocketDnsQueryType, |
| 4 | + TCPServerSocket, |
| 5 | + TCPServerSocketOpenInfo, |
| 6 | + TCPServerSocketOptions, |
| 7 | + TCPSocket, |
| 8 | + TCPSocketOpenInfo, |
| 9 | + TCPSocketOptions, |
| 10 | + UDPMessage, |
| 11 | + UDPSocket, |
| 12 | + UDPSocketOpenInfo, |
| 13 | + UDPSocketOptions, |
| 14 | +} from "w3c-direct-sockets"; |
| 15 | + |
| 16 | +async function testDirectSockets() { |
| 17 | + const remoteAddress = "192.168.1.1"; |
| 18 | + const localAddress = "0.0.0.0"; |
| 19 | + const remotePort = 8080; |
| 20 | + const localPort = 3000; |
| 21 | + |
| 22 | + // -------------------------------------------------------------------------------- |
| 23 | + // SocketDnsQueryType (Enum Test) |
| 24 | + // -------------------------------------------------------------------------------- |
| 25 | + const dnsTypeIpv4: SocketDnsQueryType = "ipv4"; |
| 26 | + // $ExpectType "ipv4" |
| 27 | + dnsTypeIpv4; |
| 28 | + |
| 29 | + // $ExpectType "ipv6" |
| 30 | + const dnsTypeIpv6: SocketDnsQueryType = "ipv6"; |
| 31 | + |
| 32 | + // -------------------------------------------------------------------------------- |
| 33 | + // UDPSocket |
| 34 | + // -------------------------------------------------------------------------------- |
| 35 | + const udpOptions: UDPSocketOptions = { |
| 36 | + remoteAddress: remoteAddress, |
| 37 | + remotePort: remotePort, |
| 38 | + localAddress: localAddress, |
| 39 | + localPort: localPort, |
| 40 | + dnsQueryType: "ipv4", |
| 41 | + ipv6Only: false, |
| 42 | + sendBufferSize: 1024, |
| 43 | + multicastAllowAddressSharing: true, |
| 44 | + multicastTimeToLive: 32, |
| 45 | + multicastLoopback: true, |
| 46 | + }; |
| 47 | + |
| 48 | + const udpSocket = new UDPSocket(udpOptions); |
| 49 | + // $ExpectType UDPSocket |
| 50 | + udpSocket; |
| 51 | + |
| 52 | + // $ExpectType Promise<UDPSocketOpenInfo> |
| 53 | + udpSocket.opened; |
| 54 | + |
| 55 | + // $ExpectType Promise<undefined> |
| 56 | + udpSocket.closed; |
| 57 | + |
| 58 | + // $ExpectType Promise<undefined> |
| 59 | + udpSocket.close(); |
| 60 | + |
| 61 | + // Cast to 'any' or 'UDPMessage' to avoid "missing properties" error during test compilation |
| 62 | + // because UDPMessage likely requires 'data' which {} does not have. |
| 63 | + const udpMessage = {} as UDPMessage; |
| 64 | + |
| 65 | + // $ExpectType BufferSource | undefined |
| 66 | + udpMessage.data; |
| 67 | + // $ExpectType string | undefined |
| 68 | + udpMessage.remoteAddress; |
| 69 | + |
| 70 | + // -------------------------------------------------------------------------------- |
| 71 | + // TCPSocket |
| 72 | + // -------------------------------------------------------------------------------- |
| 73 | + const tcpOptions: TCPSocketOptions = { |
| 74 | + noDelay: true, |
| 75 | + keepAliveDelay: 300, |
| 76 | + dnsQueryType: "ipv6", |
| 77 | + receiveBufferSize: 4096, |
| 78 | + }; |
| 79 | + |
| 80 | + const tcpSocket = new TCPSocket(remoteAddress, remotePort, tcpOptions); |
| 81 | + // $ExpectType TCPSocket |
| 82 | + tcpSocket; |
| 83 | + |
| 84 | + // $ExpectType Promise<SocketOpenInfo> |
| 85 | + tcpSocket.opened; |
| 86 | + |
| 87 | + // $ExpectType Promise<undefined> |
| 88 | + tcpSocket.closed; |
| 89 | + |
| 90 | + // $ExpectType Promise<undefined> |
| 91 | + tcpSocket.close(); |
| 92 | + |
| 93 | + // -------------------------------------------------------------------------------- |
| 94 | + // TCPServerSocket |
| 95 | + // -------------------------------------------------------------------------------- |
| 96 | + const tcpServerOptions: TCPServerSocketOptions = { |
| 97 | + localPort: 0, |
| 98 | + backlog: 5, |
| 99 | + ipv6Only: false, |
| 100 | + }; |
| 101 | + |
| 102 | + // Constructor |
| 103 | + const tcpServerSocket = new TCPServerSocket(localAddress, tcpServerOptions); |
| 104 | + // $ExpectType TCPServerSocket |
| 105 | + tcpServerSocket; |
| 106 | + |
| 107 | + // $ExpectType Promise<TCPServerSocketOpenInfo> |
| 108 | + tcpServerSocket.opened; |
| 109 | + |
| 110 | + // $ExpectType Promise<undefined> |
| 111 | + tcpServerSocket.closed; |
| 112 | + |
| 113 | + // $ExpectType Promise<undefined> |
| 114 | + tcpServerSocket.close(); |
| 115 | + |
| 116 | + // -------------------------------------------------------------------------------- |
| 117 | + // Open Info Structures |
| 118 | + // -------------------------------------------------------------------------------- |
| 119 | + |
| 120 | + const udpOpenInfo: UDPSocketOpenInfo = {}; |
| 121 | + // $ExpectType MulticastController | undefined |
| 122 | + udpOpenInfo.multicastController; |
| 123 | + // $ExpectType ReadableStream<any> | undefined |
| 124 | + udpOpenInfo.readable; |
| 125 | + |
| 126 | + const tcpOpenInfo: TCPSocketOpenInfo = {}; |
| 127 | + // @ts-expect-error |
| 128 | + tcpOpenInfo.multicastController; |
| 129 | + // $ExpectType WritableStream<any> | undefined |
| 130 | + tcpOpenInfo.writable; |
| 131 | + |
| 132 | + const tcpServerOpenInfo: TCPServerSocketOpenInfo = {}; |
| 133 | + // $ExpectType ReadableStream<any> | undefined |
| 134 | + tcpServerOpenInfo.readable; |
| 135 | + // @ts-expect-error |
| 136 | + tcpServerOpenInfo.writable; |
| 137 | + |
| 138 | + // -------------------------------------------------------------------------------- |
| 139 | + // MulticastController |
| 140 | + // -------------------------------------------------------------------------------- |
| 141 | + |
| 142 | + const multicastController = {} as MulticastController; |
| 143 | + |
| 144 | + // $ExpectType readonly string[] |
| 145 | + multicastController.joinedGroups; |
| 146 | + |
| 147 | + // $ExpectType Promise<undefined> |
| 148 | + multicastController.joinGroup(remoteAddress); |
| 149 | + |
| 150 | + // $ExpectType Promise<undefined> |
| 151 | + multicastController.leaveGroup(remoteAddress); |
| 152 | +} |
0 commit comments