@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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Horion Corporation
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,42 @@
1
- # Temporary Holding Version
1
+ # @webvpn/client
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ The [WebVPN](https://webvpn.dev) client for a web page or a worker: real TCP connections, listening ports, UDP sockets
4
+ and name lookups, through a relay, as Web Streams. It runs over WebTransport, and over a WebSocket where WebTransport
5
+ is missing or fails.
6
+
7
+ ## Use
8
+
9
+ ```sh
10
+ npm install @webvpn/client
11
+ ```
12
+
13
+ ```js
14
+ import { connect } from '@webvpn/client'
15
+
16
+ const session = await connect({ url: 'https://relay.example:4433', webSocketUrl: 'wss://relay.example:8443' })
17
+ const socket = await session.tcpConnect({ name: 'example.org', port: 80 })
18
+
19
+ const writer = socket.writable.getWriter()
20
+ await writer.write(new TextEncoder().encode('GET / HTTP/1.0\r\nHost: example.org\r\n\r\n'))
21
+ await writer.close()
22
+
23
+ const reader = socket.readable.getReader()
24
+ const decoder = new TextDecoder()
25
+ while (true) {
26
+ const { value, done } = await reader.read()
27
+ if (done) break
28
+ console.log(decoder.decode(value))
29
+ }
30
+ ```
31
+
32
+ [Using it from an app](https://webvpn.dev/docs/using/) is the guide: listening ports, UDP sockets, name lookups,
33
+ credentials, and how a session ends. `@webvpn/net` and `@webvpn/dgram` put Node's `net` and `dgram` modules on a
34
+ session, for code written for Node.
35
+
36
+ ## What it needs
37
+
38
+ - A relay to connect to: [run your own](https://webvpn.dev/docs/running-a-relay/).
39
+ - A browser with WebTransport or WebSocket. The tests run in Chromium and in Firefox.
40
+
41
+ Version 0 of the protocol is not stable: a new minor version of this package may change the wire and its own
42
+ interfaces.
@@ -0,0 +1,12 @@
1
+ /**
2
+ * The bytes of an IPv4 address in dotted decimal (`192.0.2.7`, 4 bytes) or of an IPv6 address in RFC 4291 text
3
+ * (`2001:db8::7`, `::ffff:192.0.2.7`, 16 bytes), as an `Address` holds them. `undefined` for anything else: a name,
4
+ * brackets, a port, a zone (`%eth0`) or an IPv4 part with a leading zero.
5
+ */
6
+ export declare const parseIp: (text: string) => Uint8Array | undefined;
7
+ /**
8
+ * The text of an address's 4 or 16 bytes: dotted decimal for IPv4, and for IPv6 the RFC 5952 form, in lower case
9
+ * with the longest run of zero groups shortened to `::` and an IPv4-mapped address ending in dotted decimal.
10
+ * Throws `TypeError` for any other length.
11
+ */
12
+ export declare const formatIp: (ip: Uint8Array) => string;
@@ -0,0 +1,32 @@
1
+ import type { Session } from './session';
2
+ /** Where and how `connect` reaches a relay. */
3
+ export type ConnectOptions = {
4
+ /** The relay's WebTransport URL, `https://host:port`. */
5
+ url?: string;
6
+ /** The relay's WebSocket URL, `wss://host:port`. */
7
+ webSocketUrl?: string;
8
+ /**
9
+ * `auto`, the default, takes WebTransport when the page has it and `url` is given, and the WebSocket when either
10
+ * is missing or the WebTransport session does not open. The other two take that carrier only.
11
+ */
12
+ transport?: 'auto' | 'webtransport' | 'websocket';
13
+ /** The certificate hashes of a relay with a self-signed certificate, for WebTransport only. */
14
+ serverCertificateHashes?: {
15
+ algorithm: string;
16
+ value: BufferSource;
17
+ }[];
18
+ /** How long WebTransport may take to be ready, 4000 ms by default. */
19
+ webTransportTimeoutMs?: number;
20
+ /** Presented right after the hello. `connect` rejects, with the session closed, when the relay refuses it. */
21
+ credential?: Uint8Array;
22
+ /** Names this client in its hello, for a person reading the relay's logs. */
23
+ agent?: string;
24
+ };
25
+ /**
26
+ * Opens a session with a relay. Rejects with `TypeError` when the options name no URL for the carrier to take, and
27
+ * otherwise with the last carrier's failure: a `WebVpnError` (the relay closed or refused the session, a version
28
+ * that does not suit, a refused credential, a setup that took too long) or the browser's own error. A `WebVpnError`
29
+ * the relay's close caused carries its `closeCode`, so a full relay (`SESSION_LIMIT`) or one that is stopping
30
+ * (`RESTARTING`) reads apart from one that could not be reached.
31
+ */
32
+ export declare const connect: (options: ConnectOptions) => Promise<Session>;
@@ -0,0 +1,136 @@
1
+ import type { Address, ClientPacket, RelayPacket, UdpPacket } from './protocol';
2
+ /**
3
+ * A request the relay refused, or one the client refused on its own: a feature the relay has off, no answer in
4
+ * time, a session that is closed. `code` is an `ErrorCode`; a code this package does not name is kept as the relay
5
+ * sent it and means `OTHER`. `message` is the relay's text for a person, or the client's. `closeCode` is the
6
+ * session's `CloseCode` when the session's end is why it failed, as for a `connect` that a full relay refused with
7
+ * `SESSION_LIMIT`, and absent otherwise.
8
+ */
9
+ export declare class WebVpnError extends Error {
10
+ readonly code: number;
11
+ readonly closeCode?: number;
12
+ constructor(code: number, message: string, closeCode?: number);
13
+ }
14
+ /** A stream both ways, as a carrier hands it over. */
15
+ export type Duplex = {
16
+ readable: ReadableStream<Uint8Array>;
17
+ writable: WritableStream<Uint8Array>;
18
+ };
19
+ /** How a session ended: a `CloseCode` and a reason for a person. */
20
+ export type CloseInfo = {
21
+ code: number;
22
+ reason: string;
23
+ };
24
+ /** The error of whatever a session's end made fail, with the session's `CloseCode` as its `closeCode`. */
25
+ export declare const closeError: ({ code, reason }: CloseInfo) => WebVpnError;
26
+ /**
27
+ * Who closed a listener or a UDP socket: the client with its `close()`, the relay with a `Closed` notice and its
28
+ * `ErrorCode`, or the end of the session.
29
+ */
30
+ export type ClosedBy = {
31
+ by: 'client';
32
+ } | {
33
+ by: 'relay';
34
+ code: number;
35
+ } | {
36
+ by: 'session';
37
+ };
38
+ /**
39
+ * What a session needs of its carrier, WebTransport or the WebSocket mux. The relay opens every stream, and the
40
+ * client opens only what `openUni` gives.
41
+ */
42
+ export type Carrier = {
43
+ kind: 'webtransport' | 'websocket';
44
+ /** The streams the relay opens both ways, in order. The first is the control stream. */
45
+ incoming: ReadableStream<Duplex>;
46
+ /** The streams the relay opens toward the client only. */
47
+ incomingUni: ReadableStream<ReadableStream<Uint8Array>>;
48
+ /** One `UdpPacket` in each datagram from the relay, on a carrier that has datagrams. */
49
+ datagrams?: ReadableStream<Uint8Array>;
50
+ /** Sends one `UdpPacket` outside any stream, or drops it when the carrier holds too much unsent. */
51
+ sendDatagram: (packet: Uint8Array) => void;
52
+ /** Opens a stream toward the relay only, on a carrier that has such streams. */
53
+ openUni?: () => Promise<WritableStream<Uint8Array>>;
54
+ /**
55
+ * Resolves once, with the first of the close this side asked for and the end the relay or the network caused. An
56
+ * end that carried no session close code reads as `LOST`. Never rejects.
57
+ */
58
+ closed: Promise<CloseInfo>;
59
+ /** Closes the carrier with a `CloseCode`, and does nothing once it ended. */
60
+ close: (code: number, reason: string) => void;
61
+ };
62
+ /**
63
+ * A socket's stream once its id was read: the bytes that came after the id in the same read, the reader of the
64
+ * rest, and the client's side, which a stream toward the client only has not.
65
+ */
66
+ export type SocketStream = {
67
+ head: Uint8Array;
68
+ reader: ReadableStreamDefaultReader<Uint8Array>;
69
+ writable?: WritableStream<Uint8Array>;
70
+ };
71
+ /** A request as a socket hands it to its session, which gives it its id. */
72
+ export type Request = ClientPacket extends infer Packet ? Packet extends {
73
+ id: number;
74
+ } ? Omit<Packet, 'id'> : never : never;
75
+ /** The kind of socket a request opens, so that the socket a late reply names can be closed. */
76
+ export type Opens = 'tcp' | 'listener' | 'udp';
77
+ /** A socket the relay opened, as a `socket` or an `accepted` packet names it. */
78
+ export type Opened = {
79
+ socket: number;
80
+ local: Address;
81
+ remote?: Address;
82
+ };
83
+ /** What a listener or a UDP socket hears from its session. */
84
+ export type Owner = {
85
+ /** A notice that names the socket: `closed`, and for a listener `accepted` and `acceptRefused`. */
86
+ notice: (packet: RelayPacket) => void;
87
+ /** A UDP packet the relay sent outside the socket's stream. */
88
+ packet?: (packet: UdpPacket) => void;
89
+ /** The session ended. */
90
+ end: () => void;
91
+ };
92
+ /** What a socket asks of its session. */
93
+ export type Context = {
94
+ carrier: Carrier;
95
+ /**
96
+ * Sends `packet` under a fresh id. Its reply goes to `take` as it is read, ahead of the next frame, and the promise
97
+ * resolves with what `take` returns. An `error` reply rejects with a `WebVpnError`.
98
+ */
99
+ request: <T>(packet: Request, take: (reply: RelayPacket) => T, options?: {
100
+ timeoutMs?: number;
101
+ opens?: Opens;
102
+ }) => Promise<T>;
103
+ /** The stream of `socket`, held since it arrived or awaited, for at most `timeoutMs` when it is given. */
104
+ claim: (socket: number, timeoutMs?: number) => Promise<SocketStream>;
105
+ /** Forgets a socket that ended, so that a stream that arrives for it later is dropped. */
106
+ retire: (socket: number) => void;
107
+ /** Retires a socket nobody will use, and closes it at the relay. */
108
+ release: (opens: Opens, socket: number) => void;
109
+ /** Hands the notices and UDP packets of `socket` to `owner`, until the call it returns. */
110
+ own: (socket: number, owner: Owner) => () => void;
111
+ /** Why the session ended, once it did. */
112
+ ended: () => WebVpnError | undefined;
113
+ /** The bytes of UDP packets the session holds unread, across its sockets. */
114
+ unread: {
115
+ bytes: number;
116
+ };
117
+ };
118
+ export declare const EMPTY: Uint8Array;
119
+ export declare const noop: () => void;
120
+ export declare const concat: (first: Uint8Array, second: Uint8Array) => Uint8Array<ArrayBufferLike>;
121
+ export declare const u32: (value: number) => Uint8Array<ArrayBuffer>;
122
+ export declare const readU32: (bytes: Uint8Array) => number;
123
+ /** The code a stream's reset carried, as WebTransport and the mux both report it. */
124
+ export declare const streamErrorCode: (error: unknown) => unknown;
125
+ /** Rejects with a `WebVpnError` of `code` when `promise` takes longer than `ms`. */
126
+ export declare const within: <T>(promise: Promise<T>, ms: number, code: number, message: string) => Promise<Awaited<T>>;
127
+ export declare const unexpected: (reply: RelayPacket) => WebVpnError;
128
+ export declare const ok: (reply: RelayPacket) => void;
129
+ /**
130
+ * Cuts a byte stream into frames. Each call takes the next chunk and returns the frames it completed, in order;
131
+ * `decode` returns `undefined` until the bytes hold a whole frame, so a frame split at any byte, in any number of
132
+ * chunks, comes out exactly once.
133
+ */
134
+ export declare const framed: <T extends {
135
+ taken: number;
136
+ }>(decode: (bytes: Uint8Array) => T | undefined) => (chunk: Uint8Array) => T[];
package/build/dns.d.ts ADDED
@@ -0,0 +1,18 @@
1
+ declare const TYPES: {
2
+ readonly A: 1;
3
+ readonly AAAA: 28;
4
+ };
5
+ /**
6
+ * A whole DNS message asking for the `A` or `AAAA` records of `name`, with recursion desired, as `Session.lookup`
7
+ * and a connect by name send it. The message id is 0, as RFC 8484 asks of a query that may reach a DNS over HTTPS
8
+ * resolver. The name goes as written, so a name outside ASCII must be given in its `xn--` form. Throws `TypeError`
9
+ * for a name DNS cannot carry: an empty label, a label over 63 bytes or a name over 253.
10
+ */
11
+ export declare const dnsQuery: (name: string, type?: keyof typeof TYPES) => Uint8Array;
12
+ /**
13
+ * The addresses of the `A` and `AAAA` records in the answer section of a whole DNS message, as `Session.lookup`
14
+ * returns it, in the order the message gives them: 4 bytes for an `A` record, 16 for an `AAAA`. Other records, a
15
+ * `CNAME` among them, are skipped, and a message that ends early gives the records read before its end.
16
+ */
17
+ export declare const dnsAddresses: (message: Uint8Array) => Uint8Array[];
18
+ export {};
@@ -0,0 +1,11 @@
1
+ export * from './protocol';
2
+ export { formatIp, parseIp } from './address';
3
+ export { connect } from './connect';
4
+ export type { ConnectOptions } from './connect';
5
+ export { WebVpnError } from './context';
6
+ export type { CloseInfo, ClosedBy } from './context';
7
+ export { dnsAddresses, dnsQuery } from './dns';
8
+ export type { TcpListener } from './listener';
9
+ export type { Session } from './session';
10
+ export type { TcpSocket } from './tcp';
11
+ export type { UdpDatagram, UdpSocket } from './udp';