@webvpn/client 0.0.0-stage → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,22 @@
1
+ import type { ClosedBy, Context, Opened } from './context';
2
+ import type { Address } from './protocol';
3
+ import type { TcpSocket } from './tcp';
4
+ /**
5
+ * A port the relay listens on for the client, from `Session.tcpListen`. Each connection the relay accepts comes on
6
+ * `connections`, in order; one the relay turned away (`AcceptRefused`) is not told. The listener stops at its own
7
+ * `close()`, at the relay's `Closed` notice, or when the session ends. Then `connections` ends, `closed` resolves with
8
+ * which of the three it was, and the client closes every connection not handed out yet, as well as any the relay
9
+ * accepts after.
10
+ */
11
+ export type TcpListener = {
12
+ /** The listener's id in its session. */
13
+ id: number;
14
+ /** The bound address, with the port the relay took for port 0. */
15
+ local: Address;
16
+ connections: ReadableStream<TcpSocket>;
17
+ /** Resolves once the listener stopped, with who stopped it. Never rejects. */
18
+ closed: Promise<ClosedBy>;
19
+ /** Stops the listener at once and closes it at the relay. Rejects with the relay's refusal while the session lasts. */
20
+ close: () => Promise<void>;
21
+ };
22
+ export declare const tcpListener: (context: Context, { socket, local }: Opened) => TcpListener;
@@ -0,0 +1,62 @@
1
+ /** What a relay offers, as the bits of a hello's `features`. A client sends a request only when its bit is set. */
2
+ export declare const Feature: {
3
+ /** `tcpListen` and `tcpUnlisten`. */
4
+ readonly LISTEN: number;
5
+ /** `dnsQuery` and `tcpConnectName`. */
6
+ readonly DNS: number;
7
+ /** `auth` and `authClear`. */
8
+ readonly AUTH: number;
9
+ /** Socket requests are answered `AUTH_REQUIRED` until an `auth` succeeded. */
10
+ readonly AUTH_REQUIRED: number;
11
+ };
12
+ /** Why a request failed, as an `error` reply carries it. A code not named here is treated as `OTHER`. */
13
+ export declare const ErrorCode: {
14
+ /** The relay does not know the request or the option, or has the feature off. */
15
+ readonly UNSUPPORTED: 1;
16
+ /** The relay's policy refuses this target, port or option. */
17
+ readonly REFUSED: 2;
18
+ /** A limit is reached; the same request may pass later. */
19
+ readonly CAPACITY: 3;
20
+ /** The socket has too many requests waiting; nothing was applied. */
21
+ readonly BUSY: 4;
22
+ readonly AUTH_REQUIRED: 5;
23
+ /** The credential was not accepted; the session is as it was. */
24
+ readonly AUTH_FAILED: 6;
25
+ readonly UNKNOWN_SOCKET: 7;
26
+ /** The relay could not open the socket's stream in time: the session has no room for more streams, and a new session has. */
27
+ readonly NO_STREAM: 8;
28
+ readonly CONNECTION_REFUSED: 16;
29
+ readonly TIMED_OUT: 17;
30
+ readonly HOST_UNREACHABLE: 18;
31
+ readonly NETWORK_UNREACHABLE: 19;
32
+ readonly CONNECTION_RESET: 20;
33
+ readonly ADDRESS_IN_USE: 21;
34
+ readonly ADDRESS_NOT_AVAILABLE: 22;
35
+ /** The name has no address. */
36
+ readonly NAME_NOT_FOUND: 23;
37
+ /** The resolver did not answer. */
38
+ readonly LOOKUP_FAILED: 24;
39
+ /** The message says what happened. */
40
+ readonly OTHER: 65535;
41
+ };
42
+ /** The code a session is closed with. */
43
+ export declare const CloseCode: {
44
+ readonly NORMAL: 0;
45
+ /** A frame could not be read, or setup did not finish in time. */
46
+ readonly PROTOCOL_ERROR: 1;
47
+ readonly VERSION_MISMATCH: 2;
48
+ readonly SESSION_LIMIT: 3;
49
+ /** The relay is stopping; another session may be opened at once. */
50
+ readonly RESTARTING: 4;
51
+ /** Never sent: the carrier ended with neither a `bye` nor a close code, as a lost connection does. */
52
+ readonly LOST: 5;
53
+ };
54
+ /** The WebSocket close code that carries a session close code. */
55
+ export declare const websocketCloseCode: (code: number) => number;
56
+ /** The code the relay resets its side of a socket's stream with. */
57
+ export declare const ResetCode: {
58
+ /** The client closed the socket. */
59
+ readonly CLOSED: 0;
60
+ /** The peer reset the connection. */
61
+ readonly PEER_RESET: 1;
62
+ };
@@ -0,0 +1,204 @@
1
+ import type { TcpOption, UdpOption } from './options';
2
+ import type { Address } from './wire';
3
+ /** The version this package speaks. While the major is 0, peers need the same minor. */
4
+ export declare const VERSION: {
5
+ readonly major: 0;
6
+ readonly minor: 1;
7
+ };
8
+ /** The longest frame body a relay reads from a client. */
9
+ export declare const MAX_CLIENT_FRAME_BYTES = 4096;
10
+ /** The longest frame body a client reads from a relay; a whole DNS answer fits. */
11
+ export declare const MAX_RELAY_FRAME_BYTES: number;
12
+ /** The longest agent a hello carries; a longer one is cut when it is written. */
13
+ export declare const MAX_AGENT_BYTES = 255;
14
+ /** The first frame each side writes: the relay first, then the client. */
15
+ export type Hello = {
16
+ type: 'hello';
17
+ major: number;
18
+ minor: number;
19
+ /** Bits of `Feature`. A client sends 0. */
20
+ features: number;
21
+ /** Names the implementation for a person reading a log. Nothing may branch on it. */
22
+ agent: string;
23
+ };
24
+ /** A hello at this package's version. */
25
+ export declare const hello: (features: number, agent: string) => Hello;
26
+ /** Whether this package can speak to the peer that sent `peer`. */
27
+ export declare const compatible: (peer: Hello) => boolean;
28
+ /** A control frame a client sends. `id` names the request in its reply. */
29
+ export type ClientPacket = Hello
30
+ /** Presents a credential, whose bytes the relay's operator defines. Answered `ok`. */
31
+ | {
32
+ type: 'auth';
33
+ id: number;
34
+ credential: Uint8Array;
35
+ }
36
+ /** Returns the session to no identity. Answered `ok`. */
37
+ | {
38
+ type: 'authClear';
39
+ id: number;
40
+ }
41
+ /** Answered `socket`. */
42
+ | {
43
+ type: 'tcpConnect';
44
+ id: number;
45
+ remote: Address;
46
+ }
47
+ /** Connects to the first A record the relay's resolver answers for `query`, a whole DNS message. Answered `socket`. */
48
+ | {
49
+ type: 'tcpConnectName';
50
+ id: number;
51
+ port: number;
52
+ query: Uint8Array;
53
+ }
54
+ /** Ends the socket in order. Answered `ok`, also for a socket that is gone. */
55
+ | {
56
+ type: 'tcpClose';
57
+ id: number;
58
+ socket: number;
59
+ }
60
+ /** Ends the socket with a reset toward its peer. Answered `ok`, also for a socket that is gone. */
61
+ | {
62
+ type: 'tcpReset';
63
+ id: number;
64
+ socket: number;
65
+ } | {
66
+ type: 'tcpSetOption';
67
+ id: number;
68
+ socket: number;
69
+ option: TcpOption;
70
+ }
71
+ /** Port 0 takes a free port. Answered `socket` with the bound address and no remote. */
72
+ | {
73
+ type: 'tcpListen';
74
+ id: number;
75
+ local: Address;
76
+ } | {
77
+ type: 'tcpUnlisten';
78
+ id: number;
79
+ socket: number;
80
+ }
81
+ /** Port 0 takes a free port. Answered `socket` with the bound address and no remote. */
82
+ | {
83
+ type: 'udpBind';
84
+ id: number;
85
+ local: Address;
86
+ } | {
87
+ type: 'udpClose';
88
+ id: number;
89
+ socket: number;
90
+ } | {
91
+ type: 'udpConnect';
92
+ id: number;
93
+ socket: number;
94
+ remote: Address;
95
+ } | {
96
+ type: 'udpDisconnect';
97
+ id: number;
98
+ socket: number;
99
+ } | {
100
+ type: 'udpSetOption';
101
+ id: number;
102
+ socket: number;
103
+ option: UdpOption;
104
+ }
105
+ /** `query` is a whole DNS message. Answered `dnsAnswer`. */
106
+ | {
107
+ type: 'dnsQuery';
108
+ id: number;
109
+ query: Uint8Array;
110
+ }
111
+ /** A type this package does not name. A relay answers `id`, when the body held one, with `UNSUPPORTED`. */
112
+ | {
113
+ type: 'unknown';
114
+ kind: number;
115
+ id?: number;
116
+ };
117
+ /** A control frame a relay sends: a reply, which carries the request's `id`, or a notice. */
118
+ export type RelayPacket = Hello
119
+ /** The request was done and has nothing to return. */
120
+ | {
121
+ type: 'ok';
122
+ id: number;
123
+ }
124
+ /** The request was not done. `code` is an `ErrorCode`; `message` is for a person. */
125
+ | {
126
+ type: 'error';
127
+ id: number;
128
+ code: number;
129
+ message: string;
130
+ }
131
+ /** The socket a connect, a listen or a bind opened. `remote` is the peer of a connect and absent otherwise. */
132
+ | {
133
+ type: 'socket';
134
+ id: number;
135
+ socket: number;
136
+ local: Address;
137
+ remote?: Address;
138
+ }
139
+ /** The resolver's whole DNS message. */
140
+ | {
141
+ type: 'dnsAnswer';
142
+ id: number;
143
+ answer: Uint8Array;
144
+ }
145
+ /** A listener accepted a connection, which is socket `socket` from now on. */
146
+ | {
147
+ type: 'accepted';
148
+ listener: number;
149
+ socket: number;
150
+ local: Address;
151
+ remote: Address;
152
+ }
153
+ /** A listener had a connection the relay turned away. */
154
+ | {
155
+ type: 'acceptRefused';
156
+ listener: number;
157
+ code: number;
158
+ remote: Address;
159
+ }
160
+ /** The relay ended a listener or a UDP socket on its own, and `code` says why. The socket is gone. */
161
+ | {
162
+ type: 'closed';
163
+ socket: number;
164
+ code: number;
165
+ }
166
+ /** The relay is about to stop: this session keeps its sockets, and new ones belong on a new session. */
167
+ | {
168
+ type: 'draining';
169
+ }
170
+ /**
171
+ * The relay closes the session on its own right after this frame, the last on the control stream. `code` is the
172
+ * `CloseCode` it closes with and `reason` is for a person. The session's code comes from it, since a carrier may
173
+ * not show its own close to the page.
174
+ */
175
+ | {
176
+ type: 'bye';
177
+ code: number;
178
+ reason: string;
179
+ }
180
+ /** A type this package does not name, which a client skips. */
181
+ | {
182
+ type: 'unknown';
183
+ kind: number;
184
+ };
185
+ /** The packet as one frame. */
186
+ export declare const encodeClientPacket: (packet: ClientPacket) => Uint8Array<ArrayBufferLike>;
187
+ /**
188
+ * Reads one packet off the front of `bytes`: the packet and how many bytes it took, or `undefined` until a whole
189
+ * frame is there. The packet holds copies, so `bytes` may be reused.
190
+ */
191
+ export declare const decodeClientPacket: (bytes: Uint8Array) => {
192
+ packet: ClientPacket;
193
+ taken: number;
194
+ } | undefined;
195
+ /** The packet as one frame. */
196
+ export declare const encodeRelayPacket: (packet: RelayPacket) => Uint8Array<ArrayBufferLike>;
197
+ /**
198
+ * Reads one packet off the front of `bytes`: the packet and how many bytes it took, or `undefined` until a whole
199
+ * frame is there. The packet holds copies, so `bytes` may be reused.
200
+ */
201
+ export declare const decodeRelayPacket: (bytes: Uint8Array) => {
202
+ packet: RelayPacket;
203
+ taken: number;
204
+ } | undefined;
@@ -0,0 +1,11 @@
1
+ /**
2
+ * The WebVPN wire protocol as `spec/protocol.md` lays it out: control frames, the UDP data packet and the WebSocket
3
+ * mux frame. No I/O: the caller owns the streams and hands bytes in.
4
+ */
5
+ export * from './codes';
6
+ export * from './control';
7
+ export * from './mux';
8
+ export type { TcpOption, UdpOption } from './options';
9
+ export * from './udp';
10
+ export { ProtocolError } from './wire';
11
+ export type { Address } from './wire';
@@ -0,0 +1,32 @@
1
+ /** The control stream of the WebSocket mux. It is never opened and never credited. */
2
+ export declare const CONTROL_STREAM = 0;
3
+ /** The kinds of the frame that carries a session's streams over one WebSocket: `[u8 kind][u32 stream][payload]`. */
4
+ export declare const MuxKind: {
5
+ /** Relay only: a new stream, with one `MuxStreamType` byte. */
6
+ readonly OPEN: 0;
7
+ readonly DATA: 1;
8
+ /** This side of the stream is finished. */
9
+ readonly FIN: 2;
10
+ /** Empty, or the `u32` code a stream reset carries. */
11
+ readonly RESET: 3;
12
+ /** Client only: one `UdpPacket`, on stream 0. */
13
+ readonly DATAGRAM: 4;
14
+ /** Client only: a `u32` of further bytes the relay may send on the stream. */
15
+ readonly CREDIT: 5;
16
+ };
17
+ /** What an `OPEN` frame says the stream carries. */
18
+ export declare const MuxStreamType: {
19
+ readonly TCP: 0;
20
+ /** A UDP socket's datagrams toward the client. */
21
+ readonly UDP_DOWNLOAD: 1;
22
+ };
23
+ export declare const encodeMuxFrame: (kind: number, stream: number, payload?: Uint8Array) => Uint8Array<ArrayBuffer>;
24
+ /**
25
+ * The kind, stream and payload of one WebSocket message, or `undefined` for a message too short to be a frame, which
26
+ * a receiver ignores. `payload` is a view into `message`, not a copy.
27
+ */
28
+ export declare const decodeMuxFrame: (message: Uint8Array) => {
29
+ kind: number;
30
+ stream: number;
31
+ payload: Uint8Array<ArrayBufferLike>;
32
+ } | undefined;
@@ -0,0 +1,69 @@
1
+ import type { Reader, Writer } from './wire';
2
+ /**
3
+ * A TCP socket option a client may set. `unknown` holds the tag of an option this package does not name, which a
4
+ * relay answers with `UNSUPPORTED`.
5
+ */
6
+ export type TcpOption = {
7
+ type: 'noDelay';
8
+ value: boolean;
9
+ } | {
10
+ type: 'ttl';
11
+ value: number;
12
+ }
13
+ /** `initialDelaySeconds` is the idle time before the first probe; 0 leaves the system's default. */
14
+ | {
15
+ type: 'keepAlive';
16
+ enabled: boolean;
17
+ initialDelaySeconds: number;
18
+ } | {
19
+ type: 'sendBufferSize';
20
+ value: number;
21
+ } | {
22
+ type: 'recvBufferSize';
23
+ value: number;
24
+ } | {
25
+ type: 'unknown';
26
+ tag: number;
27
+ };
28
+ /** A UDP socket option a client may set. `unknown` is as for `TcpOption`. */
29
+ export type UdpOption = {
30
+ type: 'broadcast';
31
+ value: boolean;
32
+ } | {
33
+ type: 'ttl';
34
+ value: number;
35
+ } | {
36
+ type: 'multicastTtlV4';
37
+ value: number;
38
+ } | {
39
+ type: 'multicastLoopV4';
40
+ value: boolean;
41
+ } | {
42
+ type: 'multicastLoopV6';
43
+ value: boolean;
44
+ }
45
+ /** 4 bytes each; an `interface` of 0.0.0.0 lets the system pick. */
46
+ | {
47
+ type: 'addMembershipV4' | 'dropMembershipV4';
48
+ group: Uint8Array;
49
+ interface: Uint8Array;
50
+ }
51
+ /** `group` holds 16 bytes; `interface` is an index, 0 for the default interface. */
52
+ | {
53
+ type: 'addMembershipV6' | 'dropMembershipV6';
54
+ group: Uint8Array;
55
+ interface: number;
56
+ } | {
57
+ type: 'sendBufferSize';
58
+ value: number;
59
+ } | {
60
+ type: 'recvBufferSize';
61
+ value: number;
62
+ } | {
63
+ type: 'unknown';
64
+ tag: number;
65
+ };
66
+ export declare const putTcpOption: (w: Writer, option: TcpOption) => Writer;
67
+ export declare const readTcpOption: (r: Reader) => TcpOption;
68
+ export declare const putUdpOption: (w: Writer, option: UdpOption) => Writer;
69
+ export declare const readUdpOption: (r: Reader) => UdpOption;
@@ -0,0 +1,19 @@
1
+ import type { Address } from './wire';
2
+ /** The longest payload a `UdpPacket` carries. */
3
+ export declare const MAX_UDP_PAYLOAD_BYTES: number;
4
+ /**
5
+ * One datagram of a UDP socket, the same on a stream, in a WebTransport datagram and in a mux `DATAGRAM` frame.
6
+ *
7
+ * Toward the relay `remote` is where to send it, absent on a connected socket. Toward the client it is where the
8
+ * datagram came from.
9
+ */
10
+ export type UdpPacket = {
11
+ socket: number;
12
+ remote?: Address;
13
+ data: Uint8Array;
14
+ };
15
+ export declare const encodeUdpPacket: (packet: UdpPacket) => Uint8Array<ArrayBufferLike>;
16
+ /** The packet behind the `u32` length a stream carries it with. */
17
+ export declare const encodeFramedUdpPacket: (packet: UdpPacket) => Uint8Array<ArrayBufferLike>;
18
+ /** Reads `bytes` as one whole packet. `data` is a view into `bytes`, not a copy. */
19
+ export declare const decodeUdpPacket: (bytes: Uint8Array) => UdpPacket;
@@ -0,0 +1,35 @@
1
+ /** Bytes that are not laid out as the protocol says. It ends the session. */
2
+ export declare class ProtocolError extends Error {
3
+ constructor(message: string);
4
+ }
5
+ /** A socket address. `ip` holds 4 bytes for IPv4 or 16 for IPv6. */
6
+ export type Address = {
7
+ ip: Uint8Array;
8
+ port: number;
9
+ };
10
+ export declare const addressFamily: (ip: Uint8Array) => 0 | 1;
11
+ /** Reads fields off `bytes` in order. `take` and `rest` return views into `bytes`, not copies. */
12
+ export declare const reader: (bytes: Uint8Array) => {
13
+ u8: () => number;
14
+ u16: () => number;
15
+ u32: () => number;
16
+ bool: () => boolean;
17
+ take: (length: number) => Uint8Array<ArrayBufferLike>;
18
+ rest: () => Uint8Array<ArrayBufferLike>;
19
+ left: () => number;
20
+ optionalAddress: () => Address | undefined;
21
+ address: () => Address;
22
+ };
23
+ export type Reader = ReturnType<typeof reader>;
24
+ export type Writer = {
25
+ u8(value: number): Writer;
26
+ bool(value: boolean): Writer;
27
+ u16(value: number): Writer;
28
+ u32(value: number): Writer;
29
+ put(bytes: Uint8Array): Writer;
30
+ address(address: Address | undefined): Writer;
31
+ setU32(at: number, value: number): Writer;
32
+ length(): number;
33
+ done(): Uint8Array;
34
+ };
35
+ export declare const writer: () => Writer;
@@ -0,0 +1,49 @@
1
+ import type { Carrier, CloseInfo } from './context';
2
+ import type { TcpListener } from './listener';
3
+ import type { Address } from './protocol';
4
+ import type { TcpSocket } from './tcp';
5
+ import type { UdpSocket } from './udp';
6
+ /**
7
+ * One session with a relay, over WebTransport or the WebSocket mux, from `connect`. Every request rejects with a
8
+ * `WebVpnError`: the relay's own refusal, `UNSUPPORTED` for a feature the relay's hello did not offer (the request
9
+ * is then never sent), `TIMED_OUT` when the relay gave no answer in 10 seconds (12 for a connect), and `OTHER` once
10
+ * the session is closed. Closing the session ends every socket it holds.
11
+ */
12
+ export type Session = {
13
+ carrier: 'webtransport' | 'websocket';
14
+ /** The `Feature` bits of the relay's hello. */
15
+ features: number;
16
+ /**
17
+ * Resolves when the session ended, with a `CloseCode` and a reason: the relay's `bye` when it said one, else the
18
+ * carrier's close, else `LOST` for a carrier that ended with no code. Never rejects.
19
+ */
20
+ closed: Promise<CloseInfo>;
21
+ /** Resolves when the relay says it is about to stop. The sockets keep working; new ones belong on a new session. */
22
+ draining: Promise<void>;
23
+ /** Presents a credential; a refusal leaves the session as it was. Needs the `AUTH` feature. */
24
+ authenticate: (credential: Uint8Array) => Promise<void>;
25
+ /** Returns the session to no identity. Needs the `AUTH` feature. */
26
+ clearAuth: () => Promise<void>;
27
+ /** Connects to an address, or to the first A record of a name, which the relay looks up (the `DNS` feature). */
28
+ tcpConnect: (target: Address | {
29
+ name: string;
30
+ port: number;
31
+ }) => Promise<TcpSocket>;
32
+ /** Listens for connections on the relay. Port 0 takes a free port. Needs the `LISTEN` feature. */
33
+ tcpListen: (local: Address) => Promise<TcpListener>;
34
+ /** Binds a UDP socket on the relay. Port 0 takes a free port. */
35
+ udpBind: (local: Address) => Promise<UdpSocket>;
36
+ /** Puts a whole DNS message to the relay's resolver and returns its whole answer. Needs the `DNS` feature. */
37
+ lookup: (query: Uint8Array) => Promise<Uint8Array>;
38
+ /** Closes the session with `NORMAL`. */
39
+ close: () => void;
40
+ };
41
+ /**
42
+ * Runs a session on `carrier`: reads the relay's hello from the first stream the relay opens, answers with the
43
+ * client's, and serves requests from then on. Rejects, with the carrier closed, when the relay's version does not
44
+ * suit (`VERSION_MISMATCH`), when the relay closes the session in its setup (the error's `closeCode` says why, as
45
+ * `SESSION_LIMIT` for a full relay), when the setup breaks the protocol, ends, or does not finish within 4 seconds.
46
+ */
47
+ export declare const openSession: (carrier: Carrier, options?: {
48
+ agent?: string;
49
+ }) => Promise<Session>;
package/build/tcp.d.ts ADDED
@@ -0,0 +1,32 @@
1
+ import type { Context, SocketStream } from './context';
2
+ import type { Address, TcpOption } from './protocol';
3
+ /**
4
+ * A TCP connection through the relay, from `Session.tcpConnect` or a listener.
5
+ *
6
+ * `readable` carries the peer's bytes and ends when the peer finished its side. It errors with a `WebVpnError` of
7
+ * `CONNECTION_RESET` when the peer reset the connection, and with the session's error when the session ended first.
8
+ * `writable` carries bytes to the peer, and closing it is a half close: the peer reads its end, and `readable` stays
9
+ * open. Aborting it resets this side of the socket's stream, which the relay also takes as a half close, so the peer
10
+ * reads a clean end and never gets what the abort dropped; over the WebSocket mux it ends `readable` too. `reset` is
11
+ * the way to reset the connection. After `close` or `reset`, `readable` ends without an error, and what the relay
12
+ * still sends is dropped.
13
+ */
14
+ export type TcpSocket = {
15
+ /** The socket's id in its session. */
16
+ id: number;
17
+ /** The relay's end of the connection. */
18
+ local: Address;
19
+ remote: Address;
20
+ readable: ReadableStream<Uint8Array>;
21
+ writable: WritableStream<Uint8Array>;
22
+ setOption: (option: TcpOption) => Promise<void>;
23
+ /** Ends the connection in order: the relay sends the peer what it holds of this side, then closes. */
24
+ close: () => Promise<void>;
25
+ /** Ends the connection with a reset toward the peer, dropping what was not sent. */
26
+ reset: () => Promise<void>;
27
+ };
28
+ export declare const tcpSocket: (context: Context, { socket, local, remote }: {
29
+ socket: number;
30
+ local: Address;
31
+ remote: Address;
32
+ }, { head, reader, writable: upload }: SocketStream) => TcpSocket;
package/build/udp.d.ts ADDED
@@ -0,0 +1,38 @@
1
+ import type { ClosedBy, Context, Opened } from './context';
2
+ import type { Address, UdpOption } from './protocol';
3
+ /** A datagram the socket received, and where from. */
4
+ export type UdpDatagram = {
5
+ remote: Address;
6
+ data: Uint8Array;
7
+ };
8
+ /**
9
+ * A UDP socket on the relay, from `Session.udpBind`.
10
+ *
11
+ * `readable` gives each datagram the relay received for the socket. Datagrams wait for the reader up to 4 MiB for
12
+ * the socket and 32 MiB for the session, and past that are dropped. `writable` sends a datagram to `remote`, or to
13
+ * the connected peer when `remote` is absent; it never waits, and drops a datagram the carrier cannot take now, as a
14
+ * network would. A datagram over 65536 bytes fails its write with a `WebVpnError`, and since a failed write errors a
15
+ * `WritableStream`, every later write fails too; the socket stays open to read and to close. The socket stops at its
16
+ * own `close()`, at the relay's `Closed` notice, or when the session ends: `readable` ends then, and `closed` resolves
17
+ * with which of the three it was. Cancelling `readable` closes the socket.
18
+ */
19
+ export type UdpSocket = {
20
+ /** The socket's id in its session. */
21
+ id: number;
22
+ /** The bound address, with the port the relay took for port 0. */
23
+ local: Address;
24
+ readable: ReadableStream<UdpDatagram>;
25
+ writable: WritableStream<{
26
+ remote?: Address;
27
+ data: Uint8Array;
28
+ }>;
29
+ /** Fixes the peer: datagrams then go to it, and only its datagrams arrive. */
30
+ connect: (remote: Address) => Promise<void>;
31
+ disconnect: () => Promise<void>;
32
+ setOption: (option: UdpOption) => Promise<void>;
33
+ /** Resolves once the socket stopped, with who stopped it. Never rejects. */
34
+ closed: Promise<ClosedBy>;
35
+ /** Stops the socket at once and closes it at the relay. Rejects with the relay's refusal while the session lasts. */
36
+ close: () => Promise<void>;
37
+ };
38
+ export declare const udpSocket: (context: Context, { socket, local }: Opened) => UdpSocket;
@@ -0,0 +1,7 @@
1
+ import type { Carrier } from './context';
2
+ /**
3
+ * The carrier of a WebSocket session: the mux of `spec/protocol.md` over `socket`, which the caller made just before.
4
+ * Rejects, with the socket closed, when it does not open in 6 seconds, or when it closes before it opened: with the
5
+ * session's end as the error when that close carries one of the relay's codes.
6
+ */
7
+ export declare const webSocketCarrier: (socket: WebSocket) => Promise<Carrier>;
@@ -0,0 +1,6 @@
1
+ import type { Carrier } from './context';
2
+ /**
3
+ * The carrier of a WebTransport session, `transport` made by the caller just before: its first incoming stream must
4
+ * be read before anything is awaited. Rejects, with the transport closed, when it is not ready in `readyTimeoutMs`.
5
+ */
6
+ export declare const webTransportCarrier: (transport: WebTransport, readyTimeoutMs: number) => Promise<Carrier>;
package/package.json CHANGED
@@ -1,6 +1,52 @@
1
1
  {
2
2
  "name": "@webvpn/client",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.5.0",
4
+ "description": "The WebVPN client: real TCP and UDP sockets for a web page, through a relay",
5
+ "license": "MIT",
6
+ "homepage": "https://webvpn.dev",
7
+ "keywords": [
8
+ "webvpn",
9
+ "webtransport",
10
+ "websocket",
11
+ "tcp",
12
+ "udp",
13
+ "sockets",
14
+ "browser"
15
+ ],
16
+ "type": "module",
17
+ "main": "build/index.js",
18
+ "types": "build/index.d.ts",
19
+ "exports": {
20
+ ".": {
21
+ "types": "./build/index.d.ts",
22
+ "import": "./build/index.js"
23
+ }
24
+ },
25
+ "files": [
26
+ "build"
27
+ ],
28
+ "sideEffects": false,
29
+ "scripts": {
30
+ "build": "vp build && tsc -p tsconfig.build.json",
31
+ "typecheck": "tsc -p tsconfig.json",
32
+ "test": "vp test run",
33
+ "test:unit": "vp test run --project unit",
34
+ "test:browser": "vp test run --project browser"
35
+ },
36
+ "dependencies": {
37
+ "@banou/ponyfill": "^0.0.11"
38
+ },
39
+ "devDependencies": {
40
+ "@vitest/browser": "5.0.5",
41
+ "@vitest/browser-playwright": "5.0.5",
42
+ "playwright": "^1.56.0"
43
+ },
44
+ "repository": {
45
+ "type": "git",
46
+ "url": "git+https://github.com/banou26/webvpn.git",
47
+ "directory": "packages/client"
48
+ },
49
+ "publishConfig": {
50
+ "access": "public"
51
+ }
52
+ }