@ikonai/sdk 1.10.0 → 1.11.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/app-messaging.d.ts +2 -1
- package/app-messaging.d.ts.map +1 -0
- package/assets/audio-playback-worker-BA5LbdyM.js +1 -0
- package/assets/protocol-worker-Byc-OmNW.js +3 -0
- package/assets/video-capture-worker-DRYxkwR0.js +1 -0
- package/assets/video-playback-worker-v179IJ-C.js +1 -0
- package/assets/viseme-worker-CmvzHAbf.js +1 -0
- package/audio-constants-0i9B4zLJ.js +4 -0
- package/backend/account-functions.d.ts +22 -0
- package/backend/account-functions.d.ts.map +1 -0
- package/backend/user-account.d.ts +58 -0
- package/backend/user-account.d.ts.map +1 -0
- package/capabilities-ljRyAKpA.js +248 -0
- package/capture/capture-functions.d.ts +34 -0
- package/capture/capture-functions.d.ts.map +1 -0
- package/capture/view-capture.d.ts +35 -0
- package/capture/view-capture.d.ts.map +1 -0
- package/channel/channel-manager.d.ts +3 -2
- package/channel/channel-manager.d.ts.map +1 -0
- package/channel/channel.d.ts +6 -2
- package/channel/channel.d.ts.map +1 -0
- package/channel/index.d.ts +1 -0
- package/channel/index.d.ts.map +1 -0
- package/channel-manager-ehgy3BJp.js +878 -0
- package/client/connection-state.d.ts +1 -0
- package/client/connection-state.d.ts.map +1 -0
- package/client/endpoint-selector.d.ts +2 -1
- package/client/endpoint-selector.d.ts.map +1 -0
- package/client/ikon-client-config.d.ts +14 -1
- package/client/ikon-client-config.d.ts.map +1 -0
- package/client/ikon-client.d.ts +122 -4
- package/client/ikon-client.d.ts.map +1 -0
- package/connection/authenticator.d.ts +6 -1
- package/connection/authenticator.d.ts.map +1 -0
- package/connection/urls.d.ts +1 -0
- package/connection/urls.d.ts.map +1 -0
- package/consent/consent-api.d.ts +61 -0
- package/consent/consent-api.d.ts.map +1 -0
- package/dist-BrkppKKs.js +801 -0
- package/errors/index.d.ts +11 -0
- package/errors/index.d.ts.map +1 -0
- package/feedback/feedback-api.d.ts +63 -0
- package/feedback/feedback-api.d.ts.map +1 -0
- package/functions/browser-functions.d.ts +2 -1
- package/functions/browser-functions.d.ts.map +1 -0
- package/functions/download-functions.d.ts +12 -0
- package/functions/download-functions.d.ts.map +1 -0
- package/functions/function-registry.d.ts +2 -1
- package/functions/function-registry.d.ts.map +1 -0
- package/functions/index.d.ts +2 -1
- package/functions/index.d.ts.map +1 -0
- package/functions/media-capture-functions.d.ts +1 -0
- package/functions/media-capture-functions.d.ts.map +1 -0
- package/functions/sound-functions.d.ts +3 -1
- package/functions/sound-functions.d.ts.map +1 -0
- package/functions/type-conversion.d.ts +1 -0
- package/functions/type-conversion.d.ts.map +1 -0
- package/functions/types.d.ts +2 -1
- package/functions/types.d.ts.map +1 -0
- package/ikon-media-BQjL59S_.js +2074 -0
- package/ikon-media-capture-BiGydwdg.js +1422 -0
- package/index.d.ts +23 -10
- package/index.d.ts.map +1 -0
- package/index.js +6709 -10102
- package/log-type-DewKVFaU.js +832 -0
- package/logger-gjegmH6K.js +80 -0
- package/login/index.d.ts +1 -0
- package/login/index.d.ts.map +1 -0
- package/login/login-error-interceptor.d.ts +1 -0
- package/login/login-error-interceptor.d.ts.map +1 -0
- package/login/login-store.d.ts +1 -0
- package/login/login-store.d.ts.map +1 -0
- package/login/reprovision.d.ts +1 -0
- package/login/reprovision.d.ts.map +1 -0
- package/media/audio-constants.d.ts +1 -0
- package/media/audio-constants.d.ts.map +1 -0
- package/media/capabilities.d.ts +1 -0
- package/media/capabilities.d.ts.map +1 -0
- package/media/device-detection.d.ts +1 -0
- package/media/device-detection.d.ts.map +1 -0
- package/media/ikon-audio-capture.d.ts +1 -0
- package/media/ikon-audio-capture.d.ts.map +1 -0
- package/media/ikon-audio-playback.d.ts +2 -1
- package/media/ikon-audio-playback.d.ts.map +1 -0
- package/media/ikon-image-capture.d.ts +1 -0
- package/media/ikon-image-capture.d.ts.map +1 -0
- package/media/ikon-media-capture.d.ts +1 -0
- package/media/ikon-media-capture.d.ts.map +1 -0
- package/media/ikon-media.d.ts +1 -0
- package/media/ikon-media.d.ts.map +1 -0
- package/media/ikon-video-capture.d.ts +1 -0
- package/media/ikon-video-capture.d.ts.map +1 -0
- package/media/ikon-video-playback.d.ts +2 -1
- package/media/ikon-video-playback.d.ts.map +1 -0
- package/media/index.d.ts +1 -0
- package/media/index.d.ts.map +1 -0
- package/media/media-permission.d.ts +1 -0
- package/media/media-permission.d.ts.map +1 -0
- package/media/media-session-helper.d.ts +1 -0
- package/media/media-session-helper.d.ts.map +1 -0
- package/media/ring-buffer.d.ts +1 -0
- package/media/ring-buffer.d.ts.map +1 -0
- package/media/video-constants.d.ts +1 -0
- package/media/video-constants.d.ts.map +1 -0
- package/media/viseme-tracker.d.ts +2 -1
- package/media/viseme-tracker.d.ts.map +1 -0
- package/media-session-helper-Do-OxyU6.js +51 -0
- package/package.json +1 -1
- package/reactive/reactive-registry.d.ts +1 -0
- package/reactive/reactive-registry.d.ts.map +1 -0
- package/sha2-DPTGfEM2.js +261 -0
- package/storage.d.ts +1 -0
- package/storage.d.ts.map +1 -0
- package/transport/http-stream-transport.d.ts +16 -1
- package/transport/http-stream-transport.d.ts.map +1 -0
- package/transport/keepalive-watchdog.d.ts +19 -0
- package/transport/keepalive-watchdog.d.ts.map +1 -0
- package/transport/message-framer.d.ts +1 -0
- package/transport/message-framer.d.ts.map +1 -0
- package/transport/transport-probe.d.ts +2 -1
- package/transport/transport-probe.d.ts.map +1 -0
- package/transport/transport.d.ts +9 -1
- package/transport/transport.d.ts.map +1 -0
- package/transport/web-socket-transport.d.ts +3 -1
- package/transport/web-socket-transport.d.ts.map +1 -0
- package/transport/web-transport-support.d.ts +5 -0
- package/transport/web-transport-support.d.ts.map +1 -0
- package/transport/web-transport-transport.d.ts +3 -8
- package/transport/web-transport-transport.d.ts.map +1 -0
- package/upload/file-upload.d.ts +62 -0
- package/upload/file-upload.d.ts.map +1 -0
- package/utils/abort-signal.d.ts +1 -0
- package/utils/abort-signal.d.ts.map +1 -0
- package/utils/app-routes.d.ts +1 -0
- package/utils/app-routes.d.ts.map +1 -0
- package/utils/boot-signal.d.ts +1 -0
- package/utils/boot-signal.d.ts.map +1 -0
- package/utils/browser-compat.d.ts +1 -0
- package/utils/browser-compat.d.ts.map +1 -0
- package/utils/browser-support.d.ts +1 -0
- package/utils/browser-support.d.ts.map +1 -0
- package/utils/client-identity.d.ts +1 -0
- package/utils/client-identity.d.ts.map +1 -0
- package/utils/connect-timeline.d.ts +1 -0
- package/utils/connect-timeline.d.ts.map +1 -0
- package/utils/debug-mode.d.ts +1 -0
- package/utils/debug-mode.d.ts.map +1 -0
- package/utils/deferred.d.ts +1 -0
- package/utils/deferred.d.ts.map +1 -0
- package/utils/delay.d.ts +1 -0
- package/utils/delay.d.ts.map +1 -0
- package/utils/embedded-oauth.d.ts +1 -0
- package/utils/embedded-oauth.d.ts.map +1 -0
- package/utils/external-link.d.ts +1 -0
- package/utils/external-link.d.ts.map +1 -0
- package/utils/inspect-mode.d.ts +1 -0
- package/utils/inspect-mode.d.ts.map +1 -0
- package/utils/log-sender.d.ts +1 -0
- package/utils/log-sender.d.ts.map +1 -0
- package/utils/log-sink.d.ts +1 -0
- package/utils/log-sink.d.ts.map +1 -0
- package/utils/logger.d.ts +1 -0
- package/utils/logger.d.ts.map +1 -0
- package/utils/oauth-start-url.d.ts +22 -0
- package/utils/oauth-start-url.d.ts.map +1 -0
- package/utils/opcode-names.d.ts +1 -0
- package/utils/opcode-names.d.ts.map +1 -0
- package/utils/parent-log-forward.d.ts +1 -0
- package/utils/parent-log-forward.d.ts.map +1 -0
- package/utils/pkce.d.ts +1 -0
- package/utils/pkce.d.ts.map +1 -0
- package/utils/platform.d.ts +1 -0
- package/utils/platform.d.ts.map +1 -0
- package/utils/query-params.d.ts +21 -0
- package/utils/query-params.d.ts.map +1 -0
- package/utils/user-gesture.d.ts +1 -0
- package/utils/user-gesture.d.ts.map +1 -0
- package/utils/uuid.d.ts +1 -0
- package/utils/uuid.d.ts.map +1 -0
- package/viseme-tracker-CAtczW00.js +183 -0
- package/web-transport-support-Cdyk4mQe.js +229 -0
- package/webrtc/index.d.ts +1 -0
- package/webrtc/index.d.ts.map +1 -0
- package/webrtc/webrtc-config.d.ts +1 -0
- package/webrtc/webrtc-config.d.ts.map +1 -0
- package/webrtc/webrtc-connection.d.ts +10 -0
- package/webrtc/webrtc-connection.d.ts.map +1 -0
- package/webrtc/webrtc-reachability.d.ts +1 -0
- package/webrtc/webrtc-reachability.d.ts.map +1 -0
- package/webrtc/webrtc-signaling.d.ts +12 -1
- package/webrtc/webrtc-signaling.d.ts.map +1 -0
- package/worker/audio-capture-worker.d.ts +1 -0
- package/worker/audio-capture-worker.d.ts.map +1 -0
- package/worker/audio-playback-worker.d.ts +2 -1
- package/worker/audio-playback-worker.d.ts.map +1 -0
- package/worker/protocol-worker.d.ts +1 -0
- package/worker/protocol-worker.d.ts.map +1 -0
- package/worker/video-capture-worker.d.ts +1 -0
- package/worker/video-capture-worker.d.ts.map +1 -0
- package/worker/video-playback-worker.d.ts +2 -1
- package/worker/video-playback-worker.d.ts.map +1 -0
- package/worker/viseme-worker.d.ts +2 -1
- package/worker/viseme-worker.d.ts.map +1 -0
- package/assets/audio-playback-worker-D9_GcJw4.js +0 -1
- package/assets/protocol-worker-DA6wieqV.js +0 -3
- package/assets/video-capture-worker-DLndrZf8.js +0 -1
- package/assets/video-playback-worker-DAAVfeBm.js +0 -1
- package/assets/viseme-worker-Bfd95xP6.js +0 -1
- package/capabilities-lcsAlVUh.js +0 -1146
- package/vite.config.d.ts +0 -2
|
@@ -0,0 +1,878 @@
|
|
|
1
|
+
import { C as e, S as t, _ as n, a as r, b as i, m as a, x as o, y as s } from "./log-type-DewKVFaU.js";
|
|
2
|
+
import { c, n as l } from "./logger-gjegmH6K.js";
|
|
3
|
+
import { $ as u, I as d, J as f, ct as p, et as m, n as h, t as g, tt as _ } from "./web-transport-support-Cdyk4mQe.js";
|
|
4
|
+
var v = 131074;
|
|
5
|
+
function y(e) {
|
|
6
|
+
let t = {};
|
|
7
|
+
return e && Object.assign(t, e), t;
|
|
8
|
+
}
|
|
9
|
+
function b(e) {
|
|
10
|
+
return new n(1).finish();
|
|
11
|
+
}
|
|
12
|
+
function x(e, t, n) {
|
|
13
|
+
let r = b(e);
|
|
14
|
+
return i(v, r, 1, t, n);
|
|
15
|
+
}
|
|
16
|
+
//#endregion
|
|
17
|
+
//#region src/transport/keepalive-watchdog.ts
|
|
18
|
+
var S = l("KeepaliveWatchdog"), C = class {
|
|
19
|
+
timeoutMs;
|
|
20
|
+
what;
|
|
21
|
+
onTimeout;
|
|
22
|
+
timeout = null;
|
|
23
|
+
lastResetAt = 0;
|
|
24
|
+
constructor(e, t, n) {
|
|
25
|
+
this.timeoutMs = e, this.what = t, this.onTimeout = n;
|
|
26
|
+
}
|
|
27
|
+
reset() {
|
|
28
|
+
this.clear(), this.lastResetAt = Date.now(), !(this.timeoutMs <= 0) && (this.timeout = setTimeout(() => {
|
|
29
|
+
S.error(`No keepalive received in ${this.timeoutMs}ms, closing ${this.what}`), this.fire(this.timeoutMs);
|
|
30
|
+
}, this.timeoutMs));
|
|
31
|
+
}
|
|
32
|
+
checkNow() {
|
|
33
|
+
if (this.timeout === null || this.timeoutMs <= 0) return;
|
|
34
|
+
let e = Date.now() - this.lastResetAt;
|
|
35
|
+
e > this.timeoutMs && (S.error(`Keepalive cadence missed while the page was suspended (last received ${e}ms ago, limit ${this.timeoutMs}ms), closing ${this.what}`), this.fire(this.timeoutMs));
|
|
36
|
+
}
|
|
37
|
+
checkWithin(e) {
|
|
38
|
+
if (this.lastResetAt === 0 || e <= 0) return;
|
|
39
|
+
let t = this.timeoutMs > 0 ? Math.min(e, this.timeoutMs) : e, n = Date.now() - this.lastResetAt;
|
|
40
|
+
if (n >= t) {
|
|
41
|
+
S.error(`No keepalive in ${n}ms against a ${t}ms budget, closing ${this.what}`), this.fire(t);
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
this.clear(), this.timeout = setTimeout(() => {
|
|
45
|
+
S.error(`No keepalive received within the ${t}ms budget, closing ${this.what}`), this.fire(t);
|
|
46
|
+
}, t - n);
|
|
47
|
+
}
|
|
48
|
+
clear() {
|
|
49
|
+
this.timeout &&= (clearTimeout(this.timeout), null);
|
|
50
|
+
}
|
|
51
|
+
fire(e) {
|
|
52
|
+
this.clear(), this.onTimeout(new u(e));
|
|
53
|
+
}
|
|
54
|
+
}, w = 4, T = 65536, E = class {
|
|
55
|
+
buffer = new Uint8Array(T);
|
|
56
|
+
bufferOffset = 0;
|
|
57
|
+
bufferLength = 0;
|
|
58
|
+
dataView = null;
|
|
59
|
+
dataViewBuffer = null;
|
|
60
|
+
push(e, t) {
|
|
61
|
+
this.append(e);
|
|
62
|
+
let n = this.getDataView();
|
|
63
|
+
for (; this.bufferLength >= w;) {
|
|
64
|
+
let e = n.getUint32(this.bufferOffset, !0);
|
|
65
|
+
if (e < w || e > 20971520) throw this.reset(), Error(`Invalid protocol message length: ${e}`);
|
|
66
|
+
if (this.bufferLength < e) return;
|
|
67
|
+
let r = this.buffer.subarray(this.bufferOffset, this.bufferOffset + e);
|
|
68
|
+
this.bufferOffset += e, this.bufferLength -= e, t(r);
|
|
69
|
+
}
|
|
70
|
+
this.bufferLength === 0 && (this.bufferOffset = 0);
|
|
71
|
+
}
|
|
72
|
+
reset() {
|
|
73
|
+
this.bufferOffset = 0, this.bufferLength = 0;
|
|
74
|
+
}
|
|
75
|
+
append(e) {
|
|
76
|
+
let t = this.bufferLength + e.length, n = this.bufferOffset + this.bufferLength;
|
|
77
|
+
if (n + e.length > this.buffer.length) {
|
|
78
|
+
if (t > this.buffer.length) {
|
|
79
|
+
let e = Math.max(t, this.buffer.length * 2), r = new Uint8Array(e);
|
|
80
|
+
r.set(this.buffer.subarray(this.bufferOffset, n)), this.buffer = r, this.bufferOffset = 0, this.dataViewBuffer = null;
|
|
81
|
+
} else this.buffer.copyWithin(0, this.bufferOffset, n), this.bufferOffset = 0;
|
|
82
|
+
}
|
|
83
|
+
this.buffer.set(e, this.bufferOffset + this.bufferLength), this.bufferLength += e.length;
|
|
84
|
+
}
|
|
85
|
+
getDataView() {
|
|
86
|
+
let e = this.buffer.buffer;
|
|
87
|
+
return this.dataViewBuffer !== e && (this.dataViewBuffer = e, this.dataView = new DataView(e)), this.dataView;
|
|
88
|
+
}
|
|
89
|
+
}, D = l("HttpStreamTransport"), O = 1e4, k = 3500, A = 1048576, j = 16, M = [
|
|
90
|
+
"binary",
|
|
91
|
+
"sse",
|
|
92
|
+
"poll"
|
|
93
|
+
], N = class {
|
|
94
|
+
framer = new E();
|
|
95
|
+
callbacks;
|
|
96
|
+
sessionId;
|
|
97
|
+
keepaliveTimeoutMs;
|
|
98
|
+
connectionTimeoutMs;
|
|
99
|
+
pinnedMode;
|
|
100
|
+
preferredMode;
|
|
101
|
+
baseUri = "";
|
|
102
|
+
sid = "";
|
|
103
|
+
mode = "binary";
|
|
104
|
+
downstreamAbort = null;
|
|
105
|
+
keepalive = null;
|
|
106
|
+
keepaliveResponseMessage = null;
|
|
107
|
+
isClosed = !1;
|
|
108
|
+
connected = !1;
|
|
109
|
+
connecting = !1;
|
|
110
|
+
sendQueue = [];
|
|
111
|
+
sendPumpRunning = !1;
|
|
112
|
+
firstMessageResolver = null;
|
|
113
|
+
constructor(e) {
|
|
114
|
+
this.callbacks = e.callbacks, this.sessionId = e.sessionId, this.keepaliveTimeoutMs = e.keepaliveTimeoutMs, this.connectionTimeoutMs = e.connectionTimeoutMs ?? O, this.pinnedMode = e.httpStreamMode, this.preferredMode = e.httpStreamPreferredMode;
|
|
115
|
+
}
|
|
116
|
+
get isConnected() {
|
|
117
|
+
return this.connected && !this.isClosed;
|
|
118
|
+
}
|
|
119
|
+
get activeMode() {
|
|
120
|
+
return this.mode;
|
|
121
|
+
}
|
|
122
|
+
async connect(e, t) {
|
|
123
|
+
this.isClosed = !1, this.connecting = !0, this.baseUri = e;
|
|
124
|
+
let n = Date.now(), r = null, i = this.pinnedMode ? [this.pinnedMode] : P(this.preferredMode);
|
|
125
|
+
for (let e of i) {
|
|
126
|
+
if (this.isClosed) throw new p("HTTP stream closed while connecting");
|
|
127
|
+
try {
|
|
128
|
+
this.sid = I(), await this.openMode(t, e), this.mode = e, this.connected = !0, this.connecting = !1, this.resetKeepaliveTimeout(), D.debug(`HTTP stream connected in ${e} mode in ${Date.now() - n}ms`);
|
|
129
|
+
return;
|
|
130
|
+
} catch (t) {
|
|
131
|
+
if (r = t instanceof Error ? t : Error(String(t)), this.abortDownstream(), t instanceof f) throw this.connecting = !1, t;
|
|
132
|
+
D.debug(`HTTP stream ${e} mode failed after ${Date.now() - n}ms: ${r.message}`);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
throw this.connecting = !1, new p(`HTTP stream failed in every mode: ${r?.message ?? "unknown error"}`, r ?? void 0);
|
|
136
|
+
}
|
|
137
|
+
send(e) {
|
|
138
|
+
if (this.isClosed) {
|
|
139
|
+
D.warn("Cannot send: HTTP stream closed");
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
this.sendQueue.push(e), this.runSendPump();
|
|
143
|
+
}
|
|
144
|
+
close() {
|
|
145
|
+
this.isClosed = !0, this.connected = !1, this.clearKeepaliveTimeout(), this.abortDownstream(), this.sendQueue.length = 0;
|
|
146
|
+
}
|
|
147
|
+
async openMode(e, t) {
|
|
148
|
+
let n = new AbortController();
|
|
149
|
+
this.downstreamAbort = n;
|
|
150
|
+
let r = Math.min(k, this.connectionTimeoutMs), i = setTimeout(() => n.abort(), r);
|
|
151
|
+
try {
|
|
152
|
+
await this.openChannel(e, t, n.signal), await this.openDownstream(t, n, r);
|
|
153
|
+
} finally {
|
|
154
|
+
clearTimeout(i);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
async openChannel(e, t, n) {
|
|
158
|
+
let r;
|
|
159
|
+
try {
|
|
160
|
+
r = await fetch(this.openUrl(t), {
|
|
161
|
+
method: "POST",
|
|
162
|
+
headers: { "Content-Type": "text/plain" },
|
|
163
|
+
body: L(e),
|
|
164
|
+
cache: "no-store",
|
|
165
|
+
credentials: "omit",
|
|
166
|
+
signal: n
|
|
167
|
+
});
|
|
168
|
+
} catch (e) {
|
|
169
|
+
throw n.aborted ? new p("HTTP stream channel did not open before the mode deadline") : e;
|
|
170
|
+
}
|
|
171
|
+
if (r.status === 401 || r.status === 403) throw new f(`HTTP stream rejected the auth ticket (${r.status})`);
|
|
172
|
+
if (!r.ok) throw new p(`HTTP stream could not be opened, status ${r.status}`);
|
|
173
|
+
}
|
|
174
|
+
async openDownstream(e, t, n) {
|
|
175
|
+
this.framer.reset();
|
|
176
|
+
let r = new Promise((e, r) => {
|
|
177
|
+
let i = () => {
|
|
178
|
+
this.firstMessageResolver = null, r(new p(`No server message in ${n}ms`));
|
|
179
|
+
};
|
|
180
|
+
t.signal.addEventListener("abort", i, { once: !0 }), this.firstMessageResolver = () => {
|
|
181
|
+
t.signal.removeEventListener("abort", i), this.firstMessageResolver = null, e();
|
|
182
|
+
};
|
|
183
|
+
}), i = await fetch(this.streamUrl(e), {
|
|
184
|
+
method: "GET",
|
|
185
|
+
headers: { Accept: e === "sse" ? "text/event-stream" : "application/octet-stream" },
|
|
186
|
+
cache: "no-store",
|
|
187
|
+
credentials: "omit",
|
|
188
|
+
signal: t.signal
|
|
189
|
+
});
|
|
190
|
+
if (i.status === 401 || i.status === 403) throw new f(`HTTP stream rejected the auth ticket (${i.status})`);
|
|
191
|
+
if (!i.ok || !i.body) throw new p(`HTTP stream request failed with status ${i.status}`);
|
|
192
|
+
this.runDownstreamLoop(e, i, t), await r;
|
|
193
|
+
}
|
|
194
|
+
async runDownstreamLoop(e, t, n) {
|
|
195
|
+
let r = t;
|
|
196
|
+
try {
|
|
197
|
+
for (; r && !this.isClosed && !n.signal.aborted;) {
|
|
198
|
+
if (await this.readResponse(e, r), e !== "poll") {
|
|
199
|
+
this.isClosed || this.handleClose("HTTP stream ended", !1);
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
if (r = await fetch(this.streamUrl(e), {
|
|
203
|
+
method: "GET",
|
|
204
|
+
headers: { Accept: "application/octet-stream" },
|
|
205
|
+
cache: "no-store",
|
|
206
|
+
credentials: "omit",
|
|
207
|
+
signal: n.signal
|
|
208
|
+
}), r.status === 410) {
|
|
209
|
+
this.handleClose("HTTP stream channel is gone", !1);
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
if (!r.ok || !r.body) {
|
|
213
|
+
this.handleClose(`HTTP stream poll failed with status ${r.status}`, !1);
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
} catch (e) {
|
|
218
|
+
if (n.signal.aborted || this.isClosed) return;
|
|
219
|
+
D.debug(`HTTP stream read loop ended: ${e instanceof Error ? e.message : e}`), this.handleClose("HTTP stream read failed", !1);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
async readResponse(e, t) {
|
|
223
|
+
let n = t.body.getReader(), r = e === "sse" ? new F() : null;
|
|
224
|
+
for (;;) {
|
|
225
|
+
let { value: e, done: t } = await n.read();
|
|
226
|
+
if (t) return;
|
|
227
|
+
if (e) {
|
|
228
|
+
if (r) {
|
|
229
|
+
r.push(e, (e) => this.framer.push(e, (e) => this.handleProtocolMessage(e)));
|
|
230
|
+
continue;
|
|
231
|
+
}
|
|
232
|
+
this.framer.push(e, (e) => this.handleProtocolMessage(e));
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
async runSendPump() {
|
|
237
|
+
if (!this.sendPumpRunning) {
|
|
238
|
+
this.sendPumpRunning = !0;
|
|
239
|
+
try {
|
|
240
|
+
for (; this.sendQueue.length > 0 && !this.isClosed;) {
|
|
241
|
+
let e = this.takeBatch(), t = await fetch(this.sendUrl(), {
|
|
242
|
+
method: "POST",
|
|
243
|
+
headers: { "Content-Type": "text/plain" },
|
|
244
|
+
body: e,
|
|
245
|
+
cache: "no-store",
|
|
246
|
+
credentials: "omit"
|
|
247
|
+
});
|
|
248
|
+
if (t.status === 410) {
|
|
249
|
+
this.handleClose("HTTP stream channel is gone", !1);
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
252
|
+
if (!t.ok) throw new p(`HTTP stream send failed with status ${t.status}`);
|
|
253
|
+
}
|
|
254
|
+
} catch (e) {
|
|
255
|
+
if (this.isClosed) return;
|
|
256
|
+
D.warn(`HTTP stream send failed: ${e instanceof Error ? e.message : e}`), this.handleClose("HTTP stream send failed", !1);
|
|
257
|
+
} finally {
|
|
258
|
+
this.sendPumpRunning = !1;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
takeBatch() {
|
|
263
|
+
let e = 0, t = 0;
|
|
264
|
+
for (; t < this.sendQueue.length;) {
|
|
265
|
+
let n = this.sendQueue[t].length;
|
|
266
|
+
if (t > 0 && e + n > A) break;
|
|
267
|
+
e += n, t++;
|
|
268
|
+
}
|
|
269
|
+
let n = new Uint8Array(e), r = 0;
|
|
270
|
+
for (let e = 0; e < t; e++) n.set(this.sendQueue[e], r), r += this.sendQueue[e].length;
|
|
271
|
+
return this.sendQueue.splice(0, t), n;
|
|
272
|
+
}
|
|
273
|
+
streamUrl(e) {
|
|
274
|
+
return `${this.baseUri}${this.baseUri.includes("?") ? "&" : "?"}sid=${this.sid}&mode=${e}`;
|
|
275
|
+
}
|
|
276
|
+
openUrl(e) {
|
|
277
|
+
return `${this.streamUrl(e)}&open=1`;
|
|
278
|
+
}
|
|
279
|
+
sendUrl() {
|
|
280
|
+
return `${this.baseUri}${this.baseUri.includes("?") ? "&" : "?"}sid=${this.sid}`;
|
|
281
|
+
}
|
|
282
|
+
getKeepaliveResponse() {
|
|
283
|
+
return this.keepaliveResponseMessage ||= x(y(), this.sessionId), this.keepaliveResponseMessage;
|
|
284
|
+
}
|
|
285
|
+
handleProtocolMessage(t) {
|
|
286
|
+
try {
|
|
287
|
+
let n = e(t).opcode;
|
|
288
|
+
if (this.firstMessageResolver && this.firstMessageResolver(), n === c.KEEPALIVE_REQUEST) {
|
|
289
|
+
this.send(this.getKeepaliveResponse()), this.resetKeepaliveTimeout();
|
|
290
|
+
return;
|
|
291
|
+
}
|
|
292
|
+
this.resetKeepaliveTimeout(), this.callbacks.onProtocolMessage(t.slice());
|
|
293
|
+
} catch (e) {
|
|
294
|
+
D.error("Failed to process protocol message:", e), this.callbacks.onError(e instanceof Error ? e : Error(String(e)));
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
handleClose(e, t) {
|
|
298
|
+
this.isClosed || this.connecting || (this.isClosed = !0, this.connected = !1, this.clearKeepaliveTimeout(), this.abortDownstream(), this.callbacks.onClose(e, t));
|
|
299
|
+
}
|
|
300
|
+
abortDownstream() {
|
|
301
|
+
this.downstreamAbort &&= (this.downstreamAbort.abort(), null);
|
|
302
|
+
}
|
|
303
|
+
checkKeepaliveNow() {
|
|
304
|
+
this.keepalive?.checkNow();
|
|
305
|
+
}
|
|
306
|
+
checkKeepaliveWithin(e) {
|
|
307
|
+
this.keepalive?.checkWithin(e);
|
|
308
|
+
}
|
|
309
|
+
resetKeepaliveTimeout() {
|
|
310
|
+
this.keepalive ??= new C(this.keepaliveTimeoutMs, "HTTP stream", (e) => {
|
|
311
|
+
this.callbacks.onError(e), this.close();
|
|
312
|
+
}), this.keepalive.reset();
|
|
313
|
+
}
|
|
314
|
+
clearKeepaliveTimeout() {
|
|
315
|
+
this.keepalive?.clear();
|
|
316
|
+
}
|
|
317
|
+
};
|
|
318
|
+
function P(e) {
|
|
319
|
+
return e ? [e, ...M.filter((t) => t !== e)] : M;
|
|
320
|
+
}
|
|
321
|
+
var F = class e {
|
|
322
|
+
static MaxLineLength = Math.ceil(a / 3) * 4 + 16;
|
|
323
|
+
decoder = new TextDecoder();
|
|
324
|
+
pending = "";
|
|
325
|
+
push(e, t) {
|
|
326
|
+
this.pending += this.decoder.decode(e, { stream: !0 });
|
|
327
|
+
let n = this.pending.indexOf("\n");
|
|
328
|
+
for (; n !== -1;) {
|
|
329
|
+
let e = this.pending.slice(0, n);
|
|
330
|
+
this.pending = this.pending.slice(n + 1), this.assertWithinLineBound(e.length), e.startsWith("data: ") && t(R(e.slice(6))), n = this.pending.indexOf("\n");
|
|
331
|
+
}
|
|
332
|
+
this.assertWithinLineBound(this.pending.length);
|
|
333
|
+
}
|
|
334
|
+
assertWithinLineBound(t) {
|
|
335
|
+
if (t > e.MaxLineLength) throw this.pending = "", Error(`HTTP stream line exceeds ${e.MaxLineLength} characters`);
|
|
336
|
+
}
|
|
337
|
+
};
|
|
338
|
+
function I() {
|
|
339
|
+
let e = new Uint8Array(j);
|
|
340
|
+
crypto.getRandomValues(e);
|
|
341
|
+
let t = "";
|
|
342
|
+
for (let n of e) t += n.toString(16).padStart(2, "0");
|
|
343
|
+
return t;
|
|
344
|
+
}
|
|
345
|
+
function L(e) {
|
|
346
|
+
let t = "";
|
|
347
|
+
for (let n of e) t += String.fromCharCode(n);
|
|
348
|
+
return btoa(t);
|
|
349
|
+
}
|
|
350
|
+
function R(e) {
|
|
351
|
+
let t = atob(e), n = new Uint8Array(t.length);
|
|
352
|
+
for (let e = 0; e < t.length; e++) n[e] = t.charCodeAt(e);
|
|
353
|
+
return n;
|
|
354
|
+
}
|
|
355
|
+
//#endregion
|
|
356
|
+
//#region src/transport/web-socket-transport.ts
|
|
357
|
+
var z = l("WebSocketTransport"), B = 1e4, V = class {
|
|
358
|
+
ws = null;
|
|
359
|
+
keepalive = null;
|
|
360
|
+
isClosed = !1;
|
|
361
|
+
keepaliveResponseMessage = null;
|
|
362
|
+
firstMessageResolver = null;
|
|
363
|
+
keepaliveTimeoutMs;
|
|
364
|
+
connectionTimeoutMs;
|
|
365
|
+
callbacks;
|
|
366
|
+
sessionId;
|
|
367
|
+
constructor(e) {
|
|
368
|
+
this.callbacks = e.callbacks, this.sessionId = e.sessionId, this.keepaliveTimeoutMs = e.keepaliveTimeoutMs, this.connectionTimeoutMs = e.connectionTimeoutMs ?? B;
|
|
369
|
+
}
|
|
370
|
+
get isConnected() {
|
|
371
|
+
return this.ws?.readyState === WebSocket.OPEN;
|
|
372
|
+
}
|
|
373
|
+
async connect(e, t) {
|
|
374
|
+
this.isClosed = !1;
|
|
375
|
+
let n = Date.now();
|
|
376
|
+
return new Promise((r, i) => {
|
|
377
|
+
try {
|
|
378
|
+
this.ws = new WebSocket(e), this.ws.binaryType = "arraybuffer";
|
|
379
|
+
} catch (e) {
|
|
380
|
+
i(new p(`Failed to create WebSocket: ${e}`, e instanceof Error ? e : void 0));
|
|
381
|
+
return;
|
|
382
|
+
}
|
|
383
|
+
let a = !1, o = !1, s = () => {
|
|
384
|
+
clearTimeout(u), this.ws && (this.ws.onopen = null, this.ws.onerror = null, this.ws.onclose = null, this.ws.onmessage = null);
|
|
385
|
+
}, c = () => {
|
|
386
|
+
o || (o = !0, clearTimeout(u), this.firstMessageResolver = null, z.debug(`WebSocket connected in ${Date.now() - n}ms`), r());
|
|
387
|
+
}, l = (e) => {
|
|
388
|
+
o || (o = !0, this.firstMessageResolver = null, s(), i(e));
|
|
389
|
+
};
|
|
390
|
+
this.firstMessageResolver = c;
|
|
391
|
+
let u = setTimeout(() => {
|
|
392
|
+
this.ws?.close(), z.warn(`WebSocket connection timeout after ${this.connectionTimeoutMs}ms`), l(new p("WebSocket connection timeout"));
|
|
393
|
+
}, this.connectionTimeoutMs);
|
|
394
|
+
this.ws.onopen = () => {
|
|
395
|
+
a = !0, this.ws.send(t), this.resetKeepaliveTimeout();
|
|
396
|
+
}, this.ws.onerror = () => {
|
|
397
|
+
z.debug(`WebSocket connection failed after ${Date.now() - n}ms`), l(new p("WebSocket connection failed"));
|
|
398
|
+
}, this.ws.onclose = (e) => {
|
|
399
|
+
if (this.clearKeepaliveTimeout(), !o) {
|
|
400
|
+
l(a ? new f("WebSocket closed before first server message") : new p("WebSocket connection closed before opening"));
|
|
401
|
+
return;
|
|
402
|
+
}
|
|
403
|
+
if (this.isClosed) return;
|
|
404
|
+
let t = e.wasClean && (e.code === 1e3 || e.code === 1001), n = e.reason || `code=${e.code}`;
|
|
405
|
+
this.callbacks.onClose(n, t);
|
|
406
|
+
}, this.ws.onmessage = (e) => {
|
|
407
|
+
this.handleProtocolMessage(new Uint8Array(e.data));
|
|
408
|
+
};
|
|
409
|
+
});
|
|
410
|
+
}
|
|
411
|
+
send(e) {
|
|
412
|
+
this.ws?.readyState === WebSocket.OPEN ? this.ws.send(e) : z.warn("Cannot send: WebSocket not connected");
|
|
413
|
+
}
|
|
414
|
+
close() {
|
|
415
|
+
this.isClosed = !0, this.clearKeepaliveTimeout(), this.ws &&= (this.ws.close(1e3, "Client closing"), null);
|
|
416
|
+
}
|
|
417
|
+
getKeepaliveResponse() {
|
|
418
|
+
return this.keepaliveResponseMessage ||= x(y(), this.sessionId), this.keepaliveResponseMessage;
|
|
419
|
+
}
|
|
420
|
+
handleProtocolMessage(t) {
|
|
421
|
+
try {
|
|
422
|
+
let n = e(t).opcode;
|
|
423
|
+
if (this.firstMessageResolver && this.firstMessageResolver(), n === c.KEEPALIVE_REQUEST) {
|
|
424
|
+
this.send(this.getKeepaliveResponse()), this.resetKeepaliveTimeout();
|
|
425
|
+
return;
|
|
426
|
+
}
|
|
427
|
+
this.resetKeepaliveTimeout(), this.callbacks.onProtocolMessage(t);
|
|
428
|
+
} catch (e) {
|
|
429
|
+
z.error("Failed to process protocol message:", e), this.callbacks.onError(e instanceof Error ? e : Error(String(e)));
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
checkKeepaliveNow() {
|
|
433
|
+
this.keepalive?.checkNow();
|
|
434
|
+
}
|
|
435
|
+
checkKeepaliveWithin(e) {
|
|
436
|
+
this.keepalive?.checkWithin(e);
|
|
437
|
+
}
|
|
438
|
+
resetKeepaliveTimeout() {
|
|
439
|
+
this.keepalive ??= new C(this.keepaliveTimeoutMs, "connection", (e) => {
|
|
440
|
+
this.callbacks.onError(e), this.close();
|
|
441
|
+
}), this.keepalive.reset();
|
|
442
|
+
}
|
|
443
|
+
clearKeepaliveTimeout() {
|
|
444
|
+
this.keepalive?.clear();
|
|
445
|
+
}
|
|
446
|
+
}, H = l("WebTransportTransport"), U = 1e4, W = class {
|
|
447
|
+
transport = null;
|
|
448
|
+
stream = null;
|
|
449
|
+
reader = null;
|
|
450
|
+
writer = null;
|
|
451
|
+
keepalive = null;
|
|
452
|
+
readLoopActive = !1;
|
|
453
|
+
isClosed = !1;
|
|
454
|
+
framer = new E();
|
|
455
|
+
keepaliveResponseMessage = null;
|
|
456
|
+
firstMessageResolver = null;
|
|
457
|
+
firstMessageRejecter = null;
|
|
458
|
+
keepaliveTimeoutMs;
|
|
459
|
+
connectionTimeoutMs;
|
|
460
|
+
callbacks;
|
|
461
|
+
sessionId;
|
|
462
|
+
constructor(e) {
|
|
463
|
+
this.callbacks = e.callbacks, this.sessionId = e.sessionId, this.keepaliveTimeoutMs = e.keepaliveTimeoutMs, this.connectionTimeoutMs = e.connectionTimeoutMs ?? U;
|
|
464
|
+
}
|
|
465
|
+
get isConnected() {
|
|
466
|
+
return this.transport !== null && this.writer !== null;
|
|
467
|
+
}
|
|
468
|
+
async connect(e, t) {
|
|
469
|
+
if (!g()) throw new p("WebTransport is not supported in this browser");
|
|
470
|
+
this.isClosed = !1;
|
|
471
|
+
let n = Date.now(), r = new Promise((e, t) => {
|
|
472
|
+
this.firstMessageResolver = e, this.firstMessageRejecter = t;
|
|
473
|
+
});
|
|
474
|
+
try {
|
|
475
|
+
this.transport = new WebTransport(e);
|
|
476
|
+
let i, a = new Promise((e, t) => {
|
|
477
|
+
i = setTimeout(() => t(new p("WebTransport connection timeout")), this.connectionTimeoutMs);
|
|
478
|
+
});
|
|
479
|
+
try {
|
|
480
|
+
await Promise.race([this.transport.ready, a]);
|
|
481
|
+
} catch (e) {
|
|
482
|
+
throw e instanceof p && H.warn(`WebTransport connection timeout after ${this.connectionTimeoutMs}ms`), e;
|
|
483
|
+
} finally {
|
|
484
|
+
clearTimeout(i);
|
|
485
|
+
}
|
|
486
|
+
this.transport.closed.then(() => {
|
|
487
|
+
this.handleClose("Connection closed", !0);
|
|
488
|
+
}).catch((e) => {
|
|
489
|
+
H.error("WebTransport connection closed with error:", e), this.handleClose(e instanceof Error ? e.message : "Connection error", !1);
|
|
490
|
+
}), this.stream = await this.transport.createBidirectionalStream(), this.reader = this.stream.readable.getReader(), this.writer = this.stream.writable.getWriter(), await this.writer.write(t), this.resetKeepaliveTimeout(), this.startReadLoop();
|
|
491
|
+
let o = Math.max(this.connectionTimeoutMs - (Date.now() - n), 1), s, c = new Promise((e, t) => {
|
|
492
|
+
s = setTimeout(() => t(new p("WebTransport closed before first server message")), o);
|
|
493
|
+
});
|
|
494
|
+
try {
|
|
495
|
+
await Promise.race([r, c]);
|
|
496
|
+
} finally {
|
|
497
|
+
clearTimeout(s);
|
|
498
|
+
}
|
|
499
|
+
this.firstMessageResolver = null, this.firstMessageRejecter = null, H.info(`WebTransport connected in ${Date.now() - n}ms`);
|
|
500
|
+
} catch (e) {
|
|
501
|
+
this.firstMessageResolver = null, this.firstMessageRejecter = null, this.isClosed = !0, this.cleanup();
|
|
502
|
+
let t = Date.now() - n;
|
|
503
|
+
throw H.warn(`WebTransport connection failed after ${t}ms: ${e}`), new p(`Failed to connect WebTransport: ${e}`, e instanceof Error ? e : void 0);
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
send(e) {
|
|
507
|
+
if (!this.writer) {
|
|
508
|
+
H.warn("Cannot send: WebTransport not connected");
|
|
509
|
+
return;
|
|
510
|
+
}
|
|
511
|
+
this.writer.write(e).catch((e) => {
|
|
512
|
+
H.error("Failed to send protocol message:", e), this.callbacks.onError(e instanceof Error ? e : Error(String(e)));
|
|
513
|
+
});
|
|
514
|
+
}
|
|
515
|
+
close() {
|
|
516
|
+
this.isClosed = !0, this.clearKeepaliveTimeout(), this.readLoopActive = !1, this.cleanup();
|
|
517
|
+
}
|
|
518
|
+
async startReadLoop() {
|
|
519
|
+
if (this.reader) {
|
|
520
|
+
this.readLoopActive = !0;
|
|
521
|
+
try {
|
|
522
|
+
for (; this.readLoopActive && this.reader;) {
|
|
523
|
+
let { value: e, done: t } = await this.reader.read();
|
|
524
|
+
if (t) break;
|
|
525
|
+
e && this.framer.push(e, (e) => this.handleProtocolMessage(e));
|
|
526
|
+
}
|
|
527
|
+
this.readLoopActive && this.handleClose("Stream ended", !1);
|
|
528
|
+
} catch (e) {
|
|
529
|
+
this.readLoopActive && (H.warn(`Read loop failed, closing transport: ${e instanceof Error ? e.message : String(e)}`), this.callbacks.onError(e instanceof Error ? e : Error(String(e))), this.handleClose("Read loop failed", !1));
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
getKeepaliveResponse() {
|
|
534
|
+
return this.keepaliveResponseMessage ||= x(y(), this.sessionId), this.keepaliveResponseMessage;
|
|
535
|
+
}
|
|
536
|
+
handleProtocolMessage(t) {
|
|
537
|
+
try {
|
|
538
|
+
let n = e(t).opcode;
|
|
539
|
+
if (this.firstMessageResolver && this.firstMessageResolver(), n === c.KEEPALIVE_REQUEST) {
|
|
540
|
+
this.send(this.getKeepaliveResponse()), this.resetKeepaliveTimeout();
|
|
541
|
+
return;
|
|
542
|
+
}
|
|
543
|
+
this.resetKeepaliveTimeout(), this.callbacks.onProtocolMessage(t);
|
|
544
|
+
} catch (e) {
|
|
545
|
+
H.error("Failed to process protocol message:", e), this.callbacks.onError(e instanceof Error ? e : Error(String(e)));
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
handleClose(e, t) {
|
|
549
|
+
if (!this.isClosed) {
|
|
550
|
+
if (this.isClosed = !0, this.clearKeepaliveTimeout(), this.readLoopActive = !1, this.cleanup(), this.firstMessageRejecter) {
|
|
551
|
+
this.firstMessageRejecter(new f(`WebTransport closed before first server message: ${e}`)), this.firstMessageResolver = null, this.firstMessageRejecter = null;
|
|
552
|
+
return;
|
|
553
|
+
}
|
|
554
|
+
this.callbacks.onClose(e, t);
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
checkKeepaliveNow() {
|
|
558
|
+
this.keepalive?.checkNow();
|
|
559
|
+
}
|
|
560
|
+
checkKeepaliveWithin(e) {
|
|
561
|
+
this.keepalive?.checkWithin(e);
|
|
562
|
+
}
|
|
563
|
+
resetKeepaliveTimeout() {
|
|
564
|
+
this.keepalive ??= new C(this.keepaliveTimeoutMs, "connection", (e) => {
|
|
565
|
+
this.callbacks.onError(e), this.close();
|
|
566
|
+
}), this.keepalive.reset();
|
|
567
|
+
}
|
|
568
|
+
clearKeepaliveTimeout() {
|
|
569
|
+
this.keepalive?.clear();
|
|
570
|
+
}
|
|
571
|
+
cleanup() {
|
|
572
|
+
if (this.reader &&= (this.reader.releaseLock(), null), this.writer &&= (this.writer.releaseLock(), null), this.stream = null, this.framer.reset(), this.transport) {
|
|
573
|
+
try {
|
|
574
|
+
this.transport.close();
|
|
575
|
+
} catch {}
|
|
576
|
+
this.transport = null;
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
}, G = l("Channel"), K = class {
|
|
580
|
+
state = "disconnected";
|
|
581
|
+
transport = null;
|
|
582
|
+
config;
|
|
583
|
+
entrypoint;
|
|
584
|
+
constructor(e) {
|
|
585
|
+
this.config = e, this.entrypoint = e.entrypoint;
|
|
586
|
+
}
|
|
587
|
+
get channelState() {
|
|
588
|
+
return this.state;
|
|
589
|
+
}
|
|
590
|
+
get opcodeGroups() {
|
|
591
|
+
return this.entrypoint.OpcodeGroupsFromServer;
|
|
592
|
+
}
|
|
593
|
+
get type() {
|
|
594
|
+
return this.entrypoint.Type;
|
|
595
|
+
}
|
|
596
|
+
get isConnected() {
|
|
597
|
+
return this.transport?.isConnected ?? !1;
|
|
598
|
+
}
|
|
599
|
+
get httpStreamMode() {
|
|
600
|
+
return this.transport instanceof N ? this.transport.activeMode : null;
|
|
601
|
+
}
|
|
602
|
+
async connect() {
|
|
603
|
+
if (this.state !== "disconnected") throw Error(`Cannot connect: already in state ${this.state}`);
|
|
604
|
+
this.setState("connecting");
|
|
605
|
+
try {
|
|
606
|
+
await this.connectTransport(), this.setState("connected");
|
|
607
|
+
} catch (e) {
|
|
608
|
+
throw this.setState("disconnected"), e;
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
disconnect() {
|
|
612
|
+
this.transport &&= (this.transport.close(), null), this.setState("disconnected");
|
|
613
|
+
}
|
|
614
|
+
triggerReconnect(e) {
|
|
615
|
+
this.transport &&= (this.transport.close(), null), this.config.onClose?.(e, !1);
|
|
616
|
+
}
|
|
617
|
+
checkHealth(e) {
|
|
618
|
+
this.state === "connected" && (e === void 0 ? this.transport?.checkKeepaliveNow?.() : this.transport?.checkKeepaliveWithin?.(e), this.state === "connected" && this.transport && !this.transport.isConnected && this.handleClose("Transport dead after tab resume", !1));
|
|
619
|
+
}
|
|
620
|
+
send(e) {
|
|
621
|
+
if (!this.transport?.isConnected) {
|
|
622
|
+
G.warn("Cannot send: channel not connected");
|
|
623
|
+
return;
|
|
624
|
+
}
|
|
625
|
+
this.transport.send(e);
|
|
626
|
+
}
|
|
627
|
+
async connectTransport() {
|
|
628
|
+
let e = this.entrypoint, t = {
|
|
629
|
+
sessionId: this.config.sessionId,
|
|
630
|
+
keepaliveTimeoutMs: this.config.keepaliveTimeoutMs,
|
|
631
|
+
connectionTimeoutMs: this.config.connectionTimeoutMs,
|
|
632
|
+
httpStreamMode: this.config.httpStreamMode,
|
|
633
|
+
httpStreamPreferredMode: this.config.httpStreamPreferredMode,
|
|
634
|
+
callbacks: {
|
|
635
|
+
onProtocolMessage: (e) => this.handleProtocolMessage(e),
|
|
636
|
+
onClose: (e, t) => this.handleClose(e, t),
|
|
637
|
+
onError: (e) => this.handleError(e)
|
|
638
|
+
}
|
|
639
|
+
};
|
|
640
|
+
switch (e.Type) {
|
|
641
|
+
case r.WebSocket:
|
|
642
|
+
case r.WebSocketProxy:
|
|
643
|
+
this.transport = new V(t);
|
|
644
|
+
break;
|
|
645
|
+
case r.WebTransport:
|
|
646
|
+
case r.WebTransportProxy:
|
|
647
|
+
if (!g()) throw Error("WebTransport is not supported in this browser");
|
|
648
|
+
this.transport = new W(t);
|
|
649
|
+
break;
|
|
650
|
+
case r.HttpStream:
|
|
651
|
+
case r.HttpStreamProxy:
|
|
652
|
+
this.transport = new N(t);
|
|
653
|
+
break;
|
|
654
|
+
default: throw Error(`Unsupported entrypoint type: ${r[e.Type]}`);
|
|
655
|
+
}
|
|
656
|
+
await this.transport.connect(e.Uri, e.AuthTicket);
|
|
657
|
+
}
|
|
658
|
+
handleProtocolMessage(e) {
|
|
659
|
+
try {
|
|
660
|
+
let t = s(e);
|
|
661
|
+
if (o(t) === c.CORE_ON_SERVER_STOPPING) {
|
|
662
|
+
G.debug("Received server stopping message"), this.setState("stopped"), this.transport?.close();
|
|
663
|
+
return;
|
|
664
|
+
}
|
|
665
|
+
this.config.onProtocolMessage?.(t);
|
|
666
|
+
} catch (e) {
|
|
667
|
+
G.error("Failed to handle protocol message:", e), this.config.onError?.(e instanceof Error ? e : Error(String(e)));
|
|
668
|
+
}
|
|
669
|
+
}
|
|
670
|
+
handleClose(e, t) {
|
|
671
|
+
this.transport = null, this.config.onClose?.(e, t);
|
|
672
|
+
}
|
|
673
|
+
handleError(e) {
|
|
674
|
+
this.config.onError?.(e), e instanceof u && this.handleClose("Keepalive timeout", !1);
|
|
675
|
+
}
|
|
676
|
+
setState(e) {
|
|
677
|
+
this.state !== e && (this.state = e, this.config.onStateChange?.(e));
|
|
678
|
+
}
|
|
679
|
+
}, q = l("ChannelManager"), J = 5e3, Y = class e {
|
|
680
|
+
channels = /* @__PURE__ */ new Map();
|
|
681
|
+
activeType = null;
|
|
682
|
+
state = "idle";
|
|
683
|
+
reconnectAttempts = 0;
|
|
684
|
+
shouldReconnect = !0;
|
|
685
|
+
entrypoints = [];
|
|
686
|
+
abortController = null;
|
|
687
|
+
connectionTimeoutMs;
|
|
688
|
+
stabilityTimer = null;
|
|
689
|
+
config;
|
|
690
|
+
constructor(e) {
|
|
691
|
+
this.config = e;
|
|
692
|
+
}
|
|
693
|
+
get managerState() {
|
|
694
|
+
return this.state;
|
|
695
|
+
}
|
|
696
|
+
get activeHttpStreamMode() {
|
|
697
|
+
for (let e of this.channels.values()) {
|
|
698
|
+
let t = e.httpStreamMode;
|
|
699
|
+
if (t) return t;
|
|
700
|
+
}
|
|
701
|
+
return null;
|
|
702
|
+
}
|
|
703
|
+
get activeEndpointType() {
|
|
704
|
+
return this.activeType;
|
|
705
|
+
}
|
|
706
|
+
async connect(e, t) {
|
|
707
|
+
if (this.state !== "idle" && this.state !== "offline") throw Error(`Cannot connect: already in state ${this.state}`);
|
|
708
|
+
if (this.entrypoints = e, this.shouldReconnect = !0, this.reconnectAttempts = 0, this.abortController = new AbortController(), t?.retry) {
|
|
709
|
+
this.attemptReconnect();
|
|
710
|
+
return;
|
|
711
|
+
}
|
|
712
|
+
await this.connectInternal();
|
|
713
|
+
}
|
|
714
|
+
disconnect() {
|
|
715
|
+
this.shouldReconnect = !1, this.abortController?.abort(), this.abortController = null, this.disconnectAll(), this.setState("idle");
|
|
716
|
+
}
|
|
717
|
+
sendProtocolMessage(e) {
|
|
718
|
+
let n = t(e);
|
|
719
|
+
for (let [t, r] of this.channels) if (t & n) {
|
|
720
|
+
r.send(e);
|
|
721
|
+
return;
|
|
722
|
+
}
|
|
723
|
+
this.state === "connected" && q.warn("No channel found for message");
|
|
724
|
+
}
|
|
725
|
+
sendToAll(e) {
|
|
726
|
+
for (let t of this.channels.values()) t.send(e);
|
|
727
|
+
}
|
|
728
|
+
checkHealth(e) {
|
|
729
|
+
if (this.state === "connected") for (let t of this.channels.values()) t.checkHealth(e);
|
|
730
|
+
}
|
|
731
|
+
triggerReconnect(e) {
|
|
732
|
+
if (this.state === "connected") {
|
|
733
|
+
q.debug(`Forcing reconnect: ${e}`);
|
|
734
|
+
for (let t of this.channels.values()) t.triggerReconnect(e);
|
|
735
|
+
}
|
|
736
|
+
}
|
|
737
|
+
async connectInternal(t = !1) {
|
|
738
|
+
this.setState("connecting");
|
|
739
|
+
let n = this.entrypoints.filter((t) => t.Type !== r.WebRTC && !e.isNativeOnlyType(t.Type)), i = this.groupByType(n);
|
|
740
|
+
if (i.size === 0) throw this.setState("offline"), Error("No entrypoints available");
|
|
741
|
+
let a;
|
|
742
|
+
if (this.config.orderedEndpointTypes !== void 0) {
|
|
743
|
+
let e = new Set(i.keys());
|
|
744
|
+
a = this.config.orderedEndpointTypes.filter((t) => e.has(t));
|
|
745
|
+
} else a = this.config.endpointSelector ? this.config.endpointSelector.getOrderedTypes(i) : Array.from(i.keys());
|
|
746
|
+
if (a.length === 0) throw this.setState("offline"), new _(null, null, [...i.keys()].map((e) => r[e]));
|
|
747
|
+
this.connectionTimeoutMs = a.length > 1 ? 5e3 : 1e4, q.debug(`Endpoint types to try: [${a.map((e) => r[e]).join(", ")}], transport timeout: ${this.connectionTimeoutMs}ms`);
|
|
748
|
+
for (let e of a) {
|
|
749
|
+
if (!this.shouldReconnect) return;
|
|
750
|
+
let t = i.get(e), n = Date.now();
|
|
751
|
+
q.debug(`Trying ${r[e]} (${t.length} channel(s))`);
|
|
752
|
+
try {
|
|
753
|
+
await this.connectAllChannels(t), this.config.endpointSelector?.rememberWorkingType(e), this.config.onRememberWorkingType?.(e), this.activeType = e, q.debug(`Connected via ${r[e]} in ${Date.now() - n}ms`), this.setState("connected"), this.startStabilityTimer();
|
|
754
|
+
return;
|
|
755
|
+
} catch (t) {
|
|
756
|
+
q.warn(`Failed to connect using ${r[e]} after ${Date.now() - n}ms: ${t instanceof Error ? t.message : t}`);
|
|
757
|
+
let a = this.channels.size > 0;
|
|
758
|
+
if (this.disconnectAll(), t instanceof f) throw t;
|
|
759
|
+
if (a && this.config.onRefreshEntrypoints) try {
|
|
760
|
+
q.info("Partial connection detected, refreshing entrypoints");
|
|
761
|
+
let e = await this.config.onRefreshEntrypoints();
|
|
762
|
+
this.entrypoints = e.entrypoints, this.config.sessionId = e.sessionId;
|
|
763
|
+
let t = e.entrypoints.filter((e) => e.Type !== r.WebRTC), n = this.groupByType(t);
|
|
764
|
+
for (let [e, t] of n) i.set(e, t);
|
|
765
|
+
} catch (e) {
|
|
766
|
+
q.warn(`Failed to refresh entrypoints: ${e}`);
|
|
767
|
+
}
|
|
768
|
+
}
|
|
769
|
+
}
|
|
770
|
+
throw t || this.setState("offline"), Error("Failed to connect using any endpoint type");
|
|
771
|
+
}
|
|
772
|
+
async connectAllChannels(e) {
|
|
773
|
+
if (e.length === 0) throw Error("No entrypoints to connect");
|
|
774
|
+
let t = (await Promise.allSettled(e.map((e) => this.connectChannel(e)))).filter((e) => e.status === "rejected");
|
|
775
|
+
if (t.length > 0) {
|
|
776
|
+
let e = t.map((e) => e.reason instanceof Error ? e.reason.message : String(e.reason));
|
|
777
|
+
throw t.some((e) => e.reason instanceof f) ? new f(e.join(", ")) : Error(e.join(", "));
|
|
778
|
+
}
|
|
779
|
+
}
|
|
780
|
+
async connectChannel(e) {
|
|
781
|
+
let t = new K({
|
|
782
|
+
entrypoint: e,
|
|
783
|
+
sessionId: this.config.sessionId,
|
|
784
|
+
keepaliveTimeoutMs: this.config.keepaliveTimeoutMs,
|
|
785
|
+
connectionTimeoutMs: this.connectionTimeoutMs,
|
|
786
|
+
httpStreamMode: this.config.httpStreamMode,
|
|
787
|
+
httpStreamPreferredMode: this.config.httpStreamPreferredMode,
|
|
788
|
+
onProtocolMessage: (e) => {
|
|
789
|
+
this.config.onProtocolMessage?.(e);
|
|
790
|
+
},
|
|
791
|
+
onStateChange: (t) => {
|
|
792
|
+
this.handleChannelStateChange(e.OpcodeGroupsFromServer, t);
|
|
793
|
+
},
|
|
794
|
+
onClose: (t, n) => {
|
|
795
|
+
this.handleChannelClose(e.OpcodeGroupsFromServer, t, n);
|
|
796
|
+
},
|
|
797
|
+
onError: (e) => {
|
|
798
|
+
this.config.onError?.(e);
|
|
799
|
+
}
|
|
800
|
+
});
|
|
801
|
+
await t.connect(), this.channels.set(e.OpcodeGroupsFromServer, t);
|
|
802
|
+
}
|
|
803
|
+
static isNativeOnlyType(e) {
|
|
804
|
+
switch (e) {
|
|
805
|
+
case r.Tcp:
|
|
806
|
+
case r.TcpTls:
|
|
807
|
+
case r.TcpProxy:
|
|
808
|
+
case r.Udp:
|
|
809
|
+
case r.UdpDtls:
|
|
810
|
+
case r.Https: return !0;
|
|
811
|
+
default: return !1;
|
|
812
|
+
}
|
|
813
|
+
}
|
|
814
|
+
groupByType(e) {
|
|
815
|
+
let t = /* @__PURE__ */ new Map();
|
|
816
|
+
for (let n of e) {
|
|
817
|
+
let e = t.get(n.Type) ?? [];
|
|
818
|
+
e.push(n), t.set(n.Type, e);
|
|
819
|
+
}
|
|
820
|
+
return t;
|
|
821
|
+
}
|
|
822
|
+
handleChannelStateChange(e, t) {
|
|
823
|
+
t === "stopped" && (this.shouldReconnect = !1, this.disconnectAll(), this.setState("stopped"));
|
|
824
|
+
}
|
|
825
|
+
handleChannelClose(e, t, n) {
|
|
826
|
+
this.state !== "idle" && this.state !== "stopped" && this.state !== "offline" && this.channels.has(e) && this.state === "connected" && (q.warn("Channel closed unexpectedly, attempting reconnect"), this.disconnectAll(), this.attemptReconnect());
|
|
827
|
+
}
|
|
828
|
+
async attemptReconnect() {
|
|
829
|
+
if (this.shouldReconnect) {
|
|
830
|
+
if (!d()) {
|
|
831
|
+
q.warn("Retry disabled via ikon-retry=false, skipping reconnect"), this.setState("offline"), this.config.onError?.(new m(0));
|
|
832
|
+
return;
|
|
833
|
+
}
|
|
834
|
+
if (this.reconnectAttempts >= this.config.maxReconnectAttempts) {
|
|
835
|
+
q.warn(`Max reconnect attempts (${this.config.maxReconnectAttempts}) reached`), this.setState("offline"), this.config.onError?.(new m(this.config.maxReconnectAttempts));
|
|
836
|
+
return;
|
|
837
|
+
}
|
|
838
|
+
if (this.setState("reconnecting"), this.reconnectAttempts++, this.reconnectAttempts > 1) {
|
|
839
|
+
q.info(`Reconnecting in ${this.config.reconnectBackoffMs}ms (attempt ${this.reconnectAttempts}/${this.config.maxReconnectAttempts})`);
|
|
840
|
+
try {
|
|
841
|
+
await h(this.config.reconnectBackoffMs, this.abortController?.signal);
|
|
842
|
+
} catch {
|
|
843
|
+
return;
|
|
844
|
+
}
|
|
845
|
+
} else q.info(`Reconnecting immediately (attempt ${this.reconnectAttempts}/${this.config.maxReconnectAttempts})`);
|
|
846
|
+
if (this.shouldReconnect) try {
|
|
847
|
+
await this.connectInternal(!0);
|
|
848
|
+
} catch (e) {
|
|
849
|
+
if (e instanceof f) {
|
|
850
|
+
q.warn("Auth rejected during reconnect, stopping retries and signaling full re-auth"), this.config.onError?.(e), this.setState("offline");
|
|
851
|
+
return;
|
|
852
|
+
}
|
|
853
|
+
if (e instanceof _) {
|
|
854
|
+
q.warn(`${e.message} Not retrying`), this.config.onError?.(e), this.setState("offline");
|
|
855
|
+
return;
|
|
856
|
+
}
|
|
857
|
+
await this.attemptReconnect();
|
|
858
|
+
}
|
|
859
|
+
}
|
|
860
|
+
}
|
|
861
|
+
disconnectAll() {
|
|
862
|
+
for (let e of this.channels.values()) e.disconnect();
|
|
863
|
+
this.channels.clear(), this.activeType = null;
|
|
864
|
+
}
|
|
865
|
+
setState(e) {
|
|
866
|
+
this.state !== e && (this.state === "connected" && e !== "connected" && this.clearStabilityTimer(), this.state = e, this.config.onStateChange?.(e));
|
|
867
|
+
}
|
|
868
|
+
startStabilityTimer() {
|
|
869
|
+
this.clearStabilityTimer(), this.stabilityTimer = setTimeout(() => {
|
|
870
|
+
this.stabilityTimer = null, this.state === "connected" && (this.reconnectAttempts = 0);
|
|
871
|
+
}, J);
|
|
872
|
+
}
|
|
873
|
+
clearStabilityTimer() {
|
|
874
|
+
this.stabilityTimer &&= (clearTimeout(this.stabilityTimer), null);
|
|
875
|
+
}
|
|
876
|
+
};
|
|
877
|
+
//#endregion
|
|
878
|
+
export { Y as ChannelManager };
|