@xconnio/xconn 0.0.0-stage → 0.6.2

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.
Files changed (54) hide show
  1. package/CHANGELOG.md +33 -0
  2. package/LICENSE +22 -0
  3. package/README.md +281 -2
  4. package/dist/auth.d.ts +51 -0
  5. package/dist/auth.js +57 -0
  6. package/dist/call.d.ts +38 -0
  7. package/dist/call.js +146 -0
  8. package/dist/client.d.ts +21 -0
  9. package/dist/client.js +62 -0
  10. package/dist/constants.d.ts +60 -0
  11. package/dist/constants.js +61 -0
  12. package/dist/crypto.d.ts +9 -0
  13. package/dist/crypto.js +25 -0
  14. package/dist/discovery.d.ts +20 -0
  15. package/dist/discovery.js +25 -0
  16. package/dist/exception.d.ts +12 -0
  17. package/dist/exception.js +28 -0
  18. package/dist/inbox.d.ts +14 -0
  19. package/dist/inbox.js +71 -0
  20. package/dist/index.d.ts +26 -0
  21. package/dist/index.js +15 -0
  22. package/dist/message.d.ts +33 -0
  23. package/dist/message.js +59 -0
  24. package/dist/native.d.ts +6 -0
  25. package/dist/native.js +35 -0
  26. package/dist/package.json +1 -0
  27. package/dist/payload.d.ts +48 -0
  28. package/dist/payload.js +314 -0
  29. package/dist/peer.d.ts +16 -0
  30. package/dist/peer.js +124 -0
  31. package/dist/requests.d.ts +57 -0
  32. package/dist/requests.js +121 -0
  33. package/dist/rpc-streams.d.ts +13 -0
  34. package/dist/rpc-streams.js +31 -0
  35. package/dist/serializers.d.ts +29 -0
  36. package/dist/serializers.js +44 -0
  37. package/dist/session.d.ts +67 -0
  38. package/dist/session.js +528 -0
  39. package/dist/toolchain.txt +3 -0
  40. package/dist/transport.d.ts +47 -0
  41. package/dist/transport.js +201 -0
  42. package/dist/types.d.ts +70 -0
  43. package/dist/types.js +140 -0
  44. package/dist/util.d.ts +2 -0
  45. package/dist/util.js +20 -0
  46. package/dist/wasm/xconn_browser.d.ts +176 -0
  47. package/dist/wasm/xconn_browser.js +1467 -0
  48. package/dist/wasm/xconn_browser_bg.wasm +0 -0
  49. package/dist/wasm/xconn_browser_bg.wasm.d.ts +53 -0
  50. package/dist/webrtc.d.ts +98 -0
  51. package/dist/webrtc.js +712 -0
  52. package/dist/webtransport.d.ts +31 -0
  53. package/dist/webtransport.js +242 -0
  54. package/package.json +48 -4
