@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
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { type Message, WireMessage } from "./message.js";
|
|
2
|
+
export type Payload = string | Uint8Array;
|
|
3
|
+
export interface Serializer {
|
|
4
|
+
/** Release native storage. Built-in adapters recreate it lazily if reused. */
|
|
5
|
+
free?(): void;
|
|
6
|
+
serialize(message: Message): Payload;
|
|
7
|
+
deserialize(payload: Payload): Message;
|
|
8
|
+
}
|
|
9
|
+
declare abstract class RustSerializer implements Serializer {
|
|
10
|
+
private codec?;
|
|
11
|
+
abstract readonly codecName: string;
|
|
12
|
+
get subprotocol(): string;
|
|
13
|
+
private get native();
|
|
14
|
+
free(): void;
|
|
15
|
+
serialize(message: Message): Payload;
|
|
16
|
+
deserialize(payload: Payload): WireMessage;
|
|
17
|
+
}
|
|
18
|
+
export declare class JSONSerializer extends RustSerializer {
|
|
19
|
+
readonly codecName = "json";
|
|
20
|
+
}
|
|
21
|
+
export declare class CBORSerializer extends RustSerializer {
|
|
22
|
+
readonly codecName = "cbor";
|
|
23
|
+
}
|
|
24
|
+
export declare class MsgPackSerializer extends RustSerializer {
|
|
25
|
+
readonly codecName = "msgpack";
|
|
26
|
+
}
|
|
27
|
+
export declare function getSubProtocol(serializer: Serializer): string;
|
|
28
|
+
export declare function defaultSerializer(): Serializer;
|
|
29
|
+
export {};
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { getNative, rustCall } from "./native.js";
|
|
2
|
+
import { ProtocolError } from "./exception.js";
|
|
3
|
+
import { WireMessage } from "./message.js";
|
|
4
|
+
class RustSerializer {
|
|
5
|
+
codec;
|
|
6
|
+
get subprotocol() {
|
|
7
|
+
return `wamp.2.${this.codecName}`;
|
|
8
|
+
}
|
|
9
|
+
get native() {
|
|
10
|
+
return (this.codec ??= rustCall(() => new (getNative().NativeCodec)(this.codecName)));
|
|
11
|
+
}
|
|
12
|
+
free() {
|
|
13
|
+
this.codec?.free();
|
|
14
|
+
this.codec = undefined;
|
|
15
|
+
}
|
|
16
|
+
serialize(message) {
|
|
17
|
+
const bytes = rustCall(() => this.native.encode(message.marshal()));
|
|
18
|
+
return this.codecName === "json" ? new TextDecoder().decode(bytes) : bytes;
|
|
19
|
+
}
|
|
20
|
+
deserialize(payload) {
|
|
21
|
+
const bytes = typeof payload === "string" ? new TextEncoder().encode(payload) : payload;
|
|
22
|
+
return new WireMessage(rustCall(() => this.native.decode(bytes)));
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
export class JSONSerializer extends RustSerializer {
|
|
26
|
+
codecName = "json";
|
|
27
|
+
}
|
|
28
|
+
export class CBORSerializer extends RustSerializer {
|
|
29
|
+
codecName = "cbor";
|
|
30
|
+
}
|
|
31
|
+
export class MsgPackSerializer extends RustSerializer {
|
|
32
|
+
codecName = "msgpack";
|
|
33
|
+
}
|
|
34
|
+
export function getSubProtocol(serializer) {
|
|
35
|
+
if (serializer instanceof JSONSerializer ||
|
|
36
|
+
serializer instanceof CBORSerializer ||
|
|
37
|
+
serializer instanceof MsgPackSerializer) {
|
|
38
|
+
return serializer.subprotocol;
|
|
39
|
+
}
|
|
40
|
+
throw new ProtocolError("invalid serializer");
|
|
41
|
+
}
|
|
42
|
+
export function defaultSerializer() {
|
|
43
|
+
return getNative().has_binary_codecs() ? new CBORSerializer() : new JSONSerializer();
|
|
44
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { type StreamOpener } from "./rpc-streams.js";
|
|
2
|
+
import { Discovery } from "./discovery.js";
|
|
3
|
+
import { Call } from "./call.js";
|
|
4
|
+
import type { StreamOptions } from "./call.js";
|
|
5
|
+
import { CallRequest, PublishRequest, RegisterRequest, SubscribeRequest } from "./requests.js";
|
|
6
|
+
import type { Dictionary } from "./message.js";
|
|
7
|
+
import { BaseSession, Result, Registration, Subscription } from "./types.js";
|
|
8
|
+
import type { CallControl, InvocationHandler, EventHandler } from "./types.js";
|
|
9
|
+
export interface SessionOptions {
|
|
10
|
+
closeTimeout?: number;
|
|
11
|
+
streamOpener?: StreamOpener;
|
|
12
|
+
}
|
|
13
|
+
export declare class Session {
|
|
14
|
+
private readonly base;
|
|
15
|
+
private readonly ids;
|
|
16
|
+
private readonly pending;
|
|
17
|
+
private readonly registrations;
|
|
18
|
+
private readonly subscriptions;
|
|
19
|
+
private subscriptionOperations;
|
|
20
|
+
private readonly invocations;
|
|
21
|
+
private readonly disconnectCallbacks;
|
|
22
|
+
private readonly errorCallbacks;
|
|
23
|
+
private closed;
|
|
24
|
+
private closing;
|
|
25
|
+
private closePromise?;
|
|
26
|
+
private resolveClosed;
|
|
27
|
+
private readonly closedPromise;
|
|
28
|
+
private readonly closeTimeout;
|
|
29
|
+
private readonly streamOpener?;
|
|
30
|
+
constructor(base: BaseSession, options?: SessionOptions);
|
|
31
|
+
offerStream(): Promise<{
|
|
32
|
+
reference: unknown;
|
|
33
|
+
stream: import("./rpc-streams.js").RPCStream;
|
|
34
|
+
}>;
|
|
35
|
+
openStreamArgument(reference: unknown): Promise<import("./rpc-streams.js").RPCStream>;
|
|
36
|
+
discovery(prefix: string): Discovery;
|
|
37
|
+
call(procedure: string): CallRequest;
|
|
38
|
+
publish(topic: string): PublishRequest;
|
|
39
|
+
register(procedure: string, handler: InvocationHandler): RegisterRequest;
|
|
40
|
+
subscribe(topic: string, handler: EventHandler): SubscribeRequest;
|
|
41
|
+
get details(): import("./types.js").SessionDetails;
|
|
42
|
+
isConnected(): boolean;
|
|
43
|
+
onDisconnect(callback: (reason: string) => void | Promise<void>): void;
|
|
44
|
+
/** Observe asynchronous event-handler and disconnect-listener failures. */
|
|
45
|
+
onError(callback: (error: unknown) => void): void;
|
|
46
|
+
private report;
|
|
47
|
+
private send;
|
|
48
|
+
private settle;
|
|
49
|
+
private cancel;
|
|
50
|
+
private request;
|
|
51
|
+
openStream(procedure: string, args: unknown[] | null | undefined, kwargs: Dictionary | null | undefined, options: Dictionary, control: CallControl, stream: StreamOptions): Promise<Call>;
|
|
52
|
+
executeCall(procedure: string, args?: unknown[] | null, kwargs?: Dictionary | null, options?: Dictionary | null, control?: CallControl): Promise<Result>;
|
|
53
|
+
callProgress(procedure: string, receiver: NonNullable<CallControl["onProgress"]>, args?: unknown[] | null, kwargs?: Dictionary | null, options?: Dictionary | null, control?: CallControl): Promise<Result>;
|
|
54
|
+
executeRegister(procedure: string, endpoint: InvocationHandler, options?: Dictionary | null): Promise<Registration>;
|
|
55
|
+
unregister(handle: Registration): Promise<void>;
|
|
56
|
+
executePublish(topic: string, args?: unknown[] | null, kwargs?: Dictionary | null, options?: Dictionary | null): Promise<void | null>;
|
|
57
|
+
executeSubscribe(topic: string, endpoint: EventHandler, options?: Dictionary | null): Promise<Subscription>;
|
|
58
|
+
unsubscribe(handle: Subscription): Promise<void>;
|
|
59
|
+
private subscriptionOperation;
|
|
60
|
+
private receiveLoop;
|
|
61
|
+
private receive;
|
|
62
|
+
private interrupt;
|
|
63
|
+
private invoke;
|
|
64
|
+
private finish;
|
|
65
|
+
close(): Promise<void>;
|
|
66
|
+
private closeSession;
|
|
67
|
+
}
|
package/dist/session.js
ADDED
|
@@ -0,0 +1,528 @@
|
|
|
1
|
+
import { acceptStream, offerStream } from "./rpc-streams.js";
|
|
2
|
+
import { Discovery } from "./discovery.js";
|
|
3
|
+
import { Call } from "./call.js";
|
|
4
|
+
import { CallRequest, PublishRequest, RegisterRequest, SubscribeRequest } from "./requests.js";
|
|
5
|
+
import { getNative, rustCall } from "./native.js";
|
|
6
|
+
import { Code, URI } from "./constants.js";
|
|
7
|
+
import { makeMessage, inspectMessage } from "./message.js";
|
|
8
|
+
import { ApplicationError, ProtocolError } from "./exception.js";
|
|
9
|
+
import { BaseSession, Result, Invocation, Event, Registration, Subscription } from "./types.js";
|
|
10
|
+
import { timeoutValue } from "./util.js";
|
|
11
|
+
export class Session {
|
|
12
|
+
base;
|
|
13
|
+
ids;
|
|
14
|
+
pending = new Map();
|
|
15
|
+
registrations = new Map();
|
|
16
|
+
subscriptions = new Map();
|
|
17
|
+
subscriptionOperations = Promise.resolve();
|
|
18
|
+
invocations = new Map();
|
|
19
|
+
disconnectCallbacks = [];
|
|
20
|
+
errorCallbacks = [];
|
|
21
|
+
closed = false;
|
|
22
|
+
closing = false;
|
|
23
|
+
closePromise;
|
|
24
|
+
resolveClosed;
|
|
25
|
+
closedPromise = new Promise((resolve) => {
|
|
26
|
+
this.resolveClosed = resolve;
|
|
27
|
+
});
|
|
28
|
+
closeTimeout;
|
|
29
|
+
streamOpener;
|
|
30
|
+
constructor(base, options = {}) {
|
|
31
|
+
this.base = base;
|
|
32
|
+
this.streamOpener = options.streamOpener;
|
|
33
|
+
this.closeTimeout = timeoutValue(options.closeTimeout ?? 10000, "closeTimeout");
|
|
34
|
+
this.ids = new (getNative().RequestIds)();
|
|
35
|
+
base.onDisconnect((reason) => this.finish(reason ?? "Connection closed"));
|
|
36
|
+
void this.receiveLoop();
|
|
37
|
+
}
|
|
38
|
+
offerStream() {
|
|
39
|
+
if (this.closed || this.closing)
|
|
40
|
+
throw new ProtocolError("Session closed");
|
|
41
|
+
if (!this.streamOpener)
|
|
42
|
+
throw new ProtocolError("Session transport does not support RPC streams");
|
|
43
|
+
return offerStream(this.streamOpener);
|
|
44
|
+
}
|
|
45
|
+
openStreamArgument(reference) {
|
|
46
|
+
if (this.closed || this.closing)
|
|
47
|
+
throw new ProtocolError("Session closed");
|
|
48
|
+
if (!this.streamOpener)
|
|
49
|
+
throw new ProtocolError("Session transport does not support RPC streams");
|
|
50
|
+
return acceptStream(this.streamOpener, reference);
|
|
51
|
+
}
|
|
52
|
+
discovery(prefix) {
|
|
53
|
+
return new Discovery(this, prefix);
|
|
54
|
+
}
|
|
55
|
+
call(procedure) {
|
|
56
|
+
return new CallRequest(this, procedure);
|
|
57
|
+
}
|
|
58
|
+
publish(topic) {
|
|
59
|
+
return new PublishRequest(this, topic);
|
|
60
|
+
}
|
|
61
|
+
register(procedure, handler) {
|
|
62
|
+
return new RegisterRequest(this, procedure, handler);
|
|
63
|
+
}
|
|
64
|
+
subscribe(topic, handler) {
|
|
65
|
+
return new SubscribeRequest(this, topic, handler);
|
|
66
|
+
}
|
|
67
|
+
get details() {
|
|
68
|
+
return this.base.getSessionDetails();
|
|
69
|
+
}
|
|
70
|
+
isConnected() {
|
|
71
|
+
return !this.closed && !this.closing && this.base.isConnected();
|
|
72
|
+
}
|
|
73
|
+
onDisconnect(callback) {
|
|
74
|
+
if (this.closed) {
|
|
75
|
+
queueMicrotask(() => {
|
|
76
|
+
void Promise.resolve()
|
|
77
|
+
.then(() => callback("Connection closed"))
|
|
78
|
+
.catch((error) => this.report(error));
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
this.disconnectCallbacks.push(callback);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
/** Observe asynchronous event-handler and disconnect-listener failures. */
|
|
86
|
+
onError(callback) {
|
|
87
|
+
this.errorCallbacks.push(callback);
|
|
88
|
+
}
|
|
89
|
+
report(error) {
|
|
90
|
+
if (!this.errorCallbacks.length) {
|
|
91
|
+
console.error(error);
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
for (const callback of this.errorCallbacks) {
|
|
95
|
+
try {
|
|
96
|
+
callback(error);
|
|
97
|
+
}
|
|
98
|
+
catch (error) {
|
|
99
|
+
console.error(error);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
send(code, fields) {
|
|
104
|
+
this.base.send(this.base.serializer().serialize(makeMessage(code, fields)));
|
|
105
|
+
}
|
|
106
|
+
settle(id, value, error) {
|
|
107
|
+
const pending = this.pending.get(id);
|
|
108
|
+
if (!pending) {
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
this.pending.delete(id);
|
|
112
|
+
pending.cleanup();
|
|
113
|
+
if (arguments.length === 3) {
|
|
114
|
+
pending.reject(error);
|
|
115
|
+
}
|
|
116
|
+
else {
|
|
117
|
+
pending.resolve(value);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
cancel(id, error) {
|
|
121
|
+
const pending = this.pending.get(id);
|
|
122
|
+
if (!pending) {
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
this.settle(id, undefined, error);
|
|
126
|
+
if (pending.kind === Code.CALL && this.base.isConnected()) {
|
|
127
|
+
try {
|
|
128
|
+
this.send(Code.CANCEL, { request_id: id, options: { mode: "killnowait" } });
|
|
129
|
+
}
|
|
130
|
+
catch (error) {
|
|
131
|
+
this.finish("Connection failed", error);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
request(kind, expected, fields, parse, control = {}) {
|
|
136
|
+
if (!this.isConnected()) {
|
|
137
|
+
return Promise.reject(new ProtocolError("Session is closed"));
|
|
138
|
+
}
|
|
139
|
+
if (control.signal?.aborted) {
|
|
140
|
+
return Promise.reject(control.signal.reason);
|
|
141
|
+
}
|
|
142
|
+
const timeout = timeoutValue(control.timeout ?? 0, "call timeout");
|
|
143
|
+
const id = this.ids.next();
|
|
144
|
+
return new Promise((resolve, reject) => {
|
|
145
|
+
let timer;
|
|
146
|
+
const abort = () => this.cancel(id, control.signal?.reason);
|
|
147
|
+
const cleanup = () => {
|
|
148
|
+
clearTimeout(timer);
|
|
149
|
+
control.signal?.removeEventListener("abort", abort);
|
|
150
|
+
};
|
|
151
|
+
this.pending.set(id, {
|
|
152
|
+
kind,
|
|
153
|
+
expected,
|
|
154
|
+
parse,
|
|
155
|
+
resolve,
|
|
156
|
+
reject,
|
|
157
|
+
cleanup,
|
|
158
|
+
progress: Promise.resolve(),
|
|
159
|
+
receiver: control.onProgress,
|
|
160
|
+
final: false,
|
|
161
|
+
progressRequested: fields.options?.receive_progress === true,
|
|
162
|
+
});
|
|
163
|
+
control.opened?.(id);
|
|
164
|
+
control.signal?.addEventListener("abort", abort, { once: true });
|
|
165
|
+
if (timeout > 0) {
|
|
166
|
+
timer = setTimeout(() => this.cancel(id, new DOMException("Call timed out", "TimeoutError")), timeout);
|
|
167
|
+
}
|
|
168
|
+
try {
|
|
169
|
+
this.send(kind, { ...fields, request_id: id });
|
|
170
|
+
control.sent?.();
|
|
171
|
+
}
|
|
172
|
+
catch (error) {
|
|
173
|
+
this.settle(id, undefined, error);
|
|
174
|
+
}
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
async openStream(procedure, args, kwargs, options, control, stream) {
|
|
178
|
+
const capacity = stream.bufferSize ?? 32;
|
|
179
|
+
if (!Number.isInteger(capacity) || capacity < 1 || capacity > 65536) {
|
|
180
|
+
throw new ProtocolError("result buffer size must be between 1 and 65536");
|
|
181
|
+
}
|
|
182
|
+
if (stream.progressiveInput !== undefined && typeof stream.progressiveInput !== "boolean") {
|
|
183
|
+
throw new TypeError("progressiveInput must be a boolean");
|
|
184
|
+
}
|
|
185
|
+
if (stream.progressiveResults !== undefined && typeof stream.progressiveResults !== "boolean") {
|
|
186
|
+
throw new TypeError("progressiveResults must be a boolean");
|
|
187
|
+
}
|
|
188
|
+
let requestID = 0;
|
|
189
|
+
const requestOptions = {
|
|
190
|
+
...options,
|
|
191
|
+
receive_progress: stream.progressiveResults ?? true,
|
|
192
|
+
progress: stream.progressiveInput ?? false,
|
|
193
|
+
};
|
|
194
|
+
const call = new Call({
|
|
195
|
+
input: (args, kwargs, finish) => this.send(Code.CALL, {
|
|
196
|
+
procedure,
|
|
197
|
+
request_id: requestID,
|
|
198
|
+
args,
|
|
199
|
+
kwargs,
|
|
200
|
+
options: { ...requestOptions, progress: !finish },
|
|
201
|
+
}),
|
|
202
|
+
cancel: (error) => this.cancel(requestID, error),
|
|
203
|
+
}, capacity, stream.progressiveInput ?? false);
|
|
204
|
+
let sent;
|
|
205
|
+
let failed;
|
|
206
|
+
const started = new Promise((resolve, reject) => {
|
|
207
|
+
sent = resolve;
|
|
208
|
+
failed = reject;
|
|
209
|
+
});
|
|
210
|
+
const pending = this.request(Code.CALL, Code.RESULT, { procedure, args, kwargs, options: requestOptions }, (fields) => new Result(fields.args, fields.kwargs, fields.details, (reference) => this.openStreamArgument(reference)), {
|
|
211
|
+
...control,
|
|
212
|
+
opened: (id) => {
|
|
213
|
+
requestID = id;
|
|
214
|
+
},
|
|
215
|
+
sent,
|
|
216
|
+
onProgress: (result) => call.offer(result),
|
|
217
|
+
});
|
|
218
|
+
void pending.then((result) => call.complete(result), (error) => {
|
|
219
|
+
call.fail(error);
|
|
220
|
+
failed(error);
|
|
221
|
+
});
|
|
222
|
+
await started;
|
|
223
|
+
return call;
|
|
224
|
+
}
|
|
225
|
+
async executeCall(procedure, args, kwargs, options, control = {}) {
|
|
226
|
+
if (options?.progress === true) {
|
|
227
|
+
throw new ProtocolError("progressive input requires stream()");
|
|
228
|
+
}
|
|
229
|
+
const requestOptions = { ...options };
|
|
230
|
+
if (control.onProgress) {
|
|
231
|
+
requestOptions.receive_progress = true;
|
|
232
|
+
}
|
|
233
|
+
return this.request(Code.CALL, Code.RESULT, { procedure, args, kwargs, options: requestOptions }, (fields) => new Result(fields.args, fields.kwargs, fields.details, (reference) => this.openStreamArgument(reference)), control);
|
|
234
|
+
}
|
|
235
|
+
callProgress(procedure, receiver, args, kwargs, options, control = {}) {
|
|
236
|
+
return this.executeCall(procedure, args, kwargs, options, { ...control, onProgress: receiver });
|
|
237
|
+
}
|
|
238
|
+
async executeRegister(procedure, endpoint, options) {
|
|
239
|
+
return this.request(Code.REGISTER, Code.REGISTERED, { procedure, options: options ?? {} }, (fields) => {
|
|
240
|
+
const handle = new Registration(fields.registration_id, this);
|
|
241
|
+
this.registrations.set(handle.registrationID, { handle, endpoint });
|
|
242
|
+
return handle;
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
async unregister(handle) {
|
|
246
|
+
const entry = this.registrations.get(handle.registrationID);
|
|
247
|
+
if (!entry || entry.handle !== handle) {
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
return (entry.removing ??= this.request(Code.UNREGISTER, Code.UNREGISTERED, { registration_id: handle.registrationID }, () => {
|
|
251
|
+
this.registrations.delete(handle.registrationID);
|
|
252
|
+
}).catch((error) => {
|
|
253
|
+
entry.removing = undefined;
|
|
254
|
+
throw error;
|
|
255
|
+
}));
|
|
256
|
+
}
|
|
257
|
+
async executePublish(topic, args, kwargs, options) {
|
|
258
|
+
const fields = { topic, args, kwargs, options: options ?? {} };
|
|
259
|
+
if (options?.acknowledge) {
|
|
260
|
+
return this.request(Code.PUBLISH, Code.PUBLISHED, fields, () => undefined);
|
|
261
|
+
}
|
|
262
|
+
if (!this.isConnected()) {
|
|
263
|
+
throw new ProtocolError("Session is closed");
|
|
264
|
+
}
|
|
265
|
+
this.send(Code.PUBLISH, { ...fields, request_id: this.ids.next() });
|
|
266
|
+
return null;
|
|
267
|
+
}
|
|
268
|
+
async executeSubscribe(topic, endpoint, options) {
|
|
269
|
+
return this.subscriptionOperation(() => this.request(Code.SUBSCRIBE, Code.SUBSCRIBED, { topic, options: options ?? {} }, (fields) => {
|
|
270
|
+
const handle = new Subscription(fields.subscription_id, this);
|
|
271
|
+
let group = this.subscriptions.get(handle.subscriptionID);
|
|
272
|
+
if (!group) {
|
|
273
|
+
group = { handles: new Map() };
|
|
274
|
+
this.subscriptions.set(handle.subscriptionID, group);
|
|
275
|
+
}
|
|
276
|
+
group.handles.set(handle, endpoint);
|
|
277
|
+
return handle;
|
|
278
|
+
}));
|
|
279
|
+
}
|
|
280
|
+
async unsubscribe(handle) {
|
|
281
|
+
return this.subscriptionOperation(async () => {
|
|
282
|
+
const group = this.subscriptions.get(handle.subscriptionID);
|
|
283
|
+
if (!group?.handles.has(handle)) {
|
|
284
|
+
return;
|
|
285
|
+
}
|
|
286
|
+
if (group.handles.size > 1) {
|
|
287
|
+
group.handles.delete(handle);
|
|
288
|
+
return;
|
|
289
|
+
}
|
|
290
|
+
return (group.removing ??= this.request(Code.UNSUBSCRIBE, Code.UNSUBSCRIBED, { subscription_id: handle.subscriptionID }, () => {
|
|
291
|
+
this.subscriptions.delete(handle.subscriptionID);
|
|
292
|
+
}).catch((error) => {
|
|
293
|
+
group.removing = undefined;
|
|
294
|
+
throw error;
|
|
295
|
+
}));
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
subscriptionOperation(operation) {
|
|
299
|
+
const result = this.subscriptionOperations.then(operation);
|
|
300
|
+
this.subscriptionOperations = result.then(() => undefined, () => undefined);
|
|
301
|
+
return result;
|
|
302
|
+
}
|
|
303
|
+
async receiveLoop() {
|
|
304
|
+
try {
|
|
305
|
+
while (!this.closed) {
|
|
306
|
+
const message = inspectMessage(this.base.serializer().deserialize(await this.base.receive()));
|
|
307
|
+
if (!this.closed) {
|
|
308
|
+
this.receive(message);
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
catch (error) {
|
|
313
|
+
this.finish(error instanceof Error ? error.message : String(error), error);
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
receive(message) {
|
|
317
|
+
const code = message.type();
|
|
318
|
+
const fields = message.fields;
|
|
319
|
+
if (code === Code.GOODBYE) {
|
|
320
|
+
if (!this.closing) {
|
|
321
|
+
try {
|
|
322
|
+
this.send(Code.GOODBYE, { details: {}, reason: URI.CLOSE_GOOD_BYE_AND_OUT });
|
|
323
|
+
}
|
|
324
|
+
catch {
|
|
325
|
+
/* Peer may have closed already. */
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
this.finish(fields.reason);
|
|
329
|
+
return;
|
|
330
|
+
}
|
|
331
|
+
if (code === Code.INVOCATION) {
|
|
332
|
+
void this.invoke(fields).catch((error) => this.finish("Invocation failed", error));
|
|
333
|
+
return;
|
|
334
|
+
}
|
|
335
|
+
if (code === Code.INTERRUPT) {
|
|
336
|
+
this.interrupt(fields);
|
|
337
|
+
return;
|
|
338
|
+
}
|
|
339
|
+
if (code === Code.EVENT) {
|
|
340
|
+
const group = this.subscriptions.get(fields.subscription_id);
|
|
341
|
+
if (group) {
|
|
342
|
+
for (const endpoint of [...group.handles.values()]) {
|
|
343
|
+
void Promise.resolve()
|
|
344
|
+
.then(() => endpoint(new Event(fields.args ?? [], fields.kwargs ?? {}, fields.details ?? {})))
|
|
345
|
+
.catch((error) => this.report(error));
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
return;
|
|
349
|
+
}
|
|
350
|
+
const pending = this.pending.get(fields.request_id);
|
|
351
|
+
if (!pending) {
|
|
352
|
+
if (code === Code.RESULT || code === Code.ERROR || code === Code.PUBLISHED) {
|
|
353
|
+
return;
|
|
354
|
+
}
|
|
355
|
+
throw new ProtocolError(`Unexpected WAMP response ${code}`);
|
|
356
|
+
}
|
|
357
|
+
if (code === Code.ERROR) {
|
|
358
|
+
if (fields.message_type !== pending.kind) {
|
|
359
|
+
throw new ProtocolError("ERROR does not match its request");
|
|
360
|
+
}
|
|
361
|
+
this.settle(fields.request_id, undefined, new ApplicationError(fields.uri, { args: fields.args, kwargs: fields.kwargs }));
|
|
362
|
+
return;
|
|
363
|
+
}
|
|
364
|
+
if (code !== pending.expected) {
|
|
365
|
+
throw new ProtocolError("Response does not match its request");
|
|
366
|
+
}
|
|
367
|
+
if (code === Code.RESULT && fields.details?.progress === true) {
|
|
368
|
+
if (!pending.progressRequested) {
|
|
369
|
+
throw new ProtocolError("Received an unsolicited progressive result");
|
|
370
|
+
}
|
|
371
|
+
if (pending.final) {
|
|
372
|
+
throw new ProtocolError("Progress received after final result");
|
|
373
|
+
}
|
|
374
|
+
if (pending.receiver) {
|
|
375
|
+
const receiver = pending.receiver;
|
|
376
|
+
pending.progress = pending.progress
|
|
377
|
+
.then(() => receiver(new Result(fields.args, fields.kwargs, fields.details, (reference) => this.openStreamArgument(reference))))
|
|
378
|
+
.catch((error) => this.cancel(fields.request_id, error));
|
|
379
|
+
}
|
|
380
|
+
return;
|
|
381
|
+
}
|
|
382
|
+
if (pending.final) {
|
|
383
|
+
throw new ProtocolError("Duplicate final response");
|
|
384
|
+
}
|
|
385
|
+
pending.final = true;
|
|
386
|
+
if (code === Code.RESULT) {
|
|
387
|
+
void pending.progress
|
|
388
|
+
.then(() => {
|
|
389
|
+
if (this.pending.get(fields.request_id) === pending) {
|
|
390
|
+
this.settle(fields.request_id, pending.parse(fields));
|
|
391
|
+
}
|
|
392
|
+
})
|
|
393
|
+
.catch((error) => this.settle(fields.request_id, undefined, error));
|
|
394
|
+
}
|
|
395
|
+
else {
|
|
396
|
+
// Install callbacks synchronously before the next queued message is delivered.
|
|
397
|
+
this.settle(fields.request_id, pending.parse(fields));
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
interrupt(fields) {
|
|
401
|
+
const controller = this.invocations.get(fields.request_id);
|
|
402
|
+
if (controller) {
|
|
403
|
+
this.invocations.delete(fields.request_id);
|
|
404
|
+
controller.abort(new DOMException("Invocation cancelled", "AbortError"));
|
|
405
|
+
if (fields.options?.mode === "kill") {
|
|
406
|
+
this.send(Code.ERROR, {
|
|
407
|
+
message_type: Code.INVOCATION,
|
|
408
|
+
request_id: fields.request_id,
|
|
409
|
+
details: {},
|
|
410
|
+
uri: "wamp.error.canceled",
|
|
411
|
+
});
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
async invoke(fields) {
|
|
416
|
+
if (this.closing || this.closed) {
|
|
417
|
+
return;
|
|
418
|
+
}
|
|
419
|
+
const entry = this.registrations.get(fields.registration_id);
|
|
420
|
+
if (!entry) {
|
|
421
|
+
this.send(Code.ERROR, {
|
|
422
|
+
message_type: Code.INVOCATION,
|
|
423
|
+
request_id: fields.request_id,
|
|
424
|
+
details: {},
|
|
425
|
+
uri: URI.NO_SUCH_REGISTRATION,
|
|
426
|
+
});
|
|
427
|
+
return;
|
|
428
|
+
}
|
|
429
|
+
const controller = this.invocations.get(fields.request_id) ?? new AbortController();
|
|
430
|
+
this.invocations.set(fields.request_id, controller);
|
|
431
|
+
const progress = fields.details?.receive_progress
|
|
432
|
+
? (args, kwargs) => {
|
|
433
|
+
this.send(Code.YIELD, { request_id: fields.request_id, options: { progress: true }, args, kwargs });
|
|
434
|
+
}
|
|
435
|
+
: undefined;
|
|
436
|
+
try {
|
|
437
|
+
const result = await entry.endpoint(new Invocation(fields.args ?? [], fields.kwargs ?? {}, fields.details ?? {}, controller.signal, progress, (reference) => this.openStreamArgument(reference)));
|
|
438
|
+
if (this.closed || this.closing || controller.signal.aborted) {
|
|
439
|
+
return;
|
|
440
|
+
}
|
|
441
|
+
if (!result && fields.details?.progress === true) {
|
|
442
|
+
return;
|
|
443
|
+
}
|
|
444
|
+
const reply = result ?? new Result();
|
|
445
|
+
this.send(Code.YIELD, {
|
|
446
|
+
request_id: fields.request_id,
|
|
447
|
+
options: reply.details,
|
|
448
|
+
args: reply.args,
|
|
449
|
+
kwargs: reply.kwargs,
|
|
450
|
+
});
|
|
451
|
+
}
|
|
452
|
+
catch (error) {
|
|
453
|
+
if (!this.closed && !this.closing && !controller.signal.aborted) {
|
|
454
|
+
try {
|
|
455
|
+
const application = error instanceof ApplicationError
|
|
456
|
+
? error
|
|
457
|
+
: new ApplicationError("wamp.error.runtime_error", { args: [String(error)] });
|
|
458
|
+
this.send(Code.ERROR, {
|
|
459
|
+
message_type: Code.INVOCATION,
|
|
460
|
+
request_id: fields.request_id,
|
|
461
|
+
details: {},
|
|
462
|
+
uri: application.message,
|
|
463
|
+
args: application.args,
|
|
464
|
+
kwargs: application.kwargs,
|
|
465
|
+
});
|
|
466
|
+
}
|
|
467
|
+
catch (error) {
|
|
468
|
+
this.finish("Invocation reply failed", error);
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
finally {
|
|
473
|
+
if (!fields.details?.progress) {
|
|
474
|
+
this.invocations.delete(fields.request_id);
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
finish(reason, error = new ProtocolError(reason)) {
|
|
479
|
+
if (this.closed) {
|
|
480
|
+
return;
|
|
481
|
+
}
|
|
482
|
+
this.closed = true;
|
|
483
|
+
for (const [id] of this.pending) {
|
|
484
|
+
this.settle(id, undefined, error);
|
|
485
|
+
}
|
|
486
|
+
for (const controller of this.invocations.values()) {
|
|
487
|
+
controller.abort(error);
|
|
488
|
+
}
|
|
489
|
+
this.invocations.clear();
|
|
490
|
+
this.registrations.clear();
|
|
491
|
+
this.subscriptions.clear();
|
|
492
|
+
this.ids.free();
|
|
493
|
+
this.resolveClosed();
|
|
494
|
+
for (const callback of this.disconnectCallbacks.splice(0)) {
|
|
495
|
+
void Promise.resolve()
|
|
496
|
+
.then(() => callback(reason))
|
|
497
|
+
.catch((error) => this.report(error));
|
|
498
|
+
}
|
|
499
|
+
void this.base.close().catch((error) => this.report(error));
|
|
500
|
+
}
|
|
501
|
+
close() {
|
|
502
|
+
return (this.closePromise ??= this.closeSession());
|
|
503
|
+
}
|
|
504
|
+
async closeSession() {
|
|
505
|
+
if (this.closed) {
|
|
506
|
+
return;
|
|
507
|
+
}
|
|
508
|
+
this.closing = true;
|
|
509
|
+
for (const [id] of this.pending) {
|
|
510
|
+
this.settle(id, undefined, new ProtocolError("Session is closing"));
|
|
511
|
+
}
|
|
512
|
+
let timer;
|
|
513
|
+
try {
|
|
514
|
+
this.send(Code.GOODBYE, { details: {}, reason: URI.CLOSE_REALM });
|
|
515
|
+
await Promise.race([
|
|
516
|
+
this.closedPromise,
|
|
517
|
+
new Promise((resolve) => {
|
|
518
|
+
timer = setTimeout(resolve, this.closeTimeout);
|
|
519
|
+
}),
|
|
520
|
+
]);
|
|
521
|
+
}
|
|
522
|
+
finally {
|
|
523
|
+
clearTimeout(timer);
|
|
524
|
+
this.finish("Session closed");
|
|
525
|
+
await this.base.close();
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
}
|