@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.
- package/CHANGELOG.md +33 -0
- package/LICENSE +22 -0
- package/README.md +281 -2
- package/dist/auth.d.ts +51 -0
- package/dist/auth.js +57 -0
- package/dist/call.d.ts +38 -0
- package/dist/call.js +146 -0
- package/dist/client.d.ts +21 -0
- package/dist/client.js +62 -0
- package/dist/constants.d.ts +60 -0
- package/dist/constants.js +61 -0
- package/dist/crypto.d.ts +9 -0
- package/dist/crypto.js +25 -0
- package/dist/discovery.d.ts +20 -0
- package/dist/discovery.js +25 -0
- package/dist/exception.d.ts +12 -0
- package/dist/exception.js +28 -0
- package/dist/inbox.d.ts +14 -0
- package/dist/inbox.js +71 -0
- package/dist/index.d.ts +26 -0
- package/dist/index.js +15 -0
- package/dist/message.d.ts +33 -0
- package/dist/message.js +59 -0
- package/dist/native.d.ts +6 -0
- package/dist/native.js +35 -0
- package/dist/package.json +1 -0
- package/dist/payload.d.ts +48 -0
- package/dist/payload.js +314 -0
- package/dist/peer.d.ts +16 -0
- package/dist/peer.js +124 -0
- package/dist/requests.d.ts +57 -0
- package/dist/requests.js +121 -0
- package/dist/rpc-streams.d.ts +13 -0
- package/dist/rpc-streams.js +31 -0
- package/dist/serializers.d.ts +29 -0
- package/dist/serializers.js +44 -0
- package/dist/session.d.ts +67 -0
- package/dist/session.js +528 -0
- package/dist/toolchain.txt +3 -0
- package/dist/transport.d.ts +47 -0
- package/dist/transport.js +201 -0
- package/dist/types.d.ts +70 -0
- package/dist/types.js +140 -0
- package/dist/util.d.ts +2 -0
- package/dist/util.js +20 -0
- package/dist/wasm/xconn_browser.d.ts +176 -0
- package/dist/wasm/xconn_browser.js +1467 -0
- package/dist/wasm/xconn_browser_bg.wasm +0 -0
- package/dist/wasm/xconn_browser_bg.wasm.d.ts +53 -0
- package/dist/webrtc.d.ts +98 -0
- package/dist/webrtc.js +712 -0
- package/dist/webtransport.d.ts +31 -0
- package/dist/webtransport.js +242 -0
- package/package.json +48 -4
package/dist/payload.js
ADDED
|
@@ -0,0 +1,314 @@
|
|
|
1
|
+
export class PayloadError extends Error {
|
|
2
|
+
kind;
|
|
3
|
+
constructor(kind, message) {
|
|
4
|
+
super(message);
|
|
5
|
+
this.kind = kind;
|
|
6
|
+
this.name = "PayloadError";
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
function convert(value, kind) {
|
|
10
|
+
if (value === null || value === undefined) {
|
|
11
|
+
throw new PayloadError("null", "Payload value is null");
|
|
12
|
+
}
|
|
13
|
+
if (kind === "string" && typeof value === "string") {
|
|
14
|
+
return value;
|
|
15
|
+
}
|
|
16
|
+
if (kind === "bool" && typeof value === "boolean") {
|
|
17
|
+
return value;
|
|
18
|
+
}
|
|
19
|
+
if (["int", "uint", "float"].includes(kind) && ["number", "bigint"].includes(typeof value)) {
|
|
20
|
+
if (kind === "float") {
|
|
21
|
+
const result = Number(value);
|
|
22
|
+
if (Number.isFinite(result)) {
|
|
23
|
+
return result;
|
|
24
|
+
}
|
|
25
|
+
throw new PayloadError("range", "Numeric payload must be finite");
|
|
26
|
+
}
|
|
27
|
+
if (typeof value === "number") {
|
|
28
|
+
if (!Number.isFinite(value)) {
|
|
29
|
+
throw new PayloadError("range", "Numeric payload must be finite");
|
|
30
|
+
}
|
|
31
|
+
if (!Number.isInteger(value)) {
|
|
32
|
+
throw new PayloadError("type", "Expected an integer");
|
|
33
|
+
}
|
|
34
|
+
if (!Number.isSafeInteger(value)) {
|
|
35
|
+
throw new PayloadError("range", "Use BigInt for unsafe Number integers");
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
const result = BigInt(value);
|
|
39
|
+
const lower = kind === "int" ? -(1n << 63n) : 0n;
|
|
40
|
+
const upper = kind === "int" ? 1n << 63n : 1n << 64n;
|
|
41
|
+
if (result >= lower && result < upper) {
|
|
42
|
+
return result;
|
|
43
|
+
}
|
|
44
|
+
throw new PayloadError("range", "Integer payload is out of range");
|
|
45
|
+
}
|
|
46
|
+
if (kind === "bytes" && value instanceof Uint8Array) {
|
|
47
|
+
return value;
|
|
48
|
+
}
|
|
49
|
+
if (kind === "list" && Array.isArray(value)) {
|
|
50
|
+
return value;
|
|
51
|
+
}
|
|
52
|
+
if (kind === "dict" &&
|
|
53
|
+
typeof value === "object" &&
|
|
54
|
+
(Object.getPrototypeOf(value) === null || Object.getPrototypeOf(value) === Object.prototype)) {
|
|
55
|
+
return value;
|
|
56
|
+
}
|
|
57
|
+
throw new PayloadError("type", `Expected ${kind} payload`);
|
|
58
|
+
}
|
|
59
|
+
export class Payload {
|
|
60
|
+
args;
|
|
61
|
+
kwargs;
|
|
62
|
+
constructor(args = [], kwargs = {}) {
|
|
63
|
+
this.args = args;
|
|
64
|
+
this.kwargs = kwargs;
|
|
65
|
+
}
|
|
66
|
+
requireArg(index) {
|
|
67
|
+
if (!Number.isInteger(index) || index < 0 || index >= this.args.length) {
|
|
68
|
+
throw new PayloadError("missing", `Missing positional argument ${index}`);
|
|
69
|
+
}
|
|
70
|
+
return this.args[index];
|
|
71
|
+
}
|
|
72
|
+
requireKwarg(key) {
|
|
73
|
+
if (!Object.hasOwn(this.kwargs, key)) {
|
|
74
|
+
throw new PayloadError("missing", `Missing keyword argument ${key}`);
|
|
75
|
+
}
|
|
76
|
+
return this.kwargs[key];
|
|
77
|
+
}
|
|
78
|
+
arg(index) {
|
|
79
|
+
return Number.isInteger(index) && index >= 0 ? this.args[index] : undefined;
|
|
80
|
+
}
|
|
81
|
+
kwarg(key) {
|
|
82
|
+
return Object.hasOwn(this.kwargs, key) ? this.kwargs[key] : undefined;
|
|
83
|
+
}
|
|
84
|
+
argOr(index, fallback) {
|
|
85
|
+
return this.arg(index) ?? fallback;
|
|
86
|
+
}
|
|
87
|
+
kwargOr(key, fallback) {
|
|
88
|
+
return this.kwarg(key) ?? fallback;
|
|
89
|
+
}
|
|
90
|
+
argString(index) {
|
|
91
|
+
return convert(this.requireArg(index), "string");
|
|
92
|
+
}
|
|
93
|
+
argStringOr(index, fallback) {
|
|
94
|
+
try {
|
|
95
|
+
return this.argString(index);
|
|
96
|
+
}
|
|
97
|
+
catch (error) {
|
|
98
|
+
if (error instanceof PayloadError) {
|
|
99
|
+
return fallback;
|
|
100
|
+
}
|
|
101
|
+
throw error;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
argBool(index) {
|
|
105
|
+
return convert(this.requireArg(index), "bool");
|
|
106
|
+
}
|
|
107
|
+
argBoolOr(index, fallback) {
|
|
108
|
+
try {
|
|
109
|
+
return this.argBool(index);
|
|
110
|
+
}
|
|
111
|
+
catch (error) {
|
|
112
|
+
if (error instanceof PayloadError) {
|
|
113
|
+
return fallback;
|
|
114
|
+
}
|
|
115
|
+
throw error;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
argInt64(index) {
|
|
119
|
+
return convert(this.requireArg(index), "int");
|
|
120
|
+
}
|
|
121
|
+
argInt64Or(index, fallback) {
|
|
122
|
+
try {
|
|
123
|
+
return this.argInt64(index);
|
|
124
|
+
}
|
|
125
|
+
catch (error) {
|
|
126
|
+
if (error instanceof PayloadError) {
|
|
127
|
+
return fallback;
|
|
128
|
+
}
|
|
129
|
+
throw error;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
argUInt64(index) {
|
|
133
|
+
return convert(this.requireArg(index), "uint");
|
|
134
|
+
}
|
|
135
|
+
argUInt64Or(index, fallback) {
|
|
136
|
+
try {
|
|
137
|
+
return this.argUInt64(index);
|
|
138
|
+
}
|
|
139
|
+
catch (error) {
|
|
140
|
+
if (error instanceof PayloadError) {
|
|
141
|
+
return fallback;
|
|
142
|
+
}
|
|
143
|
+
throw error;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
argFloat64(index) {
|
|
147
|
+
return convert(this.requireArg(index), "float");
|
|
148
|
+
}
|
|
149
|
+
argFloat64Or(index, fallback) {
|
|
150
|
+
try {
|
|
151
|
+
return this.argFloat64(index);
|
|
152
|
+
}
|
|
153
|
+
catch (error) {
|
|
154
|
+
if (error instanceof PayloadError) {
|
|
155
|
+
return fallback;
|
|
156
|
+
}
|
|
157
|
+
throw error;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
argBytes(index) {
|
|
161
|
+
return convert(this.requireArg(index), "bytes");
|
|
162
|
+
}
|
|
163
|
+
argBytesOr(index, fallback) {
|
|
164
|
+
try {
|
|
165
|
+
return this.argBytes(index);
|
|
166
|
+
}
|
|
167
|
+
catch (error) {
|
|
168
|
+
if (error instanceof PayloadError) {
|
|
169
|
+
return fallback;
|
|
170
|
+
}
|
|
171
|
+
throw error;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
argList(index) {
|
|
175
|
+
return convert(this.requireArg(index), "list");
|
|
176
|
+
}
|
|
177
|
+
argListOr(index, fallback) {
|
|
178
|
+
try {
|
|
179
|
+
return this.argList(index);
|
|
180
|
+
}
|
|
181
|
+
catch (error) {
|
|
182
|
+
if (error instanceof PayloadError) {
|
|
183
|
+
return fallback;
|
|
184
|
+
}
|
|
185
|
+
throw error;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
argDict(index) {
|
|
189
|
+
return convert(this.requireArg(index), "dict");
|
|
190
|
+
}
|
|
191
|
+
argDictOr(index, fallback) {
|
|
192
|
+
try {
|
|
193
|
+
return this.argDict(index);
|
|
194
|
+
}
|
|
195
|
+
catch (error) {
|
|
196
|
+
if (error instanceof PayloadError) {
|
|
197
|
+
return fallback;
|
|
198
|
+
}
|
|
199
|
+
throw error;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
kwargString(key) {
|
|
203
|
+
return convert(this.requireKwarg(key), "string");
|
|
204
|
+
}
|
|
205
|
+
kwargStringOr(key, fallback) {
|
|
206
|
+
try {
|
|
207
|
+
return this.kwargString(key);
|
|
208
|
+
}
|
|
209
|
+
catch (error) {
|
|
210
|
+
if (error instanceof PayloadError) {
|
|
211
|
+
return fallback;
|
|
212
|
+
}
|
|
213
|
+
throw error;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
kwargBool(key) {
|
|
217
|
+
return convert(this.requireKwarg(key), "bool");
|
|
218
|
+
}
|
|
219
|
+
kwargBoolOr(key, fallback) {
|
|
220
|
+
try {
|
|
221
|
+
return this.kwargBool(key);
|
|
222
|
+
}
|
|
223
|
+
catch (error) {
|
|
224
|
+
if (error instanceof PayloadError) {
|
|
225
|
+
return fallback;
|
|
226
|
+
}
|
|
227
|
+
throw error;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
kwargInt64(key) {
|
|
231
|
+
return convert(this.requireKwarg(key), "int");
|
|
232
|
+
}
|
|
233
|
+
kwargInt64Or(key, fallback) {
|
|
234
|
+
try {
|
|
235
|
+
return this.kwargInt64(key);
|
|
236
|
+
}
|
|
237
|
+
catch (error) {
|
|
238
|
+
if (error instanceof PayloadError) {
|
|
239
|
+
return fallback;
|
|
240
|
+
}
|
|
241
|
+
throw error;
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
kwargUInt64(key) {
|
|
245
|
+
return convert(this.requireKwarg(key), "uint");
|
|
246
|
+
}
|
|
247
|
+
kwargUInt64Or(key, fallback) {
|
|
248
|
+
try {
|
|
249
|
+
return this.kwargUInt64(key);
|
|
250
|
+
}
|
|
251
|
+
catch (error) {
|
|
252
|
+
if (error instanceof PayloadError) {
|
|
253
|
+
return fallback;
|
|
254
|
+
}
|
|
255
|
+
throw error;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
kwargFloat64(key) {
|
|
259
|
+
return convert(this.requireKwarg(key), "float");
|
|
260
|
+
}
|
|
261
|
+
kwargFloat64Or(key, fallback) {
|
|
262
|
+
try {
|
|
263
|
+
return this.kwargFloat64(key);
|
|
264
|
+
}
|
|
265
|
+
catch (error) {
|
|
266
|
+
if (error instanceof PayloadError) {
|
|
267
|
+
return fallback;
|
|
268
|
+
}
|
|
269
|
+
throw error;
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
kwargBytes(key) {
|
|
273
|
+
return convert(this.requireKwarg(key), "bytes");
|
|
274
|
+
}
|
|
275
|
+
kwargBytesOr(key, fallback) {
|
|
276
|
+
try {
|
|
277
|
+
return this.kwargBytes(key);
|
|
278
|
+
}
|
|
279
|
+
catch (error) {
|
|
280
|
+
if (error instanceof PayloadError) {
|
|
281
|
+
return fallback;
|
|
282
|
+
}
|
|
283
|
+
throw error;
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
kwargList(key) {
|
|
287
|
+
return convert(this.requireKwarg(key), "list");
|
|
288
|
+
}
|
|
289
|
+
kwargListOr(key, fallback) {
|
|
290
|
+
try {
|
|
291
|
+
return this.kwargList(key);
|
|
292
|
+
}
|
|
293
|
+
catch (error) {
|
|
294
|
+
if (error instanceof PayloadError) {
|
|
295
|
+
return fallback;
|
|
296
|
+
}
|
|
297
|
+
throw error;
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
kwargDict(key) {
|
|
301
|
+
return convert(this.requireKwarg(key), "dict");
|
|
302
|
+
}
|
|
303
|
+
kwargDictOr(key, fallback) {
|
|
304
|
+
try {
|
|
305
|
+
return this.kwargDict(key);
|
|
306
|
+
}
|
|
307
|
+
catch (error) {
|
|
308
|
+
if (error instanceof PayloadError) {
|
|
309
|
+
return fallback;
|
|
310
|
+
}
|
|
311
|
+
throw error;
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
}
|
package/dist/peer.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { ClientOptions } from "./client.js";
|
|
2
|
+
import type { Payload, Serializer } from "./serializers.js";
|
|
3
|
+
import { Session } from "./session.js";
|
|
4
|
+
import type { SessionOptions } from "./session.js";
|
|
5
|
+
import { BaseSession } from "./types.js";
|
|
6
|
+
/** Complete WAMP messages; transport-specific framing stays below this interface. */
|
|
7
|
+
export interface MessagePeer {
|
|
8
|
+
send(data: Payload): void | Promise<void>;
|
|
9
|
+
receive(): Promise<Payload>;
|
|
10
|
+
close(): Promise<void>;
|
|
11
|
+
onDisconnect(callback: (reason?: string) => void): void;
|
|
12
|
+
isConnected(): boolean;
|
|
13
|
+
}
|
|
14
|
+
export declare function payloadBytes(payload: Payload): Uint8Array;
|
|
15
|
+
/** Common Rust-driven join path for WebSocket, WebTransport and WebRTC. */
|
|
16
|
+
export declare function connectPeer<T extends Session = Session>(factory: (serializer: Serializer, signal: AbortSignal) => Promise<MessagePeer>, realm: string, options?: ClientOptions, createSession?: (base: BaseSession, options: SessionOptions) => T): Promise<T>;
|
package/dist/peer.js
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { AnonymousAuthenticator } from "./auth.js";
|
|
2
|
+
import { Code } from "./constants.js";
|
|
3
|
+
import { ProtocolError } from "./exception.js";
|
|
4
|
+
import { WireMessage, inspectMessage } from "./message.js";
|
|
5
|
+
import { getNative, initializeWasm, rustCall } from "./native.js";
|
|
6
|
+
import { defaultSerializer, getSubProtocol, JSONSerializer } from "./serializers.js";
|
|
7
|
+
import { Session } from "./session.js";
|
|
8
|
+
import { BaseSession, SessionDetails } from "./types.js";
|
|
9
|
+
import { abortable, timeoutValue } from "./util.js";
|
|
10
|
+
export function payloadBytes(payload) {
|
|
11
|
+
return typeof payload === "string" ? new TextEncoder().encode(payload) : payload;
|
|
12
|
+
}
|
|
13
|
+
// Byte-oriented transports carry JSON as UTF-8; the JSON serializer expects text.
|
|
14
|
+
function codecPayload(codec, data) {
|
|
15
|
+
return codec instanceof JSONSerializer && data instanceof Uint8Array ? new TextDecoder().decode(data) : data;
|
|
16
|
+
}
|
|
17
|
+
class PeerSession extends BaseSession {
|
|
18
|
+
peer;
|
|
19
|
+
details;
|
|
20
|
+
codec;
|
|
21
|
+
constructor(peer, details, codec) {
|
|
22
|
+
super();
|
|
23
|
+
this.peer = peer;
|
|
24
|
+
this.details = details;
|
|
25
|
+
this.codec = codec;
|
|
26
|
+
}
|
|
27
|
+
getSessionDetails() {
|
|
28
|
+
return this.details;
|
|
29
|
+
}
|
|
30
|
+
serializer() {
|
|
31
|
+
return this.codec;
|
|
32
|
+
}
|
|
33
|
+
isConnected() {
|
|
34
|
+
return this.peer.isConnected();
|
|
35
|
+
}
|
|
36
|
+
send(payload) {
|
|
37
|
+
void Promise.resolve(this.peer.send(payload)).catch(() => this.peer.close().catch(() => undefined));
|
|
38
|
+
}
|
|
39
|
+
async receive() {
|
|
40
|
+
return codecPayload(this.codec, await this.peer.receive());
|
|
41
|
+
}
|
|
42
|
+
sendMessage(message) {
|
|
43
|
+
this.send(this.codec.serialize(message));
|
|
44
|
+
}
|
|
45
|
+
onDisconnect(callback) {
|
|
46
|
+
this.peer.onDisconnect(callback);
|
|
47
|
+
}
|
|
48
|
+
close() {
|
|
49
|
+
return this.peer.close();
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
/** Common Rust-driven join path for WebSocket, WebTransport and WebRTC. */
|
|
53
|
+
export async function connectPeer(factory, realm, options = {}, createSession) {
|
|
54
|
+
const timeout = timeoutValue(options.connectTimeout ?? 20000, "connectTimeout");
|
|
55
|
+
const closeTimeout = timeoutValue(options.closeTimeout ?? 10000, "closeTimeout");
|
|
56
|
+
const controller = new AbortController();
|
|
57
|
+
const externalAbort = () => controller.abort(options.signal?.reason);
|
|
58
|
+
options.signal?.addEventListener("abort", externalAbort, { once: true });
|
|
59
|
+
if (options.signal?.aborted) {
|
|
60
|
+
externalAbort();
|
|
61
|
+
}
|
|
62
|
+
const timer = timeout > 0
|
|
63
|
+
? setTimeout(() => controller.abort(new ProtocolError("Connection timed out")), timeout)
|
|
64
|
+
: undefined;
|
|
65
|
+
let peer;
|
|
66
|
+
let protocol;
|
|
67
|
+
let ownedSerializer;
|
|
68
|
+
let ownedAuthenticator;
|
|
69
|
+
let transferred = false;
|
|
70
|
+
let joining = true;
|
|
71
|
+
try {
|
|
72
|
+
controller.signal.throwIfAborted();
|
|
73
|
+
await abortable(initializeWasm(), controller.signal);
|
|
74
|
+
const serializer = options.serializer ?? (ownedSerializer = defaultSerializer());
|
|
75
|
+
const auth = options.authenticator ?? (ownedAuthenticator = new AnonymousAuthenticator());
|
|
76
|
+
protocol = rustCall(() => new (getNative().ClientProtocol)(realm, getSubProtocol(serializer).slice("wamp.2.".length), auth.authMethod, auth.authID, auth.authExtra));
|
|
77
|
+
const joiner = protocol;
|
|
78
|
+
// Factories must release transport resources if setup is aborted.
|
|
79
|
+
peer = await factory(serializer, controller.signal);
|
|
80
|
+
peer.onDisconnect((reason) => {
|
|
81
|
+
if (joining) {
|
|
82
|
+
controller.abort(new ProtocolError(reason ?? "Connection closed"));
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
controller.signal.throwIfAborted();
|
|
86
|
+
await abortable(Promise.resolve(peer.send(serializer.serialize(new WireMessage(rustCall(() => joiner.hello()))))), controller.signal);
|
|
87
|
+
while (!joiner.joined()) {
|
|
88
|
+
const payload = codecPayload(serializer, await abortable(peer.receive(), controller.signal));
|
|
89
|
+
const message = inspectMessage(serializer.deserialize(payload));
|
|
90
|
+
if (message.type() === Code.CHALLENGE) {
|
|
91
|
+
rustCall(() => joiner.check_challenge());
|
|
92
|
+
if (message.authmethod !== auth.authMethod) {
|
|
93
|
+
throw new ProtocolError("Unexpected authentication method");
|
|
94
|
+
}
|
|
95
|
+
const response = await abortable(auth.authenticate(message), controller.signal);
|
|
96
|
+
rustCall(() => joiner.set_authentication(response.marshal()));
|
|
97
|
+
}
|
|
98
|
+
const response = rustCall(() => joiner.receive(message.marshal()));
|
|
99
|
+
if (response !== undefined) {
|
|
100
|
+
await abortable(Promise.resolve(peer.send(serializer.serialize(new WireMessage(response)))), controller.signal);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
const details = rustCall(() => joiner.details());
|
|
104
|
+
const base = new PeerSession(peer, new SessionDetails(details.sessionID, details.realm, details.authid, details.authrole, details.authmethod, details.authextra, details.roles), serializer);
|
|
105
|
+
const session = createSession
|
|
106
|
+
? createSession(base, { closeTimeout })
|
|
107
|
+
: new Session(base, { closeTimeout });
|
|
108
|
+
const codec = ownedSerializer;
|
|
109
|
+
session.onDisconnect(() => codec?.free?.());
|
|
110
|
+
transferred = true;
|
|
111
|
+
return session;
|
|
112
|
+
}
|
|
113
|
+
finally {
|
|
114
|
+
joining = false;
|
|
115
|
+
clearTimeout(timer);
|
|
116
|
+
options.signal?.removeEventListener("abort", externalAbort);
|
|
117
|
+
protocol?.free();
|
|
118
|
+
ownedAuthenticator?.free?.();
|
|
119
|
+
if (!transferred) {
|
|
120
|
+
ownedSerializer?.free?.();
|
|
121
|
+
await peer?.close().catch(() => undefined);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import type { Call, StreamOptions } from "./call.js";
|
|
2
|
+
import type { Session } from "./session.js";
|
|
3
|
+
import type { Dictionary } from "./message.js";
|
|
4
|
+
import type { CallControl, EventHandler, InvocationHandler, Registration, Result, Subscription } from "./types.js";
|
|
5
|
+
/** Configuration is local; only execute() sends an operation. */
|
|
6
|
+
declare abstract class Request<T> {
|
|
7
|
+
protected readonly requestOptions: Dictionary;
|
|
8
|
+
protected abstract run(): Promise<T>;
|
|
9
|
+
execute(): Promise<T>;
|
|
10
|
+
option(key: string, value: unknown): this;
|
|
11
|
+
options(values: Dictionary): this;
|
|
12
|
+
}
|
|
13
|
+
declare abstract class OutgoingRequest<T> extends Request<T> {
|
|
14
|
+
protected readonly arguments: unknown[];
|
|
15
|
+
protected readonly keywordArguments: Dictionary;
|
|
16
|
+
arg(value: unknown): this;
|
|
17
|
+
args(...values: unknown[]): this;
|
|
18
|
+
kwarg(key: string, value: unknown): this;
|
|
19
|
+
kwargs(values: Dictionary): this;
|
|
20
|
+
}
|
|
21
|
+
export declare class CallRequest extends OutgoingRequest<Result> {
|
|
22
|
+
private readonly session;
|
|
23
|
+
private readonly procedure;
|
|
24
|
+
private readonly control;
|
|
25
|
+
constructor(session: Session, procedure: string);
|
|
26
|
+
onProgress(receiver: NonNullable<CallControl["onProgress"]>): this;
|
|
27
|
+
timeout(milliseconds: number): this;
|
|
28
|
+
signal(signal: AbortSignal): this;
|
|
29
|
+
stream(options?: StreamOptions): Promise<Call>;
|
|
30
|
+
protected run(): Promise<Result>;
|
|
31
|
+
}
|
|
32
|
+
export declare class PublishRequest extends OutgoingRequest<void | null> {
|
|
33
|
+
private readonly session;
|
|
34
|
+
private readonly topic;
|
|
35
|
+
constructor(session: Session, topic: string);
|
|
36
|
+
acknowledge(value?: boolean): this;
|
|
37
|
+
excludeMe(value?: boolean): this;
|
|
38
|
+
protected run(): Promise<void | null>;
|
|
39
|
+
}
|
|
40
|
+
export declare class RegisterRequest extends Request<Registration> {
|
|
41
|
+
private readonly session;
|
|
42
|
+
private readonly procedure;
|
|
43
|
+
private readonly handler;
|
|
44
|
+
constructor(session: Session, procedure: string, handler: InvocationHandler);
|
|
45
|
+
match(value: string): this;
|
|
46
|
+
invoke(value: string): this;
|
|
47
|
+
protected run(): Promise<Registration>;
|
|
48
|
+
}
|
|
49
|
+
export declare class SubscribeRequest extends Request<Subscription> {
|
|
50
|
+
private readonly session;
|
|
51
|
+
private readonly topic;
|
|
52
|
+
private readonly handler;
|
|
53
|
+
constructor(session: Session, topic: string, handler: EventHandler);
|
|
54
|
+
match(value: string): this;
|
|
55
|
+
protected run(): Promise<Subscription>;
|
|
56
|
+
}
|
|
57
|
+
export {};
|
package/dist/requests.js
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
/** Configuration is local; only execute() sends an operation. */
|
|
2
|
+
class Request {
|
|
3
|
+
requestOptions = {};
|
|
4
|
+
execute() {
|
|
5
|
+
return this.run();
|
|
6
|
+
}
|
|
7
|
+
option(key, value) {
|
|
8
|
+
this.requestOptions[key] = value;
|
|
9
|
+
return this;
|
|
10
|
+
}
|
|
11
|
+
options(values) {
|
|
12
|
+
Object.assign(this.requestOptions, values);
|
|
13
|
+
return this;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
class OutgoingRequest extends Request {
|
|
17
|
+
arguments = [];
|
|
18
|
+
keywordArguments = {};
|
|
19
|
+
arg(value) {
|
|
20
|
+
this.arguments.push(value);
|
|
21
|
+
return this;
|
|
22
|
+
}
|
|
23
|
+
args(...values) {
|
|
24
|
+
this.arguments.push(...values);
|
|
25
|
+
return this;
|
|
26
|
+
}
|
|
27
|
+
kwarg(key, value) {
|
|
28
|
+
this.keywordArguments[key] = value;
|
|
29
|
+
return this;
|
|
30
|
+
}
|
|
31
|
+
kwargs(values) {
|
|
32
|
+
Object.assign(this.keywordArguments, values);
|
|
33
|
+
return this;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
export class CallRequest extends OutgoingRequest {
|
|
37
|
+
session;
|
|
38
|
+
procedure;
|
|
39
|
+
control = {};
|
|
40
|
+
constructor(session, procedure) {
|
|
41
|
+
super();
|
|
42
|
+
this.session = session;
|
|
43
|
+
this.procedure = procedure;
|
|
44
|
+
}
|
|
45
|
+
onProgress(receiver) {
|
|
46
|
+
this.control.onProgress = receiver;
|
|
47
|
+
return this;
|
|
48
|
+
}
|
|
49
|
+
timeout(milliseconds) {
|
|
50
|
+
this.control.timeout = milliseconds;
|
|
51
|
+
return this;
|
|
52
|
+
}
|
|
53
|
+
signal(signal) {
|
|
54
|
+
this.control.signal = signal;
|
|
55
|
+
return this;
|
|
56
|
+
}
|
|
57
|
+
async stream(options = {}) {
|
|
58
|
+
if (this.control.onProgress) {
|
|
59
|
+
throw new Error("streaming calls use receive() instead of callbacks");
|
|
60
|
+
}
|
|
61
|
+
return this.session.openStream(this.procedure, this.arguments, this.keywordArguments, this.requestOptions, this.control, options);
|
|
62
|
+
}
|
|
63
|
+
run() {
|
|
64
|
+
return this.session.executeCall(this.procedure, this.arguments, this.keywordArguments, this.requestOptions, this.control);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
export class PublishRequest extends OutgoingRequest {
|
|
68
|
+
session;
|
|
69
|
+
topic;
|
|
70
|
+
constructor(session, topic) {
|
|
71
|
+
super();
|
|
72
|
+
this.session = session;
|
|
73
|
+
this.topic = topic;
|
|
74
|
+
}
|
|
75
|
+
acknowledge(value = true) {
|
|
76
|
+
return this.option("acknowledge", value);
|
|
77
|
+
}
|
|
78
|
+
excludeMe(value = true) {
|
|
79
|
+
return this.option("exclude_me", value);
|
|
80
|
+
}
|
|
81
|
+
run() {
|
|
82
|
+
return this.session.executePublish(this.topic, this.arguments, this.keywordArguments, this.requestOptions);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
export class RegisterRequest extends Request {
|
|
86
|
+
session;
|
|
87
|
+
procedure;
|
|
88
|
+
handler;
|
|
89
|
+
constructor(session, procedure, handler) {
|
|
90
|
+
super();
|
|
91
|
+
this.session = session;
|
|
92
|
+
this.procedure = procedure;
|
|
93
|
+
this.handler = handler;
|
|
94
|
+
}
|
|
95
|
+
match(value) {
|
|
96
|
+
return this.option("match", value);
|
|
97
|
+
}
|
|
98
|
+
invoke(value) {
|
|
99
|
+
return this.option("invoke", value);
|
|
100
|
+
}
|
|
101
|
+
run() {
|
|
102
|
+
return this.session.executeRegister(this.procedure, this.handler, this.requestOptions);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
export class SubscribeRequest extends Request {
|
|
106
|
+
session;
|
|
107
|
+
topic;
|
|
108
|
+
handler;
|
|
109
|
+
constructor(session, topic, handler) {
|
|
110
|
+
super();
|
|
111
|
+
this.session = session;
|
|
112
|
+
this.topic = topic;
|
|
113
|
+
this.handler = handler;
|
|
114
|
+
}
|
|
115
|
+
match(value) {
|
|
116
|
+
return this.option("match", value);
|
|
117
|
+
}
|
|
118
|
+
run() {
|
|
119
|
+
return this.session.executeSubscribe(this.topic, this.handler, this.requestOptions);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/** Reliable byte I/O, independent of the connection carrying it. */
|
|
2
|
+
export interface RPCStream {
|
|
3
|
+
read(): Promise<Uint8Array | undefined>;
|
|
4
|
+
write(data: Uint8Array): Promise<void>;
|
|
5
|
+
writeEof(): Promise<void>;
|
|
6
|
+
close(): Promise<void>;
|
|
7
|
+
}
|
|
8
|
+
export type StreamOpener = () => Promise<RPCStream>;
|
|
9
|
+
export declare function offerStream(open: StreamOpener): Promise<{
|
|
10
|
+
reference: unknown;
|
|
11
|
+
stream: RPCStream;
|
|
12
|
+
}>;
|
|
13
|
+
export declare function acceptStream(open: StreamOpener, reference: unknown): Promise<RPCStream>;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { ProtocolError } from "./exception.js";
|
|
2
|
+
const key = "io.xconn.stream.v1";
|
|
3
|
+
function token(reference) {
|
|
4
|
+
if (typeof reference !== "object" || reference === null || Object.keys(reference).length !== 1) {
|
|
5
|
+
throw new ProtocolError("Argument is not a stream");
|
|
6
|
+
}
|
|
7
|
+
const encoded = reference[key];
|
|
8
|
+
if (typeof encoded !== "string" || !/^[a-fA-F0-9]{64}$/.test(encoded)) {
|
|
9
|
+
throw new ProtocolError("Invalid stream reference");
|
|
10
|
+
}
|
|
11
|
+
return Uint8Array.from(encoded.match(/../g), (byte) => parseInt(byte, 16));
|
|
12
|
+
}
|
|
13
|
+
async function connect(open, bytes) {
|
|
14
|
+
const stream = await open();
|
|
15
|
+
try {
|
|
16
|
+
await stream.write(new Uint8Array([0x7f, 0, 0, 0, ...bytes]));
|
|
17
|
+
return stream;
|
|
18
|
+
}
|
|
19
|
+
catch (error) {
|
|
20
|
+
await stream.close();
|
|
21
|
+
throw error;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
export async function offerStream(open) {
|
|
25
|
+
const bytes = crypto.getRandomValues(new Uint8Array(32));
|
|
26
|
+
const reference = { [key]: Array.from(bytes, (byte) => byte.toString(16).padStart(2, "0")).join("") };
|
|
27
|
+
return { reference, stream: await connect(open, bytes) };
|
|
28
|
+
}
|
|
29
|
+
export function acceptStream(open, reference) {
|
|
30
|
+
return connect(open, token(reference));
|
|
31
|
+
}
|