Skip to content

Commit a997816

Browse files
authored
[w3c-direct-sockets] Add type definitions for direct-sockets (DefinitelyTyped#74151)
1 parent 27e8b68 commit a997816

5 files changed

Lines changed: 302 additions & 0 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
*
2+
!**/*.d.ts
3+
!**/*.d.cts
4+
!**/*.d.mts
5+
!**/*.d.*.ts
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/**
2+
* @see https://github.com/WICG/direct-sockets/blob/main/docs/explainer.md
3+
*/
4+
5+
export class UDPSocket {
6+
constructor(options: UDPSocketOptions);
7+
readonly opened: Promise<UDPSocketOpenInfo>;
8+
readonly closed: Promise<undefined>;
9+
close(): Promise<undefined>;
10+
}
11+
12+
export interface UDPMessage {
13+
data?: BufferSource;
14+
remoteAddress?: string;
15+
remotePort?: number;
16+
dnsQueryType?: SocketDnsQueryType;
17+
}
18+
19+
export class TCPSocket {
20+
constructor(remoteAddress: string, remotePort: number, options?: TCPSocketOptions);
21+
readonly opened: Promise<TCPSocketOpenInfo>;
22+
readonly closed: Promise<undefined>;
23+
close(): Promise<undefined>;
24+
}
25+
26+
export class TCPServerSocket {
27+
constructor(localAddress: string, options?: TCPServerSocketOptions);
28+
readonly opened: Promise<TCPServerSocketOpenInfo>;
29+
readonly closed: Promise<undefined>;
30+
close(): Promise<undefined>;
31+
}
32+
33+
export type SocketDnsQueryType =
34+
| "ipv4"
35+
| "ipv6";
36+
37+
export interface SocketOptions {
38+
sendBufferSize?: number;
39+
receiveBufferSize?: number;
40+
}
41+
42+
export interface TCPSocketOptions extends SocketOptions {
43+
/** @default false */
44+
noDelay?: boolean;
45+
keepAliveDelay?: number;
46+
dnsQueryType?: SocketDnsQueryType;
47+
}
48+
49+
export interface UDPSocketOptions extends SocketOptions {
50+
remoteAddress?: string;
51+
remotePort?: number;
52+
localAddress?: string;
53+
localPort?: number;
54+
dnsQueryType?: SocketDnsQueryType;
55+
ipv6Only?: boolean;
56+
multicastAllowAddressSharing?: boolean;
57+
multicastTimeToLive?: number;
58+
multicastLoopback?: boolean;
59+
}
60+
61+
export interface TCPServerSocketOptions {
62+
localPort?: number;
63+
backlog?: number;
64+
ipv6Only?: boolean;
65+
}
66+
67+
export interface SocketOpenInfo {
68+
readable?: ReadableStream;
69+
writable?: WritableStream;
70+
remoteAddress?: string;
71+
remotePort?: number;
72+
localAddress?: string;
73+
localPort?: number;
74+
}
75+
76+
export type TCPSocketOpenInfo = SocketOpenInfo;
77+
78+
export interface UDPSocketOpenInfo extends SocketOpenInfo {
79+
multicastController?: MulticastController;
80+
}
81+
82+
export interface TCPServerSocketOpenInfo {
83+
readable?: ReadableStream;
84+
localAddress?: string;
85+
localPort?: number;
86+
}
87+
88+
export interface MulticastController {
89+
joinGroup(ipAddress: string): Promise<undefined>;
90+
leaveGroup(ipAddress: string): Promise<undefined>;
91+
readonly joinedGroups: readonly string[];
92+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"private": true,
3+
"nonNpm": true,
4+
"nonNpmDescription": "Implementation of Isolated Web App (IWA) APIs resides in Chromium.",
5+
"name": "@types/w3c-direct-sockets",
6+
"version": "0.0.9999",
7+
"projects": [
8+
"https://github.com/WICG/isolated-web-apps"
9+
],
10+
"devDependencies": {
11+
"@types/web": "*",
12+
"@types/w3c-direct-sockets": "workspace:."
13+
},
14+
"owners": [
15+
{
16+
"name": "Paulina Gacek",
17+
"githubUsername": "paulinagacek"
18+
},
19+
{
20+
"name": "Andrew Rayskiy",
21+
"githubUsername": "GrapeGreen"
22+
},
23+
{
24+
"name": "Simon Hangl",
25+
"githubUsername": "shangl"
26+
}
27+
]
28+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"compilerOptions": {
3+
"module": "node16",
4+
"lib": [
5+
"es6",
6+
"dom"
7+
],
8+
"noImplicitAny": true,
9+
"noImplicitThis": true,
10+
"strictFunctionTypes": true,
11+
"strictNullChecks": true,
12+
"types": [],
13+
"noEmit": true,
14+
"forceConsistentCasingInFileNames": true,
15+
"paths": {
16+
"w3c-direct-sockets": [
17+
"./index.d.ts"
18+
]
19+
}
20+
},
21+
"files": [
22+
"index.d.ts",
23+
"w3c-direct-sockets-tests.ts"
24+
]
25+
}
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
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

Comments
 (0)