gloomberb 0.20.10 → 0.20.11
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/package.json +1 -1
- package/patches/@opentui%2Fcore@0.3.2.patch +16 -0
- package/src/api-client/socket-connection.ts +355 -0
- package/src/api-client/socket-features.ts +75 -0
- package/src/api-client/socket-frames.ts +161 -0
- package/src/api-client/socket-listeners.ts +207 -0
- package/src/api-client/socket-quotes.ts +332 -0
- package/src/api-client/socket.ts +150 -983
- package/src/brokers/signed-in/adapter.ts +19 -5
- package/src/brokers/signed-in/client.ts +13 -1
- package/src/brokers/signed-in/sign-in-dialog.tsx +33 -9
- package/src/components/chart/native/kitty/manager.ts +2 -0
- package/src/components/chart/native/surface/manager.ts +211 -11
- package/src/components/command-bar/panel/index.tsx +2 -13
- package/src/components/command-bar/panel/layout.ts +1 -7
- package/src/components/command-bar/panel/runtime.ts +0 -1
- package/src/components/command-bar/panel/types.ts +0 -2
- package/src/components/data-table/column-menu.tsx +89 -0
- package/src/components/data-table/sort-menu.tsx +9 -2
- package/src/components/data-table/view.tsx +33 -6
- package/src/components/form-modal/content.tsx +10 -0
- package/src/components/index.ts +1 -1
- package/src/components/layout/pane/footer/index.tsx +25 -11
- package/src/components/layout/pane/footer/model.ts +62 -20
- package/src/components/layout/pane/sidebar.tsx +125 -15
- package/src/components/layout/shell/active-drag.ts +18 -13
- package/src/components/layout/shell/drag/magnets.ts +34 -18
- package/src/components/ticker/list-table-view.tsx +5 -0
- package/src/components/ui/data-table/column-order.ts +180 -0
- package/src/components/ui/data-table/index.tsx +12 -1
- package/src/components/ui/data-table/opentui/index.tsx +267 -23
- package/src/components/ui/data-table/opentui/model.ts +43 -4
- package/src/components/ui/data-table/types.ts +49 -0
- package/src/components/ui/data-table/use-column-order.ts +135 -0
- package/src/components/ui/index.ts +1 -1
- package/src/components/ui/table-layout.ts +120 -0
- package/src/docs/snapshot.json +3 -2
- package/src/i18n/es.ts +2 -0
- package/src/i18n/ja.ts +2 -0
- package/src/i18n/ko.ts +2 -0
- package/src/i18n/zh-cn.ts +2 -0
- package/src/i18n/zh-tw.ts +2 -0
- package/src/market-data/coordinator/index.ts +4 -3
- package/src/market-data/coordinator/quotes.ts +12 -4
- package/src/market-data/quotes/freshness.ts +12 -0
- package/src/market-data/quotes/live-valuation.ts +1 -1
- package/src/market-data/quotes/regular-session-valuation.ts +1 -1
- package/src/plugins/builtin/broker-manager/detail.tsx +5 -3
- package/src/plugins/builtin/broker-manager/index.tsx +6 -6
- package/src/plugins/builtin/broker-manager/model.ts +15 -6
- package/src/plugins/builtin/broker-manager/pane-actions.tsx +11 -4
- package/src/plugins/builtin/broker-manager/sign-in-end.ts +50 -19
- package/src/plugins/builtin/broker-manager/table.ts +10 -4
- package/src/plugins/builtin/chat/sidebar.tsx +33 -16
- package/src/plugins/builtin/cloud/askg/tool-timeline.tsx +2 -0
- package/src/plugins/builtin/equity-screener/live.ts +74 -18
- package/src/plugins/builtin/equity-screener/model.ts +19 -4
- package/src/plugins/builtin/equity-screener/pane.tsx +19 -14
- package/src/plugins/builtin/equity-screener/refresh-anchor.ts +45 -0
- package/src/plugins/builtin/equity-screener/results.ts +22 -2
- package/src/plugins/builtin/equity-screener/screen-check.ts +9 -3
- package/src/plugins/builtin/futures/index.tsx +15 -1
- package/src/plugins/builtin/futures/table.tsx +1 -1
- package/src/plugins/builtin/fx-matrix/index.tsx +2 -0
- package/src/plugins/builtin/holders/pane.tsx +30 -20
- package/src/plugins/builtin/iv-history/events.ts +5 -0
- package/src/plugins/builtin/iv-history/headless.ts +7 -2
- package/src/plugins/builtin/kelly-sizer/sections.tsx +2 -0
- package/src/plugins/builtin/macro-day/headless.ts +12 -3
- package/src/plugins/builtin/options/view.tsx +2 -0
- package/src/plugins/builtin/options-scenario/pane.tsx +2 -1
- package/src/plugins/builtin/perps/cascade-tabs.tsx +2 -1
- package/src/plugins/builtin/portfolio-list/pane/index.tsx +13 -0
- package/src/plugins/builtin/portfolio-list/table.tsx +5 -0
- package/src/plugins/builtin/rate-path/pane.tsx +2 -0
- package/src/plugins/builtin/revenue-breakdown/pane.tsx +2 -0
- package/src/plugins/builtin/reverse-dcf/pane.tsx +2 -1
- package/src/plugins/builtin/seasonality/pane.tsx +2 -1
- package/src/plugins/builtin/thirteenf/pane.tsx +2 -0
- package/src/plugins/builtin/ticker-detail/financials/model.ts +28 -6
- package/src/plugins/builtin/ticker-detail/financials/ratio-table.tsx +26 -9
- package/src/plugins/builtin/ticker-detail/financials/tab.tsx +58 -35
- package/src/plugins/builtin/vol-surface/heat-grid.tsx +2 -1
- package/src/plugins/builtin/vol-surface/pane.tsx +2 -1
- package/src/plugins/builtin/world-indices/index.tsx +5 -1
- package/src/plugins/builtin/yield-curve/index.tsx +2 -1
- package/src/renderers/dom/data-table/column-drag.ts +147 -0
- package/src/renderers/dom/data-table/index.tsx +22 -0
- package/src/renderers/dom/data-table/row.tsx +27 -5
- package/src/renderers/dom/styles.css +22 -0
- package/src/renderers/opentui/chart-surface.tsx +1 -0
- package/src/renderers/opentui/frame-hits.ts +33 -0
- package/src/renderers/opentui/host.tsx +2 -0
- package/src/renderers/opentui/native-surface.ts +14 -21
- package/src/renderers/opentui/test-utils.tsx +3 -0
- package/src/state/app/context/index.tsx +22 -0
- package/src/test-support/app-store.ts +24 -1
- package/src/time-series/live-quotes.ts +3 -9
- package/src/ui/host.tsx +18 -0
- package/src/ui/pointer-drag.ts +11 -2
- package/src/utils/fundamentals.ts +14 -3
- package/src/utils/share-multiples.cases.json +780 -2
- package/src/utils/share-multiples.ts +154 -24
package/package.json
CHANGED
|
@@ -576,6 +576,22 @@ index b9445d2cca9477dfe06026da451b6d19268fcbdd..1f5795ab76800a64fdb0acf4a1ed0186
|
|
|
576
576
|
this.waitingForPixelResolution = false;
|
|
577
577
|
this.updateStdinParserProtocolContext({
|
|
578
578
|
privateCapabilityRepliesActive: false,
|
|
579
|
+
diff --git a/index-3mx8q5t1.js b/index-3mx8q5t1.js
|
|
580
|
+
index fb66f651e3c5e5bd21b673290542e9f6b1f73818..bee91f3071845efd730b161a6fe92952bc7f93cf 100644
|
|
581
|
+
--- a/index-3mx8q5t1.js
|
|
582
|
+
+++ b/index-3mx8q5t1.js
|
|
583
|
+
@@ -9692,7 +9692,10 @@ class SliderRenderable extends Renderable {
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
set viewPortSize(size) {
|
|
587
|
+
- const clampedSize = Math.max(0.01, Math.min(size, this._max - this._min));
|
|
588
|
+
+ // The thumb is this share of the range plus this. Clamped to the range, it
|
|
589
|
+
+ // never passed half the track, and a slider sized before its content had
|
|
590
|
+
+ // a range kept a sliver of a thumb.
|
|
591
|
+
+ const clampedSize = Math.max(0.01, size);
|
|
592
|
+
if (clampedSize !== this._viewPortSize) {
|
|
593
|
+
this._viewPortSize = clampedSize;
|
|
594
|
+
this.requestRender();
|
|
579
595
|
diff --git a/lib/parse.mouse.d.ts b/lib/parse.mouse.d.ts
|
|
580
596
|
index c29f953fde0b78f09d2ec7a830e5a72452c1fe7e..04613b921bd9b4a9a7a1a4fbd0234d4012df67a1 100644
|
|
581
597
|
--- a/lib/parse.mouse.d.ts
|
|
@@ -0,0 +1,355 @@
|
|
|
1
|
+
import { logFailure } from "../utils/log-failure";
|
|
2
|
+
import { debugLog, previewLongLists } from "../utils/debug-log";
|
|
3
|
+
import {
|
|
4
|
+
GLOOM_CLOUD_SOCKET_CONNECTION_ID,
|
|
5
|
+
type ConnectionHealthRegistry,
|
|
6
|
+
} from "../core/connection-health";
|
|
7
|
+
|
|
8
|
+
/** How long a connection may take to open before the attempt is dropped and tried again. */
|
|
9
|
+
export const SOCKET_CONNECT_TIMEOUT_MS = 10_000;
|
|
10
|
+
/**
|
|
11
|
+
* An open connection that has sent nothing for this long is asked whether it
|
|
12
|
+
* is still there: a `ping`, which the server answers with a `pong`.
|
|
13
|
+
*/
|
|
14
|
+
export const SOCKET_QUIET_PING_MS = 10_000;
|
|
15
|
+
/**
|
|
16
|
+
* A connection that has not answered a ping in this long, nothing else come
|
|
17
|
+
* back meanwhile, is taken for dead (a half-open link: a laptop that slept, a
|
|
18
|
+
* network that changed under it). It is dropped and opened again; until then
|
|
19
|
+
* prices it streamed read as stale. Only an unanswered ping drops it: silence
|
|
20
|
+
* alone does not, since a check that runs late (a hidden tab's throttled
|
|
21
|
+
* timers, a wake from sleep) finds any connection silent.
|
|
22
|
+
*/
|
|
23
|
+
export const SOCKET_PING_ANSWER_MS = 10_000;
|
|
24
|
+
/** How often an open connection's silence is checked; it never needs to be exact. */
|
|
25
|
+
const SOCKET_SILENCE_CHECK_MS = 2_500;
|
|
26
|
+
/** The first reconnect waits about this long; each failure doubles it, up to RECONNECT_MAX_DELAY_MS. */
|
|
27
|
+
const RECONNECT_FIRST_DELAY_MS = 1000;
|
|
28
|
+
const RECONNECT_MAX_DELAY_MS = 10_000;
|
|
29
|
+
/** Outgoing frames worth a debug log line: the subscription changes. */
|
|
30
|
+
const LOGGED_FRAME_TYPES = new Set<unknown>([
|
|
31
|
+
"market.subscribe",
|
|
32
|
+
"market.unsubscribe",
|
|
33
|
+
"chat.subscribe",
|
|
34
|
+
"chat.unsubscribe",
|
|
35
|
+
"scanner.subscribe",
|
|
36
|
+
"scanner.unsubscribe",
|
|
37
|
+
]);
|
|
38
|
+
const cloudApiLog = debugLog.createLogger("cloud-api");
|
|
39
|
+
|
|
40
|
+
/** What opening a connection needs from the signed-in session. */
|
|
41
|
+
export type SocketConnectionSession = {
|
|
42
|
+
getBaseUrl: () => string;
|
|
43
|
+
getSocketAuthToken: () => string | null;
|
|
44
|
+
isUsingWebSocketToken: () => boolean;
|
|
45
|
+
clearWebSocketTokenForFallback: () => boolean;
|
|
46
|
+
/** The service answered on this connection (it opened, or sent a frame). */
|
|
47
|
+
onServiceAnswered?: () => void;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
/** What the connection asks of its owner, and tells it. */
|
|
51
|
+
export type SocketConnectionOwner = {
|
|
52
|
+
/** Whether anything still needs a connection: asked before one opens and before each reconnect. */
|
|
53
|
+
wanted: () => boolean;
|
|
54
|
+
/** The subscription counts the debug log lines carry. */
|
|
55
|
+
logCounts: () => { quoteTargets: number; channelTargets: number };
|
|
56
|
+
/** The connection opened. */
|
|
57
|
+
opened: () => void;
|
|
58
|
+
/** One frame from the current connection. */
|
|
59
|
+
received: (raw: string) => void;
|
|
60
|
+
/** The current connection broke off by itself (closed, or dropped unanswered), so streams may have gaps. */
|
|
61
|
+
interrupted: () => void;
|
|
62
|
+
/** The current connection ended, however it did. */
|
|
63
|
+
closed: () => void;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* The one WebSocket to Gloom Cloud: opening it, watching that it still answers, and reconnecting with backoff while
|
|
68
|
+
* its owner wants it. It reports every state to the connection health registry.
|
|
69
|
+
*/
|
|
70
|
+
export class SocketConnection {
|
|
71
|
+
private ws: WebSocket | null = null;
|
|
72
|
+
private reconnectTimer: ReturnType<typeof setTimeout> | null = null;
|
|
73
|
+
private reconnectDelayMs = RECONNECT_FIRST_DELAY_MS;
|
|
74
|
+
/** Gives up on a connection that has not opened in SOCKET_CONNECT_TIMEOUT_MS. */
|
|
75
|
+
private connectTimer: ReturnType<typeof setTimeout> | null = null;
|
|
76
|
+
/** Checks the open connection's silence (SOCKET_QUIET_PING_MS, SOCKET_PING_ANSWER_MS). */
|
|
77
|
+
private silenceTimer: ReturnType<typeof setInterval> | null = null;
|
|
78
|
+
/** When the open connection last sent anything, and when the ping still unanswered went out. */
|
|
79
|
+
private lastFrameAt = 0;
|
|
80
|
+
private pingedAt: number | null = null;
|
|
81
|
+
|
|
82
|
+
constructor(
|
|
83
|
+
private readonly session: SocketConnectionSession,
|
|
84
|
+
private readonly health: ConnectionHealthRegistry,
|
|
85
|
+
private readonly owner: SocketConnectionOwner,
|
|
86
|
+
) {}
|
|
87
|
+
|
|
88
|
+
/** The current connection, open or still opening. */
|
|
89
|
+
get socket(): WebSocket | null {
|
|
90
|
+
return this.ws;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** The current connection while it is open, null otherwise. */
|
|
94
|
+
openSocket(): WebSocket | null {
|
|
95
|
+
return this.ws?.readyState === WebSocket.OPEN ? this.ws : null;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** The next reconnect waits the shortest delay again. */
|
|
99
|
+
resetBackoff(): void {
|
|
100
|
+
this.reconnectDelayMs = RECONNECT_FIRST_DELAY_MS;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** Sends one frame on the open connection; false when there is none or the send fails. */
|
|
104
|
+
sendFrame(payload: Record<string, unknown>): boolean {
|
|
105
|
+
if (this.ws?.readyState !== WebSocket.OPEN) return false;
|
|
106
|
+
try {
|
|
107
|
+
this.ws.send(JSON.stringify(payload));
|
|
108
|
+
return true;
|
|
109
|
+
} catch (error) {
|
|
110
|
+
logFailure("cloud-socket", "WebSocket frame send failed")(error);
|
|
111
|
+
return false;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** Sends one frame on the open connection; nothing happens when there is none. */
|
|
116
|
+
send(payload: Record<string, unknown>): void {
|
|
117
|
+
if (this.ws?.readyState !== WebSocket.OPEN) return;
|
|
118
|
+
if (LOGGED_FRAME_TYPES.has(payload.type)) {
|
|
119
|
+
cloudApiLog.info("send websocket message", previewLongLists(payload));
|
|
120
|
+
}
|
|
121
|
+
this.ws.send(JSON.stringify(payload));
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** Opens a connection when the owner wants one and none is open, opening, or waiting to reconnect. */
|
|
125
|
+
ensureOpen(): void {
|
|
126
|
+
if (!this.owner.wanted() || this.ws || this.reconnectTimer) return;
|
|
127
|
+
|
|
128
|
+
const socketBaseUrl = this.getWebSocketBaseUrl();
|
|
129
|
+
if (!socketBaseUrl) {
|
|
130
|
+
this.failOpen(`Not a usable Gloom Cloud URL: ${this.session.getBaseUrl()}`);
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const socketToken = this.session.getSocketAuthToken();
|
|
135
|
+
const usingWebSocketToken = this.session.isUsingWebSocketToken();
|
|
136
|
+
const url = socketToken
|
|
137
|
+
? `${socketBaseUrl}/cloud/ws?token=${encodeURIComponent(socketToken)}`
|
|
138
|
+
: `${socketBaseUrl}/cloud/ws`;
|
|
139
|
+
cloudApiLog.info("open websocket", {
|
|
140
|
+
hasToken: !!socketToken,
|
|
141
|
+
tokenSource: usingWebSocketToken ? "websocket" : "session",
|
|
142
|
+
...this.owner.logCounts(),
|
|
143
|
+
});
|
|
144
|
+
this.health.reportSocketState(
|
|
145
|
+
GLOOM_CLOUD_SOCKET_CONNECTION_ID,
|
|
146
|
+
"connecting",
|
|
147
|
+
socketBaseUrl,
|
|
148
|
+
);
|
|
149
|
+
let ws: WebSocket;
|
|
150
|
+
try {
|
|
151
|
+
ws = new WebSocket(url);
|
|
152
|
+
} catch (error) {
|
|
153
|
+
this.failOpen(error instanceof Error ? error.message : String(error));
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
this.ws = ws;
|
|
157
|
+
let opened = false;
|
|
158
|
+
// A link that swallows the handshake never says so: the attempt is dropped and tried again.
|
|
159
|
+
this.connectTimer = setTimeout(() => {
|
|
160
|
+
this.connectTimer = null;
|
|
161
|
+
if (this.ws === ws) this.dropUnanswered(ws, "Connecting timed out");
|
|
162
|
+
}, SOCKET_CONNECT_TIMEOUT_MS);
|
|
163
|
+
|
|
164
|
+
ws.onopen = () => {
|
|
165
|
+
if (this.ws !== ws) return;
|
|
166
|
+
opened = true;
|
|
167
|
+
this.watchSilence();
|
|
168
|
+
this.session.onServiceAnswered?.();
|
|
169
|
+
cloudApiLog.info("websocket open");
|
|
170
|
+
this.health.reportSocketState(
|
|
171
|
+
GLOOM_CLOUD_SOCKET_CONNECTION_ID,
|
|
172
|
+
"open",
|
|
173
|
+
socketBaseUrl,
|
|
174
|
+
);
|
|
175
|
+
this.reconnectDelayMs = RECONNECT_FIRST_DELAY_MS;
|
|
176
|
+
this.owner.opened();
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
ws.onmessage = (event) => {
|
|
180
|
+
if (this.ws !== ws) return;
|
|
181
|
+
this.lastFrameAt = Date.now();
|
|
182
|
+
this.pingedAt = null;
|
|
183
|
+
this.session.onServiceAnswered?.();
|
|
184
|
+
this.owner.received(String(event.data));
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
ws.onclose = (event) => {
|
|
188
|
+
const activeSocket = this.ws === ws;
|
|
189
|
+
if (activeSocket) {
|
|
190
|
+
this.ws = null;
|
|
191
|
+
this.stopWatching();
|
|
192
|
+
this.owner.interrupted();
|
|
193
|
+
}
|
|
194
|
+
const closeEvent = event as CloseEvent | undefined;
|
|
195
|
+
cloudApiLog.warn("websocket closed", {
|
|
196
|
+
...this.owner.logCounts(),
|
|
197
|
+
code: closeEvent?.code,
|
|
198
|
+
reason: closeEvent?.reason,
|
|
199
|
+
tokenSource: usingWebSocketToken ? "websocket" : "session",
|
|
200
|
+
});
|
|
201
|
+
if (!activeSocket) return;
|
|
202
|
+
this.owner.closed();
|
|
203
|
+
this.health.reportSocketState(
|
|
204
|
+
GLOOM_CLOUD_SOCKET_CONNECTION_ID,
|
|
205
|
+
"closed",
|
|
206
|
+
closeEvent?.reason ||
|
|
207
|
+
(closeEvent?.code ? `Closed (${closeEvent.code})` : "Socket closed"),
|
|
208
|
+
);
|
|
209
|
+
if (
|
|
210
|
+
usingWebSocketToken &&
|
|
211
|
+
this.session.clearWebSocketTokenForFallback()
|
|
212
|
+
) {
|
|
213
|
+
this.reconnectDelayMs = RECONNECT_FIRST_DELAY_MS;
|
|
214
|
+
cloudApiLog.warn(
|
|
215
|
+
"cleared websocket token after socket close; falling back to session token",
|
|
216
|
+
);
|
|
217
|
+
}
|
|
218
|
+
if (!this.owner.wanted()) return;
|
|
219
|
+
this.scheduleReconnect();
|
|
220
|
+
};
|
|
221
|
+
|
|
222
|
+
ws.onerror = () => {
|
|
223
|
+
if (this.ws !== ws) return;
|
|
224
|
+
// A socket that was open, cut by a server restart or a proxy, fires its
|
|
225
|
+
// error just before its close: the close reports it, so the stream gets
|
|
226
|
+
// the grace a clean close does. One that never opened is a failed
|
|
227
|
+
// connect, and one open but silent is dropped by the ping check.
|
|
228
|
+
if (opened) return;
|
|
229
|
+
this.health.reportSocketState(
|
|
230
|
+
GLOOM_CLOUD_SOCKET_CONNECTION_ID,
|
|
231
|
+
"error",
|
|
232
|
+
"WebSocket error",
|
|
233
|
+
);
|
|
234
|
+
// Reconnect is handled by onclose.
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/** Closes the connection from this side and cancels a reconnect still waiting. */
|
|
239
|
+
close(): void {
|
|
240
|
+
if (this.reconnectTimer) {
|
|
241
|
+
clearTimeout(this.reconnectTimer);
|
|
242
|
+
this.reconnectTimer = null;
|
|
243
|
+
}
|
|
244
|
+
this.stopWatching();
|
|
245
|
+
const ws = this.ws;
|
|
246
|
+
this.ws = null;
|
|
247
|
+
if (ws) {
|
|
248
|
+
this.owner.closed();
|
|
249
|
+
cloudApiLog.info("teardown websocket");
|
|
250
|
+
this.health.reportSocketState(
|
|
251
|
+
GLOOM_CLOUD_SOCKET_CONNECTION_ID,
|
|
252
|
+
"idle",
|
|
253
|
+
"Socket closed locally",
|
|
254
|
+
);
|
|
255
|
+
}
|
|
256
|
+
try {
|
|
257
|
+
ws?.close();
|
|
258
|
+
} catch {
|
|
259
|
+
// Intentional ignore: Closing an already closed socket needs no further cleanup.
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/** Opens a connection again after the backoff, unless one is already waiting or the owner no longer wants one. */
|
|
264
|
+
scheduleReconnect(): void {
|
|
265
|
+
if (this.reconnectTimer || !this.owner.wanted()) return;
|
|
266
|
+
// Jittered so clients dropped together by a server restart do not all
|
|
267
|
+
// come back in the same instant.
|
|
268
|
+
const delay = Math.round(this.reconnectDelayMs * (0.5 + Math.random()));
|
|
269
|
+
this.reconnectTimer = setTimeout(() => {
|
|
270
|
+
this.reconnectTimer = null;
|
|
271
|
+
this.reconnectDelayMs = Math.min(this.reconnectDelayMs * 2, RECONNECT_MAX_DELAY_MS);
|
|
272
|
+
this.ensureOpen();
|
|
273
|
+
}, delay);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* The ws(s) form of the API base URL, or null when the base is not an
|
|
278
|
+
* http(s) URL. The web build derives it from `location.origin`, which is the
|
|
279
|
+
* string "null" on an opaque page, and `null/api` must not become a socket URL.
|
|
280
|
+
*/
|
|
281
|
+
private getWebSocketBaseUrl(): string | null {
|
|
282
|
+
const baseUrl = this.session.getBaseUrl();
|
|
283
|
+
if (!/^https?:\/\//.test(baseUrl)) return null;
|
|
284
|
+
const wsProtocol = baseUrl.startsWith("https") ? "wss" : "ws";
|
|
285
|
+
return baseUrl.replace(/^https?/, wsProtocol);
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/** From the open: the connection's silence is checked until it closes. */
|
|
289
|
+
private watchSilence(): void {
|
|
290
|
+
this.stopWatching();
|
|
291
|
+
this.lastFrameAt = Date.now();
|
|
292
|
+
this.pingedAt = null;
|
|
293
|
+
this.silenceTimer = setInterval(() => this.checkSilence(), SOCKET_SILENCE_CHECK_MS);
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
private stopWatching(): void {
|
|
297
|
+
if (this.connectTimer) clearTimeout(this.connectTimer);
|
|
298
|
+
if (this.silenceTimer) clearInterval(this.silenceTimer);
|
|
299
|
+
this.connectTimer = null;
|
|
300
|
+
this.silenceTimer = null;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/**
|
|
304
|
+
* A connection that went quiet is pinged; one that leaves the ping
|
|
305
|
+
* unanswered for SOCKET_PING_ANSWER_MS is half open, and is dropped and
|
|
306
|
+
* opened again. Anything it sends answers the ping.
|
|
307
|
+
*/
|
|
308
|
+
private checkSilence(): void {
|
|
309
|
+
const ws = this.ws;
|
|
310
|
+
if (!ws || ws.readyState !== WebSocket.OPEN) return;
|
|
311
|
+
const now = Date.now();
|
|
312
|
+
if (this.pingedAt != null) {
|
|
313
|
+
if (now - this.pingedAt >= SOCKET_PING_ANSWER_MS) this.dropUnanswered(ws, "No answer from the stream");
|
|
314
|
+
return;
|
|
315
|
+
}
|
|
316
|
+
if (now - this.lastFrameAt >= SOCKET_QUIET_PING_MS) {
|
|
317
|
+
this.pingedAt = now;
|
|
318
|
+
this.sendFrame({ type: "ping" });
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/**
|
|
323
|
+
* Gives up on a connection that never opened or went silent: what it was
|
|
324
|
+
* carrying is reported as interrupted (the stream's health turns to error,
|
|
325
|
+
* which marks its prices stale), and a new one is opened with the backoff.
|
|
326
|
+
* Its close, if it ever comes, is for a socket no longer in use.
|
|
327
|
+
*/
|
|
328
|
+
private dropUnanswered(ws: WebSocket, reason: string): void {
|
|
329
|
+
if (this.ws !== ws) return;
|
|
330
|
+
this.ws = null;
|
|
331
|
+
this.stopWatching();
|
|
332
|
+
cloudApiLog.warn("websocket unanswered; reconnecting", { reason, quoteTargets: this.owner.logCounts().quoteTargets });
|
|
333
|
+
try {
|
|
334
|
+
ws.close();
|
|
335
|
+
} catch {
|
|
336
|
+
// Intentional ignore: a socket that cannot close is dropped all the same.
|
|
337
|
+
}
|
|
338
|
+
this.owner.interrupted();
|
|
339
|
+
this.owner.closed();
|
|
340
|
+
this.health.reportSocketState(GLOOM_CLOUD_SOCKET_CONNECTION_ID, "error", reason);
|
|
341
|
+
this.scheduleReconnect();
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
/**
|
|
345
|
+
* A socket that could not be constructed is an error to show, not to throw:
|
|
346
|
+
* ensureOpen runs from effects and timers nobody awaits. The reconnect
|
|
347
|
+
* backoff retries it, so a URL that never works settles at the slowest delay.
|
|
348
|
+
*/
|
|
349
|
+
private failOpen(reason: string): void {
|
|
350
|
+
// The reason can carry the socket URL with its token, so only the health detail keeps it.
|
|
351
|
+
cloudApiLog.warn("websocket could not be opened");
|
|
352
|
+
this.health.reportSocketState(GLOOM_CLOUD_SOCKET_CONNECTION_ID, "error", reason);
|
|
353
|
+
this.scheduleReconnect();
|
|
354
|
+
}
|
|
355
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { ListenerSet } from "./socket-listeners";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Protocol features this client can read. A server lists the ones it offers
|
|
5
|
+
* in "ready"; the client opts into the shared ones, so an older server never
|
|
6
|
+
* sees a request it does not understand.
|
|
7
|
+
*/
|
|
8
|
+
const MARKET_BATCH_FEATURE = "market.batch";
|
|
9
|
+
/** Chat frames carry `attachments`; without it the server writes images into the text. */
|
|
10
|
+
export const CHAT_ATTACHMENTS_FEATURE = "chat.attachments";
|
|
11
|
+
/** The app can run terminal tools relayed from a remote assistant; it still announces itself first. */
|
|
12
|
+
export const TERMINAL_RELAY_FEATURE = "terminal.relay";
|
|
13
|
+
const CLIENT_SOCKET_FEATURES = [MARKET_BATCH_FEATURE, CHAT_ATTACHMENTS_FEATURE, TERMINAL_RELAY_FEATURE] as const;
|
|
14
|
+
/**
|
|
15
|
+
* The server streams perpetual listings (`BTC:HYPERLIQUID`) from their venues. Until a server offers it, they are
|
|
16
|
+
* kept off market.subscribe: an older one would read `BTC` on HYPERLIQUID as a stock symbol.
|
|
17
|
+
*/
|
|
18
|
+
const MARKET_PERPS_FEATURE = "market.perps";
|
|
19
|
+
|
|
20
|
+
export type SocketFeatureHooks = {
|
|
21
|
+
/** Sends a frame on the open connection; nothing happens when there is none. */
|
|
22
|
+
send: (payload: Record<string, unknown>) => void;
|
|
23
|
+
/** The connection whose server offers perpetual markets, or null when the last hello did not offer them. */
|
|
24
|
+
perpsOffered: (socket: WebSocket | null) => void;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
/** What the server offers in its "ready" frames, and the features this client opts into on each connection. */
|
|
28
|
+
export class SocketFeatures {
|
|
29
|
+
/** The connection that already opted into server features; a reconnect negotiates again. */
|
|
30
|
+
private requestedFor: WebSocket | null = null;
|
|
31
|
+
/** What the last "ready" frame offered; kept across reconnects so a blip does not hide features. */
|
|
32
|
+
private offered: ReadonlySet<string> = new Set();
|
|
33
|
+
private readonly listeners = new ListenerSet();
|
|
34
|
+
|
|
35
|
+
constructor(private readonly hooks: SocketFeatureHooks) {}
|
|
36
|
+
|
|
37
|
+
/** Whether the server's last "ready" frame offered this protocol feature. */
|
|
38
|
+
offers(feature: string): boolean {
|
|
39
|
+
return this.offered.has(feature);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Called whenever what the server offers changes. */
|
|
43
|
+
subscribe(listener: () => void): () => void {
|
|
44
|
+
return this.listeners.add(listener);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
clearListeners(): void {
|
|
48
|
+
this.listeners.clear();
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Reads a "ready" frame's offer on `socket`, the current connection, and opts into the shared features once per connection. */
|
|
52
|
+
negotiate(socket: WebSocket | null, offered: unknown): void {
|
|
53
|
+
this.record(socket, offered);
|
|
54
|
+
if (!Array.isArray(offered) || this.requestedFor === socket) return;
|
|
55
|
+
const features = CLIENT_SOCKET_FEATURES.filter((feature) => offered.includes(feature));
|
|
56
|
+
if (features.length === 0) return;
|
|
57
|
+
this.requestedFor = socket;
|
|
58
|
+
this.hooks.send({ type: "client.features", features });
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
private record(socket: WebSocket | null, offered: unknown): void {
|
|
62
|
+
const next = new Set(
|
|
63
|
+
Array.isArray(offered)
|
|
64
|
+
? offered.filter((feature): feature is string => typeof feature === "string")
|
|
65
|
+
: [],
|
|
66
|
+
);
|
|
67
|
+
const changed = next.size !== this.offered.size
|
|
68
|
+
|| [...next].some((feature) => !this.offered.has(feature));
|
|
69
|
+
this.offered = next;
|
|
70
|
+
// Perpetual listings held back since the socket opened go out once this connection's server offers them.
|
|
71
|
+
this.hooks.perpsOffered(next.has(MARKET_PERPS_FEATURE) ? socket : null);
|
|
72
|
+
if (!changed) return;
|
|
73
|
+
this.listeners.emit();
|
|
74
|
+
}
|
|
75
|
+
}
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import { logFailure } from "../utils/log-failure";
|
|
2
|
+
import { recordServerClockSample } from "../market-data/quotes/clock";
|
|
3
|
+
import type {
|
|
4
|
+
AuthUser,
|
|
5
|
+
ChatMessage,
|
|
6
|
+
ChatNotification,
|
|
7
|
+
ChatReactionsUpdate,
|
|
8
|
+
ScannerKind,
|
|
9
|
+
TeamNotification,
|
|
10
|
+
} from "./types";
|
|
11
|
+
import {
|
|
12
|
+
normalizeChatChannelStateFrame,
|
|
13
|
+
normalizeChatMessage,
|
|
14
|
+
normalizeChatNotification,
|
|
15
|
+
normalizeChatReactions,
|
|
16
|
+
normalizeTeamNotification,
|
|
17
|
+
} from "./normalizers";
|
|
18
|
+
import type { SocketListeners } from "./socket-listeners";
|
|
19
|
+
|
|
20
|
+
const SCANNER_MESSAGE_KINDS: Record<string, ScannerKind> = {
|
|
21
|
+
"scanner.hilo": "hilo",
|
|
22
|
+
"scanner.flow": "flow",
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
/** The server's hello on a connection. */
|
|
26
|
+
export type SocketReadyFrame = {
|
|
27
|
+
features?: unknown;
|
|
28
|
+
user?: Partial<AuthUser> | null;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
/** Where a server frame goes: its listeners, or the socket itself for the frames that change the session. */
|
|
32
|
+
export type SocketFrameRoutes = {
|
|
33
|
+
listeners: SocketListeners;
|
|
34
|
+
/** "ready", once the server clock reading it carries is taken. */
|
|
35
|
+
ready: (frame: SocketReadyFrame) => void;
|
|
36
|
+
/** "auth.unverified": the server holds the session's user unverified. */
|
|
37
|
+
unverified: () => void;
|
|
38
|
+
/** One quote: a market.quote frame, or one item of a market.quotes batch. */
|
|
39
|
+
quote: (frame: unknown) => void;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Reads one server frame and hands it on, in one synchronous pass. A frame that does not parse is logged; a frame
|
|
44
|
+
* of a type nobody listens for is dropped.
|
|
45
|
+
*/
|
|
46
|
+
export function dispatchSocketFrame(raw: string, routes: SocketFrameRoutes): void {
|
|
47
|
+
let parsed: any;
|
|
48
|
+
try {
|
|
49
|
+
parsed = JSON.parse(raw);
|
|
50
|
+
} catch (error) {
|
|
51
|
+
logFailure("cloud-socket", "WebSocket message decoding failed")(error);
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
const { listeners } = routes;
|
|
55
|
+
|
|
56
|
+
if (parsed?.type === "ready") {
|
|
57
|
+
recordServerClockSample(parsed.serverTime);
|
|
58
|
+
routes.ready(parsed);
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (parsed?.type === "auth.unverified") {
|
|
63
|
+
routes.unverified();
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
if (
|
|
68
|
+
parsed?.type === "chat.message" &&
|
|
69
|
+
typeof parsed.channelId === "string" &&
|
|
70
|
+
parsed.data
|
|
71
|
+
) {
|
|
72
|
+
listeners.chatChannels.emit(parsed.channelId, normalizeChatMessage(parsed.data as ChatMessage));
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (
|
|
77
|
+
parsed?.type === "chat.reactions" &&
|
|
78
|
+
typeof parsed.channelId === "string" &&
|
|
79
|
+
typeof parsed.data?.messageId === "string"
|
|
80
|
+
) {
|
|
81
|
+
const update: ChatReactionsUpdate = {
|
|
82
|
+
channelId: parsed.channelId,
|
|
83
|
+
messageId: parsed.data.messageId,
|
|
84
|
+
reactions: normalizeChatReactions(parsed.data.reactions),
|
|
85
|
+
};
|
|
86
|
+
listeners.chatReactions.emit(update);
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if (parsed?.type === "chat.notification" && parsed.data) {
|
|
91
|
+
listeners.chatNotifications.emit(normalizeChatNotification(parsed.data as ChatNotification));
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (parsed?.type === "chat.mentions_changed") {
|
|
96
|
+
listeners.chatMentionCandidates.emit();
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
if (parsed?.type === "chat.channel_state") {
|
|
100
|
+
const state = normalizeChatChannelStateFrame(parsed.data);
|
|
101
|
+
if (state) listeners.chatChannelState.emit(state);
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
if (parsed?.type === "team.notification" && parsed.data) {
|
|
106
|
+
listeners.teamNotifications.emit(normalizeTeamNotification(parsed.data as TeamNotification));
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
if (
|
|
111
|
+
parsed?.type === "chat.presence" &&
|
|
112
|
+
typeof parsed.onlineCount === "number"
|
|
113
|
+
) {
|
|
114
|
+
listeners.chatPresence.emit(parsed.onlineCount);
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
if (parsed?.type === "tape.snapshot" && parsed.data && typeof parsed.data.symbol === "string" && typeof parsed.data.exchange === "string") {
|
|
119
|
+
listeners.tape.emit(parsed.data.symbol, parsed.data.exchange, { type: "data", payload: parsed.data });
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const scannerKind =
|
|
124
|
+
typeof parsed?.type === "string"
|
|
125
|
+
? SCANNER_MESSAGE_KINDS[parsed.type]
|
|
126
|
+
: undefined;
|
|
127
|
+
if (scannerKind) {
|
|
128
|
+
const { type: _type, ...payload } = parsed;
|
|
129
|
+
listeners.scanners.emit(scannerKind, { type: "data", payload });
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
if (parsed?.type === "scanner.denied") {
|
|
134
|
+
const denied = SCANNER_MESSAGE_KINDS[`scanner.${parsed.scanner}`];
|
|
135
|
+
if (denied) {
|
|
136
|
+
listeners.scanners.emit(denied, {
|
|
137
|
+
type: "denied",
|
|
138
|
+
reason:
|
|
139
|
+
typeof parsed.reason === "string" ? parsed.reason : "pro_required",
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
if (parsed?.type === "market.quote") {
|
|
146
|
+
routes.quote(parsed);
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
if (parsed?.type === "market.quotes") {
|
|
151
|
+
recordServerClockSample(parsed.at);
|
|
152
|
+
if (!Array.isArray(parsed.quotes)) return;
|
|
153
|
+
// One synchronous pass: every item lands before the next data frame.
|
|
154
|
+
for (const item of parsed.quotes) routes.quote(item);
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
if (typeof parsed?.type === "string") {
|
|
159
|
+
listeners.cloudEvents.emit(parsed.type, parsed.data);
|
|
160
|
+
}
|
|
161
|
+
}
|