@@ -0,0 +1,31 @@
1
+ import type { ClientOptions } from "./client.js";
2
+ import { Session } from "./session.js";
3
+ export interface WebTransportOptions {
4
+ serverCertificateHashes?: WebTransportHash[];
5
+ connectTimeout?: number;
6
+ signal?: AbortSignal;
7
+ }
8
+ /** A native browser byte stream. EOF is undefined, not an empty data packet. */
9
+ export declare class ByteStream {
10
+ private readonly reader;
11
+ private readonly writer;
12
+ private closing?;
13
+ constructor(stream: WebTransportBidirectionalStream);
14
+ read(): Promise<Uint8Array | undefined>;
15
+ write(data: Uint8Array): Promise<void>;
16
+ writeEof(): Promise<void>;
17
+ close(): Promise<void>;
18
+ }
19
+ /** Browser QUIC connectivity uses HTTP/3 WebTransport, not arbitrary QUIC sockets. */
20
+ export declare class WebTransportConnection {
21
+ private readonly transport;
22
+ private rpcSessionOpened;
23
+ private readonly incoming;
24
+ private closed;
25
+ private constructor();
26
+ static connect(url: string, options?: WebTransportOptions): Promise<WebTransportConnection>;
27
+ openSession(realm: string, options?: ClientOptions): Promise<Session>;
28
+ openStream(): Promise<ByteStream>;
29
+ acceptStream(): Promise<ByteStream>;
30
+ close(): Promise<void>;
31
+ }
@@ -0,0 +1,242 @@
1
+ import { ProtocolError } from "./exception.js";
2
+ import { getNative, rustCall } from "./native.js";
3
+ import { connectPeer, payloadBytes } from "./peer.js";
4
+ import { getSubProtocol } from "./serializers.js";
5
+ import { Session } from "./session.js";
6
+ import { abortable, timeoutValue } from "./util.js";
7
+ /** A native browser byte stream. EOF is undefined, not an empty data packet. */
8
+ export class ByteStream {
9
+ reader;
10
+ writer;
11
+ closing;
12
+ constructor(stream) {
13
+ this.reader = stream.readable.getReader();
14
+ this.writer = stream.writable.getWriter();
15
+ }
16
+ async read() {
17
+ if (this.closing) {
18
+ throw new ProtocolError("Byte stream closed");
19
+ }
20
+ const value = await this.reader.read();
21
+ return value.done ? undefined : value.value;
22
+ }
23
+ async write(data) {
24
+ if (this.closing) {
25
+ throw new ProtocolError("Byte stream closed");
26
+ }
27
+ await this.writer.write(data);
28
+ }
29
+ async writeEof() {
30
+ if (this.closing) {
31
+ throw new ProtocolError("Byte stream closed");
32
+ }
33
+ await this.writer.close();
34
+ }
35
+ close() {
36
+ return (this.closing ??= (async () => {
37
+ await Promise.allSettled([this.reader.cancel(), this.writer.abort()]);
38
+ this.reader.releaseLock();
39
+ this.writer.releaseLock();
40
+ })());
41
+ }
42
+ }
43
+ class ByteReader {
44
+ stream;
45
+ buffer = new Uint8Array(0);
46
+ constructor(stream) {
47
+ this.stream = stream;
48
+ }
49
+ clear() {
50
+ this.buffer = new Uint8Array(0);
51
+ }
52
+ async exact(size) {
53
+ const output = new Uint8Array(size);
54
+ let offset = 0;
55
+ while (offset < size) {
56
+ if (this.buffer.length === 0) {
57
+ const chunk = await this.stream.read();
58
+ if (chunk === undefined) {
59
+ throw new ProtocolError("Transport stream ended");
60
+ }
61
+ this.buffer = chunk;
62
+ }
63
+ const count = Math.min(size - offset, this.buffer.length);
64
+ output.set(this.buffer.subarray(0, count), offset);
65
+ this.buffer = this.buffer.subarray(count);
66
+ offset += count;
67
+ }
68
+ return output;
69
+ }
70
+ }
71
+ class RawSocketPeer {
72
+ stream;
73
+ input;
74
+ framing;
75
+ closed = false;
76
+ listeners = new Set();
77
+ writes = Promise.resolve();
78
+ constructor(stream, serializer) {
79
+ this.stream = stream;
80
+ this.input = new ByteReader(stream);
81
+ this.framing = new (getNative().RawSocketFraming)(getSubProtocol(serializer).slice("wamp.2.".length));
82
+ }
83
+ async negotiate(signal) {
84
+ await abortable(this.stream.write(rustCall(() => this.framing.handshake())), signal);
85
+ const response = await abortable(this.input.exact(4), signal);
86
+ rustCall(() => this.framing.accept_handshake(response));
87
+ }
88
+ send(data) {
89
+ return this.frame(0, payloadBytes(data));
90
+ }
91
+ frame(kind, data) {
92
+ if (this.closed) {
93
+ return Promise.reject(new ProtocolError("Transport closed"));
94
+ }
95
+ const header = rustCall(() => this.framing.header(kind, data.length));
96
+ const frame = new Uint8Array(header.length + data.length);
97
+ frame.set(header);
98
+ frame.set(data, header.length);
99
+ const result = this.writes.then(() => this.stream.write(frame));
100
+ this.writes = result.catch(() => this.close());
101
+ return result;
102
+ }
103
+ async receive() {
104
+ try {
105
+ while (!this.closed) {
106
+ const header = await this.input.exact(4);
107
+ if (this.closed) {
108
+ throw new ProtocolError("Transport closed");
109
+ }
110
+ const [kind, length] = rustCall(() => this.framing.parse_header(header));
111
+ const data = await this.input.exact(length);
112
+ if (kind === 0) {
113
+ return data;
114
+ }
115
+ if (kind === 1) {
116
+ await this.frame(2, data);
117
+ }
118
+ }
119
+ throw new ProtocolError("Transport closed");
120
+ }
121
+ catch (error) {
122
+ await this.close();
123
+ throw error;
124
+ }
125
+ }
126
+ onDisconnect(callback) {
127
+ if (this.closed) {
128
+ callback("Transport closed");
129
+ }
130
+ else {
131
+ this.listeners.add(callback);
132
+ }
133
+ }
134
+ isConnected() {
135
+ return !this.closed;
136
+ }
137
+ async close() {
138
+ if (this.closed) {
139
+ return;
140
+ }
141
+ this.closed = true;
142
+ this.input.clear();
143
+ this.framing.free();
144
+ for (const callback of this.listeners) {
145
+ callback("Transport closed");
146
+ }
147
+ this.listeners.clear();
148
+ await this.stream.close();
149
+ }
150
+ }
151
+ /** Browser QUIC connectivity uses HTTP/3 WebTransport, not arbitrary QUIC sockets. */
152
+ export class WebTransportConnection {
153
+ transport;
154
+ rpcSessionOpened = false;
155
+ incoming;
156
+ closed = false;
157
+ constructor(transport) {
158
+ this.transport = transport;
159
+ this.incoming = transport.incomingBidirectionalStreams.getReader();
160
+ void transport.closed
161
+ .catch(() => undefined)
162
+ .finally(() => {
163
+ this.closed = true;
164
+ });
165
+ }
166
+ static async connect(url, options = {}) {
167
+ if (typeof WebTransport === "undefined") {
168
+ throw new ProtocolError("WebTransport is unavailable in this browser");
169
+ }
170
+ const controller = new AbortController();
171
+ const abort = () => controller.abort(options.signal?.reason);
172
+ options.signal?.addEventListener("abort", abort, { once: true });
173
+ if (options.signal?.aborted) {
174
+ abort();
175
+ }
176
+ const timeout = timeoutValue(options.connectTimeout ?? 20000, "connectTimeout");
177
+ const timer = timeout > 0
178
+ ? setTimeout(() => controller.abort(new ProtocolError("WebTransport connection timed out")), timeout)
179
+ : undefined;
180
+ let transport;
181
+ try {
182
+ controller.signal.throwIfAborted();
183
+ transport = new WebTransport(url, { serverCertificateHashes: options.serverCertificateHashes });
184
+ await abortable(transport.ready, controller.signal);
185
+ return new WebTransportConnection(transport);
186
+ }
187
+ catch (error) {
188
+ transport?.close();
189
+ // Suppress a ready/closed rejection even when abort won the race.
190
+ void transport?.closed.catch(() => undefined);
191
+ throw error;
192
+ }
193
+ finally {
194
+ clearTimeout(timer);
195
+ options.signal?.removeEventListener("abort", abort);
196
+ }
197
+ }
198
+ openSession(realm, options = {}) {
199
+ const streamOpener = this.rpcSessionOpened ? undefined : () => this.openStream();
200
+ this.rpcSessionOpened = true;
201
+ return connectPeer(async (serializer, signal) => {
202
+ const stream = await abortable(this.openStream().then(async (value) => {
203
+ if (signal.aborted) {
204
+ await value.close();
205
+ signal.throwIfAborted();
206
+ }
207
+ return value;
208
+ }), signal);
209
+ const peer = new RawSocketPeer(stream, serializer);
210
+ try {
211
+ await peer.negotiate(signal);
212
+ return peer;
213
+ }
214
+ catch (error) {
215
+ await peer.close();
216
+ throw error;
217
+ }
218
+ }, realm, options, (base, config) => new Session(base, { ...config, streamOpener }));
219
+ }
220
+ async openStream() {
221
+ if (this.closed) {
222
+ throw new ProtocolError("WebTransport connection closed");
223
+ }
224
+ return new ByteStream(await this.transport.createBidirectionalStream());
225
+ }
226
+ async acceptStream() {
227
+ const result = await this.incoming.read();
228
+ if (result.done) {
229
+ throw new ProtocolError("WebTransport connection closed");
230
+ }
231
+ return new ByteStream(result.value);
232
+ }
233
+ async close() {
234
+ if (this.closed) {
235
+ return;
236
+ }
237
+ this.closed = true;
238
+ this.transport.close();
239
+ await this.incoming.cancel().catch(() => undefined);
240
+ await this.transport.closed.catch(() => undefined);
241
+ }
242
+ }
package/package.json CHANGED
@@ -1,6 +1,50 @@
1
1
  {
2
2
  "name": "@xconnio/xconn",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.6.2",
4
+ "description": "Browser WAMP client backed by the shared Rust protocol engine",
5
+ "license": "MIT",
6
+ "author": "XConn Developers",
7
+ "publishConfig": {
8
+ "access": "public"
9
+ },
10
+ "type": "module",
11
+ "main": "./dist/index.js",
12
+ "types": "./dist/index.d.ts",
13
+ "exports": {
14
+ ".": {
15
+ "types": "./dist/index.d.ts",
16
+ "import": "./dist/index.js"
17
+ }
18
+ },
19
+ "files": [
20
+ "dist",
21
+ "README.md",
22
+ "CHANGELOG.md",
23
+ "LICENSE"
24
+ ],
25
+ "scripts": {
26
+ "build": "bash scripts/build.sh",
27
+ "lint": "eslint src tests",
28
+ "typecheck": "tsc --project tests/tsconfig.json",
29
+ "test": "node tests/run.mjs",
30
+ "prepack": "npm run build",
31
+ "format": "prettier --write src scripts tests package.json tsconfig.json eslint.config.js",
32
+ "format:check": "prettier --check src scripts tests package.json tsconfig.json eslint.config.js"
33
+ },
34
+ "devDependencies": {
35
+ "@eslint/js": "^10.0.1",
36
+ "eslint": "^10.13.0",
37
+ "globals": "^17.13.0",
38
+ "prettier": "^3.6.2",
39
+ "typescript": "^5.5.3",
40
+ "typescript-eslint": "^8.72.0"
41
+ },
42
+ "engines": {
43
+ "node": ">=20.11"
44
+ },
45
+ "repository": {
46
+ "type": "git",
47
+ "url": "git+https://github.com/xconnio/xconn.git",
48
+ "directory": "bindings/js"
49
+ }
50
+ }